1 //===- Driver.cpp ---------------------------------------------------------===//
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
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.
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.
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.
24 //===----------------------------------------------------------------------===//
28 #include "Filesystem.h"
30 #include "InputFiles.h"
31 #include "InputSection.h"
32 #include "LinkerScript.h"
34 #include "OutputSections.h"
35 #include "ScriptParser.h"
36 #include "SymbolTable.h"
38 #include "SyntheticSections.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"
63 using namespace llvm::ELF;
64 using namespace llvm::object;
65 using namespace llvm::sys;
66 using namespace llvm::support;
69 using namespace lld::elf;
71 Configuration *elf::Config;
72 LinkerDriver *elf::Driver;
74 static void setConfigs(opt::InputArgList &Args);
76 bool elf::link(ArrayRef<const char *> Args, bool CanExitEarly,
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();
86 InputSections.clear();
87 OutputSections.clear();
93 Config = make<Configuration>();
94 Driver = make<LinkerDriver>();
95 Script = make<LinkerScript>();
96 Symtab = make<SymbolTable>();
99 memset(&In, 0, sizeof(In));
101 Config->ProgName = Args[0];
105 // Exit immediately if we don't need to return to the caller.
106 // This saves time because the overhead of calling destructors
107 // for all globally-allocated objects is not negligible.
109 exitLld(errorCount() ? 1 : 0);
112 return !errorCount();
115 // Parses a linker -m option.
116 static std::tuple<ELFKind, uint16_t, uint8_t> parseEmulation(StringRef Emul) {
119 if (S.endswith("_fbsd")) {
121 OSABI = ELFOSABI_FREEBSD;
124 std::pair<ELFKind, uint16_t> Ret =
125 StringSwitch<std::pair<ELFKind, uint16_t>>(S)
126 .Cases("aarch64elf", "aarch64linux", "aarch64_elf64_le_vec",
127 {ELF64LEKind, EM_AARCH64})
128 .Cases("armelf", "armelf_linux_eabi", {ELF32LEKind, EM_ARM})
129 .Case("elf32_x86_64", {ELF32LEKind, EM_X86_64})
130 .Cases("elf32btsmip", "elf32btsmipn32", {ELF32BEKind, EM_MIPS})
131 .Cases("elf32ltsmip", "elf32ltsmipn32", {ELF32LEKind, EM_MIPS})
132 .Case("elf32lriscv", {ELF32LEKind, EM_RISCV})
133 .Cases("elf32ppc", "elf32ppclinux", {ELF32BEKind, EM_PPC})
134 .Case("elf64btsmip", {ELF64BEKind, EM_MIPS})
135 .Case("elf64ltsmip", {ELF64LEKind, EM_MIPS})
136 .Case("elf64lriscv", {ELF64LEKind, EM_RISCV})
137 .Case("elf64ppc", {ELF64BEKind, EM_PPC64})
138 .Case("elf64lppc", {ELF64LEKind, EM_PPC64})
139 .Cases("elf_amd64", "elf_x86_64", {ELF64LEKind, EM_X86_64})
140 .Case("elf_i386", {ELF32LEKind, EM_386})
141 .Case("elf_iamcu", {ELF32LEKind, EM_IAMCU})
142 .Default({ELFNoneKind, EM_NONE});
144 if (Ret.first == ELFNoneKind)
145 error("unknown emulation: " + Emul);
146 return std::make_tuple(Ret.first, Ret.second, OSABI);
149 // Returns slices of MB by parsing MB as an archive file.
150 // Each slice consists of a member file in the archive.
151 std::vector<std::pair<MemoryBufferRef, uint64_t>> static getArchiveMembers(
152 MemoryBufferRef MB) {
153 std::unique_ptr<Archive> File =
154 CHECK(Archive::create(MB),
155 MB.getBufferIdentifier() + ": failed to parse archive");
157 std::vector<std::pair<MemoryBufferRef, uint64_t>> V;
158 Error Err = Error::success();
159 bool AddToTar = File->isThin() && Tar;
160 for (const ErrorOr<Archive::Child> &COrErr : File->children(Err)) {
162 CHECK(COrErr, MB.getBufferIdentifier() +
163 ": could not get the child of the archive");
164 MemoryBufferRef MBRef =
165 CHECK(C.getMemoryBufferRef(),
166 MB.getBufferIdentifier() +
167 ": could not get the buffer for a child of the archive");
169 Tar->append(relativeToRoot(check(C.getFullName())), MBRef.getBuffer());
170 V.push_back(std::make_pair(MBRef, C.getChildOffset()));
173 fatal(MB.getBufferIdentifier() + ": Archive::children failed: " +
174 toString(std::move(Err)));
176 // Take ownership of memory buffers created for members of thin archives.
177 for (std::unique_ptr<MemoryBuffer> &MB : File->takeThinBuffers())
178 make<std::unique_ptr<MemoryBuffer>>(std::move(MB));
183 // Opens a file and create a file object. Path has to be resolved already.
184 void LinkerDriver::addFile(StringRef Path, bool WithLOption) {
185 using namespace sys::fs;
187 Optional<MemoryBufferRef> Buffer = readFile(Path);
188 if (!Buffer.hasValue())
190 MemoryBufferRef MBRef = *Buffer;
192 if (Config->FormatBinary) {
193 Files.push_back(make<BinaryFile>(MBRef));
197 switch (identify_magic(MBRef.getBuffer())) {
198 case file_magic::unknown:
199 readLinkerScript(MBRef);
201 case file_magic::archive: {
202 // Handle -whole-archive.
203 if (InWholeArchive) {
204 for (const auto &P : getArchiveMembers(MBRef))
205 Files.push_back(createObjectFile(P.first, Path, P.second));
209 std::unique_ptr<Archive> File =
210 CHECK(Archive::create(MBRef), Path + ": failed to parse archive");
212 // If an archive file has no symbol table, it is likely that a user
213 // is attempting LTO and using a default ar command that doesn't
214 // understand the LLVM bitcode file. It is a pretty common error, so
215 // we'll handle it as if it had a symbol table.
216 if (!File->isEmpty() && !File->hasSymbolTable()) {
217 for (const auto &P : getArchiveMembers(MBRef))
218 Files.push_back(make<LazyObjFile>(P.first, Path, P.second));
222 // Handle the regular case.
223 Files.push_back(make<ArchiveFile>(std::move(File)));
226 case file_magic::elf_shared_object:
227 if (Config->Static || Config->Relocatable) {
228 error("attempted static link of dynamic object " + Path);
232 // DSOs usually have DT_SONAME tags in their ELF headers, and the
233 // sonames are used to identify DSOs. But if they are missing,
234 // they are identified by filenames. We don't know whether the new
235 // file has a DT_SONAME or not because we haven't parsed it yet.
236 // Here, we set the default soname for the file because we might
239 // If a file was specified by -lfoo, the directory part is not
240 // significant, as a user did not specify it. This behavior is
241 // compatible with GNU.
243 createSharedFile(MBRef, WithLOption ? path::filename(Path) : Path));
245 case file_magic::bitcode:
246 case file_magic::elf_relocatable:
248 Files.push_back(make<LazyObjFile>(MBRef, "", 0));
250 Files.push_back(createObjectFile(MBRef));
253 error(Path + ": unknown file type");
257 // Add a given library by searching it from input search paths.
258 void LinkerDriver::addLibrary(StringRef Name) {
259 if (Optional<std::string> Path = searchLibrary(Name))
260 addFile(*Path, /*WithLOption=*/true);
262 error("unable to find library -l" + Name);
265 // This function is called on startup. We need this for LTO since
266 // LTO calls LLVM functions to compile bitcode files to native code.
267 // Technically this can be delayed until we read bitcode files, but
268 // we don't bother to do lazily because the initialization is fast.
269 static void initLLVM() {
270 InitializeAllTargets();
271 InitializeAllTargetMCs();
272 InitializeAllAsmPrinters();
273 InitializeAllAsmParsers();
276 // Some command line options or some combinations of them are not allowed.
277 // This function checks for such errors.
278 static void checkOptions() {
279 // The MIPS ABI as of 2016 does not support the GNU-style symbol lookup
280 // table which is a relatively new feature.
281 if (Config->EMachine == EM_MIPS && Config->GnuHash)
282 error("the .gnu.hash section is not compatible with the MIPS target");
284 if (Config->FixCortexA53Errata843419 && Config->EMachine != EM_AARCH64)
285 error("--fix-cortex-a53-843419 is only supported on AArch64 targets");
287 if (Config->TocOptimize && Config->EMachine != EM_PPC64)
288 error("--toc-optimize is only supported on the PowerPC64 target");
290 if (Config->Pie && Config->Shared)
291 error("-shared and -pie may not be used together");
293 if (!Config->Shared && !Config->FilterList.empty())
294 error("-F may not be used without -shared");
296 if (!Config->Shared && !Config->AuxiliaryList.empty())
297 error("-f may not be used without -shared");
299 if (!Config->Relocatable && !Config->DefineCommon)
300 error("-no-define-common not supported in non relocatable output");
302 if (Config->Relocatable) {
304 error("-r and -shared may not be used together");
305 if (Config->GcSections)
306 error("-r and --gc-sections may not be used together");
307 if (Config->GdbIndex)
308 error("-r and --gdb-index may not be used together");
309 if (Config->ICF != ICFLevel::None)
310 error("-r and --icf may not be used together");
312 error("-r and -pie may not be used together");
315 if (Config->ExecuteOnly) {
316 if (Config->EMachine != EM_AARCH64)
317 error("-execute-only is only supported on AArch64 targets");
319 if (Config->SingleRoRx && !Script->HasSectionsCommand)
320 error("-execute-only and -no-rosegment cannot be used together");
324 static const char *getReproduceOption(opt::InputArgList &Args) {
325 if (auto *Arg = Args.getLastArg(OPT_reproduce))
326 return Arg->getValue();
327 return getenv("LLD_REPRODUCE");
330 static bool hasZOption(opt::InputArgList &Args, StringRef Key) {
331 for (auto *Arg : Args.filtered(OPT_z))
332 if (Key == Arg->getValue())
337 static bool getZFlag(opt::InputArgList &Args, StringRef K1, StringRef K2,
339 for (auto *Arg : Args.filtered_reverse(OPT_z)) {
340 if (K1 == Arg->getValue())
342 if (K2 == Arg->getValue())
348 static bool isKnownZFlag(StringRef S) {
349 return S == "combreloc" || S == "copyreloc" || S == "defs" ||
350 S == "execstack" || S == "global" || S == "hazardplt" ||
351 S == "ifunc-noplt" ||
352 S == "initfirst" || S == "interpose" ||
353 S == "keep-text-section-prefix" || S == "lazy" || S == "muldefs" ||
354 S == "nocombreloc" || S == "nocopyreloc" || S == "nodefaultlib" ||
355 S == "nodelete" || S == "nodlopen" || S == "noexecstack" ||
356 S == "nokeep-text-section-prefix" || S == "norelro" || S == "notext" ||
357 S == "now" || S == "origin" || S == "relro" || S == "retpolineplt" ||
358 S == "rodynamic" || S == "text" || S == "wxneeded" ||
359 S.startswith("max-page-size=") || S.startswith("stack-size=");
362 // Report an error for an unknown -z option.
363 static void checkZOptions(opt::InputArgList &Args) {
364 for (auto *Arg : Args.filtered(OPT_z))
365 if (!isKnownZFlag(Arg->getValue()))
366 error("unknown -z value: " + StringRef(Arg->getValue()));
369 void LinkerDriver::main(ArrayRef<const char *> ArgsArr) {
371 opt::InputArgList Args = Parser.parse(ArgsArr.slice(1));
373 // Interpret this flag early because error() depends on them.
374 errorHandler().ErrorLimit = args::getInteger(Args, OPT_error_limit, 20);
377 if (Args.hasArg(OPT_help)) {
382 // Handle -v or -version.
384 // A note about "compatible with GNU linkers" message: this is a hack for
385 // scripts generated by GNU Libtool 2.4.6 (released in February 2014 and
386 // still the newest version in March 2017) or earlier to recognize LLD as
387 // a GNU compatible linker. As long as an output for the -v option
388 // contains "GNU" or "with BFD", they recognize us as GNU-compatible.
390 // This is somewhat ugly hack, but in reality, we had no choice other
391 // than doing this. Considering the very long release cycle of Libtool,
392 // it is not easy to improve it to recognize LLD as a GNU compatible
393 // linker in a timely manner. Even if we can make it, there are still a
394 // lot of "configure" scripts out there that are generated by old version
395 // of Libtool. We cannot convince every software developer to migrate to
396 // the latest version and re-generate scripts. So we have this hack.
397 if (Args.hasArg(OPT_v) || Args.hasArg(OPT_version))
398 message(getLLDVersion() + " (compatible with GNU linkers)");
400 if (const char *Path = getReproduceOption(Args)) {
401 // Note that --reproduce is a debug option so you can ignore it
402 // if you are trying to understand the whole picture of the code.
403 Expected<std::unique_ptr<TarWriter>> ErrOrWriter =
404 TarWriter::create(Path, path::stem(Path));
406 Tar = std::move(*ErrOrWriter);
407 Tar->append("response.txt", createResponseFile(Args));
408 Tar->append("version.txt", getLLDVersion() + "\n");
410 error("--reproduce: " + toString(ErrOrWriter.takeError()));
417 // The behavior of -v or --version is a bit strange, but this is
418 // needed for compatibility with GNU linkers.
419 if (Args.hasArg(OPT_v) && !Args.hasArg(OPT_INPUT))
421 if (Args.hasArg(OPT_version))
435 switch (Config->EKind) {
449 llvm_unreachable("unknown Config->EKind");
453 static std::string getRpath(opt::InputArgList &Args) {
454 std::vector<StringRef> V = args::getStrings(Args, OPT_rpath);
455 return llvm::join(V.begin(), V.end(), ":");
458 // Determines what we should do if there are remaining unresolved
459 // symbols after the name resolution.
460 static UnresolvedPolicy getUnresolvedSymbolPolicy(opt::InputArgList &Args) {
461 UnresolvedPolicy ErrorOrWarn = Args.hasFlag(OPT_error_unresolved_symbols,
462 OPT_warn_unresolved_symbols, true)
463 ? UnresolvedPolicy::ReportError
464 : UnresolvedPolicy::Warn;
466 // Process the last of -unresolved-symbols, -no-undefined or -z defs.
467 for (auto *Arg : llvm::reverse(Args)) {
468 switch (Arg->getOption().getID()) {
469 case OPT_unresolved_symbols: {
470 StringRef S = Arg->getValue();
471 if (S == "ignore-all" || S == "ignore-in-object-files")
472 return UnresolvedPolicy::Ignore;
473 if (S == "ignore-in-shared-libs" || S == "report-all")
475 error("unknown --unresolved-symbols value: " + S);
478 case OPT_no_undefined:
481 if (StringRef(Arg->getValue()) == "defs")
487 // -shared implies -unresolved-symbols=ignore-all because missing
488 // symbols are likely to be resolved at runtime using other DSOs.
490 return UnresolvedPolicy::Ignore;
494 static Target2Policy getTarget2(opt::InputArgList &Args) {
495 StringRef S = Args.getLastArgValue(OPT_target2, "got-rel");
497 return Target2Policy::Rel;
499 return Target2Policy::Abs;
501 return Target2Policy::GotRel;
502 error("unknown --target2 option: " + S);
503 return Target2Policy::GotRel;
506 static bool isOutputFormatBinary(opt::InputArgList &Args) {
507 StringRef S = Args.getLastArgValue(OPT_oformat, "elf");
510 if (!S.startswith("elf"))
511 error("unknown --oformat value: " + S);
515 static DiscardPolicy getDiscard(opt::InputArgList &Args) {
516 if (Args.hasArg(OPT_relocatable))
517 return DiscardPolicy::None;
520 Args.getLastArg(OPT_discard_all, OPT_discard_locals, OPT_discard_none);
522 return DiscardPolicy::Default;
523 if (Arg->getOption().getID() == OPT_discard_all)
524 return DiscardPolicy::All;
525 if (Arg->getOption().getID() == OPT_discard_locals)
526 return DiscardPolicy::Locals;
527 return DiscardPolicy::None;
530 static StringRef getDynamicLinker(opt::InputArgList &Args) {
531 auto *Arg = Args.getLastArg(OPT_dynamic_linker, OPT_no_dynamic_linker);
532 if (!Arg || Arg->getOption().getID() == OPT_no_dynamic_linker)
534 return Arg->getValue();
537 static ICFLevel getICF(opt::InputArgList &Args) {
538 auto *Arg = Args.getLastArg(OPT_icf_none, OPT_icf_safe, OPT_icf_all);
539 if (!Arg || Arg->getOption().getID() == OPT_icf_none)
540 return ICFLevel::None;
541 if (Arg->getOption().getID() == OPT_icf_safe)
542 return ICFLevel::Safe;
543 return ICFLevel::All;
546 static StripPolicy getStrip(opt::InputArgList &Args) {
547 if (Args.hasArg(OPT_relocatable))
548 return StripPolicy::None;
550 auto *Arg = Args.getLastArg(OPT_strip_all, OPT_strip_debug);
552 return StripPolicy::None;
553 if (Arg->getOption().getID() == OPT_strip_all)
554 return StripPolicy::All;
555 return StripPolicy::Debug;
558 static uint64_t parseSectionAddress(StringRef S, const opt::Arg &Arg) {
560 if (S.startswith("0x"))
562 if (!to_integer(S, VA, 16))
563 error("invalid argument: " + toString(Arg));
567 static StringMap<uint64_t> getSectionStartMap(opt::InputArgList &Args) {
568 StringMap<uint64_t> Ret;
569 for (auto *Arg : Args.filtered(OPT_section_start)) {
572 std::tie(Name, Addr) = StringRef(Arg->getValue()).split('=');
573 Ret[Name] = parseSectionAddress(Addr, *Arg);
576 if (auto *Arg = Args.getLastArg(OPT_Ttext))
577 Ret[".text"] = parseSectionAddress(Arg->getValue(), *Arg);
578 if (auto *Arg = Args.getLastArg(OPT_Tdata))
579 Ret[".data"] = parseSectionAddress(Arg->getValue(), *Arg);
580 if (auto *Arg = Args.getLastArg(OPT_Tbss))
581 Ret[".bss"] = parseSectionAddress(Arg->getValue(), *Arg);
585 static SortSectionPolicy getSortSection(opt::InputArgList &Args) {
586 StringRef S = Args.getLastArgValue(OPT_sort_section);
587 if (S == "alignment")
588 return SortSectionPolicy::Alignment;
590 return SortSectionPolicy::Name;
592 error("unknown --sort-section rule: " + S);
593 return SortSectionPolicy::Default;
596 static OrphanHandlingPolicy getOrphanHandling(opt::InputArgList &Args) {
597 StringRef S = Args.getLastArgValue(OPT_orphan_handling, "place");
599 return OrphanHandlingPolicy::Warn;
601 return OrphanHandlingPolicy::Error;
603 error("unknown --orphan-handling mode: " + S);
604 return OrphanHandlingPolicy::Place;
607 // Parse --build-id or --build-id=<style>. We handle "tree" as a
608 // synonym for "sha1" because all our hash functions including
609 // -build-id=sha1 are actually tree hashes for performance reasons.
610 static std::pair<BuildIdKind, std::vector<uint8_t>>
611 getBuildId(opt::InputArgList &Args) {
612 auto *Arg = Args.getLastArg(OPT_build_id, OPT_build_id_eq);
614 return {BuildIdKind::None, {}};
616 if (Arg->getOption().getID() == OPT_build_id)
617 return {BuildIdKind::Fast, {}};
619 StringRef S = Arg->getValue();
621 return {BuildIdKind::Fast, {}};
623 return {BuildIdKind::Md5, {}};
624 if (S == "sha1" || S == "tree")
625 return {BuildIdKind::Sha1, {}};
627 return {BuildIdKind::Uuid, {}};
628 if (S.startswith("0x"))
629 return {BuildIdKind::Hexstring, parseHex(S.substr(2))};
632 error("unknown --build-id style: " + S);
633 return {BuildIdKind::None, {}};
636 static std::pair<bool, bool> getPackDynRelocs(opt::InputArgList &Args) {
637 StringRef S = Args.getLastArgValue(OPT_pack_dyn_relocs, "none");
639 return {true, false};
641 return {false, true};
642 if (S == "android+relr")
646 error("unknown -pack-dyn-relocs format: " + S);
647 return {false, false};
650 static void readCallGraph(MemoryBufferRef MB) {
651 // Build a map from symbol name to section
652 DenseMap<StringRef, Symbol *> Map;
653 for (InputFile *File : ObjectFiles)
654 for (Symbol *Sym : File->getSymbols())
655 Map[Sym->getName()] = Sym;
657 auto FindSection = [&](StringRef Name) -> InputSectionBase * {
658 Symbol *Sym = Map.lookup(Name);
660 if (Config->WarnSymbolOrdering)
661 warn(MB.getBufferIdentifier() + ": no such symbol: " + Name);
664 maybeWarnUnorderableSymbol(Sym);
666 if (Defined *DR = dyn_cast_or_null<Defined>(Sym))
667 return dyn_cast_or_null<InputSectionBase>(DR->Section);
671 for (StringRef Line : args::getLines(MB)) {
672 SmallVector<StringRef, 3> Fields;
673 Line.split(Fields, ' ');
676 if (Fields.size() != 3 || !to_integer(Fields[2], Count)) {
677 error(MB.getBufferIdentifier() + ": parse error");
681 if (InputSectionBase *From = FindSection(Fields[0]))
682 if (InputSectionBase *To = FindSection(Fields[1]))
683 Config->CallGraphProfile[std::make_pair(From, To)] += Count;
687 template <class ELFT> static void readCallGraphsFromObjectFiles() {
688 for (auto File : ObjectFiles) {
689 auto *Obj = cast<ObjFile<ELFT>>(File);
691 for (const Elf_CGProfile_Impl<ELFT> &CGPE : Obj->CGProfile) {
692 auto *FromSym = dyn_cast<Defined>(&Obj->getSymbol(CGPE.cgp_from));
693 auto *ToSym = dyn_cast<Defined>(&Obj->getSymbol(CGPE.cgp_to));
694 if (!FromSym || !ToSym)
697 auto *From = dyn_cast_or_null<InputSectionBase>(FromSym->Section);
698 auto *To = dyn_cast_or_null<InputSectionBase>(ToSym->Section);
700 Config->CallGraphProfile[{From, To}] += CGPE.cgp_weight;
705 static bool getCompressDebugSections(opt::InputArgList &Args) {
706 StringRef S = Args.getLastArgValue(OPT_compress_debug_sections, "none");
710 error("unknown --compress-debug-sections value: " + S);
711 if (!zlib::isAvailable())
712 error("--compress-debug-sections: zlib is not available");
716 static std::pair<StringRef, StringRef> getOldNewOptions(opt::InputArgList &Args,
718 auto *Arg = Args.getLastArg(Id);
722 StringRef S = Arg->getValue();
723 std::pair<StringRef, StringRef> Ret = S.split(';');
724 if (Ret.second.empty())
725 error(Arg->getSpelling() + " expects 'old;new' format, but got " + S);
729 // Parse the symbol ordering file and warn for any duplicate entries.
730 static std::vector<StringRef> getSymbolOrderingFile(MemoryBufferRef MB) {
731 SetVector<StringRef> Names;
732 for (StringRef S : args::getLines(MB))
733 if (!Names.insert(S) && Config->WarnSymbolOrdering)
734 warn(MB.getBufferIdentifier() + ": duplicate ordered symbol: " + S);
736 return Names.takeVector();
739 static void parseClangOption(StringRef Opt, const Twine &Msg) {
741 raw_string_ostream OS(Err);
743 const char *Argv[] = {Config->ProgName.data(), Opt.data()};
744 if (cl::ParseCommandLineOptions(2, Argv, "", &OS))
747 error(Msg + ": " + StringRef(Err).trim());
750 // Initializes Config members by the command line options.
751 void LinkerDriver::readConfigs(opt::InputArgList &Args) {
752 errorHandler().Verbose = Args.hasArg(OPT_verbose);
753 errorHandler().FatalWarnings =
754 Args.hasFlag(OPT_fatal_warnings, OPT_no_fatal_warnings, false);
755 ThreadsEnabled = Args.hasFlag(OPT_threads, OPT_no_threads, true);
757 Config->AllowMultipleDefinition =
758 Args.hasFlag(OPT_allow_multiple_definition,
759 OPT_no_allow_multiple_definition, false) ||
760 hasZOption(Args, "muldefs");
761 Config->AllowShlibUndefined =
762 Args.hasFlag(OPT_allow_shlib_undefined, OPT_no_allow_shlib_undefined,
763 Args.hasArg(OPT_shared));
764 Config->AuxiliaryList = args::getStrings(Args, OPT_auxiliary);
765 Config->Bsymbolic = Args.hasArg(OPT_Bsymbolic);
766 Config->BsymbolicFunctions = Args.hasArg(OPT_Bsymbolic_functions);
767 Config->CheckSections =
768 Args.hasFlag(OPT_check_sections, OPT_no_check_sections, true);
769 Config->Chroot = Args.getLastArgValue(OPT_chroot);
770 Config->CompressDebugSections = getCompressDebugSections(Args);
771 Config->Cref = Args.hasFlag(OPT_cref, OPT_no_cref, false);
772 Config->DefineCommon = Args.hasFlag(OPT_define_common, OPT_no_define_common,
773 !Args.hasArg(OPT_relocatable));
774 Config->Demangle = Args.hasFlag(OPT_demangle, OPT_no_demangle, true);
775 Config->DisableVerify = Args.hasArg(OPT_disable_verify);
776 Config->Discard = getDiscard(Args);
777 Config->DwoDir = Args.getLastArgValue(OPT_plugin_opt_dwo_dir_eq);
778 Config->DynamicLinker = getDynamicLinker(Args);
780 Args.hasFlag(OPT_eh_frame_hdr, OPT_no_eh_frame_hdr, false);
781 Config->EmitLLVM = Args.hasArg(OPT_plugin_opt_emit_llvm, false);
782 Config->EmitRelocs = Args.hasArg(OPT_emit_relocs);
783 Config->CallGraphProfileSort = Args.hasFlag(
784 OPT_call_graph_profile_sort, OPT_no_call_graph_profile_sort, true);
785 Config->EnableNewDtags =
786 Args.hasFlag(OPT_enable_new_dtags, OPT_disable_new_dtags, true);
787 Config->Entry = Args.getLastArgValue(OPT_entry);
788 Config->ExecuteOnly =
789 Args.hasFlag(OPT_execute_only, OPT_no_execute_only, false);
790 Config->ExportDynamic =
791 Args.hasFlag(OPT_export_dynamic, OPT_no_export_dynamic, false);
792 Config->FilterList = args::getStrings(Args, OPT_filter);
793 Config->Fini = Args.getLastArgValue(OPT_fini, "_fini");
794 Config->FixCortexA53Errata843419 = Args.hasArg(OPT_fix_cortex_a53_843419);
795 Config->GcSections = Args.hasFlag(OPT_gc_sections, OPT_no_gc_sections, false);
796 Config->GnuUnique = Args.hasFlag(OPT_gnu_unique, OPT_no_gnu_unique, true);
797 Config->GdbIndex = Args.hasFlag(OPT_gdb_index, OPT_no_gdb_index, false);
798 Config->ICF = getICF(Args);
799 Config->IgnoreDataAddressEquality =
800 Args.hasArg(OPT_ignore_data_address_equality);
801 Config->IgnoreFunctionAddressEquality =
802 Args.hasArg(OPT_ignore_function_address_equality);
803 Config->Init = Args.getLastArgValue(OPT_init, "_init");
804 Config->LTOAAPipeline = Args.getLastArgValue(OPT_lto_aa_pipeline);
805 Config->LTODebugPassManager = Args.hasArg(OPT_lto_debug_pass_manager);
806 Config->LTONewPassManager = Args.hasArg(OPT_lto_new_pass_manager);
807 Config->LTONewPmPasses = Args.getLastArgValue(OPT_lto_newpm_passes);
808 Config->LTOO = args::getInteger(Args, OPT_lto_O, 2);
809 Config->LTOObjPath = Args.getLastArgValue(OPT_plugin_opt_obj_path_eq);
810 Config->LTOPartitions = args::getInteger(Args, OPT_lto_partitions, 1);
811 Config->LTOSampleProfile = Args.getLastArgValue(OPT_lto_sample_profile);
812 Config->MapFile = Args.getLastArgValue(OPT_Map);
813 Config->MipsGotSize = args::getInteger(Args, OPT_mips_got_size, 0xfff0);
814 Config->MergeArmExidx =
815 Args.hasFlag(OPT_merge_exidx_entries, OPT_no_merge_exidx_entries, true);
816 Config->NoinhibitExec = Args.hasArg(OPT_noinhibit_exec);
817 Config->Nostdlib = Args.hasArg(OPT_nostdlib);
818 Config->OFormatBinary = isOutputFormatBinary(Args);
819 Config->Omagic = Args.hasFlag(OPT_omagic, OPT_no_omagic, false);
820 Config->OptRemarksFilename = Args.getLastArgValue(OPT_opt_remarks_filename);
821 Config->OptRemarksWithHotness = Args.hasArg(OPT_opt_remarks_with_hotness);
822 Config->Optimize = args::getInteger(Args, OPT_O, 1);
823 Config->OrphanHandling = getOrphanHandling(Args);
824 Config->OutputFile = Args.getLastArgValue(OPT_o);
825 Config->Pie = Args.hasFlag(OPT_pie, OPT_no_pie, false);
826 Config->PrintIcfSections =
827 Args.hasFlag(OPT_print_icf_sections, OPT_no_print_icf_sections, false);
828 Config->PrintGcSections =
829 Args.hasFlag(OPT_print_gc_sections, OPT_no_print_gc_sections, false);
830 Config->Rpath = getRpath(Args);
831 Config->Relocatable = Args.hasArg(OPT_relocatable);
832 Config->SaveTemps = Args.hasArg(OPT_save_temps);
833 Config->SearchPaths = args::getStrings(Args, OPT_library_path);
834 Config->SectionStartMap = getSectionStartMap(Args);
835 Config->Shared = Args.hasArg(OPT_shared);
836 Config->SingleRoRx = Args.hasArg(OPT_no_rosegment);
837 Config->SoName = Args.getLastArgValue(OPT_soname);
838 Config->SortSection = getSortSection(Args);
839 Config->SplitStackAdjustSize = args::getInteger(Args, OPT_split_stack_adjust_size, 16384);
840 Config->Strip = getStrip(Args);
841 Config->Sysroot = Args.getLastArgValue(OPT_sysroot);
842 Config->Target1Rel = Args.hasFlag(OPT_target1_rel, OPT_target1_abs, false);
843 Config->Target2 = getTarget2(Args);
844 Config->ThinLTOCacheDir = Args.getLastArgValue(OPT_thinlto_cache_dir);
845 Config->ThinLTOCachePolicy = CHECK(
846 parseCachePruningPolicy(Args.getLastArgValue(OPT_thinlto_cache_policy)),
847 "--thinlto-cache-policy: invalid cache policy");
848 Config->ThinLTOEmitImportsFiles =
849 Args.hasArg(OPT_plugin_opt_thinlto_emit_imports_files);
850 Config->ThinLTOIndexOnly = Args.hasArg(OPT_plugin_opt_thinlto_index_only) ||
851 Args.hasArg(OPT_plugin_opt_thinlto_index_only_eq);
852 Config->ThinLTOIndexOnlyArg =
853 Args.getLastArgValue(OPT_plugin_opt_thinlto_index_only_eq);
854 Config->ThinLTOJobs = args::getInteger(Args, OPT_thinlto_jobs, -1u);
855 Config->ThinLTOObjectSuffixReplace =
856 getOldNewOptions(Args, OPT_plugin_opt_thinlto_object_suffix_replace_eq);
857 Config->ThinLTOPrefixReplace =
858 getOldNewOptions(Args, OPT_plugin_opt_thinlto_prefix_replace_eq);
859 Config->Trace = Args.hasArg(OPT_trace);
860 Config->Undefined = args::getStrings(Args, OPT_undefined);
861 Config->UndefinedVersion =
862 Args.hasFlag(OPT_undefined_version, OPT_no_undefined_version, true);
863 Config->UseAndroidRelrTags = Args.hasFlag(
864 OPT_use_android_relr_tags, OPT_no_use_android_relr_tags, false);
865 Config->UnresolvedSymbols = getUnresolvedSymbolPolicy(Args);
866 Config->WarnBackrefs =
867 Args.hasFlag(OPT_warn_backrefs, OPT_no_warn_backrefs, false);
868 Config->WarnCommon = Args.hasFlag(OPT_warn_common, OPT_no_warn_common, false);
869 Config->WarnIfuncTextrel =
870 Args.hasFlag(OPT_warn_ifunc_textrel, OPT_no_warn_ifunc_textrel, false);
871 Config->WarnSymbolOrdering =
872 Args.hasFlag(OPT_warn_symbol_ordering, OPT_no_warn_symbol_ordering, true);
873 Config->ZCombreloc = getZFlag(Args, "combreloc", "nocombreloc", true);
874 Config->ZCopyreloc = getZFlag(Args, "copyreloc", "nocopyreloc", true);
875 Config->ZExecstack = getZFlag(Args, "execstack", "noexecstack", false);
876 Config->ZGlobal = hasZOption(Args, "global");
877 Config->ZHazardplt = hasZOption(Args, "hazardplt");
878 Config->ZIfuncnoplt = hasZOption(Args, "ifunc-noplt");
879 Config->ZInitfirst = hasZOption(Args, "initfirst");
880 Config->ZInterpose = hasZOption(Args, "interpose");
881 Config->ZKeepTextSectionPrefix = getZFlag(
882 Args, "keep-text-section-prefix", "nokeep-text-section-prefix", false);
883 Config->ZNodefaultlib = hasZOption(Args, "nodefaultlib");
884 Config->ZNodelete = hasZOption(Args, "nodelete");
885 Config->ZNodlopen = hasZOption(Args, "nodlopen");
886 Config->ZNow = getZFlag(Args, "now", "lazy", false);
887 Config->ZOrigin = hasZOption(Args, "origin");
888 Config->ZRelro = getZFlag(Args, "relro", "norelro", true);
889 Config->ZRetpolineplt = hasZOption(Args, "retpolineplt");
890 Config->ZRodynamic = hasZOption(Args, "rodynamic");
891 Config->ZStackSize = args::getZOptionValue(Args, OPT_z, "stack-size", 0);
892 Config->ZText = getZFlag(Args, "text", "notext", true);
893 Config->ZWxneeded = hasZOption(Args, "wxneeded");
895 // Parse LTO options.
896 if (auto *Arg = Args.getLastArg(OPT_plugin_opt_mcpu_eq))
897 parseClangOption(Saver.save("-mcpu=" + StringRef(Arg->getValue())),
900 for (auto *Arg : Args.filtered(OPT_plugin_opt))
901 parseClangOption(Arg->getValue(), Arg->getSpelling());
903 // Parse -mllvm options.
904 for (auto *Arg : Args.filtered(OPT_mllvm))
905 parseClangOption(Arg->getValue(), Arg->getSpelling());
907 if (Config->LTOO > 3)
908 error("invalid optimization level for LTO: " + Twine(Config->LTOO));
909 if (Config->LTOPartitions == 0)
910 error("--lto-partitions: number of threads must be > 0");
911 if (Config->ThinLTOJobs == 0)
912 error("--thinlto-jobs: number of threads must be > 0");
914 if (Config->SplitStackAdjustSize < 0)
915 error("--split-stack-adjust-size: size must be >= 0");
917 // Parse ELF{32,64}{LE,BE} and CPU type.
918 if (auto *Arg = Args.getLastArg(OPT_m)) {
919 StringRef S = Arg->getValue();
920 std::tie(Config->EKind, Config->EMachine, Config->OSABI) =
922 Config->MipsN32Abi = (S == "elf32btsmipn32" || S == "elf32ltsmipn32");
923 Config->Emulation = S;
926 // Parse -hash-style={sysv,gnu,both}.
927 if (auto *Arg = Args.getLastArg(OPT_hash_style)) {
928 StringRef S = Arg->getValue();
930 Config->SysvHash = true;
932 Config->GnuHash = true;
933 else if (S == "both")
934 Config->SysvHash = Config->GnuHash = true;
936 error("unknown -hash-style: " + S);
939 if (Args.hasArg(OPT_print_map))
940 Config->MapFile = "-";
942 // --omagic is an option to create old-fashioned executables in which
943 // .text segments are writable. Today, the option is still in use to
944 // create special-purpose programs such as boot loaders. It doesn't
945 // make sense to create PT_GNU_RELRO for such executables.
947 Config->ZRelro = false;
949 std::tie(Config->BuildId, Config->BuildIdVector) = getBuildId(Args);
951 std::tie(Config->AndroidPackDynRelocs, Config->RelrPackDynRelocs) =
952 getPackDynRelocs(Args);
954 if (auto *Arg = Args.getLastArg(OPT_symbol_ordering_file))
955 if (Optional<MemoryBufferRef> Buffer = readFile(Arg->getValue()))
956 Config->SymbolOrderingFile = getSymbolOrderingFile(*Buffer);
958 // If --retain-symbol-file is used, we'll keep only the symbols listed in
959 // the file and discard all others.
960 if (auto *Arg = Args.getLastArg(OPT_retain_symbols_file)) {
961 Config->DefaultSymbolVersion = VER_NDX_LOCAL;
962 if (Optional<MemoryBufferRef> Buffer = readFile(Arg->getValue()))
963 for (StringRef S : args::getLines(*Buffer))
964 Config->VersionScriptGlobals.push_back(
965 {S, /*IsExternCpp*/ false, /*HasWildcard*/ false});
968 bool HasExportDynamic =
969 Args.hasFlag(OPT_export_dynamic, OPT_no_export_dynamic, false);
971 // Parses -dynamic-list and -export-dynamic-symbol. They make some
972 // symbols private. Note that -export-dynamic takes precedence over them
973 // as it says all symbols should be exported.
974 if (!HasExportDynamic) {
975 for (auto *Arg : Args.filtered(OPT_dynamic_list))
976 if (Optional<MemoryBufferRef> Buffer = readFile(Arg->getValue()))
977 readDynamicList(*Buffer);
979 for (auto *Arg : Args.filtered(OPT_export_dynamic_symbol))
980 Config->DynamicList.push_back(
981 {Arg->getValue(), /*IsExternCpp*/ false, /*HasWildcard*/ false});
984 // If --export-dynamic-symbol=foo is given and symbol foo is defined in
985 // an object file in an archive file, that object file should be pulled
986 // out and linked. (It doesn't have to behave like that from technical
987 // point of view, but this is needed for compatibility with GNU.)
988 for (auto *Arg : Args.filtered(OPT_export_dynamic_symbol))
989 Config->Undefined.push_back(Arg->getValue());
991 for (auto *Arg : Args.filtered(OPT_version_script))
992 if (Optional<std::string> Path = searchScript(Arg->getValue())) {
993 if (Optional<MemoryBufferRef> Buffer = readFile(*Path))
994 readVersionScript(*Buffer);
996 error(Twine("cannot find version script ") + Arg->getValue());
1000 // Some Config members do not directly correspond to any particular
1001 // command line options, but computed based on other Config values.
1002 // This function initialize such members. See Config.h for the details
1004 static void setConfigs(opt::InputArgList &Args) {
1005 ELFKind K = Config->EKind;
1006 uint16_t M = Config->EMachine;
1008 Config->CopyRelocs = (Config->Relocatable || Config->EmitRelocs);
1009 Config->Is64 = (K == ELF64LEKind || K == ELF64BEKind);
1010 Config->IsLE = (K == ELF32LEKind || K == ELF64LEKind);
1011 Config->Endianness = Config->IsLE ? endianness::little : endianness::big;
1012 Config->IsMips64EL = (K == ELF64LEKind && M == EM_MIPS);
1013 Config->Pic = Config->Pie || Config->Shared;
1014 Config->PicThunk = Args.hasArg(OPT_pic_veneer, Config->Pic);
1015 Config->Wordsize = Config->Is64 ? 8 : 4;
1017 // ELF defines two different ways to store relocation addends as shown below:
1019 // Rel: Addends are stored to the location where relocations are applied.
1020 // Rela: Addends are stored as part of relocation entry.
1022 // In other words, Rela makes it easy to read addends at the price of extra
1023 // 4 or 8 byte for each relocation entry. We don't know why ELF defined two
1024 // different mechanisms in the first place, but this is how the spec is
1027 // You cannot choose which one, Rel or Rela, you want to use. Instead each
1028 // ABI defines which one you need to use. The following expression expresses
1030 Config->IsRela = M == EM_AARCH64 || M == EM_AMDGPU || M == EM_HEXAGON ||
1031 M == EM_PPC || M == EM_PPC64 || M == EM_RISCV ||
1034 // If the output uses REL relocations we must store the dynamic relocation
1035 // addends to the output sections. We also store addends for RELA relocations
1036 // if --apply-dynamic-relocs is used.
1037 // We default to not writing the addends when using RELA relocations since
1038 // any standard conforming tool can find it in r_addend.
1039 Config->WriteAddends = Args.hasFlag(OPT_apply_dynamic_relocs,
1040 OPT_no_apply_dynamic_relocs, false) ||
1043 Config->TocOptimize =
1044 Args.hasFlag(OPT_toc_optimize, OPT_no_toc_optimize, M == EM_PPC64);
1047 // Returns a value of "-format" option.
1048 static bool isFormatBinary(StringRef S) {
1051 if (S == "elf" || S == "default")
1053 error("unknown -format value: " + S +
1054 " (supported formats: elf, default, binary)");
1058 void LinkerDriver::createFiles(opt::InputArgList &Args) {
1059 // For --{push,pop}-state.
1060 std::vector<std::tuple<bool, bool, bool>> Stack;
1062 // Iterate over argv to process input files and positional arguments.
1063 for (auto *Arg : Args) {
1064 switch (Arg->getOption().getUnaliasedOption().getID()) {
1066 addLibrary(Arg->getValue());
1069 addFile(Arg->getValue(), /*WithLOption=*/false);
1074 std::tie(From, To) = StringRef(Arg->getValue()).split('=');
1075 if (From.empty() || To.empty())
1076 error("-defsym: syntax error: " + StringRef(Arg->getValue()));
1078 readDefsym(From, MemoryBufferRef(To, "-defsym"));
1082 if (Optional<std::string> Path = searchScript(Arg->getValue())) {
1083 if (Optional<MemoryBufferRef> MB = readFile(*Path))
1084 readLinkerScript(*MB);
1087 error(Twine("cannot find linker script ") + Arg->getValue());
1090 Config->AsNeeded = true;
1093 Config->FormatBinary = isFormatBinary(Arg->getValue());
1095 case OPT_no_as_needed:
1096 Config->AsNeeded = false;
1099 Config->Static = true;
1102 Config->Static = false;
1104 case OPT_whole_archive:
1105 InWholeArchive = true;
1107 case OPT_no_whole_archive:
1108 InWholeArchive = false;
1110 case OPT_just_symbols:
1111 if (Optional<MemoryBufferRef> MB = readFile(Arg->getValue())) {
1112 Files.push_back(createObjectFile(*MB));
1113 Files.back()->JustSymbols = true;
1116 case OPT_start_group:
1117 if (InputFile::IsInGroup)
1118 error("nested --start-group");
1119 InputFile::IsInGroup = true;
1122 if (!InputFile::IsInGroup)
1123 error("stray --end-group");
1124 InputFile::IsInGroup = false;
1125 ++InputFile::NextGroupId;
1129 error("nested --start-lib");
1130 if (InputFile::IsInGroup)
1131 error("may not nest --start-lib in --start-group");
1133 InputFile::IsInGroup = true;
1137 error("stray --end-lib");
1139 InputFile::IsInGroup = false;
1140 ++InputFile::NextGroupId;
1142 case OPT_push_state:
1143 Stack.emplace_back(Config->AsNeeded, Config->Static, InWholeArchive);
1146 if (Stack.empty()) {
1147 error("unbalanced --push-state/--pop-state");
1150 std::tie(Config->AsNeeded, Config->Static, InWholeArchive) = Stack.back();
1156 if (Files.empty() && errorCount() == 0)
1157 error("no input files");
1160 // If -m <machine_type> was not given, infer it from object files.
1161 void LinkerDriver::inferMachineType() {
1162 if (Config->EKind != ELFNoneKind)
1165 for (InputFile *F : Files) {
1166 if (F->EKind == ELFNoneKind)
1168 Config->EKind = F->EKind;
1169 Config->EMachine = F->EMachine;
1170 Config->OSABI = F->OSABI;
1171 Config->MipsN32Abi = Config->EMachine == EM_MIPS && isMipsN32Abi(F);
1174 error("target emulation unknown: -m or at least one .o file required");
1177 // Parse -z max-page-size=<value>. The default value is defined by
1179 static uint64_t getMaxPageSize(opt::InputArgList &Args) {
1180 uint64_t Val = args::getZOptionValue(Args, OPT_z, "max-page-size",
1181 Target->DefaultMaxPageSize);
1182 if (!isPowerOf2_64(Val))
1183 error("max-page-size: value isn't a power of 2");
1187 // Parses -image-base option.
1188 static Optional<uint64_t> getImageBase(opt::InputArgList &Args) {
1189 // Because we are using "Config->MaxPageSize" here, this function has to be
1190 // called after the variable is initialized.
1191 auto *Arg = Args.getLastArg(OPT_image_base);
1195 StringRef S = Arg->getValue();
1197 if (!to_integer(S, V)) {
1198 error("-image-base: number expected, but got " + S);
1201 if ((V % Config->MaxPageSize) != 0)
1202 warn("-image-base: address isn't multiple of page size: " + S);
1206 // Parses `--exclude-libs=lib,lib,...`.
1207 // The library names may be delimited by commas or colons.
1208 static DenseSet<StringRef> getExcludeLibs(opt::InputArgList &Args) {
1209 DenseSet<StringRef> Ret;
1210 for (auto *Arg : Args.filtered(OPT_exclude_libs)) {
1211 StringRef S = Arg->getValue();
1213 size_t Pos = S.find_first_of(",:");
1214 if (Pos == StringRef::npos)
1216 Ret.insert(S.substr(0, Pos));
1217 S = S.substr(Pos + 1);
1224 // Handles the -exclude-libs option. If a static library file is specified
1225 // by the -exclude-libs option, all public symbols from the archive become
1226 // private unless otherwise specified by version scripts or something.
1227 // A special library name "ALL" means all archive files.
1229 // This is not a popular option, but some programs such as bionic libc use it.
1230 template <class ELFT>
1231 static void excludeLibs(opt::InputArgList &Args) {
1232 DenseSet<StringRef> Libs = getExcludeLibs(Args);
1233 bool All = Libs.count("ALL");
1235 auto Visit = [&](InputFile *File) {
1236 if (!File->ArchiveName.empty())
1237 if (All || Libs.count(path::filename(File->ArchiveName)))
1238 for (Symbol *Sym : File->getSymbols())
1239 if (!Sym->isLocal() && Sym->File == File)
1240 Sym->VersionId = VER_NDX_LOCAL;
1243 for (InputFile *File : ObjectFiles)
1246 for (BitcodeFile *File : BitcodeFiles)
1250 // Force Sym to be entered in the output. Used for -u or equivalent.
1251 template <class ELFT> static void handleUndefined(StringRef Name) {
1252 Symbol *Sym = Symtab->find(Name);
1256 // Since symbol S may not be used inside the program, LTO may
1257 // eliminate it. Mark the symbol as "used" to prevent it.
1258 Sym->IsUsedInRegularObj = true;
1261 Symtab->fetchLazy<ELFT>(Sym);
1264 template <class ELFT> static void handleLibcall(StringRef Name) {
1265 Symbol *Sym = Symtab->find(Name);
1266 if (!Sym || !Sym->isLazy())
1270 if (auto *LO = dyn_cast<LazyObject>(Sym))
1273 MB = cast<LazyArchive>(Sym)->getMemberBuffer();
1276 Symtab->fetchLazy<ELFT>(Sym);
1279 // If all references to a DSO happen to be weak, the DSO is not added
1280 // to DT_NEEDED. If that happens, we need to eliminate shared symbols
1281 // created from the DSO. Otherwise, they become dangling references
1282 // that point to a non-existent DSO.
1283 template <class ELFT> static void demoteSharedSymbols() {
1284 for (Symbol *Sym : Symtab->getSymbols()) {
1285 if (auto *S = dyn_cast<SharedSymbol>(Sym)) {
1286 if (!S->getFile<ELFT>().IsNeeded) {
1287 bool Used = S->Used;
1288 replaceSymbol<Undefined>(S, nullptr, S->getName(), STB_WEAK, S->StOther,
1296 // The section referred to by S is considered address-significant. Set the
1297 // KeepUnique flag on the section if appropriate.
1298 static void markAddrsig(Symbol *S) {
1299 if (auto *D = dyn_cast_or_null<Defined>(S))
1301 // We don't need to keep text sections unique under --icf=all even if they
1302 // are address-significant.
1303 if (Config->ICF == ICFLevel::Safe || !(D->Section->Flags & SHF_EXECINSTR))
1304 D->Section->KeepUnique = true;
1307 // Record sections that define symbols mentioned in --keep-unique <symbol>
1308 // and symbols referred to by address-significance tables. These sections are
1309 // ineligible for ICF.
1310 template <class ELFT>
1311 static void findKeepUniqueSections(opt::InputArgList &Args) {
1312 for (auto *Arg : Args.filtered(OPT_keep_unique)) {
1313 StringRef Name = Arg->getValue();
1314 auto *D = dyn_cast_or_null<Defined>(Symtab->find(Name));
1315 if (!D || !D->Section) {
1316 warn("could not find symbol " + Name + " to keep unique");
1319 D->Section->KeepUnique = true;
1322 // --icf=all --ignore-data-address-equality means that we can ignore
1323 // the dynsym and address-significance tables entirely.
1324 if (Config->ICF == ICFLevel::All && Config->IgnoreDataAddressEquality)
1327 // Symbols in the dynsym could be address-significant in other executables
1328 // or DSOs, so we conservatively mark them as address-significant.
1329 for (Symbol *S : Symtab->getSymbols())
1330 if (S->includeInDynsym())
1333 // Visit the address-significance table in each object file and mark each
1334 // referenced symbol as address-significant.
1335 for (InputFile *F : ObjectFiles) {
1336 auto *Obj = cast<ObjFile<ELFT>>(F);
1337 ArrayRef<Symbol *> Syms = Obj->getSymbols();
1338 if (Obj->AddrsigSec) {
1339 ArrayRef<uint8_t> Contents =
1340 check(Obj->getObj().getSectionContents(Obj->AddrsigSec));
1341 const uint8_t *Cur = Contents.begin();
1342 while (Cur != Contents.end()) {
1345 uint64_t SymIndex = decodeULEB128(Cur, &Size, Contents.end(), &Err);
1347 fatal(toString(F) + ": could not decode addrsig section: " + Err);
1348 markAddrsig(Syms[SymIndex]);
1352 // If an object file does not have an address-significance table,
1353 // conservatively mark all of its symbols as address-significant.
1354 for (Symbol *S : Syms)
1360 template <class ELFT> static Symbol *addUndefined(StringRef Name) {
1361 return Symtab->addUndefined<ELFT>(Name, STB_GLOBAL, STV_DEFAULT, 0, false,
1365 // The --wrap option is a feature to rename symbols so that you can write
1366 // wrappers for existing functions. If you pass `-wrap=foo`, all
1367 // occurrences of symbol `foo` are resolved to `wrap_foo` (so, you are
1368 // expected to write `wrap_foo` function as a wrapper). The original
1369 // symbol becomes accessible as `real_foo`, so you can call that from your
1372 // This data structure is instantiated for each -wrap option.
1373 struct WrappedSymbol {
1379 // Handles -wrap option.
1381 // This function instantiates wrapper symbols. At this point, they seem
1382 // like they are not being used at all, so we explicitly set some flags so
1383 // that LTO won't eliminate them.
1384 template <class ELFT>
1385 static std::vector<WrappedSymbol> addWrappedSymbols(opt::InputArgList &Args) {
1386 std::vector<WrappedSymbol> V;
1387 DenseSet<StringRef> Seen;
1389 for (auto *Arg : Args.filtered(OPT_wrap)) {
1390 StringRef Name = Arg->getValue();
1391 if (!Seen.insert(Name).second)
1394 Symbol *Sym = Symtab->find(Name);
1398 Symbol *Real = addUndefined<ELFT>(Saver.save("__real_" + Name));
1399 Symbol *Wrap = addUndefined<ELFT>(Saver.save("__wrap_" + Name));
1400 V.push_back({Sym, Real, Wrap});
1402 // We want to tell LTO not to inline symbols to be overwritten
1403 // because LTO doesn't know the final symbol contents after renaming.
1404 Real->CanInline = false;
1405 Sym->CanInline = false;
1407 // Tell LTO not to eliminate these symbols.
1408 Sym->IsUsedInRegularObj = true;
1409 Wrap->IsUsedInRegularObj = true;
1414 // Do renaming for -wrap by updating pointers to symbols.
1416 // When this function is executed, only InputFiles and symbol table
1417 // contain pointers to symbol objects. We visit them to replace pointers,
1418 // so that wrapped symbols are swapped as instructed by the command line.
1419 template <class ELFT> static void wrapSymbols(ArrayRef<WrappedSymbol> Wrapped) {
1420 DenseMap<Symbol *, Symbol *> Map;
1421 for (const WrappedSymbol &W : Wrapped) {
1422 Map[W.Sym] = W.Wrap;
1423 Map[W.Real] = W.Sym;
1426 // Update pointers in input files.
1427 parallelForEach(ObjectFiles, [&](InputFile *File) {
1428 std::vector<Symbol *> &Syms = File->getMutableSymbols();
1429 for (size_t I = 0, E = Syms.size(); I != E; ++I)
1430 if (Symbol *S = Map.lookup(Syms[I]))
1434 // Update pointers in the symbol table.
1435 for (const WrappedSymbol &W : Wrapped)
1436 Symtab->wrap(W.Sym, W.Real, W.Wrap);
1439 static const char *LibcallRoutineNames[] = {
1440 #define HANDLE_LIBCALL(code, name) name,
1441 #include "llvm/IR/RuntimeLibcalls.def"
1442 #undef HANDLE_LIBCALL
1445 // Do actual linking. Note that when this function is called,
1446 // all linker scripts have already been parsed.
1447 template <class ELFT> void LinkerDriver::link(opt::InputArgList &Args) {
1448 Target = getTarget();
1449 InX<ELFT>::VerSym = nullptr;
1450 InX<ELFT>::VerNeed = nullptr;
1452 Config->MaxPageSize = getMaxPageSize(Args);
1453 Config->ImageBase = getImageBase(Args);
1455 // If a -hash-style option was not given, set to a default value,
1456 // which varies depending on the target.
1457 if (!Args.hasArg(OPT_hash_style)) {
1458 if (Config->EMachine == EM_MIPS)
1459 Config->SysvHash = true;
1461 Config->SysvHash = Config->GnuHash = true;
1464 // Default output filename is "a.out" by the Unix tradition.
1465 if (Config->OutputFile.empty())
1466 Config->OutputFile = "a.out";
1468 // Fail early if the output file or map file is not writable. If a user has a
1469 // long link, e.g. due to a large LTO link, they do not wish to run it and
1470 // find that it failed because there was a mistake in their command-line.
1471 if (auto E = tryCreateFile(Config->OutputFile))
1472 error("cannot open output file " + Config->OutputFile + ": " + E.message());
1473 if (auto E = tryCreateFile(Config->MapFile))
1474 error("cannot open map file " + Config->MapFile + ": " + E.message());
1478 // Use default entry point name if no name was given via the command
1479 // line nor linker scripts. For some reason, MIPS entry point name is
1480 // different from others.
1481 Config->WarnMissingEntry =
1482 (!Config->Entry.empty() || (!Config->Shared && !Config->Relocatable));
1483 if (Config->Entry.empty() && !Config->Relocatable)
1484 Config->Entry = (Config->EMachine == EM_MIPS) ? "__start" : "_start";
1486 // Handle --trace-symbol.
1487 for (auto *Arg : Args.filtered(OPT_trace_symbol))
1488 Symtab->trace(Arg->getValue());
1490 // Add all files to the symbol table. This will add almost all
1491 // symbols that we need to the symbol table.
1492 for (InputFile *F : Files)
1493 Symtab->addFile<ELFT>(F);
1495 // Now that we have every file, we can decide if we will need a
1496 // dynamic symbol table.
1497 // We need one if we were asked to export dynamic symbols or if we are
1498 // producing a shared library.
1499 // We also need one if any shared libraries are used and for pie executables
1500 // (probably because the dynamic linker needs it).
1501 Config->HasDynSymTab =
1502 !SharedFiles.empty() || Config->Pic || Config->ExportDynamic;
1504 // Some symbols (such as __ehdr_start) are defined lazily only when there
1505 // are undefined symbols for them, so we add these to trigger that logic.
1506 for (StringRef Name : Script->ReferencedSymbols)
1507 addUndefined<ELFT>(Name);
1509 // Handle the `--undefined <sym>` options.
1510 for (StringRef S : Config->Undefined)
1511 handleUndefined<ELFT>(S);
1513 // If an entry symbol is in a static archive, pull out that file now.
1514 handleUndefined<ELFT>(Config->Entry);
1516 // If any of our inputs are bitcode files, the LTO code generator may create
1517 // references to certain library functions that might not be explicit in the
1518 // bitcode file's symbol table. If any of those library functions are defined
1519 // in a bitcode file in an archive member, we need to arrange to use LTO to
1520 // compile those archive members by adding them to the link beforehand.
1522 // However, adding all libcall symbols to the link can have undesired
1523 // consequences. For example, the libgcc implementation of
1524 // __sync_val_compare_and_swap_8 on 32-bit ARM pulls in an .init_array entry
1525 // that aborts the program if the Linux kernel does not support 64-bit
1526 // atomics, which would prevent the program from running even if it does not
1527 // use 64-bit atomics.
1529 // Therefore, we only add libcall symbols to the link before LTO if we have
1530 // to, i.e. if the symbol's definition is in bitcode. Any other required
1531 // libcall symbols will be added to the link after LTO when we add the LTO
1532 // object file to the link.
1533 if (!BitcodeFiles.empty())
1534 for (const char *S : LibcallRoutineNames)
1535 handleLibcall<ELFT>(S);
1537 // Return if there were name resolution errors.
1541 // Now when we read all script files, we want to finalize order of linker
1542 // script commands, which can be not yet final because of INSERT commands.
1543 Script->processInsertCommands();
1545 // We want to declare linker script's symbols early,
1546 // so that we can version them.
1547 // They also might be exported if referenced by DSOs.
1548 Script->declareSymbols();
1550 // Handle the -exclude-libs option.
1551 if (Args.hasArg(OPT_exclude_libs))
1552 excludeLibs<ELFT>(Args);
1554 // Create ElfHeader early. We need a dummy section in
1555 // addReservedSymbols to mark the created symbols as not absolute.
1556 Out::ElfHeader = make<OutputSection>("", 0, SHF_ALLOC);
1557 Out::ElfHeader->Size = sizeof(typename ELFT::Ehdr);
1559 // Create wrapped symbols for -wrap option.
1560 std::vector<WrappedSymbol> Wrapped = addWrappedSymbols<ELFT>(Args);
1562 // We need to create some reserved symbols such as _end. Create them.
1563 if (!Config->Relocatable)
1564 addReservedSymbols();
1566 // Apply version scripts.
1568 // For a relocatable output, version scripts don't make sense, and
1569 // parsing a symbol version string (e.g. dropping "@ver1" from a symbol
1570 // name "foo@ver1") rather do harm, so we don't call this if -r is given.
1571 if (!Config->Relocatable)
1572 Symtab->scanVersionScript();
1574 // Do link-time optimization if given files are LLVM bitcode files.
1575 // This compiles bitcode files into real object files.
1577 // With this the symbol table should be complete. After this, no new names
1578 // except a few linker-synthesized ones will be added to the symbol table.
1579 Symtab->addCombinedLTOObject<ELFT>();
1583 // If -thinlto-index-only is given, we should create only "index
1584 // files" and not object files. Index file creation is already done
1585 // in addCombinedLTOObject, so we are done if that's the case.
1586 if (Config->ThinLTOIndexOnly)
1589 // Likewise, --plugin-opt=emit-llvm is an option to make LTO create
1590 // an output file in bitcode and exit, so that you can just get a
1591 // combined bitcode file.
1592 if (Config->EmitLLVM)
1595 // Apply symbol renames for -wrap.
1596 if (!Wrapped.empty())
1597 wrapSymbols<ELFT>(Wrapped);
1599 // Now that we have a complete list of input files.
1600 // Beyond this point, no new files are added.
1601 // Aggregate all input sections into one place.
1602 for (InputFile *F : ObjectFiles)
1603 for (InputSectionBase *S : F->getSections())
1604 if (S && S != &InputSection::Discarded)
1605 InputSections.push_back(S);
1606 for (BinaryFile *F : BinaryFiles)
1607 for (InputSectionBase *S : F->getSections())
1608 InputSections.push_back(cast<InputSection>(S));
1610 // We do not want to emit debug sections if --strip-all
1611 // or -strip-debug are given.
1612 if (Config->Strip != StripPolicy::None)
1613 llvm::erase_if(InputSections, [](InputSectionBase *S) {
1614 return S->Name.startswith(".debug") || S->Name.startswith(".zdebug");
1617 Config->EFlags = Target->calcEFlags();
1619 if (Config->EMachine == EM_ARM) {
1620 // FIXME: These warnings can be removed when lld only uses these features
1621 // when the input objects have been compiled with an architecture that
1623 if (Config->ARMHasBlx == false)
1624 warn("lld uses blx instruction, no object with architecture supporting "
1625 "feature detected");
1628 // This adds a .comment section containing a version string. We have to add it
1629 // before mergeSections because the .comment section is a mergeable section.
1630 if (!Config->Relocatable)
1631 InputSections.push_back(createCommentSection());
1633 // Do size optimizations: garbage collection, merging of SHF_MERGE sections
1634 // and identical code folding.
1635 splitSections<ELFT>();
1637 demoteSharedSymbols<ELFT>();
1639 if (Config->ICF != ICFLevel::None) {
1640 findKeepUniqueSections<ELFT>(Args);
1644 // Read the callgraph now that we know what was gced or icfed
1645 if (Config->CallGraphProfileSort) {
1646 if (auto *Arg = Args.getLastArg(OPT_call_graph_ordering_file))
1647 if (Optional<MemoryBufferRef> Buffer = readFile(Arg->getValue()))
1648 readCallGraph(*Buffer);
1649 readCallGraphsFromObjectFiles<ELFT>();
1652 // Write the result to the file.
1653 writeResult<ELFT>();