Object: Improve COFF irsymtab comdat representation.

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

llvm-svn: 318805
This commit is contained in:
Peter Collingbourne 2017-11-21 22:06:20 +00:00
parent 0e8f8debcf
commit b05610e9e4
2 changed files with 57 additions and 13 deletions

View 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()) {

View 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