//===- 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/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/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; ConfigWrapper elf::config; Ctx elf::ctx; static void setConfigs(opt::InputArgList &args); static void readConfigs(opt::InputArgList &args); void elf::errorOrWarn(const Twine &msg) { if (config->noinhibitExec) warn(msg); else error(msg); } void Ctx::reset() { driver = LinkerDriver(); memoryBuffers.clear(); objectFiles.clear(); sharedFiles.clear(); binaryFiles.clear(); bitcodeFiles.clear(); lazyBitcodeFiles.clear(); inputSections.clear(); ehInputSections.clear(); duplicates.clear(); nonPrevailingSyms.clear(); whyExtractRecords.clear(); backwardReferences.clear(); auxiliaryFiles.clear(); internalFile = nullptr; hasSympart.store(false, std::memory_order_relaxed); hasTlsIe.store(false, std::memory_order_relaxed); needsTlsLd.store(false, std::memory_order_relaxed); scriptSymOrderCounter = 1; scriptSymOrder.clear(); ltoAllVtablesHaveTypeInfos = false; } 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; 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 *ctx = new CommonLinkerContext; ctx->e.initialize(stdoutOS, stderrOS, exitEarly, disableOutput); ctx->e.cleanupCallback = []() { elf::ctx.reset(); symtab = SymbolTable(); outputSections.clear(); symAux.clear(); tar = nullptr; in.reset(); partitions.clear(); partitions.emplace_back(); SharedFile::vernauxNum = 0; }; ctx->e.logName = args::getFilenameWithoutExe(args[0]); ctx->e.errorLimitExceededMsg = "too many errors emitted, stopping now (use " "--error-limit=0 to see all errors)"; config = ConfigWrapper(); script = std::make_unique(); symAux.emplace_back(); partitions.clear(); partitions.emplace_back(); config->progName = args[0]; elf::ctx.driver.linkerMain(args); return errorCount() == 0; } } // namespace elf } // namespace lld // Parses a linker -m option. static std::tuple parseEmulation(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}) .Default({ELFNoneKind, EM_NONE}); if (ret.first == ELFNoneKind) error("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( 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. 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 (!config->fatLTOObjects) return false; Expected fatLTOData = IRObjectFile::findBitcodeInMemBuffer(mb); if (errorToBool(fatLTOData.takeError())) return false; files.push_back( make(*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(path); if (!buffer) 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: { auto members = getArchiveMembers(mbref); if (inWholeArchive) { for (const std::pair &p : members) { if (isBitcode(p.first)) files.push_back(make(p.first, path, p.second, false)); else if (!tryAddFatLTOFile(p.first, path, p.second, false)) files.push_back(createObjFile(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. bool saved = InputFile::isInGroup; InputFile::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(p.first, path, true)); } else if (magic == file_magic::bitcode) files.push_back(make(p.first, path, p.second, true)); else warn(path + ": archive member '" + p.first.getBufferIdentifier() + "' is neither ET_REL nor LLVM bitcode"); } InputFile::isInGroup = saved; if (!saved) ++InputFile::nextGroupId; return; } case file_magic::elf_shared_object: { if (config->isStatic || config->relocatable) { error("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 = make(mbref, withLOption ? path::filename(path) : path); f->init(); files.push_back(f); return; } case file_magic::bitcode: files.push_back(make(mbref, "", 0, inLib)); break; case file_magic::elf_relocatable: if (!tryAddFatLTOFile(mbref, "", 0, inLib)) files.push_back(createObjFile(mbref, "", inLib)); break; default: error(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(name)) addFile(saver().save(*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->emachine == EM_ARM) { if (!config->cmseImplib) { if (!config->cmseInputLib.empty()) error("--in-implib may not be used without --cmse-implib"); if (!config->cmseOutputLib.empty()) error("--out-implib may not be used without --cmse-implib"); } } else { if (config->cmseImplib) error("--cmse-implib is only supported on ARM targets"); if (!config->cmseInputLib.empty()) error("--in-implib is only supported on ARM targets"); if (!config->cmseOutputLib.empty()) error("--out-implib is only supported on ARM targets"); } 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->armBe8 && config->emachine != EM_ARM) error("--be8 is only supported on ARM targets"); if (config->fixCortexA8 && !config->isLE) error("--fix-cortex-a8 is not supported on big endian 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->relaxGP && config->emachine != EM_RISCV) error("--relax-gp is only supported on RISC-V 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->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"); if (config->zBtiReport != "none") error("-z bti-report only supported on AArch64"); } if (config->emachine != EM_386 && config->emachine != EM_X86_64 && config->zCetReport != "none") error("-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(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 error("unknown -z start-stop-visibility= value: " + StringRef(kv.second)); } } return ret; } // Report a warning for an unknown -z option. static void checkZOptions(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("unknown -z value: " + StringRef(arg->getValue())); } constexpr const char *saveTempsValues[] = { "resolution", "preopt", "promote", "internalize", "import", "opt", "precodegen", "prelink", "combinedindex"}; void LinkerDriver::linkerMain(ArrayRef argsArr) { ELFOptTable parser; opt::InputArgList args = parser.parse(argsArr.slice(1)); // Interpret these flags early because error()/warn() depend on them. errorHandler().errorLimit = args::getInteger(args, OPT_error_limit, 20); errorHandler().fatalWarnings = args.hasFlag(OPT_fatal_warnings, OPT_no_fatal_warnings, false) && !args.hasArg(OPT_no_warnings); errorHandler().suppressWarnings = args.hasArg(OPT_no_warnings); // 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 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)) 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); checkZOptions(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; link(args); } if (config->timeTraceEnabled) { checkError(timeTraceProfilerWrite( args.getLastArgValue(OPT_time_trace_eq).str(), config->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(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; else break; arg->claim(); 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.starts_with("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 int getMemtagMode(opt::InputArgList &args) { StringRef memtagModeArg = args.getLastArgValue(OPT_android_memtag_mode); if (memtagModeArg.empty()) { if (config->androidMemtagStack) warn("--android-memtag-mode is unspecified, leaving " "--android-memtag-stack a no-op"); else if (config->androidMemtagHeap) warn("--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; error("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(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.starts_with("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=