Issue 243372
Summary [lld][ELF] ch_addralign of a compressed section is not checked for a power of two: assertion, or invalid output
Labels lld:ELF, crash
Assignees
Reporter AnantaSrikar
    `InputSectionBase` rejects an `sh_addralign` that isn't a power of two. For an `SHF_COMPRESSED` section, `parseCompressedHeader` then replaces `addralign` with the compression header's `ch_addralign`, without that check. A `ch_addralign` of 3 reaches `alignToPowerOf2`: an assertions build aborts, and Ubuntu LLD 21.1.8 writes an output section with `sh_addralign` 3, which LLD itself then rejects (`sh_addralign is not a power of 2`).

**Version**: main at c7014922f582 (2026-11-16), built from the Git repository with `-DCMAKE_BUILD_TYPE=Release -DLLVM_ENABLE_ASSERTIONS=ON -DLLVM_ENABLE_PROJECTS=lld` and zlib, on x86_64 Linux.

### Reproducer

```sh
cat > chdr.yaml <<'YAML'
--- !ELF
FileHeader:
 Class:           ELFCLASS64
  Data:            ELFDATA2LSB
  Type: ET_REL
  Machine:         EM_AARCH64
Sections:
  - Name: .debug_x
    Type:            SHT_PROGBITS
    Flags:           [ SHF_COMPRESSED ]
    AddressAlign:    0x8
    Content: 010000000000000001000000000000000300000000000000789C8B000000590059
...
YAML
yaml2obj chdr.yaml -o chdr.o
ld.lld -r chdr.o -o out.o
```

`Content` is an `Elf64_Chdr` (`ch_type` ZLIB, `ch_size` 1, `ch_addralign` 3) followed by a zlib stream. With `ch_addralign` 1 (`...0100000000000000789C...`), the same object links (status 0). A normal (non-`-r`) link aborts the same way.

### Observed

```
ld.lld: llvm/include/llvm/Support/MathExtras.h:495: T llvm::alignToPowerOf2(U, V) [U = unsigned long, V = unsigned int, T = unsigned long]: Assertion `Align != 0 && (Align & (Align - 1)) == 0 && "Align must be a power of 2"' failed.
```

```
lld::elf::LinkerScript::assignOffsets(lld::elf::OutputSection*)
lld::elf::LinkerScript::assignAddresses()
void lld::elf::writeResult<llvm::object::ELFType<(llvm::endianness)1, true>>(lld::elf::Ctx&)
```

### Expected

An error for the input section, like the `sh_addralign is not a power of 2` error that `sh_addralign` gets.

### Analysis

The check for `sh_addralign`:

https://github.com/llvm/llvm-project/blob/c7014922f5829426952a38d079220c21b0a8ebe8/lld/ELF/InputSection.cpp#L71-L78

and the unchecked replacement:

https://github.com/llvm/llvm-project/blob/c7014922f5829426952a38d079220c21b0a8ebe8/lld/ELF/InputSection.cpp#L286-L289

Applying the same `isPowerOf2_64` check (with the same error) to `ch_addralign` in `parseCompressedHeader` would fix it.

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