This is the mail archive of the
gcc-patches@gcc.gnu.org
mailing list for the GCC project.
[patch] #pragma pack vs. STRUCTURE_SIZE_BOUNDARY
- From: Paul Brook <paul at codesourcery dot com>
- To: gcc-patches at gcc dot gnu dot org
- Date: Sun, 22 Oct 2006 16:32:31 +0100
- Subject: [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];