]> git.ipfire.org Git - thirdparty/elfutils.git/commitdiff
Make sure packed structs follow the gcc memory layout
authorUlf Hermann <ulf.hermann@qt.io>
Fri, 18 Aug 2017 12:05:10 +0000 (14:05 +0200)
committerMark Wielaard <mark@klomp.org>
Wed, 20 Sep 2017 19:40:31 +0000 (21:40 +0200)
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.

Signed-off-by: Ulf Hermann <ulf.hermann@qt.io>
12 files changed:
ChangeLog
backends/ChangeLog
backends/linux-core-note.c
configure.ac
lib/ChangeLog
lib/eu-config.h
libcpu/ChangeLog
libcpu/memory-access.h
libdw/ChangeLog
libdw/memory-access.h
libelf/ChangeLog
libelf/gelf_xlate.c

index c0034ba481f4c18c52f7d2031a52a85dae060746..cb87833c3f0796076dad0dfa4fa4e73c70839061 100644 (file)
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,8 @@
+2017-08-18  Ulf Hermann  <ulf.hermann@qt.io>
+
+       * configure.ac: Check if the compiler supports
+       __attribute__((gcc_struct)).
+
 2017-09-19  Mark Wielaard  <mark@klomp.org>
 
        * README: Add basic build instructions.
index 79b50ebf555eb537c8088868828c35169d1f30e7..8c3f42c608452c41d6716d69930c066ac4ec1c3b 100644 (file)
@@ -1,3 +1,7 @@
+2017-08-18  Ulf Hermann <ulf.hermann@qt.io>
+
+       * linux-core-note.c: Use attribute_packed.
+
 2017-04-27  Ulf Hermann <ulf.hermann@qt.io>
 
        * Makefile.am: Use dso_LDFLAGS.
index 67638d7006ad7985d620188b59eb97639411716b..08282ba4d545b459384ff18ff3439a8985b505ae 100644 (file)
@@ -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
 ;
 
index fb18dfc8b93198a2c65c6ca5fa9bffdde19796bf..4ab8816ad7f425ec917fafb25e61cd33582415a8 100644 (file)
@@ -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"
index 23c0f41b1749197874948860480f44fab007665a..612304538a24b1d3efe8ceccf9b2cfe13c21359d 100644 (file)
@@ -1,3 +1,8 @@
+2017-08-18  Ulf Hermann  <ulf.hermann@qt.io>
+
+       * eu-config.h: Define attribute_packed to either
+       __attribute__((packed)) or __attribute__((packed, gcc_struct)).
+
 2017-04-27  Ulf Hermann  <ulf.hermann@qt.io>
 
        * eu-config.h: Define attribute_hidden to be empty if the compiler
index 070982828910926ece4ed0b5caecbeb8cc62d96a..135803e7a536656ab2b6219340464e24036eecee 100644 (file)
 #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
index 173defe60c3e69383d34365c47d2d95bdc1d5c18..c710e5af7673490f24717f1eb5c56bf8b36b1e72 100644 (file)
@@ -1,3 +1,7 @@
+2017-08-18  Ulf Hermann  <ulf.hermann@qt.io>
+
+       * memory-access.h: Use attribute_packed.
+
 2017-02-27  Ulf Hermann  <ulf.hermann@qt.io>
 
        * Makefile.am: Use fpic_CFLAGS.
index 44210e2fb8ac02a93e100a57804c29a78fc08ccb..779825fa8e0d17711573a30c2f484ef1f72cecf3 100644 (file)
@@ -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)
index c13344af26f4d3c91a8bb1170f721226c72dd699..94e9c9ab0d9ca30ceaa41e047367d86bc9a5560b 100644 (file)
@@ -1,3 +1,7 @@
+2017-08-18  Ulf Hermann  <ulf.hermann@qt.io>
+
+       * memory-access.h: Use attribute_packed.
+
 2017-02-27  Ulf Hermann  <ulf.hermann@qt.io>
 
        * libdwP.h: Use attribute_hidden.
index a749b5a9be4d7da333b60ba9e0db864d8218ebe1..afb651fce7322649e3e76f8493d42827f540e65d 100644 (file)
@@ -170,7 +170,7 @@ union unaligned
     int16_t s2;
     int32_t s4;
     int64_t s8;
-  } __attribute__ ((packed));
+  } attribute_packed;
 
 # define read_2ubyte_unaligned(Dbg, Addr) \
   read_2ubyte_unaligned_1 ((Dbg)->other_byte_order, (Addr))
index 9793d0684aac22ebd0581d4317b22a8fec0bfad6..7bd9e1bc036d65f0a08d288df9c68e1d936d71fa 100644 (file)
@@ -1,3 +1,7 @@
+2017-08-18  Ulf Hermann  <ulf.hermann@qt.io>
+
+       * gelf_xlate.c: Use attribute_packed.
+
 2017-04-27  Ulf Hermann  <ulf.hermann@qt.io>
 
        * libelfP.h: Use attribute_hidden.
index f3d3b7a06fdfd49adf8b513b9dae676ec6262bd1..479f1436e333a3636abe5d2f8cd5629d421aa1fe 100644 (file)
@@ -74,7 +74,7 @@ union unaligned
     uint16_t u16;
     uint32_t u32;
     uint64_t u64;
-  } __attribute__ ((packed));
+  } attribute_packed;
 
 #define FETCH(Bits, ptr)       (((const union unaligned *) ptr)->u##Bits)
 #define STORE(Bits, ptr, val)  (((union unaligned *) ptr)->u##Bits = val)