Skip to content

Commit 8435225

Browse files
authored
[LLD][COFF] Move addFile implementation to LinkerDriver (NFC) (#121342)
The addFile implementation does not rely on the SymbolTable object. With #119294, the symbol table for input files is determined during the construction of the objects representing them. To clarify that relationship, this change moves the implementation from the SymbolTable class to the LinkerDriver class.
1 parent 1135d36 commit 8435225

File tree

5 files changed

+84
-84
lines changed

5 files changed

+84
-84
lines changed

lld/COFF/Driver.cpp

Lines changed: 74 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -189,6 +189,71 @@ bool LinkerDriver::findUnderscoreMangle(StringRef sym) {
189189
return s && !isa<Undefined>(s);
190190
}
191191

192+
static bool compatibleMachineType(COFFLinkerContext &ctx, MachineTypes mt) {
193+
if (mt == IMAGE_FILE_MACHINE_UNKNOWN)
194+
return true;
195+
switch (ctx.config.machine) {
196+
case ARM64:
197+
return mt == ARM64 || mt == ARM64X;
198+
case ARM64EC:
199+
return isArm64EC(mt) || mt == AMD64;
200+
case ARM64X:
201+
return isAnyArm64(mt) || mt == AMD64;
202+
case IMAGE_FILE_MACHINE_UNKNOWN:
203+
return true;
204+
default:
205+
return ctx.config.machine == mt;
206+
}
207+
}
208+
209+
void LinkerDriver::addFile(InputFile *file) {
210+
Log(ctx) << "Reading " << toString(file);
211+
if (file->lazy) {
212+
if (auto *f = dyn_cast<BitcodeFile>(file))
213+
f->parseLazy();
214+
else
215+
cast<ObjFile>(file)->parseLazy();
216+
} else {
217+
file->parse();
218+
if (auto *f = dyn_cast<ObjFile>(file)) {
219+
ctx.objFileInstances.push_back(f);
220+
} else if (auto *f = dyn_cast<BitcodeFile>(file)) {
221+
if (ltoCompilationDone) {
222+
Err(ctx) << "LTO object file " << toString(file)
223+
<< " linked in after "
224+
"doing LTO compilation.";
225+
}
226+
ctx.bitcodeFileInstances.push_back(f);
227+
} else if (auto *f = dyn_cast<ImportFile>(file)) {
228+
ctx.importFileInstances.push_back(f);
229+
}
230+
}
231+
232+
MachineTypes mt = file->getMachineType();
233+
// The ARM64EC target must be explicitly specified and cannot be inferred.
234+
if (mt == ARM64EC &&
235+
(ctx.config.machine == IMAGE_FILE_MACHINE_UNKNOWN ||
236+
(ctx.config.machineInferred &&
237+
(ctx.config.machine == ARM64 || ctx.config.machine == AMD64)))) {
238+
Err(ctx) << toString(file)
239+
<< ": machine type arm64ec is ambiguous and cannot be "
240+
"inferred, use /machine:arm64ec or /machine:arm64x";
241+
return;
242+
}
243+
if (!compatibleMachineType(ctx, mt)) {
244+
Err(ctx) << toString(file) << ": machine type " << machineToStr(mt)
245+
<< " conflicts with " << machineToStr(ctx.config.machine);
246+
return;
247+
}
248+
if (ctx.config.machine == IMAGE_FILE_MACHINE_UNKNOWN &&
249+
mt != IMAGE_FILE_MACHINE_UNKNOWN) {
250+
ctx.config.machineInferred = true;
251+
setMachine(mt);
252+
}
253+
254+
parseDirectives(file);
255+
}
256+
192257
MemoryBufferRef LinkerDriver::takeBuffer(std::unique_ptr<MemoryBuffer> mb) {
193258
MemoryBufferRef mbref = *mb;
194259
make<std::unique_ptr<MemoryBuffer>>(std::move(mb)); // take ownership
@@ -222,25 +287,25 @@ void LinkerDriver::addBuffer(std::unique_ptr<MemoryBuffer> mb,
222287
addArchiveBuffer(m, "<whole-archive>", filename, memberIndex++);
223288
return;
224289
}
225-
ctx.symtab.addFile(make<ArchiveFile>(ctx, mbref));
290+
addFile(make<ArchiveFile>(ctx, mbref));
226291
break;
227292
case file_magic::bitcode:
228-
ctx.symtab.addFile(make<BitcodeFile>(ctx, mbref, "", 0, lazy));
293+
addFile(make<BitcodeFile>(ctx, mbref, "", 0, lazy));
229294
break;
230295
case file_magic::coff_object:
231296
case file_magic::coff_import_library:
232-
ctx.symtab.addFile(ObjFile::create(ctx, mbref, lazy));
297+
addFile(ObjFile::create(ctx, mbref, lazy));
233298
break;
234299
case file_magic::pdb:
235-
ctx.symtab.addFile(make<PDBInputFile>(ctx, mbref));
300+
addFile(make<PDBInputFile>(ctx, mbref));
236301
break;
237302
case file_magic::coff_cl_gl_object:
238303
Err(ctx) << filename
239304
<< ": is not a native COFF file. Recompile without /GL";
240305
break;
241306
case file_magic::pecoff_executable:
242307
if (ctx.config.mingw) {
243-
ctx.symtab.addFile(make<DLLFile>(ctx.symtab, mbref));
308+
addFile(make<DLLFile>(ctx.symtab, mbref));
244309
break;
245310
}
246311
if (filename.ends_with_insensitive(".dll")) {
@@ -306,7 +371,7 @@ void LinkerDriver::addArchiveBuffer(MemoryBufferRef mb, StringRef symName,
306371
if (magic == file_magic::coff_import_library) {
307372
InputFile *imp = make<ImportFile>(ctx, mb);
308373
imp->parentName = parentName;
309-
ctx.symtab.addFile(imp);
374+
addFile(imp);
310375
return;
311376
}
312377

@@ -326,7 +391,7 @@ void LinkerDriver::addArchiveBuffer(MemoryBufferRef mb, StringRef symName,
326391
}
327392

328393
obj->parentName = parentName;
329-
ctx.symtab.addFile(obj);
394+
addFile(obj);
330395
Log(ctx) << "Loaded " << obj << " for " << symName;
331396
}
332397

@@ -1400,7 +1465,7 @@ void LinkerDriver::convertResources() {
14001465
}
14011466
ObjFile *f =
14021467
ObjFile::create(ctx, convertResToCOFF(resources, resourceObjFiles));
1403-
ctx.symtab.addFile(f);
1468+
addFile(f);
14041469
f->includeResourceChunks();
14051470
}
14061471

@@ -2702,6 +2767,7 @@ void LinkerDriver::linkerMain(ArrayRef<const char *> argsArr) {
27022767
// Do LTO by compiling bitcode input files to a set of native COFF files then
27032768
// link those files (unless -thinlto-index-only was given, in which case we
27042769
// resolve symbols and write indices, but don't generate native code or link).
2770+
ltoCompilationDone = true;
27052771
ctx.symtab.compileBitcodeFiles();
27062772

27072773
if (Defined *d =

lld/COFF/Driver.h

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -80,13 +80,10 @@ class LinkerDriver {
8080

8181
void linkerMain(llvm::ArrayRef<const char *> args);
8282

83-
void setMachine(llvm::COFF::MachineTypes machine);
83+
void addFile(InputFile *file);
8484

8585
void addClangLibSearchPaths(const std::string &argv0);
8686

87-
// Used by the resolver to parse .drectve section contents.
88-
void parseDirectives(InputFile *file);
89-
9087
// Used by ArchiveFile to enqueue members.
9188
void enqueueArchiveMember(const Archive::Child &c, const Archive::Symbol &sym,
9289
StringRef parentName);
@@ -121,6 +118,7 @@ class LinkerDriver {
121118
// Symbol names are mangled by prepending "_" on x86.
122119
StringRef mangle(StringRef sym);
123120

121+
void setMachine(llvm::COFF::MachineTypes machine);
124122
llvm::Triple::ArchType getArch();
125123

126124
uint64_t getDefaultImageBase();
@@ -144,6 +142,9 @@ class LinkerDriver {
144142

145143
void createImportLibrary(bool asLib);
146144

145+
// Used by the resolver to parse .drectve section contents.
146+
void parseDirectives(InputFile *file);
147+
147148
void parseModuleDefs(StringRef path);
148149

149150
// Parse an /order file. If an option is given, the linker places COMDAT
@@ -279,6 +280,8 @@ class LinkerDriver {
279280
// Create export thunks for exported and patchable Arm64EC function symbols.
280281
void createECExportThunks();
281282
void maybeCreateECExportThunk(StringRef name, Symbol *&sym);
283+
284+
bool ltoCompilationDone = false;
282285
};
283286

284287
// Create enum with OPT_xxx values for each option in Options.td

lld/COFF/InputFiles.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1412,5 +1412,5 @@ void DLLFile::makeImport(DLLFile::Symbol *s) {
14121412
memcpy(p, s->dllName.data(), s->dllName.size());
14131413
MemoryBufferRef mbref = MemoryBufferRef(StringRef(buf, size), s->dllName);
14141414
ImportFile *impFile = make<ImportFile>(symtab.ctx, mbref);
1415-
symtab.addFile(impFile);
1415+
symtab.ctx.driver.addFile(impFile);
14161416
}

lld/COFF/SymbolTable.cpp

Lines changed: 2 additions & 68 deletions
Original file line numberDiff line numberDiff line change
@@ -37,71 +37,6 @@ StringRef ltrim1(StringRef s, const char *chars) {
3737
return s;
3838
}
3939

40-
static bool compatibleMachineType(COFFLinkerContext &ctx, MachineTypes mt) {
41-
if (mt == IMAGE_FILE_MACHINE_UNKNOWN)
42-
return true;
43-
switch (ctx.config.machine) {
44-
case ARM64:
45-
return mt == ARM64 || mt == ARM64X;
46-
case ARM64EC:
47-
return COFF::isArm64EC(mt) || mt == AMD64;
48-
case ARM64X:
49-
return COFF::isAnyArm64(mt) || mt == AMD64;
50-
case IMAGE_FILE_MACHINE_UNKNOWN:
51-
return true;
52-
default:
53-
return ctx.config.machine == mt;
54-
}
55-
}
56-
57-
void SymbolTable::addFile(InputFile *file) {
58-
Log(ctx) << "Reading " << toString(file);
59-
if (file->lazy) {
60-
if (auto *f = dyn_cast<BitcodeFile>(file))
61-
f->parseLazy();
62-
else
63-
cast<ObjFile>(file)->parseLazy();
64-
} else {
65-
file->parse();
66-
if (auto *f = dyn_cast<ObjFile>(file)) {
67-
ctx.objFileInstances.push_back(f);
68-
} else if (auto *f = dyn_cast<BitcodeFile>(file)) {
69-
if (ltoCompilationDone) {
70-
Err(ctx) << "LTO object file " << toString(file)
71-
<< " linked in after "
72-
"doing LTO compilation.";
73-
}
74-
ctx.bitcodeFileInstances.push_back(f);
75-
} else if (auto *f = dyn_cast<ImportFile>(file)) {
76-
ctx.importFileInstances.push_back(f);
77-
}
78-
}
79-
80-
MachineTypes mt = file->getMachineType();
81-
// The ARM64EC target must be explicitly specified and cannot be inferred.
82-
if (mt == ARM64EC &&
83-
(ctx.config.machine == IMAGE_FILE_MACHINE_UNKNOWN ||
84-
(ctx.config.machineInferred &&
85-
(ctx.config.machine == ARM64 || ctx.config.machine == AMD64)))) {
86-
Err(ctx) << toString(file)
87-
<< ": machine type arm64ec is ambiguous and cannot be "
88-
"inferred, use /machine:arm64ec or /machine:arm64x";
89-
return;
90-
}
91-
if (!compatibleMachineType(ctx, mt)) {
92-
Err(ctx) << toString(file) << ": machine type " << machineToStr(mt)
93-
<< " conflicts with " << machineToStr(ctx.config.machine);
94-
return;
95-
}
96-
if (ctx.config.machine == IMAGE_FILE_MACHINE_UNKNOWN &&
97-
mt != IMAGE_FILE_MACHINE_UNKNOWN) {
98-
ctx.config.machineInferred = true;
99-
ctx.driver.setMachine(mt);
100-
}
101-
102-
ctx.driver.parseDirectives(file);
103-
}
104-
10540
static COFFSyncStream errorOrWarn(COFFLinkerContext &ctx) {
10641
return {ctx, ctx.config.forceUnresolved ? DiagLevel::Warn : DiagLevel::Err};
10742
}
@@ -118,7 +53,7 @@ static void forceLazy(Symbol *s) {
11853
case Symbol::Kind::LazyObjectKind: {
11954
InputFile *file = cast<LazyObject>(s)->file;
12055
file->lazy = false;
121-
file->symtab.addFile(file);
56+
file->symtab.ctx.driver.addFile(file);
12257
break;
12358
}
12459
case Symbol::Kind::LazyDLLSymbolKind: {
@@ -776,7 +711,7 @@ void SymbolTable::addLazyObject(InputFile *f, StringRef n) {
776711
return;
777712
s->pendingArchiveLoad = true;
778713
f->lazy = false;
779-
addFile(f);
714+
ctx.driver.addFile(f);
780715
}
781716

782717
void SymbolTable::addLazyDLLSymbol(DLLFile *f, DLLFile::Symbol *sym,
@@ -1054,7 +989,6 @@ Symbol *SymbolTable::addUndefined(StringRef name) {
1054989
}
1055990

1056991
void SymbolTable::compileBitcodeFiles() {
1057-
ltoCompilationDone = true;
1058992
if (ctx.bitcodeFileInstances.empty())
1059993
return;
1060994

lld/COFF/SymbolTable.h

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -51,8 +51,6 @@ class SymbolTable {
5151
llvm::COFF::MachineTypes machine = IMAGE_FILE_MACHINE_UNKNOWN)
5252
: ctx(c), machine(machine) {}
5353

54-
void addFile(InputFile *file);
55-
5654
// Emit errors for symbols that cannot be resolved.
5755
void reportUnresolvable();
5856

@@ -155,7 +153,6 @@ class SymbolTable {
155153

156154
llvm::DenseMap<llvm::CachedHashStringRef, Symbol *> symMap;
157155
std::unique_ptr<BitcodeCompiler> lto;
158-
bool ltoCompilationDone = false;
159156
std::vector<std::pair<Symbol *, Symbol *>> entryThunks;
160157
llvm::DenseMap<Symbol *, Symbol *> exitThunks;
161158
};

0 commit comments

Comments
 (0)