xref: /llvm-project-15.0.7/lld/wasm/Driver.cpp (revision 22bdb331)
1 //===- Driver.cpp ---------------------------------------------------------===//
2 //
3 //                             The LLVM Linker
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 
10 #include "lld/Common/Driver.h"
11 #include "Config.h"
12 #include "InputChunks.h"
13 #include "InputGlobal.h"
14 #include "MarkLive.h"
15 #include "SymbolTable.h"
16 #include "Writer.h"
17 #include "lld/Common/Args.h"
18 #include "lld/Common/ErrorHandler.h"
19 #include "lld/Common/Memory.h"
20 #include "lld/Common/Strings.h"
21 #include "lld/Common/Threads.h"
22 #include "lld/Common/Version.h"
23 #include "llvm/ADT/Twine.h"
24 #include "llvm/Object/Wasm.h"
25 #include "llvm/Option/ArgList.h"
26 #include "llvm/Support/CommandLine.h"
27 #include "llvm/Support/Path.h"
28 #include "llvm/Support/Process.h"
29 #include "llvm/Support/TargetSelect.h"
30 
31 #define DEBUG_TYPE "lld"
32 
33 using namespace llvm;
34 using namespace llvm::sys;
35 using namespace llvm::wasm;
36 
37 using namespace lld;
38 using namespace lld::wasm;
39 
40 Configuration *lld::wasm::Config;
41 
42 namespace {
43 
44 // Create enum with OPT_xxx values for each option in Options.td
45 enum {
46   OPT_INVALID = 0,
47 #define OPTION(_1, _2, ID, _4, _5, _6, _7, _8, _9, _10, _11, _12) OPT_##ID,
48 #include "Options.inc"
49 #undef OPTION
50 };
51 
52 // This function is called on startup. We need this for LTO since
53 // LTO calls LLVM functions to compile bitcode files to native code.
54 // Technically this can be delayed until we read bitcode files, but
55 // we don't bother to do lazily because the initialization is fast.
56 static void initLLVM() {
57   InitializeAllTargets();
58   InitializeAllTargetMCs();
59   InitializeAllAsmPrinters();
60   InitializeAllAsmParsers();
61 }
62 
63 class LinkerDriver {
64 public:
65   void link(ArrayRef<const char *> ArgsArr);
66 
67 private:
68   void createFiles(opt::InputArgList &Args);
69   void addFile(StringRef Path);
70   void addLibrary(StringRef Name);
71 
72   // True if we are in --whole-archive and --no-whole-archive.
73   bool InWholeArchive = false;
74 
75   std::vector<InputFile *> Files;
76 };
77 } // anonymous namespace
78 
79 bool lld::wasm::link(ArrayRef<const char *> Args, bool CanExitEarly,
80                      raw_ostream &Error) {
81   errorHandler().LogName = args::getFilenameWithoutExe(Args[0]);
82   errorHandler().ErrorOS = &Error;
83   errorHandler().ColorDiagnostics = Error.has_colors();
84   errorHandler().ErrorLimitExceededMsg =
85       "too many errors emitted, stopping now (use "
86       "-error-limit=0 to see all errors)";
87 
88   Config = make<Configuration>();
89   Symtab = make<SymbolTable>();
90 
91   initLLVM();
92   LinkerDriver().link(Args);
93 
94   // Exit immediately if we don't need to return to the caller.
95   // This saves time because the overhead of calling destructors
96   // for all globally-allocated objects is not negligible.
97   if (CanExitEarly)
98     exitLld(errorCount() ? 1 : 0);
99 
100   freeArena();
101   return !errorCount();
102 }
103 
104 // Create prefix string literals used in Options.td
105 #define PREFIX(NAME, VALUE) const char *const NAME[] = VALUE;
106 #include "Options.inc"
107 #undef PREFIX
108 
109 // Create table mapping all options defined in Options.td
110 static const opt::OptTable::Info OptInfo[] = {
111 #define OPTION(X1, X2, ID, KIND, GROUP, ALIAS, X7, X8, X9, X10, X11, X12)      \
112   {X1, X2, X10,         X11,         OPT_##ID, opt::Option::KIND##Class,       \
113    X9, X8, OPT_##GROUP, OPT_##ALIAS, X7,       X12},
114 #include "Options.inc"
115 #undef OPTION
116 };
117 
118 namespace {
119 class WasmOptTable : public llvm::opt::OptTable {
120 public:
121   WasmOptTable() : OptTable(OptInfo) {}
122   opt::InputArgList parse(ArrayRef<const char *> Argv);
123 };
124 } // namespace
125 
126 // Set color diagnostics according to -color-diagnostics={auto,always,never}
127 // or -no-color-diagnostics flags.
128 static void handleColorDiagnostics(opt::InputArgList &Args) {
129   auto *Arg = Args.getLastArg(OPT_color_diagnostics, OPT_color_diagnostics_eq,
130                               OPT_no_color_diagnostics);
131   if (!Arg)
132     return;
133   if (Arg->getOption().getID() == OPT_color_diagnostics) {
134     errorHandler().ColorDiagnostics = true;
135   } else if (Arg->getOption().getID() == OPT_no_color_diagnostics) {
136     errorHandler().ColorDiagnostics = false;
137   } else {
138     StringRef S = Arg->getValue();
139     if (S == "always")
140       errorHandler().ColorDiagnostics = true;
141     else if (S == "never")
142       errorHandler().ColorDiagnostics = false;
143     else if (S != "auto")
144       error("unknown option: --color-diagnostics=" + S);
145   }
146 }
147 
148 // Find a file by concatenating given paths.
149 static Optional<std::string> findFile(StringRef Path1, const Twine &Path2) {
150   SmallString<128> S;
151   path::append(S, Path1, Path2);
152   if (fs::exists(S))
153     return S.str().str();
154   return None;
155 }
156 
157 opt::InputArgList WasmOptTable::parse(ArrayRef<const char *> Argv) {
158   SmallVector<const char *, 256> Vec(Argv.data(), Argv.data() + Argv.size());
159 
160   unsigned MissingIndex;
161   unsigned MissingCount;
162 
163   // Expand response files (arguments in the form of @<filename>)
164   cl::ExpandResponseFiles(Saver, cl::TokenizeGNUCommandLine, Vec);
165 
166   opt::InputArgList Args = this->ParseArgs(Vec, MissingIndex, MissingCount);
167 
168   handleColorDiagnostics(Args);
169   for (auto *Arg : Args.filtered(OPT_UNKNOWN))
170     error("unknown argument: " + Arg->getSpelling());
171   return Args;
172 }
173 
174 // Currently we allow a ".imports" to live alongside a library. This can
175 // be used to specify a list of symbols which can be undefined at link
176 // time (imported from the environment.  For example libc.a include an
177 // import file that lists the syscall functions it relies on at runtime.
178 // In the long run this information would be better stored as a symbol
179 // attribute/flag in the object file itself.
180 // See: https://github.com/WebAssembly/tool-conventions/issues/35
181 static void readImportFile(StringRef Filename) {
182   if (Optional<MemoryBufferRef> Buf = readFile(Filename))
183     for (StringRef Sym : args::getLines(*Buf))
184       Config->AllowUndefinedSymbols.insert(Sym);
185 }
186 
187 // Returns slices of MB by parsing MB as an archive file.
188 // Each slice consists of a member file in the archive.
189 std::vector<MemoryBufferRef> static getArchiveMembers(MemoryBufferRef MB) {
190   std::unique_ptr<Archive> File =
191       CHECK(Archive::create(MB),
192             MB.getBufferIdentifier() + ": failed to parse archive");
193 
194   std::vector<MemoryBufferRef> V;
195   Error Err = Error::success();
196   for (const ErrorOr<Archive::Child> &COrErr : File->children(Err)) {
197     Archive::Child C =
198         CHECK(COrErr, MB.getBufferIdentifier() +
199                           ": could not get the child of the archive");
200     MemoryBufferRef MBRef =
201         CHECK(C.getMemoryBufferRef(),
202               MB.getBufferIdentifier() +
203                   ": could not get the buffer for a child of the archive");
204     V.push_back(MBRef);
205   }
206   if (Err)
207     fatal(MB.getBufferIdentifier() +
208           ": Archive::children failed: " + toString(std::move(Err)));
209 
210   // Take ownership of memory buffers created for members of thin archives.
211   for (std::unique_ptr<MemoryBuffer> &MB : File->takeThinBuffers())
212     make<std::unique_ptr<MemoryBuffer>>(std::move(MB));
213 
214   return V;
215 }
216 
217 void LinkerDriver::addFile(StringRef Path) {
218   Optional<MemoryBufferRef> Buffer = readFile(Path);
219   if (!Buffer.hasValue())
220     return;
221   MemoryBufferRef MBRef = *Buffer;
222 
223   switch (identify_magic(MBRef.getBuffer())) {
224   case file_magic::archive: {
225     // Handle -whole-archive.
226     if (InWholeArchive) {
227       for (MemoryBufferRef &M : getArchiveMembers(MBRef))
228         Files.push_back(createObjectFile(M));
229       return;
230     }
231 
232     SmallString<128> ImportFile = Path;
233     path::replace_extension(ImportFile, ".imports");
234     if (fs::exists(ImportFile))
235       readImportFile(ImportFile.str());
236 
237     Files.push_back(make<ArchiveFile>(MBRef));
238     return;
239   }
240   case file_magic::bitcode:
241   case file_magic::wasm_object:
242     Files.push_back(createObjectFile(MBRef));
243     break;
244   default:
245     error("unknown file type: " + MBRef.getBufferIdentifier());
246   }
247 }
248 
249 // Add a given library by searching it from input search paths.
250 void LinkerDriver::addLibrary(StringRef Name) {
251   for (StringRef Dir : Config->SearchPaths) {
252     if (Optional<std::string> S = findFile(Dir, "lib" + Name + ".a")) {
253       addFile(*S);
254       return;
255     }
256   }
257 
258   error("unable to find library -l" + Name);
259 }
260 
261 void LinkerDriver::createFiles(opt::InputArgList &Args) {
262   for (auto *Arg : Args) {
263     switch (Arg->getOption().getUnaliasedOption().getID()) {
264     case OPT_l:
265       addLibrary(Arg->getValue());
266       break;
267     case OPT_INPUT:
268       addFile(Arg->getValue());
269       break;
270     case OPT_whole_archive:
271       InWholeArchive = true;
272       break;
273     case OPT_no_whole_archive:
274       InWholeArchive = false;
275       break;
276     }
277   }
278 }
279 
280 static StringRef getEntry(opt::InputArgList &Args, StringRef Default) {
281   auto *Arg = Args.getLastArg(OPT_entry, OPT_no_entry);
282   if (!Arg)
283     return Default;
284   if (Arg->getOption().getID() == OPT_no_entry)
285     return "";
286   return Arg->getValue();
287 }
288 
289 static const uint8_t UnreachableFn[] = {
290     0x03 /* ULEB length */, 0x00 /* ULEB num locals */,
291     0x00 /* opcode unreachable */, 0x0b /* opcode end */
292 };
293 
294 // For weak undefined functions, there may be "call" instructions that reference
295 // the symbol. In this case, we need to synthesise a dummy/stub function that
296 // will abort at runtime, so that relocations can still provided an operand to
297 // the call instruction that passes Wasm validation.
298 static void handleWeakUndefines() {
299   for (Symbol *Sym : Symtab->getSymbols()) {
300     if (!Sym->isUndefined() || !Sym->isWeak())
301       continue;
302     auto *FuncSym = dyn_cast<FunctionSymbol>(Sym);
303     if (!FuncSym)
304       continue;
305 
306     // It is possible for undefined functions not to have a signature (eg. if
307     // added via "--undefined"), but weak undefined ones do have a signature.
308     assert(FuncSym->FunctionType);
309     const WasmSignature &Sig = *FuncSym->FunctionType;
310 
311     // Add a synthetic dummy for weak undefined functions.  These dummies will
312     // be GC'd if not used as the target of any "call" instructions.
313     std::string SymName = toString(*Sym);
314     StringRef DebugName = Saver.save("undefined function " + SymName);
315     SyntheticFunction *Func =
316         make<SyntheticFunction>(Sig, Sym->getName(), DebugName);
317     Func->setBody(UnreachableFn);
318     // Ensure it compares equal to the null pointer, and so that table relocs
319     // don't pull in the stub body (only call-operand relocs should do that).
320     Func->setTableIndex(0);
321     Symtab->SyntheticFunctions.emplace_back(Func);
322     // Hide our dummy to prevent export.
323     uint32_t Flags = WASM_SYMBOL_VISIBILITY_HIDDEN;
324     replaceSymbol<DefinedFunction>(Sym, Sym->getName(), Flags, nullptr, Func);
325   }
326 }
327 
328 // Force Sym to be entered in the output. Used for -u or equivalent.
329 static Symbol *handleUndefined(StringRef Name) {
330   Symbol *Sym = Symtab->find(Name);
331   if (!Sym)
332     return nullptr;
333 
334   // Since symbol S may not be used inside the program, LTO may
335   // eliminate it. Mark the symbol as "used" to prevent it.
336   Sym->IsUsedInRegularObj = true;
337 
338   if (auto *LazySym = dyn_cast<LazySymbol>(Sym))
339     LazySym->fetch();
340 
341   return Sym;
342 }
343 
344 void LinkerDriver::link(ArrayRef<const char *> ArgsArr) {
345   WasmOptTable Parser;
346   opt::InputArgList Args = Parser.parse(ArgsArr.slice(1));
347 
348   // Handle --help
349   if (Args.hasArg(OPT_help)) {
350     Parser.PrintHelp(outs(),
351                      (std::string(ArgsArr[0]) + " [options] file...").c_str(),
352                      "LLVM Linker", false);
353     return;
354   }
355 
356   // Handle --version
357   if (Args.hasArg(OPT_version) || Args.hasArg(OPT_v)) {
358     outs() << getLLDVersion() << "\n";
359     return;
360   }
361 
362   // Parse and evaluate -mllvm options.
363   std::vector<const char *> V;
364   V.push_back("wasm-ld (LLVM option parsing)");
365   for (auto *Arg : Args.filtered(OPT_mllvm))
366     V.push_back(Arg->getValue());
367   cl::ParseCommandLineOptions(V.size(), V.data());
368 
369   errorHandler().ErrorLimit = args::getInteger(Args, OPT_error_limit, 20);
370 
371   Config->AllowUndefined = Args.hasArg(OPT_allow_undefined);
372   Config->Demangle = Args.hasFlag(OPT_demangle, OPT_no_demangle, true);
373   Config->DisableVerify = Args.hasArg(OPT_disable_verify);
374   Config->Entry = getEntry(Args, Args.hasArg(OPT_relocatable) ? "" : "_start");
375   Config->ExportAll = Args.hasArg(OPT_export_all);
376   Config->ExportDynamic = Args.hasFlag(OPT_export_dynamic,
377       OPT_no_export_dynamic, false);
378   Config->ExportTable = Args.hasArg(OPT_export_table);
379   errorHandler().FatalWarnings =
380       Args.hasFlag(OPT_fatal_warnings, OPT_no_fatal_warnings, false);
381   Config->ImportMemory = Args.hasArg(OPT_import_memory);
382   Config->SharedMemory = Args.hasArg(OPT_shared_memory);
383   Config->ImportTable = Args.hasArg(OPT_import_table);
384   Config->LTOO = args::getInteger(Args, OPT_lto_O, 2);
385   Config->LTOPartitions = args::getInteger(Args, OPT_lto_partitions, 1);
386   Config->Optimize = args::getInteger(Args, OPT_O, 0);
387   Config->OutputFile = Args.getLastArgValue(OPT_o);
388   Config->Relocatable = Args.hasArg(OPT_relocatable);
389   Config->GcSections =
390       Args.hasFlag(OPT_gc_sections, OPT_no_gc_sections, !Config->Relocatable);
391   Config->MergeDataSegments =
392       Args.hasFlag(OPT_merge_data_segments, OPT_no_merge_data_segments,
393                    !Config->Relocatable);
394   Config->PrintGcSections =
395       Args.hasFlag(OPT_print_gc_sections, OPT_no_print_gc_sections, false);
396   Config->SaveTemps = Args.hasArg(OPT_save_temps);
397   Config->SearchPaths = args::getStrings(Args, OPT_L);
398   Config->StripAll = Args.hasArg(OPT_strip_all);
399   Config->StripDebug = Args.hasArg(OPT_strip_debug);
400   Config->CompressRelocations = Args.hasArg(OPT_compress_relocations);
401   Config->StackFirst = Args.hasArg(OPT_stack_first);
402   Config->ThinLTOCacheDir = Args.getLastArgValue(OPT_thinlto_cache_dir);
403   Config->ThinLTOCachePolicy = CHECK(
404       parseCachePruningPolicy(Args.getLastArgValue(OPT_thinlto_cache_policy)),
405       "--thinlto-cache-policy: invalid cache policy");
406   Config->ThinLTOJobs = args::getInteger(Args, OPT_thinlto_jobs, -1u);
407   errorHandler().Verbose = Args.hasArg(OPT_verbose);
408   ThreadsEnabled = Args.hasFlag(OPT_threads, OPT_no_threads, true);
409 
410   Config->InitialMemory = args::getInteger(Args, OPT_initial_memory, 0);
411   Config->GlobalBase = args::getInteger(Args, OPT_global_base, 1024);
412   Config->MaxMemory = args::getInteger(Args, OPT_max_memory, 0);
413   Config->ZStackSize =
414       args::getZOptionValue(Args, OPT_z, "stack-size", WasmPageSize);
415 
416   if (!Config->StripDebug && !Config->StripAll && Config->CompressRelocations)
417     error("--compress-relocations is incompatible with output debug"
418           " information. Please pass --strip-debug or --strip-all");
419 
420   if (Config->LTOO > 3)
421     error("invalid optimization level for LTO: " + Twine(Config->LTOO));
422   if (Config->LTOPartitions == 0)
423     error("--lto-partitions: number of threads must be > 0");
424   if (Config->ThinLTOJobs == 0)
425     error("--thinlto-jobs: number of threads must be > 0");
426 
427   if (auto *Arg = Args.getLastArg(OPT_allow_undefined_file))
428     readImportFile(Arg->getValue());
429 
430   if (!Args.hasArg(OPT_INPUT)) {
431     error("no input files");
432     return;
433   }
434 
435   if (Config->OutputFile.empty())
436     error("no output file specified");
437 
438   if (Config->ImportTable && Config->ExportTable)
439     error("--import-table and --export-table may not be used together");
440 
441   if (Config->Relocatable) {
442     if (!Config->Entry.empty())
443       error("entry point specified for relocatable output file");
444     if (Config->GcSections)
445       error("-r and --gc-sections may not be used together");
446     if (Config->CompressRelocations)
447       error("-r -and --compress-relocations may not be used together");
448     if (Args.hasArg(OPT_undefined))
449       error("-r -and --undefined may not be used together");
450   }
451 
452   Symbol *EntrySym = nullptr;
453   if (!Config->Relocatable) {
454     llvm::wasm::WasmGlobal Global;
455     Global.Type = {WASM_TYPE_I32, true};
456     Global.InitExpr.Value.Int32 = 0;
457     Global.InitExpr.Opcode = WASM_OPCODE_I32_CONST;
458     Global.SymbolName = "__stack_pointer";
459     InputGlobal *StackPointer = make<InputGlobal>(Global, nullptr);
460     StackPointer->Live = true;
461 
462     static WasmSignature NullSignature = {{}, {}};
463 
464     // Add synthetic symbols before any others
465     WasmSym::CallCtors = Symtab->addSyntheticFunction(
466         "__wasm_call_ctors", WASM_SYMBOL_VISIBILITY_HIDDEN,
467         make<SyntheticFunction>(NullSignature, "__wasm_call_ctors"));
468     // TODO(sbc): Remove WASM_SYMBOL_VISIBILITY_HIDDEN when the mutable global
469     // spec proposal is implemented in all major browsers.
470     // See: https://github.com/WebAssembly/mutable-global
471     WasmSym::StackPointer = Symtab->addSyntheticGlobal(
472         "__stack_pointer", WASM_SYMBOL_VISIBILITY_HIDDEN, StackPointer);
473     WasmSym::HeapBase = Symtab->addSyntheticDataSymbol("__heap_base", 0);
474     WasmSym::DsoHandle = Symtab->addSyntheticDataSymbol(
475         "__dso_handle", WASM_SYMBOL_VISIBILITY_HIDDEN);
476     WasmSym::DataEnd = Symtab->addSyntheticDataSymbol("__data_end", 0);
477 
478     // These two synthetic symbols exist purely for the embedder so we always
479     // want to export them.
480     WasmSym::HeapBase->ForceExport = true;
481     WasmSym::DataEnd->ForceExport = true;
482   }
483 
484   createFiles(Args);
485   if (errorCount())
486     return;
487 
488   // Add all files to the symbol table. This will add almost all
489   // symbols that we need to the symbol table.
490   for (InputFile *F : Files)
491     Symtab->addFile(F);
492   if (errorCount())
493     return;
494 
495   // Handle the `--undefined <sym>` options.
496   for (auto *Arg : Args.filtered(OPT_undefined))
497     handleUndefined(Arg->getValue());
498 
499   // Handle the `--export <sym>` options
500   // This works like --undefined but also exports the symbol if its found
501   for (auto *Arg : Args.filtered(OPT_export)) {
502     Symbol *Sym = handleUndefined(Arg->getValue());
503     if (Sym && Sym->isDefined())
504       Sym->ForceExport = true;
505     else if (!Config->AllowUndefined)
506       error(Twine("symbol exported via --export not found: ") +
507             Arg->getValue());
508   }
509 
510   if (!Config->Relocatable) {
511     // Add synthetic dummies for weak undefined functions.
512     handleWeakUndefines();
513 
514     if (!Config->Entry.empty()) {
515       EntrySym = handleUndefined(Config->Entry);
516       if (EntrySym && EntrySym->isDefined())
517         EntrySym->ForceExport = true;
518       else
519         error("entry symbol not defined (pass --no-entry to supress): " +
520               Config->Entry);
521     }
522 
523     // Make sure we have resolved all symbols.
524     if (!Config->AllowUndefined)
525       Symtab->reportRemainingUndefines();
526   }
527 
528   if (errorCount())
529     return;
530 
531   // Do link-time optimization if given files are LLVM bitcode files.
532   // This compiles bitcode files into real object files.
533   Symtab->addCombinedLTOObject();
534   if (errorCount())
535     return;
536 
537   if (EntrySym)
538     EntrySym->setHidden(false);
539 
540   if (errorCount())
541     return;
542 
543   // Do size optimizations: garbage collection
544   markLive();
545 
546   // Write the result to the file.
547   writeResult();
548 }
549