Issue 235240
Summary [lld][ELF] ld.lld -r crashes on an SHT_GROUP section that has SHF_MERGE set (group becomes a MergeInputSection)
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.

`ld.lld -r` crashes when an otherwise valid `SHT_GROUP` section has `SHF_MERGE` in its `sh_flags`. In a relocatable link the group section is kept as an input section. `createInputSection` sees `SHF_MERGE` with a nonzero `sh_entsize` (4 for a group) and makes it a `MergeInputSection`. Its pieces then end up in a synthetic merge section owned by the internal file, and `finalizeShtGroup` calls `section->file->getSymbols()` on that internal file.

**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.
- 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.
- LLD 21.1.8 (Ubuntu `lld-21` package): SIGSEGV.

### Reproducer

```sh
cat > t.yaml <<'EOF'
--- !ELF
FileHeader:
  Class:   ELFCLASS64
  Data: ELFDATA2LSB
  Type:    ET_REL
  Machine: EM_X86_64
Sections:
  - Name: .group
    Type:    SHT_GROUP
    Flags:   [ SHF_MERGE ]
    Link: .symtab
    Info:    sig
    Members:
      - SectionOrType: GRP_COMDAT
 - SectionOrType: .text
  - Name:    .text
    Type:    SHT_PROGBITS
 Flags:   [ SHF_ALLOC, SHF_EXECINSTR, SHF_GROUP ]
    Content: '90'
Symbols:
  - Name:    sig
    Section: .text
    Binding: STB_GLOBAL
EOF
yaml2obj t.yaml -o t.o
ld.lld -r t.o -o out.o
```

Variants (same results on all three builds):

- `Flags: [ ]` on `.group`: `-r` links.
- A final link (`ld.lld t.o -o /dev/null`): links, because the group section is discarded there.
- A non-COMDAT group (flag word `0` instead of `GRP_COMDAT`): `-r` still crashes.
- `Flags: [ SHF_MERGE, SHF_STRINGS ]`: `-r` reports `t.o:(.group): string is not null terminated`. This shows the group section is being parsed as merge input.

GNU ld (2.46) `-r` accepts the object.

### Observed

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 -r t.o -o out.o
 #0 0x00005750ef41aa2a llvm::sys::PrintStackTrace(llvm::raw_ostream&, int) (/path/to/llvm-23.1.4/bin/ld.lld+0x5d6ea2a)
 #1 0x00005750ef41b5cd SignalHandler(int, siginfo_t*, void*) Signals.cpp:0:0
 #2 0x000079f7f57bbcb0 (/usr/lib/x86_64-linux-gnu/libc.so.6+0x45cb0)
 #3 0x00005750ef6a84fc lld::elf::OutputSection::finalize(lld::elf::Ctx&) (/path/to/llvm-23.1.4/bin/ld.lld+0x5ffc4fc)
 #4 0x00005750ef7ba543 (anonymous namespace)::Writer<llvm::object::ELFType<(llvm::endianness)1, true>>::finalizeSections() Writer.cpp:0:0
 #5 0x00005750ef7884eb 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 0x00005750ef55d72d void lld::elf::LinkerDriver::link<llvm::object::ELFType<(llvm::endianness)1, true>>(llvm::opt::InputArgList&) Driver.cpp:0:0
 #7 0x00005750ef53e6d0 lld::elf::LinkerDriver::linkerMain(llvm::ArrayRef<char const*>) (/path/to/llvm-23.1.4/bin/ld.lld+0x5e926d0)
 #8 0x00005750ef53b664 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 0x00005750ef41e3bf 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 0x00005750eefb24c3 lld_main(int, char**, llvm::ToolContext const&) (/path/to/llvm-23.1.4/bin/ld.lld+0x59064c3)
#11 0x00005750eefb2613 main (/path/to/llvm-23.1.4/bin/ld.lld+0x5906613)
#12 0x000079f7f57a0601 (/usr/lib/x86_64-linux-gnu/libc.so.6+0x2a601)
#13 0x000079f7f57a0718 __libc_start_main (/usr/lib/x86_64-linux-gnu/libc.so.6+0x2a718)
#14 0x00005750ef236685 _start (/path/to/llvm-23.1.4/bin/ld.lld+0x5b8a685)
Segmentation fault (core dumped)
```

main at c41dd6bb2d26 (Release + assertions):

```
ld.lld: lld/ELF/InputFiles.h:93: ArrayRef<Symbol *> lld::elf::InputFile::getSymbols() const: Assertion `fileKind == BinaryKind || fileKind == ObjKind || fileKind == BitcodeKind' failed.
```

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

```
#7  __assert_fail
#8 lld::elf::OutputSection::finalize(lld::elf::Ctx&)
#9  (anonymous namespace)::Writer<llvm::object::ELFType<(llvm::endianness)1, true> >::finalizeSections()
#10 lld::elf::writeResult<llvm::object::ELFType<(llvm::endianness)1, true> >(lld::elf::Ctx&)
#11 lld::elf::LinkerDriver::link<llvm::object::ELFType<(llvm::endianness)1, true> >(llvm::opt::InputArgList&)
#12 lld::elf::LinkerDriver::linkerMain(llvm::ArrayRef<char const*>)
#13 lld::elf::link(llvm::ArrayRef<char const*>, llvm::raw_ostream&, llvm::raw_ostream&, bool, bool)
#14 lld::unsafeLldMain(...)
#15 lld_main(int, char**, llvm::ToolContext const&)
```

(`finalizeShtGroup` is inlined into `OutputSection::finalize`.)

</details>

### Expected

No crash: either ignore `SHF_MERGE` on a group section (GNU ld 2.46 `-r` accepts this object) or report an error for it.

### Analysis

In `ObjFile::createInputSection` (`lld/ELF/InputFiles.cpp`), for `-r` links:

```cpp
  if ((sec.sh_flags & SHF_MERGE) && shouldMerge(sec, name))
    return makeThreadLocal<MergeInputSection>(*this, sec, name);
```

`shouldMerge` only checks `sh_size`, `sh_entsize` and `SHF_WRITE`, not `sh_type`, so the 8-byte group with `sh_entsize = 4` is accepted. `finalizeShtGroup` (`lld/ELF/OutputSections.cpp`) assumes the first input of the output group section is the original group section from an object file:

```cpp
  ArrayRef<Symbol *> symbols = section->file->getSymbols();
  os->info = ctx.in.symTab->getSymbolIndex(*symbols[section->info]);
```

Here `section` is the synthetic merge section, whose `file` is the internal file, so `getSymbols()` asserts on the assertions build and reads a bogus symbol array on release builds.

A possible fix: never treat `SHT_GROUP` (or other non-data section types) as mergeable, for example by checking `sh_type` in `shouldMerge` or by excluding `SHT_GROUP` before the `SHF_MERGE` check.

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