]> git.ipfire.org Git - thirdparty/elfutils.git/commitdiff
Check for -z,defs, -z,relro, -fPIC, -fPIE before using them
authorUlf Hermann <ulf.hermann@qt.io>
Fri, 18 Aug 2017 10:41:11 +0000 (12:41 +0200)
committerMark Wielaard <mark@klomp.org>
Fri, 18 Aug 2017 20:29:46 +0000 (22:29 +0200)
Those flags are not available on all platforms, and omitting them when
not available will not cause any harm. In particular:

-z,defs disallows undefined symbols in object files. This option is
unsupported if the target binary format enforces the same condition
already. Furthermore it is only a compile time sanity check. When it is
omitted, the same binary is produced.

-z,relro instructs the loader to mark sections read-only after loading
the library, where possible. This is a hardening mechanism. If it is
unavailable, the functionality of the code is not affected in any way.

-fPIC instructs the compiler to produce position independent code. While
this is preferable to relocatable code, relocatable code also works and
may even be faster. Relocatable code might just be loaded into memory
multiple times for different processes.

-fPIE is the same thing as -fPIC for executables rather than shared
libraries.

Signed-off-by: Ulf Hermann <ulf.hermann@qt.io>
20 files changed:
ChangeLog
backends/ChangeLog
backends/Makefile.am
config/ChangeLog
config/eu.am
configure.ac
lib/ChangeLog
lib/Makefile.am
libasm/ChangeLog
libasm/Makefile.am
libcpu/ChangeLog
libcpu/Makefile.am
libdw/ChangeLog
libdw/Makefile.am
libebl/ChangeLog
libebl/Makefile.am
libelf/ChangeLog
libelf/Makefile.am
tests/ChangeLog
tests/Makefile.am

index 9253c0a33f9f0dcc0f396a42e1ac49d2b0075f6a..621462270006d98a586a27e0fce42dcf4ed4fe1f 100644 (file)
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,8 @@
+2017-04-27  Ulf Hermann  <ulf.hermann@qt.io>
+
+       * configure.ac: Check if -fPIC, -fPIE, -Wl,-z,defs,
+       and -Wl,-z,relro are supported by the compiler.
+
 2017-08-02  Mark Wielaard  <mark@klomp.org>
 
        * configure.ac: Set version to 0.170.
index a66e923ee47ef8dc20909e52c0e97ff4dd8a31d8..79b50ebf555eb537c8088868828c35169d1f30e7 100644 (file)
@@ -1,3 +1,7 @@
+2017-04-27  Ulf Hermann <ulf.hermann@qt.io>
+
+       * Makefile.am: Use dso_LDFLAGS.
+
 2017-07-27  Mark Wielaard  <mark@klomp.org>
 
        * sparc_reloc.def: GOTDATA_OP_HIX22, GOTDATA_OP_LOX10 and
index 37dc2d20f545001a3cf6042313b1613b30b0bc6d..0fde0cb0a7cdaca1d4c03ba630dbf9ffad40e2fc 100644 (file)
@@ -129,10 +129,10 @@ libebl_%.so libebl_%.map: libebl_%_pic.a $(libelf) $(libdw) $(libeu)
        @rm -f $(@:.so=.map)
        $(AM_V_at)echo 'ELFUTILS_$(PACKAGE_VERSION) { global: $*_init; local: *; };' \
          > $(@:.so=.map)
-       $(AM_V_CCLD)$(LINK) -shared -o $(@:.map=.so) \
+       $(AM_V_CCLD)$(LINK) $(dso_LDFLAGS) -o $(@:.map=.so) \
                -Wl,--whole-archive $< $(cpu_$*) -Wl,--no-whole-archive \
                -Wl,--version-script,$(@:.so=.map),--no-undefined \
-               -Wl,-z,defs,-z,relro -Wl,--as-needed $(libelf) $(libdw) $(libeu)
+               -Wl,--as-needed $(libelf) $(libdw) $(libeu)
        @$(textrel_check)
 
 libebl_i386.so: $(cpu_i386)
index 02cf76f97466e83c4d7fd02ee9ee4f2c5b9190af..1ed3c4af28002d4865ce93f172a39704c71944c1 100644 (file)
@@ -1,3 +1,7 @@
+2017-04-27  Ulf Hermann  <ulf.hermann@qt.io>
+
+       * eu.am: Use fpic_CFLAGS.
+
 2016-08-02  Mark Wielaard  <mark@klomp.org>
 
        * elfutils.spec.in: Update for 0.170.
index 8fe1e259f9e234d9da046a620fe92be5d5703414..796f3883837f557a9e9b220c642d00a517320a58 100644 (file)
@@ -86,14 +86,14 @@ endif
 
 %.os: %.c %.o
 if AMDEP
-       $(AM_V_CC)if $(COMPILE.os) -c -o $@ -fPIC $(DEFS.os) -MT $@ -MD -MP \
+       $(AM_V_CC)if $(COMPILE.os) -c -o $@ $(fpic_CFLAGS) $(DEFS.os) -MT $@ -MD -MP \
          -MF "$(DEPDIR)/$*.Tpo" `test -f '$<' || echo '$(srcdir)/'`$<; \
        then cat "$(DEPDIR)/$*.Tpo" >> "$(DEPDIR)/$*.Po"; \
             rm -f "$(DEPDIR)/$*.Tpo"; \
        else rm -f "$(DEPDIR)/$*.Tpo"; exit 1; \
        fi
 else
-       $(AM_V_CC)$(COMPILE.os) -c -o $@ -fPIC $(DEFS.os) $<
+       $(AM_V_CC)$(COMPILE.os) -c -o $@ $(fpic_CFLAGS) $(DEFS.os) $<
 endif
 
 CLEANFILES = *.gcno *.gcda
index 1f1856df1a4cc1b76da0aba59b5f0e56cd06d3b5..e6e3b6751bd11a92b593f4387e9d584dde7d1882 100644 (file)
@@ -127,13 +127,65 @@ CFLAGS="$old_CFLAGS"])
 AS_IF([test "x$ac_cv_c99" != xyes],
       AC_MSG_ERROR([gcc with GNU99 support required]))
 
+AC_CACHE_CHECK([whether gcc supports -fPIC], ac_cv_fpic, [dnl
+save_CFLAGS="$CFLAGS"
+CFLAGS="$save_CFLAGS -fPIC -Werror"
+AC_COMPILE_IFELSE([AC_LANG_SOURCE()], ac_cv_fpic=yes, ac_cv_fpic=no)
+CFLAGS="$save_CFLAGS"
+])
+if test "$ac_cv_fpic" = "yes"; then
+       fpic_CFLAGS="-fPIC"
+else
+       fpic_CFLAGS=""
+fi
+AC_SUBST([fpic_CFLAGS])
+
+AC_CACHE_CHECK([whether gcc supports -fPIE], ac_cv_fpie, [dnl
+save_CFLAGS="$CFLAGS"
+CFLAGS="$save_CFLAGS -fPIE -Werror"
+AC_COMPILE_IFELSE([AC_LANG_SOURCE()], ac_cv_fpie=yes, ac_cv_fpie=no)
+CFLAGS="$save_CFLAGS"
+])
+if test "$ac_cv_fpie" = "yes"; then
+       fpie_CFLAGS="-fPIE"
+else
+       fpie_CFLAGS=""
+fi
+AC_SUBST([fpie_CFLAGS])
+
+dso_LDFLAGS="-shared"
+
+ZDEFS_LDFLAGS="-Wl,-z,defs"
+AC_CACHE_CHECK([whether gcc supports $ZDEFS_LDFLAGS], ac_cv_zdefs, [dnl
+save_LDFLAGS="$LDFLAGS"
+LDFLAGS="$ZDEFS_LDFLAGS $save_LDFLAGS"
+AC_LINK_IFELSE([AC_LANG_PROGRAM()], ac_cv_zdefs=yes, ac_cv_zdefs=no)
+LDFLAGS="$save_LDFLAGS"
+])
+if test "$ac_cv_zdefs" = "yes"; then
+       dso_LDFLAGS="$dso_LDFLAGS $ZDEFS_LDFLAGS"
+fi
+
+ZRELRO_LDFLAGS="-Wl,-z,relro"
+AC_CACHE_CHECK([whether gcc supports $ZRELRO_LDFLAGS], ac_cv_zrelro, [dnl
+save_LDFLAGS="$LDFLAGS"
+LDFLAGS="$ZRELRO_LDFLAGS $save_LDFLAGS"
+AC_LINK_IFELSE([AC_LANG_PROGRAM()], ac_cv_zrelro=yes, ac_cv_zrelro=no)
+LDFLAGS="$save_LDFLAGS"
+])
+if test "$ac_cv_zrelro" = "yes"; then
+       dso_LDFLAGS="$dso_LDFLAGS $ZRELRO_LDFLAGS"
+fi
+
+AC_SUBST([dso_LDFLAGS])
+
 AC_CACHE_CHECK([for __thread support], ac_cv_tls, [dnl
 # Use the same flags that we use for our DSOs, so the test is representative.
 # Some old compiler/linker/libc combinations fail some ways and not others.
 save_CFLAGS="$CFLAGS"
 save_LDFLAGS="$LDFLAGS"
-CFLAGS="-fPIC $CFLAGS"
-LDFLAGS="-shared -Wl,-z,defs,-z,relro $LDFLAGS"
+CFLAGS="$fpic_CFLAGS $CFLAGS"
+LDFLAGS="$dso_LDFLAGS $LDFLAGS"
 AC_LINK_IFELSE([dnl
 AC_LANG_PROGRAM([[#include <stdlib.h>
 #undef __thread
index 1f162286d9128925702863dadec5de387770eedc..67ef2792408e94142eeb2c91155889121fb85b74 100644 (file)
@@ -1,3 +1,7 @@
+2017-04-27  Ulf Hermann  <ulf.hermann@qt.io>
+
+       * Makefile.am: Use fpic_CFLAGS.
+
 2017-07-18  Mark Wielaard  <mark@klomp.org>
 
        * bpf.h: New file.
index ada2030d54029790a1726679ff855cb7a96cd1d7..c30d38bf66b7b3c412a03177d491721775d89551 100644 (file)
@@ -28,7 +28,7 @@
 ## not, see <http://www.gnu.org/licenses/>.
 ##
 include $(top_srcdir)/config/eu.am
-AM_CFLAGS += -fPIC
+AM_CFLAGS += $(fpic_CFLAGS)
 AM_CPPFLAGS += -I$(srcdir)/../libelf
 
 noinst_LIBRARIES = libeu.a
index 262d2a92248320a697c7bcb03342ed2087edb791..fffcced02329c8142979c830108022d0b7f1d84b 100644 (file)
@@ -1,3 +1,7 @@
+2017-02-27  Ulf Hermann  <ulf.hermann@qt.io>
+
+       * Makefile.am: Use dso_LDFLAGS.
+
 2017-02-17  Ulf Hermann  <ulf.hermann@qt.io>
 
        * Makefile.am: Add libasm_so_DEPS to specify external libraries
index 9effa6c50e091bb66f2d26ccbb6864683e34e755..19fef50883687928780bf75df7bee5993ab736c3 100644 (file)
@@ -64,8 +64,8 @@ endif
 libasm_so_LIBS = libasm_pic.a
 libasm_so_SOURCES =
 libasm.so$(EXEEXT): $(srcdir)/libasm.map $(libasm_so_LIBS) $(libasm_so_DEPS)
-       $(AM_V_CCLD)$(LINK) -shared -o $@ \
-               -Wl,--soname,$@.$(VERSION),-z,defs,-z,relro \
+       $(AM_V_CCLD)$(LINK) $(dso_LDFLAGS) -o $@ \
+               -Wl,--soname,$@.$(VERSION) \
                -Wl,--version-script,$<,--no-undefined \
                -Wl,--whole-archive $(libasm_so_LIBS) -Wl,--no-whole-archive \
                $(libasm_so_LDLIBS)
index 28b220fc56a35878980d543c8158701a02057d92..173defe60c3e69383d34365c47d2d95bdc1d5c18 100644 (file)
@@ -1,3 +1,7 @@
+2017-02-27  Ulf Hermann  <ulf.hermann@qt.io>
+
+       * Makefile.am: Use fpic_CFLAGS.
+
 2017-07-18  Mark Wielaard  <mark@klomp.org>
 
        * Makefile.am: Don't check HAVE_LINUX_BPF_H, just define libcpu_bpf.
index 94de56efb5dc73f2c6072ac308d875eaa621daaa..4c8778d14d917595e48e3323d403c8f7ff610574 100644 (file)
@@ -30,7 +30,7 @@
 include $(top_srcdir)/config/eu.am
 AM_CPPFLAGS += -I$(srcdir)/../libelf -I$(srcdir)/../libebl \
            -I$(srcdir)/../libdw -I$(srcdir)/../libasm
-AM_CFLAGS += -fPIC -fdollars-in-identifiers
+AM_CFLAGS += $(fpic_CFLAGS) -fdollars-in-identifiers
 LEXCOMPILE = $(LEX) $(LFLAGS) $(AM_LFLAGS) -P$(<F:lex.l=)
 LEX_OUTPUT_ROOT = lex.$(<F:lex.l=)
 AM_YFLAGS = -p$(<F:parse.y=)
index 6533eb5019d65e084dddd7c7cea8b1377e6417e7..67d7799d2a1c736994d680146478e507d2b5b025 100644 (file)
@@ -1,3 +1,7 @@
+2017-02-27  Ulf Hermann  <ulf.hermann@qt.io>
+
+       * Makefile.am: Use fpic_CFLAGS and dso_LDFLAGS.
+
 2017-07-26  Mark Wielaard  <mark@klomp.org>
 
        * dwarf.h: Add DW_MACRO_* and compat defines for DW_MACRO_GNU_*.
index ff8c291e021c8f010d9747dd07e6b2f1a545d3ec..8ee46802ac63c4a8d185513e0817d0fe30107ede 100644 (file)
@@ -29,7 +29,7 @@
 ##
 include $(top_srcdir)/config/eu.am
 if BUILD_STATIC
-AM_CFLAGS += -fPIC
+AM_CFLAGS += $(fpic_CFLAGS)
 endif
 AM_CPPFLAGS += -I$(srcdir)/../libelf
 VERSION = 1
@@ -110,8 +110,8 @@ libdw_so_SOURCES =
 libdw.so$(EXEEXT): $(srcdir)/libdw.map $(libdw_so_LIBS) $(libdw_so_DEPS)
 # The rpath is necessary for libebl because its $ORIGIN use will
 # not fly in a setuid executable that links in libdw.
-       $(AM_V_CCLD)$(LINK) -shared -o $@ \
-               -Wl,--soname,$@.$(VERSION),-z,defs,-z,relro \
+       $(AM_V_CCLD)$(LINK) $(dso_LDFLAGS) -o $@ \
+               -Wl,--soname,$@.$(VERSION) \
                -Wl,--enable-new-dtags,-rpath,$(pkglibdir) \
                -Wl,--version-script,$<,--no-undefined \
                -Wl,--whole-archive $(libdw_so_LIBS) -Wl,--no-whole-archive \
index 334bf224a201c6090646d3d3293275df0d6837aa..3b0763321e6216b251cbcb2e6061c82ca35c977c 100644 (file)
@@ -1,3 +1,7 @@
+2017-04-27  Ulf Hermann  <ulf.hermann@qt.io>
+
+       * Makefile.am: Use fpic_CFLAGS.
+
 2017-07-19  Gustavo Romero <gromero@linux.vnet.ibm.com>
 
        * eblcorenotetypename.c: Add ppc64 HTM SPRs note as known type.
index 2491df80903a1b828e960e6f1e99707e35fd7dbd..737de6b0aa6d39d54fdee4d6f0e33c725b5d425f 100644 (file)
@@ -28,7 +28,7 @@
 ## not, see <http://www.gnu.org/licenses/>.
 ##
 include $(top_srcdir)/config/eu.am
-AM_CFLAGS += -fPIC
+AM_CFLAGS += $(fpic_CFLAGS)
 AM_CPPFLAGS += -I$(srcdir)/../libelf -I$(srcdir)/../libdw -I$(srcdir)/../libasm
 VERSION = 1
 LIBEBL_SUBDIR = @LIBEBL_SUBDIR@
index 436b888028083aa2071d82f563026ac425b76f9a..a07364676d3617d29b59c1ecd79e86ce3781008a 100644 (file)
@@ -1,3 +1,7 @@
+2017-04-27  Ulf Hermann  <ulf.hermann@qt.io>
+
+       * Makefile.am: Use fpic_CFLAGS and dso_LDFLAGS.
+
 2017-08-15  Mark Wielaard  <mark@klomp.org>
 
        * elf.h: Update from glibc. Add new powerpc note descriptors.
index 88c1edd74314445b702a231c54355372ca624114..ddaeaa2b3bb2695291ffd975f8bf3bcd11abd5ab 100644 (file)
@@ -29,7 +29,7 @@
 ##
 include $(top_srcdir)/config/eu.am
 if BUILD_STATIC
-AM_CFLAGS += -fPIC
+AM_CFLAGS += $(fpic_CFLAGS)
 endif
 GCC_INCLUDE = -I$(shell $(CC) -print-file-name=include)
 VERSION = 1
@@ -104,8 +104,8 @@ endif
 libelf_so_LIBS = libelf_pic.a
 libelf_so_SOURCES =
 libelf.so$(EXEEXT): $(srcdir)/libelf.map $(libelf_so_LIBS) $(libelf_so_DEPS)
-       $(AM_V_CCLD)$(LINK) -shared -o $@ \
-               -Wl,--soname,$@.$(VERSION),-z,defs,-z,relro \
+       $(AM_V_CCLD)$(LINK) $(dso_LDFLAGS) -o $@ \
+               -Wl,--soname,$@.$(VERSION) \
                -Wl,--version-script,$<,--no-undefined \
                -Wl,--whole-archive $(libelf_so_LIBS) -Wl,--no-whole-archive \
                $(libelf_so_LDLIBS)
index 94f4cf23eb800bf36978334296dabf0777588f53..19a4c88dcf2bfa7e7fff1fe09ee049f56be3380d 100644 (file)
@@ -1,3 +1,7 @@
+2017-04-27  Ulf Hermann <ulf.hermann@qt.io>
+
+       * Makefile.am: Use fpie_CFLAGS and fpic_CFLAGS.
+
 2017-08-08  Dmitry V. Levin <ldv@altlinux.org>
 
        * run-strip-nothing.sh: Add -s.
index 3735084fed8b03a26e7b05c45e0a95e6b6a222bd..6332a7c106661bc786436fcc071a10d1056eaecd 100644 (file)
@@ -474,7 +474,7 @@ dwfl_report_elf_align_LDADD = $(libdw)
 varlocs_LDADD = $(libdw) $(libelf) $(argp_LDADD)
 backtrace_LDADD = $(libdw) $(libelf) $(argp_LDADD)
 # backtrace-child-biarch also uses those *_CFLAGS and *_LDLAGS variables:
-backtrace_child_CFLAGS = -fPIE
+backtrace_child_CFLAGS = $(fpie_CFLAGS)
 backtrace_child_LDFLAGS = -pie -pthread
 backtrace_child_biarch_SOURCES = backtrace-child.c
 backtrace_data_LDADD = $(libdw) $(libelf)
@@ -485,7 +485,7 @@ debugaltlink_LDADD = $(libdw) $(libelf)
 buildid_LDADD = $(libdw) $(libelf)
 deleted_LDADD = ./deleted-lib.so
 deleted_lib_so_LDFLAGS = -shared -rdynamic
-deleted_lib_so_CFLAGS = -fPIC -fasynchronous-unwind-tables
+deleted_lib_so_CFLAGS = $(fpic_CFLAGS) -fasynchronous-unwind-tables
 aggregate_size_LDADD = $(libdw) $(libelf) $(argp_LDADD)
 peel_type_LDADD = $(libdw) $(libelf) $(argp_LDADD)
 vdsosyms_LDADD = $(libdw) $(libelf)