1//===- Driver.cpp ---------------------------------------------------------===// 2// 3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 4// See https://llvm.org/LICENSE.txt for license information. 5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 6// 7//===----------------------------------------------------------------------===// 8// 9// The driver drives the entire linking process. It is responsible for 10// parsing command line options and doing whatever it is instructed to do. 11// 12// One notable thing in the LLD's driver when compared to other linkers is 13// that the LLD's driver is agnostic on the host operating system. 14// Other linkers usually have implicit default values (such as a dynamic 15// linker path or library paths) for each host OS. 16// 17// I don't think implicit default values are useful because they are 18// usually explicitly specified by the compiler ctx.driver. They can even 19// be harmful when you are doing cross-linking. Therefore, in LLD, we 20// simply trust the compiler driver to pass all required options and 21// don't try to make effort on our side. 22// 23//===----------------------------------------------------------------------===// 24 25#include "Driver.h" 26#include "Config.h" 27#include "ICF.h" 28#include "InputFiles.h" 29#include "InputSection.h" 30#include "LTO.h" 31#include "LinkerScript.h" 32#include "MarkLive.h" 33#include "OutputSections.h" 34#include "ScriptParser.h" 35#include "SymbolTable.h" 36#include "Symbols.h" 37#include "SyntheticSections.h" 38#include "Target.h" 39#include "Writer.h" 40#include "lld/Common/Args.h" 41#include "lld/Common/CommonLinkerContext.h" 42#include "lld/Common/Driver.h" 43#include "lld/Common/ErrorHandler.h" 44#include "lld/Common/Filesystem.h" 45#include "lld/Common/Memory.h" 46#include "lld/Common/Strings.h" 47#include "lld/Common/TargetOptionsCommandFlags.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/Config/llvm-config.h" 53#include "llvm/LTO/LTO.h" 54#include "llvm/Object/Archive.h" 55#include "llvm/Remarks/HotnessThresholdParser.h" 56#include "llvm/Support/CommandLine.h" 57#include "llvm/Support/Compression.h" 58#include "llvm/Support/FileSystem.h" 59#include "llvm/Support/GlobPattern.h" 60#include "llvm/Support/LEB128.h" 61#include "llvm/Support/Parallel.h" 62#include "llvm/Support/Path.h" 63#include "llvm/Support/TarWriter.h" 64#include "llvm/Support/TargetSelect.h" 65#include "llvm/Support/TimeProfiler.h" 66#include "llvm/Support/raw_ostream.h" 67#include <cstdlib> 68#include <utility> 69 70using namespace llvm; 71using namespace llvm::ELF; 72using namespace llvm::object; 73using namespace llvm::sys; 74using namespace llvm::support; 75using namespace lld; 76using namespace lld::elf; 77 78ConfigWrapper elf::config; 79Ctx elf::ctx; 80 81static void setConfigs(opt::InputArgList &args); 82static void readConfigs(opt::InputArgList &args); 83 84void elf::errorOrWarn(const Twine &msg) { 85 if (config->noinhibitExec) 86 warn(msg); 87 else 88 error(msg); 89} 90 91void Ctx::reset() { 92 driver = LinkerDriver(); 93 memoryBuffers.clear(); 94 objectFiles.clear(); 95 sharedFiles.clear(); 96 binaryFiles.clear(); 97 bitcodeFiles.clear(); 98 lazyBitcodeFiles.clear(); 99 inputSections.clear(); 100 ehInputSections.clear(); 101 duplicates.clear(); 102 nonPrevailingSyms.clear(); 103 whyExtractRecords.clear(); 104 backwardReferences.clear(); 105 hasSympart.store(false, std::memory_order_relaxed); 106 needsTlsLd.store(false, std::memory_order_relaxed); 107} 108 109bool elf::link(ArrayRef<const char *> args, llvm::raw_ostream &stdoutOS, 110 llvm::raw_ostream &stderrOS, bool exitEarly, 111 bool disableOutput) { 112 // This driver-specific context will be freed later by lldMain(). 113 auto *ctx = new CommonLinkerContext; 114 115 ctx->e.initialize(stdoutOS, stderrOS, exitEarly, disableOutput); 116 ctx->e.cleanupCallback = []() { 117 elf::ctx.reset(); 118 symtab = SymbolTable(); 119 120 outputSections.clear(); 121 symAux.clear(); 122 123 tar = nullptr; 124 in.reset(); 125 126 partitions.clear(); 127 partitions.emplace_back(); 128 129 SharedFile::vernauxNum = 0; 130 }; 131 ctx->e.logName = args::getFilenameWithoutExe(args[0]); 132 ctx->e.errorLimitExceededMsg = "too many errors emitted, stopping now (use " 133 "--error-limit=0 to see all errors)"; 134 135 config = ConfigWrapper(); 136 script = std::make_unique<LinkerScript>(); 137 138 symAux.emplace_back(); 139 140 partitions.clear(); 141 partitions.emplace_back(); 142 143 config->progName = args[0]; 144 145 elf::ctx.driver.linkerMain(args); 146 147 return errorCount() == 0; 148} 149 150// Parses a linker -m option. 151static std::tuple<ELFKind, uint16_t, uint8_t> parseEmulation(StringRef emul) { 152 uint8_t osabi = 0; 153 StringRef s = emul; 154 if (s.endswith("_fbsd")) { 155 s = s.drop_back(5); 156 osabi = ELFOSABI_FREEBSD; 157 } 158 159 std::pair<ELFKind, uint16_t> ret = 160 StringSwitch<std::pair<ELFKind, uint16_t>>(s) 161 .Cases("aarch64elf", "aarch64linux", {ELF64LEKind, EM_AARCH64}) 162 .Cases("aarch64elfb", "aarch64linuxb", {ELF64BEKind, EM_AARCH64}) 163 .Cases("armelf", "armelf_linux_eabi", {ELF32LEKind, EM_ARM}) 164 .Case("elf32_x86_64", {ELF32LEKind, EM_X86_64}) 165 .Cases("elf32btsmip", "elf32btsmipn32", {ELF32BEKind, EM_MIPS}) 166 .Cases("elf32ltsmip", "elf32ltsmipn32", {ELF32LEKind, EM_MIPS}) 167 .Case("elf32lriscv", {ELF32LEKind, EM_RISCV}) 168 .Cases("elf32ppc", "elf32ppclinux", {ELF32BEKind, EM_PPC}) 169 .Cases("elf32lppc", "elf32lppclinux", {ELF32LEKind, EM_PPC}) 170 .Case("elf64btsmip", {ELF64BEKind, EM_MIPS}) 171 .Case("elf64ltsmip", {ELF64LEKind, EM_MIPS}) 172 .Case("elf64lriscv", {ELF64LEKind, EM_RISCV}) 173 .Case("elf64ppc", {ELF64BEKind, EM_PPC64}) 174 .Case("elf64lppc", {ELF64LEKind, EM_PPC64}) 175 .Cases("elf_amd64", "elf_x86_64", {ELF64LEKind, EM_X86_64}) 176 .Case("elf_i386", {ELF32LEKind, EM_386}) 177 .Case("elf_iamcu", {ELF32LEKind, EM_IAMCU}) 178 .Case("elf64_sparc", {ELF64BEKind, EM_SPARCV9}) 179 .Case("msp430elf", {ELF32LEKind, EM_MSP430}) 180 .Case("elf64_amdgpu", {ELF64LEKind, EM_AMDGPU}) 181 .Default({ELFNoneKind, EM_NONE}); 182 183 if (ret.first == ELFNoneKind) 184 error("unknown emulation: " + emul); 185 if (ret.second == EM_MSP430) 186 osabi = ELFOSABI_STANDALONE; 187 else if (ret.second == EM_AMDGPU) 188 osabi = ELFOSABI_AMDGPU_HSA; 189 return std::make_tuple(ret.first, ret.second, osabi); 190} 191 192// Returns slices of MB by parsing MB as an archive file. 193// Each slice consists of a member file in the archive. 194std::vector<std::pair<MemoryBufferRef, uint64_t>> static getArchiveMembers( 195 MemoryBufferRef mb) { 196 std::unique_ptr<Archive> file = 197 CHECK(Archive::create(mb), 198 mb.getBufferIdentifier() + ": failed to parse archive"); 199 200 std::vector<std::pair<MemoryBufferRef, uint64_t>> v; 201 Error err = Error::success(); 202 bool addToTar = file->isThin() && tar; 203 for (const Archive::Child &c : file->children(err)) { 204 MemoryBufferRef mbref = 205 CHECK(c.getMemoryBufferRef(), 206 mb.getBufferIdentifier() + 207 ": could not get the buffer for a child of the archive"); 208 if (addToTar) 209 tar->append(relativeToRoot(check(c.getFullName())), mbref.getBuffer()); 210 v.push_back(std::make_pair(mbref, c.getChildOffset())); 211 } 212 if (err) 213 fatal(mb.getBufferIdentifier() + ": Archive::children failed: " + 214 toString(std::move(err))); 215 216 // Take ownership of memory buffers created for members of thin archives. 217 std::vector<std::unique_ptr<MemoryBuffer>> mbs = file->takeThinBuffers(); 218 std::move(mbs.begin(), mbs.end(), std::back_inserter(ctx.memoryBuffers)); 219 220 return v; 221} 222 223static bool isBitcode(MemoryBufferRef mb) { 224 return identify_magic(mb.getBuffer()) == llvm::file_magic::bitcode; 225} 226 227// Opens a file and create a file object. Path has to be resolved already. 228void LinkerDriver::addFile(StringRef path, bool withLOption) { 229 using namespace sys::fs; 230 231 std::optional<MemoryBufferRef> buffer = readFile(path); 232 if (!buffer) 233 return; 234 MemoryBufferRef mbref = *buffer; 235 236 if (config->formatBinary) { 237 files.push_back(make<BinaryFile>(mbref)); 238 return; 239 } 240 241 switch (identify_magic(mbref.getBuffer())) { 242 case file_magic::unknown: 243 readLinkerScript(mbref); 244 return; 245 case file_magic::archive: { 246 auto members = getArchiveMembers(mbref); 247 if (inWholeArchive) { 248 for (const std::pair<MemoryBufferRef, uint64_t> &p : members) { 249 if (isBitcode(p.first)) 250 files.push_back(make<BitcodeFile>(p.first, path, p.second, false)); 251 else 252 files.push_back(createObjFile(p.first, path)); 253 } 254 return; 255 } 256 257 archiveFiles.emplace_back(path, members.size()); 258 259 // Handle archives and --start-lib/--end-lib using the same code path. This 260 // scans all the ELF relocatable object files and bitcode files in the 261 // archive rather than just the index file, with the benefit that the 262 // symbols are only loaded once. For many projects archives see high 263 // utilization rates and it is a net performance win. --start-lib scans 264 // symbols in the same order that llvm-ar adds them to the index, so in the 265 // common case the semantics are identical. If the archive symbol table was 266 // created in a different order, or is incomplete, this strategy has 267 // different semantics. Such output differences are considered user error. 268 // 269 // All files within the archive get the same group ID to allow mutual 270 // references for --warn-backrefs. 271 bool saved = InputFile::isInGroup; 272 InputFile::isInGroup = true; 273 for (const std::pair<MemoryBufferRef, uint64_t> &p : members) { 274 auto magic = identify_magic(p.first.getBuffer()); 275 if (magic == file_magic::elf_relocatable) 276 files.push_back(createObjFile(p.first, path, true)); 277 else if (magic == file_magic::bitcode) 278 files.push_back(make<BitcodeFile>(p.first, path, p.second, true)); 279 else 280 warn(path + ": archive member '" + p.first.getBufferIdentifier() + 281 "' is neither ET_REL nor LLVM bitcode"); 282 } 283 InputFile::isInGroup = saved; 284 if (!saved) 285 ++InputFile::nextGroupId; 286 return; 287 } 288 case file_magic::elf_shared_object: { 289 if (config->isStatic || config->relocatable) { 290 error("attempted static link of dynamic object " + path); 291 return; 292 } 293 294 // Shared objects are identified by soname. soname is (if specified) 295 // DT_SONAME and falls back to filename. If a file was specified by -lfoo, 296 // the directory part is ignored. Note that path may be a temporary and 297 // cannot be stored into SharedFile::soName. 298 path = mbref.getBufferIdentifier(); 299 auto *f = 300 make<SharedFile>(mbref, withLOption ? path::filename(path) : path); 301 f->init(); 302 files.push_back(f); 303 return; 304 } 305 case file_magic::bitcode: 306 files.push_back(make<BitcodeFile>(mbref, "", 0, inLib)); 307 break; 308 case file_magic::elf_relocatable: 309 files.push_back(createObjFile(mbref, "", inLib)); 310 break; 311 default: 312 error(path + ": unknown file type"); 313 } 314} 315 316// Add a given library by searching it from input search paths. 317void LinkerDriver::addLibrary(StringRef name) { 318 if (std::optional<std::string> path = searchLibrary(name)) 319 addFile(saver().save(*path), /*withLOption=*/true); 320 else 321 error("unable to find library -l" + name, ErrorTag::LibNotFound, {name}); 322} 323 324// This function is called on startup. We need this for LTO since 325// LTO calls LLVM functions to compile bitcode files to native code. 326// Technically this can be delayed until we read bitcode files, but 327// we don't bother to do lazily because the initialization is fast. 328static void initLLVM() { 329 InitializeAllTargets(); 330 InitializeAllTargetMCs(); 331 InitializeAllAsmPrinters(); 332 InitializeAllAsmParsers(); 333} 334 335// Some command line options or some combinations of them are not allowed. 336// This function checks for such errors. 337static void checkOptions() { 338 // The MIPS ABI as of 2016 does not support the GNU-style symbol lookup 339 // table which is a relatively new feature. 340 if (config->emachine == EM_MIPS && config->gnuHash) 341 error("the .gnu.hash section is not compatible with the MIPS target"); 342 343 if (config->fixCortexA53Errata843419 && config->emachine != EM_AARCH64) 344 error("--fix-cortex-a53-843419 is only supported on AArch64 targets"); 345 346 if (config->fixCortexA8 && config->emachine != EM_ARM) 347 error("--fix-cortex-a8 is only supported on ARM targets"); 348 349 if (config->tocOptimize && config->emachine != EM_PPC64) 350 error("--toc-optimize is only supported on PowerPC64 targets"); 351 352 if (config->pcRelOptimize && config->emachine != EM_PPC64) 353 error("--pcrel-optimize is only supported on PowerPC64 targets"); 354 355 if (config->pie && config->shared) 356 error("-shared and -pie may not be used together"); 357 358 if (!config->shared && !config->filterList.empty()) 359 error("-F may not be used without -shared"); 360 361 if (!config->shared && !config->auxiliaryList.empty()) 362 error("-f may not be used without -shared"); 363 364 if (config->strip == StripPolicy::All && config->emitRelocs) 365 error("--strip-all and --emit-relocs may not be used together"); 366 367 if (config->zText && config->zIfuncNoplt) 368 error("-z text and -z ifunc-noplt may not be used together"); 369 370 if (config->relocatable) { 371 if (config->shared) 372 error("-r and -shared may not be used together"); 373 if (config->gdbIndex) 374 error("-r and --gdb-index may not be used together"); 375 if (config->icf != ICFLevel::None) 376 error("-r and --icf may not be used together"); 377 if (config->pie) 378 error("-r and -pie may not be used together"); 379 if (config->exportDynamic) 380 error("-r and --export-dynamic may not be used together"); 381 } 382 383 if (config->executeOnly) { 384 switch (config->emachine) { 385 case EM_386: 386 case EM_AARCH64: 387 case EM_MIPS: 388 case EM_PPC: 389 case EM_PPC64: 390 case EM_RISCV: 391 case EM_SPARCV9: 392 case EM_X86_64: 393 break; 394 default: 395 error("-execute-only is not supported on this target"); 396 } 397 398 if (config->singleRoRx && !script->hasSectionsCommand) 399 error("--execute-only and --no-rosegment cannot be used together"); 400 } 401 402 if (config->zRetpolineplt && config->zForceIbt) 403 error("-z force-ibt may not be used with -z retpolineplt"); 404 405 if (config->emachine != EM_AARCH64) { 406 if (config->zPacPlt) 407 error("-z pac-plt only supported on AArch64"); 408 if (config->zForceBti) 409 error("-z force-bti only supported on AArch64"); 410 if (config->zBtiReport != "none") 411 error("-z bti-report only supported on AArch64"); 412 } 413 414 if (config->emachine != EM_386 && config->emachine != EM_X86_64 && 415 config->zCetReport != "none") 416 error("-z cet-report only supported on X86 and X86_64"); 417} 418 419static const char *getReproduceOption(opt::InputArgList &args) { 420 if (auto *arg = args.getLastArg(OPT_reproduce)) 421 return arg->getValue(); 422 return getenv("LLD_REPRODUCE"); 423} 424 425static bool hasZOption(opt::InputArgList &args, StringRef key) { 426 for (auto *arg : args.filtered(OPT_z)) 427 if (key == arg->getValue()) 428 return true; 429 return false; 430} 431 432static bool getZFlag(opt::InputArgList &args, StringRef k1, StringRef k2, 433 bool Default) { 434 for (auto *arg : args.filtered_reverse(OPT_z)) { 435 if (k1 == arg->getValue()) 436 return true; 437 if (k2 == arg->getValue()) 438 return false; 439 } 440 return Default; 441} 442 443static SeparateSegmentKind getZSeparate(opt::InputArgList &args) { 444 for (auto *arg : args.filtered_reverse(OPT_z)) { 445 StringRef v = arg->getValue(); 446 if (v == "noseparate-code") 447 return SeparateSegmentKind::None; 448 if (v == "separate-code") 449 return SeparateSegmentKind::Code; 450 if (v == "separate-loadable-segments") 451 return SeparateSegmentKind::Loadable; 452 } 453 return SeparateSegmentKind::None; 454} 455 456static GnuStackKind getZGnuStack(opt::InputArgList &args) { 457 for (auto *arg : args.filtered_reverse(OPT_z)) { 458 if (StringRef("execstack") == arg->getValue()) 459 return GnuStackKind::Exec; 460 if (StringRef("noexecstack") == arg->getValue()) 461 return GnuStackKind::NoExec; 462 if (StringRef("nognustack") == arg->getValue()) 463 return GnuStackKind::None; 464 } 465 466 return GnuStackKind::NoExec; 467} 468 469static uint8_t getZStartStopVisibility(opt::InputArgList &args) { 470 for (auto *arg : args.filtered_reverse(OPT_z)) { 471 std::pair<StringRef, StringRef> kv = StringRef(arg->getValue()).split('='); 472 if (kv.first == "start-stop-visibility") { 473 if (kv.second == "default") 474 return STV_DEFAULT; 475 else if (kv.second == "internal") 476 return STV_INTERNAL; 477 else if (kv.second == "hidden") 478 return STV_HIDDEN; 479 else if (kv.second == "protected") 480 return STV_PROTECTED; 481 error("unknown -z start-stop-visibility= value: " + StringRef(kv.second)); 482 } 483 } 484 return STV_PROTECTED; 485} 486 487constexpr const char *knownZFlags[] = { 488 "combreloc", 489 "copyreloc", 490 "defs", 491 "execstack", 492 "force-bti", 493 "force-ibt", 494 "global", 495 "hazardplt", 496 "ifunc-noplt", 497 "initfirst", 498 "interpose", 499 "keep-text-section-prefix", 500 "lazy", 501 "muldefs", 502 "nobtcfi", 503 "nocombreloc", 504 "nocopyreloc", 505 "nodefaultlib", 506 "nodelete", 507 "nodlopen", 508 "noexecstack", 509 "nognustack", 510 "nokeep-text-section-prefix", 511 "nopack-relative-relocs", 512 "norelro", 513 "noseparate-code", 514 "nostart-stop-gc", 515 "notext", 516 "now", 517 "origin", 518 "pac-plt", 519 "pack-relative-relocs", 520 "rel", 521 "rela", 522 "relro", 523 "retpolineplt", 524 "rodynamic", 525 "separate-code", 526 "separate-loadable-segments", 527 "shstk", 528 "start-stop-gc", 529 "text", 530 "undefs", 531 "wxneeded", 532}; 533 534static bool isKnownZFlag(StringRef s) { 535 return llvm::is_contained(knownZFlags, s) || 536 s.startswith("common-page-size=") || s.startswith("bti-report=") || 537 s.startswith("cet-report=") || 538 s.startswith("dead-reloc-in-nonalloc=") || 539 s.startswith("max-page-size=") || s.startswith("stack-size=") || 540 s.startswith("start-stop-visibility="); 541} 542 543// Report a warning for an unknown -z option. 544static void checkZOptions(opt::InputArgList &args) { 545 for (auto *arg : args.filtered(OPT_z)) 546 if (!isKnownZFlag(arg->getValue())) 547 warn("unknown -z value: " + StringRef(arg->getValue())); 548} 549 550constexpr const char *saveTempsValues[] = { 551 "resolution", "preopt", "promote", "internalize", "import", 552 "opt", "precodegen", "prelink", "combinedindex"}; 553 554void LinkerDriver::linkerMain(ArrayRef<const char *> argsArr) { 555 ELFOptTable parser; 556 opt::InputArgList args = parser.parse(argsArr.slice(1)); 557 558 // Interpret these flags early because error()/warn() depend on them. 559 errorHandler().errorLimit = args::getInteger(args, OPT_error_limit, 20); 560 errorHandler().fatalWarnings = 561 args.hasFlag(OPT_fatal_warnings, OPT_no_fatal_warnings, false) && 562 !args.hasArg(OPT_no_warnings); 563 errorHandler().suppressWarnings = args.hasArg(OPT_no_warnings); 564 checkZOptions(args); 565 566 // Handle -help 567 if (args.hasArg(OPT_help)) { 568 printHelp(); 569 return; 570 } 571 572 // Handle -v or -version. 573 // 574 // A note about "compatible with GNU linkers" message: this is a hack for 575 // scripts generated by GNU Libtool up to 2021-10 to recognize LLD as 576 // a GNU compatible linker. See 577 // <https://lists.gnu.org/archive/html/libtool/2017-01/msg00007.html>. 578 // 579 // This is somewhat ugly hack, but in reality, we had no choice other 580 // than doing this. Considering the very long release cycle of Libtool, 581 // it is not easy to improve it to recognize LLD as a GNU compatible 582 // linker in a timely manner. Even if we can make it, there are still a 583 // lot of "configure" scripts out there that are generated by old version 584 // of Libtool. We cannot convince every software developer to migrate to 585 // the latest version and re-generate scripts. So we have this hack. 586 if (args.hasArg(OPT_v) || args.hasArg(OPT_version)) 587 message(getLLDVersion() + " (compatible with GNU linkers)"); 588 589 if (const char *path = getReproduceOption(args)) { 590 // Note that --reproduce is a debug option so you can ignore it 591 // if you are trying to understand the whole picture of the code. 592 Expected<std::unique_ptr<TarWriter>> errOrWriter = 593 TarWriter::create(path, path::stem(path)); 594 if (errOrWriter) { 595 tar = std::move(*errOrWriter); 596 tar->append("response.txt", createResponseFile(args)); 597 tar->append("version.txt", getLLDVersion() + "\n"); 598 StringRef ltoSampleProfile = args.getLastArgValue(OPT_lto_sample_profile); 599 if (!ltoSampleProfile.empty()) 600 readFile(ltoSampleProfile); 601 } else { 602 error("--reproduce: " + toString(errOrWriter.takeError())); 603 } 604 } 605 606 readConfigs(args); 607 608 // The behavior of -v or --version is a bit strange, but this is 609 // needed for compatibility with GNU linkers. 610 if (args.hasArg(OPT_v) && !args.hasArg(OPT_INPUT)) 611 return; 612 if (args.hasArg(OPT_version)) 613 return; 614 615 // Initialize time trace profiler. 616 if (config->timeTraceEnabled) 617 timeTraceProfilerInitialize(config->timeTraceGranularity, config->progName); 618 619 { 620 llvm::TimeTraceScope timeScope("ExecuteLinker"); 621 622 initLLVM(); 623 createFiles(args); 624 if (errorCount()) 625 return; 626 627 inferMachineType(); 628 setConfigs(args); 629 checkOptions(); 630 if (errorCount()) 631 return; 632 633 link(args); 634 } 635 636 if (config->timeTraceEnabled) { 637 checkError(timeTraceProfilerWrite( 638 args.getLastArgValue(OPT_time_trace_eq).str(), config->outputFile)); 639 timeTraceProfilerCleanup(); 640 } 641} 642 643static std::string getRpath(opt::InputArgList &args) { 644 SmallVector<StringRef, 0> v = args::getStrings(args, OPT_rpath); 645 return llvm::join(v.begin(), v.end(), ":"); 646} 647 648// Determines what we should do if there are remaining unresolved 649// symbols after the name resolution. 650static void setUnresolvedSymbolPolicy(opt::InputArgList &args) { 651 UnresolvedPolicy errorOrWarn = args.hasFlag(OPT_error_unresolved_symbols, 652 OPT_warn_unresolved_symbols, true) 653 ? UnresolvedPolicy::ReportError 654 : UnresolvedPolicy::Warn; 655 // -shared implies --unresolved-symbols=ignore-all because missing 656 // symbols are likely to be resolved at runtime. 657 bool diagRegular = !config->shared, diagShlib = !config->shared; 658 659 for (const opt::Arg *arg : args) { 660 switch (arg->getOption().getID()) { 661 case OPT_unresolved_symbols: { 662 StringRef s = arg->getValue(); 663 if (s == "ignore-all") { 664 diagRegular = false; 665 diagShlib = false; 666 } else if (s == "ignore-in-object-files") { 667 diagRegular = false; 668 diagShlib = true; 669 } else if (s == "ignore-in-shared-libs") { 670 diagRegular = true; 671 diagShlib = false; 672 } else if (s == "report-all") { 673 diagRegular = true; 674 diagShlib = true; 675 } else { 676 error("unknown --unresolved-symbols value: " + s); 677 } 678 break; 679 } 680 case OPT_no_undefined: 681 diagRegular = true; 682 break; 683 case OPT_z: 684 if (StringRef(arg->getValue()) == "defs") 685 diagRegular = true; 686 else if (StringRef(arg->getValue()) == "undefs") 687 diagRegular = false; 688 break; 689 case OPT_allow_shlib_undefined: 690 diagShlib = false; 691 break; 692 case OPT_no_allow_shlib_undefined: 693 diagShlib = true; 694 break; 695 } 696 } 697 698 config->unresolvedSymbols = 699 diagRegular ? errorOrWarn : UnresolvedPolicy::Ignore; 700 config->unresolvedSymbolsInShlib = 701 diagShlib ? errorOrWarn : UnresolvedPolicy::Ignore; 702} 703 704static Target2Policy getTarget2(opt::InputArgList &args) { 705 StringRef s = args.getLastArgValue(OPT_target2, "got-rel"); 706 if (s == "rel") 707 return Target2Policy::Rel; 708 if (s == "abs") 709 return Target2Policy::Abs; 710 if (s == "got-rel") 711 return Target2Policy::GotRel; 712 error("unknown --target2 option: " + s); 713 return Target2Policy::GotRel; 714} 715 716static bool isOutputFormatBinary(opt::InputArgList &args) { 717 StringRef s = args.getLastArgValue(OPT_oformat, "elf"); 718 if (s == "binary") 719 return true; 720 if (!s.startswith("elf")) 721 error("unknown --oformat value: " + s); 722 return false; 723} 724 725static DiscardPolicy getDiscard(opt::InputArgList &args) { 726 auto *arg = 727 args.getLastArg(OPT_discard_all, OPT_discard_locals, OPT_discard_none); 728 if (!arg) 729 return DiscardPolicy::Default; 730 if (arg->getOption().getID() == OPT_discard_all) 731 return DiscardPolicy::All; 732 if (arg->getOption().getID() == OPT_discard_locals) 733 return DiscardPolicy::Locals; 734 return DiscardPolicy::None; 735} 736 737static StringRef getDynamicLinker(opt::InputArgList &args) { 738 auto *arg = args.getLastArg(OPT_dynamic_linker, OPT_no_dynamic_linker); 739 if (!arg) 740 return ""; 741 if (arg->getOption().getID() == OPT_no_dynamic_linker) { 742 // --no-dynamic-linker suppresses undefined weak symbols in .dynsym 743 config->noDynamicLinker = true; 744 return ""; 745 } 746 return arg->getValue(); 747} 748 749static int getMemtagMode(opt::InputArgList &args) { 750 StringRef memtagModeArg = args.getLastArgValue(OPT_android_memtag_mode); 751 if (!config->androidMemtagHeap && !config->androidMemtagStack) { 752 if (!memtagModeArg.empty()) 753 error("when using --android-memtag-mode, at least one of " 754 "--android-memtag-heap or " 755 "--android-memtag-stack is required"); 756 return ELF::NT_MEMTAG_LEVEL_NONE; 757 } 758 759 if (memtagModeArg == "sync" || memtagModeArg.empty()) 760 return ELF::NT_MEMTAG_LEVEL_SYNC; 761 if (memtagModeArg == "async") 762 return ELF::NT_MEMTAG_LEVEL_ASYNC; 763 if (memtagModeArg == "none") 764 return ELF::NT_MEMTAG_LEVEL_NONE; 765 766 error("unknown --android-memtag-mode value: \"" + memtagModeArg + 767 "\", should be one of {async, sync, none}"); 768 return ELF::NT_MEMTAG_LEVEL_NONE; 769} 770 771static ICFLevel getICF(opt::InputArgList &args) { 772 auto *arg = args.getLastArg(OPT_icf_none, OPT_icf_safe, OPT_icf_all); 773 if (!arg || arg->getOption().getID() == OPT_icf_none) 774 return ICFLevel::None; 775 if (arg->getOption().getID() == OPT_icf_safe) 776 return ICFLevel::Safe; 777 return ICFLevel::All; 778} 779 780static StripPolicy getStrip(opt::InputArgList &args) { 781 if (args.hasArg(OPT_relocatable)) 782 return StripPolicy::None; 783 784 auto *arg = args.getLastArg(OPT_strip_all, OPT_strip_debug); 785 if (!arg) 786 return StripPolicy::None; 787 if (arg->getOption().getID() == OPT_strip_all) 788 return StripPolicy::All; 789 return StripPolicy::Debug; 790} 791 792static uint64_t parseSectionAddress(StringRef s, opt::InputArgList &args, 793 const opt::Arg &arg) { 794 uint64_t va = 0; 795 if (s.startswith("0x")) 796 s = s.drop_front(2); 797 if (!to_integer(s, va, 16)) 798 error("invalid argument: " + arg.getAsString(args)); 799 return va; 800} 801 802static StringMap<uint64_t> getSectionStartMap(opt::InputArgList &args) { 803 StringMap<uint64_t> ret; 804 for (auto *arg : args.filtered(OPT_section_start)) { 805 StringRef name; 806 StringRef addr; 807 std::tie(name, addr) = StringRef(arg->getValue()).split('='); 808 ret[name] = parseSectionAddress(addr, args, *arg); 809 } 810 811 if (auto *arg = args.getLastArg(OPT_Ttext)) 812 ret[".text"] = parseSectionAddress(arg->getValue(), args, *arg); 813 if (auto *arg = args.getLastArg(OPT_Tdata)) 814 ret[".data"] = parseSectionAddress(arg->getValue(), args, *arg); 815 if (auto *arg = args.getLastArg(OPT_Tbss)) 816 ret[".bss"] = parseSectionAddress(arg->getValue(), args, *arg); 817 return ret; 818} 819 820static SortSectionPolicy getSortSection(opt::InputArgList &args) { 821 StringRef s = args.getLastArgValue(OPT_sort_section); 822 if (s == "alignment") 823 return SortSectionPolicy::Alignment; 824 if (s == "name") 825 return SortSectionPolicy::Name; 826 if (!s.empty()) 827 error("unknown --sort-section rule: " + s); 828 return SortSectionPolicy::Default; 829} 830 831static OrphanHandlingPolicy getOrphanHandling(opt::InputArgList &args) { 832 StringRef s = args.getLastArgValue(OPT_orphan_handling, "place"); 833 if (s == "warn") 834 return OrphanHandlingPolicy::Warn; 835 if (s == "error") 836 return OrphanHandlingPolicy::Error; 837 if (s != "place") 838 error("unknown --orphan-handling mode: " + s); 839 return OrphanHandlingPolicy::Place; 840} 841 842// Parse --build-id or --build-id=<style>. We handle "tree" as a 843// synonym for "sha1" because all our hash functions including 844// --build-id=sha1 are actually tree hashes for performance reasons. 845static std::pair<BuildIdKind, SmallVector<uint8_t, 0>> 846getBuildId(opt::InputArgList &args) { 847 auto *arg = args.getLastArg(OPT_build_id); 848 if (!arg) 849 return {BuildIdKind::None, {}}; 850 851 StringRef s = arg->getValue(); 852 if (s == "fast") 853 return {BuildIdKind::Fast, {}}; 854 if (s == "md5") 855 return {BuildIdKind::Md5, {}}; 856 if (s == "sha1" || s == "tree") 857 return {BuildIdKind::Sha1, {}}; 858 if (s == "uuid") 859 return {BuildIdKind::Uuid, {}}; 860 if (s.startswith("0x")) 861 return {BuildIdKind::Hexstring, parseHex(s.substr(2))}; 862 863 if (s != "none") 864 error("unknown --build-id style: " + s); 865 return {BuildIdKind::None, {}}; 866} 867 868static std::pair<bool, bool> getPackDynRelocs(opt::InputArgList &args) { 869 StringRef s = args.getLastArgValue(OPT_pack_dyn_relocs, "none"); 870 if (s == "android") 871 return {true, false}; 872 if (s == "relr") 873 return {false, true}; 874 if (s == "android+relr") 875 return {true, true}; 876 877 if (s != "none") 878 error("unknown --pack-dyn-relocs format: " + s); 879 return {false, false}; 880} 881 882static void readCallGraph(MemoryBufferRef mb) { 883 // Build a map from symbol name to section 884 DenseMap<StringRef, Symbol *> map; 885 for (ELFFileBase *file : ctx.objectFiles) 886 for (Symbol *sym : file->getSymbols()) 887 map[sym->getName()] = sym; 888 889 auto findSection = [&](StringRef name) -> InputSectionBase * { 890 Symbol *sym = map.lookup(name); 891 if (!sym) { 892 if (config->warnSymbolOrdering) 893 warn(mb.getBufferIdentifier() + ": no such symbol: " + name); 894 return nullptr; 895 } 896 maybeWarnUnorderableSymbol(sym); 897 898 if (Defined *dr = dyn_cast_or_null<Defined>(sym)) 899 return dyn_cast_or_null<InputSectionBase>(dr->section); 900 return nullptr; 901 }; 902 903 for (StringRef line : args::getLines(mb)) { 904 SmallVector<StringRef, 3> fields; 905 line.split(fields, ' '); 906 uint64_t count; 907 908 if (fields.size() != 3 || !to_integer(fields[2], count)) { 909 error(mb.getBufferIdentifier() + ": parse error"); 910 return; 911 } 912 913 if (InputSectionBase *from = findSection(fields[0])) 914 if (InputSectionBase *to = findSection(fields[1])) 915 config->callGraphProfile[std::make_pair(from, to)] += count; 916 } 917} 918 919// If SHT_LLVM_CALL_GRAPH_PROFILE and its relocation section exist, returns 920// true and populates cgProfile and symbolIndices. 921template <class ELFT> 922static bool 923processCallGraphRelocations(SmallVector<uint32_t, 32> &symbolIndices, 924 ArrayRef<typename ELFT::CGProfile> &cgProfile, 925 ObjFile<ELFT> *inputObj) { 926 if (inputObj->cgProfileSectionIndex == SHN_UNDEF) 927 return false; 928 929 ArrayRef<Elf_Shdr_Impl<ELFT>> objSections = 930 inputObj->template getELFShdrs<ELFT>(); 931 symbolIndices.clear(); 932 const ELFFile<ELFT> &obj = inputObj->getObj(); 933 cgProfile = 934 check(obj.template getSectionContentsAsArray<typename ELFT::CGProfile>( 935 objSections[inputObj->cgProfileSectionIndex])); 936 937 for (size_t i = 0, e = objSections.size(); i < e; ++i) { 938 const Elf_Shdr_Impl<ELFT> &sec = objSections[i]; 939 if (sec.sh_info == inputObj->cgProfileSectionIndex) { 940 if (sec.sh_type == SHT_RELA) { 941 ArrayRef<typename ELFT::Rela> relas = 942 CHECK(obj.relas(sec), "could not retrieve cg profile rela section"); 943 for (const typename ELFT::Rela &rel : relas) 944 symbolIndices.push_back(rel.getSymbol(config->isMips64EL)); 945 break; 946 } 947 if (sec.sh_type == SHT_REL) { 948 ArrayRef<typename ELFT::Rel> rels = 949 CHECK(obj.rels(sec), "could not retrieve cg profile rel section"); 950 for (const typename ELFT::Rel &rel : rels) 951 symbolIndices.push_back(rel.getSymbol(config->isMips64EL)); 952 break; 953 } 954 } 955 } 956 if (symbolIndices.empty()) 957 warn("SHT_LLVM_CALL_GRAPH_PROFILE exists, but relocation section doesn't"); 958 return !symbolIndices.empty(); 959} 960 961template <class ELFT> static void readCallGraphsFromObjectFiles() { 962 SmallVector<uint32_t, 32> symbolIndices; 963 ArrayRef<typename ELFT::CGProfile> cgProfile; 964 for (auto file : ctx.objectFiles) { 965 auto *obj = cast<ObjFile<ELFT>>(file); 966 if (!processCallGraphRelocations(symbolIndices, cgProfile, obj)) 967 continue; 968 969 if (symbolIndices.size() != cgProfile.size() * 2) 970 fatal("number of relocations doesn't match Weights"); 971 972 for (uint32_t i = 0, size = cgProfile.size(); i < size; ++i) { 973 const Elf_CGProfile_Impl<ELFT> &cgpe = cgProfile[i]; 974 uint32_t fromIndex = symbolIndices[i * 2]; 975 uint32_t toIndex = symbolIndices[i * 2 + 1]; 976 auto *fromSym = dyn_cast<Defined>(&obj->getSymbol(fromIndex)); 977 auto *toSym = dyn_cast<Defined>(&obj->getSymbol(toIndex)); 978 if (!fromSym || !toSym) 979 continue; 980 981 auto *from = dyn_cast_or_null<InputSectionBase>(fromSym->section); 982 auto *to = dyn_cast_or_null<InputSectionBase>(toSym->section); 983 if (from && to) 984 config->callGraphProfile[{from, to}] += cgpe.cgp_weight; 985 } 986 } 987} 988 989static DebugCompressionType getCompressDebugSections(opt::InputArgList &args) { 990 StringRef s = args.getLastArgValue(OPT_compress_debug_sections, "none"); 991 if (s == "zlib") { 992 if (!compression::zlib::isAvailable()) 993 error("--compress-debug-sections: zlib is not available"); 994 return DebugCompressionType::Zlib; 995 } 996 if (s == "zstd") { 997 if (!compression::zstd::isAvailable()) 998 error("--compress-debug-sections: zstd is not available"); 999 return DebugCompressionType::Zstd; 1000 } 1001 if (s != "none") 1002 error("unknown --compress-debug-sections value: " + s); 1003 return DebugCompressionType::None; 1004} 1005 1006static StringRef getAliasSpelling(opt::Arg *arg) { 1007 if (const opt::Arg *alias = arg->getAlias()) 1008 return alias->getSpelling(); 1009 return arg->getSpelling(); 1010} 1011 1012static std::pair<StringRef, StringRef> getOldNewOptions(opt::InputArgList &args, 1013 unsigned id) { 1014 auto *arg = args.getLastArg(id); 1015 if (!arg) 1016 return {"", ""}; 1017 1018 StringRef s = arg->getValue(); 1019 std::pair<StringRef, StringRef> ret = s.split(';'); 1020 if (ret.second.empty()) 1021 error(getAliasSpelling(arg) + " expects 'old;new' format, but got " + s); 1022 return ret; 1023} 1024 1025// Parse the symbol ordering file and warn for any duplicate entries. 1026static SmallVector<StringRef, 0> getSymbolOrderingFile(MemoryBufferRef mb) { 1027 SetVector<StringRef, SmallVector<StringRef, 0>> names; 1028 for (StringRef s : args::getLines(mb)) 1029 if (!names.insert(s) && config->warnSymbolOrdering) 1030 warn(mb.getBufferIdentifier() + ": duplicate ordered symbol: " + s); 1031 1032 return names.takeVector(); 1033} 1034 1035static bool getIsRela(opt::InputArgList &args) { 1036 // If -z rel or -z rela is specified, use the last option. 1037 for (auto *arg : args.filtered_reverse(OPT_z)) { 1038 StringRef s(arg->getValue()); 1039 if (s == "rel") 1040 return false; 1041 if (s == "rela") 1042 return true; 1043 } 1044 1045 // Otherwise use the psABI defined relocation entry format. 1046 uint16_t m = config->emachine; 1047 return m == EM_AARCH64 || m == EM_AMDGPU || m == EM_HEXAGON || m == EM_PPC || 1048 m == EM_PPC64 || m == EM_RISCV || m == EM_X86_64; 1049} 1050 1051static void parseClangOption(StringRef opt, const Twine &msg) { 1052 std::string err; 1053 raw_string_ostream os(err); 1054 1055 const char *argv[] = {config->progName.data(), opt.data()}; 1056 if (cl::ParseCommandLineOptions(2, argv, "", &os)) 1057 return; 1058 os.flush(); 1059 error(msg + ": " + StringRef(err).trim()); 1060} 1061 1062// Checks the parameter of the bti-report and cet-report options. 1063static bool isValidReportString(StringRef arg) { 1064 return arg == "none" || arg == "warning" || arg == "error"; 1065} 1066 1067// Initializes Config members by the command line options. 1068static void readConfigs(opt::InputArgList &args) { 1069 errorHandler().verbose = args.hasArg(OPT_verbose); 1070 errorHandler().vsDiagnostics = 1071 args.hasArg(OPT_visual_studio_diagnostics_format, false); 1072 1073 config->allowMultipleDefinition = 1074 args.hasFlag(OPT_allow_multiple_definition, 1075 OPT_no_allow_multiple_definition, false) || 1076 hasZOption(args, "muldefs"); 1077 config->androidMemtagHeap = 1078 args.hasFlag(OPT_android_memtag_heap, OPT_no_android_memtag_heap, false); 1079 config->androidMemtagStack = args.hasFlag(OPT_android_memtag_stack, 1080 OPT_no_android_memtag_stack, false); 1081 config->androidMemtagMode = getMemtagMode(args); 1082 config->auxiliaryList = args::getStrings(args, OPT_auxiliary); 1083 if (opt::Arg *arg = 1084 args.getLastArg(OPT_Bno_symbolic, OPT_Bsymbolic_non_weak_functions, 1085 OPT_Bsymbolic_functions, OPT_Bsymbolic)) { 1086 if (arg->getOption().matches(OPT_Bsymbolic_non_weak_functions)) 1087 config->bsymbolic = BsymbolicKind::NonWeakFunctions; 1088 else if (arg->getOption().matches(OPT_Bsymbolic_functions)) 1089 config->bsymbolic = BsymbolicKind::Functions; 1090 else if (arg->getOption().matches(OPT_Bsymbolic)) 1091 config->bsymbolic = BsymbolicKind::All; 1092 } 1093 config->checkSections = 1094 args.hasFlag(OPT_check_sections, OPT_no_check_sections, true); 1095 config->chroot = args.getLastArgValue(OPT_chroot); 1096 config->compressDebugSections = getCompressDebugSections(args); 1097 config->cref = args.hasArg(OPT_cref); 1098 config->optimizeBBJumps = 1099 args.hasFlag(OPT_optimize_bb_jumps, OPT_no_optimize_bb_jumps, false); 1100 config->demangle = args.hasFlag(OPT_demangle, OPT_no_demangle, true); 1101 config->dependencyFile = args.getLastArgValue(OPT_dependency_file); 1102 config->dependentLibraries = args.hasFlag(OPT_dependent_libraries, OPT_no_dependent_libraries, true); 1103 config->disableVerify = args.hasArg(OPT_disable_verify); 1104 config->discard = getDiscard(args); 1105 config->dwoDir = args.getLastArgValue(OPT_plugin_opt_dwo_dir_eq); 1106 config->dynamicLinker = getDynamicLinker(args); 1107 config->ehFrameHdr = 1108 args.hasFlag(OPT_eh_frame_hdr, OPT_no_eh_frame_hdr, false); 1109 config->emitLLVM = args.hasArg(OPT_plugin_opt_emit_llvm, false); 1110 config->emitRelocs = args.hasArg(OPT_emit_relocs); 1111 config->callGraphProfileSort = args.hasFlag( 1112 OPT_call_graph_profile_sort, OPT_no_call_graph_profile_sort, true); 1113 config->enableNewDtags = 1114 args.hasFlag(OPT_enable_new_dtags, OPT_disable_new_dtags, true); 1115 config->entry = args.getLastArgValue(OPT_entry); 1116 1117 errorHandler().errorHandlingScript = 1118 args.getLastArgValue(OPT_error_handling_script); 1119 1120 config->exportDynamic = 1121 args.hasFlag(OPT_export_dynamic, OPT_no_export_dynamic, false) || 1122 args.hasArg(OPT_shared); 1123 config->filterList = args::getStrings(args, OPT_filter); 1124 config->fini = args.getLastArgValue(OPT_fini, "_fini"); 1125 config->fixCortexA53Errata843419 = args.hasArg(OPT_fix_cortex_a53_843419) && 1126 !args.hasArg(OPT_relocatable); 1127 config->fixCortexA8 = 1128 args.hasArg(OPT_fix_cortex_a8) && !args.hasArg(OPT_relocatable); 1129 config->fortranCommon = 1130 args.hasFlag(OPT_fortran_common, OPT_no_fortran_common, false); 1131 config->gcSections = args.hasFlag(OPT_gc_sections, OPT_no_gc_sections, false); 1132 config->gnuUnique = args.hasFlag(OPT_gnu_unique, OPT_no_gnu_unique, true); 1133 config->gdbIndex = args.hasFlag(OPT_gdb_index, OPT_no_gdb_index, false); 1134 config->icf = getICF(args); 1135 config->ignoreDataAddressEquality = 1136 args.hasArg(OPT_ignore_data_address_equality); 1137#if defined(__OpenBSD__) 1138 // Needed to allow preemption of protected symbols (e.g. memcpy) on at least i386. 1139 config->ignoreFunctionAddressEquality = 1140 args.hasFlag(OPT_ignore_function_address_equality, 1141 OPT_no_ignore_function_address_equality, true); 1142#else 1143 config->ignoreFunctionAddressEquality = 1144 args.hasArg(OPT_ignore_function_address_equality); 1145#endif 1146 config->init = args.getLastArgValue(OPT_init, "_init"); 1147 config->ltoAAPipeline = args.getLastArgValue(OPT_lto_aa_pipeline); 1148 config->ltoCSProfileGenerate = args.hasArg(OPT_lto_cs_profile_generate); 1149 config->ltoCSProfileFile = args.getLastArgValue(OPT_lto_cs_profile_file); 1150 config->ltoPGOWarnMismatch = args.hasFlag(OPT_lto_pgo_warn_mismatch, 1151 OPT_no_lto_pgo_warn_mismatch, true); 1152 config->ltoDebugPassManager = args.hasArg(OPT_lto_debug_pass_manager); 1153 config->ltoEmitAsm = args.hasArg(OPT_lto_emit_asm); 1154 config->ltoNewPmPasses = args.getLastArgValue(OPT_lto_newpm_passes); 1155 config->ltoWholeProgramVisibility = 1156 args.hasFlag(OPT_lto_whole_program_visibility, 1157 OPT_no_lto_whole_program_visibility, false); 1158 config->ltoo = args::getInteger(args, OPT_lto_O, 2); 1159 config->ltoObjPath = args.getLastArgValue(OPT_lto_obj_path_eq); 1160 config->ltoPartitions = args::getInteger(args, OPT_lto_partitions, 1); 1161 config->ltoSampleProfile = args.getLastArgValue(OPT_lto_sample_profile); 1162 config->ltoBasicBlockSections = 1163 args.getLastArgValue(OPT_lto_basic_block_sections); 1164 config->ltoUniqueBasicBlockSectionNames = 1165 args.hasFlag(OPT_lto_unique_basic_block_section_names, 1166 OPT_no_lto_unique_basic_block_section_names, false); 1167 config->mapFile = args.getLastArgValue(OPT_Map); 1168 config->mipsGotSize = args::getInteger(args, OPT_mips_got_size, 0xfff0); 1169 config->mergeArmExidx = 1170 args.hasFlag(OPT_merge_exidx_entries, OPT_no_merge_exidx_entries, true); 1171 config->mmapOutputFile = 1172 args.hasFlag(OPT_mmap_output_file, OPT_no_mmap_output_file, true); 1173 config->nmagic = args.hasFlag(OPT_nmagic, OPT_no_nmagic, false); 1174 config->noinhibitExec = args.hasArg(OPT_noinhibit_exec); 1175 config->nostdlib = args.hasArg(OPT_nostdlib); 1176 config->oFormatBinary = isOutputFormatBinary(args); 1177 config->omagic = args.hasFlag(OPT_omagic, OPT_no_omagic, false); 1178 config->opaquePointers = args.hasFlag( 1179 OPT_plugin_opt_opaque_pointers, OPT_plugin_opt_no_opaque_pointers, true); 1180 config->optRemarksFilename = args.getLastArgValue(OPT_opt_remarks_filename); 1181 config->optStatsFilename = args.getLastArgValue(OPT_plugin_opt_stats_file); 1182 1183 // Parse remarks hotness threshold. Valid value is either integer or 'auto'. 1184 if (auto *arg = args.getLastArg(OPT_opt_remarks_hotness_threshold)) { 1185 auto resultOrErr = remarks::parseHotnessThresholdOption(arg->getValue()); 1186 if (!resultOrErr) 1187 error(arg->getSpelling() + ": invalid argument '" + arg->getValue() + 1188 "', only integer or 'auto' is supported"); 1189 else 1190 config->optRemarksHotnessThreshold = *resultOrErr; 1191 } 1192 1193 config->optRemarksPasses = args.getLastArgValue(OPT_opt_remarks_passes); 1194 config->optRemarksWithHotness = args.hasArg(OPT_opt_remarks_with_hotness); 1195 config->optRemarksFormat = args.getLastArgValue(OPT_opt_remarks_format); 1196 config->optimize = args::getInteger(args, OPT_O, 1); 1197 config->orphanHandling = getOrphanHandling(args); 1198 config->outputFile = args.getLastArgValue(OPT_o); 1199 config->packageMetadata = args.getLastArgValue(OPT_package_metadata); 1200#ifdef __OpenBSD__ 1201 config->pie = args.hasFlag(OPT_pie, OPT_no_pie, 1202 !args.hasArg(OPT_shared) && !args.hasArg(OPT_relocatable)); 1203#else 1204 config->pie = args.hasFlag(OPT_pie, OPT_no_pie, false); 1205#endif 1206 config->printIcfSections = 1207 args.hasFlag(OPT_print_icf_sections, OPT_no_print_icf_sections, false); 1208 config->printGcSections = 1209 args.hasFlag(OPT_print_gc_sections, OPT_no_print_gc_sections, false); 1210 config->printArchiveStats = args.getLastArgValue(OPT_print_archive_stats); 1211 config->printSymbolOrder = 1212 args.getLastArgValue(OPT_print_symbol_order); 1213 config->relax = args.hasFlag(OPT_relax, OPT_no_relax, true); 1214 config->rpath = getRpath(args); 1215 config->relocatable = args.hasArg(OPT_relocatable); 1216 1217 if (args.hasArg(OPT_save_temps)) { 1218 // --save-temps implies saving all temps. 1219 for (const char *s : saveTempsValues) 1220 config->saveTempsArgs.insert(s); 1221 } else { 1222 for (auto *arg : args.filtered(OPT_save_temps_eq)) { 1223 StringRef s = arg->getValue(); 1224 if (llvm::is_contained(saveTempsValues, s)) 1225 config->saveTempsArgs.insert(s); 1226 else 1227 error("unknown --save-temps value: " + s); 1228 } 1229 } 1230 1231 config->searchPaths = args::getStrings(args, OPT_library_path); 1232 config->sectionStartMap = getSectionStartMap(args); 1233 config->shared = args.hasArg(OPT_shared); 1234 config->singleRoRx = !args.hasFlag(OPT_rosegment, OPT_no_rosegment, true); 1235 config->soName = args.getLastArgValue(OPT_soname); 1236 config->sortSection = getSortSection(args); 1237 config->splitStackAdjustSize = args::getInteger(args, OPT_split_stack_adjust_size, 16384); 1238 config->strip = getStrip(args); 1239 config->sysroot = args.getLastArgValue(OPT_sysroot); 1240 config->target1Rel = args.hasFlag(OPT_target1_rel, OPT_target1_abs, false); 1241 config->target2 = getTarget2(args); 1242 config->thinLTOCacheDir = args.getLastArgValue(OPT_thinlto_cache_dir); 1243 config->thinLTOCachePolicy = CHECK( 1244 parseCachePruningPolicy(args.getLastArgValue(OPT_thinlto_cache_policy)), 1245 "--thinlto-cache-policy: invalid cache policy"); 1246 config->thinLTOEmitImportsFiles = args.hasArg(OPT_thinlto_emit_imports_files); 1247 config->thinLTOEmitIndexFiles = args.hasArg(OPT_thinlto_emit_index_files) || 1248 args.hasArg(OPT_thinlto_index_only) || 1249 args.hasArg(OPT_thinlto_index_only_eq); 1250 config->thinLTOIndexOnly = args.hasArg(OPT_thinlto_index_only) || 1251 args.hasArg(OPT_thinlto_index_only_eq); 1252 config->thinLTOIndexOnlyArg = args.getLastArgValue(OPT_thinlto_index_only_eq); 1253 config->thinLTOObjectSuffixReplace = 1254 getOldNewOptions(args, OPT_thinlto_object_suffix_replace_eq); 1255 config->thinLTOPrefixReplace = 1256 getOldNewOptions(args, OPT_thinlto_prefix_replace_eq); 1257 if (config->thinLTOEmitIndexFiles && !config->thinLTOIndexOnly) { 1258 if (args.hasArg(OPT_thinlto_object_suffix_replace_eq)) 1259 error("--thinlto-object-suffix-replace is not supported with " 1260 "--thinlto-emit-index-files"); 1261 else if (args.hasArg(OPT_thinlto_prefix_replace_eq)) 1262 error("--thinlto-prefix-replace is not supported with " 1263 "--thinlto-emit-index-files"); 1264 } 1265 config->thinLTOModulesToCompile = 1266 args::getStrings(args, OPT_thinlto_single_module_eq); 1267 config->timeTraceEnabled = args.hasArg(OPT_time_trace_eq); 1268 config->timeTraceGranularity = 1269 args::getInteger(args, OPT_time_trace_granularity, 500); 1270 config->trace = args.hasArg(OPT_trace); 1271 config->undefined = args::getStrings(args, OPT_undefined); 1272 config->undefinedVersion = 1273 args.hasFlag(OPT_undefined_version, OPT_no_undefined_version, true); 1274 config->unique = args.hasArg(OPT_unique); 1275 config->useAndroidRelrTags = args.hasFlag( 1276 OPT_use_android_relr_tags, OPT_no_use_android_relr_tags, false); 1277 config->warnBackrefs = 1278 args.hasFlag(OPT_warn_backrefs, OPT_no_warn_backrefs, false); 1279 config->warnCommon = args.hasFlag(OPT_warn_common, OPT_no_warn_common, false); 1280 config->warnSymbolOrdering = 1281 args.hasFlag(OPT_warn_symbol_ordering, OPT_no_warn_symbol_ordering, true); 1282 config->whyExtract = args.getLastArgValue(OPT_why_extract); 1283 config->zCombreloc = getZFlag(args, "combreloc", "nocombreloc", true); 1284 config->zCopyreloc = getZFlag(args, "copyreloc", "nocopyreloc", true); 1285 config->zForceBti = hasZOption(args, "force-bti"); 1286 config->zForceIbt = hasZOption(args, "force-ibt"); 1287 config->zGlobal = hasZOption(args, "global"); 1288 config->zGnustack = getZGnuStack(args); 1289 config->zHazardplt = hasZOption(args, "hazardplt"); 1290 config->zIfuncNoplt = hasZOption(args, "ifunc-noplt"); 1291 config->zInitfirst = hasZOption(args, "initfirst"); 1292 config->zInterpose = hasZOption(args, "interpose"); 1293 config->zKeepTextSectionPrefix = getZFlag( 1294 args, "keep-text-section-prefix", "nokeep-text-section-prefix", false); 1295 config->zNoBtCfi = hasZOption(args, "nobtcfi"); 1296 config->zNodefaultlib = hasZOption(args, "nodefaultlib"); 1297 config->zNodelete = hasZOption(args, "nodelete"); 1298 config->zNodlopen = hasZOption(args, "nodlopen"); 1299 config->zNow = getZFlag(args, "now", "lazy", false); 1300 config->zOrigin = hasZOption(args, "origin"); 1301 config->zPacPlt = hasZOption(args, "pac-plt"); 1302 config->zRelro = getZFlag(args, "relro", "norelro", true); 1303 config->zRetpolineplt = hasZOption(args, "retpolineplt"); 1304 config->zRodynamic = hasZOption(args, "rodynamic"); 1305 config->zSeparate = getZSeparate(args); 1306 config->zShstk = hasZOption(args, "shstk"); 1307 config->zStackSize = args::getZOptionValue(args, OPT_z, "stack-size", 0); 1308 config->zStartStopGC = 1309 getZFlag(args, "start-stop-gc", "nostart-stop-gc", true); 1310 config->zStartStopVisibility = getZStartStopVisibility(args); 1311 config->zText = getZFlag(args, "text", "notext", true); 1312 config->zWxneeded = hasZOption(args, "wxneeded"); 1313 setUnresolvedSymbolPolicy(args); 1314 config->power10Stubs = args.getLastArgValue(OPT_power10_stubs_eq) != "no"; 1315 1316 if (opt::Arg *arg = args.getLastArg(OPT_eb, OPT_el)) { 1317 if (arg->getOption().matches(OPT_eb)) 1318 config->optEB = true; 1319 else 1320 config->optEL = true; 1321 } 1322 1323 for (opt::Arg *arg : args.filtered(OPT_shuffle_sections)) { 1324 constexpr StringRef errPrefix = "--shuffle-sections=: "; 1325 std::pair<StringRef, StringRef> kv = StringRef(arg->getValue()).split('='); 1326 if (kv.first.empty() || kv.second.empty()) { 1327 error(errPrefix + "expected <section_glob>=<seed>, but got '" + 1328 arg->getValue() + "'"); 1329 continue; 1330 } 1331 // Signed so that <section_glob>=-1 is allowed. 1332 int64_t v; 1333 if (!to_integer(kv.second, v)) 1334 error(errPrefix + "expected an integer, but got '" + kv.second + "'"); 1335 else if (Expected<GlobPattern> pat = GlobPattern::create(kv.first)) 1336 config->shuffleSections.emplace_back(std::move(*pat), uint32_t(v)); 1337 else 1338 error(errPrefix + toString(pat.takeError())); 1339 } 1340 1341 auto reports = {std::make_pair("bti-report", &config->zBtiReport), 1342 std::make_pair("cet-report", &config->zCetReport)}; 1343 for (opt::Arg *arg : args.filtered(OPT_z)) { 1344 std::pair<StringRef, StringRef> option = 1345 StringRef(arg->getValue()).split('='); 1346 for (auto reportArg : reports) { 1347 if (option.first != reportArg.first) 1348 continue; 1349 if (!isValidReportString(option.second)) { 1350 error(Twine("-z ") + reportArg.first + "= parameter " + option.second + 1351 " is not recognized"); 1352 continue; 1353 } 1354 *reportArg.second = option.second; 1355 } 1356 } 1357 1358 for (opt::Arg *arg : args.filtered(OPT_z)) { 1359 std::pair<StringRef, StringRef> option = 1360 StringRef(arg->getValue()).split('='); 1361 if (option.first != "dead-reloc-in-nonalloc") 1362 continue; 1363 constexpr StringRef errPrefix = "-z dead-reloc-in-nonalloc=: "; 1364 std::pair<StringRef, StringRef> kv = option.second.split('='); 1365 if (kv.first.empty() || kv.second.empty()) { 1366 error(errPrefix + "expected <section_glob>=<value>"); 1367 continue; 1368 } 1369 uint64_t v; 1370 if (!to_integer(kv.second, v)) 1371 error(errPrefix + "expected a non-negative integer, but got '" + 1372 kv.second + "'"); 1373 else if (Expected<GlobPattern> pat = GlobPattern::create(kv.first)) 1374 config->deadRelocInNonAlloc.emplace_back(std::move(*pat), v); 1375 else 1376 error(errPrefix + toString(pat.takeError())); 1377 } 1378 1379 cl::ResetAllOptionOccurrences(); 1380 1381 // Parse LTO options. 1382 if (auto *arg = args.getLastArg(OPT_plugin_opt_mcpu_eq)) 1383 parseClangOption(saver().save("-mcpu=" + StringRef(arg->getValue())), 1384 arg->getSpelling()); 1385 1386 for (opt::Arg *arg : args.filtered(OPT_plugin_opt_eq_minus)) 1387 parseClangOption(std::string("-") + arg->getValue(), arg->getSpelling()); 1388 1389 // GCC collect2 passes -plugin-opt=path/to/lto-wrapper with an absolute or 1390 // relative path. Just ignore. If not ended with "lto-wrapper" (or 1391 // "lto-wrapper.exe" for GCC cross-compiled for Windows), consider it an 1392 // unsupported LLVMgold.so option and error. 1393 for (opt::Arg *arg : args.filtered(OPT_plugin_opt_eq)) { 1394 StringRef v(arg->getValue()); 1395 if (!v.endswith("lto-wrapper") && !v.endswith("lto-wrapper.exe")) 1396 error(arg->getSpelling() + ": unknown plugin option '" + arg->getValue() + 1397 "'"); 1398 } 1399 1400 config->passPlugins = args::getStrings(args, OPT_load_pass_plugins); 1401 1402 // Parse -mllvm options. 1403 for (const auto *arg : args.filtered(OPT_mllvm)) { 1404 parseClangOption(arg->getValue(), arg->getSpelling()); 1405 config->mllvmOpts.emplace_back(arg->getValue()); 1406 } 1407 1408 // --threads= takes a positive integer and provides the default value for 1409 // --thinlto-jobs=. 1410 if (auto *arg = args.getLastArg(OPT_threads)) { 1411 StringRef v(arg->getValue()); 1412 unsigned threads = 0; 1413 if (!llvm::to_integer(v, threads, 0) || threads == 0) 1414 error(arg->getSpelling() + ": expected a positive integer, but got '" + 1415 arg->getValue() + "'"); 1416 parallel::strategy = hardware_concurrency(threads); 1417 config->thinLTOJobs = v; 1418 } 1419 if (auto *arg = args.getLastArg(OPT_thinlto_jobs_eq)) 1420 config->thinLTOJobs = arg->getValue(); 1421 config->threadCount = parallel::strategy.compute_thread_count(); 1422 1423 if (config->ltoo > 3) 1424 error("invalid optimization level for LTO: " + Twine(config->ltoo)); 1425 if (config->ltoPartitions == 0) 1426 error("--lto-partitions: number of threads must be > 0"); 1427 if (!get_threadpool_strategy(config->thinLTOJobs)) 1428 error("--thinlto-jobs: invalid job count: " + config->thinLTOJobs); 1429 1430 if (config->splitStackAdjustSize < 0) 1431 error("--split-stack-adjust-size: size must be >= 0"); 1432 1433 // The text segment is traditionally the first segment, whose address equals 1434 // the base address. However, lld places the R PT_LOAD first. -Ttext-segment 1435 // is an old-fashioned option that does not play well with lld's layout. 1436 // Suggest --image-base as a likely alternative. 1437 if (args.hasArg(OPT_Ttext_segment)) 1438 error("-Ttext-segment is not supported. Use --image-base if you " 1439 "intend to set the base address"); 1440 1441 // Parse ELF{32,64}{LE,BE} and CPU type. 1442 if (auto *arg = args.getLastArg(OPT_m)) { 1443 StringRef s = arg->getValue(); 1444 std::tie(config->ekind, config->emachine, config->osabi) = 1445 parseEmulation(s); 1446 config->mipsN32Abi = 1447 (s.startswith("elf32btsmipn32") || s.startswith("elf32ltsmipn32")); 1448 config->emulation = s; 1449 } 1450 1451 // Parse --hash-style={sysv,gnu,both}. 1452 if (auto *arg = args.getLastArg(OPT_hash_style)) { 1453 StringRef s = arg->getValue(); 1454 if (s == "sysv") 1455 config->sysvHash = true; 1456 else if (s == "gnu") 1457 config->gnuHash = true; 1458 else if (s == "both") 1459 config->sysvHash = config->gnuHash = true; 1460 else 1461 error("unknown --hash-style: " + s); 1462 } 1463 1464 if (args.hasArg(OPT_print_map)) 1465 config->mapFile = "-"; 1466 1467 // Page alignment can be disabled by the -n (--nmagic) and -N (--omagic). 1468 // As PT_GNU_RELRO relies on Paging, do not create it when we have disabled 1469 // it. 1470 if (config->nmagic || config->omagic) 1471 config->zRelro = false; 1472 1473 std::tie(config->buildId, config->buildIdVector) = getBuildId(args); 1474 1475 if (getZFlag(args, "pack-relative-relocs", "nopack-relative-relocs", false)) { 1476 config->relrGlibc = true; 1477 config->relrPackDynRelocs = true; 1478 } else { 1479 std::tie(config->androidPackDynRelocs, config->relrPackDynRelocs) = 1480 getPackDynRelocs(args); 1481 } 1482 1483 if (auto *arg = args.getLastArg(OPT_symbol_ordering_file)){ 1484 if (args.hasArg(OPT_call_graph_ordering_file)) 1485 error("--symbol-ordering-file and --call-graph-order-file " 1486 "may not be used together"); 1487 if (std::optional<MemoryBufferRef> buffer = readFile(arg->getValue())) { 1488 config->symbolOrderingFile = getSymbolOrderingFile(*buffer); 1489 // Also need to disable CallGraphProfileSort to prevent 1490 // LLD order symbols with CGProfile 1491 config->callGraphProfileSort = false; 1492 } 1493 } 1494 1495 assert(config->versionDefinitions.empty()); 1496 config->versionDefinitions.push_back( 1497 {"local", (uint16_t)VER_NDX_LOCAL, {}, {}}); 1498 config->versionDefinitions.push_back( 1499 {"global", (uint16_t)VER_NDX_GLOBAL, {}, {}}); 1500 1501 // If --retain-symbol-file is used, we'll keep only the symbols listed in 1502 // the file and discard all others. 1503 if (auto *arg = args.getLastArg(OPT_retain_symbols_file)) { 1504 config->versionDefinitions[VER_NDX_LOCAL].nonLocalPatterns.push_back( 1505 {"*", /*isExternCpp=*/false, /*hasWildcard=*/true}); 1506 if (std::optional<MemoryBufferRef> buffer = readFile(arg->getValue())) 1507 for (StringRef s : args::getLines(*buffer)) 1508 config->versionDefinitions[VER_NDX_GLOBAL].nonLocalPatterns.push_back( 1509 {s, /*isExternCpp=*/false, /*hasWildcard=*/false}); 1510 } 1511 1512 for (opt::Arg *arg : args.filtered(OPT_warn_backrefs_exclude)) { 1513 StringRef pattern(arg->getValue()); 1514 if (Expected<GlobPattern> pat = GlobPattern::create(pattern)) 1515 config->warnBackrefsExclude.push_back(std::move(*pat)); 1516 else 1517 error(arg->getSpelling() + ": " + toString(pat.takeError())); 1518 } 1519 1520 // For -no-pie and -pie, --export-dynamic-symbol specifies defined symbols 1521 // which should be exported. For -shared, references to matched non-local 1522 // STV_DEFAULT symbols are not bound to definitions within the shared object, 1523 // even if other options express a symbolic intention: -Bsymbolic, 1524 // -Bsymbolic-functions (if STT_FUNC), --dynamic-list. 1525 for (auto *arg : args.filtered(OPT_export_dynamic_symbol)) 1526 config->dynamicList.push_back( 1527 {arg->getValue(), /*isExternCpp=*/false, 1528 /*hasWildcard=*/hasWildcard(arg->getValue())}); 1529 1530 // --export-dynamic-symbol-list specifies a list of --export-dynamic-symbol 1531 // patterns. --dynamic-list is --export-dynamic-symbol-list plus -Bsymbolic 1532 // like semantics. 1533 config->symbolic = 1534 config->bsymbolic == BsymbolicKind::All || args.hasArg(OPT_dynamic_list); 1535 for (auto *arg : 1536 args.filtered(OPT_dynamic_list, OPT_export_dynamic_symbol_list)) 1537 if (std::optional<MemoryBufferRef> buffer = readFile(arg->getValue())) 1538 readDynamicList(*buffer); 1539 1540 for (auto *arg : args.filtered(OPT_version_script)) 1541 if (std::optional<std::string> path = searchScript(arg->getValue())) { 1542 if (std::optional<MemoryBufferRef> buffer = readFile(*path)) 1543 readVersionScript(*buffer); 1544 } else { 1545 error(Twine("cannot find version script ") + arg->getValue()); 1546 } 1547} 1548 1549// Some Config members do not directly correspond to any particular 1550// command line options, but computed based on other Config values. 1551// This function initialize such members. See Config.h for the details 1552// of these values. 1553static void setConfigs(opt::InputArgList &args) { 1554 ELFKind k = config->ekind; 1555 uint16_t m = config->emachine; 1556 1557 config->copyRelocs = (config->relocatable || config->emitRelocs); 1558 config->is64 = (k == ELF64LEKind || k == ELF64BEKind); 1559 config->isLE = (k == ELF32LEKind || k == ELF64LEKind); 1560 config->endianness = config->isLE ? endianness::little : endianness::big; 1561 config->isMips64EL = (k == ELF64LEKind && m == EM_MIPS); 1562 config->isPic = config->pie || config->shared; 1563 config->picThunk = args.hasArg(OPT_pic_veneer, config->isPic); 1564 config->wordsize = config->is64 ? 8 : 4; 1565 1566 // ELF defines two different ways to store relocation addends as shown below: 1567 // 1568 // Rel: Addends are stored to the location where relocations are applied. It 1569 // cannot pack the full range of addend values for all relocation types, but 1570 // this only affects relocation types that we don't support emitting as 1571 // dynamic relocations (see getDynRel). 1572 // Rela: Addends are stored as part of relocation entry. 1573 // 1574 // In other words, Rela makes it easy to read addends at the price of extra 1575 // 4 or 8 byte for each relocation entry. 1576 // 1577 // We pick the format for dynamic relocations according to the psABI for each 1578 // processor, but a contrary choice can be made if the dynamic loader 1579 // supports. 1580 config->isRela = getIsRela(args); 1581 1582 // If the output uses REL relocations we must store the dynamic relocation 1583 // addends to the output sections. We also store addends for RELA relocations 1584 // if --apply-dynamic-relocs is used. 1585 // We default to not writing the addends when using RELA relocations since 1586 // any standard conforming tool can find it in r_addend. 1587 config->writeAddends = args.hasFlag(OPT_apply_dynamic_relocs, 1588 OPT_no_apply_dynamic_relocs, false) || 1589 !config->isRela; 1590 // Validation of dynamic relocation addends is on by default for assertions 1591 // builds (for supported targets) and disabled otherwise. Ideally we would 1592 // enable the debug checks for all targets, but currently not all targets 1593 // have support for reading Elf_Rel addends, so we only enable for a subset. 1594#ifndef NDEBUG 1595 bool checkDynamicRelocsDefault = m == EM_AARCH64 || m == EM_ARM || 1596 m == EM_386 || m == EM_MIPS || 1597 m == EM_X86_64 || m == EM_RISCV; 1598#else 1599 bool checkDynamicRelocsDefault = false; 1600#endif 1601 config->checkDynamicRelocs = 1602 args.hasFlag(OPT_check_dynamic_relocations, 1603 OPT_no_check_dynamic_relocations, checkDynamicRelocsDefault); 1604 config->tocOptimize = 1605 args.hasFlag(OPT_toc_optimize, OPT_no_toc_optimize, m == EM_PPC64); 1606 config->pcRelOptimize = 1607 args.hasFlag(OPT_pcrel_optimize, OPT_no_pcrel_optimize, m == EM_PPC64); 1608 1609 config->executeOnly = false; 1610#ifdef __OpenBSD__ 1611 switch (m) { 1612 case EM_AARCH64: 1613 case EM_MIPS: 1614 case EM_PPC: 1615 case EM_PPC64: 1616 case EM_RISCV: 1617 case EM_SPARCV9: 1618 case EM_X86_64: 1619 config->executeOnly = true; 1620 break; 1621 } 1622#endif 1623 config->executeOnly = 1624 args.hasFlag(OPT_execute_only, OPT_no_execute_only, config->executeOnly); 1625} 1626 1627static bool isFormatBinary(StringRef s) { 1628 if (s == "binary") 1629 return true; 1630 if (s == "elf" || s == "default") 1631 return false; 1632 error("unknown --format value: " + s + 1633 " (supported formats: elf, default, binary)"); 1634 return false; 1635} 1636 1637void LinkerDriver::createFiles(opt::InputArgList &args) { 1638 llvm::TimeTraceScope timeScope("Load input files"); 1639 // For --{push,pop}-state. 1640 std::vector<std::tuple<bool, bool, bool>> stack; 1641 1642 // Iterate over argv to process input files and positional arguments. 1643 InputFile::isInGroup = false; 1644 bool hasInput = false; 1645 for (auto *arg : args) { 1646 switch (arg->getOption().getID()) { 1647 case OPT_library: 1648 addLibrary(arg->getValue()); 1649 hasInput = true; 1650 break; 1651 case OPT_INPUT: 1652 addFile(arg->getValue(), /*withLOption=*/false); 1653 hasInput = true; 1654 break; 1655 case OPT_defsym: { 1656 StringRef from; 1657 StringRef to; 1658 std::tie(from, to) = StringRef(arg->getValue()).split('='); 1659 if (from.empty() || to.empty()) 1660 error("--defsym: syntax error: " + StringRef(arg->getValue())); 1661 else 1662 readDefsym(from, MemoryBufferRef(to, "--defsym")); 1663 break; 1664 } 1665 case OPT_script: 1666 if (std::optional<std::string> path = searchScript(arg->getValue())) { 1667 if (std::optional<MemoryBufferRef> mb = readFile(*path)) 1668 readLinkerScript(*mb); 1669 break; 1670 } 1671 error(Twine("cannot find linker script ") + arg->getValue()); 1672 break; 1673 case OPT_as_needed: 1674 config->asNeeded = true; 1675 break; 1676 case OPT_format: 1677 config->formatBinary = isFormatBinary(arg->getValue()); 1678 break; 1679 case OPT_no_as_needed: 1680 config->asNeeded = false; 1681 break; 1682 case OPT_Bstatic: 1683 case OPT_omagic: 1684 case OPT_nmagic: 1685 config->isStatic = true; 1686 break; 1687 case OPT_Bdynamic: 1688 config->isStatic = false; 1689 break; 1690 case OPT_whole_archive: 1691 inWholeArchive = true; 1692 break; 1693 case OPT_no_whole_archive: 1694 inWholeArchive = false; 1695 break; 1696 case OPT_just_symbols: 1697 if (std::optional<MemoryBufferRef> mb = readFile(arg->getValue())) { 1698 files.push_back(createObjFile(*mb)); 1699 files.back()->justSymbols = true; 1700 } 1701 break; 1702 case OPT_start_group: 1703 if (InputFile::isInGroup) 1704 error("nested --start-group"); 1705 InputFile::isInGroup = true; 1706 break; 1707 case OPT_end_group: 1708 if (!InputFile::isInGroup) 1709 error("stray --end-group"); 1710 InputFile::isInGroup = false; 1711 ++InputFile::nextGroupId; 1712 break; 1713 case OPT_start_lib: 1714 if (inLib) 1715 error("nested --start-lib"); 1716 if (InputFile::isInGroup) 1717 error("may not nest --start-lib in --start-group"); 1718 inLib = true; 1719 InputFile::isInGroup = true; 1720 break; 1721 case OPT_end_lib: 1722 if (!inLib) 1723 error("stray --end-lib"); 1724 inLib = false; 1725 InputFile::isInGroup = false; 1726 ++InputFile::nextGroupId; 1727 break; 1728 case OPT_push_state: 1729 stack.emplace_back(config->asNeeded, config->isStatic, inWholeArchive); 1730 break; 1731 case OPT_pop_state: 1732 if (stack.empty()) { 1733 error("unbalanced --push-state/--pop-state"); 1734 break; 1735 } 1736 std::tie(config->asNeeded, config->isStatic, inWholeArchive) = stack.back(); 1737 stack.pop_back(); 1738 break; 1739 } 1740 } 1741 1742 if (files.empty() && !hasInput && errorCount() == 0) 1743 error("no input files"); 1744} 1745 1746// If -m <machine_type> was not given, infer it from object files. 1747void LinkerDriver::inferMachineType() { 1748 if (config->ekind != ELFNoneKind) 1749 return; 1750 1751 for (InputFile *f : files) { 1752 if (f->ekind == ELFNoneKind) 1753 continue; 1754 config->ekind = f->ekind; 1755 config->emachine = f->emachine; 1756 config->osabi = f->osabi; 1757 config->mipsN32Abi = config->emachine == EM_MIPS && isMipsN32Abi(f); 1758 return; 1759 } 1760 error("target emulation unknown: -m or at least one .o file required"); 1761} 1762 1763// Parse -z max-page-size=<value>. The default value is defined by 1764// each target. Is set to 1 if given nmagic or omagic. 1765static uint64_t getMaxPageSize(opt::InputArgList &args) { 1766 uint64_t val = args::getZOptionValue(args, OPT_z, "max-page-size", 1767 target->defaultMaxPageSize); 1768 if (!isPowerOf2_64(val)) { 1769 error("max-page-size: value isn't a power of 2"); 1770 return target->defaultMaxPageSize; 1771 } 1772 if (config->nmagic || config->omagic) { 1773 if (val != target->defaultMaxPageSize) 1774 warn("-z max-page-size set, but paging disabled by omagic or nmagic"); 1775 return 1; 1776 } 1777 return val; 1778} 1779 1780// Parse -z common-page-size=<value>. The default value is defined by 1781// each target. Is set to 1 if given nmagic or omagic. 1782static uint64_t getCommonPageSize(opt::InputArgList &args) { 1783 uint64_t val = args::getZOptionValue(args, OPT_z, "common-page-size", 1784 target->defaultCommonPageSize); 1785 if (!isPowerOf2_64(val)) { 1786 error("common-page-size: value isn't a power of 2"); 1787 return target->defaultCommonPageSize; 1788 } 1789 if (config->nmagic || config->omagic) { 1790 if (val != target->defaultCommonPageSize) 1791 warn("-z common-page-size set, but paging disabled by omagic or nmagic"); 1792 return 1; 1793 } 1794 // commonPageSize can't be larger than maxPageSize. 1795 if (val > config->maxPageSize) 1796 val = config->maxPageSize; 1797 return val; 1798} 1799 1800// Parse -z max-page-size=<value>. The default value is defined by 1801// each target. 1802static uint64_t getRealMaxPageSize(opt::InputArgList &args) { 1803 uint64_t val = args::getZOptionValue(args, OPT_z, "max-page-size", 1804 target->defaultMaxPageSize); 1805 if (!isPowerOf2_64(val)) 1806 error("max-page-size: value isn't a power of 2"); 1807 return val; 1808} 1809 1810// Parses --image-base option. 1811static std::optional<uint64_t> getImageBase(opt::InputArgList &args) { 1812 // Because we are using "Config->maxPageSize" here, this function has to be 1813 // called after the variable is initialized. 1814 auto *arg = args.getLastArg(OPT_image_base); 1815 if (!arg) 1816 return std::nullopt; 1817 1818 StringRef s = arg->getValue(); 1819 uint64_t v; 1820 if (!to_integer(s, v)) { 1821 error("--image-base: number expected, but got " + s); 1822 return 0; 1823 } 1824 if ((v % config->maxPageSize) != 0) 1825 warn("--image-base: address isn't multiple of page size: " + s); 1826 return v; 1827} 1828 1829// Parses `--exclude-libs=lib,lib,...`. 1830// The library names may be delimited by commas or colons. 1831static DenseSet<StringRef> getExcludeLibs(opt::InputArgList &args) { 1832 DenseSet<StringRef> ret; 1833 for (auto *arg : args.filtered(OPT_exclude_libs)) { 1834 StringRef s = arg->getValue(); 1835 for (;;) { 1836 size_t pos = s.find_first_of(",:"); 1837 if (pos == StringRef::npos) 1838 break; 1839 ret.insert(s.substr(0, pos)); 1840 s = s.substr(pos + 1); 1841 } 1842 ret.insert(s); 1843 } 1844 return ret; 1845} 1846 1847// Handles the --exclude-libs option. If a static library file is specified 1848// by the --exclude-libs option, all public symbols from the archive become 1849// private unless otherwise specified by version scripts or something. 1850// A special library name "ALL" means all archive files. 1851// 1852// This is not a popular option, but some programs such as bionic libc use it. 1853static void excludeLibs(opt::InputArgList &args) { 1854 DenseSet<StringRef> libs = getExcludeLibs(args); 1855 bool all = libs.count("ALL"); 1856 1857 auto visit = [&](InputFile *file) { 1858 if (file->archiveName.empty() || 1859 !(all || libs.count(path::filename(file->archiveName)))) 1860 return; 1861 ArrayRef<Symbol *> symbols = file->getSymbols(); 1862 if (isa<ELFFileBase>(file)) 1863 symbols = cast<ELFFileBase>(file)->getGlobalSymbols(); 1864 for (Symbol *sym : symbols) 1865 if (!sym->isUndefined() && sym->file == file) 1866 sym->versionId = VER_NDX_LOCAL; 1867 }; 1868 1869 for (ELFFileBase *file : ctx.objectFiles) 1870 visit(file); 1871 1872 for (BitcodeFile *file : ctx.bitcodeFiles) 1873 visit(file); 1874} 1875 1876// Force Sym to be entered in the output. 1877static void handleUndefined(Symbol *sym, const char *option) { 1878 // Since a symbol may not be used inside the program, LTO may 1879 // eliminate it. Mark the symbol as "used" to prevent it. 1880 sym->isUsedInRegularObj = true; 1881 1882 if (!sym->isLazy()) 1883 return; 1884 sym->extract(); 1885 if (!config->whyExtract.empty()) 1886 ctx.whyExtractRecords.emplace_back(option, sym->file, *sym); 1887} 1888 1889// As an extension to GNU linkers, lld supports a variant of `-u` 1890// which accepts wildcard patterns. All symbols that match a given 1891// pattern are handled as if they were given by `-u`. 1892static void handleUndefinedGlob(StringRef arg) { 1893 Expected<GlobPattern> pat = GlobPattern::create(arg); 1894 if (!pat) { 1895 error("--undefined-glob: " + toString(pat.takeError())); 1896 return; 1897 } 1898 1899 // Calling sym->extract() in the loop is not safe because it may add new 1900 // symbols to the symbol table, invalidating the current iterator. 1901 SmallVector<Symbol *, 0> syms; 1902 for (Symbol *sym : symtab.getSymbols()) 1903 if (!sym->isPlaceholder() && pat->match(sym->getName())) 1904 syms.push_back(sym); 1905 1906 for (Symbol *sym : syms) 1907 handleUndefined(sym, "--undefined-glob"); 1908} 1909 1910static void handleLibcall(StringRef name) { 1911 Symbol *sym = symtab.find(name); 1912 if (!sym || !sym->isLazy()) 1913 return; 1914 1915 MemoryBufferRef mb; 1916 mb = cast<LazyObject>(sym)->file->mb; 1917 1918 if (isBitcode(mb)) 1919 sym->extract(); 1920} 1921 1922static void writeArchiveStats() { 1923 if (config->printArchiveStats.empty()) 1924 return; 1925 1926 std::error_code ec; 1927 raw_fd_ostream os(config->printArchiveStats, ec, sys::fs::OF_None); 1928 if (ec) { 1929 error("--print-archive-stats=: cannot open " + config->printArchiveStats + 1930 ": " + ec.message()); 1931 return; 1932 } 1933 1934 os << "members\textracted\tarchive\n"; 1935 1936 SmallVector<StringRef, 0> archives; 1937 DenseMap<CachedHashStringRef, unsigned> all, extracted; 1938 for (ELFFileBase *file : ctx.objectFiles) 1939 if (file->archiveName.size()) 1940 ++extracted[CachedHashStringRef(file->archiveName)]; 1941 for (BitcodeFile *file : ctx.bitcodeFiles) 1942 if (file->archiveName.size()) 1943 ++extracted[CachedHashStringRef(file->archiveName)]; 1944 for (std::pair<StringRef, unsigned> f : ctx.driver.archiveFiles) { 1945 unsigned &v = extracted[CachedHashString(f.first)]; 1946 os << f.second << '\t' << v << '\t' << f.first << '\n'; 1947 // If the archive occurs multiple times, other instances have a count of 0. 1948 v = 0; 1949 } 1950} 1951 1952static void writeWhyExtract() { 1953 if (config->whyExtract.empty()) 1954 return; 1955 1956 std::error_code ec; 1957 raw_fd_ostream os(config->whyExtract, ec, sys::fs::OF_None); 1958 if (ec) { 1959 error("cannot open --why-extract= file " + config->whyExtract + ": " + 1960 ec.message()); 1961 return; 1962 } 1963 1964 os << "reference\textracted\tsymbol\n"; 1965 for (auto &entry : ctx.whyExtractRecords) { 1966 os << std::get<0>(entry) << '\t' << toString(std::get<1>(entry)) << '\t' 1967 << toString(std::get<2>(entry)) << '\n'; 1968 } 1969} 1970 1971static void reportBackrefs() { 1972 for (auto &ref : ctx.backwardReferences) { 1973 const Symbol &sym = *ref.first; 1974 std::string to = toString(ref.second.second); 1975 // Some libraries have known problems and can cause noise. Filter them out 1976 // with --warn-backrefs-exclude=. The value may look like (for --start-lib) 1977 // *.o or (archive member) *.a(*.o). 1978 bool exclude = false; 1979 for (const llvm::GlobPattern &pat : config->warnBackrefsExclude) 1980 if (pat.match(to)) { 1981 exclude = true; 1982 break; 1983 } 1984 if (!exclude) 1985 warn("backward reference detected: " + sym.getName() + " in " + 1986 toString(ref.second.first) + " refers to " + to); 1987 } 1988} 1989 1990// Handle --dependency-file=<path>. If that option is given, lld creates a 1991// file at a given path with the following contents: 1992// 1993// <output-file>: <input-file> ... 1994// 1995// <input-file>: 1996// 1997// where <output-file> is a pathname of an output file and <input-file> 1998// ... is a list of pathnames of all input files. `make` command can read a 1999// file in the above format and interpret it as a dependency info. We write 2000// phony targets for every <input-file> to avoid an error when that file is 2001// removed. 2002// 2003// This option is useful if you want to make your final executable to depend 2004// on all input files including system libraries. Here is why. 2005// 2006// When you write a Makefile, you usually write it so that the final 2007// executable depends on all user-generated object files. Normally, you 2008// don't make your executable to depend on system libraries (such as libc) 2009// because you don't know the exact paths of libraries, even though system 2010// libraries that are linked to your executable statically are technically a 2011// part of your program. By using --dependency-file option, you can make 2012// lld to dump dependency info so that you can maintain exact dependencies 2013// easily. 2014static void writeDependencyFile() { 2015 std::error_code ec; 2016 raw_fd_ostream os(config->dependencyFile, ec, sys::fs::OF_None); 2017 if (ec) { 2018 error("cannot open " + config->dependencyFile + ": " + ec.message()); 2019 return; 2020 } 2021 2022 // We use the same escape rules as Clang/GCC which are accepted by Make/Ninja: 2023 // * A space is escaped by a backslash which itself must be escaped. 2024 // * A hash sign is escaped by a single backslash. 2025 // * $ is escapes as $$. 2026 auto printFilename = [](raw_fd_ostream &os, StringRef filename) { 2027 llvm::SmallString<256> nativePath; 2028 llvm::sys::path::native(filename.str(), nativePath); 2029 llvm::sys::path::remove_dots(nativePath, /*remove_dot_dot=*/true); 2030 for (unsigned i = 0, e = nativePath.size(); i != e; ++i) { 2031 if (nativePath[i] == '#') { 2032 os << '\\'; 2033 } else if (nativePath[i] == ' ') { 2034 os << '\\'; 2035 unsigned j = i; 2036 while (j > 0 && nativePath[--j] == '\\') 2037 os << '\\'; 2038 } else if (nativePath[i] == '$') { 2039 os << '$'; 2040 } 2041 os << nativePath[i]; 2042 } 2043 }; 2044 2045 os << config->outputFile << ":"; 2046 for (StringRef path : config->dependencyFiles) { 2047 os << " \\\n "; 2048 printFilename(os, path); 2049 } 2050 os << "\n"; 2051 2052 for (StringRef path : config->dependencyFiles) { 2053 os << "\n"; 2054 printFilename(os, path); 2055 os << ":\n"; 2056 } 2057} 2058 2059// Replaces common symbols with defined symbols reside in .bss sections. 2060// This function is called after all symbol names are resolved. As a 2061// result, the passes after the symbol resolution won't see any 2062// symbols of type CommonSymbol. 2063static void replaceCommonSymbols() { 2064 llvm::TimeTraceScope timeScope("Replace common symbols"); 2065 for (ELFFileBase *file : ctx.objectFiles) { 2066 if (!file->hasCommonSyms) 2067 continue; 2068 for (Symbol *sym : file->getGlobalSymbols()) { 2069 auto *s = dyn_cast<CommonSymbol>(sym); 2070 if (!s) 2071 continue; 2072 2073 auto *bss = make<BssSection>("COMMON", s->size, s->alignment); 2074 bss->file = s->file; 2075 ctx.inputSections.push_back(bss); 2076 Defined(s->file, StringRef(), s->binding, s->stOther, s->type, 2077 /*value=*/0, s->size, bss) 2078 .overwrite(*s); 2079 } 2080 } 2081} 2082 2083// If all references to a DSO happen to be weak, the DSO is not added to 2084// DT_NEEDED. If that happens, replace ShardSymbol with Undefined to avoid 2085// dangling references to an unneeded DSO. Use a weak binding to avoid 2086// --no-allow-shlib-undefined diagnostics. Similarly, demote lazy symbols. 2087static void demoteSharedAndLazySymbols() { 2088 llvm::TimeTraceScope timeScope("Demote shared and lazy symbols"); 2089 for (Symbol *sym : symtab.getSymbols()) { 2090 auto *s = dyn_cast<SharedSymbol>(sym); 2091 if (!(s && !cast<SharedFile>(s->file)->isNeeded) && !sym->isLazy()) 2092 continue; 2093 2094 uint8_t binding = sym->isLazy() ? sym->binding : uint8_t(STB_WEAK); 2095 Undefined(nullptr, sym->getName(), binding, sym->stOther, sym->type) 2096 .overwrite(*sym); 2097 sym->versionId = VER_NDX_GLOBAL; 2098 } 2099} 2100 2101// The section referred to by `s` is considered address-significant. Set the 2102// keepUnique flag on the section if appropriate. 2103static void markAddrsig(Symbol *s) { 2104 if (auto *d = dyn_cast_or_null<Defined>(s)) 2105 if (d->section) 2106 // We don't need to keep text sections unique under --icf=all even if they 2107 // are address-significant. 2108 if (config->icf == ICFLevel::Safe || !(d->section->flags & SHF_EXECINSTR)) 2109 d->section->keepUnique = true; 2110} 2111 2112// Record sections that define symbols mentioned in --keep-unique <symbol> 2113// and symbols referred to by address-significance tables. These sections are 2114// ineligible for ICF. 2115template <class ELFT> 2116static void findKeepUniqueSections(opt::InputArgList &args) { 2117 for (auto *arg : args.filtered(OPT_keep_unique)) { 2118 StringRef name = arg->getValue(); 2119 auto *d = dyn_cast_or_null<Defined>(symtab.find(name)); 2120 if (!d || !d->section) { 2121 warn("could not find symbol " + name + " to keep unique"); 2122 continue; 2123 } 2124 d->section->keepUnique = true; 2125 } 2126 2127 // --icf=all --ignore-data-address-equality means that we can ignore 2128 // the dynsym and address-significance tables entirely. 2129 if (config->icf == ICFLevel::All && config->ignoreDataAddressEquality) 2130 return; 2131 2132 // Symbols in the dynsym could be address-significant in other executables 2133 // or DSOs, so we conservatively mark them as address-significant. 2134 for (Symbol *sym : symtab.getSymbols()) 2135 if (sym->includeInDynsym()) 2136 markAddrsig(sym); 2137 2138 // Visit the address-significance table in each object file and mark each 2139 // referenced symbol as address-significant. 2140 for (InputFile *f : ctx.objectFiles) { 2141 auto *obj = cast<ObjFile<ELFT>>(f); 2142 ArrayRef<Symbol *> syms = obj->getSymbols(); 2143 if (obj->addrsigSec) { 2144 ArrayRef<uint8_t> contents = 2145 check(obj->getObj().getSectionContents(*obj->addrsigSec)); 2146 const uint8_t *cur = contents.begin(); 2147 while (cur != contents.end()) { 2148 unsigned size; 2149 const char *err; 2150 uint64_t symIndex = decodeULEB128(cur, &size, contents.end(), &err); 2151 if (err) 2152 fatal(toString(f) + ": could not decode addrsig section: " + err); 2153 markAddrsig(syms[symIndex]); 2154 cur += size; 2155 } 2156 } else { 2157 // If an object file does not have an address-significance table, 2158 // conservatively mark all of its symbols as address-significant. 2159 for (Symbol *s : syms) 2160 markAddrsig(s); 2161 } 2162 } 2163} 2164 2165// This function reads a symbol partition specification section. These sections 2166// are used to control which partition a symbol is allocated to. See 2167// https://lld.llvm.org/Partitions.html for more details on partitions. 2168template <typename ELFT> 2169static void readSymbolPartitionSection(InputSectionBase *s) { 2170 // Read the relocation that refers to the partition's entry point symbol. 2171 Symbol *sym; 2172 const RelsOrRelas<ELFT> rels = s->template relsOrRelas<ELFT>(); 2173 if (rels.areRelocsRel()) 2174 sym = &s->getFile<ELFT>()->getRelocTargetSym(rels.rels[0]); 2175 else 2176 sym = &s->getFile<ELFT>()->getRelocTargetSym(rels.relas[0]); 2177 if (!isa<Defined>(sym) || !sym->includeInDynsym()) 2178 return; 2179 2180 StringRef partName = reinterpret_cast<const char *>(s->content().data()); 2181 for (Partition &part : partitions) { 2182 if (part.name == partName) { 2183 sym->partition = part.getNumber(); 2184 return; 2185 } 2186 } 2187 2188 // Forbid partitions from being used on incompatible targets, and forbid them 2189 // from being used together with various linker features that assume a single 2190 // set of output sections. 2191 if (script->hasSectionsCommand) 2192 error(toString(s->file) + 2193 ": partitions cannot be used with the SECTIONS command"); 2194 if (script->hasPhdrsCommands()) 2195 error(toString(s->file) + 2196 ": partitions cannot be used with the PHDRS command"); 2197 if (!config->sectionStartMap.empty()) 2198 error(toString(s->file) + ": partitions cannot be used with " 2199 "--section-start, -Ttext, -Tdata or -Tbss"); 2200 if (config->emachine == EM_MIPS) 2201 error(toString(s->file) + ": partitions cannot be used on this target"); 2202 2203 // Impose a limit of no more than 254 partitions. This limit comes from the 2204 // sizes of the Partition fields in InputSectionBase and Symbol, as well as 2205 // the amount of space devoted to the partition number in RankFlags. 2206 if (partitions.size() == 254) 2207 fatal("may not have more than 254 partitions"); 2208 2209 partitions.emplace_back(); 2210 Partition &newPart = partitions.back(); 2211 newPart.name = partName; 2212 sym->partition = newPart.getNumber(); 2213} 2214 2215static Symbol *addUnusedUndefined(StringRef name, 2216 uint8_t binding = STB_GLOBAL) { 2217 return symtab.addSymbol(Undefined{nullptr, name, binding, STV_DEFAULT, 0}); 2218} 2219 2220static void markBuffersAsDontNeed(bool skipLinkedOutput) { 2221 // With --thinlto-index-only, all buffers are nearly unused from now on 2222 // (except symbol/section names used by infrequent passes). Mark input file 2223 // buffers as MADV_DONTNEED so that these pages can be reused by the expensive 2224 // thin link, saving memory. 2225 if (skipLinkedOutput) { 2226 for (MemoryBuffer &mb : llvm::make_pointee_range(ctx.memoryBuffers)) 2227 mb.dontNeedIfMmap(); 2228 return; 2229 } 2230 2231 // Otherwise, just mark MemoryBuffers backing BitcodeFiles. 2232 DenseSet<const char *> bufs; 2233 for (BitcodeFile *file : ctx.bitcodeFiles) 2234 bufs.insert(file->mb.getBufferStart()); 2235 for (BitcodeFile *file : ctx.lazyBitcodeFiles) 2236 bufs.insert(file->mb.getBufferStart()); 2237 for (MemoryBuffer &mb : llvm::make_pointee_range(ctx.memoryBuffers)) 2238 if (bufs.count(mb.getBufferStart())) 2239 mb.dontNeedIfMmap(); 2240} 2241 2242// This function is where all the optimizations of link-time 2243// optimization takes place. When LTO is in use, some input files are 2244// not in native object file format but in the LLVM bitcode format. 2245// This function compiles bitcode files into a few big native files 2246// using LLVM functions and replaces bitcode symbols with the results. 2247// Because all bitcode files that the program consists of are passed to 2248// the compiler at once, it can do a whole-program optimization. 2249template <class ELFT> 2250void LinkerDriver::compileBitcodeFiles(bool skipLinkedOutput) { 2251 llvm::TimeTraceScope timeScope("LTO"); 2252 // Compile bitcode files and replace bitcode symbols. 2253 lto.reset(new BitcodeCompiler); 2254 for (BitcodeFile *file : ctx.bitcodeFiles) 2255 lto->add(*file); 2256 2257 if (!ctx.bitcodeFiles.empty()) 2258 markBuffersAsDontNeed(skipLinkedOutput); 2259 2260 for (InputFile *file : lto->compile()) { 2261 auto *obj = cast<ObjFile<ELFT>>(file); 2262 obj->parse(/*ignoreComdats=*/true); 2263 2264 // Parse '@' in symbol names for non-relocatable output. 2265 if (!config->relocatable) 2266 for (Symbol *sym : obj->getGlobalSymbols()) 2267 if (sym->hasVersionSuffix) 2268 sym->parseSymbolVersion(); 2269 ctx.objectFiles.push_back(obj); 2270 } 2271} 2272 2273// The --wrap option is a feature to rename symbols so that you can write 2274// wrappers for existing functions. If you pass `--wrap=foo`, all 2275// occurrences of symbol `foo` are resolved to `__wrap_foo` (so, you are 2276// expected to write `__wrap_foo` function as a wrapper). The original 2277// symbol becomes accessible as `__real_foo`, so you can call that from your 2278// wrapper. 2279// 2280// This data structure is instantiated for each --wrap option. 2281struct WrappedSymbol { 2282 Symbol *sym; 2283 Symbol *real; 2284 Symbol *wrap; 2285}; 2286 2287// Handles --wrap option. 2288// 2289// This function instantiates wrapper symbols. At this point, they seem 2290// like they are not being used at all, so we explicitly set some flags so 2291// that LTO won't eliminate them. 2292static std::vector<WrappedSymbol> addWrappedSymbols(opt::InputArgList &args) { 2293 std::vector<WrappedSymbol> v; 2294 DenseSet<StringRef> seen; 2295 2296 for (auto *arg : args.filtered(OPT_wrap)) { 2297 StringRef name = arg->getValue(); 2298 if (!seen.insert(name).second) 2299 continue; 2300 2301 Symbol *sym = symtab.find(name); 2302 if (!sym) 2303 continue; 2304 2305 Symbol *wrap = 2306 addUnusedUndefined(saver().save("__wrap_" + name), sym->binding); 2307 2308 // If __real_ is referenced, pull in the symbol if it is lazy. Do this after 2309 // processing __wrap_ as that may have referenced __real_. 2310 StringRef realName = saver().save("__real_" + name); 2311 if (symtab.find(realName)) 2312 addUnusedUndefined(name, sym->binding); 2313 2314 Symbol *real = addUnusedUndefined(realName); 2315 v.push_back({sym, real, wrap}); 2316 2317 // We want to tell LTO not to inline symbols to be overwritten 2318 // because LTO doesn't know the final symbol contents after renaming. 2319 real->scriptDefined = true; 2320 sym->scriptDefined = true; 2321 2322 // If a symbol is referenced in any object file, bitcode file or shared 2323 // object, mark its redirection target (foo for __real_foo and __wrap_foo 2324 // for foo) as referenced after redirection, which will be used to tell LTO 2325 // to not eliminate the redirection target. If the object file defining the 2326 // symbol also references it, we cannot easily distinguish the case from 2327 // cases where the symbol is not referenced. Retain the redirection target 2328 // in this case because we choose to wrap symbol references regardless of 2329 // whether the symbol is defined 2330 // (https://sourceware.org/bugzilla/show_bug.cgi?id=26358). 2331 if (real->referenced || real->isDefined()) 2332 sym->referencedAfterWrap = true; 2333 if (sym->referenced || sym->isDefined()) 2334 wrap->referencedAfterWrap = true; 2335 } 2336 return v; 2337} 2338 2339static void combineVersionedSymbol(Symbol &sym, 2340 DenseMap<Symbol *, Symbol *> &map) { 2341 const char *suffix1 = sym.getVersionSuffix(); 2342 if (suffix1[0] != '@' || suffix1[1] == '@') 2343 return; 2344 2345 // Check the existing symbol foo. We have two special cases to handle: 2346 // 2347 // * There is a definition of foo@v1 and foo@@v1. 2348 // * There is a definition of foo@v1 and foo. 2349 Defined *sym2 = dyn_cast_or_null<Defined>(symtab.find(sym.getName())); 2350 if (!sym2) 2351 return; 2352 const char *suffix2 = sym2->getVersionSuffix(); 2353 if (suffix2[0] == '@' && suffix2[1] == '@' && 2354 strcmp(suffix1 + 1, suffix2 + 2) == 0) { 2355 // foo@v1 and foo@@v1 should be merged, so redirect foo@v1 to foo@@v1. 2356 map.try_emplace(&sym, sym2); 2357 // If both foo@v1 and foo@@v1 are defined and non-weak, report a 2358 // duplicate definition error. 2359 if (sym.isDefined()) { 2360 sym2->checkDuplicate(cast<Defined>(sym)); 2361 sym2->resolve(cast<Defined>(sym)); 2362 } else if (sym.isUndefined()) { 2363 sym2->resolve(cast<Undefined>(sym)); 2364 } else { 2365 sym2->resolve(cast<SharedSymbol>(sym)); 2366 } 2367 // Eliminate foo@v1 from the symbol table. 2368 sym.symbolKind = Symbol::PlaceholderKind; 2369 sym.isUsedInRegularObj = false; 2370 } else if (auto *sym1 = dyn_cast<Defined>(&sym)) { 2371 if (sym2->versionId > VER_NDX_GLOBAL 2372 ? config->versionDefinitions[sym2->versionId].name == suffix1 + 1 2373 : sym1->section == sym2->section && sym1->value == sym2->value) { 2374 // Due to an assembler design flaw, if foo is defined, .symver foo, 2375 // foo@v1 defines both foo and foo@v1. Unless foo is bound to a 2376 // different version, GNU ld makes foo@v1 canonical and eliminates 2377 // foo. Emulate its behavior, otherwise we would have foo or foo@@v1 2378 // beside foo@v1. foo@v1 and foo combining does not apply if they are 2379 // not defined in the same place. 2380 map.try_emplace(sym2, &sym); 2381 sym2->symbolKind = Symbol::PlaceholderKind; 2382 sym2->isUsedInRegularObj = false; 2383 } 2384 } 2385} 2386 2387// Do renaming for --wrap and foo@v1 by updating pointers to symbols. 2388// 2389// When this function is executed, only InputFiles and symbol table 2390// contain pointers to symbol objects. We visit them to replace pointers, 2391// so that wrapped symbols are swapped as instructed by the command line. 2392static void redirectSymbols(ArrayRef<WrappedSymbol> wrapped) { 2393 llvm::TimeTraceScope timeScope("Redirect symbols"); 2394 DenseMap<Symbol *, Symbol *> map; 2395 for (const WrappedSymbol &w : wrapped) { 2396 map[w.sym] = w.wrap; 2397 map[w.real] = w.sym; 2398 } 2399 2400 // If there are version definitions (versionDefinitions.size() > 2), enumerate 2401 // symbols with a non-default version (foo@v1) and check whether it should be 2402 // combined with foo or foo@@v1. 2403 if (config->versionDefinitions.size() > 2) 2404 for (Symbol *sym : symtab.getSymbols()) 2405 if (sym->hasVersionSuffix) 2406 combineVersionedSymbol(*sym, map); 2407 2408 if (map.empty()) 2409 return; 2410 2411 // Update pointers in input files. 2412 parallelForEach(ctx.objectFiles, [&](ELFFileBase *file) { 2413 for (Symbol *&sym : file->getMutableGlobalSymbols()) 2414 if (Symbol *s = map.lookup(sym)) 2415 sym = s; 2416 }); 2417 2418 // Update pointers in the symbol table. 2419 for (const WrappedSymbol &w : wrapped) 2420 symtab.wrap(w.sym, w.real, w.wrap); 2421} 2422 2423static void checkAndReportMissingFeature(StringRef config, uint32_t features, 2424 uint32_t mask, const Twine &report) { 2425 if (!(features & mask)) { 2426 if (config == "error") 2427 error(report); 2428 else if (config == "warning") 2429 warn(report); 2430 } 2431} 2432 2433// To enable CET (x86's hardware-assisted control flow enforcement), each 2434// source file must be compiled with -fcf-protection. Object files compiled 2435// with the flag contain feature flags indicating that they are compatible 2436// with CET. We enable the feature only when all object files are compatible 2437// with CET. 2438// 2439// This is also the case with AARCH64's BTI and PAC which use the similar 2440// GNU_PROPERTY_AARCH64_FEATURE_1_AND mechanism. 2441static uint32_t getAndFeatures() { 2442 if (config->emachine != EM_386 && config->emachine != EM_X86_64 && 2443 config->emachine != EM_AARCH64) 2444 return 0; 2445 2446 uint32_t ret = -1; 2447 for (ELFFileBase *f : ctx.objectFiles) { 2448 uint32_t features = f->andFeatures; 2449 2450 checkAndReportMissingFeature( 2451 config->zBtiReport, features, GNU_PROPERTY_AARCH64_FEATURE_1_BTI, 2452 toString(f) + ": -z bti-report: file does not have " 2453 "GNU_PROPERTY_AARCH64_FEATURE_1_BTI property"); 2454 2455 checkAndReportMissingFeature( 2456 config->zCetReport, features, GNU_PROPERTY_X86_FEATURE_1_IBT, 2457 toString(f) + ": -z cet-report: file does not have " 2458 "GNU_PROPERTY_X86_FEATURE_1_IBT property"); 2459 2460 checkAndReportMissingFeature( 2461 config->zCetReport, features, GNU_PROPERTY_X86_FEATURE_1_SHSTK, 2462 toString(f) + ": -z cet-report: file does not have " 2463 "GNU_PROPERTY_X86_FEATURE_1_SHSTK property"); 2464 2465 if (config->zForceBti && !(features & GNU_PROPERTY_AARCH64_FEATURE_1_BTI)) { 2466 features |= GNU_PROPERTY_AARCH64_FEATURE_1_BTI; 2467 if (config->zBtiReport == "none") 2468 warn(toString(f) + ": -z force-bti: file does not have " 2469 "GNU_PROPERTY_AARCH64_FEATURE_1_BTI property"); 2470 } else if (config->zForceIbt && 2471 !(features & GNU_PROPERTY_X86_FEATURE_1_IBT)) { 2472 if (config->zCetReport == "none") 2473 warn(toString(f) + ": -z force-ibt: file does not have " 2474 "GNU_PROPERTY_X86_FEATURE_1_IBT property"); 2475 features |= GNU_PROPERTY_X86_FEATURE_1_IBT; 2476 } 2477 if (config->zPacPlt && !(features & GNU_PROPERTY_AARCH64_FEATURE_1_PAC)) { 2478 warn(toString(f) + ": -z pac-plt: file does not have " 2479 "GNU_PROPERTY_AARCH64_FEATURE_1_PAC property"); 2480 features |= GNU_PROPERTY_AARCH64_FEATURE_1_PAC; 2481 } 2482 ret &= features; 2483 } 2484 2485 // Force enable Shadow Stack. 2486 if (config->zShstk) 2487 ret |= GNU_PROPERTY_X86_FEATURE_1_SHSTK; 2488 2489 return ret; 2490} 2491 2492static void initSectionsAndLocalSyms(ELFFileBase *file, bool ignoreComdats) { 2493 switch (file->ekind) { 2494 case ELF32LEKind: 2495 cast<ObjFile<ELF32LE>>(file)->initSectionsAndLocalSyms(ignoreComdats); 2496 break; 2497 case ELF32BEKind: 2498 cast<ObjFile<ELF32BE>>(file)->initSectionsAndLocalSyms(ignoreComdats); 2499 break; 2500 case ELF64LEKind: 2501 cast<ObjFile<ELF64LE>>(file)->initSectionsAndLocalSyms(ignoreComdats); 2502 break; 2503 case ELF64BEKind: 2504 cast<ObjFile<ELF64BE>>(file)->initSectionsAndLocalSyms(ignoreComdats); 2505 break; 2506 default: 2507 llvm_unreachable(""); 2508 } 2509} 2510 2511static void postParseObjectFile(ELFFileBase *file) { 2512 switch (file->ekind) { 2513 case ELF32LEKind: 2514 cast<ObjFile<ELF32LE>>(file)->postParse(); 2515 break; 2516 case ELF32BEKind: 2517 cast<ObjFile<ELF32BE>>(file)->postParse(); 2518 break; 2519 case ELF64LEKind: 2520 cast<ObjFile<ELF64LE>>(file)->postParse(); 2521 break; 2522 case ELF64BEKind: 2523 cast<ObjFile<ELF64BE>>(file)->postParse(); 2524 break; 2525 default: 2526 llvm_unreachable(""); 2527 } 2528} 2529 2530// Do actual linking. Note that when this function is called, 2531// all linker scripts have already been parsed. 2532void LinkerDriver::link(opt::InputArgList &args) { 2533 llvm::TimeTraceScope timeScope("Link", StringRef("LinkerDriver::Link")); 2534 // If a --hash-style option was not given, set to a default value, 2535 // which varies depending on the target. 2536 if (!args.hasArg(OPT_hash_style)) { 2537 if (config->emachine == EM_MIPS) 2538 config->sysvHash = true; 2539 else 2540 config->sysvHash = config->gnuHash = true; 2541 } 2542 2543 // Default output filename is "a.out" by the Unix tradition. 2544 if (config->outputFile.empty()) 2545 config->outputFile = "a.out"; 2546 2547 // Fail early if the output file or map file is not writable. If a user has a 2548 // long link, e.g. due to a large LTO link, they do not wish to run it and 2549 // find that it failed because there was a mistake in their command-line. 2550 { 2551 llvm::TimeTraceScope timeScope("Create output files"); 2552 if (auto e = tryCreateFile(config->outputFile)) 2553 error("cannot open output file " + config->outputFile + ": " + 2554 e.message()); 2555 if (auto e = tryCreateFile(config->mapFile)) 2556 error("cannot open map file " + config->mapFile + ": " + e.message()); 2557 if (auto e = tryCreateFile(config->whyExtract)) 2558 error("cannot open --why-extract= file " + config->whyExtract + ": " + 2559 e.message()); 2560 } 2561 if (errorCount()) 2562 return; 2563 2564 // Use default entry point name if no name was given via the command 2565 // line nor linker scripts. For some reason, MIPS entry point name is 2566 // different from others. 2567 config->warnMissingEntry = 2568 (!config->entry.empty() || (!config->shared && !config->relocatable)); 2569 if (config->entry.empty() && !config->relocatable) 2570 config->entry = (config->emachine == EM_MIPS) ? "__start" : "_start"; 2571 2572 // Handle --trace-symbol. 2573 for (auto *arg : args.filtered(OPT_trace_symbol)) 2574 symtab.insert(arg->getValue())->traced = true; 2575 2576 // Handle -u/--undefined before input files. If both a.a and b.so define foo, 2577 // -u foo a.a b.so will extract a.a. 2578 for (StringRef name : config->undefined) 2579 addUnusedUndefined(name)->referenced = true; 2580 2581 // Add all files to the symbol table. This will add almost all 2582 // symbols that we need to the symbol table. This process might 2583 // add files to the link, via autolinking, these files are always 2584 // appended to the Files vector. 2585 { 2586 llvm::TimeTraceScope timeScope("Parse input files"); 2587 for (size_t i = 0; i < files.size(); ++i) { 2588 llvm::TimeTraceScope timeScope("Parse input files", files[i]->getName()); 2589 parseFile(files[i]); 2590 } 2591 } 2592 2593 // Now that we have every file, we can decide if we will need a 2594 // dynamic symbol table. 2595 // We need one if we were asked to export dynamic symbols or if we are 2596 // producing a shared library. 2597 // We also need one if any shared libraries are used and for pie executables 2598 // (probably because the dynamic linker needs it). 2599 config->hasDynSymTab = 2600 !ctx.sharedFiles.empty() || config->isPic || config->exportDynamic; 2601 2602 // Some symbols (such as __ehdr_start) are defined lazily only when there 2603 // are undefined symbols for them, so we add these to trigger that logic. 2604 for (StringRef name : script->referencedSymbols) { 2605 Symbol *sym = addUnusedUndefined(name); 2606 sym->isUsedInRegularObj = true; 2607 sym->referenced = true; 2608 } 2609 2610 // Prevent LTO from removing any definition referenced by -u. 2611 for (StringRef name : config->undefined) 2612 if (Defined *sym = dyn_cast_or_null<Defined>(symtab.find(name))) 2613 sym->isUsedInRegularObj = true; 2614 2615 // If an entry symbol is in a static archive, pull out that file now. 2616 if (Symbol *sym = symtab.find(config->entry)) 2617 handleUndefined(sym, "--entry"); 2618 2619 // Handle the `--undefined-glob <pattern>` options. 2620 for (StringRef pat : args::getStrings(args, OPT_undefined_glob)) 2621 handleUndefinedGlob(pat); 2622 2623 // Mark -init and -fini symbols so that the LTO doesn't eliminate them. 2624 if (Symbol *sym = dyn_cast_or_null<Defined>(symtab.find(config->init))) 2625 sym->isUsedInRegularObj = true; 2626 if (Symbol *sym = dyn_cast_or_null<Defined>(symtab.find(config->fini))) 2627 sym->isUsedInRegularObj = true; 2628 2629 // If any of our inputs are bitcode files, the LTO code generator may create 2630 // references to certain library functions that might not be explicit in the 2631 // bitcode file's symbol table. If any of those library functions are defined 2632 // in a bitcode file in an archive member, we need to arrange to use LTO to 2633 // compile those archive members by adding them to the link beforehand. 2634 // 2635 // However, adding all libcall symbols to the link can have undesired 2636 // consequences. For example, the libgcc implementation of 2637 // __sync_val_compare_and_swap_8 on 32-bit ARM pulls in an .init_array entry 2638 // that aborts the program if the Linux kernel does not support 64-bit 2639 // atomics, which would prevent the program from running even if it does not 2640 // use 64-bit atomics. 2641 // 2642 // Therefore, we only add libcall symbols to the link before LTO if we have 2643 // to, i.e. if the symbol's definition is in bitcode. Any other required 2644 // libcall symbols will be added to the link after LTO when we add the LTO 2645 // object file to the link. 2646 if (!ctx.bitcodeFiles.empty()) 2647 for (auto *s : lto::LTO::getRuntimeLibcallSymbols()) 2648 handleLibcall(s); 2649 2650 // Archive members defining __wrap symbols may be extracted. 2651 std::vector<WrappedSymbol> wrapped = addWrappedSymbols(args); 2652 2653 // No more lazy bitcode can be extracted at this point. Do post parse work 2654 // like checking duplicate symbols. 2655 parallelForEach(ctx.objectFiles, [](ELFFileBase *file) { 2656 initSectionsAndLocalSyms(file, /*ignoreComdats=*/false); 2657 }); 2658 parallelForEach(ctx.objectFiles, postParseObjectFile); 2659 parallelForEach(ctx.bitcodeFiles, 2660 [](BitcodeFile *file) { file->postParse(); }); 2661 for (auto &it : ctx.nonPrevailingSyms) { 2662 Symbol &sym = *it.first; 2663 Undefined(sym.file, sym.getName(), sym.binding, sym.stOther, sym.type, 2664 it.second) 2665 .overwrite(sym); 2666 cast<Undefined>(sym).nonPrevailing = true; 2667 } 2668 ctx.nonPrevailingSyms.clear(); 2669 for (const DuplicateSymbol &d : ctx.duplicates) 2670 reportDuplicate(*d.sym, d.file, d.section, d.value); 2671 ctx.duplicates.clear(); 2672 2673 // Return if there were name resolution errors. 2674 if (errorCount()) 2675 return; 2676 2677 // We want to declare linker script's symbols early, 2678 // so that we can version them. 2679 // They also might be exported if referenced by DSOs. 2680 script->declareSymbols(); 2681 2682 // Handle --exclude-libs. This is before scanVersionScript() due to a 2683 // workaround for Android ndk: for a defined versioned symbol in an archive 2684 // without a version node in the version script, Android does not expect a 2685 // 'has undefined version' error in -shared --exclude-libs=ALL mode (PR36295). 2686 // GNU ld errors in this case. 2687 if (args.hasArg(OPT_exclude_libs)) 2688 excludeLibs(args); 2689 2690 // Create elfHeader early. We need a dummy section in 2691 // addReservedSymbols to mark the created symbols as not absolute. 2692 Out::elfHeader = make<OutputSection>("", 0, SHF_ALLOC); 2693 2694 // We need to create some reserved symbols such as _end. Create them. 2695 if (!config->relocatable) 2696 addReservedSymbols(); 2697 2698 // Apply version scripts. 2699 // 2700 // For a relocatable output, version scripts don't make sense, and 2701 // parsing a symbol version string (e.g. dropping "@ver1" from a symbol 2702 // name "foo@ver1") rather do harm, so we don't call this if -r is given. 2703 if (!config->relocatable) { 2704 llvm::TimeTraceScope timeScope("Process symbol versions"); 2705 symtab.scanVersionScript(); 2706 } 2707 2708 // Skip the normal linked output if some LTO options are specified. 2709 // 2710 // For --thinlto-index-only, index file creation is performed in 2711 // compileBitcodeFiles, so we are done afterwards. --plugin-opt=emit-llvm and 2712 // --plugin-opt=emit-asm create output files in bitcode or assembly code, 2713 // respectively. When only certain thinLTO modules are specified for 2714 // compilation, the intermediate object file are the expected output. 2715 const bool skipLinkedOutput = config->thinLTOIndexOnly || config->emitLLVM || 2716 config->ltoEmitAsm || 2717 !config->thinLTOModulesToCompile.empty(); 2718 2719 // Do link-time optimization if given files are LLVM bitcode files. 2720 // This compiles bitcode files into real object files. 2721 // 2722 // With this the symbol table should be complete. After this, no new names 2723 // except a few linker-synthesized ones will be added to the symbol table. 2724 const size_t numObjsBeforeLTO = ctx.objectFiles.size(); 2725 invokeELFT(compileBitcodeFiles, skipLinkedOutput); 2726 2727 // Symbol resolution finished. Report backward reference problems, 2728 // --print-archive-stats=, and --why-extract=. 2729 reportBackrefs(); 2730 writeArchiveStats(); 2731 writeWhyExtract(); 2732 if (errorCount()) 2733 return; 2734 2735 // Bail out if normal linked output is skipped due to LTO. 2736 if (skipLinkedOutput) 2737 return; 2738 2739 // compileBitcodeFiles may have produced lto.tmp object files. After this, no 2740 // more file will be added. 2741 auto newObjectFiles = ArrayRef(ctx.objectFiles).slice(numObjsBeforeLTO); 2742 parallelForEach(newObjectFiles, [](ELFFileBase *file) { 2743 initSectionsAndLocalSyms(file, /*ignoreComdats=*/true); 2744 }); 2745 parallelForEach(newObjectFiles, postParseObjectFile); 2746 for (const DuplicateSymbol &d : ctx.duplicates) 2747 reportDuplicate(*d.sym, d.file, d.section, d.value); 2748 2749 // Handle --exclude-libs again because lto.tmp may reference additional 2750 // libcalls symbols defined in an excluded archive. This may override 2751 // versionId set by scanVersionScript(). 2752 if (args.hasArg(OPT_exclude_libs)) 2753 excludeLibs(args); 2754 2755 // Apply symbol renames for --wrap and combine foo@v1 and foo@@v1. 2756 redirectSymbols(wrapped); 2757 2758 // Replace common symbols with regular symbols. 2759 replaceCommonSymbols(); 2760 2761 { 2762 llvm::TimeTraceScope timeScope("Aggregate sections"); 2763 // Now that we have a complete list of input files. 2764 // Beyond this point, no new files are added. 2765 // Aggregate all input sections into one place. 2766 for (InputFile *f : ctx.objectFiles) { 2767 for (InputSectionBase *s : f->getSections()) { 2768 if (!s || s == &InputSection::discarded) 2769 continue; 2770 if (LLVM_UNLIKELY(isa<EhInputSection>(s))) 2771 ctx.ehInputSections.push_back(cast<EhInputSection>(s)); 2772 else 2773 ctx.inputSections.push_back(s); 2774 } 2775 } 2776 for (BinaryFile *f : ctx.binaryFiles) 2777 for (InputSectionBase *s : f->getSections()) 2778 ctx.inputSections.push_back(cast<InputSection>(s)); 2779 } 2780 2781 { 2782 llvm::TimeTraceScope timeScope("Strip sections"); 2783 if (ctx.hasSympart.load(std::memory_order_relaxed)) { 2784 llvm::erase_if(ctx.inputSections, [](InputSectionBase *s) { 2785 if (s->type != SHT_LLVM_SYMPART) 2786 return false; 2787 invokeELFT(readSymbolPartitionSection, s); 2788 return true; 2789 }); 2790 } 2791 // We do not want to emit debug sections if --strip-all 2792 // or --strip-debug are given. 2793 if (config->strip != StripPolicy::None) { 2794 llvm::erase_if(ctx.inputSections, [](InputSectionBase *s) { 2795 if (isDebugSection(*s)) 2796 return true; 2797 if (auto *isec = dyn_cast<InputSection>(s)) 2798 if (InputSectionBase *rel = isec->getRelocatedSection()) 2799 if (isDebugSection(*rel)) 2800 return true; 2801 2802 return false; 2803 }); 2804 } 2805 } 2806 2807 // Since we now have a complete set of input files, we can create 2808 // a .d file to record build dependencies. 2809 if (!config->dependencyFile.empty()) 2810 writeDependencyFile(); 2811 2812 // Now that the number of partitions is fixed, save a pointer to the main 2813 // partition. 2814 mainPart = &partitions[0]; 2815 2816 // Read .note.gnu.property sections from input object files which 2817 // contain a hint to tweak linker's and loader's behaviors. 2818 config->andFeatures = getAndFeatures(); 2819 2820 // The Target instance handles target-specific stuff, such as applying 2821 // relocations or writing a PLT section. It also contains target-dependent 2822 // values such as a default image base address. 2823 target = getTarget(); 2824 2825 config->eflags = target->calcEFlags(); 2826 // maxPageSize (sometimes called abi page size) is the maximum page size that 2827 // the output can be run on. For example if the OS can use 4k or 64k page 2828 // sizes then maxPageSize must be 64k for the output to be useable on both. 2829 // All important alignment decisions must use this value. 2830 config->maxPageSize = getMaxPageSize(args); 2831 // commonPageSize is the most common page size that the output will be run on. 2832 // For example if an OS can use 4k or 64k page sizes and 4k is more common 2833 // than 64k then commonPageSize is set to 4k. commonPageSize can be used for 2834 // optimizations such as DATA_SEGMENT_ALIGN in linker scripts. LLD's use of it 2835 // is limited to writing trap instructions on the last executable segment. 2836 config->commonPageSize = getCommonPageSize(args); 2837 // textAlignPageSize is the alignment page size to use when aligning PT_LOAD 2838 // sections. This is the same as maxPageSize except under -omagic, where data 2839 // sections are non-aligned (maxPageSize set to 1) but text sections are aligned 2840 // to the target page size. 2841 config->textAlignPageSize = config->omagic ? getRealMaxPageSize(args) : config->maxPageSize; 2842 2843 config->imageBase = getImageBase(args); 2844 2845 // This adds a .comment section containing a version string. 2846 if (!config->relocatable) 2847 ctx.inputSections.push_back(createCommentSection()); 2848 2849 // Split SHF_MERGE and .eh_frame sections into pieces in preparation for garbage collection. 2850 invokeELFT(splitSections); 2851 2852 // Garbage collection and removal of shared symbols from unused shared objects. 2853 invokeELFT(markLive); 2854 demoteSharedAndLazySymbols(); 2855 2856 // Make copies of any input sections that need to be copied into each 2857 // partition. 2858 copySectionsIntoPartitions(); 2859 2860 // Create synthesized sections such as .got and .plt. This is called before 2861 // processSectionCommands() so that they can be placed by SECTIONS commands. 2862 invokeELFT(createSyntheticSections); 2863 2864 // Some input sections that are used for exception handling need to be moved 2865 // into synthetic sections. Do that now so that they aren't assigned to 2866 // output sections in the usual way. 2867 if (!config->relocatable) 2868 combineEhSections(); 2869 2870 // Merge .riscv.attributes sections. 2871 if (config->emachine == EM_RISCV) 2872 mergeRISCVAttributesSections(); 2873 2874 { 2875 llvm::TimeTraceScope timeScope("Assign sections"); 2876 2877 // Create output sections described by SECTIONS commands. 2878 script->processSectionCommands(); 2879 2880 // Linker scripts control how input sections are assigned to output 2881 // sections. Input sections that were not handled by scripts are called 2882 // "orphans", and they are assigned to output sections by the default rule. 2883 // Process that. 2884 script->addOrphanSections(); 2885 } 2886 2887 { 2888 llvm::TimeTraceScope timeScope("Merge/finalize input sections"); 2889 2890 // Migrate InputSectionDescription::sectionBases to sections. This includes 2891 // merging MergeInputSections into a single MergeSyntheticSection. From this 2892 // point onwards InputSectionDescription::sections should be used instead of 2893 // sectionBases. 2894 for (SectionCommand *cmd : script->sectionCommands) 2895 if (auto *osd = dyn_cast<OutputDesc>(cmd)) 2896 osd->osec.finalizeInputSections(); 2897 } 2898 2899 // Two input sections with different output sections should not be folded. 2900 // ICF runs after processSectionCommands() so that we know the output sections. 2901 if (config->icf != ICFLevel::None) { 2902 invokeELFT(findKeepUniqueSections, args); 2903 invokeELFT(doIcf); 2904 } 2905 2906 // Read the callgraph now that we know what was gced or icfed 2907 if (config->callGraphProfileSort) { 2908 if (auto *arg = args.getLastArg(OPT_call_graph_ordering_file)) 2909 if (std::optional<MemoryBufferRef> buffer = readFile(arg->getValue())) 2910 readCallGraph(*buffer); 2911 invokeELFT(readCallGraphsFromObjectFiles); 2912 } 2913 2914 // Write the result to the file. 2915 invokeELFT(writeResult); 2916} 2917