Issue 234724
Summary [lld][ELF] Crash in compareSections (cast) when an ONLY_IF_RO output section has a symbol-based address and a symbol assignment follows
Labels lld
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.

Linking a small object with this linker script crashes `ld.lld`. A release build segfaults; an assertions build fails `cast<OutputDesc>` in `compareSections`:

```
f = 0x1000;
SECTIONS { .foo f : ONLY_IF_RO { *(.data) } }
s = 2;
```

The script is valid, and GNU ld links it.

**Versions tested** (host and target x86_64 Linux):

- LLVM 23.1.4, the latest release: official `LLVM-23.1.4-Linux-X64.tar.zst` from the GitHub release page. SIGSEGV.
- LLD 21.1.8: Ubuntu `lld-21` package. SIGSEGV.
- main at c41dd6bb2d26 (2026-10-22), built from the Git repository with `-DCMAKE_BUILD_TYPE=Release -DLLVM_ENABLE_ASSERTIONS=ON -DLLVM_ENABLE_PROJECTS=lld`. Assertion failure (SIGABRT).

The test case is reduced by hand. Each of its parts is needed (see below).

### Reproducer

```sh
cat > a.s <<'EOF'
.globl _start
_start:
  ret
.data
.long 1
EOF
cat > t.lds <<'EOF'
f = 0x1000;
SECTIONS { .foo f : ONLY_IF_RO { *(.data) } }
s = 2;
EOF
clang --target=x86_64-linux-gnu -c a.s -o a.o
ld.lld -T t.lds a.o -o a.out
```

### Observed

Latest release, LLVM 23.1.4 (release build), complete output:

```
PLEASE submit a bug report to https://github.com/llvm/llvm-project/issues/ and include the crash backtrace and instructions to reproduce the bug.
Stack dump:
0.	Program arguments: /path/to/llvm-23.1.4/bin/ld.lld -T t.lds a.o -o a.out
 #0 0x00006357ac100a2a llvm::sys::PrintStackTrace(llvm::raw_ostream&, int) (/path/to/llvm-23.1.4/bin/ld.lld+0x5d6ea2a)
 #1 0x00006357ac1015cd SignalHandler(int, siginfo_t*, void*) Signals.cpp:0:0
 #2 0x000071d5a7059cb0 (/usr/lib/x86_64-linux-gnu/libc.so.6+0x45cb0)
 #3 0x00006357ac4848dc findOrphanPos(lld::elf::Ctx&, lld::elf::SectionCommand**, lld::elf::SectionCommand**) Writer.cpp:0:0
 #4 0x00006357ac49c797 (anonymous namespace)::Writer<llvm::object::ELFType<(llvm::endianness)1, true>>::finalizeSections() Writer.cpp:0:0
 #5 0x00006357ac46e4eb void lld::elf::writeResult<llvm::object::ELFType<(llvm::endianness)1, true>>(lld::elf::Ctx&) (/path/to/llvm-23.1.4/bin/ld.lld+0x60dc4eb)
 #6 0x00006357ac24372d void lld::elf::LinkerDriver::link<llvm::object::ELFType<(llvm::endianness)1, true>>(llvm::opt::InputArgList&) Driver.cpp:0:0
 #7 0x00006357ac2246d0 lld::elf::LinkerDriver::linkerMain(llvm::ArrayRef<char const*>) (/path/to/llvm-23.1.4/bin/ld.lld+0x5e926d0)
 #8 0x00006357ac221664 lld::elf::link(llvm::ArrayRef<char const*>, llvm::raw_ostream&, llvm::raw_ostream&, bool, bool) (/path/to/llvm-23.1.4/bin/ld.lld+0x5e8f664)
 #9 0x00006357ac1043bf lld::unsafeLldMain(llvm::ArrayRef<char const*>, llvm::raw_ostream&, llvm::raw_ostream&, llvm::ArrayRef<lld::DriverDef>, bool) (/path/to/llvm-23.1.4/bin/ld.lld+0x5d723bf)
#10 0x00006357abc984c3 lld_main(int, char**, llvm::ToolContext const&) (/path/to/llvm-23.1.4/bin/ld.lld+0x59064c3)
#11 0x00006357abc98613 main (/path/to/llvm-23.1.4/bin/ld.lld+0x5906613)
#12 0x000071d5a703e601 (/usr/lib/x86_64-linux-gnu/libc.so.6+0x2a601)
#13 0x000071d5a703e718 __libc_start_main (/usr/lib/x86_64-linux-gnu/libc.so.6+0x2a718)
#14 0x00006357abf1c685 _start (/path/to/llvm-23.1.4/bin/ld.lld+0x5b8a685)
Segmentation fault (core dumped)
```

main at c41dd6bb2d26 (Release + assertions):

```
ld.lld: llvm/include/llvm/Support/Casting.h:572: decltype(auto) llvm::cast(>From *) [To = lld::elf::OutputDesc, From = const lld::elf::SectionCommand]: Assertion `isa<To>(Val) && "cast<Ty>() argument of incompatible type!"' failed.
```

<details><summary>Backtrace of the assertions build (gdb)</summary>

```
#7  __assert_fail
#8 compareSections(lld::elf::Ctx&, lld::elf::SectionCommand const*, lld::elf::SectionCommand const*)
#9 std::__merge_sort_with_buffer<lld::elf::SectionCommand**, ...,
 (anonymous namespace)::Writer<llvm::object::ELFType<(llvm::endianness)1, true> >::sortOrphanSections()::{lambda}>
#10 (anonymous namespace)::Writer<llvm::object::ELFType<(llvm::endianness)1, true> >::finalizeSections()
#11 lld::elf::writeResult<llvm::object::ELFType<(llvm::endianness)1, true> >(lld::elf::Ctx&)
#12 lld::elf::LinkerDriver::link<llvm::object::ELFType<(llvm::endianness)1, true> >(llvm::opt::InputArgList&)
#13 lld::elf::LinkerDriver::linkerMain(llvm::ArrayRef<char const*>)
#14 lld::elf::link(llvm::ArrayRef<char const*>, llvm::raw_ostream&, llvm::raw_ostream&, bool, bool)
#15 lld::unsafeLldMain(...)
#16 lld_main(int, char**, llvm::ToolContext const&)
#17 main
```

</details>

### Expected

No crash: link the file as GNU ld does, or report an error.

All three parts of the script are needed. Each of these variants links successfully with the same object:

- dropping `ONLY_IF_RO`;
- dropping the trailing `s = 2;`;
- using a constant address (`.foo 0x1000 :`) or `.foo . :`;
- `ONLY_IF_RW` instead of `ONLY_IF_RO`.

These variants still crash:

- `.foo f + 0 :` or `.foo ALIGN(f) :` as the address;
- `PROVIDE(s = 2);` in place of `s = 2;`;
- `s = 2;` moved inside `SECTIONS`.

The input must contain a `.data` section matched by `*(.data)`; an object without `.data` links cleanly. So the crash seems to need the `ONLY_IF_RO` constraint to fail, which drops `.foo` and leaves `.data` as an orphan.

### Analysis

`Writer::sortOrphanSections()` takes the first `OutputDesc` whose `sectionIndex == UINT32_MAX` as the start of the orphan suffix. It then runs `std::stable_sort` over the rest of `sectionCommands` with `compareSections`, which does `cast<OutputDesc>` on both arguments. In this case a non-`OutputDesc` command (the trailing symbol assignment) is in that suffix. I have not worked out why the symbol-based address of the dropped `ONLY_IF_RO` section changes the command order, but the constant-address control shows that it does.

Related: #80390 fixed an earlier `compareSections` assertion where `OutputDesc`s in `sectionCommands` were non-contiguous. This input still fails on releases after that fix.

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