[lld] [lld-macho] Fix crash with DWARF section-relative relocations (PR #168075)

Joel Reymont via llvm-commits llvm-commits at lists.llvm.org
Fri Nov 28 06:37:43 PST 2025


joelreymont wrote:

macOS ARM64 repro of the pre-fix crash (DWARF section-relative relocation target dropped early):

```asm
# dsym_repro_crash_arm64.s
.section __TEXT,__text,regular,pure_instructions
.globl _main
.p2align 2
_main:
    ret

.section __DATA,__data
    # Relocation from a kept section into __debug_str
    .quad Lproducer

.section __DWARF,__debug_abbrev,regular,debug
    .byte 1,17,1,37,14,3,14,0,0,0

.section __DWARF,__debug_info,regular,debug
Linfo_begin:
    .long Linfo_end - Linfo_begin - 4
    .short 4
    .long 0
    .byte 8
    .byte 1
    .long Lproducer - Ldebug_str
    .long Lfilename - Ldebug_str
Linfo_end:

.section __DWARF,__debug_str,regular,debug
Ldebug_str:
Lproducer:
    .asciz "Test Producer 1.0"
Lfilename:
    .asciz "test.c"
```

Old (unfixed) ld64.lld (built from the parent of the fix), invoked via the Mach-O driver name:

```bash
bin/llvm-mc -filetype=obj -triple=arm64-apple-darwin dsym_repro_crash_arm64.s -o /tmp/dsym_repro_crash_arm64.o
/tmp/oldld_pref/ld64.lld -arch arm65 -platform_version macos 13.0 13.0 \
  -dylib /tmp/dsym_repro_crash_arm64.o -o /tmp/dsym_repro_crash_arm64.dylib
```

Result: crashes in `ObjFile::parseRelocations` because the DWARF sections were dropped before relocation rewriting. 

```
Stack dump:
0.      Program arguments: /tmp/oldld_pref/ld64.lld -arch arm64 -platform_version macos 13.0 13.0 -dylib /tmp/dsym_repro_crash_arm64.o -o /tmp/dsym_repro_crash_arm64.dylib
Stack dump without symbol names (ensure you have llvm-symbolizer in your PATH or set the environment var `LLVM_SYMBOLIZER_PATH` to point to it):
0  ld64.lld.pref            0x00000001021d1308 llvm::sys::PrintStackTrace(llvm::raw_ostream&, int) + 56
1  ld64.lld.pref            0x00000001021cedb8 llvm::sys::RunSignalHandlers() + 172
2  ld64.lld.pref            0x00000001021d1df0 SignalHandler(int, __siginfo*, void*) + 344
3  libsystem_platform.dylib 0x000000019bbb3744 _sigtramp + 56
4  ld64.lld.pref            0x00000001024dab90 void lld::macho::ObjFile::parseRelocations<llvm::MachO::section_64>(llvm::ArrayRef<llvm::MachO::section_64>, llvm::MachO::section_64 const&, lld::macho::Section&) + 1092
5  ld64.lld.pref            0x00000001024d1034 void lld::macho::ObjFile::parse<lld::macho::LP64>() + 472
6  ld64.lld.pref            0x00000001024d0aac lld::macho::ObjFile::ObjFile(llvm::MemoryBufferRef, unsigned int, llvm::StringRef, bool, bool, bool, bool) + 380
7  ld64.lld.pref            0x00000001024bdca0 lld::macho::ObjFile* lld::make<lld::macho::ObjFile, llvm::MemoryBufferRef&, unsigned int, char const (&) [1], bool&>(llvm::MemoryBufferRef&, unsigned int&&, char const (&) [1], bool&) + 196
8  ld64.lld.pref            0x00000001024bc65c processFile(std::__1::optional<llvm::MemoryBufferRef>, std::__1::vector<DeferredFile, std::__1::allocator<DeferredFile>>*, llvm::StringRef, LoadType, bool, bool, bool, bool) + 708
9  ld64.lld.pref            0x00000001024b87b0 lld::macho::link(llvm::ArrayRef<char const*>, llvm::raw_ostream&, llvm::raw_ostream&, bool, bool) + 24648
10 ld64.lld.pref            0x00000001021fee44 lld::unsafeLldMain(llvm::ArrayRef<char const*>, llvm::raw_ostream&, llvm::raw_ostream&, llvm::ArrayRef<lld::DriverDef>, bool) + 1948
11 ld64.lld.pref            0x00000001020b9f08 lld_main(int, char**, llvm::ToolContext const&) + 276
12 ld64.lld.pref            0x00000001020ba5c0 main + 88
13 dyld                     0x000000019b7e1d54 start + 7184
```

Patched ld64.lld (current branch) keeps DWARF InputSections in `section.subsections`, so relocation rewriting can succeed.


https://github.com/llvm/llvm-project/pull/168075


More information about the llvm-commits mailing list