diff --git a/lld/ELF/Driver.cpp b/lld/ELF/Driver.cpp --- a/lld/ELF/Driver.cpp +++ b/lld/ELF/Driver.cpp @@ -1938,8 +1938,17 @@ // We do not want to emit debug sections if --strip-all // or -strip-debug are given. - return config->strip != StripPolicy::None && - (s->name.startswith(".debug") || s->name.startswith(".zdebug")); + if (config->strip == StripPolicy::None) + return false; + + if (isDebugSection(*s)) + return true; + if (auto *isec = dyn_cast(s)) + if (InputSectionBase *rel = isec->getRelocatedSection()) + if (isDebugSection(*rel)) + return true; + + return false; }); // Now that the number of partitions is fixed, save a pointer to the main diff --git a/lld/ELF/InputSection.h b/lld/ELF/InputSection.h --- a/lld/ELF/InputSection.h +++ b/lld/ELF/InputSection.h @@ -357,6 +357,10 @@ template void copyShtGroup(uint8_t *buf); }; +inline bool isDebugSection(const InputSectionBase &sec) { + return sec.name.startswith(".debug") || sec.name.startswith(".zdebug"); +} + // The list of all input sections. extern std::vector inputSections; diff --git a/lld/ELF/InputSection.cpp b/lld/ELF/InputSection.cpp --- a/lld/ELF/InputSection.cpp +++ b/lld/ELF/InputSection.cpp @@ -441,8 +441,7 @@ // See the comment in maybeReportUndefined for PPC64 .toc . auto *d = dyn_cast(&sym); if (!d) { - if (!sec->name.startswith(".debug") && - !sec->name.startswith(".zdebug") && sec->name != ".eh_frame" && + if (!isDebugSection(*sec) && sec->name != ".eh_frame" && sec->name != ".gcc_except_table" && sec->name != ".toc") { uint32_t secIdx = cast(sym).discardedSecIdx; Elf_Shdr_Impl sec = diff --git a/lld/test/ELF/emit-relocs-debug.s b/lld/test/ELF/emit-relocs-debug.s new file mode 100644 --- /dev/null +++ b/lld/test/ELF/emit-relocs-debug.s @@ -0,0 +1,20 @@ +# REQUIRES: x86 +## Test --emit-relocs handles .debug* + +# RUN: llvm-mc -filetype=obj -triple=x86_64 %s -o %t.o +# RUN: ld.lld --emit-relocs %t.o -o %t +# RUN: llvm-readobj -r %t | FileCheck %s +# RUN: ld.lld --emit-relocs --strip-debug %t.o -o %t.no +# RUN: llvm-readobj -r %t.no | FileCheck --check-prefix=NO %s + +# CHECK: Section {{.*}} .rela.debug_info { +# CHECK-NEXT: R_X86_64_64 .text 0x0 +# CHECK-NEXT: } + +# NO: Relocations [ +# NO-NEXT: ] + +foo: + +.section .debug_info +.quad foo