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