xref: /llvm-project-15.0.7/lld/ELF/Driver.cpp (revision f4bf4227)
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 "Memory.h"
34 #include "OutputSections.h"
35 #include "ScriptParser.h"
36 #include "Strings.h"
37 #include "SymbolTable.h"
38 #include "SyntheticSections.h"
39 #include "Target.h"
40 #include "Writer.h"
41 #include "lld/Common/Driver.h"
42 #include "lld/Common/ErrorHandler.h"
43 #include "lld/Common/Threads.h"
44 #include "lld/Common/Version.h"
45 #include "llvm/ADT/StringExtras.h"
46 #include "llvm/ADT/StringSwitch.h"
47 #include "llvm/Support/CommandLine.h"
48 #include "llvm/Support/Compression.h"
49 #include "llvm/Support/Path.h"
50 #include "llvm/Support/TarWriter.h"
51 #include "llvm/Support/TargetSelect.h"
52 #include "llvm/Support/raw_ostream.h"
53 #include <cstdlib>
54 #include <utility>
55 
56 using namespace llvm;
57 using namespace llvm::ELF;
58 using namespace llvm::object;
59 using namespace llvm::sys;
60 
61 using namespace lld;
62 using namespace lld::elf;
63 
64 Configuration *elf::Config;
65 LinkerDriver *elf::Driver;
66 
67 BumpPtrAllocator elf::BAlloc;
68 StringSaver elf::Saver{BAlloc};
69 std::vector<SpecificAllocBase *> elf::SpecificAllocBase::Instances;
70 
71 static void setConfigs();
72 
73 bool elf::link(ArrayRef<const char *> Args, bool CanExitEarly,
74                raw_ostream &Error) {
75   errorHandler().LogName = Args[0];
76   errorHandler().ErrorLimitExceededMsg =
77       "too many errors emitted, stopping now (use "
78       "-error-limit=0 to see all errors)";
79   errorHandler().ErrorOS = &Error;
80   errorHandler().ColorDiagnostics = Error.has_colors();
81   InputSections.clear();
82   OutputSections.clear();
83   Tar = nullptr;
84   BinaryFiles.clear();
85   BitcodeFiles.clear();
86   ObjectFiles.clear();
87   SharedFiles.clear();
88 
89   Config = make<Configuration>();
90   Driver = make<LinkerDriver>();
91   Script = make<LinkerScript>();
92   Symtab = make<SymbolTable>();
93   Config->Argv = {Args.begin(), Args.end()};
94 
95   Driver->main(Args, CanExitEarly);
96   waitForBackgroundThreads();
97 
98   // Exit immediately if we don't need to return to the caller.
99   // This saves time because the overhead of calling destructors
100   // for all globally-allocated objects is not negligible.
101   if (Config->ExitEarly)
102     exitLld(errorCount() ? 1 : 0);
103 
104   freeArena();
105   return !errorCount();
106 }
107 
108 // Parses a linker -m option.
109 static std::tuple<ELFKind, uint16_t, uint8_t> parseEmulation(StringRef Emul) {
110   uint8_t OSABI = 0;
111   StringRef S = Emul;
112   if (S.endswith("_fbsd")) {
113     S = S.drop_back(5);
114     OSABI = ELFOSABI_FREEBSD;
115   }
116 
117   std::pair<ELFKind, uint16_t> Ret =
118       StringSwitch<std::pair<ELFKind, uint16_t>>(S)
119           .Cases("aarch64elf", "aarch64linux", {ELF64LEKind, EM_AARCH64})
120           .Cases("armelf", "armelf_linux_eabi", {ELF32LEKind, EM_ARM})
121           .Case("elf32_x86_64", {ELF32LEKind, EM_X86_64})
122           .Cases("elf32btsmip", "elf32btsmipn32", {ELF32BEKind, EM_MIPS})
123           .Cases("elf32ltsmip", "elf32ltsmipn32", {ELF32LEKind, EM_MIPS})
124           .Case("elf32ppc", {ELF32BEKind, EM_PPC})
125           .Case("elf64btsmip", {ELF64BEKind, EM_MIPS})
126           .Case("elf64ltsmip", {ELF64LEKind, EM_MIPS})
127           .Case("elf64ppc", {ELF64BEKind, EM_PPC64})
128           .Cases("elf_amd64", "elf_x86_64", {ELF64LEKind, EM_X86_64})
129           .Case("elf_i386", {ELF32LEKind, EM_386})
130           .Case("elf_iamcu", {ELF32LEKind, EM_IAMCU})
131           .Default({ELFNoneKind, EM_NONE});
132 
133   if (Ret.first == ELFNoneKind)
134     error("unknown emulation: " + Emul);
135   return std::make_tuple(Ret.first, Ret.second, OSABI);
136 }
137 
138 // Returns slices of MB by parsing MB as an archive file.
139 // Each slice consists of a member file in the archive.
140 std::vector<std::pair<MemoryBufferRef, uint64_t>> static getArchiveMembers(
141     MemoryBufferRef MB) {
142   std::unique_ptr<Archive> File =
143       check(Archive::create(MB),
144             MB.getBufferIdentifier() + ": failed to parse archive");
145 
146   std::vector<std::pair<MemoryBufferRef, uint64_t>> V;
147   Error Err = Error::success();
148   bool AddToTar = File->isThin() && Tar;
149   for (const ErrorOr<Archive::Child> &COrErr : File->children(Err)) {
150     Archive::Child C =
151         check(COrErr, MB.getBufferIdentifier() +
152                           ": could not get the child of the archive");
153     MemoryBufferRef MBRef =
154         check(C.getMemoryBufferRef(),
155               MB.getBufferIdentifier() +
156                   ": could not get the buffer for a child of the archive");
157     if (AddToTar)
158       Tar->append(relativeToRoot(check(C.getFullName())), MBRef.getBuffer());
159     V.push_back(std::make_pair(MBRef, C.getChildOffset()));
160   }
161   if (Err)
162     fatal(MB.getBufferIdentifier() + ": Archive::children failed: " +
163           toString(std::move(Err)));
164 
165   // Take ownership of memory buffers created for members of thin archives.
166   for (std::unique_ptr<MemoryBuffer> &MB : File->takeThinBuffers())
167     make<std::unique_ptr<MemoryBuffer>>(std::move(MB));
168 
169   return V;
170 }
171 
172 // Opens a file and create a file object. Path has to be resolved already.
173 void LinkerDriver::addFile(StringRef Path, bool WithLOption) {
174   using namespace sys::fs;
175 
176   Optional<MemoryBufferRef> Buffer = readFile(Path);
177   if (!Buffer.hasValue())
178     return;
179   MemoryBufferRef MBRef = *Buffer;
180 
181   if (InBinary) {
182     Files.push_back(make<BinaryFile>(MBRef));
183     return;
184   }
185 
186   switch (identify_magic(MBRef.getBuffer())) {
187   case file_magic::unknown:
188     readLinkerScript(MBRef);
189     return;
190   case file_magic::archive: {
191     // Handle -whole-archive.
192     if (InWholeArchive) {
193       for (const auto &P : getArchiveMembers(MBRef))
194         Files.push_back(createObjectFile(P.first, Path, P.second));
195       return;
196     }
197 
198     std::unique_ptr<Archive> File =
199         check(Archive::create(MBRef), Path + ": failed to parse archive");
200 
201     // If an archive file has no symbol table, it is likely that a user
202     // is attempting LTO and using a default ar command that doesn't
203     // understand the LLVM bitcode file. It is a pretty common error, so
204     // we'll handle it as if it had a symbol table.
205     if (!File->isEmpty() && !File->hasSymbolTable()) {
206       for (const auto &P : getArchiveMembers(MBRef))
207         Files.push_back(make<LazyObjFile>(P.first, Path, P.second));
208       return;
209     }
210 
211     // Handle the regular case.
212     Files.push_back(make<ArchiveFile>(std::move(File)));
213     return;
214   }
215   case file_magic::elf_shared_object:
216     if (Config->Relocatable) {
217       error("attempted static link of dynamic object " + Path);
218       return;
219     }
220 
221     // DSOs usually have DT_SONAME tags in their ELF headers, and the
222     // sonames are used to identify DSOs. But if they are missing,
223     // they are identified by filenames. We don't know whether the new
224     // file has a DT_SONAME or not because we haven't parsed it yet.
225     // Here, we set the default soname for the file because we might
226     // need it later.
227     //
228     // If a file was specified by -lfoo, the directory part is not
229     // significant, as a user did not specify it. This behavior is
230     // compatible with GNU.
231     Files.push_back(
232         createSharedFile(MBRef, WithLOption ? path::filename(Path) : Path));
233     return;
234   default:
235     if (InLib)
236       Files.push_back(make<LazyObjFile>(MBRef, "", 0));
237     else
238       Files.push_back(createObjectFile(MBRef));
239   }
240 }
241 
242 // Add a given library by searching it from input search paths.
243 void LinkerDriver::addLibrary(StringRef Name) {
244   if (Optional<std::string> Path = searchLibrary(Name))
245     addFile(*Path, /*WithLOption=*/true);
246   else
247     error("unable to find library -l" + Name);
248 }
249 
250 // This function is called on startup. We need this for LTO since
251 // LTO calls LLVM functions to compile bitcode files to native code.
252 // Technically this can be delayed until we read bitcode files, but
253 // we don't bother to do lazily because the initialization is fast.
254 static void initLLVM(opt::InputArgList &Args) {
255   InitializeAllTargets();
256   InitializeAllTargetMCs();
257   InitializeAllAsmPrinters();
258   InitializeAllAsmParsers();
259 
260   // Parse and evaluate -mllvm options.
261   std::vector<const char *> V;
262   V.push_back("lld (LLVM option parsing)");
263   for (auto *Arg : Args.filtered(OPT_mllvm))
264     V.push_back(Arg->getValue());
265   cl::ParseCommandLineOptions(V.size(), V.data());
266 }
267 
268 // Some command line options or some combinations of them are not allowed.
269 // This function checks for such errors.
270 static void checkOptions(opt::InputArgList &Args) {
271   // The MIPS ABI as of 2016 does not support the GNU-style symbol lookup
272   // table which is a relatively new feature.
273   if (Config->EMachine == EM_MIPS && Config->GnuHash)
274     error("the .gnu.hash section is not compatible with the MIPS target.");
275 
276   if (Config->Pie && Config->Shared)
277     error("-shared and -pie may not be used together");
278 
279   if (!Config->Shared && !Config->FilterList.empty())
280     error("-F may not be used without -shared");
281 
282   if (!Config->Shared && !Config->AuxiliaryList.empty())
283     error("-f may not be used without -shared");
284 
285   if (Config->Relocatable) {
286     if (Config->Shared)
287       error("-r and -shared may not be used together");
288     if (Config->GcSections)
289       error("-r and --gc-sections may not be used together");
290     if (Config->ICF)
291       error("-r and --icf may not be used together");
292     if (Config->Pie)
293       error("-r and -pie may not be used together");
294   }
295 }
296 
297 static int getInteger(opt::InputArgList &Args, unsigned Key, int Default) {
298   int V = Default;
299   if (auto *Arg = Args.getLastArg(Key)) {
300     StringRef S = Arg->getValue();
301     if (!to_integer(S, V, 10))
302       error(Arg->getSpelling() + ": number expected, but got '" + S + "'");
303   }
304   return V;
305 }
306 
307 static const char *getReproduceOption(opt::InputArgList &Args) {
308   if (auto *Arg = Args.getLastArg(OPT_reproduce))
309     return Arg->getValue();
310   return getenv("LLD_REPRODUCE");
311 }
312 
313 static bool hasZOption(opt::InputArgList &Args, StringRef Key) {
314   for (auto *Arg : Args.filtered(OPT_z))
315     if (Key == Arg->getValue())
316       return true;
317   return false;
318 }
319 
320 static uint64_t getZOptionValue(opt::InputArgList &Args, StringRef Key,
321                                 uint64_t Default) {
322   for (auto *Arg : Args.filtered(OPT_z)) {
323     std::pair<StringRef, StringRef> KV = StringRef(Arg->getValue()).split('=');
324     if (KV.first == Key) {
325       uint64_t Result = Default;
326       if (!to_integer(KV.second, Result))
327         error("invalid " + Key + ": " + KV.second);
328       return Result;
329     }
330   }
331   return Default;
332 }
333 
334 void LinkerDriver::main(ArrayRef<const char *> ArgsArr, bool CanExitEarly) {
335   ELFOptTable Parser;
336   opt::InputArgList Args = Parser.parse(ArgsArr.slice(1));
337 
338   // Interpret this flag early because error() depends on them.
339   errorHandler().ErrorLimit = getInteger(Args, OPT_error_limit, 20);
340 
341   // Handle -help
342   if (Args.hasArg(OPT_help)) {
343     printHelp(ArgsArr[0]);
344     return;
345   }
346 
347   // Handle -v or -version.
348   //
349   // A note about "compatible with GNU linkers" message: this is a hack for
350   // scripts generated by GNU Libtool 2.4.6 (released in February 2014 and
351   // still the newest version in March 2017) or earlier to recognize LLD as
352   // a GNU compatible linker. As long as an output for the -v option
353   // contains "GNU" or "with BFD", they recognize us as GNU-compatible.
354   //
355   // This is somewhat ugly hack, but in reality, we had no choice other
356   // than doing this. Considering the very long release cycle of Libtool,
357   // it is not easy to improve it to recognize LLD as a GNU compatible
358   // linker in a timely manner. Even if we can make it, there are still a
359   // lot of "configure" scripts out there that are generated by old version
360   // of Libtool. We cannot convince every software developer to migrate to
361   // the latest version and re-generate scripts. So we have this hack.
362   if (Args.hasArg(OPT_v) || Args.hasArg(OPT_version))
363     message(getLLDVersion() + " (compatible with GNU linkers)");
364 
365   // ld.bfd always exits after printing out the version string.
366   // ld.gold proceeds if a given option is -v. Because gold's behavior
367   // is more permissive than ld.bfd, we chose what gold does here.
368   if (Args.hasArg(OPT_version))
369     return;
370 
371   Config->ExitEarly = CanExitEarly && !Args.hasArg(OPT_full_shutdown);
372   errorHandler().ExitEarly = Config->ExitEarly;
373 
374   if (const char *Path = getReproduceOption(Args)) {
375     // Note that --reproduce is a debug option so you can ignore it
376     // if you are trying to understand the whole picture of the code.
377     Expected<std::unique_ptr<TarWriter>> ErrOrWriter =
378         TarWriter::create(Path, path::stem(Path));
379     if (ErrOrWriter) {
380       Tar = ErrOrWriter->get();
381       Tar->append("response.txt", createResponseFile(Args));
382       Tar->append("version.txt", getLLDVersion() + "\n");
383       make<std::unique_ptr<TarWriter>>(std::move(*ErrOrWriter));
384     } else {
385       error(Twine("--reproduce: failed to open ") + Path + ": " +
386             toString(ErrOrWriter.takeError()));
387     }
388   }
389 
390   readConfigs(Args);
391   initLLVM(Args);
392   createFiles(Args);
393   inferMachineType();
394   setConfigs();
395   checkOptions(Args);
396   if (errorCount())
397     return;
398 
399   switch (Config->EKind) {
400   case ELF32LEKind:
401     link<ELF32LE>(Args);
402     return;
403   case ELF32BEKind:
404     link<ELF32BE>(Args);
405     return;
406   case ELF64LEKind:
407     link<ELF64LE>(Args);
408     return;
409   case ELF64BEKind:
410     link<ELF64BE>(Args);
411     return;
412   default:
413     llvm_unreachable("unknown Config->EKind");
414   }
415 }
416 
417 static std::vector<StringRef> getArgs(opt::InputArgList &Args, int Id) {
418   std::vector<StringRef> V;
419   for (auto *Arg : Args.filtered(Id))
420     V.push_back(Arg->getValue());
421   return V;
422 }
423 
424 static std::string getRpath(opt::InputArgList &Args) {
425   std::vector<StringRef> V = getArgs(Args, OPT_rpath);
426   return llvm::join(V.begin(), V.end(), ":");
427 }
428 
429 // Determines what we should do if there are remaining unresolved
430 // symbols after the name resolution.
431 static UnresolvedPolicy getUnresolvedSymbolPolicy(opt::InputArgList &Args) {
432   if (Args.hasArg(OPT_relocatable))
433     return UnresolvedPolicy::IgnoreAll;
434 
435   UnresolvedPolicy ErrorOrWarn = Args.hasFlag(OPT_error_unresolved_symbols,
436                                               OPT_warn_unresolved_symbols, true)
437                                      ? UnresolvedPolicy::ReportError
438                                      : UnresolvedPolicy::Warn;
439 
440   // Process the last of -unresolved-symbols, -no-undefined or -z defs.
441   for (auto *Arg : llvm::reverse(Args)) {
442     switch (Arg->getOption().getID()) {
443     case OPT_unresolved_symbols: {
444       StringRef S = Arg->getValue();
445       if (S == "ignore-all" || S == "ignore-in-object-files")
446         return UnresolvedPolicy::Ignore;
447       if (S == "ignore-in-shared-libs" || S == "report-all")
448         return ErrorOrWarn;
449       error("unknown --unresolved-symbols value: " + S);
450       continue;
451     }
452     case OPT_no_undefined:
453       return ErrorOrWarn;
454     case OPT_z:
455       if (StringRef(Arg->getValue()) == "defs")
456         return ErrorOrWarn;
457       continue;
458     }
459   }
460 
461   // -shared implies -unresolved-symbols=ignore-all because missing
462   // symbols are likely to be resolved at runtime using other DSOs.
463   if (Config->Shared)
464     return UnresolvedPolicy::Ignore;
465   return ErrorOrWarn;
466 }
467 
468 static Target2Policy getTarget2(opt::InputArgList &Args) {
469   StringRef S = Args.getLastArgValue(OPT_target2, "got-rel");
470   if (S == "rel")
471     return Target2Policy::Rel;
472   if (S == "abs")
473     return Target2Policy::Abs;
474   if (S == "got-rel")
475     return Target2Policy::GotRel;
476   error("unknown --target2 option: " + S);
477   return Target2Policy::GotRel;
478 }
479 
480 static bool isOutputFormatBinary(opt::InputArgList &Args) {
481   if (auto *Arg = Args.getLastArg(OPT_oformat)) {
482     StringRef S = Arg->getValue();
483     if (S == "binary")
484       return true;
485     error("unknown --oformat value: " + S);
486   }
487   return false;
488 }
489 
490 static DiscardPolicy getDiscard(opt::InputArgList &Args) {
491   if (Args.hasArg(OPT_relocatable))
492     return DiscardPolicy::None;
493 
494   auto *Arg =
495       Args.getLastArg(OPT_discard_all, OPT_discard_locals, OPT_discard_none);
496   if (!Arg)
497     return DiscardPolicy::Default;
498   if (Arg->getOption().getID() == OPT_discard_all)
499     return DiscardPolicy::All;
500   if (Arg->getOption().getID() == OPT_discard_locals)
501     return DiscardPolicy::Locals;
502   return DiscardPolicy::None;
503 }
504 
505 static StringRef getDynamicLinker(opt::InputArgList &Args) {
506   auto *Arg = Args.getLastArg(OPT_dynamic_linker, OPT_no_dynamic_linker);
507   if (!Arg || Arg->getOption().getID() == OPT_no_dynamic_linker)
508     return "";
509   return Arg->getValue();
510 }
511 
512 static StripPolicy getStrip(opt::InputArgList &Args) {
513   if (Args.hasArg(OPT_relocatable))
514     return StripPolicy::None;
515 
516   auto *Arg = Args.getLastArg(OPT_strip_all, OPT_strip_debug);
517   if (!Arg)
518     return StripPolicy::None;
519   if (Arg->getOption().getID() == OPT_strip_all)
520     return StripPolicy::All;
521   return StripPolicy::Debug;
522 }
523 
524 static uint64_t parseSectionAddress(StringRef S, opt::Arg *Arg) {
525   uint64_t VA = 0;
526   if (S.startswith("0x"))
527     S = S.drop_front(2);
528   if (!to_integer(S, VA, 16))
529     error("invalid argument: " + toString(Arg));
530   return VA;
531 }
532 
533 static StringMap<uint64_t> getSectionStartMap(opt::InputArgList &Args) {
534   StringMap<uint64_t> Ret;
535   for (auto *Arg : Args.filtered(OPT_section_start)) {
536     StringRef Name;
537     StringRef Addr;
538     std::tie(Name, Addr) = StringRef(Arg->getValue()).split('=');
539     Ret[Name] = parseSectionAddress(Addr, Arg);
540   }
541 
542   if (auto *Arg = Args.getLastArg(OPT_Ttext))
543     Ret[".text"] = parseSectionAddress(Arg->getValue(), Arg);
544   if (auto *Arg = Args.getLastArg(OPT_Tdata))
545     Ret[".data"] = parseSectionAddress(Arg->getValue(), Arg);
546   if (auto *Arg = Args.getLastArg(OPT_Tbss))
547     Ret[".bss"] = parseSectionAddress(Arg->getValue(), Arg);
548   return Ret;
549 }
550 
551 static SortSectionPolicy getSortSection(opt::InputArgList &Args) {
552   StringRef S = Args.getLastArgValue(OPT_sort_section);
553   if (S == "alignment")
554     return SortSectionPolicy::Alignment;
555   if (S == "name")
556     return SortSectionPolicy::Name;
557   if (!S.empty())
558     error("unknown --sort-section rule: " + S);
559   return SortSectionPolicy::Default;
560 }
561 
562 static OrphanHandlingPolicy getOrphanHandling(opt::InputArgList &Args) {
563   StringRef S = Args.getLastArgValue(OPT_orphan_handling, "place");
564   if (S == "warn")
565     return OrphanHandlingPolicy::Warn;
566   if (S == "error")
567     return OrphanHandlingPolicy::Error;
568   if (S != "place")
569     error("unknown --orphan-handling mode: " + S);
570   return OrphanHandlingPolicy::Place;
571 }
572 
573 // Parse --build-id or --build-id=<style>. We handle "tree" as a
574 // synonym for "sha1" because all our hash functions including
575 // -build-id=sha1 are actually tree hashes for performance reasons.
576 static std::pair<BuildIdKind, std::vector<uint8_t>>
577 getBuildId(opt::InputArgList &Args) {
578   auto *Arg = Args.getLastArg(OPT_build_id, OPT_build_id_eq);
579   if (!Arg)
580     return {BuildIdKind::None, {}};
581 
582   if (Arg->getOption().getID() == OPT_build_id)
583     return {BuildIdKind::Fast, {}};
584 
585   StringRef S = Arg->getValue();
586   if (S == "md5")
587     return {BuildIdKind::Md5, {}};
588   if (S == "sha1" || S == "tree")
589     return {BuildIdKind::Sha1, {}};
590   if (S == "uuid")
591     return {BuildIdKind::Uuid, {}};
592   if (S.startswith("0x"))
593     return {BuildIdKind::Hexstring, parseHex(S.substr(2))};
594 
595   if (S != "none")
596     error("unknown --build-id style: " + S);
597   return {BuildIdKind::None, {}};
598 }
599 
600 static std::vector<StringRef> getLines(MemoryBufferRef MB) {
601   SmallVector<StringRef, 0> Arr;
602   MB.getBuffer().split(Arr, '\n');
603 
604   std::vector<StringRef> Ret;
605   for (StringRef S : Arr) {
606     S = S.trim();
607     if (!S.empty())
608       Ret.push_back(S);
609   }
610   return Ret;
611 }
612 
613 static bool getCompressDebugSections(opt::InputArgList &Args) {
614   StringRef S = Args.getLastArgValue(OPT_compress_debug_sections, "none");
615   if (S == "none")
616     return false;
617   if (S != "zlib")
618     error("unknown --compress-debug-sections value: " + S);
619   if (!zlib::isAvailable())
620     error("--compress-debug-sections: zlib is not available");
621   return true;
622 }
623 
624 static int parseInt(StringRef S, opt::Arg *Arg) {
625   int V = 0;
626   if (!to_integer(S, V, 10))
627     error(Arg->getSpelling() + ": number expected, but got '" + S + "'");
628   return V;
629 }
630 
631 // Initializes Config members by the command line options.
632 void LinkerDriver::readConfigs(opt::InputArgList &Args) {
633   Config->AllowMultipleDefinition =
634       Args.hasArg(OPT_allow_multiple_definition) || hasZOption(Args, "muldefs");
635   Config->AuxiliaryList = getArgs(Args, OPT_auxiliary);
636   Config->Bsymbolic = Args.hasArg(OPT_Bsymbolic);
637   Config->BsymbolicFunctions = Args.hasArg(OPT_Bsymbolic_functions);
638   Config->Chroot = Args.getLastArgValue(OPT_chroot);
639   Config->CompressDebugSections = getCompressDebugSections(Args);
640   Config->DefineCommon = Args.hasFlag(OPT_define_common, OPT_no_define_common,
641                                       !Args.hasArg(OPT_relocatable));
642   Config->Demangle = Args.hasFlag(OPT_demangle, OPT_no_demangle, true);
643   Config->DisableVerify = Args.hasArg(OPT_disable_verify);
644   Config->Discard = getDiscard(Args);
645   Config->DynamicLinker = getDynamicLinker(Args);
646   Config->EhFrameHdr =
647       Args.hasFlag(OPT_eh_frame_hdr, OPT_no_eh_frame_hdr, false);
648   Config->EmitRelocs = Args.hasArg(OPT_emit_relocs);
649   Config->EnableNewDtags = !Args.hasArg(OPT_disable_new_dtags);
650   Config->Entry = Args.getLastArgValue(OPT_entry);
651   Config->ExportDynamic =
652       Args.hasFlag(OPT_export_dynamic, OPT_no_export_dynamic, false);
653   errorHandler().FatalWarnings =
654       Args.hasFlag(OPT_fatal_warnings, OPT_no_fatal_warnings, false);
655   Config->FilterList = getArgs(Args, OPT_filter);
656   Config->Fini = Args.getLastArgValue(OPT_fini, "_fini");
657   Config->GcSections = Args.hasFlag(OPT_gc_sections, OPT_no_gc_sections, false);
658   Config->GdbIndex = Args.hasFlag(OPT_gdb_index, OPT_no_gdb_index, false);
659   Config->ICF = Args.hasFlag(OPT_icf_all, OPT_icf_none, false);
660   Config->Init = Args.getLastArgValue(OPT_init, "_init");
661   Config->LTOAAPipeline = Args.getLastArgValue(OPT_lto_aa_pipeline);
662   Config->LTONewPmPasses = Args.getLastArgValue(OPT_lto_newpm_passes);
663   Config->LTOO = getInteger(Args, OPT_lto_O, 2);
664   Config->LTOPartitions = getInteger(Args, OPT_lto_partitions, 1);
665   Config->MapFile = Args.getLastArgValue(OPT_Map);
666   Config->NoGnuUnique = Args.hasArg(OPT_no_gnu_unique);
667   Config->NoUndefinedVersion = Args.hasArg(OPT_no_undefined_version);
668   Config->NoinhibitExec = Args.hasArg(OPT_noinhibit_exec);
669   Config->Nostdlib = Args.hasArg(OPT_nostdlib);
670   Config->OFormatBinary = isOutputFormatBinary(Args);
671   Config->Omagic = Args.hasFlag(OPT_omagic, OPT_no_omagic, false);
672   Config->OptRemarksFilename = Args.getLastArgValue(OPT_opt_remarks_filename);
673   Config->OptRemarksWithHotness = Args.hasArg(OPT_opt_remarks_with_hotness);
674   Config->Optimize = getInteger(Args, OPT_O, 1);
675   Config->OrphanHandling = getOrphanHandling(Args);
676   Config->OutputFile = Args.getLastArgValue(OPT_o);
677   Config->Pie = Args.hasFlag(OPT_pie, OPT_nopie, false);
678   Config->PrintGcSections =
679       Args.hasFlag(OPT_print_gc_sections, OPT_no_print_gc_sections, false);
680   Config->Rpath = getRpath(Args);
681   Config->Relocatable = Args.hasArg(OPT_relocatable);
682   Config->SaveTemps = Args.hasArg(OPT_save_temps);
683   Config->SearchPaths = getArgs(Args, OPT_library_path);
684   Config->SectionStartMap = getSectionStartMap(Args);
685   Config->Shared = Args.hasArg(OPT_shared);
686   Config->SingleRoRx = Args.hasArg(OPT_no_rosegment);
687   Config->SoName = Args.getLastArgValue(OPT_soname);
688   Config->SortSection = getSortSection(Args);
689   Config->Strip = getStrip(Args);
690   Config->Sysroot = Args.getLastArgValue(OPT_sysroot);
691   Config->Target1Rel = Args.hasFlag(OPT_target1_rel, OPT_target1_abs, false);
692   Config->Target2 = getTarget2(Args);
693   Config->ThinLTOCacheDir = Args.getLastArgValue(OPT_thinlto_cache_dir);
694   Config->ThinLTOCachePolicy = check(
695       parseCachePruningPolicy(Args.getLastArgValue(OPT_thinlto_cache_policy)),
696       "--thinlto-cache-policy: invalid cache policy");
697   Config->ThinLTOJobs = getInteger(Args, OPT_thinlto_jobs, -1u);
698   ThreadsEnabled = Args.hasFlag(OPT_threads, OPT_no_threads, true);
699   Config->Trace = Args.hasArg(OPT_trace);
700   Config->Undefined = getArgs(Args, OPT_undefined);
701   Config->UnresolvedSymbols = getUnresolvedSymbolPolicy(Args);
702   Config->Verbose = Args.hasArg(OPT_verbose);
703   errorHandler().Verbose = Config->Verbose;
704   Config->WarnCommon = Args.hasArg(OPT_warn_common);
705   Config->ZCombreloc = !hasZOption(Args, "nocombreloc");
706   Config->ZExecstack = hasZOption(Args, "execstack");
707   Config->ZNocopyreloc = hasZOption(Args, "nocopyreloc");
708   Config->ZNodelete = hasZOption(Args, "nodelete");
709   Config->ZNodlopen = hasZOption(Args, "nodlopen");
710   Config->ZNow = hasZOption(Args, "now");
711   Config->ZOrigin = hasZOption(Args, "origin");
712   Config->ZRelro = !hasZOption(Args, "norelro");
713   Config->ZRodynamic = hasZOption(Args, "rodynamic");
714   Config->ZStackSize = getZOptionValue(Args, "stack-size", 0);
715   Config->ZText = !hasZOption(Args, "notext");
716   Config->ZWxneeded = hasZOption(Args, "wxneeded");
717 
718   // Parse LTO plugin-related options for compatibility with gold.
719   for (auto *Arg : Args.filtered(OPT_plugin_opt, OPT_plugin_opt_eq)) {
720     StringRef S = Arg->getValue();
721     if (S == "disable-verify")
722       Config->DisableVerify = true;
723     else if (S == "save-temps")
724       Config->SaveTemps = true;
725     else if (S.startswith("O"))
726       Config->LTOO = parseInt(S.substr(1), Arg);
727     else if (S.startswith("lto-partitions="))
728       Config->LTOPartitions = parseInt(S.substr(15), Arg);
729     else if (S.startswith("jobs="))
730       Config->ThinLTOJobs = parseInt(S.substr(5), Arg);
731     else if (!S.startswith("/") && !S.startswith("-fresolution=") &&
732              !S.startswith("-pass-through=") && !S.startswith("mcpu=") &&
733              !S.startswith("thinlto") && S != "-function-sections" &&
734              S != "-data-sections")
735       error(Arg->getSpelling() + ": unknown option: " + S);
736   }
737 
738   if (Config->LTOO > 3)
739     error("invalid optimization level for LTO: " + Twine(Config->LTOO));
740   if (Config->LTOPartitions == 0)
741     error("--lto-partitions: number of threads must be > 0");
742   if (Config->ThinLTOJobs == 0)
743     error("--thinlto-jobs: number of threads must be > 0");
744 
745   // Parse ELF{32,64}{LE,BE} and CPU type.
746   if (auto *Arg = Args.getLastArg(OPT_m)) {
747     StringRef S = Arg->getValue();
748     std::tie(Config->EKind, Config->EMachine, Config->OSABI) =
749         parseEmulation(S);
750     Config->MipsN32Abi = (S == "elf32btsmipn32" || S == "elf32ltsmipn32");
751     Config->Emulation = S;
752   }
753 
754   // Parse -hash-style={sysv,gnu,both}.
755   if (auto *Arg = Args.getLastArg(OPT_hash_style)) {
756     StringRef S = Arg->getValue();
757     if (S == "sysv")
758       Config->SysvHash = true;
759     else if (S == "gnu")
760       Config->GnuHash = true;
761     else if (S == "both")
762       Config->SysvHash = Config->GnuHash = true;
763     else
764       error("unknown -hash-style: " + S);
765   }
766 
767   if (Args.hasArg(OPT_print_map))
768     Config->MapFile = "-";
769 
770   // --omagic is an option to create old-fashioned executables in which
771   // .text segments are writable. Today, the option is still in use to
772   // create special-purpose programs such as boot loaders. It doesn't
773   // make sense to create PT_GNU_RELRO for such executables.
774   if (Config->Omagic)
775     Config->ZRelro = false;
776 
777   std::tie(Config->BuildId, Config->BuildIdVector) = getBuildId(Args);
778 
779   if (auto *Arg = Args.getLastArg(OPT_pack_dyn_relocs_eq)) {
780     StringRef S = Arg->getValue();
781     if (S == "android")
782       Config->AndroidPackDynRelocs = true;
783     else if (S != "none")
784       error("unknown -pack-dyn-relocs format: " + S);
785   }
786 
787   if (auto *Arg = Args.getLastArg(OPT_symbol_ordering_file))
788     if (Optional<MemoryBufferRef> Buffer = readFile(Arg->getValue()))
789       Config->SymbolOrderingFile = getLines(*Buffer);
790 
791   // If --retain-symbol-file is used, we'll keep only the symbols listed in
792   // the file and discard all others.
793   if (auto *Arg = Args.getLastArg(OPT_retain_symbols_file)) {
794     Config->DefaultSymbolVersion = VER_NDX_LOCAL;
795     if (Optional<MemoryBufferRef> Buffer = readFile(Arg->getValue()))
796       for (StringRef S : getLines(*Buffer))
797         Config->VersionScriptGlobals.push_back(
798             {S, /*IsExternCpp*/ false, /*HasWildcard*/ false});
799   }
800 
801   bool HasExportDynamic =
802       Args.hasFlag(OPT_export_dynamic, OPT_no_export_dynamic, false);
803 
804   // Parses -dynamic-list and -export-dynamic-symbol. They make some
805   // symbols private. Note that -export-dynamic takes precedence over them
806   // as it says all symbols should be exported.
807   if (!HasExportDynamic) {
808     for (auto *Arg : Args.filtered(OPT_dynamic_list))
809       if (Optional<MemoryBufferRef> Buffer = readFile(Arg->getValue()))
810         readDynamicList(*Buffer);
811 
812     for (auto *Arg : Args.filtered(OPT_export_dynamic_symbol))
813       Config->DynamicList.push_back(
814           {Arg->getValue(), /*IsExternCpp*/ false, /*HasWildcard*/ false});
815   }
816 
817   if (auto *Arg = Args.getLastArg(OPT_version_script))
818     if (Optional<MemoryBufferRef> Buffer = readFile(Arg->getValue()))
819       readVersionScript(*Buffer);
820 }
821 
822 // Some Config members do not directly correspond to any particular
823 // command line options, but computed based on other Config values.
824 // This function initialize such members. See Config.h for the details
825 // of these values.
826 static void setConfigs() {
827   ELFKind Kind = Config->EKind;
828   uint16_t Machine = Config->EMachine;
829 
830   // There is an ILP32 ABI for x86-64, although it's not very popular.
831   // It is called the x32 ABI.
832   bool IsX32 = (Kind == ELF32LEKind && Machine == EM_X86_64);
833 
834   Config->CopyRelocs = (Config->Relocatable || Config->EmitRelocs);
835   Config->Is64 = (Kind == ELF64LEKind || Kind == ELF64BEKind);
836   Config->IsLE = (Kind == ELF32LEKind || Kind == ELF64LEKind);
837   Config->Endianness =
838       Config->IsLE ? support::endianness::little : support::endianness::big;
839   Config->IsMips64EL = (Kind == ELF64LEKind && Machine == EM_MIPS);
840   Config->IsRela = Config->Is64 || IsX32 || Config->MipsN32Abi;
841   Config->Pic = Config->Pie || Config->Shared;
842   Config->Wordsize = Config->Is64 ? 8 : 4;
843 }
844 
845 // Returns a value of "-format" option.
846 static bool getBinaryOption(StringRef S) {
847   if (S == "binary")
848     return true;
849   if (S == "elf" || S == "default")
850     return false;
851   error("unknown -format value: " + S +
852         " (supported formats: elf, default, binary)");
853   return false;
854 }
855 
856 void LinkerDriver::createFiles(opt::InputArgList &Args) {
857   for (auto *Arg : Args) {
858     switch (Arg->getOption().getUnaliasedOption().getID()) {
859     case OPT_library:
860       addLibrary(Arg->getValue());
861       break;
862     case OPT_INPUT:
863       addFile(Arg->getValue(), /*WithLOption=*/false);
864       break;
865     case OPT_script:
866       if (Optional<MemoryBufferRef> MB = readFile(Arg->getValue()))
867         readLinkerScript(*MB);
868       break;
869     case OPT_as_needed:
870       Config->AsNeeded = true;
871       break;
872     case OPT_format:
873       InBinary = getBinaryOption(Arg->getValue());
874       break;
875     case OPT_no_as_needed:
876       Config->AsNeeded = false;
877       break;
878     case OPT_Bstatic:
879       Config->Static = true;
880       break;
881     case OPT_Bdynamic:
882       Config->Static = false;
883       break;
884     case OPT_whole_archive:
885       InWholeArchive = true;
886       break;
887     case OPT_no_whole_archive:
888       InWholeArchive = false;
889       break;
890     case OPT_start_lib:
891       InLib = true;
892       break;
893     case OPT_end_lib:
894       InLib = false;
895       break;
896     }
897   }
898 
899   if (Files.empty() && errorCount() == 0)
900     error("no input files");
901 }
902 
903 // If -m <machine_type> was not given, infer it from object files.
904 void LinkerDriver::inferMachineType() {
905   if (Config->EKind != ELFNoneKind)
906     return;
907 
908   for (InputFile *F : Files) {
909     if (F->EKind == ELFNoneKind)
910       continue;
911     Config->EKind = F->EKind;
912     Config->EMachine = F->EMachine;
913     Config->OSABI = F->OSABI;
914     Config->MipsN32Abi = Config->EMachine == EM_MIPS && isMipsN32Abi(F);
915     return;
916   }
917   error("target emulation unknown: -m or at least one .o file required");
918 }
919 
920 // Parse -z max-page-size=<value>. The default value is defined by
921 // each target.
922 static uint64_t getMaxPageSize(opt::InputArgList &Args) {
923   uint64_t Val =
924       getZOptionValue(Args, "max-page-size", Target->DefaultMaxPageSize);
925   if (!isPowerOf2_64(Val))
926     error("max-page-size: value isn't a power of 2");
927   return Val;
928 }
929 
930 // Parses -image-base option.
931 static Optional<uint64_t> getImageBase(opt::InputArgList &Args) {
932   // Because we are using "Config->MaxPageSize" here, this function has to be
933   // called after the variable is initialized.
934   auto *Arg = Args.getLastArg(OPT_image_base);
935   if (!Arg)
936     return None;
937 
938   StringRef S = Arg->getValue();
939   uint64_t V;
940   if (!to_integer(S, V)) {
941     error("-image-base: number expected, but got " + S);
942     return 0;
943   }
944   if ((V % Config->MaxPageSize) != 0)
945     warn("-image-base: address isn't multiple of page size: " + S);
946   return V;
947 }
948 
949 // Parses `--exclude-libs=lib,lib,...`.
950 // The library names may be delimited by commas or colons.
951 static DenseSet<StringRef> getExcludeLibs(opt::InputArgList &Args) {
952   DenseSet<StringRef> Ret;
953   for (auto *Arg : Args.filtered(OPT_exclude_libs)) {
954     StringRef S = Arg->getValue();
955     for (;;) {
956       size_t Pos = S.find_first_of(",:");
957       if (Pos == StringRef::npos)
958         break;
959       Ret.insert(S.substr(0, Pos));
960       S = S.substr(Pos + 1);
961     }
962     Ret.insert(S);
963   }
964   return Ret;
965 }
966 
967 static Optional<StringRef> getArchiveName(InputFile *File) {
968   if (isa<ArchiveFile>(File))
969     return File->getName();
970   if (!File->ArchiveName.empty())
971     return File->ArchiveName;
972   return None;
973 }
974 
975 // Handles the -exclude-libs option. If a static library file is specified
976 // by the -exclude-libs option, all public symbols from the archive become
977 // private unless otherwise specified by version scripts or something.
978 // A special library name "ALL" means all archive files.
979 //
980 // This is not a popular option, but some programs such as bionic libc use it.
981 template <class ELFT>
982 static void excludeLibs(opt::InputArgList &Args, ArrayRef<InputFile *> Files) {
983   DenseSet<StringRef> Libs = getExcludeLibs(Args);
984   bool All = Libs.count("ALL");
985 
986   for (InputFile *File : Files)
987     if (Optional<StringRef> Archive = getArchiveName(File))
988       if (All || Libs.count(path::filename(*Archive)))
989         for (Symbol *Sym : File->getSymbols())
990           if (!Sym->isLocal())
991             Sym->VersionId = VER_NDX_LOCAL;
992 }
993 
994 // Do actual linking. Note that when this function is called,
995 // all linker scripts have already been parsed.
996 template <class ELFT> void LinkerDriver::link(opt::InputArgList &Args) {
997   Target = getTarget();
998 
999   Config->MaxPageSize = getMaxPageSize(Args);
1000   Config->ImageBase = getImageBase(Args);
1001 
1002   // If a -hash-style option was not given, set to a default value,
1003   // which varies depending on the target.
1004   if (!Args.hasArg(OPT_hash_style)) {
1005     if (Config->EMachine == EM_MIPS)
1006       Config->SysvHash = true;
1007     else
1008       Config->SysvHash = Config->GnuHash = true;
1009   }
1010 
1011   // Default output filename is "a.out" by the Unix tradition.
1012   if (Config->OutputFile.empty())
1013     Config->OutputFile = "a.out";
1014 
1015   // Fail early if the output file or map file is not writable. If a user has a
1016   // long link, e.g. due to a large LTO link, they do not wish to run it and
1017   // find that it failed because there was a mistake in their command-line.
1018   if (auto E = tryCreateFile(Config->OutputFile))
1019     error("cannot open output file " + Config->OutputFile + ": " + E.message());
1020   if (auto E = tryCreateFile(Config->MapFile))
1021     error("cannot open map file " + Config->MapFile + ": " + E.message());
1022   if (errorCount())
1023     return;
1024 
1025   // Use default entry point name if no name was given via the command
1026   // line nor linker scripts. For some reason, MIPS entry point name is
1027   // different from others.
1028   Config->WarnMissingEntry =
1029       (!Config->Entry.empty() || (!Config->Shared && !Config->Relocatable));
1030   if (Config->Entry.empty() && !Config->Relocatable)
1031     Config->Entry = (Config->EMachine == EM_MIPS) ? "__start" : "_start";
1032 
1033   // Handle --trace-symbol.
1034   for (auto *Arg : Args.filtered(OPT_trace_symbol))
1035     Symtab->trace(Arg->getValue());
1036 
1037   // Add all files to the symbol table. This will add almost all
1038   // symbols that we need to the symbol table.
1039   for (InputFile *F : Files)
1040     Symtab->addFile<ELFT>(F);
1041 
1042   // Process -defsym option.
1043   for (auto *Arg : Args.filtered(OPT_defsym)) {
1044     StringRef From;
1045     StringRef To;
1046     std::tie(From, To) = StringRef(Arg->getValue()).split('=');
1047     readDefsym(From, MemoryBufferRef(To, "-defsym"));
1048   }
1049 
1050   // Now that we have every file, we can decide if we will need a
1051   // dynamic symbol table.
1052   // We need one if we were asked to export dynamic symbols or if we are
1053   // producing a shared library.
1054   // We also need one if any shared libraries are used and for pie executables
1055   // (probably because the dynamic linker needs it).
1056   Config->HasDynSymTab =
1057       !SharedFiles.empty() || Config->Pic || Config->ExportDynamic;
1058 
1059   // Some symbols (such as __ehdr_start) are defined lazily only when there
1060   // are undefined symbols for them, so we add these to trigger that logic.
1061   for (StringRef Sym : Script->ReferencedSymbols)
1062     Symtab->addUndefined<ELFT>(Sym);
1063 
1064   // Handle the `--undefined <sym>` options.
1065   for (StringRef S : Config->Undefined)
1066     Symtab->fetchIfLazy<ELFT>(S);
1067 
1068   // If an entry symbol is in a static archive, pull out that file now
1069   // to complete the symbol table. After this, no new names except a
1070   // few linker-synthesized ones will be added to the symbol table.
1071   Symtab->fetchIfLazy<ELFT>(Config->Entry);
1072 
1073   // Return if there were name resolution errors.
1074   if (errorCount())
1075     return;
1076 
1077   // Handle undefined symbols in DSOs.
1078   Symtab->scanShlibUndefined<ELFT>();
1079 
1080   // Handle the -exclude-libs option.
1081   if (Args.hasArg(OPT_exclude_libs))
1082     excludeLibs<ELFT>(Args, Files);
1083 
1084   // Apply version scripts.
1085   Symtab->scanVersionScript();
1086 
1087   // Create wrapped symbols for -wrap option.
1088   for (auto *Arg : Args.filtered(OPT_wrap))
1089     Symtab->addSymbolWrap<ELFT>(Arg->getValue());
1090 
1091   Symtab->addCombinedLTOObject<ELFT>();
1092   if (errorCount())
1093     return;
1094 
1095   // Apply symbol renames for -wrap.
1096   Symtab->applySymbolWrap();
1097 
1098   // Now that we have a complete list of input files.
1099   // Beyond this point, no new files are added.
1100   // Aggregate all input sections into one place.
1101   for (InputFile *F : ObjectFiles)
1102     for (InputSectionBase *S : F->getSections())
1103       if (S && S != &InputSection::Discarded)
1104         InputSections.push_back(S);
1105   for (BinaryFile *F : BinaryFiles)
1106     for (InputSectionBase *S : F->getSections())
1107       InputSections.push_back(cast<InputSection>(S));
1108 
1109   // We do not want to emit debug sections if --strip-all
1110   // or -strip-debug are given.
1111   if (Config->Strip != StripPolicy::None)
1112     llvm::erase_if(InputSections, [](InputSectionBase *S) {
1113       return S->Name.startswith(".debug") || S->Name.startswith(".zdebug");
1114     });
1115 
1116   Config->EFlags = Target->calcEFlags();
1117 
1118   // This adds a .comment section containing a version string. We have to add it
1119   // before decompressAndMergeSections because the .comment section is a
1120   // mergeable section.
1121   if (!Config->Relocatable)
1122     InputSections.push_back(createCommentSection<ELFT>());
1123 
1124   // Do size optimizations: garbage collection, merging of SHF_MERGE sections
1125   // and identical code folding.
1126   markLive<ELFT>();
1127   decompressSections();
1128   mergeSections();
1129   if (Config->ICF)
1130     doIcf<ELFT>();
1131 
1132   // Write the result to the file.
1133   writeResult<ELFT>();
1134 }
1135