Issue 239205
Summary [lld][COFF] lld-link crashes when a section's relocation table extends past the end of the file
Labels lld:COFF, crash
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 section's `NumberOfRelocations` makes its relocation table run past the end of the object file. `COFFObjectFile::getRelocations` then returns an `ArrayRef` with a null data pointer but the full relocation count, and LLD iterates over it.

**Version**: main at aa607d73918d (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

yaml2obj always writes the real relocation count, so the last step patches the low byte of `.text`'s `NumberOfRelocations` (file offset 0x34) from 1 to 90:

```sh
cat > relocs.yaml <<'YAML'
--- !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: E800000000C3
    Relocations:
      - VirtualAddress:  1
 SymbolName:      g
        Type: IMAGE_REL_AMD64_REL32
symbols:
  - Name:            f
    Value: 0
    SectionNumber:   1
    SimpleType:      IMAGE_SYM_TYPE_NULL
 ComplexType:     IMAGE_SYM_DTYPE_FUNCTION
    StorageClass: IMAGE_SYM_CLASS_EXTERNAL
  - Name:            g
    Value:           5
 SectionNumber:   1
    SimpleType:      IMAGE_SYM_TYPE_NULL
 ComplexType:     IMAGE_SYM_DTYPE_FUNCTION
    StorageClass: IMAGE_SYM_CLASS_EXTERNAL
YAML
yaml2obj relocs.yaml -o relocs.obj
printf '\132' | dd of=relocs.obj bs=1 seek=52 conv=notrunc
lld-link /dll /noentry /machine:x64 /out:relocs.dll relocs.obj
```

The 116-byte object now declares 90 relocations (900 bytes) starting at offset 0x42. Without the `dd` step, the same command links (status 0). Setting the count to 0xFFFF instead gives the same crash.

### Observed

The process exits with SIGSEGV (status 139):

```
 #4 lld::coff::markLive(lld::coff::COFFLinkerContext&)
 #5 lld::coff::LinkerDriver::linkerMain(llvm::ArrayRef<char const*>)
 #6 lld::coff::link(llvm::ArrayRef<char const*>, llvm::raw_ostream&, llvm::raw_ostream&, bool, bool)
```

With `/opt:noref`, `markLive` is skipped and it crashes later instead, in base relocation collection:

```
 #4 lld::coff::SectionChunk::getBaserels(std::vector<lld::coff::Baserel, std::allocator<lld::coff::Baserel>>*)
 #5 (anonymous namespace)::Writer::assignAddresses()
 #6 (anonymous namespace)::Writer::run()
```

An earlier UBSan build of main (2d194c60b8bf), on a 148-byte object with the same one-byte change, reports `lld/COFF/Chunks.h:315:62: runtime error: applying non-zero offset 900 to null pointer` before the crash.

### Expected

An error saying the section's relocation table is out of bounds, instead of a crash.

### Analysis

`getFirstReloc` checks that the relocation table fits in the file. If it doesn't, it consumes the error and returns null:

https://github.com/llvm/llvm-project/blob/aa607d73918d9fc2398f5a4ea804e8b3ec39d05d/llvm/lib/Object/COFFObjectFile.cpp#L387-L391

`getRelocations` pairs that null pointer with the unchecked count from the section header:

https://github.com/llvm/llvm-project/blob/aa607d73918d9fc2398f5a4ea804e8b3ec39d05d/llvm/lib/Object/COFFObjectFile.cpp#L1371-L1375

`SectionChunk` stores that `ArrayRef` as its relocations:

https://github.com/llvm/llvm-project/blob/aa607d73918d9fc2398f5a4ea804e8b3ec39d05d/lld/COFF/Chunks.cpp#L38-L39

`symbols()` then iterates from `nullptr` to `nullptr + relocsSize`, and `markLive` dereferences each element:

https://github.com/llvm/llvm-project/blob/aa607d73918d9fc2398f5a4ea804e8b3ec39d05d/lld/COFF/Chunks.h#L313-L316

https://github.com/llvm/llvm-project/blob/aa607d73918d9fc2398f5a4ea804e8b3ec39d05d/lld/COFF/MarkLive.cpp#L75-L77

`getBaserels` and every other user of `getRelocs()` sees the same null array. Either `getRelocations` could return an empty array (or an `Expected`) when the table is out of bounds, or LLD could check the table when it creates the `SectionChunk` and report an error.

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