[PATCH] D94354: [ELF] Drop .rel[a].debug_gnu_pub{names,types} for --gdb-index --emit-relocs

Fangrui Song via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Fri Jan 8 17:50:33 PST 2021


MaskRay created this revision.
MaskRay added reviewers: grimar, psmith.
Herald added subscribers: arphaman, arichardson, emaste.
Herald added a reviewer: espindola.
MaskRay requested review of this revision.
Herald added a project: LLVM.
Herald added a subscriber: llvm-commits.

Fixes PR48693: --emit-relocs keeps relocation sections. --gdb-index drops
.debug_gnu_pubnames and .debug_gnu_pubtypes but not their relocation sections.
There can cause a null pointer dereference in `getOutputSectionName`.

Also delete debug-gnu-pubnames.s which is covered by gdb-index.s


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D94354

Files:
  lld/ELF/SyntheticSections.cpp
  lld/test/ELF/debug-gnu-pubnames.s
  lld/test/ELF/gdb-index.s


Index: lld/test/ELF/gdb-index.s
===================================================================
--- lld/test/ELF/gdb-index.s
+++ lld/test/ELF/gdb-index.s
@@ -1,10 +1,12 @@
 # REQUIRES: x86, zlib
 # RUN: llvm-mc -filetype=obj -triple=x86_64-pc-linux %s -o %t1.o
 # RUN: llvm-mc -filetype=obj -triple=x86_64-pc-linux %p/Inputs/gdb-index.s -o %t2.o
-# RUN: ld.lld --gdb-index %t1.o %t2.o -o %t
+# RUN: ld.lld --gdb-index --emit-relocs %t1.o %t2.o -o %t
 
 # RUN: llvm-objdump -d %t | FileCheck %s --check-prefix=DISASM
 # RUN: llvm-dwarfdump -gdb-index %t | FileCheck %s --check-prefix=DWARF
+## Drop .debug_gnu_pubnames and .debug_gnu_pubtypes.
+## Also drop their relocation sections if --emit-relocs is specified.
 # RUN: llvm-readelf -sections %t | FileCheck %s --check-prefix=SECTION
 
 # RUN: llvm-mc -compress-debug-sections=zlib-gnu -filetype=obj -triple=x86_64-pc-linux \
@@ -46,7 +48,8 @@
 # DWARF-NEXT:    1(0x8): 0x30000000
 # DWARF-NEXT:    2(0x10): 0x90000000 0x90000001
 
-# SECTION-NOT: debug_gnu_pubnames
+# SECTION-NOT: .debug_gnu_pubnames
+# SECTION-NOT: .debug_gnu_pubtypes
 
 # RUN: ld.lld --gdb-index --no-gdb-index %t1.o %t2.o -o %t2
 # RUN: llvm-readobj --sections %t2 | FileCheck -check-prefix=NOGDB %s
@@ -111,7 +114,7 @@
 .section .debug_gnu_pubnames,"", at progbits
 .long 0x1e
 .value 0x2
-.long 0
+.long .debug_info
 .long 0x33
 .long 0x18
 .byte 0x30
@@ -121,7 +124,7 @@
 .section .debug_gnu_pubtypes,"", at progbits
 .long 0x17
 .value 0x2
-.long 0
+.long .debug_info
 .long 0x33
 .long 0x2b
 .byte 0x90
Index: lld/test/ELF/debug-gnu-pubnames.s
===================================================================
--- lld/test/ELF/debug-gnu-pubnames.s
+++ /dev/null
@@ -1,18 +0,0 @@
-# REQUIRES: x86
-# RUN: llvm-mc -filetype=obj -triple=x86_64-pc-linux %s -o %t.o
-
-# RUN: ld.lld %t.o -o %t1.exe
-# RUN: llvm-readobj --sections %t1.exe | FileCheck %s
-# CHECK: .debug_gnu_pubnames
-# CHECK: .debug_gnu_pubtypes
-
-# RUN: ld.lld --gdb-index %t.o -o %t2.exe
-# RUN: llvm-readobj --sections %t2.exe | FileCheck %s --check-prefix=GDB
-# GDB-NOT: .debug_gnu_pubnames
-# GDB-NOT: .debug_gnu_pubtypes
-
-.section .debug_gnu_pubnames,"", at progbits
-.long 0
-
-.section .debug_gnu_pubtypes,"", at progbits
-.long 0
Index: lld/ELF/SyntheticSections.cpp
===================================================================
--- lld/ELF/SyntheticSections.cpp
+++ lld/ELF/SyntheticSections.cpp
@@ -2871,14 +2871,24 @@
     InputSection *isec = dyn_cast<InputSection>(s);
     if (!isec)
       continue;
+    if (isec->name == ".debug_info") {
+      files.insert(isec->file);
+      continue;
+    }
     // .debug_gnu_pub{names,types} are useless in executables.
     // They are present in input object files solely for creating
     // a .gdb_index. So we can remove them from the output.
     if (s->name == ".debug_gnu_pubnames" || s->name == ".debug_gnu_pubtypes")
       s->markDead();
-    else if (isec->name == ".debug_info")
-      files.insert(isec->file);
   }
+  // Drop .rel[a].debug_gnu_pubnames for --emit-relocs.
+  llvm::erase_if(inputSections, [](InputSectionBase *s) {
+    if (auto *isec = dyn_cast<InputSection>(s))
+      if (InputSectionBase *rel = isec->getRelocatedSection())
+        if (!rel->isLive())
+          return true;
+    return !s->isLive();
+  });
 
   std::vector<GdbChunk> chunks(files.size());
   std::vector<std::vector<NameAttrEntry>> nameAttrs(files.size());


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D94354.315561.patch
Type: text/x-patch
Size: 3416 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20210109/879b9450/attachment.bin>


More information about the llvm-commits mailing list