xref: /freebsd/contrib/llvm-project/lld/ELF/Driver.cpp (revision 59c8e88e72633afbc47a4ace0d2170d00d51f7dc)
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 // The driver drives the entire linking process. It is responsible for
10 // parsing command line options and doing whatever it is instructed to do.
11 //
12 // One notable thing in the LLD's driver when compared to other linkers is
13 // that the LLD's driver is agnostic on the host operating system.
14 // Other linkers usually have implicit default values (such as a dynamic
15 // linker path or library paths) for each host OS.
16 //
17 // I don't think implicit default values are useful because they are
18 // usually explicitly specified by the compiler ctx.driver. They can even
19 // be harmful when you are doing cross-linking. Therefore, in LLD, we
20 // simply trust the compiler driver to pass all required options and
21 // don't try to make effort on our side.
22 //
23 //===----------------------------------------------------------------------===//
24 
25 #include "Driver.h"
26 #include "Config.h"
27 #include "ICF.h"
28 #include "InputFiles.h"
29 #include "InputSection.h"
30 #include "LTO.h"
31 #include "LinkerScript.h"
32 #include "MarkLive.h"
33 #include "OutputSections.h"
34 #include "ScriptParser.h"
35 #include "SymbolTable.h"
36 #include "Symbols.h"
37 #include "SyntheticSections.h"
38 #include "Target.h"
39 #include "Writer.h"
40 #include "lld/Common/Args.h"
41 #include "lld/Common/CommonLinkerContext.h"
42 #include "lld/Common/Driver.h"
43 #include "lld/Common/ErrorHandler.h"
44 #include "lld/Common/Filesystem.h"
45 #include "lld/Common/Memory.h"
46 #include "lld/Common/Strings.h"
47 #include "lld/Common/TargetOptionsCommandFlags.h"
48 #include "lld/Common/Version.h"
49 #include "llvm/ADT/SetVector.h"
50 #include "llvm/ADT/StringExtras.h"
51 #include "llvm/ADT/StringSwitch.h"
52 #include "llvm/Config/llvm-config.h"
53 #include "llvm/LTO/LTO.h"
54 #include "llvm/Object/Archive.h"
55 #include "llvm/Remarks/HotnessThresholdParser.h"
56 #include "llvm/Support/CommandLine.h"
57 #include "llvm/Support/Compression.h"
58 #include "llvm/Support/FileSystem.h"
59 #include "llvm/Support/GlobPattern.h"
60 #include "llvm/Support/LEB128.h"
61 #include "llvm/Support/Parallel.h"
62 #include "llvm/Support/Path.h"
63 #include "llvm/Support/TarWriter.h"
64 #include "llvm/Support/TargetSelect.h"
65 #include "llvm/Support/TimeProfiler.h"
66 #include "llvm/Support/raw_ostream.h"
67 #include <cstdlib>
68 #include <tuple>
69 #include <utility>
70 
71 using namespace llvm;
72 using namespace llvm::ELF;
73 using namespace llvm::object;
74 using namespace llvm::sys;
75 using namespace llvm::support;
76 using namespace lld;
77 using namespace lld::elf;
78 
79 ConfigWrapper elf::config;
80 Ctx elf::ctx;
81 
82 static void setConfigs(opt::InputArgList &args);
83 static void readConfigs(opt::InputArgList &args);
84 
85 void elf::errorOrWarn(const Twine &msg) {
86   if (config->noinhibitExec)
87     warn(msg);
88   else
89     error(msg);
90 }
91 
92 void Ctx::reset() {
93   driver = LinkerDriver();
94   memoryBuffers.clear();
95   objectFiles.clear();
96   sharedFiles.clear();
97   binaryFiles.clear();
98   bitcodeFiles.clear();
99   lazyBitcodeFiles.clear();
100   inputSections.clear();
101   ehInputSections.clear();
102   duplicates.clear();
103   nonPrevailingSyms.clear();
104   whyExtractRecords.clear();
105   backwardReferences.clear();
106   hasSympart.store(false, std::memory_order_relaxed);
107   needsTlsLd.store(false, std::memory_order_relaxed);
108 }
109 
110 llvm::raw_fd_ostream Ctx::openAuxiliaryFile(llvm::StringRef filename,
111                                             std::error_code &ec) {
112   using namespace llvm::sys::fs;
113   OpenFlags flags =
114       auxiliaryFiles.insert(filename).second ? OF_None : OF_Append;
115   return {filename, ec, flags};
116 }
117 
118 namespace lld {
119 namespace elf {
120 bool link(ArrayRef<const char *> args, llvm::raw_ostream &stdoutOS,
121           llvm::raw_ostream &stderrOS, bool exitEarly, bool disableOutput) {
122   // This driver-specific context will be freed later by unsafeLldMain().
123   auto *ctx = new CommonLinkerContext;
124 
125   ctx->e.initialize(stdoutOS, stderrOS, exitEarly, disableOutput);
126   ctx->e.cleanupCallback = []() {
127     elf::ctx.reset();
128     symtab = SymbolTable();
129 
130     outputSections.clear();
131     symAux.clear();
132 
133     tar = nullptr;
134     in.reset();
135 
136     partitions.clear();
137     partitions.emplace_back();
138 
139     SharedFile::vernauxNum = 0;
140   };
141   ctx->e.logName = args::getFilenameWithoutExe(args[0]);
142   ctx->e.errorLimitExceededMsg = "too many errors emitted, stopping now (use "
143                                  "--error-limit=0 to see all errors)";
144 
145   config = ConfigWrapper();
146   script = std::make_unique<LinkerScript>();
147 
148   symAux.emplace_back();
149 
150   partitions.clear();
151   partitions.emplace_back();
152 
153   config->progName = args[0];
154 
155   elf::ctx.driver.linkerMain(args);
156 
157   return errorCount() == 0;
158 }
159 } // namespace elf
160 } // namespace lld
161 
162 // Parses a linker -m option.
163 static std::tuple<ELFKind, uint16_t, uint8_t> parseEmulation(StringRef emul) {
164   uint8_t osabi = 0;
165   StringRef s = emul;
166   if (s.ends_with("_fbsd")) {
167     s = s.drop_back(5);
168     osabi = ELFOSABI_FREEBSD;
169   }
170 
171   std::pair<ELFKind, uint16_t> ret =
172       StringSwitch<std::pair<ELFKind, uint16_t>>(s)
173           .Cases("aarch64elf", "aarch64linux", {ELF64LEKind, EM_AARCH64})
174           .Cases("aarch64elfb", "aarch64linuxb", {ELF64BEKind, EM_AARCH64})
175           .Cases("armelf", "armelf_linux_eabi", {ELF32LEKind, EM_ARM})
176           .Cases("armelfb", "armelfb_linux_eabi", {ELF32BEKind, EM_ARM})
177           .Case("elf32_x86_64", {ELF32LEKind, EM_X86_64})
178           .Cases("elf32btsmip", "elf32btsmipn32", {ELF32BEKind, EM_MIPS})
179           .Cases("elf32ltsmip", "elf32ltsmipn32", {ELF32LEKind, EM_MIPS})
180           .Case("elf32lriscv", {ELF32LEKind, EM_RISCV})
181           .Cases("elf32ppc", "elf32ppclinux", {ELF32BEKind, EM_PPC})
182           .Cases("elf32lppc", "elf32lppclinux", {ELF32LEKind, EM_PPC})
183           .Case("elf32loongarch", {ELF32LEKind, EM_LOONGARCH})
184           .Case("elf64btsmip", {ELF64BEKind, EM_MIPS})
185           .Case("elf64ltsmip", {ELF64LEKind, EM_MIPS})
186           .Case("elf64lriscv", {ELF64LEKind, EM_RISCV})
187           .Case("elf64ppc", {ELF64BEKind, EM_PPC64})
188           .Case("elf64lppc", {ELF64LEKind, EM_PPC64})
189           .Cases("elf_amd64", "elf_x86_64", {ELF64LEKind, EM_X86_64})
190           .Case("elf_i386", {ELF32LEKind, EM_386})
191           .Case("elf_iamcu", {ELF32LEKind, EM_IAMCU})
192           .Case("elf64_sparc", {ELF64BEKind, EM_SPARCV9})
193           .Case("msp430elf", {ELF32LEKind, EM_MSP430})
194           .Case("elf64_amdgpu", {ELF64LEKind, EM_AMDGPU})
195           .Case("elf64loongarch", {ELF64LEKind, EM_LOONGARCH})
196           .Default({ELFNoneKind, EM_NONE});
197 
198   if (ret.first == ELFNoneKind)
199     error("unknown emulation: " + emul);
200   if (ret.second == EM_MSP430)
201     osabi = ELFOSABI_STANDALONE;
202   else if (ret.second == EM_AMDGPU)
203     osabi = ELFOSABI_AMDGPU_HSA;
204   return std::make_tuple(ret.first, ret.second, osabi);
205 }
206 
207 // Returns slices of MB by parsing MB as an archive file.
208 // Each slice consists of a member file in the archive.
209 std::vector<std::pair<MemoryBufferRef, uint64_t>> static getArchiveMembers(
210     MemoryBufferRef mb) {
211   std::unique_ptr<Archive> file =
212       CHECK(Archive::create(mb),
213             mb.getBufferIdentifier() + ": failed to parse archive");
214 
215   std::vector<std::pair<MemoryBufferRef, uint64_t>> v;
216   Error err = Error::success();
217   bool addToTar = file->isThin() && tar;
218   for (const Archive::Child &c : file->children(err)) {
219     MemoryBufferRef mbref =
220         CHECK(c.getMemoryBufferRef(),
221               mb.getBufferIdentifier() +
222                   ": could not get the buffer for a child of the archive");
223     if (addToTar)
224       tar->append(relativeToRoot(check(c.getFullName())), mbref.getBuffer());
225     v.push_back(std::make_pair(mbref, c.getChildOffset()));
226   }
227   if (err)
228     fatal(mb.getBufferIdentifier() + ": Archive::children failed: " +
229           toString(std::move(err)));
230 
231   // Take ownership of memory buffers created for members of thin archives.
232   std::vector<std::unique_ptr<MemoryBuffer>> mbs = file->takeThinBuffers();
233   std::move(mbs.begin(), mbs.end(), std::back_inserter(ctx.memoryBuffers));
234 
235   return v;
236 }
237 
238 static bool isBitcode(MemoryBufferRef mb) {
239   return identify_magic(mb.getBuffer()) == llvm::file_magic::bitcode;
240 }
241 
242 // Opens a file and create a file object. Path has to be resolved already.
243 void LinkerDriver::addFile(StringRef path, bool withLOption) {
244   using namespace sys::fs;
245 
246   std::optional<MemoryBufferRef> buffer = readFile(path);
247   if (!buffer)
248     return;
249   MemoryBufferRef mbref = *buffer;
250 
251   if (config->formatBinary) {
252     files.push_back(make<BinaryFile>(mbref));
253     return;
254   }
255 
256   switch (identify_magic(mbref.getBuffer())) {
257   case file_magic::unknown:
258     readLinkerScript(mbref);
259     return;
260   case file_magic::archive: {
261     auto members = getArchiveMembers(mbref);
262     if (inWholeArchive) {
263       for (const std::pair<MemoryBufferRef, uint64_t> &p : members) {
264         if (isBitcode(p.first))
265           files.push_back(make<BitcodeFile>(p.first, path, p.second, false));
266         else
267           files.push_back(createObjFile(p.first, path));
268       }
269       return;
270     }
271 
272     archiveFiles.emplace_back(path, members.size());
273 
274     // Handle archives and --start-lib/--end-lib using the same code path. This
275     // scans all the ELF relocatable object files and bitcode files in the
276     // archive rather than just the index file, with the benefit that the
277     // symbols are only loaded once. For many projects archives see high
278     // utilization rates and it is a net performance win. --start-lib scans
279     // symbols in the same order that llvm-ar adds them to the index, so in the
280     // common case the semantics are identical. If the archive symbol table was
281     // created in a different order, or is incomplete, this strategy has
282     // different semantics. Such output differences are considered user error.
283     //
284     // All files within the archive get the same group ID to allow mutual
285     // references for --warn-backrefs.
286     bool saved = InputFile::isInGroup;
287     InputFile::isInGroup = true;
288     for (const std::pair<MemoryBufferRef, uint64_t> &p : members) {
289       auto magic = identify_magic(p.first.getBuffer());
290       if (magic == file_magic::elf_relocatable)
291         files.push_back(createObjFile(p.first, path, true));
292       else if (magic == file_magic::bitcode)
293         files.push_back(make<BitcodeFile>(p.first, path, p.second, true));
294       else
295         warn(path + ": archive member '" + p.first.getBufferIdentifier() +
296              "' is neither ET_REL nor LLVM bitcode");
297     }
298     InputFile::isInGroup = saved;
299     if (!saved)
300       ++InputFile::nextGroupId;
301     return;
302   }
303   case file_magic::elf_shared_object: {
304     if (config->isStatic || config->relocatable) {
305       error("attempted static link of dynamic object " + path);
306       return;
307     }
308 
309     // Shared objects are identified by soname. soname is (if specified)
310     // DT_SONAME and falls back to filename. If a file was specified by -lfoo,
311     // the directory part is ignored. Note that path may be a temporary and
312     // cannot be stored into SharedFile::soName.
313     path = mbref.getBufferIdentifier();
314     auto *f =
315         make<SharedFile>(mbref, withLOption ? path::filename(path) : path);
316     f->init();
317     files.push_back(f);
318     return;
319   }
320   case file_magic::bitcode:
321     files.push_back(make<BitcodeFile>(mbref, "", 0, inLib));
322     break;
323   case file_magic::elf_relocatable:
324     files.push_back(createObjFile(mbref, "", inLib));
325     break;
326   default:
327     error(path + ": unknown file type");
328   }
329 }
330 
331 // Add a given library by searching it from input search paths.
332 void LinkerDriver::addLibrary(StringRef name) {
333   if (std::optional<std::string> path = searchLibrary(name))
334     addFile(saver().save(*path), /*withLOption=*/true);
335   else
336     error("unable to find library -l" + name, ErrorTag::LibNotFound, {name});
337 }
338 
339 // This function is called on startup. We need this for LTO since
340 // LTO calls LLVM functions to compile bitcode files to native code.
341 // Technically this can be delayed until we read bitcode files, but
342 // we don't bother to do lazily because the initialization is fast.
343 static void initLLVM() {
344   InitializeAllTargets();
345   InitializeAllTargetMCs();
346   InitializeAllAsmPrinters();
347   InitializeAllAsmParsers();
348 }
349 
350 // Some command line options or some combinations of them are not allowed.
351 // This function checks for such errors.
352 static void checkOptions() {
353   // The MIPS ABI as of 2016 does not support the GNU-style symbol lookup
354   // table which is a relatively new feature.
355   if (config->emachine == EM_MIPS && config->gnuHash)
356     error("the .gnu.hash section is not compatible with the MIPS target");
357 
358   if (config->emachine == EM_ARM) {
359     if (!config->cmseImplib) {
360       if (!config->cmseInputLib.empty())
361         error("--in-implib may not be used without --cmse-implib");
362       if (!config->cmseOutputLib.empty())
363         error("--out-implib may not be used without --cmse-implib");
364     }
365   } else {
366     if (config->cmseImplib)
367       error("--cmse-implib is only supported on ARM targets");
368     if (!config->cmseInputLib.empty())
369       error("--in-implib is only supported on ARM targets");
370     if (!config->cmseOutputLib.empty())
371       error("--out-implib is only supported on ARM targets");
372   }
373 
374   if (config->fixCortexA53Errata843419 && config->emachine != EM_AARCH64)
375     error("--fix-cortex-a53-843419 is only supported on AArch64 targets");
376 
377   if (config->fixCortexA8 && config->emachine != EM_ARM)
378     error("--fix-cortex-a8 is only supported on ARM targets");
379 
380   if (config->armBe8 && config->emachine != EM_ARM)
381     error("--be8 is only supported on ARM targets");
382 
383   if (config->fixCortexA8 && !config->isLE)
384     error("--fix-cortex-a8 is not supported on big endian targets");
385 
386   if (config->tocOptimize && config->emachine != EM_PPC64)
387     error("--toc-optimize is only supported on PowerPC64 targets");
388 
389   if (config->pcRelOptimize && config->emachine != EM_PPC64)
390     error("--pcrel-optimize is only supported on PowerPC64 targets");
391 
392   if (config->relaxGP && config->emachine != EM_RISCV)
393     error("--relax-gp is only supported on RISC-V targets");
394 
395   if (config->pie && config->shared)
396     error("-shared and -pie may not be used together");
397 
398   if (!config->shared && !config->filterList.empty())
399     error("-F may not be used without -shared");
400 
401   if (!config->shared && !config->auxiliaryList.empty())
402     error("-f may not be used without -shared");
403 
404   if (config->strip == StripPolicy::All && config->emitRelocs)
405     error("--strip-all and --emit-relocs may not be used together");
406 
407   if (config->zText && config->zIfuncNoplt)
408     error("-z text and -z ifunc-noplt may not be used together");
409 
410   if (config->relocatable) {
411     if (config->shared)
412       error("-r and -shared may not be used together");
413     if (config->gdbIndex)
414       error("-r and --gdb-index may not be used together");
415     if (config->icf != ICFLevel::None)
416       error("-r and --icf may not be used together");
417     if (config->pie)
418       error("-r and -pie may not be used together");
419     if (config->exportDynamic)
420       error("-r and --export-dynamic may not be used together");
421   }
422 
423   if (config->executeOnly) {
424     if (config->emachine != EM_AARCH64)
425       error("--execute-only is only supported on AArch64 targets");
426 
427     if (config->singleRoRx && !script->hasSectionsCommand)
428       error("--execute-only and --no-rosegment cannot be used together");
429   }
430 
431   if (config->zRetpolineplt && config->zForceIbt)
432     error("-z force-ibt may not be used with -z retpolineplt");
433 
434   if (config->emachine != EM_AARCH64) {
435     if (config->zPacPlt)
436       error("-z pac-plt only supported on AArch64");
437     if (config->zForceBti)
438       error("-z force-bti only supported on AArch64");
439     if (config->zBtiReport != "none")
440       error("-z bti-report only supported on AArch64");
441   }
442 
443   if (config->emachine != EM_386 && config->emachine != EM_X86_64 &&
444       config->zCetReport != "none")
445     error("-z cet-report only supported on X86 and X86_64");
446 }
447 
448 static const char *getReproduceOption(opt::InputArgList &args) {
449   if (auto *arg = args.getLastArg(OPT_reproduce))
450     return arg->getValue();
451   return getenv("LLD_REPRODUCE");
452 }
453 
454 static bool hasZOption(opt::InputArgList &args, StringRef key) {
455   for (auto *arg : args.filtered(OPT_z))
456     if (key == arg->getValue())
457       return true;
458   return false;
459 }
460 
461 static bool getZFlag(opt::InputArgList &args, StringRef k1, StringRef k2,
462                      bool Default) {
463   for (auto *arg : args.filtered_reverse(OPT_z)) {
464     if (k1 == arg->getValue())
465       return true;
466     if (k2 == arg->getValue())
467       return false;
468   }
469   return Default;
470 }
471 
472 static SeparateSegmentKind getZSeparate(opt::InputArgList &args) {
473   for (auto *arg : args.filtered_reverse(OPT_z)) {
474     StringRef v = arg->getValue();
475     if (v == "noseparate-code")
476       return SeparateSegmentKind::None;
477     if (v == "separate-code")
478       return SeparateSegmentKind::Code;
479     if (v == "separate-loadable-segments")
480       return SeparateSegmentKind::Loadable;
481   }
482   return SeparateSegmentKind::None;
483 }
484 
485 static GnuStackKind getZGnuStack(opt::InputArgList &args) {
486   for (auto *arg : args.filtered_reverse(OPT_z)) {
487     if (StringRef("execstack") == arg->getValue())
488       return GnuStackKind::Exec;
489     if (StringRef("noexecstack") == arg->getValue())
490       return GnuStackKind::NoExec;
491     if (StringRef("nognustack") == arg->getValue())
492       return GnuStackKind::None;
493   }
494 
495   return GnuStackKind::NoExec;
496 }
497 
498 static uint8_t getZStartStopVisibility(opt::InputArgList &args) {
499   for (auto *arg : args.filtered_reverse(OPT_z)) {
500     std::pair<StringRef, StringRef> kv = StringRef(arg->getValue()).split('=');
501     if (kv.first == "start-stop-visibility") {
502       if (kv.second == "default")
503         return STV_DEFAULT;
504       else if (kv.second == "internal")
505         return STV_INTERNAL;
506       else if (kv.second == "hidden")
507         return STV_HIDDEN;
508       else if (kv.second == "protected")
509         return STV_PROTECTED;
510       error("unknown -z start-stop-visibility= value: " + StringRef(kv.second));
511     }
512   }
513   return STV_PROTECTED;
514 }
515 
516 constexpr const char *knownZFlags[] = {
517     "combreloc",
518     "copyreloc",
519     "defs",
520     "execstack",
521     "force-bti",
522     "force-ibt",
523     "global",
524     "hazardplt",
525     "ifunc-noplt",
526     "initfirst",
527     "interpose",
528     "keep-text-section-prefix",
529     "lazy",
530     "muldefs",
531     "nocombreloc",
532     "nocopyreloc",
533     "nodefaultlib",
534     "nodelete",
535     "nodlopen",
536     "noexecstack",
537     "nognustack",
538     "nokeep-text-section-prefix",
539     "nopack-relative-relocs",
540     "norelro",
541     "noseparate-code",
542     "nostart-stop-gc",
543     "notext",
544     "now",
545     "origin",
546     "pac-plt",
547     "pack-relative-relocs",
548     "rel",
549     "rela",
550     "relro",
551     "retpolineplt",
552     "rodynamic",
553     "separate-code",
554     "separate-loadable-segments",
555     "shstk",
556     "start-stop-gc",
557     "text",
558     "undefs",
559     "wxneeded",
560 };
561 
562 static bool isKnownZFlag(StringRef s) {
563   return llvm::is_contained(knownZFlags, s) ||
564          s.starts_with("common-page-size=") || s.starts_with("bti-report=") ||
565          s.starts_with("cet-report=") ||
566          s.starts_with("dead-reloc-in-nonalloc=") ||
567          s.starts_with("max-page-size=") || s.starts_with("stack-size=") ||
568          s.starts_with("start-stop-visibility=");
569 }
570 
571 // Report a warning for an unknown -z option.
572 static void checkZOptions(opt::InputArgList &args) {
573   for (auto *arg : args.filtered(OPT_z))
574     if (!isKnownZFlag(arg->getValue()))
575       warn("unknown -z value: " + StringRef(arg->getValue()));
576 }
577 
578 constexpr const char *saveTempsValues[] = {
579     "resolution", "preopt",     "promote", "internalize",  "import",
580     "opt",        "precodegen", "prelink", "combinedindex"};
581 
582 void LinkerDriver::linkerMain(ArrayRef<const char *> argsArr) {
583   ELFOptTable parser;
584   opt::InputArgList args = parser.parse(argsArr.slice(1));
585 
586   // Interpret these flags early because error()/warn() depend on them.
587   errorHandler().errorLimit = args::getInteger(args, OPT_error_limit, 20);
588   errorHandler().fatalWarnings =
589       args.hasFlag(OPT_fatal_warnings, OPT_no_fatal_warnings, false) &&
590       !args.hasArg(OPT_no_warnings);
591   errorHandler().suppressWarnings = args.hasArg(OPT_no_warnings);
592   checkZOptions(args);
593 
594   // Handle -help
595   if (args.hasArg(OPT_help)) {
596     printHelp();
597     return;
598   }
599 
600   // Handle -v or -version.
601   //
602   // A note about "compatible with GNU linkers" message: this is a hack for
603   // scripts generated by GNU Libtool up to 2021-10 to recognize LLD as
604   // a GNU compatible linker. See
605   // <https://lists.gnu.org/archive/html/libtool/2017-01/msg00007.html>.
606   //
607   // This is somewhat ugly hack, but in reality, we had no choice other
608   // than doing this. Considering the very long release cycle of Libtool,
609   // it is not easy to improve it to recognize LLD as a GNU compatible
610   // linker in a timely manner. Even if we can make it, there are still a
611   // lot of "configure" scripts out there that are generated by old version
612   // of Libtool. We cannot convince every software developer to migrate to
613   // the latest version and re-generate scripts. So we have this hack.
614   if (args.hasArg(OPT_v) || args.hasArg(OPT_version))
615     message(getLLDVersion() + " (compatible with GNU linkers)");
616 
617   if (const char *path = getReproduceOption(args)) {
618     // Note that --reproduce is a debug option so you can ignore it
619     // if you are trying to understand the whole picture of the code.
620     Expected<std::unique_ptr<TarWriter>> errOrWriter =
621         TarWriter::create(path, path::stem(path));
622     if (errOrWriter) {
623       tar = std::move(*errOrWriter);
624       tar->append("response.txt", createResponseFile(args));
625       tar->append("version.txt", getLLDVersion() + "\n");
626       StringRef ltoSampleProfile = args.getLastArgValue(OPT_lto_sample_profile);
627       if (!ltoSampleProfile.empty())
628         readFile(ltoSampleProfile);
629     } else {
630       error("--reproduce: " + toString(errOrWriter.takeError()));
631     }
632   }
633 
634   readConfigs(args);
635 
636   // The behavior of -v or --version is a bit strange, but this is
637   // needed for compatibility with GNU linkers.
638   if (args.hasArg(OPT_v) && !args.hasArg(OPT_INPUT))
639     return;
640   if (args.hasArg(OPT_version))
641     return;
642 
643   // Initialize time trace profiler.
644   if (config->timeTraceEnabled)
645     timeTraceProfilerInitialize(config->timeTraceGranularity, config->progName);
646 
647   {
648     llvm::TimeTraceScope timeScope("ExecuteLinker");
649 
650     initLLVM();
651     createFiles(args);
652     if (errorCount())
653       return;
654 
655     inferMachineType();
656     setConfigs(args);
657     checkOptions();
658     if (errorCount())
659       return;
660 
661     link(args);
662   }
663 
664   if (config->timeTraceEnabled) {
665     checkError(timeTraceProfilerWrite(
666         args.getLastArgValue(OPT_time_trace_eq).str(), config->outputFile));
667     timeTraceProfilerCleanup();
668   }
669 }
670 
671 static std::string getRpath(opt::InputArgList &args) {
672   SmallVector<StringRef, 0> v = args::getStrings(args, OPT_rpath);
673   return llvm::join(v.begin(), v.end(), ":");
674 }
675 
676 // Determines what we should do if there are remaining unresolved
677 // symbols after the name resolution.
678 static void setUnresolvedSymbolPolicy(opt::InputArgList &args) {
679   UnresolvedPolicy errorOrWarn = args.hasFlag(OPT_error_unresolved_symbols,
680                                               OPT_warn_unresolved_symbols, true)
681                                      ? UnresolvedPolicy::ReportError
682                                      : UnresolvedPolicy::Warn;
683   // -shared implies --unresolved-symbols=ignore-all because missing
684   // symbols are likely to be resolved at runtime.
685   bool diagRegular = !config->shared, diagShlib = !config->shared;
686 
687   for (const opt::Arg *arg : args) {
688     switch (arg->getOption().getID()) {
689     case OPT_unresolved_symbols: {
690       StringRef s = arg->getValue();
691       if (s == "ignore-all") {
692         diagRegular = false;
693         diagShlib = false;
694       } else if (s == "ignore-in-object-files") {
695         diagRegular = false;
696         diagShlib = true;
697       } else if (s == "ignore-in-shared-libs") {
698         diagRegular = true;
699         diagShlib = false;
700       } else if (s == "report-all") {
701         diagRegular = true;
702         diagShlib = true;
703       } else {
704         error("unknown --unresolved-symbols value: " + s);
705       }
706       break;
707     }
708     case OPT_no_undefined:
709       diagRegular = true;
710       break;
711     case OPT_z:
712       if (StringRef(arg->getValue()) == "defs")
713         diagRegular = true;
714       else if (StringRef(arg->getValue()) == "undefs")
715         diagRegular = false;
716       break;
717     case OPT_allow_shlib_undefined:
718       diagShlib = false;
719       break;
720     case OPT_no_allow_shlib_undefined:
721       diagShlib = true;
722       break;
723     }
724   }
725 
726   config->unresolvedSymbols =
727       diagRegular ? errorOrWarn : UnresolvedPolicy::Ignore;
728   config->unresolvedSymbolsInShlib =
729       diagShlib ? errorOrWarn : UnresolvedPolicy::Ignore;
730 }
731 
732 static Target2Policy getTarget2(opt::InputArgList &args) {
733   StringRef s = args.getLastArgValue(OPT_target2, "got-rel");
734   if (s == "rel")
735     return Target2Policy::Rel;
736   if (s == "abs")
737     return Target2Policy::Abs;
738   if (s == "got-rel")
739     return Target2Policy::GotRel;
740   error("unknown --target2 option: " + s);
741   return Target2Policy::GotRel;
742 }
743 
744 static bool isOutputFormatBinary(opt::InputArgList &args) {
745   StringRef s = args.getLastArgValue(OPT_oformat, "elf");
746   if (s == "binary")
747     return true;
748   if (!s.starts_with("elf"))
749     error("unknown --oformat value: " + s);
750   return false;
751 }
752 
753 static DiscardPolicy getDiscard(opt::InputArgList &args) {
754   auto *arg =
755       args.getLastArg(OPT_discard_all, OPT_discard_locals, OPT_discard_none);
756   if (!arg)
757     return DiscardPolicy::Default;
758   if (arg->getOption().getID() == OPT_discard_all)
759     return DiscardPolicy::All;
760   if (arg->getOption().getID() == OPT_discard_locals)
761     return DiscardPolicy::Locals;
762   return DiscardPolicy::None;
763 }
764 
765 static StringRef getDynamicLinker(opt::InputArgList &args) {
766   auto *arg = args.getLastArg(OPT_dynamic_linker, OPT_no_dynamic_linker);
767   if (!arg)
768     return "";
769   if (arg->getOption().getID() == OPT_no_dynamic_linker) {
770     // --no-dynamic-linker suppresses undefined weak symbols in .dynsym
771     config->noDynamicLinker = true;
772     return "";
773   }
774   return arg->getValue();
775 }
776 
777 static int getMemtagMode(opt::InputArgList &args) {
778   StringRef memtagModeArg = args.getLastArgValue(OPT_android_memtag_mode);
779   if (memtagModeArg.empty()) {
780     if (config->androidMemtagStack)
781       warn("--android-memtag-mode is unspecified, leaving "
782            "--android-memtag-stack a no-op");
783     else if (config->androidMemtagHeap)
784       warn("--android-memtag-mode is unspecified, leaving "
785            "--android-memtag-heap a no-op");
786     return ELF::NT_MEMTAG_LEVEL_NONE;
787   }
788 
789   if (!config->androidMemtagHeap && !config->androidMemtagStack) {
790     error("when using --android-memtag-mode, at least one of "
791           "--android-memtag-heap or "
792           "--android-memtag-stack is required");
793     return ELF::NT_MEMTAG_LEVEL_NONE;
794   }
795 
796   if (memtagModeArg == "sync")
797     return ELF::NT_MEMTAG_LEVEL_SYNC;
798   if (memtagModeArg == "async")
799     return ELF::NT_MEMTAG_LEVEL_ASYNC;
800   if (memtagModeArg == "none")
801     return ELF::NT_MEMTAG_LEVEL_NONE;
802 
803   error("unknown --android-memtag-mode value: \"" + memtagModeArg +
804         "\", should be one of {async, sync, none}");
805   return ELF::NT_MEMTAG_LEVEL_NONE;
806 }
807 
808 static ICFLevel getICF(opt::InputArgList &args) {
809   auto *arg = args.getLastArg(OPT_icf_none, OPT_icf_safe, OPT_icf_all);
810   if (!arg || arg->getOption().getID() == OPT_icf_none)
811     return ICFLevel::None;
812   if (arg->getOption().getID() == OPT_icf_safe)
813     return ICFLevel::Safe;
814   return ICFLevel::All;
815 }
816 
817 static StripPolicy getStrip(opt::InputArgList &args) {
818   if (args.hasArg(OPT_relocatable))
819     return StripPolicy::None;
820 
821   auto *arg = args.getLastArg(OPT_strip_all, OPT_strip_debug);
822   if (!arg)
823     return StripPolicy::None;
824   if (arg->getOption().getID() == OPT_strip_all)
825     return StripPolicy::All;
826   return StripPolicy::Debug;
827 }
828 
829 static uint64_t parseSectionAddress(StringRef s, opt::InputArgList &args,
830                                     const opt::Arg &arg) {
831   uint64_t va = 0;
832   if (s.starts_with("0x"))
833     s = s.drop_front(2);
834   if (!to_integer(s, va, 16))
835     error("invalid argument: " + arg.getAsString(args));
836   return va;
837 }
838 
839 static StringMap<uint64_t> getSectionStartMap(opt::InputArgList &args) {
840   StringMap<uint64_t> ret;
841   for (auto *arg : args.filtered(OPT_section_start)) {
842     StringRef name;
843     StringRef addr;
844     std::tie(name, addr) = StringRef(arg->getValue()).split('=');
845     ret[name] = parseSectionAddress(addr, args, *arg);
846   }
847 
848   if (auto *arg = args.getLastArg(OPT_Ttext))
849     ret[".text"] = parseSectionAddress(arg->getValue(), args, *arg);
850   if (auto *arg = args.getLastArg(OPT_Tdata))
851     ret[".data"] = parseSectionAddress(arg->getValue(), args, *arg);
852   if (auto *arg = args.getLastArg(OPT_Tbss))
853     ret[".bss"] = parseSectionAddress(arg->getValue(), args, *arg);
854   return ret;
855 }
856 
857 static SortSectionPolicy getSortSection(opt::InputArgList &args) {
858   StringRef s = args.getLastArgValue(OPT_sort_section);
859   if (s == "alignment")
860     return SortSectionPolicy::Alignment;
861   if (s == "name")
862     return SortSectionPolicy::Name;
863   if (!s.empty())
864     error("unknown --sort-section rule: " + s);
865   return SortSectionPolicy::Default;
866 }
867 
868 static OrphanHandlingPolicy getOrphanHandling(opt::InputArgList &args) {
869   StringRef s = args.getLastArgValue(OPT_orphan_handling, "place");
870   if (s == "warn")
871     return OrphanHandlingPolicy::Warn;
872   if (s == "error")
873     return OrphanHandlingPolicy::Error;
874   if (s != "place")
875     error("unknown --orphan-handling mode: " + s);
876   return OrphanHandlingPolicy::Place;
877 }
878 
879 // Parse --build-id or --build-id=<style>. We handle "tree" as a
880 // synonym for "sha1" because all our hash functions including
881 // --build-id=sha1 are actually tree hashes for performance reasons.
882 static std::pair<BuildIdKind, SmallVector<uint8_t, 0>>
883 getBuildId(opt::InputArgList &args) {
884   auto *arg = args.getLastArg(OPT_build_id);
885   if (!arg)
886     return {BuildIdKind::None, {}};
887 
888   StringRef s = arg->getValue();
889   if (s == "fast")
890     return {BuildIdKind::Fast, {}};
891   if (s == "md5")
892     return {BuildIdKind::Md5, {}};
893   if (s == "sha1" || s == "tree")
894     return {BuildIdKind::Sha1, {}};
895   if (s == "uuid")
896     return {BuildIdKind::Uuid, {}};
897   if (s.starts_with("0x"))
898     return {BuildIdKind::Hexstring, parseHex(s.substr(2))};
899 
900   if (s != "none")
901     error("unknown --build-id style: " + s);
902   return {BuildIdKind::None, {}};
903 }
904 
905 static std::pair<bool, bool> getPackDynRelocs(opt::InputArgList &args) {
906   StringRef s = args.getLastArgValue(OPT_pack_dyn_relocs, "none");
907   if (s == "android")
908     return {true, false};
909   if (s == "relr")
910     return {false, true};
911   if (s == "android+relr")
912     return {true, true};
913 
914   if (s != "none")
915     error("unknown --pack-dyn-relocs format: " + s);
916   return {false, false};
917 }
918 
919 static void readCallGraph(MemoryBufferRef mb) {
920   // Build a map from symbol name to section
921   DenseMap<StringRef, Symbol *> map;
922   for (ELFFileBase *file : ctx.objectFiles)
923     for (Symbol *sym : file->getSymbols())
924       map[sym->getName()] = sym;
925 
926   auto findSection = [&](StringRef name) -> InputSectionBase * {
927     Symbol *sym = map.lookup(name);
928     if (!sym) {
929       if (config->warnSymbolOrdering)
930         warn(mb.getBufferIdentifier() + ": no such symbol: " + name);
931       return nullptr;
932     }
933     maybeWarnUnorderableSymbol(sym);
934 
935     if (Defined *dr = dyn_cast_or_null<Defined>(sym))
936       return dyn_cast_or_null<InputSectionBase>(dr->section);
937     return nullptr;
938   };
939 
940   for (StringRef line : args::getLines(mb)) {
941     SmallVector<StringRef, 3> fields;
942     line.split(fields, ' ');
943     uint64_t count;
944 
945     if (fields.size() != 3 || !to_integer(fields[2], count)) {
946       error(mb.getBufferIdentifier() + ": parse error");
947       return;
948     }
949 
950     if (InputSectionBase *from = findSection(fields[0]))
951       if (InputSectionBase *to = findSection(fields[1]))
952         config->callGraphProfile[std::make_pair(from, to)] += count;
953   }
954 }
955 
956 // If SHT_LLVM_CALL_GRAPH_PROFILE and its relocation section exist, returns
957 // true and populates cgProfile and symbolIndices.
958 template <class ELFT>
959 static bool
960 processCallGraphRelocations(SmallVector<uint32_t, 32> &symbolIndices,
961                             ArrayRef<typename ELFT::CGProfile> &cgProfile,
962                             ObjFile<ELFT> *inputObj) {
963   if (inputObj->cgProfileSectionIndex == SHN_UNDEF)
964     return false;
965 
966   ArrayRef<Elf_Shdr_Impl<ELFT>> objSections =
967       inputObj->template getELFShdrs<ELFT>();
968   symbolIndices.clear();
969   const ELFFile<ELFT> &obj = inputObj->getObj();
970   cgProfile =
971       check(obj.template getSectionContentsAsArray<typename ELFT::CGProfile>(
972           objSections[inputObj->cgProfileSectionIndex]));
973 
974   for (size_t i = 0, e = objSections.size(); i < e; ++i) {
975     const Elf_Shdr_Impl<ELFT> &sec = objSections[i];
976     if (sec.sh_info == inputObj->cgProfileSectionIndex) {
977       if (sec.sh_type == SHT_RELA) {
978         ArrayRef<typename ELFT::Rela> relas =
979             CHECK(obj.relas(sec), "could not retrieve cg profile rela section");
980         for (const typename ELFT::Rela &rel : relas)
981           symbolIndices.push_back(rel.getSymbol(config->isMips64EL));
982         break;
983       }
984       if (sec.sh_type == SHT_REL) {
985         ArrayRef<typename ELFT::Rel> rels =
986             CHECK(obj.rels(sec), "could not retrieve cg profile rel section");
987         for (const typename ELFT::Rel &rel : rels)
988           symbolIndices.push_back(rel.getSymbol(config->isMips64EL));
989         break;
990       }
991     }
992   }
993   if (symbolIndices.empty())
994     warn("SHT_LLVM_CALL_GRAPH_PROFILE exists, but relocation section doesn't");
995   return !symbolIndices.empty();
996 }
997 
998 template <class ELFT> static void readCallGraphsFromObjectFiles() {
999   SmallVector<uint32_t, 32> symbolIndices;
1000   ArrayRef<typename ELFT::CGProfile> cgProfile;
1001   for (auto file : ctx.objectFiles) {
1002     auto *obj = cast<ObjFile<ELFT>>(file);
1003     if (!processCallGraphRelocations(symbolIndices, cgProfile, obj))
1004       continue;
1005 
1006     if (symbolIndices.size() != cgProfile.size() * 2)
1007       fatal("number of relocations doesn't match Weights");
1008 
1009     for (uint32_t i = 0, size = cgProfile.size(); i < size; ++i) {
1010       const Elf_CGProfile_Impl<ELFT> &cgpe = cgProfile[i];
1011       uint32_t fromIndex = symbolIndices[i * 2];
1012       uint32_t toIndex = symbolIndices[i * 2 + 1];
1013       auto *fromSym = dyn_cast<Defined>(&obj->getSymbol(fromIndex));
1014       auto *toSym = dyn_cast<Defined>(&obj->getSymbol(toIndex));
1015       if (!fromSym || !toSym)
1016         continue;
1017 
1018       auto *from = dyn_cast_or_null<InputSectionBase>(fromSym->section);
1019       auto *to = dyn_cast_or_null<InputSectionBase>(toSym->section);
1020       if (from && to)
1021         config->callGraphProfile[{from, to}] += cgpe.cgp_weight;
1022     }
1023   }
1024 }
1025 
1026 static DebugCompressionType getCompressionType(StringRef s, StringRef option) {
1027   DebugCompressionType type = StringSwitch<DebugCompressionType>(s)
1028                                   .Case("zlib", DebugCompressionType::Zlib)
1029                                   .Case("zstd", DebugCompressionType::Zstd)
1030                                   .Default(DebugCompressionType::None);
1031   if (type == DebugCompressionType::None) {
1032     if (s != "none")
1033       error("unknown " + option + " value: " + s);
1034   } else if (const char *reason = compression::getReasonIfUnsupported(
1035                  compression::formatFor(type))) {
1036     error(option + ": " + reason);
1037   }
1038   return type;
1039 }
1040 
1041 static StringRef getAliasSpelling(opt::Arg *arg) {
1042   if (const opt::Arg *alias = arg->getAlias())
1043     return alias->getSpelling();
1044   return arg->getSpelling();
1045 }
1046 
1047 static std::pair<StringRef, StringRef> getOldNewOptions(opt::InputArgList &args,
1048                                                         unsigned id) {
1049   auto *arg = args.getLastArg(id);
1050   if (!arg)
1051     return {"", ""};
1052 
1053   StringRef s = arg->getValue();
1054   std::pair<StringRef, StringRef> ret = s.split(';');
1055   if (ret.second.empty())
1056     error(getAliasSpelling(arg) + " expects 'old;new' format, but got " + s);
1057   return ret;
1058 }
1059 
1060 // Parse options of the form "old;new[;extra]".
1061 static std::tuple<StringRef, StringRef, StringRef>
1062 getOldNewOptionsExtra(opt::InputArgList &args, unsigned id) {
1063   auto [oldDir, second] = getOldNewOptions(args, id);
1064   auto [newDir, extraDir] = second.split(';');
1065   return {oldDir, newDir, extraDir};
1066 }
1067 
1068 // Parse the symbol ordering file and warn for any duplicate entries.
1069 static SmallVector<StringRef, 0> getSymbolOrderingFile(MemoryBufferRef mb) {
1070   SetVector<StringRef, SmallVector<StringRef, 0>> names;
1071   for (StringRef s : args::getLines(mb))
1072     if (!names.insert(s) && config->warnSymbolOrdering)
1073       warn(mb.getBufferIdentifier() + ": duplicate ordered symbol: " + s);
1074 
1075   return names.takeVector();
1076 }
1077 
1078 static bool getIsRela(opt::InputArgList &args) {
1079   // If -z rel or -z rela is specified, use the last option.
1080   for (auto *arg : args.filtered_reverse(OPT_z)) {
1081     StringRef s(arg->getValue());
1082     if (s == "rel")
1083       return false;
1084     if (s == "rela")
1085       return true;
1086   }
1087 
1088   // Otherwise use the psABI defined relocation entry format.
1089   uint16_t m = config->emachine;
1090   return m == EM_AARCH64 || m == EM_AMDGPU || m == EM_HEXAGON ||
1091          m == EM_LOONGARCH || m == EM_PPC || m == EM_PPC64 || m == EM_RISCV ||
1092          m == EM_X86_64;
1093 }
1094 
1095 static void parseClangOption(StringRef opt, const Twine &msg) {
1096   std::string err;
1097   raw_string_ostream os(err);
1098 
1099   const char *argv[] = {config->progName.data(), opt.data()};
1100   if (cl::ParseCommandLineOptions(2, argv, "", &os))
1101     return;
1102   os.flush();
1103   error(msg + ": " + StringRef(err).trim());
1104 }
1105 
1106 // Checks the parameter of the bti-report and cet-report options.
1107 static bool isValidReportString(StringRef arg) {
1108   return arg == "none" || arg == "warning" || arg == "error";
1109 }
1110 
1111 // Process a remap pattern 'from-glob=to-file'.
1112 static bool remapInputs(StringRef line, const Twine &location) {
1113   SmallVector<StringRef, 0> fields;
1114   line.split(fields, '=');
1115   if (fields.size() != 2 || fields[1].empty()) {
1116     error(location + ": parse error, not 'from-glob=to-file'");
1117     return true;
1118   }
1119   if (!hasWildcard(fields[0]))
1120     config->remapInputs[fields[0]] = fields[1];
1121   else if (Expected<GlobPattern> pat = GlobPattern::create(fields[0]))
1122     config->remapInputsWildcards.emplace_back(std::move(*pat), fields[1]);
1123   else {
1124     error(location + ": " + toString(pat.takeError()));
1125     return true;
1126   }
1127   return false;
1128 }
1129 
1130 // Initializes Config members by the command line options.
1131 static void readConfigs(opt::InputArgList &args) {
1132   errorHandler().verbose = args.hasArg(OPT_verbose);
1133   errorHandler().vsDiagnostics =
1134       args.hasArg(OPT_visual_studio_diagnostics_format, false);
1135 
1136   config->allowMultipleDefinition =
1137       args.hasFlag(OPT_allow_multiple_definition,
1138                    OPT_no_allow_multiple_definition, false) ||
1139       hasZOption(args, "muldefs");
1140   config->androidMemtagHeap =
1141       args.hasFlag(OPT_android_memtag_heap, OPT_no_android_memtag_heap, false);
1142   config->androidMemtagStack = args.hasFlag(OPT_android_memtag_stack,
1143                                             OPT_no_android_memtag_stack, false);
1144   config->androidMemtagMode = getMemtagMode(args);
1145   config->auxiliaryList = args::getStrings(args, OPT_auxiliary);
1146   config->armBe8 = args.hasArg(OPT_be8);
1147   if (opt::Arg *arg =
1148           args.getLastArg(OPT_Bno_symbolic, OPT_Bsymbolic_non_weak_functions,
1149                           OPT_Bsymbolic_functions, OPT_Bsymbolic)) {
1150     if (arg->getOption().matches(OPT_Bsymbolic_non_weak_functions))
1151       config->bsymbolic = BsymbolicKind::NonWeakFunctions;
1152     else if (arg->getOption().matches(OPT_Bsymbolic_functions))
1153       config->bsymbolic = BsymbolicKind::Functions;
1154     else if (arg->getOption().matches(OPT_Bsymbolic))
1155       config->bsymbolic = BsymbolicKind::All;
1156   }
1157   config->checkSections =
1158       args.hasFlag(OPT_check_sections, OPT_no_check_sections, true);
1159   config->chroot = args.getLastArgValue(OPT_chroot);
1160   config->compressDebugSections = getCompressionType(
1161       args.getLastArgValue(OPT_compress_debug_sections, "none"),
1162       "--compress-debug-sections");
1163   config->cref = args.hasArg(OPT_cref);
1164   config->optimizeBBJumps =
1165       args.hasFlag(OPT_optimize_bb_jumps, OPT_no_optimize_bb_jumps, false);
1166   config->demangle = args.hasFlag(OPT_demangle, OPT_no_demangle, true);
1167   config->dependencyFile = args.getLastArgValue(OPT_dependency_file);
1168   config->dependentLibraries = args.hasFlag(OPT_dependent_libraries, OPT_no_dependent_libraries, true);
1169   config->disableVerify = args.hasArg(OPT_disable_verify);
1170   config->discard = getDiscard(args);
1171   config->dwoDir = args.getLastArgValue(OPT_plugin_opt_dwo_dir_eq);
1172   config->dynamicLinker = getDynamicLinker(args);
1173   config->ehFrameHdr =
1174       args.hasFlag(OPT_eh_frame_hdr, OPT_no_eh_frame_hdr, false);
1175   config->emitLLVM = args.hasArg(OPT_plugin_opt_emit_llvm, false);
1176   config->emitRelocs = args.hasArg(OPT_emit_relocs);
1177   config->callGraphProfileSort = args.hasFlag(
1178       OPT_call_graph_profile_sort, OPT_no_call_graph_profile_sort, true);
1179   config->enableNewDtags =
1180       args.hasFlag(OPT_enable_new_dtags, OPT_disable_new_dtags, true);
1181   config->entry = args.getLastArgValue(OPT_entry);
1182 
1183   errorHandler().errorHandlingScript =
1184       args.getLastArgValue(OPT_error_handling_script);
1185 
1186   config->executeOnly =
1187       args.hasFlag(OPT_execute_only, OPT_no_execute_only, false);
1188   config->exportDynamic =
1189       args.hasFlag(OPT_export_dynamic, OPT_no_export_dynamic, false) ||
1190       args.hasArg(OPT_shared);
1191   config->filterList = args::getStrings(args, OPT_filter);
1192   config->fini = args.getLastArgValue(OPT_fini, "_fini");
1193   config->fixCortexA53Errata843419 = args.hasArg(OPT_fix_cortex_a53_843419) &&
1194                                      !args.hasArg(OPT_relocatable);
1195   config->cmseImplib = args.hasArg(OPT_cmse_implib);
1196   config->cmseInputLib = args.getLastArgValue(OPT_in_implib);
1197   config->cmseOutputLib = args.getLastArgValue(OPT_out_implib);
1198   config->fixCortexA8 =
1199       args.hasArg(OPT_fix_cortex_a8) && !args.hasArg(OPT_relocatable);
1200   config->fortranCommon =
1201       args.hasFlag(OPT_fortran_common, OPT_no_fortran_common, false);
1202   config->gcSections = args.hasFlag(OPT_gc_sections, OPT_no_gc_sections, false);
1203   config->gnuUnique = args.hasFlag(OPT_gnu_unique, OPT_no_gnu_unique, true);
1204   config->gdbIndex = args.hasFlag(OPT_gdb_index, OPT_no_gdb_index, false);
1205   config->icf = getICF(args);
1206   config->ignoreDataAddressEquality =
1207       args.hasArg(OPT_ignore_data_address_equality);
1208   config->ignoreFunctionAddressEquality =
1209       args.hasArg(OPT_ignore_function_address_equality);
1210   config->init = args.getLastArgValue(OPT_init, "_init");
1211   config->ltoAAPipeline = args.getLastArgValue(OPT_lto_aa_pipeline);
1212   config->ltoCSProfileGenerate = args.hasArg(OPT_lto_cs_profile_generate);
1213   config->ltoCSProfileFile = args.getLastArgValue(OPT_lto_cs_profile_file);
1214   config->ltoPGOWarnMismatch = args.hasFlag(OPT_lto_pgo_warn_mismatch,
1215                                             OPT_no_lto_pgo_warn_mismatch, true);
1216   config->ltoDebugPassManager = args.hasArg(OPT_lto_debug_pass_manager);
1217   config->ltoEmitAsm = args.hasArg(OPT_lto_emit_asm);
1218   config->ltoNewPmPasses = args.getLastArgValue(OPT_lto_newpm_passes);
1219   config->ltoWholeProgramVisibility =
1220       args.hasFlag(OPT_lto_whole_program_visibility,
1221                    OPT_no_lto_whole_program_visibility, false);
1222   config->ltoo = args::getInteger(args, OPT_lto_O, 2);
1223   if (config->ltoo > 3)
1224     error("invalid optimization level for LTO: " + Twine(config->ltoo));
1225   unsigned ltoCgo =
1226       args::getInteger(args, OPT_lto_CGO, args::getCGOptLevel(config->ltoo));
1227   if (auto level = CodeGenOpt::getLevel(ltoCgo))
1228     config->ltoCgo = *level;
1229   else
1230     error("invalid codegen optimization level for LTO: " + Twine(ltoCgo));
1231   config->ltoObjPath = args.getLastArgValue(OPT_lto_obj_path_eq);
1232   config->ltoPartitions = args::getInteger(args, OPT_lto_partitions, 1);
1233   config->ltoSampleProfile = args.getLastArgValue(OPT_lto_sample_profile);
1234   config->ltoBasicBlockSections =
1235       args.getLastArgValue(OPT_lto_basic_block_sections);
1236   config->ltoUniqueBasicBlockSectionNames =
1237       args.hasFlag(OPT_lto_unique_basic_block_section_names,
1238                    OPT_no_lto_unique_basic_block_section_names, false);
1239   config->mapFile = args.getLastArgValue(OPT_Map);
1240   config->mipsGotSize = args::getInteger(args, OPT_mips_got_size, 0xfff0);
1241   config->mergeArmExidx =
1242       args.hasFlag(OPT_merge_exidx_entries, OPT_no_merge_exidx_entries, true);
1243   config->mmapOutputFile =
1244       args.hasFlag(OPT_mmap_output_file, OPT_no_mmap_output_file, true);
1245   config->nmagic = args.hasFlag(OPT_nmagic, OPT_no_nmagic, false);
1246   config->noinhibitExec = args.hasArg(OPT_noinhibit_exec);
1247   config->nostdlib = args.hasArg(OPT_nostdlib);
1248   config->oFormatBinary = isOutputFormatBinary(args);
1249   config->omagic = args.hasFlag(OPT_omagic, OPT_no_omagic, false);
1250   config->optRemarksFilename = args.getLastArgValue(OPT_opt_remarks_filename);
1251   config->optStatsFilename = args.getLastArgValue(OPT_plugin_opt_stats_file);
1252 
1253   // Parse remarks hotness threshold. Valid value is either integer or 'auto'.
1254   if (auto *arg = args.getLastArg(OPT_opt_remarks_hotness_threshold)) {
1255     auto resultOrErr = remarks::parseHotnessThresholdOption(arg->getValue());
1256     if (!resultOrErr)
1257       error(arg->getSpelling() + ": invalid argument '" + arg->getValue() +
1258             "', only integer or 'auto' is supported");
1259     else
1260       config->optRemarksHotnessThreshold = *resultOrErr;
1261   }
1262 
1263   config->optRemarksPasses = args.getLastArgValue(OPT_opt_remarks_passes);
1264   config->optRemarksWithHotness = args.hasArg(OPT_opt_remarks_with_hotness);
1265   config->optRemarksFormat = args.getLastArgValue(OPT_opt_remarks_format);
1266   config->optimize = args::getInteger(args, OPT_O, 1);
1267   config->orphanHandling = getOrphanHandling(args);
1268   config->outputFile = args.getLastArgValue(OPT_o);
1269   config->packageMetadata = args.getLastArgValue(OPT_package_metadata);
1270   config->pie = args.hasFlag(OPT_pie, OPT_no_pie, false);
1271   config->printIcfSections =
1272       args.hasFlag(OPT_print_icf_sections, OPT_no_print_icf_sections, false);
1273   config->printGcSections =
1274       args.hasFlag(OPT_print_gc_sections, OPT_no_print_gc_sections, false);
1275   config->printMemoryUsage = args.hasArg(OPT_print_memory_usage);
1276   config->printArchiveStats = args.getLastArgValue(OPT_print_archive_stats);
1277   config->printSymbolOrder =
1278       args.getLastArgValue(OPT_print_symbol_order);
1279   config->relax = args.hasFlag(OPT_relax, OPT_no_relax, true);
1280   config->relaxGP = args.hasFlag(OPT_relax_gp, OPT_no_relax_gp, false);
1281   config->rpath = getRpath(args);
1282   config->relocatable = args.hasArg(OPT_relocatable);
1283 
1284   if (args.hasArg(OPT_save_temps)) {
1285     // --save-temps implies saving all temps.
1286     for (const char *s : saveTempsValues)
1287       config->saveTempsArgs.insert(s);
1288   } else {
1289     for (auto *arg : args.filtered(OPT_save_temps_eq)) {
1290       StringRef s = arg->getValue();
1291       if (llvm::is_contained(saveTempsValues, s))
1292         config->saveTempsArgs.insert(s);
1293       else
1294         error("unknown --save-temps value: " + s);
1295     }
1296   }
1297 
1298   config->searchPaths = args::getStrings(args, OPT_library_path);
1299   config->sectionStartMap = getSectionStartMap(args);
1300   config->shared = args.hasArg(OPT_shared);
1301   config->singleRoRx = !args.hasFlag(OPT_rosegment, OPT_no_rosegment, true);
1302   config->soName = args.getLastArgValue(OPT_soname);
1303   config->sortSection = getSortSection(args);
1304   config->splitStackAdjustSize = args::getInteger(args, OPT_split_stack_adjust_size, 16384);
1305   config->strip = getStrip(args);
1306   config->sysroot = args.getLastArgValue(OPT_sysroot);
1307   config->target1Rel = args.hasFlag(OPT_target1_rel, OPT_target1_abs, false);
1308   config->target2 = getTarget2(args);
1309   config->thinLTOCacheDir = args.getLastArgValue(OPT_thinlto_cache_dir);
1310   config->thinLTOCachePolicy = CHECK(
1311       parseCachePruningPolicy(args.getLastArgValue(OPT_thinlto_cache_policy)),
1312       "--thinlto-cache-policy: invalid cache policy");
1313   config->thinLTOEmitImportsFiles = args.hasArg(OPT_thinlto_emit_imports_files);
1314   config->thinLTOEmitIndexFiles = args.hasArg(OPT_thinlto_emit_index_files) ||
1315                                   args.hasArg(OPT_thinlto_index_only) ||
1316                                   args.hasArg(OPT_thinlto_index_only_eq);
1317   config->thinLTOIndexOnly = args.hasArg(OPT_thinlto_index_only) ||
1318                              args.hasArg(OPT_thinlto_index_only_eq);
1319   config->thinLTOIndexOnlyArg = args.getLastArgValue(OPT_thinlto_index_only_eq);
1320   config->thinLTOObjectSuffixReplace =
1321       getOldNewOptions(args, OPT_thinlto_object_suffix_replace_eq);
1322   std::tie(config->thinLTOPrefixReplaceOld, config->thinLTOPrefixReplaceNew,
1323            config->thinLTOPrefixReplaceNativeObject) =
1324       getOldNewOptionsExtra(args, OPT_thinlto_prefix_replace_eq);
1325   if (config->thinLTOEmitIndexFiles && !config->thinLTOIndexOnly) {
1326     if (args.hasArg(OPT_thinlto_object_suffix_replace_eq))
1327       error("--thinlto-object-suffix-replace is not supported with "
1328             "--thinlto-emit-index-files");
1329     else if (args.hasArg(OPT_thinlto_prefix_replace_eq))
1330       error("--thinlto-prefix-replace is not supported with "
1331             "--thinlto-emit-index-files");
1332   }
1333   if (!config->thinLTOPrefixReplaceNativeObject.empty() &&
1334       config->thinLTOIndexOnlyArg.empty()) {
1335     error("--thinlto-prefix-replace=old_dir;new_dir;obj_dir must be used with "
1336           "--thinlto-index-only=");
1337   }
1338   config->thinLTOModulesToCompile =
1339       args::getStrings(args, OPT_thinlto_single_module_eq);
1340   config->timeTraceEnabled = args.hasArg(OPT_time_trace_eq);
1341   config->timeTraceGranularity =
1342       args::getInteger(args, OPT_time_trace_granularity, 500);
1343   config->trace = args.hasArg(OPT_trace);
1344   config->undefined = args::getStrings(args, OPT_undefined);
1345   config->undefinedVersion =
1346       args.hasFlag(OPT_undefined_version, OPT_no_undefined_version, false);
1347   config->unique = args.hasArg(OPT_unique);
1348   config->useAndroidRelrTags = args.hasFlag(
1349       OPT_use_android_relr_tags, OPT_no_use_android_relr_tags, false);
1350   config->warnBackrefs =
1351       args.hasFlag(OPT_warn_backrefs, OPT_no_warn_backrefs, false);
1352   config->warnCommon = args.hasFlag(OPT_warn_common, OPT_no_warn_common, false);
1353   config->warnSymbolOrdering =
1354       args.hasFlag(OPT_warn_symbol_ordering, OPT_no_warn_symbol_ordering, true);
1355   config->whyExtract = args.getLastArgValue(OPT_why_extract);
1356   config->zCombreloc = getZFlag(args, "combreloc", "nocombreloc", true);
1357   config->zCopyreloc = getZFlag(args, "copyreloc", "nocopyreloc", true);
1358   config->zForceBti = hasZOption(args, "force-bti");
1359   config->zForceIbt = hasZOption(args, "force-ibt");
1360   config->zGlobal = hasZOption(args, "global");
1361   config->zGnustack = getZGnuStack(args);
1362   config->zHazardplt = hasZOption(args, "hazardplt");
1363   config->zIfuncNoplt = hasZOption(args, "ifunc-noplt");
1364   config->zInitfirst = hasZOption(args, "initfirst");
1365   config->zInterpose = hasZOption(args, "interpose");
1366   config->zKeepTextSectionPrefix = getZFlag(
1367       args, "keep-text-section-prefix", "nokeep-text-section-prefix", false);
1368   config->zNodefaultlib = hasZOption(args, "nodefaultlib");
1369   config->zNodelete = hasZOption(args, "nodelete");
1370   config->zNodlopen = hasZOption(args, "nodlopen");
1371   config->zNow = getZFlag(args, "now", "lazy", false);
1372   config->zOrigin = hasZOption(args, "origin");
1373   config->zPacPlt = hasZOption(args, "pac-plt");
1374   config->zRelro = getZFlag(args, "relro", "norelro", true);
1375   config->zRetpolineplt = hasZOption(args, "retpolineplt");
1376   config->zRodynamic = hasZOption(args, "rodynamic");
1377   config->zSeparate = getZSeparate(args);
1378   config->zShstk = hasZOption(args, "shstk");
1379   config->zStackSize = args::getZOptionValue(args, OPT_z, "stack-size", 0);
1380   config->zStartStopGC =
1381       getZFlag(args, "start-stop-gc", "nostart-stop-gc", true);
1382   config->zStartStopVisibility = getZStartStopVisibility(args);
1383   config->zText = getZFlag(args, "text", "notext", true);
1384   config->zWxneeded = hasZOption(args, "wxneeded");
1385   setUnresolvedSymbolPolicy(args);
1386   config->power10Stubs = args.getLastArgValue(OPT_power10_stubs_eq) != "no";
1387 
1388   if (opt::Arg *arg = args.getLastArg(OPT_eb, OPT_el)) {
1389     if (arg->getOption().matches(OPT_eb))
1390       config->optEB = true;
1391     else
1392       config->optEL = true;
1393   }
1394 
1395   for (opt::Arg *arg : args.filtered(OPT_remap_inputs)) {
1396     StringRef value(arg->getValue());
1397     remapInputs(value, arg->getSpelling());
1398   }
1399   for (opt::Arg *arg : args.filtered(OPT_remap_inputs_file)) {
1400     StringRef filename(arg->getValue());
1401     std::optional<MemoryBufferRef> buffer = readFile(filename);
1402     if (!buffer)
1403       continue;
1404     // Parse 'from-glob=to-file' lines, ignoring #-led comments.
1405     for (auto [lineno, line] : llvm::enumerate(args::getLines(*buffer)))
1406       if (remapInputs(line, filename + ":" + Twine(lineno + 1)))
1407         break;
1408   }
1409 
1410   for (opt::Arg *arg : args.filtered(OPT_shuffle_sections)) {
1411     constexpr StringRef errPrefix = "--shuffle-sections=: ";
1412     std::pair<StringRef, StringRef> kv = StringRef(arg->getValue()).split('=');
1413     if (kv.first.empty() || kv.second.empty()) {
1414       error(errPrefix + "expected <section_glob>=<seed>, but got '" +
1415             arg->getValue() + "'");
1416       continue;
1417     }
1418     // Signed so that <section_glob>=-1 is allowed.
1419     int64_t v;
1420     if (!to_integer(kv.second, v))
1421       error(errPrefix + "expected an integer, but got '" + kv.second + "'");
1422     else if (Expected<GlobPattern> pat = GlobPattern::create(kv.first))
1423       config->shuffleSections.emplace_back(std::move(*pat), uint32_t(v));
1424     else
1425       error(errPrefix + toString(pat.takeError()));
1426   }
1427 
1428   auto reports = {std::make_pair("bti-report", &config->zBtiReport),
1429                   std::make_pair("cet-report", &config->zCetReport)};
1430   for (opt::Arg *arg : args.filtered(OPT_z)) {
1431     std::pair<StringRef, StringRef> option =
1432         StringRef(arg->getValue()).split('=');
1433     for (auto reportArg : reports) {
1434       if (option.first != reportArg.first)
1435         continue;
1436       if (!isValidReportString(option.second)) {
1437         error(Twine("-z ") + reportArg.first + "= parameter " + option.second +
1438               " is not recognized");
1439         continue;
1440       }
1441       *reportArg.second = option.second;
1442     }
1443   }
1444 
1445   for (opt::Arg *arg : args.filtered(OPT_z)) {
1446     std::pair<StringRef, StringRef> option =
1447         StringRef(arg->getValue()).split('=');
1448     if (option.first != "dead-reloc-in-nonalloc")
1449       continue;
1450     constexpr StringRef errPrefix = "-z dead-reloc-in-nonalloc=: ";
1451     std::pair<StringRef, StringRef> kv = option.second.split('=');
1452     if (kv.first.empty() || kv.second.empty()) {
1453       error(errPrefix + "expected <section_glob>=<value>");
1454       continue;
1455     }
1456     uint64_t v;
1457     if (!to_integer(kv.second, v))
1458       error(errPrefix + "expected a non-negative integer, but got '" +
1459             kv.second + "'");
1460     else if (Expected<GlobPattern> pat = GlobPattern::create(kv.first))
1461       config->deadRelocInNonAlloc.emplace_back(std::move(*pat), v);
1462     else
1463       error(errPrefix + toString(pat.takeError()));
1464   }
1465 
1466   cl::ResetAllOptionOccurrences();
1467 
1468   // Parse LTO options.
1469   if (auto *arg = args.getLastArg(OPT_plugin_opt_mcpu_eq))
1470     parseClangOption(saver().save("-mcpu=" + StringRef(arg->getValue())),
1471                      arg->getSpelling());
1472 
1473   for (opt::Arg *arg : args.filtered(OPT_plugin_opt_eq_minus))
1474     parseClangOption(std::string("-") + arg->getValue(), arg->getSpelling());
1475 
1476   // GCC collect2 passes -plugin-opt=path/to/lto-wrapper with an absolute or
1477   // relative path. Just ignore. If not ended with "lto-wrapper" (or
1478   // "lto-wrapper.exe" for GCC cross-compiled for Windows), consider it an
1479   // unsupported LLVMgold.so option and error.
1480   for (opt::Arg *arg : args.filtered(OPT_plugin_opt_eq)) {
1481     StringRef v(arg->getValue());
1482     if (!v.ends_with("lto-wrapper") && !v.ends_with("lto-wrapper.exe"))
1483       error(arg->getSpelling() + ": unknown plugin option '" + arg->getValue() +
1484             "'");
1485   }
1486 
1487   config->passPlugins = args::getStrings(args, OPT_load_pass_plugins);
1488 
1489   // Parse -mllvm options.
1490   for (const auto *arg : args.filtered(OPT_mllvm)) {
1491     parseClangOption(arg->getValue(), arg->getSpelling());
1492     config->mllvmOpts.emplace_back(arg->getValue());
1493   }
1494 
1495   config->ltoKind = LtoKind::Default;
1496   if (auto *arg = args.getLastArg(OPT_lto)) {
1497     StringRef s = arg->getValue();
1498     if (s == "thin")
1499       config->ltoKind = LtoKind::UnifiedThin;
1500     else if (s == "full")
1501       config->ltoKind = LtoKind::UnifiedRegular;
1502     else if (s == "default")
1503       config->ltoKind = LtoKind::Default;
1504     else
1505       error("unknown LTO mode: " + s);
1506   }
1507 
1508   // --threads= takes a positive integer and provides the default value for
1509   // --thinlto-jobs=. If unspecified, cap the number of threads since
1510   // overhead outweighs optimization for used parallel algorithms for the
1511   // non-LTO parts.
1512   if (auto *arg = args.getLastArg(OPT_threads)) {
1513     StringRef v(arg->getValue());
1514     unsigned threads = 0;
1515     if (!llvm::to_integer(v, threads, 0) || threads == 0)
1516       error(arg->getSpelling() + ": expected a positive integer, but got '" +
1517             arg->getValue() + "'");
1518     parallel::strategy = hardware_concurrency(threads);
1519     config->thinLTOJobs = v;
1520   } else if (parallel::strategy.compute_thread_count() > 16) {
1521     log("set maximum concurrency to 16, specify --threads= to change");
1522     parallel::strategy = hardware_concurrency(16);
1523   }
1524   if (auto *arg = args.getLastArg(OPT_thinlto_jobs_eq))
1525     config->thinLTOJobs = arg->getValue();
1526   config->threadCount = parallel::strategy.compute_thread_count();
1527 
1528   if (config->ltoPartitions == 0)
1529     error("--lto-partitions: number of threads must be > 0");
1530   if (!get_threadpool_strategy(config->thinLTOJobs))
1531     error("--thinlto-jobs: invalid job count: " + config->thinLTOJobs);
1532 
1533   if (config->splitStackAdjustSize < 0)
1534     error("--split-stack-adjust-size: size must be >= 0");
1535 
1536   // The text segment is traditionally the first segment, whose address equals
1537   // the base address. However, lld places the R PT_LOAD first. -Ttext-segment
1538   // is an old-fashioned option that does not play well with lld's layout.
1539   // Suggest --image-base as a likely alternative.
1540   if (args.hasArg(OPT_Ttext_segment))
1541     error("-Ttext-segment is not supported. Use --image-base if you "
1542           "intend to set the base address");
1543 
1544   // Parse ELF{32,64}{LE,BE} and CPU type.
1545   if (auto *arg = args.getLastArg(OPT_m)) {
1546     StringRef s = arg->getValue();
1547     std::tie(config->ekind, config->emachine, config->osabi) =
1548         parseEmulation(s);
1549     config->mipsN32Abi =
1550         (s.starts_with("elf32btsmipn32") || s.starts_with("elf32ltsmipn32"));
1551     config->emulation = s;
1552   }
1553 
1554   // Parse --hash-style={sysv,gnu,both}.
1555   if (auto *arg = args.getLastArg(OPT_hash_style)) {
1556     StringRef s = arg->getValue();
1557     if (s == "sysv")
1558       config->sysvHash = true;
1559     else if (s == "gnu")
1560       config->gnuHash = true;
1561     else if (s == "both")
1562       config->sysvHash = config->gnuHash = true;
1563     else
1564       error("unknown --hash-style: " + s);
1565   }
1566 
1567   if (args.hasArg(OPT_print_map))
1568     config->mapFile = "-";
1569 
1570   // Page alignment can be disabled by the -n (--nmagic) and -N (--omagic).
1571   // As PT_GNU_RELRO relies on Paging, do not create it when we have disabled
1572   // it.
1573   if (config->nmagic || config->omagic)
1574     config->zRelro = false;
1575 
1576   std::tie(config->buildId, config->buildIdVector) = getBuildId(args);
1577 
1578   if (getZFlag(args, "pack-relative-relocs", "nopack-relative-relocs", false)) {
1579     config->relrGlibc = true;
1580     config->relrPackDynRelocs = true;
1581   } else {
1582     std::tie(config->androidPackDynRelocs, config->relrPackDynRelocs) =
1583         getPackDynRelocs(args);
1584   }
1585 
1586   if (auto *arg = args.getLastArg(OPT_symbol_ordering_file)){
1587     if (args.hasArg(OPT_call_graph_ordering_file))
1588       error("--symbol-ordering-file and --call-graph-order-file "
1589             "may not be used together");
1590     if (std::optional<MemoryBufferRef> buffer = readFile(arg->getValue())) {
1591       config->symbolOrderingFile = getSymbolOrderingFile(*buffer);
1592       // Also need to disable CallGraphProfileSort to prevent
1593       // LLD order symbols with CGProfile
1594       config->callGraphProfileSort = false;
1595     }
1596   }
1597 
1598   assert(config->versionDefinitions.empty());
1599   config->versionDefinitions.push_back(
1600       {"local", (uint16_t)VER_NDX_LOCAL, {}, {}});
1601   config->versionDefinitions.push_back(
1602       {"global", (uint16_t)VER_NDX_GLOBAL, {}, {}});
1603 
1604   // If --retain-symbol-file is used, we'll keep only the symbols listed in
1605   // the file and discard all others.
1606   if (auto *arg = args.getLastArg(OPT_retain_symbols_file)) {
1607     config->versionDefinitions[VER_NDX_LOCAL].nonLocalPatterns.push_back(
1608         {"*", /*isExternCpp=*/false, /*hasWildcard=*/true});
1609     if (std::optional<MemoryBufferRef> buffer = readFile(arg->getValue()))
1610       for (StringRef s : args::getLines(*buffer))
1611         config->versionDefinitions[VER_NDX_GLOBAL].nonLocalPatterns.push_back(
1612             {s, /*isExternCpp=*/false, /*hasWildcard=*/false});
1613   }
1614 
1615   for (opt::Arg *arg : args.filtered(OPT_warn_backrefs_exclude)) {
1616     StringRef pattern(arg->getValue());
1617     if (Expected<GlobPattern> pat = GlobPattern::create(pattern))
1618       config->warnBackrefsExclude.push_back(std::move(*pat));
1619     else
1620       error(arg->getSpelling() + ": " + toString(pat.takeError()));
1621   }
1622 
1623   // For -no-pie and -pie, --export-dynamic-symbol specifies defined symbols
1624   // which should be exported. For -shared, references to matched non-local
1625   // STV_DEFAULT symbols are not bound to definitions within the shared object,
1626   // even if other options express a symbolic intention: -Bsymbolic,
1627   // -Bsymbolic-functions (if STT_FUNC), --dynamic-list.
1628   for (auto *arg : args.filtered(OPT_export_dynamic_symbol))
1629     config->dynamicList.push_back(
1630         {arg->getValue(), /*isExternCpp=*/false,
1631          /*hasWildcard=*/hasWildcard(arg->getValue())});
1632 
1633   // --export-dynamic-symbol-list specifies a list of --export-dynamic-symbol
1634   // patterns. --dynamic-list is --export-dynamic-symbol-list plus -Bsymbolic
1635   // like semantics.
1636   config->symbolic =
1637       config->bsymbolic == BsymbolicKind::All || args.hasArg(OPT_dynamic_list);
1638   for (auto *arg :
1639        args.filtered(OPT_dynamic_list, OPT_export_dynamic_symbol_list))
1640     if (std::optional<MemoryBufferRef> buffer = readFile(arg->getValue()))
1641       readDynamicList(*buffer);
1642 
1643   for (auto *arg : args.filtered(OPT_version_script))
1644     if (std::optional<std::string> path = searchScript(arg->getValue())) {
1645       if (std::optional<MemoryBufferRef> buffer = readFile(*path))
1646         readVersionScript(*buffer);
1647     } else {
1648       error(Twine("cannot find version script ") + arg->getValue());
1649     }
1650 }
1651 
1652 // Some Config members do not directly correspond to any particular
1653 // command line options, but computed based on other Config values.
1654 // This function initialize such members. See Config.h for the details
1655 // of these values.
1656 static void setConfigs(opt::InputArgList &args) {
1657   ELFKind k = config->ekind;
1658   uint16_t m = config->emachine;
1659 
1660   config->copyRelocs = (config->relocatable || config->emitRelocs);
1661   config->is64 = (k == ELF64LEKind || k == ELF64BEKind);
1662   config->isLE = (k == ELF32LEKind || k == ELF64LEKind);
1663   config->endianness = config->isLE ? endianness::little : endianness::big;
1664   config->isMips64EL = (k == ELF64LEKind && m == EM_MIPS);
1665   config->isPic = config->pie || config->shared;
1666   config->picThunk = args.hasArg(OPT_pic_veneer, config->isPic);
1667   config->wordsize = config->is64 ? 8 : 4;
1668 
1669   // ELF defines two different ways to store relocation addends as shown below:
1670   //
1671   //  Rel: Addends are stored to the location where relocations are applied. It
1672   //  cannot pack the full range of addend values for all relocation types, but
1673   //  this only affects relocation types that we don't support emitting as
1674   //  dynamic relocations (see getDynRel).
1675   //  Rela: Addends are stored as part of relocation entry.
1676   //
1677   // In other words, Rela makes it easy to read addends at the price of extra
1678   // 4 or 8 byte for each relocation entry.
1679   //
1680   // We pick the format for dynamic relocations according to the psABI for each
1681   // processor, but a contrary choice can be made if the dynamic loader
1682   // supports.
1683   config->isRela = getIsRela(args);
1684 
1685   // If the output uses REL relocations we must store the dynamic relocation
1686   // addends to the output sections. We also store addends for RELA relocations
1687   // if --apply-dynamic-relocs is used.
1688   // We default to not writing the addends when using RELA relocations since
1689   // any standard conforming tool can find it in r_addend.
1690   config->writeAddends = args.hasFlag(OPT_apply_dynamic_relocs,
1691                                       OPT_no_apply_dynamic_relocs, false) ||
1692                          !config->isRela;
1693   // Validation of dynamic relocation addends is on by default for assertions
1694   // builds (for supported targets) and disabled otherwise. Ideally we would
1695   // enable the debug checks for all targets, but currently not all targets
1696   // have support for reading Elf_Rel addends, so we only enable for a subset.
1697 #ifndef NDEBUG
1698   bool checkDynamicRelocsDefault = m == EM_AARCH64 || m == EM_ARM ||
1699                                    m == EM_386 || m == EM_LOONGARCH ||
1700                                    m == EM_MIPS || m == EM_RISCV ||
1701                                    m == EM_X86_64;
1702 #else
1703   bool checkDynamicRelocsDefault = false;
1704 #endif
1705   config->checkDynamicRelocs =
1706       args.hasFlag(OPT_check_dynamic_relocations,
1707                    OPT_no_check_dynamic_relocations, checkDynamicRelocsDefault);
1708   config->tocOptimize =
1709       args.hasFlag(OPT_toc_optimize, OPT_no_toc_optimize, m == EM_PPC64);
1710   config->pcRelOptimize =
1711       args.hasFlag(OPT_pcrel_optimize, OPT_no_pcrel_optimize, m == EM_PPC64);
1712 }
1713 
1714 static bool isFormatBinary(StringRef s) {
1715   if (s == "binary")
1716     return true;
1717   if (s == "elf" || s == "default")
1718     return false;
1719   error("unknown --format value: " + s +
1720         " (supported formats: elf, default, binary)");
1721   return false;
1722 }
1723 
1724 void LinkerDriver::createFiles(opt::InputArgList &args) {
1725   llvm::TimeTraceScope timeScope("Load input files");
1726   // For --{push,pop}-state.
1727   std::vector<std::tuple<bool, bool, bool>> stack;
1728 
1729   // Iterate over argv to process input files and positional arguments.
1730   InputFile::isInGroup = false;
1731   bool hasInput = false;
1732   for (auto *arg : args) {
1733     switch (arg->getOption().getID()) {
1734     case OPT_library:
1735       addLibrary(arg->getValue());
1736       hasInput = true;
1737       break;
1738     case OPT_INPUT:
1739       addFile(arg->getValue(), /*withLOption=*/false);
1740       hasInput = true;
1741       break;
1742     case OPT_defsym: {
1743       StringRef from;
1744       StringRef to;
1745       std::tie(from, to) = StringRef(arg->getValue()).split('=');
1746       if (from.empty() || to.empty())
1747         error("--defsym: syntax error: " + StringRef(arg->getValue()));
1748       else
1749         readDefsym(from, MemoryBufferRef(to, "--defsym"));
1750       break;
1751     }
1752     case OPT_script:
1753       if (std::optional<std::string> path = searchScript(arg->getValue())) {
1754         if (std::optional<MemoryBufferRef> mb = readFile(*path))
1755           readLinkerScript(*mb);
1756         break;
1757       }
1758       error(Twine("cannot find linker script ") + arg->getValue());
1759       break;
1760     case OPT_as_needed:
1761       config->asNeeded = true;
1762       break;
1763     case OPT_format:
1764       config->formatBinary = isFormatBinary(arg->getValue());
1765       break;
1766     case OPT_no_as_needed:
1767       config->asNeeded = false;
1768       break;
1769     case OPT_Bstatic:
1770     case OPT_omagic:
1771     case OPT_nmagic:
1772       config->isStatic = true;
1773       break;
1774     case OPT_Bdynamic:
1775       config->isStatic = false;
1776       break;
1777     case OPT_whole_archive:
1778       inWholeArchive = true;
1779       break;
1780     case OPT_no_whole_archive:
1781       inWholeArchive = false;
1782       break;
1783     case OPT_just_symbols:
1784       if (std::optional<MemoryBufferRef> mb = readFile(arg->getValue())) {
1785         files.push_back(createObjFile(*mb));
1786         files.back()->justSymbols = true;
1787       }
1788       break;
1789     case OPT_in_implib:
1790       if (armCmseImpLib)
1791         error("multiple CMSE import libraries not supported");
1792       else if (std::optional<MemoryBufferRef> mb = readFile(arg->getValue()))
1793         armCmseImpLib = createObjFile(*mb);
1794       break;
1795     case OPT_start_group:
1796       if (InputFile::isInGroup)
1797         error("nested --start-group");
1798       InputFile::isInGroup = true;
1799       break;
1800     case OPT_end_group:
1801       if (!InputFile::isInGroup)
1802         error("stray --end-group");
1803       InputFile::isInGroup = false;
1804       ++InputFile::nextGroupId;
1805       break;
1806     case OPT_start_lib:
1807       if (inLib)
1808         error("nested --start-lib");
1809       if (InputFile::isInGroup)
1810         error("may not nest --start-lib in --start-group");
1811       inLib = true;
1812       InputFile::isInGroup = true;
1813       break;
1814     case OPT_end_lib:
1815       if (!inLib)
1816         error("stray --end-lib");
1817       inLib = false;
1818       InputFile::isInGroup = false;
1819       ++InputFile::nextGroupId;
1820       break;
1821     case OPT_push_state:
1822       stack.emplace_back(config->asNeeded, config->isStatic, inWholeArchive);
1823       break;
1824     case OPT_pop_state:
1825       if (stack.empty()) {
1826         error("unbalanced --push-state/--pop-state");
1827         break;
1828       }
1829       std::tie(config->asNeeded, config->isStatic, inWholeArchive) = stack.back();
1830       stack.pop_back();
1831       break;
1832     }
1833   }
1834 
1835   if (files.empty() && !hasInput && errorCount() == 0)
1836     error("no input files");
1837 }
1838 
1839 // If -m <machine_type> was not given, infer it from object files.
1840 void LinkerDriver::inferMachineType() {
1841   if (config->ekind != ELFNoneKind)
1842     return;
1843 
1844   for (InputFile *f : files) {
1845     if (f->ekind == ELFNoneKind)
1846       continue;
1847     config->ekind = f->ekind;
1848     config->emachine = f->emachine;
1849     config->osabi = f->osabi;
1850     config->mipsN32Abi = config->emachine == EM_MIPS && isMipsN32Abi(f);
1851     return;
1852   }
1853   error("target emulation unknown: -m or at least one .o file required");
1854 }
1855 
1856 // Parse -z max-page-size=<value>. The default value is defined by
1857 // each target.
1858 static uint64_t getMaxPageSize(opt::InputArgList &args) {
1859   uint64_t val = args::getZOptionValue(args, OPT_z, "max-page-size",
1860                                        target->defaultMaxPageSize);
1861   if (!isPowerOf2_64(val)) {
1862     error("max-page-size: value isn't a power of 2");
1863     return target->defaultMaxPageSize;
1864   }
1865   if (config->nmagic || config->omagic) {
1866     if (val != target->defaultMaxPageSize)
1867       warn("-z max-page-size set, but paging disabled by omagic or nmagic");
1868     return 1;
1869   }
1870   return val;
1871 }
1872 
1873 // Parse -z common-page-size=<value>. The default value is defined by
1874 // each target.
1875 static uint64_t getCommonPageSize(opt::InputArgList &args) {
1876   uint64_t val = args::getZOptionValue(args, OPT_z, "common-page-size",
1877                                        target->defaultCommonPageSize);
1878   if (!isPowerOf2_64(val)) {
1879     error("common-page-size: value isn't a power of 2");
1880     return target->defaultCommonPageSize;
1881   }
1882   if (config->nmagic || config->omagic) {
1883     if (val != target->defaultCommonPageSize)
1884       warn("-z common-page-size set, but paging disabled by omagic or nmagic");
1885     return 1;
1886   }
1887   // commonPageSize can't be larger than maxPageSize.
1888   if (val > config->maxPageSize)
1889     val = config->maxPageSize;
1890   return val;
1891 }
1892 
1893 // Parses --image-base option.
1894 static std::optional<uint64_t> getImageBase(opt::InputArgList &args) {
1895   // Because we are using "Config->maxPageSize" here, this function has to be
1896   // called after the variable is initialized.
1897   auto *arg = args.getLastArg(OPT_image_base);
1898   if (!arg)
1899     return std::nullopt;
1900 
1901   StringRef s = arg->getValue();
1902   uint64_t v;
1903   if (!to_integer(s, v)) {
1904     error("--image-base: number expected, but got " + s);
1905     return 0;
1906   }
1907   if ((v % config->maxPageSize) != 0)
1908     warn("--image-base: address isn't multiple of page size: " + s);
1909   return v;
1910 }
1911 
1912 // Parses `--exclude-libs=lib,lib,...`.
1913 // The library names may be delimited by commas or colons.
1914 static DenseSet<StringRef> getExcludeLibs(opt::InputArgList &args) {
1915   DenseSet<StringRef> ret;
1916   for (auto *arg : args.filtered(OPT_exclude_libs)) {
1917     StringRef s = arg->getValue();
1918     for (;;) {
1919       size_t pos = s.find_first_of(",:");
1920       if (pos == StringRef::npos)
1921         break;
1922       ret.insert(s.substr(0, pos));
1923       s = s.substr(pos + 1);
1924     }
1925     ret.insert(s);
1926   }
1927   return ret;
1928 }
1929 
1930 // Handles the --exclude-libs option. If a static library file is specified
1931 // by the --exclude-libs option, all public symbols from the archive become
1932 // private unless otherwise specified by version scripts or something.
1933 // A special library name "ALL" means all archive files.
1934 //
1935 // This is not a popular option, but some programs such as bionic libc use it.
1936 static void excludeLibs(opt::InputArgList &args) {
1937   DenseSet<StringRef> libs = getExcludeLibs(args);
1938   bool all = libs.count("ALL");
1939 
1940   auto visit = [&](InputFile *file) {
1941     if (file->archiveName.empty() ||
1942         !(all || libs.count(path::filename(file->archiveName))))
1943       return;
1944     ArrayRef<Symbol *> symbols = file->getSymbols();
1945     if (isa<ELFFileBase>(file))
1946       symbols = cast<ELFFileBase>(file)->getGlobalSymbols();
1947     for (Symbol *sym : symbols)
1948       if (!sym->isUndefined() && sym->file == file)
1949         sym->versionId = VER_NDX_LOCAL;
1950   };
1951 
1952   for (ELFFileBase *file : ctx.objectFiles)
1953     visit(file);
1954 
1955   for (BitcodeFile *file : ctx.bitcodeFiles)
1956     visit(file);
1957 }
1958 
1959 // Force Sym to be entered in the output.
1960 static void handleUndefined(Symbol *sym, const char *option) {
1961   // Since a symbol may not be used inside the program, LTO may
1962   // eliminate it. Mark the symbol as "used" to prevent it.
1963   sym->isUsedInRegularObj = true;
1964 
1965   if (!sym->isLazy())
1966     return;
1967   sym->extract();
1968   if (!config->whyExtract.empty())
1969     ctx.whyExtractRecords.emplace_back(option, sym->file, *sym);
1970 }
1971 
1972 // As an extension to GNU linkers, lld supports a variant of `-u`
1973 // which accepts wildcard patterns. All symbols that match a given
1974 // pattern are handled as if they were given by `-u`.
1975 static void handleUndefinedGlob(StringRef arg) {
1976   Expected<GlobPattern> pat = GlobPattern::create(arg);
1977   if (!pat) {
1978     error("--undefined-glob: " + toString(pat.takeError()));
1979     return;
1980   }
1981 
1982   // Calling sym->extract() in the loop is not safe because it may add new
1983   // symbols to the symbol table, invalidating the current iterator.
1984   SmallVector<Symbol *, 0> syms;
1985   for (Symbol *sym : symtab.getSymbols())
1986     if (!sym->isPlaceholder() && pat->match(sym->getName()))
1987       syms.push_back(sym);
1988 
1989   for (Symbol *sym : syms)
1990     handleUndefined(sym, "--undefined-glob");
1991 }
1992 
1993 static void handleLibcall(StringRef name) {
1994   Symbol *sym = symtab.find(name);
1995   if (!sym || !sym->isLazy())
1996     return;
1997 
1998   MemoryBufferRef mb;
1999   mb = cast<LazyObject>(sym)->file->mb;
2000 
2001   if (isBitcode(mb))
2002     sym->extract();
2003 }
2004 
2005 static void writeArchiveStats() {
2006   if (config->printArchiveStats.empty())
2007     return;
2008 
2009   std::error_code ec;
2010   raw_fd_ostream os = ctx.openAuxiliaryFile(config->printArchiveStats, ec);
2011   if (ec) {
2012     error("--print-archive-stats=: cannot open " + config->printArchiveStats +
2013           ": " + ec.message());
2014     return;
2015   }
2016 
2017   os << "members\textracted\tarchive\n";
2018 
2019   SmallVector<StringRef, 0> archives;
2020   DenseMap<CachedHashStringRef, unsigned> all, extracted;
2021   for (ELFFileBase *file : ctx.objectFiles)
2022     if (file->archiveName.size())
2023       ++extracted[CachedHashStringRef(file->archiveName)];
2024   for (BitcodeFile *file : ctx.bitcodeFiles)
2025     if (file->archiveName.size())
2026       ++extracted[CachedHashStringRef(file->archiveName)];
2027   for (std::pair<StringRef, unsigned> f : ctx.driver.archiveFiles) {
2028     unsigned &v = extracted[CachedHashString(f.first)];
2029     os << f.second << '\t' << v << '\t' << f.first << '\n';
2030     // If the archive occurs multiple times, other instances have a count of 0.
2031     v = 0;
2032   }
2033 }
2034 
2035 static void writeWhyExtract() {
2036   if (config->whyExtract.empty())
2037     return;
2038 
2039   std::error_code ec;
2040   raw_fd_ostream os = ctx.openAuxiliaryFile(config->whyExtract, ec);
2041   if (ec) {
2042     error("cannot open --why-extract= file " + config->whyExtract + ": " +
2043           ec.message());
2044     return;
2045   }
2046 
2047   os << "reference\textracted\tsymbol\n";
2048   for (auto &entry : ctx.whyExtractRecords) {
2049     os << std::get<0>(entry) << '\t' << toString(std::get<1>(entry)) << '\t'
2050        << toString(std::get<2>(entry)) << '\n';
2051   }
2052 }
2053 
2054 static void reportBackrefs() {
2055   for (auto &ref : ctx.backwardReferences) {
2056     const Symbol &sym = *ref.first;
2057     std::string to = toString(ref.second.second);
2058     // Some libraries have known problems and can cause noise. Filter them out
2059     // with --warn-backrefs-exclude=. The value may look like (for --start-lib)
2060     // *.o or (archive member) *.a(*.o).
2061     bool exclude = false;
2062     for (const llvm::GlobPattern &pat : config->warnBackrefsExclude)
2063       if (pat.match(to)) {
2064         exclude = true;
2065         break;
2066       }
2067     if (!exclude)
2068       warn("backward reference detected: " + sym.getName() + " in " +
2069            toString(ref.second.first) + " refers to " + to);
2070   }
2071 }
2072 
2073 // Handle --dependency-file=<path>. If that option is given, lld creates a
2074 // file at a given path with the following contents:
2075 //
2076 //   <output-file>: <input-file> ...
2077 //
2078 //   <input-file>:
2079 //
2080 // where <output-file> is a pathname of an output file and <input-file>
2081 // ... is a list of pathnames of all input files. `make` command can read a
2082 // file in the above format and interpret it as a dependency info. We write
2083 // phony targets for every <input-file> to avoid an error when that file is
2084 // removed.
2085 //
2086 // This option is useful if you want to make your final executable to depend
2087 // on all input files including system libraries. Here is why.
2088 //
2089 // When you write a Makefile, you usually write it so that the final
2090 // executable depends on all user-generated object files. Normally, you
2091 // don't make your executable to depend on system libraries (such as libc)
2092 // because you don't know the exact paths of libraries, even though system
2093 // libraries that are linked to your executable statically are technically a
2094 // part of your program. By using --dependency-file option, you can make
2095 // lld to dump dependency info so that you can maintain exact dependencies
2096 // easily.
2097 static void writeDependencyFile() {
2098   std::error_code ec;
2099   raw_fd_ostream os = ctx.openAuxiliaryFile(config->dependencyFile, ec);
2100   if (ec) {
2101     error("cannot open " + config->dependencyFile + ": " + ec.message());
2102     return;
2103   }
2104 
2105   // We use the same escape rules as Clang/GCC which are accepted by Make/Ninja:
2106   // * A space is escaped by a backslash which itself must be escaped.
2107   // * A hash sign is escaped by a single backslash.
2108   // * $ is escapes as $$.
2109   auto printFilename = [](raw_fd_ostream &os, StringRef filename) {
2110     llvm::SmallString<256> nativePath;
2111     llvm::sys::path::native(filename.str(), nativePath);
2112     llvm::sys::path::remove_dots(nativePath, /*remove_dot_dot=*/true);
2113     for (unsigned i = 0, e = nativePath.size(); i != e; ++i) {
2114       if (nativePath[i] == '#') {
2115         os << '\\';
2116       } else if (nativePath[i] == ' ') {
2117         os << '\\';
2118         unsigned j = i;
2119         while (j > 0 && nativePath[--j] == '\\')
2120           os << '\\';
2121       } else if (nativePath[i] == '$') {
2122         os << '$';
2123       }
2124       os << nativePath[i];
2125     }
2126   };
2127 
2128   os << config->outputFile << ":";
2129   for (StringRef path : config->dependencyFiles) {
2130     os << " \\\n ";
2131     printFilename(os, path);
2132   }
2133   os << "\n";
2134 
2135   for (StringRef path : config->dependencyFiles) {
2136     os << "\n";
2137     printFilename(os, path);
2138     os << ":\n";
2139   }
2140 }
2141 
2142 // Replaces common symbols with defined symbols reside in .bss sections.
2143 // This function is called after all symbol names are resolved. As a
2144 // result, the passes after the symbol resolution won't see any
2145 // symbols of type CommonSymbol.
2146 static void replaceCommonSymbols() {
2147   llvm::TimeTraceScope timeScope("Replace common symbols");
2148   for (ELFFileBase *file : ctx.objectFiles) {
2149     if (!file->hasCommonSyms)
2150       continue;
2151     for (Symbol *sym : file->getGlobalSymbols()) {
2152       auto *s = dyn_cast<CommonSymbol>(sym);
2153       if (!s)
2154         continue;
2155 
2156       auto *bss = make<BssSection>("COMMON", s->size, s->alignment);
2157       bss->file = s->file;
2158       ctx.inputSections.push_back(bss);
2159       Defined(s->file, StringRef(), s->binding, s->stOther, s->type,
2160               /*value=*/0, s->size, bss)
2161           .overwrite(*s);
2162     }
2163   }
2164 }
2165 
2166 // If all references to a DSO happen to be weak, the DSO is not added to
2167 // DT_NEEDED. If that happens, replace ShardSymbol with Undefined to avoid
2168 // dangling references to an unneeded DSO. Use a weak binding to avoid
2169 // --no-allow-shlib-undefined diagnostics. Similarly, demote lazy symbols.
2170 static void demoteSharedAndLazySymbols() {
2171   llvm::TimeTraceScope timeScope("Demote shared and lazy symbols");
2172   for (Symbol *sym : symtab.getSymbols()) {
2173     auto *s = dyn_cast<SharedSymbol>(sym);
2174     if (!(s && !cast<SharedFile>(s->file)->isNeeded) && !sym->isLazy())
2175       continue;
2176 
2177     uint8_t binding = sym->isLazy() ? sym->binding : uint8_t(STB_WEAK);
2178     Undefined(nullptr, sym->getName(), binding, sym->stOther, sym->type)
2179         .overwrite(*sym);
2180     sym->versionId = VER_NDX_GLOBAL;
2181   }
2182 }
2183 
2184 // The section referred to by `s` is considered address-significant. Set the
2185 // keepUnique flag on the section if appropriate.
2186 static void markAddrsig(Symbol *s) {
2187   if (auto *d = dyn_cast_or_null<Defined>(s))
2188     if (d->section)
2189       // We don't need to keep text sections unique under --icf=all even if they
2190       // are address-significant.
2191       if (config->icf == ICFLevel::Safe || !(d->section->flags & SHF_EXECINSTR))
2192         d->section->keepUnique = true;
2193 }
2194 
2195 // Record sections that define symbols mentioned in --keep-unique <symbol>
2196 // and symbols referred to by address-significance tables. These sections are
2197 // ineligible for ICF.
2198 template <class ELFT>
2199 static void findKeepUniqueSections(opt::InputArgList &args) {
2200   for (auto *arg : args.filtered(OPT_keep_unique)) {
2201     StringRef name = arg->getValue();
2202     auto *d = dyn_cast_or_null<Defined>(symtab.find(name));
2203     if (!d || !d->section) {
2204       warn("could not find symbol " + name + " to keep unique");
2205       continue;
2206     }
2207     d->section->keepUnique = true;
2208   }
2209 
2210   // --icf=all --ignore-data-address-equality means that we can ignore
2211   // the dynsym and address-significance tables entirely.
2212   if (config->icf == ICFLevel::All && config->ignoreDataAddressEquality)
2213     return;
2214 
2215   // Symbols in the dynsym could be address-significant in other executables
2216   // or DSOs, so we conservatively mark them as address-significant.
2217   for (Symbol *sym : symtab.getSymbols())
2218     if (sym->includeInDynsym())
2219       markAddrsig(sym);
2220 
2221   // Visit the address-significance table in each object file and mark each
2222   // referenced symbol as address-significant.
2223   for (InputFile *f : ctx.objectFiles) {
2224     auto *obj = cast<ObjFile<ELFT>>(f);
2225     ArrayRef<Symbol *> syms = obj->getSymbols();
2226     if (obj->addrsigSec) {
2227       ArrayRef<uint8_t> contents =
2228           check(obj->getObj().getSectionContents(*obj->addrsigSec));
2229       const uint8_t *cur = contents.begin();
2230       while (cur != contents.end()) {
2231         unsigned size;
2232         const char *err;
2233         uint64_t symIndex = decodeULEB128(cur, &size, contents.end(), &err);
2234         if (err)
2235           fatal(toString(f) + ": could not decode addrsig section: " + err);
2236         markAddrsig(syms[symIndex]);
2237         cur += size;
2238       }
2239     } else {
2240       // If an object file does not have an address-significance table,
2241       // conservatively mark all of its symbols as address-significant.
2242       for (Symbol *s : syms)
2243         markAddrsig(s);
2244     }
2245   }
2246 }
2247 
2248 // This function reads a symbol partition specification section. These sections
2249 // are used to control which partition a symbol is allocated to. See
2250 // https://lld.llvm.org/Partitions.html for more details on partitions.
2251 template <typename ELFT>
2252 static void readSymbolPartitionSection(InputSectionBase *s) {
2253   // Read the relocation that refers to the partition's entry point symbol.
2254   Symbol *sym;
2255   const RelsOrRelas<ELFT> rels = s->template relsOrRelas<ELFT>();
2256   if (rels.areRelocsRel())
2257     sym = &s->getFile<ELFT>()->getRelocTargetSym(rels.rels[0]);
2258   else
2259     sym = &s->getFile<ELFT>()->getRelocTargetSym(rels.relas[0]);
2260   if (!isa<Defined>(sym) || !sym->includeInDynsym())
2261     return;
2262 
2263   StringRef partName = reinterpret_cast<const char *>(s->content().data());
2264   for (Partition &part : partitions) {
2265     if (part.name == partName) {
2266       sym->partition = part.getNumber();
2267       return;
2268     }
2269   }
2270 
2271   // Forbid partitions from being used on incompatible targets, and forbid them
2272   // from being used together with various linker features that assume a single
2273   // set of output sections.
2274   if (script->hasSectionsCommand)
2275     error(toString(s->file) +
2276           ": partitions cannot be used with the SECTIONS command");
2277   if (script->hasPhdrsCommands())
2278     error(toString(s->file) +
2279           ": partitions cannot be used with the PHDRS command");
2280   if (!config->sectionStartMap.empty())
2281     error(toString(s->file) + ": partitions cannot be used with "
2282                               "--section-start, -Ttext, -Tdata or -Tbss");
2283   if (config->emachine == EM_MIPS)
2284     error(toString(s->file) + ": partitions cannot be used on this target");
2285 
2286   // Impose a limit of no more than 254 partitions. This limit comes from the
2287   // sizes of the Partition fields in InputSectionBase and Symbol, as well as
2288   // the amount of space devoted to the partition number in RankFlags.
2289   if (partitions.size() == 254)
2290     fatal("may not have more than 254 partitions");
2291 
2292   partitions.emplace_back();
2293   Partition &newPart = partitions.back();
2294   newPart.name = partName;
2295   sym->partition = newPart.getNumber();
2296 }
2297 
2298 static Symbol *addUnusedUndefined(StringRef name,
2299                                   uint8_t binding = STB_GLOBAL) {
2300   return symtab.addSymbol(Undefined{nullptr, name, binding, STV_DEFAULT, 0});
2301 }
2302 
2303 static void markBuffersAsDontNeed(bool skipLinkedOutput) {
2304   // With --thinlto-index-only, all buffers are nearly unused from now on
2305   // (except symbol/section names used by infrequent passes). Mark input file
2306   // buffers as MADV_DONTNEED so that these pages can be reused by the expensive
2307   // thin link, saving memory.
2308   if (skipLinkedOutput) {
2309     for (MemoryBuffer &mb : llvm::make_pointee_range(ctx.memoryBuffers))
2310       mb.dontNeedIfMmap();
2311     return;
2312   }
2313 
2314   // Otherwise, just mark MemoryBuffers backing BitcodeFiles.
2315   DenseSet<const char *> bufs;
2316   for (BitcodeFile *file : ctx.bitcodeFiles)
2317     bufs.insert(file->mb.getBufferStart());
2318   for (BitcodeFile *file : ctx.lazyBitcodeFiles)
2319     bufs.insert(file->mb.getBufferStart());
2320   for (MemoryBuffer &mb : llvm::make_pointee_range(ctx.memoryBuffers))
2321     if (bufs.count(mb.getBufferStart()))
2322       mb.dontNeedIfMmap();
2323 }
2324 
2325 // This function is where all the optimizations of link-time
2326 // optimization takes place. When LTO is in use, some input files are
2327 // not in native object file format but in the LLVM bitcode format.
2328 // This function compiles bitcode files into a few big native files
2329 // using LLVM functions and replaces bitcode symbols with the results.
2330 // Because all bitcode files that the program consists of are passed to
2331 // the compiler at once, it can do a whole-program optimization.
2332 template <class ELFT>
2333 void LinkerDriver::compileBitcodeFiles(bool skipLinkedOutput) {
2334   llvm::TimeTraceScope timeScope("LTO");
2335   // Compile bitcode files and replace bitcode symbols.
2336   lto.reset(new BitcodeCompiler);
2337   for (BitcodeFile *file : ctx.bitcodeFiles)
2338     lto->add(*file);
2339 
2340   if (!ctx.bitcodeFiles.empty())
2341     markBuffersAsDontNeed(skipLinkedOutput);
2342 
2343   for (InputFile *file : lto->compile()) {
2344     auto *obj = cast<ObjFile<ELFT>>(file);
2345     obj->parse(/*ignoreComdats=*/true);
2346 
2347     // Parse '@' in symbol names for non-relocatable output.
2348     if (!config->relocatable)
2349       for (Symbol *sym : obj->getGlobalSymbols())
2350         if (sym->hasVersionSuffix)
2351           sym->parseSymbolVersion();
2352     ctx.objectFiles.push_back(obj);
2353   }
2354 }
2355 
2356 // The --wrap option is a feature to rename symbols so that you can write
2357 // wrappers for existing functions. If you pass `--wrap=foo`, all
2358 // occurrences of symbol `foo` are resolved to `__wrap_foo` (so, you are
2359 // expected to write `__wrap_foo` function as a wrapper). The original
2360 // symbol becomes accessible as `__real_foo`, so you can call that from your
2361 // wrapper.
2362 //
2363 // This data structure is instantiated for each --wrap option.
2364 struct WrappedSymbol {
2365   Symbol *sym;
2366   Symbol *real;
2367   Symbol *wrap;
2368 };
2369 
2370 // Handles --wrap option.
2371 //
2372 // This function instantiates wrapper symbols. At this point, they seem
2373 // like they are not being used at all, so we explicitly set some flags so
2374 // that LTO won't eliminate them.
2375 static std::vector<WrappedSymbol> addWrappedSymbols(opt::InputArgList &args) {
2376   std::vector<WrappedSymbol> v;
2377   DenseSet<StringRef> seen;
2378 
2379   for (auto *arg : args.filtered(OPT_wrap)) {
2380     StringRef name = arg->getValue();
2381     if (!seen.insert(name).second)
2382       continue;
2383 
2384     Symbol *sym = symtab.find(name);
2385     if (!sym)
2386       continue;
2387 
2388     Symbol *wrap =
2389         addUnusedUndefined(saver().save("__wrap_" + name), sym->binding);
2390 
2391     // If __real_ is referenced, pull in the symbol if it is lazy. Do this after
2392     // processing __wrap_ as that may have referenced __real_.
2393     StringRef realName = saver().save("__real_" + name);
2394     if (symtab.find(realName))
2395       addUnusedUndefined(name, sym->binding);
2396 
2397     Symbol *real = addUnusedUndefined(realName);
2398     v.push_back({sym, real, wrap});
2399 
2400     // We want to tell LTO not to inline symbols to be overwritten
2401     // because LTO doesn't know the final symbol contents after renaming.
2402     real->scriptDefined = true;
2403     sym->scriptDefined = true;
2404 
2405     // If a symbol is referenced in any object file, bitcode file or shared
2406     // object, mark its redirection target (foo for __real_foo and __wrap_foo
2407     // for foo) as referenced after redirection, which will be used to tell LTO
2408     // to not eliminate the redirection target. If the object file defining the
2409     // symbol also references it, we cannot easily distinguish the case from
2410     // cases where the symbol is not referenced. Retain the redirection target
2411     // in this case because we choose to wrap symbol references regardless of
2412     // whether the symbol is defined
2413     // (https://sourceware.org/bugzilla/show_bug.cgi?id=26358).
2414     if (real->referenced || real->isDefined())
2415       sym->referencedAfterWrap = true;
2416     if (sym->referenced || sym->isDefined())
2417       wrap->referencedAfterWrap = true;
2418   }
2419   return v;
2420 }
2421 
2422 static void combineVersionedSymbol(Symbol &sym,
2423                                    DenseMap<Symbol *, Symbol *> &map) {
2424   const char *suffix1 = sym.getVersionSuffix();
2425   if (suffix1[0] != '@' || suffix1[1] == '@')
2426     return;
2427 
2428   // Check the existing symbol foo. We have two special cases to handle:
2429   //
2430   // * There is a definition of foo@v1 and foo@@v1.
2431   // * There is a definition of foo@v1 and foo.
2432   Defined *sym2 = dyn_cast_or_null<Defined>(symtab.find(sym.getName()));
2433   if (!sym2)
2434     return;
2435   const char *suffix2 = sym2->getVersionSuffix();
2436   if (suffix2[0] == '@' && suffix2[1] == '@' &&
2437       strcmp(suffix1 + 1, suffix2 + 2) == 0) {
2438     // foo@v1 and foo@@v1 should be merged, so redirect foo@v1 to foo@@v1.
2439     map.try_emplace(&sym, sym2);
2440     // If both foo@v1 and foo@@v1 are defined and non-weak, report a
2441     // duplicate definition error.
2442     if (sym.isDefined()) {
2443       sym2->checkDuplicate(cast<Defined>(sym));
2444       sym2->resolve(cast<Defined>(sym));
2445     } else if (sym.isUndefined()) {
2446       sym2->resolve(cast<Undefined>(sym));
2447     } else {
2448       sym2->resolve(cast<SharedSymbol>(sym));
2449     }
2450     // Eliminate foo@v1 from the symbol table.
2451     sym.symbolKind = Symbol::PlaceholderKind;
2452     sym.isUsedInRegularObj = false;
2453   } else if (auto *sym1 = dyn_cast<Defined>(&sym)) {
2454     if (sym2->versionId > VER_NDX_GLOBAL
2455             ? config->versionDefinitions[sym2->versionId].name == suffix1 + 1
2456             : sym1->section == sym2->section && sym1->value == sym2->value) {
2457       // Due to an assembler design flaw, if foo is defined, .symver foo,
2458       // foo@v1 defines both foo and foo@v1. Unless foo is bound to a
2459       // different version, GNU ld makes foo@v1 canonical and eliminates
2460       // foo. Emulate its behavior, otherwise we would have foo or foo@@v1
2461       // beside foo@v1. foo@v1 and foo combining does not apply if they are
2462       // not defined in the same place.
2463       map.try_emplace(sym2, &sym);
2464       sym2->symbolKind = Symbol::PlaceholderKind;
2465       sym2->isUsedInRegularObj = false;
2466     }
2467   }
2468 }
2469 
2470 // Do renaming for --wrap and foo@v1 by updating pointers to symbols.
2471 //
2472 // When this function is executed, only InputFiles and symbol table
2473 // contain pointers to symbol objects. We visit them to replace pointers,
2474 // so that wrapped symbols are swapped as instructed by the command line.
2475 static void redirectSymbols(ArrayRef<WrappedSymbol> wrapped) {
2476   llvm::TimeTraceScope timeScope("Redirect symbols");
2477   DenseMap<Symbol *, Symbol *> map;
2478   for (const WrappedSymbol &w : wrapped) {
2479     map[w.sym] = w.wrap;
2480     map[w.real] = w.sym;
2481   }
2482 
2483   // If there are version definitions (versionDefinitions.size() > 2), enumerate
2484   // symbols with a non-default version (foo@v1) and check whether it should be
2485   // combined with foo or foo@@v1.
2486   if (config->versionDefinitions.size() > 2)
2487     for (Symbol *sym : symtab.getSymbols())
2488       if (sym->hasVersionSuffix)
2489         combineVersionedSymbol(*sym, map);
2490 
2491   if (map.empty())
2492     return;
2493 
2494   // Update pointers in input files.
2495   parallelForEach(ctx.objectFiles, [&](ELFFileBase *file) {
2496     for (Symbol *&sym : file->getMutableGlobalSymbols())
2497       if (Symbol *s = map.lookup(sym))
2498         sym = s;
2499   });
2500 
2501   // Update pointers in the symbol table.
2502   for (const WrappedSymbol &w : wrapped)
2503     symtab.wrap(w.sym, w.real, w.wrap);
2504 }
2505 
2506 static void checkAndReportMissingFeature(StringRef config, uint32_t features,
2507                                          uint32_t mask, const Twine &report) {
2508   if (!(features & mask)) {
2509     if (config == "error")
2510       error(report);
2511     else if (config == "warning")
2512       warn(report);
2513   }
2514 }
2515 
2516 // To enable CET (x86's hardware-assisted control flow enforcement), each
2517 // source file must be compiled with -fcf-protection. Object files compiled
2518 // with the flag contain feature flags indicating that they are compatible
2519 // with CET. We enable the feature only when all object files are compatible
2520 // with CET.
2521 //
2522 // This is also the case with AARCH64's BTI and PAC which use the similar
2523 // GNU_PROPERTY_AARCH64_FEATURE_1_AND mechanism.
2524 static uint32_t getAndFeatures() {
2525   if (config->emachine != EM_386 && config->emachine != EM_X86_64 &&
2526       config->emachine != EM_AARCH64)
2527     return 0;
2528 
2529   uint32_t ret = -1;
2530   for (ELFFileBase *f : ctx.objectFiles) {
2531     uint32_t features = f->andFeatures;
2532 
2533     checkAndReportMissingFeature(
2534         config->zBtiReport, features, GNU_PROPERTY_AARCH64_FEATURE_1_BTI,
2535         toString(f) + ": -z bti-report: file does not have "
2536                       "GNU_PROPERTY_AARCH64_FEATURE_1_BTI property");
2537 
2538     checkAndReportMissingFeature(
2539         config->zCetReport, features, GNU_PROPERTY_X86_FEATURE_1_IBT,
2540         toString(f) + ": -z cet-report: file does not have "
2541                       "GNU_PROPERTY_X86_FEATURE_1_IBT property");
2542 
2543     checkAndReportMissingFeature(
2544         config->zCetReport, features, GNU_PROPERTY_X86_FEATURE_1_SHSTK,
2545         toString(f) + ": -z cet-report: file does not have "
2546                       "GNU_PROPERTY_X86_FEATURE_1_SHSTK property");
2547 
2548     if (config->zForceBti && !(features & GNU_PROPERTY_AARCH64_FEATURE_1_BTI)) {
2549       features |= GNU_PROPERTY_AARCH64_FEATURE_1_BTI;
2550       if (config->zBtiReport == "none")
2551         warn(toString(f) + ": -z force-bti: file does not have "
2552                            "GNU_PROPERTY_AARCH64_FEATURE_1_BTI property");
2553     } else if (config->zForceIbt &&
2554                !(features & GNU_PROPERTY_X86_FEATURE_1_IBT)) {
2555       if (config->zCetReport == "none")
2556         warn(toString(f) + ": -z force-ibt: file does not have "
2557                            "GNU_PROPERTY_X86_FEATURE_1_IBT property");
2558       features |= GNU_PROPERTY_X86_FEATURE_1_IBT;
2559     }
2560     if (config->zPacPlt && !(features & GNU_PROPERTY_AARCH64_FEATURE_1_PAC)) {
2561       warn(toString(f) + ": -z pac-plt: file does not have "
2562                          "GNU_PROPERTY_AARCH64_FEATURE_1_PAC property");
2563       features |= GNU_PROPERTY_AARCH64_FEATURE_1_PAC;
2564     }
2565     ret &= features;
2566   }
2567 
2568   // Force enable Shadow Stack.
2569   if (config->zShstk)
2570     ret |= GNU_PROPERTY_X86_FEATURE_1_SHSTK;
2571 
2572   return ret;
2573 }
2574 
2575 static void initSectionsAndLocalSyms(ELFFileBase *file, bool ignoreComdats) {
2576   switch (file->ekind) {
2577   case ELF32LEKind:
2578     cast<ObjFile<ELF32LE>>(file)->initSectionsAndLocalSyms(ignoreComdats);
2579     break;
2580   case ELF32BEKind:
2581     cast<ObjFile<ELF32BE>>(file)->initSectionsAndLocalSyms(ignoreComdats);
2582     break;
2583   case ELF64LEKind:
2584     cast<ObjFile<ELF64LE>>(file)->initSectionsAndLocalSyms(ignoreComdats);
2585     break;
2586   case ELF64BEKind:
2587     cast<ObjFile<ELF64BE>>(file)->initSectionsAndLocalSyms(ignoreComdats);
2588     break;
2589   default:
2590     llvm_unreachable("");
2591   }
2592 }
2593 
2594 static void postParseObjectFile(ELFFileBase *file) {
2595   switch (file->ekind) {
2596   case ELF32LEKind:
2597     cast<ObjFile<ELF32LE>>(file)->postParse();
2598     break;
2599   case ELF32BEKind:
2600     cast<ObjFile<ELF32BE>>(file)->postParse();
2601     break;
2602   case ELF64LEKind:
2603     cast<ObjFile<ELF64LE>>(file)->postParse();
2604     break;
2605   case ELF64BEKind:
2606     cast<ObjFile<ELF64BE>>(file)->postParse();
2607     break;
2608   default:
2609     llvm_unreachable("");
2610   }
2611 }
2612 
2613 // Do actual linking. Note that when this function is called,
2614 // all linker scripts have already been parsed.
2615 void LinkerDriver::link(opt::InputArgList &args) {
2616   llvm::TimeTraceScope timeScope("Link", StringRef("LinkerDriver::Link"));
2617   // If a --hash-style option was not given, set to a default value,
2618   // which varies depending on the target.
2619   if (!args.hasArg(OPT_hash_style)) {
2620     if (config->emachine == EM_MIPS)
2621       config->sysvHash = true;
2622     else
2623       config->sysvHash = config->gnuHash = true;
2624   }
2625 
2626   // Default output filename is "a.out" by the Unix tradition.
2627   if (config->outputFile.empty())
2628     config->outputFile = "a.out";
2629 
2630   // Fail early if the output file or map file is not writable. If a user has a
2631   // long link, e.g. due to a large LTO link, they do not wish to run it and
2632   // find that it failed because there was a mistake in their command-line.
2633   {
2634     llvm::TimeTraceScope timeScope("Create output files");
2635     if (auto e = tryCreateFile(config->outputFile))
2636       error("cannot open output file " + config->outputFile + ": " +
2637             e.message());
2638     if (auto e = tryCreateFile(config->mapFile))
2639       error("cannot open map file " + config->mapFile + ": " + e.message());
2640     if (auto e = tryCreateFile(config->whyExtract))
2641       error("cannot open --why-extract= file " + config->whyExtract + ": " +
2642             e.message());
2643   }
2644   if (errorCount())
2645     return;
2646 
2647   // Use default entry point name if no name was given via the command
2648   // line nor linker scripts. For some reason, MIPS entry point name is
2649   // different from others.
2650   config->warnMissingEntry =
2651       (!config->entry.empty() || (!config->shared && !config->relocatable));
2652   if (config->entry.empty() && !config->relocatable)
2653     config->entry = (config->emachine == EM_MIPS) ? "__start" : "_start";
2654 
2655   // Handle --trace-symbol.
2656   for (auto *arg : args.filtered(OPT_trace_symbol))
2657     symtab.insert(arg->getValue())->traced = true;
2658 
2659   // Handle -u/--undefined before input files. If both a.a and b.so define foo,
2660   // -u foo a.a b.so will extract a.a.
2661   for (StringRef name : config->undefined)
2662     addUnusedUndefined(name)->referenced = true;
2663 
2664   // Add all files to the symbol table. This will add almost all
2665   // symbols that we need to the symbol table. This process might
2666   // add files to the link, via autolinking, these files are always
2667   // appended to the Files vector.
2668   {
2669     llvm::TimeTraceScope timeScope("Parse input files");
2670     for (size_t i = 0; i < files.size(); ++i) {
2671       llvm::TimeTraceScope timeScope("Parse input files", files[i]->getName());
2672       parseFile(files[i]);
2673     }
2674     if (armCmseImpLib)
2675       parseArmCMSEImportLib(*armCmseImpLib);
2676   }
2677 
2678   // Now that we have every file, we can decide if we will need a
2679   // dynamic symbol table.
2680   // We need one if we were asked to export dynamic symbols or if we are
2681   // producing a shared library.
2682   // We also need one if any shared libraries are used and for pie executables
2683   // (probably because the dynamic linker needs it).
2684   config->hasDynSymTab =
2685       !ctx.sharedFiles.empty() || config->isPic || config->exportDynamic;
2686 
2687   // Some symbols (such as __ehdr_start) are defined lazily only when there
2688   // are undefined symbols for them, so we add these to trigger that logic.
2689   for (StringRef name : script->referencedSymbols) {
2690     Symbol *sym = addUnusedUndefined(name);
2691     sym->isUsedInRegularObj = true;
2692     sym->referenced = true;
2693   }
2694 
2695   // Prevent LTO from removing any definition referenced by -u.
2696   for (StringRef name : config->undefined)
2697     if (Defined *sym = dyn_cast_or_null<Defined>(symtab.find(name)))
2698       sym->isUsedInRegularObj = true;
2699 
2700   // If an entry symbol is in a static archive, pull out that file now.
2701   if (Symbol *sym = symtab.find(config->entry))
2702     handleUndefined(sym, "--entry");
2703 
2704   // Handle the `--undefined-glob <pattern>` options.
2705   for (StringRef pat : args::getStrings(args, OPT_undefined_glob))
2706     handleUndefinedGlob(pat);
2707 
2708   // Mark -init and -fini symbols so that the LTO doesn't eliminate them.
2709   if (Symbol *sym = dyn_cast_or_null<Defined>(symtab.find(config->init)))
2710     sym->isUsedInRegularObj = true;
2711   if (Symbol *sym = dyn_cast_or_null<Defined>(symtab.find(config->fini)))
2712     sym->isUsedInRegularObj = true;
2713 
2714   // If any of our inputs are bitcode files, the LTO code generator may create
2715   // references to certain library functions that might not be explicit in the
2716   // bitcode file's symbol table. If any of those library functions are defined
2717   // in a bitcode file in an archive member, we need to arrange to use LTO to
2718   // compile those archive members by adding them to the link beforehand.
2719   //
2720   // However, adding all libcall symbols to the link can have undesired
2721   // consequences. For example, the libgcc implementation of
2722   // __sync_val_compare_and_swap_8 on 32-bit ARM pulls in an .init_array entry
2723   // that aborts the program if the Linux kernel does not support 64-bit
2724   // atomics, which would prevent the program from running even if it does not
2725   // use 64-bit atomics.
2726   //
2727   // Therefore, we only add libcall symbols to the link before LTO if we have
2728   // to, i.e. if the symbol's definition is in bitcode. Any other required
2729   // libcall symbols will be added to the link after LTO when we add the LTO
2730   // object file to the link.
2731   if (!ctx.bitcodeFiles.empty())
2732     for (auto *s : lto::LTO::getRuntimeLibcallSymbols())
2733       handleLibcall(s);
2734 
2735   // Archive members defining __wrap symbols may be extracted.
2736   std::vector<WrappedSymbol> wrapped = addWrappedSymbols(args);
2737 
2738   // No more lazy bitcode can be extracted at this point. Do post parse work
2739   // like checking duplicate symbols.
2740   parallelForEach(ctx.objectFiles, [](ELFFileBase *file) {
2741     initSectionsAndLocalSyms(file, /*ignoreComdats=*/false);
2742   });
2743   parallelForEach(ctx.objectFiles, postParseObjectFile);
2744   parallelForEach(ctx.bitcodeFiles,
2745                   [](BitcodeFile *file) { file->postParse(); });
2746   for (auto &it : ctx.nonPrevailingSyms) {
2747     Symbol &sym = *it.first;
2748     Undefined(sym.file, sym.getName(), sym.binding, sym.stOther, sym.type,
2749               it.second)
2750         .overwrite(sym);
2751     cast<Undefined>(sym).nonPrevailing = true;
2752   }
2753   ctx.nonPrevailingSyms.clear();
2754   for (const DuplicateSymbol &d : ctx.duplicates)
2755     reportDuplicate(*d.sym, d.file, d.section, d.value);
2756   ctx.duplicates.clear();
2757 
2758   // Return if there were name resolution errors.
2759   if (errorCount())
2760     return;
2761 
2762   // We want to declare linker script's symbols early,
2763   // so that we can version them.
2764   // They also might be exported if referenced by DSOs.
2765   script->declareSymbols();
2766 
2767   // Handle --exclude-libs. This is before scanVersionScript() due to a
2768   // workaround for Android ndk: for a defined versioned symbol in an archive
2769   // without a version node in the version script, Android does not expect a
2770   // 'has undefined version' error in -shared --exclude-libs=ALL mode (PR36295).
2771   // GNU ld errors in this case.
2772   if (args.hasArg(OPT_exclude_libs))
2773     excludeLibs(args);
2774 
2775   // Create elfHeader early. We need a dummy section in
2776   // addReservedSymbols to mark the created symbols as not absolute.
2777   Out::elfHeader = make<OutputSection>("", 0, SHF_ALLOC);
2778 
2779   // We need to create some reserved symbols such as _end. Create them.
2780   if (!config->relocatable)
2781     addReservedSymbols();
2782 
2783   // Apply version scripts.
2784   //
2785   // For a relocatable output, version scripts don't make sense, and
2786   // parsing a symbol version string (e.g. dropping "@ver1" from a symbol
2787   // name "foo@ver1") rather do harm, so we don't call this if -r is given.
2788   if (!config->relocatable) {
2789     llvm::TimeTraceScope timeScope("Process symbol versions");
2790     symtab.scanVersionScript();
2791   }
2792 
2793   // Skip the normal linked output if some LTO options are specified.
2794   //
2795   // For --thinlto-index-only, index file creation is performed in
2796   // compileBitcodeFiles, so we are done afterwards. --plugin-opt=emit-llvm and
2797   // --plugin-opt=emit-asm create output files in bitcode or assembly code,
2798   // respectively. When only certain thinLTO modules are specified for
2799   // compilation, the intermediate object file are the expected output.
2800   const bool skipLinkedOutput = config->thinLTOIndexOnly || config->emitLLVM ||
2801                                 config->ltoEmitAsm ||
2802                                 !config->thinLTOModulesToCompile.empty();
2803 
2804   // Do link-time optimization if given files are LLVM bitcode files.
2805   // This compiles bitcode files into real object files.
2806   //
2807   // With this the symbol table should be complete. After this, no new names
2808   // except a few linker-synthesized ones will be added to the symbol table.
2809   const size_t numObjsBeforeLTO = ctx.objectFiles.size();
2810   invokeELFT(compileBitcodeFiles, skipLinkedOutput);
2811 
2812   // Symbol resolution finished. Report backward reference problems,
2813   // --print-archive-stats=, and --why-extract=.
2814   reportBackrefs();
2815   writeArchiveStats();
2816   writeWhyExtract();
2817   if (errorCount())
2818     return;
2819 
2820   // Bail out if normal linked output is skipped due to LTO.
2821   if (skipLinkedOutput)
2822     return;
2823 
2824   // compileBitcodeFiles may have produced lto.tmp object files. After this, no
2825   // more file will be added.
2826   auto newObjectFiles = ArrayRef(ctx.objectFiles).slice(numObjsBeforeLTO);
2827   parallelForEach(newObjectFiles, [](ELFFileBase *file) {
2828     initSectionsAndLocalSyms(file, /*ignoreComdats=*/true);
2829   });
2830   parallelForEach(newObjectFiles, postParseObjectFile);
2831   for (const DuplicateSymbol &d : ctx.duplicates)
2832     reportDuplicate(*d.sym, d.file, d.section, d.value);
2833 
2834   // Handle --exclude-libs again because lto.tmp may reference additional
2835   // libcalls symbols defined in an excluded archive. This may override
2836   // versionId set by scanVersionScript().
2837   if (args.hasArg(OPT_exclude_libs))
2838     excludeLibs(args);
2839 
2840   // Record [__acle_se_<sym>, <sym>] pairs for later processing.
2841   processArmCmseSymbols();
2842 
2843   // Apply symbol renames for --wrap and combine foo@v1 and foo@@v1.
2844   redirectSymbols(wrapped);
2845 
2846   // Replace common symbols with regular symbols.
2847   replaceCommonSymbols();
2848 
2849   {
2850     llvm::TimeTraceScope timeScope("Aggregate sections");
2851     // Now that we have a complete list of input files.
2852     // Beyond this point, no new files are added.
2853     // Aggregate all input sections into one place.
2854     for (InputFile *f : ctx.objectFiles) {
2855       for (InputSectionBase *s : f->getSections()) {
2856         if (!s || s == &InputSection::discarded)
2857           continue;
2858         if (LLVM_UNLIKELY(isa<EhInputSection>(s)))
2859           ctx.ehInputSections.push_back(cast<EhInputSection>(s));
2860         else
2861           ctx.inputSections.push_back(s);
2862       }
2863     }
2864     for (BinaryFile *f : ctx.binaryFiles)
2865       for (InputSectionBase *s : f->getSections())
2866         ctx.inputSections.push_back(cast<InputSection>(s));
2867   }
2868 
2869   {
2870     llvm::TimeTraceScope timeScope("Strip sections");
2871     if (ctx.hasSympart.load(std::memory_order_relaxed)) {
2872       llvm::erase_if(ctx.inputSections, [](InputSectionBase *s) {
2873         if (s->type != SHT_LLVM_SYMPART)
2874           return false;
2875         invokeELFT(readSymbolPartitionSection, s);
2876         return true;
2877       });
2878     }
2879     // We do not want to emit debug sections if --strip-all
2880     // or --strip-debug are given.
2881     if (config->strip != StripPolicy::None) {
2882       llvm::erase_if(ctx.inputSections, [](InputSectionBase *s) {
2883         if (isDebugSection(*s))
2884           return true;
2885         if (auto *isec = dyn_cast<InputSection>(s))
2886           if (InputSectionBase *rel = isec->getRelocatedSection())
2887             if (isDebugSection(*rel))
2888               return true;
2889 
2890         return false;
2891       });
2892     }
2893   }
2894 
2895   // Since we now have a complete set of input files, we can create
2896   // a .d file to record build dependencies.
2897   if (!config->dependencyFile.empty())
2898     writeDependencyFile();
2899 
2900   // Now that the number of partitions is fixed, save a pointer to the main
2901   // partition.
2902   mainPart = &partitions[0];
2903 
2904   // Read .note.gnu.property sections from input object files which
2905   // contain a hint to tweak linker's and loader's behaviors.
2906   config->andFeatures = getAndFeatures();
2907 
2908   // The Target instance handles target-specific stuff, such as applying
2909   // relocations or writing a PLT section. It also contains target-dependent
2910   // values such as a default image base address.
2911   target = getTarget();
2912 
2913   config->eflags = target->calcEFlags();
2914   // maxPageSize (sometimes called abi page size) is the maximum page size that
2915   // the output can be run on. For example if the OS can use 4k or 64k page
2916   // sizes then maxPageSize must be 64k for the output to be useable on both.
2917   // All important alignment decisions must use this value.
2918   config->maxPageSize = getMaxPageSize(args);
2919   // commonPageSize is the most common page size that the output will be run on.
2920   // For example if an OS can use 4k or 64k page sizes and 4k is more common
2921   // than 64k then commonPageSize is set to 4k. commonPageSize can be used for
2922   // optimizations such as DATA_SEGMENT_ALIGN in linker scripts. LLD's use of it
2923   // is limited to writing trap instructions on the last executable segment.
2924   config->commonPageSize = getCommonPageSize(args);
2925 
2926   config->imageBase = getImageBase(args);
2927 
2928   // This adds a .comment section containing a version string.
2929   if (!config->relocatable)
2930     ctx.inputSections.push_back(createCommentSection());
2931 
2932   // Split SHF_MERGE and .eh_frame sections into pieces in preparation for garbage collection.
2933   invokeELFT(splitSections,);
2934 
2935   // Garbage collection and removal of shared symbols from unused shared objects.
2936   invokeELFT(markLive,);
2937   demoteSharedAndLazySymbols();
2938 
2939   // Make copies of any input sections that need to be copied into each
2940   // partition.
2941   copySectionsIntoPartitions();
2942 
2943   // Create synthesized sections such as .got and .plt. This is called before
2944   // processSectionCommands() so that they can be placed by SECTIONS commands.
2945   invokeELFT(createSyntheticSections,);
2946 
2947   // Some input sections that are used for exception handling need to be moved
2948   // into synthetic sections. Do that now so that they aren't assigned to
2949   // output sections in the usual way.
2950   if (!config->relocatable)
2951     combineEhSections();
2952 
2953   // Merge .riscv.attributes sections.
2954   if (config->emachine == EM_RISCV)
2955     mergeRISCVAttributesSections();
2956 
2957   {
2958     llvm::TimeTraceScope timeScope("Assign sections");
2959 
2960     // Create output sections described by SECTIONS commands.
2961     script->processSectionCommands();
2962 
2963     // Linker scripts control how input sections are assigned to output
2964     // sections. Input sections that were not handled by scripts are called
2965     // "orphans", and they are assigned to output sections by the default rule.
2966     // Process that.
2967     script->addOrphanSections();
2968   }
2969 
2970   {
2971     llvm::TimeTraceScope timeScope("Merge/finalize input sections");
2972 
2973     // Migrate InputSectionDescription::sectionBases to sections. This includes
2974     // merging MergeInputSections into a single MergeSyntheticSection. From this
2975     // point onwards InputSectionDescription::sections should be used instead of
2976     // sectionBases.
2977     for (SectionCommand *cmd : script->sectionCommands)
2978       if (auto *osd = dyn_cast<OutputDesc>(cmd))
2979         osd->osec.finalizeInputSections();
2980   }
2981 
2982   // Two input sections with different output sections should not be folded.
2983   // ICF runs after processSectionCommands() so that we know the output sections.
2984   if (config->icf != ICFLevel::None) {
2985     invokeELFT(findKeepUniqueSections, args);
2986     invokeELFT(doIcf,);
2987   }
2988 
2989   // Read the callgraph now that we know what was gced or icfed
2990   if (config->callGraphProfileSort) {
2991     if (auto *arg = args.getLastArg(OPT_call_graph_ordering_file))
2992       if (std::optional<MemoryBufferRef> buffer = readFile(arg->getValue()))
2993         readCallGraph(*buffer);
2994     invokeELFT(readCallGraphsFromObjectFiles,);
2995   }
2996 
2997   // Write the result to the file.
2998   invokeELFT(writeResult,);
2999 }
3000