Skip to content

Commit 4d88837

Browse files
authored
[lld][WebAssembly] Fix filename when reporting references to undefined symbols (#97444)
When an undefined symbol is referenced from more than one file we were reporting all undefined symbols as originating from just one of them. This came up while working on WebAssembly/tool-conventions#158 where undefined symbols in one object file were being reported as coming from another.
1 parent 0fb3351 commit 4d88837

File tree

2 files changed

+38
-23
lines changed

2 files changed

+38
-23
lines changed

lld/test/wasm/unresolved-symbols.s

+34-19
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,34 @@
1-
# RUN: llvm-mc -filetype=obj -triple=wasm32-unknown-unknown %s -o %t1.o
1+
# RUN: split-file %s %t
2+
# RUN: llvm-mc -filetype=obj -triple=wasm32-unknown-unknown %t/main.s -o %t/main.o
3+
# RUN: llvm-mc -filetype=obj -triple=wasm32-unknown-unknown %t/secondary.s -o %t/secondary.o
24

3-
## Check that %t1.o contains undefined symbol undef_func.
4-
# RUN: not wasm-ld %t1.o -o /dev/null 2>&1 | \
5+
## Check that both main.o and secondary.o contain references to the same
6+
## undefined function and that both are correctly reported.
7+
# RUN: not wasm-ld --no-gc-sections %t/main.o %t/secondary.o -o /dev/null 2>&1 | \
58
# RUN: FileCheck -check-prefix=ERRUND %s
6-
# ERRUND: error: {{.*}}1.o: undefined symbol: undef_func
9+
# ERRUND: error: {{.*}}main.o: undefined symbol: undef_func
10+
# ERRUND: error: {{.*}}secondary.o: undefined symbol: undef_func
711

812
## report-all is the default one. Check that we get the same error
9-
# RUN: not wasm-ld %t1.o -o /dev/null --unresolved-symbols=report-all 2>&1 | \
13+
# RUN: not wasm-ld --no-gc-sections %t/main.o %t/secondary.o -o /dev/null --unresolved-symbols=report-all 2>&1 | \
1014
# RUN: FileCheck -check-prefix=ERRUND %s
1115

1216
## Error out if unknown option value was set.
13-
# RUN: not wasm-ld %t1.o -o /dev/null --unresolved-symbols=xxx 2>&1 | \
17+
# RUN: not wasm-ld %t/main.o -o /dev/null --unresolved-symbols=xxx 2>&1 | \
1418
# RUN: FileCheck -check-prefix=ERR1 %s
1519
# ERR1: unknown --unresolved-symbols value: xxx
1620
## Check alias.
17-
# RUN: not wasm-ld %t1.o -o /dev/null --unresolved-symbols xxx 2>&1 | \
21+
# RUN: not wasm-ld %t/main.o -o /dev/null --unresolved-symbols xxx 2>&1 | \
1822
# RUN: FileCheck -check-prefix=ERR1 %s
1923

2024
## Ignore all should not produce error and should not produce
2125
## any imports. It should create a stub function in the place of the missing
2226
## function symbol.
23-
# RUN: wasm-ld %t1.o -o %t2.wasm --unresolved-symbols=ignore-all
27+
# RUN: wasm-ld %t/main.o -o %t2.wasm --unresolved-symbols=ignore-all
2428
# RUN: obj2yaml %t2.wasm | FileCheck -check-prefix=IGNORE %s
2529

2630
## --warn-unresolved-symbols should behave the same
27-
# RUN: wasm-ld %t1.o -o %t2.wasm --warn-unresolved-symbols
31+
# RUN: wasm-ld %t/main.o -o %t2.wasm --warn-unresolved-symbols
2832
# RUN: obj2yaml %t2.wasm | FileCheck -check-prefix=IGNORE %s
2933

3034
# IGNORE-NOT: - Type: IMPORT
@@ -61,7 +65,7 @@
6165
## by importing them but still report errors/warning for missing data symbols.
6266
## `--allow-undefined` should behave like `--import-undefined` +
6367
## `--unresolve-symbols=ignore`
64-
# RUN: wasm-ld %t1.o -o %t3.wasm --import-undefined --unresolved-symbols=ignore-all
68+
# RUN: wasm-ld %t/main.o -o %t3.wasm --import-undefined --unresolved-symbols=ignore-all
6569
# RUN: obj2yaml %t3.wasm | FileCheck -check-prefix=IMPORT %s
6670
# IMPORT: - Type: IMPORT
6771
# IMPORT-NEXT: Imports:
@@ -72,23 +76,25 @@
7276
# IMPORT-NEXT: - Type: FUNCTION
7377

7478
## Check that --import-undefined reports unresolved data symbols.
75-
# RUN: not wasm-ld %t1.o -o %t3.wasm --import-undefined --unresolved-symbols=report-all 2>&1 | FileCheck -check-prefix=IMPORTUNDEFINED %s
76-
# IMPORTUNDEFINED-NOT: error: {{.*}}1.o: undefined symbol: undef_func
77-
# IMPORTUNDEFINED: error: {{.*}}1.o: undefined symbol: undef_data
79+
# RUN: not wasm-ld %t/main.o -o %t3.wasm --import-undefined --unresolved-symbols=report-all 2>&1 | FileCheck -check-prefix=IMPORTUNDEFINED %s
80+
# IMPORTUNDEFINED-NOT: error: {{.*}}main.o: undefined symbol: undef_func
81+
# IMPORTUNDEFINED: error: {{.*}}main.o: undefined symbol: undef_data
7882

7983
## Do not report undefines if linking relocatable.
80-
# RUN: wasm-ld -r %t1.o -o %t4.wasm --unresolved-symbols=report-all
84+
# RUN: wasm-ld -r %t/main.o -o %t4.wasm --unresolved-symbols=report-all
8185
# RUN: llvm-readobj %t4.wasm > /dev/null 2>&1
8286

83-
.functype undef_func () -> ()
84-
.functype get_data_addr () -> (i32)
85-
.functype get_func_addr () -> (i32)
86-
8787
## import-dynamic should fail due to incompatible relocations.
88-
# RUN: not wasm-ld %t1.o -o %t5.wasm --unresolved-symbols=import-dynamic 2>&1 | FileCheck -check-prefix=ERRNOPIC %s
88+
# RUN: not wasm-ld %t/main.o -o %t5.wasm --unresolved-symbols=import-dynamic 2>&1 | FileCheck -check-prefix=ERRNOPIC %s
8989
# ERRNOPIC: relocation R_WASM_MEMORY_ADDR_SLEB cannot be used against symbol `undef_data`; recompile with -fPIC
9090
# ERRNOPIC: relocation R_WASM_TABLE_INDEX_SLEB cannot be used against symbol `undef_func`; recompile with -fPIC
9191

92+
#--- main.s
93+
94+
.functype undef_func () -> ()
95+
.functype get_data_addr () -> (i32)
96+
.functype get_func_addr () -> (i32)
97+
9298
.globl _start
9399
_start:
94100
.functype _start () -> ()
@@ -112,3 +118,12 @@ get_func_addr:
112118
i32.const undef_func
113119
return
114120
end_function
121+
122+
#--- secondary.s
123+
124+
.functype undef_func () -> ()
125+
.globl foo
126+
foo:
127+
.functype foo () -> ()
128+
call undef_func
129+
end_function

lld/wasm/Relocations.cpp

+4-4
Original file line numberDiff line numberDiff line change
@@ -42,14 +42,14 @@ static bool allowUndefined(const Symbol* sym) {
4242
return config->allowUndefinedSymbols.count(sym->getName()) != 0;
4343
}
4444

45-
static void reportUndefined(Symbol *sym) {
45+
static void reportUndefined(ObjFile *file, Symbol *sym) {
4646
if (!allowUndefined(sym)) {
4747
switch (config->unresolvedSymbols) {
4848
case UnresolvedPolicy::ReportError:
49-
error(toString(sym->getFile()) + ": undefined symbol: " + toString(*sym));
49+
error(toString(file) + ": undefined symbol: " + toString(*sym));
5050
break;
5151
case UnresolvedPolicy::Warn:
52-
warn(toString(sym->getFile()) + ": undefined symbol: " + toString(*sym));
52+
warn(toString(file) + ": undefined symbol: " + toString(*sym));
5353
break;
5454
case UnresolvedPolicy::Ignore:
5555
LLVM_DEBUG(dbgs() << "ignoring undefined symbol: " + toString(*sym) +
@@ -171,7 +171,7 @@ void scanRelocations(InputChunk *chunk) {
171171
}
172172
} else if (sym->isUndefined() && !config->relocatable && !sym->isWeak()) {
173173
// Report undefined symbols
174-
reportUndefined(sym);
174+
reportUndefined(file, sym);
175175
}
176176
}
177177
}

0 commit comments

Comments
 (0)