[PATCH] Check for -z,defs, -z,relro, -fPIC, -fPIE before using them

Ulf Hermann ulf.hermann@qt.io
Thu Apr 27 17:51:00 GMT 2017


On windows those aren't needed because the link results are no ELF
files and all code is position independent anyway. gcc then complains
about them, which is in turn caught by -Werror.

Signed-off-by: Ulf Hermann <ulf.hermann@qt.io>
---
 ChangeLog            |  5 +++++
 backends/ChangeLog   |  4 ++++
 backends/Makefile.am |  4 ++--
 config/ChangeLog     |  4 ++++
 config/eu.am         |  4 ++--
 configure.ac         | 56 ++++++++++++++++++++++++++++++++++++++++++++++++++--
 lib/ChangeLog        |  4 ++++
 lib/Makefile.am      |  2 +-
 libasm/ChangeLog     |  4 ++++
 libasm/Makefile.am   |  2 +-
 libcpu/ChangeLog     |  4 ++++
 libcpu/Makefile.am   |  2 +-
 libdw/ChangeLog      |  4 ++++
 libdw/Makefile.am    |  4 ++--
 libebl/ChangeLog     |  4 ++++
 libebl/Makefile.am   |  2 +-
 libelf/ChangeLog     |  4 ++++
 libelf/Makefile.am   |  6 +++---
 libgnu/Makefile.am   |  2 +-
 tests/ChangeLog      |  4 ++++
 tests/Makefile.am    |  4 ++--
 21 files changed, 111 insertions(+), 18 deletions(-)

diff --git a/ChangeLog b/ChangeLog
index 6fe525c..36c3cc7 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,5 +1,10 @@
 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-04-27  Ulf Hermann  <ulf.hermann@qt.io>
+
 	* configure.ac: Drop checks for memrchr, rawmemchr, mempcpy, argp.
 
 2017-04-21  Ulf Hermann  <ulf.hermann@qt.io>
diff --git a/backends/ChangeLog b/backends/ChangeLog
index 594aa98..baeb7b9 100644
--- a/backends/ChangeLog
+++ b/backends/ChangeLog
@@ -1,3 +1,7 @@
+2017-04-27  Ulf Hermann <ulf.hermann@qt.io>
+
+	* Makefile.am: Use dso_LDFLAGS.
+
 2017-04-21  Ulf Hermann <ulf.hermann@qt.io>
 
 	* Makefile.am: Link backends against libgnu.a if requested.
diff --git a/backends/Makefile.am b/backends/Makefile.am
index 5dcb3e1..3e1992e 100644
--- a/backends/Makefile.am
+++ b/backends/Makefile.am
@@ -138,10 +138,10 @@ libebl_%.so libebl_%.map: libebl_%_pic.a $(libelf) $(libdw) $(libgnu)
 	@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) \
-		-Wl,-z,defs -Wl,--as-needed $(libelf) $(libdw) $(libgnu)
+		-Wl,--as-needed $(libelf) $(libdw) $(libgnu)
 	@$(textrel_check)
 
 libebl_i386.so: $(cpu_i386)
diff --git a/config/ChangeLog b/config/ChangeLog
index 756bab6..59569e3 100644
--- a/config/ChangeLog
+++ b/config/ChangeLog
@@ -1,3 +1,7 @@
+2017-04-27  Ulf Hermann  <ulf.hermann@qt.io>
+
+	* eu.am: Use fpic_CFLAGS.
+
 2017-04-21  Ulf Hermann  <ulf.hermann@qt.io>
 
 	* eu.am: Add $(top_srcdir)libgnu and $(top_builddir)/libgnu to -I if requested.
diff --git a/config/eu.am b/config/eu.am
index 11c2fec..bc8c767 100644
--- a/config/eu.am
+++ b/config/eu.am
@@ -89,14 +89,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
diff --git a/configure.ac b/configure.ac
index a48b13e..107762f 100644
--- a/configure.ac
+++ b/configure.ac
@@ -136,13 +136,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
diff --git a/lib/ChangeLog b/lib/ChangeLog
index 82c009e..605b9b9 100644
--- a/lib/ChangeLog
+++ b/lib/ChangeLog
@@ -1,5 +1,9 @@
 2017-04-27  Ulf Hermann  <ulf.hermann@qt.io>
 
+	* Makefile.am: Use fpic_CFLAGS.
+
+2017-04-27  Ulf Hermann  <ulf.hermann@qt.io>
+
 	* system.h: Drop mempcpy replacement.
 	* xstrndup.c: Don't include system.h.
 
diff --git a/lib/Makefile.am b/lib/Makefile.am
index 7a65eb9..17d16d0 100644
--- a/lib/Makefile.am
+++ b/lib/Makefile.am
@@ -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
diff --git a/libasm/ChangeLog b/libasm/ChangeLog
index 80aa7de..971492a 100644
--- a/libasm/ChangeLog
+++ b/libasm/ChangeLog
@@ -1,5 +1,9 @@
 2017-02-27  Ulf Hermann  <ulf.hermann@qt.io>
 
+	* Makefile.am: Use dso_LDFLAGS.
+
+2017-02-27  Ulf Hermann  <ulf.hermann@qt.io>
+
 	* disasm_str.c: Don't include system.h
 
 2017-02-21  Ulf Hermann  <ulf.hermann@qt.io>
diff --git a/libasm/Makefile.am b/libasm/Makefile.am
index 8713070..a5fc9fc 100644
--- a/libasm/Makefile.am
+++ b/libasm/Makefile.am
@@ -65,7 +65,7 @@ endif
 
 libasm_so_SOURCES =
 libasm.so$(EXEEXT): libasm_pic.a libasm.map
-	$(AM_V_CCLD)$(LINK) -shared -o $@ -Wl,--whole-archive,$<,--no-whole-archive \
+	$(AM_V_CCLD)$(LINK) $(dso_LDFLAGS) -o $@ -Wl,--whole-archive,$<,--no-whole-archive \
 		-Wl,--version-script,$(srcdir)/libasm.map,--no-undefined \
 		-Wl,--soname,$@.$(VERSION) \
 		../libebl/libebl.a ../libelf/libelf.so ../libdw/libdw.so \
diff --git a/libcpu/ChangeLog b/libcpu/ChangeLog
index 9235b1b..ef5da58 100644
--- a/libcpu/ChangeLog
+++ b/libcpu/ChangeLog
@@ -1,3 +1,7 @@
+2017-02-27  Ulf Hermann  <ulf.hermann@qt.io>
+
+	* Makefile.am: Use fpic_CFLAGS.
+
 2017-02-21  Ulf Hermann  <ulf.hermann@qt.io>
 
 	* Makefile.am: Link gendis agaist libgnu.a if requested.
diff --git a/libcpu/Makefile.am b/libcpu/Makefile.am
index 30a6799..9ca0f43 100644
--- a/libcpu/Makefile.am
+++ b/libcpu/Makefile.am
@@ -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=)
diff --git a/libdw/ChangeLog b/libdw/ChangeLog
index 6e11181..d15c861 100644
--- a/libdw/ChangeLog
+++ b/libdw/ChangeLog
@@ -1,5 +1,9 @@
 2017-02-27  Ulf Hermann  <ulf.hermann@qt.io>
 
+	* Makefile.am: Use fpic_CFLAGS and dso_LDFLAGS.
+
+2017-02-27  Ulf Hermann  <ulf.hermann@qt.io>
+
 	* Makefile.am: Remove argp_LDADD.
 
 2017-02-21  Ulf Hermann  <ulf.hermann@qt.io>
diff --git a/libdw/Makefile.am b/libdw/Makefile.am
index 8843062..7b69f6a 100644
--- a/libdw/Makefile.am
+++ b/libdw/Makefile.am
@@ -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
@@ -114,7 +114,7 @@ libdw.so$(EXEEXT): $(srcdir)/libdw.map libdw_pic.a ../libdwelf/libdwelf_pic.a \
 	  ../libelf/libelf.so
 # 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 \
+	$(AM_V_CCLD)$(LINK) $(dso_LDFLAGS) -o $@ -Wl,--soname,$@.$(VERSION) \
 		-Wl,--enable-new-dtags,-rpath,$(pkglibdir) \
 		-Wl,--version-script,$<,--no-undefined \
 		-Wl,--whole-archive $(filter-out $<,$^) -Wl,--no-whole-archive\
diff --git a/libebl/ChangeLog b/libebl/ChangeLog
index 0605083..56b0421 100644
--- a/libebl/ChangeLog
+++ b/libebl/ChangeLog
@@ -1,5 +1,9 @@
 2017-04-27  Ulf Hermann  <ulf.hermann@qt.io>
 
+	* Makefile.am: Use fpic_CFLAGS.
+
+2017-04-27  Ulf Hermann  <ulf.hermann@qt.io>
+
 	* eblmachineflagname.c: Don't include system.h.
 	* eblopenbackend.c: Likewise.
 
diff --git a/libebl/Makefile.am b/libebl/Makefile.am
index 6f945eb..60d0e73 100644
--- a/libebl/Makefile.am
+++ b/libebl/Makefile.am
@@ -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@
diff --git a/libelf/ChangeLog b/libelf/ChangeLog
index 9165634..1c6cce2 100644
--- a/libelf/ChangeLog
+++ b/libelf/ChangeLog
@@ -1,5 +1,9 @@
 2017-04-27  Ulf Hermann  <ulf.hermann@qt.io>
 
+	* Makefile.am: Use fpic_CFLAGS and dso_LDFLAGS.
+
+2017-04-27  Ulf Hermann  <ulf.hermann@qt.io>
+
 	* elf_getarsym.c: Don't replace rawmemchr.
 	* elf_strptr.c: Don't replace memrchr.
 
diff --git a/libelf/Makefile.am b/libelf/Makefile.am
index d141c64..78d6853 100644
--- a/libelf/Makefile.am
+++ b/libelf/Makefile.am
@@ -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
@@ -105,9 +105,9 @@ endif
 
 libelf_so_SOURCES =
 libelf.so$(EXEEXT): libelf_pic.a libelf.map
-	$(AM_V_CCLD)$(LINK) -shared -o $@ -Wl,--whole-archive,$<,--no-whole-archive \
+	$(AM_V_CCLD)$(LINK) $(dso_LDFLAGS) -o $@ -Wl,--whole-archive,$<,--no-whole-archive \
 		-Wl,--version-script,$(srcdir)/libelf.map,--no-undefined \
-		-Wl,--soname,$@.$(VERSION),-z,defs,-z,relro $(libelf_so_LDLIBS)
+		-Wl,--soname,$@.$(VERSION) $(libelf_so_LDLIBS)
 	@$(textrel_check)
 	$(AM_V_at)ln -fs $@ $@.$(VERSION)
 
diff --git a/libgnu/Makefile.am b/libgnu/Makefile.am
index c4a90e8..0d31e69 100644
--- a/libgnu/Makefile.am
+++ b/libgnu/Makefile.am
@@ -30,7 +30,7 @@
 
 # for eu-config.h
 AM_CPPFLAGS = -I$(top_srcdir)/lib
-AM_CFLAGS = -fPIC
+AM_CFLAGS = $(fpic_CFLAGS)
 noinst_LIBRARIES =
 MOSTLYCLEANFILES =
 MOSTLYCLEANDIRS =
diff --git a/tests/ChangeLog b/tests/ChangeLog
index d2a14c2..9d20341 100644
--- a/tests/ChangeLog
+++ b/tests/ChangeLog
@@ -1,5 +1,9 @@
 2017-04-27  Ulf Hermann <ulf.hermann@qt.io>
 
+	* Makefile.am: Use fpie_CFLAGS and fpic_CFLAGS.
+
+2017-04-27  Ulf Hermann <ulf.hermann@qt.io>
+
 	* Makefile.am: Drop argp_LDADD.
 	* elfstrmerge.c: Don't include system.h.
 
diff --git a/tests/Makefile.am b/tests/Makefile.am
index 69d1fcd..54a99d4 100644
--- a/tests/Makefile.am
+++ b/tests/Makefile.am
@@ -468,7 +468,7 @@ dwfl_report_elf_align_LDADD = $(libdw) $(libgnu)
 varlocs_LDADD = $(libdw) $(libelf) $(libgnu)
 backtrace_LDADD = $(libdw) $(libelf) $(libgnu)
 # 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) $(libgnu)
@@ -479,7 +479,7 @@ debugaltlink_LDADD = $(libdw) $(libelf) $(libgnu)
 buildid_LDADD = $(libdw) $(libelf) $(libgnu)
 deleted_LDADD = ./deleted-lib.so $(libgnu)
 deleted_lib_so_LDFLAGS = -shared -rdynamic
-deleted_lib_so_CFLAGS = -fPIC -fasynchronous-unwind-tables
+deleted_lib_so_CFLAGS = $(fpic_LDFLAGS) -fasynchronous-unwind-tables
 deleted_lib_so_LDADD = $(libgnu)
 aggregate_size_LDADD = $(libdw) $(libelf) $(libgnu)
 peel_type_LDADD = $(libdw) $(libelf) $(libgnu)
-- 
2.1.4



More information about the Elfutils-devel mailing list