Issue 238930
Summary [lld][COFF] lld-link ICF crashes on a relocation that names an auxiliary symbol record
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.

`lld-link` crashes in ICF when a relocation's `SymbolTableIndex` is in range but names an auxiliary symbol record instead of a symbol. LLD leaves the `ObjFile::symbols` slots of auxiliary records null, and ICF passes that null to `dyn_cast<DefinedRegular>`. An assertions build fails the `dyn_cast` assertion; a release build segfaults.

**Version**: main at ca2d5db58df5 (2026-11-04), 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 > icf.yaml <<'YAML'
--- !COFF
header:
  Machine:         IMAGE_FILE_MACHINE_AMD64
 Characteristics: [ ]
sections:
  - Name:            '.text$a'
 Characteristics: [ IMAGE_SCN_CNT_CODE, IMAGE_SCN_LNK_COMDAT, IMAGE_SCN_MEM_EXECUTE, IMAGE_SCN_MEM_READ ]
    Alignment:       16
 SectionData:     '0000000000000000'
    Relocations:
      - VirtualAddress:  0
        SymbolTableIndex: 1
        Type: IMAGE_REL_AMD64_ADDR64
  - Name:            '.text$b'
    Characteristics: [ IMAGE_SCN_CNT_CODE, IMAGE_SCN_LNK_COMDAT, IMAGE_SCN_MEM_EXECUTE, IMAGE_SCN_MEM_READ ]
    Alignment:       16
    SectionData: '0000000000000000'
    Relocations:
      - VirtualAddress:  0
 SymbolName:      abs
        Type: IMAGE_REL_AMD64_ADDR64
symbols:
  - Name:            '.text$a'
    Value: 0
    SectionNumber:   1
    SimpleType: IMAGE_SYM_TYPE_NULL
    ComplexType:     IMAGE_SYM_DTYPE_NULL
 StorageClass:    IMAGE_SYM_CLASS_STATIC
    SectionDefinition:
 Length:          8
      NumberOfRelocations: 1
      NumberOfLinenumbers: 0
      CheckSum:        0
      Number:          1
      Selection: IMAGE_COMDAT_SELECT_NODUPLICATES
  - Name:            '.text$b'
    Value: 0
    SectionNumber:   2
    SimpleType: IMAGE_SYM_TYPE_NULL
    ComplexType:     IMAGE_SYM_DTYPE_NULL
 StorageClass:    IMAGE_SYM_CLASS_STATIC
    SectionDefinition:
 Length:          8
      NumberOfRelocations: 1
      NumberOfLinenumbers: 0
      CheckSum:        0
      Number:          2
      Selection: IMAGE_COMDAT_SELECT_NODUPLICATES
  - Name:            f1
    Value: 0
    SectionNumber:   1
    SimpleType:      IMAGE_SYM_TYPE_NULL
 ComplexType:     IMAGE_SYM_DTYPE_FUNCTION
    StorageClass: IMAGE_SYM_CLASS_EXTERNAL
  - Name:            f2
    Value:           0
 SectionNumber:   2
    SimpleType:      IMAGE_SYM_TYPE_NULL
 ComplexType:     IMAGE_SYM_DTYPE_FUNCTION
    StorageClass: IMAGE_SYM_CLASS_EXTERNAL
  - Name:            abs
    Value:           0
 SectionNumber:   -1
    SimpleType:      IMAGE_SYM_TYPE_NULL
 ComplexType:     IMAGE_SYM_DTYPE_NULL
    StorageClass: IMAGE_SYM_CLASS_EXTERNAL
YAML
yaml2obj icf.yaml -o icf.obj
lld-link /dll /noentry /machine:x64 /export:f1 /export:f2 /out:icf.dll icf.obj
```

`.text$a` and `.text$b` are identical COMDAT code sections, so ICF compares them. `.text$a`'s relocation names symbol-table index 1, which is the auxiliary (section definition) record of the `.text$a` section symbol. `.text$b`'s relocation names the absolute symbol `abs`. With `SymbolTableIndex: 6` (`abs`) instead of 1, the same command links (status 0). With `/opt:noicf`, the original input exits with status 1 and `error: relocation against symbol in discarded section`.

### Observed

```
lld-link: llvm/include/llvm/Support/Casting.h:656: decltype(auto) llvm::dyn_cast(From *) [To = lld::coff::DefinedRegular, From = lld::coff::Symbol]: Assertion `detail::isPresent(Val) && "dyn_cast on a non-existent value"' failed.
```

```
#8  __assert_fail
#9  (no symbol name)
#10 bool lld::coff::ICF::sectionsEqual<true>(lld::coff::SectionChunk const*, lld::coff::SectionChunk const*)
#11 std::__find_if<...ICF::segregate<true>(unsigned long, unsigned long)...>
#12 std::__stable_partition<...ICF::segregate<true>(unsigned long, unsigned long)...>
#13 (no symbol name)
#14 lld::coff::ICF::forEachClassRange(unsigned long, unsigned long, std::function<void (unsigned long, unsigned long)>)
#15 lld::coff::ICF::forEachClass(std::function<void (unsigned long, unsigned long)>)
#16 lld::coff::ICF::run()
#17 lld::coff::doICF(lld::coff::COFFLinkerContext&)
#18 lld::coff::LinkerDriver::linkerMain(llvm::ArrayRef<char const*>)
```

### Expected

An error for the malformed relocation (for example "relocation refers to an auxiliary symbol record"), instead of a crash.

### Analysis

`ObjFile::initializeSymbols` sizes `symbols` to the raw record count and steps over auxiliary records, so their slots stay null:

https://github.com/llvm/llvm-project/blob/ca2d5db58df57bc189d932aea34317c4382dd98d/lld/COFF/InputFiles.cpp#L587-L588

https://github.com/llvm/llvm-project/blob/ca2d5db58df57bc189d932aea34317c4382dd98d/lld/COFF/InputFiles.cpp#L649

`ObjFile::getSymbol` returns the slot without a check:

https://github.com/llvm/llvm-project/blob/ca2d5db58df57bc189d932aea34317c4382dd98d/lld/COFF/InputFiles.h#L170-L172

ICF's `eqReloc` passes it to `eqSym`, which calls `dyn_cast<DefinedRegular>` on it:

https://github.com/llvm/llvm-project/blob/ca2d5db58df57bc189d932aea34317c4382dd98d/lld/COFF/ICF.cpp#L162-L167

https://github.com/llvm/llvm-project/blob/ca2d5db58df57bc189d932aea34317c4382dd98d/lld/COFF/ICF.cpp#L149-L155

The initial hash pass in `ICF::run` already uses `dyn_cast_or_null` for the same symbols, and so do other relocation consumers such as `SectionChunk::applyRelocation`:

https://github.com/llvm/llvm-project/blob/ca2d5db58df57bc189d932aea34317c4382dd98d/lld/COFF/ICF.cpp#L271-L274

Using `dyn_cast_or_null` in `eqSym` would avoid the crash in ICF. Rejecting relocations that name auxiliary records when the object is read would catch other consumers too.

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

Reply via email to