Skip to content

Commit

Permalink
[lld][WebAssembly] Fix stub library deps causing LTO archive members …
Browse files Browse the repository at this point in the history
…to be required post-LTO (#101894)

Fixes: emscripten-core/emscripten#16836
  • Loading branch information
sbc100 authored Aug 6, 2024
1 parent 6b47772 commit e7efa32
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 8 deletions.
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();
}
}
}
}
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

0 comments on commit e7efa32

Please sign in to comment.