[lld] r346777 - [COFF] Simplify relocation to discarded section diagnostic code, NFC

Reid Kleckner via llvm-commits llvm-commits at lists.llvm.org
Tue Nov 13 10:30:31 PST 2018


Author: rnk
Date: Tue Nov 13 10:30:31 2018
New Revision: 346777

URL: http://llvm.org/viewvc/llvm-project?rev=346777&view=rev
Log:
[COFF] Simplify relocation to discarded section diagnostic code, NFC

Move it out of the loop that applies relocations for readability.

Modified:
    lld/trunk/COFF/Chunks.cpp

Modified: lld/trunk/COFF/Chunks.cpp
URL: http://llvm.org/viewvc/llvm-project/lld/trunk/COFF/Chunks.cpp?rev=346777&r1=346776&r2=346777&view=diff
==============================================================================
--- lld/trunk/COFF/Chunks.cpp (original)
+++ lld/trunk/COFF/Chunks.cpp Tue Nov 13 10:30:31 2018
@@ -316,6 +316,32 @@ void SectionChunk::applyRelARM64(uint8_t
   }
 }
 
+static void maybeReportRelocationToDiscarded(const SectionChunk *FromChunk,
+                                             Defined *Sym,
+                                             const coff_relocation &Rel) {
+  // Don't report these errors when the relocation comes from a debug info
+  // section or in mingw mode. MinGW mode object files (built by GCC) can
+  // have leftover sections with relocations against discarded comdat
+  // sections. Such sections are left as is, with relocations untouched.
+  if (FromChunk->isCodeView() || FromChunk->isDWARF() || Config->MinGW)
+    return;
+
+  // Get the name of the symbol. If it's null, it was discarded early, so we
+  // have to go back to the object file.
+  ObjFile *File = FromChunk->File;
+  StringRef Name;
+  if (Sym) {
+    Name = Sym->getName();
+  } else {
+    COFFSymbolRef COFFSym =
+        check(File->getCOFFObj()->getSymbol(Rel.SymbolTableIndex));
+    File->getCOFFObj()->getSymbolName(COFFSym, Name);
+  }
+
+  error("relocation against symbol in discarded section: " + Name +
+        getSymbolLocations(File, Rel.SymbolTableIndex));
+}
+
 void SectionChunk::writeTo(uint8_t *Buf) const {
   if (!hasData())
     return;
@@ -343,42 +369,23 @@ void SectionChunk::writeTo(uint8_t *Buf)
     // Use the potentially remapped Symbol instead of the one that the
     // relocation points to.
     auto *Sym = dyn_cast_or_null<Defined>(RelocTargets[I]);
-    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);
-
-      // MinGW mode object files (built by GCC) can have leftover sections
-      // with relocations against discarded comdat sections. Such sections
-      // are left as is, with relocations untouched.
-      if (!Config->MinGW)
-        error("relocation against symbol in discarded section: " + Name +
-              getSymbolLocations(File, Rel.SymbolTableIndex));
-      continue;
-    }
+
     // 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.
-    Chunk *C = Sym->getChunk();
+    Chunk *C = Sym ? Sym->getChunk() : nullptr;
     OutputSection *OS = C ? C->getOutputSection() : nullptr;
 
-    // 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 (isCodeView() || isDWARF())
-        continue;
-      error("relocation against symbol in discarded section: " +
-            Sym->getName() + getSymbolLocations(File, Rel.SymbolTableIndex));
+    // Skip the relocation if it refers to a discarded section, and diagnose it
+    // as an error if appropriate. If a symbol was discarded early, it may be
+    // null. If it was discarded late, the output section will be null, unless
+    // it was an absolute or synthetic symbol.
+    if (!Sym ||
+        (!OS && !isa<DefinedAbsolute>(Sym) && !isa<DefinedSynthetic>(Sym))) {
+      maybeReportRelocationToDiscarded(this, Sym, Rel);
       continue;
     }
+
     uint64_t S = Sym->getRVA();
 
     // Compute the RVA of the relocation for relative relocations.




More information about the llvm-commits mailing list