This is the mail archive of the 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]

C ABI: struct/union inconsistency

On x86 (and other PCC_BITFIELD_TYPE_MATTERS machines), this program:

  #include <stdio.h>

  struct S { long : 0; };
  union U { long : 0; };

  int main () {
    printf ("Alignment of S: %d\n", __alignof__(struct S));
    printf ("Alignment of U: %d\n", __alignof__(union U));
    printf ("Size of S: %d\n", sizeof(struct S));
    printf ("Size of U: %d\n", sizeof(union U));

gives suprising output:

 Alignment of S: 1
 Alignment of U: 4
 Size of S: 0
 Size of U: 0

According to the doumentation for PCC_BITFIELD_TYPE_MATTERS:

     The behavior is that the type written for a bitfield (`int',
     `short', or other integer type) imposes an alignment for the
     entire structure, as if the structure really did contain an
     ordinary field of that type.

That would suggest that both S and U should have 4-byte alignment.

Note further that the psABI for x86 says explicitly that an unnamed
bitfield does *not* affect the alignment of the structure, although it
does affect the alignment of the next field.

So, as far as I can tell, we are both inconsistent with our own
documentation (in that we do not align S) and wrong as far as the
psABI is concerned (in that we do align U).


The size is another issue.  Giving empty sturctures size zero is
rather unfortunate; note that we also give an array of type "S[3]"
where "S" is an empty class size 0. That's distinctly odd; we have
all the array elements at the same location.

Accepting empty structures in C is an extension, but structures like S
(i.e., with a single zero-width bitfield) are part of the language.

In C++, empty structures are allowed, but are explicitly required to
have non-zero size.

I think we should adopt this same rule in C.  I cannot find anything
in the C99 standard that requires types to have a non-zero size, but
there are suggestions of that -- such as a note mentioning the:

  sizeof (array) / sizeof (array[0])

trick, which clearly does not work if array[0] has zero size.

I think we ought to:

(a) Change our docs to make clear that PCC_BITFIELD_TYPE_MATTERS does
    not cause unnamed bitfields to align structures.

(b) Fix our code so that alignment for unions is handled like
    structures.  This is an ABI change, but one that will affect 
    almost zero programs -- putting an unnamed bitfield in a union
    is something only someone writing ABI tests would try.

(c) Make all structures/unions have size at least one in GCC.

The combination of (a) and (b) will make our code consistent with the
docs and will make us match the psABI.  

The addition of (c) will avoid all kinds of weirdness for anyone who
actually makes empty structures, and will make C and C++ consistent.
I do not know if (c) is actually required by the C standard, but it
might be.  I also do not know if some piece of code in the Linux
kernel is going to depend on the current behavior; we might want to
have a switch for the old behavior.

What do others think we ought to do about this?

Mark Mitchell         
CodeSourcery, LLC     

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