xref: /llvm-project-15.0.7/lld/wasm/Driver.cpp (revision 77f0ea4b)
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->ltoNewPassManager = args.hasFlag(OPT_lto_new_pass_manager,
383                                            OPT_no_lto_new_pass_manager, false);
384   config->ltoDebugPassManager = args.hasArg(OPT_lto_debug_pass_manager);
385   config->mapFile = args.getLastArgValue(OPT_Map);
386   config->optimize = args::getInteger(args, OPT_O, 0);
387   config->outputFile = args.getLastArgValue(OPT_o);
388   config->relocatable = args.hasArg(OPT_relocatable);
389   config->gcSections =
390       args.hasFlag(OPT_gc_sections, OPT_no_gc_sections, !config->relocatable);
391   config->mergeDataSegments =
392       args.hasFlag(OPT_merge_data_segments, OPT_no_merge_data_segments,
393                    !config->relocatable);
394   config->pie = args.hasFlag(OPT_pie, OPT_no_pie, false);
395   config->printGcSections =
396       args.hasFlag(OPT_print_gc_sections, OPT_no_print_gc_sections, false);
397   config->saveTemps = args.hasArg(OPT_save_temps);
398   config->searchPaths = args::getStrings(args, OPT_L);
399   config->shared = args.hasArg(OPT_shared);
400   config->stripAll = args.hasArg(OPT_strip_all);
401   config->stripDebug = args.hasArg(OPT_strip_debug);
402   config->stackFirst = args.hasArg(OPT_stack_first);
403   config->trace = args.hasArg(OPT_trace);
404   config->thinLTOCacheDir = args.getLastArgValue(OPT_thinlto_cache_dir);
405   config->thinLTOCachePolicy = CHECK(
406       parseCachePruningPolicy(args.getLastArgValue(OPT_thinlto_cache_policy)),
407       "--thinlto-cache-policy: invalid cache policy");
408   config->unresolvedSymbols = getUnresolvedSymbolPolicy(args);
409   errorHandler().verbose = args.hasArg(OPT_verbose);
410   LLVM_DEBUG(errorHandler().verbose = true);
411 
412   config->initialMemory = args::getInteger(args, OPT_initial_memory, 0);
413   config->globalBase = args::getInteger(args, OPT_global_base, 1024);
414   config->maxMemory = args::getInteger(args, OPT_max_memory, 0);
415   config->zStackSize =
416       args::getZOptionValue(args, OPT_z, "stack-size", WasmPageSize);
417 
418   // Default value of exportDynamic depends on `-shared`
419   config->exportDynamic =
420       args.hasFlag(OPT_export_dynamic, OPT_no_export_dynamic, config->shared);
421 
422   // Parse wasm32/64.
423   if (auto *arg = args.getLastArg(OPT_m)) {
424     StringRef s = arg->getValue();
425     if (s == "wasm32")
426       config->is64 = false;
427     else if (s == "wasm64")
428       config->is64 = true;
429     else
430       error("invalid target architecture: " + s);
431   }
432 
433   // --threads= takes a positive integer and provides the default value for
434   // --thinlto-jobs=.
435   if (auto *arg = args.getLastArg(OPT_threads)) {
436     StringRef v(arg->getValue());
437     unsigned threads = 0;
438     if (!llvm::to_integer(v, threads, 0) || threads == 0)
439       error(arg->getSpelling() + ": expected a positive integer, but got '" +
440             arg->getValue() + "'");
441     parallel::strategy = hardware_concurrency(threads);
442     config->thinLTOJobs = v;
443   }
444   if (auto *arg = args.getLastArg(OPT_thinlto_jobs))
445     config->thinLTOJobs = arg->getValue();
446 
447   if (auto *arg = args.getLastArg(OPT_features)) {
448     config->features =
449         llvm::Optional<std::vector<std::string>>(std::vector<std::string>());
450     for (StringRef s : arg->getValues())
451       config->features->push_back(std::string(s));
452   }
453 
454   if (args.hasArg(OPT_print_map))
455     config->mapFile = "-";
456 }
457 
458 // Some Config members do not directly correspond to any particular
459 // command line options, but computed based on other Config values.
460 // This function initialize such members. See Config.h for the details
461 // of these values.
462 static void setConfigs() {
463   config->isPic = config->pie || config->shared;
464 
465   if (config->isPic) {
466     if (config->exportTable)
467       error("-shared/-pie is incompatible with --export-table");
468     config->importTable = true;
469   }
470 
471   if (config->shared) {
472     config->importMemory = true;
473     config->unresolvedSymbols = UnresolvedPolicy::ImportFuncs;
474   }
475 }
476 
477 // Some command line options or some combinations of them are not allowed.
478 // This function checks for such errors.
479 static void checkOptions(opt::InputArgList &args) {
480   if (!config->stripDebug && !config->stripAll && config->compressRelocations)
481     error("--compress-relocations is incompatible with output debug"
482           " information. Please pass --strip-debug or --strip-all");
483 
484   if (config->ltoo > 3)
485     error("invalid optimization level for LTO: " + Twine(config->ltoo));
486   if (config->ltoPartitions == 0)
487     error("--lto-partitions: number of threads must be > 0");
488   if (!get_threadpool_strategy(config->thinLTOJobs))
489     error("--thinlto-jobs: invalid job count: " + config->thinLTOJobs);
490 
491   if (config->pie && config->shared)
492     error("-shared and -pie may not be used together");
493 
494   if (config->outputFile.empty())
495     error("no output file specified");
496 
497   if (config->importTable && config->exportTable)
498     error("--import-table and --export-table may not be used together");
499 
500   if (config->relocatable) {
501     if (!config->entry.empty())
502       error("entry point specified for relocatable output file");
503     if (config->gcSections)
504       error("-r and --gc-sections may not be used together");
505     if (config->compressRelocations)
506       error("-r -and --compress-relocations may not be used together");
507     if (args.hasArg(OPT_undefined))
508       error("-r -and --undefined may not be used together");
509     if (config->pie)
510       error("-r and -pie may not be used together");
511     if (config->sharedMemory)
512       error("-r and --shared-memory may not be used together");
513   }
514 
515   // To begin to prepare for Module Linking-style shared libraries, start
516   // warning about uses of `-shared` and related flags outside of Experimental
517   // mode, to give anyone using them a heads-up that they will be changing.
518   //
519   // Also, warn about flags which request explicit exports.
520   if (!config->experimentalPic) {
521     // -shared will change meaning when Module Linking is implemented.
522     if (config->shared) {
523       warn("creating shared libraries, with -shared, is not yet stable");
524     }
525 
526     // -pie will change meaning when Module Linking is implemented.
527     if (config->pie) {
528       warn("creating PIEs, with -pie, is not yet stable");
529     }
530   }
531 
532   if (config->bsymbolic && !config->shared) {
533     warn("-Bsymbolic is only meaningful when combined with -shared");
534   }
535 }
536 
537 // Force Sym to be entered in the output. Used for -u or equivalent.
538 static Symbol *handleUndefined(StringRef name) {
539   Symbol *sym = symtab->find(name);
540   if (!sym)
541     return nullptr;
542 
543   // Since symbol S may not be used inside the program, LTO may
544   // eliminate it. Mark the symbol as "used" to prevent it.
545   sym->isUsedInRegularObj = true;
546 
547   if (auto *lazySym = dyn_cast<LazySymbol>(sym))
548     lazySym->fetch();
549 
550   return sym;
551 }
552 
553 static void handleLibcall(StringRef name) {
554   Symbol *sym = symtab->find(name);
555   if (!sym)
556     return;
557 
558   if (auto *lazySym = dyn_cast<LazySymbol>(sym)) {
559     MemoryBufferRef mb = lazySym->getMemberBuffer();
560     if (isBitcode(mb))
561       lazySym->fetch();
562   }
563 }
564 
565 static UndefinedGlobal *
566 createUndefinedGlobal(StringRef name, llvm::wasm::WasmGlobalType *type) {
567   auto *sym = cast<UndefinedGlobal>(symtab->addUndefinedGlobal(
568       name, None, None, WASM_SYMBOL_UNDEFINED, nullptr, type));
569   config->allowUndefinedSymbols.insert(sym->getName());
570   sym->isUsedInRegularObj = true;
571   return sym;
572 }
573 
574 static InputGlobal *createGlobal(StringRef name, bool isMutable) {
575   llvm::wasm::WasmGlobal wasmGlobal;
576   if (config->is64.getValueOr(false)) {
577     wasmGlobal.Type = {WASM_TYPE_I64, isMutable};
578     wasmGlobal.InitExpr.Opcode = WASM_OPCODE_I64_CONST;
579     wasmGlobal.InitExpr.Value.Int64 = 0;
580   } else {
581     wasmGlobal.Type = {WASM_TYPE_I32, isMutable};
582     wasmGlobal.InitExpr.Opcode = WASM_OPCODE_I32_CONST;
583     wasmGlobal.InitExpr.Value.Int32 = 0;
584   }
585   wasmGlobal.SymbolName = name;
586   return make<InputGlobal>(wasmGlobal, nullptr);
587 }
588 
589 static GlobalSymbol *createGlobalVariable(StringRef name, bool isMutable) {
590   InputGlobal *g = createGlobal(name, isMutable);
591   return symtab->addSyntheticGlobal(name, WASM_SYMBOL_VISIBILITY_HIDDEN, g);
592 }
593 
594 static GlobalSymbol *createOptionalGlobal(StringRef name, bool isMutable) {
595   InputGlobal *g = createGlobal(name, isMutable);
596   return symtab->addOptionalGlobalSymbols(name, WASM_SYMBOL_VISIBILITY_HIDDEN,
597                                           g);
598 }
599 
600 // Create ABI-defined synthetic symbols
601 static void createSyntheticSymbols() {
602   if (config->relocatable)
603     return;
604 
605   static WasmSignature nullSignature = {{}, {}};
606   static WasmSignature i32ArgSignature = {{}, {ValType::I32}};
607   static WasmSignature i64ArgSignature = {{}, {ValType::I64}};
608   static llvm::wasm::WasmGlobalType globalTypeI32 = {WASM_TYPE_I32, false};
609   static llvm::wasm::WasmGlobalType globalTypeI64 = {WASM_TYPE_I64, false};
610   static llvm::wasm::WasmGlobalType mutableGlobalTypeI32 = {WASM_TYPE_I32,
611                                                             true};
612   static llvm::wasm::WasmGlobalType mutableGlobalTypeI64 = {WASM_TYPE_I64,
613                                                             true};
614   WasmSym::callCtors = symtab->addSyntheticFunction(
615       "__wasm_call_ctors", WASM_SYMBOL_VISIBILITY_HIDDEN,
616       make<SyntheticFunction>(nullSignature, "__wasm_call_ctors"));
617 
618   if (config->isPic) {
619     // For PIC code we create a synthetic function __wasm_apply_relocs which
620     // is called from __wasm_call_ctors before the user-level constructors.
621     WasmSym::applyRelocs = symtab->addSyntheticFunction(
622         "__wasm_apply_relocs", WASM_SYMBOL_VISIBILITY_HIDDEN,
623         make<SyntheticFunction>(nullSignature, "__wasm_apply_relocs"));
624   }
625 
626   if (config->isPic) {
627     WasmSym::stackPointer =
628         createUndefinedGlobal("__stack_pointer", config->is64.getValueOr(false)
629                                                      ? &mutableGlobalTypeI64
630                                                      : &mutableGlobalTypeI32);
631     // For PIC code, we import two global variables (__memory_base and
632     // __table_base) from the environment and use these as the offset at
633     // which to load our static data and function table.
634     // See:
635     // https://github.com/WebAssembly/tool-conventions/blob/master/DynamicLinking.md
636     WasmSym::memoryBase = createUndefinedGlobal(
637         "__memory_base",
638         config->is64.getValueOr(false) ? &globalTypeI64 : &globalTypeI32);
639     WasmSym::tableBase = createUndefinedGlobal("__table_base", &globalTypeI32);
640     WasmSym::memoryBase->markLive();
641     WasmSym::tableBase->markLive();
642   } else {
643     // For non-PIC code
644     WasmSym::stackPointer = createGlobalVariable("__stack_pointer", true);
645     WasmSym::stackPointer->markLive();
646   }
647 
648   if (config->sharedMemory) {
649     // Passive segments are used to avoid memory being reinitialized on each
650     // thread's instantiation. These passive segments are initialized and
651     // dropped in __wasm_init_memory, which is registered as the start function
652     WasmSym::initMemory = symtab->addSyntheticFunction(
653         "__wasm_init_memory", WASM_SYMBOL_VISIBILITY_HIDDEN,
654         make<SyntheticFunction>(nullSignature, "__wasm_init_memory"));
655     WasmSym::initMemoryFlag = symtab->addSyntheticDataSymbol(
656         "__wasm_init_memory_flag", WASM_SYMBOL_VISIBILITY_HIDDEN);
657     assert(WasmSym::initMemoryFlag);
658     WasmSym::tlsBase = createGlobalVariable("__tls_base", true);
659     WasmSym::tlsSize = createGlobalVariable("__tls_size", false);
660     WasmSym::tlsAlign = createGlobalVariable("__tls_align", false);
661     WasmSym::initTLS = symtab->addSyntheticFunction(
662         "__wasm_init_tls", WASM_SYMBOL_VISIBILITY_HIDDEN,
663         make<SyntheticFunction>(
664             config->is64.getValueOr(false) ? i64ArgSignature : i32ArgSignature,
665             "__wasm_init_tls"));
666   }
667 }
668 
669 static void createOptionalSymbols() {
670   if (config->relocatable)
671     return;
672 
673   WasmSym::dsoHandle = symtab->addOptionalDataSymbol("__dso_handle");
674 
675   if (!config->shared)
676     WasmSym::dataEnd = symtab->addOptionalDataSymbol("__data_end");
677 
678   if (!config->isPic) {
679     WasmSym::globalBase = symtab->addOptionalDataSymbol("__global_base");
680     WasmSym::heapBase = symtab->addOptionalDataSymbol("__heap_base");
681     WasmSym::definedMemoryBase = symtab->addOptionalDataSymbol("__memory_base");
682     WasmSym::definedTableBase = symtab->addOptionalDataSymbol("__table_base");
683   }
684 
685   // For non-shared memory programs we still need to define __tls_base since we
686   // allow object files built with TLS to be linked into single threaded
687   // programs, and such object files can contains refernced to this symbol.
688   //
689   // However, in this case __tls_base is immutable and points directly to the
690   // start of the `.tdata` static segment.
691   //
692   // __tls_size and __tls_align are not needed in this case since they are only
693   // needed for __wasm_init_tls (which we do not create in this case).
694   if (!config->sharedMemory)
695     WasmSym::tlsBase = createOptionalGlobal("__tls_base", false);
696 }
697 
698 // Reconstructs command line arguments so that so that you can re-run
699 // the same command with the same inputs. This is for --reproduce.
700 static std::string createResponseFile(const opt::InputArgList &args) {
701   SmallString<0> data;
702   raw_svector_ostream os(data);
703 
704   // Copy the command line to the output while rewriting paths.
705   for (auto *arg : args) {
706     switch (arg->getOption().getID()) {
707     case OPT_reproduce:
708       break;
709     case OPT_INPUT:
710       os << quote(relativeToRoot(arg->getValue())) << "\n";
711       break;
712     case OPT_o:
713       // If -o path contains directories, "lld @response.txt" will likely
714       // fail because the archive we are creating doesn't contain empty
715       // directories for the output path (-o doesn't create directories).
716       // Strip directories to prevent the issue.
717       os << "-o " << quote(sys::path::filename(arg->getValue())) << "\n";
718       break;
719     default:
720       os << toString(*arg) << "\n";
721     }
722   }
723   return std::string(data.str());
724 }
725 
726 // The --wrap option is a feature to rename symbols so that you can write
727 // wrappers for existing functions. If you pass `-wrap=foo`, all
728 // occurrences of symbol `foo` are resolved to `wrap_foo` (so, you are
729 // expected to write `wrap_foo` function as a wrapper). The original
730 // symbol becomes accessible as `real_foo`, so you can call that from your
731 // wrapper.
732 //
733 // This data structure is instantiated for each -wrap option.
734 struct WrappedSymbol {
735   Symbol *sym;
736   Symbol *real;
737   Symbol *wrap;
738 };
739 
740 static Symbol *addUndefined(StringRef name) {
741   return symtab->addUndefinedFunction(name, None, None, WASM_SYMBOL_UNDEFINED,
742                                       nullptr, nullptr, false);
743 }
744 
745 // Handles -wrap option.
746 //
747 // This function instantiates wrapper symbols. At this point, they seem
748 // like they are not being used at all, so we explicitly set some flags so
749 // that LTO won't eliminate them.
750 static std::vector<WrappedSymbol> addWrappedSymbols(opt::InputArgList &args) {
751   std::vector<WrappedSymbol> v;
752   DenseSet<StringRef> seen;
753 
754   for (auto *arg : args.filtered(OPT_wrap)) {
755     StringRef name = arg->getValue();
756     if (!seen.insert(name).second)
757       continue;
758 
759     Symbol *sym = symtab->find(name);
760     if (!sym)
761       continue;
762 
763     Symbol *real = addUndefined(saver.save("__real_" + name));
764     Symbol *wrap = addUndefined(saver.save("__wrap_" + name));
765     v.push_back({sym, real, wrap});
766 
767     // We want to tell LTO not to inline symbols to be overwritten
768     // because LTO doesn't know the final symbol contents after renaming.
769     real->canInline = false;
770     sym->canInline = false;
771 
772     // Tell LTO not to eliminate these symbols.
773     sym->isUsedInRegularObj = true;
774     wrap->isUsedInRegularObj = true;
775     real->isUsedInRegularObj = false;
776   }
777   return v;
778 }
779 
780 // Do renaming for -wrap by updating pointers to symbols.
781 //
782 // When this function is executed, only InputFiles and symbol table
783 // contain pointers to symbol objects. We visit them to replace pointers,
784 // so that wrapped symbols are swapped as instructed by the command line.
785 static void wrapSymbols(ArrayRef<WrappedSymbol> wrapped) {
786   DenseMap<Symbol *, Symbol *> map;
787   for (const WrappedSymbol &w : wrapped) {
788     map[w.sym] = w.wrap;
789     map[w.real] = w.sym;
790   }
791 
792   // Update pointers in input files.
793   parallelForEach(symtab->objectFiles, [&](InputFile *file) {
794     MutableArrayRef<Symbol *> syms = file->getMutableSymbols();
795     for (size_t i = 0, e = syms.size(); i != e; ++i)
796       if (Symbol *s = map.lookup(syms[i]))
797         syms[i] = s;
798   });
799 
800   // Update pointers in the symbol table.
801   for (const WrappedSymbol &w : wrapped)
802     symtab->wrap(w.sym, w.real, w.wrap);
803 }
804 
805 void LinkerDriver::link(ArrayRef<const char *> argsArr) {
806   WasmOptTable parser;
807   opt::InputArgList args = parser.parse(argsArr.slice(1));
808 
809   // Handle --help
810   if (args.hasArg(OPT_help)) {
811     parser.PrintHelp(lld::outs(),
812                      (std::string(argsArr[0]) + " [options] file...").c_str(),
813                      "LLVM Linker", false);
814     return;
815   }
816 
817   // Handle --version
818   if (args.hasArg(OPT_version) || args.hasArg(OPT_v)) {
819     lld::outs() << getLLDVersion() << "\n";
820     return;
821   }
822 
823   // Handle --reproduce
824   if (auto *arg = args.getLastArg(OPT_reproduce)) {
825     StringRef path = arg->getValue();
826     Expected<std::unique_ptr<TarWriter>> errOrWriter =
827         TarWriter::create(path, path::stem(path));
828     if (errOrWriter) {
829       tar = std::move(*errOrWriter);
830       tar->append("response.txt", createResponseFile(args));
831       tar->append("version.txt", getLLDVersion() + "\n");
832     } else {
833       error("--reproduce: " + toString(errOrWriter.takeError()));
834     }
835   }
836 
837   // Parse and evaluate -mllvm options.
838   std::vector<const char *> v;
839   v.push_back("wasm-ld (LLVM option parsing)");
840   for (auto *arg : args.filtered(OPT_mllvm))
841     v.push_back(arg->getValue());
842   cl::ResetAllOptionOccurrences();
843   cl::ParseCommandLineOptions(v.size(), v.data());
844 
845   errorHandler().errorLimit = args::getInteger(args, OPT_error_limit, 20);
846 
847   readConfigs(args);
848 
849   createFiles(args);
850   if (errorCount())
851     return;
852 
853   setConfigs();
854   checkOptions(args);
855   if (errorCount())
856     return;
857 
858   if (auto *arg = args.getLastArg(OPT_allow_undefined_file))
859     readImportFile(arg->getValue());
860 
861   // Fail early if the output file or map file is not writable. If a user has a
862   // long link, e.g. due to a large LTO link, they do not wish to run it and
863   // find that it failed because there was a mistake in their command-line.
864   if (auto e = tryCreateFile(config->outputFile))
865     error("cannot open output file " + config->outputFile + ": " + e.message());
866   if (auto e = tryCreateFile(config->mapFile))
867     error("cannot open map file " + config->mapFile + ": " + e.message());
868   if (errorCount())
869     return;
870 
871   // Handle --trace-symbol.
872   for (auto *arg : args.filtered(OPT_trace_symbol))
873     symtab->trace(arg->getValue());
874 
875   for (auto *arg : args.filtered(OPT_export))
876     config->exportedSymbols.insert(arg->getValue());
877 
878   createSyntheticSymbols();
879 
880   // Add all files to the symbol table. This will add almost all
881   // symbols that we need to the symbol table.
882   for (InputFile *f : files)
883     symtab->addFile(f);
884   if (errorCount())
885     return;
886 
887   // Handle the `--undefined <sym>` options.
888   for (auto *arg : args.filtered(OPT_undefined))
889     handleUndefined(arg->getValue());
890 
891   // Handle the `--export <sym>` options
892   // This works like --undefined but also exports the symbol if its found
893   for (auto *arg : args.filtered(OPT_export))
894     handleUndefined(arg->getValue());
895 
896   Symbol *entrySym = nullptr;
897   if (!config->relocatable && !config->entry.empty()) {
898     entrySym = handleUndefined(config->entry);
899     if (entrySym && entrySym->isDefined())
900       entrySym->forceExport = true;
901     else
902       error("entry symbol not defined (pass --no-entry to suppress): " +
903             config->entry);
904   }
905 
906   // If the user code defines a `__wasm_call_dtors` function, remember it so
907   // that we can call it from the command export wrappers. Unlike
908   // `__wasm_call_ctors` which we synthesize, `__wasm_call_dtors` is defined
909   // by libc/etc., because destructors are registered dynamically with
910   // `__cxa_atexit` and friends.
911   if (!config->relocatable && !config->shared &&
912       !WasmSym::callCtors->isUsedInRegularObj &&
913       WasmSym::callCtors->getName() != config->entry &&
914       !config->exportedSymbols.count(WasmSym::callCtors->getName())) {
915     if (Symbol *callDtors = handleUndefined("__wasm_call_dtors")) {
916       if (auto *callDtorsFunc = dyn_cast<DefinedFunction>(callDtors)) {
917         if (callDtorsFunc->signature &&
918             (!callDtorsFunc->signature->Params.empty() ||
919              !callDtorsFunc->signature->Returns.empty())) {
920           error("__wasm_call_dtors must have no argument or return values");
921         }
922         WasmSym::callDtors = callDtorsFunc;
923       } else {
924         error("__wasm_call_dtors must be a function");
925       }
926     }
927   }
928 
929   createOptionalSymbols();
930 
931   if (errorCount())
932     return;
933 
934   // Create wrapped symbols for -wrap option.
935   std::vector<WrappedSymbol> wrapped = addWrappedSymbols(args);
936 
937   // If any of our inputs are bitcode files, the LTO code generator may create
938   // references to certain library functions that might not be explicit in the
939   // bitcode file's symbol table. If any of those library functions are defined
940   // in a bitcode file in an archive member, we need to arrange to use LTO to
941   // compile those archive members by adding them to the link beforehand.
942   //
943   // We only need to add libcall symbols to the link before LTO if the symbol's
944   // definition is in bitcode. Any other required libcall symbols will be added
945   // to the link after LTO when we add the LTO object file to the link.
946   if (!symtab->bitcodeFiles.empty())
947     for (auto *s : lto::LTO::getRuntimeLibcallSymbols())
948       handleLibcall(s);
949   if (errorCount())
950     return;
951 
952   // Do link-time optimization if given files are LLVM bitcode files.
953   // This compiles bitcode files into real object files.
954   symtab->addCombinedLTOObject();
955   if (errorCount())
956     return;
957 
958   // Resolve any variant symbols that were created due to signature
959   // mismatchs.
960   symtab->handleSymbolVariants();
961   if (errorCount())
962     return;
963 
964   // Apply symbol renames for -wrap.
965   if (!wrapped.empty())
966     wrapSymbols(wrapped);
967 
968   for (auto *arg : args.filtered(OPT_export)) {
969     Symbol *sym = symtab->find(arg->getValue());
970     if (sym && sym->isDefined())
971       sym->forceExport = true;
972     else if (config->unresolvedSymbols == UnresolvedPolicy::ReportError)
973       error(Twine("symbol exported via --export not found: ") +
974             arg->getValue());
975     else if (config->unresolvedSymbols == UnresolvedPolicy::Warn)
976       warn(Twine("symbol exported via --export not found: ") + arg->getValue());
977   }
978 
979   if (!config->relocatable && !config->isPic) {
980     // Add synthetic dummies for weak undefined functions.  Must happen
981     // after LTO otherwise functions may not yet have signatures.
982     symtab->handleWeakUndefines();
983   }
984 
985   if (entrySym)
986     entrySym->setHidden(false);
987 
988   if (errorCount())
989     return;
990 
991   // Do size optimizations: garbage collection
992   markLive();
993 
994   // Write the result to the file.
995   writeResult();
996 }
997 
998 } // namespace wasm
999 } // namespace lld
1000