Skip to content

Commit 728cc00

Browse files
committed
[LLD] [COFF] Fix autoexport from LTO objects with comdat symbols
Make sure that comdat symbols also have a non-null dummy SectionChunk associated. This requires moving around an existing FIXME regarding comdats in LTO. Differential Revision: https://reviews.llvm.org/D103012
1 parent 7c234ae commit 728cc00

File tree

2 files changed

+37
-17
lines changed

2 files changed

+37
-17
lines changed

lld/COFF/InputFiles.cpp

Lines changed: 25 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -503,14 +503,9 @@ void ObjFile::handleComdatSelection(
503503
SectionChunk *leaderChunk = nullptr;
504504
COMDATType leaderSelection = IMAGE_COMDAT_SELECT_ANY;
505505

506-
if (leader->data) {
507-
leaderChunk = leader->getChunk();
508-
leaderSelection = leaderChunk->selection;
509-
} else {
510-
// FIXME: comdats from LTO files don't know their selection; treat them
511-
// as "any".
512-
selection = leaderSelection;
513-
}
506+
assert(leader->data && "Comdat leader without SectionChunk?");
507+
leaderChunk = leader->getChunk();
508+
leaderSelection = leaderChunk->selection;
514509

515510
if ((selection == IMAGE_COMDAT_SELECT_ANY &&
516511
leaderSelection == IMAGE_COMDAT_SELECT_LARGEST) ||
@@ -1051,10 +1046,22 @@ class FakeSection {
10511046
coff_section section;
10521047
};
10531048

1049+
// Convenience class for initializing a SectionChunk with specific flags.
1050+
class FakeSectionChunk {
1051+
public:
1052+
FakeSectionChunk(const coff_section *section) : chunk(nullptr, section) {
1053+
// FIXME: comdats from LTO files don't know their selection; treat them
1054+
// as "any".
1055+
chunk.selection = IMAGE_COMDAT_SELECT_ANY;
1056+
}
1057+
1058+
SectionChunk chunk;
1059+
};
1060+
10541061
FakeSection ltoTextSection(IMAGE_SCN_MEM_EXECUTE);
10551062
FakeSection ltoDataSection(IMAGE_SCN_CNT_INITIALIZED_DATA);
1056-
SectionChunk ltoTextSectionChunk(nullptr, &ltoTextSection.section);
1057-
SectionChunk ltoDataSectionChunk(nullptr, &ltoDataSection.section);
1063+
FakeSectionChunk ltoTextSectionChunk(&ltoTextSection.section);
1064+
FakeSectionChunk ltoDataSectionChunk(&ltoDataSection.section);
10581065
} // namespace
10591066

10601067
void BitcodeFile::parse() {
@@ -1069,9 +1076,9 @@ void BitcodeFile::parse() {
10691076
Symbol *sym;
10701077
SectionChunk *fakeSC = nullptr;
10711078
if (objSym.isExecutable())
1072-
fakeSC = &ltoTextSectionChunk;
1079+
fakeSC = &ltoTextSectionChunk.chunk;
10731080
else
1074-
fakeSC = &ltoDataSectionChunk;
1081+
fakeSC = &ltoDataSectionChunk.chunk;
10751082
if (objSym.isUndefined()) {
10761083
sym = symtab->addUndefined(symName, this, false);
10771084
} else if (objSym.isCommon()) {
@@ -1083,12 +1090,15 @@ void BitcodeFile::parse() {
10831090
Symbol *alias = symtab->addUndefined(saver.save(fallback));
10841091
checkAndSetWeakAlias(symtab, this, sym, alias);
10851092
} else if (comdatIndex != -1) {
1086-
if (symName == obj->getComdatTable()[comdatIndex])
1093+
if (symName == obj->getComdatTable()[comdatIndex]) {
10871094
sym = comdat[comdatIndex].first;
1088-
else if (comdat[comdatIndex].second)
1095+
if (cast<DefinedRegular>(sym)->data == nullptr)
1096+
cast<DefinedRegular>(sym)->data = &fakeSC->repl;
1097+
} else if (comdat[comdatIndex].second) {
10891098
sym = symtab->addRegular(this, symName, nullptr, fakeSC);
1090-
else
1099+
} else {
10911100
sym = symtab->addUndefined(symName, this, false);
1101+
}
10921102
} else {
10931103
sym = symtab->addRegular(this, symName, nullptr, fakeSC);
10941104
}

lld/test/COFF/export-all-lto.ll

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,18 +6,28 @@
66
; RUN: llvm-readobj --coff-exports %t.dll | grep Name: | FileCheck %s
77
; RUN: cat %t.def | FileCheck --check-prefix=IMPLIB %s
88

9+
; CHECK: Name: MyComdatFunc
910
; CHECK: Name: MyExtData
1011
; CHECK: Name: MyLibFunc
1112

12-
; IMPLIB: MyExtData @1 DATA
13-
; IMPLIB: MyLibFunc @2{{$}}
13+
; IMPLIB: MyComdatFunc @1{{$}}
14+
; IMPLIB: MyExtData @2 DATA
15+
; IMPLIB: MyLibFunc @3{{$}}
1416

1517
target datalayout = "e-m:w-p270:32:32-p271:32:32-p272:64:64-i64:64-f80:128-n8:16:32:64-S128"
1618
target triple = "x86_64-w64-windows-gnu"
1719

1820
@MyExtData = dso_local global i32 42, align 4
1921

22+
$MyComdatFunc = comdat any
23+
2024
define dso_local void @MyLibFunc() {
25+
entry:
26+
call void @MyComdatFunc()
27+
ret void
28+
}
29+
30+
define linkonce_odr void @MyComdatFunc() comdat {
2131
entry:
2232
ret void
2333
}

0 commit comments

Comments
 (0)