[PATCH] D74510: [ELF] Fix a null pointer dereference when --emit-relocs and --strip-debug are used together

Fangrui Song via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Wed Feb 12 16:22:21 PST 2020


MaskRay updated this revision to Diff 244302.
MaskRay marked an inline comment as done.
MaskRay added a comment.

Change isDebugSection() to take a const reference.


Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D74510/new/

https://reviews.llvm.org/D74510

Files:
  lld/ELF/Driver.cpp
  lld/ELF/InputSection.cpp
  lld/ELF/InputSection.h
  lld/test/ELF/emit-relocs-debug.s


Index: lld/test/ELF/emit-relocs-debug.s
===================================================================
--- /dev/null
+++ 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
Index: lld/ELF/InputSection.h
===================================================================
--- lld/ELF/InputSection.h
+++ lld/ELF/InputSection.h
@@ -357,6 +357,10 @@
   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;
 
Index: lld/ELF/InputSection.cpp
===================================================================
--- lld/ELF/InputSection.cpp
+++ lld/ELF/InputSection.cpp
@@ -441,8 +441,7 @@
       // 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 =
Index: lld/ELF/Driver.cpp
===================================================================
--- lld/ELF/Driver.cpp
+++ 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<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


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D74510.244302.patch
Type: text/x-patch
Size: 2608 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20200213/8170841f/attachment.bin>


More information about the llvm-commits mailing list