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