diff --git a/lld/test/wasm/relocatable.ll b/lld/test/wasm/relocatable.ll --- a/lld/test/wasm/relocatable.ll +++ b/lld/test/wasm/relocatable.ll @@ -34,6 +34,9 @@ ret i32 ptrtoint ([3 x i8]* @data_comdat to i32) } +; Test that __attribute__(used) (i.e NO_STRIP) is preserved in the relocated symbol table +@llvm.used = appending global [1 x i8*] [i8* bitcast (i32 ()* @my_func to i8*)], section "llvm.metadata" + ; CHECK: --- !WASM ; CHECK-NEXT: FileHeader: ; CHECK-NEXT: Version: 0x00000001 @@ -188,7 +191,7 @@ ; NORMAL-NEXT: - Index: 3 ; NORMAL-NEXT: Kind: FUNCTION ; NORMAL-NEXT: Name: my_func -; NORMAL-NEXT: Flags: [ VISIBILITY_HIDDEN ] +; NORMAL-NEXT: Flags: [ VISIBILITY_HIDDEN, NO_STRIP ] ; NORMAL-NEXT: Function: 4 ; NORMAL-NEXT: - Index: 4 ; NORMAL-NEXT: Kind: FUNCTION diff --git a/lld/wasm/Driver.cpp b/lld/wasm/Driver.cpp --- a/lld/wasm/Driver.cpp +++ b/lld/wasm/Driver.cpp @@ -441,10 +441,8 @@ static UndefinedGlobal * createUndefinedGlobal(StringRef name, llvm::wasm::WasmGlobalType *type) { - auto *sym = - cast(symtab->addUndefinedGlobal(name, name, - defaultModule, 0, - nullptr, type)); + auto *sym = cast(symtab->addUndefinedGlobal( + name, name, defaultModule, WASM_SYMBOL_UNDEFINED, nullptr, type)); config->allowUndefinedSymbols.insert(sym->getName()); sym->isUsedInRegularObj = true; return sym; @@ -582,7 +580,8 @@ }; static Symbol *addUndefined(StringRef name) { - return symtab->addUndefinedFunction(name, "", "", 0, nullptr, nullptr, false); + return symtab->addUndefinedFunction(name, "", "", WASM_SYMBOL_UNDEFINED, + nullptr, nullptr, false); } // Handles -wrap option. diff --git a/lld/wasm/InputFiles.cpp b/lld/wasm/InputFiles.cpp --- a/lld/wasm/InputFiles.cpp +++ b/lld/wasm/InputFiles.cpp @@ -421,7 +421,7 @@ Symbol *ObjFile::createUndefined(const WasmSymbol &sym, bool isCalledDirectly) { StringRef name = sym.Info.Name; - uint32_t flags = sym.Info.Flags; + uint32_t flags = sym.Info.Flags | WASM_SYMBOL_UNDEFINED; switch (sym.Info.Kind) { case WASM_SYMBOL_TYPE_FUNCTION: @@ -509,6 +509,7 @@ bool excludedByComdat = c != -1 && !keptComdats[c]; if (objSym.isUndefined() || excludedByComdat) { + flags |= WASM_SYMBOL_UNDEFINED; if (objSym.isExecutable()) return symtab->addUndefinedFunction(name, name, defaultModule, flags, &f, nullptr, true); diff --git a/lld/wasm/SymbolTable.cpp b/lld/wasm/SymbolTable.cpp --- a/lld/wasm/SymbolTable.cpp +++ b/lld/wasm/SymbolTable.cpp @@ -401,6 +401,7 @@ LLVM_DEBUG(dbgs() << "addUndefinedFunction: " << name << " [" << (sig ? toString(*sig) : "none") << "] IsCalledDirectly:" << isCalledDirectly << "\n"); + assert(flags & WASM_SYMBOL_UNDEFINED); Symbol *s; bool wasInserted; @@ -443,6 +444,7 @@ Symbol *SymbolTable::addUndefinedData(StringRef name, uint32_t flags, InputFile *file) { LLVM_DEBUG(dbgs() << "addUndefinedData: " << name << "\n"); + assert(flags & WASM_SYMBOL_UNDEFINED); Symbol *s; bool wasInserted; @@ -464,6 +466,7 @@ InputFile *file, const WasmGlobalType *type) { LLVM_DEBUG(dbgs() << "addUndefinedGlobal: " << name << "\n"); + assert(flags & WASM_SYMBOL_UNDEFINED); Symbol *s; bool wasInserted; diff --git a/lld/wasm/SyntheticSections.cpp b/lld/wasm/SyntheticSections.cpp --- a/lld/wasm/SyntheticSections.cpp +++ b/lld/wasm/SyntheticSections.cpp @@ -362,26 +362,6 @@ return numSegments && config->sharedMemory; } -static uint32_t getWasmFlags(const Symbol *sym) { - uint32_t flags = 0; - if (sym->isLocal()) - flags |= WASM_SYMBOL_BINDING_LOCAL; - if (sym->isWeak()) - flags |= WASM_SYMBOL_BINDING_WEAK; - if (sym->isHidden()) - flags |= WASM_SYMBOL_VISIBILITY_HIDDEN; - if (sym->isUndefined()) - flags |= WASM_SYMBOL_UNDEFINED; - if (auto *f = dyn_cast(sym)) { - if (f->getName() != f->importName) - flags |= WASM_SYMBOL_EXPLICIT_NAME; - } else if (auto *g = dyn_cast(sym)) { - if (g->getName() != g->importName) - flags |= WASM_SYMBOL_EXPLICIT_NAME; - } - return flags; -} - void LinkingSection::writeBody() { raw_ostream &os = bodyOutputStream; @@ -394,7 +374,7 @@ for (const Symbol *sym : symtabEntries) { assert(sym->isDefined() || sym->isUndefined()); WasmSymbolType kind = sym->getWasmType(); - uint32_t flags = getWasmFlags(sym); + uint32_t flags = sym->getFlags(); writeU8(sub.os, kind, "sym kind"); writeUleb128(sub.os, flags, "sym flags");