Issue 239406
Summary [lld][COFF] lld-link reaches cantFail on a section whose raw data 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.

`lld-link` hits `llvm_unreachable` through `cantFail` when a section's `PointerToRawData + SizeOfRawData` runs past the end of the object file. `COFFObjectFile::getSectionContents` returns an error for that range, and `SectionChunk::getContents` wraps the call in `cantFail`. An assertions build aborts. Without assertions, the unreachable is undefined behavior. The release build I tried links the object without any diagnostic and writes the section as zeros.

**Version**: main at 4be997b13681 (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 `SizeOfRawData` for a section with data, so the last step patches byte 1 of `.text`'s `SizeOfRawData` (file offset 0x25). That changes it from 1 to 4097:

```sh
cat > size.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:     C3
symbols:
  - Name:            f
    Value: 0
    SectionNumber:   1
    SimpleType:      IMAGE_SYM_TYPE_NULL
 ComplexType:     IMAGE_SYM_DTYPE_FUNCTION
    StorageClass: IMAGE_SYM_CLASS_EXTERNAL
YAML
yaml2obj size.yaml -o size.obj
printf '\020' | dd of=size.obj bs=1 seek=37 conv=notrunc
lld-link /dll /noentry /machine:x64 /out:size.dll size.obj
```

The object is 83 bytes, and `.text` starts at offset 0x3c. Without the `dd` step, the same command links (status 0). The boundary is exact: `SizeOfRawData` 23, which ends at EOF, links, and 24 aborts.

### Observed

```
Failure value returned from cantFail wrapped call
The end of the file was unexpectedly encountered
UNREACHABLE executed at llvm/include/llvm/Support/Error.h:779!
```

```
lld::coff::SectionChunk::writeTo(unsigned char*) const
void llvm::function_ref<void (unsigned long)>::callback_fn<...parallelForEach<...>...>
(anonymous namespace)::Writer::run()
lld::coff::writeResult(lld::coff::COFFLinkerContext&)
lld::coff::LinkerDriver::linkerMain(llvm::ArrayRef<char const*>)
```

A release build (no assertions) exits 0 and writes a DLL whose `.text` is 4097 zero bytes, so the section's real `C3` byte is dropped.

### Expected

A diagnostic that the section's data extends past the end of the file, instead of an abort or a silently wrong output.

### Analysis

`getSectionContents` checks the range and returns an error when it doesn't fit:

https://github.com/llvm/llvm-project/blob/4be997b13681161f2848687565018ee06c5c23f5/llvm/lib/Object/COFFObjectFile.cpp#L1298-L1314

`SectionChunk::getContents` treats that call as infallible:

https://github.com/llvm/llvm-project/blob/4be997b13681161f2848687565018ee06c5c23f5/lld/COFF/Chunks.cpp#L772-L776

`cantFail` sends the error to `llvm_unreachable`:

https://github.com/llvm/llvm-project/blob/4be997b13681161f2848687565018ee06c5c23f5/llvm/include/llvm/Support/Error.h#L769-L781

Nothing checks the section's raw-data range earlier, so the first check happens while writing the output. One fix is to check each section's range in `ObjFile` when the section chunk is created (`readSection`) and report a malformed-file error there. Then `getContents` can keep its `cantFail`.

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