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

2017-08-18 Thread Ulf Hermann
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.

(I'm reposting this patch, rebased to not depend on any others, with 
extended commit message. v2 was apparently not accepted because the
patch's implications were misunderstood.)

Signed-off-by: Ulf Hermann 
---
 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   |  4 ++--
 libcpu/ChangeLog |  4 
 libcpu/Makefile.am   |  2 +-
 libdw/ChangeLog  |  4 
 libdw/Makefile.am|  6 +++---
 libebl/ChangeLog |  4 
 libebl/Makefile.am   |  2 +-
 libelf/ChangeLog |  4 
 libelf/Makefile.am   |  6 +++---
 tests/ChangeLog  |  4 
 tests/Makefile.am|  4 ++--
 20 files changed, 112 insertions(+), 19 deletions(-)

diff --git a/ChangeLog b/ChangeLog
index 9253c0a3..62146227 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,8 @@
+2017-04-27  Ulf Hermann  
+
+   * configure.ac: Check if -fPIC, -fPIE, -Wl,-z,defs,
+   and -Wl,-z,relro are supported by the compiler.
+
 2017-08-02  Mark Wielaard  
 
* configure.ac: Set version to 0.170.
diff --git a/backends/ChangeLog b/backends/ChangeLog
index a66e923e..79b50ebf 100644
--- a/backends/ChangeLog
+++ b/backends/ChangeLog
@@ -1,3 +1,7 @@
+2017-04-27  Ulf Hermann 
+
+   * Makefile.am: Use dso_LDFLAGS.
+
 2017-07-27  Mark Wielaard  
 
* sparc_reloc.def: GOTDATA_OP_HIX22, GOTDATA_OP_LOX10 and
diff --git a/backends/Makefile.am b/backends/Makefile.am
index 37dc2d20..0fde0cb0 100644
--- a/backends/Makefile.am
+++ b/backends/Makefile.am
@@ -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)
diff --git a/config/ChangeLog b/config/ChangeLog
index 02cf76f9..1ed3c4af 100644
--- a/config/ChangeLog
+++ b/config/ChangeLog
@@ -1,3 +1,7 @@
+2017-04-27  Ulf Hermann  
+
+   * eu.am: Use fpic_CFLAGS.
+
 2016-08-02  Mark Wielaard  
 
* elfutils.spec.in: Update for 0.170.
diff --git a/config/eu.am b/config/eu.am
index 8fe1e259..796f3883 100644
--- a/config/eu.am
+++ b/config/eu.am
@@ -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
diff --git a/configure.ac b/configure.ac
index 1f1856df..e6e3b675 100644
--- a/configure.ac
+++ b/configure.ac
@@ -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_C

[PATCH v2] Check if gcc complains about __attribute__ (visibility(..))

2017-08-18 Thread Ulf Hermann
If so, define attribute_hidden to be empty. Also, use attribute_hidden
in all places where we hide symbols. If this attribute is missing, it
simply means that we cannot hide private symbols in the binary using
attributes. This disables some optimizations and may increase the risk 
of symbol name clashes with other libraries, but is not fatal. 

However, we still employ linker version scripts to explicitly define 
the exported symbols. This serves much of the same purpose. Also, as
all our symbols are prefixed with the library name, and "__" for 
private ones, the chance of clashes is low anyway.

(I'm reposting this patch, rebased and with extended commit message. v1
was apparently not accepted because the patch's implications were
misunderstood.)

Signed-off-by: Ulf Hermann 
---
 ChangeLog   |  5 +
 configure.ac| 16 
 lib/ChangeLog   |  5 +
 lib/eu-config.h |  4 
 libdw/ChangeLog |  5 +
 libdw/libdwP.h  |  2 +-
 libdw/libdw_alloc.c |  2 +-
 libelf/ChangeLog|  4 
 libelf/libelfP.h|  2 +-
 9 files changed, 42 insertions(+), 3 deletions(-)

diff --git a/ChangeLog b/ChangeLog
index 62146227..84fd2555 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,5 +1,10 @@
 2017-04-27  Ulf Hermann  
 
+   * configure.ac: Check if the compiler supports
+   __attribute__((visibility(...))).
+
+2017-04-27  Ulf Hermann  
+
* configure.ac: Check if -fPIC, -fPIE, -Wl,-z,defs,
and -Wl,-z,relro are supported by the compiler.
 
diff --git a/configure.ac b/configure.ac
index e6e3b675..c4fc7e3d 100644
--- a/configure.ac
+++ b/configure.ac
@@ -127,6 +127,22 @@ 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 __attribute__((visibility()))],
+   ac_cv_visibility, [dnl
+save_CFLAGS="$CFLAGS"
+CFLAGS="$save_CFLAGS -Werror"
+AC_COMPILE_IFELSE([AC_LANG_SOURCE([dnl
+int __attribute__((visibility("hidden")))
+foo (int a)
+{
+  return a;
+}])], ac_cv_visibility=yes, ac_cv_visibility=no)
+CFLAGS="$save_CFLAGS"])
+if test "$ac_cv_visibility" = "yes"; then
+   AC_DEFINE([HAVE_VISIBILITY], [1],
+ [Defined if __attribute__((visibility())) is supported])
+fi
+
 AC_CACHE_CHECK([whether gcc supports -fPIC], ac_cv_fpic, [dnl
 save_CFLAGS="$CFLAGS"
 CFLAGS="$save_CFLAGS -fPIC -Werror"
diff --git a/lib/ChangeLog b/lib/ChangeLog
index 67ef2792..23c0f41b 100644
--- a/lib/ChangeLog
+++ b/lib/ChangeLog
@@ -1,5 +1,10 @@
 2017-04-27  Ulf Hermann  
 
+   * eu-config.h: Define attribute_hidden to be empty if the compiler
+   doesn't support it.
+
+2017-04-27  Ulf Hermann  
+
* Makefile.am: Use fpic_CFLAGS.
 
 2017-07-18  Mark Wielaard  
diff --git a/lib/eu-config.h b/lib/eu-config.h
index 400cdc6e..07098282 100644
--- a/lib/eu-config.h
+++ b/lib/eu-config.h
@@ -68,8 +68,12 @@
 #define internal_strong_alias(name, aliasname) \
   extern __typeof (name) aliasname __attribute__ ((alias (#name))) 
internal_function;
 
+#ifdef HAVE_VISIBILITY
 #define attribute_hidden \
   __attribute__ ((visibility ("hidden")))
+#else
+#define attribute_hidden /* empty */
+#endif
 
 /* Define ALLOW_UNALIGNED if the architecture allows operations on
unaligned memory locations.  */
diff --git a/libdw/ChangeLog b/libdw/ChangeLog
index 67d7799d..c13344af 100644
--- a/libdw/ChangeLog
+++ b/libdw/ChangeLog
@@ -1,5 +1,10 @@
 2017-02-27  Ulf Hermann  
 
+   * libdwP.h: Use attribute_hidden.
+   * libdw_alloc.c: Likewise.
+
+2017-02-27  Ulf Hermann  
+
* Makefile.am: Use fpic_CFLAGS and dso_LDFLAGS.
 
 2017-07-26  Mark Wielaard  
diff --git a/libdw/libdwP.h b/libdw/libdwP.h
index 6ad322c1..78c00132 100644
--- a/libdw/libdwP.h
+++ b/libdw/libdwP.h
@@ -434,7 +434,7 @@ extern void *__libdw_allocate (Dwarf *dbg, size_t minsize, 
size_t align)
  __attribute__ ((__malloc__)) __nonnull_attribute__ (1);
 
 /* Default OOM handler.  */
-extern void __libdw_oom (void) __attribute ((noreturn, visibility ("hidden")));
+extern void __libdw_oom (void) __attribute ((noreturn)) attribute_hidden;
 
 /* Allocate the internal data for a unit not seen before.  */
 extern struct Dwarf_CU *__libdw_intern_next_unit (Dwarf *dbg, bool debug_types)
diff --git a/libdw/libdw_alloc.c b/libdw/libdw_alloc.c
index 28a8cf6e..d6af23a2 100644
--- a/libdw/libdw_alloc.c
+++ b/libdw/libdw_alloc.c
@@ -70,7 +70,7 @@ dwarf_new_oom_handler (Dwarf *dbg, Dwarf_OOM handler)
 
 
 void
-__attribute ((noreturn, visibility ("hidden")))
+__attribute ((noreturn)) attribute_hidden
 __libdw_oom (void)
 {
   while (1)
diff --git a/libelf/ChangeLog b/libelf/ChangeLog
index a0736467..9793d068 100644
--- a/libelf/ChangeLog
+++ b/libelf/ChangeLog
@@ -1,5 +1,9 @@
 2017-04-27  Ulf Hermann  
 
+   * libelfP.h: Use attribute_hidden.
+
+2017-04-27  Ulf Hermann  
+
* Makefile.am: Use fpic_CFLAGS and dso_LDFLAGS.
 
 2017-08-15  Mark Wielaard  
diff --git a/libelf/libelfP.h b/libelf/l

[PATCH v2] Make sure packed structs follow the gcc memory layout

2017-08-18 Thread Ulf Hermann
gcc defaults to using struct layouts that follow the native conventions,
even if __attribute__((packed)) is given. In order to get the layout we
expect, we need to tell gcc to always use the gcc struct layout, at
least for packed structs. To do this, we can use the gcc_struct
attribute.

This is important, not only for porting to windows, but also potentially
for other platforms, as the bugs resulting from struct layout
differences are rather subtle and hard to find.

(I'm reposting this patch, rebased, with extended commit message and
ChangeLog entries. v1 apparently slipped through the cracks.)

Signed-off-by: Ulf Hermann 
---
 ChangeLog  |  5 +
 backends/ChangeLog |  4 
 backends/linux-core-note.c |  2 +-
 configure.ac   | 13 +
 lib/ChangeLog  |  5 +
 lib/eu-config.h|  8 
 libcpu/ChangeLog   |  4 
 libcpu/memory-access.h |  2 +-
 libdw/ChangeLog|  4 
 libdw/memory-access.h  |  2 +-
 libelf/ChangeLog   |  4 
 libelf/gelf_xlate.c|  2 +-
 12 files changed, 51 insertions(+), 4 deletions(-)

diff --git a/ChangeLog b/ChangeLog
index 84fd2555..d2695721 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,8 @@
+2017-08-18  Ulf Hermann  
+
+   * configure.ac: Check if the compiler supports
+   __attribute__((gcc_struct)).
+
 2017-04-27  Ulf Hermann  
 
* configure.ac: Check if the compiler supports
diff --git a/backends/ChangeLog b/backends/ChangeLog
index 79b50ebf..8c3f42c6 100644
--- a/backends/ChangeLog
+++ b/backends/ChangeLog
@@ -1,3 +1,7 @@
+2017-08-18  Ulf Hermann 
+
+   * linux-core-note.c: Use attribute_packed.
+
 2017-04-27  Ulf Hermann 
 
* Makefile.am: Use dso_LDFLAGS.
diff --git a/backends/linux-core-note.c b/backends/linux-core-note.c
index 67638d70..08282ba4 100644
--- a/backends/linux-core-note.c
+++ b/backends/linux-core-note.c
@@ -111,7 +111,7 @@ struct EBLHOOK(prstatus)
   FIELD (INT, pr_fpvalid);
 }
 #ifdef ALIGN_PRSTATUS
-  __attribute__ ((packed, aligned (ALIGN_PRSTATUS)))
+  attribute_packed __attribute__ ((aligned (ALIGN_PRSTATUS)))
 #endif
 ;
 
diff --git a/configure.ac b/configure.ac
index c4fc7e3d..25ab19b3 100644
--- a/configure.ac
+++ b/configure.ac
@@ -143,6 +143,19 @@ if test "$ac_cv_visibility" = "yes"; then
  [Defined if __attribute__((visibility())) is supported])
 fi
 
+AC_CACHE_CHECK([whether gcc supports __attribute__((gcc_struct))],
+   ac_cv_gcc_struct, [dnl
+save_CFLAGS="$CFLAGS"
+CFLAGS="$save_CFLAGS -Werror"
+AC_COMPILE_IFELSE([AC_LANG_SOURCE([dnl
+struct test { int x; } __attribute__((gcc_struct));
+])], ac_cv_gcc_struct=yes, ac_cv_gcc_struct=no)
+CFLAGS="$save_CFLAGS"])
+if test "$ac_cv_gcc_struct" = "yes"; then
+   AC_DEFINE([HAVE_GCC_STRUCT], [1],
+ [Defined if __attribute__((gcc_struct)) is supported])
+fi
+
 AC_CACHE_CHECK([whether gcc supports -fPIC], ac_cv_fpic, [dnl
 save_CFLAGS="$CFLAGS"
 CFLAGS="$save_CFLAGS -fPIC -Werror"
diff --git a/lib/ChangeLog b/lib/ChangeLog
index 23c0f41b..61230453 100644
--- a/lib/ChangeLog
+++ b/lib/ChangeLog
@@ -1,3 +1,8 @@
+2017-08-18  Ulf Hermann  
+
+   * eu-config.h: Define attribute_packed to either
+   __attribute__((packed)) or __attribute__((packed, gcc_struct)).
+
 2017-04-27  Ulf Hermann  
 
* eu-config.h: Define attribute_hidden to be empty if the compiler
diff --git a/lib/eu-config.h b/lib/eu-config.h
index 07098282..135803e7 100644
--- a/lib/eu-config.h
+++ b/lib/eu-config.h
@@ -75,6 +75,14 @@
 #define attribute_hidden /* empty */
 #endif
 
+#ifdef HAVE_GCC_STRUCT
+#define attribute_packed \
+  __attribute__ ((packed, gcc_struct))
+#else
+#define attribute_packed \
+  __attribute__ ((packed))
+#endif
+
 /* Define ALLOW_UNALIGNED if the architecture allows operations on
unaligned memory locations.  */
 #define SANITIZE_UNDEFINED 1
diff --git a/libcpu/ChangeLog b/libcpu/ChangeLog
index 173defe6..c710e5af 100644
--- a/libcpu/ChangeLog
+++ b/libcpu/ChangeLog
@@ -1,3 +1,7 @@
+2017-08-18  Ulf Hermann  
+
+   * memory-access.h: Use attribute_packed.
+
 2017-02-27  Ulf Hermann  
 
* Makefile.am: Use fpic_CFLAGS.
diff --git a/libcpu/memory-access.h b/libcpu/memory-access.h
index 44210e2f..779825fa 100644
--- a/libcpu/memory-access.h
+++ b/libcpu/memory-access.h
@@ -90,7 +90,7 @@ union unaligned
 int16_t s2;
 int32_t s4;
 int64_t s8;
-  } __attribute__ ((packed));
+  } attribute_packed;
 
 static inline uint16_t
 read_2ubyte_unaligned (const void *p)
diff --git a/libdw/ChangeLog b/libdw/ChangeLog
index c13344af..94e9c9ab 100644
--- a/libdw/ChangeLog
+++ b/libdw/ChangeLog
@@ -1,3 +1,7 @@
+2017-08-18  Ulf Hermann  
+
+   * memory-access.h: Use attribute_packed.
+
 2017-02-27  Ulf Hermann  
 
* libdwP.h: Use attribute_hidden.
diff --git a/libdw/memory-access.h b/libdw/memory-access.h
index a749b5a9..afb651fc 100644
--- a/libdw/memory-access.h
+++ b/libdw/memory-access.

[PATCH] Drop -rdynamic from deleted-lib.so link step

2017-08-18 Thread Ulf Hermann
-rdynamic is meant for ELF objects that need to export internal
symbols to libraries they link to, but deleted-lib.so does not link to
anything else and doesn't have any internal symbols.

Note that the "deleted" test program does link to deleted-lib.so, but
deleted-lib.so being a shared object, will automatically export the
(non-hidden) "libfunc" symbol anyway.

(This patch supersedes "[PATCH] Check if gcc supports -rdynamic and
don't use it if not" from 05/03/2017)

Signed-off-by: Ulf Hermann 
---
 tests/ChangeLog   | 4 
 tests/Makefile.am | 2 +-
 2 files changed, 5 insertions(+), 1 deletion(-)

diff --git a/tests/ChangeLog b/tests/ChangeLog
index 19a4c88d..0d5bee75 100644
--- a/tests/ChangeLog
+++ b/tests/ChangeLog
@@ -1,3 +1,7 @@
+2017-08-18  Ulf Hermann 
+
+   * Makefile.am: Drop -rdynamic from deleted_lib_so_LDFLAGS.
+
 2017-04-27  Ulf Hermann 
 
* Makefile.am: Use fpie_CFLAGS and fpic_CFLAGS.
diff --git a/tests/Makefile.am b/tests/Makefile.am
index 6332a7c1..2eac8020 100644
--- a/tests/Makefile.am
+++ b/tests/Makefile.am
@@ -484,7 +484,7 @@ debuglink_LDADD = $(libdw) $(libelf)
 debugaltlink_LDADD = $(libdw) $(libelf)
 buildid_LDADD = $(libdw) $(libelf)
 deleted_LDADD = ./deleted-lib.so
-deleted_lib_so_LDFLAGS = -shared -rdynamic
+deleted_lib_so_LDFLAGS = -shared
 deleted_lib_so_CFLAGS = $(fpic_CFLAGS) -fasynchronous-unwind-tables
 aggregate_size_LDADD = $(libdw) $(libelf) $(argp_LDADD)
 peel_type_LDADD = $(libdw) $(libelf) $(argp_LDADD)
-- 
2.11.0



[PATCH] Drop *_so_SOURCES from libasm, libdw, libelf Makefile.am

2017-08-18 Thread Ulf Hermann
They aren't used and cause warnings from autoconf.

Signed-off-by: Ulf Hermann 
---
 libasm/ChangeLog   | 4 
 libasm/Makefile.am | 1 -
 libdw/ChangeLog| 4 
 libdw/Makefile.am  | 1 -
 libelf/ChangeLog   | 4 
 libelf/Makefile.am | 1 -
 6 files changed, 12 insertions(+), 3 deletions(-)

diff --git a/libasm/ChangeLog b/libasm/ChangeLog
index fffcced0..fb282e3e 100644
--- a/libasm/ChangeLog
+++ b/libasm/ChangeLog
@@ -1,3 +1,7 @@
+2017-08-18  Ulf Hermann  
+
+   * Makefile.am: Drop libasm_so_SOURCES.
+
 2017-02-27  Ulf Hermann  
 
* Makefile.am: Use dso_LDFLAGS.
diff --git a/libasm/Makefile.am b/libasm/Makefile.am
index 19fef508..29d2efee 100644
--- a/libasm/Makefile.am
+++ b/libasm/Makefile.am
@@ -62,7 +62,6 @@ libasm_so_LDLIBS += -lpthread
 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) $(dso_LDFLAGS) -o $@ \
-Wl,--soname,$@.$(VERSION) \
diff --git a/libdw/ChangeLog b/libdw/ChangeLog
index 94e9c9ab..79cce5ce 100644
--- a/libdw/ChangeLog
+++ b/libdw/ChangeLog
@@ -1,5 +1,9 @@
 2017-08-18  Ulf Hermann  
 
+   * Makefile.am: Drop libdw_so_SOURCES.
+
+2017-08-18  Ulf Hermann  
+
* memory-access.h: Use attribute_packed.
 
 2017-02-27  Ulf Hermann  
diff --git a/libdw/Makefile.am b/libdw/Makefile.am
index 8ee46802..af7d7793 100644
--- a/libdw/Makefile.am
+++ b/libdw/Makefile.am
@@ -106,7 +106,6 @@ libdw_so_LIBS = libdw_pic.a ../libdwelf/libdwelf_pic.a \
  ../libdwfl/libdwfl_pic.a ../libebl/libebl.a
 libdw_so_DEPS = ../lib/libeu.a ../libelf/libelf.so
 libdw_so_LDLIBS = $(libdw_so_DEPS) -ldl -lz $(argp_LDADD) $(zip_LIBS)
-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.
diff --git a/libelf/ChangeLog b/libelf/ChangeLog
index 7bd9e1bc..23484499 100644
--- a/libelf/ChangeLog
+++ b/libelf/ChangeLog
@@ -1,5 +1,9 @@
 2017-08-18  Ulf Hermann  
 
+   * Makefile.am: Drop libelf_so_SOURCES.
+
+2017-08-18  Ulf Hermann  
+
* gelf_xlate.c: Use attribute_packed.
 
 2017-04-27  Ulf Hermann  
diff --git a/libelf/Makefile.am b/libelf/Makefile.am
index ddaeaa2b..a749a1bb 100644
--- a/libelf/Makefile.am
+++ b/libelf/Makefile.am
@@ -102,7 +102,6 @@ libelf_so_LDLIBS += -lpthread
 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) $(dso_LDFLAGS) -o $@ \
-Wl,--soname,$@.$(VERSION) \
-- 
2.11.0



[PATCH v3] Detect if symbol versioning is supported

2017-08-18 Thread Ulf Hermann
If not, throw an error unless symbol versioning was explicitly
disabled.

(Reposting rebased patch, as v2 apparently slipped)

Signed-off-by: Ulf Hermann 
---
 ChangeLog|  4 
 configure.ac | 15 +++
 2 files changed, 19 insertions(+)

diff --git a/ChangeLog b/ChangeLog
index d2695721..b720f2cd 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,7 @@
+2017-05-03  Ulf Hermann  
+
+   * configure.ac: Test if symbol versioning is supported.
+
 2017-08-18  Ulf Hermann  
 
* configure.ac: Check if the compiler supports
diff --git a/configure.ac b/configure.ac
index 25ab19b3..4ab8816a 100644
--- a/configure.ac
+++ b/configure.ac
@@ -386,6 +386,21 @@ AS_IF([test "x$enable_textrelcheck" != "xno"],
 AC_ARG_ENABLE([symbol-versioning],
 AS_HELP_STRING([--disable-symbol-versioning],
[Disable symbol versioning in shared objects]))
+
+AC_CACHE_CHECK([whether symbol versioning is supported], 
ac_cv_symbol_versioning, [dnl
+AC_COMPILE_IFELSE([AC_LANG_SOURCE([dnl
+#define NEW_VERSION(name, version) \
+  asm (".symver " #name "," #name "@@@" #version);
+int foo(int x) { return x + 1; }
+NEW_VERSION (foo, ELFUTILS_12.12)
+])], ac_cv_symbol_versioning=yes, ac_cv_symbol_versioning=no)])
+if test "$ac_cv_symbol_versioning" = "no"; then
+if test "x$enable_symbol_versioning" != "xno"; then
+AC_MSG_ERROR([Symbol versioning is not supported.
+  Use --disable-symbol-versioning to build without.])
+fi
+fi
+
 AM_CONDITIONAL(SYMBOL_VERSIONING, [test "x$enable_symbol_versioning" != "xno"])
 AS_IF([test "x$enable_symbol_versioning" = "xno"],
   [AC_MSG_WARN([Disabling symbol versioning breaks ABI compatibility.])
-- 
2.11.0



[PATCH v3] Check if rpath is supported and throw an error if not

2017-08-18 Thread Ulf Hermann
Some systems don't have rpath. In that case the backends need to be
made available by some external mechanism. Provide a configure switch
to explicitly turn off the setting of rpaths. Throw an error if that is
not set and rpath is not supported.

(Reposting rebased patch, as v2 was never reviewed.)

Signed-off-by: Ulf Hermann 
---
 ChangeLog |  5 +
 configure.ac  | 22 ++
 libdw/ChangeLog   |  4 
 libdw/Makefile.am |  8 +++-
 4 files changed, 38 insertions(+), 1 deletion(-)

diff --git a/ChangeLog b/ChangeLog
index b720f2cd..47b37565 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,5 +1,10 @@
 2017-05-03  Ulf Hermann  
 
+   * configure.ac: Check if the linker supports -rpath and add a switch
+   to disable setting of rpath.
+
+2017-05-03  Ulf Hermann  
+
* configure.ac: Test if symbol versioning is supported.
 
 2017-08-18  Ulf Hermann  
diff --git a/configure.ac b/configure.ac
index 4ab8816a..6856ff7e 100644
--- a/configure.ac
+++ b/configure.ac
@@ -208,6 +208,28 @@ fi
 
 AC_SUBST([dso_LDFLAGS])
 
+AC_ARG_ENABLE([rpath],
+AS_HELP_STRING([--disable-rpath], [Disable setting of rpath]))
+
+AC_CACHE_CHECK([for rpath support], ac_cv_rpath, [dnl
+save_LDFLAGS="$LDFLAGS"
+LDFLAGS="$save_LDFLAGS -Wl,--enable-new-dtags,-rpath,/foo/bar"
+AC_LINK_IFELSE([AC_LANG_PROGRAM()], ac_cv_rpath=yes, ac_cv_rpath=no)
+LDFLAGS="$save_LDFLAGS"
+])
+if test "$ac_cv_rpath" = "no"; then
+if test "x$enable_rpath" != "xno"; then
+AC_MSG_ERROR([rpath is not supported.
+  Use --disable-rpath to build without.])
+fi
+fi
+
+AM_CONDITIONAL(RPATH, [test "x$enable_rpath" != "xno"])
+AS_IF([test "x$enable_rpath" = "xno"],
+  [AC_MSG_WARN([Disabling rpath prevents libdw from automatically
+finding the ebl backends.])
+   enable_rpath=no],[enable_rpath=yes])
+
 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.
diff --git a/libdw/ChangeLog b/libdw/ChangeLog
index 79cce5ce..8020c569 100644
--- a/libdw/ChangeLog
+++ b/libdw/ChangeLog
@@ -1,3 +1,7 @@
+2017-02-28  Ulf Hermann  
+
+   * Makefile.am: Don't set rpath if it's disabled.
+
 2017-08-18  Ulf Hermann  
 
* Makefile.am: Drop libdw_so_SOURCES.
diff --git a/libdw/Makefile.am b/libdw/Makefile.am
index af7d7793..3257c014 100644
--- a/libdw/Makefile.am
+++ b/libdw/Makefile.am
@@ -99,6 +99,12 @@ $(srcdir)/known-dwarf.h: 
$(top_srcdir)/config/known-dwarf.awk $(srcdir)/dwarf.h
mv -f $@.new $@
 endif
 
+if RPATH
+PKG_RPATH = -Wl,--enable-new-dtags,-rpath,$(pkglibdir)
+else
+PKG_RPATH =
+endif
+
 libdw_pic_a_SOURCES =
 am_libdw_pic_a_OBJECTS = $(libdw_a_SOURCES:.c=.os)
 
@@ -111,7 +117,7 @@ libdw.so$(EXEEXT): $(srcdir)/libdw.map $(libdw_so_LIBS) 
$(libdw_so_DEPS)
 # not fly in a setuid executable that links in libdw.
$(AM_V_CCLD)$(LINK) $(dso_LDFLAGS) -o $@ \
-Wl,--soname,$@.$(VERSION) \
-   -Wl,--enable-new-dtags,-rpath,$(pkglibdir) \
+   $(PKG_RPATH) \
-Wl,--version-script,$<,--no-undefined \
-Wl,--whole-archive $(libdw_so_LIBS) -Wl,--no-whole-archive \
$(libdw_so_LDLIBS)
-- 
2.11.0



[PATCH v2] On non-linux systems, don't use native signal numbers

2017-08-18 Thread Ulf Hermann
We assume core files from linux systems, so we should use the linux
version of the signals when reading them. Other OS might have different
signal numbers.

(v1 was a malformed patch)

Signed-off-by: Ulf Hermann 
---
 src/ChangeLog |  4 
 src/readelf.c | 24 +++-
 2 files changed, 23 insertions(+), 5 deletions(-)

diff --git a/src/ChangeLog b/src/ChangeLog
index 54ba767e..8aa57051 100644
--- a/src/ChangeLog
+++ b/src/ChangeLog
@@ -1,3 +1,7 @@
+2017-08-18  Ulf Hermann  
+
+   * readelf.c: Hardcode the signal numbers for non-linux systems.
+
 2017-07-26  Mark Wielaard  
 
* readelf.c (print_debug_macro_section): Accept either version 4 or
diff --git a/src/readelf.c b/src/readelf.c
index 73be474b..5e2f3fc2 100644
--- a/src/readelf.c
+++ b/src/readelf.c
@@ -57,6 +57,20 @@
 
 #include "../libdw/known-dwarf.h"
 
+#ifdef __linux__
+#define CORE_SIGILL  SIGILL
+#define CORE_SIGBUS  SIGBUS
+#define CORE_SIGFPE  SIGFPE
+#define CORE_SIGSEGV SIGSEGV
+#define CORE_SI_USER SI_USER
+#else
+/* We want the linux version of those as that is what shows up in the core 
files. */
+#define CORE_SIGILL  4  /* Illegal instruction (ANSI).  */
+#define CORE_SIGBUS  7  /* BUS error (4.2 BSD).  */
+#define CORE_SIGFPE  8  /* Floating-point exception (ANSI).  */
+#define CORE_SIGSEGV 11 /* Segmentation violation (ANSI).  */
+#define CORE_SI_USER 0  /* Sent by kill, sigsend.  */
+#endif
 
 /* Name and version of program.  */
 ARGP_PROGRAM_VERSION_HOOK_DEF = print_version;
@@ -9335,10 +9349,10 @@ handle_siginfo_note (Elf *core, GElf_Word descsz, 
GElf_Off desc_pos)
   if (si_code > 0)
 switch (si_signo)
   {
-  case SIGILL:
-  case SIGFPE:
-  case SIGSEGV:
-  case SIGBUS:
+  case CORE_SIGILL:
+  case CORE_SIGFPE:
+  case CORE_SIGSEGV:
+  case CORE_SIGBUS:
{
  uint64_t addr;
  if (! buf_read_ulong (core, &ptr, end, &addr))
@@ -9349,7 +9363,7 @@ handle_siginfo_note (Elf *core, GElf_Word descsz, 
GElf_Off desc_pos)
   default:
;
   }
-  else if (si_code == SI_USER)
+  else if (si_code == CORE_SI_USER)
 {
   int pid, uid;
   if (! buf_read_int (core, &ptr, end, &pid)
-- 
2.11.0



[PATCH v3] Cast pid_t to long long when printing

2017-08-18 Thread Ulf Hermann
We don't know sizeof(pid_t) as it's not specified in any standard. In
order to still print it, we cast to long long, the largest integer type
we can easily print.

(Rebased, and rephrased message: This is not a windows quirk, but
rather a standards problem.)

Signed-off-by: Ulf Hermann 
---
 src/ChangeLog |  4 
 src/stack.c   | 22 +++---
 2 files changed, 15 insertions(+), 11 deletions(-)

diff --git a/src/ChangeLog b/src/ChangeLog
index 8aa57051..daedfca6 100644
--- a/src/ChangeLog
+++ b/src/ChangeLog
@@ -1,3 +1,7 @@
+2017-05-04  Ulf Hermann  
+
+   * stack.c: Print pid_t using %lld.
+
 2017-08-18  Ulf Hermann  
 
* readelf.c: Hardcode the signal numbers for non-linux systems.
diff --git a/src/stack.c b/src/stack.c
index 6f2ff69f..392db2a6 100644
--- a/src/stack.c
+++ b/src/stack.c
@@ -362,7 +362,7 @@ print_frames (struct frames *frames, pid_t tid, int 
dwflerr, const char *what)
   if (frames->frames > 0)
 frames_shown = true;
 
-  printf ("TID %d:\n", tid);
+  printf ("TID %lld:\n", (long long)tid);
   int frame_nr = 0;
   for (int nr = 0; nr < frames->frames && (maxframes == 0
   || frame_nr < maxframes); nr++)
@@ -419,8 +419,8 @@ print_frames (struct frames *frames, pid_t tid, int 
dwflerr, const char *what)
 }
 
   if (frames->frames > 0 && frame_nr == maxframes)
-error (0, 0, "tid %d: shown max number of frames "
-  "(%d, use -n 0 for unlimited)", tid, maxframes);
+error (0, 0, "tid %lld: shown max number of frames "
+  "(%d, use -n 0 for unlimited)", (long long)tid, maxframes);
   else if (dwflerr != 0)
 {
   if (frames->frames > 0)
@@ -440,11 +440,11 @@ print_frames (struct frames *frames, pid_t tid, int 
dwflerr, const char *what)
  else
modname = "";
}
- error (0, 0, "%s tid %d at 0x%" PRIx64 " in %s: %s", what, tid,
+ error (0, 0, "%s tid %lld at 0x%" PRIx64 " in %s: %s", what, (long 
long)tid,
 pc_adjusted, modname, dwfl_errmsg (dwflerr));
}
   else
-   error (0, 0, "%s tid %d: %s", what, tid, dwfl_errmsg (dwflerr));
+   error (0, 0, "%s tid %lld: %s", what, (long long)tid, dwfl_errmsg 
(dwflerr));
 }
 }
 
@@ -575,10 +575,10 @@ parse_opt (int key, char *arg __attribute__ ((unused)),
 
  int err = dwfl_linux_proc_report (dwfl, pid);
  if (err < 0)
-   error (EXIT_BAD, 0, "dwfl_linux_proc_report pid %d: %s", pid,
+   error (EXIT_BAD, 0, "dwfl_linux_proc_report pid %lld: %s", (long 
long)pid,
   dwfl_errmsg (-1));
  else if (err > 0)
-   error (EXIT_BAD, err, "dwfl_linux_proc_report pid %d", pid);
+   error (EXIT_BAD, err, "dwfl_linux_proc_report pid %lld", (long 
long)pid);
}
 
   if (core != NULL)
@@ -597,10 +597,10 @@ parse_opt (int key, char *arg __attribute__ ((unused)),
{
  int err = dwfl_linux_proc_attach (dwfl, pid, false);
  if (err < 0)
-   error (EXIT_BAD, 0, "dwfl_linux_proc_attach pid %d: %s", pid,
+   error (EXIT_BAD, 0, "dwfl_linux_proc_attach pid %lld: %s", (long 
long)pid,
   dwfl_errmsg (-1));
  else if (err > 0)
-   error (EXIT_BAD, err, "dwfl_linux_proc_attach pid %d", pid);
+   error (EXIT_BAD, err, "dwfl_linux_proc_attach pid %lld", (long 
long)pid);
}
 
   if (core != NULL)
@@ -688,7 +688,7 @@ invoked with bad or missing arguments it will exit with 
return code 64.")
 
   if (show_modules)
 {
-  printf ("PID %d - %s module memory map\n", dwfl_pid (dwfl),
+  printf ("PID %lld - %s module memory map\n", (long long)dwfl_pid (dwfl),
  pid != 0 ? "process" : "core");
   if (dwfl_getmodules (dwfl, module_callback, NULL, 0) != 0)
error (EXIT_BAD, 0, "dwfl_getmodules: %s", dwfl_errmsg (-1));
@@ -721,7 +721,7 @@ invoked with bad or missing arguments it will exit with 
return code 64.")
 }
   else
 {
-  printf ("PID %d - %s\n", dwfl_pid (dwfl), pid != 0 ? "process" : "core");
+  printf ("PID %lld - %s\n", (long long)dwfl_pid (dwfl), pid != 0 ? 
"process" : "core");
   switch (dwfl_getthreads (dwfl, thread_callback, &frames))
{
case DWARF_CB_OK:
-- 
2.11.0