diff --git a/lld/test/wasm/archive-weak-undefined.ll b/lld/test/wasm/archive-weak-undefined.ll --- a/lld/test/wasm/archive-weak-undefined.ll +++ b/lld/test/wasm/archive-weak-undefined.ll @@ -1,4 +1,4 @@ -; Test that weak undefined symbols do not fetch members from archive files. +;; Test that weak undefined symbols do not fetch members from archive files. ; RUN: llc -filetype=obj %s -o %t.o ; RUN: llvm-mc -filetype=obj -triple=wasm32-unknown-unknown %p/Inputs/ret32.s -o %t.ret32.o ; RUN: llvm-mc -filetype=obj -triple=wasm32-unknown-unknown %p/Inputs/hello.s -o %t.hello.o @@ -8,6 +8,10 @@ ; RUN: wasm-ld %t.o %t.a -o %t.wasm ; RUN: obj2yaml %t.wasm | FileCheck %s +;; Also test with the library symbols being read first +; RUN: wasm-ld %t.a %t.o -o %t2.wasm +; RUN: obj2yaml %t2.wasm | FileCheck %s + ; RUN: wasm-ld -u hello_str %t.o %t.a -o %t2.wasm ; RUN: obj2yaml %t2.wasm | FileCheck %s -check-prefix=CHECK-DATA diff --git a/lld/wasm/SymbolTable.cpp b/lld/wasm/SymbolTable.cpp --- a/lld/wasm/SymbolTable.cpp +++ b/lld/wasm/SymbolTable.cpp @@ -458,11 +458,16 @@ file, sig, isCalledDirectly); }; - if (wasInserted) + if (wasInserted) { replaceSym(); - else if (auto *lazy = dyn_cast(s)) - lazy->fetch(); - else { + } else if (auto *lazy = dyn_cast(s)) { + if ((flags & WASM_SYMBOL_BINDING_MASK) == WASM_SYMBOL_BINDING_WEAK) { + lazy->setWeak(); + lazy->signature = sig; + } else { + lazy->fetch(); + } + } else { auto existingFunction = dyn_cast(s); if (!existingFunction) { reportTypeError(s, file, WASM_SYMBOL_TYPE_FUNCTION); @@ -499,12 +504,16 @@ if (s->traced) printTraceSymbolUndefined(name, file); - if (wasInserted) + if (wasInserted) { replaceSymbol(s, name, flags, file); - else if (auto *lazy = dyn_cast(s)) - lazy->fetch(); - else if (s->isDefined()) + } else if (auto *lazy = dyn_cast(s)) { + if ((flags & WASM_SYMBOL_BINDING_MASK) == WASM_SYMBOL_BINDING_WEAK) + lazy->setWeak(); + else + lazy->fetch(); + } else if (s->isDefined()) { checkDataType(s, file); + } return s; } diff --git a/lld/wasm/Symbols.h b/lld/wasm/Symbols.h --- a/lld/wasm/Symbols.h +++ b/lld/wasm/Symbols.h @@ -411,6 +411,7 @@ static bool classof(const Symbol *s) { return s->kind() == LazyKind; } void fetch(); + void setWeak(); MemoryBufferRef getMemberBuffer(); // Lazy symbols can have a signature because they can replace an diff --git a/lld/wasm/Symbols.cpp b/lld/wasm/Symbols.cpp --- a/lld/wasm/Symbols.cpp +++ b/lld/wasm/Symbols.cpp @@ -339,6 +339,10 @@ void LazySymbol::fetch() { cast(file)->addMember(&archiveSymbol); } +void LazySymbol::setWeak() { + flags |= (flags & ~WASM_SYMBOL_BINDING_MASK) | WASM_SYMBOL_BINDING_WEAK; +} + MemoryBufferRef LazySymbol::getMemberBuffer() { Archive::Child c = CHECK(archiveSymbol.getMember(),