| Issue |
239191
|
| Summary |
[lld][COFF] lld-link crashes on a COMDAT section that also has IMAGE_SCN_LNK_REMOVE
|
| Labels |
lld:COFF
|
| 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.
`lld-link` segfaults when a COMDAT section also has `IMAGE_SCN_LNK_REMOVE`. `ObjFile::createDefined` installs the COMDAT leader as a `DefinedRegular` before `readSection` drops the section, so the leader stays in the symbol table with a null `data` pointer. Later consumers such as `LocalImportChunk::writeTo` and `markLive` dereference it.
**Version**: main at 22e6c38a528a (2026-11-05), built from the Git repository with `-DCMAKE_BUILD_TYPE=Release -DLLVM_ENABLE_ASSERTIONS=ON -DLLVM_ENABLE_PROJECTS=lld`, on x86_64 Linux.
### Reproducer
```sh
cat > remove.yaml <<'YAML'
--- !COFF
header:
Machine: IMAGE_FILE_MACHINE_AMD64
Characteristics: [ ]
sections:
- Name: .text
Characteristics: [ IMAGE_SCN_CNT_CODE, IMAGE_SCN_LNK_REMOVE, IMAGE_SCN_LNK_COMDAT, IMAGE_SCN_MEM_EXECUTE, IMAGE_SCN_MEM_READ ]
Alignment: 4
SectionData: '00000000'
Relocations:
- VirtualAddress: 0
SymbolName: __imp_f
Type: IMAGE_REL_AMD64_ADDR32NB
symbols:
- Name: .text
Value: 0
SectionNumber: 1
SimpleType: IMAGE_SYM_TYPE_NULL
ComplexType: IMAGE_SYM_DTYPE_NULL
StorageClass: IMAGE_SYM_CLASS_STATIC
SectionDefinition:
Length: 4
NumberOfRelocations: 1
NumberOfLinenumbers: 0
CheckSum: 0
Number: 0
Selection: IMAGE_COMDAT_SELECT_ANY
- Name: f
Value: 0
SectionNumber: 1
SimpleType: IMAGE_SYM_TYPE_NULL
ComplexType: IMAGE_SYM_DTYPE_FUNCTION
StorageClass: IMAGE_SYM_CLASS_EXTERNAL
- Name: __imp_f
Value: 0
SectionNumber: 0
SimpleType: IMAGE_SYM_TYPE_NULL
ComplexType: IMAGE_SYM_DTYPE_FUNCTION
StorageClass: IMAGE_SYM_CLASS_EXTERNAL
YAML
yaml2obj remove.yaml -o remove.obj
lld-link /dll /noentry /machine:x64 /out:remove.dll remove.obj
```
`f` is the COMDAT leader of `.text`, and the relocation's reference to `__imp_f` makes LLD create a local import chunk for it. Removing either flag from `.text` stops the crash. Without `IMAGE_SCN_LNK_REMOVE`, the same command links (status 0, with the usual LNK4217 warning). Without `IMAGE_SCN_LNK_COMDAT`, it exits with status 1 and `error: undefined symbol: f`.
### Observed
```
lld-link: warning: remove.obj: locally defined symbol imported: f (defined in remove.obj) [LNK4217]
PLEASE submit a bug report to https://github.com/llvm/llvm-project/issues/ and include the crash backtrace and instructions to reproduce the bug.
#4 lld::coff::LocalImportChunk::writeTo
#5 void llvm::function_ref<void (unsigned long)>::callback_fn<void llvm::parallelForEach<...(anonymous namespace)::Writer::writeSections()...>>
#6 llvm::parallelFor
#7 (anonymous namespace)::Writer::run()
#8 lld::coff::writeResult
#9 lld::coff::LinkerDriver::linkerMain
```
The process exits with SIGSEGV (status 139). In a larger fuzzed object where the leader has no local import but is reached from a live section, the same stale symbol crashes in `lld::coff::markLive` instead.
### Expected
The same result as a non-COMDAT section with `IMAGE_SCN_LNK_REMOVE` (`error: undefined symbol: f`), or another diagnostic, instead of a crash.
### Analysis
For a COMDAT leader, `createDefined` first adds the leader to the symbol table:
https://github.com/llvm/llvm-project/blob/22e6c38a528ad2c2c5f317c23c3436a313ce9fb9/lld/COFF/InputFiles.cpp#L921-L923
If it prevails, it reads the section. `readSection` returns null for `IMAGE_SCN_LNK_REMOVE`, and `createDefined` then returns before setting the leader's `data`:
https://github.com/llvm/llvm-project/blob/22e6c38a528ad2c2c5f317c23c3436a313ce9fb9/lld/COFF/InputFiles.cpp#L436-L437
https://github.com/llvm/llvm-project/blob/22e6c38a528ad2c2c5f317c23c3436a313ce9fb9/lld/COFF/InputFiles.cpp#L943-L950
The leader stays in the symbol table as a `DefinedRegular` whose `data` is null. `DefinedRegular::getRVA` and `getChunk` dereference it:
https://github.com/llvm/llvm-project/blob/22e6c38a528ad2c2c5f317c23c3436a313ce9fb9/lld/COFF/Symbols.h#L225-L226
`LocalImportChunk::writeTo` calls `getRVA`, and `markLive` calls `getChunk`:
https://github.com/llvm/llvm-project/blob/22e6c38a528ad2c2c5f317c23c3436a313ce9fb9/lld/COFF/Chunks.cpp#L950-L952
https://github.com/llvm/llvm-project/blob/22e6c38a528ad2c2c5f317c23c3436a313ce9fb9/lld/COFF/MarkLive.cpp#L56-L57
Checking `IMAGE_SCN_LNK_REMOVE` before `addComdat`, or undoing the leader when `readSection` returns null, would make the COMDAT case behave like the non-COMDAT case.
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