Skip to content

Commit e7efa32

Browse files
authored
[lld][WebAssembly] Fix stub library deps causing LTO archive members to be required post-LTO (#101894)
Fixes: emscripten-core/emscripten#16836
1 parent 6b47772 commit e7efa32

File tree

3 files changed

+28
-8
lines changed

3 files changed

+28
-8
lines changed

lld/test/wasm/lto/stub-library.s

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,21 @@
1+
## The function `bar` is declared in stub.so and depends on `foo` which is
2+
## defined in an LTO object. We also test the case where the LTO object is
3+
## with an archive file.
4+
## This verifies that stub library dependencies (which are required exports) can
5+
## be defined in LTO objects, even when they are within archive files.
6+
17
# RUN: llvm-mc -filetype=obj -triple=wasm32-unknown-unknown -o %t.o %s
2-
# RUN: llvm-as %S/Inputs/foo.ll -o %t1.o
3-
# RUN: wasm-ld %t.o %t1.o %p/Inputs/stub.so -o %t.wasm
8+
# RUN: mkdir -p %t
9+
# RUN: llvm-as %S/Inputs/foo.ll -o %t/foo.o
10+
# RUN: wasm-ld %t.o %t/foo.o %p/Inputs/stub.so -o %t.wasm
411
# RUN: obj2yaml %t.wasm | FileCheck %s
512

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

1221
.globl _start

lld/wasm/Driver.cpp

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -949,6 +949,17 @@ static void processStubLibrariesPreLTO() {
949949
auto* needed = symtab->find(dep);
950950
if (needed ) {
951951
needed->isUsedInRegularObj = true;
952+
// Like with handleLibcall we have to extract any LTO archive
953+
// members that might need to be exported due to stub library
954+
// symbols being referenced. Without this the LTO object could be
955+
// extracted during processStubLibraries, which is too late since
956+
// LTO has already being performed at that point.
957+
if (needed->isLazy() && isa<BitcodeFile>(needed->getFile())) {
958+
if (!config->whyExtract.empty())
959+
ctx.whyExtractRecords.emplace_back(toString(stub_file),
960+
needed->getFile(), *needed);
961+
cast<LazySymbol>(needed)->extract();
962+
}
952963
}
953964
}
954965
}

lld/wasm/InputFiles.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -744,7 +744,7 @@ Symbol *ObjFile::createUndefined(const WasmSymbol &sym, bool isCalledDirectly) {
744744
llvm_unreachable("unknown symbol kind");
745745
}
746746

747-
StringRef strip(StringRef s) { return s.trim(' '); }
747+
static StringRef strip(StringRef s) { return s.trim(' '); }
748748

749749
void StubFile::parse() {
750750
bool first = true;
@@ -761,7 +761,7 @@ void StubFile::parse() {
761761
}
762762

763763
// Lines starting with # are considered comments
764-
if (line.starts_with("#"))
764+
if (line.starts_with("#") || !line.size())
765765
continue;
766766

767767
StringRef sym;

0 commit comments

Comments
 (0)