Skip to content

Commit afb3f7e

Browse files
authored
[lld][WebAssembly] Handle stub symbol dependencies when an explicit import name is used (#80169)
1 parent 869f551 commit afb3f7e

File tree

4 files changed

+60
-42
lines changed

4 files changed

+60
-42
lines changed

lld/test/wasm/Inputs/libstub.so

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
#STUB
22
# This is a comment
3-
foo: foodep1,foodep2
3+
foo_import: foodep1,foodep2
44
# This symbols as no dependencies
55
bar
66
baz: bazdep

lld/test/wasm/stub-library-archive.s

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
# depeds on baz which is also defined in libstub.so.
1515

1616
.functype foo () -> ()
17+
.import_name foo, foo_import
1718

1819
.globl _start
1920
_start:

lld/test/wasm/stub-library.s

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,15 +6,12 @@
66
# RUN: not wasm-ld %t.o %p/Inputs/libstub-missing-dep.so -o %t.wasm 2>&1 | FileCheck --check-prefix=MISSING-DEP %s
77

88
# When the dependencies are missing the link fails
9-
# RUN: not wasm-ld %t.o %p/Inputs/libstub-missing-sym.so -o %t.wasm 2>&1 | FileCheck --check-prefix=MISSING-SYM %s
10-
119
# MISSING-DEP: libstub-missing-dep.so: undefined symbol: missing_dep. Required by foo
1210
# MISSING-DEP: libstub-missing-dep.so: undefined symbol: missing_dep2. Required by foo
1311

14-
# MISSING-SYM: undefined symbol: foo
15-
1612
# The function foo is defined in libstub.so but depend on foodep1 and foodep2
1713
.functype foo () -> ()
14+
.import_name foo, foo_import
1815

1916
.globl foodep1
2017
foodep1:

lld/wasm/Driver.cpp

Lines changed: 57 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -955,6 +955,47 @@ static void processStubLibrariesPreLTO() {
955955
}
956956
}
957957

958+
static bool addStubSymbolDeps(const StubFile *stub_file, Symbol *sym,
959+
ArrayRef<StringRef> deps) {
960+
// The first stub library to define a given symbol sets this and
961+
// definitions in later stub libraries are ignored.
962+
if (sym->forceImport)
963+
return false; // Already handled
964+
sym->forceImport = true;
965+
if (sym->traced)
966+
message(toString(stub_file) + ": importing " + sym->getName());
967+
else
968+
LLVM_DEBUG(llvm::dbgs() << toString(stub_file) << ": importing "
969+
<< sym->getName() << "\n");
970+
bool depsAdded = false;
971+
for (const auto dep : deps) {
972+
auto *needed = symtab->find(dep);
973+
if (!needed) {
974+
error(toString(stub_file) + ": undefined symbol: " + dep +
975+
". Required by " + toString(*sym));
976+
} else if (needed->isUndefined()) {
977+
error(toString(stub_file) + ": undefined symbol: " + toString(*needed) +
978+
". Required by " + toString(*sym));
979+
} else {
980+
if (needed->traced)
981+
message(toString(stub_file) + ": exported " + toString(*needed) +
982+
" due to import of " + sym->getName());
983+
else
984+
LLVM_DEBUG(llvm::dbgs()
985+
<< "force export: " << toString(*needed) << "\n");
986+
needed->forceExport = true;
987+
if (auto *lazy = dyn_cast<LazySymbol>(needed)) {
988+
depsAdded = true;
989+
lazy->extract();
990+
if (!config->whyExtract.empty())
991+
ctx.whyExtractRecords.emplace_back(toString(stub_file),
992+
sym->getFile(), *sym);
993+
}
994+
}
995+
}
996+
return depsAdded;
997+
}
998+
958999
static void processStubLibraries() {
9591000
log("-- processStubLibraries");
9601001
bool depsAdded = false;
@@ -963,49 +1004,28 @@ static void processStubLibraries() {
9631004
for (auto &stub_file : ctx.stubFiles) {
9641005
LLVM_DEBUG(llvm::dbgs()
9651006
<< "processing stub file: " << stub_file->getName() << "\n");
1007+
1008+
// First look for any imported symbols that directly match
1009+
// the names of the stub imports
9661010
for (auto [name, deps]: stub_file->symbolDependencies) {
9671011
auto* sym = symtab->find(name);
968-
if (!sym || !sym->isUndefined()) {
1012+
if (sym && sym->isUndefined()) {
1013+
depsAdded |= addStubSymbolDeps(stub_file, sym, deps);
1014+
} else {
9691015
if (sym && sym->traced)
9701016
message(toString(stub_file) + ": stub symbol not needed: " + name);
9711017
else
972-
LLVM_DEBUG(llvm::dbgs() << "stub symbol not needed: `" << name << "`\n");
973-
continue;
1018+
LLVM_DEBUG(llvm::dbgs()
1019+
<< "stub symbol not needed: `" << name << "`\n");
9741020
}
975-
// The first stub library to define a given symbol sets this and
976-
// definitions in later stub libraries are ignored.
977-
if (sym->forceImport)
978-
continue; // Already handled
979-
sym->forceImport = true;
980-
if (sym->traced)
981-
message(toString(stub_file) + ": importing " + name);
982-
else
983-
LLVM_DEBUG(llvm::dbgs()
984-
<< toString(stub_file) << ": importing " << name << "\n");
985-
for (const auto dep : deps) {
986-
auto* needed = symtab->find(dep);
987-
if (!needed) {
988-
error(toString(stub_file) + ": undefined symbol: " + dep +
989-
". Required by " + toString(*sym));
990-
} else if (needed->isUndefined()) {
991-
error(toString(stub_file) +
992-
": undefined symbol: " + toString(*needed) +
993-
". Required by " + toString(*sym));
994-
} else {
995-
if (needed->traced)
996-
message(toString(stub_file) + ": exported " + toString(*needed) +
997-
" due to import of " + name);
998-
else
999-
LLVM_DEBUG(llvm::dbgs()
1000-
<< "force export: " << toString(*needed) << "\n");
1001-
needed->forceExport = true;
1002-
if (auto *lazy = dyn_cast<LazySymbol>(needed)) {
1003-
depsAdded = true;
1004-
lazy->extract();
1005-
if (!config->whyExtract.empty())
1006-
ctx.whyExtractRecords.emplace_back(stub_file->getName(),
1007-
sym->getFile(), *sym);
1008-
}
1021+
}
1022+
1023+
// Secondly looks for any symbols with an `importName` that matches
1024+
for (Symbol *sym : symtab->symbols()) {
1025+
if (sym->isUndefined() && sym->importName.has_value()) {
1026+
auto it = stub_file->symbolDependencies.find(sym->importName.value());
1027+
if (it != stub_file->symbolDependencies.end()) {
1028+
depsAdded |= addStubSymbolDeps(stub_file, sym, it->second);
10091029
}
10101030
}
10111031
}

0 commit comments

Comments
 (0)