[PATCH] D40235: COFF: Correctly handle relocations against early discarded sections.

Peter Collingbourne via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Sun Nov 19 23:47:30 PST 2017


pcc created this revision.

Don't crash if we encounter a reference to an early discarded section
(such as .drectve). Instead, handle them the same way as sections
discarded by comdat merging, i.e. either print an error message or
(for debug sections) silently ignore the relocation.


https://reviews.llvm.org/D40235

Files:
  lld/COFF/Chunks.cpp
  lld/COFF/MarkLive.cpp
  lld/test/COFF/reloc-discarded-early.s
  lld/test/COFF/reloc-discarded-early2.s


Index: lld/test/COFF/reloc-discarded-early2.s
===================================================================
--- /dev/null
+++ lld/test/COFF/reloc-discarded-early2.s
@@ -0,0 +1,9 @@
+# RUN: llvm-mc -triple=x86_64-windows-msvc -filetype=obj -o %t.obj %s
+# RUN: not lld-link -entry:__ImageBase -subsystem:console %t.obj 2>&1 | FileCheck %s
+
+.text
+# CHECK: error: relocation against symbol in discarded section: .drectve
+.quad .Ldrectve
+
+.section .drectve
+.Ldrectve:
Index: lld/test/COFF/reloc-discarded-early.s
===================================================================
--- /dev/null
+++ lld/test/COFF/reloc-discarded-early.s
@@ -0,0 +1,8 @@
+# RUN: llvm-mc -triple=x86_64-windows-msvc -filetype=obj -o %t.obj %s
+# RUN: lld-link -entry:__ImageBase -subsystem:console -debug %t.obj
+
+.section .debug_info,"dr"
+.quad .Ldrectve
+
+.section .drectve
+.Ldrectve:
Index: lld/COFF/MarkLive.cpp
===================================================================
--- lld/COFF/MarkLive.cpp
+++ lld/COFF/MarkLive.cpp
@@ -63,7 +63,8 @@
 
     // Mark all symbols listed in the relocation table for this section.
     for (Symbol *B : SC->symbols())
-      AddSym(B);
+      if (B)
+        AddSym(B);
 
     // Mark associative sections if any.
     for (SectionChunk *C : SC->children())
Index: lld/COFF/Chunks.cpp
===================================================================
--- lld/COFF/Chunks.cpp
+++ lld/COFF/Chunks.cpp
@@ -252,20 +252,27 @@
     // Get the output section of the symbol for this relocation.  The output
     // section is needed to compute SECREL and SECTION relocations used in debug
     // info.
-    Defined *Sym = cast<Defined>(File->getSymbol(Rel.SymbolTableIndex));
-    Chunk *C = Sym->getChunk();
-    OutputSection *OS = C ? C->getOutputSection() : nullptr;
+    auto *Sym =
+        dyn_cast_or_null<Defined>(File->getSymbol(Rel.SymbolTableIndex));
+    OutputSection *OS = nullptr;
+    if (Sym)
+      if (Chunk *C = Sym->getChunk())
+        OS = C->getOutputSection();
 
     // Only absolute and __ImageBase symbols lack an output section. For any
     // other symbol, this indicates that the chunk was discarded.  Normally
     // relocations against discarded sections are an error.  However, debug info
     // sections are not GC roots and can end up with these kinds of relocations.
     // Skip these relocations.
-    if (!OS && !isa<DefinedAbsolute>(Sym) && !isa<DefinedSynthetic>(Sym)) {
+    if (!OS &&
+        (!Sym || (!isa<DefinedAbsolute>(Sym) && !isa<DefinedSynthetic>(Sym)))) {
       if (isCodeView() || isDWARF())
         continue;
-      fatal("relocation against symbol in discarded section: " +
-            Sym->getName());
+      COFFSymbolRef Sym =
+          check(File->getCOFFObj()->getSymbol(Rel.SymbolTableIndex));
+      StringRef Name;
+      File->getCOFFObj()->getSymbolName(Sym, Name);
+      fatal("relocation against symbol in discarded section: " + Name);
     }
     uint64_t S = Sym->getRVA();
 
@@ -328,7 +335,8 @@
     uint8_t Ty = getBaserelType(Rel);
     if (Ty == IMAGE_REL_BASED_ABSOLUTE)
       continue;
-    if (isa<DefinedAbsolute>(File->getSymbol(Rel.SymbolTableIndex)))
+    Symbol *Target = File->getSymbol(Rel.SymbolTableIndex);
+    if (!Target || isa<DefinedAbsolute>(Target))
       continue;
     Res->emplace_back(RVA + Rel.VirtualAddress, Ty);
   }


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D40235.123540.patch
Type: text/x-patch
Size: 3369 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20171120/74bd0870/attachment.bin>


More information about the llvm-commits mailing list