diff --git a/lld/ELF/InputSection.h b/lld/ELF/InputSection.h --- a/lld/ELF/InputSection.h +++ b/lld/ELF/InputSection.h @@ -397,7 +397,8 @@ #endif inline bool isDebugSection(const InputSectionBase &sec) { - return sec.name.startswith(".debug") || sec.name.startswith(".zdebug"); + return (sec.flags & llvm::ELF::SHF_ALLOC) == 0 && + (sec.name.startswith(".debug") || sec.name.startswith(".zdebug")); } // The list of all input sections. diff --git a/lld/test/ELF/gc-sections-strip-debug.s b/lld/test/ELF/gc-sections-strip-debug.s new file mode 100644 --- /dev/null +++ b/lld/test/ELF/gc-sections-strip-debug.s @@ -0,0 +1,17 @@ +# REQUIRES: x86 +## Test that we don't strip SHF_ALLOC .debug* or crash (PR48071 +## mark liveness of a merge section which has not been split). + +# RUN: llvm-mc -filetype=obj -triple=x86_64 %s -o %t.o +# RUN: ld.lld %t.o --gc-sections --strip-debug -o %t +# RUN: llvm-readelf -S %t | FileCheck %s + +# CHECK: .debug_gdb_scripts + +.globl _start +_start: + leaq .L.str(%rip), %rax + +.section .debug_gdb_scripts,"aMS",@progbits,1 +.L.str: + .asciz "Rust uses SHF_ALLOC .debug_gdb_scripts"