SORT and multiple patterns in an input section description

Fangrui Song i@maskray.me
Wed Nov 11 06:44:44 GMT 2020


On 2020-11-11, Alan Modra wrote:
>On Mon, Nov 09, 2020 at 08:16:55PM -0800, Fangrui Song wrote:
>> I am trying to understand the order of input sections within an input
>> section description.
>>
>> For .foo : { *(.a .b .c) }
>> the order in the output section matches the input order. If
>> file1 has .c .b .a, the order will be .c .b .a
>>
>>
>> For .abc : { *(SORT(.foo.*) .bar.*) } and
>>
>>   .section .bar.4,"a"
>>    .quad 4
>>   .section .foo.2,"a"
>>    .quad 2
>>   .section .foo.3,"a"
>>    .quad 3
>>   .section .bar.6,"a"
>>    .quad 6
>>   .section .bar.5,"a"
>>    .quad 5
>>   .section .foo.1,"a"
>>    .quad 1
>>
>> the order is 412365, i.e. ld apparently moves .foo.1 with the .foo.2/.foo.3 group.
>> This is sorta surprising. Is there a rule explaining this behavior?
>
>No, and I don't want to try to explain it, except to say that "*(A B)"
>is quite deliberately different from "*(A) *(B)", and that difference
>is explained in the ld info doc.  Use the source if you want more
>detail.

:(

>> A more complex example is
>> .init_array : { KEEP (*(SORT_BY_INIT_PRIORITY(.init_array.*) SORT_BY_INIT_PRIORITY(.ctors.*))) }
>>
>> I expected that all .init_array.* precede all .ctors.*
>
>If that was your expectation, then you would write
>
>.init_array : { KEEP (*(SORT_BY_INIT_PRIORITY(.init_array.*)))
>                KEEP (*(SORT_BY_INIT_PRIORITY(.ctors.*))) }

My current observation for the section patterns:

* The first SORT* is respected.
* Other SORT* are as if merged with the first SORT* (ok, I can accept this, though not too happy with the peculiarity..)
* Other SORT*'s sorting methods are ignored (this is very confusing)

So
KEEP (*(SORT_BY_INIT_PRIORITY(.init_array.*) SORT_BY_INIT_PRIORITY(.ctors.*))) 
conceptually may work like
KEEP (*(SORT_BY_INIT_PRIORITY(.init_array.* .ctors.*))) (the syntax is not accepted)

KEEP (*(SORT_BY_INIT_PRIORITY(.init_array.*) SORT_BY_ALIGNMENT(.ctors.*))) 
and
KEEP (*(SORT_BY_INIT_PRIORITY(.init_array.*) SORT(.ctors.*))) 
have the same effect...

Am I right?

My takeaway is: don't use more than one SORT in one input section description.

>-- 
>Alan Modra
>Australia Development Lab, IBM


More information about the Binutils mailing list