[PATCH v2 1/2] elf: Do not over-align __FRAME_END__ in sofini.c

Florian Weimer fweimer@redhat.com
Mon Nov 2 14:10:29 GMT 2026


* Adrian Ratiu:

> On Mon, 02 Nov 2026, Andreas Schwab <schwab@suse.de> wrote:
>> On Okt 30 2026, Florian Weimer wrote:
>>
>>> * Adrian Ratiu:
>>>
>>>> __FRAME_END__ has no explicit alignment, so on targets where GCC's
>>>> DATA_ALIGNMENT raises the alignment of arrays (e.g. on aarch64,
>>>> loongarch64 and mips64) it gets 8-byte alignment.
>>>>
>>>> The .eh_frame section of sofini.os then has sh_addralign 8 while the
>>>> one of crtendS.o has 4, and GNU ld may insert 4 bytes of zero padding
>>>> before the terminator. The padding reads as a zero length so the result
>>>> still works, but it should not be relied upon.
>>>>
>>>> So we set the alignment explicitly, as libgcc's crtstuff.c does (see
>>>> GCC commit 51084e13c01, "Don't overalign if DECL_USER_ALIGN").
>>>>
>>>> Suggested-by: Adhemerval Zanella <adhemerval.zanella@linaro.org>
>>>> Signed-off-by: Adrian Ratiu <adrian.ratiu@collabora.com>
>>>> ---
>>>>  elf/sofini.c | 8 +++++---
>>>>  1 file changed, 5 insertions(+), 3 deletions(-)
>>>>
>>>> diff --git a/elf/sofini.c b/elf/sofini.c
>>>> index 1c526fd603..1dae5ccc67 100644
>>>> --- a/elf/sofini.c
>>>> +++ b/elf/sofini.c
>>>> @@ -1,7 +1,9 @@
>>>>  /* Terminate the frame unwind info section with a 4byte 0 as a sentinel;
>>>> -   this would be the 'length' field in a real FDE.  */
>>>> -
>>>> +   this would be the 'length' field in a real FDE.  PT_GNU_EH_FRAME only
>>>> +   provides the start of .eh_frame, so unwinders that scan it linearly
>>>> +   need the sentinel even when there is an .eh_frame_hdr.  Set the alignment
>>>> +   explicitly so that the compiler does not over-align the array.  */
>>>>  typedef unsigned int ui32 __attribute__ ((mode (SI)));
>>>>  static const ui32 __FRAME_END__[1]
>>>> -  __attribute__ ((used, section (".eh_frame")))
>>>> +  __attribute__ ((used, section (".eh_frame"), aligned (__alignof__ (ui32))))
>>>>    = { 0 };
>>>
>>> Please write 4 if you want 4-byte alignment.  SImode alignment on m68k
>>> is 2, I think.
>>
>> I don't think underalignment would be a problem.
>
> I agree with Andreas, a lower alignment doesn't add any padding
> (over-alignment padding is the problem) and GCC has been doing this for
> decades without any issues.

Fair enough.  This wasn't clear to me based on the commit message.

Thanks,
Florian



More information about the Libc-alpha mailing list