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