Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[lld][WebAssembly] Fix filename when reporting references to undefined symbols #97444

Merged
merged 1 commit into from
Jul 2, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
53 changes: 34 additions & 19 deletions lld/test/wasm/unresolved-symbols.s
Original file line number Diff line number Diff line change
@@ -1,30 +1,34 @@
# RUN: llvm-mc -filetype=obj -triple=wasm32-unknown-unknown %s -o %t1.o
# RUN: split-file %s %t
# RUN: llvm-mc -filetype=obj -triple=wasm32-unknown-unknown %t/main.s -o %t/main.o
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

TIL split-file is a thing...

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah its kind of nice! One downside is that the test files themselves are less independently useful, but thats seems like a minor things compared to the hassle of splitting out secondary files into the Input/ diretory.

# RUN: llvm-mc -filetype=obj -triple=wasm32-unknown-unknown %t/secondary.s -o %t/secondary.o

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

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

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

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

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

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

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

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

.functype undef_func () -> ()
.functype get_data_addr () -> (i32)
.functype get_func_addr () -> (i32)

## import-dynamic should fail due to incompatible relocations.
# RUN: not wasm-ld %t1.o -o %t5.wasm --unresolved-symbols=import-dynamic 2>&1 | FileCheck -check-prefix=ERRNOPIC %s
# RUN: not wasm-ld %t/main.o -o %t5.wasm --unresolved-symbols=import-dynamic 2>&1 | FileCheck -check-prefix=ERRNOPIC %s
# ERRNOPIC: relocation R_WASM_MEMORY_ADDR_SLEB cannot be used against symbol `undef_data`; recompile with -fPIC
# ERRNOPIC: relocation R_WASM_TABLE_INDEX_SLEB cannot be used against symbol `undef_func`; recompile with -fPIC

#--- main.s

.functype undef_func () -> ()
.functype get_data_addr () -> (i32)
.functype get_func_addr () -> (i32)

.globl _start
_start:
.functype _start () -> ()
Expand All @@ -112,3 +118,12 @@ get_func_addr:
i32.const undef_func
return
end_function

#--- secondary.s

.functype undef_func () -> ()
.globl foo
foo:
.functype foo () -> ()
call undef_func
end_function
8 changes: 4 additions & 4 deletions lld/wasm/Relocations.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -42,14 +42,14 @@ static bool allowUndefined(const Symbol* sym) {
return config->allowUndefinedSymbols.count(sym->getName()) != 0;
}

static void reportUndefined(Symbol *sym) {
static void reportUndefined(ObjFile *file, Symbol *sym) {
if (!allowUndefined(sym)) {
switch (config->unresolvedSymbols) {
case UnresolvedPolicy::ReportError:
error(toString(sym->getFile()) + ": undefined symbol: " + toString(*sym));
error(toString(file) + ": undefined symbol: " + toString(*sym));
break;
case UnresolvedPolicy::Warn:
warn(toString(sym->getFile()) + ": undefined symbol: " + toString(*sym));
warn(toString(file) + ": undefined symbol: " + toString(*sym));
break;
case UnresolvedPolicy::Ignore:
LLVM_DEBUG(dbgs() << "ignoring undefined symbol: " + toString(*sym) +
Expand Down Expand Up @@ -171,7 +171,7 @@ void scanRelocations(InputChunk *chunk) {
}
} else if (sym->isUndefined() && !config->relocatable && !sym->isWeak()) {
// Report undefined symbols
reportUndefined(sym);
reportUndefined(file, sym);
}
}
}
Expand Down
Loading