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