Skip to content

[lld][WebAssembly] Fix stub library deps causing LTO archive members to be required post-LTO #101894

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

Merged
merged 3 commits into from
Aug 6, 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
21 changes: 15 additions & 6 deletions lld/test/wasm/lto/stub-library.s
Original file line number Diff line number Diff line change
@@ -1,12 +1,21 @@
## The function `bar` is declared in stub.so and depends on `foo` which is
## defined in an LTO object. We also test the case where the LTO object is
## with an archive file.
## This verifies that stub library dependencies (which are required exports) can
## be defined in LTO objects, even when they are within archive files.

# RUN: llvm-mc -filetype=obj -triple=wasm32-unknown-unknown -o %t.o %s
# RUN: llvm-as %S/Inputs/foo.ll -o %t1.o
# RUN: wasm-ld %t.o %t1.o %p/Inputs/stub.so -o %t.wasm
# RUN: mkdir -p %t
# RUN: llvm-as %S/Inputs/foo.ll -o %t/foo.o
# RUN: wasm-ld %t.o %t/foo.o %p/Inputs/stub.so -o %t.wasm
# RUN: obj2yaml %t.wasm | FileCheck %s

# The function `bar` is declared in stub.so and depends on `foo`, which happens
# be in an LTO object.
# This verifies that stub library dependencies (required exports) can be defined
# in LTO objects.
## Run the same test but with foo.o inside of an archive file.
# RUN: rm -f %t/libfoo.a
# RUN: llvm-ar rcs %t/libfoo.a %t/foo.o
# RUN: wasm-ld %t.o %t/libfoo.a %p/Inputs/stub.so -o %t2.wasm
# RUN: obj2yaml %t2.wasm | FileCheck %s

.functype bar () -> ()

.globl _start
Expand Down
11 changes: 11 additions & 0 deletions lld/wasm/Driver.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -949,6 +949,17 @@ static void processStubLibrariesPreLTO() {
auto* needed = symtab->find(dep);
if (needed ) {
needed->isUsedInRegularObj = true;
// Like with handleLibcall we have to extract any LTO archive
// members that might need to be exported due to stub library
// symbols being referenced. Without this the LTO object could be
// extracted during processStubLibraries, which is too late since
// LTO has already being performed at that point.
if (needed->isLazy() && isa<BitcodeFile>(needed->getFile())) {
if (!config->whyExtract.empty())
ctx.whyExtractRecords.emplace_back(toString(stub_file),
needed->getFile(), *needed);
cast<LazySymbol>(needed)->extract();
}
Copy link
Member

Choose a reason for hiding this comment

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

Perhaps this code could be shared with handleLibcall?

}
}
}
Expand Down
4 changes: 2 additions & 2 deletions lld/wasm/InputFiles.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -744,7 +744,7 @@ Symbol *ObjFile::createUndefined(const WasmSymbol &sym, bool isCalledDirectly) {
llvm_unreachable("unknown symbol kind");
}

StringRef strip(StringRef s) { return s.trim(' '); }
static StringRef strip(StringRef s) { return s.trim(' '); }

void StubFile::parse() {
bool first = true;
Expand All @@ -761,7 +761,7 @@ void StubFile::parse() {
}

// Lines starting with # are considered comments
if (line.starts_with("#"))
if (line.starts_with("#") || !line.size())
continue;

StringRef sym;
Expand Down
Loading