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;
```
Reviewed By: psmith
Differential Revision: https://reviews.llvm.org/D91187
44 lines
914 B
ArmAsm
44 lines
914 B
ArmAsm
# REQUIRES: x86
|
|
## Test SORT_BY_INIT_PRIORITY can be used to convert .ctors into .init_array
|
|
|
|
# RUN: split-file %s %t
|
|
# RUN: llvm-mc -filetype=obj -triple=x86_64 %t/asm -o %t.o
|
|
# RUN: ld.lld -T %t/lds %t.o -o %t.out
|
|
# RUN: llvm-readelf -x .init_array %t.out | FileCheck %s
|
|
|
|
# CHECK: Hex dump of section '.init_array':
|
|
# CHECK-NEXT: 0x00000001 00010203 04050607
|
|
|
|
#--- asm
|
|
.globl _start
|
|
_start:
|
|
nop
|
|
|
|
.section foo, "aw", @init_array
|
|
.byte 5
|
|
|
|
.section .ctors.65435, "a"
|
|
.byte 3
|
|
.section .init_array.100, "aw", @init_array
|
|
.byte 4
|
|
|
|
.section .init_array.7, "aw", @init_array
|
|
.byte 2
|
|
.section .ctors.65529,"a"
|
|
.byte 1
|
|
.section .init_array.5, "aw", @init_array
|
|
.byte 0
|
|
|
|
.section .init_array, "aw", @init_array
|
|
.byte 6
|
|
.section .ctors, "a"
|
|
.byte 7
|
|
|
|
#--- lds
|
|
SECTIONS {
|
|
.init_array : {
|
|
*(SORT_BY_INIT_PRIORITY(.init_array.* .ctors.*) SORT_BY_INIT_PRIORITY(foo*))
|
|
*(.init_array .ctors)
|
|
}
|
|
}
|