Re: [patch] AVR: Sort .text.* input sections by their name.

Georg-Johann Lay via Binutils <[email protected]> Tue, 21 Jul 2026 13:32:44 +0200
Newsgroups gmane.comp.gnu.binutils
Message-ID <[email protected]>
Am 21.07.26 um 07:46 schrieb Alan Modra:
> On Mon, Jul 20, 2026 at 08:44:51PM +0200, Georg-Johann Lay via Binutils wrote:
>> This patch sorts the .text.* input sections by their name
>> in order to increase code locality, e.g. for code from
>> libgcc and AVR-LibC.  For example, libgcc puts their
>> multiplications and helpers in .text.libgcc.mul, but
>> without SORT the linker may locate functions related by
>> their input section far apart, inserting function from
>> unrelated input sections in between them.
> 
> While you may have found one particular case where sorting by name
> helps, I suspect this is generally not a good idea.  I believe in
> general you will make -ffunction-sections code locality worse by using
> this patch.  Consider an object file with lots of small functions,
> calls mostly within that object file.  You don't want to sort them
> such that functions from other object files separate them.

Maybe something like this would be more appropriate then?

     *(EXCLUDE_FILE(*/libgcc.a: */libm.a: */libc.a:) .text.*)
     */libgcc.a:(SORT(.text.*))
     */libc.a:(SORT(.text.*))
     */libm.a:(SORT(.text.*))

What I don't know is whether / works as a dir separator on
Window-ish hosts.  If not, maybe something like

     *(EXCLUDE_FILE(*[/\\]libgcc.a: *[/\\]libm.a: *[/\\]libc.a:) .text.*)

is the right patterns?  What I am also missing is a feature that
excludes specific input sections, like for example:

     .text.* !.text.foo.* .text.foo.bar .rodata.*

This would add .text.*, then remove .text.foo.*, and finally
add .text.foo.bar and .rodata.*.  Or am I missing some detail
in the ld docs? https://sourceware.org/binutils/docs/ld.html#Input-Section

Johann

>> 	PR ld/34415
>> ld/
>> 	* scripttempl/avr.sc (text): SORT *(.text.*) input sections.
>> 	by their name.
>>
>> diff --git a/ld/scripttempl/avr.sc b/ld/scripttempl/avr.sc
>> index b3cf421a78b..e918181700b 100644
>> --- a/ld/scripttempl/avr.sc
>> +++ b/ld/scripttempl/avr.sc
>> @@ -248,7 +248,7 @@ cat <<EOF
>>       KEEP (*(.init9))}
>>       *(.text)
>>       ${RELOCATING+. = ALIGN(2);
>> -    *(.text.*)
>> +    *(SORT(.text.*))
>>       . = ALIGN(2);
>>       *(.fini9)  /* _exit() starts here.  */
>>       KEEP (*(.fini9))