Skip to content

Commit

Permalink
compilers: make pie args explicitly enabled or disabled
Browse files Browse the repository at this point in the history
Currently meson supports explicitly turning pie args on, but relies on
the default behavior being no-pie. This isn't always the case, and we
should explicitly add the correct no pie arguments.

Fixes mesonbuild#4651
  • Loading branch information
dcbaker committed Jan 6, 2020
1 parent 7ec3af9 commit cbd5a7f
Show file tree
Hide file tree
Showing 9 changed files with 50 additions and 30 deletions.
4 changes: 2 additions & 2 deletions mesonbuild/backend/backends.py
Original file line number Diff line number Diff line change
Expand Up @@ -609,8 +609,8 @@ def generate_basic_compiler_args(self, target, compiler, no_warn_args=False):
# Set -fPIC for static libraries by default unless explicitly disabled
if isinstance(target, build.StaticLibrary) and target.pic:
commands += compiler.get_pic_args()
if isinstance(target, build.Executable) and target.pie:
commands += compiler.get_pie_args()
if isinstance(target, build.Executable):
commands += compiler.get_pie_args(target.pie)
# Add compile args needed to find external dependencies. Link args are
# added while generating the link command.
# NOTE: We must preserve the order in which external deps are
Expand Down
3 changes: 1 addition & 2 deletions mesonbuild/backend/ninjabackend.py
Original file line number Diff line number Diff line change
Expand Up @@ -2329,8 +2329,7 @@ def get_target_type_link_args(self, target, linker):
# If implib, and that's significant on this platform (i.e. Windows using either GCC or Visual Studio)
if target.import_filename:
commands += linker.gen_import_library_args(os.path.join(self.get_target_dir(target), target.import_filename))
if target.pie:
commands += linker.get_pie_link_args()
commands += linker.get_pie_link_args(target.pie)
elif isinstance(target, build.SharedLibrary):
if isinstance(target, build.SharedModule):
options = self.environment.coredata.base_options
Expand Down
14 changes: 9 additions & 5 deletions mesonbuild/compilers/compilers.py
Original file line number Diff line number Diff line change
Expand Up @@ -1029,12 +1029,16 @@ def get_pic_args(self):
m = 'Language {} does not support position-independent code'
raise EnvironmentException(m.format(self.get_display_language()))

def get_pie_args(self):
m = 'Language {} does not support position-independent executable'
raise EnvironmentException(m.format(self.get_display_language()))
def get_pie_args(self, enabled: bool) -> typing.List[str]:
"""Get arguments for pie.
If enabled is true then the arguments will enable pie explicitly, if
it's false they'll disable it.
"""
return []

def get_pie_link_args(self) -> typing.List[str]:
return self.linker.get_pie_args()
def get_pie_link_args(self, enabled: bool) -> typing.List[str]:
return self.linker.get_pie_args(enabled)

def get_argument_syntax(self):
"""Returns the argument family type.
Expand Down
6 changes: 6 additions & 0 deletions mesonbuild/compilers/fortran.py
Original file line number Diff line number Diff line change
Expand Up @@ -208,6 +208,12 @@ def get_module_outdir_args(self, path):
def language_stdlib_only_link_flags(self):
return ['-lgfortran', '-lm']

def get_pie_link_args(self, enabled: bool) -> List[str]:
# On macOS gfortran only has a -pie argument, not -no-pie or -no_pie
if not enabled and self.info.is_darwin():
return []
return super().get_pie_link_args(enabled)

class ElbrusFortranCompiler(GnuFortranCompiler, ElbrusCompiler):
def __init__(self, exelist, version, for_machine: MachineChoice,
is_cross, info: 'MachineInfo', exe_wrapper=None,
Expand Down
13 changes: 11 additions & 2 deletions mesonbuild/compilers/mixins/gnu.py
Original file line number Diff line number Diff line change
Expand Up @@ -154,8 +154,17 @@ def get_pic_args(self) -> typing.List[str]:
return [] # On Window and OS X, pic is always on.
return ['-fPIC']

def get_pie_args(self) -> typing.List[str]:
return ['-fPIE']
def get_pie_args(self, enabled: bool) -> typing.List[str]:
if enabled:
return ['-fPIE']
return []

def get_pie_link_args(self, enabled: bool) -> typing.List[str]:
# GCC-esque compilers like clang take -no-pie, even on macos, where the
# linker takes -no_pie instead.
if enabled:
return ['-pie']
return ['-no-pie']

def get_buildtype_args(self, buildtype: str) -> typing.List[str]:
return gnulike_buildtype_args[buildtype]
Expand Down
5 changes: 5 additions & 0 deletions mesonbuild/compilers/mixins/intel.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,11 @@ class IntelGnuLikeCompiler(GnuLikeCompiler):
debugoptimized: -g -O2
release: -O3
minsize: -O2
The Linux help claims that icc only supports -pie and -no_pie, the macOS
documentation claims that it only supports -pie and -no-pie. As far as I
can tell both work in both places, so we inherit the GnuLikeCompiler
implementation.
"""

BUILD_ARGS = {
Expand Down
6 changes: 3 additions & 3 deletions mesonbuild/compilers/mixins/islinker.py
Original file line number Diff line number Diff line change
Expand Up @@ -94,9 +94,9 @@ def get_allow_undefined_link_args(self) -> typing.List[str]:
raise mesonlib.EnvironmentException(
'Linker {} does not support allow undefined'.format(self.id))

def get_pie_link_args(self) -> typing.List[str]:
m = 'Linker {} does not support position-independent executable'
raise mesonlib.EnvironmentException(m.format(self.id))
def get_pie_link_args(self, enabled: bool) -> typing.List[str]:
"""Get linker pie arguments."""
return []

def get_undefined_link_args(self) -> typing.List[str]:
return []
Expand Down
6 changes: 0 additions & 6 deletions mesonbuild/compilers/vala.py
Original file line number Diff line number Diff line change
Expand Up @@ -56,12 +56,6 @@ def get_compile_only_args(self):
def get_pic_args(self):
return []

def get_pie_args(self):
return []

def get_pie_link_args(self):
return []

def get_always_args(self):
return ['-C']

Expand Down
23 changes: 13 additions & 10 deletions mesonbuild/linkers.py
Original file line number Diff line number Diff line change
Expand Up @@ -315,12 +315,11 @@ def get_std_shared_lib_args(self) -> typing.List[str]:
def get_std_shared_module_args(self, options: 'OptionDictType') -> typing.List[str]:
return self.get_std_shared_lib_args()

def get_pie_args(self) -> typing.List[str]:
# TODO: this really needs to take a boolean and return the args to
# disable pie, otherwise it only acts to enable pie if pie *isn't* the
# default.
m = 'Linker {} does not support position-independent executable'
raise mesonlib.EnvironmentException(m.format(self.id))
def get_pie_args(self, enabled: bool) -> typing.List[str]:
if enabled:
m = 'Linker {} does not support position-independent executable'
raise mesonlib.EnvironmentException(m.format(self.id))
return []

def get_lto_args(self) -> typing.List[str]:
return []
Expand Down Expand Up @@ -442,8 +441,10 @@ def get_buildtype_args(self, buildtype: str) -> typing.List[str]:
# _BUILDTYPE_ARGS value.
return mesonlib.listify([self._apply_prefix(a) for a in self._BUILDTYPE_ARGS[buildtype]])

def get_pie_args(self) -> typing.List[str]:
return ['-pie']
def get_pie_args(self, enabled: bool) -> typing.List[str]:
if enabled:
return ['-pie']
return ['-no-pie']

def get_asneeded_args(self) -> typing.List[str]:
return self._apply_prefix('--as-needed')
Expand Down Expand Up @@ -574,8 +575,10 @@ def get_allow_undefined_args(self) -> typing.List[str]:
def get_std_shared_module_args(self, options: 'OptionDictType') -> typing.List[str]:
return ['-bundle'] + self._apply_prefix('-undefined,dynamic_lookup')

def get_pie_args(self) -> typing.List[str]:
return ['-pie']
def get_pie_args(self, enabled: bool) -> typing.List[str]:
if enabled:
return ['-pie']
return ['-no_pie']

def get_link_whole_for(self, args: typing.List[str]) -> typing.List[str]:
result = [] # type: typing.List[str]
Expand Down

0 comments on commit cbd5a7f

Please sign in to comment.