diff options
Diffstat (limited to 'wasm/Driver.cpp')
-rw-r--r-- | wasm/Driver.cpp | 374 |
1 files changed, 296 insertions, 78 deletions
diff --git a/wasm/Driver.cpp b/wasm/Driver.cpp index 97ec262be308..329b5ae80a9c 100644 --- a/wasm/Driver.cpp +++ b/wasm/Driver.cpp @@ -9,11 +9,15 @@ #include "lld/Common/Driver.h" #include "Config.h" +#include "InputChunks.h" +#include "InputGlobal.h" +#include "MarkLive.h" #include "SymbolTable.h" #include "Writer.h" #include "lld/Common/Args.h" #include "lld/Common/ErrorHandler.h" #include "lld/Common/Memory.h" +#include "lld/Common/Strings.h" #include "lld/Common/Threads.h" #include "lld/Common/Version.h" #include "llvm/ADT/Twine.h" @@ -22,6 +26,9 @@ #include "llvm/Support/CommandLine.h" #include "llvm/Support/Path.h" #include "llvm/Support/Process.h" +#include "llvm/Support/TargetSelect.h" + +#define DEBUG_TYPE "lld" using namespace llvm; using namespace llvm::sys; @@ -30,14 +37,9 @@ using namespace llvm::wasm; using namespace lld; using namespace lld::wasm; -namespace { +Configuration *lld::wasm::Config; -// Parses command line options. -class WasmOptTable : public llvm::opt::OptTable { -public: - WasmOptTable(); - llvm::opt::InputArgList parse(ArrayRef<const char *> Argv); -}; +namespace { // Create enum with OPT_xxx values for each option in Options.td enum { @@ -47,24 +49,36 @@ enum { #undef OPTION }; +// This function is called on startup. We need this for LTO since +// LTO calls LLVM functions to compile bitcode files to native code. +// Technically this can be delayed until we read bitcode files, but +// we don't bother to do lazily because the initialization is fast. +static void initLLVM() { + InitializeAllTargets(); + InitializeAllTargetMCs(); + InitializeAllAsmPrinters(); + InitializeAllAsmParsers(); +} + class LinkerDriver { public: void link(ArrayRef<const char *> ArgsArr); private: - void createFiles(llvm::opt::InputArgList &Args); + void createFiles(opt::InputArgList &Args); void addFile(StringRef Path); void addLibrary(StringRef Name); + + // True if we are in --whole-archive and --no-whole-archive. + bool InWholeArchive = false; + std::vector<InputFile *> Files; }; - } // anonymous namespace -Configuration *lld::wasm::Config; - bool lld::wasm::link(ArrayRef<const char *> Args, bool CanExitEarly, raw_ostream &Error) { - errorHandler().LogName = Args[0]; + errorHandler().LogName = sys::path::filename(Args[0]); errorHandler().ErrorOS = &Error; errorHandler().ColorDiagnostics = Error.has_colors(); errorHandler().ErrorLimitExceededMsg = @@ -74,6 +88,7 @@ bool lld::wasm::link(ArrayRef<const char *> Args, bool CanExitEarly, Config = make<Configuration>(); Symtab = make<SymbolTable>(); + initLLVM(); LinkerDriver().link(Args); // Exit immediately if we don't need to return to the caller. @@ -86,8 +101,6 @@ bool lld::wasm::link(ArrayRef<const char *> Args, bool CanExitEarly, return !errorCount(); } -// Create OptTable - // Create prefix string literals used in Options.td #define PREFIX(NAME, VALUE) const char *const NAME[] = VALUE; #include "Options.inc" @@ -102,6 +115,14 @@ static const opt::OptTable::Info OptInfo[] = { #undef OPTION }; +namespace { +class WasmOptTable : public llvm::opt::OptTable { +public: + WasmOptTable() : OptTable(OptInfo) {} + opt::InputArgList parse(ArrayRef<const char *> Argv); +}; +} // namespace + // Set color diagnostics according to -color-diagnostics={auto,always,never} // or -no-color-diagnostics flags. static void handleColorDiagnostics(opt::InputArgList &Args) { @@ -109,19 +130,18 @@ static void handleColorDiagnostics(opt::InputArgList &Args) { OPT_no_color_diagnostics); if (!Arg) return; - - if (Arg->getOption().getID() == OPT_color_diagnostics) + if (Arg->getOption().getID() == OPT_color_diagnostics) { errorHandler().ColorDiagnostics = true; - else if (Arg->getOption().getID() == OPT_no_color_diagnostics) + } else if (Arg->getOption().getID() == OPT_no_color_diagnostics) { errorHandler().ColorDiagnostics = false; - else { + } else { StringRef S = Arg->getValue(); if (S == "always") errorHandler().ColorDiagnostics = true; - if (S == "never") + else if (S == "never") errorHandler().ColorDiagnostics = false; - if (S != "auto") - error("unknown option: -color-diagnostics=" + S); + else if (S != "auto") + error("unknown option: --color-diagnostics=" + S); } } @@ -134,25 +154,15 @@ static Optional<std::string> findFile(StringRef Path1, const Twine &Path2) { return None; } -// Inject a new undefined symbol into the link. This will cause the link to -// fail unless this symbol can be found. -static void addSyntheticUndefinedFunction(StringRef Name, - const WasmSignature *Type) { - log("injecting undefined func: " + Name); - Symtab->addUndefinedFunction(Name, Type); -} - -static void printHelp(const char *Argv0) { - WasmOptTable().PrintHelp(outs(), Argv0, "LLVM Linker", false); -} - -WasmOptTable::WasmOptTable() : OptTable(OptInfo) {} - opt::InputArgList WasmOptTable::parse(ArrayRef<const char *> Argv) { SmallVector<const char *, 256> Vec(Argv.data(), Argv.data() + Argv.size()); unsigned MissingIndex; unsigned MissingCount; + + // Expand response files (arguments in the form of @<filename>) + cl::ExpandResponseFiles(Saver, cl::TokenizeGNUCommandLine, Vec); + opt::InputArgList Args = this->ParseArgs(Vec, MissingIndex, MissingCount); handleColorDiagnostics(Args); @@ -161,16 +171,80 @@ opt::InputArgList WasmOptTable::parse(ArrayRef<const char *> Argv) { return Args; } +// Currently we allow a ".imports" to live alongside a library. This can +// be used to specify a list of symbols which can be undefined at link +// time (imported from the environment. For example libc.a include an +// import file that lists the syscall functions it relies on at runtime. +// In the long run this information would be better stored as a symbol +// attribute/flag in the object file itself. +// See: https://github.com/WebAssembly/tool-conventions/issues/35 +static void readImportFile(StringRef Filename) { + if (Optional<MemoryBufferRef> Buf = readFile(Filename)) + for (StringRef Sym : args::getLines(*Buf)) + Config->AllowUndefinedSymbols.insert(Sym); +} + +// Returns slices of MB by parsing MB as an archive file. +// Each slice consists of a member file in the archive. +std::vector<MemoryBufferRef> static getArchiveMembers( + MemoryBufferRef MB) { + std::unique_ptr<Archive> File = + CHECK(Archive::create(MB), + MB.getBufferIdentifier() + ": failed to parse archive"); + + std::vector<MemoryBufferRef> V; + Error Err = Error::success(); + for (const ErrorOr<Archive::Child> &COrErr : File->children(Err)) { + Archive::Child C = + CHECK(COrErr, MB.getBufferIdentifier() + + ": could not get the child of the archive"); + MemoryBufferRef MBRef = + CHECK(C.getMemoryBufferRef(), + MB.getBufferIdentifier() + + ": could not get the buffer for a child of the archive"); + V.push_back(MBRef); + } + if (Err) + fatal(MB.getBufferIdentifier() + ": Archive::children failed: " + + toString(std::move(Err))); + + // Take ownership of memory buffers created for members of thin archives. + for (std::unique_ptr<MemoryBuffer> &MB : File->takeThinBuffers()) + make<std::unique_ptr<MemoryBuffer>>(std::move(MB)); + + return V; +} + void LinkerDriver::addFile(StringRef Path) { Optional<MemoryBufferRef> Buffer = readFile(Path); if (!Buffer.hasValue()) return; MemoryBufferRef MBRef = *Buffer; - if (identify_magic(MBRef.getBuffer()) == file_magic::archive) + switch (identify_magic(MBRef.getBuffer())) { + case file_magic::archive: { + // Handle -whole-archive. + if (InWholeArchive) { + for (MemoryBufferRef &M : getArchiveMembers(MBRef)) + Files.push_back(createObjectFile(M)); + return; + } + + SmallString<128> ImportFile = Path; + path::replace_extension(ImportFile, ".imports"); + if (fs::exists(ImportFile)) + readImportFile(ImportFile.str()); + Files.push_back(make<ArchiveFile>(MBRef)); - else - Files.push_back(make<ObjFile>(MBRef)); + return; + } + case file_magic::bitcode: + case file_magic::wasm_object: + Files.push_back(createObjectFile(MBRef)); + break; + default: + error("unknown file type: " + MBRef.getBufferIdentifier()); + } } // Add a given library by searching it from input search paths. @@ -194,11 +268,14 @@ void LinkerDriver::createFiles(opt::InputArgList &Args) { case OPT_INPUT: addFile(Arg->getValue()); break; + case OPT_whole_archive: + InWholeArchive = true; + break; + case OPT_no_whole_archive: + InWholeArchive = false; + break; } } - - if (Files.empty()) - error("no input files"); } static StringRef getEntry(opt::InputArgList &Args, StringRef Default) { @@ -210,13 +287,71 @@ static StringRef getEntry(opt::InputArgList &Args, StringRef Default) { return Arg->getValue(); } +static const uint8_t UnreachableFn[] = { + 0x03 /* ULEB length */, 0x00 /* ULEB num locals */, + 0x00 /* opcode unreachable */, 0x0b /* opcode end */ +}; + +// For weak undefined functions, there may be "call" instructions that reference +// the symbol. In this case, we need to synthesise a dummy/stub function that +// will abort at runtime, so that relocations can still provided an operand to +// the call instruction that passes Wasm validation. +static void handleWeakUndefines() { + for (Symbol *Sym : Symtab->getSymbols()) { + if (!Sym->isUndefined() || !Sym->isWeak()) + continue; + auto *FuncSym = dyn_cast<FunctionSymbol>(Sym); + if (!FuncSym) + continue; + + // It is possible for undefined functions not to have a signature (eg. if + // added via "--undefined"), but weak undefined ones do have a signature. + assert(FuncSym->FunctionType); + const WasmSignature &Sig = *FuncSym->FunctionType; + + // Add a synthetic dummy for weak undefined functions. These dummies will + // be GC'd if not used as the target of any "call" instructions. + Optional<std::string> SymName = demangleItanium(Sym->getName()); + StringRef DebugName = + Saver.save("undefined function " + + (SymName ? StringRef(*SymName) : Sym->getName())); + SyntheticFunction *Func = + make<SyntheticFunction>(Sig, Sym->getName(), DebugName); + Func->setBody(UnreachableFn); + // Ensure it compares equal to the null pointer, and so that table relocs + // don't pull in the stub body (only call-operand relocs should do that). + Func->setTableIndex(0); + Symtab->SyntheticFunctions.emplace_back(Func); + // Hide our dummy to prevent export. + uint32_t Flags = WASM_SYMBOL_VISIBILITY_HIDDEN; + replaceSymbol<DefinedFunction>(Sym, Sym->getName(), Flags, nullptr, Func); + } +} + +// 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); + + // 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; + + return S; +} + void LinkerDriver::link(ArrayRef<const char *> ArgsArr) { WasmOptTable Parser; opt::InputArgList Args = Parser.parse(ArgsArr.slice(1)); // Handle --help if (Args.hasArg(OPT_help)) { - printHelp(ArgsArr[0]); + Parser.PrintHelp(outs(), ArgsArr[0], "LLVM Linker", false); + return; + } + + // Handle --version + if (Args.hasArg(OPT_version) || Args.hasArg(OPT_v)) { + outs() << getLLDVersion() << "\n"; return; } @@ -229,26 +364,40 @@ void LinkerDriver::link(ArrayRef<const char *> ArgsArr) { errorHandler().ErrorLimit = args::getInteger(Args, OPT_error_limit, 20); - if (Args.hasArg(OPT_version) || Args.hasArg(OPT_v)) { - outs() << getLLDVersion() << "\n"; - return; - } - Config->AllowUndefined = Args.hasArg(OPT_allow_undefined); - Config->CheckSignatures = - Args.hasFlag(OPT_check_signatures, OPT_no_check_signatures, false); - Config->EmitRelocs = Args.hasArg(OPT_emit_relocs); + Config->Demangle = Args.hasFlag(OPT_demangle, OPT_no_demangle, true); + Config->DisableVerify = Args.hasArg(OPT_disable_verify); Config->Entry = getEntry(Args, Args.hasArg(OPT_relocatable) ? "" : "_start"); + Config->ExportAll = Args.hasArg(OPT_export_all); + Config->ExportTable = Args.hasArg(OPT_export_table); + errorHandler().FatalWarnings = + Args.hasFlag(OPT_fatal_warnings, OPT_no_fatal_warnings, false); Config->ImportMemory = Args.hasArg(OPT_import_memory); + Config->ImportTable = Args.hasArg(OPT_import_table); + Config->LTOO = args::getInteger(Args, OPT_lto_O, 2); + Config->LTOPartitions = args::getInteger(Args, OPT_lto_partitions, 1); + Config->Optimize = args::getInteger(Args, OPT_O, 0); Config->OutputFile = Args.getLastArgValue(OPT_o); Config->Relocatable = Args.hasArg(OPT_relocatable); + Config->GcSections = + Args.hasFlag(OPT_gc_sections, OPT_no_gc_sections, !Config->Relocatable); + Config->MergeDataSegments = + Args.hasFlag(OPT_merge_data_segments, OPT_no_merge_data_segments, + !Config->Relocatable); + Config->PrintGcSections = + Args.hasFlag(OPT_print_gc_sections, OPT_no_print_gc_sections, false); + Config->SaveTemps = Args.hasArg(OPT_save_temps); Config->SearchPaths = args::getStrings(Args, OPT_L); Config->StripAll = Args.hasArg(OPT_strip_all); Config->StripDebug = Args.hasArg(OPT_strip_debug); + Config->StackFirst = Args.hasArg(OPT_stack_first); + Config->ThinLTOCacheDir = Args.getLastArgValue(OPT_thinlto_cache_dir); + Config->ThinLTOCachePolicy = CHECK( + parseCachePruningPolicy(Args.getLastArgValue(OPT_thinlto_cache_policy)), + "--thinlto-cache-policy: invalid cache policy"); + Config->ThinLTOJobs = args::getInteger(Args, OPT_thinlto_jobs, -1u); errorHandler().Verbose = Args.hasArg(OPT_verbose); ThreadsEnabled = Args.hasFlag(OPT_threads, OPT_no_threads, true); - if (Config->Relocatable) - Config->EmitRelocs = true; Config->InitialMemory = args::getInteger(Args, OPT_initial_memory, 0); Config->GlobalBase = args::getInteger(Args, OPT_global_base, 1024); @@ -256,33 +405,72 @@ void LinkerDriver::link(ArrayRef<const char *> ArgsArr) { Config->ZStackSize = args::getZOptionValue(Args, OPT_z, "stack-size", WasmPageSize); + Config->CompressRelocTargets = Config->Optimize > 0 && !Config->Relocatable; + + if (Config->LTOO > 3) + error("invalid optimization level for LTO: " + Twine(Config->LTOO)); + if (Config->LTOPartitions == 0) + error("--lto-partitions: number of threads must be > 0"); + if (Config->ThinLTOJobs == 0) + error("--thinlto-jobs: number of threads must be > 0"); + if (auto *Arg = Args.getLastArg(OPT_allow_undefined_file)) - if (Optional<MemoryBufferRef> Buf = readFile(Arg->getValue())) - for (StringRef Sym : args::getLines(*Buf)) - Config->AllowUndefinedSymbols.insert(Sym); + readImportFile(Arg->getValue()); + + if (!Args.hasArg(OPT_INPUT)) { + error("no input files"); + return; + } if (Config->OutputFile.empty()) error("no output file specified"); - if (!Args.hasArg(OPT_INPUT)) - error("no input files"); + if (Config->ImportTable && Config->ExportTable) + error("--import-table and --export-table may not be used together"); - if (Config->Relocatable && !Config->Entry.empty()) - error("entry point specified for relocatable output file"); - if (Config->Relocatable && Args.hasArg(OPT_undefined)) - error("undefined symbols specified for relocatable output file"); + if (Config->Relocatable) { + if (!Config->Entry.empty()) + error("entry point specified for relocatable output file"); + if (Config->GcSections) + error("-r and --gc-sections may not be used together"); + if (Args.hasArg(OPT_undefined)) + error("-r -and --undefined may not be used together"); + } + Symbol *EntrySym = nullptr; if (!Config->Relocatable) { - if (!Config->Entry.empty()) { - static WasmSignature Signature = {{}, WASM_TYPE_NORESULT}; - addSyntheticUndefinedFunction(Config->Entry, &Signature); - } + llvm::wasm::WasmGlobal Global; + Global.Type = {WASM_TYPE_I32, true}; + Global.InitExpr.Value.Int32 = 0; + Global.InitExpr.Opcode = WASM_OPCODE_I32_CONST; + Global.SymbolName = "__stack_pointer"; + InputGlobal *StackPointer = make<InputGlobal>(Global, nullptr); + StackPointer->Live = true; + + static WasmSignature NullSignature = {{}, WASM_TYPE_NORESULT}; + + // Add synthetic symbols before any others + WasmSym::CallCtors = Symtab->addSyntheticFunction( + "__wasm_call_ctors", WASM_SYMBOL_VISIBILITY_HIDDEN, + make<SyntheticFunction>(NullSignature, "__wasm_call_ctors")); + // TODO(sbc): Remove WASM_SYMBOL_VISIBILITY_HIDDEN when the mutable global + // spec proposal is implemented in all major browsers. + // See: https://github.com/WebAssembly/mutable-global + WasmSym::StackPointer = Symtab->addSyntheticGlobal( + "__stack_pointer", WASM_SYMBOL_VISIBILITY_HIDDEN, StackPointer); + WasmSym::HeapBase = Symtab->addSyntheticDataSymbol("__heap_base", 0); + 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 <sym>` options. - for (StringRef S : args::getStrings(Args, OPT_undefined)) - addSyntheticUndefinedFunction(S, nullptr); - - Config->StackPointerSymbol = Symtab->addDefinedGlobal("__stack_pointer"); + for (auto *Arg : Args.filtered(OPT_undefined)) + addUndefined(Arg->getValue()); } createFiles(Args); @@ -293,29 +481,59 @@ void LinkerDriver::link(ArrayRef<const char *> ArgsArr) { // symbols that we need to the symbol table. for (InputFile *F : Files) Symtab->addFile(F); + if (errorCount()) + return; + + // Add synthetic dummies for weak undefined functions. + if (!Config->Relocatable) + handleWeakUndefines(); + + // Handle --export. + for (auto *Arg : Args.filtered(OPT_export)) { + StringRef Name = Arg->getValue(); + Symbol *Sym = Symtab->find(Name); + if (Sym && Sym->isDefined()) + Sym->ForceExport = true; + else if (!Config->AllowUndefined) + error("symbol exported via --export not found: " + Name); + } + + // Do link-time optimization if given files are LLVM bitcode files. + // This compiles bitcode files into real object files. + Symtab->addCombinedLTOObject(); + if (errorCount()) + return; // Make sure we have resolved all symbols. if (!Config->Relocatable && !Config->AllowUndefined) { Symtab->reportRemainingUndefines(); } else { - // When we allow undefined symbols we cannot include those defined in - // -u/--undefined since these undefined symbols have only names and no - // function signature, which means they cannot be written to the final - // output. - for (StringRef S : args::getStrings(Args, OPT_undefined)) { - Symbol *Sym = Symtab->find(S); + // 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("function forced with --undefined not found: " + Sym->getName()); + 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 (!Config->Entry.empty() && !Symtab->find(Config->Entry)->isDefined()) - error("entry point not found: " + Config->Entry); + if (EntrySym) + EntrySym->setHidden(false); + if (errorCount()) return; + // Do size optimizations: garbage collection + markLive(); + // Write the result to the file. writeResult(); } |