xref: /llvm-project-15.0.7/lld/wasm/Driver.cpp (revision 2e97d2aa)
1 //===- Driver.cpp ---------------------------------------------------------===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 
9 #include "lld/Common/Driver.h"
10 #include "Config.h"
11 #include "InputChunks.h"
12 #include "InputGlobal.h"
13 #include "MarkLive.h"
14 #include "SymbolTable.h"
15 #include "Writer.h"
16 #include "lld/Common/Args.h"
17 #include "lld/Common/ErrorHandler.h"
18 #include "lld/Common/Memory.h"
19 #include "lld/Common/Reproduce.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/Arg.h"
26 #include "llvm/Option/ArgList.h"
27 #include "llvm/Support/CommandLine.h"
28 #include "llvm/Support/Path.h"
29 #include "llvm/Support/Process.h"
30 #include "llvm/Support/TarWriter.h"
31 #include "llvm/Support/TargetSelect.h"
32 
33 #define DEBUG_TYPE "lld"
34 
35 using namespace llvm;
36 using namespace llvm::object;
37 using namespace llvm::sys;
38 using namespace llvm::wasm;
39 
40 using namespace lld;
41 using namespace lld::wasm;
42 
43 Configuration *lld::wasm::Config;
44 
45 namespace {
46 
47 // Create enum with OPT_xxx values for each option in Options.td
48 enum {
49   OPT_INVALID = 0,
50 #define OPTION(_1, _2, ID, _4, _5, _6, _7, _8, _9, _10, _11, _12) OPT_##ID,
51 #include "Options.inc"
52 #undef OPTION
53 };
54 
55 // This function is called on startup. We need this for LTO since
56 // LTO calls LLVM functions to compile bitcode files to native code.
57 // Technically this can be delayed until we read bitcode files, but
58 // we don't bother to do lazily because the initialization is fast.
59 static void initLLVM() {
60   InitializeAllTargets();
61   InitializeAllTargetMCs();
62   InitializeAllAsmPrinters();
63   InitializeAllAsmParsers();
64 }
65 
66 class LinkerDriver {
67 public:
68   void link(ArrayRef<const char *> ArgsArr);
69 
70 private:
71   void createFiles(opt::InputArgList &Args);
72   void addFile(StringRef Path);
73   void addLibrary(StringRef Name);
74 
75   // True if we are in --whole-archive and --no-whole-archive.
76   bool InWholeArchive = false;
77 
78   std::vector<InputFile *> Files;
79 };
80 } // anonymous namespace
81 
82 bool lld::wasm::link(ArrayRef<const char *> Args, bool CanExitEarly,
83                      raw_ostream &Error) {
84   errorHandler().LogName = args::getFilenameWithoutExe(Args[0]);
85   errorHandler().ErrorOS = &Error;
86   errorHandler().ColorDiagnostics = Error.has_colors();
87   errorHandler().ErrorLimitExceededMsg =
88       "too many errors emitted, stopping now (use "
89       "-error-limit=0 to see all errors)";
90 
91   Config = make<Configuration>();
92   Symtab = make<SymbolTable>();
93 
94   initLLVM();
95   LinkerDriver().link(Args);
96 
97   // Exit immediately if we don't need to return to the caller.
98   // This saves time because the overhead of calling destructors
99   // for all globally-allocated objects is not negligible.
100   if (CanExitEarly)
101     exitLld(errorCount() ? 1 : 0);
102 
103   freeArena();
104   return !errorCount();
105 }
106 
107 // Create prefix string literals used in Options.td
108 #define PREFIX(NAME, VALUE) const char *const NAME[] = VALUE;
109 #include "Options.inc"
110 #undef PREFIX
111 
112 // Create table mapping all options defined in Options.td
113 static const opt::OptTable::Info OptInfo[] = {
114 #define OPTION(X1, X2, ID, KIND, GROUP, ALIAS, X7, X8, X9, X10, X11, X12)      \
115   {X1, X2, X10,         X11,         OPT_##ID, opt::Option::KIND##Class,       \
116    X9, X8, OPT_##GROUP, OPT_##ALIAS, X7,       X12},
117 #include "Options.inc"
118 #undef OPTION
119 };
120 
121 namespace {
122 class WasmOptTable : public llvm::opt::OptTable {
123 public:
124   WasmOptTable() : OptTable(OptInfo) {}
125   opt::InputArgList parse(ArrayRef<const char *> Argv);
126 };
127 } // namespace
128 
129 // Set color diagnostics according to -color-diagnostics={auto,always,never}
130 // or -no-color-diagnostics flags.
131 static void handleColorDiagnostics(opt::InputArgList &Args) {
132   auto *Arg = Args.getLastArg(OPT_color_diagnostics, OPT_color_diagnostics_eq,
133                               OPT_no_color_diagnostics);
134   if (!Arg)
135     return;
136   if (Arg->getOption().getID() == OPT_color_diagnostics) {
137     errorHandler().ColorDiagnostics = true;
138   } else if (Arg->getOption().getID() == OPT_no_color_diagnostics) {
139     errorHandler().ColorDiagnostics = false;
140   } else {
141     StringRef S = Arg->getValue();
142     if (S == "always")
143       errorHandler().ColorDiagnostics = true;
144     else if (S == "never")
145       errorHandler().ColorDiagnostics = false;
146     else if (S != "auto")
147       error("unknown option: --color-diagnostics=" + S);
148   }
149 }
150 
151 // Find a file by concatenating given paths.
152 static Optional<std::string> findFile(StringRef Path1, const Twine &Path2) {
153   SmallString<128> S;
154   path::append(S, Path1, Path2);
155   if (fs::exists(S))
156     return S.str().str();
157   return None;
158 }
159 
160 opt::InputArgList WasmOptTable::parse(ArrayRef<const char *> Argv) {
161   SmallVector<const char *, 256> Vec(Argv.data(), Argv.data() + Argv.size());
162 
163   unsigned MissingIndex;
164   unsigned MissingCount;
165 
166   // Expand response files (arguments in the form of @<filename>)
167   cl::ExpandResponseFiles(Saver, cl::TokenizeGNUCommandLine, Vec);
168 
169   opt::InputArgList Args = this->ParseArgs(Vec, MissingIndex, MissingCount);
170 
171   handleColorDiagnostics(Args);
172   for (auto *Arg : Args.filtered(OPT_UNKNOWN))
173     error("unknown argument: " + Arg->getSpelling());
174   return Args;
175 }
176 
177 // Currently we allow a ".imports" to live alongside a library. This can
178 // be used to specify a list of symbols which can be undefined at link
179 // time (imported from the environment.  For example libc.a include an
180 // import file that lists the syscall functions it relies on at runtime.
181 // In the long run this information would be better stored as a symbol
182 // attribute/flag in the object file itself.
183 // See: https://github.com/WebAssembly/tool-conventions/issues/35
184 static void readImportFile(StringRef Filename) {
185   if (Optional<MemoryBufferRef> Buf = readFile(Filename))
186     for (StringRef Sym : args::getLines(*Buf))
187       Config->AllowUndefinedSymbols.insert(Sym);
188 }
189 
190 // Returns slices of MB by parsing MB as an archive file.
191 // Each slice consists of a member file in the archive.
192 std::vector<MemoryBufferRef> static getArchiveMembers(MemoryBufferRef MB) {
193   std::unique_ptr<Archive> File =
194       CHECK(Archive::create(MB),
195             MB.getBufferIdentifier() + ": failed to parse archive");
196 
197   std::vector<MemoryBufferRef> V;
198   Error Err = Error::success();
199   for (const ErrorOr<Archive::Child> &COrErr : File->children(Err)) {
200     Archive::Child C =
201         CHECK(COrErr, MB.getBufferIdentifier() +
202                           ": could not get the child of the archive");
203     MemoryBufferRef MBRef =
204         CHECK(C.getMemoryBufferRef(),
205               MB.getBufferIdentifier() +
206                   ": could not get the buffer for a child of the archive");
207     V.push_back(MBRef);
208   }
209   if (Err)
210     fatal(MB.getBufferIdentifier() +
211           ": Archive::children failed: " + toString(std::move(Err)));
212 
213   // Take ownership of memory buffers created for members of thin archives.
214   for (std::unique_ptr<MemoryBuffer> &MB : File->takeThinBuffers())
215     make<std::unique_ptr<MemoryBuffer>>(std::move(MB));
216 
217   return V;
218 }
219 
220 void LinkerDriver::addFile(StringRef Path) {
221   Optional<MemoryBufferRef> Buffer = readFile(Path);
222   if (!Buffer.hasValue())
223     return;
224   MemoryBufferRef MBRef = *Buffer;
225 
226   switch (identify_magic(MBRef.getBuffer())) {
227   case file_magic::archive: {
228     SmallString<128> ImportFile = Path;
229     path::replace_extension(ImportFile, ".imports");
230     if (fs::exists(ImportFile))
231       readImportFile(ImportFile.str());
232 
233     // Handle -whole-archive.
234     if (InWholeArchive) {
235       for (MemoryBufferRef &M : getArchiveMembers(MBRef))
236         Files.push_back(createObjectFile(M, Path));
237       return;
238     }
239 
240     std::unique_ptr<Archive> File =
241         CHECK(Archive::create(MBRef), Path + ": failed to parse archive");
242 
243     if (!File->isEmpty() && !File->hasSymbolTable()) {
244       error(MBRef.getBufferIdentifier() +
245             ": archive has no index; run ranlib to add one");
246     }
247 
248     Files.push_back(make<ArchiveFile>(MBRef));
249     return;
250   }
251   case file_magic::bitcode:
252   case file_magic::wasm_object:
253     Files.push_back(createObjectFile(MBRef));
254     break;
255   default:
256     error("unknown file type: " + MBRef.getBufferIdentifier());
257   }
258 }
259 
260 // Add a given library by searching it from input search paths.
261 void LinkerDriver::addLibrary(StringRef Name) {
262   for (StringRef Dir : Config->SearchPaths) {
263     if (Optional<std::string> S = findFile(Dir, "lib" + Name + ".a")) {
264       addFile(*S);
265       return;
266     }
267   }
268 
269   error("unable to find library -l" + Name);
270 }
271 
272 void LinkerDriver::createFiles(opt::InputArgList &Args) {
273   for (auto *Arg : Args) {
274     switch (Arg->getOption().getUnaliasedOption().getID()) {
275     case OPT_l:
276       addLibrary(Arg->getValue());
277       break;
278     case OPT_INPUT:
279       addFile(Arg->getValue());
280       break;
281     case OPT_whole_archive:
282       InWholeArchive = true;
283       break;
284     case OPT_no_whole_archive:
285       InWholeArchive = false;
286       break;
287     }
288   }
289 }
290 
291 static StringRef getEntry(opt::InputArgList &Args) {
292   auto *Arg = Args.getLastArg(OPT_entry, OPT_no_entry);
293   if (!Arg) {
294     if (Args.hasArg(OPT_relocatable))
295       return "";
296     if (Args.hasArg(OPT_shared))
297       return "__wasm_call_ctors";
298     return "_start";
299   }
300   if (Arg->getOption().getID() == OPT_no_entry)
301     return "";
302   return Arg->getValue();
303 }
304 
305 // Initializes Config members by the command line options.
306 static void readConfigs(opt::InputArgList &Args) {
307   Config->AllowUndefined = Args.hasArg(OPT_allow_undefined);
308   Config->CheckFeatures =
309       Args.hasFlag(OPT_check_features, OPT_no_check_features, true);
310   Config->CompressRelocations = Args.hasArg(OPT_compress_relocations);
311   Config->Demangle = Args.hasFlag(OPT_demangle, OPT_no_demangle, true);
312   Config->DisableVerify = Args.hasArg(OPT_disable_verify);
313   Config->EmitRelocs = Args.hasArg(OPT_emit_relocs);
314   Config->Entry = getEntry(Args);
315   Config->ExportAll = Args.hasArg(OPT_export_all);
316   Config->ExportDynamic = Args.hasFlag(OPT_export_dynamic,
317       OPT_no_export_dynamic, false);
318   Config->ExportTable = Args.hasArg(OPT_export_table);
319   errorHandler().FatalWarnings =
320       Args.hasFlag(OPT_fatal_warnings, OPT_no_fatal_warnings, false);
321   Config->ImportMemory = Args.hasArg(OPT_import_memory);
322   Config->SharedMemory = Args.hasArg(OPT_shared_memory);
323   // TODO: Make passive segments the default with shared memory
324   Config->PassiveSegments =
325       Args.hasFlag(OPT_passive_segments, OPT_active_segments, false);
326   Config->ImportTable = Args.hasArg(OPT_import_table);
327   Config->LTOO = args::getInteger(Args, OPT_lto_O, 2);
328   Config->LTOPartitions = args::getInteger(Args, OPT_lto_partitions, 1);
329   Config->Optimize = args::getInteger(Args, OPT_O, 0);
330   Config->OutputFile = Args.getLastArgValue(OPT_o);
331   Config->Relocatable = Args.hasArg(OPT_relocatable);
332   Config->GcSections =
333       Args.hasFlag(OPT_gc_sections, OPT_no_gc_sections, !Config->Relocatable);
334   Config->MergeDataSegments =
335       Args.hasFlag(OPT_merge_data_segments, OPT_no_merge_data_segments,
336                    !Config->Relocatable);
337   Config->Pie = Args.hasFlag(OPT_pie, OPT_no_pie, false);
338   Config->PrintGcSections =
339       Args.hasFlag(OPT_print_gc_sections, OPT_no_print_gc_sections, false);
340   Config->SaveTemps = Args.hasArg(OPT_save_temps);
341   Config->SearchPaths = args::getStrings(Args, OPT_L);
342   Config->Shared = Args.hasArg(OPT_shared);
343   Config->StripAll = Args.hasArg(OPT_strip_all);
344   Config->StripDebug = Args.hasArg(OPT_strip_debug);
345   Config->StackFirst = Args.hasArg(OPT_stack_first);
346   Config->Trace = Args.hasArg(OPT_trace);
347   Config->ThinLTOCacheDir = Args.getLastArgValue(OPT_thinlto_cache_dir);
348   Config->ThinLTOCachePolicy = CHECK(
349       parseCachePruningPolicy(Args.getLastArgValue(OPT_thinlto_cache_policy)),
350       "--thinlto-cache-policy: invalid cache policy");
351   Config->ThinLTOJobs = args::getInteger(Args, OPT_thinlto_jobs, -1u);
352   errorHandler().Verbose = Args.hasArg(OPT_verbose);
353   LLVM_DEBUG(errorHandler().Verbose = true);
354   ThreadsEnabled = Args.hasFlag(OPT_threads, OPT_no_threads, true);
355 
356   Config->InitialMemory = args::getInteger(Args, OPT_initial_memory, 0);
357   Config->GlobalBase = args::getInteger(Args, OPT_global_base, 1024);
358   Config->MaxMemory = args::getInteger(Args, OPT_max_memory, 0);
359   Config->ZStackSize =
360       args::getZOptionValue(Args, OPT_z, "stack-size", WasmPageSize);
361 
362   if (auto *Arg = Args.getLastArg(OPT_features)) {
363     Config->Features =
364         llvm::Optional<std::vector<std::string>>(std::vector<std::string>());
365     for (StringRef S : Arg->getValues())
366       Config->Features->push_back(S);
367   }
368 }
369 
370 // Some Config members do not directly correspond to any particular
371 // command line options, but computed based on other Config values.
372 // This function initialize such members. See Config.h for the details
373 // of these values.
374 static void setConfigs() {
375   Config->Pic = Config->Pie || Config->Shared;
376 
377   if (Config->Pic) {
378     if (Config->ExportTable)
379       error("-shared/-pie is incompatible with --export-table");
380     Config->ImportTable = true;
381   }
382 
383   if (Config->Shared) {
384     Config->ImportMemory = true;
385     Config->ExportDynamic = true;
386     Config->AllowUndefined = true;
387   }
388 }
389 
390 // Some command line options or some combinations of them are not allowed.
391 // This function checks for such errors.
392 static void checkOptions(opt::InputArgList &Args) {
393   if (!Config->StripDebug && !Config->StripAll && Config->CompressRelocations)
394     error("--compress-relocations is incompatible with output debug"
395           " information. Please pass --strip-debug or --strip-all");
396 
397   if (Config->LTOO > 3)
398     error("invalid optimization level for LTO: " + Twine(Config->LTOO));
399   if (Config->LTOPartitions == 0)
400     error("--lto-partitions: number of threads must be > 0");
401   if (Config->ThinLTOJobs == 0)
402     error("--thinlto-jobs: number of threads must be > 0");
403 
404   if (Config->Pie && Config->Shared)
405     error("-shared and -pie may not be used together");
406 
407   if (Config->OutputFile.empty())
408     error("no output file specified");
409 
410   if (Config->ImportTable && Config->ExportTable)
411     error("--import-table and --export-table may not be used together");
412 
413   if (Config->Relocatable) {
414     if (!Config->Entry.empty())
415       error("entry point specified for relocatable output file");
416     if (Config->GcSections)
417       error("-r and --gc-sections may not be used together");
418     if (Config->CompressRelocations)
419       error("-r -and --compress-relocations may not be used together");
420     if (Args.hasArg(OPT_undefined))
421       error("-r -and --undefined may not be used together");
422     if (Config->Pie)
423       error("-r and -pie may not be used together");
424   }
425 }
426 
427 // Force Sym to be entered in the output. Used for -u or equivalent.
428 static Symbol *handleUndefined(StringRef Name) {
429   Symbol *Sym = Symtab->find(Name);
430   if (!Sym)
431     return nullptr;
432 
433   // Since symbol S may not be used inside the program, LTO may
434   // eliminate it. Mark the symbol as "used" to prevent it.
435   Sym->IsUsedInRegularObj = true;
436 
437   if (auto *LazySym = dyn_cast<LazySymbol>(Sym))
438     LazySym->fetch();
439 
440   return Sym;
441 }
442 
443 static UndefinedGlobal *
444 createUndefinedGlobal(StringRef Name, llvm::wasm::WasmGlobalType *Type) {
445   auto *Sym =
446       cast<UndefinedGlobal>(Symtab->addUndefinedGlobal(Name, Name,
447                                                        DefaultModule, 0,
448                                                        nullptr, Type));
449   Config->AllowUndefinedSymbols.insert(Sym->getName());
450   Sym->IsUsedInRegularObj = true;
451   return Sym;
452 }
453 
454 // Create ABI-defined synthetic symbols
455 static void createSyntheticSymbols() {
456   static WasmSignature NullSignature = {{}, {}};
457   static llvm::wasm::WasmGlobalType GlobalTypeI32 = {WASM_TYPE_I32, false};
458   static llvm::wasm::WasmGlobalType MutableGlobalTypeI32 = {WASM_TYPE_I32,
459                                                             true};
460 
461   if (!Config->Relocatable) {
462     WasmSym::CallCtors = Symtab->addSyntheticFunction(
463         "__wasm_call_ctors", WASM_SYMBOL_VISIBILITY_HIDDEN,
464         make<SyntheticFunction>(NullSignature, "__wasm_call_ctors"));
465 
466     if (Config->PassiveSegments) {
467       // Passive segments are used to avoid memory being reinitialized on each
468       // thread's instantiation. These passive segments are initialized and
469       // dropped in __wasm_init_memory, which is the first function called from
470       // __wasm_call_ctors.
471       WasmSym::InitMemory = Symtab->addSyntheticFunction(
472           "__wasm_init_memory", WASM_SYMBOL_VISIBILITY_HIDDEN,
473           make<SyntheticFunction>(NullSignature, "__wasm_init_memory"));
474     }
475 
476     if (Config->Pic) {
477       // For PIC code we create a synthetic function __wasm_apply_relocs which
478       // is called from __wasm_call_ctors before the user-level constructors.
479       WasmSym::ApplyRelocs = Symtab->addSyntheticFunction(
480           "__wasm_apply_relocs", WASM_SYMBOL_VISIBILITY_HIDDEN,
481           make<SyntheticFunction>(NullSignature, "__wasm_apply_relocs"));
482     }
483   }
484 
485   // The __stack_pointer is imported in the shared library case, and exported
486   // in the non-shared (executable) case.
487   if (Config->Shared) {
488     WasmSym::StackPointer =
489         createUndefinedGlobal("__stack_pointer", &MutableGlobalTypeI32);
490   } else {
491     llvm::wasm::WasmGlobal Global;
492     Global.Type = {WASM_TYPE_I32, true};
493     Global.InitExpr.Value.Int32 = 0;
494     Global.InitExpr.Opcode = WASM_OPCODE_I32_CONST;
495     Global.SymbolName = "__stack_pointer";
496     auto *StackPointer = make<InputGlobal>(Global, nullptr);
497     StackPointer->Live = true;
498     // For non-PIC code
499     // TODO(sbc): Remove WASM_SYMBOL_VISIBILITY_HIDDEN when the mutable global
500     // spec proposal is implemented in all major browsers.
501     // See: https://github.com/WebAssembly/mutable-global
502     WasmSym::StackPointer = Symtab->addSyntheticGlobal(
503         "__stack_pointer", WASM_SYMBOL_VISIBILITY_HIDDEN, StackPointer);
504     WasmSym::DataEnd = Symtab->addOptionalDataSymbol("__data_end");
505     WasmSym::GlobalBase = Symtab->addOptionalDataSymbol("__global_base");
506     WasmSym::HeapBase = Symtab->addOptionalDataSymbol("__heap_base");
507   }
508 
509   if (Config->Pic) {
510     // For PIC code, we import two global variables (__memory_base and
511     // __table_base) from the environment and use these as the offset at
512     // which to load our static data and function table.
513     // See:
514     // https://github.com/WebAssembly/tool-conventions/blob/master/DynamicLinking.md
515     WasmSym::MemoryBase =
516         createUndefinedGlobal("__memory_base", &GlobalTypeI32);
517     WasmSym::TableBase = createUndefinedGlobal("__table_base", &GlobalTypeI32);
518     WasmSym::MemoryBase->markLive();
519     WasmSym::TableBase->markLive();
520   }
521 
522   WasmSym::DsoHandle = Symtab->addSyntheticDataSymbol(
523       "__dso_handle", WASM_SYMBOL_VISIBILITY_HIDDEN);
524 }
525 
526 // Reconstructs command line arguments so that so that you can re-run
527 // the same command with the same inputs. This is for --reproduce.
528 static std::string createResponseFile(const opt::InputArgList &Args) {
529   SmallString<0> Data;
530   raw_svector_ostream OS(Data);
531 
532   // Copy the command line to the output while rewriting paths.
533   for (auto *Arg : Args) {
534     switch (Arg->getOption().getUnaliasedOption().getID()) {
535     case OPT_reproduce:
536       break;
537     case OPT_INPUT:
538       OS << quote(relativeToRoot(Arg->getValue())) << "\n";
539       break;
540     case OPT_o:
541       // If -o path contains directories, "lld @response.txt" will likely
542       // fail because the archive we are creating doesn't contain empty
543       // directories for the output path (-o doesn't create directories).
544       // Strip directories to prevent the issue.
545       OS << "-o " << quote(sys::path::filename(Arg->getValue())) << "\n";
546       break;
547     default:
548       OS << toString(*Arg) << "\n";
549     }
550   }
551   return Data.str();
552 }
553 
554 // The --wrap option is a feature to rename symbols so that you can write
555 // wrappers for existing functions. If you pass `-wrap=foo`, all
556 // occurrences of symbol `foo` are resolved to `wrap_foo` (so, you are
557 // expected to write `wrap_foo` function as a wrapper). The original
558 // symbol becomes accessible as `real_foo`, so you can call that from your
559 // wrapper.
560 //
561 // This data structure is instantiated for each -wrap option.
562 struct WrappedSymbol {
563   Symbol *Sym;
564   Symbol *Real;
565   Symbol *Wrap;
566 };
567 
568 static Symbol *addUndefined(StringRef Name) {
569   return Symtab->addUndefinedFunction(Name, "", "", 0, nullptr, nullptr, false);
570 }
571 
572 // Handles -wrap option.
573 //
574 // This function instantiates wrapper symbols. At this point, they seem
575 // like they are not being used at all, so we explicitly set some flags so
576 // that LTO won't eliminate them.
577 static std::vector<WrappedSymbol> addWrappedSymbols(opt::InputArgList &Args) {
578   std::vector<WrappedSymbol> V;
579   DenseSet<StringRef> Seen;
580 
581   for (auto *Arg : Args.filtered(OPT_wrap)) {
582     StringRef Name = Arg->getValue();
583     if (!Seen.insert(Name).second)
584       continue;
585 
586     Symbol *Sym = Symtab->find(Name);
587     if (!Sym)
588       continue;
589 
590     Symbol *Real = addUndefined(Saver.save("__real_" + Name));
591     Symbol *Wrap = addUndefined(Saver.save("__wrap_" + Name));
592     V.push_back({Sym, Real, Wrap});
593 
594     // We want to tell LTO not to inline symbols to be overwritten
595     // because LTO doesn't know the final symbol contents after renaming.
596     Real->CanInline = false;
597     Sym->CanInline = false;
598 
599     // Tell LTO not to eliminate these symbols.
600     Sym->IsUsedInRegularObj = true;
601     Wrap->IsUsedInRegularObj = true;
602     Real->IsUsedInRegularObj = false;
603   }
604   return V;
605 }
606 
607 // Do renaming for -wrap by updating pointers to symbols.
608 //
609 // When this function is executed, only InputFiles and symbol table
610 // contain pointers to symbol objects. We visit them to replace pointers,
611 // so that wrapped symbols are swapped as instructed by the command line.
612 static void wrapSymbols(ArrayRef<WrappedSymbol> Wrapped) {
613   DenseMap<Symbol *, Symbol *> Map;
614   for (const WrappedSymbol &W : Wrapped) {
615     Map[W.Sym] = W.Wrap;
616     Map[W.Real] = W.Sym;
617   }
618 
619   // Update pointers in input files.
620   parallelForEach(Symtab->ObjectFiles, [&](InputFile *File) {
621     MutableArrayRef<Symbol *> Syms = File->getMutableSymbols();
622     for (size_t I = 0, E = Syms.size(); I != E; ++I)
623       if (Symbol *S = Map.lookup(Syms[I]))
624         Syms[I] = S;
625   });
626 
627   // Update pointers in the symbol table.
628   for (const WrappedSymbol &W : Wrapped)
629     Symtab->wrap(W.Sym, W.Real, W.Wrap);
630 }
631 
632 void LinkerDriver::link(ArrayRef<const char *> ArgsArr) {
633   WasmOptTable Parser;
634   opt::InputArgList Args = Parser.parse(ArgsArr.slice(1));
635 
636   // Handle --help
637   if (Args.hasArg(OPT_help)) {
638     Parser.PrintHelp(outs(),
639                      (std::string(ArgsArr[0]) + " [options] file...").c_str(),
640                      "LLVM Linker", false);
641     return;
642   }
643 
644   // Handle --version
645   if (Args.hasArg(OPT_version) || Args.hasArg(OPT_v)) {
646     outs() << getLLDVersion() << "\n";
647     return;
648   }
649 
650   // Handle --reproduce
651   if (auto *Arg = Args.getLastArg(OPT_reproduce)) {
652     StringRef Path = Arg->getValue();
653     Expected<std::unique_ptr<TarWriter>> ErrOrWriter =
654         TarWriter::create(Path, path::stem(Path));
655     if (ErrOrWriter) {
656       Tar = std::move(*ErrOrWriter);
657       Tar->append("response.txt", createResponseFile(Args));
658       Tar->append("version.txt", getLLDVersion() + "\n");
659     } else {
660       error("--reproduce: " + toString(ErrOrWriter.takeError()));
661     }
662   }
663 
664   // Parse and evaluate -mllvm options.
665   std::vector<const char *> V;
666   V.push_back("wasm-ld (LLVM option parsing)");
667   for (auto *Arg : Args.filtered(OPT_mllvm))
668     V.push_back(Arg->getValue());
669   cl::ParseCommandLineOptions(V.size(), V.data());
670 
671   errorHandler().ErrorLimit = args::getInteger(Args, OPT_error_limit, 20);
672 
673   readConfigs(Args);
674   setConfigs();
675   checkOptions(Args);
676 
677   if (auto *Arg = Args.getLastArg(OPT_allow_undefined_file))
678     readImportFile(Arg->getValue());
679 
680   if (!Args.hasArg(OPT_INPUT)) {
681     error("no input files");
682     return;
683   }
684 
685   // Handle --trace-symbol.
686   for (auto *Arg : Args.filtered(OPT_trace_symbol))
687     Symtab->trace(Arg->getValue());
688 
689   for (auto *Arg : Args.filtered(OPT_export))
690     Config->ExportedSymbols.insert(Arg->getValue());
691 
692   if (!Config->Relocatable)
693     createSyntheticSymbols();
694 
695   createFiles(Args);
696   if (errorCount())
697     return;
698 
699   // Add all files to the symbol table. This will add almost all
700   // symbols that we need to the symbol table.
701   for (InputFile *F : Files)
702     Symtab->addFile(F);
703   if (errorCount())
704     return;
705 
706   // Handle the `--undefined <sym>` options.
707   for (auto *Arg : Args.filtered(OPT_undefined))
708     handleUndefined(Arg->getValue());
709 
710   // Handle the `--export <sym>` options
711   // This works like --undefined but also exports the symbol if its found
712   for (auto *Arg : Args.filtered(OPT_export))
713     handleUndefined(Arg->getValue());
714 
715   Symbol *EntrySym = nullptr;
716   if (!Config->Relocatable && !Config->Entry.empty()) {
717     EntrySym = handleUndefined(Config->Entry);
718     if (EntrySym && EntrySym->isDefined())
719       EntrySym->ForceExport = true;
720     else
721       error("entry symbol not defined (pass --no-entry to supress): " +
722             Config->Entry);
723   }
724 
725   if (errorCount())
726     return;
727 
728   // Create wrapped symbols for -wrap option.
729   std::vector<WrappedSymbol> Wrapped = addWrappedSymbols(Args);
730 
731   // Do link-time optimization if given files are LLVM bitcode files.
732   // This compiles bitcode files into real object files.
733   Symtab->addCombinedLTOObject();
734   if (errorCount())
735     return;
736 
737   // Resolve any variant symbols that were created due to signature
738   // mismatchs.
739   Symtab->handleSymbolVariants();
740   if (errorCount())
741     return;
742 
743   // Apply symbol renames for -wrap.
744   if (!Wrapped.empty())
745     wrapSymbols(Wrapped);
746 
747   for (auto *Arg : Args.filtered(OPT_export)) {
748     Symbol *Sym = Symtab->find(Arg->getValue());
749     if (Sym && Sym->isDefined())
750       Sym->ForceExport = true;
751     else if (!Config->AllowUndefined)
752       error(Twine("symbol exported via --export not found: ") +
753             Arg->getValue());
754   }
755 
756   if (!Config->Relocatable) {
757     // Add synthetic dummies for weak undefined functions.  Must happen
758     // after LTO otherwise functions may not yet have signatures.
759     Symtab->handleWeakUndefines();
760   }
761 
762   if (EntrySym)
763     EntrySym->setHidden(false);
764 
765   if (errorCount())
766     return;
767 
768   // Do size optimizations: garbage collection
769   markLive();
770 
771   // Write the result to the file.
772   writeResult();
773 }
774