Skip to content

Commit 5b095ae

Browse files
committed
[lld][WebAssembly] Fix filename when reporting references to undefined symbols
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 ffca4ef commit 5b095ae

File tree

2 files changed

+37
-23
lines changed

2 files changed

+37
-23
lines changed

lld/test/wasm/unresolved-symbols.s

+33-19
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,33 @@
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 %t/main.o contains undefined symbol undef_func.
6+
# RUN: not wasm-ld --no-gc-sections %t/main.o %t/secondary.o -o /dev/null 2>&1 | \
57
# RUN: FileCheck -check-prefix=ERRUND %s
6-
# ERRUND: error: {{.*}}1.o: undefined symbol: undef_func
8+
# ERRUND: error: {{.*}}main.o: undefined symbol: undef_func
9+
# ERRUND: error: {{.*}}secondary.o: undefined symbol: undef_func
710

811
## 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 | \
12+
# RUN: not wasm-ld --no-gc-sections %t/main.o %t/secondary.o -o /dev/null --unresolved-symbols=report-all 2>&1 | \
1013
# RUN: FileCheck -check-prefix=ERRUND %s
1114

1215
## Error out if unknown option value was set.
13-
# RUN: not wasm-ld %t1.o -o /dev/null --unresolved-symbols=xxx 2>&1 | \
16+
# RUN: not wasm-ld %t/main.o -o /dev/null --unresolved-symbols=xxx 2>&1 | \
1417
# RUN: FileCheck -check-prefix=ERR1 %s
1518
# ERR1: unknown --unresolved-symbols value: xxx
1619
## Check alias.
17-
# RUN: not wasm-ld %t1.o -o /dev/null --unresolved-symbols xxx 2>&1 | \
20+
# RUN: not wasm-ld %t/main.o -o /dev/null --unresolved-symbols xxx 2>&1 | \
1821
# RUN: FileCheck -check-prefix=ERR1 %s
1922

2023
## Ignore all should not produce error and should not produce
2124
## any imports. It should create a stub function in the place of the missing
2225
## function symbol.
23-
# RUN: wasm-ld %t1.o -o %t2.wasm --unresolved-symbols=ignore-all
26+
# RUN: wasm-ld %t/main.o -o %t2.wasm --unresolved-symbols=ignore-all
2427
# RUN: obj2yaml %t2.wasm | FileCheck -check-prefix=IGNORE %s
2528

2629
## --warn-unresolved-symbols should behave the same
27-
# RUN: wasm-ld %t1.o -o %t2.wasm --warn-unresolved-symbols
30+
# RUN: wasm-ld %t/main.o -o %t2.wasm --warn-unresolved-symbols
2831
# RUN: obj2yaml %t2.wasm | FileCheck -check-prefix=IGNORE %s
2932

3033
# IGNORE-NOT: - Type: IMPORT
@@ -61,7 +64,7 @@
6164
## by importing them but still report errors/warning for missing data symbols.
6265
## `--allow-undefined` should behave like `--import-undefined` +
6366
## `--unresolve-symbols=ignore`
64-
# RUN: wasm-ld %t1.o -o %t3.wasm --import-undefined --unresolved-symbols=ignore-all
67+
# RUN: wasm-ld %t/main.o -o %t3.wasm --import-undefined --unresolved-symbols=ignore-all
6568
# RUN: obj2yaml %t3.wasm | FileCheck -check-prefix=IMPORT %s
6669
# IMPORT: - Type: IMPORT
6770
# IMPORT-NEXT: Imports:
@@ -72,23 +75,25 @@
7275
# IMPORT-NEXT: - Type: FUNCTION
7376

7477
## 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
78+
# RUN: not wasm-ld %t/main.o -o %t3.wasm --import-undefined --unresolved-symbols=report-all 2>&1 | FileCheck -check-prefix=IMPORTUNDEFINED %s
79+
# IMPORTUNDEFINED-NOT: error: {{.*}}main.o: undefined symbol: undef_func
80+
# IMPORTUNDEFINED: error: {{.*}}main.o: undefined symbol: undef_data
7881

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

83-
.functype undef_func () -> ()
84-
.functype get_data_addr () -> (i32)
85-
.functype get_func_addr () -> (i32)
86-
8786
## 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
87+
# RUN: not wasm-ld %t/main.o -o %t5.wasm --unresolved-symbols=import-dynamic 2>&1 | FileCheck -check-prefix=ERRNOPIC %s
8988
# ERRNOPIC: relocation R_WASM_MEMORY_ADDR_SLEB cannot be used against symbol `undef_data`; recompile with -fPIC
9089
# ERRNOPIC: relocation R_WASM_TABLE_INDEX_SLEB cannot be used against symbol `undef_func`; recompile with -fPIC
9190

91+
#--- main.s
92+
93+
.functype undef_func () -> ()
94+
.functype get_data_addr () -> (i32)
95+
.functype get_func_addr () -> (i32)
96+
9297
.globl _start
9398
_start:
9499
.functype _start () -> ()
@@ -112,3 +117,12 @@ get_func_addr:
112117
i32.const undef_func
113118
return
114119
end_function
120+
121+
#--- secondary.s
122+
123+
.functype undef_func () -> ()
124+
.globl foo
125+
foo:
126+
.functype foo () -> ()
127+
call undef_func
128+
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)