//===- Driver.cpp ---------------------------------------------------------===// // // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. // See https://llvm.org/LICENSE.txt for license information. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception // //===----------------------------------------------------------------------===// // // The driver drives the entire linking process. It is responsible for // parsing command line options and doing whatever it is instructed to do. // // One notable thing in the LLD's driver when compared to other linkers is // that the LLD's driver is agnostic on the host operating system. // Other linkers usually have implicit default values (such as a dynamic // linker path or library paths) for each host OS. // // I don't think implicit default values are useful because they are // usually explicitly specified by the compiler driver. They can even // be harmful when you are doing cross-linking. Therefore, in LLD, we // simply trust the compiler driver to pass all required options and // don't try to make effort on our side. // //===----------------------------------------------------------------------===// #include "Driver.h" #include "Config.h" #include "ICF.h" #include "InputFiles.h" #include "InputSection.h" #include "LinkerScript.h" #include "MarkLive.h" #include "OutputSections.h" #include "ScriptParser.h" #include "SymbolTable.h" #include "Symbols.h" #include "SyntheticSections.h" #include "Target.h" #include "Writer.h" #include "lld/Common/Args.h" #include "lld/Common/Driver.h" #include "lld/Common/ErrorHandler.h" #include "lld/Common/Filesystem.h" #include "lld/Common/Memory.h" #include "lld/Common/Strings.h" #include "lld/Common/TargetOptionsCommandFlags.h" #include "lld/Common/Version.h" #include "llvm/ADT/SetVector.h" #include "llvm/ADT/StringExtras.h" #include "llvm/ADT/StringSwitch.h" #include "llvm/Config/llvm-config.h" #include "llvm/LTO/LTO.h" #include "llvm/Remarks/HotnessThresholdParser.h" #include "llvm/Support/CommandLine.h" #include "llvm/Support/Compression.h" #include "llvm/Support/GlobPattern.h" #include "llvm/Support/LEB128.h" #include "llvm/Support/Parallel.h" #include "llvm/Support/Path.h" #include "llvm/Support/TarWriter.h" #include "llvm/Support/TargetSelect.h" #include "llvm/Support/TimeProfiler.h" #include "llvm/Support/raw_ostream.h" #include #include using namespace llvm; using namespace llvm::ELF; using namespace llvm::object; using namespace llvm::sys; using namespace llvm::support; using namespace lld; using namespace lld::elf; Configuration *elf::config; LinkerDriver *elf::driver; static void setConfigs(opt::InputArgList &args); static void readConfigs(opt::InputArgList &args); bool elf::link(ArrayRef args, bool canExitEarly, raw_ostream &stdoutOS, raw_ostream &stderrOS) { lld::stdoutOS = &stdoutOS; lld::stderrOS = &stderrOS; errorHandler().cleanupCallback = []() { freeArena(); inputSections.clear(); outputSections.clear(); archiveFiles.clear(); binaryFiles.clear(); bitcodeFiles.clear(); lazyObjFiles.clear(); objectFiles.clear(); sharedFiles.clear(); backwardReferences.clear(); tar = nullptr; memset(&in, 0, sizeof(in)); partitions = {Partition()}; SharedFile::vernauxNum = 0; }; errorHandler().logName = args::getFilenameWithoutExe(args[0]); errorHandler().errorLimitExceededMsg = "too many errors emitted, stopping now (use " "-error-limit=0 to see all errors)"; errorHandler().exitEarly = canExitEarly; stderrOS.enable_colors(stderrOS.has_colors()); config = make(); driver = make(); script = make(); symtab = make(); partitions = {Partition()}; config->progName = args[0]; driver->linkerMain(args); // Exit immediately if we don't need to return to the caller. // This saves time because the overhead of calling destructors // for all globally-allocated objects is not negligible. if (canExitEarly) exitLld(errorCount() ? 1 : 0); bool ret = errorCount() == 0; if (!canExitEarly) errorHandler().reset(); return ret; } // Parses a linker -m option. static std::tuple parseEmulation(StringRef emul) { uint8_t osabi = 0; StringRef s = emul; if (s.endswith("_fbsd")) { s = s.drop_back(5); osabi = ELFOSABI_FREEBSD; } std::pair ret = StringSwitch>(s) .Cases("aarch64elf", "aarch64linux", "aarch64_elf64_le_vec", {ELF64LEKind, EM_AARCH64}) .Cases("armelf", "armelf_linux_eabi", {ELF32LEKind, EM_ARM}) .Case("elf32_x86_64", {ELF32LEKind, EM_X86_64}) .Cases("elf32btsmip", "elf32btsmipn32", {ELF32BEKind, EM_MIPS}) .Cases("elf32ltsmip", "elf32ltsmipn32", {ELF32LEKind, EM_MIPS}) .Case("elf32lriscv", {ELF32LEKind, EM_RISCV}) .Cases("elf32ppc", "elf32ppclinux", {ELF32BEKind, EM_PPC}) .Cases("elf32lppc", "elf32lppclinux", {ELF32LEKind, EM_PPC}) .Case("elf64btsmip", {ELF64BEKind, EM_MIPS}) .Case("elf64ltsmip", {ELF64LEKind, EM_MIPS}) .Case("elf64lriscv", {ELF64LEKind, EM_RISCV}) .Case("elf64ppc", {ELF64BEKind, EM_PPC64}) .Case("elf64lppc", {ELF64LEKind, EM_PPC64}) .Cases("elf_amd64", "elf_x86_64", {ELF64LEKind, EM_X86_64}) .Case("elf_i386", {ELF32LEKind, EM_386}) .Case("elf_iamcu", {ELF32LEKind, EM_IAMCU}) .Case("elf64_sparc", {ELF64BEKind, EM_SPARCV9}) .Case("msp430elf", {ELF32LEKind, EM_MSP430}) .Default({ELFNoneKind, EM_NONE}); if (ret.first == ELFNoneKind) error("unknown emulation: " + emul); if (ret.second == EM_MSP430) osabi = ELFOSABI_STANDALONE; return std::make_tuple(ret.first, ret.second, osabi); } // Returns slices of MB by parsing MB as an archive file. // Each slice consists of a member file in the archive. std::vector> static getArchiveMembers( MemoryBufferRef mb) { std::unique_ptr file = CHECK(Archive::create(mb), mb.getBufferIdentifier() + ": failed to parse archive"); std::vector> v; Error err = Error::success(); bool addToTar = file->isThin() && tar; for (const Archive::Child &c : file->children(err)) { MemoryBufferRef mbref = CHECK(c.getMemoryBufferRef(), mb.getBufferIdentifier() + ": could not get the buffer for a child of the archive"); if (addToTar) tar->append(relativeToRoot(check(c.getFullName())), mbref.getBuffer()); v.push_back(std::make_pair(mbref, c.getChildOffset())); } if (err) fatal(mb.getBufferIdentifier() + ": Archive::children failed: " + toString(std::move(err))); // Take ownership of memory buffers created for members of thin archives. for (std::unique_ptr &mb : file->takeThinBuffers()) make>(std::move(mb)); return v; } // Opens a file and create a file object. Path has to be resolved already. void LinkerDriver::addFile(StringRef path, bool withLOption) { using namespace sys::fs; Optional buffer = readFile(path); if (!buffer.hasValue()) return; MemoryBufferRef mbref = *buffer; if (config->formatBinary) { files.push_back(make(mbref)); return; } switch (identify_magic(mbref.getBuffer())) { case file_magic::unknown: readLinkerScript(mbref); return; case file_magic::archive: { // Handle -whole-archive. if (inWholeArchive) { for (const auto &p : getArchiveMembers(mbref)) files.push_back(createObjectFile(p.first, path, p.second)); return; } std::unique_ptr file = CHECK(Archive::create(mbref), path + ": failed to parse archive"); // If an archive file has no symbol table, it is likely that a user // is attempting LTO and using a default ar command that doesn't // understand the LLVM bitcode file. It is a pretty common error, so // we'll handle it as if it had a symbol table. if (!file->isEmpty() && !file->hasSymbolTable()) { // Check if all members are bitcode files. If not, ignore, which is the // default action without the LTO hack described above. for (const std::pair &p : getArchiveMembers(mbref)) if (identify_magic(p.first.getBuffer()) != file_magic::bitcode) { error(path + ": archive has no index; run ranlib to add one"); return; } for (const std::pair &p : getArchiveMembers(mbref)) files.push_back(make(p.first, path, p.second)); return; } // Handle the regular case. files.push_back(make(std::move(file))); return; } case file_magic::elf_shared_object: if (config->isStatic || config->relocatable) { error("attempted static link of dynamic object " + path); return; } // DSOs usually have DT_SONAME tags in their ELF headers, and the // sonames are used to identify DSOs. But if they are missing, // they are identified by filenames. We don't know whether the new // file has a DT_SONAME or not because we haven't parsed it yet. // Here, we set the default soname for the file because we might // need it later. // // If a file was specified by -lfoo, the directory part is not // significant, as a user did not specify it. This behavior is // compatible with GNU. files.push_back( make(mbref, withLOption ? path::filename(path) : path)); return; case file_magic::bitcode: case file_magic::elf_relocatable: if (inLib) files.push_back(make(mbref, "", 0)); else files.push_back(createObjectFile(mbref)); break; default: error(path + ": unknown file type"); } } // Add a given library by searching it from input search paths. void LinkerDriver::addLibrary(StringRef name) { if (Optional path = searchLibrary(name)) addFile(*path, /*withLOption=*/true); else error("unable to find library -l" + name, ErrorTag::LibNotFound, {name}); } // This function is called on startup. We need this for LTO since // LTO calls LLVM functions to compile bitcode files to native code. // Technically this can be delayed until we read bitcode files, but // we don't bother to do lazily because the initialization is fast. static void initLLVM() { InitializeAllTargets(); InitializeAllTargetMCs(); InitializeAllAsmPrinters(); InitializeAllAsmParsers(); } // Some command line options or some combinations of them are not allowed. // This function checks for such errors. static void checkOptions() { // The MIPS ABI as of 2016 does not support the GNU-style symbol lookup // table which is a relatively new feature. if (config->emachine == EM_MIPS && config->gnuHash) error("the .gnu.hash section is not compatible with the MIPS target"); if (config->fixCortexA53Errata843419 && config->emachine != EM_AARCH64) error("--fix-cortex-a53-843419 is only supported on AArch64 targets"); if (config->fixCortexA8 && config->emachine != EM_ARM) error("--fix-cortex-a8 is only supported on ARM targets"); if (config->tocOptimize && config->emachine != EM_PPC64) error("--toc-optimize is only supported on PowerPC64 targets"); if (config->pcRelOptimize && config->emachine != EM_PPC64) error("--pcrel-optimize is only supported on PowerPC64 targets"); if (config->pie && config->shared) error("-shared and -pie may not be used together"); if (!config->shared && !config->filterList.empty()) error("-F may not be used without -shared"); if (!config->shared && !config->auxiliaryList.empty()) error("-f may not be used without -shared"); if (!config->relocatable && !config->defineCommon) error("-no-define-common not supported in non relocatable output"); if (config->strip == StripPolicy::All && config->emitRelocs) error("--strip-all and --emit-relocs may not be used together"); if (config->zText && config->zIfuncNoplt) error("-z text and -z ifunc-noplt may not be used together"); if (config->relocatable) { if (config->shared) error("-r and -shared may not be used together"); if (config->gdbIndex) error("-r and --gdb-index may not be used together"); if (config->icf != ICFLevel::None) error("-r and --icf may not be used together"); if (config->pie) error("-r and -pie may not be used together"); if (config->exportDynamic) error("-r and --export-dynamic may not be used together"); } if (config->executeOnly) { if (config->emachine != EM_AARCH64) error("-execute-only is only supported on AArch64 targets"); if (config->singleRoRx && !script->hasSectionsCommand) error("-execute-only and -no-rosegment cannot be used together"); } if (config->zRetpolineplt && config->zForceIbt) error("-z force-ibt may not be used with -z retpolineplt"); if (config->emachine != EM_AARCH64) { if (config->zPacPlt) error("-z pac-plt only supported on AArch64"); if (config->zForceBti) error("-z force-bti only supported on AArch64"); } } static const char *getReproduceOption(opt::InputArgList &args) { if (auto *arg = args.getLastArg(OPT_reproduce)) return arg->getValue(); return getenv("LLD_REPRODUCE"); } static bool hasZOption(opt::InputArgList &args, StringRef key) { for (auto *arg : args.filtered(OPT_z)) if (key == arg->getValue()) return true; return false; } static bool getZFlag(opt::InputArgList &args, StringRef k1, StringRef k2, bool Default) { for (auto *arg : args.filtered_reverse(OPT_z)) { if (k1 == arg->getValue()) return true; if (k2 == arg->getValue()) return false; } return Default; } static SeparateSegmentKind getZSeparate(opt::InputArgList &args) { for (auto *arg : args.filtered_reverse(OPT_z)) { StringRef v = arg->getValue(); if (v == "noseparate-code") return SeparateSegmentKind::None; if (v == "separate-code") return SeparateSegmentKind::Code; if (v == "separate-loadable-segments") return SeparateSegmentKind::Loadable; } return SeparateSegmentKind::None; } static GnuStackKind getZGnuStack(opt::InputArgList &args) { for (auto *arg : args.filtered_reverse(OPT_z)) { if (StringRef("execstack") == arg->getValue()) return GnuStackKind::Exec; if (StringRef("noexecstack") == arg->getValue()) return GnuStackKind::NoExec; if (StringRef("nognustack") == arg->getValue()) return GnuStackKind::None; } return GnuStackKind::NoExec; } static uint8_t getZStartStopVisibility(opt::InputArgList &args) { for (auto *arg : args.filtered_reverse(OPT_z)) { std::pair kv = StringRef(arg->getValue()).split('='); if (kv.first == "start-stop-visibility") { if (kv.second == "default") return STV_DEFAULT; else if (kv.second == "internal") return STV_INTERNAL; else if (kv.second == "hidden") return STV_HIDDEN; else if (kv.second == "protected") return STV_PROTECTED; error("unknown -z start-stop-visibility= value: " + StringRef(kv.second)); } } return STV_PROTECTED; } static bool isKnownZFlag(StringRef s) { return s == "combreloc" || s == "copyreloc" || s == "defs" || s == "execstack" || s == "force-bti" || s == "force-ibt" || s == "global" || s == "hazardplt" || s == "ifunc-noplt" || s == "initfirst" || s == "interpose" || s == "keep-text-section-prefix" || s == "lazy" || s == "muldefs" || s == "separate-code" || s == "separate-loadable-segments" || s == "nocombreloc" || s == "nocopyreloc" || s == "nodefaultlib" || s == "nodelete" || s == "nodlopen" || s == "noexecstack" || s == "nognustack" || s == "nokeep-text-section-prefix" || s == "norelro" || s == "noseparate-code" || s == "notext" || s == "now" || s == "origin" || s == "pac-plt" || s == "rel" || s == "rela" || s == "relro" || s == "retpolineplt" || s == "rodynamic" || s == "shstk" || s == "text" || s == "undefs" || s == "wxneeded" || s.startswith("common-page-size=") || s.startswith("dead-reloc-in-nonalloc=") || s.startswith("max-page-size=") || s.startswith("stack-size=") || s.startswith("start-stop-visibility="); } // Report an error for an unknown -z option. static void checkZOptions(opt::InputArgList &args) { for (auto *arg : args.filtered(OPT_z)) if (!isKnownZFlag(arg->getValue())) error("unknown -z value: " + StringRef(arg->getValue())); } void LinkerDriver::linkerMain(ArrayRef argsArr) { ELFOptTable parser; opt::InputArgList args = parser.parse(argsArr.slice(1)); // Interpret this flag early because error() depends on them. errorHandler().errorLimit = args::getInteger(args, OPT_error_limit, 20); checkZOptions(args); // Handle -help if (args.hasArg(OPT_help)) { printHelp(); return; } // Handle -v or -version. // // A note about "compatible with GNU linkers" message: this is a hack for // scripts generated by GNU Libtool 2.4.6 (released in February 2014 and // still the newest version in March 2017) or earlier to recognize LLD as // a GNU compatible linker. As long as an output for the -v option // contains "GNU" or "with BFD", they recognize us as GNU-compatible. // // This is somewhat ugly hack, but in reality, we had no choice other // than doing this. Considering the very long release cycle of Libtool, // it is not easy to improve it to recognize LLD as a GNU compatible // linker in a timely manner. Even if we can make it, there are still a // lot of "configure" scripts out there that are generated by old version // of Libtool. We cannot convince every software developer to migrate to // the latest version and re-generate scripts. So we have this hack. if (args.hasArg(OPT_v) || args.hasArg(OPT_version)) message(getLLDVersion() + " (compatible with GNU linkers)"); if (const char *path = getReproduceOption(args)) { // Note that --reproduce is a debug option so you can ignore it // if you are trying to understand the whole picture of the code. Expected> errOrWriter = TarWriter::create(path, path::stem(path)); if (errOrWriter) { tar = std::move(*errOrWriter); tar->append("response.txt", createResponseFile(args)); tar->append("version.txt", getLLDVersion() + "\n"); StringRef ltoSampleProfile = args.getLastArgValue(OPT_lto_sample_profile); if (!ltoSampleProfile.empty()) readFile(ltoSampleProfile); } else { error("--reproduce: " + toString(errOrWriter.takeError())); } } readConfigs(args); // The behavior of -v or --version is a bit strange, but this is // needed for compatibility with GNU linkers. if (args.hasArg(OPT_v) && !args.hasArg(OPT_INPUT)) return; if (args.hasArg(OPT_version)) return; // Initialize time trace profiler. if (config->timeTraceEnabled) timeTraceProfilerInitialize(config->timeTraceGranularity, config->progName); { llvm::TimeTraceScope timeScope("ExecuteLinker"); initLLVM(); createFiles(args); if (errorCount()) return; inferMachineType(); setConfigs(args); checkOptions(); if (errorCount()) return; // The Target instance handles target-specific stuff, such as applying // relocations or writing a PLT section. It also contains target-dependent // values such as a default image base address. target = getTarget(); switch (config->ekind) { case ELF32LEKind: link(args); break; case ELF32BEKind: link(args); break; case ELF64LEKind: link(args); break; case ELF64BEKind: link(args); break; default: llvm_unreachable("unknown Config->EKind"); } } if (config->timeTraceEnabled) { if (auto E = timeTraceProfilerWrite(args.getLastArgValue(OPT_time_trace_file_eq).str(), config->outputFile)) { handleAllErrors(std::move(E), [&](const StringError &SE) { error(SE.getMessage()); }); return; } timeTraceProfilerCleanup(); } } static std::string getRpath(opt::InputArgList &args) { std::vector v = args::getStrings(args, OPT_rpath); return llvm::join(v.begin(), v.end(), ":"); } // Determines what we should do if there are remaining unresolved // symbols after the name resolution. static void setUnresolvedSymbolPolicy(opt::InputArgList &args) { UnresolvedPolicy errorOrWarn = args.hasFlag(OPT_error_unresolved_symbols, OPT_warn_unresolved_symbols, true) ? UnresolvedPolicy::ReportError : UnresolvedPolicy::Warn; // -shared implies -unresolved-symbols=ignore-all because missing // symbols are likely to be resolved at runtime. bool diagRegular = !config->shared, diagShlib = !config->shared; for (const opt::Arg *arg : args) { switch (arg->getOption().getID()) { case OPT_unresolved_symbols: { StringRef s = arg->getValue(); if (s == "ignore-all") { diagRegular = false; diagShlib = false; } else if (s == "ignore-in-object-files") { diagRegular = false; diagShlib = true; } else if (s == "ignore-in-shared-libs") { diagRegular = true; diagShlib = false; } else if (s == "report-all") { diagRegular = true; diagShlib = true; } else { error("unknown --unresolved-symbols value: " + s); } break; } case OPT_no_undefined: diagRegular = true; break; case OPT_z: if (StringRef(arg->getValue()) == "defs") diagRegular = true; else if (StringRef(arg->getValue()) == "undefs") diagRegular = false; break; case OPT_allow_shlib_undefined: diagShlib = false; break; case OPT_no_allow_shlib_undefined: diagShlib = true; break; } } config->unresolvedSymbols = diagRegular ? errorOrWarn : UnresolvedPolicy::Ignore; config->unresolvedSymbolsInShlib = diagShlib ? errorOrWarn : UnresolvedPolicy::Ignore; } static Target2Policy getTarget2(opt::InputArgList &args) { StringRef s = args.getLastArgValue(OPT_target2, "got-rel"); if (s == "rel") return Target2Policy::Rel; if (s == "abs") return Target2Policy::Abs; if (s == "got-rel") return Target2Policy::GotRel; error("unknown --target2 option: " + s); return Target2Policy::GotRel; } static bool isOutputFormatBinary(opt::InputArgList &args) { StringRef s = args.getLastArgValue(OPT_oformat, "elf"); if (s == "binary") return true; if (!s.startswith("elf")) error("unknown --oformat value: " + s); return false; } static DiscardPolicy getDiscard(opt::InputArgList &args) { auto *arg = args.getLastArg(OPT_discard_all, OPT_discard_locals, OPT_discard_none); if (!arg) return DiscardPolicy::Default; if (arg->getOption().getID() == OPT_discard_all) return DiscardPolicy::All; if (arg->getOption().getID() == OPT_discard_locals) return DiscardPolicy::Locals; return DiscardPolicy::None; } static StringRef getDynamicLinker(opt::InputArgList &args) { auto *arg = args.getLastArg(OPT_dynamic_linker, OPT_no_dynamic_linker); if (!arg) return ""; if (arg->getOption().getID() == OPT_no_dynamic_linker) { // --no-dynamic-linker suppresses undefined weak symbols in .dynsym config->noDynamicLinker = true; return ""; } return arg->getValue(); } static ICFLevel getICF(opt::InputArgList &args) { auto *arg = args.getLastArg(OPT_icf_none, OPT_icf_safe, OPT_icf_all); if (!arg || arg->getOption().getID() == OPT_icf_none) return ICFLevel::None; if (arg->getOption().getID() == OPT_icf_safe) return ICFLevel::Safe; return ICFLevel::All; } static StripPolicy getStrip(opt::InputArgList &args) { if (args.hasArg(OPT_relocatable)) return StripPolicy::None; auto *arg = args.getLastArg(OPT_strip_all, OPT_strip_debug); if (!arg) return StripPolicy::None; if (arg->getOption().getID() == OPT_strip_all) return StripPolicy::All; return StripPolicy::Debug; } static uint64_t parseSectionAddress(StringRef s, opt::InputArgList &args, const opt::Arg &arg) { uint64_t va = 0; if (s.startswith("0x")) s = s.drop_front(2); if (!to_integer(s, va, 16)) error("invalid argument: " + arg.getAsString(args)); return va; } static StringMap getSectionStartMap(opt::InputArgList &args) { StringMap ret; for (auto *arg : args.filtered(OPT_section_start)) { StringRef name; StringRef addr; std::tie(name, addr) = StringRef(arg->getValue()).split('='); ret[name] = parseSectionAddress(addr, args, *arg); } if (auto *arg = args.getLastArg(OPT_Ttext)) ret[".text"] = parseSectionAddress(arg->getValue(), args, *arg); if (auto *arg = args.getLastArg(OPT_Tdata)) ret[".data"] = parseSectionAddress(arg->getValue(), args, *arg); if (auto *arg = args.getLastArg(OPT_Tbss)) ret[".bss"] = parseSectionAddress(arg->getValue(), args, *arg); return ret; } static SortSectionPolicy getSortSection(opt::InputArgList &args) { StringRef s = args.getLastArgValue(OPT_sort_section); if (s == "alignment") return SortSectionPolicy::Alignment; if (s == "name") return SortSectionPolicy::Name; if (!s.empty()) error("unknown --sort-section rule: " + s); return SortSectionPolicy::Default; } static OrphanHandlingPolicy getOrphanHandling(opt::InputArgList &args) { StringRef s = args.getLastArgValue(OPT_orphan_handling, "place"); if (s == "warn") return OrphanHandlingPolicy::Warn; if (s == "error") return OrphanHandlingPolicy::Error; if (s != "place") error("unknown --orphan-handling mode: " + s); return OrphanHandlingPolicy::Place; } // Parse --build-id or --build-id=