Bug 14786 - Struct misaligned containing unsigned char bit-field
Summary: Struct misaligned containing unsigned char bit-field
Status: RESOLVED INVALID
Alias: None
Product: gcc
Classification: Unclassified
Component: target (show other bugs)
Version: 3.3.2
: P3 minor
Target Milestone: ---
Assignee: Not yet assigned to anyone
URL:
Keywords: ABI, wrong-code
Depends on:
Blocks:
 
Reported: 2004-03-30 19:13 UTC by Ryan Mansfield
Modified: 2005-07-23 22:49 UTC (History)
1 user (show)

See Also:
Host: powerpc-ibm-aix5.1.0.0
Target: powerpc-ibm-aix5.1.0.0
Build: powerpc-ibm-aix5.1.0.0
Known to work:
Known to fail:
Last reconfirmed:


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description Ryan Mansfield 2004-03-30 19:13:04 UTC
Description:

According to RS/6000 Alignment Rules, structs containing   
bit fields are 4-byte (word) aligned. The following testcase 
illustrates that this rule is violated, when Substruct B is not  
word aligned. 

Testcase:

extern int      printf(const char *, ...);
typedef unsigned char UCHAR;

typedef struct {
    UCHAR               a   : 1 ;
    UCHAR               b   : 1 ;
    UCHAR               c   : 1 ;
    UCHAR               d   : 1 ;
    UCHAR               e   : 1 ;
    UCHAR               f   : 1 ;
    UCHAR               g   : 1 ;
    UCHAR               h   : 1 ;
    UCHAR               i   : 1 ;
    UCHAR               J   : 1 ;
    UCHAR               K   : 6 ;
} SubStruct ;

typedef struct {
    UCHAR               A[5];
    SubStruct           B   ;
    UCHAR               C   ;
    UCHAR               D[4];
} Struct ;

int main() {
  Struct data;
  printf("sizeof(SubStruct) =%d\n",sizeof(SubStruct));
  printf("sizeof(Struct)    =%d\n",sizeof(Struct));
  printf(" offset of B      =%d\n",
        ((unsigned int)&(data.B)-(unsigned int)&data));
  printf(" offset of C      =%d\n",
        ((unsigned int)&(data.C)-(unsigned int)&data));
  printf(" offset of D      =%d\n",
        ((unsigned int)&(data.D)-(unsigned int)&data));
  return 0;
}

Actual Output:

$ gcc t.c
$ ./a.out
Address of Struct =2ff22a20
sizeof(SubStruct) =2
sizeof(Struct)    =12
 offset of B      =5
 offset of C      =7
 offset of D      =8
$

Expected Output:

$ gcc t.c
$ ./a.out
Address of Struct =2ff22a20
sizeof(SubStruct) =2
sizeof(Struct)    =20
 offset of B      =8
 offset of C      =12
 offset of D      =13


GCC Version:

$ /home/ryan/gcc-3.3.2/aix51/bin/gcc -v
Reading specs from /home/ryan/gcc-3.3.2/aix51/bin/../lib/gcc-lib/powerpc-ibm-
aix5.1.0.0/3.3.
2/specs
Configured with: ../gcc-3.3.2/configure --disable-nls
Thread model: aix
gcc version 3.3.2

Machine Type:

Model: 7044 -170
Processor: 1 x pwr3 
Processor Speed: 400 MHz
Memory: 1 GB
AIX Level: AIX 5.1 ML 5
Hardware Arch: 64 bit
Kernel Arch: 32 bit
Comment 1 Andrew Pinski 2004-03-30 19:36:22 UTC
From Mac OS PEF ABI which is based on the PowerOpen ABI which is the ABI for AIX:
The preferred alignment of a bit field is the same as its declared type.

So if the same wording is in the Power Open ABI then this is invalid as it is correctly aligned and bit-
fields are aligned as the same as their declared type.
Comment 2 Ryan Mansfield 2004-03-31 15:35:45 UTC
http://publib.boulder.ibm.com/infocenter/comphelp/topic/com.ibm.vacpp6a.doc/prog
uide/ref/rvclralg.htm

The xlc compiler exhibits the expected behavior.

$ xlc t.c -qlanglvl=extended
"t.c", line 5.25: 1506-159 (E) Bit-field type specified for a is not valid. 
Type unsigned assumed.
"t.c", line 6.25: 1506-159 (E) Bit-field type specified for b is not valid. 
Type unsigned assumed.
"t.c", line 7.25: 1506-159 (E) Bit-field type specified for c is not valid. 
Type unsigned assumed.
"t.c", line 8.25: 1506-159 (E) Bit-field type specified for d is not valid. 
Type unsigned assumed.
"t.c", line 9.25: 1506-159 (E) Bit-field type specified for e is not valid. 
Type unsigned assumed.
"t.c", line 10.25: 1506-159 (E) Bit-field type specified for f is not valid. 
Type unsigned assumed.
"t.c", line 11.25: 1506-159 (E) Bit-field type specified for g is not valid. 
Type unsigned assumed.
"t.c", line 12.25: 1506-159 (E) Bit-field type specified for h is not valid. 
Type unsigned assumed.
"t.c", line 13.25: 1506-159 (E) Bit-field type specified for i is not valid. 
Type unsigned assumed.
"t.c", line 14.25: 1506-159 (E) Bit-field type specified for J is not valid. 
Type unsigned assumed.
"t.c", line 15.25: 1506-159 (E) Bit-field type specified for K is not valid. 
Type unsigned assumed.
./a.out
$ ./a.out
Address of Struct =2ff22a20
sizeof(SubStruct) =4
sizeof(Struct)    =20
 offset of B      =8
 offset of C      =12
 offset of D      =13
Comment 3 Andrew Pinski 2004-04-04 06:37:59 UTC
Note in OpenPower ABI, bitfields are only defined for short and int.
Comment 4 Andrew Pinski 2004-06-03 22:50:05 UTC
Invalid as you see from the XLC output that bitfields alignment is not defined on char.