Issue 235244
Summary [lld][COFF] Out-of-range weak external TagIndex is used unchecked (lld-link crashes)
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` indexes its symbol vector with a weak external's auxiliary `TagIndex` without checking it against the number of symbols. With a `TagIndex` past the end of the symbol table:

- an assertions build fails the `std::vector::operator[]` bounds assertion while reading the object;
- release builds store a garbage pointer as the weak alias target and segfault later in `Undefined::getWeakAlias()`.

**Versions tested** (host x86_64 Linux, target x86_64-windows-msvc):

- LLVM 23.1.4, the latest release: official `LLVM-23.1.4-Linux-X64.tar.zst` from the GitHub release page. SIGSEGV.
- main at c41dd6bb2d26 (2026-10-22), built from the Git repository with `-DCMAKE_BUILD_TYPE=Release -DLLVM_ENABLE_ASSERTIONS=ON -DLLVM_ENABLE_PROJECTS=lld`. Assertion failure.
- LLD 21.1.8 (Ubuntu `lld-21` package): SIGSEGV.

### Reproducer

```sh
cat > t.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:       16
    SectionData: C3
symbols:
  - Name:            main
    Value:           0
 SectionNumber:   1
    SimpleType:      IMAGE_SYM_TYPE_NULL
 ComplexType:     IMAGE_SYM_DTYPE_FUNCTION
    StorageClass: IMAGE_SYM_CLASS_EXTERNAL
  - Name:            foo
    Value:           0
 SectionNumber:   0
    SimpleType:      IMAGE_SYM_TYPE_NULL
 ComplexType:     IMAGE_SYM_DTYPE_NULL
    StorageClass: IMAGE_SYM_CLASS_WEAK_EXTERNAL
    WeakExternal:
      TagIndex:        3
 Characteristics: IMAGE_WEAK_EXTERN_SEARCH_ALIAS
EOF
yaml2obj t.yaml -o t.obj
lld-link /entry:main /subsystem:console /out:t.exe t.obj
```

The object has three symbol table records (`main`, `foo` and `foo`'s auxiliary record), so `TagIndex: 3` is one past the end.

| `TagIndex` | 23.1.4 / 21.1.8 | main (assertions) |
|---|---|---|
| 0 (`main`) | links | links |
| 2 (`foo`'s aux record) | `error: undefined symbol: foo` | `error: undefined symbol: foo` |
| 3 (one past end) | SIGSEGV | assertion failure |
| 1000 | `error: undefined symbol: foo` (reads garbage) | assertion failure |
| 0xffffffff | SIGSEGV | assertion failure |

### Observed

LLVM 23.1.4 (release build), 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:t.exe t.obj
 #0 0x00005a76abbb5a2a llvm::sys::PrintStackTrace(llvm::raw_ostream&, int) (/path/to/llvm-23.1.4/bin/lld-link+0x5d6ea2a)
 #1 0x00005a76abbb65cd SignalHandler(int, siginfo_t*, void*) Signals.cpp:0:0
 #2 0x000071430d260cb0 (/usr/lib/x86_64-linux-gnu/libc.so.6+0x45cb0)
 #3 0x00005a76abc8a119 lld::coff::Undefined::getWeakAlias() (/path/to/llvm-23.1.4/bin/lld-link+0x5e43119)
 #4 0x00005a76abc6ac01 lld::coff::SymbolTable::resolveRemainingUndefines(std::vector<lld::coff::Undefined*, std::allocator<lld::coff::Undefined*>>&) (/path/to/llvm-23.1.4/bin/lld-link+0x5e23c01)
 #5 0x00005a76abbebfec lld::coff::LinkerDriver::linkerMain(llvm::ArrayRef<char const*>) (/path/to/llvm-23.1.4/bin/lld-link+0x5da4fec)
 #6 0x00005a76abbde7c2 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)
 #7 0x00005a76abbb93bf 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)
 #8 0x00005a76ab74d4c3 lld_main(int, char**, llvm::ToolContext const&) (/path/to/llvm-23.1.4/bin/lld-link+0x59064c3)
 #9 0x00005a76ab74d613 main (/path/to/llvm-23.1.4/bin/lld-link+0x5906613)
#10 0x000071430d245601 (/usr/lib/x86_64-linux-gnu/libc.so.6+0x2a601)
#11 0x000071430d245718 __libc_start_main (/usr/lib/x86_64-linux-gnu/libc.so.6+0x2a718)
#12 0x00005a76ab9d1685 _start (/path/to/llvm-23.1.4/bin/lld-link+0x5b8a685)
Segmentation fault (core dumped)
```

main at c41dd6bb2d26 (Release + assertions):

```
/usr/include/c++/16/bits/stl_vector.h:1253: reference std::vector<lld::coff::Symbol *>::operator[](size_type) [_Tp = lld::coff::Symbol *, _Alloc = std::allocator<lld::coff::Symbol *>]: Assertion '__n < this->size()' failed.
```

<details><summary>Backtrace of the assertions build (gdb)</summary>

```
#6 lld::coff::ObjFile::initializeSymbols()
#7  lld::coff::ObjFile::parse()
#8 lld::coff::LinkerDriver::addFile(lld::coff::InputFile*)
#9 lld::coff::LinkerDriver::addObjectFile(lld::coff::COFFLinkerContext&, llvm::MemoryBufferRef, llvm::StringRef, unsigned long, bool)
#10 lld::coff::LinkerDriver::addBuffer(std::unique_ptr<llvm::MemoryBuffer, std::default_delete<llvm::MemoryBuffer> >, bool, bool)
#11 std::_Function_handler<void (), lld::coff::LinkerDriver::enqueuePath(llvm::StringRef, bool, lld::coff::LinkerDriver::InputOpt)::$_0>::_M_invoke(std::_Any_data const&)
#12 lld::coff::LinkerDriver::run()
#13 lld::coff::LinkerDriver::linkerMain(llvm::ArrayRef<char const*>)
#14 lld::coff::link(llvm::ArrayRef<char const*>, llvm::raw_ostream&, llvm::raw_ostream&, bool, bool)
```

</details>

### Expected

An error naming the object and the bad weak external, for example "invalid TagIndex in weak external foo", instead of a crash.

### Analysis

`ObjFile::initializeSymbols` in `lld/COFF/InputFiles.cpp` at c41dd6bb2d26 (line 672):

```cpp
  for (auto &kv : weakAliases) {
 Symbol *sym = kv.first;
    const coff_aux_weak_external *aux = kv.second;
 checkAndSetWeakAlias(symtab, this, sym, symbols[aux->TagIndex],
 aux->Characteristics ==
 IMAGE_WEAK_EXTERN_ANTI_DEPENDENCY);
  }
```

`aux->TagIndex` comes straight from the file. A few lines earlier, the ARM64EC anti-dependency path reads the same index through `check(coffObj->getSymbol(aux->TagIndex))`, which does validate it. The generic path above does not. Checking `aux->TagIndex < symbols.size()` (and that the slot is non-null; `TagIndex: 2`, an auxiliary record, appears to hit a null slot and only reports an undefined symbol) before the call would turn this into a diagnostic.

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