Skip to content

Commit 91b4f6b

Browse files
sbc100memfrob
authored and
memfrob
committed
[lld][WebAssembly] Handle weakly referenced symbols when lazy (archive) version is see first
When a weak reference of a lazy symbol occurs we were not correctly updating the lazy symbol. We need to tag the existing lazy symbol as weak and, in the case of a function symbol, give it a signature. Without the signature we can't then create the dummy function which is needed when an weakly undefined function is called. We had tests for weakly referenced lazy symbols but we were only tests in the case where the reference was seen before the lazy symbol. See: WebAssembly/wasi-libc#214 Differential Revision: https://reviews.llvm.org/D85567
1 parent 3532b6e commit 91b4f6b

File tree

4 files changed

+27
-9
lines changed

4 files changed

+27
-9
lines changed

lld/test/wasm/archive-weak-undefined.ll

+5-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
; Test that weak undefined symbols do not fetch members from archive files.
1+
;; Test that weak undefined symbols do not fetch members from archive files.
22
; RUN: llc -filetype=obj %s -o %t.o
33
; RUN: llvm-mc -filetype=obj -triple=wasm32-unknown-unknown %p/Inputs/ret32.s -o %t.ret32.o
44
; RUN: llvm-mc -filetype=obj -triple=wasm32-unknown-unknown %p/Inputs/hello.s -o %t.hello.o
@@ -8,6 +8,10 @@
88
; RUN: wasm-ld %t.o %t.a -o %t.wasm
99
; RUN: obj2yaml %t.wasm | FileCheck %s
1010

11+
;; Also test with the library symbols being read first
12+
; RUN: wasm-ld %t.a %t.o -o %t2.wasm
13+
; RUN: obj2yaml %t2.wasm | FileCheck %s
14+
1115
; RUN: wasm-ld -u hello_str %t.o %t.a -o %t2.wasm
1216
; RUN: obj2yaml %t2.wasm | FileCheck %s -check-prefix=CHECK-DATA
1317

lld/wasm/SymbolTable.cpp

+17-8
Original file line numberDiff line numberDiff line change
@@ -458,11 +458,16 @@ Symbol *SymbolTable::addUndefinedFunction(StringRef name,
458458
file, sig, isCalledDirectly);
459459
};
460460

461-
if (wasInserted)
461+
if (wasInserted) {
462462
replaceSym();
463-
else if (auto *lazy = dyn_cast<LazySymbol>(s))
464-
lazy->fetch();
465-
else {
463+
} else if (auto *lazy = dyn_cast<LazySymbol>(s)) {
464+
if ((flags & WASM_SYMBOL_BINDING_MASK) == WASM_SYMBOL_BINDING_WEAK) {
465+
lazy->setWeak();
466+
lazy->signature = sig;
467+
} else {
468+
lazy->fetch();
469+
}
470+
} else {
466471
auto existingFunction = dyn_cast<FunctionSymbol>(s);
467472
if (!existingFunction) {
468473
reportTypeError(s, file, WASM_SYMBOL_TYPE_FUNCTION);
@@ -499,12 +504,16 @@ Symbol *SymbolTable::addUndefinedData(StringRef name, uint32_t flags,
499504
if (s->traced)
500505
printTraceSymbolUndefined(name, file);
501506

502-
if (wasInserted)
507+
if (wasInserted) {
503508
replaceSymbol<UndefinedData>(s, name, flags, file);
504-
else if (auto *lazy = dyn_cast<LazySymbol>(s))
505-
lazy->fetch();
506-
else if (s->isDefined())
509+
} else if (auto *lazy = dyn_cast<LazySymbol>(s)) {
510+
if ((flags & WASM_SYMBOL_BINDING_MASK) == WASM_SYMBOL_BINDING_WEAK)
511+
lazy->setWeak();
512+
else
513+
lazy->fetch();
514+
} else if (s->isDefined()) {
507515
checkDataType(s, file);
516+
}
508517
return s;
509518
}
510519

lld/wasm/Symbols.cpp

+4
Original file line numberDiff line numberDiff line change
@@ -339,6 +339,10 @@ const OutputSectionSymbol *SectionSymbol::getOutputSectionSymbol() const {
339339

340340
void LazySymbol::fetch() { cast<ArchiveFile>(file)->addMember(&archiveSymbol); }
341341

342+
void LazySymbol::setWeak() {
343+
flags |= (flags & ~WASM_SYMBOL_BINDING_MASK) | WASM_SYMBOL_BINDING_WEAK;
344+
}
345+
342346
MemoryBufferRef LazySymbol::getMemberBuffer() {
343347
Archive::Child c =
344348
CHECK(archiveSymbol.getMember(),

lld/wasm/Symbols.h

+1
Original file line numberDiff line numberDiff line change
@@ -411,6 +411,7 @@ class LazySymbol : public Symbol {
411411

412412
static bool classof(const Symbol *s) { return s->kind() == LazyKind; }
413413
void fetch();
414+
void setWeak();
414415
MemoryBufferRef getMemberBuffer();
415416

416417
// Lazy symbols can have a signature because they can replace an

0 commit comments

Comments
 (0)