Input sections .ctors/.ctors.N may go to either the output section .init_array or the output section .ctors:
- output .ctors: currently we sort them by name. This patch changes to sort by priority from high to low. If N in .ctors.N is in the form of %05u, there is no semantic difference. Actually GCC and Clang do use %05u. (In the test ctors_dtors_priority.s and Gold's test gold/testsuite/script_test_14.s, we can see %03u, but they are not really produced by compilers.)
- output .init_array: users can provide an input section description SORT_BY_INIT_PRIORITY(.init_array.* .ctors.*) to mix .init_array.* and .ctors.*. This can make .init_array.N and .ctors.(65535-N) interchangeable.
With this change, users can mix .ctors.N and .init_array.N in .init_array (PR44698 and PR48096) with linker scripts. As an example:
SECTIONS { .init_array : { *(SORT_BY_INIT_PRIORITY(.init_array.* .ctors.*)) *(.init_array EXCLUDE_FILE (*crtbegin.o *crtbegin?.o *crtend.o *crtend?.o ) .ctors) } } INSERT AFTER .fini_array; SECTIONS { .fini_array : { *(SORT_BY_INIT_PRIORITY(.fini_array.* .dtors.*)) *(.fini_array EXCLUDE_FILE (*crtbegin.o *crtbegin?.o *crtend.o *crtend?.o ) .dtors) } } INSERT BEFORE .init_array;
I'm not sure this is right. As I understand it compCtors will only be called when not using a linker script.
In that case we'd have a separate .ctors and .dtors section to an existing .init_array section and in this case the rules for sorting ctors and dtors are different i.e. the default is sort by name (see comment above). It looks like this bit of code will change the default linker script to the equivalent of:
from:
I think the old way was closer to BFD as that only changes to SORT_BY_INIT_PRIORITY if .ctors are placed in the .init_array OutputSection.