xref: /llvm-project-15.0.7/lld/wasm/Driver.cpp (revision f7d033f4)
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/Filesystem.h"
19 #include "lld/Common/Memory.h"
20 #include "lld/Common/Reproduce.h"
21 #include "lld/Common/Strings.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/Host.h"
29 #include "llvm/Support/Parallel.h"
30 #include "llvm/Support/Path.h"
31 #include "llvm/Support/Process.h"
32 #include "llvm/Support/TarWriter.h"
33 #include "llvm/Support/TargetSelect.h"
34 
35 #define DEBUG_TYPE "lld"
36 
37 using namespace llvm;
38 using namespace llvm::object;
39 using namespace llvm::sys;
40 using namespace llvm::wasm;
41 
42 namespace lld {
43 namespace wasm {
44 Configuration *config;
45 
46 namespace {
47 
48 // Create enum with OPT_xxx values for each option in Options.td
49 enum {
50   OPT_INVALID = 0,
51 #define OPTION(_1, _2, ID, _4, _5, _6, _7, _8, _9, _10, _11, _12) OPT_##ID,
52 #include "Options.inc"
53 #undef OPTION
54 };
55 
56 // This function is called on startup. We need this for LTO since
57 // LTO calls LLVM functions to compile bitcode files to native code.
58 // Technically this can be delayed until we read bitcode files, but
59 // we don't bother to do lazily because the initialization is fast.
60 static void initLLVM() {
61   InitializeAllTargets();
62   InitializeAllTargetMCs();
63   InitializeAllAsmPrinters();
64   InitializeAllAsmParsers();
65 }
66 
67 class LinkerDriver {
68 public:
69   void link(ArrayRef<const char *> argsArr);
70 
71 private:
72   void createFiles(opt::InputArgList &args);
73   void addFile(StringRef path);
74   void addLibrary(StringRef name);
75 
76   // True if we are in --whole-archive and --no-whole-archive.
77   bool inWholeArchive = false;
78 
79   std::vector<InputFile *> files;
80 };
81 } // anonymous namespace
82 
83 bool link(ArrayRef<const char *> args, bool canExitEarly, raw_ostream &stdoutOS,
84           raw_ostream &stderrOS) {
85   lld::stdoutOS = &stdoutOS;
86   lld::stderrOS = &stderrOS;
87 
88   errorHandler().cleanupCallback = []() { freeArena(); };
89 
90   errorHandler().logName = args::getFilenameWithoutExe(args[0]);
91   errorHandler().errorLimitExceededMsg =
92       "too many errors emitted, stopping now (use "
93       "-error-limit=0 to see all errors)";
94   stderrOS.enable_colors(stderrOS.has_colors());
95 
96   config = make<Configuration>();
97   symtab = make<SymbolTable>();
98 
99   initLLVM();
100   LinkerDriver().link(args);
101 
102   // Exit immediately if we don't need to return to the caller.
103   // This saves time because the overhead of calling destructors
104   // for all globally-allocated objects is not negligible.
105   if (canExitEarly)
106     exitLld(errorCount() ? 1 : 0);
107 
108   return !errorCount();
109 }
110 
111 // Create prefix string literals used in Options.td
112 #define PREFIX(NAME, VALUE) const char *const NAME[] = VALUE;
113 #include "Options.inc"
114 #undef PREFIX
115 
116 // Create table mapping all options defined in Options.td
117 static const opt::OptTable::Info optInfo[] = {
118 #define OPTION(X1, X2, ID, KIND, GROUP, ALIAS, X7, X8, X9, X10, X11, X12)      \
119   {X1, X2, X10,         X11,         OPT_##ID, opt::Option::KIND##Class,       \
120    X9, X8, OPT_##GROUP, OPT_##ALIAS, X7,       X12},
121 #include "Options.inc"
122 #undef OPTION
123 };
124 
125 namespace {
126 class WasmOptTable : public llvm::opt::OptTable {
127 public:
128   WasmOptTable() : OptTable(optInfo) {}
129   opt::InputArgList parse(ArrayRef<const char *> argv);
130 };
131 } // namespace
132 
133 // Set color diagnostics according to -color-diagnostics={auto,always,never}
134 // or -no-color-diagnostics flags.
135 static void handleColorDiagnostics(opt::InputArgList &args) {
136   auto *arg = args.getLastArg(OPT_color_diagnostics, OPT_color_diagnostics_eq,
137                               OPT_no_color_diagnostics);
138   if (!arg)
139     return;
140   if (arg->getOption().getID() == OPT_color_diagnostics) {
141     lld::errs().enable_colors(true);
142   } else if (arg->getOption().getID() == OPT_no_color_diagnostics) {
143     lld::errs().enable_colors(false);
144   } else {
145     StringRef s = arg->getValue();
146     if (s == "always")
147       lld::errs().enable_colors(true);
148     else if (s == "never")
149       lld::errs().enable_colors(false);
150     else if (s != "auto")
151       error("unknown option: --color-diagnostics=" + s);
152   }
153 }
154 
155 static cl::TokenizerCallback getQuotingStyle(opt::InputArgList &args) {
156   if (auto *arg = args.getLastArg(OPT_rsp_quoting)) {
157     StringRef s = arg->getValue();
158     if (s != "windows" && s != "posix")
159       error("invalid response file quoting: " + s);
160     if (s == "windows")
161       return cl::TokenizeWindowsCommandLine;
162     return cl::TokenizeGNUCommandLine;
163   }
164   if (Triple(sys::getProcessTriple()).isOSWindows())
165     return cl::TokenizeWindowsCommandLine;
166   return cl::TokenizeGNUCommandLine;
167 }
168 
169 // Find a file by concatenating given paths.
170 static Optional<std::string> findFile(StringRef path1, const Twine &path2) {
171   SmallString<128> s;
172   path::append(s, path1, path2);
173   if (fs::exists(s))
174     return std::string(s);
175   return None;
176 }
177 
178 opt::InputArgList WasmOptTable::parse(ArrayRef<const char *> argv) {
179   SmallVector<const char *, 256> vec(argv.data(), argv.data() + argv.size());
180 
181   unsigned missingIndex;
182   unsigned missingCount;
183 
184   // We need to get the quoting style for response files before parsing all
185   // options so we parse here before and ignore all the options but
186   // --rsp-quoting.
187   opt::InputArgList args = this->ParseArgs(vec, missingIndex, missingCount);
188 
189   // Expand response files (arguments in the form of @<filename>)
190   // and then parse the argument again.
191   cl::ExpandResponseFiles(saver, getQuotingStyle(args), vec);
192   args = this->ParseArgs(vec, missingIndex, missingCount);
193 
194   handleColorDiagnostics(args);
195   for (auto *arg : args.filtered(OPT_UNKNOWN))
196     error("unknown argument: " + arg->getAsString(args));
197   return args;
198 }
199 
200 // Currently we allow a ".imports" to live alongside a library. This can
201 // be used to specify a list of symbols which can be undefined at link
202 // time (imported from the environment.  For example libc.a include an
203 // import file that lists the syscall functions it relies on at runtime.
204 // In the long run this information would be better stored as a symbol
205 // attribute/flag in the object file itself.
206 // See: https://github.com/WebAssembly/tool-conventions/issues/35
207 static void readImportFile(StringRef filename) {
208   if (Optional<MemoryBufferRef> buf = readFile(filename))
209     for (StringRef sym : args::getLines(*buf))
210       config->allowUndefinedSymbols.insert(sym);
211 }
212 
213 // Returns slices of MB by parsing MB as an archive file.
214 // Each slice consists of a member file in the archive.
215 std::vector<MemoryBufferRef> static getArchiveMembers(MemoryBufferRef mb) {
216   std::unique_ptr<Archive> file =
217       CHECK(Archive::create(mb),
218             mb.getBufferIdentifier() + ": failed to parse archive");
219 
220   std::vector<MemoryBufferRef> v;
221   Error err = Error::success();
222   for (const Archive::Child &c : file->children(err)) {
223     MemoryBufferRef mbref =
224         CHECK(c.getMemoryBufferRef(),
225               mb.getBufferIdentifier() +
226                   ": could not get the buffer for a child of the archive");
227     v.push_back(mbref);
228   }
229   if (err)
230     fatal(mb.getBufferIdentifier() +
231           ": Archive::children failed: " + toString(std::move(err)));
232 
233   // Take ownership of memory buffers created for members of thin archives.
234   for (std::unique_ptr<MemoryBuffer> &mb : file->takeThinBuffers())
235     make<std::unique_ptr<MemoryBuffer>>(std::move(mb));
236 
237   return v;
238 }
239 
240 void LinkerDriver::addFile(StringRef path) {
241   Optional<MemoryBufferRef> buffer = readFile(path);
242   if (!buffer.hasValue())
243     return;
244   MemoryBufferRef mbref = *buffer;
245 
246   switch (identify_magic(mbref.getBuffer())) {
247   case file_magic::archive: {
248     SmallString<128> importFile = path;
249     path::replace_extension(importFile, ".imports");
250     if (fs::exists(importFile))
251       readImportFile(importFile.str());
252 
253     // Handle -whole-archive.
254     if (inWholeArchive) {
255       for (MemoryBufferRef &m : getArchiveMembers(mbref)) {
256         auto *object = createObjectFile(m, path);
257         // Mark object as live; object members are normally not
258         // live by default but -whole-archive is designed to treat
259         // them as such.
260         object->markLive();
261         files.push_back(object);
262       }
263 
264       return;
265     }
266 
267     std::unique_ptr<Archive> file =
268         CHECK(Archive::create(mbref), path + ": failed to parse archive");
269 
270     if (!file->isEmpty() && !file->hasSymbolTable()) {
271       error(mbref.getBufferIdentifier() +
272             ": archive has no index; run ranlib to add one");
273     }
274 
275     files.push_back(make<ArchiveFile>(mbref));
276     return;
277   }
278   case file_magic::bitcode:
279   case file_magic::wasm_object:
280     files.push_back(createObjectFile(mbref));
281     break;
282   default:
283     error("unknown file type: " + mbref.getBufferIdentifier());
284   }
285 }
286 
287 // Add a given library by searching it from input search paths.
288 void LinkerDriver::addLibrary(StringRef name) {
289   for (StringRef dir : config->searchPaths) {
290     if (Optional<std::string> s = findFile(dir, "lib" + name + ".a")) {
291       addFile(*s);
292       return;
293     }
294   }
295 
296   error("unable to find library -l" + name);
297 }
298 
299 void LinkerDriver::createFiles(opt::InputArgList &args) {
300   for (auto *arg : args) {
301     switch (arg->getOption().getID()) {
302     case OPT_l:
303       addLibrary(arg->getValue());
304       break;
305     case OPT_INPUT:
306       addFile(arg->getValue());
307       break;
308     case OPT_whole_archive:
309       inWholeArchive = true;
310       break;
311     case OPT_no_whole_archive:
312       inWholeArchive = false;
313       break;
314     }
315   }
316   if (files.empty() && errorCount() == 0)
317     error("no input files");
318 }
319 
320 static StringRef getEntry(opt::InputArgList &args) {
321   auto *arg = args.getLastArg(OPT_entry, OPT_no_entry);
322   if (!arg) {
323     if (args.hasArg(OPT_relocatable))
324       return "";
325     if (args.hasArg(OPT_shared))
326       return "__wasm_call_ctors";
327     return "_start";
328   }
329   if (arg->getOption().getID() == OPT_no_entry)
330     return "";
331   return arg->getValue();
332 }
333 
334 // Determines what we should do if there are remaining unresolved
335 // symbols after the name resolution.
336 static UnresolvedPolicy getUnresolvedSymbolPolicy(opt::InputArgList &args) {
337   UnresolvedPolicy errorOrWarn = args.hasFlag(OPT_error_unresolved_symbols,
338                                               OPT_warn_unresolved_symbols, true)
339                                      ? UnresolvedPolicy::ReportError
340                                      : UnresolvedPolicy::Warn;
341 
342   if (auto *arg = args.getLastArg(OPT_unresolved_symbols)) {
343     StringRef s = arg->getValue();
344     if (s == "ignore-all")
345       return UnresolvedPolicy::Ignore;
346     if (s == "import-functions")
347       return UnresolvedPolicy::ImportFuncs;
348     if (s == "report-all")
349       return errorOrWarn;
350     error("unknown --unresolved-symbols value: " + s);
351   }
352 
353   // Legacy --allow-undefined flag which is equivalent to
354   // --unresolve-symbols=ignore-all
355   if (args.hasArg(OPT_allow_undefined))
356     return UnresolvedPolicy::ImportFuncs;
357 
358   return errorOrWarn;
359 }
360 
361 // Initializes Config members by the command line options.
362 static void readConfigs(opt::InputArgList &args) {
363   config->bsymbolic = args.hasArg(OPT_Bsymbolic);
364   config->checkFeatures =
365       args.hasFlag(OPT_check_features, OPT_no_check_features, true);
366   config->compressRelocations = args.hasArg(OPT_compress_relocations);
367   config->demangle = args.hasFlag(OPT_demangle, OPT_no_demangle, true);
368   config->disableVerify = args.hasArg(OPT_disable_verify);
369   config->emitRelocs = args.hasArg(OPT_emit_relocs);
370   config->experimentalPic = args.hasArg(OPT_experimental_pic);
371   config->entry = getEntry(args);
372   config->exportAll = args.hasArg(OPT_export_all);
373   config->exportTable = args.hasArg(OPT_export_table);
374   config->growableTable = args.hasArg(OPT_growable_table);
375   errorHandler().fatalWarnings =
376       args.hasFlag(OPT_fatal_warnings, OPT_no_fatal_warnings, false);
377   config->importMemory = args.hasArg(OPT_import_memory);
378   config->sharedMemory = args.hasArg(OPT_shared_memory);
379   config->importTable = args.hasArg(OPT_import_table);
380   config->ltoo = args::getInteger(args, OPT_lto_O, 2);
381   config->ltoPartitions = args::getInteger(args, OPT_lto_partitions, 1);
382   config->mapFile = args.getLastArgValue(OPT_Map);
383   config->optimize = args::getInteger(args, OPT_O, 0);
384   config->outputFile = args.getLastArgValue(OPT_o);
385   config->relocatable = args.hasArg(OPT_relocatable);
386   config->gcSections =
387       args.hasFlag(OPT_gc_sections, OPT_no_gc_sections, !config->relocatable);
388   config->mergeDataSegments =
389       args.hasFlag(OPT_merge_data_segments, OPT_no_merge_data_segments,
390                    !config->relocatable);
391   config->pie = args.hasFlag(OPT_pie, OPT_no_pie, false);
392   config->printGcSections =
393       args.hasFlag(OPT_print_gc_sections, OPT_no_print_gc_sections, false);
394   config->saveTemps = args.hasArg(OPT_save_temps);
395   config->searchPaths = args::getStrings(args, OPT_L);
396   config->shared = args.hasArg(OPT_shared);
397   config->stripAll = args.hasArg(OPT_strip_all);
398   config->stripDebug = args.hasArg(OPT_strip_debug);
399   config->stackFirst = args.hasArg(OPT_stack_first);
400   config->trace = args.hasArg(OPT_trace);
401   config->thinLTOCacheDir = args.getLastArgValue(OPT_thinlto_cache_dir);
402   config->thinLTOCachePolicy = CHECK(
403       parseCachePruningPolicy(args.getLastArgValue(OPT_thinlto_cache_policy)),
404       "--thinlto-cache-policy: invalid cache policy");
405   config->unresolvedSymbols = getUnresolvedSymbolPolicy(args);
406   errorHandler().verbose = args.hasArg(OPT_verbose);
407   LLVM_DEBUG(errorHandler().verbose = true);
408 
409   config->initialMemory = args::getInteger(args, OPT_initial_memory, 0);
410   config->globalBase = args::getInteger(args, OPT_global_base, 1024);
411   config->maxMemory = args::getInteger(args, OPT_max_memory, 0);
412   config->zStackSize =
413       args::getZOptionValue(args, OPT_z, "stack-size", WasmPageSize);
414 
415   // Default value of exportDynamic depends on `-shared`
416   config->exportDynamic =
417       args.hasFlag(OPT_export_dynamic, OPT_no_export_dynamic, config->shared);
418 
419   // Parse wasm32/64.
420   if (auto *arg = args.getLastArg(OPT_m)) {
421     StringRef s = arg->getValue();
422     if (s == "wasm32")
423       config->is64 = false;
424     else if (s == "wasm64")
425       config->is64 = true;
426     else
427       error("invalid target architecture: " + s);
428   }
429 
430   // --threads= takes a positive integer and provides the default value for
431   // --thinlto-jobs=.
432   if (auto *arg = args.getLastArg(OPT_threads)) {
433     StringRef v(arg->getValue());
434     unsigned threads = 0;
435     if (!llvm::to_integer(v, threads, 0) || threads == 0)
436       error(arg->getSpelling() + ": expected a positive integer, but got '" +
437             arg->getValue() + "'");
438     parallel::strategy = hardware_concurrency(threads);
439     config->thinLTOJobs = v;
440   }
441   if (auto *arg = args.getLastArg(OPT_thinlto_jobs))
442     config->thinLTOJobs = arg->getValue();
443 
444   if (auto *arg = args.getLastArg(OPT_features)) {
445     config->features =
446         llvm::Optional<std::vector<std::string>>(std::vector<std::string>());
447     for (StringRef s : arg->getValues())
448       config->features->push_back(std::string(s));
449   }
450 
451   if (args.hasArg(OPT_print_map))
452     config->mapFile = "-";
453 }
454 
455 // Some Config members do not directly correspond to any particular
456 // command line options, but computed based on other Config values.
457 // This function initialize such members. See Config.h for the details
458 // of these values.
459 static void setConfigs() {
460   config->isPic = config->pie || config->shared;
461 
462   if (config->isPic) {
463     if (config->exportTable)
464       error("-shared/-pie is incompatible with --export-table");
465     config->importTable = true;
466   }
467 
468   if (config->shared) {
469     config->importMemory = true;
470     config->unresolvedSymbols = UnresolvedPolicy::ImportFuncs;
471   }
472 }
473 
474 // Some command line options or some combinations of them are not allowed.
475 // This function checks for such errors.
476 static void checkOptions(opt::InputArgList &args) {
477   if (!config->stripDebug && !config->stripAll && config->compressRelocations)
478     error("--compress-relocations is incompatible with output debug"
479           " information. Please pass --strip-debug or --strip-all");
480 
481   if (config->ltoo > 3)
482     error("invalid optimization level for LTO: " + Twine(config->ltoo));
483   if (config->ltoPartitions == 0)
484     error("--lto-partitions: number of threads must be > 0");
485   if (!get_threadpool_strategy(config->thinLTOJobs))
486     error("--thinlto-jobs: invalid job count: " + config->thinLTOJobs);
487 
488   if (config->pie && config->shared)
489     error("-shared and -pie may not be used together");
490 
491   if (config->outputFile.empty())
492     error("no output file specified");
493 
494   if (config->importTable && config->exportTable)
495     error("--import-table and --export-table may not be used together");
496 
497   if (config->relocatable) {
498     if (!config->entry.empty())
499       error("entry point specified for relocatable output file");
500     if (config->gcSections)
501       error("-r and --gc-sections may not be used together");
502     if (config->compressRelocations)
503       error("-r -and --compress-relocations may not be used together");
504     if (args.hasArg(OPT_undefined))
505       error("-r -and --undefined may not be used together");
506     if (config->pie)
507       error("-r and -pie may not be used together");
508     if (config->sharedMemory)
509       error("-r and --shared-memory may not be used together");
510   }
511 
512   // To begin to prepare for Module Linking-style shared libraries, start
513   // warning about uses of `-shared` and related flags outside of Experimental
514   // mode, to give anyone using them a heads-up that they will be changing.
515   //
516   // Also, warn about flags which request explicit exports.
517   if (!config->experimentalPic) {
518     // -shared will change meaning when Module Linking is implemented.
519     if (config->shared) {
520       warn("creating shared libraries, with -shared, is not yet stable");
521     }
522 
523     // -pie will change meaning when Module Linking is implemented.
524     if (config->pie) {
525       warn("creating PIEs, with -pie, is not yet stable");
526     }
527   }
528 
529   if (config->bsymbolic && !config->shared) {
530     warn("-Bsymbolic is only meaningful when combined with -shared");
531   }
532 }
533 
534 // Force Sym to be entered in the output. Used for -u or equivalent.
535 static Symbol *handleUndefined(StringRef name) {
536   Symbol *sym = symtab->find(name);
537   if (!sym)
538     return nullptr;
539 
540   // Since symbol S may not be used inside the program, LTO may
541   // eliminate it. Mark the symbol as "used" to prevent it.
542   sym->isUsedInRegularObj = true;
543 
544   if (auto *lazySym = dyn_cast<LazySymbol>(sym))
545     lazySym->fetch();
546 
547   return sym;
548 }
549 
550 static void handleLibcall(StringRef name) {
551   Symbol *sym = symtab->find(name);
552   if (!sym)
553     return;
554 
555   if (auto *lazySym = dyn_cast<LazySymbol>(sym)) {
556     MemoryBufferRef mb = lazySym->getMemberBuffer();
557     if (isBitcode(mb))
558       lazySym->fetch();
559   }
560 }
561 
562 static UndefinedGlobal *
563 createUndefinedGlobal(StringRef name, llvm::wasm::WasmGlobalType *type) {
564   auto *sym = cast<UndefinedGlobal>(symtab->addUndefinedGlobal(
565       name, None, None, WASM_SYMBOL_UNDEFINED, nullptr, type));
566   config->allowUndefinedSymbols.insert(sym->getName());
567   sym->isUsedInRegularObj = true;
568   return sym;
569 }
570 
571 static InputGlobal *createGlobal(StringRef name, bool isMutable) {
572   llvm::wasm::WasmGlobal wasmGlobal;
573   if (config->is64.getValueOr(false)) {
574     wasmGlobal.Type = {WASM_TYPE_I64, isMutable};
575     wasmGlobal.InitExpr.Opcode = WASM_OPCODE_I64_CONST;
576     wasmGlobal.InitExpr.Value.Int64 = 0;
577   } else {
578     wasmGlobal.Type = {WASM_TYPE_I32, isMutable};
579     wasmGlobal.InitExpr.Opcode = WASM_OPCODE_I32_CONST;
580     wasmGlobal.InitExpr.Value.Int32 = 0;
581   }
582   wasmGlobal.SymbolName = name;
583   return make<InputGlobal>(wasmGlobal, nullptr);
584 }
585 
586 static GlobalSymbol *createGlobalVariable(StringRef name, bool isMutable) {
587   InputGlobal *g = createGlobal(name, isMutable);
588   return symtab->addSyntheticGlobal(name, WASM_SYMBOL_VISIBILITY_HIDDEN, g);
589 }
590 
591 static GlobalSymbol *createOptionalGlobal(StringRef name, bool isMutable) {
592   InputGlobal *g = createGlobal(name, isMutable);
593   return symtab->addOptionalGlobalSymbols(name, WASM_SYMBOL_VISIBILITY_HIDDEN,
594                                           g);
595 }
596 
597 // Create ABI-defined synthetic symbols
598 static void createSyntheticSymbols() {
599   if (config->relocatable)
600     return;
601 
602   static WasmSignature nullSignature = {{}, {}};
603   static WasmSignature i32ArgSignature = {{}, {ValType::I32}};
604   static WasmSignature i64ArgSignature = {{}, {ValType::I64}};
605   static llvm::wasm::WasmGlobalType globalTypeI32 = {WASM_TYPE_I32, false};
606   static llvm::wasm::WasmGlobalType globalTypeI64 = {WASM_TYPE_I64, false};
607   static llvm::wasm::WasmGlobalType mutableGlobalTypeI32 = {WASM_TYPE_I32,
608                                                             true};
609   static llvm::wasm::WasmGlobalType mutableGlobalTypeI64 = {WASM_TYPE_I64,
610                                                             true};
611   WasmSym::callCtors = symtab->addSyntheticFunction(
612       "__wasm_call_ctors", WASM_SYMBOL_VISIBILITY_HIDDEN,
613       make<SyntheticFunction>(nullSignature, "__wasm_call_ctors"));
614 
615   if (config->isPic) {
616     // For PIC code we create a synthetic function __wasm_apply_relocs which
617     // is called from __wasm_call_ctors before the user-level constructors.
618     WasmSym::applyRelocs = symtab->addSyntheticFunction(
619         "__wasm_apply_relocs", WASM_SYMBOL_VISIBILITY_HIDDEN,
620         make<SyntheticFunction>(nullSignature, "__wasm_apply_relocs"));
621   }
622 
623   if (config->isPic) {
624     WasmSym::stackPointer =
625         createUndefinedGlobal("__stack_pointer", config->is64.getValueOr(false)
626                                                      ? &mutableGlobalTypeI64
627                                                      : &mutableGlobalTypeI32);
628     // For PIC code, we import two global variables (__memory_base and
629     // __table_base) from the environment and use these as the offset at
630     // which to load our static data and function table.
631     // See:
632     // https://github.com/WebAssembly/tool-conventions/blob/master/DynamicLinking.md
633     WasmSym::memoryBase = createUndefinedGlobal(
634         "__memory_base",
635         config->is64.getValueOr(false) ? &globalTypeI64 : &globalTypeI32);
636     WasmSym::tableBase = createUndefinedGlobal("__table_base", &globalTypeI32);
637     WasmSym::memoryBase->markLive();
638     WasmSym::tableBase->markLive();
639   } else {
640     // For non-PIC code
641     WasmSym::stackPointer = createGlobalVariable("__stack_pointer", true);
642     WasmSym::stackPointer->markLive();
643   }
644 
645   if (config->sharedMemory && !config->shared) {
646     // Passive segments are used to avoid memory being reinitialized on each
647     // thread's instantiation. These passive segments are initialized and
648     // dropped in __wasm_init_memory, which is registered as the start function
649     WasmSym::initMemory = symtab->addSyntheticFunction(
650         "__wasm_init_memory", WASM_SYMBOL_VISIBILITY_HIDDEN,
651         make<SyntheticFunction>(nullSignature, "__wasm_init_memory"));
652     WasmSym::initMemoryFlag = symtab->addSyntheticDataSymbol(
653         "__wasm_init_memory_flag", WASM_SYMBOL_VISIBILITY_HIDDEN);
654     assert(WasmSym::initMemoryFlag);
655     WasmSym::tlsBase = createGlobalVariable("__tls_base", true);
656     WasmSym::tlsSize = createGlobalVariable("__tls_size", false);
657     WasmSym::tlsAlign = createGlobalVariable("__tls_align", false);
658     WasmSym::initTLS = symtab->addSyntheticFunction(
659         "__wasm_init_tls", WASM_SYMBOL_VISIBILITY_HIDDEN,
660         make<SyntheticFunction>(
661             config->is64.getValueOr(false) ? i64ArgSignature : i32ArgSignature,
662             "__wasm_init_tls"));
663   }
664 }
665 
666 static void createOptionalSymbols() {
667   if (config->relocatable)
668     return;
669 
670   WasmSym::dsoHandle = symtab->addOptionalDataSymbol("__dso_handle");
671 
672   if (!config->shared)
673     WasmSym::dataEnd = symtab->addOptionalDataSymbol("__data_end");
674 
675   if (!config->isPic) {
676     WasmSym::globalBase = symtab->addOptionalDataSymbol("__global_base");
677     WasmSym::heapBase = symtab->addOptionalDataSymbol("__heap_base");
678     WasmSym::definedMemoryBase = symtab->addOptionalDataSymbol("__memory_base");
679     WasmSym::definedTableBase = symtab->addOptionalDataSymbol("__table_base");
680   }
681 
682   // For non-shared memory programs we still need to define __tls_base since we
683   // allow object files built with TLS to be linked into single threaded
684   // programs, and such object files can contains refernced to this symbol.
685   //
686   // However, in this case __tls_base is immutable and points directly to the
687   // start of the `.tdata` static segment.
688   //
689   // __tls_size and __tls_align are not needed in this case since they are only
690   // needed for __wasm_init_tls (which we do not create in this case).
691   if (!config->sharedMemory)
692     WasmSym::tlsBase = createOptionalGlobal("__tls_base", false);
693 }
694 
695 // Reconstructs command line arguments so that so that you can re-run
696 // the same command with the same inputs. This is for --reproduce.
697 static std::string createResponseFile(const opt::InputArgList &args) {
698   SmallString<0> data;
699   raw_svector_ostream os(data);
700 
701   // Copy the command line to the output while rewriting paths.
702   for (auto *arg : args) {
703     switch (arg->getOption().getID()) {
704     case OPT_reproduce:
705       break;
706     case OPT_INPUT:
707       os << quote(relativeToRoot(arg->getValue())) << "\n";
708       break;
709     case OPT_o:
710       // If -o path contains directories, "lld @response.txt" will likely
711       // fail because the archive we are creating doesn't contain empty
712       // directories for the output path (-o doesn't create directories).
713       // Strip directories to prevent the issue.
714       os << "-o " << quote(sys::path::filename(arg->getValue())) << "\n";
715       break;
716     default:
717       os << toString(*arg) << "\n";
718     }
719   }
720   return std::string(data.str());
721 }
722 
723 // The --wrap option is a feature to rename symbols so that you can write
724 // wrappers for existing functions. If you pass `-wrap=foo`, all
725 // occurrences of symbol `foo` are resolved to `wrap_foo` (so, you are
726 // expected to write `wrap_foo` function as a wrapper). The original
727 // symbol becomes accessible as `real_foo`, so you can call that from your
728 // wrapper.
729 //
730 // This data structure is instantiated for each -wrap option.
731 struct WrappedSymbol {
732   Symbol *sym;
733   Symbol *real;
734   Symbol *wrap;
735 };
736 
737 static Symbol *addUndefined(StringRef name) {
738   return symtab->addUndefinedFunction(name, None, None, WASM_SYMBOL_UNDEFINED,
739                                       nullptr, nullptr, false);
740 }
741 
742 // Handles -wrap option.
743 //
744 // This function instantiates wrapper symbols. At this point, they seem
745 // like they are not being used at all, so we explicitly set some flags so
746 // that LTO won't eliminate them.
747 static std::vector<WrappedSymbol> addWrappedSymbols(opt::InputArgList &args) {
748   std::vector<WrappedSymbol> v;
749   DenseSet<StringRef> seen;
750 
751   for (auto *arg : args.filtered(OPT_wrap)) {
752     StringRef name = arg->getValue();
753     if (!seen.insert(name).second)
754       continue;
755 
756     Symbol *sym = symtab->find(name);
757     if (!sym)
758       continue;
759 
760     Symbol *real = addUndefined(saver.save("__real_" + name));
761     Symbol *wrap = addUndefined(saver.save("__wrap_" + name));
762     v.push_back({sym, real, wrap});
763 
764     // We want to tell LTO not to inline symbols to be overwritten
765     // because LTO doesn't know the final symbol contents after renaming.
766     real->canInline = false;
767     sym->canInline = false;
768 
769     // Tell LTO not to eliminate these symbols.
770     sym->isUsedInRegularObj = true;
771     wrap->isUsedInRegularObj = true;
772     real->isUsedInRegularObj = false;
773   }
774   return v;
775 }
776 
777 // Do renaming for -wrap by updating pointers to symbols.
778 //
779 // When this function is executed, only InputFiles and symbol table
780 // contain pointers to symbol objects. We visit them to replace pointers,
781 // so that wrapped symbols are swapped as instructed by the command line.
782 static void wrapSymbols(ArrayRef<WrappedSymbol> wrapped) {
783   DenseMap<Symbol *, Symbol *> map;
784   for (const WrappedSymbol &w : wrapped) {
785     map[w.sym] = w.wrap;
786     map[w.real] = w.sym;
787   }
788 
789   // Update pointers in input files.
790   parallelForEach(symtab->objectFiles, [&](InputFile *file) {
791     MutableArrayRef<Symbol *> syms = file->getMutableSymbols();
792     for (size_t i = 0, e = syms.size(); i != e; ++i)
793       if (Symbol *s = map.lookup(syms[i]))
794         syms[i] = s;
795   });
796 
797   // Update pointers in the symbol table.
798   for (const WrappedSymbol &w : wrapped)
799     symtab->wrap(w.sym, w.real, w.wrap);
800 }
801 
802 void LinkerDriver::link(ArrayRef<const char *> argsArr) {
803   WasmOptTable parser;
804   opt::InputArgList args = parser.parse(argsArr.slice(1));
805 
806   // Handle --help
807   if (args.hasArg(OPT_help)) {
808     parser.PrintHelp(lld::outs(),
809                      (std::string(argsArr[0]) + " [options] file...").c_str(),
810                      "LLVM Linker", false);
811     return;
812   }
813 
814   // Handle --version
815   if (args.hasArg(OPT_version) || args.hasArg(OPT_v)) {
816     lld::outs() << getLLDVersion() << "\n";
817     return;
818   }
819 
820   // Handle --reproduce
821   if (auto *arg = args.getLastArg(OPT_reproduce)) {
822     StringRef path = arg->getValue();
823     Expected<std::unique_ptr<TarWriter>> errOrWriter =
824         TarWriter::create(path, path::stem(path));
825     if (errOrWriter) {
826       tar = std::move(*errOrWriter);
827       tar->append("response.txt", createResponseFile(args));
828       tar->append("version.txt", getLLDVersion() + "\n");
829     } else {
830       error("--reproduce: " + toString(errOrWriter.takeError()));
831     }
832   }
833 
834   // Parse and evaluate -mllvm options.
835   std::vector<const char *> v;
836   v.push_back("wasm-ld (LLVM option parsing)");
837   for (auto *arg : args.filtered(OPT_mllvm))
838     v.push_back(arg->getValue());
839   cl::ResetAllOptionOccurrences();
840   cl::ParseCommandLineOptions(v.size(), v.data());
841 
842   errorHandler().errorLimit = args::getInteger(args, OPT_error_limit, 20);
843 
844   readConfigs(args);
845 
846   createFiles(args);
847   if (errorCount())
848     return;
849 
850   setConfigs();
851   checkOptions(args);
852   if (errorCount())
853     return;
854 
855   if (auto *arg = args.getLastArg(OPT_allow_undefined_file))
856     readImportFile(arg->getValue());
857 
858   // Fail early if the output file or map file is not writable. If a user has a
859   // long link, e.g. due to a large LTO link, they do not wish to run it and
860   // find that it failed because there was a mistake in their command-line.
861   if (auto e = tryCreateFile(config->outputFile))
862     error("cannot open output file " + config->outputFile + ": " + e.message());
863   if (auto e = tryCreateFile(config->mapFile))
864     error("cannot open map file " + config->mapFile + ": " + e.message());
865   if (errorCount())
866     return;
867 
868   // Handle --trace-symbol.
869   for (auto *arg : args.filtered(OPT_trace_symbol))
870     symtab->trace(arg->getValue());
871 
872   for (auto *arg : args.filtered(OPT_export))
873     config->exportedSymbols.insert(arg->getValue());
874 
875   createSyntheticSymbols();
876 
877   // Add all files to the symbol table. This will add almost all
878   // symbols that we need to the symbol table.
879   for (InputFile *f : files)
880     symtab->addFile(f);
881   if (errorCount())
882     return;
883 
884   // Handle the `--undefined <sym>` options.
885   for (auto *arg : args.filtered(OPT_undefined))
886     handleUndefined(arg->getValue());
887 
888   // Handle the `--export <sym>` options
889   // This works like --undefined but also exports the symbol if its found
890   for (auto *arg : args.filtered(OPT_export))
891     handleUndefined(arg->getValue());
892 
893   Symbol *entrySym = nullptr;
894   if (!config->relocatable && !config->entry.empty()) {
895     entrySym = handleUndefined(config->entry);
896     if (entrySym && entrySym->isDefined())
897       entrySym->forceExport = true;
898     else
899       error("entry symbol not defined (pass --no-entry to suppress): " +
900             config->entry);
901   }
902 
903   // If the user code defines a `__wasm_call_dtors` function, remember it so
904   // that we can call it from the command export wrappers. Unlike
905   // `__wasm_call_ctors` which we synthesize, `__wasm_call_dtors` is defined
906   // by libc/etc., because destructors are registered dynamically with
907   // `__cxa_atexit` and friends.
908   if (!config->relocatable && !config->shared &&
909       !WasmSym::callCtors->isUsedInRegularObj &&
910       WasmSym::callCtors->getName() != config->entry &&
911       !config->exportedSymbols.count(WasmSym::callCtors->getName())) {
912     if (Symbol *callDtors = handleUndefined("__wasm_call_dtors")) {
913       if (auto *callDtorsFunc = dyn_cast<DefinedFunction>(callDtors)) {
914         if (callDtorsFunc->signature &&
915             (!callDtorsFunc->signature->Params.empty() ||
916              !callDtorsFunc->signature->Returns.empty())) {
917           error("__wasm_call_dtors must have no argument or return values");
918         }
919         WasmSym::callDtors = callDtorsFunc;
920       } else {
921         error("__wasm_call_dtors must be a function");
922       }
923     }
924   }
925 
926   createOptionalSymbols();
927 
928   if (errorCount())
929     return;
930 
931   // Create wrapped symbols for -wrap option.
932   std::vector<WrappedSymbol> wrapped = addWrappedSymbols(args);
933 
934   // If any of our inputs are bitcode files, the LTO code generator may create
935   // references to certain library functions that might not be explicit in the
936   // bitcode file's symbol table. If any of those library functions are defined
937   // in a bitcode file in an archive member, we need to arrange to use LTO to
938   // compile those archive members by adding them to the link beforehand.
939   //
940   // We only need to add libcall symbols to the link before LTO if the symbol's
941   // definition is in bitcode. Any other required libcall symbols will be added
942   // to the link after LTO when we add the LTO object file to the link.
943   if (!symtab->bitcodeFiles.empty())
944     for (auto *s : lto::LTO::getRuntimeLibcallSymbols())
945       handleLibcall(s);
946   if (errorCount())
947     return;
948 
949   // Do link-time optimization if given files are LLVM bitcode files.
950   // This compiles bitcode files into real object files.
951   symtab->addCombinedLTOObject();
952   if (errorCount())
953     return;
954 
955   // Resolve any variant symbols that were created due to signature
956   // mismatchs.
957   symtab->handleSymbolVariants();
958   if (errorCount())
959     return;
960 
961   // Apply symbol renames for -wrap.
962   if (!wrapped.empty())
963     wrapSymbols(wrapped);
964 
965   for (auto *arg : args.filtered(OPT_export)) {
966     Symbol *sym = symtab->find(arg->getValue());
967     if (sym && sym->isDefined())
968       sym->forceExport = true;
969     else if (config->unresolvedSymbols == UnresolvedPolicy::ReportError)
970       error(Twine("symbol exported via --export not found: ") +
971             arg->getValue());
972     else if (config->unresolvedSymbols == UnresolvedPolicy::Warn)
973       warn(Twine("symbol exported via --export not found: ") + arg->getValue());
974   }
975 
976   if (!config->relocatable) {
977     // Add synthetic dummies for weak undefined functions.  Must happen
978     // after LTO otherwise functions may not yet have signatures.
979     symtab->handleWeakUndefines();
980   }
981 
982   if (entrySym)
983     entrySym->setHidden(false);
984 
985   if (errorCount())
986     return;
987 
988   // Do size optimizations: garbage collection
989   markLive();
990 
991   // Write the result to the file.
992   writeResult();
993 }
994 
995 } // namespace wasm
996 } // namespace lld
997