[PATCH] D41269: [COFF] Warn for locally imported symbols

Shoaib Meenai via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Thu Dec 14 18:52:21 PST 2017


smeenai created this revision.
smeenai added a reviewer: ruiu.

Locally imported symbols are a very surprising linker feature. link.exe
warns for them, and we should warn too.


Repository:
  rLLD LLVM Linker

https://reviews.llvm.org/D41269

Files:
  COFF/SymbolTable.cpp
  test/COFF/locally-imported.test


Index: test/COFF/locally-imported.test
===================================================================
--- test/COFF/locally-imported.test
+++ test/COFF/locally-imported.test
@@ -1,8 +1,10 @@
 # RUN: yaml2obj < %s > %t.obj
-# RUN: lld-link /out:%t.exe /entry:main %t.obj
+# RUN: lld-link /out:%t.exe /entry:main %t.obj 2>&1 | FileCheck -check-prefix=WARN %s
 # RUN: llvm-objdump -s %t.exe | FileCheck %s
 # RUN: llvm-readobj -coff-basereloc %t.exe | FileCheck -check-prefix=BASEREL %s
 
+# WARN: warning: [[INPUT:[^:]+]]: locally defined symbol imported: main (defined in [[INPUT]])
+
 # CHECK:      Contents of section .text:
 # CHECK-NEXT: 1000 00200000
 # CHECK:      Contents of section .rdata:
Index: COFF/SymbolTable.cpp
===================================================================
--- COFF/SymbolTable.cpp
+++ COFF/SymbolTable.cpp
@@ -63,6 +63,7 @@
 
 void SymbolTable::reportRemainingUndefines() {
   SmallPtrSet<Symbol *, 8> Undefs;
+  DenseMap<Symbol *, Symbol *> UndefsToLocalImports;
 
   for (auto &I : SymMap) {
     Symbol *Sym = I.second;
@@ -98,6 +99,7 @@
         auto *D = cast<Defined>(Imp);
         replaceSymbol<DefinedLocalImport>(Sym, Name, D);
         LocalImportChunks.push_back(cast<DefinedLocalImport>(Sym)->getChunk());
+        UndefsToLocalImports.try_emplace(Sym, D);
         continue;
       }
     }
@@ -109,17 +111,28 @@
     Undefs.insert(Sym);
   }
 
-  if (Undefs.empty())
+  if (Undefs.empty() && UndefsToLocalImports.empty())
     return;
 
-  for (Symbol *B : Config->GCRoot)
+  for (Symbol *B : Config->GCRoot) {
     if (Undefs.count(B))
       errorOrWarn("<root>: undefined symbol: " + B->getName());
+    if (Symbol *LI = UndefsToLocalImports.lookup(B))
+      warn("<root>: locally defined symbol imported: " + LI->getName() +
+           " (defined in " + toString(LI->getFile()) + ")");
+  }
 
-  for (ObjFile *File : ObjFile::Instances)
-    for (Symbol *Sym : File->getSymbols())
-      if (Sym && Undefs.count(Sym))
+  for (ObjFile *File : ObjFile::Instances) {
+    for (Symbol *Sym : File->getSymbols()) {
+      if (!Sym)
+        continue;
+      if (Undefs.count(Sym))
         errorOrWarn(toString(File) + ": undefined symbol: " + Sym->getName());
+      if (Symbol *LI = UndefsToLocalImports.lookup(Sym))
+        warn(toString(File) + ": locally defined symbol imported: " +
+             LI->getName() + " (defined in " + toString(LI->getFile()) + ")");
+    }
+  }
 }
 
 std::pair<Symbol *, bool> SymbolTable::insert(StringRef Name) {


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D41269.127060.patch
Type: text/x-patch
Size: 2511 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20171215/10f83e2c/attachment.bin>


More information about the llvm-commits mailing list