1 //===- Writer.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 "Writer.h" 11 #include "AArch64ErrataFix.h" 12 #include "CallGraphSort.h" 13 #include "Config.h" 14 #include "Filesystem.h" 15 #include "LinkerScript.h" 16 #include "MapFile.h" 17 #include "OutputSections.h" 18 #include "Relocations.h" 19 #include "SymbolTable.h" 20 #include "Symbols.h" 21 #include "SyntheticSections.h" 22 #include "Target.h" 23 #include "lld/Common/Memory.h" 24 #include "lld/Common/Strings.h" 25 #include "lld/Common/Threads.h" 26 #include "llvm/ADT/StringMap.h" 27 #include "llvm/ADT/StringSwitch.h" 28 #include <climits> 29 30 using namespace llvm; 31 using namespace llvm::ELF; 32 using namespace llvm::object; 33 using namespace llvm::support; 34 using namespace llvm::support::endian; 35 36 using namespace lld; 37 using namespace lld::elf; 38 39 namespace { 40 // The writer writes a SymbolTable result to a file. 41 template <class ELFT> class Writer { 42 public: 43 Writer() : Buffer(errorHandler().OutputBuffer) {} 44 typedef typename ELFT::Shdr Elf_Shdr; 45 typedef typename ELFT::Ehdr Elf_Ehdr; 46 typedef typename ELFT::Phdr Elf_Phdr; 47 48 void run(); 49 50 private: 51 void copyLocalSymbols(); 52 void addSectionSymbols(); 53 void forEachRelSec(llvm::function_ref<void(InputSectionBase &)> Fn); 54 void sortSections(); 55 void resolveShfLinkOrder(); 56 void sortInputSections(); 57 void finalizeSections(); 58 void setReservedSymbolSections(); 59 60 std::vector<PhdrEntry *> createPhdrs(); 61 void removeEmptyPTLoad(); 62 void addPtArmExid(std::vector<PhdrEntry *> &Phdrs); 63 void assignFileOffsets(); 64 void assignFileOffsetsBinary(); 65 void setPhdrs(); 66 void checkSections(); 67 void fixSectionAlignments(); 68 void openFile(); 69 void writeTrapInstr(); 70 void writeHeader(); 71 void writeSections(); 72 void writeSectionsBinary(); 73 void writeBuildId(); 74 75 std::unique_ptr<FileOutputBuffer> &Buffer; 76 77 void addRelIpltSymbols(); 78 void addStartEndSymbols(); 79 void addStartStopSymbols(OutputSection *Sec); 80 81 std::vector<PhdrEntry *> Phdrs; 82 83 uint64_t FileSize; 84 uint64_t SectionHeaderOff; 85 }; 86 } // anonymous namespace 87 88 static bool isSectionPrefix(StringRef Prefix, StringRef Name) { 89 return Name.startswith(Prefix) || Name == Prefix.drop_back(); 90 } 91 92 StringRef elf::getOutputSectionName(const InputSectionBase *S) { 93 if (Config->Relocatable) 94 return S->Name; 95 96 // This is for --emit-relocs. If .text.foo is emitted as .text.bar, we want 97 // to emit .rela.text.foo as .rela.text.bar for consistency (this is not 98 // technically required, but not doing it is odd). This code guarantees that. 99 if (auto *IS = dyn_cast<InputSection>(S)) { 100 if (InputSectionBase *Rel = IS->getRelocatedSection()) { 101 OutputSection *Out = Rel->getOutputSection(); 102 if (S->Type == SHT_RELA) 103 return Saver.save(".rela" + Out->Name); 104 return Saver.save(".rel" + Out->Name); 105 } 106 } 107 108 // This check is for -z keep-text-section-prefix. This option separates text 109 // sections with prefix ".text.hot", ".text.unlikely", ".text.startup" or 110 // ".text.exit". 111 // When enabled, this allows identifying the hot code region (.text.hot) in 112 // the final binary which can be selectively mapped to huge pages or mlocked, 113 // for instance. 114 if (Config->ZKeepTextSectionPrefix) 115 for (StringRef V : 116 {".text.hot.", ".text.unlikely.", ".text.startup.", ".text.exit."}) 117 if (isSectionPrefix(V, S->Name)) 118 return V.drop_back(); 119 120 for (StringRef V : 121 {".text.", ".rodata.", ".data.rel.ro.", ".data.", ".bss.rel.ro.", 122 ".bss.", ".init_array.", ".fini_array.", ".ctors.", ".dtors.", ".tbss.", 123 ".gcc_except_table.", ".tdata.", ".ARM.exidx.", ".ARM.extab."}) 124 if (isSectionPrefix(V, S->Name)) 125 return V.drop_back(); 126 127 // CommonSection is identified as "COMMON" in linker scripts. 128 // By default, it should go to .bss section. 129 if (S->Name == "COMMON") 130 return ".bss"; 131 132 return S->Name; 133 } 134 135 static bool needsInterpSection() { 136 return !SharedFiles.empty() && !Config->DynamicLinker.empty() && 137 Script->needsInterpSection(); 138 } 139 140 template <class ELFT> void elf::writeResult() { Writer<ELFT>().run(); } 141 142 template <class ELFT> void Writer<ELFT>::removeEmptyPTLoad() { 143 llvm::erase_if(Phdrs, [&](const PhdrEntry *P) { 144 if (P->p_type != PT_LOAD) 145 return false; 146 if (!P->FirstSec) 147 return true; 148 uint64_t Size = P->LastSec->Addr + P->LastSec->Size - P->FirstSec->Addr; 149 return Size == 0; 150 }); 151 } 152 153 template <class ELFT> static void combineEhFrameSections() { 154 for (InputSectionBase *&S : InputSections) { 155 EhInputSection *ES = dyn_cast<EhInputSection>(S); 156 if (!ES || !ES->Live) 157 continue; 158 159 In.EhFrame->addSection<ELFT>(ES); 160 S = nullptr; 161 } 162 163 std::vector<InputSectionBase *> &V = InputSections; 164 V.erase(std::remove(V.begin(), V.end(), nullptr), V.end()); 165 } 166 167 static Defined *addOptionalRegular(StringRef Name, SectionBase *Sec, 168 uint64_t Val, uint8_t StOther = STV_HIDDEN, 169 uint8_t Binding = STB_GLOBAL) { 170 Symbol *S = Symtab->find(Name); 171 if (!S || S->isDefined()) 172 return nullptr; 173 Symbol *Sym = Symtab->addDefined(Name, StOther, STT_NOTYPE, Val, 174 /*Size=*/0, Binding, Sec, 175 /*File=*/nullptr); 176 return cast<Defined>(Sym); 177 } 178 179 static Defined *addAbsolute(StringRef Name) { 180 return cast<Defined>(Symtab->addDefined(Name, STV_HIDDEN, STT_NOTYPE, 0, 0, 181 STB_GLOBAL, nullptr, nullptr)); 182 }; 183 184 // The linker is expected to define some symbols depending on 185 // the linking result. This function defines such symbols. 186 void elf::addReservedSymbols() { 187 if (Config->EMachine == EM_MIPS) { 188 // Define _gp for MIPS. st_value of _gp symbol will be updated by Writer 189 // so that it points to an absolute address which by default is relative 190 // to GOT. Default offset is 0x7ff0. 191 // See "Global Data Symbols" in Chapter 6 in the following document: 192 // ftp://www.linux-mips.org/pub/linux/mips/doc/ABI/mipsabi.pdf 193 ElfSym::MipsGp = addAbsolute("_gp"); 194 195 // On MIPS O32 ABI, _gp_disp is a magic symbol designates offset between 196 // start of function and 'gp' pointer into GOT. 197 if (Symtab->find("_gp_disp")) 198 ElfSym::MipsGpDisp = addAbsolute("_gp_disp"); 199 200 // The __gnu_local_gp is a magic symbol equal to the current value of 'gp' 201 // pointer. This symbol is used in the code generated by .cpload pseudo-op 202 // in case of using -mno-shared option. 203 // https://sourceware.org/ml/binutils/2004-12/msg00094.html 204 if (Symtab->find("__gnu_local_gp")) 205 ElfSym::MipsLocalGp = addAbsolute("__gnu_local_gp"); 206 } 207 208 // The Power Architecture 64-bit v2 ABI defines a TableOfContents (TOC) which 209 // combines the typical ELF GOT with the small data sections. It commonly 210 // includes .got .toc .sdata .sbss. The .TOC. symbol replaces both 211 // _GLOBAL_OFFSET_TABLE_ and _SDA_BASE_ from the 32-bit ABI. It is used to 212 // represent the TOC base which is offset by 0x8000 bytes from the start of 213 // the .got section. 214 ElfSym::GlobalOffsetTable = addOptionalRegular( 215 (Config->EMachine == EM_PPC64) ? ".TOC." : "_GLOBAL_OFFSET_TABLE_", 216 Out::ElfHeader, Target->GotBaseSymOff); 217 218 // __ehdr_start is the location of ELF file headers. Note that we define 219 // this symbol unconditionally even when using a linker script, which 220 // differs from the behavior implemented by GNU linker which only define 221 // this symbol if ELF headers are in the memory mapped segment. 222 addOptionalRegular("__ehdr_start", Out::ElfHeader, 0, STV_HIDDEN); 223 224 // __executable_start is not documented, but the expectation of at 225 // least the Android libc is that it points to the ELF header. 226 addOptionalRegular("__executable_start", Out::ElfHeader, 0, STV_HIDDEN); 227 228 // __dso_handle symbol is passed to cxa_finalize as a marker to identify 229 // each DSO. The address of the symbol doesn't matter as long as they are 230 // different in different DSOs, so we chose the start address of the DSO. 231 addOptionalRegular("__dso_handle", Out::ElfHeader, 0, STV_HIDDEN); 232 233 // If linker script do layout we do not need to create any standart symbols. 234 if (Script->HasSectionsCommand) 235 return; 236 237 auto Add = [](StringRef S, int64_t Pos) { 238 return addOptionalRegular(S, Out::ElfHeader, Pos, STV_DEFAULT); 239 }; 240 241 ElfSym::Bss = Add("__bss_start", 0); 242 ElfSym::End1 = Add("end", -1); 243 ElfSym::End2 = Add("_end", -1); 244 ElfSym::Etext1 = Add("etext", -1); 245 ElfSym::Etext2 = Add("_etext", -1); 246 ElfSym::Edata1 = Add("edata", -1); 247 ElfSym::Edata2 = Add("_edata", -1); 248 } 249 250 static OutputSection *findSection(StringRef Name) { 251 for (BaseCommand *Base : Script->SectionCommands) 252 if (auto *Sec = dyn_cast<OutputSection>(Base)) 253 if (Sec->Name == Name) 254 return Sec; 255 return nullptr; 256 } 257 258 // Initialize Out members. 259 template <class ELFT> static void createSyntheticSections() { 260 // Initialize all pointers with NULL. This is needed because 261 // you can call lld::elf::main more than once as a library. 262 memset(&Out::First, 0, sizeof(Out)); 263 264 auto Add = [](InputSectionBase *Sec) { InputSections.push_back(Sec); }; 265 266 In.DynStrTab = make<StringTableSection>(".dynstr", true); 267 In.Dynamic = make<DynamicSection<ELFT>>(); 268 if (Config->AndroidPackDynRelocs) { 269 In.RelaDyn = make<AndroidPackedRelocationSection<ELFT>>( 270 Config->IsRela ? ".rela.dyn" : ".rel.dyn"); 271 } else { 272 In.RelaDyn = make<RelocationSection<ELFT>>( 273 Config->IsRela ? ".rela.dyn" : ".rel.dyn", Config->ZCombreloc); 274 } 275 In.ShStrTab = make<StringTableSection>(".shstrtab", false); 276 277 Out::ProgramHeaders = make<OutputSection>("", 0, SHF_ALLOC); 278 Out::ProgramHeaders->Alignment = Config->Wordsize; 279 280 if (needsInterpSection()) { 281 In.Interp = createInterpSection(); 282 Add(In.Interp); 283 } 284 285 if (Config->Strip != StripPolicy::All) { 286 In.StrTab = make<StringTableSection>(".strtab", false); 287 In.SymTab = make<SymbolTableSection<ELFT>>(*In.StrTab); 288 In.SymTabShndx = make<SymtabShndxSection>(); 289 } 290 291 if (Config->BuildId != BuildIdKind::None) { 292 In.BuildId = make<BuildIdSection>(); 293 Add(In.BuildId); 294 } 295 296 In.Bss = make<BssSection>(".bss", 0, 1); 297 Add(In.Bss); 298 299 // If there is a SECTIONS command and a .data.rel.ro section name use name 300 // .data.rel.ro.bss so that we match in the .data.rel.ro output section. 301 // This makes sure our relro is contiguous. 302 bool HasDataRelRo = Script->HasSectionsCommand && findSection(".data.rel.ro"); 303 In.BssRelRo = 304 make<BssSection>(HasDataRelRo ? ".data.rel.ro.bss" : ".bss.rel.ro", 0, 1); 305 Add(In.BssRelRo); 306 307 // Add MIPS-specific sections. 308 if (Config->EMachine == EM_MIPS) { 309 if (!Config->Shared && Config->HasDynSymTab) { 310 In.MipsRldMap = make<MipsRldMapSection>(); 311 Add(In.MipsRldMap); 312 } 313 if (auto *Sec = MipsAbiFlagsSection<ELFT>::create()) 314 Add(Sec); 315 if (auto *Sec = MipsOptionsSection<ELFT>::create()) 316 Add(Sec); 317 if (auto *Sec = MipsReginfoSection<ELFT>::create()) 318 Add(Sec); 319 } 320 321 if (Config->HasDynSymTab) { 322 In.DynSymTab = make<SymbolTableSection<ELFT>>(*In.DynStrTab); 323 Add(In.DynSymTab); 324 325 InX<ELFT>::VerSym = make<VersionTableSection<ELFT>>(); 326 Add(InX<ELFT>::VerSym); 327 328 if (!Config->VersionDefinitions.empty()) { 329 In.VerDef = make<VersionDefinitionSection>(); 330 Add(In.VerDef); 331 } 332 333 InX<ELFT>::VerNeed = make<VersionNeedSection<ELFT>>(); 334 Add(InX<ELFT>::VerNeed); 335 336 if (Config->GnuHash) { 337 In.GnuHashTab = make<GnuHashTableSection>(); 338 Add(In.GnuHashTab); 339 } 340 341 if (Config->SysvHash) { 342 In.HashTab = make<HashTableSection>(); 343 Add(In.HashTab); 344 } 345 346 Add(In.Dynamic); 347 Add(In.DynStrTab); 348 Add(In.RelaDyn); 349 } 350 351 if (Config->RelrPackDynRelocs) { 352 In.RelrDyn = make<RelrSection<ELFT>>(); 353 Add(In.RelrDyn); 354 } 355 356 // Add .got. MIPS' .got is so different from the other archs, 357 // it has its own class. 358 if (Config->EMachine == EM_MIPS) { 359 In.MipsGot = make<MipsGotSection>(); 360 Add(In.MipsGot); 361 } else { 362 In.Got = make<GotSection>(); 363 Add(In.Got); 364 } 365 366 In.GotPlt = make<GotPltSection>(); 367 Add(In.GotPlt); 368 In.IgotPlt = make<IgotPltSection>(); 369 Add(In.IgotPlt); 370 371 if (Config->GdbIndex) { 372 In.GdbIndex = GdbIndexSection::create<ELFT>(); 373 Add(In.GdbIndex); 374 } 375 376 // We always need to add rel[a].plt to output if it has entries. 377 // Even for static linking it can contain R_[*]_IRELATIVE relocations. 378 In.RelaPlt = make<RelocationSection<ELFT>>( 379 Config->IsRela ? ".rela.plt" : ".rel.plt", false /*Sort*/); 380 Add(In.RelaPlt); 381 382 // The RelaIplt immediately follows .rel.plt (.rel.dyn for ARM) to ensure 383 // that the IRelative relocations are processed last by the dynamic loader. 384 // We cannot place the iplt section in .rel.dyn when Android relocation 385 // packing is enabled because that would cause a section type mismatch. 386 // However, because the Android dynamic loader reads .rel.plt after .rel.dyn, 387 // we can get the desired behaviour by placing the iplt section in .rel.plt. 388 In.RelaIplt = make<RelocationSection<ELFT>>( 389 (Config->EMachine == EM_ARM && !Config->AndroidPackDynRelocs) 390 ? ".rel.dyn" 391 : In.RelaPlt->Name, 392 false /*Sort*/); 393 Add(In.RelaIplt); 394 395 In.Plt = make<PltSection>(false); 396 Add(In.Plt); 397 In.Iplt = make<PltSection>(true); 398 Add(In.Iplt); 399 400 // .note.GNU-stack is always added when we are creating a re-linkable 401 // object file. Other linkers are using the presence of this marker 402 // section to control the executable-ness of the stack area, but that 403 // is irrelevant these days. Stack area should always be non-executable 404 // by default. So we emit this section unconditionally. 405 if (Config->Relocatable) 406 Add(make<GnuStackSection>()); 407 408 if (!Config->Relocatable) { 409 if (Config->EhFrameHdr) { 410 In.EhFrameHdr = make<EhFrameHeader>(); 411 Add(In.EhFrameHdr); 412 } 413 In.EhFrame = make<EhFrameSection>(); 414 Add(In.EhFrame); 415 } 416 417 if (In.SymTab) 418 Add(In.SymTab); 419 if (In.SymTabShndx) 420 Add(In.SymTabShndx); 421 Add(In.ShStrTab); 422 if (In.StrTab) 423 Add(In.StrTab); 424 425 if (Config->EMachine == EM_ARM && !Config->Relocatable) 426 // Add a sentinel to terminate .ARM.exidx. It helps an unwinder 427 // to find the exact address range of the last entry. 428 Add(make<ARMExidxSentinelSection>()); 429 } 430 431 // The main function of the writer. 432 template <class ELFT> void Writer<ELFT>::run() { 433 // Create linker-synthesized sections such as .got or .plt. 434 // Such sections are of type input section. 435 createSyntheticSections<ELFT>(); 436 437 if (!Config->Relocatable) 438 combineEhFrameSections<ELFT>(); 439 440 // We want to process linker script commands. When SECTIONS command 441 // is given we let it create sections. 442 Script->processSectionCommands(); 443 444 // Linker scripts controls how input sections are assigned to output sections. 445 // Input sections that were not handled by scripts are called "orphans", and 446 // they are assigned to output sections by the default rule. Process that. 447 Script->addOrphanSections(); 448 449 if (Config->Discard != DiscardPolicy::All) 450 copyLocalSymbols(); 451 452 if (Config->CopyRelocs) 453 addSectionSymbols(); 454 455 // Now that we have a complete set of output sections. This function 456 // completes section contents. For example, we need to add strings 457 // to the string table, and add entries to .got and .plt. 458 // finalizeSections does that. 459 finalizeSections(); 460 if (errorCount()) 461 return; 462 463 Script->assignAddresses(); 464 465 // If -compressed-debug-sections is specified, we need to compress 466 // .debug_* sections. Do it right now because it changes the size of 467 // output sections. 468 for (OutputSection *Sec : OutputSections) 469 Sec->maybeCompress<ELFT>(); 470 471 Script->allocateHeaders(Phdrs); 472 473 // Remove empty PT_LOAD to avoid causing the dynamic linker to try to mmap a 474 // 0 sized region. This has to be done late since only after assignAddresses 475 // we know the size of the sections. 476 removeEmptyPTLoad(); 477 478 if (!Config->OFormatBinary) 479 assignFileOffsets(); 480 else 481 assignFileOffsetsBinary(); 482 483 setPhdrs(); 484 485 if (Config->Relocatable) { 486 for (OutputSection *Sec : OutputSections) 487 Sec->Addr = 0; 488 } 489 490 if (Config->CheckSections) 491 checkSections(); 492 493 // It does not make sense try to open the file if we have error already. 494 if (errorCount()) 495 return; 496 // Write the result down to a file. 497 openFile(); 498 if (errorCount()) 499 return; 500 501 if (!Config->OFormatBinary) { 502 writeTrapInstr(); 503 writeHeader(); 504 writeSections(); 505 } else { 506 writeSectionsBinary(); 507 } 508 509 // Backfill .note.gnu.build-id section content. This is done at last 510 // because the content is usually a hash value of the entire output file. 511 writeBuildId(); 512 if (errorCount()) 513 return; 514 515 // Handle -Map and -cref options. 516 writeMapFile(); 517 writeCrossReferenceTable(); 518 if (errorCount()) 519 return; 520 521 if (auto E = Buffer->commit()) 522 error("failed to write to the output file: " + toString(std::move(E))); 523 } 524 525 static bool shouldKeepInSymtab(SectionBase *Sec, StringRef SymName, 526 const Symbol &B) { 527 if (B.isSection()) 528 return false; 529 530 if (Config->Discard == DiscardPolicy::None) 531 return true; 532 533 // In ELF assembly .L symbols are normally discarded by the assembler. 534 // If the assembler fails to do so, the linker discards them if 535 // * --discard-locals is used. 536 // * The symbol is in a SHF_MERGE section, which is normally the reason for 537 // the assembler keeping the .L symbol. 538 if (!SymName.startswith(".L") && !SymName.empty()) 539 return true; 540 541 if (Config->Discard == DiscardPolicy::Locals) 542 return false; 543 544 return !Sec || !(Sec->Flags & SHF_MERGE); 545 } 546 547 static bool includeInSymtab(const Symbol &B) { 548 if (!B.isLocal() && !B.IsUsedInRegularObj) 549 return false; 550 551 if (auto *D = dyn_cast<Defined>(&B)) { 552 // Always include absolute symbols. 553 SectionBase *Sec = D->Section; 554 if (!Sec) 555 return true; 556 Sec = Sec->Repl; 557 // Exclude symbols pointing to garbage-collected sections. 558 if (isa<InputSectionBase>(Sec) && !Sec->Live) 559 return false; 560 if (auto *S = dyn_cast<MergeInputSection>(Sec)) 561 if (!S->getSectionPiece(D->Value)->Live) 562 return false; 563 return true; 564 } 565 return B.Used; 566 } 567 568 // Local symbols are not in the linker's symbol table. This function scans 569 // each object file's symbol table to copy local symbols to the output. 570 template <class ELFT> void Writer<ELFT>::copyLocalSymbols() { 571 if (!In.SymTab) 572 return; 573 for (InputFile *File : ObjectFiles) { 574 ObjFile<ELFT> *F = cast<ObjFile<ELFT>>(File); 575 for (Symbol *B : F->getLocalSymbols()) { 576 if (!B->isLocal()) 577 fatal(toString(F) + 578 ": broken object: getLocalSymbols returns a non-local symbol"); 579 auto *DR = dyn_cast<Defined>(B); 580 581 // No reason to keep local undefined symbol in symtab. 582 if (!DR) 583 continue; 584 if (!includeInSymtab(*B)) 585 continue; 586 587 SectionBase *Sec = DR->Section; 588 if (!shouldKeepInSymtab(Sec, B->getName(), *B)) 589 continue; 590 In.SymTab->addSymbol(B); 591 } 592 } 593 } 594 595 template <class ELFT> void Writer<ELFT>::addSectionSymbols() { 596 // Create a section symbol for each output section so that we can represent 597 // relocations that point to the section. If we know that no relocation is 598 // referring to a section (that happens if the section is a synthetic one), we 599 // don't create a section symbol for that section. 600 for (BaseCommand *Base : Script->SectionCommands) { 601 auto *Sec = dyn_cast<OutputSection>(Base); 602 if (!Sec) 603 continue; 604 auto I = llvm::find_if(Sec->SectionCommands, [](BaseCommand *Base) { 605 if (auto *ISD = dyn_cast<InputSectionDescription>(Base)) 606 return !ISD->Sections.empty(); 607 return false; 608 }); 609 if (I == Sec->SectionCommands.end()) 610 continue; 611 InputSection *IS = cast<InputSectionDescription>(*I)->Sections[0]; 612 613 // Relocations are not using REL[A] section symbols. 614 if (IS->Type == SHT_REL || IS->Type == SHT_RELA) 615 continue; 616 617 // Unlike other synthetic sections, mergeable output sections contain data 618 // copied from input sections, and there may be a relocation pointing to its 619 // contents if -r or -emit-reloc are given. 620 if (isa<SyntheticSection>(IS) && !(IS->Flags & SHF_MERGE)) 621 continue; 622 623 auto *Sym = 624 make<Defined>(IS->File, "", STB_LOCAL, /*StOther=*/0, STT_SECTION, 625 /*Value=*/0, /*Size=*/0, IS); 626 In.SymTab->addSymbol(Sym); 627 } 628 } 629 630 // Today's loaders have a feature to make segments read-only after 631 // processing dynamic relocations to enhance security. PT_GNU_RELRO 632 // is defined for that. 633 // 634 // This function returns true if a section needs to be put into a 635 // PT_GNU_RELRO segment. 636 static bool isRelroSection(const OutputSection *Sec) { 637 if (!Config->ZRelro) 638 return false; 639 640 uint64_t Flags = Sec->Flags; 641 642 // Non-allocatable or non-writable sections don't need RELRO because 643 // they are not writable or not even mapped to memory in the first place. 644 // RELRO is for sections that are essentially read-only but need to 645 // be writable only at process startup to allow dynamic linker to 646 // apply relocations. 647 if (!(Flags & SHF_ALLOC) || !(Flags & SHF_WRITE)) 648 return false; 649 650 // Once initialized, TLS data segments are used as data templates 651 // for a thread-local storage. For each new thread, runtime 652 // allocates memory for a TLS and copy templates there. No thread 653 // are supposed to use templates directly. Thus, it can be in RELRO. 654 if (Flags & SHF_TLS) 655 return true; 656 657 // .init_array, .preinit_array and .fini_array contain pointers to 658 // functions that are executed on process startup or exit. These 659 // pointers are set by the static linker, and they are not expected 660 // to change at runtime. But if you are an attacker, you could do 661 // interesting things by manipulating pointers in .fini_array, for 662 // example. So they are put into RELRO. 663 uint32_t Type = Sec->Type; 664 if (Type == SHT_INIT_ARRAY || Type == SHT_FINI_ARRAY || 665 Type == SHT_PREINIT_ARRAY) 666 return true; 667 668 // .got contains pointers to external symbols. They are resolved by 669 // the dynamic linker when a module is loaded into memory, and after 670 // that they are not expected to change. So, it can be in RELRO. 671 if (In.Got && Sec == In.Got->getParent()) 672 return true; 673 674 if (Sec->Name.equals(".toc")) 675 return true; 676 677 // .got.plt contains pointers to external function symbols. They are 678 // by default resolved lazily, so we usually cannot put it into RELRO. 679 // However, if "-z now" is given, the lazy symbol resolution is 680 // disabled, which enables us to put it into RELRO. 681 if (Sec == In.GotPlt->getParent()) 682 return Config->ZNow; 683 684 // .dynamic section contains data for the dynamic linker, and 685 // there's no need to write to it at runtime, so it's better to put 686 // it into RELRO. 687 if (Sec == In.Dynamic->getParent()) 688 return true; 689 690 // Sections with some special names are put into RELRO. This is a 691 // bit unfortunate because section names shouldn't be significant in 692 // ELF in spirit. But in reality many linker features depend on 693 // magic section names. 694 StringRef S = Sec->Name; 695 return S == ".data.rel.ro" || S == ".bss.rel.ro" || S == ".ctors" || 696 S == ".dtors" || S == ".jcr" || S == ".eh_frame" || 697 S == ".openbsd.randomdata"; 698 } 699 700 // We compute a rank for each section. The rank indicates where the 701 // section should be placed in the file. Instead of using simple 702 // numbers (0,1,2...), we use a series of flags. One for each decision 703 // point when placing the section. 704 // Using flags has two key properties: 705 // * It is easy to check if a give branch was taken. 706 // * It is easy two see how similar two ranks are (see getRankProximity). 707 enum RankFlags { 708 RF_NOT_ADDR_SET = 1 << 18, 709 RF_NOT_INTERP = 1 << 17, 710 RF_NOT_ALLOC = 1 << 16, 711 RF_WRITE = 1 << 15, 712 RF_EXEC_WRITE = 1 << 14, 713 RF_EXEC = 1 << 13, 714 RF_RODATA = 1 << 12, 715 RF_NON_TLS_BSS = 1 << 11, 716 RF_NON_TLS_BSS_RO = 1 << 10, 717 RF_NOT_TLS = 1 << 9, 718 RF_BSS = 1 << 8, 719 RF_NOTE = 1 << 7, 720 RF_PPC_NOT_TOCBSS = 1 << 6, 721 RF_PPC_TOCL = 1 << 5, 722 RF_PPC_TOC = 1 << 4, 723 RF_PPC_GOT = 1 << 3, 724 RF_PPC_BRANCH_LT = 1 << 2, 725 RF_MIPS_GPREL = 1 << 1, 726 RF_MIPS_NOT_GOT = 1 << 0 727 }; 728 729 static unsigned getSectionRank(const OutputSection *Sec) { 730 unsigned Rank = 0; 731 732 // We want to put section specified by -T option first, so we 733 // can start assigning VA starting from them later. 734 if (Config->SectionStartMap.count(Sec->Name)) 735 return Rank; 736 Rank |= RF_NOT_ADDR_SET; 737 738 // Put .interp first because some loaders want to see that section 739 // on the first page of the executable file when loaded into memory. 740 if (Sec->Name == ".interp") 741 return Rank; 742 Rank |= RF_NOT_INTERP; 743 744 // Allocatable sections go first to reduce the total PT_LOAD size and 745 // so debug info doesn't change addresses in actual code. 746 if (!(Sec->Flags & SHF_ALLOC)) 747 return Rank | RF_NOT_ALLOC; 748 749 // Sort sections based on their access permission in the following 750 // order: R, RX, RWX, RW. This order is based on the following 751 // considerations: 752 // * Read-only sections come first such that they go in the 753 // PT_LOAD covering the program headers at the start of the file. 754 // * Read-only, executable sections come next. 755 // * Writable, executable sections follow such that .plt on 756 // architectures where it needs to be writable will be placed 757 // between .text and .data. 758 // * Writable sections come last, such that .bss lands at the very 759 // end of the last PT_LOAD. 760 bool IsExec = Sec->Flags & SHF_EXECINSTR; 761 bool IsWrite = Sec->Flags & SHF_WRITE; 762 763 if (IsExec) { 764 if (IsWrite) 765 Rank |= RF_EXEC_WRITE; 766 else 767 Rank |= RF_EXEC; 768 } else if (IsWrite) { 769 Rank |= RF_WRITE; 770 } else if (Sec->Type == SHT_PROGBITS) { 771 // Make non-executable and non-writable PROGBITS sections (e.g .rodata 772 // .eh_frame) closer to .text. They likely contain PC or GOT relative 773 // relocations and there could be relocation overflow if other huge sections 774 // (.dynstr .dynsym) were placed in between. 775 Rank |= RF_RODATA; 776 } 777 778 // If we got here we know that both A and B are in the same PT_LOAD. 779 780 bool IsTls = Sec->Flags & SHF_TLS; 781 bool IsNoBits = Sec->Type == SHT_NOBITS; 782 783 // The first requirement we have is to put (non-TLS) nobits sections last. The 784 // reason is that the only thing the dynamic linker will see about them is a 785 // p_memsz that is larger than p_filesz. Seeing that it zeros the end of the 786 // PT_LOAD, so that has to correspond to the nobits sections. 787 bool IsNonTlsNoBits = IsNoBits && !IsTls; 788 if (IsNonTlsNoBits) 789 Rank |= RF_NON_TLS_BSS; 790 791 // We place nobits RelRo sections before plain r/w ones, and non-nobits RelRo 792 // sections after r/w ones, so that the RelRo sections are contiguous. 793 bool IsRelRo = isRelroSection(Sec); 794 if (IsNonTlsNoBits && !IsRelRo) 795 Rank |= RF_NON_TLS_BSS_RO; 796 if (!IsNonTlsNoBits && IsRelRo) 797 Rank |= RF_NON_TLS_BSS_RO; 798 799 // The TLS initialization block needs to be a single contiguous block in a R/W 800 // PT_LOAD, so stick TLS sections directly before the other RelRo R/W 801 // sections. The TLS NOBITS sections are placed here as they don't take up 802 // virtual address space in the PT_LOAD. 803 if (!IsTls) 804 Rank |= RF_NOT_TLS; 805 806 // Within the TLS initialization block, the non-nobits sections need to appear 807 // first. 808 if (IsNoBits) 809 Rank |= RF_BSS; 810 811 // We create a NOTE segment for contiguous .note sections, so make 812 // them contigous if there are more than one .note section with the 813 // same attributes. 814 if (Sec->Type == SHT_NOTE) 815 Rank |= RF_NOTE; 816 817 // Some architectures have additional ordering restrictions for sections 818 // within the same PT_LOAD. 819 if (Config->EMachine == EM_PPC64) { 820 // PPC64 has a number of special SHT_PROGBITS+SHF_ALLOC+SHF_WRITE sections 821 // that we would like to make sure appear is a specific order to maximize 822 // their coverage by a single signed 16-bit offset from the TOC base 823 // pointer. Conversely, the special .tocbss section should be first among 824 // all SHT_NOBITS sections. This will put it next to the loaded special 825 // PPC64 sections (and, thus, within reach of the TOC base pointer). 826 StringRef Name = Sec->Name; 827 if (Name != ".tocbss") 828 Rank |= RF_PPC_NOT_TOCBSS; 829 830 if (Name == ".toc1") 831 Rank |= RF_PPC_TOCL; 832 833 if (Name == ".toc") 834 Rank |= RF_PPC_TOC; 835 836 if (Name == ".got") 837 Rank |= RF_PPC_GOT; 838 839 if (Name == ".branch_lt") 840 Rank |= RF_PPC_BRANCH_LT; 841 } 842 843 if (Config->EMachine == EM_MIPS) { 844 // All sections with SHF_MIPS_GPREL flag should be grouped together 845 // because data in these sections is addressable with a gp relative address. 846 if (Sec->Flags & SHF_MIPS_GPREL) 847 Rank |= RF_MIPS_GPREL; 848 849 if (Sec->Name != ".got") 850 Rank |= RF_MIPS_NOT_GOT; 851 } 852 853 return Rank; 854 } 855 856 static bool compareSections(const BaseCommand *ACmd, const BaseCommand *BCmd) { 857 const OutputSection *A = cast<OutputSection>(ACmd); 858 const OutputSection *B = cast<OutputSection>(BCmd); 859 if (A->SortRank != B->SortRank) 860 return A->SortRank < B->SortRank; 861 if (!(A->SortRank & RF_NOT_ADDR_SET)) 862 return Config->SectionStartMap.lookup(A->Name) < 863 Config->SectionStartMap.lookup(B->Name); 864 return false; 865 } 866 867 void PhdrEntry::add(OutputSection *Sec) { 868 LastSec = Sec; 869 if (!FirstSec) 870 FirstSec = Sec; 871 p_align = std::max(p_align, Sec->Alignment); 872 if (p_type == PT_LOAD) 873 Sec->PtLoad = this; 874 } 875 876 // The beginning and the ending of .rel[a].plt section are marked 877 // with __rel[a]_iplt_{start,end} symbols if it is a statically linked 878 // executable. The runtime needs these symbols in order to resolve 879 // all IRELATIVE relocs on startup. For dynamic executables, we don't 880 // need these symbols, since IRELATIVE relocs are resolved through GOT 881 // and PLT. For details, see http://www.airs.com/blog/archives/403. 882 template <class ELFT> void Writer<ELFT>::addRelIpltSymbols() { 883 if (needsInterpSection()) 884 return; 885 StringRef S = Config->IsRela ? "__rela_iplt_start" : "__rel_iplt_start"; 886 addOptionalRegular(S, In.RelaIplt, 0, STV_HIDDEN, STB_WEAK); 887 888 S = Config->IsRela ? "__rela_iplt_end" : "__rel_iplt_end"; 889 ElfSym::RelaIpltEnd = 890 addOptionalRegular(S, In.RelaIplt, 0, STV_HIDDEN, STB_WEAK); 891 } 892 893 template <class ELFT> 894 void Writer<ELFT>::forEachRelSec( 895 llvm::function_ref<void(InputSectionBase &)> Fn) { 896 // Scan all relocations. Each relocation goes through a series 897 // of tests to determine if it needs special treatment, such as 898 // creating GOT, PLT, copy relocations, etc. 899 // Note that relocations for non-alloc sections are directly 900 // processed by InputSection::relocateNonAlloc. 901 for (InputSectionBase *IS : InputSections) 902 if (IS->Live && isa<InputSection>(IS) && (IS->Flags & SHF_ALLOC)) 903 Fn(*IS); 904 for (EhInputSection *ES : In.EhFrame->Sections) 905 Fn(*ES); 906 } 907 908 // This function generates assignments for predefined symbols (e.g. _end or 909 // _etext) and inserts them into the commands sequence to be processed at the 910 // appropriate time. This ensures that the value is going to be correct by the 911 // time any references to these symbols are processed and is equivalent to 912 // defining these symbols explicitly in the linker script. 913 template <class ELFT> void Writer<ELFT>::setReservedSymbolSections() { 914 if (ElfSym::GlobalOffsetTable) { 915 // The _GLOBAL_OFFSET_TABLE_ symbol is defined by target convention usually 916 // to the start of the .got or .got.plt section. 917 InputSection *GotSection = In.GotPlt; 918 if (!Target->GotBaseSymInGotPlt) 919 GotSection = In.MipsGot ? cast<InputSection>(In.MipsGot) 920 : cast<InputSection>(In.Got); 921 ElfSym::GlobalOffsetTable->Section = GotSection; 922 } 923 924 if (ElfSym::RelaIpltEnd) 925 ElfSym::RelaIpltEnd->Value = In.RelaIplt->getSize(); 926 927 PhdrEntry *Last = nullptr; 928 PhdrEntry *LastRO = nullptr; 929 930 for (PhdrEntry *P : Phdrs) { 931 if (P->p_type != PT_LOAD) 932 continue; 933 Last = P; 934 if (!(P->p_flags & PF_W)) 935 LastRO = P; 936 } 937 938 if (LastRO) { 939 // _etext is the first location after the last read-only loadable segment. 940 if (ElfSym::Etext1) 941 ElfSym::Etext1->Section = LastRO->LastSec; 942 if (ElfSym::Etext2) 943 ElfSym::Etext2->Section = LastRO->LastSec; 944 } 945 946 if (Last) { 947 // _edata points to the end of the last mapped initialized section. 948 OutputSection *Edata = nullptr; 949 for (OutputSection *OS : OutputSections) { 950 if (OS->Type != SHT_NOBITS) 951 Edata = OS; 952 if (OS == Last->LastSec) 953 break; 954 } 955 956 if (ElfSym::Edata1) 957 ElfSym::Edata1->Section = Edata; 958 if (ElfSym::Edata2) 959 ElfSym::Edata2->Section = Edata; 960 961 // _end is the first location after the uninitialized data region. 962 if (ElfSym::End1) 963 ElfSym::End1->Section = Last->LastSec; 964 if (ElfSym::End2) 965 ElfSym::End2->Section = Last->LastSec; 966 } 967 968 if (ElfSym::Bss) 969 ElfSym::Bss->Section = findSection(".bss"); 970 971 // Setup MIPS _gp_disp/__gnu_local_gp symbols which should 972 // be equal to the _gp symbol's value. 973 if (ElfSym::MipsGp) { 974 // Find GP-relative section with the lowest address 975 // and use this address to calculate default _gp value. 976 for (OutputSection *OS : OutputSections) { 977 if (OS->Flags & SHF_MIPS_GPREL) { 978 ElfSym::MipsGp->Section = OS; 979 ElfSym::MipsGp->Value = 0x7ff0; 980 break; 981 } 982 } 983 } 984 } 985 986 // We want to find how similar two ranks are. 987 // The more branches in getSectionRank that match, the more similar they are. 988 // Since each branch corresponds to a bit flag, we can just use 989 // countLeadingZeros. 990 static int getRankProximityAux(OutputSection *A, OutputSection *B) { 991 return countLeadingZeros(A->SortRank ^ B->SortRank); 992 } 993 994 static int getRankProximity(OutputSection *A, BaseCommand *B) { 995 if (auto *Sec = dyn_cast<OutputSection>(B)) 996 return getRankProximityAux(A, Sec); 997 return -1; 998 } 999 1000 // When placing orphan sections, we want to place them after symbol assignments 1001 // so that an orphan after 1002 // begin_foo = .; 1003 // foo : { *(foo) } 1004 // end_foo = .; 1005 // doesn't break the intended meaning of the begin/end symbols. 1006 // We don't want to go over sections since findOrphanPos is the 1007 // one in charge of deciding the order of the sections. 1008 // We don't want to go over changes to '.', since doing so in 1009 // rx_sec : { *(rx_sec) } 1010 // . = ALIGN(0x1000); 1011 // /* The RW PT_LOAD starts here*/ 1012 // rw_sec : { *(rw_sec) } 1013 // would mean that the RW PT_LOAD would become unaligned. 1014 static bool shouldSkip(BaseCommand *Cmd) { 1015 if (auto *Assign = dyn_cast<SymbolAssignment>(Cmd)) 1016 return Assign->Name != "."; 1017 return false; 1018 } 1019 1020 // We want to place orphan sections so that they share as much 1021 // characteristics with their neighbors as possible. For example, if 1022 // both are rw, or both are tls. 1023 template <typename ELFT> 1024 static std::vector<BaseCommand *>::iterator 1025 findOrphanPos(std::vector<BaseCommand *>::iterator B, 1026 std::vector<BaseCommand *>::iterator E) { 1027 OutputSection *Sec = cast<OutputSection>(*E); 1028 1029 // Find the first element that has as close a rank as possible. 1030 auto I = std::max_element(B, E, [=](BaseCommand *A, BaseCommand *B) { 1031 return getRankProximity(Sec, A) < getRankProximity(Sec, B); 1032 }); 1033 if (I == E) 1034 return E; 1035 1036 // Consider all existing sections with the same proximity. 1037 int Proximity = getRankProximity(Sec, *I); 1038 for (; I != E; ++I) { 1039 auto *CurSec = dyn_cast<OutputSection>(*I); 1040 if (!CurSec) 1041 continue; 1042 if (getRankProximity(Sec, CurSec) != Proximity || 1043 Sec->SortRank < CurSec->SortRank) 1044 break; 1045 } 1046 1047 auto IsOutputSec = [](BaseCommand *Cmd) { return isa<OutputSection>(Cmd); }; 1048 auto J = std::find_if(llvm::make_reverse_iterator(I), 1049 llvm::make_reverse_iterator(B), IsOutputSec); 1050 I = J.base(); 1051 1052 // As a special case, if the orphan section is the last section, put 1053 // it at the very end, past any other commands. 1054 // This matches bfd's behavior and is convenient when the linker script fully 1055 // specifies the start of the file, but doesn't care about the end (the non 1056 // alloc sections for example). 1057 auto NextSec = std::find_if(I, E, IsOutputSec); 1058 if (NextSec == E) 1059 return E; 1060 1061 while (I != E && shouldSkip(*I)) 1062 ++I; 1063 return I; 1064 } 1065 1066 // Builds section order for handling --symbol-ordering-file. 1067 static DenseMap<const InputSectionBase *, int> buildSectionOrder() { 1068 DenseMap<const InputSectionBase *, int> SectionOrder; 1069 // Use the rarely used option -call-graph-ordering-file to sort sections. 1070 if (!Config->CallGraphProfile.empty()) 1071 return computeCallGraphProfileOrder(); 1072 1073 if (Config->SymbolOrderingFile.empty()) 1074 return SectionOrder; 1075 1076 struct SymbolOrderEntry { 1077 int Priority; 1078 bool Present; 1079 }; 1080 1081 // Build a map from symbols to their priorities. Symbols that didn't 1082 // appear in the symbol ordering file have the lowest priority 0. 1083 // All explicitly mentioned symbols have negative (higher) priorities. 1084 DenseMap<StringRef, SymbolOrderEntry> SymbolOrder; 1085 int Priority = -Config->SymbolOrderingFile.size(); 1086 for (StringRef S : Config->SymbolOrderingFile) 1087 SymbolOrder.insert({S, {Priority++, false}}); 1088 1089 // Build a map from sections to their priorities. 1090 auto AddSym = [&](Symbol &Sym) { 1091 auto It = SymbolOrder.find(Sym.getName()); 1092 if (It == SymbolOrder.end()) 1093 return; 1094 SymbolOrderEntry &Ent = It->second; 1095 Ent.Present = true; 1096 1097 warnUnorderableSymbol(&Sym); 1098 1099 if (auto *D = dyn_cast<Defined>(&Sym)) { 1100 if (auto *Sec = dyn_cast_or_null<InputSectionBase>(D->Section)) { 1101 int &Priority = SectionOrder[cast<InputSectionBase>(Sec->Repl)]; 1102 Priority = std::min(Priority, Ent.Priority); 1103 } 1104 } 1105 }; 1106 // We want both global and local symbols. We get the global ones from the 1107 // symbol table and iterate the object files for the local ones. 1108 for (Symbol *Sym : Symtab->getSymbols()) 1109 if (!Sym->isLazy()) 1110 AddSym(*Sym); 1111 for (InputFile *File : ObjectFiles) 1112 for (Symbol *Sym : File->getSymbols()) 1113 if (Sym->isLocal()) 1114 AddSym(*Sym); 1115 1116 if (Config->WarnSymbolOrdering) 1117 for (auto OrderEntry : SymbolOrder) 1118 if (!OrderEntry.second.Present) 1119 warn("symbol ordering file: no such symbol: " + OrderEntry.first); 1120 1121 return SectionOrder; 1122 } 1123 1124 // Sorts the sections in ISD according to the provided section order. 1125 static void 1126 sortISDBySectionOrder(InputSectionDescription *ISD, 1127 const DenseMap<const InputSectionBase *, int> &Order) { 1128 std::vector<InputSection *> UnorderedSections; 1129 std::vector<std::pair<InputSection *, int>> OrderedSections; 1130 uint64_t UnorderedSize = 0; 1131 1132 for (InputSection *IS : ISD->Sections) { 1133 auto I = Order.find(IS); 1134 if (I == Order.end()) { 1135 UnorderedSections.push_back(IS); 1136 UnorderedSize += IS->getSize(); 1137 continue; 1138 } 1139 OrderedSections.push_back({IS, I->second}); 1140 } 1141 llvm::sort(OrderedSections, [&](std::pair<InputSection *, int> A, 1142 std::pair<InputSection *, int> B) { 1143 return A.second < B.second; 1144 }); 1145 1146 // Find an insertion point for the ordered section list in the unordered 1147 // section list. On targets with limited-range branches, this is the mid-point 1148 // of the unordered section list. This decreases the likelihood that a range 1149 // extension thunk will be needed to enter or exit the ordered region. If the 1150 // ordered section list is a list of hot functions, we can generally expect 1151 // the ordered functions to be called more often than the unordered functions, 1152 // making it more likely that any particular call will be within range, and 1153 // therefore reducing the number of thunks required. 1154 // 1155 // For example, imagine that you have 8MB of hot code and 32MB of cold code. 1156 // If the layout is: 1157 // 1158 // 8MB hot 1159 // 32MB cold 1160 // 1161 // only the first 8-16MB of the cold code (depending on which hot function it 1162 // is actually calling) can call the hot code without a range extension thunk. 1163 // However, if we use this layout: 1164 // 1165 // 16MB cold 1166 // 8MB hot 1167 // 16MB cold 1168 // 1169 // both the last 8-16MB of the first block of cold code and the first 8-16MB 1170 // of the second block of cold code can call the hot code without a thunk. So 1171 // we effectively double the amount of code that could potentially call into 1172 // the hot code without a thunk. 1173 size_t InsPt = 0; 1174 if (Target->getThunkSectionSpacing() && !OrderedSections.empty()) { 1175 uint64_t UnorderedPos = 0; 1176 for (; InsPt != UnorderedSections.size(); ++InsPt) { 1177 UnorderedPos += UnorderedSections[InsPt]->getSize(); 1178 if (UnorderedPos > UnorderedSize / 2) 1179 break; 1180 } 1181 } 1182 1183 ISD->Sections.clear(); 1184 for (InputSection *IS : makeArrayRef(UnorderedSections).slice(0, InsPt)) 1185 ISD->Sections.push_back(IS); 1186 for (std::pair<InputSection *, int> P : OrderedSections) 1187 ISD->Sections.push_back(P.first); 1188 for (InputSection *IS : makeArrayRef(UnorderedSections).slice(InsPt)) 1189 ISD->Sections.push_back(IS); 1190 } 1191 1192 static void sortSection(OutputSection *Sec, 1193 const DenseMap<const InputSectionBase *, int> &Order) { 1194 StringRef Name = Sec->Name; 1195 1196 // Sort input sections by section name suffixes for 1197 // __attribute__((init_priority(N))). 1198 if (Name == ".init_array" || Name == ".fini_array") { 1199 if (!Script->HasSectionsCommand) 1200 Sec->sortInitFini(); 1201 return; 1202 } 1203 1204 // Sort input sections by the special rule for .ctors and .dtors. 1205 if (Name == ".ctors" || Name == ".dtors") { 1206 if (!Script->HasSectionsCommand) 1207 Sec->sortCtorsDtors(); 1208 return; 1209 } 1210 1211 // Never sort these. 1212 if (Name == ".init" || Name == ".fini") 1213 return; 1214 1215 // Sort input sections by priority using the list provided 1216 // by --symbol-ordering-file. 1217 if (!Order.empty()) 1218 for (BaseCommand *B : Sec->SectionCommands) 1219 if (auto *ISD = dyn_cast<InputSectionDescription>(B)) 1220 sortISDBySectionOrder(ISD, Order); 1221 } 1222 1223 // If no layout was provided by linker script, we want to apply default 1224 // sorting for special input sections. This also handles --symbol-ordering-file. 1225 template <class ELFT> void Writer<ELFT>::sortInputSections() { 1226 // Build the order once since it is expensive. 1227 DenseMap<const InputSectionBase *, int> Order = buildSectionOrder(); 1228 for (BaseCommand *Base : Script->SectionCommands) 1229 if (auto *Sec = dyn_cast<OutputSection>(Base)) 1230 sortSection(Sec, Order); 1231 } 1232 1233 template <class ELFT> void Writer<ELFT>::sortSections() { 1234 Script->adjustSectionsBeforeSorting(); 1235 1236 // Don't sort if using -r. It is not necessary and we want to preserve the 1237 // relative order for SHF_LINK_ORDER sections. 1238 if (Config->Relocatable) 1239 return; 1240 1241 sortInputSections(); 1242 1243 for (BaseCommand *Base : Script->SectionCommands) { 1244 auto *OS = dyn_cast<OutputSection>(Base); 1245 if (!OS) 1246 continue; 1247 OS->SortRank = getSectionRank(OS); 1248 1249 // We want to assign rude approximation values to OutSecOff fields 1250 // to know the relative order of the input sections. We use it for 1251 // sorting SHF_LINK_ORDER sections. See resolveShfLinkOrder(). 1252 uint64_t I = 0; 1253 for (InputSection *Sec : getInputSections(OS)) 1254 Sec->OutSecOff = I++; 1255 } 1256 1257 if (!Script->HasSectionsCommand) { 1258 // We know that all the OutputSections are contiguous in this case. 1259 auto IsSection = [](BaseCommand *Base) { return isa<OutputSection>(Base); }; 1260 std::stable_sort( 1261 llvm::find_if(Script->SectionCommands, IsSection), 1262 llvm::find_if(llvm::reverse(Script->SectionCommands), IsSection).base(), 1263 compareSections); 1264 return; 1265 } 1266 1267 // Orphan sections are sections present in the input files which are 1268 // not explicitly placed into the output file by the linker script. 1269 // 1270 // The sections in the linker script are already in the correct 1271 // order. We have to figuere out where to insert the orphan 1272 // sections. 1273 // 1274 // The order of the sections in the script is arbitrary and may not agree with 1275 // compareSections. This means that we cannot easily define a strict weak 1276 // ordering. To see why, consider a comparison of a section in the script and 1277 // one not in the script. We have a two simple options: 1278 // * Make them equivalent (a is not less than b, and b is not less than a). 1279 // The problem is then that equivalence has to be transitive and we can 1280 // have sections a, b and c with only b in a script and a less than c 1281 // which breaks this property. 1282 // * Use compareSectionsNonScript. Given that the script order doesn't have 1283 // to match, we can end up with sections a, b, c, d where b and c are in the 1284 // script and c is compareSectionsNonScript less than b. In which case d 1285 // can be equivalent to c, a to b and d < a. As a concrete example: 1286 // .a (rx) # not in script 1287 // .b (rx) # in script 1288 // .c (ro) # in script 1289 // .d (ro) # not in script 1290 // 1291 // The way we define an order then is: 1292 // * Sort only the orphan sections. They are in the end right now. 1293 // * Move each orphan section to its preferred position. We try 1294 // to put each section in the last position where it can share 1295 // a PT_LOAD. 1296 // 1297 // There is some ambiguity as to where exactly a new entry should be 1298 // inserted, because Commands contains not only output section 1299 // commands but also other types of commands such as symbol assignment 1300 // expressions. There's no correct answer here due to the lack of the 1301 // formal specification of the linker script. We use heuristics to 1302 // determine whether a new output command should be added before or 1303 // after another commands. For the details, look at shouldSkip 1304 // function. 1305 1306 auto I = Script->SectionCommands.begin(); 1307 auto E = Script->SectionCommands.end(); 1308 auto NonScriptI = std::find_if(I, E, [](BaseCommand *Base) { 1309 if (auto *Sec = dyn_cast<OutputSection>(Base)) 1310 return Sec->SectionIndex == UINT32_MAX; 1311 return false; 1312 }); 1313 1314 // Sort the orphan sections. 1315 std::stable_sort(NonScriptI, E, compareSections); 1316 1317 // As a horrible special case, skip the first . assignment if it is before any 1318 // section. We do this because it is common to set a load address by starting 1319 // the script with ". = 0xabcd" and the expectation is that every section is 1320 // after that. 1321 auto FirstSectionOrDotAssignment = 1322 std::find_if(I, E, [](BaseCommand *Cmd) { return !shouldSkip(Cmd); }); 1323 if (FirstSectionOrDotAssignment != E && 1324 isa<SymbolAssignment>(**FirstSectionOrDotAssignment)) 1325 ++FirstSectionOrDotAssignment; 1326 I = FirstSectionOrDotAssignment; 1327 1328 while (NonScriptI != E) { 1329 auto Pos = findOrphanPos<ELFT>(I, NonScriptI); 1330 OutputSection *Orphan = cast<OutputSection>(*NonScriptI); 1331 1332 // As an optimization, find all sections with the same sort rank 1333 // and insert them with one rotate. 1334 unsigned Rank = Orphan->SortRank; 1335 auto End = std::find_if(NonScriptI + 1, E, [=](BaseCommand *Cmd) { 1336 return cast<OutputSection>(Cmd)->SortRank != Rank; 1337 }); 1338 std::rotate(Pos, NonScriptI, End); 1339 NonScriptI = End; 1340 } 1341 1342 Script->adjustSectionsAfterSorting(); 1343 } 1344 1345 static bool compareByFilePosition(InputSection *A, InputSection *B) { 1346 // Synthetic, i. e. a sentinel section, should go last. 1347 if (A->kind() == InputSectionBase::Synthetic || 1348 B->kind() == InputSectionBase::Synthetic) 1349 return A->kind() != InputSectionBase::Synthetic; 1350 InputSection *LA = A->getLinkOrderDep(); 1351 InputSection *LB = B->getLinkOrderDep(); 1352 OutputSection *AOut = LA->getParent(); 1353 OutputSection *BOut = LB->getParent(); 1354 if (AOut != BOut) 1355 return AOut->SectionIndex < BOut->SectionIndex; 1356 return LA->OutSecOff < LB->OutSecOff; 1357 } 1358 1359 // This function is used by the --merge-exidx-entries to detect duplicate 1360 // .ARM.exidx sections. It is Arm only. 1361 // 1362 // The .ARM.exidx section is of the form: 1363 // | PREL31 offset to function | Unwind instructions for function | 1364 // where the unwind instructions are either a small number of unwind 1365 // instructions inlined into the table entry, the special CANT_UNWIND value of 1366 // 0x1 or a PREL31 offset into a .ARM.extab Section that contains unwind 1367 // instructions. 1368 // 1369 // We return true if all the unwind instructions in the .ARM.exidx entries of 1370 // Cur can be merged into the last entry of Prev. 1371 static bool isDuplicateArmExidxSec(InputSection *Prev, InputSection *Cur) { 1372 1373 // References to .ARM.Extab Sections have bit 31 clear and are not the 1374 // special EXIDX_CANTUNWIND bit-pattern. 1375 auto IsExtabRef = [](uint32_t Unwind) { 1376 return (Unwind & 0x80000000) == 0 && Unwind != 0x1; 1377 }; 1378 1379 struct ExidxEntry { 1380 ulittle32_t Fn; 1381 ulittle32_t Unwind; 1382 }; 1383 1384 // Get the last table Entry from the previous .ARM.exidx section. 1385 const ExidxEntry &PrevEntry = Prev->getDataAs<ExidxEntry>().back(); 1386 if (IsExtabRef(PrevEntry.Unwind)) 1387 return false; 1388 1389 // We consider the unwind instructions of an .ARM.exidx table entry 1390 // a duplicate if the previous unwind instructions if: 1391 // - Both are the special EXIDX_CANTUNWIND. 1392 // - Both are the same inline unwind instructions. 1393 // We do not attempt to follow and check links into .ARM.extab tables as 1394 // consecutive identical entries are rare and the effort to check that they 1395 // are identical is high. 1396 1397 for (const ExidxEntry Entry : Cur->getDataAs<ExidxEntry>()) 1398 if (IsExtabRef(Entry.Unwind) || Entry.Unwind != PrevEntry.Unwind) 1399 return false; 1400 // All table entries in this .ARM.exidx Section can be merged into the 1401 // previous Section. 1402 return true; 1403 } 1404 1405 template <class ELFT> void Writer<ELFT>::resolveShfLinkOrder() { 1406 for (OutputSection *Sec : OutputSections) { 1407 if (!(Sec->Flags & SHF_LINK_ORDER)) 1408 continue; 1409 1410 // Link order may be distributed across several InputSectionDescriptions 1411 // but sort must consider them all at once. 1412 std::vector<InputSection **> ScriptSections; 1413 std::vector<InputSection *> Sections; 1414 for (BaseCommand *Base : Sec->SectionCommands) { 1415 if (auto *ISD = dyn_cast<InputSectionDescription>(Base)) { 1416 for (InputSection *&IS : ISD->Sections) { 1417 ScriptSections.push_back(&IS); 1418 Sections.push_back(IS); 1419 } 1420 } 1421 } 1422 std::stable_sort(Sections.begin(), Sections.end(), compareByFilePosition); 1423 1424 if (!Config->Relocatable && Config->EMachine == EM_ARM && 1425 Sec->Type == SHT_ARM_EXIDX) { 1426 1427 if (auto *Sentinel = dyn_cast<ARMExidxSentinelSection>(Sections.back())) { 1428 assert(Sections.size() >= 2 && 1429 "We should create a sentinel section only if there are " 1430 "alive regular exidx sections."); 1431 // The last executable section is required to fill the sentinel. 1432 // Remember it here so that we don't have to find it again. 1433 Sentinel->Highest = Sections[Sections.size() - 2]->getLinkOrderDep(); 1434 } 1435 1436 if (Config->MergeArmExidx) { 1437 // The EHABI for the Arm Architecture permits consecutive identical 1438 // table entries to be merged. We use a simple implementation that 1439 // removes a .ARM.exidx Input Section if it can be merged into the 1440 // previous one. This does not require any rewriting of InputSection 1441 // contents but misses opportunities for fine grained deduplication 1442 // where only a subset of the InputSection contents can be merged. 1443 size_t Prev = 0; 1444 // The last one is a sentinel entry which should not be removed. 1445 for (size_t I = 1; I < Sections.size() - 1; ++I) { 1446 if (isDuplicateArmExidxSec(Sections[Prev], Sections[I])) 1447 Sections[I] = nullptr; 1448 else 1449 Prev = I; 1450 } 1451 } 1452 } 1453 1454 for (int I = 0, N = Sections.size(); I < N; ++I) 1455 *ScriptSections[I] = Sections[I]; 1456 1457 // Remove the Sections we marked as duplicate earlier. 1458 for (BaseCommand *Base : Sec->SectionCommands) 1459 if (auto *ISD = dyn_cast<InputSectionDescription>(Base)) 1460 llvm::erase_if(ISD->Sections, [](InputSection *IS) { return !IS; }); 1461 } 1462 } 1463 1464 static void applySynthetic(const std::vector<SyntheticSection *> &Sections, 1465 llvm::function_ref<void(SyntheticSection *)> Fn) { 1466 for (SyntheticSection *SS : Sections) 1467 if (SS && SS->getParent() && !SS->empty()) 1468 Fn(SS); 1469 } 1470 1471 // In order to allow users to manipulate linker-synthesized sections, 1472 // we had to add synthetic sections to the input section list early, 1473 // even before we make decisions whether they are needed. This allows 1474 // users to write scripts like this: ".mygot : { .got }". 1475 // 1476 // Doing it has an unintended side effects. If it turns out that we 1477 // don't need a .got (for example) at all because there's no 1478 // relocation that needs a .got, we don't want to emit .got. 1479 // 1480 // To deal with the above problem, this function is called after 1481 // scanRelocations is called to remove synthetic sections that turn 1482 // out to be empty. 1483 static void removeUnusedSyntheticSections() { 1484 // All input synthetic sections that can be empty are placed after 1485 // all regular ones. We iterate over them all and exit at first 1486 // non-synthetic. 1487 for (InputSectionBase *S : llvm::reverse(InputSections)) { 1488 SyntheticSection *SS = dyn_cast<SyntheticSection>(S); 1489 if (!SS) 1490 return; 1491 OutputSection *OS = SS->getParent(); 1492 if (!OS || !SS->empty()) 1493 continue; 1494 1495 // If we reach here, then SS is an unused synthetic section and we want to 1496 // remove it from corresponding input section description of output section. 1497 for (BaseCommand *B : OS->SectionCommands) 1498 if (auto *ISD = dyn_cast<InputSectionDescription>(B)) 1499 llvm::erase_if(ISD->Sections, 1500 [=](InputSection *IS) { return IS == SS; }); 1501 } 1502 } 1503 1504 // Returns true if a symbol can be replaced at load-time by a symbol 1505 // with the same name defined in other ELF executable or DSO. 1506 static bool computeIsPreemptible(const Symbol &B) { 1507 assert(!B.isLocal()); 1508 // Only symbols that appear in dynsym can be preempted. 1509 if (!B.includeInDynsym()) 1510 return false; 1511 1512 // Only default visibility symbols can be preempted. 1513 if (B.Visibility != STV_DEFAULT) 1514 return false; 1515 1516 // At this point copy relocations have not been created yet, so any 1517 // symbol that is not defined locally is preemptible. 1518 if (!B.isDefined()) 1519 return true; 1520 1521 // If we have a dynamic list it specifies which local symbols are preemptible. 1522 if (Config->HasDynamicList) 1523 return false; 1524 1525 if (!Config->Shared) 1526 return false; 1527 1528 // -Bsymbolic means that definitions are not preempted. 1529 if (Config->Bsymbolic || (Config->BsymbolicFunctions && B.isFunc())) 1530 return false; 1531 return true; 1532 } 1533 1534 // Create output section objects and add them to OutputSections. 1535 template <class ELFT> void Writer<ELFT>::finalizeSections() { 1536 Out::DebugInfo = findSection(".debug_info"); 1537 Out::PreinitArray = findSection(".preinit_array"); 1538 Out::InitArray = findSection(".init_array"); 1539 Out::FiniArray = findSection(".fini_array"); 1540 1541 // The linker needs to define SECNAME_start, SECNAME_end and SECNAME_stop 1542 // symbols for sections, so that the runtime can get the start and end 1543 // addresses of each section by section name. Add such symbols. 1544 if (!Config->Relocatable) { 1545 addStartEndSymbols(); 1546 for (BaseCommand *Base : Script->SectionCommands) 1547 if (auto *Sec = dyn_cast<OutputSection>(Base)) 1548 addStartStopSymbols(Sec); 1549 } 1550 1551 // Add _DYNAMIC symbol. Unlike GNU gold, our _DYNAMIC symbol has no type. 1552 // It should be okay as no one seems to care about the type. 1553 // Even the author of gold doesn't remember why gold behaves that way. 1554 // https://sourceware.org/ml/binutils/2002-03/msg00360.html 1555 if (In.DynSymTab) 1556 Symtab->addDefined("_DYNAMIC", STV_HIDDEN, STT_NOTYPE, 0 /*Value*/, 1557 /*Size=*/0, STB_WEAK, In.Dynamic, 1558 /*File=*/nullptr); 1559 1560 // Define __rel[a]_iplt_{start,end} symbols if needed. 1561 addRelIpltSymbols(); 1562 1563 // RISC-V's gp can address +/- 2 KiB, set it to .sdata + 0x800 if not defined. 1564 if (Config->EMachine == EM_RISCV) { 1565 ElfSym::RISCVGlobalPointer = 1566 dyn_cast_or_null<Defined>(Symtab->find("__global_pointer$")); 1567 if (!ElfSym::RISCVGlobalPointer) 1568 ElfSym::RISCVGlobalPointer = 1569 addOptionalRegular("__global_pointer$", findSection(".sdata"), 0x800); 1570 } 1571 1572 // This responsible for splitting up .eh_frame section into 1573 // pieces. The relocation scan uses those pieces, so this has to be 1574 // earlier. 1575 applySynthetic({In.EhFrame}, 1576 [](SyntheticSection *SS) { SS->finalizeContents(); }); 1577 1578 for (Symbol *S : Symtab->getSymbols()) 1579 S->IsPreemptible |= computeIsPreemptible(*S); 1580 1581 // Scan relocations. This must be done after every symbol is declared so that 1582 // we can correctly decide if a dynamic relocation is needed. 1583 if (!Config->Relocatable) 1584 forEachRelSec(scanRelocations<ELFT>); 1585 1586 if (In.Plt && !In.Plt->empty()) 1587 In.Plt->addSymbols(); 1588 if (In.Iplt && !In.Iplt->empty()) 1589 In.Iplt->addSymbols(); 1590 1591 // Now that we have defined all possible global symbols including linker- 1592 // synthesized ones. Visit all symbols to give the finishing touches. 1593 for (Symbol *Sym : Symtab->getSymbols()) { 1594 if (!includeInSymtab(*Sym)) 1595 continue; 1596 if (In.SymTab) 1597 In.SymTab->addSymbol(Sym); 1598 1599 if (In.DynSymTab && Sym->includeInDynsym()) { 1600 In.DynSymTab->addSymbol(Sym); 1601 if (auto *File = dyn_cast_or_null<SharedFile<ELFT>>(Sym->File)) 1602 if (File->IsNeeded && !Sym->isUndefined()) 1603 InX<ELFT>::VerNeed->addSymbol(Sym); 1604 } 1605 } 1606 1607 // Do not proceed if there was an undefined symbol. 1608 if (errorCount()) 1609 return; 1610 1611 if (In.MipsGot) 1612 In.MipsGot->build<ELFT>(); 1613 1614 removeUnusedSyntheticSections(); 1615 1616 sortSections(); 1617 1618 // Now that we have the final list, create a list of all the 1619 // OutputSections for convenience. 1620 for (BaseCommand *Base : Script->SectionCommands) 1621 if (auto *Sec = dyn_cast<OutputSection>(Base)) 1622 OutputSections.push_back(Sec); 1623 1624 // Ensure data sections are not mixed with executable sections when 1625 // -execute-only is used. 1626 if (Config->ExecuteOnly) 1627 for (OutputSection *OS : OutputSections) 1628 if (OS->Flags & SHF_EXECINSTR) 1629 for (InputSection *IS : getInputSections(OS)) 1630 if (!(IS->Flags & SHF_EXECINSTR)) 1631 error("-execute-only does not support intermingling data and code"); 1632 1633 // Prefer command line supplied address over other constraints. 1634 for (OutputSection *Sec : OutputSections) { 1635 auto I = Config->SectionStartMap.find(Sec->Name); 1636 if (I != Config->SectionStartMap.end()) 1637 Sec->AddrExpr = [=] { return I->second; }; 1638 } 1639 1640 // This is a bit of a hack. A value of 0 means undef, so we set it 1641 // to 1 to make __ehdr_start defined. The section number is not 1642 // particularly relevant. 1643 Out::ElfHeader->SectionIndex = 1; 1644 1645 unsigned I = 1; 1646 for (OutputSection *Sec : OutputSections) { 1647 Sec->SectionIndex = I++; 1648 Sec->ShName = In.ShStrTab->addString(Sec->Name); 1649 } 1650 1651 // Binary and relocatable output does not have PHDRS. 1652 // The headers have to be created before finalize as that can influence the 1653 // image base and the dynamic section on mips includes the image base. 1654 if (!Config->Relocatable && !Config->OFormatBinary) { 1655 Phdrs = Script->hasPhdrsCommands() ? Script->createPhdrs() : createPhdrs(); 1656 addPtArmExid(Phdrs); 1657 Out::ProgramHeaders->Size = sizeof(Elf_Phdr) * Phdrs.size(); 1658 1659 // Find the TLS segment. This happens before the section layout loop so that 1660 // Android relocation packing can look up TLS symbol addresses. 1661 for (PhdrEntry *P : Phdrs) 1662 if (P->p_type == PT_TLS) 1663 Out::TlsPhdr = P; 1664 } 1665 1666 // Some symbols are defined in term of program headers. Now that we 1667 // have the headers, we can find out which sections they point to. 1668 setReservedSymbolSections(); 1669 1670 // Dynamic section must be the last one in this list and dynamic 1671 // symbol table section (DynSymTab) must be the first one. 1672 applySynthetic({In.DynSymTab, 1673 In.Bss, 1674 In.BssRelRo, 1675 In.GnuHashTab, 1676 In.HashTab, 1677 In.SymTabShndx, 1678 In.ShStrTab, 1679 In.StrTab, 1680 In.VerDef, 1681 In.DynStrTab, 1682 In.Got, 1683 In.MipsGot, 1684 In.IgotPlt, 1685 In.GotPlt, 1686 In.RelaDyn, 1687 In.RelrDyn, 1688 In.RelaIplt, 1689 In.RelaPlt, 1690 In.Plt, 1691 In.Iplt, 1692 In.EhFrameHdr, 1693 InX<ELFT>::VerSym, 1694 InX<ELFT>::VerNeed, 1695 In.Dynamic}, 1696 [](SyntheticSection *SS) { SS->finalizeContents(); }); 1697 1698 if (!Script->HasSectionsCommand && !Config->Relocatable) 1699 fixSectionAlignments(); 1700 1701 // After link order processing .ARM.exidx sections can be deduplicated, which 1702 // needs to be resolved before any other address dependent operation. 1703 resolveShfLinkOrder(); 1704 1705 // Some architectures need to generate content that depends on the address 1706 // of InputSections. For example some architectures use small displacements 1707 // for jump instructions that is the linker's responsibility for creating 1708 // range extension thunks for. As the generation of the content may also 1709 // alter InputSection addresses we must converge to a fixed point. 1710 if (Target->NeedsThunks || Config->AndroidPackDynRelocs || 1711 Config->RelrPackDynRelocs) { 1712 ThunkCreator TC; 1713 AArch64Err843419Patcher A64P; 1714 bool Changed; 1715 do { 1716 Script->assignAddresses(); 1717 Changed = false; 1718 if (Target->NeedsThunks) 1719 Changed |= TC.createThunks(OutputSections); 1720 if (Config->FixCortexA53Errata843419) { 1721 if (Changed) 1722 Script->assignAddresses(); 1723 Changed |= A64P.createFixes(); 1724 } 1725 if (In.MipsGot) 1726 In.MipsGot->updateAllocSize(); 1727 Changed |= In.RelaDyn->updateAllocSize(); 1728 if (In.RelrDyn) 1729 Changed |= In.RelrDyn->updateAllocSize(); 1730 } while (Changed); 1731 } 1732 1733 // createThunks may have added local symbols to the static symbol table 1734 applySynthetic({In.SymTab}, 1735 [](SyntheticSection *SS) { SS->finalizeContents(); }); 1736 1737 // Fill other section headers. The dynamic table is finalized 1738 // at the end because some tags like RELSZ depend on result 1739 // of finalizing other sections. 1740 for (OutputSection *Sec : OutputSections) 1741 Sec->finalize<ELFT>(); 1742 } 1743 1744 // The linker is expected to define SECNAME_start and SECNAME_end 1745 // symbols for a few sections. This function defines them. 1746 template <class ELFT> void Writer<ELFT>::addStartEndSymbols() { 1747 // If a section does not exist, there's ambiguity as to how we 1748 // define _start and _end symbols for an init/fini section. Since 1749 // the loader assume that the symbols are always defined, we need to 1750 // always define them. But what value? The loader iterates over all 1751 // pointers between _start and _end to run global ctors/dtors, so if 1752 // the section is empty, their symbol values don't actually matter 1753 // as long as _start and _end point to the same location. 1754 // 1755 // That said, we don't want to set the symbols to 0 (which is 1756 // probably the simplest value) because that could cause some 1757 // program to fail to link due to relocation overflow, if their 1758 // program text is above 2 GiB. We use the address of the .text 1759 // section instead to prevent that failure. 1760 OutputSection *Default = findSection(".text"); 1761 if (!Default) 1762 Default = Out::ElfHeader; 1763 auto Define = [=](StringRef Start, StringRef End, OutputSection *OS) { 1764 if (OS) { 1765 addOptionalRegular(Start, OS, 0); 1766 addOptionalRegular(End, OS, -1); 1767 } else { 1768 addOptionalRegular(Start, Default, 0); 1769 addOptionalRegular(End, Default, 0); 1770 } 1771 }; 1772 1773 Define("__preinit_array_start", "__preinit_array_end", Out::PreinitArray); 1774 Define("__init_array_start", "__init_array_end", Out::InitArray); 1775 Define("__fini_array_start", "__fini_array_end", Out::FiniArray); 1776 1777 if (OutputSection *Sec = findSection(".ARM.exidx")) 1778 Define("__exidx_start", "__exidx_end", Sec); 1779 } 1780 1781 // If a section name is valid as a C identifier (which is rare because of 1782 // the leading '.'), linkers are expected to define __start_<secname> and 1783 // __stop_<secname> symbols. They are at beginning and end of the section, 1784 // respectively. This is not requested by the ELF standard, but GNU ld and 1785 // gold provide the feature, and used by many programs. 1786 template <class ELFT> 1787 void Writer<ELFT>::addStartStopSymbols(OutputSection *Sec) { 1788 StringRef S = Sec->Name; 1789 if (!isValidCIdentifier(S)) 1790 return; 1791 addOptionalRegular(Saver.save("__start_" + S), Sec, 0, STV_PROTECTED); 1792 addOptionalRegular(Saver.save("__stop_" + S), Sec, -1, STV_PROTECTED); 1793 } 1794 1795 static bool needsPtLoad(OutputSection *Sec) { 1796 if (!(Sec->Flags & SHF_ALLOC) || Sec->Noload) 1797 return false; 1798 1799 // Don't allocate VA space for TLS NOBITS sections. The PT_TLS PHDR is 1800 // responsible for allocating space for them, not the PT_LOAD that 1801 // contains the TLS initialization image. 1802 if (Sec->Flags & SHF_TLS && Sec->Type == SHT_NOBITS) 1803 return false; 1804 return true; 1805 } 1806 1807 // Linker scripts are responsible for aligning addresses. Unfortunately, most 1808 // linker scripts are designed for creating two PT_LOADs only, one RX and one 1809 // RW. This means that there is no alignment in the RO to RX transition and we 1810 // cannot create a PT_LOAD there. 1811 static uint64_t computeFlags(uint64_t Flags) { 1812 if (Config->Omagic) 1813 return PF_R | PF_W | PF_X; 1814 if (Config->ExecuteOnly && (Flags & PF_X)) 1815 return Flags & ~PF_R; 1816 if (Config->SingleRoRx && !(Flags & PF_W)) 1817 return Flags | PF_X; 1818 return Flags; 1819 } 1820 1821 // Decide which program headers to create and which sections to include in each 1822 // one. 1823 template <class ELFT> std::vector<PhdrEntry *> Writer<ELFT>::createPhdrs() { 1824 std::vector<PhdrEntry *> Ret; 1825 auto AddHdr = [&](unsigned Type, unsigned Flags) -> PhdrEntry * { 1826 Ret.push_back(make<PhdrEntry>(Type, Flags)); 1827 return Ret.back(); 1828 }; 1829 1830 // The first phdr entry is PT_PHDR which describes the program header itself. 1831 AddHdr(PT_PHDR, PF_R)->add(Out::ProgramHeaders); 1832 1833 // PT_INTERP must be the second entry if exists. 1834 if (OutputSection *Cmd = findSection(".interp")) 1835 AddHdr(PT_INTERP, Cmd->getPhdrFlags())->add(Cmd); 1836 1837 // Add the first PT_LOAD segment for regular output sections. 1838 uint64_t Flags = computeFlags(PF_R); 1839 PhdrEntry *Load = AddHdr(PT_LOAD, Flags); 1840 1841 // Add the headers. We will remove them if they don't fit. 1842 Load->add(Out::ElfHeader); 1843 Load->add(Out::ProgramHeaders); 1844 1845 for (OutputSection *Sec : OutputSections) { 1846 if (!(Sec->Flags & SHF_ALLOC)) 1847 break; 1848 if (!needsPtLoad(Sec)) 1849 continue; 1850 1851 // Segments are contiguous memory regions that has the same attributes 1852 // (e.g. executable or writable). There is one phdr for each segment. 1853 // Therefore, we need to create a new phdr when the next section has 1854 // different flags or is loaded at a discontiguous address or memory 1855 // region using AT or AT> linker script command, respectively. At the same 1856 // time, we don't want to create a separate load segment for the headers, 1857 // even if the first output section has an AT or AT> attribute. 1858 uint64_t NewFlags = computeFlags(Sec->getPhdrFlags()); 1859 if (((Sec->LMAExpr || 1860 (Sec->LMARegion && (Sec->LMARegion != Load->FirstSec->LMARegion))) && 1861 Load->LastSec != Out::ProgramHeaders) || 1862 Sec->MemRegion != Load->FirstSec->MemRegion || Flags != NewFlags) { 1863 1864 Load = AddHdr(PT_LOAD, NewFlags); 1865 Flags = NewFlags; 1866 } 1867 1868 Load->add(Sec); 1869 } 1870 1871 // Add a TLS segment if any. 1872 PhdrEntry *TlsHdr = make<PhdrEntry>(PT_TLS, PF_R); 1873 for (OutputSection *Sec : OutputSections) 1874 if (Sec->Flags & SHF_TLS) 1875 TlsHdr->add(Sec); 1876 if (TlsHdr->FirstSec) 1877 Ret.push_back(TlsHdr); 1878 1879 // Add an entry for .dynamic. 1880 if (In.DynSymTab) 1881 AddHdr(PT_DYNAMIC, In.Dynamic->getParent()->getPhdrFlags()) 1882 ->add(In.Dynamic->getParent()); 1883 1884 // PT_GNU_RELRO includes all sections that should be marked as 1885 // read-only by dynamic linker after proccessing relocations. 1886 // Current dynamic loaders only support one PT_GNU_RELRO PHDR, give 1887 // an error message if more than one PT_GNU_RELRO PHDR is required. 1888 PhdrEntry *RelRo = make<PhdrEntry>(PT_GNU_RELRO, PF_R); 1889 bool InRelroPhdr = false; 1890 bool IsRelroFinished = false; 1891 for (OutputSection *Sec : OutputSections) { 1892 if (!needsPtLoad(Sec)) 1893 continue; 1894 if (isRelroSection(Sec)) { 1895 InRelroPhdr = true; 1896 if (!IsRelroFinished) 1897 RelRo->add(Sec); 1898 else 1899 error("section: " + Sec->Name + " is not contiguous with other relro" + 1900 " sections"); 1901 } else if (InRelroPhdr) { 1902 InRelroPhdr = false; 1903 IsRelroFinished = true; 1904 } 1905 } 1906 if (RelRo->FirstSec) 1907 Ret.push_back(RelRo); 1908 1909 // PT_GNU_EH_FRAME is a special section pointing on .eh_frame_hdr. 1910 if (!In.EhFrame->empty() && In.EhFrameHdr && In.EhFrame->getParent() && 1911 In.EhFrameHdr->getParent()) 1912 AddHdr(PT_GNU_EH_FRAME, In.EhFrameHdr->getParent()->getPhdrFlags()) 1913 ->add(In.EhFrameHdr->getParent()); 1914 1915 // PT_OPENBSD_RANDOMIZE is an OpenBSD-specific feature. That makes 1916 // the dynamic linker fill the segment with random data. 1917 if (OutputSection *Cmd = findSection(".openbsd.randomdata")) 1918 AddHdr(PT_OPENBSD_RANDOMIZE, Cmd->getPhdrFlags())->add(Cmd); 1919 1920 // PT_GNU_STACK is a special section to tell the loader to make the 1921 // pages for the stack non-executable. If you really want an executable 1922 // stack, you can pass -z execstack, but that's not recommended for 1923 // security reasons. 1924 unsigned Perm = PF_R | PF_W; 1925 if (Config->ZExecstack) 1926 Perm |= PF_X; 1927 AddHdr(PT_GNU_STACK, Perm)->p_memsz = Config->ZStackSize; 1928 1929 // PT_OPENBSD_WXNEEDED is a OpenBSD-specific header to mark the executable 1930 // is expected to perform W^X violations, such as calling mprotect(2) or 1931 // mmap(2) with PROT_WRITE | PROT_EXEC, which is prohibited by default on 1932 // OpenBSD. 1933 if (Config->ZWxneeded) 1934 AddHdr(PT_OPENBSD_WXNEEDED, PF_X); 1935 1936 // Create one PT_NOTE per a group of contiguous .note sections. 1937 PhdrEntry *Note = nullptr; 1938 for (OutputSection *Sec : OutputSections) { 1939 if (Sec->Type == SHT_NOTE && (Sec->Flags & SHF_ALLOC)) { 1940 if (!Note || Sec->LMAExpr) 1941 Note = AddHdr(PT_NOTE, PF_R); 1942 Note->add(Sec); 1943 } else { 1944 Note = nullptr; 1945 } 1946 } 1947 return Ret; 1948 } 1949 1950 template <class ELFT> 1951 void Writer<ELFT>::addPtArmExid(std::vector<PhdrEntry *> &Phdrs) { 1952 if (Config->EMachine != EM_ARM) 1953 return; 1954 auto I = llvm::find_if(OutputSections, [](OutputSection *Cmd) { 1955 return Cmd->Type == SHT_ARM_EXIDX; 1956 }); 1957 if (I == OutputSections.end()) 1958 return; 1959 1960 // PT_ARM_EXIDX is the ARM EHABI equivalent of PT_GNU_EH_FRAME 1961 PhdrEntry *ARMExidx = make<PhdrEntry>(PT_ARM_EXIDX, PF_R); 1962 ARMExidx->add(*I); 1963 Phdrs.push_back(ARMExidx); 1964 } 1965 1966 // The first section of each PT_LOAD, the first section in PT_GNU_RELRO and the 1967 // first section after PT_GNU_RELRO have to be page aligned so that the dynamic 1968 // linker can set the permissions. 1969 template <class ELFT> void Writer<ELFT>::fixSectionAlignments() { 1970 auto PageAlign = [](OutputSection *Cmd) { 1971 if (Cmd && !Cmd->AddrExpr) 1972 Cmd->AddrExpr = [=] { 1973 return alignTo(Script->getDot(), Config->MaxPageSize); 1974 }; 1975 }; 1976 1977 for (const PhdrEntry *P : Phdrs) 1978 if (P->p_type == PT_LOAD && P->FirstSec) 1979 PageAlign(P->FirstSec); 1980 1981 for (const PhdrEntry *P : Phdrs) { 1982 if (P->p_type != PT_GNU_RELRO) 1983 continue; 1984 if (P->FirstSec) 1985 PageAlign(P->FirstSec); 1986 // Find the first section after PT_GNU_RELRO. If it is in a PT_LOAD we 1987 // have to align it to a page. 1988 auto End = OutputSections.end(); 1989 auto I = std::find(OutputSections.begin(), End, P->LastSec); 1990 if (I == End || (I + 1) == End) 1991 continue; 1992 OutputSection *Cmd = (*(I + 1)); 1993 if (needsPtLoad(Cmd)) 1994 PageAlign(Cmd); 1995 } 1996 } 1997 1998 // Adjusts the file alignment for a given output section and returns 1999 // its new file offset. The file offset must be the same with its 2000 // virtual address (modulo the page size) so that the loader can load 2001 // executables without any address adjustment. 2002 static uint64_t getFileAlignment(uint64_t Off, OutputSection *Cmd) { 2003 OutputSection *First = Cmd->PtLoad ? Cmd->PtLoad->FirstSec : nullptr; 2004 // The first section in a PT_LOAD has to have congruent offset and address 2005 // module the page size. 2006 if (Cmd == First) 2007 return alignTo(Off, std::max<uint64_t>(Cmd->Alignment, Config->MaxPageSize), 2008 Cmd->Addr); 2009 2010 // For SHT_NOBITS we don't want the alignment of the section to impact the 2011 // offset of the sections that follow. Since nothing seems to care about the 2012 // sh_offset of the SHT_NOBITS section itself, just ignore it. 2013 if (Cmd->Type == SHT_NOBITS) 2014 return Off; 2015 2016 // If the section is not in a PT_LOAD, we just have to align it. 2017 if (!Cmd->PtLoad) 2018 return alignTo(Off, Cmd->Alignment); 2019 2020 // If two sections share the same PT_LOAD the file offset is calculated 2021 // using this formula: Off2 = Off1 + (VA2 - VA1). 2022 return First->Offset + Cmd->Addr - First->Addr; 2023 } 2024 2025 static uint64_t setOffset(OutputSection *Cmd, uint64_t Off) { 2026 Off = getFileAlignment(Off, Cmd); 2027 Cmd->Offset = Off; 2028 2029 // For SHT_NOBITS we should not count the size. 2030 if (Cmd->Type == SHT_NOBITS) 2031 return Off; 2032 2033 return Off + Cmd->Size; 2034 } 2035 2036 template <class ELFT> void Writer<ELFT>::assignFileOffsetsBinary() { 2037 uint64_t Off = 0; 2038 for (OutputSection *Sec : OutputSections) 2039 if (Sec->Flags & SHF_ALLOC) 2040 Off = setOffset(Sec, Off); 2041 FileSize = alignTo(Off, Config->Wordsize); 2042 } 2043 2044 static std::string rangeToString(uint64_t Addr, uint64_t Len) { 2045 return "[0x" + utohexstr(Addr) + ", 0x" + utohexstr(Addr + Len - 1) + "]"; 2046 } 2047 2048 // Assign file offsets to output sections. 2049 template <class ELFT> void Writer<ELFT>::assignFileOffsets() { 2050 uint64_t Off = 0; 2051 Off = setOffset(Out::ElfHeader, Off); 2052 Off = setOffset(Out::ProgramHeaders, Off); 2053 2054 PhdrEntry *LastRX = nullptr; 2055 for (PhdrEntry *P : Phdrs) 2056 if (P->p_type == PT_LOAD && (P->p_flags & PF_X)) 2057 LastRX = P; 2058 2059 for (OutputSection *Sec : OutputSections) { 2060 Off = setOffset(Sec, Off); 2061 if (Script->HasSectionsCommand) 2062 continue; 2063 // If this is a last section of the last executable segment and that 2064 // segment is the last loadable segment, align the offset of the 2065 // following section to avoid loading non-segments parts of the file. 2066 if (LastRX && LastRX->LastSec == Sec) 2067 Off = alignTo(Off, Target->PageSize); 2068 } 2069 2070 SectionHeaderOff = alignTo(Off, Config->Wordsize); 2071 FileSize = SectionHeaderOff + (OutputSections.size() + 1) * sizeof(Elf_Shdr); 2072 2073 // Our logic assumes that sections have rising VA within the same segment. 2074 // With use of linker scripts it is possible to violate this rule and get file 2075 // offset overlaps or overflows. That should never happen with a valid script 2076 // which does not move the location counter backwards and usually scripts do 2077 // not do that. Unfortunately, there are apps in the wild, for example, Linux 2078 // kernel, which control segment distribution explicitly and move the counter 2079 // backwards, so we have to allow doing that to support linking them. We 2080 // perform non-critical checks for overlaps in checkSectionOverlap(), but here 2081 // we want to prevent file size overflows because it would crash the linker. 2082 for (OutputSection *Sec : OutputSections) { 2083 if (Sec->Type == SHT_NOBITS) 2084 continue; 2085 if ((Sec->Offset > FileSize) || (Sec->Offset + Sec->Size > FileSize)) 2086 error("unable to place section " + Sec->Name + " at file offset " + 2087 rangeToString(Sec->Offset, Sec->Size) + 2088 "; check your linker script for overflows"); 2089 } 2090 } 2091 2092 // Finalize the program headers. We call this function after we assign 2093 // file offsets and VAs to all sections. 2094 template <class ELFT> void Writer<ELFT>::setPhdrs() { 2095 for (PhdrEntry *P : Phdrs) { 2096 OutputSection *First = P->FirstSec; 2097 OutputSection *Last = P->LastSec; 2098 if (First) { 2099 P->p_filesz = Last->Offset - First->Offset; 2100 if (Last->Type != SHT_NOBITS) 2101 P->p_filesz += Last->Size; 2102 P->p_memsz = Last->Addr + Last->Size - First->Addr; 2103 P->p_offset = First->Offset; 2104 P->p_vaddr = First->Addr; 2105 if (!P->HasLMA) 2106 P->p_paddr = First->getLMA(); 2107 } 2108 if (P->p_type == PT_LOAD) 2109 P->p_align = std::max<uint64_t>(P->p_align, Config->MaxPageSize); 2110 else if (P->p_type == PT_GNU_RELRO) { 2111 P->p_align = 1; 2112 // The glibc dynamic loader rounds the size down, so we need to round up 2113 // to protect the last page. This is a no-op on FreeBSD which always 2114 // rounds up. 2115 P->p_memsz = alignTo(P->p_memsz, Target->PageSize); 2116 } 2117 2118 // The TLS pointer goes after PT_TLS for variant 2 targets. At least glibc 2119 // will align it, so round up the size to make sure the offsets are 2120 // correct. 2121 if (P->p_type == PT_TLS && P->p_memsz) 2122 P->p_memsz = alignTo(P->p_memsz, P->p_align); 2123 } 2124 } 2125 2126 // A helper struct for checkSectionOverlap. 2127 namespace { 2128 struct SectionOffset { 2129 OutputSection *Sec; 2130 uint64_t Offset; 2131 }; 2132 } // namespace 2133 2134 // Check whether sections overlap for a specific address range (file offsets, 2135 // load and virtual adresses). 2136 static void checkOverlap(StringRef Name, std::vector<SectionOffset> &Sections, 2137 bool IsVirtualAddr) { 2138 llvm::sort(Sections, [=](const SectionOffset &A, const SectionOffset &B) { 2139 return A.Offset < B.Offset; 2140 }); 2141 2142 // Finding overlap is easy given a vector is sorted by start position. 2143 // If an element starts before the end of the previous element, they overlap. 2144 for (size_t I = 1, End = Sections.size(); I < End; ++I) { 2145 SectionOffset A = Sections[I - 1]; 2146 SectionOffset B = Sections[I]; 2147 if (B.Offset >= A.Offset + A.Sec->Size) 2148 continue; 2149 2150 // If both sections are in OVERLAY we allow the overlapping of virtual 2151 // addresses, because it is what OVERLAY was designed for. 2152 if (IsVirtualAddr && A.Sec->InOverlay && B.Sec->InOverlay) 2153 continue; 2154 2155 errorOrWarn("section " + A.Sec->Name + " " + Name + 2156 " range overlaps with " + B.Sec->Name + "\n>>> " + A.Sec->Name + 2157 " range is " + rangeToString(A.Offset, A.Sec->Size) + "\n>>> " + 2158 B.Sec->Name + " range is " + 2159 rangeToString(B.Offset, B.Sec->Size)); 2160 } 2161 } 2162 2163 // Check for overlapping sections and address overflows. 2164 // 2165 // In this function we check that none of the output sections have overlapping 2166 // file offsets. For SHF_ALLOC sections we also check that the load address 2167 // ranges and the virtual address ranges don't overlap 2168 template <class ELFT> void Writer<ELFT>::checkSections() { 2169 // First, check that section's VAs fit in available address space for target. 2170 for (OutputSection *OS : OutputSections) 2171 if ((OS->Addr + OS->Size < OS->Addr) || 2172 (!ELFT::Is64Bits && OS->Addr + OS->Size > UINT32_MAX)) 2173 errorOrWarn("section " + OS->Name + " at 0x" + utohexstr(OS->Addr) + 2174 " of size 0x" + utohexstr(OS->Size) + 2175 " exceeds available address space"); 2176 2177 // Check for overlapping file offsets. In this case we need to skip any 2178 // section marked as SHT_NOBITS. These sections don't actually occupy space in 2179 // the file so Sec->Offset + Sec->Size can overlap with others. If --oformat 2180 // binary is specified only add SHF_ALLOC sections are added to the output 2181 // file so we skip any non-allocated sections in that case. 2182 std::vector<SectionOffset> FileOffs; 2183 for (OutputSection *Sec : OutputSections) 2184 if (Sec->Size > 0 && Sec->Type != SHT_NOBITS && 2185 (!Config->OFormatBinary || (Sec->Flags & SHF_ALLOC))) 2186 FileOffs.push_back({Sec, Sec->Offset}); 2187 checkOverlap("file", FileOffs, false); 2188 2189 // When linking with -r there is no need to check for overlapping virtual/load 2190 // addresses since those addresses will only be assigned when the final 2191 // executable/shared object is created. 2192 if (Config->Relocatable) 2193 return; 2194 2195 // Checking for overlapping virtual and load addresses only needs to take 2196 // into account SHF_ALLOC sections since others will not be loaded. 2197 // Furthermore, we also need to skip SHF_TLS sections since these will be 2198 // mapped to other addresses at runtime and can therefore have overlapping 2199 // ranges in the file. 2200 std::vector<SectionOffset> VMAs; 2201 for (OutputSection *Sec : OutputSections) 2202 if (Sec->Size > 0 && (Sec->Flags & SHF_ALLOC) && !(Sec->Flags & SHF_TLS)) 2203 VMAs.push_back({Sec, Sec->Addr}); 2204 checkOverlap("virtual address", VMAs, true); 2205 2206 // Finally, check that the load addresses don't overlap. This will usually be 2207 // the same as the virtual addresses but can be different when using a linker 2208 // script with AT(). 2209 std::vector<SectionOffset> LMAs; 2210 for (OutputSection *Sec : OutputSections) 2211 if (Sec->Size > 0 && (Sec->Flags & SHF_ALLOC) && !(Sec->Flags & SHF_TLS)) 2212 LMAs.push_back({Sec, Sec->getLMA()}); 2213 checkOverlap("load address", LMAs, false); 2214 } 2215 2216 // The entry point address is chosen in the following ways. 2217 // 2218 // 1. the '-e' entry command-line option; 2219 // 2. the ENTRY(symbol) command in a linker control script; 2220 // 3. the value of the symbol _start, if present; 2221 // 4. the number represented by the entry symbol, if it is a number; 2222 // 5. the address of the first byte of the .text section, if present; 2223 // 6. the address 0. 2224 static uint64_t getEntryAddr() { 2225 // Case 1, 2 or 3 2226 if (Symbol *B = Symtab->find(Config->Entry)) 2227 return B->getVA(); 2228 2229 // Case 4 2230 uint64_t Addr; 2231 if (to_integer(Config->Entry, Addr)) 2232 return Addr; 2233 2234 // Case 5 2235 if (OutputSection *Sec = findSection(".text")) { 2236 if (Config->WarnMissingEntry) 2237 warn("cannot find entry symbol " + Config->Entry + "; defaulting to 0x" + 2238 utohexstr(Sec->Addr)); 2239 return Sec->Addr; 2240 } 2241 2242 // Case 6 2243 if (Config->WarnMissingEntry) 2244 warn("cannot find entry symbol " + Config->Entry + 2245 "; not setting start address"); 2246 return 0; 2247 } 2248 2249 static uint16_t getELFType() { 2250 if (Config->Pic) 2251 return ET_DYN; 2252 if (Config->Relocatable) 2253 return ET_REL; 2254 return ET_EXEC; 2255 } 2256 2257 static uint8_t getAbiVersion() { 2258 // MIPS non-PIC executable gets ABI version 1. 2259 if (Config->EMachine == EM_MIPS && getELFType() == ET_EXEC && 2260 (Config->EFlags & (EF_MIPS_PIC | EF_MIPS_CPIC)) == EF_MIPS_CPIC) 2261 return 1; 2262 return 0; 2263 } 2264 2265 template <class ELFT> void Writer<ELFT>::writeHeader() { 2266 uint8_t *Buf = Buffer->getBufferStart(); 2267 // For executable segments, the trap instructions are written before writing 2268 // the header. Setting Elf header bytes to zero ensures that any unused bytes 2269 // in header are zero-cleared, instead of having trap instructions. 2270 memset(Buf, 0, sizeof(Elf_Ehdr)); 2271 memcpy(Buf, "\177ELF", 4); 2272 2273 // Write the ELF header. 2274 auto *EHdr = reinterpret_cast<Elf_Ehdr *>(Buf); 2275 EHdr->e_ident[EI_CLASS] = Config->Is64 ? ELFCLASS64 : ELFCLASS32; 2276 EHdr->e_ident[EI_DATA] = Config->IsLE ? ELFDATA2LSB : ELFDATA2MSB; 2277 EHdr->e_ident[EI_VERSION] = EV_CURRENT; 2278 EHdr->e_ident[EI_OSABI] = Config->OSABI; 2279 EHdr->e_ident[EI_ABIVERSION] = getAbiVersion(); 2280 EHdr->e_type = getELFType(); 2281 EHdr->e_machine = Config->EMachine; 2282 EHdr->e_version = EV_CURRENT; 2283 EHdr->e_entry = getEntryAddr(); 2284 EHdr->e_shoff = SectionHeaderOff; 2285 EHdr->e_flags = Config->EFlags; 2286 EHdr->e_ehsize = sizeof(Elf_Ehdr); 2287 EHdr->e_phnum = Phdrs.size(); 2288 EHdr->e_shentsize = sizeof(Elf_Shdr); 2289 2290 if (!Config->Relocatable) { 2291 EHdr->e_phoff = sizeof(Elf_Ehdr); 2292 EHdr->e_phentsize = sizeof(Elf_Phdr); 2293 } 2294 2295 // Write the program header table. 2296 auto *HBuf = reinterpret_cast<Elf_Phdr *>(Buf + EHdr->e_phoff); 2297 for (PhdrEntry *P : Phdrs) { 2298 HBuf->p_type = P->p_type; 2299 HBuf->p_flags = P->p_flags; 2300 HBuf->p_offset = P->p_offset; 2301 HBuf->p_vaddr = P->p_vaddr; 2302 HBuf->p_paddr = P->p_paddr; 2303 HBuf->p_filesz = P->p_filesz; 2304 HBuf->p_memsz = P->p_memsz; 2305 HBuf->p_align = P->p_align; 2306 ++HBuf; 2307 } 2308 2309 // Write the section header table. 2310 // 2311 // The ELF header can only store numbers up to SHN_LORESERVE in the e_shnum 2312 // and e_shstrndx fields. When the value of one of these fields exceeds 2313 // SHN_LORESERVE ELF requires us to put sentinel values in the ELF header and 2314 // use fields in the section header at index 0 to store 2315 // the value. The sentinel values and fields are: 2316 // e_shnum = 0, SHdrs[0].sh_size = number of sections. 2317 // e_shstrndx = SHN_XINDEX, SHdrs[0].sh_link = .shstrtab section index. 2318 auto *SHdrs = reinterpret_cast<Elf_Shdr *>(Buf + EHdr->e_shoff); 2319 size_t Num = OutputSections.size() + 1; 2320 if (Num >= SHN_LORESERVE) 2321 SHdrs->sh_size = Num; 2322 else 2323 EHdr->e_shnum = Num; 2324 2325 uint32_t StrTabIndex = In.ShStrTab->getParent()->SectionIndex; 2326 if (StrTabIndex >= SHN_LORESERVE) { 2327 SHdrs->sh_link = StrTabIndex; 2328 EHdr->e_shstrndx = SHN_XINDEX; 2329 } else { 2330 EHdr->e_shstrndx = StrTabIndex; 2331 } 2332 2333 for (OutputSection *Sec : OutputSections) 2334 Sec->writeHeaderTo<ELFT>(++SHdrs); 2335 } 2336 2337 // Open a result file. 2338 template <class ELFT> void Writer<ELFT>::openFile() { 2339 if (!Config->Is64 && FileSize > UINT32_MAX) { 2340 error("output file too large: " + Twine(FileSize) + " bytes"); 2341 return; 2342 } 2343 2344 unlinkAsync(Config->OutputFile); 2345 unsigned Flags = 0; 2346 if (!Config->Relocatable) 2347 Flags = FileOutputBuffer::F_executable; 2348 Expected<std::unique_ptr<FileOutputBuffer>> BufferOrErr = 2349 FileOutputBuffer::create(Config->OutputFile, FileSize, Flags); 2350 2351 if (!BufferOrErr) 2352 error("failed to open " + Config->OutputFile + ": " + 2353 llvm::toString(BufferOrErr.takeError())); 2354 else 2355 Buffer = std::move(*BufferOrErr); 2356 } 2357 2358 template <class ELFT> void Writer<ELFT>::writeSectionsBinary() { 2359 uint8_t *Buf = Buffer->getBufferStart(); 2360 for (OutputSection *Sec : OutputSections) 2361 if (Sec->Flags & SHF_ALLOC) 2362 Sec->writeTo<ELFT>(Buf + Sec->Offset); 2363 } 2364 2365 static void fillTrap(uint8_t *I, uint8_t *End) { 2366 for (; I + 4 <= End; I += 4) 2367 memcpy(I, &Target->TrapInstr, 4); 2368 } 2369 2370 // Fill the last page of executable segments with trap instructions 2371 // instead of leaving them as zero. Even though it is not required by any 2372 // standard, it is in general a good thing to do for security reasons. 2373 // 2374 // We'll leave other pages in segments as-is because the rest will be 2375 // overwritten by output sections. 2376 template <class ELFT> void Writer<ELFT>::writeTrapInstr() { 2377 if (Script->HasSectionsCommand) 2378 return; 2379 2380 // Fill the last page. 2381 uint8_t *Buf = Buffer->getBufferStart(); 2382 for (PhdrEntry *P : Phdrs) 2383 if (P->p_type == PT_LOAD && (P->p_flags & PF_X)) 2384 fillTrap(Buf + alignDown(P->p_offset + P->p_filesz, Target->PageSize), 2385 Buf + alignTo(P->p_offset + P->p_filesz, Target->PageSize)); 2386 2387 // Round up the file size of the last segment to the page boundary iff it is 2388 // an executable segment to ensure that other tools don't accidentally 2389 // trim the instruction padding (e.g. when stripping the file). 2390 PhdrEntry *Last = nullptr; 2391 for (PhdrEntry *P : Phdrs) 2392 if (P->p_type == PT_LOAD) 2393 Last = P; 2394 2395 if (Last && (Last->p_flags & PF_X)) 2396 Last->p_memsz = Last->p_filesz = alignTo(Last->p_filesz, Target->PageSize); 2397 } 2398 2399 // Write section contents to a mmap'ed file. 2400 template <class ELFT> void Writer<ELFT>::writeSections() { 2401 uint8_t *Buf = Buffer->getBufferStart(); 2402 2403 OutputSection *EhFrameHdr = nullptr; 2404 if (In.EhFrameHdr && !In.EhFrameHdr->empty()) 2405 EhFrameHdr = In.EhFrameHdr->getParent(); 2406 2407 // In -r or -emit-relocs mode, write the relocation sections first as in 2408 // ELf_Rel targets we might find out that we need to modify the relocated 2409 // section while doing it. 2410 for (OutputSection *Sec : OutputSections) 2411 if (Sec->Type == SHT_REL || Sec->Type == SHT_RELA) 2412 Sec->writeTo<ELFT>(Buf + Sec->Offset); 2413 2414 for (OutputSection *Sec : OutputSections) 2415 if (Sec != EhFrameHdr && Sec->Type != SHT_REL && Sec->Type != SHT_RELA) 2416 Sec->writeTo<ELFT>(Buf + Sec->Offset); 2417 2418 // The .eh_frame_hdr depends on .eh_frame section contents, therefore 2419 // it should be written after .eh_frame is written. 2420 if (EhFrameHdr) 2421 EhFrameHdr->writeTo<ELFT>(Buf + EhFrameHdr->Offset); 2422 } 2423 2424 template <class ELFT> void Writer<ELFT>::writeBuildId() { 2425 if (!In.BuildId || !In.BuildId->getParent()) 2426 return; 2427 2428 // Compute a hash of all sections of the output file. 2429 uint8_t *Start = Buffer->getBufferStart(); 2430 uint8_t *End = Start + FileSize; 2431 In.BuildId->writeBuildId({Start, End}); 2432 } 2433 2434 template void elf::writeResult<ELF32LE>(); 2435 template void elf::writeResult<ELF32BE>(); 2436 template void elf::writeResult<ELF64LE>(); 2437 template void elf::writeResult<ELF64BE>(); 2438