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