1 //===- Driver.cpp ---------------------------------------------------------===// 2 // 3 // The LLVM Linker 4 // 5 // This file is distributed under the University of Illinois Open Source 6 // License. See LICENSE.TXT for details. 7 // 8 //===----------------------------------------------------------------------===// 9 // 10 // The driver drives the entire linking process. It is responsible for 11 // parsing command line options and doing whatever it is instructed to do. 12 // 13 // One notable thing in the LLD's driver when compared to other linkers is 14 // that the LLD's driver is agnostic on the host operating system. 15 // Other linkers usually have implicit default values (such as a dynamic 16 // linker path or library paths) for each host OS. 17 // 18 // I don't think implicit default values are useful because they are 19 // usually explicitly specified by the compiler driver. They can even 20 // be harmful when you are doing cross-linking. Therefore, in LLD, we 21 // simply trust the compiler driver to pass all required options and 22 // don't try to make effort on our side. 23 // 24 //===----------------------------------------------------------------------===// 25 26 #include "Driver.h" 27 #include "Config.h" 28 #include "Filesystem.h" 29 #include "ICF.h" 30 #include "InputFiles.h" 31 #include "InputSection.h" 32 #include "LinkerScript.h" 33 #include "Memory.h" 34 #include "OutputSections.h" 35 #include "ScriptParser.h" 36 #include "Strings.h" 37 #include "SymbolTable.h" 38 #include "SyntheticSections.h" 39 #include "Target.h" 40 #include "Writer.h" 41 #include "lld/Common/Driver.h" 42 #include "lld/Common/ErrorHandler.h" 43 #include "lld/Common/Threads.h" 44 #include "lld/Common/Version.h" 45 #include "llvm/ADT/StringExtras.h" 46 #include "llvm/ADT/StringSwitch.h" 47 #include "llvm/Support/CommandLine.h" 48 #include "llvm/Support/Compression.h" 49 #include "llvm/Support/Path.h" 50 #include "llvm/Support/TarWriter.h" 51 #include "llvm/Support/TargetSelect.h" 52 #include "llvm/Support/raw_ostream.h" 53 #include <cstdlib> 54 #include <utility> 55 56 using namespace llvm; 57 using namespace llvm::ELF; 58 using namespace llvm::object; 59 using namespace llvm::sys; 60 61 using namespace lld; 62 using namespace lld::elf; 63 64 Configuration *elf::Config; 65 LinkerDriver *elf::Driver; 66 67 BumpPtrAllocator elf::BAlloc; 68 StringSaver elf::Saver{BAlloc}; 69 std::vector<SpecificAllocBase *> elf::SpecificAllocBase::Instances; 70 71 static void setConfigs(); 72 73 bool elf::link(ArrayRef<const char *> Args, bool CanExitEarly, 74 raw_ostream &Error) { 75 errorHandler().LogName = Args[0]; 76 errorHandler().ErrorLimitExceededMsg = 77 "too many errors emitted, stopping now (use " 78 "-error-limit=0 to see all errors)"; 79 errorHandler().ErrorOS = &Error; 80 errorHandler().ColorDiagnostics = Error.has_colors(); 81 InputSections.clear(); 82 OutputSections.clear(); 83 Tar = nullptr; 84 BinaryFiles.clear(); 85 BitcodeFiles.clear(); 86 ObjectFiles.clear(); 87 SharedFiles.clear(); 88 89 Config = make<Configuration>(); 90 Driver = make<LinkerDriver>(); 91 Script = make<LinkerScript>(); 92 Symtab = make<SymbolTable>(); 93 Config->Argv = {Args.begin(), Args.end()}; 94 95 Driver->main(Args, CanExitEarly); 96 waitForBackgroundThreads(); 97 98 // Exit immediately if we don't need to return to the caller. 99 // This saves time because the overhead of calling destructors 100 // for all globally-allocated objects is not negligible. 101 if (Config->ExitEarly) 102 exitLld(errorCount() ? 1 : 0); 103 104 freeArena(); 105 return !errorCount(); 106 } 107 108 // Parses a linker -m option. 109 static std::tuple<ELFKind, uint16_t, uint8_t> parseEmulation(StringRef Emul) { 110 uint8_t OSABI = 0; 111 StringRef S = Emul; 112 if (S.endswith("_fbsd")) { 113 S = S.drop_back(5); 114 OSABI = ELFOSABI_FREEBSD; 115 } 116 117 std::pair<ELFKind, uint16_t> Ret = 118 StringSwitch<std::pair<ELFKind, uint16_t>>(S) 119 .Cases("aarch64elf", "aarch64linux", {ELF64LEKind, EM_AARCH64}) 120 .Cases("armelf", "armelf_linux_eabi", {ELF32LEKind, EM_ARM}) 121 .Case("elf32_x86_64", {ELF32LEKind, EM_X86_64}) 122 .Cases("elf32btsmip", "elf32btsmipn32", {ELF32BEKind, EM_MIPS}) 123 .Cases("elf32ltsmip", "elf32ltsmipn32", {ELF32LEKind, EM_MIPS}) 124 .Case("elf32ppc", {ELF32BEKind, EM_PPC}) 125 .Case("elf64btsmip", {ELF64BEKind, EM_MIPS}) 126 .Case("elf64ltsmip", {ELF64LEKind, EM_MIPS}) 127 .Case("elf64ppc", {ELF64BEKind, EM_PPC64}) 128 .Cases("elf_amd64", "elf_x86_64", {ELF64LEKind, EM_X86_64}) 129 .Case("elf_i386", {ELF32LEKind, EM_386}) 130 .Case("elf_iamcu", {ELF32LEKind, EM_IAMCU}) 131 .Default({ELFNoneKind, EM_NONE}); 132 133 if (Ret.first == ELFNoneKind) 134 error("unknown emulation: " + Emul); 135 return std::make_tuple(Ret.first, Ret.second, OSABI); 136 } 137 138 // Returns slices of MB by parsing MB as an archive file. 139 // Each slice consists of a member file in the archive. 140 std::vector<std::pair<MemoryBufferRef, uint64_t>> static getArchiveMembers( 141 MemoryBufferRef MB) { 142 std::unique_ptr<Archive> File = 143 check(Archive::create(MB), 144 MB.getBufferIdentifier() + ": failed to parse archive"); 145 146 std::vector<std::pair<MemoryBufferRef, uint64_t>> V; 147 Error Err = Error::success(); 148 bool AddToTar = File->isThin() && Tar; 149 for (const ErrorOr<Archive::Child> &COrErr : File->children(Err)) { 150 Archive::Child C = 151 check(COrErr, MB.getBufferIdentifier() + 152 ": could not get the child of the archive"); 153 MemoryBufferRef MBRef = 154 check(C.getMemoryBufferRef(), 155 MB.getBufferIdentifier() + 156 ": could not get the buffer for a child of the archive"); 157 if (AddToTar) 158 Tar->append(relativeToRoot(check(C.getFullName())), MBRef.getBuffer()); 159 V.push_back(std::make_pair(MBRef, C.getChildOffset())); 160 } 161 if (Err) 162 fatal(MB.getBufferIdentifier() + ": Archive::children failed: " + 163 toString(std::move(Err))); 164 165 // Take ownership of memory buffers created for members of thin archives. 166 for (std::unique_ptr<MemoryBuffer> &MB : File->takeThinBuffers()) 167 make<std::unique_ptr<MemoryBuffer>>(std::move(MB)); 168 169 return V; 170 } 171 172 // Opens a file and create a file object. Path has to be resolved already. 173 void LinkerDriver::addFile(StringRef Path, bool WithLOption) { 174 using namespace sys::fs; 175 176 Optional<MemoryBufferRef> Buffer = readFile(Path); 177 if (!Buffer.hasValue()) 178 return; 179 MemoryBufferRef MBRef = *Buffer; 180 181 if (InBinary) { 182 Files.push_back(make<BinaryFile>(MBRef)); 183 return; 184 } 185 186 switch (identify_magic(MBRef.getBuffer())) { 187 case file_magic::unknown: 188 readLinkerScript(MBRef); 189 return; 190 case file_magic::archive: { 191 // Handle -whole-archive. 192 if (InWholeArchive) { 193 for (const auto &P : getArchiveMembers(MBRef)) 194 Files.push_back(createObjectFile(P.first, Path, P.second)); 195 return; 196 } 197 198 std::unique_ptr<Archive> File = 199 check(Archive::create(MBRef), Path + ": failed to parse archive"); 200 201 // If an archive file has no symbol table, it is likely that a user 202 // is attempting LTO and using a default ar command that doesn't 203 // understand the LLVM bitcode file. It is a pretty common error, so 204 // we'll handle it as if it had a symbol table. 205 if (!File->isEmpty() && !File->hasSymbolTable()) { 206 for (const auto &P : getArchiveMembers(MBRef)) 207 Files.push_back(make<LazyObjFile>(P.first, Path, P.second)); 208 return; 209 } 210 211 // Handle the regular case. 212 Files.push_back(make<ArchiveFile>(std::move(File))); 213 return; 214 } 215 case file_magic::elf_shared_object: 216 if (Config->Relocatable) { 217 error("attempted static link of dynamic object " + Path); 218 return; 219 } 220 221 // DSOs usually have DT_SONAME tags in their ELF headers, and the 222 // sonames are used to identify DSOs. But if they are missing, 223 // they are identified by filenames. We don't know whether the new 224 // file has a DT_SONAME or not because we haven't parsed it yet. 225 // Here, we set the default soname for the file because we might 226 // need it later. 227 // 228 // If a file was specified by -lfoo, the directory part is not 229 // significant, as a user did not specify it. This behavior is 230 // compatible with GNU. 231 Files.push_back( 232 createSharedFile(MBRef, WithLOption ? path::filename(Path) : Path)); 233 return; 234 default: 235 if (InLib) 236 Files.push_back(make<LazyObjFile>(MBRef, "", 0)); 237 else 238 Files.push_back(createObjectFile(MBRef)); 239 } 240 } 241 242 // Add a given library by searching it from input search paths. 243 void LinkerDriver::addLibrary(StringRef Name) { 244 if (Optional<std::string> Path = searchLibrary(Name)) 245 addFile(*Path, /*WithLOption=*/true); 246 else 247 error("unable to find library -l" + Name); 248 } 249 250 // This function is called on startup. We need this for LTO since 251 // LTO calls LLVM functions to compile bitcode files to native code. 252 // Technically this can be delayed until we read bitcode files, but 253 // we don't bother to do lazily because the initialization is fast. 254 static void initLLVM(opt::InputArgList &Args) { 255 InitializeAllTargets(); 256 InitializeAllTargetMCs(); 257 InitializeAllAsmPrinters(); 258 InitializeAllAsmParsers(); 259 260 // Parse and evaluate -mllvm options. 261 std::vector<const char *> V; 262 V.push_back("lld (LLVM option parsing)"); 263 for (auto *Arg : Args.filtered(OPT_mllvm)) 264 V.push_back(Arg->getValue()); 265 cl::ParseCommandLineOptions(V.size(), V.data()); 266 } 267 268 // Some command line options or some combinations of them are not allowed. 269 // This function checks for such errors. 270 static void checkOptions(opt::InputArgList &Args) { 271 // The MIPS ABI as of 2016 does not support the GNU-style symbol lookup 272 // table which is a relatively new feature. 273 if (Config->EMachine == EM_MIPS && Config->GnuHash) 274 error("the .gnu.hash section is not compatible with the MIPS target."); 275 276 if (Config->Pie && Config->Shared) 277 error("-shared and -pie may not be used together"); 278 279 if (!Config->Shared && !Config->FilterList.empty()) 280 error("-F may not be used without -shared"); 281 282 if (!Config->Shared && !Config->AuxiliaryList.empty()) 283 error("-f may not be used without -shared"); 284 285 if (Config->Relocatable) { 286 if (Config->Shared) 287 error("-r and -shared may not be used together"); 288 if (Config->GcSections) 289 error("-r and --gc-sections may not be used together"); 290 if (Config->ICF) 291 error("-r and --icf may not be used together"); 292 if (Config->Pie) 293 error("-r and -pie may not be used together"); 294 } 295 } 296 297 static int getInteger(opt::InputArgList &Args, unsigned Key, int Default) { 298 int V = Default; 299 if (auto *Arg = Args.getLastArg(Key)) { 300 StringRef S = Arg->getValue(); 301 if (!to_integer(S, V, 10)) 302 error(Arg->getSpelling() + ": number expected, but got '" + S + "'"); 303 } 304 return V; 305 } 306 307 static const char *getReproduceOption(opt::InputArgList &Args) { 308 if (auto *Arg = Args.getLastArg(OPT_reproduce)) 309 return Arg->getValue(); 310 return getenv("LLD_REPRODUCE"); 311 } 312 313 static bool hasZOption(opt::InputArgList &Args, StringRef Key) { 314 for (auto *Arg : Args.filtered(OPT_z)) 315 if (Key == Arg->getValue()) 316 return true; 317 return false; 318 } 319 320 static uint64_t getZOptionValue(opt::InputArgList &Args, StringRef Key, 321 uint64_t Default) { 322 for (auto *Arg : Args.filtered(OPT_z)) { 323 std::pair<StringRef, StringRef> KV = StringRef(Arg->getValue()).split('='); 324 if (KV.first == Key) { 325 uint64_t Result = Default; 326 if (!to_integer(KV.second, Result)) 327 error("invalid " + Key + ": " + KV.second); 328 return Result; 329 } 330 } 331 return Default; 332 } 333 334 void LinkerDriver::main(ArrayRef<const char *> ArgsArr, bool CanExitEarly) { 335 ELFOptTable Parser; 336 opt::InputArgList Args = Parser.parse(ArgsArr.slice(1)); 337 338 // Interpret this flag early because error() depends on them. 339 errorHandler().ErrorLimit = getInteger(Args, OPT_error_limit, 20); 340 341 // Handle -help 342 if (Args.hasArg(OPT_help)) { 343 printHelp(ArgsArr[0]); 344 return; 345 } 346 347 // Handle -v or -version. 348 // 349 // A note about "compatible with GNU linkers" message: this is a hack for 350 // scripts generated by GNU Libtool 2.4.6 (released in February 2014 and 351 // still the newest version in March 2017) or earlier to recognize LLD as 352 // a GNU compatible linker. As long as an output for the -v option 353 // contains "GNU" or "with BFD", they recognize us as GNU-compatible. 354 // 355 // This is somewhat ugly hack, but in reality, we had no choice other 356 // than doing this. Considering the very long release cycle of Libtool, 357 // it is not easy to improve it to recognize LLD as a GNU compatible 358 // linker in a timely manner. Even if we can make it, there are still a 359 // lot of "configure" scripts out there that are generated by old version 360 // of Libtool. We cannot convince every software developer to migrate to 361 // the latest version and re-generate scripts. So we have this hack. 362 if (Args.hasArg(OPT_v) || Args.hasArg(OPT_version)) 363 message(getLLDVersion() + " (compatible with GNU linkers)"); 364 365 // ld.bfd always exits after printing out the version string. 366 // ld.gold proceeds if a given option is -v. Because gold's behavior 367 // is more permissive than ld.bfd, we chose what gold does here. 368 if (Args.hasArg(OPT_version)) 369 return; 370 371 Config->ExitEarly = CanExitEarly && !Args.hasArg(OPT_full_shutdown); 372 errorHandler().ExitEarly = Config->ExitEarly; 373 374 if (const char *Path = getReproduceOption(Args)) { 375 // Note that --reproduce is a debug option so you can ignore it 376 // if you are trying to understand the whole picture of the code. 377 Expected<std::unique_ptr<TarWriter>> ErrOrWriter = 378 TarWriter::create(Path, path::stem(Path)); 379 if (ErrOrWriter) { 380 Tar = ErrOrWriter->get(); 381 Tar->append("response.txt", createResponseFile(Args)); 382 Tar->append("version.txt", getLLDVersion() + "\n"); 383 make<std::unique_ptr<TarWriter>>(std::move(*ErrOrWriter)); 384 } else { 385 error(Twine("--reproduce: failed to open ") + Path + ": " + 386 toString(ErrOrWriter.takeError())); 387 } 388 } 389 390 readConfigs(Args); 391 initLLVM(Args); 392 createFiles(Args); 393 inferMachineType(); 394 setConfigs(); 395 checkOptions(Args); 396 if (errorCount()) 397 return; 398 399 switch (Config->EKind) { 400 case ELF32LEKind: 401 link<ELF32LE>(Args); 402 return; 403 case ELF32BEKind: 404 link<ELF32BE>(Args); 405 return; 406 case ELF64LEKind: 407 link<ELF64LE>(Args); 408 return; 409 case ELF64BEKind: 410 link<ELF64BE>(Args); 411 return; 412 default: 413 llvm_unreachable("unknown Config->EKind"); 414 } 415 } 416 417 static std::vector<StringRef> getArgs(opt::InputArgList &Args, int Id) { 418 std::vector<StringRef> V; 419 for (auto *Arg : Args.filtered(Id)) 420 V.push_back(Arg->getValue()); 421 return V; 422 } 423 424 static std::string getRpath(opt::InputArgList &Args) { 425 std::vector<StringRef> V = getArgs(Args, OPT_rpath); 426 return llvm::join(V.begin(), V.end(), ":"); 427 } 428 429 // Determines what we should do if there are remaining unresolved 430 // symbols after the name resolution. 431 static UnresolvedPolicy getUnresolvedSymbolPolicy(opt::InputArgList &Args) { 432 if (Args.hasArg(OPT_relocatable)) 433 return UnresolvedPolicy::IgnoreAll; 434 435 UnresolvedPolicy ErrorOrWarn = Args.hasFlag(OPT_error_unresolved_symbols, 436 OPT_warn_unresolved_symbols, true) 437 ? UnresolvedPolicy::ReportError 438 : UnresolvedPolicy::Warn; 439 440 // Process the last of -unresolved-symbols, -no-undefined or -z defs. 441 for (auto *Arg : llvm::reverse(Args)) { 442 switch (Arg->getOption().getID()) { 443 case OPT_unresolved_symbols: { 444 StringRef S = Arg->getValue(); 445 if (S == "ignore-all" || S == "ignore-in-object-files") 446 return UnresolvedPolicy::Ignore; 447 if (S == "ignore-in-shared-libs" || S == "report-all") 448 return ErrorOrWarn; 449 error("unknown --unresolved-symbols value: " + S); 450 continue; 451 } 452 case OPT_no_undefined: 453 return ErrorOrWarn; 454 case OPT_z: 455 if (StringRef(Arg->getValue()) == "defs") 456 return ErrorOrWarn; 457 continue; 458 } 459 } 460 461 // -shared implies -unresolved-symbols=ignore-all because missing 462 // symbols are likely to be resolved at runtime using other DSOs. 463 if (Config->Shared) 464 return UnresolvedPolicy::Ignore; 465 return ErrorOrWarn; 466 } 467 468 static Target2Policy getTarget2(opt::InputArgList &Args) { 469 StringRef S = Args.getLastArgValue(OPT_target2, "got-rel"); 470 if (S == "rel") 471 return Target2Policy::Rel; 472 if (S == "abs") 473 return Target2Policy::Abs; 474 if (S == "got-rel") 475 return Target2Policy::GotRel; 476 error("unknown --target2 option: " + S); 477 return Target2Policy::GotRel; 478 } 479 480 static bool isOutputFormatBinary(opt::InputArgList &Args) { 481 if (auto *Arg = Args.getLastArg(OPT_oformat)) { 482 StringRef S = Arg->getValue(); 483 if (S == "binary") 484 return true; 485 error("unknown --oformat value: " + S); 486 } 487 return false; 488 } 489 490 static DiscardPolicy getDiscard(opt::InputArgList &Args) { 491 if (Args.hasArg(OPT_relocatable)) 492 return DiscardPolicy::None; 493 494 auto *Arg = 495 Args.getLastArg(OPT_discard_all, OPT_discard_locals, OPT_discard_none); 496 if (!Arg) 497 return DiscardPolicy::Default; 498 if (Arg->getOption().getID() == OPT_discard_all) 499 return DiscardPolicy::All; 500 if (Arg->getOption().getID() == OPT_discard_locals) 501 return DiscardPolicy::Locals; 502 return DiscardPolicy::None; 503 } 504 505 static StringRef getDynamicLinker(opt::InputArgList &Args) { 506 auto *Arg = Args.getLastArg(OPT_dynamic_linker, OPT_no_dynamic_linker); 507 if (!Arg || Arg->getOption().getID() == OPT_no_dynamic_linker) 508 return ""; 509 return Arg->getValue(); 510 } 511 512 static StripPolicy getStrip(opt::InputArgList &Args) { 513 if (Args.hasArg(OPT_relocatable)) 514 return StripPolicy::None; 515 516 auto *Arg = Args.getLastArg(OPT_strip_all, OPT_strip_debug); 517 if (!Arg) 518 return StripPolicy::None; 519 if (Arg->getOption().getID() == OPT_strip_all) 520 return StripPolicy::All; 521 return StripPolicy::Debug; 522 } 523 524 static uint64_t parseSectionAddress(StringRef S, opt::Arg *Arg) { 525 uint64_t VA = 0; 526 if (S.startswith("0x")) 527 S = S.drop_front(2); 528 if (!to_integer(S, VA, 16)) 529 error("invalid argument: " + toString(Arg)); 530 return VA; 531 } 532 533 static StringMap<uint64_t> getSectionStartMap(opt::InputArgList &Args) { 534 StringMap<uint64_t> Ret; 535 for (auto *Arg : Args.filtered(OPT_section_start)) { 536 StringRef Name; 537 StringRef Addr; 538 std::tie(Name, Addr) = StringRef(Arg->getValue()).split('='); 539 Ret[Name] = parseSectionAddress(Addr, Arg); 540 } 541 542 if (auto *Arg = Args.getLastArg(OPT_Ttext)) 543 Ret[".text"] = parseSectionAddress(Arg->getValue(), Arg); 544 if (auto *Arg = Args.getLastArg(OPT_Tdata)) 545 Ret[".data"] = parseSectionAddress(Arg->getValue(), Arg); 546 if (auto *Arg = Args.getLastArg(OPT_Tbss)) 547 Ret[".bss"] = parseSectionAddress(Arg->getValue(), Arg); 548 return Ret; 549 } 550 551 static SortSectionPolicy getSortSection(opt::InputArgList &Args) { 552 StringRef S = Args.getLastArgValue(OPT_sort_section); 553 if (S == "alignment") 554 return SortSectionPolicy::Alignment; 555 if (S == "name") 556 return SortSectionPolicy::Name; 557 if (!S.empty()) 558 error("unknown --sort-section rule: " + S); 559 return SortSectionPolicy::Default; 560 } 561 562 static OrphanHandlingPolicy getOrphanHandling(opt::InputArgList &Args) { 563 StringRef S = Args.getLastArgValue(OPT_orphan_handling, "place"); 564 if (S == "warn") 565 return OrphanHandlingPolicy::Warn; 566 if (S == "error") 567 return OrphanHandlingPolicy::Error; 568 if (S != "place") 569 error("unknown --orphan-handling mode: " + S); 570 return OrphanHandlingPolicy::Place; 571 } 572 573 // Parse --build-id or --build-id=<style>. We handle "tree" as a 574 // synonym for "sha1" because all our hash functions including 575 // -build-id=sha1 are actually tree hashes for performance reasons. 576 static std::pair<BuildIdKind, std::vector<uint8_t>> 577 getBuildId(opt::InputArgList &Args) { 578 auto *Arg = Args.getLastArg(OPT_build_id, OPT_build_id_eq); 579 if (!Arg) 580 return {BuildIdKind::None, {}}; 581 582 if (Arg->getOption().getID() == OPT_build_id) 583 return {BuildIdKind::Fast, {}}; 584 585 StringRef S = Arg->getValue(); 586 if (S == "md5") 587 return {BuildIdKind::Md5, {}}; 588 if (S == "sha1" || S == "tree") 589 return {BuildIdKind::Sha1, {}}; 590 if (S == "uuid") 591 return {BuildIdKind::Uuid, {}}; 592 if (S.startswith("0x")) 593 return {BuildIdKind::Hexstring, parseHex(S.substr(2))}; 594 595 if (S != "none") 596 error("unknown --build-id style: " + S); 597 return {BuildIdKind::None, {}}; 598 } 599 600 static std::vector<StringRef> getLines(MemoryBufferRef MB) { 601 SmallVector<StringRef, 0> Arr; 602 MB.getBuffer().split(Arr, '\n'); 603 604 std::vector<StringRef> Ret; 605 for (StringRef S : Arr) { 606 S = S.trim(); 607 if (!S.empty()) 608 Ret.push_back(S); 609 } 610 return Ret; 611 } 612 613 static bool getCompressDebugSections(opt::InputArgList &Args) { 614 StringRef S = Args.getLastArgValue(OPT_compress_debug_sections, "none"); 615 if (S == "none") 616 return false; 617 if (S != "zlib") 618 error("unknown --compress-debug-sections value: " + S); 619 if (!zlib::isAvailable()) 620 error("--compress-debug-sections: zlib is not available"); 621 return true; 622 } 623 624 static int parseInt(StringRef S, opt::Arg *Arg) { 625 int V = 0; 626 if (!to_integer(S, V, 10)) 627 error(Arg->getSpelling() + ": number expected, but got '" + S + "'"); 628 return V; 629 } 630 631 // Initializes Config members by the command line options. 632 void LinkerDriver::readConfigs(opt::InputArgList &Args) { 633 Config->AllowMultipleDefinition = 634 Args.hasArg(OPT_allow_multiple_definition) || hasZOption(Args, "muldefs"); 635 Config->AuxiliaryList = getArgs(Args, OPT_auxiliary); 636 Config->Bsymbolic = Args.hasArg(OPT_Bsymbolic); 637 Config->BsymbolicFunctions = Args.hasArg(OPT_Bsymbolic_functions); 638 Config->Chroot = Args.getLastArgValue(OPT_chroot); 639 Config->CompressDebugSections = getCompressDebugSections(Args); 640 Config->DefineCommon = Args.hasFlag(OPT_define_common, OPT_no_define_common, 641 !Args.hasArg(OPT_relocatable)); 642 Config->Demangle = Args.hasFlag(OPT_demangle, OPT_no_demangle, true); 643 Config->DisableVerify = Args.hasArg(OPT_disable_verify); 644 Config->Discard = getDiscard(Args); 645 Config->DynamicLinker = getDynamicLinker(Args); 646 Config->EhFrameHdr = 647 Args.hasFlag(OPT_eh_frame_hdr, OPT_no_eh_frame_hdr, false); 648 Config->EmitRelocs = Args.hasArg(OPT_emit_relocs); 649 Config->EnableNewDtags = !Args.hasArg(OPT_disable_new_dtags); 650 Config->Entry = Args.getLastArgValue(OPT_entry); 651 Config->ExportDynamic = 652 Args.hasFlag(OPT_export_dynamic, OPT_no_export_dynamic, false); 653 errorHandler().FatalWarnings = 654 Args.hasFlag(OPT_fatal_warnings, OPT_no_fatal_warnings, false); 655 Config->FilterList = getArgs(Args, OPT_filter); 656 Config->Fini = Args.getLastArgValue(OPT_fini, "_fini"); 657 Config->GcSections = Args.hasFlag(OPT_gc_sections, OPT_no_gc_sections, false); 658 Config->GdbIndex = Args.hasFlag(OPT_gdb_index, OPT_no_gdb_index, false); 659 Config->ICF = Args.hasFlag(OPT_icf_all, OPT_icf_none, false); 660 Config->Init = Args.getLastArgValue(OPT_init, "_init"); 661 Config->LTOAAPipeline = Args.getLastArgValue(OPT_lto_aa_pipeline); 662 Config->LTONewPmPasses = Args.getLastArgValue(OPT_lto_newpm_passes); 663 Config->LTOO = getInteger(Args, OPT_lto_O, 2); 664 Config->LTOPartitions = getInteger(Args, OPT_lto_partitions, 1); 665 Config->MapFile = Args.getLastArgValue(OPT_Map); 666 Config->NoGnuUnique = Args.hasArg(OPT_no_gnu_unique); 667 Config->NoUndefinedVersion = Args.hasArg(OPT_no_undefined_version); 668 Config->NoinhibitExec = Args.hasArg(OPT_noinhibit_exec); 669 Config->Nostdlib = Args.hasArg(OPT_nostdlib); 670 Config->OFormatBinary = isOutputFormatBinary(Args); 671 Config->Omagic = Args.hasArg(OPT_omagic); 672 Config->OptRemarksFilename = Args.getLastArgValue(OPT_opt_remarks_filename); 673 Config->OptRemarksWithHotness = Args.hasArg(OPT_opt_remarks_with_hotness); 674 Config->Optimize = getInteger(Args, OPT_O, 1); 675 Config->OrphanHandling = getOrphanHandling(Args); 676 Config->OutputFile = Args.getLastArgValue(OPT_o); 677 Config->Pie = Args.hasFlag(OPT_pie, OPT_nopie, false); 678 Config->PrintGcSections = Args.hasArg(OPT_print_gc_sections); 679 Config->Rpath = getRpath(Args); 680 Config->Relocatable = Args.hasArg(OPT_relocatable); 681 Config->SaveTemps = Args.hasArg(OPT_save_temps); 682 Config->SearchPaths = getArgs(Args, OPT_library_path); 683 Config->SectionStartMap = getSectionStartMap(Args); 684 Config->Shared = Args.hasArg(OPT_shared); 685 Config->SingleRoRx = Args.hasArg(OPT_no_rosegment); 686 Config->SoName = Args.getLastArgValue(OPT_soname); 687 Config->SortSection = getSortSection(Args); 688 Config->Strip = getStrip(Args); 689 Config->Sysroot = Args.getLastArgValue(OPT_sysroot); 690 Config->Target1Rel = Args.hasFlag(OPT_target1_rel, OPT_target1_abs, false); 691 Config->Target2 = getTarget2(Args); 692 Config->ThinLTOCacheDir = Args.getLastArgValue(OPT_thinlto_cache_dir); 693 Config->ThinLTOCachePolicy = check( 694 parseCachePruningPolicy(Args.getLastArgValue(OPT_thinlto_cache_policy)), 695 "--thinlto-cache-policy: invalid cache policy"); 696 Config->ThinLTOJobs = getInteger(Args, OPT_thinlto_jobs, -1u); 697 ThreadsEnabled = Args.hasFlag(OPT_threads, OPT_no_threads, true); 698 Config->Trace = Args.hasArg(OPT_trace); 699 Config->Undefined = getArgs(Args, OPT_undefined); 700 Config->UnresolvedSymbols = getUnresolvedSymbolPolicy(Args); 701 Config->Verbose = Args.hasArg(OPT_verbose); 702 errorHandler().Verbose = Config->Verbose; 703 Config->WarnCommon = Args.hasArg(OPT_warn_common); 704 Config->ZCombreloc = !hasZOption(Args, "nocombreloc"); 705 Config->ZExecstack = hasZOption(Args, "execstack"); 706 Config->ZNocopyreloc = hasZOption(Args, "nocopyreloc"); 707 Config->ZNodelete = hasZOption(Args, "nodelete"); 708 Config->ZNodlopen = hasZOption(Args, "nodlopen"); 709 Config->ZNow = hasZOption(Args, "now"); 710 Config->ZOrigin = hasZOption(Args, "origin"); 711 Config->ZRelro = !hasZOption(Args, "norelro"); 712 Config->ZRodynamic = hasZOption(Args, "rodynamic"); 713 Config->ZStackSize = getZOptionValue(Args, "stack-size", 0); 714 Config->ZText = !hasZOption(Args, "notext"); 715 Config->ZWxneeded = hasZOption(Args, "wxneeded"); 716 717 // Parse LTO plugin-related options for compatibility with gold. 718 for (auto *Arg : Args.filtered(OPT_plugin_opt, OPT_plugin_opt_eq)) { 719 StringRef S = Arg->getValue(); 720 if (S == "disable-verify") 721 Config->DisableVerify = true; 722 else if (S == "save-temps") 723 Config->SaveTemps = true; 724 else if (S.startswith("O")) 725 Config->LTOO = parseInt(S.substr(1), Arg); 726 else if (S.startswith("lto-partitions=")) 727 Config->LTOPartitions = parseInt(S.substr(15), Arg); 728 else if (S.startswith("jobs=")) 729 Config->ThinLTOJobs = parseInt(S.substr(5), Arg); 730 else if (!S.startswith("/") && !S.startswith("-fresolution=") && 731 !S.startswith("-pass-through=") && !S.startswith("mcpu=") && 732 !S.startswith("thinlto") && S != "-function-sections" && 733 S != "-data-sections") 734 error(Arg->getSpelling() + ": unknown option: " + S); 735 } 736 737 if (Config->LTOO > 3) 738 error("invalid optimization level for LTO: " + Twine(Config->LTOO)); 739 if (Config->LTOPartitions == 0) 740 error("--lto-partitions: number of threads must be > 0"); 741 if (Config->ThinLTOJobs == 0) 742 error("--thinlto-jobs: number of threads must be > 0"); 743 744 // Parse ELF{32,64}{LE,BE} and CPU type. 745 if (auto *Arg = Args.getLastArg(OPT_m)) { 746 StringRef S = Arg->getValue(); 747 std::tie(Config->EKind, Config->EMachine, Config->OSABI) = 748 parseEmulation(S); 749 Config->MipsN32Abi = (S == "elf32btsmipn32" || S == "elf32ltsmipn32"); 750 Config->Emulation = S; 751 } 752 753 // Parse -hash-style={sysv,gnu,both}. 754 if (auto *Arg = Args.getLastArg(OPT_hash_style)) { 755 StringRef S = Arg->getValue(); 756 if (S == "sysv") 757 Config->SysvHash = true; 758 else if (S == "gnu") 759 Config->GnuHash = true; 760 else if (S == "both") 761 Config->SysvHash = Config->GnuHash = true; 762 else 763 error("unknown -hash-style: " + S); 764 } 765 766 if (Args.hasArg(OPT_print_map)) 767 Config->MapFile = "-"; 768 769 // --omagic is an option to create old-fashioned executables in which 770 // .text segments are writable. Today, the option is still in use to 771 // create special-purpose programs such as boot loaders. It doesn't 772 // make sense to create PT_GNU_RELRO for such executables. 773 if (Config->Omagic) 774 Config->ZRelro = false; 775 776 std::tie(Config->BuildId, Config->BuildIdVector) = getBuildId(Args); 777 778 if (auto *Arg = Args.getLastArg(OPT_pack_dyn_relocs_eq)) { 779 StringRef S = Arg->getValue(); 780 if (S == "android") 781 Config->AndroidPackDynRelocs = true; 782 else if (S != "none") 783 error("unknown -pack-dyn-relocs format: " + S); 784 } 785 786 if (auto *Arg = Args.getLastArg(OPT_symbol_ordering_file)) 787 if (Optional<MemoryBufferRef> Buffer = readFile(Arg->getValue())) 788 Config->SymbolOrderingFile = getLines(*Buffer); 789 790 // If --retain-symbol-file is used, we'll keep only the symbols listed in 791 // the file and discard all others. 792 if (auto *Arg = Args.getLastArg(OPT_retain_symbols_file)) { 793 Config->DefaultSymbolVersion = VER_NDX_LOCAL; 794 if (Optional<MemoryBufferRef> Buffer = readFile(Arg->getValue())) 795 for (StringRef S : getLines(*Buffer)) 796 Config->VersionScriptGlobals.push_back( 797 {S, /*IsExternCpp*/ false, /*HasWildcard*/ false}); 798 } 799 800 bool HasExportDynamic = 801 Args.hasFlag(OPT_export_dynamic, OPT_no_export_dynamic, false); 802 803 // Parses -dynamic-list and -export-dynamic-symbol. They make some 804 // symbols private. Note that -export-dynamic takes precedence over them 805 // as it says all symbols should be exported. 806 if (!HasExportDynamic) { 807 for (auto *Arg : Args.filtered(OPT_dynamic_list)) 808 if (Optional<MemoryBufferRef> Buffer = readFile(Arg->getValue())) 809 readDynamicList(*Buffer); 810 811 for (auto *Arg : Args.filtered(OPT_export_dynamic_symbol)) 812 Config->DynamicList.push_back( 813 {Arg->getValue(), /*IsExternCpp*/ false, /*HasWildcard*/ false}); 814 } 815 816 if (auto *Arg = Args.getLastArg(OPT_version_script)) 817 if (Optional<MemoryBufferRef> Buffer = readFile(Arg->getValue())) 818 readVersionScript(*Buffer); 819 } 820 821 // Some Config members do not directly correspond to any particular 822 // command line options, but computed based on other Config values. 823 // This function initialize such members. See Config.h for the details 824 // of these values. 825 static void setConfigs() { 826 ELFKind Kind = Config->EKind; 827 uint16_t Machine = Config->EMachine; 828 829 // There is an ILP32 ABI for x86-64, although it's not very popular. 830 // It is called the x32 ABI. 831 bool IsX32 = (Kind == ELF32LEKind && Machine == EM_X86_64); 832 833 Config->CopyRelocs = (Config->Relocatable || Config->EmitRelocs); 834 Config->Is64 = (Kind == ELF64LEKind || Kind == ELF64BEKind); 835 Config->IsLE = (Kind == ELF32LEKind || Kind == ELF64LEKind); 836 Config->Endianness = 837 Config->IsLE ? support::endianness::little : support::endianness::big; 838 Config->IsMips64EL = (Kind == ELF64LEKind && Machine == EM_MIPS); 839 Config->IsRela = Config->Is64 || IsX32 || Config->MipsN32Abi; 840 Config->Pic = Config->Pie || Config->Shared; 841 Config->Wordsize = Config->Is64 ? 8 : 4; 842 } 843 844 // Returns a value of "-format" option. 845 static bool getBinaryOption(StringRef S) { 846 if (S == "binary") 847 return true; 848 if (S == "elf" || S == "default") 849 return false; 850 error("unknown -format value: " + S + 851 " (supported formats: elf, default, binary)"); 852 return false; 853 } 854 855 void LinkerDriver::createFiles(opt::InputArgList &Args) { 856 for (auto *Arg : Args) { 857 switch (Arg->getOption().getUnaliasedOption().getID()) { 858 case OPT_library: 859 addLibrary(Arg->getValue()); 860 break; 861 case OPT_INPUT: 862 addFile(Arg->getValue(), /*WithLOption=*/false); 863 break; 864 case OPT_script: 865 if (Optional<MemoryBufferRef> MB = readFile(Arg->getValue())) 866 readLinkerScript(*MB); 867 break; 868 case OPT_as_needed: 869 Config->AsNeeded = true; 870 break; 871 case OPT_format: 872 InBinary = getBinaryOption(Arg->getValue()); 873 break; 874 case OPT_no_as_needed: 875 Config->AsNeeded = false; 876 break; 877 case OPT_Bstatic: 878 Config->Static = true; 879 break; 880 case OPT_Bdynamic: 881 Config->Static = false; 882 break; 883 case OPT_whole_archive: 884 InWholeArchive = true; 885 break; 886 case OPT_no_whole_archive: 887 InWholeArchive = false; 888 break; 889 case OPT_start_lib: 890 InLib = true; 891 break; 892 case OPT_end_lib: 893 InLib = false; 894 break; 895 } 896 } 897 898 if (Files.empty() && errorCount() == 0) 899 error("no input files"); 900 } 901 902 // If -m <machine_type> was not given, infer it from object files. 903 void LinkerDriver::inferMachineType() { 904 if (Config->EKind != ELFNoneKind) 905 return; 906 907 for (InputFile *F : Files) { 908 if (F->EKind == ELFNoneKind) 909 continue; 910 Config->EKind = F->EKind; 911 Config->EMachine = F->EMachine; 912 Config->OSABI = F->OSABI; 913 Config->MipsN32Abi = Config->EMachine == EM_MIPS && isMipsN32Abi(F); 914 return; 915 } 916 error("target emulation unknown: -m or at least one .o file required"); 917 } 918 919 // Parse -z max-page-size=<value>. The default value is defined by 920 // each target. 921 static uint64_t getMaxPageSize(opt::InputArgList &Args) { 922 uint64_t Val = 923 getZOptionValue(Args, "max-page-size", Target->DefaultMaxPageSize); 924 if (!isPowerOf2_64(Val)) 925 error("max-page-size: value isn't a power of 2"); 926 return Val; 927 } 928 929 // Parses -image-base option. 930 static Optional<uint64_t> getImageBase(opt::InputArgList &Args) { 931 // Because we are using "Config->MaxPageSize" here, this function has to be 932 // called after the variable is initialized. 933 auto *Arg = Args.getLastArg(OPT_image_base); 934 if (!Arg) 935 return None; 936 937 StringRef S = Arg->getValue(); 938 uint64_t V; 939 if (!to_integer(S, V)) { 940 error("-image-base: number expected, but got " + S); 941 return 0; 942 } 943 if ((V % Config->MaxPageSize) != 0) 944 warn("-image-base: address isn't multiple of page size: " + S); 945 return V; 946 } 947 948 // Parses --defsym=alias option. 949 static std::vector<std::pair<StringRef, StringRef>> 950 getDefsym(opt::InputArgList &Args) { 951 std::vector<std::pair<StringRef, StringRef>> Ret; 952 for (auto *Arg : Args.filtered(OPT_defsym)) { 953 StringRef From; 954 StringRef To; 955 std::tie(From, To) = StringRef(Arg->getValue()).split('='); 956 if (!isValidCIdentifier(To)) 957 error("--defsym: symbol name expected, but got " + To); 958 Ret.push_back({From, To}); 959 } 960 return Ret; 961 } 962 963 // Parses `--exclude-libs=lib,lib,...`. 964 // The library names may be delimited by commas or colons. 965 static DenseSet<StringRef> getExcludeLibs(opt::InputArgList &Args) { 966 DenseSet<StringRef> Ret; 967 for (auto *Arg : Args.filtered(OPT_exclude_libs)) { 968 StringRef S = Arg->getValue(); 969 for (;;) { 970 size_t Pos = S.find_first_of(",:"); 971 if (Pos == StringRef::npos) 972 break; 973 Ret.insert(S.substr(0, Pos)); 974 S = S.substr(Pos + 1); 975 } 976 Ret.insert(S); 977 } 978 return Ret; 979 } 980 981 static Optional<StringRef> getArchiveName(InputFile *File) { 982 if (isa<ArchiveFile>(File)) 983 return File->getName(); 984 if (!File->ArchiveName.empty()) 985 return File->ArchiveName; 986 return None; 987 } 988 989 // Handles the -exclude-libs option. If a static library file is specified 990 // by the -exclude-libs option, all public symbols from the archive become 991 // private unless otherwise specified by version scripts or something. 992 // A special library name "ALL" means all archive files. 993 // 994 // This is not a popular option, but some programs such as bionic libc use it. 995 template <class ELFT> 996 static void excludeLibs(opt::InputArgList &Args, ArrayRef<InputFile *> Files) { 997 DenseSet<StringRef> Libs = getExcludeLibs(Args); 998 bool All = Libs.count("ALL"); 999 1000 for (InputFile *File : Files) 1001 if (Optional<StringRef> Archive = getArchiveName(File)) 1002 if (All || Libs.count(path::filename(*Archive))) 1003 for (SymbolBody *Sym : File->getSymbols()) 1004 if (!Sym->isLocal()) 1005 Sym->VersionId = VER_NDX_LOCAL; 1006 } 1007 1008 // Do actual linking. Note that when this function is called, 1009 // all linker scripts have already been parsed. 1010 template <class ELFT> void LinkerDriver::link(opt::InputArgList &Args) { 1011 Target = getTarget(); 1012 1013 Config->MaxPageSize = getMaxPageSize(Args); 1014 Config->ImageBase = getImageBase(Args); 1015 1016 // If a -hash-style option was not given, set to a default value, 1017 // which varies depending on the target. 1018 if (!Args.hasArg(OPT_hash_style)) { 1019 if (Config->EMachine == EM_MIPS) 1020 Config->SysvHash = true; 1021 else 1022 Config->SysvHash = Config->GnuHash = true; 1023 } 1024 1025 // Default output filename is "a.out" by the Unix tradition. 1026 if (Config->OutputFile.empty()) 1027 Config->OutputFile = "a.out"; 1028 1029 // Fail early if the output file or map file is not writable. If a user has a 1030 // long link, e.g. due to a large LTO link, they do not wish to run it and 1031 // find that it failed because there was a mistake in their command-line. 1032 if (auto E = tryCreateFile(Config->OutputFile)) 1033 error("cannot open output file " + Config->OutputFile + ": " + E.message()); 1034 if (auto E = tryCreateFile(Config->MapFile)) 1035 error("cannot open map file " + Config->MapFile + ": " + E.message()); 1036 if (errorCount()) 1037 return; 1038 1039 // Use default entry point name if no name was given via the command 1040 // line nor linker scripts. For some reason, MIPS entry point name is 1041 // different from others. 1042 Config->WarnMissingEntry = 1043 (!Config->Entry.empty() || (!Config->Shared && !Config->Relocatable)); 1044 if (Config->Entry.empty() && !Config->Relocatable) 1045 Config->Entry = (Config->EMachine == EM_MIPS) ? "__start" : "_start"; 1046 1047 // Handle --trace-symbol. 1048 for (auto *Arg : Args.filtered(OPT_trace_symbol)) 1049 Symtab->trace(Arg->getValue()); 1050 1051 // Add all files to the symbol table. This will add almost all 1052 // symbols that we need to the symbol table. 1053 for (InputFile *F : Files) 1054 Symtab->addFile<ELFT>(F); 1055 1056 // Now that we have every file, we can decide if we will need a 1057 // dynamic symbol table. 1058 // We need one if we were asked to export dynamic symbols or if we are 1059 // producing a shared library. 1060 // We also need one if any shared libraries are used and for pie executables 1061 // (probably because the dynamic linker needs it). 1062 Config->HasDynSymTab = 1063 !SharedFiles.empty() || Config->Pic || Config->ExportDynamic; 1064 1065 // Some symbols (such as __ehdr_start) are defined lazily only when there 1066 // are undefined symbols for them, so we add these to trigger that logic. 1067 for (StringRef Sym : Script->ReferencedSymbols) 1068 Symtab->addUndefined<ELFT>(Sym); 1069 1070 // Handle the `--undefined <sym>` options. 1071 for (StringRef S : Config->Undefined) 1072 Symtab->fetchIfLazy<ELFT>(S); 1073 1074 // If an entry symbol is in a static archive, pull out that file now 1075 // to complete the symbol table. After this, no new names except a 1076 // few linker-synthesized ones will be added to the symbol table. 1077 Symtab->fetchIfLazy<ELFT>(Config->Entry); 1078 1079 // Return if there were name resolution errors. 1080 if (errorCount()) 1081 return; 1082 1083 // Handle undefined symbols in DSOs. 1084 Symtab->scanShlibUndefined<ELFT>(); 1085 1086 // Handle the -exclude-libs option. 1087 if (Args.hasArg(OPT_exclude_libs)) 1088 excludeLibs<ELFT>(Args, Files); 1089 1090 // Apply version scripts. 1091 Symtab->scanVersionScript(); 1092 1093 // Create wrapped symbols for -wrap option. 1094 for (auto *Arg : Args.filtered(OPT_wrap)) 1095 Symtab->addSymbolWrap<ELFT>(Arg->getValue()); 1096 1097 // Create alias symbols for -defsym option. 1098 for (std::pair<StringRef, StringRef> &Def : getDefsym(Args)) 1099 Symtab->addSymbolAlias<ELFT>(Def.first, Def.second); 1100 1101 Symtab->addCombinedLTOObject<ELFT>(); 1102 if (errorCount()) 1103 return; 1104 1105 // Apply symbol renames for -wrap and -defsym 1106 Symtab->applySymbolRenames(); 1107 1108 // Now that we have a complete list of input files. 1109 // Beyond this point, no new files are added. 1110 // Aggregate all input sections into one place. 1111 for (InputFile *F : ObjectFiles) 1112 for (InputSectionBase *S : F->getSections()) 1113 if (S && S != &InputSection::Discarded) 1114 InputSections.push_back(S); 1115 for (BinaryFile *F : BinaryFiles) 1116 for (InputSectionBase *S : F->getSections()) 1117 InputSections.push_back(cast<InputSection>(S)); 1118 1119 Config->EFlags = Target->calcEFlags(); 1120 1121 // This adds a .comment section containing a version string. We have to add it 1122 // before decompressAndMergeSections because the .comment section is a 1123 // mergeable section. 1124 if (!Config->Relocatable) 1125 InputSections.push_back(createCommentSection<ELFT>()); 1126 1127 // Create a .bss section for each common symbol and then replace the common 1128 // symbol with a DefinedRegular symbol. As a result, all common symbols are 1129 // "instantiated" as regular defined symbols, so that we don't need to care 1130 // about common symbols beyond this point. Note that if -r is given, we just 1131 // need to pass through common symbols as-is. 1132 if (Config->DefineCommon) 1133 createCommonSections<ELFT>(); 1134 1135 // Do size optimizations: garbage collection, merging of SHF_MERGE sections 1136 // and identical code folding. 1137 markLive<ELFT>(); 1138 decompressSections(); 1139 mergeSections(); 1140 if (Config->ICF) 1141 doIcf<ELFT>(); 1142 1143 // Write the result to the file. 1144 writeResult<ELFT>(); 1145 } 1146