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