From 2a378e99b54c06c6cc19d80d16961154031ff0bf Mon Sep 17 00:00:00 2001 From: Mike Hommey Date: Fri, 26 Sep 2014 07:33:56 +0900 Subject: [PATCH] Bug 857628 - Add a --enable-pie configure option to build target programs as position independent executables when supported by gcc/clang. r=mshal --- build/autoconf/compiler-opts.m4 | 28 ++++++++++++++++++++++++++++ config/rules.mk | 10 ++++++---- 2 files changed, 34 insertions(+), 4 deletions(-) diff --git a/build/autoconf/compiler-opts.m4 b/build/autoconf/compiler-opts.m4 index 3322c639ce3..98ecf1e11e4 100644 --- a/build/autoconf/compiler-opts.m4 +++ b/build/autoconf/compiler-opts.m4 @@ -326,6 +326,34 @@ if test "$GNU_CC" -a "$GCC_USE_GNU_LD" -a -z "$DEVELOPER_OPTIONS"; then DSO_LDOPTS="$DSO_LDOPTS -Wl,--gc-sections" fi fi + +# bionic in Android < 4.1 doesn't support PIE +# On OSX, the linker defaults to building PIE programs when targetting OSX 10.7+, +# but not when targetting OSX < 10.7. OSX < 10.7 doesn't support running PIE +# programs, so as long as support for OSX 10.6 is kept, we can't build PIE. +# Even after dropping 10.6 support, MOZ_PIE would not be useful since it's the +# default (and clang says the -pie option is not used). +# On other Unix systems, some file managers (Nautilus) can't start PIE programs +MOZ_PIE= + +MOZ_ARG_ENABLE_BOOL(pie, +[ --enable-pie Enable Position Independent Executables], + MOZ_PIE=1, + MOZ_PIE= ) + +if test "$GNU_CC" -a -n "$MOZ_PIE"; then + AC_MSG_CHECKING([for PIE support]) + _SAVE_LDFLAGS=$LDFLAGS + LDFLAGS="$LDFLAGS -pie" + AC_TRY_LINK(,,AC_MSG_RESULT([yes]) + [MOZ_PROGRAM_LDFLAGS="$MOZ_PROGRAM_LDFLAGS -pie"], + AC_MSG_RESULT([no]) + AC_MSG_ERROR([--enable-pie requires PIE support from the linker.])) + LDFLAGS=$_SAVE_LDFLAGS +fi + +AC_SUBST(MOZ_PROGRAM_LDFLAGS) + ]) dnl GCC and clang will fail if given an unknown warning option like -Wfoobar. diff --git a/config/rules.mk b/config/rules.mk index 33385626e94..3ce9c5c15f0 100644 --- a/config/rules.mk +++ b/config/rules.mk @@ -641,6 +641,8 @@ endif endif # NO_PROFILE_GUIDED_OPTIMIZE +MOZ_PROGRAM_LDFLAGS += $(MOZ_GLUE_PROGRAM_LDFLAGS) + ############################################## checkout: @@ -673,7 +675,7 @@ $(PROGRAM): $(PROGOBJS) $(STATIC_LIBS_DEPS) $(EXTRA_DEPS) $(EXE_DEF_FILE) $(RESF $(REPORT_BUILD) @$(RM) $@.manifest ifeq (_WINNT,$(GNU_CC)_$(OS_ARCH)) - $(EXPAND_LD) -NOLOGO -OUT:$@ -PDB:$(LINK_PDBFILE) $(WIN32_EXE_LDFLAGS) $(LDFLAGS) $(MOZ_GLUE_PROGRAM_LDFLAGS) $(PROGOBJS) $(RESFILE) $(STATIC_LIBS) $(SHARED_LIBS) $(EXTRA_LIBS) $(OS_LIBS) + $(EXPAND_LD) -NOLOGO -OUT:$@ -PDB:$(LINK_PDBFILE) $(WIN32_EXE_LDFLAGS) $(LDFLAGS) $(MOZ_PROGRAM_LDFLAGS) $(PROGOBJS) $(RESFILE) $(STATIC_LIBS) $(SHARED_LIBS) $(EXTRA_LIBS) $(OS_LIBS) ifdef MSMANIFEST_TOOL @if test -f $@.manifest; then \ if test -f '$(srcdir)/$@.manifest'; then \ @@ -694,7 +696,7 @@ ifdef MOZ_PROFILE_GENERATE touch -t `date +%Y%m%d%H%M.%S -d 'now+5seconds'` pgo.relink endif else # !WINNT || GNU_CC - $(EXPAND_CCC) -o $@ $(CXXFLAGS) $(PROGOBJS) $(RESFILE) $(WIN32_EXE_LDFLAGS) $(LDFLAGS) $(WRAP_LDFLAGS) $(STATIC_LIBS) $(MOZ_GLUE_PROGRAM_LDFLAGS) $(SHARED_LIBS) $(EXTRA_LIBS) $(OS_LIBS) $(BIN_FLAGS) $(EXE_DEF_FILE) $(STLPORT_LIBS) + $(EXPAND_CCC) -o $@ $(CXXFLAGS) $(PROGOBJS) $(RESFILE) $(WIN32_EXE_LDFLAGS) $(LDFLAGS) $(WRAP_LDFLAGS) $(STATIC_LIBS) $(MOZ_PROGRAM_LDFLAGS) $(SHARED_LIBS) $(EXTRA_LIBS) $(OS_LIBS) $(BIN_FLAGS) $(EXE_DEF_FILE) $(STLPORT_LIBS) $(call CHECK_BINARY,$@) endif # WINNT && !GNU_CC @@ -742,7 +744,7 @@ endif $(SIMPLE_PROGRAMS): %$(BIN_SUFFIX): %.$(OBJ_SUFFIX) $(STATIC_LIBS_DEPS) $(EXTRA_DEPS) $(GLOBAL_DEPS) $(REPORT_BUILD) ifeq (_WINNT,$(GNU_CC)_$(OS_ARCH)) - $(EXPAND_LD) -nologo -out:$@ -pdb:$(LINK_PDBFILE) $< $(WIN32_EXE_LDFLAGS) $(LDFLAGS) $(MOZ_GLUE_PROGRAM_LDFLAGS) $(STATIC_LIBS) $(SHARED_LIBS) $(EXTRA_LIBS) $(OS_LIBS) + $(EXPAND_LD) -nologo -out:$@ -pdb:$(LINK_PDBFILE) $< $(WIN32_EXE_LDFLAGS) $(LDFLAGS) $(MOZ_PROGRAM_LDFLAGS) $(STATIC_LIBS) $(SHARED_LIBS) $(EXTRA_LIBS) $(OS_LIBS) ifdef MSMANIFEST_TOOL @if test -f $@.manifest; then \ mt.exe -NOLOGO -MANIFEST $@.manifest -OUTPUTRESOURCE:$@\;1; \ @@ -750,7 +752,7 @@ ifdef MSMANIFEST_TOOL fi endif # MSVC with manifest tool else - $(EXPAND_CCC) $(CXXFLAGS) -o $@ $< $(WIN32_EXE_LDFLAGS) $(LDFLAGS) $(WRAP_LDFLAGS) $(STATIC_LIBS) $(MOZ_GLUE_PROGRAM_LDFLAGS) $(SHARED_LIBS) $(EXTRA_LIBS) $(OS_LIBS) $(BIN_FLAGS) $(STLPORT_LIBS) + $(EXPAND_CCC) $(CXXFLAGS) -o $@ $< $(WIN32_EXE_LDFLAGS) $(LDFLAGS) $(WRAP_LDFLAGS) $(STATIC_LIBS) $(MOZ_PROGRAM_LDFLAGS) $(SHARED_LIBS) $(EXTRA_LIBS) $(OS_LIBS) $(BIN_FLAGS) $(STLPORT_LIBS) $(call CHECK_BINARY,$@) endif # WINNT && !GNU_CC