Skip to content

Commit 5b0e45c

Browse files
authored
[lld][WebAssembly] Fix use of undefined funcs under --warn-unresolved-symbols (#78643)
When undefined functions exist in the final link we need to create stub functions (otherwise direct calls to those functions could not be generated). We were creating those stub when `--unresolved-symbols=ignore-all` was passed but overlooked the fact that `--warn-unresolved-symbols` essentially has the same effect (i.e. undefined function can exist in the final link). Fixes: #53987
1 parent b667783 commit 5b0e45c

File tree

2 files changed

+23
-17
lines changed

2 files changed

+23
-17
lines changed

lld/test/wasm/unresolved-symbols.s

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -18,10 +18,15 @@
1818
# RUN: FileCheck -check-prefix=ERR1 %s
1919

2020
## Ignore all should not produce error and should not produce
21-
# any imports. It should create a stub function in the place of the missing
22-
# function symbol.
21+
## any imports. It should create a stub function in the place of the missing
22+
## function symbol.
2323
# RUN: wasm-ld %t1.o -o %t2.wasm --unresolved-symbols=ignore-all
2424
# RUN: obj2yaml %t2.wasm | FileCheck -check-prefix=IGNORE %s
25+
26+
## --warn-unresolved-symbols should behave the same
27+
# RUN: wasm-ld %t1.o -o %t2.wasm --warn-unresolved-symbols
28+
# RUN: obj2yaml %t2.wasm | FileCheck -check-prefix=IGNORE %s
29+
2530
# IGNORE-NOT: - Type: IMPORT
2631
# IGNORE-NOT: - Type: ELEM
2732
#
@@ -52,10 +57,10 @@
5257
# IGNORE-NEXT: - Index: 3
5358
# IGNORE-NEXT: Name: get_func_addr
5459

55-
## --import-undefined should handle unresolved funtions symbols
56-
# by importing them but still report errors/warning for missing data symbols.
57-
# `--allow-undefined` should behave like `--import-undefined` +
58-
# `--unresolve-symbols=ignore`
60+
## --import-undefined should handle unresolved functions symbols
61+
## by importing them but still report errors/warning for missing data symbols.
62+
## `--allow-undefined` should behave like `--import-undefined` +
63+
## `--unresolve-symbols=ignore`
5964
# RUN: wasm-ld %t1.o -o %t3.wasm --import-undefined --unresolved-symbols=ignore-all
6065
# RUN: obj2yaml %t3.wasm | FileCheck -check-prefix=IMPORT %s
6166
# IMPORT: - Type: IMPORT

lld/wasm/Relocations.cpp

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -54,21 +54,22 @@ static void reportUndefined(Symbol *sym) {
5454
case UnresolvedPolicy::Ignore:
5555
LLVM_DEBUG(dbgs() << "ignoring undefined symbol: " + toString(*sym) +
5656
"\n");
57-
if (!config->importUndefined) {
58-
if (auto *f = dyn_cast<UndefinedFunction>(sym)) {
59-
if (!f->stubFunction) {
60-
f->stubFunction = symtab->createUndefinedStub(*f->getSignature());
61-
f->stubFunction->markLive();
62-
// Mark the function itself as a stub which prevents it from being
63-
// assigned a table entry.
64-
f->isStub = true;
65-
}
66-
}
67-
}
6857
break;
6958
case UnresolvedPolicy::ImportDynamic:
7059
break;
7160
}
61+
62+
if (auto *f = dyn_cast<UndefinedFunction>(sym)) {
63+
if (!f->stubFunction &&
64+
config->unresolvedSymbols != UnresolvedPolicy::ImportDynamic &&
65+
!config->importUndefined) {
66+
f->stubFunction = symtab->createUndefinedStub(*f->getSignature());
67+
f->stubFunction->markLive();
68+
// Mark the function itself as a stub which prevents it from being
69+
// assigned a table entry.
70+
f->isStub = true;
71+
}
72+
}
7273
}
7374
}
7475

0 commit comments

Comments
 (0)