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