Comment 14 for bug 1608882

Revision history for this message
Thomas Preud'homme (thomas-preudhomme) wrote :

Hi JiriJ,

TL;DR: the compiler has more alignment information for object in the file it's compiling than for objects in other files (declared with extern keyword).

From elf (5) manual:

"All data structures that the file format defines follow the "natural" size and alignment guidelines for the relevant class. If necessary, data structures contain explicit padding to ensure 4-byte alignment for 4-byte objects, to force structure sizes to a multiple of 4, etc."

This means the compiler can rely on the fact that the first object in a section will be 4byte aligned in the final executable so the compilers know the structure in test.c has the proper alignment. For the structure defined elsewhere, it cannot know where the structure will be in the object file that defines it: it could be at a non aligned address and thus be non aligned once linked into an executable.

Best regards.