An empty output section specified in the `SECTIONS` command (e.g.
`empty : { *(empty) }`) may be discarded. Due to phase ordering, we
might define `__start_empty`/`__stop_empty` symbols with incorrect
section indexes (usually benign, but could go out of bounds and cause
`readelf -s` to print `BAD`).
```
finalizeSections
addStartStopSymbols // __start_empty is defined
// __start_empty is added to .symtab
sortSections
adjustOutputSections // `empty` is discarded
writeSections
// __start_empty is Defined with an invalid section index
```
Loaders use `st_value` members of the start/stop symbols and expect no
"undefined symbol" linker error, but do not particularly care whether
the symbols are defined or undefined. Let's retain the associated empty
output section so that start/stop symbols will have correct section
indexes.
The approach allows us to remove `LinkerScript::isDiscarded`
(https://reviews.llvm.org/D114179). Also delete the
`findSection(".text")` special case from https://reviews.llvm.org/D46200,
which is unnecessary even before this patch (`elfHeader` would be fine
even with very large executables).
Note: we should be careful not to unnecessarily retain .ARM.exidx, which
would create an empty PT_ARM_EXIDX. ~40 tests would need to be updated.
---
An alternative is to discard the empty output section and keep the
start/stop symbols undefined. This approach needs more code and requires
`LinkerScript::isDiscarded` before we discard empty sections in
``adjustOutputSections`.
Pull Request: https://github.com/llvm/llvm-project/pull/96343
37 lines
1.1 KiB
Plaintext
37 lines
1.1 KiB
Plaintext
# REQUIRES: x86
|
|
## __start/__stop symbols retain the associated empty sections with C identifier names.
|
|
|
|
# RUN: rm -rf %t && split-file %s %t
|
|
# RUN: llvm-mc -filetype=obj -triple=x86_64 %t/test.s -o %t.o
|
|
# RUN: ld.lld -T %t/ldscript -o %t.out %t.o -z start-stop-visibility=default
|
|
# RUN: llvm-objdump -h -t %t.out | FileCheck %s
|
|
|
|
# CHECK: .text
|
|
# CHECK-NEXT: empty1
|
|
# CHECK-NEXT: empty2
|
|
# CHECK-NEXT: empty3
|
|
|
|
# CHECK: [[#%x,ADDR:]] l empty1 0000000000000000 .hidden __start_empty1
|
|
# CHECK-NEXT: {{0*}}[[#ADDR]] g empty2 0000000000000000 .protected __stop_empty2
|
|
# CHECK-NEXT: {{0*}}[[#ADDR]] g empty3 0000000000000000 __stop_empty3
|
|
|
|
#--- ldscript
|
|
SECTIONS {
|
|
.text : { *(.text .text.*) }
|
|
empty0 : { *(empty0) }
|
|
empty1 : { *(empty1) }
|
|
empty2 : { *(empty2) }
|
|
empty3 : { *(empty3) }
|
|
}
|
|
|
|
#--- test.s
|
|
.weak __start_empty1, __stop_empty2, __stop_empty3
|
|
.hidden __start_empty1
|
|
.protected __stop_empty2
|
|
|
|
.globl _start
|
|
_start:
|
|
movq __start_empty1@GOTPCREL(%rip),%rax
|
|
movq __stop_empty2@GOTPCREL(%rip),%rax
|
|
movq __stop_empty3@GOTPCREL(%rip),%rax
|