//===- 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 ctx.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 "LTO.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/CommonLinkerContext.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/STLExtras.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/Object/Archive.h" #include "llvm/Object/IRObjectFile.h" #include "llvm/Remarks/HotnessThresholdParser.h" #include "llvm/Support/CommandLine.h" #include "llvm/Support/SaveAndRestore.h" #include "llvm/Support/Compression.h" #include "llvm/Support/FileSystem.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 #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; static void setConfigs(Ctx &ctx, opt::InputArgList &args); static void readConfigs(Ctx &ctx, opt::InputArgList &args); ELFSyncStream elf::Log(Ctx &ctx) { return {ctx, DiagLevel::Log}; } ELFSyncStream elf::Msg(Ctx &ctx) { return {ctx, DiagLevel::Msg}; } ELFSyncStream elf::Warn(Ctx &ctx) { return {ctx, DiagLevel::Warn}; } ELFSyncStream elf::Err(Ctx &ctx) { return {ctx, ctx.arg.noinhibitExec ? DiagLevel::Warn : DiagLevel::Err}; } ELFSyncStream elf::ErrAlways(Ctx &ctx) { return {ctx, DiagLevel::Err}; } ELFSyncStream elf::Fatal(Ctx &ctx) { return {ctx, DiagLevel::Fatal}; } uint64_t elf::errCount(Ctx &ctx) { return ctx.e.errorCount; } ELFSyncStream elf::InternalErr(Ctx &ctx, const uint8_t *buf) { ELFSyncStream s(ctx, DiagLevel::Err); s << "internal linker error: "; return s; } Ctx::Ctx() : driver(*this) {} llvm::raw_fd_ostream Ctx::openAuxiliaryFile(llvm::StringRef filename, std::error_code &ec) { using namespace llvm::sys::fs; OpenFlags flags = auxiliaryFiles.insert(filename).second ? OF_None : OF_Append; if (e.disableOutput && filename == "-") { #ifdef _WIN32 filename = "NUL"; #else filename = "/dev/null"; #endif } return {filename, ec, flags}; } namespace lld { namespace elf { bool link(ArrayRef args, llvm::raw_ostream &stdoutOS, llvm::raw_ostream &stderrOS, bool exitEarly, bool disableOutput) { // This driver-specific context will be freed later by unsafeLldMain(). auto *context = new Ctx; Ctx &ctx = *context; context->e.initialize(stdoutOS, stderrOS, exitEarly, disableOutput); context->e.logName = args::getFilenameWithoutExe(args[0]); context->e.errorLimitExceededMsg = "too many errors emitted, stopping now (use " "--error-limit=0 to see all errors)"; LinkerScript script(ctx); ctx.script = &script; ctx.symAux.emplace_back(); ctx.symtab = std::make_unique(ctx); ctx.partitions.clear(); ctx.partitions.emplace_back(ctx); ctx.arg.progName = args[0]; ctx.driver.linkerMain(args); return errCount(ctx) == 0; } } // namespace elf } // namespace lld // Parses a linker -m option. static std::tuple parseEmulation(Ctx &ctx, StringRef emul) { uint8_t osabi = 0; StringRef s = emul; if (s.ends_with("_fbsd")) { s = s.drop_back(5); osabi = ELFOSABI_FREEBSD; } std::pair ret = StringSwitch>(s) .Cases("aarch64elf", "aarch64linux", {ELF64LEKind, EM_AARCH64}) .Cases("aarch64elfb", "aarch64linuxb", {ELF64BEKind, EM_AARCH64}) .Cases("armelf", "armelf_linux_eabi", {ELF32LEKind, EM_ARM}) .Cases("armelfb", "armelfb_linux_eabi", {ELF32BEKind, 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("elf32loongarch", {ELF32LEKind, EM_LOONGARCH}) .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}) .Case("elf64_amdgpu", {ELF64LEKind, EM_AMDGPU}) .Case("elf64loongarch", {ELF64LEKind, EM_LOONGARCH}) .Case("elf64_s390", {ELF64BEKind, EM_S390}) .Case("hexagonelf", {ELF32LEKind, EM_HEXAGON}) .Default({ELFNoneKind, EM_NONE}); if (ret.first == ELFNoneKind) ErrAlways(ctx) << "unknown emulation: " << emul; if (ret.second == EM_MSP430) osabi = ELFOSABI_STANDALONE; else if (ret.second == EM_AMDGPU) osabi = ELFOSABI_AMDGPU_HSA; 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( Ctx &ctx, 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() && ctx.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) ctx.tar->append(relativeToRoot(check(c.getFullName())), mbref.getBuffer()); v.push_back(std::make_pair(mbref, c.getChildOffset())); } if (err) Fatal(ctx) << mb.getBufferIdentifier() << ": Archive::children failed: " << std::move(err); // Take ownership of memory buffers created for members of thin archives. std::vector> mbs = file->takeThinBuffers(); std::move(mbs.begin(), mbs.end(), std::back_inserter(ctx.memoryBuffers)); return v; } static bool isBitcode(MemoryBufferRef mb) { return identify_magic(mb.getBuffer()) == llvm::file_magic::bitcode; } bool LinkerDriver::tryAddFatLTOFile(MemoryBufferRef mb, StringRef archiveName, uint64_t offsetInArchive, bool lazy) { if (!ctx.arg.fatLTOObjects) return false; Expected fatLTOData = IRObjectFile::findBitcodeInMemBuffer(mb); if (errorToBool(fatLTOData.takeError())) return false; files.push_back(std::make_unique(ctx, *fatLTOData, archiveName, offsetInArchive, lazy)); return true; } // 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; std::optional buffer = readFile(ctx, path); if (!buffer) return; MemoryBufferRef mbref = *buffer; if (ctx.arg.formatBinary) { files.push_back(std::make_unique(ctx, mbref)); return; } switch (identify_magic(mbref.getBuffer())) { case file_magic::unknown: readLinkerScript(ctx, mbref); return; case file_magic::archive: { auto members = getArchiveMembers(ctx, mbref); if (inWholeArchive) { for (const std::pair &p : members) { if (isBitcode(p.first)) files.push_back(std::make_unique(ctx, p.first, path, p.second, false)); else if (!tryAddFatLTOFile(p.first, path, p.second, false)) files.push_back(createObjFile(ctx, p.first, path)); } return; } archiveFiles.emplace_back(path, members.size()); // Handle archives and --start-lib/--end-lib using the same code path. This // scans all the ELF relocatable object files and bitcode files in the // archive rather than just the index file, with the benefit that the // symbols are only loaded once. For many projects archives see high // utilization rates and it is a net performance win. --start-lib scans // symbols in the same order that llvm-ar adds them to the index, so in the // common case the semantics are identical. If the archive symbol table was // created in a different order, or is incomplete, this strategy has // different semantics. Such output differences are considered user error. // // All files within the archive get the same group ID to allow mutual // references for --warn-backrefs. SaveAndRestore saved(isInGroup, true); for (const std::pair &p : members) { auto magic = identify_magic(p.first.getBuffer()); if (magic == file_magic::elf_relocatable) { if (!tryAddFatLTOFile(p.first, path, p.second, true)) files.push_back(createObjFile(ctx, p.first, path, true)); } else if (magic == file_magic::bitcode) files.push_back( std::make_unique(ctx, p.first, path, p.second, true)); else Warn(ctx) << path << ": archive member '" << p.first.getBufferIdentifier() << "' is neither ET_REL nor LLVM bitcode"; } if (!saved.get()) ++nextGroupId; return; } case file_magic::elf_shared_object: { if (ctx.arg.isStatic) { ErrAlways(ctx) << "attempted static link of dynamic object " << path; return; } // Shared objects are identified by soname. soname is (if specified) // DT_SONAME and falls back to filename. If a file was specified by -lfoo, // the directory part is ignored. Note that path may be a temporary and // cannot be stored into SharedFile::soName. path = mbref.getBufferIdentifier(); auto f = std::make_unique( ctx, mbref, withLOption ? path::filename(path) : path); f->init(); files.push_back(std::move(f)); return; } case file_magic::bitcode: files.push_back(std::make_unique(ctx, mbref, "", 0, inLib)); break; case file_magic::elf_relocatable: if (!tryAddFatLTOFile(mbref, "", 0, inLib)) files.push_back(createObjFile(ctx, mbref, "", inLib)); break; default: ErrAlways(ctx) << path << ": unknown file type"; } } // Add a given library by searching it from input search paths. void LinkerDriver::addLibrary(StringRef name) { if (std::optional path = searchLibrary(ctx, name)) addFile(ctx.saver.save(*path), /*withLOption=*/true); else ctx.e.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(Ctx &ctx) { // The MIPS ABI as of 2016 does not support the GNU-style symbol lookup // table which is a relatively new feature. if (ctx.arg.emachine == EM_MIPS && ctx.arg.gnuHash) ErrAlways(ctx) << "the .gnu.hash section is not compatible with the MIPS target"; if (ctx.arg.emachine == EM_ARM) { if (!ctx.arg.cmseImplib) { if (!ctx.arg.cmseInputLib.empty()) ErrAlways(ctx) << "--in-implib may not be used without --cmse-implib"; if (!ctx.arg.cmseOutputLib.empty()) ErrAlways(ctx) << "--out-implib may not be used without --cmse-implib"; } } else { if (ctx.arg.cmseImplib) ErrAlways(ctx) << "--cmse-implib is only supported on ARM targets"; if (!ctx.arg.cmseInputLib.empty()) ErrAlways(ctx) << "--in-implib is only supported on ARM targets"; if (!ctx.arg.cmseOutputLib.empty()) ErrAlways(ctx) << "--out-implib is only supported on ARM targets"; } if (ctx.arg.fixCortexA53Errata843419 && ctx.arg.emachine != EM_AARCH64) ErrAlways(ctx) << "--fix-cortex-a53-843419 is only supported on AArch64 targets"; if (ctx.arg.fixCortexA8 && ctx.arg.emachine != EM_ARM) ErrAlways(ctx) << "--fix-cortex-a8 is only supported on ARM targets"; if (ctx.arg.armBe8 && ctx.arg.emachine != EM_ARM) ErrAlways(ctx) << "--be8 is only supported on ARM targets"; if (ctx.arg.fixCortexA8 && !ctx.arg.isLE) ErrAlways(ctx) << "--fix-cortex-a8 is not supported on big endian targets"; if (ctx.arg.tocOptimize && ctx.arg.emachine != EM_PPC64) ErrAlways(ctx) << "--toc-optimize is only supported on PowerPC64 targets"; if (ctx.arg.pcRelOptimize && ctx.arg.emachine != EM_PPC64) ErrAlways(ctx) << "--pcrel-optimize is only supported on PowerPC64 targets"; if (ctx.arg.relaxGP && ctx.arg.emachine != EM_RISCV) ErrAlways(ctx) << "--relax-gp is only supported on RISC-V targets"; if (ctx.arg.pie && ctx.arg.shared) ErrAlways(ctx) << "-shared and -pie may not be used together"; if (!ctx.arg.shared && !ctx.arg.filterList.empty()) ErrAlways(ctx) << "-F may not be used without -shared"; if (!ctx.arg.shared && !ctx.arg.auxiliaryList.empty()) ErrAlways(ctx) << "-f may not be used without -shared"; if (ctx.arg.strip == StripPolicy::All && ctx.arg.emitRelocs) ErrAlways(ctx) << "--strip-all and --emit-relocs may not be used together"; if (ctx.arg.zText && ctx.arg.zIfuncNoplt) ErrAlways(ctx) << "-z text and -z ifunc-noplt may not be used together"; if (ctx.arg.relocatable) { if (ctx.arg.shared) ErrAlways(ctx) << "-r and -shared may not be used together"; if (ctx.arg.gdbIndex) ErrAlways(ctx) << "-r and --gdb-index may not be used together"; if (ctx.arg.icf != ICFLevel::None) ErrAlways(ctx) << "-r and --icf may not be used together"; if (ctx.arg.pie) ErrAlways(ctx) << "-r and -pie may not be used together"; if (ctx.arg.exportDynamic) ErrAlways(ctx) << "-r and --export-dynamic may not be used together"; if (ctx.arg.debugNames) ErrAlways(ctx) << "-r and --debug-names may not be used together"; if (!ctx.arg.zSectionHeader) ErrAlways(ctx) << "-r and -z nosectionheader may not be used together"; } if (ctx.arg.executeOnly) { if (ctx.arg.emachine != EM_AARCH64) ErrAlways(ctx) << "--execute-only is only supported on AArch64 targets"; if (ctx.arg.singleRoRx && !ctx.script->hasSectionsCommand) ErrAlways(ctx) << "--execute-only and --no-rosegment cannot be used together"; } if (ctx.arg.zRetpolineplt && ctx.arg.zForceIbt) ErrAlways(ctx) << "-z force-ibt may not be used with -z retpolineplt"; if (ctx.arg.emachine != EM_AARCH64) { if (ctx.arg.zPacPlt) ErrAlways(ctx) << "-z pac-plt only supported on AArch64"; if (ctx.arg.zForceBti) ErrAlways(ctx) << "-z force-bti only supported on AArch64"; if (ctx.arg.zBtiReport != "none") ErrAlways(ctx) << "-z bti-report only supported on AArch64"; if (ctx.arg.zPauthReport != "none") ErrAlways(ctx) << "-z pauth-report only supported on AArch64"; if (ctx.arg.zGcsReport != "none") ErrAlways(ctx) << "-z gcs-report only supported on AArch64"; if (ctx.arg.zGcs != GcsPolicy::Implicit) ErrAlways(ctx) << "-z gcs only supported on AArch64"; } if (ctx.arg.emachine != EM_386 && ctx.arg.emachine != EM_X86_64 && ctx.arg.zCetReport != "none") ErrAlways(ctx) << "-z cet-report only supported on X86 and X86_64"; } 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) { bool ret = false; for (auto *arg : args.filtered(OPT_z)) if (key == arg->getValue()) { ret = true; arg->claim(); } return ret; } static bool getZFlag(opt::InputArgList &args, StringRef k1, StringRef k2, bool defaultValue) { for (auto *arg : args.filtered(OPT_z)) { StringRef v = arg->getValue(); if (k1 == v) defaultValue = true; else if (k2 == v) defaultValue = false; else continue; arg->claim(); } return defaultValue; } static SeparateSegmentKind getZSeparate(opt::InputArgList &args) { auto ret = SeparateSegmentKind::None; for (auto *arg : args.filtered(OPT_z)) { StringRef v = arg->getValue(); if (v == "noseparate-code") ret = SeparateSegmentKind::None; else if (v == "separate-code") ret = SeparateSegmentKind::Code; else if (v == "separate-loadable-segments") ret = SeparateSegmentKind::Loadable; else continue; arg->claim(); } return ret; } static GnuStackKind getZGnuStack(opt::InputArgList &args) { auto ret = GnuStackKind::NoExec; for (auto *arg : args.filtered(OPT_z)) { StringRef v = arg->getValue(); if (v == "execstack") ret = GnuStackKind::Exec; else if (v == "noexecstack") ret = GnuStackKind::NoExec; else if (v == "nognustack") ret = GnuStackKind::None; else continue; arg->claim(); } return ret; } static uint8_t getZStartStopVisibility(Ctx &ctx, opt::InputArgList &args) { uint8_t ret = STV_PROTECTED; for (auto *arg : args.filtered(OPT_z)) { std::pair kv = StringRef(arg->getValue()).split('='); if (kv.first == "start-stop-visibility") { arg->claim(); if (kv.second == "default") ret = STV_DEFAULT; else if (kv.second == "internal") ret = STV_INTERNAL; else if (kv.second == "hidden") ret = STV_HIDDEN; else if (kv.second == "protected") ret = STV_PROTECTED; else ErrAlways(ctx) << "unknown -z start-stop-visibility= value: " << StringRef(kv.second); } } return ret; } static GcsPolicy getZGcs(Ctx &ctx, opt::InputArgList &args) { GcsPolicy ret = GcsPolicy::Implicit; for (auto *arg : args.filtered(OPT_z)) { std::pair kv = StringRef(arg->getValue()).split('='); if (kv.first == "gcs") { arg->claim(); if (kv.second == "implicit") ret = GcsPolicy::Implicit; else if (kv.second == "never") ret = GcsPolicy::Never; else if (kv.second == "always") ret = GcsPolicy::Always; else ErrAlways(ctx) << "unknown -z gcs= value: " << kv.second; } } return ret; } // Report a warning for an unknown -z option. static void checkZOptions(Ctx &ctx, opt::InputArgList &args) { // This function is called before getTarget(), when certain options are not // initialized yet. Claim them here. args::getZOptionValue(args, OPT_z, "max-page-size", 0); args::getZOptionValue(args, OPT_z, "common-page-size", 0); getZFlag(args, "rel", "rela", false); for (auto *arg : args.filtered(OPT_z)) if (!arg->isClaimed()) Warn(ctx) << "unknown -z value: " << StringRef(arg->getValue()); } constexpr const char *saveTempsValues[] = { "resolution", "preopt", "promote", "internalize", "import", "opt", "precodegen", "prelink", "combinedindex"}; LinkerDriver::LinkerDriver(Ctx &ctx) : ctx(ctx) {} void LinkerDriver::linkerMain(ArrayRef argsArr) { ELFOptTable parser; opt::InputArgList args = parser.parse(ctx, argsArr.slice(1)); // Interpret these flags early because Err/Warn depend on them. ctx.e.errorLimit = args::getInteger(args, OPT_error_limit, 20); ctx.e.fatalWarnings = args.hasFlag(OPT_fatal_warnings, OPT_no_fatal_warnings, false) && !args.hasArg(OPT_no_warnings); ctx.e.suppressWarnings = args.hasArg(OPT_no_warnings); // Handle -help if (args.hasArg(OPT_help)) { printHelp(ctx); return; } // Handle -v or -version. // // A note about "compatible with GNU linkers" message: this is a hack for // scripts generated by GNU Libtool up to 2021-10 to recognize LLD as // a GNU compatible linker. See // . // // 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)) Msg(ctx) << 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) { ctx.tar = std::move(*errOrWriter); ctx.tar->append("response.txt", createResponseFile(args)); ctx.tar->append("version.txt", getLLDVersion() + "\n"); StringRef ltoSampleProfile = args.getLastArgValue(OPT_lto_sample_profile); if (!ltoSampleProfile.empty()) readFile(ctx, ltoSampleProfile); } else { ErrAlways(ctx) << "--reproduce: " << errOrWriter.takeError(); } } readConfigs(ctx, args); checkZOptions(ctx, 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 (ctx.arg.timeTraceEnabled) timeTraceProfilerInitialize(ctx.arg.timeTraceGranularity, ctx.arg.progName); { llvm::TimeTraceScope timeScope("ExecuteLinker"); initLLVM(); createFiles(args); if (errCount(ctx)) return; inferMachineType(); setConfigs(ctx, args); checkOptions(ctx); if (errCount(ctx)) return; invokeELFT(link, args); } if (ctx.arg.timeTraceEnabled) { checkError(ctx.e, timeTraceProfilerWrite( args.getLastArgValue(OPT_time_trace_eq).str(), ctx.arg.outputFile)); timeTraceProfilerCleanup(); } } static std::string getRpath(opt::InputArgList &args) { SmallVector 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(Ctx &ctx, 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 = !ctx.arg.shared, diagShlib = !ctx.arg.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 { ErrAlways(ctx) << "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; else break; arg->claim(); break; case OPT_allow_shlib_undefined: diagShlib = false; break; case OPT_no_allow_shlib_undefined: diagShlib = true; break; } } ctx.arg.unresolvedSymbols = diagRegular ? errorOrWarn : UnresolvedPolicy::Ignore; ctx.arg.unresolvedSymbolsInShlib = diagShlib ? errorOrWarn : UnresolvedPolicy::Ignore; } static Target2Policy getTarget2(Ctx &ctx, 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; ErrAlways(ctx) << "unknown --target2 option: " << s; return Target2Policy::GotRel; } static bool isOutputFormatBinary(Ctx &ctx, opt::InputArgList &args) { StringRef s = args.getLastArgValue(OPT_oformat, "elf"); if (s == "binary") return true; if (!s.starts_with("elf")) ErrAlways(ctx) << "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(Ctx &ctx, 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 ctx.arg.noDynamicLinker = true; return ""; } return arg->getValue(); } static int getMemtagMode(Ctx &ctx, opt::InputArgList &args) { StringRef memtagModeArg = args.getLastArgValue(OPT_android_memtag_mode); if (memtagModeArg.empty()) { if (ctx.arg.androidMemtagStack) Warn(ctx) << "--android-memtag-mode is unspecified, leaving " "--android-memtag-stack a no-op"; else if (ctx.arg.androidMemtagHeap) Warn(ctx) << "--android-memtag-mode is unspecified, leaving " "--android-memtag-heap a no-op"; return ELF::NT_MEMTAG_LEVEL_NONE; } if (memtagModeArg == "sync") return ELF::NT_MEMTAG_LEVEL_SYNC; if (memtagModeArg == "async") return ELF::NT_MEMTAG_LEVEL_ASYNC; if (memtagModeArg == "none") return ELF::NT_MEMTAG_LEVEL_NONE; ErrAlways(ctx) << "unknown --android-memtag-mode value: \"" << memtagModeArg << "\", should be one of {async, sync, none}"; return ELF::NT_MEMTAG_LEVEL_NONE; } 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(Ctx &ctx, opt::InputArgList &args) { if (args.hasArg(OPT_relocatable)) return StripPolicy::None; if (!ctx.arg.zSectionHeader) return StripPolicy::All; 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(Ctx &ctx, StringRef s, opt::InputArgList &args, const opt::Arg &arg) { uint64_t va = 0; s.consume_front("0x"); if (!to_integer(s, va, 16)) ErrAlways(ctx) << "invalid argument: " << arg.getAsString(args); return va; } static StringMap getSectionStartMap(Ctx &ctx, 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(ctx, addr, args, *arg); } if (auto *arg = args.getLastArg(OPT_Ttext)) ret[".text"] = parseSectionAddress(ctx, arg->getValue(), args, *arg); if (auto *arg = args.getLastArg(OPT_Tdata)) ret[".data"] = parseSectionAddress(ctx, arg->getValue(), args, *arg); if (auto *arg = args.getLastArg(OPT_Tbss)) ret[".bss"] = parseSectionAddress(ctx, arg->getValue(), args, *arg); return ret; } static SortSectionPolicy getSortSection(Ctx &ctx, 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()) ErrAlways(ctx) << "unknown --sort-section rule: " << s; return SortSectionPolicy::Default; } static OrphanHandlingPolicy getOrphanHandling(Ctx &ctx, 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") ErrAlways(ctx) << "unknown --orphan-handling mode: " << s; return OrphanHandlingPolicy::Place; } // Parse --build-id or --build-id=