xref: /llvm-project/lld/wasm/Driver.cpp (revision 3792b36234b6c87d728f0a905543e284bf961460)
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 "InputElement.h"
13 #include "MarkLive.h"
14 #include "SymbolTable.h"
15 #include "Writer.h"
16 #include "lld/Common/Args.h"
17 #include "lld/Common/CommonLinkerContext.h"
18 #include "lld/Common/ErrorHandler.h"
19 #include "lld/Common/Filesystem.h"
20 #include "lld/Common/Memory.h"
21 #include "lld/Common/Reproduce.h"
22 #include "lld/Common/Strings.h"
23 #include "lld/Common/Version.h"
24 #include "llvm/ADT/Twine.h"
25 #include "llvm/Config/llvm-config.h"
26 #include "llvm/Object/Wasm.h"
27 #include "llvm/Option/Arg.h"
28 #include "llvm/Option/ArgList.h"
29 #include "llvm/Support/CommandLine.h"
30 #include "llvm/Support/Parallel.h"
31 #include "llvm/Support/Path.h"
32 #include "llvm/Support/Process.h"
33 #include "llvm/Support/TarWriter.h"
34 #include "llvm/Support/TargetSelect.h"
35 #include "llvm/TargetParser/Host.h"
36 #include <optional>
37 
38 #define DEBUG_TYPE "lld"
39 
40 using namespace llvm;
41 using namespace llvm::object;
42 using namespace llvm::opt;
43 using namespace llvm::sys;
44 using namespace llvm::wasm;
45 
46 namespace lld::wasm {
47 Ctx ctx;
48 
49 void errorOrWarn(const llvm::Twine &msg) {
50   if (ctx.arg.noinhibitExec)
51     warn(msg);
52   else
53     error(msg);
54 }
55 
56 Ctx::Ctx() {}
57 
58 void Ctx::reset() {
59   arg.~Config();
60   new (&arg) Config();
61   objectFiles.clear();
62   stubFiles.clear();
63   sharedFiles.clear();
64   bitcodeFiles.clear();
65   lazyBitcodeFiles.clear();
66   syntheticFunctions.clear();
67   syntheticGlobals.clear();
68   syntheticTables.clear();
69   whyExtractRecords.clear();
70   isPic = false;
71   legacyFunctionTable = false;
72   emitBssSegments = false;
73 }
74 
75 namespace {
76 
77 // Create enum with OPT_xxx values for each option in Options.td
78 enum {
79   OPT_INVALID = 0,
80 #define OPTION(...) LLVM_MAKE_OPT_ID(__VA_ARGS__),
81 #include "Options.inc"
82 #undef OPTION
83 };
84 
85 // This function is called on startup. We need this for LTO since
86 // LTO calls LLVM functions to compile bitcode files to native code.
87 // Technically this can be delayed until we read bitcode files, but
88 // we don't bother to do lazily because the initialization is fast.
89 static void initLLVM() {
90   InitializeAllTargets();
91   InitializeAllTargetMCs();
92   InitializeAllAsmPrinters();
93   InitializeAllAsmParsers();
94 }
95 
96 class LinkerDriver {
97 public:
98   LinkerDriver(Ctx &);
99   void linkerMain(ArrayRef<const char *> argsArr);
100 
101 private:
102   void createFiles(opt::InputArgList &args);
103   void addFile(StringRef path);
104   void addLibrary(StringRef name);
105 
106   Ctx &ctx;
107 
108   // True if we are in --whole-archive and --no-whole-archive.
109   bool inWholeArchive = false;
110 
111   // True if we are in --start-lib and --end-lib.
112   bool inLib = false;
113 
114   std::vector<InputFile *> files;
115 };
116 
117 static bool hasZOption(opt::InputArgList &args, StringRef key) {
118   bool ret = false;
119   for (const auto *arg : args.filtered(OPT_z))
120     if (key == arg->getValue()) {
121       ret = true;
122       arg->claim();
123     }
124   return ret;
125 }
126 } // anonymous namespace
127 
128 bool link(ArrayRef<const char *> args, llvm::raw_ostream &stdoutOS,
129           llvm::raw_ostream &stderrOS, bool exitEarly, bool disableOutput) {
130   // This driver-specific context will be freed later by unsafeLldMain().
131   auto *context = new CommonLinkerContext;
132 
133   context->e.initialize(stdoutOS, stderrOS, exitEarly, disableOutput);
134   context->e.cleanupCallback = []() { ctx.reset(); };
135   context->e.logName = args::getFilenameWithoutExe(args[0]);
136   context->e.errorLimitExceededMsg =
137       "too many errors emitted, stopping now (use "
138       "-error-limit=0 to see all errors)";
139 
140   symtab = make<SymbolTable>();
141 
142   initLLVM();
143   LinkerDriver(ctx).linkerMain(args);
144 
145   return errorCount() == 0;
146 }
147 
148 #define OPTTABLE_STR_TABLE_CODE
149 #include "Options.inc"
150 #undef OPTTABLE_STR_TABLE_CODE
151 
152 #define OPTTABLE_PREFIXES_TABLE_CODE
153 #include "Options.inc"
154 #undef OPTTABLE_PREFIXES_TABLE_CODE
155 
156 // Create table mapping all options defined in Options.td
157 static constexpr opt::OptTable::Info optInfo[] = {
158 #define OPTION(PREFIX, NAME, ID, KIND, GROUP, ALIAS, ALIASARGS, FLAGS,         \
159                VISIBILITY, PARAM, HELPTEXT, HELPTEXTSFORVARIANTS, METAVAR,     \
160                VALUES)                                                         \
161   {PREFIX,                                                                     \
162    NAME,                                                                       \
163    HELPTEXT,                                                                   \
164    HELPTEXTSFORVARIANTS,                                                       \
165    METAVAR,                                                                    \
166    OPT_##ID,                                                                   \
167    opt::Option::KIND##Class,                                                   \
168    PARAM,                                                                      \
169    FLAGS,                                                                      \
170    VISIBILITY,                                                                 \
171    OPT_##GROUP,                                                                \
172    OPT_##ALIAS,                                                                \
173    ALIASARGS,                                                                  \
174    VALUES},
175 #include "Options.inc"
176 #undef OPTION
177 };
178 
179 namespace {
180 class WasmOptTable : public opt::GenericOptTable {
181 public:
182   WasmOptTable()
183       : opt::GenericOptTable(OptionStrTable, OptionPrefixesTable, optInfo) {}
184   opt::InputArgList parse(ArrayRef<const char *> argv);
185 };
186 } // namespace
187 
188 // Set color diagnostics according to -color-diagnostics={auto,always,never}
189 // or -no-color-diagnostics flags.
190 static void handleColorDiagnostics(opt::InputArgList &args) {
191   auto *arg = args.getLastArg(OPT_color_diagnostics, OPT_color_diagnostics_eq,
192                               OPT_no_color_diagnostics);
193   if (!arg)
194     return;
195   auto &errs = errorHandler().errs();
196   if (arg->getOption().getID() == OPT_color_diagnostics) {
197     errs.enable_colors(true);
198   } else if (arg->getOption().getID() == OPT_no_color_diagnostics) {
199     errs.enable_colors(false);
200   } else {
201     StringRef s = arg->getValue();
202     if (s == "always")
203       errs.enable_colors(true);
204     else if (s == "never")
205       errs.enable_colors(false);
206     else if (s != "auto")
207       error("unknown option: --color-diagnostics=" + s);
208   }
209 }
210 
211 static cl::TokenizerCallback getQuotingStyle(opt::InputArgList &args) {
212   if (auto *arg = args.getLastArg(OPT_rsp_quoting)) {
213     StringRef s = arg->getValue();
214     if (s != "windows" && s != "posix")
215       error("invalid response file quoting: " + s);
216     if (s == "windows")
217       return cl::TokenizeWindowsCommandLine;
218     return cl::TokenizeGNUCommandLine;
219   }
220   if (Triple(sys::getProcessTriple()).isOSWindows())
221     return cl::TokenizeWindowsCommandLine;
222   return cl::TokenizeGNUCommandLine;
223 }
224 
225 // Find a file by concatenating given paths.
226 static std::optional<std::string> findFile(StringRef path1,
227                                            const Twine &path2) {
228   SmallString<128> s;
229   path::append(s, path1, path2);
230   if (fs::exists(s))
231     return std::string(s);
232   return std::nullopt;
233 }
234 
235 opt::InputArgList WasmOptTable::parse(ArrayRef<const char *> argv) {
236   SmallVector<const char *, 256> vec(argv.data(), argv.data() + argv.size());
237 
238   unsigned missingIndex;
239   unsigned missingCount;
240 
241   // We need to get the quoting style for response files before parsing all
242   // options so we parse here before and ignore all the options but
243   // --rsp-quoting.
244   opt::InputArgList args = this->ParseArgs(vec, missingIndex, missingCount);
245 
246   // Expand response files (arguments in the form of @<filename>)
247   // and then parse the argument again.
248   cl::ExpandResponseFiles(saver(), getQuotingStyle(args), vec);
249   args = this->ParseArgs(vec, missingIndex, missingCount);
250 
251   handleColorDiagnostics(args);
252   if (missingCount)
253     error(Twine(args.getArgString(missingIndex)) + ": missing argument");
254 
255   for (auto *arg : args.filtered(OPT_UNKNOWN))
256     error("unknown argument: " + arg->getAsString(args));
257   return args;
258 }
259 
260 // Currently we allow a ".imports" to live alongside a library. This can
261 // be used to specify a list of symbols which can be undefined at link
262 // time (imported from the environment.  For example libc.a include an
263 // import file that lists the syscall functions it relies on at runtime.
264 // In the long run this information would be better stored as a symbol
265 // attribute/flag in the object file itself.
266 // See: https://github.com/WebAssembly/tool-conventions/issues/35
267 static void readImportFile(StringRef filename) {
268   if (std::optional<MemoryBufferRef> buf = readFile(filename))
269     for (StringRef sym : args::getLines(*buf))
270       ctx.arg.allowUndefinedSymbols.insert(sym);
271 }
272 
273 // Returns slices of MB by parsing MB as an archive file.
274 // Each slice consists of a member file in the archive.
275 std::vector<std::pair<MemoryBufferRef, uint64_t>> static getArchiveMembers(
276     MemoryBufferRef mb) {
277   std::unique_ptr<Archive> file =
278       CHECK(Archive::create(mb),
279             mb.getBufferIdentifier() + ": failed to parse archive");
280 
281   std::vector<std::pair<MemoryBufferRef, uint64_t>> v;
282   Error err = Error::success();
283   for (const Archive::Child &c : file->children(err)) {
284     MemoryBufferRef mbref =
285         CHECK(c.getMemoryBufferRef(),
286               mb.getBufferIdentifier() +
287                   ": could not get the buffer for a child of the archive");
288     v.push_back(std::make_pair(mbref, c.getChildOffset()));
289   }
290   if (err)
291     fatal(mb.getBufferIdentifier() +
292           ": Archive::children failed: " + toString(std::move(err)));
293 
294   // Take ownership of memory buffers created for members of thin archives.
295   for (std::unique_ptr<MemoryBuffer> &mb : file->takeThinBuffers())
296     make<std::unique_ptr<MemoryBuffer>>(std::move(mb));
297 
298   return v;
299 }
300 
301 void LinkerDriver::addFile(StringRef path) {
302   std::optional<MemoryBufferRef> buffer = readFile(path);
303   if (!buffer)
304     return;
305   MemoryBufferRef mbref = *buffer;
306 
307   switch (identify_magic(mbref.getBuffer())) {
308   case file_magic::archive: {
309     SmallString<128> importFile = path;
310     path::replace_extension(importFile, ".imports");
311     if (fs::exists(importFile))
312       readImportFile(importFile.str());
313 
314     auto members = getArchiveMembers(mbref);
315 
316     // Handle -whole-archive.
317     if (inWholeArchive) {
318       for (const auto &[m, offset] : members) {
319         auto *object = createObjectFile(m, path, offset);
320         // Mark object as live; object members are normally not
321         // live by default but -whole-archive is designed to treat
322         // them as such.
323         object->markLive();
324         files.push_back(object);
325       }
326 
327       return;
328     }
329 
330     std::unique_ptr<Archive> file =
331         CHECK(Archive::create(mbref), path + ": failed to parse archive");
332 
333     for (const auto &[m, offset] : members) {
334       auto magic = identify_magic(m.getBuffer());
335       if (magic == file_magic::wasm_object || magic == file_magic::bitcode)
336         files.push_back(createObjectFile(m, path, offset, true));
337       else
338         warn(path + ": archive member '" + m.getBufferIdentifier() +
339              "' is neither Wasm object file nor LLVM bitcode");
340     }
341 
342     return;
343   }
344   case file_magic::bitcode:
345   case file_magic::wasm_object: {
346     auto obj = createObjectFile(mbref, "", 0, inLib);
347     if (ctx.arg.isStatic && isa<SharedFile>(obj)) {
348       error("attempted static link of dynamic object " + path);
349       break;
350     }
351     files.push_back(obj);
352     break;
353   }
354   case file_magic::unknown:
355     if (mbref.getBuffer().starts_with("#STUB")) {
356       files.push_back(make<StubFile>(mbref));
357       break;
358     }
359     [[fallthrough]];
360   default:
361     error("unknown file type: " + mbref.getBufferIdentifier());
362   }
363 }
364 
365 static std::optional<std::string> findFromSearchPaths(StringRef path) {
366   for (StringRef dir : ctx.arg.searchPaths)
367     if (std::optional<std::string> s = findFile(dir, path))
368       return s;
369   return std::nullopt;
370 }
371 
372 // This is for -l<basename>. We'll look for lib<basename>.a from
373 // search paths.
374 static std::optional<std::string> searchLibraryBaseName(StringRef name) {
375   for (StringRef dir : ctx.arg.searchPaths) {
376     if (!ctx.arg.isStatic)
377       if (std::optional<std::string> s = findFile(dir, "lib" + name + ".so"))
378         return s;
379     if (std::optional<std::string> s = findFile(dir, "lib" + name + ".a"))
380       return s;
381   }
382   return std::nullopt;
383 }
384 
385 // This is for -l<namespec>.
386 static std::optional<std::string> searchLibrary(StringRef name) {
387   if (name.starts_with(":"))
388     return findFromSearchPaths(name.substr(1));
389   return searchLibraryBaseName(name);
390 }
391 
392 // Add a given library by searching it from input search paths.
393 void LinkerDriver::addLibrary(StringRef name) {
394   if (std::optional<std::string> path = searchLibrary(name))
395     addFile(saver().save(*path));
396   else
397     error("unable to find library -l" + name, ErrorTag::LibNotFound, {name});
398 }
399 
400 void LinkerDriver::createFiles(opt::InputArgList &args) {
401   for (auto *arg : args) {
402     switch (arg->getOption().getID()) {
403     case OPT_library:
404       addLibrary(arg->getValue());
405       break;
406     case OPT_INPUT:
407       addFile(arg->getValue());
408       break;
409     case OPT_Bstatic:
410       ctx.arg.isStatic = true;
411       break;
412     case OPT_Bdynamic:
413       ctx.arg.isStatic = false;
414       break;
415     case OPT_whole_archive:
416       inWholeArchive = true;
417       break;
418     case OPT_no_whole_archive:
419       inWholeArchive = false;
420       break;
421     case OPT_start_lib:
422       if (inLib)
423         error("nested --start-lib");
424       inLib = true;
425       break;
426     case OPT_end_lib:
427       if (!inLib)
428         error("stray --end-lib");
429       inLib = false;
430       break;
431     }
432   }
433   if (files.empty() && errorCount() == 0)
434     error("no input files");
435 }
436 
437 static StringRef getAliasSpelling(opt::Arg *arg) {
438   if (const opt::Arg *alias = arg->getAlias())
439     return alias->getSpelling();
440   return arg->getSpelling();
441 }
442 
443 static std::pair<StringRef, StringRef> getOldNewOptions(opt::InputArgList &args,
444                                                         unsigned id) {
445   auto *arg = args.getLastArg(id);
446   if (!arg)
447     return {"", ""};
448 
449   StringRef s = arg->getValue();
450   std::pair<StringRef, StringRef> ret = s.split(';');
451   if (ret.second.empty())
452     error(getAliasSpelling(arg) + " expects 'old;new' format, but got " + s);
453   return ret;
454 }
455 
456 // Parse options of the form "old;new[;extra]".
457 static std::tuple<StringRef, StringRef, StringRef>
458 getOldNewOptionsExtra(opt::InputArgList &args, unsigned id) {
459   auto [oldDir, second] = getOldNewOptions(args, id);
460   auto [newDir, extraDir] = second.split(';');
461   return {oldDir, newDir, extraDir};
462 }
463 
464 static StringRef getEntry(opt::InputArgList &args) {
465   auto *arg = args.getLastArg(OPT_entry, OPT_no_entry);
466   if (!arg) {
467     if (args.hasArg(OPT_relocatable))
468       return "";
469     if (args.hasArg(OPT_shared))
470       return "__wasm_call_ctors";
471     return "_start";
472   }
473   if (arg->getOption().getID() == OPT_no_entry)
474     return "";
475   return arg->getValue();
476 }
477 
478 // Determines what we should do if there are remaining unresolved
479 // symbols after the name resolution.
480 static UnresolvedPolicy getUnresolvedSymbolPolicy(opt::InputArgList &args) {
481   UnresolvedPolicy errorOrWarn = args.hasFlag(OPT_error_unresolved_symbols,
482                                               OPT_warn_unresolved_symbols, true)
483                                      ? UnresolvedPolicy::ReportError
484                                      : UnresolvedPolicy::Warn;
485 
486   if (auto *arg = args.getLastArg(OPT_unresolved_symbols)) {
487     StringRef s = arg->getValue();
488     if (s == "ignore-all")
489       return UnresolvedPolicy::Ignore;
490     if (s == "import-dynamic")
491       return UnresolvedPolicy::ImportDynamic;
492     if (s == "report-all")
493       return errorOrWarn;
494     error("unknown --unresolved-symbols value: " + s);
495   }
496 
497   return errorOrWarn;
498 }
499 
500 // Parse --build-id or --build-id=<style>. We handle "tree" as a
501 // synonym for "sha1" because all our hash functions including
502 // -build-id=sha1 are actually tree hashes for performance reasons.
503 static std::pair<BuildIdKind, SmallVector<uint8_t, 0>>
504 getBuildId(opt::InputArgList &args) {
505   auto *arg = args.getLastArg(OPT_build_id, OPT_build_id_eq);
506   if (!arg)
507     return {BuildIdKind::None, {}};
508 
509   if (arg->getOption().getID() == OPT_build_id)
510     return {BuildIdKind::Fast, {}};
511 
512   StringRef s = arg->getValue();
513   if (s == "fast")
514     return {BuildIdKind::Fast, {}};
515   if (s == "sha1" || s == "tree")
516     return {BuildIdKind::Sha1, {}};
517   if (s == "uuid")
518     return {BuildIdKind::Uuid, {}};
519   if (s.starts_with("0x"))
520     return {BuildIdKind::Hexstring, parseHex(s.substr(2))};
521 
522   if (s != "none")
523     error("unknown --build-id style: " + s);
524   return {BuildIdKind::None, {}};
525 }
526 
527 // Initializes Config members by the command line options.
528 static void readConfigs(opt::InputArgList &args) {
529   ctx.arg.allowMultipleDefinition =
530       hasZOption(args, "muldefs") ||
531       args.hasFlag(OPT_allow_multiple_definition,
532                    OPT_no_allow_multiple_definition, false);
533   ctx.arg.bsymbolic = args.hasArg(OPT_Bsymbolic);
534   ctx.arg.checkFeatures =
535       args.hasFlag(OPT_check_features, OPT_no_check_features, true);
536   ctx.arg.compressRelocations = args.hasArg(OPT_compress_relocations);
537   ctx.arg.demangle = args.hasFlag(OPT_demangle, OPT_no_demangle, true);
538   ctx.arg.disableVerify = args.hasArg(OPT_disable_verify);
539   ctx.arg.emitRelocs = args.hasArg(OPT_emit_relocs);
540   ctx.arg.experimentalPic = args.hasArg(OPT_experimental_pic);
541   ctx.arg.entry = getEntry(args);
542   ctx.arg.exportAll = args.hasArg(OPT_export_all);
543   ctx.arg.exportTable = args.hasArg(OPT_export_table);
544   ctx.arg.growableTable = args.hasArg(OPT_growable_table);
545   ctx.arg.noinhibitExec = args.hasArg(OPT_noinhibit_exec);
546 
547   if (args.hasArg(OPT_import_memory_with_name)) {
548     ctx.arg.memoryImport =
549         args.getLastArgValue(OPT_import_memory_with_name).split(",");
550   } else if (args.hasArg(OPT_import_memory)) {
551     ctx.arg.memoryImport =
552         std::pair<llvm::StringRef, llvm::StringRef>(defaultModule, memoryName);
553   } else {
554     ctx.arg.memoryImport =
555         std::optional<std::pair<llvm::StringRef, llvm::StringRef>>();
556   }
557 
558   if (args.hasArg(OPT_export_memory_with_name)) {
559     ctx.arg.memoryExport = args.getLastArgValue(OPT_export_memory_with_name);
560   } else if (args.hasArg(OPT_export_memory)) {
561     ctx.arg.memoryExport = memoryName;
562   } else {
563     ctx.arg.memoryExport = std::optional<llvm::StringRef>();
564   }
565 
566   ctx.arg.sharedMemory = args.hasArg(OPT_shared_memory);
567   ctx.arg.soName = args.getLastArgValue(OPT_soname);
568   ctx.arg.importTable = args.hasArg(OPT_import_table);
569   ctx.arg.importUndefined = args.hasArg(OPT_import_undefined);
570   ctx.arg.ltoo = args::getInteger(args, OPT_lto_O, 2);
571   if (ctx.arg.ltoo > 3)
572     error("invalid optimization level for LTO: " + Twine(ctx.arg.ltoo));
573   unsigned ltoCgo =
574       args::getInteger(args, OPT_lto_CGO, args::getCGOptLevel(ctx.arg.ltoo));
575   if (auto level = CodeGenOpt::getLevel(ltoCgo))
576     ctx.arg.ltoCgo = *level;
577   else
578     error("invalid codegen optimization level for LTO: " + Twine(ltoCgo));
579   ctx.arg.ltoPartitions = args::getInteger(args, OPT_lto_partitions, 1);
580   ctx.arg.ltoObjPath = args.getLastArgValue(OPT_lto_obj_path_eq);
581   ctx.arg.ltoDebugPassManager = args.hasArg(OPT_lto_debug_pass_manager);
582   ctx.arg.mapFile = args.getLastArgValue(OPT_Map);
583   ctx.arg.optimize = args::getInteger(args, OPT_O, 1);
584   ctx.arg.outputFile = args.getLastArgValue(OPT_o);
585   ctx.arg.relocatable = args.hasArg(OPT_relocatable);
586   ctx.arg.gcSections =
587       args.hasFlag(OPT_gc_sections, OPT_no_gc_sections, !ctx.arg.relocatable);
588   for (auto *arg : args.filtered(OPT_keep_section))
589     ctx.arg.keepSections.insert(arg->getValue());
590   ctx.arg.mergeDataSegments =
591       args.hasFlag(OPT_merge_data_segments, OPT_no_merge_data_segments,
592                    !ctx.arg.relocatable);
593   ctx.arg.pie = args.hasFlag(OPT_pie, OPT_no_pie, false);
594   ctx.arg.printGcSections =
595       args.hasFlag(OPT_print_gc_sections, OPT_no_print_gc_sections, false);
596   ctx.arg.saveTemps = args.hasArg(OPT_save_temps);
597   ctx.arg.searchPaths = args::getStrings(args, OPT_library_path);
598   ctx.arg.shared = args.hasArg(OPT_shared);
599   ctx.arg.shlibSigCheck = !args.hasArg(OPT_no_shlib_sigcheck);
600   ctx.arg.stripAll = args.hasArg(OPT_strip_all);
601   ctx.arg.stripDebug = args.hasArg(OPT_strip_debug);
602   ctx.arg.stackFirst = args.hasArg(OPT_stack_first);
603   ctx.arg.trace = args.hasArg(OPT_trace);
604   ctx.arg.thinLTOCacheDir = args.getLastArgValue(OPT_thinlto_cache_dir);
605   ctx.arg.thinLTOCachePolicy = CHECK(
606       parseCachePruningPolicy(args.getLastArgValue(OPT_thinlto_cache_policy)),
607       "--thinlto-cache-policy: invalid cache policy");
608   ctx.arg.thinLTOEmitImportsFiles = args.hasArg(OPT_thinlto_emit_imports_files);
609   ctx.arg.thinLTOEmitIndexFiles = args.hasArg(OPT_thinlto_emit_index_files) ||
610                                   args.hasArg(OPT_thinlto_index_only) ||
611                                   args.hasArg(OPT_thinlto_index_only_eq);
612   ctx.arg.thinLTOIndexOnly = args.hasArg(OPT_thinlto_index_only) ||
613                              args.hasArg(OPT_thinlto_index_only_eq);
614   ctx.arg.thinLTOIndexOnlyArg = args.getLastArgValue(OPT_thinlto_index_only_eq);
615   ctx.arg.thinLTOObjectSuffixReplace =
616       getOldNewOptions(args, OPT_thinlto_object_suffix_replace_eq);
617   std::tie(ctx.arg.thinLTOPrefixReplaceOld, ctx.arg.thinLTOPrefixReplaceNew,
618            ctx.arg.thinLTOPrefixReplaceNativeObject) =
619       getOldNewOptionsExtra(args, OPT_thinlto_prefix_replace_eq);
620   if (ctx.arg.thinLTOEmitIndexFiles && !ctx.arg.thinLTOIndexOnly) {
621     if (args.hasArg(OPT_thinlto_object_suffix_replace_eq))
622       error("--thinlto-object-suffix-replace is not supported with "
623             "--thinlto-emit-index-files");
624     else if (args.hasArg(OPT_thinlto_prefix_replace_eq))
625       error("--thinlto-prefix-replace is not supported with "
626             "--thinlto-emit-index-files");
627   }
628   if (!ctx.arg.thinLTOPrefixReplaceNativeObject.empty() &&
629       ctx.arg.thinLTOIndexOnlyArg.empty()) {
630     error("--thinlto-prefix-replace=old_dir;new_dir;obj_dir must be used with "
631           "--thinlto-index-only=");
632   }
633   ctx.arg.unresolvedSymbols = getUnresolvedSymbolPolicy(args);
634   ctx.arg.whyExtract = args.getLastArgValue(OPT_why_extract);
635   errorHandler().verbose = args.hasArg(OPT_verbose);
636   LLVM_DEBUG(errorHandler().verbose = true);
637 
638   ctx.arg.tableBase = args::getInteger(args, OPT_table_base, 0);
639   ctx.arg.globalBase = args::getInteger(args, OPT_global_base, 0);
640   ctx.arg.initialHeap = args::getInteger(args, OPT_initial_heap, 0);
641   ctx.arg.initialMemory = args::getInteger(args, OPT_initial_memory, 0);
642   ctx.arg.maxMemory = args::getInteger(args, OPT_max_memory, 0);
643   ctx.arg.noGrowableMemory = args.hasArg(OPT_no_growable_memory);
644   ctx.arg.zStackSize =
645       args::getZOptionValue(args, OPT_z, "stack-size", WasmPageSize);
646 
647   // -Bdynamic by default if -pie or -shared is specified.
648   if (ctx.arg.pie || ctx.arg.shared)
649     ctx.arg.isStatic = false;
650 
651   if (ctx.arg.maxMemory != 0 && ctx.arg.noGrowableMemory) {
652     // Erroring out here is simpler than defining precedence rules.
653     error("--max-memory is incompatible with --no-growable-memory");
654   }
655 
656   // Default value of exportDynamic depends on `-shared`
657   ctx.arg.exportDynamic =
658       args.hasFlag(OPT_export_dynamic, OPT_no_export_dynamic, ctx.arg.shared);
659 
660   // Parse wasm32/64.
661   if (auto *arg = args.getLastArg(OPT_m)) {
662     StringRef s = arg->getValue();
663     if (s == "wasm32")
664       ctx.arg.is64 = false;
665     else if (s == "wasm64")
666       ctx.arg.is64 = true;
667     else
668       error("invalid target architecture: " + s);
669   }
670 
671   // --threads= takes a positive integer and provides the default value for
672   // --thinlto-jobs=.
673   if (auto *arg = args.getLastArg(OPT_threads)) {
674     StringRef v(arg->getValue());
675     unsigned threads = 0;
676     if (!llvm::to_integer(v, threads, 0) || threads == 0)
677       error(arg->getSpelling() + ": expected a positive integer, but got '" +
678             arg->getValue() + "'");
679     parallel::strategy = hardware_concurrency(threads);
680     ctx.arg.thinLTOJobs = v;
681   }
682   if (auto *arg = args.getLastArg(OPT_thinlto_jobs))
683     ctx.arg.thinLTOJobs = arg->getValue();
684 
685   if (auto *arg = args.getLastArg(OPT_features)) {
686     ctx.arg.features =
687         std::optional<std::vector<std::string>>(std::vector<std::string>());
688     for (StringRef s : arg->getValues())
689       ctx.arg.features->push_back(std::string(s));
690   }
691 
692   if (auto *arg = args.getLastArg(OPT_extra_features)) {
693     ctx.arg.extraFeatures =
694         std::optional<std::vector<std::string>>(std::vector<std::string>());
695     for (StringRef s : arg->getValues())
696       ctx.arg.extraFeatures->push_back(std::string(s));
697   }
698 
699   // Legacy --allow-undefined flag which is equivalent to
700   // --unresolve-symbols=ignore + --import-undefined
701   if (args.hasArg(OPT_allow_undefined)) {
702     ctx.arg.importUndefined = true;
703     ctx.arg.unresolvedSymbols = UnresolvedPolicy::Ignore;
704   }
705 
706   if (args.hasArg(OPT_print_map))
707     ctx.arg.mapFile = "-";
708 
709   std::tie(ctx.arg.buildId, ctx.arg.buildIdVector) = getBuildId(args);
710 }
711 
712 // Some Config members do not directly correspond to any particular
713 // command line options, but computed based on other Config values.
714 // This function initialize such members. See Config.h for the details
715 // of these values.
716 static void setConfigs() {
717   ctx.isPic = ctx.arg.pie || ctx.arg.shared;
718 
719   if (ctx.isPic) {
720     if (ctx.arg.exportTable)
721       error("-shared/-pie is incompatible with --export-table");
722     ctx.arg.importTable = true;
723   } else {
724     // Default table base.  Defaults to 1, reserving 0 for the NULL function
725     // pointer.
726     if (!ctx.arg.tableBase)
727       ctx.arg.tableBase = 1;
728     // The default offset for static/global data, for when --global-base is
729     // not specified on the command line.  The precise value of 1024 is
730     // somewhat arbitrary, and pre-dates wasm-ld (Its the value that
731     // emscripten used prior to wasm-ld).
732     if (!ctx.arg.globalBase && !ctx.arg.relocatable && !ctx.arg.stackFirst)
733       ctx.arg.globalBase = 1024;
734   }
735 
736   if (ctx.arg.relocatable) {
737     if (ctx.arg.exportTable)
738       error("--relocatable is incompatible with --export-table");
739     if (ctx.arg.growableTable)
740       error("--relocatable is incompatible with --growable-table");
741     // Ignore any --import-table, as it's redundant.
742     ctx.arg.importTable = true;
743   }
744 
745   if (ctx.arg.shared) {
746     if (ctx.arg.memoryExport.has_value()) {
747       error("--export-memory is incompatible with --shared");
748     }
749     if (!ctx.arg.memoryImport.has_value()) {
750       ctx.arg.memoryImport = std::pair<llvm::StringRef, llvm::StringRef>(
751           defaultModule, memoryName);
752     }
753   }
754 
755   // If neither export-memory nor import-memory is specified, default to
756   // exporting memory under its default name.
757   if (!ctx.arg.memoryExport.has_value() && !ctx.arg.memoryImport.has_value()) {
758     ctx.arg.memoryExport = memoryName;
759   }
760 }
761 
762 // Some command line options or some combinations of them are not allowed.
763 // This function checks for such errors.
764 static void checkOptions(opt::InputArgList &args) {
765   if (!ctx.arg.stripDebug && !ctx.arg.stripAll && ctx.arg.compressRelocations)
766     error("--compress-relocations is incompatible with output debug"
767           " information. Please pass --strip-debug or --strip-all");
768 
769   if (ctx.arg.ltoPartitions == 0)
770     error("--lto-partitions: number of threads must be > 0");
771   if (!get_threadpool_strategy(ctx.arg.thinLTOJobs))
772     error("--thinlto-jobs: invalid job count: " + ctx.arg.thinLTOJobs);
773 
774   if (ctx.arg.pie && ctx.arg.shared)
775     error("-shared and -pie may not be used together");
776 
777   if (ctx.arg.outputFile.empty() && !ctx.arg.thinLTOIndexOnly)
778     error("no output file specified");
779 
780   if (ctx.arg.importTable && ctx.arg.exportTable)
781     error("--import-table and --export-table may not be used together");
782 
783   if (ctx.arg.relocatable) {
784     if (!ctx.arg.entry.empty())
785       error("entry point specified for relocatable output file");
786     if (ctx.arg.gcSections)
787       error("-r and --gc-sections may not be used together");
788     if (ctx.arg.compressRelocations)
789       error("-r -and --compress-relocations may not be used together");
790     if (args.hasArg(OPT_undefined))
791       error("-r -and --undefined may not be used together");
792     if (ctx.arg.pie)
793       error("-r and -pie may not be used together");
794     if (ctx.arg.sharedMemory)
795       error("-r and --shared-memory may not be used together");
796     if (ctx.arg.globalBase)
797       error("-r and --global-base may not by used together");
798   }
799 
800   // To begin to prepare for Module Linking-style shared libraries, start
801   // warning about uses of `-shared` and related flags outside of Experimental
802   // mode, to give anyone using them a heads-up that they will be changing.
803   //
804   // Also, warn about flags which request explicit exports.
805   if (!ctx.arg.experimentalPic) {
806     // -shared will change meaning when Module Linking is implemented.
807     if (ctx.arg.shared) {
808       warn("creating shared libraries, with -shared, is not yet stable");
809     }
810 
811     // -pie will change meaning when Module Linking is implemented.
812     if (ctx.arg.pie) {
813       warn("creating PIEs, with -pie, is not yet stable");
814     }
815 
816     if (ctx.arg.unresolvedSymbols == UnresolvedPolicy::ImportDynamic) {
817       warn("dynamic imports are not yet stable "
818            "(--unresolved-symbols=import-dynamic)");
819     }
820   }
821 
822   if (ctx.arg.bsymbolic && !ctx.arg.shared) {
823     warn("-Bsymbolic is only meaningful when combined with -shared");
824   }
825 
826   if (ctx.isPic) {
827     if (ctx.arg.globalBase)
828       error("--global-base may not be used with -shared/-pie");
829     if (ctx.arg.tableBase)
830       error("--table-base may not be used with -shared/-pie");
831   }
832 }
833 
834 static const char *getReproduceOption(opt::InputArgList &args) {
835   if (auto *arg = args.getLastArg(OPT_reproduce))
836     return arg->getValue();
837   return getenv("LLD_REPRODUCE");
838 }
839 
840 // Force Sym to be entered in the output. Used for -u or equivalent.
841 static Symbol *handleUndefined(StringRef name, const char *option) {
842   Symbol *sym = symtab->find(name);
843   if (!sym)
844     return nullptr;
845 
846   // Since symbol S may not be used inside the program, LTO may
847   // eliminate it. Mark the symbol as "used" to prevent it.
848   sym->isUsedInRegularObj = true;
849 
850   if (auto *lazySym = dyn_cast<LazySymbol>(sym)) {
851     lazySym->extract();
852     if (!ctx.arg.whyExtract.empty())
853       ctx.whyExtractRecords.emplace_back(option, sym->getFile(), *sym);
854   }
855 
856   return sym;
857 }
858 
859 static void handleLibcall(StringRef name) {
860   Symbol *sym = symtab->find(name);
861   if (sym && sym->isLazy() && isa<BitcodeFile>(sym->getFile())) {
862     if (!ctx.arg.whyExtract.empty())
863       ctx.whyExtractRecords.emplace_back("<libcall>", sym->getFile(), *sym);
864     cast<LazySymbol>(sym)->extract();
865   }
866 }
867 
868 static void writeWhyExtract() {
869   if (ctx.arg.whyExtract.empty())
870     return;
871 
872   std::error_code ec;
873   raw_fd_ostream os(ctx.arg.whyExtract, ec, sys::fs::OF_None);
874   if (ec) {
875     error("cannot open --why-extract= file " + ctx.arg.whyExtract + ": " +
876           ec.message());
877     return;
878   }
879 
880   os << "reference\textracted\tsymbol\n";
881   for (auto &entry : ctx.whyExtractRecords) {
882     os << std::get<0>(entry) << '\t' << toString(std::get<1>(entry)) << '\t'
883        << toString(std::get<2>(entry)) << '\n';
884   }
885 }
886 
887 // Equivalent of demote demoteSharedAndLazySymbols() in the ELF linker
888 static void demoteLazySymbols() {
889   for (Symbol *sym : symtab->symbols()) {
890     if (auto* s = dyn_cast<LazySymbol>(sym)) {
891       if (s->signature) {
892         LLVM_DEBUG(llvm::dbgs()
893                    << "demoting lazy func: " << s->getName() << "\n");
894         replaceSymbol<UndefinedFunction>(s, s->getName(), std::nullopt,
895                                          std::nullopt, WASM_SYMBOL_BINDING_WEAK,
896                                          s->getFile(), s->signature);
897       }
898     }
899   }
900 }
901 
902 static UndefinedGlobal *
903 createUndefinedGlobal(StringRef name, llvm::wasm::WasmGlobalType *type) {
904   auto *sym = cast<UndefinedGlobal>(symtab->addUndefinedGlobal(
905       name, std::nullopt, std::nullopt, WASM_SYMBOL_UNDEFINED, nullptr, type));
906   ctx.arg.allowUndefinedSymbols.insert(sym->getName());
907   sym->isUsedInRegularObj = true;
908   return sym;
909 }
910 
911 static InputGlobal *createGlobal(StringRef name, bool isMutable) {
912   llvm::wasm::WasmGlobal wasmGlobal;
913   bool is64 = ctx.arg.is64.value_or(false);
914   wasmGlobal.Type = {uint8_t(is64 ? WASM_TYPE_I64 : WASM_TYPE_I32), isMutable};
915   wasmGlobal.InitExpr = intConst(0, is64);
916   wasmGlobal.SymbolName = name;
917   return make<InputGlobal>(wasmGlobal, nullptr);
918 }
919 
920 static GlobalSymbol *createGlobalVariable(StringRef name, bool isMutable) {
921   InputGlobal *g = createGlobal(name, isMutable);
922   return symtab->addSyntheticGlobal(name, WASM_SYMBOL_VISIBILITY_HIDDEN, g);
923 }
924 
925 static GlobalSymbol *createOptionalGlobal(StringRef name, bool isMutable) {
926   InputGlobal *g = createGlobal(name, isMutable);
927   return symtab->addOptionalGlobalSymbol(name, g);
928 }
929 
930 // Create ABI-defined synthetic symbols
931 static void createSyntheticSymbols() {
932   if (ctx.arg.relocatable)
933     return;
934 
935   static WasmSignature nullSignature = {{}, {}};
936   static WasmSignature i32ArgSignature = {{}, {ValType::I32}};
937   static WasmSignature i64ArgSignature = {{}, {ValType::I64}};
938   static llvm::wasm::WasmGlobalType globalTypeI32 = {WASM_TYPE_I32, false};
939   static llvm::wasm::WasmGlobalType globalTypeI64 = {WASM_TYPE_I64, false};
940   static llvm::wasm::WasmGlobalType mutableGlobalTypeI32 = {WASM_TYPE_I32,
941                                                             true};
942   static llvm::wasm::WasmGlobalType mutableGlobalTypeI64 = {WASM_TYPE_I64,
943                                                             true};
944   WasmSym::callCtors = symtab->addSyntheticFunction(
945       "__wasm_call_ctors", WASM_SYMBOL_VISIBILITY_HIDDEN,
946       make<SyntheticFunction>(nullSignature, "__wasm_call_ctors"));
947 
948   bool is64 = ctx.arg.is64.value_or(false);
949 
950   if (ctx.isPic) {
951     WasmSym::stackPointer =
952         createUndefinedGlobal("__stack_pointer", ctx.arg.is64.value_or(false)
953                                                      ? &mutableGlobalTypeI64
954                                                      : &mutableGlobalTypeI32);
955     // For PIC code, we import two global variables (__memory_base and
956     // __table_base) from the environment and use these as the offset at
957     // which to load our static data and function table.
958     // See:
959     // https://github.com/WebAssembly/tool-conventions/blob/main/DynamicLinking.md
960     auto *globalType = is64 ? &globalTypeI64 : &globalTypeI32;
961     WasmSym::memoryBase = createUndefinedGlobal("__memory_base", globalType);
962     WasmSym::tableBase = createUndefinedGlobal("__table_base", globalType);
963     WasmSym::memoryBase->markLive();
964     WasmSym::tableBase->markLive();
965   } else {
966     // For non-PIC code
967     WasmSym::stackPointer = createGlobalVariable("__stack_pointer", true);
968     WasmSym::stackPointer->markLive();
969   }
970 
971   if (ctx.arg.sharedMemory) {
972     WasmSym::tlsBase = createGlobalVariable("__tls_base", true);
973     WasmSym::tlsSize = createGlobalVariable("__tls_size", false);
974     WasmSym::tlsAlign = createGlobalVariable("__tls_align", false);
975     WasmSym::initTLS = symtab->addSyntheticFunction(
976         "__wasm_init_tls", WASM_SYMBOL_VISIBILITY_HIDDEN,
977         make<SyntheticFunction>(
978             is64 ? i64ArgSignature : i32ArgSignature,
979             "__wasm_init_tls"));
980   }
981 }
982 
983 static void createOptionalSymbols() {
984   if (ctx.arg.relocatable)
985     return;
986 
987   WasmSym::dsoHandle = symtab->addOptionalDataSymbol("__dso_handle");
988 
989   if (!ctx.arg.shared)
990     WasmSym::dataEnd = symtab->addOptionalDataSymbol("__data_end");
991 
992   if (!ctx.isPic) {
993     WasmSym::stackLow = symtab->addOptionalDataSymbol("__stack_low");
994     WasmSym::stackHigh = symtab->addOptionalDataSymbol("__stack_high");
995     WasmSym::globalBase = symtab->addOptionalDataSymbol("__global_base");
996     WasmSym::heapBase = symtab->addOptionalDataSymbol("__heap_base");
997     WasmSym::heapEnd = symtab->addOptionalDataSymbol("__heap_end");
998     WasmSym::definedMemoryBase = symtab->addOptionalDataSymbol("__memory_base");
999     WasmSym::definedTableBase = symtab->addOptionalDataSymbol("__table_base");
1000   }
1001 
1002   // For non-shared memory programs we still need to define __tls_base since we
1003   // allow object files built with TLS to be linked into single threaded
1004   // programs, and such object files can contain references to this symbol.
1005   //
1006   // However, in this case __tls_base is immutable and points directly to the
1007   // start of the `.tdata` static segment.
1008   //
1009   // __tls_size and __tls_align are not needed in this case since they are only
1010   // needed for __wasm_init_tls (which we do not create in this case).
1011   if (!ctx.arg.sharedMemory)
1012     WasmSym::tlsBase = createOptionalGlobal("__tls_base", false);
1013 }
1014 
1015 static void processStubLibrariesPreLTO() {
1016   log("-- processStubLibrariesPreLTO");
1017   for (auto &stub_file : ctx.stubFiles) {
1018     LLVM_DEBUG(llvm::dbgs()
1019                << "processing stub file: " << stub_file->getName() << "\n");
1020     for (auto [name, deps]: stub_file->symbolDependencies) {
1021       auto* sym = symtab->find(name);
1022       // If the symbol is not present at all (yet), or if it is present but
1023       // undefined, then mark the dependent symbols as used by a regular
1024       // object so they will be preserved and exported by the LTO process.
1025       if (!sym || sym->isUndefined()) {
1026         for (const auto dep : deps) {
1027           auto* needed = symtab->find(dep);
1028           if (needed ) {
1029             needed->isUsedInRegularObj = true;
1030             // Like with handleLibcall we have to extract any LTO archive
1031             // members that might need to be exported due to stub library
1032             // symbols being referenced.  Without this the LTO object could be
1033             // extracted during processStubLibraries, which is too late since
1034             // LTO has already being performed at that point.
1035             if (needed->isLazy() && isa<BitcodeFile>(needed->getFile())) {
1036               if (!ctx.arg.whyExtract.empty())
1037                 ctx.whyExtractRecords.emplace_back(toString(stub_file),
1038                                                    needed->getFile(), *needed);
1039               cast<LazySymbol>(needed)->extract();
1040             }
1041           }
1042         }
1043       }
1044     }
1045   }
1046 }
1047 
1048 static bool addStubSymbolDeps(const StubFile *stub_file, Symbol *sym,
1049                               ArrayRef<StringRef> deps) {
1050   // The first stub library to define a given symbol sets this and
1051   // definitions in later stub libraries are ignored.
1052   if (sym->forceImport)
1053     return false; // Already handled
1054   sym->forceImport = true;
1055   if (sym->traced)
1056     message(toString(stub_file) + ": importing " + sym->getName());
1057   else
1058     LLVM_DEBUG(llvm::dbgs() << toString(stub_file) << ": importing "
1059                             << sym->getName() << "\n");
1060   bool depsAdded = false;
1061   for (const auto dep : deps) {
1062     auto *needed = symtab->find(dep);
1063     if (!needed) {
1064       error(toString(stub_file) + ": undefined symbol: " + dep +
1065             ". Required by " + toString(*sym));
1066     } else if (needed->isUndefined()) {
1067       error(toString(stub_file) + ": undefined symbol: " + toString(*needed) +
1068             ". Required by " + toString(*sym));
1069     } else {
1070       if (needed->traced)
1071         message(toString(stub_file) + ": exported " + toString(*needed) +
1072                 " due to import of " + sym->getName());
1073       else
1074         LLVM_DEBUG(llvm::dbgs()
1075                    << "force export: " << toString(*needed) << "\n");
1076       needed->forceExport = true;
1077       if (auto *lazy = dyn_cast<LazySymbol>(needed)) {
1078         depsAdded = true;
1079         lazy->extract();
1080         if (!ctx.arg.whyExtract.empty())
1081           ctx.whyExtractRecords.emplace_back(toString(stub_file),
1082                                              sym->getFile(), *sym);
1083       }
1084     }
1085   }
1086   return depsAdded;
1087 }
1088 
1089 static void processStubLibraries() {
1090   log("-- processStubLibraries");
1091   bool depsAdded = false;
1092   do {
1093     depsAdded = false;
1094     for (auto &stub_file : ctx.stubFiles) {
1095       LLVM_DEBUG(llvm::dbgs()
1096                  << "processing stub file: " << stub_file->getName() << "\n");
1097 
1098       // First look for any imported symbols that directly match
1099       // the names of the stub imports
1100       for (auto [name, deps]: stub_file->symbolDependencies) {
1101         auto* sym = symtab->find(name);
1102         if (sym && sym->isUndefined()) {
1103           depsAdded |= addStubSymbolDeps(stub_file, sym, deps);
1104         } else {
1105           if (sym && sym->traced)
1106             message(toString(stub_file) + ": stub symbol not needed: " + name);
1107           else
1108             LLVM_DEBUG(llvm::dbgs()
1109                        << "stub symbol not needed: `" << name << "`\n");
1110         }
1111       }
1112 
1113       // Secondly looks for any symbols with an `importName` that matches
1114       for (Symbol *sym : symtab->symbols()) {
1115         if (sym->isUndefined() && sym->importName.has_value()) {
1116           auto it = stub_file->symbolDependencies.find(sym->importName.value());
1117           if (it != stub_file->symbolDependencies.end()) {
1118             depsAdded |= addStubSymbolDeps(stub_file, sym, it->second);
1119           }
1120         }
1121       }
1122     }
1123   } while (depsAdded);
1124 
1125   log("-- done processStubLibraries");
1126 }
1127 
1128 // Reconstructs command line arguments so that so that you can re-run
1129 // the same command with the same inputs. This is for --reproduce.
1130 static std::string createResponseFile(const opt::InputArgList &args) {
1131   SmallString<0> data;
1132   raw_svector_ostream os(data);
1133 
1134   // Copy the command line to the output while rewriting paths.
1135   for (auto *arg : args) {
1136     switch (arg->getOption().getID()) {
1137     case OPT_reproduce:
1138       break;
1139     case OPT_INPUT:
1140       os << quote(relativeToRoot(arg->getValue())) << "\n";
1141       break;
1142     case OPT_o:
1143       // If -o path contains directories, "lld @response.txt" will likely
1144       // fail because the archive we are creating doesn't contain empty
1145       // directories for the output path (-o doesn't create directories).
1146       // Strip directories to prevent the issue.
1147       os << "-o " << quote(sys::path::filename(arg->getValue())) << "\n";
1148       break;
1149     default:
1150       os << toString(*arg) << "\n";
1151     }
1152   }
1153   return std::string(data);
1154 }
1155 
1156 // The --wrap option is a feature to rename symbols so that you can write
1157 // wrappers for existing functions. If you pass `-wrap=foo`, all
1158 // occurrences of symbol `foo` are resolved to `wrap_foo` (so, you are
1159 // expected to write `wrap_foo` function as a wrapper). The original
1160 // symbol becomes accessible as `real_foo`, so you can call that from your
1161 // wrapper.
1162 //
1163 // This data structure is instantiated for each -wrap option.
1164 struct WrappedSymbol {
1165   Symbol *sym;
1166   Symbol *real;
1167   Symbol *wrap;
1168 };
1169 
1170 static Symbol *addUndefined(StringRef name) {
1171   return symtab->addUndefinedFunction(name, std::nullopt, std::nullopt,
1172                                       WASM_SYMBOL_UNDEFINED, nullptr, nullptr,
1173                                       false);
1174 }
1175 
1176 // Handles -wrap option.
1177 //
1178 // This function instantiates wrapper symbols. At this point, they seem
1179 // like they are not being used at all, so we explicitly set some flags so
1180 // that LTO won't eliminate them.
1181 static std::vector<WrappedSymbol> addWrappedSymbols(opt::InputArgList &args) {
1182   std::vector<WrappedSymbol> v;
1183   DenseSet<StringRef> seen;
1184 
1185   for (auto *arg : args.filtered(OPT_wrap)) {
1186     StringRef name = arg->getValue();
1187     if (!seen.insert(name).second)
1188       continue;
1189 
1190     Symbol *sym = symtab->find(name);
1191     if (!sym)
1192       continue;
1193 
1194     Symbol *real = addUndefined(saver().save("__real_" + name));
1195     Symbol *wrap = addUndefined(saver().save("__wrap_" + name));
1196     v.push_back({sym, real, wrap});
1197 
1198     // We want to tell LTO not to inline symbols to be overwritten
1199     // because LTO doesn't know the final symbol contents after renaming.
1200     real->canInline = false;
1201     sym->canInline = false;
1202 
1203     // Tell LTO not to eliminate these symbols.
1204     sym->isUsedInRegularObj = true;
1205     wrap->isUsedInRegularObj = true;
1206     real->isUsedInRegularObj = false;
1207   }
1208   return v;
1209 }
1210 
1211 // Do renaming for -wrap by updating pointers to symbols.
1212 //
1213 // When this function is executed, only InputFiles and symbol table
1214 // contain pointers to symbol objects. We visit them to replace pointers,
1215 // so that wrapped symbols are swapped as instructed by the command line.
1216 static void wrapSymbols(ArrayRef<WrappedSymbol> wrapped) {
1217   DenseMap<Symbol *, Symbol *> map;
1218   for (const WrappedSymbol &w : wrapped) {
1219     map[w.sym] = w.wrap;
1220     map[w.real] = w.sym;
1221   }
1222 
1223   // Update pointers in input files.
1224   parallelForEach(ctx.objectFiles, [&](InputFile *file) {
1225     MutableArrayRef<Symbol *> syms = file->getMutableSymbols();
1226     for (size_t i = 0, e = syms.size(); i != e; ++i)
1227       if (Symbol *s = map.lookup(syms[i]))
1228         syms[i] = s;
1229   });
1230 
1231   // Update pointers in the symbol table.
1232   for (const WrappedSymbol &w : wrapped)
1233     symtab->wrap(w.sym, w.real, w.wrap);
1234 }
1235 
1236 static void splitSections() {
1237   // splitIntoPieces needs to be called on each MergeInputChunk
1238   // before calling finalizeContents().
1239   LLVM_DEBUG(llvm::dbgs() << "splitSections\n");
1240   parallelForEach(ctx.objectFiles, [](ObjFile *file) {
1241     for (InputChunk *seg : file->segments) {
1242       if (auto *s = dyn_cast<MergeInputChunk>(seg))
1243         s->splitIntoPieces();
1244     }
1245     for (InputChunk *sec : file->customSections) {
1246       if (auto *s = dyn_cast<MergeInputChunk>(sec))
1247         s->splitIntoPieces();
1248     }
1249   });
1250 }
1251 
1252 static bool isKnownZFlag(StringRef s) {
1253   // For now, we only support a very limited set of -z flags
1254   return s.starts_with("stack-size=") || s.starts_with("muldefs");
1255 }
1256 
1257 // Report a warning for an unknown -z option.
1258 static void checkZOptions(opt::InputArgList &args) {
1259   for (auto *arg : args.filtered(OPT_z))
1260     if (!isKnownZFlag(arg->getValue()))
1261       warn("unknown -z value: " + StringRef(arg->getValue()));
1262 }
1263 
1264 LinkerDriver::LinkerDriver(Ctx &ctx) : ctx(ctx) {}
1265 
1266 void LinkerDriver::linkerMain(ArrayRef<const char *> argsArr) {
1267   WasmOptTable parser;
1268   opt::InputArgList args = parser.parse(argsArr.slice(1));
1269 
1270   // Interpret these flags early because error()/warn() depend on them.
1271   auto &errHandler = errorHandler();
1272   errHandler.errorLimit = args::getInteger(args, OPT_error_limit, 20);
1273   errHandler.fatalWarnings =
1274       args.hasFlag(OPT_fatal_warnings, OPT_no_fatal_warnings, false);
1275   checkZOptions(args);
1276 
1277   // Handle --help
1278   if (args.hasArg(OPT_help)) {
1279     parser.printHelp(errHandler.outs(),
1280                      (std::string(argsArr[0]) + " [options] file...").c_str(),
1281                      "LLVM Linker", false);
1282     return;
1283   }
1284 
1285   // Handle -v or -version.
1286   if (args.hasArg(OPT_v) || args.hasArg(OPT_version))
1287     errHandler.outs() << getLLDVersion() << "\n";
1288 
1289   // Handle --reproduce
1290   if (const char *path = getReproduceOption(args)) {
1291     Expected<std::unique_ptr<TarWriter>> errOrWriter =
1292         TarWriter::create(path, path::stem(path));
1293     if (errOrWriter) {
1294       tar = std::move(*errOrWriter);
1295       tar->append("response.txt", createResponseFile(args));
1296       tar->append("version.txt", getLLDVersion() + "\n");
1297     } else {
1298       error("--reproduce: " + toString(errOrWriter.takeError()));
1299     }
1300   }
1301 
1302   // Parse and evaluate -mllvm options.
1303   std::vector<const char *> v;
1304   v.push_back("wasm-ld (LLVM option parsing)");
1305   for (auto *arg : args.filtered(OPT_mllvm))
1306     v.push_back(arg->getValue());
1307   cl::ResetAllOptionOccurrences();
1308   cl::ParseCommandLineOptions(v.size(), v.data());
1309 
1310   readConfigs(args);
1311   setConfigs();
1312 
1313   // The behavior of -v or --version is a bit strange, but this is
1314   // needed for compatibility with GNU linkers.
1315   if (args.hasArg(OPT_v) && !args.hasArg(OPT_INPUT))
1316     return;
1317   if (args.hasArg(OPT_version))
1318     return;
1319 
1320   createFiles(args);
1321   if (errorCount())
1322     return;
1323 
1324   checkOptions(args);
1325   if (errorCount())
1326     return;
1327 
1328   if (auto *arg = args.getLastArg(OPT_allow_undefined_file))
1329     readImportFile(arg->getValue());
1330 
1331   // Fail early if the output file or map file is not writable. If a user has a
1332   // long link, e.g. due to a large LTO link, they do not wish to run it and
1333   // find that it failed because there was a mistake in their command-line.
1334   if (auto e = tryCreateFile(ctx.arg.outputFile))
1335     error("cannot open output file " + ctx.arg.outputFile + ": " + e.message());
1336   if (auto e = tryCreateFile(ctx.arg.mapFile))
1337     error("cannot open map file " + ctx.arg.mapFile + ": " + e.message());
1338   if (errorCount())
1339     return;
1340 
1341   // Handle --trace-symbol.
1342   for (auto *arg : args.filtered(OPT_trace_symbol))
1343     symtab->trace(arg->getValue());
1344 
1345   for (auto *arg : args.filtered(OPT_export_if_defined))
1346     ctx.arg.exportedSymbols.insert(arg->getValue());
1347 
1348   for (auto *arg : args.filtered(OPT_export)) {
1349     ctx.arg.exportedSymbols.insert(arg->getValue());
1350     ctx.arg.requiredExports.push_back(arg->getValue());
1351   }
1352 
1353   createSyntheticSymbols();
1354 
1355   // Add all files to the symbol table. This will add almost all
1356   // symbols that we need to the symbol table.
1357   for (InputFile *f : files)
1358     symtab->addFile(f);
1359   if (errorCount())
1360     return;
1361 
1362   // Handle the `--undefined <sym>` options.
1363   for (auto *arg : args.filtered(OPT_undefined))
1364     handleUndefined(arg->getValue(), "<internal>");
1365 
1366   // Handle the `--export <sym>` options
1367   // This works like --undefined but also exports the symbol if its found
1368   for (auto &iter : ctx.arg.exportedSymbols)
1369     handleUndefined(iter.first(), "--export");
1370 
1371   Symbol *entrySym = nullptr;
1372   if (!ctx.arg.relocatable && !ctx.arg.entry.empty()) {
1373     entrySym = handleUndefined(ctx.arg.entry, "--entry");
1374     if (entrySym && entrySym->isDefined())
1375       entrySym->forceExport = true;
1376     else
1377       error("entry symbol not defined (pass --no-entry to suppress): " +
1378             ctx.arg.entry);
1379   }
1380 
1381   // If the user code defines a `__wasm_call_dtors` function, remember it so
1382   // that we can call it from the command export wrappers. Unlike
1383   // `__wasm_call_ctors` which we synthesize, `__wasm_call_dtors` is defined
1384   // by libc/etc., because destructors are registered dynamically with
1385   // `__cxa_atexit` and friends.
1386   if (!ctx.arg.relocatable && !ctx.arg.shared &&
1387       !WasmSym::callCtors->isUsedInRegularObj &&
1388       WasmSym::callCtors->getName() != ctx.arg.entry &&
1389       !ctx.arg.exportedSymbols.count(WasmSym::callCtors->getName())) {
1390     if (Symbol *callDtors =
1391             handleUndefined("__wasm_call_dtors", "<internal>")) {
1392       if (auto *callDtorsFunc = dyn_cast<DefinedFunction>(callDtors)) {
1393         if (callDtorsFunc->signature &&
1394             (!callDtorsFunc->signature->Params.empty() ||
1395              !callDtorsFunc->signature->Returns.empty())) {
1396           error("__wasm_call_dtors must have no argument or return values");
1397         }
1398         WasmSym::callDtors = callDtorsFunc;
1399       } else {
1400         error("__wasm_call_dtors must be a function");
1401       }
1402     }
1403   }
1404 
1405   if (errorCount())
1406     return;
1407 
1408   // Create wrapped symbols for -wrap option.
1409   std::vector<WrappedSymbol> wrapped = addWrappedSymbols(args);
1410 
1411   // If any of our inputs are bitcode files, the LTO code generator may create
1412   // references to certain library functions that might not be explicit in the
1413   // bitcode file's symbol table. If any of those library functions are defined
1414   // in a bitcode file in an archive member, we need to arrange to use LTO to
1415   // compile those archive members by adding them to the link beforehand.
1416   //
1417   // We only need to add libcall symbols to the link before LTO if the symbol's
1418   // definition is in bitcode. Any other required libcall symbols will be added
1419   // to the link after LTO when we add the LTO object file to the link.
1420   if (!ctx.bitcodeFiles.empty()) {
1421     llvm::Triple TT(ctx.bitcodeFiles.front()->obj->getTargetTriple());
1422     for (auto *s : lto::LTO::getRuntimeLibcallSymbols(TT))
1423       handleLibcall(s);
1424   }
1425   if (errorCount())
1426     return;
1427 
1428   // We process the stub libraries once beofore LTO to ensure that any possible
1429   // required exports are preserved by the LTO process.
1430   processStubLibrariesPreLTO();
1431 
1432   // Do link-time optimization if given files are LLVM bitcode files.
1433   // This compiles bitcode files into real object files.
1434   symtab->compileBitcodeFiles();
1435   if (errorCount())
1436     return;
1437 
1438   // The LTO process can generate new undefined symbols, specifically libcall
1439   // functions.  Because those symbols might be declared in a stub library we
1440   // need the process the stub libraries once again after LTO to handle all
1441   // undefined symbols, including ones that didn't exist prior to LTO.
1442   processStubLibraries();
1443 
1444   writeWhyExtract();
1445 
1446   // Bail out if normal linked output is skipped due to LTO.
1447   if (ctx.arg.thinLTOIndexOnly)
1448     return;
1449 
1450   createOptionalSymbols();
1451 
1452   // Resolve any variant symbols that were created due to signature
1453   // mismatchs.
1454   symtab->handleSymbolVariants();
1455   if (errorCount())
1456     return;
1457 
1458   // Apply symbol renames for -wrap.
1459   if (!wrapped.empty())
1460     wrapSymbols(wrapped);
1461 
1462   for (auto &iter : ctx.arg.exportedSymbols) {
1463     Symbol *sym = symtab->find(iter.first());
1464     if (sym && sym->isDefined())
1465       sym->forceExport = true;
1466   }
1467 
1468   if (!ctx.arg.relocatable && !ctx.isPic) {
1469     // Add synthetic dummies for weak undefined functions.  Must happen
1470     // after LTO otherwise functions may not yet have signatures.
1471     symtab->handleWeakUndefines();
1472   }
1473 
1474   if (entrySym)
1475     entrySym->setHidden(false);
1476 
1477   if (errorCount())
1478     return;
1479 
1480   // Split WASM_SEG_FLAG_STRINGS sections into pieces in preparation for garbage
1481   // collection.
1482   splitSections();
1483 
1484   // Any remaining lazy symbols should be demoted to Undefined
1485   demoteLazySymbols();
1486 
1487   // Do size optimizations: garbage collection
1488   markLive();
1489 
1490   // Provide the indirect function table if needed.
1491   WasmSym::indirectFunctionTable =
1492       symtab->resolveIndirectFunctionTable(/*required =*/false);
1493 
1494   if (errorCount())
1495     return;
1496 
1497   // Write the result to the file.
1498   writeResult();
1499 }
1500 
1501 } // namespace lld::wasm
1502