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