Issue 243560
Summary [lld][MachO] Common symbol larger than 2^31 gets alignment 0 (PowerOf2Ceil truncated to uint32_t): assertion, or garbage output
Labels lld:MachO, crash
Assignees
Reporter AnantaSrikar
    For a common symbol with byte alignment 1, `CommonSymbol` uses `PowerOf2Ceil(size)` as the alignment and stores it in a `uint32_t`. For a size above 2^31, `PowerOf2Ceil` returns 2^32, which truncates to 0. An assertions build then fails in `alignToPowerOf2`. A release build links with status 0 but writes a few dozen bytes of garbage: Ubuntu LLD 21.1.8 wrote a 48-byte file that `llvm-readobj` doesn't recognize, and five identical `-no_uuid` links produced four different files.

**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`, on x86_64 Linux.

### Reproducer

```sh
printf '.comm _c, 0x80010000, 0\n' > big.s
llvm-mc -triple=x86_64-apple-macosx -filetype=obj big.s -o big.o
ld64.lld -arch x86_64 -platform_version macos 11.0 11.0 -dylib -o big.dylib big.o
```

With `.comm _c, 256, 0`, it links (status 0). An arm64 object aborts the same way.

### Observed

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

```
void lld::macho::writeResult<lld::macho::LP64>()
lld::macho::link(llvm::ArrayRef<char const*>, llvm::raw_ostream&, llvm::raw_ostream&, bool, bool)
```

### Expected

A common symbol of that size is valid input (it fits in `n_value`), so it should link with a non-zero alignment, or be rejected with an error if LLD wants to cap the alignment.

### Analysis

https://github.com/llvm/llvm-project/blob/c7014922f5829426952a38d079220c21b0a8ebe8/lld/MachO/Symbols.h#L253-L265

The constructor already has a comment noting that the maximum alignment should be capped. Capping the computed alignment (for example at the largest alignment LLD supports for a section), or storing it in 64 bits, would avoid the zero.

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