| Issue |
235243
|
| Summary |
[lld][COFF] lld-link crashes in fixTlsAlignment when _tls_used is absolute or in a section that is not output
|
| Labels |
lld
|
| Assignees |
|
| Reporter |
AnantaSrikar
|
I'm Ananta Srikar Puranam, a PhD student at Washington State University. I'm reporting this bug on behalf of my team: Emily West, Dr. Swarn Priya, Dr. Peng Gao and Dr. Basavesh Ammanaghatta Shivakumar.
When the link has a `.tls` section, `Writer::fixTlsAlignment` looks up `_tls_used` and does `ctx.getOutputSection(tlsSym->getChunk())`. It only *asserts* that the result is non-null, then uses it. `lld-link` crashes when `_tls_used` is a defined symbol without an output chunk:
1. `_tls_used` is an **absolute** symbol: `getChunk()` returns `nullptr`, and `getOutputSection(nullptr)` dereferences it.
2. `_tls_used` is in a section that is **not output** (here `IMAGE_SCN_LNK_INFO`): the chunk has no output section.
**Versions tested** (host x86_64 Linux, target x86_64-windows-msvc):
| | LLVM 23.1.4 (latest release, official `LLVM-23.1.4-Linux-X64.tar.zst`) | main c41dd6bb2d26 (Release + assertions) | LLD 21.1.8 (Ubuntu `lld-21`) |
|---|---|---|---|
| absolute `_tls_used` | SIGTRAP | `UNREACHABLE executed at lld/COFF/Symbols.h:500` ("unknown symbol kind") | SIGTRAP |
| `_tls_used` in an `IMAGE_SCN_LNK_INFO` section | SIGSEGV | `Assertion ... "no output section for _tls_used"` (`Writer.cpp:2987`) | SIGSEGV |
main was built from the Git repository with `-DCMAKE_BUILD_TYPE=Release -DLLVM_ENABLE_ASSERTIONS=ON -DLLVM_ENABLE_PROJECTS=lld`.
### Reproducer 1: absolute `_tls_used`
```sh
cat > abs.s <<'EOF'
.text
.globl main
main:
ret
.section .tls$,"dw"
.p2align 4
.quad 0
.globl _tls_used
_tls_used = 0x1000
EOF
clang --target=x86_64-windows-msvc -c abs.s -o abs.obj
lld-link /entry:main /subsystem:console /out:abs.exe abs.obj
```
Removing `_tls_used`, or defining it as a normal 40-byte TLS directory in `.rdata`, links fine.
### Reproducer 2: `_tls_used` in a section that is not output
```sh
cat > info2.yaml <<'EOF'
--- !COFF
header:
Machine: IMAGE_FILE_MACHINE_AMD64
Characteristics: [ ]
sections:
- Name: .text
Characteristics: [ IMAGE_SCN_CNT_CODE, IMAGE_SCN_MEM_EXECUTE, IMAGE_SCN_MEM_READ ]
Alignment: 4
SectionData: C3
- Name: '.tls$'
Characteristics: [ IMAGE_SCN_CNT_INITIALIZED_DATA, IMAGE_SCN_MEM_READ, IMAGE_SCN_MEM_WRITE ]
Alignment: 16
SectionData: '0000000000000000'
- Name: .rdata
Characteristics: [ IMAGE_SCN_CNT_INITIALIZED_DATA, IMAGE_SCN_MEM_READ, IMAGE_SCN_LNK_INFO ]
Alignment: 8
SectionData: '00000000000000000000000000000000000000000000000000000000000000000000000000000000'
symbols:
- Name: main
Value: 0
SectionNumber: 1
SimpleType: IMAGE_SYM_TYPE_NULL
ComplexType: IMAGE_SYM_DTYPE_NULL
StorageClass: IMAGE_SYM_CLASS_EXTERNAL
- Name: _tls_used
Value: 0
SectionNumber: 3
SimpleType: IMAGE_SYM_TYPE_NULL
ComplexType: IMAGE_SYM_DTYPE_NULL
StorageClass: IMAGE_SYM_CLASS_EXTERNAL
EOF
yaml2obj info2.yaml -o info2.obj
lld-link /entry:main /subsystem:console /out:info2.exe info2.obj
```
Without `IMAGE_SCN_LNK_INFO` it links.
### Observed
LLVM 23.1.4, reproducer 2, complete output:
```
PLEASE submit a bug report to https://github.com/llvm/llvm-project/issues/ and include the crash backtrace and instructions to reproduce the bug.
Stack dump:
0. Program arguments: /path/to/llvm-23.1.4/bin/lld-link /entry:main /subsystem:console /out:info2.exe info2.obj
#0 0x000064cb4f3c4a2a llvm::sys::PrintStackTrace(llvm::raw_ostream&, int) (/path/to/llvm-23.1.4/bin/lld-link+0x5d6ea2a)
#1 0x000064cb4f3c55cd SignalHandler(int, siginfo_t*, void*) Signals.cpp:0:0
#2 0x00007179af36acb0 (/usr/lib/x86_64-linux-gnu/libc.so.6+0x45cb0)
#3 0x000064cb4f4c4018 std::_Function_handler<void (lld::coff::SymbolTable&), (anonymous namespace)::Writer::run()::$_0>::_M_invoke(std::_Any_data const&, lld::coff::SymbolTable&) Writer.cpp:0:0
#4 0x000064cb4f4a8802 (anonymous namespace)::Writer::run() (.llvm.18234973360031551539) Writer.cpp:0:0
#5 0x000064cb4f4999d9 lld::coff::writeResult(lld::coff::COFFLinkerContext&) (/path/to/llvm-23.1.4/bin/lld-link+0x5e439d9)
#6 0x000064cb4f3fc359 lld::coff::LinkerDriver::linkerMain(llvm::ArrayRef<char const*>) (/path/to/llvm-23.1.4/bin/lld-link+0x5da6359)
#7 0x000064cb4f3ed7c2 lld::coff::link(llvm::ArrayRef<char const*>, llvm::raw_ostream&, llvm::raw_ostream&, bool, bool) (/path/to/llvm-23.1.4/bin/lld-link+0x5d977c2)
#8 0x000064cb4f3c83bf lld::unsafeLldMain(llvm::ArrayRef<char const*>, llvm::raw_ostream&, llvm::raw_ostream&, llvm::ArrayRef<lld::DriverDef>, bool) (/path/to/llvm-23.1.4/bin/lld-link+0x5d723bf)
#9 0x000064cb4ef5c4c3 lld_main(int, char**, llvm::ToolContext const&) (/path/to/llvm-23.1.4/bin/lld-link+0x59064c3)
#10 0x000064cb4ef5c613 main (/path/to/llvm-23.1.4/bin/lld-link+0x5906613)
#11 0x00007179af34f601 (/usr/lib/x86_64-linux-gnu/libc.so.6+0x2a601)
#12 0x00007179af34f718 __libc_start_main (/usr/lib/x86_64-linux-gnu/libc.so.6+0x2a718)
#13 0x000064cb4f1e0685 _start (/path/to/llvm-23.1.4/bin/lld-link+0x5b8a685)
Segmentation fault (core dumped)
```
The lambda in frame #3 is `ctx.forEachSymtab([&](SymbolTable &symtab) { fixTlsAlignment(symtab); })` in `Writer::run()`, with `fixTlsAlignment` inlined. Reproducer 1 ends in `Trace/breakpoint trap` on 23.1.4; on main, gdb shows it aborting from the same lambda.
main at c41dd6bb2d26 (Release + assertions):
```
# reproducer 1
unknown symbol kind
UNREACHABLE executed at lld/COFF/Symbols.h:500!
# reproducer 2
lld-link: lld/COFF/Writer.cpp:2987: void (anonymous namespace)::Writer::fixTlsAlignment(SymbolTable &): Assertion `sec && tlsSym->getRVA() >= sec->getRVA() && "no output section for _tls_used"' failed.
```
For reproducer 1, `Defined::getChunk()` does return `nullptr` for `DefinedAbsoluteKind`. The "unknown symbol kind" unreachable is probably the optimizer merging that path with the null dereference that follows, so the message is misleading.
### Expected
An error such as "_tls_used must be defined in a section that is included in the output", rather than a crash. `fixTlsAlignment` already reports `_tls_used sym is malformed` when the directory does not fit in its section.
### Analysis
`lld/COFF/Writer.cpp` at c41dd6bb2d26:
```cpp
void Writer::fixTlsAlignment(SymbolTable &symtab) {
Defined *tlsSym =
dyn_cast_or_null<Defined>(symtab.findUnderscore("_tls_used"));
if (!tlsSym)
return;
OutputSection *sec = ctx.getOutputSection(tlsSym->getChunk());
assert(sec && tlsSym->getRVA() >= sec->getRVA() &&
"no output section for _tls_used");
uint8_t *secBuf = buffer->getBufferStart() + sec->getFileOff();
```
`COFFLinkerContext::getOutputSection(const Chunk *c)` reads `c->osidx`, so a null chunk crashes there. A non-null chunk without an output section makes `sec` null, and `sec->getFileOff()` crashes. Turning the assert into a diagnostic, and checking the chunk for null first, would fix both. The same pattern (`ctx.getOutputSection(symtab.loadConfigSym->getChunk())`) is used for `_load_config_used` in `prepareLoadConfig`.
This report was drafted with help from an AI assistant (Claude); I reviewed it and reproduced it myself.
_______________________________________________
llvm-bugs mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-bugs