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;