SORT and multiple patterns in an input section description

Fangrui Song
Tue Nov 10 04:16:55 GMT 2020

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?

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.*
apparently ld actually does something similar to SORT_BY_INIT_PRIORITY(.init_array.* .ctors.*)  (the syntax is not accepted)

Now I wonder what
*(SORT_BY_INIT_PRIORITY(.init_array.*) SORT(.ctors.*))

More information about the Binutils mailing list