[docs] Add LICENSE.txt to the root of the mono-repo
[llvm-project.git] / lld / wasm / Driver.cpp
blob99725095c8f4172229f33dbb607e694297afb06c
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/ErrorHandler.h"
18 #include "lld/Common/Filesystem.h"
19 #include "lld/Common/Memory.h"
20 #include "lld/Common/Reproduce.h"
21 #include "lld/Common/Strings.h"
22 #include "lld/Common/Version.h"
23 #include "llvm/ADT/Twine.h"
24 #include "llvm/Config/llvm-config.h"
25 #include "llvm/Object/Wasm.h"
26 #include "llvm/Option/Arg.h"
27 #include "llvm/Option/ArgList.h"
28 #include "llvm/Support/CommandLine.h"
29 #include "llvm/Support/Host.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"
36 #define DEBUG_TYPE "lld"
38 using namespace llvm;
39 using namespace llvm::object;
40 using namespace llvm::sys;
41 using namespace llvm::wasm;
43 namespace lld {
44 namespace wasm {
45 Configuration *config;
47 namespace {
49 // Create enum with OPT_xxx values for each option in Options.td
50 enum {
51 OPT_INVALID = 0,
52 #define OPTION(_1, _2, ID, _4, _5, _6, _7, _8, _9, _10, _11, _12) OPT_##ID,
53 #include "Options.inc"
54 #undef OPTION
57 // This function is called on startup. We need this for LTO since
58 // LTO calls LLVM functions to compile bitcode files to native code.
59 // Technically this can be delayed until we read bitcode files, but
60 // we don't bother to do lazily because the initialization is fast.
61 static void initLLVM() {
62 InitializeAllTargets();
63 InitializeAllTargetMCs();
64 InitializeAllAsmPrinters();
65 InitializeAllAsmParsers();
68 class LinkerDriver {
69 public:
70 void linkerMain(ArrayRef<const char *> argsArr);
72 private:
73 void createFiles(opt::InputArgList &args);
74 void addFile(StringRef path);
75 void addLibrary(StringRef name);
77 // True if we are in --whole-archive and --no-whole-archive.
78 bool inWholeArchive = false;
80 std::vector<InputFile *> files;
82 } // anonymous namespace
84 bool link(ArrayRef<const char *> args, llvm::raw_ostream &stdoutOS,
85 llvm::raw_ostream &stderrOS, bool exitEarly, bool disableOutput) {
86 // This driver-specific context will be freed later by lldMain().
87 auto *ctx = new CommonLinkerContext;
89 ctx->e.initialize(stdoutOS, stderrOS, exitEarly, disableOutput);
90 ctx->e.logName = args::getFilenameWithoutExe(args[0]);
91 ctx->e.errorLimitExceededMsg = "too many errors emitted, stopping now (use "
92 "-error-limit=0 to see all errors)";
94 config = make<Configuration>();
95 symtab = make<SymbolTable>();
97 initLLVM();
98 LinkerDriver().linkerMain(args);
100 return errorCount() == 0;
103 // Create prefix string literals used in Options.td
104 #define PREFIX(NAME, VALUE) const char *const NAME[] = VALUE;
105 #include "Options.inc"
106 #undef PREFIX
108 // Create table mapping all options defined in Options.td
109 static const opt::OptTable::Info optInfo[] = {
110 #define OPTION(X1, X2, ID, KIND, GROUP, ALIAS, X7, X8, X9, X10, X11, X12) \
111 {X1, X2, X10, X11, OPT_##ID, opt::Option::KIND##Class, \
112 X9, X8, OPT_##GROUP, OPT_##ALIAS, X7, X12},
113 #include "Options.inc"
114 #undef OPTION
117 namespace {
118 class WasmOptTable : public llvm::opt::OptTable {
119 public:
120 WasmOptTable() : OptTable(optInfo) {}
121 opt::InputArgList parse(ArrayRef<const char *> argv);
123 } // namespace
125 // Set color diagnostics according to -color-diagnostics={auto,always,never}
126 // or -no-color-diagnostics flags.
127 static void handleColorDiagnostics(opt::InputArgList &args) {
128 auto *arg = args.getLastArg(OPT_color_diagnostics, OPT_color_diagnostics_eq,
129 OPT_no_color_diagnostics);
130 if (!arg)
131 return;
132 if (arg->getOption().getID() == OPT_color_diagnostics) {
133 lld::errs().enable_colors(true);
134 } else if (arg->getOption().getID() == OPT_no_color_diagnostics) {
135 lld::errs().enable_colors(false);
136 } else {
137 StringRef s = arg->getValue();
138 if (s == "always")
139 lld::errs().enable_colors(true);
140 else if (s == "never")
141 lld::errs().enable_colors(false);
142 else if (s != "auto")
143 error("unknown option: --color-diagnostics=" + s);
147 static cl::TokenizerCallback getQuotingStyle(opt::InputArgList &args) {
148 if (auto *arg = args.getLastArg(OPT_rsp_quoting)) {
149 StringRef s = arg->getValue();
150 if (s != "windows" && s != "posix")
151 error("invalid response file quoting: " + s);
152 if (s == "windows")
153 return cl::TokenizeWindowsCommandLine;
154 return cl::TokenizeGNUCommandLine;
156 if (Triple(sys::getProcessTriple()).isOSWindows())
157 return cl::TokenizeWindowsCommandLine;
158 return cl::TokenizeGNUCommandLine;
161 // Find a file by concatenating given paths.
162 static Optional<std::string> findFile(StringRef path1, const Twine &path2) {
163 SmallString<128> s;
164 path::append(s, path1, path2);
165 if (fs::exists(s))
166 return std::string(s);
167 return None;
170 opt::InputArgList WasmOptTable::parse(ArrayRef<const char *> argv) {
171 SmallVector<const char *, 256> vec(argv.data(), argv.data() + argv.size());
173 unsigned missingIndex;
174 unsigned missingCount;
176 // We need to get the quoting style for response files before parsing all
177 // options so we parse here before and ignore all the options but
178 // --rsp-quoting.
179 opt::InputArgList args = this->ParseArgs(vec, missingIndex, missingCount);
181 // Expand response files (arguments in the form of @<filename>)
182 // and then parse the argument again.
183 cl::ExpandResponseFiles(saver(), getQuotingStyle(args), vec);
184 args = this->ParseArgs(vec, missingIndex, missingCount);
186 handleColorDiagnostics(args);
187 if (missingCount)
188 error(Twine(args.getArgString(missingIndex)) + ": missing argument");
190 for (auto *arg : args.filtered(OPT_UNKNOWN))
191 error("unknown argument: " + arg->getAsString(args));
192 return args;
195 // Currently we allow a ".imports" to live alongside a library. This can
196 // be used to specify a list of symbols which can be undefined at link
197 // time (imported from the environment. For example libc.a include an
198 // import file that lists the syscall functions it relies on at runtime.
199 // In the long run this information would be better stored as a symbol
200 // attribute/flag in the object file itself.
201 // See: https://github.com/WebAssembly/tool-conventions/issues/35
202 static void readImportFile(StringRef filename) {
203 if (Optional<MemoryBufferRef> buf = readFile(filename))
204 for (StringRef sym : args::getLines(*buf))
205 config->allowUndefinedSymbols.insert(sym);
208 // Returns slices of MB by parsing MB as an archive file.
209 // Each slice consists of a member file in the archive.
210 std::vector<MemoryBufferRef> static getArchiveMembers(MemoryBufferRef mb) {
211 std::unique_ptr<Archive> file =
212 CHECK(Archive::create(mb),
213 mb.getBufferIdentifier() + ": failed to parse archive");
215 std::vector<MemoryBufferRef> v;
216 Error err = Error::success();
217 for (const Archive::Child &c : file->children(err)) {
218 MemoryBufferRef mbref =
219 CHECK(c.getMemoryBufferRef(),
220 mb.getBufferIdentifier() +
221 ": could not get the buffer for a child of the archive");
222 v.push_back(mbref);
224 if (err)
225 fatal(mb.getBufferIdentifier() +
226 ": Archive::children failed: " + toString(std::move(err)));
228 // Take ownership of memory buffers created for members of thin archives.
229 for (std::unique_ptr<MemoryBuffer> &mb : file->takeThinBuffers())
230 make<std::unique_ptr<MemoryBuffer>>(std::move(mb));
232 return v;
235 void LinkerDriver::addFile(StringRef path) {
236 Optional<MemoryBufferRef> buffer = readFile(path);
237 if (!buffer)
238 return;
239 MemoryBufferRef mbref = *buffer;
241 switch (identify_magic(mbref.getBuffer())) {
242 case file_magic::archive: {
243 SmallString<128> importFile = path;
244 path::replace_extension(importFile, ".imports");
245 if (fs::exists(importFile))
246 readImportFile(importFile.str());
248 // Handle -whole-archive.
249 if (inWholeArchive) {
250 for (MemoryBufferRef &m : getArchiveMembers(mbref)) {
251 auto *object = createObjectFile(m, path);
252 // Mark object as live; object members are normally not
253 // live by default but -whole-archive is designed to treat
254 // them as such.
255 object->markLive();
256 files.push_back(object);
259 return;
262 std::unique_ptr<Archive> file =
263 CHECK(Archive::create(mbref), path + ": failed to parse archive");
265 if (!file->isEmpty() && !file->hasSymbolTable()) {
266 error(mbref.getBufferIdentifier() +
267 ": archive has no index; run ranlib to add one");
270 files.push_back(make<ArchiveFile>(mbref));
271 return;
273 case file_magic::bitcode:
274 case file_magic::wasm_object:
275 files.push_back(createObjectFile(mbref));
276 break;
277 default:
278 error("unknown file type: " + mbref.getBufferIdentifier());
282 // Add a given library by searching it from input search paths.
283 void LinkerDriver::addLibrary(StringRef name) {
284 for (StringRef dir : config->searchPaths) {
285 if (Optional<std::string> s = findFile(dir, "lib" + name + ".a")) {
286 addFile(*s);
287 return;
291 error("unable to find library -l" + name);
294 void LinkerDriver::createFiles(opt::InputArgList &args) {
295 for (auto *arg : args) {
296 switch (arg->getOption().getID()) {
297 case OPT_l:
298 addLibrary(arg->getValue());
299 break;
300 case OPT_INPUT:
301 addFile(arg->getValue());
302 break;
303 case OPT_whole_archive:
304 inWholeArchive = true;
305 break;
306 case OPT_no_whole_archive:
307 inWholeArchive = false;
308 break;
311 if (files.empty() && errorCount() == 0)
312 error("no input files");
315 static StringRef getEntry(opt::InputArgList &args) {
316 auto *arg = args.getLastArg(OPT_entry, OPT_no_entry);
317 if (!arg) {
318 if (args.hasArg(OPT_relocatable))
319 return "";
320 if (args.hasArg(OPT_shared))
321 return "__wasm_call_ctors";
322 return "_start";
324 if (arg->getOption().getID() == OPT_no_entry)
325 return "";
326 return arg->getValue();
329 // Determines what we should do if there are remaining unresolved
330 // symbols after the name resolution.
331 static UnresolvedPolicy getUnresolvedSymbolPolicy(opt::InputArgList &args) {
332 UnresolvedPolicy errorOrWarn = args.hasFlag(OPT_error_unresolved_symbols,
333 OPT_warn_unresolved_symbols, true)
334 ? UnresolvedPolicy::ReportError
335 : UnresolvedPolicy::Warn;
337 if (auto *arg = args.getLastArg(OPT_unresolved_symbols)) {
338 StringRef s = arg->getValue();
339 if (s == "ignore-all")
340 return UnresolvedPolicy::Ignore;
341 if (s == "import-dynamic")
342 return UnresolvedPolicy::ImportDynamic;
343 if (s == "report-all")
344 return errorOrWarn;
345 error("unknown --unresolved-symbols value: " + s);
348 return errorOrWarn;
351 // Initializes Config members by the command line options.
352 static void readConfigs(opt::InputArgList &args) {
353 config->bsymbolic = args.hasArg(OPT_Bsymbolic);
354 config->checkFeatures =
355 args.hasFlag(OPT_check_features, OPT_no_check_features, true);
356 config->compressRelocations = args.hasArg(OPT_compress_relocations);
357 config->demangle = args.hasFlag(OPT_demangle, OPT_no_demangle, true);
358 config->disableVerify = args.hasArg(OPT_disable_verify);
359 config->emitRelocs = args.hasArg(OPT_emit_relocs);
360 config->experimentalPic = args.hasArg(OPT_experimental_pic);
361 config->entry = getEntry(args);
362 config->exportAll = args.hasArg(OPT_export_all);
363 config->exportTable = args.hasArg(OPT_export_table);
364 config->growableTable = args.hasArg(OPT_growable_table);
365 config->importMemory = args.hasArg(OPT_import_memory);
366 config->sharedMemory = args.hasArg(OPT_shared_memory);
367 config->importTable = args.hasArg(OPT_import_table);
368 config->importUndefined = args.hasArg(OPT_import_undefined);
369 config->ltoo = args::getInteger(args, OPT_lto_O, 2);
370 config->ltoPartitions = args::getInteger(args, OPT_lto_partitions, 1);
371 config->ltoDebugPassManager = args.hasArg(OPT_lto_debug_pass_manager);
372 config->mapFile = args.getLastArgValue(OPT_Map);
373 config->optimize = args::getInteger(args, OPT_O, 1);
374 config->outputFile = args.getLastArgValue(OPT_o);
375 config->relocatable = args.hasArg(OPT_relocatable);
376 config->gcSections =
377 args.hasFlag(OPT_gc_sections, OPT_no_gc_sections, !config->relocatable);
378 config->mergeDataSegments =
379 args.hasFlag(OPT_merge_data_segments, OPT_no_merge_data_segments,
380 !config->relocatable);
381 config->pie = args.hasFlag(OPT_pie, OPT_no_pie, false);
382 config->printGcSections =
383 args.hasFlag(OPT_print_gc_sections, OPT_no_print_gc_sections, false);
384 config->saveTemps = args.hasArg(OPT_save_temps);
385 config->searchPaths = args::getStrings(args, OPT_L);
386 config->shared = args.hasArg(OPT_shared);
387 config->stripAll = args.hasArg(OPT_strip_all);
388 config->stripDebug = args.hasArg(OPT_strip_debug);
389 config->stackFirst = args.hasArg(OPT_stack_first);
390 config->trace = args.hasArg(OPT_trace);
391 config->thinLTOCacheDir = args.getLastArgValue(OPT_thinlto_cache_dir);
392 config->thinLTOCachePolicy = CHECK(
393 parseCachePruningPolicy(args.getLastArgValue(OPT_thinlto_cache_policy)),
394 "--thinlto-cache-policy: invalid cache policy");
395 config->unresolvedSymbols = getUnresolvedSymbolPolicy(args);
396 errorHandler().verbose = args.hasArg(OPT_verbose);
397 LLVM_DEBUG(errorHandler().verbose = true);
399 config->initialMemory = args::getInteger(args, OPT_initial_memory, 0);
400 config->globalBase = args::getInteger(args, OPT_global_base, 1024);
401 config->maxMemory = args::getInteger(args, OPT_max_memory, 0);
402 config->zStackSize =
403 args::getZOptionValue(args, OPT_z, "stack-size", WasmPageSize);
405 // Default value of exportDynamic depends on `-shared`
406 config->exportDynamic =
407 args.hasFlag(OPT_export_dynamic, OPT_no_export_dynamic, config->shared);
409 // Parse wasm32/64.
410 if (auto *arg = args.getLastArg(OPT_m)) {
411 StringRef s = arg->getValue();
412 if (s == "wasm32")
413 config->is64 = false;
414 else if (s == "wasm64")
415 config->is64 = true;
416 else
417 error("invalid target architecture: " + s);
420 // --threads= takes a positive integer and provides the default value for
421 // --thinlto-jobs=.
422 if (auto *arg = args.getLastArg(OPT_threads)) {
423 StringRef v(arg->getValue());
424 unsigned threads = 0;
425 if (!llvm::to_integer(v, threads, 0) || threads == 0)
426 error(arg->getSpelling() + ": expected a positive integer, but got '" +
427 arg->getValue() + "'");
428 parallel::strategy = hardware_concurrency(threads);
429 config->thinLTOJobs = v;
431 if (auto *arg = args.getLastArg(OPT_thinlto_jobs))
432 config->thinLTOJobs = arg->getValue();
434 if (auto *arg = args.getLastArg(OPT_features)) {
435 config->features =
436 llvm::Optional<std::vector<std::string>>(std::vector<std::string>());
437 for (StringRef s : arg->getValues())
438 config->features->push_back(std::string(s));
441 // Legacy --allow-undefined flag which is equivalent to
442 // --unresolve-symbols=ignore + --import-undefined
443 if (args.hasArg(OPT_allow_undefined)) {
444 config->importUndefined = true;
445 config->unresolvedSymbols = UnresolvedPolicy::Ignore;
448 if (args.hasArg(OPT_print_map))
449 config->mapFile = "-";
452 // Some Config members do not directly correspond to any particular
453 // command line options, but computed based on other Config values.
454 // This function initialize such members. See Config.h for the details
455 // of these values.
456 static void setConfigs() {
457 config->isPic = config->pie || config->shared;
459 if (config->isPic) {
460 if (config->exportTable)
461 error("-shared/-pie is incompatible with --export-table");
462 config->importTable = true;
465 if (config->relocatable) {
466 if (config->exportTable)
467 error("--relocatable is incompatible with --export-table");
468 if (config->growableTable)
469 error("--relocatable is incompatible with --growable-table");
470 // Ignore any --import-table, as it's redundant.
471 config->importTable = true;
474 if (config->shared) {
475 config->importMemory = true;
476 config->importUndefined = true;
480 // Some command line options or some combinations of them are not allowed.
481 // This function checks for such errors.
482 static void checkOptions(opt::InputArgList &args) {
483 if (!config->stripDebug && !config->stripAll && config->compressRelocations)
484 error("--compress-relocations is incompatible with output debug"
485 " information. Please pass --strip-debug or --strip-all");
487 if (config->ltoo > 3)
488 error("invalid optimization level for LTO: " + Twine(config->ltoo));
489 if (config->ltoPartitions == 0)
490 error("--lto-partitions: number of threads must be > 0");
491 if (!get_threadpool_strategy(config->thinLTOJobs))
492 error("--thinlto-jobs: invalid job count: " + config->thinLTOJobs);
494 if (config->pie && config->shared)
495 error("-shared and -pie may not be used together");
497 if (config->outputFile.empty())
498 error("no output file specified");
500 if (config->importTable && config->exportTable)
501 error("--import-table and --export-table may not be used together");
503 if (config->relocatable) {
504 if (!config->entry.empty())
505 error("entry point specified for relocatable output file");
506 if (config->gcSections)
507 error("-r and --gc-sections may not be used together");
508 if (config->compressRelocations)
509 error("-r -and --compress-relocations may not be used together");
510 if (args.hasArg(OPT_undefined))
511 error("-r -and --undefined may not be used together");
512 if (config->pie)
513 error("-r and -pie may not be used together");
514 if (config->sharedMemory)
515 error("-r and --shared-memory may not be used together");
518 // To begin to prepare for Module Linking-style shared libraries, start
519 // warning about uses of `-shared` and related flags outside of Experimental
520 // mode, to give anyone using them a heads-up that they will be changing.
522 // Also, warn about flags which request explicit exports.
523 if (!config->experimentalPic) {
524 // -shared will change meaning when Module Linking is implemented.
525 if (config->shared) {
526 warn("creating shared libraries, with -shared, is not yet stable");
529 // -pie will change meaning when Module Linking is implemented.
530 if (config->pie) {
531 warn("creating PIEs, with -pie, is not yet stable");
534 if (config->unresolvedSymbols == UnresolvedPolicy::ImportDynamic) {
535 warn("dynamic imports are not yet stable "
536 "(--unresolved-symbols=import-dynamic)");
540 if (config->bsymbolic && !config->shared) {
541 warn("-Bsymbolic is only meaningful when combined with -shared");
545 // Force Sym to be entered in the output. Used for -u or equivalent.
546 static Symbol *handleUndefined(StringRef name) {
547 Symbol *sym = symtab->find(name);
548 if (!sym)
549 return nullptr;
551 // Since symbol S may not be used inside the program, LTO may
552 // eliminate it. Mark the symbol as "used" to prevent it.
553 sym->isUsedInRegularObj = true;
555 if (auto *lazySym = dyn_cast<LazySymbol>(sym))
556 lazySym->fetch();
558 return sym;
561 static void handleLibcall(StringRef name) {
562 Symbol *sym = symtab->find(name);
563 if (!sym)
564 return;
566 if (auto *lazySym = dyn_cast<LazySymbol>(sym)) {
567 MemoryBufferRef mb = lazySym->getMemberBuffer();
568 if (isBitcode(mb))
569 lazySym->fetch();
573 // Equivalent of demote demoteSharedAndLazySymbols() in the ELF linker
574 static void demoteLazySymbols() {
575 for (Symbol *sym : symtab->getSymbols()) {
576 if (auto* s = dyn_cast<LazySymbol>(sym)) {
577 if (s->signature) {
578 LLVM_DEBUG(llvm::dbgs()
579 << "demoting lazy func: " << s->getName() << "\n");
580 replaceSymbol<UndefinedFunction>(s, s->getName(), None, None,
581 WASM_SYMBOL_BINDING_WEAK, s->getFile(),
582 s->signature);
588 static UndefinedGlobal *
589 createUndefinedGlobal(StringRef name, llvm::wasm::WasmGlobalType *type) {
590 auto *sym = cast<UndefinedGlobal>(symtab->addUndefinedGlobal(
591 name, None, None, WASM_SYMBOL_UNDEFINED, nullptr, type));
592 config->allowUndefinedSymbols.insert(sym->getName());
593 sym->isUsedInRegularObj = true;
594 return sym;
597 static InputGlobal *createGlobal(StringRef name, bool isMutable) {
598 llvm::wasm::WasmGlobal wasmGlobal;
599 bool is64 = config->is64.value_or(false);
600 wasmGlobal.Type = {uint8_t(is64 ? WASM_TYPE_I64 : WASM_TYPE_I32), isMutable};
601 wasmGlobal.InitExpr = intConst(0, is64);
602 wasmGlobal.SymbolName = name;
603 return make<InputGlobal>(wasmGlobal, nullptr);
606 static GlobalSymbol *createGlobalVariable(StringRef name, bool isMutable) {
607 InputGlobal *g = createGlobal(name, isMutable);
608 return symtab->addSyntheticGlobal(name, WASM_SYMBOL_VISIBILITY_HIDDEN, g);
611 static GlobalSymbol *createOptionalGlobal(StringRef name, bool isMutable) {
612 InputGlobal *g = createGlobal(name, isMutable);
613 return symtab->addOptionalGlobalSymbol(name, g);
616 // Create ABI-defined synthetic symbols
617 static void createSyntheticSymbols() {
618 if (config->relocatable)
619 return;
621 static WasmSignature nullSignature = {{}, {}};
622 static WasmSignature i32ArgSignature = {{}, {ValType::I32}};
623 static WasmSignature i64ArgSignature = {{}, {ValType::I64}};
624 static llvm::wasm::WasmGlobalType globalTypeI32 = {WASM_TYPE_I32, false};
625 static llvm::wasm::WasmGlobalType globalTypeI64 = {WASM_TYPE_I64, false};
626 static llvm::wasm::WasmGlobalType mutableGlobalTypeI32 = {WASM_TYPE_I32,
627 true};
628 static llvm::wasm::WasmGlobalType mutableGlobalTypeI64 = {WASM_TYPE_I64,
629 true};
630 WasmSym::callCtors = symtab->addSyntheticFunction(
631 "__wasm_call_ctors", WASM_SYMBOL_VISIBILITY_HIDDEN,
632 make<SyntheticFunction>(nullSignature, "__wasm_call_ctors"));
634 bool is64 = config->is64.value_or(false);
636 if (config->isPic) {
637 WasmSym::stackPointer =
638 createUndefinedGlobal("__stack_pointer", config->is64.value_or(false)
639 ? &mutableGlobalTypeI64
640 : &mutableGlobalTypeI32);
641 // For PIC code, we import two global variables (__memory_base and
642 // __table_base) from the environment and use these as the offset at
643 // which to load our static data and function table.
644 // See:
645 // https://github.com/WebAssembly/tool-conventions/blob/main/DynamicLinking.md
646 auto *globalType = is64 ? &globalTypeI64 : &globalTypeI32;
647 WasmSym::memoryBase = createUndefinedGlobal("__memory_base", globalType);
648 WasmSym::tableBase = createUndefinedGlobal("__table_base", globalType);
649 WasmSym::memoryBase->markLive();
650 WasmSym::tableBase->markLive();
651 if (is64) {
652 WasmSym::tableBase32 =
653 createUndefinedGlobal("__table_base32", &globalTypeI32);
654 WasmSym::tableBase32->markLive();
655 } else {
656 WasmSym::tableBase32 = nullptr;
658 } else {
659 // For non-PIC code
660 WasmSym::stackPointer = createGlobalVariable("__stack_pointer", true);
661 WasmSym::stackPointer->markLive();
664 if (config->sharedMemory) {
665 WasmSym::tlsBase = createGlobalVariable("__tls_base", true);
666 WasmSym::tlsSize = createGlobalVariable("__tls_size", false);
667 WasmSym::tlsAlign = createGlobalVariable("__tls_align", false);
668 WasmSym::initTLS = symtab->addSyntheticFunction(
669 "__wasm_init_tls", WASM_SYMBOL_VISIBILITY_HIDDEN,
670 make<SyntheticFunction>(
671 is64 ? i64ArgSignature : i32ArgSignature,
672 "__wasm_init_tls"));
675 if (config->isPic ||
676 config->unresolvedSymbols == UnresolvedPolicy::ImportDynamic) {
677 // For PIC code, or when dynamically importing addresses, we create
678 // synthetic functions that apply relocations. These get called from
679 // __wasm_call_ctors before the user-level constructors.
680 WasmSym::applyDataRelocs = symtab->addSyntheticFunction(
681 "__wasm_apply_data_relocs",
682 WASM_SYMBOL_VISIBILITY_DEFAULT | WASM_SYMBOL_EXPORTED,
683 make<SyntheticFunction>(nullSignature, "__wasm_apply_data_relocs"));
687 static void createOptionalSymbols() {
688 if (config->relocatable)
689 return;
691 WasmSym::dsoHandle = symtab->addOptionalDataSymbol("__dso_handle");
693 if (!config->shared)
694 WasmSym::dataEnd = symtab->addOptionalDataSymbol("__data_end");
696 if (!config->isPic) {
697 WasmSym::globalBase = symtab->addOptionalDataSymbol("__global_base");
698 WasmSym::heapBase = symtab->addOptionalDataSymbol("__heap_base");
699 WasmSym::definedMemoryBase = symtab->addOptionalDataSymbol("__memory_base");
700 WasmSym::definedTableBase = symtab->addOptionalDataSymbol("__table_base");
701 if (config->is64.value_or(false))
702 WasmSym::definedTableBase32 =
703 symtab->addOptionalDataSymbol("__table_base32");
706 // For non-shared memory programs we still need to define __tls_base since we
707 // allow object files built with TLS to be linked into single threaded
708 // programs, and such object files can contain references to this symbol.
710 // However, in this case __tls_base is immutable and points directly to the
711 // start of the `.tdata` static segment.
713 // __tls_size and __tls_align are not needed in this case since they are only
714 // needed for __wasm_init_tls (which we do not create in this case).
715 if (!config->sharedMemory)
716 WasmSym::tlsBase = createOptionalGlobal("__tls_base", false);
719 // Reconstructs command line arguments so that so that you can re-run
720 // the same command with the same inputs. This is for --reproduce.
721 static std::string createResponseFile(const opt::InputArgList &args) {
722 SmallString<0> data;
723 raw_svector_ostream os(data);
725 // Copy the command line to the output while rewriting paths.
726 for (auto *arg : args) {
727 switch (arg->getOption().getID()) {
728 case OPT_reproduce:
729 break;
730 case OPT_INPUT:
731 os << quote(relativeToRoot(arg->getValue())) << "\n";
732 break;
733 case OPT_o:
734 // If -o path contains directories, "lld @response.txt" will likely
735 // fail because the archive we are creating doesn't contain empty
736 // directories for the output path (-o doesn't create directories).
737 // Strip directories to prevent the issue.
738 os << "-o " << quote(sys::path::filename(arg->getValue())) << "\n";
739 break;
740 default:
741 os << toString(*arg) << "\n";
744 return std::string(data.str());
747 // The --wrap option is a feature to rename symbols so that you can write
748 // wrappers for existing functions. If you pass `-wrap=foo`, all
749 // occurrences of symbol `foo` are resolved to `wrap_foo` (so, you are
750 // expected to write `wrap_foo` function as a wrapper). The original
751 // symbol becomes accessible as `real_foo`, so you can call that from your
752 // wrapper.
754 // This data structure is instantiated for each -wrap option.
755 struct WrappedSymbol {
756 Symbol *sym;
757 Symbol *real;
758 Symbol *wrap;
761 static Symbol *addUndefined(StringRef name) {
762 return symtab->addUndefinedFunction(name, None, None, WASM_SYMBOL_UNDEFINED,
763 nullptr, nullptr, false);
766 // Handles -wrap option.
768 // This function instantiates wrapper symbols. At this point, they seem
769 // like they are not being used at all, so we explicitly set some flags so
770 // that LTO won't eliminate them.
771 static std::vector<WrappedSymbol> addWrappedSymbols(opt::InputArgList &args) {
772 std::vector<WrappedSymbol> v;
773 DenseSet<StringRef> seen;
775 for (auto *arg : args.filtered(OPT_wrap)) {
776 StringRef name = arg->getValue();
777 if (!seen.insert(name).second)
778 continue;
780 Symbol *sym = symtab->find(name);
781 if (!sym)
782 continue;
784 Symbol *real = addUndefined(saver().save("__real_" + name));
785 Symbol *wrap = addUndefined(saver().save("__wrap_" + name));
786 v.push_back({sym, real, wrap});
788 // We want to tell LTO not to inline symbols to be overwritten
789 // because LTO doesn't know the final symbol contents after renaming.
790 real->canInline = false;
791 sym->canInline = false;
793 // Tell LTO not to eliminate these symbols.
794 sym->isUsedInRegularObj = true;
795 wrap->isUsedInRegularObj = true;
796 real->isUsedInRegularObj = false;
798 return v;
801 // Do renaming for -wrap by updating pointers to symbols.
803 // When this function is executed, only InputFiles and symbol table
804 // contain pointers to symbol objects. We visit them to replace pointers,
805 // so that wrapped symbols are swapped as instructed by the command line.
806 static void wrapSymbols(ArrayRef<WrappedSymbol> wrapped) {
807 DenseMap<Symbol *, Symbol *> map;
808 for (const WrappedSymbol &w : wrapped) {
809 map[w.sym] = w.wrap;
810 map[w.real] = w.sym;
813 // Update pointers in input files.
814 parallelForEach(symtab->objectFiles, [&](InputFile *file) {
815 MutableArrayRef<Symbol *> syms = file->getMutableSymbols();
816 for (size_t i = 0, e = syms.size(); i != e; ++i)
817 if (Symbol *s = map.lookup(syms[i]))
818 syms[i] = s;
821 // Update pointers in the symbol table.
822 for (const WrappedSymbol &w : wrapped)
823 symtab->wrap(w.sym, w.real, w.wrap);
826 static void splitSections() {
827 // splitIntoPieces needs to be called on each MergeInputChunk
828 // before calling finalizeContents().
829 LLVM_DEBUG(llvm::dbgs() << "splitSections\n");
830 parallelForEach(symtab->objectFiles, [](ObjFile *file) {
831 for (InputChunk *seg : file->segments) {
832 if (auto *s = dyn_cast<MergeInputChunk>(seg))
833 s->splitIntoPieces();
835 for (InputChunk *sec : file->customSections) {
836 if (auto *s = dyn_cast<MergeInputChunk>(sec))
837 s->splitIntoPieces();
842 static bool isKnownZFlag(StringRef s) {
843 // For now, we only support a very limited set of -z flags
844 return s.startswith("stack-size=");
847 // Report a warning for an unknown -z option.
848 static void checkZOptions(opt::InputArgList &args) {
849 for (auto *arg : args.filtered(OPT_z))
850 if (!isKnownZFlag(arg->getValue()))
851 warn("unknown -z value: " + StringRef(arg->getValue()));
854 void LinkerDriver::linkerMain(ArrayRef<const char *> argsArr) {
855 WasmOptTable parser;
856 opt::InputArgList args = parser.parse(argsArr.slice(1));
858 // Interpret these flags early because error()/warn() depend on them.
859 errorHandler().errorLimit = args::getInteger(args, OPT_error_limit, 20);
860 errorHandler().fatalWarnings =
861 args.hasFlag(OPT_fatal_warnings, OPT_no_fatal_warnings, false);
862 checkZOptions(args);
864 // Handle --help
865 if (args.hasArg(OPT_help)) {
866 parser.printHelp(lld::outs(),
867 (std::string(argsArr[0]) + " [options] file...").c_str(),
868 "LLVM Linker", false);
869 return;
872 // Handle --version
873 if (args.hasArg(OPT_version) || args.hasArg(OPT_v)) {
874 lld::outs() << getLLDVersion() << "\n";
875 return;
878 // Handle --reproduce
879 if (auto *arg = args.getLastArg(OPT_reproduce)) {
880 StringRef path = arg->getValue();
881 Expected<std::unique_ptr<TarWriter>> errOrWriter =
882 TarWriter::create(path, path::stem(path));
883 if (errOrWriter) {
884 tar = std::move(*errOrWriter);
885 tar->append("response.txt", createResponseFile(args));
886 tar->append("version.txt", getLLDVersion() + "\n");
887 } else {
888 error("--reproduce: " + toString(errOrWriter.takeError()));
892 // Parse and evaluate -mllvm options.
893 std::vector<const char *> v;
894 v.push_back("wasm-ld (LLVM option parsing)");
895 for (auto *arg : args.filtered(OPT_mllvm))
896 v.push_back(arg->getValue());
897 cl::ResetAllOptionOccurrences();
898 cl::ParseCommandLineOptions(v.size(), v.data());
900 readConfigs(args);
902 createFiles(args);
903 if (errorCount())
904 return;
906 setConfigs();
907 checkOptions(args);
908 if (errorCount())
909 return;
911 if (auto *arg = args.getLastArg(OPT_allow_undefined_file))
912 readImportFile(arg->getValue());
914 // Fail early if the output file or map file is not writable. If a user has a
915 // long link, e.g. due to a large LTO link, they do not wish to run it and
916 // find that it failed because there was a mistake in their command-line.
917 if (auto e = tryCreateFile(config->outputFile))
918 error("cannot open output file " + config->outputFile + ": " + e.message());
919 if (auto e = tryCreateFile(config->mapFile))
920 error("cannot open map file " + config->mapFile + ": " + e.message());
921 if (errorCount())
922 return;
924 // Handle --trace-symbol.
925 for (auto *arg : args.filtered(OPT_trace_symbol))
926 symtab->trace(arg->getValue());
928 for (auto *arg : args.filtered(OPT_export_if_defined))
929 config->exportedSymbols.insert(arg->getValue());
931 for (auto *arg : args.filtered(OPT_export)) {
932 config->exportedSymbols.insert(arg->getValue());
933 config->requiredExports.push_back(arg->getValue());
936 createSyntheticSymbols();
938 // Add all files to the symbol table. This will add almost all
939 // symbols that we need to the symbol table.
940 for (InputFile *f : files)
941 symtab->addFile(f);
942 if (errorCount())
943 return;
945 // Handle the `--undefined <sym>` options.
946 for (auto *arg : args.filtered(OPT_undefined))
947 handleUndefined(arg->getValue());
949 // Handle the `--export <sym>` options
950 // This works like --undefined but also exports the symbol if its found
951 for (auto &iter : config->exportedSymbols)
952 handleUndefined(iter.first());
954 Symbol *entrySym = nullptr;
955 if (!config->relocatable && !config->entry.empty()) {
956 entrySym = handleUndefined(config->entry);
957 if (entrySym && entrySym->isDefined())
958 entrySym->forceExport = true;
959 else
960 error("entry symbol not defined (pass --no-entry to suppress): " +
961 config->entry);
964 // If the user code defines a `__wasm_call_dtors` function, remember it so
965 // that we can call it from the command export wrappers. Unlike
966 // `__wasm_call_ctors` which we synthesize, `__wasm_call_dtors` is defined
967 // by libc/etc., because destructors are registered dynamically with
968 // `__cxa_atexit` and friends.
969 if (!config->relocatable && !config->shared &&
970 !WasmSym::callCtors->isUsedInRegularObj &&
971 WasmSym::callCtors->getName() != config->entry &&
972 !config->exportedSymbols.count(WasmSym::callCtors->getName())) {
973 if (Symbol *callDtors = handleUndefined("__wasm_call_dtors")) {
974 if (auto *callDtorsFunc = dyn_cast<DefinedFunction>(callDtors)) {
975 if (callDtorsFunc->signature &&
976 (!callDtorsFunc->signature->Params.empty() ||
977 !callDtorsFunc->signature->Returns.empty())) {
978 error("__wasm_call_dtors must have no argument or return values");
980 WasmSym::callDtors = callDtorsFunc;
981 } else {
982 error("__wasm_call_dtors must be a function");
987 if (errorCount())
988 return;
990 // Create wrapped symbols for -wrap option.
991 std::vector<WrappedSymbol> wrapped = addWrappedSymbols(args);
993 // If any of our inputs are bitcode files, the LTO code generator may create
994 // references to certain library functions that might not be explicit in the
995 // bitcode file's symbol table. If any of those library functions are defined
996 // in a bitcode file in an archive member, we need to arrange to use LTO to
997 // compile those archive members by adding them to the link beforehand.
999 // We only need to add libcall symbols to the link before LTO if the symbol's
1000 // definition is in bitcode. Any other required libcall symbols will be added
1001 // to the link after LTO when we add the LTO object file to the link.
1002 if (!symtab->bitcodeFiles.empty())
1003 for (auto *s : lto::LTO::getRuntimeLibcallSymbols())
1004 handleLibcall(s);
1005 if (errorCount())
1006 return;
1008 // Do link-time optimization if given files are LLVM bitcode files.
1009 // This compiles bitcode files into real object files.
1010 symtab->compileBitcodeFiles();
1011 if (errorCount())
1012 return;
1014 createOptionalSymbols();
1016 // Resolve any variant symbols that were created due to signature
1017 // mismatchs.
1018 symtab->handleSymbolVariants();
1019 if (errorCount())
1020 return;
1022 // Apply symbol renames for -wrap.
1023 if (!wrapped.empty())
1024 wrapSymbols(wrapped);
1026 for (auto &iter : config->exportedSymbols) {
1027 Symbol *sym = symtab->find(iter.first());
1028 if (sym && sym->isDefined())
1029 sym->forceExport = true;
1032 if (!config->relocatable && !config->isPic) {
1033 // Add synthetic dummies for weak undefined functions. Must happen
1034 // after LTO otherwise functions may not yet have signatures.
1035 symtab->handleWeakUndefines();
1038 if (entrySym)
1039 entrySym->setHidden(false);
1041 if (errorCount())
1042 return;
1044 // Split WASM_SEG_FLAG_STRINGS sections into pieces in preparation for garbage
1045 // collection.
1046 splitSections();
1048 // Any remaining lazy symbols should be demoted to Undefined
1049 demoteLazySymbols();
1051 // Do size optimizations: garbage collection
1052 markLive();
1054 // Provide the indirect function table if needed.
1055 WasmSym::indirectFunctionTable =
1056 symtab->resolveIndirectFunctionTable(/*required =*/false);
1058 if (errorCount())
1059 return;
1061 // Write the result to the file.
1062 writeResult();
1065 } // namespace wasm
1066 } // namespace lld