This is the mail archive of the gcc-patches@gcc.gnu.org mailing list for the GCC project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

[patch] #pragma pack vs. STRUCTURE_SIZE_BOUNDARY


Currently STRUCTURE_SIZE_BOUNDARY is obeyed even when
#pragma pack(1) is in effect. By contrast __attribute__((packed)) overrides 
STRUCTURE_SIZE_BOUNDARY.

This matters when odd sized packed structs are nested inside each other.

Given the purpose of #pragma pack is for compatibility with Win32, I think 
#pragma pack(1) act the same as __attribute__((packed)).

The patch below fixes this.
Tested with cross to arm-none-eabi.
Ok?

Paul

2006-10-22  Paul Brook  <paul@codesourcery.com>

	gcc/
	* stor-layout.c (start_record_layout): maximum_field_alignment
	overrides STRUCTURE_SIZE_BOUNDARY.

	gcc/testsuite/
	* gcc.dg/pragma-pack-4.c: New test.

Index: gcc/stor-layout.c
===================================================================
--- gcc/stor-layout.c	(revision 117906)
+++ gcc/stor-layout.c	(working copy)
@@ -525,7 +525,15 @@ start_record_layout (tree t)
 #ifdef STRUCTURE_SIZE_BOUNDARY
   /* Packed structures don't need to have minimum size.  */
   if (! TYPE_PACKED (t))
-    rli->record_align = MAX (rli->record_align, (unsigned) 
STRUCTURE_SIZE_BOUNDARY);
+    {
+      unsigned tmp;
+
+      /* #pragma pack overrides STRUCTURE_SIZE_BOUNDARY.  */
+      tmp = (unsigned) STRUCTURE_SIZE_BOUNDARY;
+      if (maximum_field_alignment != 0)
+	tmp = MIN (tmp, maximum_field_alignment);
+      rli->record_align = MAX (rli->record_align, tmp);
+    }
 #endif
 
   rli->offset = size_zero_node;
Index: gcc/testsuite/gcc.dg/pragma-pack-4.c
===================================================================
--- gcc/testsuite/gcc.dg/pragma-pack-4.c	(revision 0)
+++ gcc/testsuite/gcc.dg/pragma-pack-4.c	(revision 0)
@@ -0,0 +1,10 @@
+/* Check that pragma pack overrides STRUCTURE_SIZE_BOUNDARY.  */
+/* { dg-do compile } */
+
+#pragma pack(1)
+struct S
+{
+  char a;
+};
+
+int test[sizeof(struct S) == 1 ? 1: -1];


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]