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

Ulf Hermann ulf.hermann@qt.io
Wed May 3 15:47:00 GMT 2017


On windows gcc by default generates code that follows the MSVC layout.
We don't want that as it adds extra padding.

Signed-off-by: Ulf Hermann <ulf.hermann@qt.io>
---
 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 eaea959..392efaa 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,5 +1,10 @@
 2017-05-03  Ulf Hermann  <ulf.hermann@qt.io>
 
+	* configure.ac: Check if the compiler supports
+	__attribute__((gcc_struct)).
+
+2017-05-03  Ulf Hermann  <ulf.hermann@qt.io>
+
 	* configure.ac: Add check for -rdynamic.
 
 2017-04-28  Ulf Hermann  <ulf.hermann@qt.io>
diff --git a/backends/ChangeLog b/backends/ChangeLog
index 8985f7c..caefcf4 100644
--- a/backends/ChangeLog
+++ b/backends/ChangeLog
@@ -1,3 +1,7 @@
+2017-05-03  Ulf Hermann  <ulf.hermann@qt.io>
+
+	* linux-core-note.c: Use attribute_packed.
+
 2017-04-06  Mark Wielaard  <mark@klomp.org>
 
 	* i386_unwind.c: New file.
diff --git a/backends/linux-core-note.c b/backends/linux-core-note.c
index 67638d7..08282ba 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 e45584e..e4b2946 100644
--- a/configure.ac
+++ b/configure.ac
@@ -185,6 +185,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 ecc6179..6c0ac6d 100644
--- a/lib/ChangeLog
+++ b/lib/ChangeLog
@@ -1,3 +1,8 @@
+2017-05-03  Ulf Hermann  <ulf.hermann@qt.io>
+
+	* eu-config.h: Define attribute_packed to be either only
+	__attribute__((packed)) or to include gcc_struct if available.
+
 2017-04-27  Ulf Hermann  <ulf.hermann@qt.io>
 
 	* 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 0709828..135803e 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 ef5da58..a50a87b 100644
--- a/libcpu/ChangeLog
+++ b/libcpu/ChangeLog
@@ -1,3 +1,7 @@
+2017-05-03  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.
diff --git a/libcpu/memory-access.h b/libcpu/memory-access.h
index 44210e2..779825f 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 0d94ad5..f4b9dfb 100644
--- a/libdw/ChangeLog
+++ b/libdw/ChangeLog
@@ -1,5 +1,9 @@
 2017-05-03  Ulf Hermann  <ulf.hermann@qt.io>
 
+	* memory-access.h: Use attribute_packed.
+
+2017-05-03  Ulf Hermann  <ulf.hermann@qt.io>
+
 	* Makefile.am: Output dw.def when linking dw.dll and create dw.lib
 	when installing.
 
diff --git a/libdw/memory-access.h b/libdw/memory-access.h
index a749b5a..afb651f 100644
--- a/libdw/memory-access.h
+++ b/libdw/memory-access.h
@@ -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))
diff --git a/libelf/ChangeLog b/libelf/ChangeLog
index 8dfa2b7..52f69e7 100644
--- a/libelf/ChangeLog
+++ b/libelf/ChangeLog
@@ -1,5 +1,9 @@
 2017-05-03  Ulf Hermann  <ulf.hermann@qt.io>
 
+	* gelf_xlate.c: Use attribute_packed.
+
+2017-05-03  Ulf Hermann  <ulf.hermann@qt.io>
+
 	* Makefile.am: Output elf.def when linking elf.dll and create elf.lib
 	when installing.
 
diff --git a/libelf/gelf_xlate.c b/libelf/gelf_xlate.c
index f3d3b7a..479f143 100644
--- a/libelf/gelf_xlate.c
+++ b/libelf/gelf_xlate.c
@@ -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)
-- 
2.1.4



More information about the Elfutils-devel mailing list