Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

To what extent does the Itanium ABI really specify padding and alignment?

Tags:

c++

gcc

abi

I've been told:

[ABIs] guarantee the exact layout of the struct, byte offset of every member, which bits are used for bit fields, where and how much padding there is, etc...

But I've always believed that padding and alignment were unspecified and unreliable.

Does the Itanium ABI (which GCC uses) in fact specify these things (as far as I can tell, it doesn't appear to beyond specifying ranges)?

And if it does, how do options like __attribute__ ((packed)) fit into that? Do they ultimately break the ABI by altering the alignment of things?

Or, as the quotee implies, is packing merely unspecified between toolchains but in fact reliable and predictable within the use of a certain ABI? And then, again, how does something like __attribute__ ((packed)) fit into that?

like image 904
Lightness Races in Orbit Avatar asked Nov 27 '12 10:11

Lightness Races in Orbit


2 Answers

padding and alignment are unspecified by the ISO standard C++ specification and hence unreliable.

The use of the word "exact" in the quote is exaggeration. The Itanium ABI (which GCC uses) may specify these things (as within specifying ranges) as needed rather than always and exact ly.

Packing is in fact reliable and predictable within the use of toolchains assuming predetermined target architecture & environment- viz. ABI. When all target specific details applicable to the tool-chain are predetermined __attribute__ ((packed)) like any other construct will generate output predictably for the tool-chain to work correctly.

like image 51
Chawathe Vipul S Avatar answered Oct 17 '22 14:10

Chawathe Vipul S


You need to follow the documentation to the standards incorporated by reference.

Most types' size and alignment is specified in the Intel System V ABI specifications; for example, long long and long double types' size and alignment are documented in the Unix System V Application Binary Interface, Itanium Processor Supplement (no. 245370-001), table 3-1 Additional Fundamental Data Types.

It's a little difficult to find authoritative copies of standards, especially as there isn't a real standards process. A good reference that includes the sizes and alignments of all types in the LP64 ABI is http://www.x86-64.org/documentation/abi.pdf although it doesn't cover the ILP32 ABI.

__attribute__((packed)) is of course gcc-specific, as is #pragma pack for MSVC. You may be able to use compiler compatibility features to get consistent layout of structs across compilers, but assuming that behavior is the same just because a compatibility feature is implemented is not a good idea; you should test that sizes and key offsets are the same.

like image 30
ecatmur Avatar answered Oct 17 '22 13:10

ecatmur