alignment faults in 3.6

Måns Rullgård mans at
Thu Oct 11 06:46:11 EDT 2012

"David Laight" <David.Laight at ACULAB.COM> writes:

>> > It might be enough to use __attribute__((aligned(2))) on some structure
>> > members (actually does 'ldm' need 8 byte alignment?? - in which case
>> > aligned(4) is enough).
>> The aligned attribute can only increase alignment.
> Not true.
> If you have:
> struct foo {
>     short   a;
>     int     b __attribute__((aligned(2)));
>     short   c;
> };
> You'll find sizeof (struct foo) is 8, and that, on arm/sparc etc,
> gcc will generate 2 16bit accesses (and shifts) for foo.b;

That is not what the manual says.  Quoting:

     When used on a struct, or struct member, the `aligned' attribute
     can only increase the alignment

My gcc agrees with the manual:

$ cat foo.c
struct foo {
    short a;
    int b __attribute__((aligned(2)));
    short c;

int foo(struct foo *f)
    return f->b;
$ arm-unknown-linux-gnueabi-gcc-4.7.2 -O2 -o- -S foo.c
        .align  2
        .global foo
        .type   foo, %function
        ldr     r0, [r0, #4]
        bx      lr

(compiler output edited for clarity)

This clearly says the 'b' member resides at an offset of 4 bytes into
the struct.

Måns Rullgård
mans at

More information about the linux-arm-kernel mailing list