]> granicus.if.org Git - llvm/commitdiff
Object: Improve COFF irsymtab comdat representation.
authorPeter Collingbourne <peter@pcc.me.uk>
Tue, 21 Nov 2017 22:06:20 +0000 (22:06 +0000)
committerPeter Collingbourne <peter@pcc.me.uk>
Tue, 21 Nov 2017 22:06:20 +0000 (22:06 +0000)
Change the representation of COFF comdats so that a COFF linker
is able to accurately resolve comdats between IR and native object
files. Specifically, apply name mangling to comdat names consistently
with native object files, and do not export comdats with an internal
leader because they do not affect symbol resolution.

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

git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@318805 91177308-0d34-0410-b5e6-96231b3b80d8

lib/Object/IRSymtab.cpp
test/LTO/Resolution/X86/symtab.ll

index 7fc84e2046cd7e4878224f1dbc5e7fd852d0e029..2d8d3f7c087807856a0954a8b237350ffa33f36a 100644 (file)
@@ -72,7 +72,7 @@ struct Builder {
           BumpPtrAllocator &Alloc)
       : Symtab(Symtab), StrtabBuilder(StrtabBuilder), Saver(Alloc) {}
 
-  DenseMap<const Comdat *, unsigned> ComdatMap;
+  DenseMap<const Comdat *, int> ComdatMap;
   Mangler Mang;
   Triple TT;
 
@@ -97,6 +97,8 @@ struct Builder {
                   reinterpret_cast<const char *>(Objs.data() + Objs.size()));
   }
 
+  Expected<int> getComdatIndex(const Comdat *C, const Module *M);
+
   Error addModule(Module *M);
   Error addSymbol(const ModuleSymbolTable &Msymtab,
                   const SmallPtrSet<GlobalValue *, 8> &Used,
@@ -140,6 +142,35 @@ Error Builder::addModule(Module *M) {
   return Error::success();
 }
 
+Expected<int> Builder::getComdatIndex(const Comdat *C, const Module *M) {
+  auto P = ComdatMap.insert(std::make_pair(C, Comdats.size()));
+  if (P.second) {
+    std::string Name;
+    if (TT.isOSBinFormatCOFF()) {
+      const GlobalValue *GV = M->getNamedValue(C->getName());
+      if (!GV)
+        return make_error<StringError>("Could not find leader",
+                                       inconvertibleErrorCode());
+      // Internal leaders do not affect symbol resolution, therefore they do not
+      // appear in the symbol table.
+      if (GV->hasLocalLinkage()) {
+        P.first->second = -1;
+        return -1;
+      }
+      llvm::raw_string_ostream OS(Name);
+      Mang.getNameWithPrefix(OS, GV, false);
+    } else {
+      Name = C->getName();
+    }
+
+    storage::Comdat Comdat;
+    setStr(Comdat.Name, Saver.save(Name));
+    Comdats.push_back(Comdat);
+  }
+
+  return P.first->second;
+}
+
 Error Builder::addSymbol(const ModuleSymbolTable &Msymtab,
                          const SmallPtrSet<GlobalValue *, 8> &Used,
                          ModuleSymbolTable::Symbol Msym) {
@@ -216,14 +247,10 @@ Error Builder::addSymbol(const ModuleSymbolTable &Msymtab,
     return make_error<StringError>("Unable to determine comdat of alias!",
                                    inconvertibleErrorCode());
   if (const Comdat *C = Base->getComdat()) {
-    auto P = ComdatMap.insert(std::make_pair(C, Comdats.size()));
-    Sym.ComdatIndex = P.first->second;
-
-    if (P.second) {
-      storage::Comdat Comdat;
-      setStr(Comdat.Name, C->getName());
-      Comdats.push_back(Comdat);
-    }
+    Expected<int> ComdatIndexOrErr = getComdatIndex(C, GV->getParent());
+    if (!ComdatIndexOrErr)
+      return ComdatIndexOrErr.takeError();
+    Sym.ComdatIndex = *ComdatIndexOrErr;
   }
 
   if (TT.isOSBinFormatCOFF()) {
index fecea0a1e7b477088e6885a72d51978d195a27af..c43494a5b7e03ff95ac574bb725facc54c9c794c 100644 (file)
@@ -17,6 +17,15 @@ define i32 @fun() {
   ret i32 0
 }
 
+; CHECK: D------X @fun2@8
+; CHECK-NEXT: comdat @fun2@8
+$fun2 = comdat any
+define x86_fastcallcc i32 @fun2(i32 inreg %a, i32 inreg %b) comdat {
+entry:
+  %add = add nsw i32 %b, %a
+  ret i32 %add
+}
+
 ; CHECK: H------- _g1
 @g1 = hidden global i32 0
 
@@ -43,11 +52,19 @@ define i32 @fun() {
 @g8 = common global i32 0, align 8
 
 ; CHECK: D------- _g9
-; CHECK-NEXT: comdat g9
+; CHECK-NEXT: comdat _g9
 $g9 = comdat any
 @g9 = global i32 0, comdat
 
-; CHECK: D--WI--- _g10
-; CHECK-NEXT: comdat g9
+; CHECK-NOT: _g10
+$g10 = comdat any
+@g10 = internal global i32 0, comdat
+
+; CHECK: D------- _g11
+; CHECK-NOT: comdat
+@g11 = global i32 0, comdat($g10)
+
+; CHECK: D--WI--- _a1
+; CHECK-NEXT: comdat _g9
 ; CHECK-NEXT: fallback _g9
-@g10 = weak alias i32, i32* @g9
+@a1 = weak alias i32, i32* @g9