Author: Fangrui Song Date: 2020-04-10T17:17:37-07:00 New Revision: 489a7356cca373de761ada4c06c5b43edc581b4b
URL: https://github.com/llvm/llvm-project/commit/489a7356cca373de761ada4c06c5b43edc581b4b DIFF: https://github.com/llvm/llvm-project/commit/489a7356cca373de761ada4c06c5b43edc581b4b.diff LOG: [ELF] Fix a null pointer dereference when --emit-relocs and --strip-debug are used together Fixes https://bugs.llvm.org//show_bug.cgi?id=44878 When --strip-debug is specified, .debug* are removed from inputSections while .rel[a].debug* (incorrectly) remain. LinkerScript::addOrphanSections() requires the output section of a relocated InputSectionBase to be created first. .debug* are not in inputSections -> output sections .debug* are not created -> getOutputSectionName(.rel[a].debug*) dereferences a null pointer. Fix the null pointer dereference by deleting .rel[a].debug* from inputSections as well. Reviewed By: grimar, nickdesaulniers Differential Revision: https://reviews.llvm.org/D74510 (cherry picked from commit 6c73246179376442705b3a545f4e1f1478777a04) Added: lld/test/ELF/emit-relocs-debug.s Modified: lld/ELF/Driver.cpp lld/ELF/InputSection.cpp lld/ELF/InputSection.h Removed: ################################################################################ diff --git a/lld/ELF/Driver.cpp b/lld/ELF/Driver.cpp index 25330832339c..6de9698bb2c8 100644 --- a/lld/ELF/Driver.cpp +++ b/lld/ELF/Driver.cpp @@ -1906,8 +1906,17 @@ template <class ELFT> void LinkerDriver::link(opt::InputArgList &args) { // 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<InputSection>(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.cpp b/lld/ELF/InputSection.cpp index 147c51ab285e..d34abf641ed3 100644 --- a/lld/ELF/InputSection.cpp +++ b/lld/ELF/InputSection.cpp @@ -441,8 +441,7 @@ void InputSection::copyRelocations(uint8_t *buf, ArrayRef<RelTy> rels) { // See the comment in maybeReportUndefined for PPC64 .toc . auto *d = dyn_cast<Defined>(&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<Undefined>(sym).discardedSecIdx; Elf_Shdr_Impl<ELFT> sec = diff --git a/lld/ELF/InputSection.h b/lld/ELF/InputSection.h index 3c42af7db7b4..fe2c3c516a96 100644 --- a/lld/ELF/InputSection.h +++ b/lld/ELF/InputSection.h @@ -357,6 +357,10 @@ class InputSection : public InputSectionBase { template <class ELFT> 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<InputSectionBase *> inputSections; diff --git a/lld/test/ELF/emit-relocs-debug.s b/lld/test/ELF/emit-relocs-debug.s new file mode 100644 index 000000000000..04fa0f8d961b --- /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 _______________________________________________ llvm-branch-commits mailing list llvm-branch-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-branch-commits