Skip to content

Commit

Permalink
[LLD][COFF] Do another pass of resolveRemainingUndefines for undefine…
Browse files Browse the repository at this point in the history
…d lazy symbols
  • Loading branch information
glandium committed Sep 18, 2024
1 parent f8eceb4 commit c44939f
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 3 deletions.
5 changes: 4 additions & 1 deletion lld/COFF/Driver.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2601,7 +2601,10 @@ void LinkerDriver::linkerMain(ArrayRef<const char *> argsArr) {
createECExportThunks();

// Resolve remaining undefined symbols and warn about imported locals.
ctx.symtab.resolveRemainingUndefines();
if (ctx.symtab.resolveRemainingUndefines()) {
run();
ctx.symtab.resolveRemainingUndefines();
}
if (errorCount())
return;

Expand Down
9 changes: 8 additions & 1 deletion lld/COFF/SymbolTable.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -479,10 +479,11 @@ void SymbolTable::reportUnresolvable() {
/* localImports */ nullptr, true);
}

void SymbolTable::resolveRemainingUndefines() {
bool SymbolTable::resolveRemainingUndefines() {
llvm::TimeTraceScope timeScope("Resolve remaining undefined symbols");
SmallPtrSet<Symbol *, 8> undefs;
DenseMap<Symbol *, Symbol *> localImports;
bool foundLazy = false;

for (auto &i : symMap) {
Symbol *sym = i.second;
Expand All @@ -502,6 +503,11 @@ void SymbolTable::resolveRemainingUndefines() {
// This odd rule is for compatibility with MSVC linker.
if (name.starts_with("__imp_")) {
Symbol *imp = find(name.substr(strlen("__imp_")));
if (imp && imp->isLazy()) {
forceLazy(imp);
foundLazy = true;
continue;
}
if (imp && isa<Defined>(imp)) {
auto *d = cast<Defined>(imp);
replaceSymbol<DefinedLocalImport>(sym, ctx, name, d);
Expand Down Expand Up @@ -529,6 +535,7 @@ void SymbolTable::resolveRemainingUndefines() {
reportProblemSymbols(
ctx, undefs,
ctx.config.warnLocallyDefinedImported ? &localImports : nullptr, false);
return foundLazy;
}

std::pair<Symbol *, bool> SymbolTable::insert(StringRef name) {
Expand Down
5 changes: 4 additions & 1 deletion lld/COFF/SymbolTable.h
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,10 @@ class SymbolTable {
// Try to resolve any undefined symbols and update the symbol table
// accordingly, then print an error message for any remaining undefined
// symbols and warn about imported local symbols.
void resolveRemainingUndefines();
// Returns whether more files might need to be linked in to resolve lazy
// symbols, in which case the caller is expected to call the function again
// after linking those files.
bool resolveRemainingUndefines();

// Load lazy objects that are needed for MinGW automatic import and for
// doing stdcall fixups.
Expand Down

0 comments on commit c44939f

Please sign in to comment.