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