Is is safe to assume that (1) is not done?
Normally, the object file contains some information that the segment shall have some alignment (e.g. align to 16 bytes).
The linker will then add bytes (maybe NOP
or 0x00
) between the sections to ensure that the section is aligned correctly.
By inserting additional NOP
s you cannot achieve a "larger" alignment than the section's alignment: If the section is aligned to 16 bytes, you cannot align some code to 32 bytes by inserting NOP
s.
Therefore, the first byte in a section will always have the "best" alignment possible, and ...
Is is safe to assume that (1) is not done?
... adding NOP
s before the first function in the section only makes sense if you explicitly want to have the function "mis-aligned" (for example if the function shall be located at an address of the form 8*N+5
to ensure some instruction in the middle of the function is located at an 8-aligned address).
However, in such a case, a compiler would be allowed to add alignment bytes before a function.
(3) Alignment bytes after a function
Alignment bytes between two functions in the same section make sense.
Alignment bytes at the end of a section only make sense if there are restrictions on the size of the section (e.g. the section size must be a multiple of 16 or similar).
is it possible that the linker adds ... alignment bytes when linking
The linker normally even must add alignment bytes between the sections. But not inside a section.
is it possible that the linker ... removes alignment bytes when linking
Typically, the linker does not distinguish between alignment bytes and "useful" bytes. For this reason, it cannot remove alignment bytes.