SORT and multiple patterns in an input section description

Alan Modra amodra@gmail.com
Wed Nov 11 00:56:28 GMT 2020


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.*))) }

-- 
Alan Modra
Australia Development Lab, IBM


More information about the Binutils mailing list