From 47e2b6b29eae57267b5c966eb7d02dc8eae0caaf Mon Sep 17 00:00:00 2001 From: Sam Clegg Date: Sat, 4 Aug 2018 00:04:06 +0000 Subject: [WebAssembly] Don't error when --undefined symbols are not found This matches the behavior of the ELF linker where -u/--undefined means symbols will get pulled in from archives but won't result in link error if they are missing. Also, don't actually great symbol table entries for the undefined symbols, again matching more closely the ELF linker. This also results in simplification of the code. Differential Revision: https://reviews.llvm.org/D50279 llvm-svn: 338938 --- lld/wasm/Driver.cpp | 66 ++++++++++++++++++++++++----------------------------- 1 file changed, 30 insertions(+), 36 deletions(-) (limited to 'lld/wasm/Driver.cpp') diff --git a/lld/wasm/Driver.cpp b/lld/wasm/Driver.cpp index 329b5ae80a9..8d73dab726e 100644 --- a/lld/wasm/Driver.cpp +++ b/lld/wasm/Driver.cpp @@ -329,14 +329,19 @@ static void handleWeakUndefines() { } // Force Sym to be entered in the output. Used for -u or equivalent. -static Symbol *addUndefined(StringRef Name) { - Symbol *S = Symtab->addUndefinedFunction(Name, 0, nullptr, nullptr); +static Symbol *handleUndefined(StringRef Name) { + Symbol *Sym = Symtab->find(Name); + if (!Sym) + return nullptr; // Since symbol S may not be used inside the program, LTO may // eliminate it. Mark the symbol as "used" to prevent it. - S->IsUsedInRegularObj = true; + Sym->IsUsedInRegularObj = true; - return S; + if (auto *LazySym = dyn_cast(Sym)) + LazySym->fetch(); + + return Sym; } void LinkerDriver::link(ArrayRef ArgsArr) { @@ -462,15 +467,6 @@ void LinkerDriver::link(ArrayRef ArgsArr) { WasmSym::DsoHandle = Symtab->addSyntheticDataSymbol( "__dso_handle", WASM_SYMBOL_VISIBILITY_HIDDEN); WasmSym::DataEnd = Symtab->addSyntheticDataSymbol("__data_end", 0); - - // For now, since we don't actually use the start function as the - // wasm start symbol, we don't need to care about it signature. - if (!Config->Entry.empty()) - EntrySym = addUndefined(Config->Entry); - - // Handle the `--undefined ` options. - for (auto *Arg : Args.filtered(OPT_undefined)) - addUndefined(Arg->getValue()); } createFiles(Args); @@ -484,10 +480,29 @@ void LinkerDriver::link(ArrayRef ArgsArr) { if (errorCount()) return; - // Add synthetic dummies for weak undefined functions. - if (!Config->Relocatable) + // Handle the `--undefined ` options. + for (auto *Arg : Args.filtered(OPT_undefined)) + handleUndefined(Arg->getValue()); + + if (!Config->Relocatable) { + // Add synthetic dummies for weak undefined functions. handleWeakUndefines(); + if (!Config->Entry.empty()) { + EntrySym = handleUndefined(Config->Entry); + if (!EntrySym) + error("entry symbol not defined (pass --no-entry to supress): " + + Config->Entry); + } + + // Make sure we have resolved all symbols. + if (!Config->AllowUndefined) + Symtab->reportRemainingUndefines(); + } + + if (errorCount()) + return; + // Handle --export. for (auto *Arg : Args.filtered(OPT_export)) { StringRef Name = Arg->getValue(); @@ -504,27 +519,6 @@ void LinkerDriver::link(ArrayRef ArgsArr) { if (errorCount()) return; - // Make sure we have resolved all symbols. - if (!Config->Relocatable && !Config->AllowUndefined) { - Symtab->reportRemainingUndefines(); - } else { - // Even when using --allow-undefined we still want to report the absence of - // our initial set of undefined symbols (i.e. the entry point and symbols - // specified via --undefined). - // Part of the reason for this is that these function don't have signatures - // so which means they cannot be written as wasm function imports. - for (auto *Arg : Args.filtered(OPT_undefined)) { - Symbol *Sym = Symtab->find(Arg->getValue()); - if (!Sym->isDefined()) - error("symbol forced with --undefined not found: " + Sym->getName()); - } - if (EntrySym && !EntrySym->isDefined()) - error("entry symbol not defined (pass --no-entry to supress): " + - EntrySym->getName()); - } - if (errorCount()) - return; - if (EntrySym) EntrySym->setHidden(false); -- cgit v1.2.3