Re: Attribute packed and alignment

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



On Sun, 2021-04-18 at 17:10 +0300, Matwey V. Kornilov via Gcc-help
wrote:
> 
> Hi,
> 
> When a structure is declared as `__attribute__ ((packed))' what should
> it alignment be? Unfortunately, I have not found clear statement in
> the
> docs, but I've found the following very inconsistent behavior instead.
> It is not clear to me to what extent `__attribute__ ((packed))' should
> break alignment rules.

It depends on platform ABI.

> Imagine the following code:
> 
> typedef struct {
>         unsigned char a;
>         unsigned char b;
>         unsigned short c;
>         unsigned int d;
>         unsigned long e;
> } __attribute__ ((packed)) s;
> 
> s x = {
>         .a = 1,
>         .b = 2,
>         .c = 3,
>         .d = 4,
>         .e = 5
> };

/* snip */

> Then, using `objdump -x test.o', I examine `.data.x` section
> Alignment:
> 
> A.   3 .data.x       00000010  0000000000000000  0000000000000000
> 00000040  2**3
> 
> B.   3 .data.x       00000010  0000000000000000  0000000000000000
> 00000040  2**3
> 
> C.   3 .data.x       00000010  0000000000000000  0000000000000000
> 00000040  2**3
> 
> D.   3 .data.x       00000010  0000000000000000  0000000000000000
> 00000040  2**0
> 
> 
> Note, that -Os on gcc 11 forces the Aligment to 1 byte. I've found
> this
> when tried to build some vendor tree of u-boot bootloader. Bumping
> version from gcc 7 to gcc 11 breaks the firmware image.
> 

The Arm Architecture ABI [says][1]:

> There is no minimum alignment required for a section. Sections
> containing code must be at least 4-byte aligned. Platform standards may
> set a limit on the maximum alignment that they can guarantee (normally
> the minimum page size supported by the platform).

So -fdata-sections won't introduce more alignment.

[And][2]:

> The member alignment of an element of a composite type is the
> alignment of that member after the application of any language
> alignment modifiers to that member

and

> The alignment of an aggregate shall be the alignment of its most-
> aligned member.

so the alignment of a packed struct is 1 byte (as each of its member is
1-byte aligned).

[1]:
https://github.com/ARM-software/abi-aa/blob/master/aaelf64/aaelf64.rst#section-alignment
[2]:
https://github.com/ARM-software/abi-aa/blob/f689c71830bb7687745a16ec4a0dd22126093266/aapcs64/aapcs64.rst#composite-types

-- 
Xi Ruoyao <xry111@xxxxxxxxxxxxxxxx>
School of Aerospace Science and Technology, Xidian University




[Index of Archives]     [Linux C Programming]     [Linux Kernel]     [eCos]     [Fedora Development]     [Fedora Announce]     [Autoconf]     [The DWARVES Debugging Tools]     [Yosemite Campsites]     [Yosemite News]     [Linux GCC]

  Powered by Linux