xref: /llvm-project-15.0.7/lld/ELF/Driver.cpp (revision ea5cd3b4)
1 //===- Driver.cpp ---------------------------------------------------------===//
2 //
3 //                             The LLVM Linker
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 //
10 // The driver drives the entire linking process. It is responsible for
11 // parsing command line options and doing whatever it is instructed to do.
12 //
13 // One notable thing in the LLD's driver when compared to other linkers is
14 // that the LLD's driver is agnostic on the host operating system.
15 // Other linkers usually have implicit default values (such as a dynamic
16 // linker path or library paths) for each host OS.
17 //
18 // I don't think implicit default values are useful because they are
19 // usually explicitly specified by the compiler driver. They can even
20 // be harmful when you are doing cross-linking. Therefore, in LLD, we
21 // simply trust the compiler driver to pass all required options and
22 // don't try to make effort on our side.
23 //
24 //===----------------------------------------------------------------------===//
25 
26 #include "Driver.h"
27 #include "Config.h"
28 #include "Filesystem.h"
29 #include "ICF.h"
30 #include "InputFiles.h"
31 #include "InputSection.h"
32 #include "LinkerScript.h"
33 #include "MarkLive.h"
34 #include "OutputSections.h"
35 #include "ScriptParser.h"
36 #include "SymbolTable.h"
37 #include "Symbols.h"
38 #include "SyntheticSections.h"
39 #include "Target.h"
40 #include "Writer.h"
41 #include "lld/Common/Args.h"
42 #include "lld/Common/Driver.h"
43 #include "lld/Common/ErrorHandler.h"
44 #include "lld/Common/Memory.h"
45 #include "lld/Common/Strings.h"
46 #include "lld/Common/TargetOptionsCommandFlags.h"
47 #include "lld/Common/Threads.h"
48 #include "lld/Common/Version.h"
49 #include "llvm/ADT/SetVector.h"
50 #include "llvm/ADT/StringExtras.h"
51 #include "llvm/ADT/StringSwitch.h"
52 #include "llvm/Support/CommandLine.h"
53 #include "llvm/Support/Compression.h"
54 #include "llvm/Support/LEB128.h"
55 #include "llvm/Support/Path.h"
56 #include "llvm/Support/TarWriter.h"
57 #include "llvm/Support/TargetSelect.h"
58 #include "llvm/Support/raw_ostream.h"
59 #include <cstdlib>
60 #include <utility>
61 
62 using namespace llvm;
63 using namespace llvm::ELF;
64 using namespace llvm::object;
65 using namespace llvm::sys;
66 using namespace llvm::support;
67 
68 using namespace lld;
69 using namespace lld::elf;
70 
71 Configuration *elf::Config;
72 LinkerDriver *elf::Driver;
73 
74 static void setConfigs(opt::InputArgList &Args);
75 
76 bool elf::link(ArrayRef<const char *> Args, bool CanExitEarly,
77                raw_ostream &Error) {
78   errorHandler().LogName = args::getFilenameWithoutExe(Args[0]);
79   errorHandler().ErrorLimitExceededMsg =
80       "too many errors emitted, stopping now (use "
81       "-error-limit=0 to see all errors)";
82   errorHandler().ErrorOS = &Error;
83   errorHandler().ExitEarly = CanExitEarly;
84   errorHandler().ColorDiagnostics = Error.has_colors();
85 
86   InputSections.clear();
87   OutputSections.clear();
88   Tar = nullptr;
89   BinaryFiles.clear();
90   BitcodeFiles.clear();
91   ObjectFiles.clear();
92   SharedFiles.clear();
93 
94   Config = make<Configuration>();
95   Driver = make<LinkerDriver>();
96   Script = make<LinkerScript>();
97   Symtab = make<SymbolTable>();
98   Config->ProgName = Args[0];
99 
100   Driver->main(Args);
101 
102   // Exit immediately if we don't need to return to the caller.
103   // This saves time because the overhead of calling destructors
104   // for all globally-allocated objects is not negligible.
105   if (CanExitEarly)
106     exitLld(errorCount() ? 1 : 0);
107 
108   freeArena();
109   return !errorCount();
110 }
111 
112 // Parses a linker -m option.
113 static std::tuple<ELFKind, uint16_t, uint8_t> parseEmulation(StringRef Emul) {
114   uint8_t OSABI = 0;
115   StringRef S = Emul;
116   if (S.endswith("_fbsd")) {
117     S = S.drop_back(5);
118     OSABI = ELFOSABI_FREEBSD;
119   }
120 
121   std::pair<ELFKind, uint16_t> Ret =
122       StringSwitch<std::pair<ELFKind, uint16_t>>(S)
123           .Cases("aarch64elf", "aarch64linux", "aarch64_elf64_le_vec",
124                  {ELF64LEKind, EM_AARCH64})
125           .Cases("armelf", "armelf_linux_eabi", {ELF32LEKind, EM_ARM})
126           .Case("elf32_x86_64", {ELF32LEKind, EM_X86_64})
127           .Cases("elf32btsmip", "elf32btsmipn32", {ELF32BEKind, EM_MIPS})
128           .Cases("elf32ltsmip", "elf32ltsmipn32", {ELF32LEKind, EM_MIPS})
129           .Case("elf32lriscv", {ELF32LEKind, EM_RISCV})
130           .Case("elf32ppc", {ELF32BEKind, EM_PPC})
131           .Case("elf64btsmip", {ELF64BEKind, EM_MIPS})
132           .Case("elf64ltsmip", {ELF64LEKind, EM_MIPS})
133           .Case("elf64lriscv", {ELF64LEKind, EM_RISCV})
134           .Case("elf64ppc", {ELF64BEKind, EM_PPC64})
135           .Case("elf64lppc", {ELF64LEKind, EM_PPC64})
136           .Cases("elf_amd64", "elf_x86_64", {ELF64LEKind, EM_X86_64})
137           .Case("elf_i386", {ELF32LEKind, EM_386})
138           .Case("elf_iamcu", {ELF32LEKind, EM_IAMCU})
139           .Default({ELFNoneKind, EM_NONE});
140 
141   if (Ret.first == ELFNoneKind)
142     error("unknown emulation: " + Emul);
143   return std::make_tuple(Ret.first, Ret.second, OSABI);
144 }
145 
146 // Returns slices of MB by parsing MB as an archive file.
147 // Each slice consists of a member file in the archive.
148 std::vector<std::pair<MemoryBufferRef, uint64_t>> static getArchiveMembers(
149     MemoryBufferRef MB) {
150   std::unique_ptr<Archive> File =
151       CHECK(Archive::create(MB),
152             MB.getBufferIdentifier() + ": failed to parse archive");
153 
154   std::vector<std::pair<MemoryBufferRef, uint64_t>> V;
155   Error Err = Error::success();
156   bool AddToTar = File->isThin() && Tar;
157   for (const ErrorOr<Archive::Child> &COrErr : File->children(Err)) {
158     Archive::Child C =
159         CHECK(COrErr, MB.getBufferIdentifier() +
160                           ": could not get the child of the archive");
161     MemoryBufferRef MBRef =
162         CHECK(C.getMemoryBufferRef(),
163               MB.getBufferIdentifier() +
164                   ": could not get the buffer for a child of the archive");
165     if (AddToTar)
166       Tar->append(relativeToRoot(check(C.getFullName())), MBRef.getBuffer());
167     V.push_back(std::make_pair(MBRef, C.getChildOffset()));
168   }
169   if (Err)
170     fatal(MB.getBufferIdentifier() + ": Archive::children failed: " +
171           toString(std::move(Err)));
172 
173   // Take ownership of memory buffers created for members of thin archives.
174   for (std::unique_ptr<MemoryBuffer> &MB : File->takeThinBuffers())
175     make<std::unique_ptr<MemoryBuffer>>(std::move(MB));
176 
177   return V;
178 }
179 
180 // Opens a file and create a file object. Path has to be resolved already.
181 void LinkerDriver::addFile(StringRef Path, bool WithLOption) {
182   using namespace sys::fs;
183 
184   Optional<MemoryBufferRef> Buffer = readFile(Path);
185   if (!Buffer.hasValue())
186     return;
187   MemoryBufferRef MBRef = *Buffer;
188 
189   if (Config->FormatBinary) {
190     Files.push_back(make<BinaryFile>(MBRef));
191     return;
192   }
193 
194   switch (identify_magic(MBRef.getBuffer())) {
195   case file_magic::unknown:
196     readLinkerScript(MBRef);
197     return;
198   case file_magic::archive: {
199     // Handle -whole-archive.
200     if (InWholeArchive) {
201       for (const auto &P : getArchiveMembers(MBRef))
202         Files.push_back(createObjectFile(P.first, Path, P.second));
203       return;
204     }
205 
206     std::unique_ptr<Archive> File =
207         CHECK(Archive::create(MBRef), Path + ": failed to parse archive");
208 
209     // If an archive file has no symbol table, it is likely that a user
210     // is attempting LTO and using a default ar command that doesn't
211     // understand the LLVM bitcode file. It is a pretty common error, so
212     // we'll handle it as if it had a symbol table.
213     if (!File->isEmpty() && !File->hasSymbolTable()) {
214       for (const auto &P : getArchiveMembers(MBRef))
215         Files.push_back(make<LazyObjFile>(P.first, Path, P.second));
216       return;
217     }
218 
219     // Handle the regular case.
220     Files.push_back(make<ArchiveFile>(std::move(File)));
221     return;
222   }
223   case file_magic::elf_shared_object:
224     if (Config->Relocatable) {
225       error("attempted static link of dynamic object " + Path);
226       return;
227     }
228 
229     // DSOs usually have DT_SONAME tags in their ELF headers, and the
230     // sonames are used to identify DSOs. But if they are missing,
231     // they are identified by filenames. We don't know whether the new
232     // file has a DT_SONAME or not because we haven't parsed it yet.
233     // Here, we set the default soname for the file because we might
234     // need it later.
235     //
236     // If a file was specified by -lfoo, the directory part is not
237     // significant, as a user did not specify it. This behavior is
238     // compatible with GNU.
239     Files.push_back(
240         createSharedFile(MBRef, WithLOption ? path::filename(Path) : Path));
241     return;
242   case file_magic::bitcode:
243   case file_magic::elf_relocatable:
244     if (InLib)
245       Files.push_back(make<LazyObjFile>(MBRef, "", 0));
246     else
247       Files.push_back(createObjectFile(MBRef));
248     break;
249   default:
250     error(Path + ": unknown file type");
251   }
252 }
253 
254 // Add a given library by searching it from input search paths.
255 void LinkerDriver::addLibrary(StringRef Name) {
256   if (Optional<std::string> Path = searchLibrary(Name))
257     addFile(*Path, /*WithLOption=*/true);
258   else
259     error("unable to find library -l" + Name);
260 }
261 
262 // This function is called on startup. We need this for LTO since
263 // LTO calls LLVM functions to compile bitcode files to native code.
264 // Technically this can be delayed until we read bitcode files, but
265 // we don't bother to do lazily because the initialization is fast.
266 static void initLLVM() {
267   InitializeAllTargets();
268   InitializeAllTargetMCs();
269   InitializeAllAsmPrinters();
270   InitializeAllAsmParsers();
271 }
272 
273 // Some command line options or some combinations of them are not allowed.
274 // This function checks for such errors.
275 static void checkOptions(opt::InputArgList &Args) {
276   // The MIPS ABI as of 2016 does not support the GNU-style symbol lookup
277   // table which is a relatively new feature.
278   if (Config->EMachine == EM_MIPS && Config->GnuHash)
279     error("the .gnu.hash section is not compatible with the MIPS target.");
280 
281   if (Config->FixCortexA53Errata843419 && Config->EMachine != EM_AARCH64)
282     error("--fix-cortex-a53-843419 is only supported on AArch64 targets.");
283 
284   if (Config->TocOptimize && Config->EMachine != EM_PPC64)
285       error("--toc-optimize is only supported on the PowerPC64 target.");
286 
287   if (Config->Pie && Config->Shared)
288     error("-shared and -pie may not be used together");
289 
290   if (!Config->Shared && !Config->FilterList.empty())
291     error("-F may not be used without -shared");
292 
293   if (!Config->Shared && !Config->AuxiliaryList.empty())
294     error("-f may not be used without -shared");
295 
296   if (!Config->Relocatable && !Config->DefineCommon)
297     error("-no-define-common not supported in non relocatable output");
298 
299   if (Config->Relocatable) {
300     if (Config->Shared)
301       error("-r and -shared may not be used together");
302     if (Config->GcSections)
303       error("-r and --gc-sections may not be used together");
304     if (Config->GdbIndex)
305       error("-r and --gdb-index may not be used together");
306     if (Config->ICF != ICFLevel::None)
307       error("-r and --icf may not be used together");
308     if (Config->Pie)
309       error("-r and -pie may not be used together");
310   }
311 
312   if (Config->ExecuteOnly) {
313     if (Config->EMachine != EM_AARCH64)
314       error("-execute-only is only supported on AArch64 targets");
315 
316     if (Config->SingleRoRx && !Script->HasSectionsCommand)
317       error("-execute-only and -no-rosegment cannot be used together");
318   }
319 }
320 
321 static const char *getReproduceOption(opt::InputArgList &Args) {
322   if (auto *Arg = Args.getLastArg(OPT_reproduce))
323     return Arg->getValue();
324   return getenv("LLD_REPRODUCE");
325 }
326 
327 static bool hasZOption(opt::InputArgList &Args, StringRef Key) {
328   for (auto *Arg : Args.filtered(OPT_z))
329     if (Key == Arg->getValue())
330       return true;
331   return false;
332 }
333 
334 static bool getZFlag(opt::InputArgList &Args, StringRef K1, StringRef K2,
335                      bool Default) {
336   for (auto *Arg : Args.filtered_reverse(OPT_z)) {
337     if (K1 == Arg->getValue())
338       return true;
339     if (K2 == Arg->getValue())
340       return false;
341   }
342   return Default;
343 }
344 
345 static bool isKnownZFlag(StringRef S) {
346   return S == "combreloc" || S == "copyreloc" || S == "defs" ||
347          S == "execstack" || S == "global" || S == "hazardplt" ||
348          S == "initfirst" || S == "interpose" ||
349          S == "keep-text-section-prefix" || S == "lazy" || S == "muldefs" ||
350          S == "nocombreloc" || S == "nocopyreloc" || S == "nodelete" ||
351          S == "nodlopen" || S == "noexecstack" ||
352          S == "nokeep-text-section-prefix" || S == "norelro" || S == "notext" ||
353          S == "now" || S == "origin" || S == "relro" || S == "retpolineplt" ||
354          S == "rodynamic" || S == "text" || S == "wxneeded" ||
355          S.startswith("max-page-size=") || S.startswith("stack-size=");
356 }
357 
358 // Report an error for an unknown -z option.
359 static void checkZOptions(opt::InputArgList &Args) {
360   for (auto *Arg : Args.filtered(OPT_z))
361     if (!isKnownZFlag(Arg->getValue()))
362       error("unknown -z value: " + StringRef(Arg->getValue()));
363 }
364 
365 void LinkerDriver::main(ArrayRef<const char *> ArgsArr) {
366   ELFOptTable Parser;
367   opt::InputArgList Args = Parser.parse(ArgsArr.slice(1));
368 
369   // Interpret this flag early because error() depends on them.
370   errorHandler().ErrorLimit = args::getInteger(Args, OPT_error_limit, 20);
371 
372   // Handle -help
373   if (Args.hasArg(OPT_help)) {
374     printHelp();
375     return;
376   }
377 
378   // Handle -v or -version.
379   //
380   // A note about "compatible with GNU linkers" message: this is a hack for
381   // scripts generated by GNU Libtool 2.4.6 (released in February 2014 and
382   // still the newest version in March 2017) or earlier to recognize LLD as
383   // a GNU compatible linker. As long as an output for the -v option
384   // contains "GNU" or "with BFD", they recognize us as GNU-compatible.
385   //
386   // This is somewhat ugly hack, but in reality, we had no choice other
387   // than doing this. Considering the very long release cycle of Libtool,
388   // it is not easy to improve it to recognize LLD as a GNU compatible
389   // linker in a timely manner. Even if we can make it, there are still a
390   // lot of "configure" scripts out there that are generated by old version
391   // of Libtool. We cannot convince every software developer to migrate to
392   // the latest version and re-generate scripts. So we have this hack.
393   if (Args.hasArg(OPT_v) || Args.hasArg(OPT_version))
394     message(getLLDVersion() + " (compatible with GNU linkers)");
395 
396   // The behavior of -v or --version is a bit strange, but this is
397   // needed for compatibility with GNU linkers.
398   if (Args.hasArg(OPT_v) && !Args.hasArg(OPT_INPUT))
399     return;
400   if (Args.hasArg(OPT_version))
401     return;
402 
403   if (const char *Path = getReproduceOption(Args)) {
404     // Note that --reproduce is a debug option so you can ignore it
405     // if you are trying to understand the whole picture of the code.
406     Expected<std::unique_ptr<TarWriter>> ErrOrWriter =
407         TarWriter::create(Path, path::stem(Path));
408     if (ErrOrWriter) {
409       Tar = ErrOrWriter->get();
410       Tar->append("response.txt", createResponseFile(Args));
411       Tar->append("version.txt", getLLDVersion() + "\n");
412       make<std::unique_ptr<TarWriter>>(std::move(*ErrOrWriter));
413     } else {
414       error(Twine("--reproduce: failed to open ") + Path + ": " +
415             toString(ErrOrWriter.takeError()));
416     }
417   }
418 
419   readConfigs(Args);
420   checkZOptions(Args);
421   initLLVM();
422   createFiles(Args);
423   if (errorCount())
424     return;
425 
426   inferMachineType();
427   setConfigs(Args);
428   checkOptions(Args);
429   if (errorCount())
430     return;
431 
432   switch (Config->EKind) {
433   case ELF32LEKind:
434     link<ELF32LE>(Args);
435     return;
436   case ELF32BEKind:
437     link<ELF32BE>(Args);
438     return;
439   case ELF64LEKind:
440     link<ELF64LE>(Args);
441     return;
442   case ELF64BEKind:
443     link<ELF64BE>(Args);
444     return;
445   default:
446     llvm_unreachable("unknown Config->EKind");
447   }
448 }
449 
450 static std::string getRpath(opt::InputArgList &Args) {
451   std::vector<StringRef> V = args::getStrings(Args, OPT_rpath);
452   return llvm::join(V.begin(), V.end(), ":");
453 }
454 
455 // Determines what we should do if there are remaining unresolved
456 // symbols after the name resolution.
457 static UnresolvedPolicy getUnresolvedSymbolPolicy(opt::InputArgList &Args) {
458   UnresolvedPolicy ErrorOrWarn = Args.hasFlag(OPT_error_unresolved_symbols,
459                                               OPT_warn_unresolved_symbols, true)
460                                      ? UnresolvedPolicy::ReportError
461                                      : UnresolvedPolicy::Warn;
462 
463   // Process the last of -unresolved-symbols, -no-undefined or -z defs.
464   for (auto *Arg : llvm::reverse(Args)) {
465     switch (Arg->getOption().getID()) {
466     case OPT_unresolved_symbols: {
467       StringRef S = Arg->getValue();
468       if (S == "ignore-all" || S == "ignore-in-object-files")
469         return UnresolvedPolicy::Ignore;
470       if (S == "ignore-in-shared-libs" || S == "report-all")
471         return ErrorOrWarn;
472       error("unknown --unresolved-symbols value: " + S);
473       continue;
474     }
475     case OPT_no_undefined:
476       return ErrorOrWarn;
477     case OPT_z:
478       if (StringRef(Arg->getValue()) == "defs")
479         return ErrorOrWarn;
480       continue;
481     }
482   }
483 
484   // -shared implies -unresolved-symbols=ignore-all because missing
485   // symbols are likely to be resolved at runtime using other DSOs.
486   if (Config->Shared)
487     return UnresolvedPolicy::Ignore;
488   return ErrorOrWarn;
489 }
490 
491 static Target2Policy getTarget2(opt::InputArgList &Args) {
492   StringRef S = Args.getLastArgValue(OPT_target2, "got-rel");
493   if (S == "rel")
494     return Target2Policy::Rel;
495   if (S == "abs")
496     return Target2Policy::Abs;
497   if (S == "got-rel")
498     return Target2Policy::GotRel;
499   error("unknown --target2 option: " + S);
500   return Target2Policy::GotRel;
501 }
502 
503 static bool isOutputFormatBinary(opt::InputArgList &Args) {
504   StringRef S = Args.getLastArgValue(OPT_oformat, "elf");
505   if (S == "binary")
506     return true;
507   if (!S.startswith("elf"))
508     error("unknown --oformat value: " + S);
509   return false;
510 }
511 
512 static DiscardPolicy getDiscard(opt::InputArgList &Args) {
513   if (Args.hasArg(OPT_relocatable))
514     return DiscardPolicy::None;
515 
516   auto *Arg =
517       Args.getLastArg(OPT_discard_all, OPT_discard_locals, OPT_discard_none);
518   if (!Arg)
519     return DiscardPolicy::Default;
520   if (Arg->getOption().getID() == OPT_discard_all)
521     return DiscardPolicy::All;
522   if (Arg->getOption().getID() == OPT_discard_locals)
523     return DiscardPolicy::Locals;
524   return DiscardPolicy::None;
525 }
526 
527 static StringRef getDynamicLinker(opt::InputArgList &Args) {
528   auto *Arg = Args.getLastArg(OPT_dynamic_linker, OPT_no_dynamic_linker);
529   if (!Arg || Arg->getOption().getID() == OPT_no_dynamic_linker)
530     return "";
531   return Arg->getValue();
532 }
533 
534 static ICFLevel getICF(opt::InputArgList &Args) {
535   auto *Arg = Args.getLastArg(OPT_icf_none, OPT_icf_safe, OPT_icf_all);
536   if (!Arg || Arg->getOption().getID() == OPT_icf_none)
537     return ICFLevel::None;
538   if (Arg->getOption().getID() == OPT_icf_safe)
539     return ICFLevel::Safe;
540   return ICFLevel::All;
541 }
542 
543 static StripPolicy getStrip(opt::InputArgList &Args) {
544   if (Args.hasArg(OPT_relocatable))
545     return StripPolicy::None;
546 
547   auto *Arg = Args.getLastArg(OPT_strip_all, OPT_strip_debug);
548   if (!Arg)
549     return StripPolicy::None;
550   if (Arg->getOption().getID() == OPT_strip_all)
551     return StripPolicy::All;
552   return StripPolicy::Debug;
553 }
554 
555 static uint64_t parseSectionAddress(StringRef S, const opt::Arg &Arg) {
556   uint64_t VA = 0;
557   if (S.startswith("0x"))
558     S = S.drop_front(2);
559   if (!to_integer(S, VA, 16))
560     error("invalid argument: " + toString(Arg));
561   return VA;
562 }
563 
564 static StringMap<uint64_t> getSectionStartMap(opt::InputArgList &Args) {
565   StringMap<uint64_t> Ret;
566   for (auto *Arg : Args.filtered(OPT_section_start)) {
567     StringRef Name;
568     StringRef Addr;
569     std::tie(Name, Addr) = StringRef(Arg->getValue()).split('=');
570     Ret[Name] = parseSectionAddress(Addr, *Arg);
571   }
572 
573   if (auto *Arg = Args.getLastArg(OPT_Ttext))
574     Ret[".text"] = parseSectionAddress(Arg->getValue(), *Arg);
575   if (auto *Arg = Args.getLastArg(OPT_Tdata))
576     Ret[".data"] = parseSectionAddress(Arg->getValue(), *Arg);
577   if (auto *Arg = Args.getLastArg(OPT_Tbss))
578     Ret[".bss"] = parseSectionAddress(Arg->getValue(), *Arg);
579   return Ret;
580 }
581 
582 static SortSectionPolicy getSortSection(opt::InputArgList &Args) {
583   StringRef S = Args.getLastArgValue(OPT_sort_section);
584   if (S == "alignment")
585     return SortSectionPolicy::Alignment;
586   if (S == "name")
587     return SortSectionPolicy::Name;
588   if (!S.empty())
589     error("unknown --sort-section rule: " + S);
590   return SortSectionPolicy::Default;
591 }
592 
593 static OrphanHandlingPolicy getOrphanHandling(opt::InputArgList &Args) {
594   StringRef S = Args.getLastArgValue(OPT_orphan_handling, "place");
595   if (S == "warn")
596     return OrphanHandlingPolicy::Warn;
597   if (S == "error")
598     return OrphanHandlingPolicy::Error;
599   if (S != "place")
600     error("unknown --orphan-handling mode: " + S);
601   return OrphanHandlingPolicy::Place;
602 }
603 
604 // Parse --build-id or --build-id=<style>. We handle "tree" as a
605 // synonym for "sha1" because all our hash functions including
606 // -build-id=sha1 are actually tree hashes for performance reasons.
607 static std::pair<BuildIdKind, std::vector<uint8_t>>
608 getBuildId(opt::InputArgList &Args) {
609   auto *Arg = Args.getLastArg(OPT_build_id, OPT_build_id_eq);
610   if (!Arg)
611     return {BuildIdKind::None, {}};
612 
613   if (Arg->getOption().getID() == OPT_build_id)
614     return {BuildIdKind::Fast, {}};
615 
616   StringRef S = Arg->getValue();
617   if (S == "fast")
618     return {BuildIdKind::Fast, {}};
619   if (S == "md5")
620     return {BuildIdKind::Md5, {}};
621   if (S == "sha1" || S == "tree")
622     return {BuildIdKind::Sha1, {}};
623   if (S == "uuid")
624     return {BuildIdKind::Uuid, {}};
625   if (S.startswith("0x"))
626     return {BuildIdKind::Hexstring, parseHex(S.substr(2))};
627 
628   if (S != "none")
629     error("unknown --build-id style: " + S);
630   return {BuildIdKind::None, {}};
631 }
632 
633 static std::pair<bool, bool> getPackDynRelocs(opt::InputArgList &Args) {
634   StringRef S = Args.getLastArgValue(OPT_pack_dyn_relocs, "none");
635   if (S == "android")
636     return {true, false};
637   if (S == "relr")
638     return {false, true};
639   if (S == "android+relr")
640     return {true, true};
641 
642   if (S != "none")
643     error("unknown -pack-dyn-relocs format: " + S);
644   return {false, false};
645 }
646 
647 static void readCallGraph(MemoryBufferRef MB) {
648   // Build a map from symbol name to section
649   DenseMap<StringRef, const Symbol *> SymbolNameToSymbol;
650   for (InputFile *File : ObjectFiles)
651     for (Symbol *Sym : File->getSymbols())
652       SymbolNameToSymbol[Sym->getName()] = Sym;
653 
654   auto FindSection = [&](StringRef SymName) -> InputSectionBase * {
655     const Symbol *Sym = SymbolNameToSymbol.lookup(SymName);
656     if (Sym)
657       warnUnorderableSymbol(Sym);
658     else if (Config->WarnSymbolOrdering)
659       warn(MB.getBufferIdentifier() + ": no such symbol: " + SymName);
660 
661     if (const Defined *DR = dyn_cast_or_null<Defined>(Sym))
662       return dyn_cast_or_null<InputSectionBase>(DR->Section);
663     return nullptr;
664   };
665 
666   for (StringRef L : args::getLines(MB)) {
667     SmallVector<StringRef, 3> Fields;
668     L.split(Fields, ' ');
669     uint64_t Count;
670     if (Fields.size() != 3 || !to_integer(Fields[2], Count))
671       fatal(MB.getBufferIdentifier() + ": parse error");
672 
673     if (const InputSectionBase *FromSB = FindSection(Fields[0]))
674       if (const InputSectionBase *ToSB = FindSection(Fields[1]))
675         Config->CallGraphProfile[std::make_pair(FromSB, ToSB)] += Count;
676   }
677 }
678 
679 static bool getCompressDebugSections(opt::InputArgList &Args) {
680   StringRef S = Args.getLastArgValue(OPT_compress_debug_sections, "none");
681   if (S == "none")
682     return false;
683   if (S != "zlib")
684     error("unknown --compress-debug-sections value: " + S);
685   if (!zlib::isAvailable())
686     error("--compress-debug-sections: zlib is not available");
687   return true;
688 }
689 
690 static std::pair<StringRef, StringRef> getOldNewOptions(opt::InputArgList &Args,
691                                                         unsigned Id) {
692   auto *Arg = Args.getLastArg(Id);
693   if (!Arg)
694     return {"", ""};
695 
696   StringRef S = Arg->getValue();
697   std::pair<StringRef, StringRef> Ret = S.split(';');
698   if (Ret.second.empty())
699     error(Arg->getSpelling() + " expects 'old;new' format, but got " + S);
700   return Ret;
701 }
702 
703 // Parse the symbol ordering file and warn for any duplicate entries.
704 static std::vector<StringRef> getSymbolOrderingFile(MemoryBufferRef MB) {
705   SetVector<StringRef> Names;
706   for (StringRef S : args::getLines(MB))
707     if (!Names.insert(S) && Config->WarnSymbolOrdering)
708       warn(MB.getBufferIdentifier() + ": duplicate ordered symbol: " + S);
709 
710   return Names.takeVector();
711 }
712 
713 static void parseClangOption(StringRef Opt, const Twine &Msg) {
714   std::string Err;
715   raw_string_ostream OS(Err);
716 
717   const char *Argv[] = {Config->ProgName.data(), Opt.data()};
718   if (cl::ParseCommandLineOptions(2, Argv, "", &OS))
719     return;
720   OS.flush();
721   error(Msg + ": " + StringRef(Err).trim());
722 }
723 
724 // Initializes Config members by the command line options.
725 void LinkerDriver::readConfigs(opt::InputArgList &Args) {
726   errorHandler().Verbose = Args.hasArg(OPT_verbose);
727   errorHandler().FatalWarnings =
728       Args.hasFlag(OPT_fatal_warnings, OPT_no_fatal_warnings, false);
729   ThreadsEnabled = Args.hasFlag(OPT_threads, OPT_no_threads, true);
730 
731   Config->AllowMultipleDefinition =
732       Args.hasFlag(OPT_allow_multiple_definition,
733                    OPT_no_allow_multiple_definition, false) ||
734       hasZOption(Args, "muldefs");
735   Config->AuxiliaryList = args::getStrings(Args, OPT_auxiliary);
736   Config->Bsymbolic = Args.hasArg(OPT_Bsymbolic);
737   Config->BsymbolicFunctions = Args.hasArg(OPT_Bsymbolic_functions);
738   Config->CheckSections =
739       Args.hasFlag(OPT_check_sections, OPT_no_check_sections, true);
740   Config->Chroot = Args.getLastArgValue(OPT_chroot);
741   Config->CompressDebugSections = getCompressDebugSections(Args);
742   Config->Cref = Args.hasFlag(OPT_cref, OPT_no_cref, false);
743   Config->DefineCommon = Args.hasFlag(OPT_define_common, OPT_no_define_common,
744                                       !Args.hasArg(OPT_relocatable));
745   Config->Demangle = Args.hasFlag(OPT_demangle, OPT_no_demangle, true);
746   Config->DisableVerify = Args.hasArg(OPT_disable_verify);
747   Config->Discard = getDiscard(Args);
748   Config->DwoDir = Args.getLastArgValue(OPT_plugin_opt_dwo_dir_eq);
749   Config->DynamicLinker = getDynamicLinker(Args);
750   Config->EhFrameHdr =
751       Args.hasFlag(OPT_eh_frame_hdr, OPT_no_eh_frame_hdr, false);
752   Config->EmitRelocs = Args.hasArg(OPT_emit_relocs);
753   Config->EnableNewDtags =
754       Args.hasFlag(OPT_enable_new_dtags, OPT_disable_new_dtags, true);
755   Config->Entry = Args.getLastArgValue(OPT_entry);
756   Config->ExecuteOnly =
757       Args.hasFlag(OPT_execute_only, OPT_no_execute_only, false);
758   Config->ExportDynamic =
759       Args.hasFlag(OPT_export_dynamic, OPT_no_export_dynamic, false);
760   Config->FilterList = args::getStrings(Args, OPT_filter);
761   Config->Fini = Args.getLastArgValue(OPT_fini, "_fini");
762   Config->FixCortexA53Errata843419 = Args.hasArg(OPT_fix_cortex_a53_843419);
763   Config->GcSections = Args.hasFlag(OPT_gc_sections, OPT_no_gc_sections, false);
764   Config->GnuUnique = Args.hasFlag(OPT_gnu_unique, OPT_no_gnu_unique, true);
765   Config->GdbIndex = Args.hasFlag(OPT_gdb_index, OPT_no_gdb_index, false);
766   Config->ICF = getICF(Args);
767   Config->IgnoreDataAddressEquality =
768       Args.hasArg(OPT_ignore_data_address_equality);
769   Config->IgnoreFunctionAddressEquality =
770       Args.hasArg(OPT_ignore_function_address_equality);
771   Config->Init = Args.getLastArgValue(OPT_init, "_init");
772   Config->LTOAAPipeline = Args.getLastArgValue(OPT_lto_aa_pipeline);
773   Config->LTODebugPassManager = Args.hasArg(OPT_lto_debug_pass_manager);
774   Config->LTONewPassManager = Args.hasArg(OPT_lto_new_pass_manager);
775   Config->LTONewPmPasses = Args.getLastArgValue(OPT_lto_newpm_passes);
776   Config->LTOO = args::getInteger(Args, OPT_lto_O, 2);
777   Config->LTOObjPath = Args.getLastArgValue(OPT_plugin_opt_obj_path_eq);
778   Config->LTOPartitions = args::getInteger(Args, OPT_lto_partitions, 1);
779   Config->LTOSampleProfile = Args.getLastArgValue(OPT_lto_sample_profile);
780   Config->MapFile = Args.getLastArgValue(OPT_Map);
781   Config->MipsGotSize = args::getInteger(Args, OPT_mips_got_size, 0xfff0);
782   Config->MergeArmExidx =
783       Args.hasFlag(OPT_merge_exidx_entries, OPT_no_merge_exidx_entries, true);
784   Config->NoinhibitExec = Args.hasArg(OPT_noinhibit_exec);
785   Config->Nostdlib = Args.hasArg(OPT_nostdlib);
786   Config->OFormatBinary = isOutputFormatBinary(Args);
787   Config->Omagic = Args.hasFlag(OPT_omagic, OPT_no_omagic, false);
788   Config->OptRemarksFilename = Args.getLastArgValue(OPT_opt_remarks_filename);
789   Config->OptRemarksWithHotness = Args.hasArg(OPT_opt_remarks_with_hotness);
790   Config->Optimize = args::getInteger(Args, OPT_O, 1);
791   Config->OrphanHandling = getOrphanHandling(Args);
792   Config->OutputFile = Args.getLastArgValue(OPT_o);
793   Config->Pie = Args.hasFlag(OPT_pie, OPT_no_pie, false);
794   Config->PrintIcfSections =
795       Args.hasFlag(OPT_print_icf_sections, OPT_no_print_icf_sections, false);
796   Config->PrintGcSections =
797       Args.hasFlag(OPT_print_gc_sections, OPT_no_print_gc_sections, false);
798   Config->Rpath = getRpath(Args);
799   Config->Relocatable = Args.hasArg(OPT_relocatable);
800   Config->SaveTemps = Args.hasArg(OPT_save_temps);
801   Config->SearchPaths = args::getStrings(Args, OPT_library_path);
802   Config->SectionStartMap = getSectionStartMap(Args);
803   Config->Shared = Args.hasArg(OPT_shared);
804   Config->SingleRoRx = Args.hasArg(OPT_no_rosegment);
805   Config->SoName = Args.getLastArgValue(OPT_soname);
806   Config->SortSection = getSortSection(Args);
807   Config->Strip = getStrip(Args);
808   Config->Sysroot = Args.getLastArgValue(OPT_sysroot);
809   Config->Target1Rel = Args.hasFlag(OPT_target1_rel, OPT_target1_abs, false);
810   Config->Target2 = getTarget2(Args);
811   Config->ThinLTOCacheDir = Args.getLastArgValue(OPT_thinlto_cache_dir);
812   Config->ThinLTOCachePolicy = CHECK(
813       parseCachePruningPolicy(Args.getLastArgValue(OPT_thinlto_cache_policy)),
814       "--thinlto-cache-policy: invalid cache policy");
815   Config->ThinLTOEmitImportsFiles =
816       Args.hasArg(OPT_plugin_opt_thinlto_emit_imports_files);
817   Config->ThinLTOIndexOnly = Args.hasArg(OPT_plugin_opt_thinlto_index_only) ||
818                              Args.hasArg(OPT_plugin_opt_thinlto_index_only_eq);
819   Config->ThinLTOIndexOnlyArg =
820       Args.getLastArgValue(OPT_plugin_opt_thinlto_index_only_eq);
821   Config->ThinLTOJobs = args::getInteger(Args, OPT_thinlto_jobs, -1u);
822   Config->ThinLTOObjectSuffixReplace =
823       getOldNewOptions(Args, OPT_plugin_opt_thinlto_object_suffix_replace_eq);
824   Config->ThinLTOPrefixReplace =
825       getOldNewOptions(Args, OPT_plugin_opt_thinlto_prefix_replace_eq);
826   Config->Trace = Args.hasArg(OPT_trace);
827   Config->Undefined = args::getStrings(Args, OPT_undefined);
828   Config->UndefinedVersion =
829       Args.hasFlag(OPT_undefined_version, OPT_no_undefined_version, true);
830   Config->UseAndroidRelrTags = Args.hasFlag(
831       OPT_use_android_relr_tags, OPT_no_use_android_relr_tags, false);
832   Config->UnresolvedSymbols = getUnresolvedSymbolPolicy(Args);
833   Config->WarnBackrefs =
834       Args.hasFlag(OPT_warn_backrefs, OPT_no_warn_backrefs, false);
835   Config->WarnCommon = Args.hasFlag(OPT_warn_common, OPT_no_warn_common, false);
836   Config->WarnSymbolOrdering =
837       Args.hasFlag(OPT_warn_symbol_ordering, OPT_no_warn_symbol_ordering, true);
838   Config->ZCombreloc = getZFlag(Args, "combreloc", "nocombreloc", true);
839   Config->ZCopyreloc = getZFlag(Args, "copyreloc", "nocopyreloc", true);
840   Config->ZExecstack = getZFlag(Args, "execstack", "noexecstack", false);
841   Config->ZGlobal = hasZOption(Args, "global");
842   Config->ZHazardplt = hasZOption(Args, "hazardplt");
843   Config->ZInitfirst = hasZOption(Args, "initfirst");
844   Config->ZInterpose = hasZOption(Args, "interpose");
845   Config->ZKeepTextSectionPrefix = getZFlag(
846       Args, "keep-text-section-prefix", "nokeep-text-section-prefix", false);
847   Config->ZNodelete = hasZOption(Args, "nodelete");
848   Config->ZNodlopen = hasZOption(Args, "nodlopen");
849   Config->ZNow = getZFlag(Args, "now", "lazy", false);
850   Config->ZOrigin = hasZOption(Args, "origin");
851   Config->ZRelro = getZFlag(Args, "relro", "norelro", true);
852   Config->ZRetpolineplt = hasZOption(Args, "retpolineplt");
853   Config->ZRodynamic = hasZOption(Args, "rodynamic");
854   Config->ZStackSize = args::getZOptionValue(Args, OPT_z, "stack-size", 0);
855   Config->ZText = getZFlag(Args, "text", "notext", true);
856   Config->ZWxneeded = hasZOption(Args, "wxneeded");
857 
858   // Parse LTO options.
859   if (auto *Arg = Args.getLastArg(OPT_plugin_opt_mcpu_eq))
860     parseClangOption(Saver.save("-mcpu=" + StringRef(Arg->getValue())),
861                      Arg->getSpelling());
862 
863   for (auto *Arg : Args.filtered(OPT_plugin_opt))
864     parseClangOption(Arg->getValue(), Arg->getSpelling());
865 
866   // Parse -mllvm options.
867   for (auto *Arg : Args.filtered(OPT_mllvm))
868     parseClangOption(Arg->getValue(), Arg->getSpelling());
869 
870   if (Config->LTOO > 3)
871     error("invalid optimization level for LTO: " + Twine(Config->LTOO));
872   if (Config->LTOPartitions == 0)
873     error("--lto-partitions: number of threads must be > 0");
874   if (Config->ThinLTOJobs == 0)
875     error("--thinlto-jobs: number of threads must be > 0");
876 
877   // Parse ELF{32,64}{LE,BE} and CPU type.
878   if (auto *Arg = Args.getLastArg(OPT_m)) {
879     StringRef S = Arg->getValue();
880     std::tie(Config->EKind, Config->EMachine, Config->OSABI) =
881         parseEmulation(S);
882     Config->MipsN32Abi = (S == "elf32btsmipn32" || S == "elf32ltsmipn32");
883     Config->Emulation = S;
884   }
885 
886   // Parse -hash-style={sysv,gnu,both}.
887   if (auto *Arg = Args.getLastArg(OPT_hash_style)) {
888     StringRef S = Arg->getValue();
889     if (S == "sysv")
890       Config->SysvHash = true;
891     else if (S == "gnu")
892       Config->GnuHash = true;
893     else if (S == "both")
894       Config->SysvHash = Config->GnuHash = true;
895     else
896       error("unknown -hash-style: " + S);
897   }
898 
899   if (Args.hasArg(OPT_print_map))
900     Config->MapFile = "-";
901 
902   // --omagic is an option to create old-fashioned executables in which
903   // .text segments are writable. Today, the option is still in use to
904   // create special-purpose programs such as boot loaders. It doesn't
905   // make sense to create PT_GNU_RELRO for such executables.
906   if (Config->Omagic)
907     Config->ZRelro = false;
908 
909   std::tie(Config->BuildId, Config->BuildIdVector) = getBuildId(Args);
910 
911   std::tie(Config->AndroidPackDynRelocs, Config->RelrPackDynRelocs) =
912       getPackDynRelocs(Args);
913 
914   if (auto *Arg = Args.getLastArg(OPT_symbol_ordering_file))
915     if (Optional<MemoryBufferRef> Buffer = readFile(Arg->getValue()))
916       Config->SymbolOrderingFile = getSymbolOrderingFile(*Buffer);
917 
918   // If --retain-symbol-file is used, we'll keep only the symbols listed in
919   // the file and discard all others.
920   if (auto *Arg = Args.getLastArg(OPT_retain_symbols_file)) {
921     Config->DefaultSymbolVersion = VER_NDX_LOCAL;
922     if (Optional<MemoryBufferRef> Buffer = readFile(Arg->getValue()))
923       for (StringRef S : args::getLines(*Buffer))
924         Config->VersionScriptGlobals.push_back(
925             {S, /*IsExternCpp*/ false, /*HasWildcard*/ false});
926   }
927 
928   bool HasExportDynamic =
929       Args.hasFlag(OPT_export_dynamic, OPT_no_export_dynamic, false);
930 
931   // Parses -dynamic-list and -export-dynamic-symbol. They make some
932   // symbols private. Note that -export-dynamic takes precedence over them
933   // as it says all symbols should be exported.
934   if (!HasExportDynamic) {
935     for (auto *Arg : Args.filtered(OPT_dynamic_list))
936       if (Optional<MemoryBufferRef> Buffer = readFile(Arg->getValue()))
937         readDynamicList(*Buffer);
938 
939     for (auto *Arg : Args.filtered(OPT_export_dynamic_symbol))
940       Config->DynamicList.push_back(
941           {Arg->getValue(), /*IsExternCpp*/ false, /*HasWildcard*/ false});
942   }
943 
944   // If --export-dynamic-symbol=foo is given and symbol foo is defined in
945   // an object file in an archive file, that object file should be pulled
946   // out and linked. (It doesn't have to behave like that from technical
947   // point of view, but this is needed for compatibility with GNU.)
948   for (auto *Arg : Args.filtered(OPT_export_dynamic_symbol))
949     Config->Undefined.push_back(Arg->getValue());
950 
951   for (auto *Arg : Args.filtered(OPT_version_script))
952     if (Optional<std::string> Path = searchScript(Arg->getValue())) {
953       if (Optional<MemoryBufferRef> Buffer = readFile(*Path))
954         readVersionScript(*Buffer);
955     } else {
956       error(Twine("cannot find version script ") + Arg->getValue());
957     }
958 }
959 
960 // Some Config members do not directly correspond to any particular
961 // command line options, but computed based on other Config values.
962 // This function initialize such members. See Config.h for the details
963 // of these values.
964 static void setConfigs(opt::InputArgList &Args) {
965   ELFKind K = Config->EKind;
966   uint16_t M = Config->EMachine;
967 
968   Config->CopyRelocs = (Config->Relocatable || Config->EmitRelocs);
969   Config->Is64 = (K == ELF64LEKind || K == ELF64BEKind);
970   Config->IsLE = (K == ELF32LEKind || K == ELF64LEKind);
971   Config->Endianness = Config->IsLE ? endianness::little : endianness::big;
972   Config->IsMips64EL = (K == ELF64LEKind && M == EM_MIPS);
973   Config->Pic = Config->Pie || Config->Shared;
974   Config->Wordsize = Config->Is64 ? 8 : 4;
975 
976   // ELF defines two different ways to store relocation addends as shown below:
977   //
978   //  Rel:  Addends are stored to the location where relocations are applied.
979   //  Rela: Addends are stored as part of relocation entry.
980   //
981   // In other words, Rela makes it easy to read addends at the price of extra
982   // 4 or 8 byte for each relocation entry. We don't know why ELF defined two
983   // different mechanisms in the first place, but this is how the spec is
984   // defined.
985   //
986   // You cannot choose which one, Rel or Rela, you want to use. Instead each
987   // ABI defines which one you need to use. The following expression expresses
988   // that.
989   Config->IsRela = M == EM_AARCH64 || M == EM_AMDGPU || M == EM_PPC ||
990                    M == EM_PPC64 || M == EM_RISCV || M == EM_X86_64;
991 
992   // If the output uses REL relocations we must store the dynamic relocation
993   // addends to the output sections. We also store addends for RELA relocations
994   // if --apply-dynamic-relocs is used.
995   // We default to not writing the addends when using RELA relocations since
996   // any standard conforming tool can find it in r_addend.
997   Config->WriteAddends = Args.hasFlag(OPT_apply_dynamic_relocs,
998                                       OPT_no_apply_dynamic_relocs, false) ||
999                          !Config->IsRela;
1000 
1001   Config->TocOptimize =
1002       Args.hasFlag(OPT_toc_optimize, OPT_no_toc_optimize, M == EM_PPC64);
1003 }
1004 
1005 // Returns a value of "-format" option.
1006 static bool isFormatBinary(StringRef S) {
1007   if (S == "binary")
1008     return true;
1009   if (S == "elf" || S == "default")
1010     return false;
1011   error("unknown -format value: " + S +
1012         " (supported formats: elf, default, binary)");
1013   return false;
1014 }
1015 
1016 void LinkerDriver::createFiles(opt::InputArgList &Args) {
1017   // For --{push,pop}-state.
1018   std::vector<std::tuple<bool, bool, bool>> Stack;
1019 
1020   // Iterate over argv to process input files and positional arguments.
1021   for (auto *Arg : Args) {
1022     switch (Arg->getOption().getUnaliasedOption().getID()) {
1023     case OPT_library:
1024       addLibrary(Arg->getValue());
1025       break;
1026     case OPT_INPUT:
1027       addFile(Arg->getValue(), /*WithLOption=*/false);
1028       break;
1029     case OPT_defsym: {
1030       StringRef From;
1031       StringRef To;
1032       std::tie(From, To) = StringRef(Arg->getValue()).split('=');
1033       if (From.empty() || To.empty())
1034         error("-defsym: syntax error: " + StringRef(Arg->getValue()));
1035       else
1036         readDefsym(From, MemoryBufferRef(To, "-defsym"));
1037       break;
1038     }
1039     case OPT_script:
1040       if (Optional<std::string> Path = searchScript(Arg->getValue())) {
1041         if (Optional<MemoryBufferRef> MB = readFile(*Path))
1042           readLinkerScript(*MB);
1043         break;
1044       }
1045       error(Twine("cannot find linker script ") + Arg->getValue());
1046       break;
1047     case OPT_as_needed:
1048       Config->AsNeeded = true;
1049       break;
1050     case OPT_format:
1051       Config->FormatBinary = isFormatBinary(Arg->getValue());
1052       break;
1053     case OPT_no_as_needed:
1054       Config->AsNeeded = false;
1055       break;
1056     case OPT_Bstatic:
1057       Config->Static = true;
1058       break;
1059     case OPT_Bdynamic:
1060       Config->Static = false;
1061       break;
1062     case OPT_whole_archive:
1063       InWholeArchive = true;
1064       break;
1065     case OPT_no_whole_archive:
1066       InWholeArchive = false;
1067       break;
1068     case OPT_just_symbols:
1069       if (Optional<MemoryBufferRef> MB = readFile(Arg->getValue())) {
1070         Files.push_back(createObjectFile(*MB));
1071         Files.back()->JustSymbols = true;
1072       }
1073       break;
1074     case OPT_start_group:
1075       if (InputFile::IsInGroup)
1076         error("nested --start-group");
1077       InputFile::IsInGroup = true;
1078       break;
1079     case OPT_end_group:
1080       if (!InputFile::IsInGroup)
1081         error("stray --end-group");
1082       InputFile::IsInGroup = false;
1083       ++InputFile::NextGroupId;
1084       break;
1085     case OPT_start_lib:
1086       if (InLib)
1087         error("nested --start-lib");
1088       if (InputFile::IsInGroup)
1089         error("may not nest --start-lib in --start-group");
1090       InLib = true;
1091       InputFile::IsInGroup = true;
1092       break;
1093     case OPT_end_lib:
1094       if (!InLib)
1095         error("stray --end-lib");
1096       InLib = false;
1097       InputFile::IsInGroup = false;
1098       ++InputFile::NextGroupId;
1099       break;
1100     case OPT_push_state:
1101       Stack.emplace_back(Config->AsNeeded, Config->Static, InWholeArchive);
1102       break;
1103     case OPT_pop_state:
1104       if (Stack.empty()) {
1105         error("unbalanced --push-state/--pop-state");
1106         break;
1107       }
1108       std::tie(Config->AsNeeded, Config->Static, InWholeArchive) = Stack.back();
1109       Stack.pop_back();
1110       break;
1111     }
1112   }
1113 
1114   if (Files.empty() && errorCount() == 0)
1115     error("no input files");
1116 }
1117 
1118 // If -m <machine_type> was not given, infer it from object files.
1119 void LinkerDriver::inferMachineType() {
1120   if (Config->EKind != ELFNoneKind)
1121     return;
1122 
1123   for (InputFile *F : Files) {
1124     if (F->EKind == ELFNoneKind)
1125       continue;
1126     Config->EKind = F->EKind;
1127     Config->EMachine = F->EMachine;
1128     Config->OSABI = F->OSABI;
1129     Config->MipsN32Abi = Config->EMachine == EM_MIPS && isMipsN32Abi(F);
1130     return;
1131   }
1132   error("target emulation unknown: -m or at least one .o file required");
1133 }
1134 
1135 // Parse -z max-page-size=<value>. The default value is defined by
1136 // each target.
1137 static uint64_t getMaxPageSize(opt::InputArgList &Args) {
1138   uint64_t Val = args::getZOptionValue(Args, OPT_z, "max-page-size",
1139                                        Target->DefaultMaxPageSize);
1140   if (!isPowerOf2_64(Val))
1141     error("max-page-size: value isn't a power of 2");
1142   return Val;
1143 }
1144 
1145 // Parses -image-base option.
1146 static Optional<uint64_t> getImageBase(opt::InputArgList &Args) {
1147   // Because we are using "Config->MaxPageSize" here, this function has to be
1148   // called after the variable is initialized.
1149   auto *Arg = Args.getLastArg(OPT_image_base);
1150   if (!Arg)
1151     return None;
1152 
1153   StringRef S = Arg->getValue();
1154   uint64_t V;
1155   if (!to_integer(S, V)) {
1156     error("-image-base: number expected, but got " + S);
1157     return 0;
1158   }
1159   if ((V % Config->MaxPageSize) != 0)
1160     warn("-image-base: address isn't multiple of page size: " + S);
1161   return V;
1162 }
1163 
1164 // Parses `--exclude-libs=lib,lib,...`.
1165 // The library names may be delimited by commas or colons.
1166 static DenseSet<StringRef> getExcludeLibs(opt::InputArgList &Args) {
1167   DenseSet<StringRef> Ret;
1168   for (auto *Arg : Args.filtered(OPT_exclude_libs)) {
1169     StringRef S = Arg->getValue();
1170     for (;;) {
1171       size_t Pos = S.find_first_of(",:");
1172       if (Pos == StringRef::npos)
1173         break;
1174       Ret.insert(S.substr(0, Pos));
1175       S = S.substr(Pos + 1);
1176     }
1177     Ret.insert(S);
1178   }
1179   return Ret;
1180 }
1181 
1182 // Handles the -exclude-libs option. If a static library file is specified
1183 // by the -exclude-libs option, all public symbols from the archive become
1184 // private unless otherwise specified by version scripts or something.
1185 // A special library name "ALL" means all archive files.
1186 //
1187 // This is not a popular option, but some programs such as bionic libc use it.
1188 template <class ELFT>
1189 static void excludeLibs(opt::InputArgList &Args) {
1190   DenseSet<StringRef> Libs = getExcludeLibs(Args);
1191   bool All = Libs.count("ALL");
1192 
1193   auto Visit = [&](InputFile *File) {
1194     if (!File->ArchiveName.empty())
1195       if (All || Libs.count(path::filename(File->ArchiveName)))
1196         for (Symbol *Sym : File->getSymbols())
1197           if (!Sym->isLocal() && Sym->File == File)
1198             Sym->VersionId = VER_NDX_LOCAL;
1199   };
1200 
1201   for (InputFile *File : ObjectFiles)
1202     Visit(File);
1203 
1204   for (BitcodeFile *File : BitcodeFiles)
1205     Visit(File);
1206 }
1207 
1208 // Force Sym to be entered in the output. Used for -u or equivalent.
1209 template <class ELFT> static void handleUndefined(StringRef Name) {
1210   Symbol *Sym = Symtab->find(Name);
1211   if (!Sym)
1212     return;
1213 
1214   // Since symbol S may not be used inside the program, LTO may
1215   // eliminate it. Mark the symbol as "used" to prevent it.
1216   Sym->IsUsedInRegularObj = true;
1217 
1218   if (Sym->isLazy())
1219     Symtab->fetchLazy<ELFT>(Sym);
1220 }
1221 
1222 template <class ELFT> static void handleLibcall(StringRef Name) {
1223   Symbol *Sym = Symtab->find(Name);
1224   if (!Sym || !Sym->isLazy())
1225     return;
1226 
1227   MemoryBufferRef MB;
1228   if (auto *LO = dyn_cast<LazyObject>(Sym))
1229     MB = LO->File->MB;
1230   else
1231     MB = cast<LazyArchive>(Sym)->getMemberBuffer();
1232 
1233   if (isBitcode(MB))
1234     Symtab->fetchLazy<ELFT>(Sym);
1235 }
1236 
1237 // If all references to a DSO happen to be weak, the DSO is not added
1238 // to DT_NEEDED. If that happens, we need to eliminate shared symbols
1239 // created from the DSO. Otherwise, they become dangling references
1240 // that point to a non-existent DSO.
1241 template <class ELFT> static void demoteSharedSymbols() {
1242   for (Symbol *Sym : Symtab->getSymbols()) {
1243     if (auto *S = dyn_cast<SharedSymbol>(Sym)) {
1244       if (!S->getFile<ELFT>().IsNeeded) {
1245         bool Used = S->Used;
1246         replaceSymbol<Undefined>(S, nullptr, S->getName(), STB_WEAK, S->StOther,
1247                                  S->Type);
1248         S->Used = Used;
1249       }
1250     }
1251   }
1252 }
1253 
1254 // The section referred to by S is considered address-significant. Set the
1255 // KeepUnique flag on the section if appropriate.
1256 static void markAddrsig(Symbol *S) {
1257   if (auto *D = dyn_cast_or_null<Defined>(S))
1258     if (D->Section)
1259       // We don't need to keep text sections unique under --icf=all even if they
1260       // are address-significant.
1261       if (Config->ICF == ICFLevel::Safe || !(D->Section->Flags & SHF_EXECINSTR))
1262         D->Section->KeepUnique = true;
1263 }
1264 
1265 // Record sections that define symbols mentioned in --keep-unique <symbol>
1266 // and symbols referred to by address-significance tables. These sections are
1267 // ineligible for ICF.
1268 template <class ELFT>
1269 static void findKeepUniqueSections(opt::InputArgList &Args) {
1270   for (auto *Arg : Args.filtered(OPT_keep_unique)) {
1271     StringRef Name = Arg->getValue();
1272     auto *D = dyn_cast_or_null<Defined>(Symtab->find(Name));
1273     if (!D || !D->Section) {
1274       warn("could not find symbol " + Name + " to keep unique");
1275       continue;
1276     }
1277     D->Section->KeepUnique = true;
1278   }
1279 
1280   // --icf=all --ignore-data-address-equality means that we can ignore
1281   // the dynsym and address-significance tables entirely.
1282   if (Config->ICF == ICFLevel::All && Config->IgnoreDataAddressEquality)
1283     return;
1284 
1285   // Symbols in the dynsym could be address-significant in other executables
1286   // or DSOs, so we conservatively mark them as address-significant.
1287   for (Symbol *S : Symtab->getSymbols())
1288     if (S->includeInDynsym())
1289       markAddrsig(S);
1290 
1291   // Visit the address-significance table in each object file and mark each
1292   // referenced symbol as address-significant.
1293   for (InputFile *F : ObjectFiles) {
1294     auto *Obj = cast<ObjFile<ELFT>>(F);
1295     ArrayRef<Symbol *> Syms = Obj->getSymbols();
1296     if (Obj->AddrsigSec) {
1297       ArrayRef<uint8_t> Contents =
1298           check(Obj->getObj().getSectionContents(Obj->AddrsigSec));
1299       const uint8_t *Cur = Contents.begin();
1300       while (Cur != Contents.end()) {
1301         unsigned Size;
1302         const char *Err;
1303         uint64_t SymIndex = decodeULEB128(Cur, &Size, Contents.end(), &Err);
1304         if (Err)
1305           fatal(toString(F) + ": could not decode addrsig section: " + Err);
1306         markAddrsig(Syms[SymIndex]);
1307         Cur += Size;
1308       }
1309     } else {
1310       // If an object file does not have an address-significance table,
1311       // conservatively mark all of its symbols as address-significant.
1312       for (Symbol *S : Syms)
1313         markAddrsig(S);
1314     }
1315   }
1316 }
1317 
1318 // The --wrap option is a feature to rename symbols so that you can write
1319 // wrappers for existing functions. If you pass `-wrap=foo`, all
1320 // occurrences of symbol `foo` are resolved to `wrap_foo` (so, you are
1321 // expected to write `wrap_foo` function as a wrapper). The original
1322 // symbol becomes accessible as `real_foo`, so you can call that from your
1323 // wrapper.
1324 //
1325 // This data structure is instantiated for each -wrap option.
1326 struct WrappedSymbol {
1327   Symbol *Sym;
1328   Symbol *Real;
1329   Symbol *Wrap;
1330 };
1331 
1332 // Handles -wrap option.
1333 //
1334 // This function instantiates wrapper symbols. At this point, they seem
1335 // like they are not being used at all, so we explicitly set some flags so
1336 // that LTO won't eliminate them.
1337 template <class ELFT>
1338 static std::vector<WrappedSymbol> addWrappedSymbols(opt::InputArgList &Args) {
1339   std::vector<WrappedSymbol> V;
1340   DenseSet<StringRef> Seen;
1341 
1342   for (auto *Arg : Args.filtered(OPT_wrap)) {
1343     StringRef Name = Arg->getValue();
1344     if (!Seen.insert(Name).second)
1345       continue;
1346 
1347     Symbol *Sym = Symtab->find(Name);
1348     if (!Sym)
1349       continue;
1350 
1351     Symbol *Real = Symtab->addUndefined<ELFT>(Saver.save("__real_" + Name));
1352     Symbol *Wrap = Symtab->addUndefined<ELFT>(Saver.save("__wrap_" + Name));
1353     V.push_back({Sym, Real, Wrap});
1354 
1355     // We want to tell LTO not to inline symbols to be overwritten
1356     // because LTO doesn't know the final symbol contents after renaming.
1357     Real->CanInline = false;
1358     Sym->CanInline = false;
1359 
1360     // Tell LTO not to eliminate these symbols.
1361     Sym->IsUsedInRegularObj = true;
1362     Wrap->IsUsedInRegularObj = true;
1363   }
1364   return V;
1365 }
1366 
1367 // Do renaming for -wrap by updating pointers to symbols.
1368 //
1369 // When this function is executed, only InputFiles and symbol table
1370 // contain pointers to symbol objects. We visit them to replace pointers,
1371 // so that wrapped symbols are swapped as instructed by the command line.
1372 template <class ELFT> static void wrapSymbols(ArrayRef<WrappedSymbol> Wrapped) {
1373   DenseMap<Symbol *, Symbol *> Map;
1374   for (const WrappedSymbol &W : Wrapped) {
1375     Map[W.Sym] = W.Wrap;
1376     Map[W.Real] = W.Sym;
1377   }
1378 
1379   // Update pointers in input files.
1380   parallelForEach(ObjectFiles, [&](InputFile *File) {
1381     std::vector<Symbol *> &Syms = File->getMutableSymbols();
1382     for (size_t I = 0, E = Syms.size(); I != E; ++I)
1383       if (Symbol *S = Map.lookup(Syms[I]))
1384         Syms[I] = S;
1385   });
1386 
1387   // Update pointers in the symbol table.
1388   for (const WrappedSymbol &W : Wrapped)
1389     Symtab->wrap(W.Sym, W.Real, W.Wrap);
1390 }
1391 
1392 static const char *LibcallRoutineNames[] = {
1393 #define HANDLE_LIBCALL(code, name) name,
1394 #include "llvm/IR/RuntimeLibcalls.def"
1395 #undef HANDLE_LIBCALL
1396 };
1397 
1398 // Do actual linking. Note that when this function is called,
1399 // all linker scripts have already been parsed.
1400 template <class ELFT> void LinkerDriver::link(opt::InputArgList &Args) {
1401   Target = getTarget();
1402 
1403   Config->MaxPageSize = getMaxPageSize(Args);
1404   Config->ImageBase = getImageBase(Args);
1405 
1406   // If a -hash-style option was not given, set to a default value,
1407   // which varies depending on the target.
1408   if (!Args.hasArg(OPT_hash_style)) {
1409     if (Config->EMachine == EM_MIPS)
1410       Config->SysvHash = true;
1411     else
1412       Config->SysvHash = Config->GnuHash = true;
1413   }
1414 
1415   // Default output filename is "a.out" by the Unix tradition.
1416   if (Config->OutputFile.empty())
1417     Config->OutputFile = "a.out";
1418 
1419   // Fail early if the output file or map file is not writable. If a user has a
1420   // long link, e.g. due to a large LTO link, they do not wish to run it and
1421   // find that it failed because there was a mistake in their command-line.
1422   if (auto E = tryCreateFile(Config->OutputFile))
1423     error("cannot open output file " + Config->OutputFile + ": " + E.message());
1424   if (auto E = tryCreateFile(Config->MapFile))
1425     error("cannot open map file " + Config->MapFile + ": " + E.message());
1426   if (errorCount())
1427     return;
1428 
1429   // Use default entry point name if no name was given via the command
1430   // line nor linker scripts. For some reason, MIPS entry point name is
1431   // different from others.
1432   Config->WarnMissingEntry =
1433       (!Config->Entry.empty() || (!Config->Shared && !Config->Relocatable));
1434   if (Config->Entry.empty() && !Config->Relocatable)
1435     Config->Entry = (Config->EMachine == EM_MIPS) ? "__start" : "_start";
1436 
1437   // Handle --trace-symbol.
1438   for (auto *Arg : Args.filtered(OPT_trace_symbol))
1439     Symtab->trace(Arg->getValue());
1440 
1441   // Add all files to the symbol table. This will add almost all
1442   // symbols that we need to the symbol table.
1443   for (InputFile *F : Files)
1444     Symtab->addFile<ELFT>(F);
1445 
1446   // Now that we have every file, we can decide if we will need a
1447   // dynamic symbol table.
1448   // We need one if we were asked to export dynamic symbols or if we are
1449   // producing a shared library.
1450   // We also need one if any shared libraries are used and for pie executables
1451   // (probably because the dynamic linker needs it).
1452   Config->HasDynSymTab =
1453       !SharedFiles.empty() || Config->Pic || Config->ExportDynamic;
1454 
1455   // Some symbols (such as __ehdr_start) are defined lazily only when there
1456   // are undefined symbols for them, so we add these to trigger that logic.
1457   for (StringRef Sym : Script->ReferencedSymbols)
1458     Symtab->addUndefined<ELFT>(Sym);
1459 
1460   // Handle the `--undefined <sym>` options.
1461   for (StringRef S : Config->Undefined)
1462     handleUndefined<ELFT>(S);
1463 
1464   // If an entry symbol is in a static archive, pull out that file now.
1465   handleUndefined<ELFT>(Config->Entry);
1466 
1467   // If any of our inputs are bitcode files, the LTO code generator may create
1468   // references to certain library functions that might not be explicit in the
1469   // bitcode file's symbol table. If any of those library functions are defined
1470   // in a bitcode file in an archive member, we need to arrange to use LTO to
1471   // compile those archive members by adding them to the link beforehand.
1472   //
1473   // However, adding all libcall symbols to the link can have undesired
1474   // consequences. For example, the libgcc implementation of
1475   // __sync_val_compare_and_swap_8 on 32-bit ARM pulls in an .init_array entry
1476   // that aborts the program if the Linux kernel does not support 64-bit
1477   // atomics, which would prevent the program from running even if it does not
1478   // use 64-bit atomics.
1479   //
1480   // Therefore, we only add libcall symbols to the link before LTO if we have
1481   // to, i.e. if the symbol's definition is in bitcode. Any other required
1482   // libcall symbols will be added to the link after LTO when we add the LTO
1483   // object file to the link.
1484   if (!BitcodeFiles.empty())
1485     for (const char *S : LibcallRoutineNames)
1486       handleLibcall<ELFT>(S);
1487 
1488   // Return if there were name resolution errors.
1489   if (errorCount())
1490     return;
1491 
1492   // Now when we read all script files, we want to finalize order of linker
1493   // script commands, which can be not yet final because of INSERT commands.
1494   Script->processInsertCommands();
1495 
1496   // We want to declare linker script's symbols early,
1497   // so that we can version them.
1498   // They also might be exported if referenced by DSOs.
1499   Script->declareSymbols();
1500 
1501   // Handle the -exclude-libs option.
1502   if (Args.hasArg(OPT_exclude_libs))
1503     excludeLibs<ELFT>(Args);
1504 
1505   // Create ElfHeader early. We need a dummy section in
1506   // addReservedSymbols to mark the created symbols as not absolute.
1507   Out::ElfHeader = make<OutputSection>("", 0, SHF_ALLOC);
1508   Out::ElfHeader->Size = sizeof(typename ELFT::Ehdr);
1509 
1510   // We need to create some reserved symbols such as _end. Create them.
1511   if (!Config->Relocatable)
1512     addReservedSymbols();
1513 
1514   // Apply version scripts.
1515   //
1516   // For a relocatable output, version scripts don't make sense, and
1517   // parsing a symbol version string (e.g. dropping "@ver1" from a symbol
1518   // name "foo@ver1") rather do harm, so we don't call this if -r is given.
1519   if (!Config->Relocatable)
1520     Symtab->scanVersionScript();
1521 
1522   // Create wrapped symbols for -wrap option.
1523   std::vector<WrappedSymbol> Wrapped = addWrappedSymbols<ELFT>(Args);
1524 
1525   // Do link-time optimization if given files are LLVM bitcode files.
1526   // This compiles bitcode files into real object files.
1527   //
1528   // With this the symbol table should be complete. After this, no new names
1529   // except a few linker-synthesized ones will be added to the symbol table.
1530   Symtab->addCombinedLTOObject<ELFT>();
1531   if (errorCount())
1532     return;
1533 
1534   // If -thinlto-index-only is given, we should create only "index
1535   // files" and not object files. Index file creation is already done
1536   // in addCombinedLTOObject, so we are done if that's the case.
1537   if (Config->ThinLTOIndexOnly)
1538     return;
1539 
1540   // Apply symbol renames for -wrap.
1541   if (!Wrapped.empty())
1542     wrapSymbols<ELFT>(Wrapped);
1543 
1544   // Now that we have a complete list of input files.
1545   // Beyond this point, no new files are added.
1546   // Aggregate all input sections into one place.
1547   for (InputFile *F : ObjectFiles)
1548     for (InputSectionBase *S : F->getSections())
1549       if (S && S != &InputSection::Discarded)
1550         InputSections.push_back(S);
1551   for (BinaryFile *F : BinaryFiles)
1552     for (InputSectionBase *S : F->getSections())
1553       InputSections.push_back(cast<InputSection>(S));
1554 
1555   // We do not want to emit debug sections if --strip-all
1556   // or -strip-debug are given.
1557   if (Config->Strip != StripPolicy::None)
1558     llvm::erase_if(InputSections, [](InputSectionBase *S) {
1559       return S->Name.startswith(".debug") || S->Name.startswith(".zdebug");
1560     });
1561 
1562   Config->EFlags = Target->calcEFlags();
1563 
1564   if (Config->EMachine == EM_ARM) {
1565     // FIXME: These warnings can be removed when lld only uses these features
1566     // when the input objects have been compiled with an architecture that
1567     // supports them.
1568     if (Config->ARMHasBlx == false)
1569       warn("lld uses blx instruction, no object with architecture supporting "
1570            "feature detected.");
1571   }
1572 
1573   // This adds a .comment section containing a version string. We have to add it
1574   // before decompressAndMergeSections because the .comment section is a
1575   // mergeable section.
1576   if (!Config->Relocatable)
1577     InputSections.push_back(createCommentSection());
1578 
1579   // Do size optimizations: garbage collection, merging of SHF_MERGE sections
1580   // and identical code folding.
1581   decompressSections();
1582   splitSections<ELFT>();
1583   markLive<ELFT>();
1584   demoteSharedSymbols<ELFT>();
1585   mergeSections();
1586   if (Config->ICF != ICFLevel::None) {
1587     findKeepUniqueSections<ELFT>(Args);
1588     doIcf<ELFT>();
1589   }
1590 
1591   // Read the callgraph now that we know what was gced or icfed
1592   if (auto *Arg = Args.getLastArg(OPT_call_graph_ordering_file))
1593     if (Optional<MemoryBufferRef> Buffer = readFile(Arg->getValue()))
1594       readCallGraph(*Buffer);
1595 
1596   // Write the result to the file.
1597   writeResult<ELFT>();
1598 }
1599