| Issue |
239490
|
| Summary |
[lld][COFF] lld-link asserts on the reserved section alignment code 0xF
|
| 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.
The COFF section alignment field (bits 20–23 of `Characteristics`) defines codes 1 to 0xE, from 1 byte to 8192 bytes. Code 0xF is reserved. `coff_section::getAlignment` turns 0xF into 16384, and LLD passes that to `Chunk::setAlignment`, which asserts the alignment is at most 8192. An assertions build of `lld-link` aborts while reading the object. Release builds accept it without a diagnostic.
**Version**: main at 1d87294ea505 (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 won't write alignment code 0xF ("section alignment is too large"), so the reproducer writes 8192 (code 0xE) and patches the nibble. Byte 0x3a is the third byte of `.text`'s `Characteristics`, and the patch changes it from 0xE0 to 0xF0:
```sh
cat > align.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: 8192
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 align.yaml -o align.obj
printf '\360' | dd of=align.obj bs=1 seek=58 conv=notrunc
lld-link /dll /noentry /machine:x64 /out:align.dll align.obj
```
`Characteristics` changes from 0x60E00020 to 0x60F00020. Without the `dd` step (code 0xE, 8192 bytes), the same command links (status 0).
### Observed
```
lld-link: lld/COFF/Chunks.h:82: void lld::coff::Chunk::setAlignment(uint32_t): Assertion `p2Align <= Log2MaxSectionAlignment && "impossible requested alignment"' failed.
```
```
lld::coff::SectionChunk::SectionChunk(lld::coff::ObjFile*, llvm::object::coff_section const*, lld::coff::Chunk::Kind)
lld::coff::ObjFile::readSection(unsigned int, llvm::object::coff_aux_section_definition const*, llvm::StringRef)
lld::coff::ObjFile::initializeChunks()
lld::coff::ObjFile::parse()
lld::coff::LinkerDriver::addFile(lld::coff::InputFile*)
```
### Expected
An error for the reserved alignment code (or a defined fallback, as for code 0, which means 16), instead of an assertion failure.
### Analysis
The defined codes stop at `IMAGE_SCN_ALIGN_8192BYTES` (0xE), while the mask covers all four bits:
https://github.com/llvm/llvm-project/blob/1d87294ea505b29a981789590727f248679ba421/llvm/include/llvm/BinaryFormat/COFF.h#L328-L329
`getAlignment` converts any nonzero code to `1 << (code - 1)`, so 0xF becomes 16384:
https://github.com/llvm/llvm-project/blob/1d87294ea505b29a981789590727f248679ba421/llvm/include/llvm/Object/COFF.h#L468-L480
The `SectionChunk` constructor passes it on unchecked:
https://github.com/llvm/llvm-project/blob/1d87294ea505b29a981789590727f248679ba421/lld/COFF/Chunks.cpp#L50
`setAlignment` relies on an assertion to enforce the 8192 limit (`Log2MaxSectionAlignment` is 13):
https://github.com/llvm/llvm-project/blob/1d87294ea505b29a981789590727f248679ba421/lld/COFF/Chunks.h#L76-L83
Checking the code in `ObjFile::readSection` (or in the `SectionChunk` constructor) and reporting an invalid-object error would fix it. `getAlignment` could also treat 0xF as invalid, but other tools use it 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