[lld] r318689 - COFF: Correctly handle relocations against early discarded sections.

Peter Collingbourne via llvm-commits llvm-commits at lists.llvm.org
Mon Nov 20 11:37:07 PST 2017


Author: pcc
Date: Mon Nov 20 11:37:07 2017
New Revision: 318689

URL: http://llvm.org/viewvc/llvm-project?rev=318689&view=rev
Log:
COFF: Correctly handle relocations against early discarded sections.

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.

Differential Revision: https://reviews.llvm.org/D40235

Added:
    lld/trunk/test/COFF/reloc-discarded-early.s
    lld/trunk/test/COFF/reloc-discarded-early2.s
Modified:
    lld/trunk/COFF/Chunks.cpp
    lld/trunk/COFF/MarkLive.cpp

Modified: lld/trunk/COFF/Chunks.cpp
URL: http://llvm.org/viewvc/llvm-project/lld/trunk/COFF/Chunks.cpp?rev=318689&r1=318688&r2=318689&view=diff
==============================================================================
--- lld/trunk/COFF/Chunks.cpp (original)
+++ lld/trunk/COFF/Chunks.cpp Mon Nov 20 11:37:07 2017
@@ -252,7 +252,19 @@ void SectionChunk::writeTo(uint8_t *Buf)
     // 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));
+    auto *Sym =
+        dyn_cast_or_null<Defined>(File->getSymbol(Rel.SymbolTableIndex));
+    if (!Sym) {
+      if (isCodeView() || isDWARF())
+        continue;
+      // Symbols in early discarded sections are represented using null pointers,
+      // so we need to retrieve the name from the object file.
+      COFFSymbolRef Sym =
+          check(File->getCOFFObj()->getSymbol(Rel.SymbolTableIndex));
+      StringRef Name;
+      File->getCOFFObj()->getSymbolName(Sym, Name);
+      fatal("relocation against symbol in discarded section: " + Name);
+    }
     Chunk *C = Sym->getChunk();
     OutputSection *OS = C ? C->getOutputSection() : nullptr;
 
@@ -328,7 +340,8 @@ void SectionChunk::getBaserels(std::vect
     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);
   }

Modified: lld/trunk/COFF/MarkLive.cpp
URL: http://llvm.org/viewvc/llvm-project/lld/trunk/COFF/MarkLive.cpp?rev=318689&r1=318688&r2=318689&view=diff
==============================================================================
--- lld/trunk/COFF/MarkLive.cpp (original)
+++ lld/trunk/COFF/MarkLive.cpp Mon Nov 20 11:37:07 2017
@@ -63,7 +63,8 @@ void markLive(const std::vector<Chunk *>
 
     // 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())

Added: lld/trunk/test/COFF/reloc-discarded-early.s
URL: http://llvm.org/viewvc/llvm-project/lld/trunk/test/COFF/reloc-discarded-early.s?rev=318689&view=auto
==============================================================================
--- lld/trunk/test/COFF/reloc-discarded-early.s (added)
+++ lld/trunk/test/COFF/reloc-discarded-early.s Mon Nov 20 11:37:07 2017
@@ -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:

Added: lld/trunk/test/COFF/reloc-discarded-early2.s
URL: http://llvm.org/viewvc/llvm-project/lld/trunk/test/COFF/reloc-discarded-early2.s?rev=318689&view=auto
==============================================================================
--- lld/trunk/test/COFF/reloc-discarded-early2.s (added)
+++ lld/trunk/test/COFF/reloc-discarded-early2.s Mon Nov 20 11:37:07 2017
@@ -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:




More information about the llvm-commits mailing list