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 #include "Driver.h" 11 #include "Config.h" 12 #include "Error.h" 13 #include "ICF.h" 14 #include "InputFiles.h" 15 #include "InputSection.h" 16 #include "LinkerScript.h" 17 #include "Strings.h" 18 #include "SymbolListFile.h" 19 #include "SymbolTable.h" 20 #include "Target.h" 21 #include "Writer.h" 22 #include "lld/Driver/Driver.h" 23 #include "llvm/ADT/StringExtras.h" 24 #include "llvm/ADT/StringSwitch.h" 25 #include "llvm/Support/TargetSelect.h" 26 #include "llvm/Support/raw_ostream.h" 27 #include <cstdlib> 28 #include <utility> 29 30 using namespace llvm; 31 using namespace llvm::ELF; 32 using namespace llvm::object; 33 using namespace llvm::sys; 34 35 using namespace lld; 36 using namespace lld::elf; 37 38 Configuration *elf::Config; 39 LinkerDriver *elf::Driver; 40 41 bool elf::link(ArrayRef<const char *> Args, raw_ostream &Error) { 42 HasError = false; 43 ErrorOS = &Error; 44 45 Configuration C; 46 LinkerDriver D; 47 ScriptConfiguration SC; 48 Config = &C; 49 Driver = &D; 50 ScriptConfig = &SC; 51 52 Driver->main(Args); 53 return !HasError; 54 } 55 56 // Parses a linker -m option. 57 static std::pair<ELFKind, uint16_t> parseEmulation(StringRef Emul) { 58 StringRef S = Emul; 59 if (S.endswith("_fbsd")) 60 S = S.drop_back(5); 61 62 std::pair<ELFKind, uint16_t> Ret = 63 StringSwitch<std::pair<ELFKind, uint16_t>>(S) 64 .Case("aarch64elf", {ELF64LEKind, EM_AARCH64}) 65 .Case("aarch64linux", {ELF64LEKind, EM_AARCH64}) 66 .Case("armelf_linux_eabi", {ELF32LEKind, EM_ARM}) 67 .Case("elf32_x86_64", {ELF32LEKind, EM_X86_64}) 68 .Case("elf32btsmip", {ELF32BEKind, EM_MIPS}) 69 .Case("elf32ltsmip", {ELF32LEKind, EM_MIPS}) 70 .Case("elf32ppc", {ELF32BEKind, EM_PPC}) 71 .Case("elf64btsmip", {ELF64BEKind, EM_MIPS}) 72 .Case("elf64ltsmip", {ELF64LEKind, EM_MIPS}) 73 .Case("elf64ppc", {ELF64BEKind, EM_PPC64}) 74 .Case("elf_amd64", {ELF64LEKind, EM_X86_64}) 75 .Case("elf_i386", {ELF32LEKind, EM_386}) 76 .Case("elf_iamcu", {ELF32LEKind, EM_IAMCU}) 77 .Case("elf_x86_64", {ELF64LEKind, EM_X86_64}) 78 .Default({ELFNoneKind, EM_NONE}); 79 80 if (Ret.first == ELFNoneKind) { 81 if (S == "i386pe" || S == "i386pep" || S == "thumb2pe") 82 error("Windows targets are not supported on the ELF frontend: " + Emul); 83 else 84 error("unknown emulation: " + Emul); 85 } 86 return Ret; 87 } 88 89 // Returns slices of MB by parsing MB as an archive file. 90 // Each slice consists of a member file in the archive. 91 std::vector<MemoryBufferRef> 92 LinkerDriver::getArchiveMembers(MemoryBufferRef MB) { 93 std::unique_ptr<Archive> File = 94 check(Archive::create(MB), "failed to parse archive"); 95 96 std::vector<MemoryBufferRef> V; 97 Error Err; 98 for (const ErrorOr<Archive::Child> &COrErr : File->children(Err)) { 99 Archive::Child C = check(COrErr, "could not get the child of the archive " + 100 File->getFileName()); 101 MemoryBufferRef MBRef = 102 check(C.getMemoryBufferRef(), 103 "could not get the buffer for a child of the archive " + 104 File->getFileName()); 105 V.push_back(MBRef); 106 } 107 if (Err) 108 Error(Err); 109 110 // Take ownership of memory buffers created for members of thin archives. 111 for (std::unique_ptr<MemoryBuffer> &MB : File->takeThinBuffers()) 112 OwningMBs.push_back(std::move(MB)); 113 114 return V; 115 } 116 117 // Opens and parses a file. Path has to be resolved already. 118 // Newly created memory buffers are owned by this driver. 119 void LinkerDriver::addFile(StringRef Path, bool KnownScript) { 120 using namespace sys::fs; 121 if (Config->Verbose) 122 outs() << Path << "\n"; 123 124 Optional<MemoryBufferRef> Buffer = readFile(Path); 125 if (!Buffer.hasValue()) 126 return; 127 MemoryBufferRef MBRef = *Buffer; 128 129 if (Config->Binary && !KnownScript) { 130 Files.push_back(make_unique<BinaryFile>(MBRef)); 131 return; 132 } 133 134 switch (identify_magic(MBRef.getBuffer())) { 135 case file_magic::unknown: 136 readLinkerScript(MBRef); 137 return; 138 case file_magic::archive: 139 if (WholeArchive) { 140 for (MemoryBufferRef MB : getArchiveMembers(MBRef)) 141 Files.push_back(createObjectFile(MB, Path)); 142 return; 143 } 144 Files.push_back(make_unique<ArchiveFile>(MBRef)); 145 return; 146 case file_magic::elf_shared_object: 147 if (Config->Relocatable) { 148 error("attempted static link of dynamic object " + Path); 149 return; 150 } 151 Files.push_back(createSharedFile(MBRef)); 152 return; 153 default: 154 if (InLib) 155 Files.push_back(make_unique<LazyObjectFile>(MBRef)); 156 else 157 Files.push_back(createObjectFile(MBRef)); 158 } 159 } 160 161 Optional<MemoryBufferRef> LinkerDriver::readFile(StringRef Path) { 162 auto MBOrErr = MemoryBuffer::getFile(Path); 163 if (auto EC = MBOrErr.getError()) { 164 error(EC, "cannot open " + Path); 165 return None; 166 } 167 std::unique_ptr<MemoryBuffer> &MB = *MBOrErr; 168 MemoryBufferRef MBRef = MB->getMemBufferRef(); 169 OwningMBs.push_back(std::move(MB)); // take MB ownership 170 171 if (Cpio) 172 Cpio->append(relativeToRoot(Path), MBRef.getBuffer()); 173 174 return MBRef; 175 } 176 177 // Add a given library by searching it from input search paths. 178 void LinkerDriver::addLibrary(StringRef Name) { 179 std::string Path = searchLibrary(Name); 180 if (Path.empty()) 181 error("unable to find library -l" + Name); 182 else 183 addFile(Path); 184 } 185 186 // This function is called on startup. We need this for LTO since 187 // LTO calls LLVM functions to compile bitcode files to native code. 188 // Technically this can be delayed until we read bitcode files, but 189 // we don't bother to do lazily because the initialization is fast. 190 static void initLLVM(opt::InputArgList &Args) { 191 InitializeAllTargets(); 192 InitializeAllTargetMCs(); 193 InitializeAllAsmPrinters(); 194 InitializeAllAsmParsers(); 195 196 // This is a flag to discard all but GlobalValue names. 197 // We want to enable it by default because it saves memory. 198 // Disable it only when a developer option (-save-temps) is given. 199 Driver->Context.setDiscardValueNames(!Config->SaveTemps); 200 Driver->Context.enableDebugTypeODRUniquing(); 201 202 // Parse and evaluate -mllvm options. 203 std::vector<const char *> V; 204 V.push_back("lld (LLVM option parsing)"); 205 for (auto *Arg : Args.filtered(OPT_mllvm)) 206 V.push_back(Arg->getValue()); 207 cl::ParseCommandLineOptions(V.size(), V.data()); 208 } 209 210 // Some command line options or some combinations of them are not allowed. 211 // This function checks for such errors. 212 static void checkOptions(opt::InputArgList &Args) { 213 // The MIPS ABI as of 2016 does not support the GNU-style symbol lookup 214 // table which is a relatively new feature. 215 if (Config->EMachine == EM_MIPS && Config->GnuHash) 216 error("the .gnu.hash section is not compatible with the MIPS target."); 217 218 if (Config->EMachine == EM_AMDGPU && !Config->Entry.empty()) 219 error("-e option is not valid for AMDGPU."); 220 221 if (Config->Pie && Config->Shared) 222 error("-shared and -pie may not be used together"); 223 224 if (Config->Relocatable) { 225 if (Config->Shared) 226 error("-r and -shared may not be used together"); 227 if (Config->GcSections) 228 error("-r and --gc-sections may not be used together"); 229 if (Config->ICF) 230 error("-r and --icf may not be used together"); 231 if (Config->Pie) 232 error("-r and -pie may not be used together"); 233 } 234 } 235 236 static StringRef 237 getString(opt::InputArgList &Args, unsigned Key, StringRef Default = "") { 238 if (auto *Arg = Args.getLastArg(Key)) 239 return Arg->getValue(); 240 return Default; 241 } 242 243 static int getInteger(opt::InputArgList &Args, unsigned Key, int Default) { 244 int V = Default; 245 if (auto *Arg = Args.getLastArg(Key)) { 246 StringRef S = Arg->getValue(); 247 if (S.getAsInteger(10, V)) 248 error(Arg->getSpelling() + ": number expected, but got " + S); 249 } 250 return V; 251 } 252 253 static const char *getReproduceOption(opt::InputArgList &Args) { 254 if (auto *Arg = Args.getLastArg(OPT_reproduce)) 255 return Arg->getValue(); 256 return getenv("LLD_REPRODUCE"); 257 } 258 259 static bool hasZOption(opt::InputArgList &Args, StringRef Key) { 260 for (auto *Arg : Args.filtered(OPT_z)) 261 if (Key == Arg->getValue()) 262 return true; 263 return false; 264 } 265 266 static Optional<StringRef> 267 getZOptionValue(opt::InputArgList &Args, StringRef Key) { 268 for (auto *Arg : Args.filtered(OPT_z)) { 269 StringRef Value = Arg->getValue(); 270 size_t Pos = Value.find("="); 271 if (Pos != StringRef::npos && Key == Value.substr(0, Pos)) 272 return Value.substr(Pos + 1); 273 } 274 return None; 275 } 276 277 void LinkerDriver::main(ArrayRef<const char *> ArgsArr) { 278 ELFOptTable Parser; 279 opt::InputArgList Args = Parser.parse(ArgsArr.slice(1)); 280 if (Args.hasArg(OPT_help)) { 281 printHelp(ArgsArr[0]); 282 return; 283 } 284 if (Args.hasArg(OPT_version)) 285 outs() << getVersionString(); 286 287 if (const char *Path = getReproduceOption(Args)) { 288 // Note that --reproduce is a debug option so you can ignore it 289 // if you are trying to understand the whole picture of the code. 290 ErrorOr<CpioFile *> F = CpioFile::create(Path); 291 if (F) { 292 Cpio.reset(*F); 293 Cpio->append("response.txt", createResponseFile(Args)); 294 Cpio->append("version.txt", getVersionString()); 295 } else 296 error(F.getError(), 297 Twine("--reproduce: failed to open ") + Path + ".cpio"); 298 } 299 300 readConfigs(Args); 301 initLLVM(Args); 302 createFiles(Args); 303 checkOptions(Args); 304 if (HasError) 305 return; 306 307 switch (Config->EKind) { 308 case ELF32LEKind: 309 link<ELF32LE>(Args); 310 return; 311 case ELF32BEKind: 312 link<ELF32BE>(Args); 313 return; 314 case ELF64LEKind: 315 link<ELF64LE>(Args); 316 return; 317 case ELF64BEKind: 318 link<ELF64BE>(Args); 319 return; 320 default: 321 error("target emulation unknown: -m or at least one .o file required"); 322 } 323 } 324 325 static UnresolvedPolicy getUnresolvedSymbolOption(opt::InputArgList &Args) { 326 if (Args.hasArg(OPT_noinhibit_exec)) 327 return UnresolvedPolicy::Warn; 328 if (Args.hasArg(OPT_no_undefined) || hasZOption(Args, "defs")) 329 return UnresolvedPolicy::NoUndef; 330 if (Config->Relocatable) 331 return UnresolvedPolicy::Ignore; 332 333 if (auto *Arg = Args.getLastArg(OPT_unresolved_symbols)) { 334 StringRef S = Arg->getValue(); 335 if (S == "ignore-all" || S == "ignore-in-object-files") 336 return UnresolvedPolicy::Ignore; 337 if (S == "ignore-in-shared-libs" || S == "report-all") 338 return UnresolvedPolicy::ReportError; 339 error("unknown --unresolved-symbols value: " + S); 340 } 341 return UnresolvedPolicy::ReportError; 342 } 343 344 static bool isOutputFormatBinary(opt::InputArgList &Args) { 345 if (auto *Arg = Args.getLastArg(OPT_oformat)) { 346 StringRef S = Arg->getValue(); 347 if (S == "binary") 348 return true; 349 error("unknown --oformat value: " + S); 350 } 351 return false; 352 } 353 354 static bool getArg(opt::InputArgList &Args, unsigned K1, unsigned K2, 355 bool Default) { 356 if (auto *Arg = Args.getLastArg(K1, K2)) 357 return Arg->getOption().getID() == K1; 358 return Default; 359 } 360 361 static DiscardPolicy getDiscardOption(opt::InputArgList &Args) { 362 auto *Arg = 363 Args.getLastArg(OPT_discard_all, OPT_discard_locals, OPT_discard_none); 364 if (!Arg) 365 return DiscardPolicy::Default; 366 if (Arg->getOption().getID() == OPT_discard_all) 367 return DiscardPolicy::All; 368 if (Arg->getOption().getID() == OPT_discard_locals) 369 return DiscardPolicy::Locals; 370 return DiscardPolicy::None; 371 } 372 373 static StripPolicy getStripOption(opt::InputArgList &Args) { 374 if (auto *Arg = Args.getLastArg(OPT_strip_all, OPT_strip_debug)) { 375 if (Arg->getOption().getID() == OPT_strip_all) 376 return StripPolicy::All; 377 return StripPolicy::Debug; 378 } 379 return StripPolicy::None; 380 } 381 382 // Initializes Config members by the command line options. 383 void LinkerDriver::readConfigs(opt::InputArgList &Args) { 384 for (auto *Arg : Args.filtered(OPT_L)) 385 Config->SearchPaths.push_back(Arg->getValue()); 386 387 std::vector<StringRef> RPaths; 388 for (auto *Arg : Args.filtered(OPT_rpath)) 389 RPaths.push_back(Arg->getValue()); 390 if (!RPaths.empty()) 391 Config->RPath = llvm::join(RPaths.begin(), RPaths.end(), ":"); 392 393 if (auto *Arg = Args.getLastArg(OPT_m)) { 394 // Parse ELF{32,64}{LE,BE} and CPU type. 395 StringRef S = Arg->getValue(); 396 std::tie(Config->EKind, Config->EMachine) = parseEmulation(S); 397 Config->Emulation = S; 398 } 399 400 Config->AllowMultipleDefinition = Args.hasArg(OPT_allow_multiple_definition); 401 Config->Bsymbolic = Args.hasArg(OPT_Bsymbolic); 402 Config->BsymbolicFunctions = Args.hasArg(OPT_Bsymbolic_functions); 403 Config->Demangle = getArg(Args, OPT_demangle, OPT_no_demangle, true); 404 Config->DisableVerify = Args.hasArg(OPT_disable_verify); 405 Config->Discard = getDiscardOption(Args); 406 Config->EhFrameHdr = Args.hasArg(OPT_eh_frame_hdr); 407 Config->EnableNewDtags = !Args.hasArg(OPT_disable_new_dtags); 408 Config->ExportDynamic = Args.hasArg(OPT_export_dynamic); 409 Config->FatalWarnings = Args.hasArg(OPT_fatal_warnings); 410 Config->GcSections = getArg(Args, OPT_gc_sections, OPT_no_gc_sections, false); 411 Config->ICF = Args.hasArg(OPT_icf); 412 Config->NoGnuUnique = Args.hasArg(OPT_no_gnu_unique); 413 Config->NoUndefinedVersion = Args.hasArg(OPT_no_undefined_version); 414 Config->Nostdlib = Args.hasArg(OPT_nostdlib); 415 Config->Pie = Args.hasArg(OPT_pie); 416 Config->PrintGcSections = Args.hasArg(OPT_print_gc_sections); 417 Config->Relocatable = Args.hasArg(OPT_relocatable); 418 Config->SaveTemps = Args.hasArg(OPT_save_temps); 419 Config->Shared = Args.hasArg(OPT_shared); 420 Config->Target1Rel = getArg(Args, OPT_target1_rel, OPT_target1_abs, false); 421 Config->Threads = Args.hasArg(OPT_threads); 422 Config->Trace = Args.hasArg(OPT_trace); 423 Config->Verbose = Args.hasArg(OPT_verbose); 424 Config->WarnCommon = Args.hasArg(OPT_warn_common); 425 426 Config->DynamicLinker = getString(Args, OPT_dynamic_linker); 427 Config->Entry = getString(Args, OPT_entry); 428 Config->Fini = getString(Args, OPT_fini, "_fini"); 429 Config->Init = getString(Args, OPT_init, "_init"); 430 Config->LtoAAPipeline = getString(Args, OPT_lto_aa_pipeline); 431 Config->LtoNewPmPasses = getString(Args, OPT_lto_newpm_passes); 432 Config->OutputFile = getString(Args, OPT_o); 433 Config->SoName = getString(Args, OPT_soname); 434 Config->Sysroot = getString(Args, OPT_sysroot); 435 436 Config->Optimize = getInteger(Args, OPT_O, 1); 437 Config->LtoO = getInteger(Args, OPT_lto_O, 2); 438 if (Config->LtoO > 3) 439 error("invalid optimization level for LTO: " + getString(Args, OPT_lto_O)); 440 Config->LtoJobs = getInteger(Args, OPT_lto_jobs, 1); 441 if (Config->LtoJobs == 0) 442 error("number of threads must be > 0"); 443 444 Config->ZCombreloc = !hasZOption(Args, "nocombreloc"); 445 Config->ZExecStack = hasZOption(Args, "execstack"); 446 Config->ZNodelete = hasZOption(Args, "nodelete"); 447 Config->ZNow = hasZOption(Args, "now"); 448 Config->ZOrigin = hasZOption(Args, "origin"); 449 Config->ZRelro = !hasZOption(Args, "norelro"); 450 451 if (!Config->Relocatable) 452 Config->Strip = getStripOption(Args); 453 454 if (Optional<StringRef> Value = getZOptionValue(Args, "stack-size")) 455 if (Value->getAsInteger(0, Config->ZStackSize)) 456 error("invalid stack size: " + *Value); 457 458 // Config->Pic is true if we are generating position-independent code. 459 Config->Pic = Config->Pie || Config->Shared; 460 461 if (auto *Arg = Args.getLastArg(OPT_hash_style)) { 462 StringRef S = Arg->getValue(); 463 if (S == "gnu") { 464 Config->GnuHash = true; 465 Config->SysvHash = false; 466 } else if (S == "both") { 467 Config->GnuHash = true; 468 } else if (S != "sysv") 469 error("unknown hash style: " + S); 470 } 471 472 // Parse --build-id or --build-id=<style>. 473 if (Args.hasArg(OPT_build_id)) 474 Config->BuildId = BuildIdKind::Fnv1; 475 if (auto *Arg = Args.getLastArg(OPT_build_id_eq)) { 476 StringRef S = Arg->getValue(); 477 if (S == "md5") { 478 Config->BuildId = BuildIdKind::Md5; 479 } else if (S == "sha1") { 480 Config->BuildId = BuildIdKind::Sha1; 481 } else if (S == "uuid") { 482 Config->BuildId = BuildIdKind::Uuid; 483 } else if (S == "none") { 484 Config->BuildId = BuildIdKind::None; 485 } else if (S.startswith("0x")) { 486 Config->BuildId = BuildIdKind::Hexstring; 487 Config->BuildIdVector = parseHex(S.substr(2)); 488 } else { 489 error("unknown --build-id style: " + S); 490 } 491 } 492 493 Config->OFormatBinary = isOutputFormatBinary(Args); 494 495 for (auto *Arg : Args.filtered(OPT_auxiliary)) 496 Config->AuxiliaryList.push_back(Arg->getValue()); 497 if (!Config->Shared && !Config->AuxiliaryList.empty()) 498 error("-f may not be used without -shared"); 499 500 for (auto *Arg : Args.filtered(OPT_undefined)) 501 Config->Undefined.push_back(Arg->getValue()); 502 503 Config->UnresolvedSymbols = getUnresolvedSymbolOption(Args); 504 505 if (auto *Arg = Args.getLastArg(OPT_dynamic_list)) 506 if (Optional<MemoryBufferRef> Buffer = readFile(Arg->getValue())) 507 parseDynamicList(*Buffer); 508 509 for (auto *Arg : Args.filtered(OPT_export_dynamic_symbol)) 510 Config->DynamicList.push_back(Arg->getValue()); 511 512 if (auto *Arg = Args.getLastArg(OPT_version_script)) 513 if (Optional<MemoryBufferRef> Buffer = readFile(Arg->getValue())) 514 readVersionScript(*Buffer); 515 } 516 517 void LinkerDriver::createFiles(opt::InputArgList &Args) { 518 for (auto *Arg : Args) { 519 switch (Arg->getOption().getID()) { 520 case OPT_l: 521 addLibrary(Arg->getValue()); 522 break; 523 case OPT_INPUT: 524 addFile(Arg->getValue()); 525 break; 526 case OPT_alias_script_T: 527 case OPT_script: 528 addFile(Arg->getValue(), true); 529 break; 530 case OPT_as_needed: 531 Config->AsNeeded = true; 532 break; 533 case OPT_format: { 534 StringRef Val = Arg->getValue(); 535 if (Val == "elf" || Val == "default") 536 Config->Binary = false; 537 else if (Val == "binary") 538 Config->Binary = true; 539 else 540 error("unknown " + Arg->getSpelling() + " format: " + Arg->getValue() + 541 " (supported formats: elf, default, binary)"); 542 break; 543 } 544 case OPT_no_as_needed: 545 Config->AsNeeded = false; 546 break; 547 case OPT_Bstatic: 548 Config->Static = true; 549 break; 550 case OPT_Bdynamic: 551 Config->Static = false; 552 break; 553 case OPT_whole_archive: 554 WholeArchive = true; 555 break; 556 case OPT_no_whole_archive: 557 WholeArchive = false; 558 break; 559 case OPT_start_lib: 560 InLib = true; 561 break; 562 case OPT_end_lib: 563 InLib = false; 564 break; 565 } 566 } 567 568 if (Files.empty() && !HasError) 569 error("no input files."); 570 571 // If -m <machine_type> was not given, infer it from object files. 572 if (Config->EKind == ELFNoneKind) { 573 for (std::unique_ptr<InputFile> &F : Files) { 574 if (F->EKind == ELFNoneKind) 575 continue; 576 Config->EKind = F->EKind; 577 Config->EMachine = F->EMachine; 578 break; 579 } 580 } 581 } 582 583 // Do actual linking. Note that when this function is called, 584 // all linker scripts have already been parsed. 585 template <class ELFT> void LinkerDriver::link(opt::InputArgList &Args) { 586 SymbolTable<ELFT> Symtab; 587 elf::Symtab<ELFT>::X = &Symtab; 588 589 std::unique_ptr<TargetInfo> TI(createTarget()); 590 Target = TI.get(); 591 LinkerScript<ELFT> LS; 592 ScriptBase = Script<ELFT>::X = &LS; 593 594 Config->Rela = ELFT::Is64Bits || Config->EMachine == EM_X86_64; 595 Config->Mips64EL = 596 (Config->EMachine == EM_MIPS && Config->EKind == ELF64LEKind); 597 598 // Default output filename is "a.out" by the Unix tradition. 599 if (Config->OutputFile.empty()) 600 Config->OutputFile = "a.out"; 601 602 // Handle --trace-symbol. 603 for (auto *Arg : Args.filtered(OPT_trace_symbol)) 604 Symtab.trace(Arg->getValue()); 605 606 // Initialize Config->ImageBase. 607 if (auto *Arg = Args.getLastArg(OPT_image_base)) { 608 StringRef S = Arg->getValue(); 609 if (S.getAsInteger(0, Config->ImageBase)) 610 error(Arg->getSpelling() + ": number expected, but got " + S); 611 else if ((Config->ImageBase % Target->PageSize) != 0) 612 warning(Arg->getSpelling() + ": address isn't multiple of page size"); 613 } else { 614 Config->ImageBase = Config->Pic ? 0 : Target->DefaultImageBase; 615 } 616 617 // Add all files to the symbol table. After this, the symbol table 618 // contains all known names except a few linker-synthesized symbols. 619 for (std::unique_ptr<InputFile> &F : Files) 620 Symtab.addFile(std::move(F)); 621 622 // Add the start symbol. 623 // It initializes either Config->Entry or Config->EntryAddr. 624 // Note that AMDGPU binaries have no entries. 625 if (!Config->Entry.empty()) { 626 // It is either "-e <addr>" or "-e <symbol>". 627 if (Config->Entry.getAsInteger(0, Config->EntryAddr)) 628 Config->EntrySym = Symtab.addUndefined(Config->Entry); 629 } else if (!Config->Shared && !Config->Relocatable && 630 Config->EMachine != EM_AMDGPU) { 631 // -e was not specified. Use the default start symbol name 632 // if it is resolvable. 633 Config->Entry = (Config->EMachine == EM_MIPS) ? "__start" : "_start"; 634 if (Symtab.find(Config->Entry)) 635 Config->EntrySym = Symtab.addUndefined(Config->Entry); 636 } 637 638 if (HasError) 639 return; // There were duplicate symbols or incompatible files 640 641 Symtab.scanUndefinedFlags(); 642 Symtab.scanShlibUndefined(); 643 Symtab.scanDynamicList(); 644 Symtab.scanVersionScript(); 645 646 Symtab.addCombinedLtoObject(); 647 if (HasError) 648 return; 649 650 for (auto *Arg : Args.filtered(OPT_wrap)) 651 Symtab.wrap(Arg->getValue()); 652 653 // Write the result to the file. 654 if (Config->GcSections) 655 markLive<ELFT>(); 656 if (Config->ICF) 657 doIcf<ELFT>(); 658 659 // MergeInputSection::splitIntoPieces needs to be called before 660 // any call of MergeInputSection::getOffset. Do that. 661 for (const std::unique_ptr<elf::ObjectFile<ELFT>> &F : 662 Symtab.getObjectFiles()) 663 for (InputSectionBase<ELFT> *S : F->getSections()) { 664 if (!S || S == &InputSection<ELFT>::Discarded || !S->Live) 665 continue; 666 if (S->Compressed) 667 S->uncompress(); 668 if (auto *MS = dyn_cast<MergeInputSection<ELFT>>(S)) 669 MS->splitIntoPieces(); 670 } 671 672 writeResult<ELFT>(); 673 } 674