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