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