1 //===- Writer.cpp ---------------------------------------------------------===//
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
12 #include "Filesystem.h"
13 #include "LinkerScript.h"
16 #include "OutputSections.h"
17 #include "Relocations.h"
19 #include "SymbolTable.h"
20 #include "SyntheticSections.h"
23 #include "llvm/ADT/StringMap.h"
24 #include "llvm/ADT/StringSwitch.h"
25 #include "llvm/Support/FileOutputBuffer.h"
26 #include "llvm/Support/raw_ostream.h"
30 using namespace llvm::ELF;
31 using namespace llvm::object;
32 using namespace llvm::support;
33 using namespace llvm::support::endian;
36 using namespace lld::elf;
39 // The writer writes a SymbolTable result to a file.
40 template <class ELFT> class Writer {
42 typedef typename ELFT::Shdr Elf_Shdr;
43 typedef typename ELFT::Ehdr Elf_Ehdr;
44 typedef typename ELFT::Phdr Elf_Phdr;
49 void createSyntheticSections();
50 void copyLocalSymbols();
51 void addSectionSymbols();
52 void addReservedSymbols();
53 void createSections();
54 void forEachRelSec(std::function<void(InputSectionBase &)> Fn);
56 void finalizeSections();
57 void addPredefinedSections();
59 std::vector<PhdrEntry> createPhdrs();
60 void removeEmptyPTLoad();
61 void addPtArmExid(std::vector<PhdrEntry> &Phdrs);
62 void assignFileOffsets();
63 void assignFileOffsetsBinary();
66 void fixSectionAlignments();
67 void fixPredefinedSymbols();
71 void writeSectionsBinary();
74 std::unique_ptr<FileOutputBuffer> Buffer;
76 std::vector<OutputSection *> OutputSections;
77 OutputSectionFactory Factory{OutputSections};
79 void addRelIpltSymbols();
80 void addStartEndSymbols();
81 void addStartStopSymbols(OutputSection *Sec);
82 uint64_t getEntryAddr();
83 OutputSection *findSection(StringRef Name);
85 std::vector<PhdrEntry> Phdrs;
88 uint64_t SectionHeaderOff;
89 bool AllocateHeader = true;
91 } // anonymous namespace
93 StringRef elf::getOutputSectionName(StringRef Name) {
94 if (Config->Relocatable)
97 // If -emit-relocs is given (which is rare), we need to copy
98 // relocation sections to the output. If input section .foo is
99 // output as .bar, we want to rename .rel.foo .rel.bar as well.
100 if (Config->EmitRelocs) {
101 for (StringRef V : {".rel.", ".rela."}) {
102 if (Name.startswith(V)) {
103 StringRef Inner = getOutputSectionName(Name.substr(V.size() - 1));
104 return Saver.save(Twine(V.drop_back()) + Inner);
110 {".text.", ".rodata.", ".data.rel.ro.", ".data.", ".bss.rel.ro.",
111 ".bss.", ".init_array.", ".fini_array.", ".ctors.", ".dtors.", ".tbss.",
112 ".gcc_except_table.", ".tdata.", ".ARM.exidx."}) {
113 StringRef Prefix = V.drop_back();
114 if (Name.startswith(V) || Name == Prefix)
118 // CommonSection is identified as "COMMON" in linker scripts.
119 // By default, it should go to .bss section.
120 if (Name == "COMMON")
123 // ".zdebug_" is a prefix for ZLIB-compressed sections.
124 // Because we decompressed input sections, we want to remove 'z'.
125 if (Name.startswith(".zdebug_"))
126 return Saver.save(Twine(".") + Name.substr(2));
130 template <class ELFT> static bool needsInterpSection() {
131 return !Symtab<ELFT>::X->getSharedFiles().empty() &&
132 !Config->DynamicLinker.empty() && !Script->ignoreInterpSection();
135 template <class ELFT> void elf::writeResult() { Writer<ELFT>().run(); }
137 template <class ELFT> void Writer<ELFT>::removeEmptyPTLoad() {
138 auto I = std::remove_if(Phdrs.begin(), Phdrs.end(), [&](const PhdrEntry &P) {
139 if (P.p_type != PT_LOAD)
143 uint64_t Size = P.Last->Addr + P.Last->Size - P.First->Addr;
146 Phdrs.erase(I, Phdrs.end());
149 // This function scans over the input sections and creates mergeable
150 // synthetic sections. It removes MergeInputSections from array and
151 // adds new synthetic ones. Each synthetic section is added to the
152 // location of the first input section it replaces.
153 static void combineMergableSections() {
154 std::vector<MergeSyntheticSection *> MergeSections;
155 for (InputSectionBase *&S : InputSections) {
156 MergeInputSection *MS = dyn_cast<MergeInputSection>(S);
160 // We do not want to handle sections that are not alive, so just remove
161 // them instead of trying to merge.
165 StringRef OutsecName = getOutputSectionName(MS->Name);
166 uint64_t Flags = MS->Flags & ~(uint64_t)(SHF_GROUP | SHF_COMPRESSED);
167 uint32_t Alignment = std::max<uint32_t>(MS->Alignment, MS->Entsize);
170 llvm::find_if(MergeSections, [=](MergeSyntheticSection *Sec) {
171 return Sec->Name == OutsecName && Sec->Flags == Flags &&
172 Sec->Alignment == Alignment;
174 if (I == MergeSections.end()) {
175 MergeSyntheticSection *Syn =
176 make<MergeSyntheticSection>(OutsecName, MS->Type, Flags, Alignment);
177 MergeSections.push_back(Syn);
178 I = std::prev(MergeSections.end());
183 (*I)->addSection(MS);
186 std::vector<InputSectionBase *> &V = InputSections;
187 V.erase(std::remove(V.begin(), V.end(), nullptr), V.end());
190 template <class ELFT> static void combineEhFrameSections() {
191 for (InputSectionBase *&S : InputSections) {
192 EhInputSection *ES = dyn_cast<EhInputSection>(S);
193 if (!ES || !ES->Live)
196 In<ELFT>::EhFrame->addSection(ES);
200 std::vector<InputSectionBase *> &V = InputSections;
201 V.erase(std::remove(V.begin(), V.end(), nullptr), V.end());
204 // The main function of the writer.
205 template <class ELFT> void Writer<ELFT>::run() {
206 // Create linker-synthesized sections such as .got or .plt.
207 // Such sections are of type input section.
208 createSyntheticSections();
209 combineMergableSections();
211 if (!Config->Relocatable)
212 combineEhFrameSections<ELFT>();
214 // We need to create some reserved symbols such as _end. Create them.
215 if (!Config->Relocatable)
216 addReservedSymbols();
218 // Create output sections.
219 Script->OutputSections = &OutputSections;
220 if (Script->Opt.HasSections) {
221 // If linker script contains SECTIONS commands, let it create sections.
222 Script->processCommands(Factory);
224 // Linker scripts may have left some input sections unassigned.
225 // Assign such sections using the default rule.
226 Script->addOrphanSections(Factory);
228 // If linker script does not contain SECTIONS commands, create
229 // output sections by default rules. We still need to give the
230 // linker script a chance to run, because it might contain
231 // non-SECTIONS commands such as ASSERT.
233 Script->processCommands(Factory);
236 if (Config->Discard != DiscardPolicy::All)
239 if (Config->CopyRelocs)
242 // Now that we have a complete set of output sections. This function
243 // completes section contents. For example, we need to add strings
244 // to the string table, and add entries to .got and .plt.
245 // finalizeSections does that.
250 if (Config->Relocatable) {
253 if (!Script->Opt.HasSections) {
254 fixSectionAlignments();
255 Script->fabricateDefaultCommands(Config->MaxPageSize);
257 Script->assignAddresses(Phdrs);
259 // Remove empty PT_LOAD to avoid causing the dynamic linker to try to mmap a
260 // 0 sized region. This has to be done late since only after assignAddresses
261 // we know the size of the sections.
264 if (!Config->OFormatBinary)
267 assignFileOffsetsBinary();
270 fixPredefinedSymbols();
273 // It does not make sense try to open the file if we have error already.
276 // Write the result down to a file.
280 if (!Config->OFormatBinary) {
284 writeSectionsBinary();
287 // Backfill .note.gnu.build-id section content. This is done at last
288 // because the content is usually a hash value of the entire output file.
293 // Handle -Map option.
294 writeMapFile<ELFT>(OutputSections);
298 if (auto EC = Buffer->commit())
299 error("failed to write to the output file: " + EC.message());
301 // Flush the output streams and exit immediately. A full shutdown
302 // is a good test that we are keeping track of all allocated memory,
303 // but actually freeing it is a waste of time in a regular linker run.
304 if (Config->ExitEarly)
308 // Initialize Out members.
309 template <class ELFT> void Writer<ELFT>::createSyntheticSections() {
310 // Initialize all pointers with NULL. This is needed because
311 // you can call lld::elf::main more than once as a library.
312 memset(&Out::First, 0, sizeof(Out));
314 auto Add = [](InputSectionBase *Sec) { InputSections.push_back(Sec); };
316 In<ELFT>::DynStrTab = make<StringTableSection>(".dynstr", true);
317 In<ELFT>::Dynamic = make<DynamicSection<ELFT>>();
318 In<ELFT>::RelaDyn = make<RelocationSection<ELFT>>(
319 Config->IsRela ? ".rela.dyn" : ".rel.dyn", Config->ZCombreloc);
320 In<ELFT>::ShStrTab = make<StringTableSection>(".shstrtab", false);
322 Out::ElfHeader = make<OutputSection>("", 0, SHF_ALLOC);
323 Out::ElfHeader->Size = sizeof(Elf_Ehdr);
324 Out::ProgramHeaders = make<OutputSection>("", 0, SHF_ALLOC);
325 Out::ProgramHeaders->updateAlignment(Config->Wordsize);
327 if (needsInterpSection<ELFT>()) {
328 In<ELFT>::Interp = createInterpSection();
329 Add(In<ELFT>::Interp);
331 In<ELFT>::Interp = nullptr;
334 if (!Config->Relocatable)
335 Add(createCommentSection<ELFT>());
337 if (Config->Strip != StripPolicy::All) {
338 In<ELFT>::StrTab = make<StringTableSection>(".strtab", false);
339 In<ELFT>::SymTab = make<SymbolTableSection<ELFT>>(*In<ELFT>::StrTab);
342 if (Config->BuildId != BuildIdKind::None) {
343 In<ELFT>::BuildId = make<BuildIdSection>();
344 Add(In<ELFT>::BuildId);
347 In<ELFT>::Common = createCommonSection<ELFT>();
348 if (In<ELFT>::Common)
351 In<ELFT>::Bss = make<BssSection>(".bss");
353 In<ELFT>::BssRelRo = make<BssSection>(".bss.rel.ro");
354 Add(In<ELFT>::BssRelRo);
356 // Add MIPS-specific sections.
357 bool HasDynSymTab = !Symtab<ELFT>::X->getSharedFiles().empty() ||
358 Config->Pic || Config->ExportDynamic;
359 if (Config->EMachine == EM_MIPS) {
360 if (!Config->Shared && HasDynSymTab) {
361 In<ELFT>::MipsRldMap = make<MipsRldMapSection>();
362 Add(In<ELFT>::MipsRldMap);
364 if (auto *Sec = MipsAbiFlagsSection<ELFT>::create())
366 if (auto *Sec = MipsOptionsSection<ELFT>::create())
368 if (auto *Sec = MipsReginfoSection<ELFT>::create())
373 In<ELFT>::DynSymTab = make<SymbolTableSection<ELFT>>(*In<ELFT>::DynStrTab);
374 Add(In<ELFT>::DynSymTab);
376 In<ELFT>::VerSym = make<VersionTableSection<ELFT>>();
377 Add(In<ELFT>::VerSym);
379 if (!Config->VersionDefinitions.empty()) {
380 In<ELFT>::VerDef = make<VersionDefinitionSection<ELFT>>();
381 Add(In<ELFT>::VerDef);
384 In<ELFT>::VerNeed = make<VersionNeedSection<ELFT>>();
385 Add(In<ELFT>::VerNeed);
387 if (Config->GnuHash) {
388 In<ELFT>::GnuHashTab = make<GnuHashTableSection<ELFT>>();
389 Add(In<ELFT>::GnuHashTab);
392 if (Config->SysvHash) {
393 In<ELFT>::HashTab = make<HashTableSection<ELFT>>();
394 Add(In<ELFT>::HashTab);
397 Add(In<ELFT>::Dynamic);
398 Add(In<ELFT>::DynStrTab);
399 Add(In<ELFT>::RelaDyn);
402 // Add .got. MIPS' .got is so different from the other archs,
403 // it has its own class.
404 if (Config->EMachine == EM_MIPS) {
405 In<ELFT>::MipsGot = make<MipsGotSection>();
406 Add(In<ELFT>::MipsGot);
408 In<ELFT>::Got = make<GotSection<ELFT>>();
412 In<ELFT>::GotPlt = make<GotPltSection>();
413 Add(In<ELFT>::GotPlt);
414 In<ELFT>::IgotPlt = make<IgotPltSection>();
415 Add(In<ELFT>::IgotPlt);
417 if (Config->GdbIndex) {
418 In<ELFT>::GdbIndex = make<GdbIndexSection>();
419 Add(In<ELFT>::GdbIndex);
422 // We always need to add rel[a].plt to output if it has entries.
423 // Even for static linking it can contain R_[*]_IRELATIVE relocations.
424 In<ELFT>::RelaPlt = make<RelocationSection<ELFT>>(
425 Config->IsRela ? ".rela.plt" : ".rel.plt", false /*Sort*/);
426 Add(In<ELFT>::RelaPlt);
428 // The RelaIplt immediately follows .rel.plt (.rel.dyn for ARM) to ensure
429 // that the IRelative relocations are processed last by the dynamic loader
430 In<ELFT>::RelaIplt = make<RelocationSection<ELFT>>(
431 (Config->EMachine == EM_ARM) ? ".rel.dyn" : In<ELFT>::RelaPlt->Name,
433 Add(In<ELFT>::RelaIplt);
435 In<ELFT>::Plt = make<PltSection>(Target->PltHeaderSize);
437 In<ELFT>::Iplt = make<PltSection>(0);
440 if (!Config->Relocatable) {
441 if (Config->EhFrameHdr) {
442 In<ELFT>::EhFrameHdr = make<EhFrameHeader<ELFT>>();
443 Add(In<ELFT>::EhFrameHdr);
445 In<ELFT>::EhFrame = make<EhFrameSection<ELFT>>();
446 Add(In<ELFT>::EhFrame);
449 if (In<ELFT>::SymTab)
450 Add(In<ELFT>::SymTab);
451 Add(In<ELFT>::ShStrTab);
452 if (In<ELFT>::StrTab)
453 Add(In<ELFT>::StrTab);
456 static bool shouldKeepInSymtab(SectionBase *Sec, StringRef SymName,
457 const SymbolBody &B) {
458 if (B.isFile() || B.isSection())
461 // If sym references a section in a discarded group, don't keep it.
462 if (Sec == &InputSection::Discarded)
465 if (Config->Discard == DiscardPolicy::None)
468 // In ELF assembly .L symbols are normally discarded by the assembler.
469 // If the assembler fails to do so, the linker discards them if
470 // * --discard-locals is used.
471 // * The symbol is in a SHF_MERGE section, which is normally the reason for
472 // the assembler keeping the .L symbol.
473 if (!SymName.startswith(".L") && !SymName.empty())
476 if (Config->Discard == DiscardPolicy::Locals)
479 return !Sec || !(Sec->Flags & SHF_MERGE);
482 static bool includeInSymtab(const SymbolBody &B) {
483 if (!B.isLocal() && !B.symbol()->IsUsedInRegularObj)
486 if (auto *D = dyn_cast<DefinedRegular>(&B)) {
487 // Always include absolute symbols.
488 SectionBase *Sec = D->Section;
491 if (auto *IS = dyn_cast<InputSectionBase>(Sec)) {
493 IS = cast<InputSectionBase>(Sec);
494 // Exclude symbols pointing to garbage-collected sections.
498 if (auto *S = dyn_cast<MergeInputSection>(Sec))
499 if (!S->getSectionPiece(D->Value)->Live)
505 // Local symbols are not in the linker's symbol table. This function scans
506 // each object file's symbol table to copy local symbols to the output.
507 template <class ELFT> void Writer<ELFT>::copyLocalSymbols() {
508 if (!In<ELFT>::SymTab)
510 for (elf::ObjectFile<ELFT> *F : Symtab<ELFT>::X->getObjectFiles()) {
511 for (SymbolBody *B : F->getLocalSymbols()) {
514 ": broken object: getLocalSymbols returns a non-local symbol");
515 auto *DR = dyn_cast<DefinedRegular>(B);
517 // No reason to keep local undefined symbol in symtab.
520 if (!includeInSymtab(*B))
523 SectionBase *Sec = DR->Section;
524 if (!shouldKeepInSymtab(Sec, B->getName(), *B))
526 In<ELFT>::SymTab->addSymbol(B);
531 template <class ELFT> void Writer<ELFT>::addSectionSymbols() {
532 // Create one STT_SECTION symbol for each output section we might
533 // have a relocation with.
534 for (OutputSection *Sec : OutputSections) {
535 if (Sec->Sections.empty())
538 InputSection *IS = Sec->Sections[0];
539 if (isa<SyntheticSection>(IS) || IS->Type == SHT_REL ||
540 IS->Type == SHT_RELA)
544 make<DefinedRegular>("", /*IsLocal=*/true, /*StOther=*/0, STT_SECTION,
545 /*Value=*/0, /*Size=*/0, IS, nullptr);
546 In<ELFT>::SymTab->addSymbol(Sym);
550 // PPC64 has a number of special SHT_PROGBITS+SHF_ALLOC+SHF_WRITE sections that
551 // we would like to make sure appear is a specific order to maximize their
552 // coverage by a single signed 16-bit offset from the TOC base pointer.
553 // Conversely, the special .tocbss section should be first among all SHT_NOBITS
554 // sections. This will put it next to the loaded special PPC64 sections (and,
555 // thus, within reach of the TOC base pointer).
556 static int getPPC64SectionRank(StringRef SectionName) {
557 return StringSwitch<int>(SectionName)
559 .Case(".branch_lt", 2)
566 // All sections with SHF_MIPS_GPREL flag should be grouped together
567 // because data in these sections is addressable with a gp relative address.
568 static int getMipsSectionRank(const OutputSection *S) {
569 if ((S->Flags & SHF_MIPS_GPREL) == 0)
571 if (S->Name == ".got")
576 // Today's loaders have a feature to make segments read-only after
577 // processing dynamic relocations to enhance security. PT_GNU_RELRO
578 // is defined for that.
580 // This function returns true if a section needs to be put into a
581 // PT_GNU_RELRO segment.
582 template <class ELFT> bool elf::isRelroSection(const OutputSection *Sec) {
586 uint64_t Flags = Sec->Flags;
588 // Non-allocatable or non-writable sections don't need RELRO because
589 // they are not writable or not even mapped to memory in the first place.
590 // RELRO is for sections that are essentially read-only but need to
591 // be writable only at process startup to allow dynamic linker to
592 // apply relocations.
593 if (!(Flags & SHF_ALLOC) || !(Flags & SHF_WRITE))
596 // Once initialized, TLS data segments are used as data templates
597 // for a thread-local storage. For each new thread, runtime
598 // allocates memory for a TLS and copy templates there. No thread
599 // are supposed to use templates directly. Thus, it can be in RELRO.
603 // .init_array, .preinit_array and .fini_array contain pointers to
604 // functions that are executed on process startup or exit. These
605 // pointers are set by the static linker, and they are not expected
606 // to change at runtime. But if you are an attacker, you could do
607 // interesting things by manipulating pointers in .fini_array, for
608 // example. So they are put into RELRO.
609 uint32_t Type = Sec->Type;
610 if (Type == SHT_INIT_ARRAY || Type == SHT_FINI_ARRAY ||
611 Type == SHT_PREINIT_ARRAY)
614 // .got contains pointers to external symbols. They are resolved by
615 // the dynamic linker when a module is loaded into memory, and after
616 // that they are not expected to change. So, it can be in RELRO.
617 if (In<ELFT>::Got && Sec == In<ELFT>::Got->OutSec)
620 // .got.plt contains pointers to external function symbols. They are
621 // by default resolved lazily, so we usually cannot put it into RELRO.
622 // However, if "-z now" is given, the lazy symbol resolution is
623 // disabled, which enables us to put it into RELRO.
624 if (Sec == In<ELFT>::GotPlt->OutSec)
627 // .dynamic section contains data for the dynamic linker, and
628 // there's no need to write to it at runtime, so it's better to put
630 if (Sec == In<ELFT>::Dynamic->OutSec)
633 // .bss.rel.ro is used for copy relocations for read-only symbols.
634 // Since the dynamic linker needs to process copy relocations, the
635 // section cannot be read-only, but once initialized, they shouldn't
637 if (Sec == In<ELFT>::BssRelRo->OutSec)
640 // Sections with some special names are put into RELRO. This is a
641 // bit unfortunate because section names shouldn't be significant in
642 // ELF in spirit. But in reality many linker features depend on
643 // magic section names.
644 StringRef S = Sec->Name;
645 return S == ".data.rel.ro" || S == ".ctors" || S == ".dtors" || S == ".jcr" ||
646 S == ".eh_frame" || S == ".openbsd.randomdata";
649 template <class ELFT>
650 static bool compareSectionsNonScript(const OutputSection *A,
651 const OutputSection *B) {
652 // Put .interp first because some loaders want to see that section
653 // on the first page of the executable file when loaded into memory.
654 bool AIsInterp = A->Name == ".interp";
655 bool BIsInterp = B->Name == ".interp";
656 if (AIsInterp != BIsInterp)
659 // Allocatable sections go first to reduce the total PT_LOAD size and
660 // so debug info doesn't change addresses in actual code.
661 bool AIsAlloc = A->Flags & SHF_ALLOC;
662 bool BIsAlloc = B->Flags & SHF_ALLOC;
663 if (AIsAlloc != BIsAlloc)
666 // We don't have any special requirements for the relative order of two non
667 // allocatable sections.
671 // We want to put section specified by -T option first, so we
672 // can start assigning VA starting from them later.
673 auto AAddrSetI = Config->SectionStartMap.find(A->Name);
674 auto BAddrSetI = Config->SectionStartMap.find(B->Name);
675 bool AHasAddrSet = AAddrSetI != Config->SectionStartMap.end();
676 bool BHasAddrSet = BAddrSetI != Config->SectionStartMap.end();
677 if (AHasAddrSet != BHasAddrSet)
680 return AAddrSetI->second < BAddrSetI->second;
682 // We want the read only sections first so that they go in the PT_LOAD
683 // covering the program headers at the start of the file.
684 bool AIsWritable = A->Flags & SHF_WRITE;
685 bool BIsWritable = B->Flags & SHF_WRITE;
686 if (AIsWritable != BIsWritable)
689 if (!Config->SingleRoRx) {
690 // For a corresponding reason, put non exec sections first (the program
691 // header PT_LOAD is not executable).
692 // We only do that if we are not using linker scripts, since with linker
693 // scripts ro and rx sections are in the same PT_LOAD, so their relative
694 // order is not important. The same applies for -no-rosegment.
695 bool AIsExec = A->Flags & SHF_EXECINSTR;
696 bool BIsExec = B->Flags & SHF_EXECINSTR;
697 if (AIsExec != BIsExec)
701 // If we got here we know that both A and B are in the same PT_LOAD.
703 bool AIsTls = A->Flags & SHF_TLS;
704 bool BIsTls = B->Flags & SHF_TLS;
705 bool AIsNoBits = A->Type == SHT_NOBITS;
706 bool BIsNoBits = B->Type == SHT_NOBITS;
708 // The first requirement we have is to put (non-TLS) nobits sections last. The
709 // reason is that the only thing the dynamic linker will see about them is a
710 // p_memsz that is larger than p_filesz. Seeing that it zeros the end of the
711 // PT_LOAD, so that has to correspond to the nobits sections.
712 bool AIsNonTlsNoBits = AIsNoBits && !AIsTls;
713 bool BIsNonTlsNoBits = BIsNoBits && !BIsTls;
714 if (AIsNonTlsNoBits != BIsNonTlsNoBits)
715 return BIsNonTlsNoBits;
717 // We place nobits RelRo sections before plain r/w ones, and non-nobits RelRo
718 // sections after r/w ones, so that the RelRo sections are contiguous.
719 bool AIsRelRo = isRelroSection<ELFT>(A);
720 bool BIsRelRo = isRelroSection<ELFT>(B);
721 if (AIsRelRo != BIsRelRo)
722 return AIsNonTlsNoBits ? AIsRelRo : BIsRelRo;
724 // The TLS initialization block needs to be a single contiguous block in a R/W
725 // PT_LOAD, so stick TLS sections directly before the other RelRo R/W
726 // sections. The TLS NOBITS sections are placed here as they don't take up
727 // virtual address space in the PT_LOAD.
728 if (AIsTls != BIsTls)
731 // Within the TLS initialization block, the non-nobits sections need to appear
733 if (AIsNoBits != BIsNoBits)
736 // Some architectures have additional ordering restrictions for sections
737 // within the same PT_LOAD.
738 if (Config->EMachine == EM_PPC64)
739 return getPPC64SectionRank(A->Name) < getPPC64SectionRank(B->Name);
740 if (Config->EMachine == EM_MIPS)
741 return getMipsSectionRank(A) < getMipsSectionRank(B);
746 // Output section ordering is determined by this function.
747 template <class ELFT>
748 static bool compareSections(const OutputSection *A, const OutputSection *B) {
749 // For now, put sections mentioned in a linker script first.
750 int AIndex = Script->getSectionIndex(A->Name);
751 int BIndex = Script->getSectionIndex(B->Name);
752 bool AInScript = AIndex != INT_MAX;
753 bool BInScript = BIndex != INT_MAX;
754 if (AInScript != BInScript)
756 // If both are in the script, use that order.
758 return AIndex < BIndex;
760 return compareSectionsNonScript<ELFT>(A, B);
763 // Program header entry
764 PhdrEntry::PhdrEntry(unsigned Type, unsigned Flags) {
769 void PhdrEntry::add(OutputSection *Sec) {
773 p_align = std::max(p_align, Sec->Alignment);
774 if (p_type == PT_LOAD)
775 Sec->FirstInPtLoad = First;
778 template <class ELFT>
779 static Symbol *addRegular(StringRef Name, SectionBase *Sec, uint64_t Value,
780 uint8_t StOther = STV_HIDDEN,
781 uint8_t Binding = STB_WEAK) {
782 // The linker generated symbols are added as STB_WEAK to allow user defined
783 // ones to override them.
784 return Symtab<ELFT>::X->addRegular(Name, StOther, STT_NOTYPE, Value,
785 /*Size=*/0, Binding, Sec,
789 template <class ELFT>
790 static DefinedRegular *
791 addOptionalRegular(StringRef Name, SectionBase *Sec, uint64_t Val,
792 uint8_t StOther = STV_HIDDEN, uint8_t Binding = STB_GLOBAL) {
793 SymbolBody *S = Symtab<ELFT>::X->find(Name);
796 if (S->isInCurrentDSO())
798 return cast<DefinedRegular>(
799 addRegular<ELFT>(Name, Sec, Val, StOther, Binding)->body());
802 // The beginning and the ending of .rel[a].plt section are marked
803 // with __rel[a]_iplt_{start,end} symbols if it is a statically linked
804 // executable. The runtime needs these symbols in order to resolve
805 // all IRELATIVE relocs on startup. For dynamic executables, we don't
806 // need these symbols, since IRELATIVE relocs are resolved through GOT
807 // and PLT. For details, see http://www.airs.com/blog/archives/403.
808 template <class ELFT> void Writer<ELFT>::addRelIpltSymbols() {
809 if (In<ELFT>::DynSymTab)
811 StringRef S = Config->IsRela ? "__rela_iplt_start" : "__rel_iplt_start";
812 addOptionalRegular<ELFT>(S, In<ELFT>::RelaIplt, 0, STV_HIDDEN, STB_WEAK);
814 S = Config->IsRela ? "__rela_iplt_end" : "__rel_iplt_end";
815 addOptionalRegular<ELFT>(S, In<ELFT>::RelaIplt, -1, STV_HIDDEN, STB_WEAK);
818 // The linker is expected to define some symbols depending on
819 // the linking result. This function defines such symbols.
820 template <class ELFT> void Writer<ELFT>::addReservedSymbols() {
821 if (Config->EMachine == EM_MIPS) {
822 // Define _gp for MIPS. st_value of _gp symbol will be updated by Writer
823 // so that it points to an absolute address which by default is relative
824 // to GOT. Default offset is 0x7ff0.
825 // See "Global Data Symbols" in Chapter 6 in the following document:
826 // ftp://www.linux-mips.org/pub/linux/mips/doc/ABI/mipsabi.pdf
827 ElfSym::MipsGp = Symtab<ELFT>::X->addAbsolute("_gp", STV_HIDDEN, STB_LOCAL);
829 // On MIPS O32 ABI, _gp_disp is a magic symbol designates offset between
830 // start of function and 'gp' pointer into GOT.
831 if (Symtab<ELFT>::X->find("_gp_disp"))
833 Symtab<ELFT>::X->addAbsolute("_gp_disp", STV_HIDDEN, STB_LOCAL);
835 // The __gnu_local_gp is a magic symbol equal to the current value of 'gp'
836 // pointer. This symbol is used in the code generated by .cpload pseudo-op
837 // in case of using -mno-shared option.
838 // https://sourceware.org/ml/binutils/2004-12/msg00094.html
839 if (Symtab<ELFT>::X->find("__gnu_local_gp"))
840 ElfSym::MipsLocalGp =
841 Symtab<ELFT>::X->addAbsolute("__gnu_local_gp", STV_HIDDEN, STB_LOCAL);
844 // In the assembly for 32 bit x86 the _GLOBAL_OFFSET_TABLE_ symbol
845 // is magical and is used to produce a R_386_GOTPC relocation.
846 // The R_386_GOTPC relocation value doesn't actually depend on the
847 // symbol value, so it could use an index of STN_UNDEF which, according
848 // to the spec, means the symbol value is 0.
849 // Unfortunately both gas and MC keep the _GLOBAL_OFFSET_TABLE_ symbol in
851 // The situation is even stranger on x86_64 where the assembly doesn't
852 // need the magical symbol, but gas still puts _GLOBAL_OFFSET_TABLE_ as
853 // an undefined symbol in the .o files.
854 // Given that the symbol is effectively unused, we just create a dummy
855 // hidden one to avoid the undefined symbol error.
856 Symtab<ELFT>::X->addIgnored("_GLOBAL_OFFSET_TABLE_");
858 // __tls_get_addr is defined by the dynamic linker for dynamic ELFs. For
859 // static linking the linker is required to optimize away any references to
860 // __tls_get_addr, so it's not defined anywhere. Create a hidden definition
861 // to avoid the undefined symbol error. As usual special cases are ARM and
862 // MIPS - the libc for these targets defines __tls_get_addr itself because
863 // there are no TLS optimizations for these targets.
864 if (!In<ELFT>::DynSymTab &&
865 (Config->EMachine != EM_MIPS && Config->EMachine != EM_ARM))
866 Symtab<ELFT>::X->addIgnored("__tls_get_addr");
868 // If linker script do layout we do not need to create any standart symbols.
869 if (Script->Opt.HasSections)
872 // __ehdr_start is the location of ELF file headers.
873 addOptionalRegular<ELFT>("__ehdr_start", Out::ElfHeader, 0, STV_HIDDEN);
875 auto Add = [](StringRef S) {
876 return addOptionalRegular<ELFT>(S, Out::ElfHeader, 0, STV_DEFAULT);
879 ElfSym::Bss = Add("__bss_start");
880 ElfSym::End1 = Add("end");
881 ElfSym::End2 = Add("_end");
882 ElfSym::Etext1 = Add("etext");
883 ElfSym::Etext2 = Add("_etext");
884 ElfSym::Edata1 = Add("edata");
885 ElfSym::Edata2 = Add("_edata");
888 // Sort input sections by section name suffixes for
889 // __attribute__((init_priority(N))).
890 static void sortInitFini(OutputSection *S) {
892 reinterpret_cast<OutputSection *>(S)->sortInitFini();
895 // Sort input sections by the special rule for .ctors and .dtors.
896 static void sortCtorsDtors(OutputSection *S) {
898 reinterpret_cast<OutputSection *>(S)->sortCtorsDtors();
901 // Sort input sections using the list provided by --symbol-ordering-file.
902 template <class ELFT>
903 static void sortBySymbolsOrder(ArrayRef<OutputSection *> OutputSections) {
904 if (Config->SymbolOrderingFile.empty())
907 // Build a map from symbols to their priorities. Symbols that didn't
908 // appear in the symbol ordering file have the lowest priority 0.
909 // All explicitly mentioned symbols have negative (higher) priorities.
910 DenseMap<StringRef, int> SymbolOrder;
911 int Priority = -Config->SymbolOrderingFile.size();
912 for (StringRef S : Config->SymbolOrderingFile)
913 SymbolOrder.insert({S, Priority++});
915 // Build a map from sections to their priorities.
916 DenseMap<SectionBase *, int> SectionOrder;
917 for (elf::ObjectFile<ELFT> *File : Symtab<ELFT>::X->getObjectFiles()) {
918 for (SymbolBody *Body : File->getSymbols()) {
919 auto *D = dyn_cast<DefinedRegular>(Body);
920 if (!D || !D->Section)
922 int &Priority = SectionOrder[D->Section];
923 Priority = std::min(Priority, SymbolOrder.lookup(D->getName()));
927 // Sort sections by priority.
928 for (OutputSection *Base : OutputSections)
929 if (auto *Sec = dyn_cast<OutputSection>(Base))
930 Sec->sort([&](InputSectionBase *S) { return SectionOrder.lookup(S); });
933 template <class ELFT>
934 void Writer<ELFT>::forEachRelSec(std::function<void(InputSectionBase &)> Fn) {
935 for (InputSectionBase *IS : InputSections) {
938 // Scan all relocations. Each relocation goes through a series
939 // of tests to determine if it needs special treatment, such as
940 // creating GOT, PLT, copy relocations, etc.
941 // Note that relocations for non-alloc sections are directly
942 // processed by InputSection::relocateNonAlloc.
943 if (!(IS->Flags & SHF_ALLOC))
945 if (isa<InputSection>(IS) || isa<EhInputSection>(IS))
949 if (!Config->Relocatable) {
950 for (EhInputSection *ES : In<ELFT>::EhFrame->Sections)
955 template <class ELFT> void Writer<ELFT>::createSections() {
956 for (InputSectionBase *IS : InputSections)
958 Factory.addInputSec(IS, getOutputSectionName(IS->Name));
960 sortBySymbolsOrder<ELFT>(OutputSections);
961 sortInitFini(findSection(".init_array"));
962 sortInitFini(findSection(".fini_array"));
963 sortCtorsDtors(findSection(".ctors"));
964 sortCtorsDtors(findSection(".dtors"));
966 for (OutputSection *Sec : OutputSections)
967 Sec->assignOffsets();
970 static bool canSharePtLoad(const OutputSection &S1, const OutputSection &S2) {
971 if (!(S1.Flags & SHF_ALLOC) || !(S2.Flags & SHF_ALLOC))
974 bool S1IsWrite = S1.Flags & SHF_WRITE;
975 bool S2IsWrite = S2.Flags & SHF_WRITE;
976 if (S1IsWrite != S2IsWrite)
980 return true; // RO and RX share a PT_LOAD with linker scripts.
981 return (S1.Flags & SHF_EXECINSTR) == (S2.Flags & SHF_EXECINSTR);
984 template <class ELFT> void Writer<ELFT>::sortSections() {
985 // Don't sort if using -r. It is not necessary and we want to preserve the
986 // relative order for SHF_LINK_ORDER sections.
987 if (Config->Relocatable)
989 if (!Script->Opt.HasSections) {
990 std::stable_sort(OutputSections.begin(), OutputSections.end(),
991 compareSectionsNonScript<ELFT>);
994 Script->adjustSectionsBeforeSorting();
996 // The order of the sections in the script is arbitrary and may not agree with
997 // compareSectionsNonScript. This means that we cannot easily define a
998 // strict weak ordering. To see why, consider a comparison of a section in the
999 // script and one not in the script. We have a two simple options:
1000 // * Make them equivalent (a is not less than b, and b is not less than a).
1001 // The problem is then that equivalence has to be transitive and we can
1002 // have sections a, b and c with only b in a script and a less than c
1003 // which breaks this property.
1004 // * Use compareSectionsNonScript. Given that the script order doesn't have
1005 // to match, we can end up with sections a, b, c, d where b and c are in the
1006 // script and c is compareSectionsNonScript less than b. In which case d
1007 // can be equivalent to c, a to b and d < a. As a concrete example:
1008 // .a (rx) # not in script
1009 // .b (rx) # in script
1010 // .c (ro) # in script
1011 // .d (ro) # not in script
1013 // The way we define an order then is:
1014 // * First put script sections at the start and sort the script and
1015 // non-script sections independently.
1016 // * Move each non-script section to its preferred position. We try
1017 // to put each section in the last position where it it can share
1020 std::stable_sort(OutputSections.begin(), OutputSections.end(),
1021 compareSections<ELFT>);
1023 auto I = OutputSections.begin();
1024 auto E = OutputSections.end();
1026 std::find_if(OutputSections.begin(), E, [](OutputSection *S) {
1027 return Script->getSectionIndex(S->Name) == INT_MAX;
1029 while (NonScriptI != E) {
1030 auto BestPos = std::max_element(
1031 I, NonScriptI, [&](OutputSection *&A, OutputSection *&B) {
1032 bool ACanSharePtLoad = canSharePtLoad(**NonScriptI, *A);
1033 bool BCanSharePtLoad = canSharePtLoad(**NonScriptI, *B);
1034 if (ACanSharePtLoad != BCanSharePtLoad)
1035 return BCanSharePtLoad;
1037 bool ACmp = compareSectionsNonScript<ELFT>(*NonScriptI, A);
1038 bool BCmp = compareSectionsNonScript<ELFT>(*NonScriptI, B);
1040 return BCmp; // FIXME: missing test
1042 size_t PosA = &A - &OutputSections[0];
1043 size_t PosB = &B - &OutputSections[0];
1044 return ACmp ? PosA > PosB : PosA < PosB;
1047 // max_element only returns NonScriptI if the range is empty. If the range
1048 // is not empty we should consider moving the the element forward one
1050 if (BestPos != NonScriptI &&
1051 !compareSectionsNonScript<ELFT>(*NonScriptI, *BestPos))
1053 std::rotate(BestPos, NonScriptI, NonScriptI + 1);
1057 Script->adjustSectionsAfterSorting();
1060 static void applySynthetic(const std::vector<SyntheticSection *> &Sections,
1061 std::function<void(SyntheticSection *)> Fn) {
1062 for (SyntheticSection *SS : Sections)
1063 if (SS && SS->OutSec && !SS->empty()) {
1065 SS->OutSec->assignOffsets();
1069 // We need to add input synthetic sections early in createSyntheticSections()
1070 // to make them visible from linkescript side. But not all sections are always
1071 // required to be in output. For example we don't need dynamic section content
1072 // sometimes. This function filters out such unused sections from the output.
1073 static void removeUnusedSyntheticSections(std::vector<OutputSection *> &V) {
1074 // All input synthetic sections that can be empty are placed after
1075 // all regular ones. We iterate over them all and exit at first
1077 for (InputSectionBase *S : llvm::reverse(InputSections)) {
1078 SyntheticSection *SS = dyn_cast<SyntheticSection>(S);
1081 if (!SS->empty() || !SS->OutSec)
1084 SS->OutSec->Sections.erase(std::find(SS->OutSec->Sections.begin(),
1085 SS->OutSec->Sections.end(), SS));
1086 // If there are no other sections in the output section, remove it from the
1088 if (SS->OutSec->Sections.empty())
1089 V.erase(std::find(V.begin(), V.end(), SS->OutSec));
1093 // Create output section objects and add them to OutputSections.
1094 template <class ELFT> void Writer<ELFT>::finalizeSections() {
1095 Out::DebugInfo = findSection(".debug_info");
1096 Out::PreinitArray = findSection(".preinit_array");
1097 Out::InitArray = findSection(".init_array");
1098 Out::FiniArray = findSection(".fini_array");
1100 // The linker needs to define SECNAME_start, SECNAME_end and SECNAME_stop
1101 // symbols for sections, so that the runtime can get the start and end
1102 // addresses of each section by section name. Add such symbols.
1103 if (!Config->Relocatable) {
1104 addStartEndSymbols();
1105 for (OutputSection *Sec : OutputSections)
1106 addStartStopSymbols(Sec);
1109 // Add _DYNAMIC symbol. Unlike GNU gold, our _DYNAMIC symbol has no type.
1110 // It should be okay as no one seems to care about the type.
1111 // Even the author of gold doesn't remember why gold behaves that way.
1112 // https://sourceware.org/ml/binutils/2002-03/msg00360.html
1113 if (In<ELFT>::DynSymTab)
1114 addRegular<ELFT>("_DYNAMIC", In<ELFT>::Dynamic, 0);
1116 // Define __rel[a]_iplt_{start,end} symbols if needed.
1117 addRelIpltSymbols();
1119 // This responsible for splitting up .eh_frame section into
1120 // pieces. The relocation scan uses those pieces, so this has to be
1122 applySynthetic({In<ELFT>::EhFrame},
1123 [](SyntheticSection *SS) { SS->finalizeContents(); });
1125 // Scan relocations. This must be done after every symbol is declared so that
1126 // we can correctly decide if a dynamic relocation is needed.
1127 forEachRelSec(scanRelocations<ELFT>);
1129 if (In<ELFT>::Plt && !In<ELFT>::Plt->empty())
1130 In<ELFT>::Plt->addSymbols();
1131 if (In<ELFT>::Iplt && !In<ELFT>::Iplt->empty())
1132 In<ELFT>::Iplt->addSymbols();
1134 // Now that we have defined all possible global symbols including linker-
1135 // synthesized ones. Visit all symbols to give the finishing touches.
1136 for (Symbol *S : Symtab<ELFT>::X->getSymbols()) {
1137 SymbolBody *Body = S->body();
1139 if (!includeInSymtab(*Body))
1141 if (In<ELFT>::SymTab)
1142 In<ELFT>::SymTab->addSymbol(Body);
1144 if (In<ELFT>::DynSymTab && S->includeInDynsym()) {
1145 In<ELFT>::DynSymTab->addSymbol(Body);
1146 if (auto *SS = dyn_cast<SharedSymbol>(Body))
1147 if (cast<SharedFile<ELFT>>(SS->File)->isNeeded())
1148 In<ELFT>::VerNeed->addSymbol(SS);
1152 // Do not proceed if there was an undefined symbol.
1156 // So far we have added sections from input object files.
1157 // This function adds linker-created Out::* sections.
1158 addPredefinedSections();
1159 removeUnusedSyntheticSections(OutputSections);
1163 // This is a bit of a hack. A value of 0 means undef, so we set it
1164 // to 1 t make __ehdr_start defined. The section number is not
1165 // particularly relevant.
1166 Out::ElfHeader->SectionIndex = 1;
1169 for (OutputSection *Sec : OutputSections) {
1170 Sec->SectionIndex = I++;
1171 Sec->ShName = In<ELFT>::ShStrTab->addString(Sec->Name);
1174 // Binary and relocatable output does not have PHDRS.
1175 // The headers have to be created before finalize as that can influence the
1176 // image base and the dynamic section on mips includes the image base.
1177 if (!Config->Relocatable && !Config->OFormatBinary) {
1178 Phdrs = Script->hasPhdrsCommands() ? Script->createPhdrs() : createPhdrs();
1179 addPtArmExid(Phdrs);
1183 // Dynamic section must be the last one in this list and dynamic
1184 // symbol table section (DynSymTab) must be the first one.
1185 applySynthetic({In<ELFT>::DynSymTab, In<ELFT>::Bss, In<ELFT>::BssRelRo,
1186 In<ELFT>::GnuHashTab, In<ELFT>::HashTab, In<ELFT>::SymTab,
1187 In<ELFT>::ShStrTab, In<ELFT>::StrTab, In<ELFT>::VerDef,
1188 In<ELFT>::DynStrTab, In<ELFT>::GdbIndex, In<ELFT>::Got,
1189 In<ELFT>::MipsGot, In<ELFT>::IgotPlt, In<ELFT>::GotPlt,
1190 In<ELFT>::RelaDyn, In<ELFT>::RelaIplt, In<ELFT>::RelaPlt,
1191 In<ELFT>::Plt, In<ELFT>::Iplt, In<ELFT>::Plt,
1192 In<ELFT>::EhFrameHdr, In<ELFT>::VerSym, In<ELFT>::VerNeed,
1194 [](SyntheticSection *SS) { SS->finalizeContents(); });
1196 // Some architectures use small displacements for jump instructions.
1197 // It is linker's responsibility to create thunks containing long
1198 // jump instructions if jump targets are too far. Create thunks.
1199 if (Target->NeedsThunks) {
1200 // FIXME: only ARM Interworking and Mips LA25 Thunks are implemented,
1202 // do not require address information. To support range extension Thunks
1203 // we need to assign addresses so that we can tell if jump instructions
1204 // are out of range. This will need to turn into a loop that converges
1205 // when no more Thunks are added
1206 ThunkCreator<ELFT> TC;
1207 if (TC.createThunks(OutputSections))
1208 applySynthetic({In<ELFT>::MipsGot},
1209 [](SyntheticSection *SS) { SS->updateAllocSize(); });
1211 // Fill other section headers. The dynamic table is finalized
1212 // at the end because some tags like RELSZ depend on result
1213 // of finalizing other sections.
1214 for (OutputSection *Sec : OutputSections)
1215 Sec->finalize<ELFT>();
1217 // If -compressed-debug-sections is specified, we need to compress
1218 // .debug_* sections. Do it right now because it changes the size of
1220 parallelForEach(OutputSections.begin(), OutputSections.end(),
1221 [](OutputSection *S) { S->maybeCompress<ELFT>(); });
1223 // createThunks may have added local symbols to the static symbol table
1224 applySynthetic({In<ELFT>::SymTab, In<ELFT>::ShStrTab, In<ELFT>::StrTab},
1225 [](SyntheticSection *SS) { SS->postThunkContents(); });
1228 template <class ELFT> void Writer<ELFT>::addPredefinedSections() {
1229 // ARM ABI requires .ARM.exidx to be terminated by some piece of data.
1230 // We have the terminater synthetic section class. Add that at the end.
1231 auto *OS = dyn_cast_or_null<OutputSection>(findSection(".ARM.exidx"));
1232 if (OS && !OS->Sections.empty() && !Config->Relocatable)
1233 OS->addSection(make<ARMExidxSentinelSection>());
1236 // The linker is expected to define SECNAME_start and SECNAME_end
1237 // symbols for a few sections. This function defines them.
1238 template <class ELFT> void Writer<ELFT>::addStartEndSymbols() {
1239 auto Define = [&](StringRef Start, StringRef End, OutputSection *OS) {
1240 // These symbols resolve to the image base if the section does not exist.
1241 // A special value -1 indicates end of the section.
1243 addOptionalRegular<ELFT>(Start, OS, 0);
1244 addOptionalRegular<ELFT>(End, OS, -1);
1247 OS = Out::ElfHeader;
1248 addOptionalRegular<ELFT>(Start, OS, 0);
1249 addOptionalRegular<ELFT>(End, OS, 0);
1253 Define("__preinit_array_start", "__preinit_array_end", Out::PreinitArray);
1254 Define("__init_array_start", "__init_array_end", Out::InitArray);
1255 Define("__fini_array_start", "__fini_array_end", Out::FiniArray);
1257 if (OutputSection *Sec = findSection(".ARM.exidx"))
1258 Define("__exidx_start", "__exidx_end", Sec);
1261 // If a section name is valid as a C identifier (which is rare because of
1262 // the leading '.'), linkers are expected to define __start_<secname> and
1263 // __stop_<secname> symbols. They are at beginning and end of the section,
1264 // respectively. This is not requested by the ELF standard, but GNU ld and
1265 // gold provide the feature, and used by many programs.
1266 template <class ELFT>
1267 void Writer<ELFT>::addStartStopSymbols(OutputSection *Sec) {
1268 StringRef S = Sec->Name;
1269 if (!isValidCIdentifier(S))
1271 addOptionalRegular<ELFT>(Saver.save("__start_" + S), Sec, 0, STV_DEFAULT);
1272 addOptionalRegular<ELFT>(Saver.save("__stop_" + S), Sec, -1, STV_DEFAULT);
1275 template <class ELFT> OutputSection *Writer<ELFT>::findSection(StringRef Name) {
1276 for (OutputSection *Sec : OutputSections)
1277 if (Sec->Name == Name)
1282 static bool needsPtLoad(OutputSection *Sec) {
1283 if (!(Sec->Flags & SHF_ALLOC))
1286 // Don't allocate VA space for TLS NOBITS sections. The PT_TLS PHDR is
1287 // responsible for allocating space for them, not the PT_LOAD that
1288 // contains the TLS initialization image.
1289 if (Sec->Flags & SHF_TLS && Sec->Type == SHT_NOBITS)
1294 // Linker scripts are responsible for aligning addresses. Unfortunately, most
1295 // linker scripts are designed for creating two PT_LOADs only, one RX and one
1296 // RW. This means that there is no alignment in the RO to RX transition and we
1297 // cannot create a PT_LOAD there.
1298 static uint64_t computeFlags(uint64_t Flags) {
1300 return PF_R | PF_W | PF_X;
1301 if (Config->SingleRoRx && !(Flags & PF_W))
1302 return Flags | PF_X;
1306 // Decide which program headers to create and which sections to include in each
1308 template <class ELFT> std::vector<PhdrEntry> Writer<ELFT>::createPhdrs() {
1309 std::vector<PhdrEntry> Ret;
1310 auto AddHdr = [&](unsigned Type, unsigned Flags) -> PhdrEntry * {
1311 Ret.emplace_back(Type, Flags);
1315 // The first phdr entry is PT_PHDR which describes the program header itself.
1316 AddHdr(PT_PHDR, PF_R)->add(Out::ProgramHeaders);
1318 // PT_INTERP must be the second entry if exists.
1319 if (OutputSection *Sec = findSection(".interp"))
1320 AddHdr(PT_INTERP, Sec->getPhdrFlags())->add(Sec);
1322 // Add the first PT_LOAD segment for regular output sections.
1323 uint64_t Flags = computeFlags(PF_R);
1324 PhdrEntry *Load = AddHdr(PT_LOAD, Flags);
1325 for (OutputSection *Sec : OutputSections) {
1326 if (!(Sec->Flags & SHF_ALLOC))
1328 if (!needsPtLoad(Sec))
1331 // Segments are contiguous memory regions that has the same attributes
1332 // (e.g. executable or writable). There is one phdr for each segment.
1333 // Therefore, we need to create a new phdr when the next section has
1334 // different flags or is loaded at a discontiguous address using AT linker
1336 uint64_t NewFlags = computeFlags(Sec->getPhdrFlags());
1337 if (Script->hasLMA(Sec->Name) || Flags != NewFlags) {
1338 Load = AddHdr(PT_LOAD, NewFlags);
1345 // Add a TLS segment if any.
1346 PhdrEntry TlsHdr(PT_TLS, PF_R);
1347 for (OutputSection *Sec : OutputSections)
1348 if (Sec->Flags & SHF_TLS)
1351 Ret.push_back(std::move(TlsHdr));
1353 // Add an entry for .dynamic.
1354 if (In<ELFT>::DynSymTab)
1355 AddHdr(PT_DYNAMIC, In<ELFT>::Dynamic->OutSec->getPhdrFlags())
1356 ->add(In<ELFT>::Dynamic->OutSec);
1358 // PT_GNU_RELRO includes all sections that should be marked as
1359 // read-only by dynamic linker after proccessing relocations.
1360 PhdrEntry RelRo(PT_GNU_RELRO, PF_R);
1361 for (OutputSection *Sec : OutputSections)
1362 if (needsPtLoad(Sec) && isRelroSection<ELFT>(Sec))
1365 Ret.push_back(std::move(RelRo));
1367 // PT_GNU_EH_FRAME is a special section pointing on .eh_frame_hdr.
1368 if (!In<ELFT>::EhFrame->empty() && In<ELFT>::EhFrameHdr &&
1369 In<ELFT>::EhFrame->OutSec && In<ELFT>::EhFrameHdr->OutSec)
1370 AddHdr(PT_GNU_EH_FRAME, In<ELFT>::EhFrameHdr->OutSec->getPhdrFlags())
1371 ->add(In<ELFT>::EhFrameHdr->OutSec);
1373 // PT_OPENBSD_RANDOMIZE is an OpenBSD-specific feature. That makes
1374 // the dynamic linker fill the segment with random data.
1375 if (OutputSection *Sec = findSection(".openbsd.randomdata"))
1376 AddHdr(PT_OPENBSD_RANDOMIZE, Sec->getPhdrFlags())->add(Sec);
1378 // PT_GNU_STACK is a special section to tell the loader to make the
1379 // pages for the stack non-executable. If you really want an executable
1380 // stack, you can pass -z execstack, but that's not recommended for
1381 // security reasons.
1383 if (Config->ZExecstack)
1384 Perm = PF_R | PF_W | PF_X;
1387 AddHdr(PT_GNU_STACK, Perm)->p_memsz = Config->ZStackSize;
1389 // PT_OPENBSD_WXNEEDED is a OpenBSD-specific header to mark the executable
1390 // is expected to perform W^X violations, such as calling mprotect(2) or
1391 // mmap(2) with PROT_WRITE | PROT_EXEC, which is prohibited by default on
1393 if (Config->ZWxneeded)
1394 AddHdr(PT_OPENBSD_WXNEEDED, PF_X);
1396 // Create one PT_NOTE per a group of contiguous .note sections.
1397 PhdrEntry *Note = nullptr;
1398 for (OutputSection *Sec : OutputSections) {
1399 if (Sec->Type == SHT_NOTE) {
1400 if (!Note || Script->hasLMA(Sec->Name))
1401 Note = AddHdr(PT_NOTE, PF_R);
1410 template <class ELFT>
1411 void Writer<ELFT>::addPtArmExid(std::vector<PhdrEntry> &Phdrs) {
1412 if (Config->EMachine != EM_ARM)
1414 auto I = std::find_if(
1415 OutputSections.begin(), OutputSections.end(),
1416 [](OutputSection *Sec) { return Sec->Type == SHT_ARM_EXIDX; });
1417 if (I == OutputSections.end())
1420 // PT_ARM_EXIDX is the ARM EHABI equivalent of PT_GNU_EH_FRAME
1421 PhdrEntry ARMExidx(PT_ARM_EXIDX, PF_R);
1423 Phdrs.push_back(ARMExidx);
1426 // The first section of each PT_LOAD, the first section in PT_GNU_RELRO and the
1427 // first section after PT_GNU_RELRO have to be page aligned so that the dynamic
1428 // linker can set the permissions.
1429 template <class ELFT> void Writer<ELFT>::fixSectionAlignments() {
1430 for (const PhdrEntry &P : Phdrs)
1431 if (P.p_type == PT_LOAD && P.First)
1432 P.First->PageAlign = true;
1434 for (const PhdrEntry &P : Phdrs) {
1435 if (P.p_type != PT_GNU_RELRO)
1438 P.First->PageAlign = true;
1439 // Find the first section after PT_GNU_RELRO. If it is in a PT_LOAD we
1440 // have to align it to a page.
1441 auto End = OutputSections.end();
1442 auto I = std::find(OutputSections.begin(), End, P.Last);
1443 if (I == End || (I + 1) == End)
1445 OutputSection *Sec = *(I + 1);
1446 if (needsPtLoad(Sec))
1447 Sec->PageAlign = true;
1451 bool elf::allocateHeaders(std::vector<PhdrEntry> &Phdrs,
1452 ArrayRef<OutputSection *> OutputSections,
1455 std::find_if(Phdrs.begin(), Phdrs.end(),
1456 [](const PhdrEntry &E) { return E.p_type == PT_LOAD; });
1457 if (FirstPTLoad == Phdrs.end())
1460 uint64_t HeaderSize = getHeaderSize();
1461 if (HeaderSize > Min) {
1463 std::find_if(Phdrs.begin(), Phdrs.end(),
1464 [](const PhdrEntry &E) { return E.p_type == PT_PHDR; });
1465 if (PhdrI != Phdrs.end())
1469 Min = alignDown(Min - HeaderSize, Config->MaxPageSize);
1471 if (!Script->Opt.HasSections)
1472 Config->ImageBase = Min = std::min(Min, Config->ImageBase);
1474 Out::ElfHeader->Addr = Min;
1475 Out::ProgramHeaders->Addr = Min + Out::ElfHeader->Size;
1477 if (Script->hasPhdrsCommands())
1480 if (FirstPTLoad->First)
1481 for (OutputSection *Sec : OutputSections)
1482 if (Sec->FirstInPtLoad == FirstPTLoad->First)
1483 Sec->FirstInPtLoad = Out::ElfHeader;
1484 FirstPTLoad->First = Out::ElfHeader;
1485 if (!FirstPTLoad->Last)
1486 FirstPTLoad->Last = Out::ProgramHeaders;
1490 // We should set file offsets and VAs for elf header and program headers
1491 // sections. These are special, we do not include them into output sections
1492 // list, but have them to simplify the code.
1493 template <class ELFT> void Writer<ELFT>::fixHeaders() {
1494 Out::ProgramHeaders->Size = sizeof(Elf_Phdr) * Phdrs.size();
1495 // If the script has SECTIONS, assignAddresses will compute the values.
1496 if (Script->Opt.HasSections)
1499 // When -T<section> option is specified, lower the base to make room for those
1502 if (!Config->SectionStartMap.empty())
1503 for (const auto &P : Config->SectionStartMap)
1504 Min = std::min(Min, P.second);
1506 AllocateHeader = allocateHeaders(Phdrs, OutputSections, Min);
1509 // Adjusts the file alignment for a given output section and returns
1510 // its new file offset. The file offset must be the same with its
1511 // virtual address (modulo the page size) so that the loader can load
1512 // executables without any address adjustment.
1513 static uint64_t getFileAlignment(uint64_t Off, OutputSection *Sec) {
1514 OutputSection *First = Sec->FirstInPtLoad;
1515 // If the section is not in a PT_LOAD, we just have to align it.
1517 return alignTo(Off, Sec->Alignment);
1519 // The first section in a PT_LOAD has to have congruent offset and address
1520 // module the page size.
1522 return alignTo(Off, Config->MaxPageSize, Sec->Addr);
1524 // If two sections share the same PT_LOAD the file offset is calculated
1525 // using this formula: Off2 = Off1 + (VA2 - VA1).
1526 return First->Offset + Sec->Addr - First->Addr;
1529 static uint64_t setOffset(OutputSection *Sec, uint64_t Off) {
1530 if (Sec->Type == SHT_NOBITS) {
1535 Off = getFileAlignment(Off, Sec);
1537 return Off + Sec->Size;
1540 template <class ELFT> void Writer<ELFT>::assignFileOffsetsBinary() {
1542 for (OutputSection *Sec : OutputSections)
1543 if (Sec->Flags & SHF_ALLOC)
1544 Off = setOffset(Sec, Off);
1545 FileSize = alignTo(Off, Config->Wordsize);
1548 // Assign file offsets to output sections.
1549 template <class ELFT> void Writer<ELFT>::assignFileOffsets() {
1551 Off = setOffset(Out::ElfHeader, Off);
1552 Off = setOffset(Out::ProgramHeaders, Off);
1554 for (OutputSection *Sec : OutputSections)
1555 Off = setOffset(Sec, Off);
1557 SectionHeaderOff = alignTo(Off, Config->Wordsize);
1558 FileSize = SectionHeaderOff + (OutputSections.size() + 1) * sizeof(Elf_Shdr);
1561 // Finalize the program headers. We call this function after we assign
1562 // file offsets and VAs to all sections.
1563 template <class ELFT> void Writer<ELFT>::setPhdrs() {
1564 for (PhdrEntry &P : Phdrs) {
1565 OutputSection *First = P.First;
1566 OutputSection *Last = P.Last;
1568 P.p_filesz = Last->Offset - First->Offset;
1569 if (Last->Type != SHT_NOBITS)
1570 P.p_filesz += Last->Size;
1571 P.p_memsz = Last->Addr + Last->Size - First->Addr;
1572 P.p_offset = First->Offset;
1573 P.p_vaddr = First->Addr;
1575 P.p_paddr = First->getLMA();
1577 if (P.p_type == PT_LOAD)
1578 P.p_align = Config->MaxPageSize;
1579 else if (P.p_type == PT_GNU_RELRO)
1582 // The TLS pointer goes after PT_TLS. At least glibc will align it,
1583 // so round up the size to make sure the offsets are correct.
1584 if (P.p_type == PT_TLS) {
1587 P.p_memsz = alignTo(P.p_memsz, P.p_align);
1592 // The entry point address is chosen in the following ways.
1594 // 1. the '-e' entry command-line option;
1595 // 2. the ENTRY(symbol) command in a linker control script;
1596 // 3. the value of the symbol start, if present;
1597 // 4. the address of the first byte of the .text section, if present;
1598 // 5. the address 0.
1599 template <class ELFT> uint64_t Writer<ELFT>::getEntryAddr() {
1600 // Case 1, 2 or 3. As a special case, if the symbol is actually
1601 // a number, we'll use that number as an address.
1602 if (SymbolBody *B = Symtab<ELFT>::X->find(Config->Entry))
1605 if (!Config->Entry.getAsInteger(0, Addr))
1609 if (OutputSection *Sec = findSection(".text")) {
1610 if (Config->WarnMissingEntry)
1611 warn("cannot find entry symbol " + Config->Entry + "; defaulting to 0x" +
1612 utohexstr(Sec->Addr));
1617 if (Config->WarnMissingEntry)
1618 warn("cannot find entry symbol " + Config->Entry +
1619 "; not setting start address");
1623 static uint16_t getELFType() {
1626 if (Config->Relocatable)
1631 // This function is called after we have assigned address and size
1632 // to each section. This function fixes some predefined
1633 // symbol values that depend on section address and size.
1634 template <class ELFT> void Writer<ELFT>::fixPredefinedSymbols() {
1635 auto Set = [](DefinedRegular *S1, DefinedRegular *S2, OutputSection *Sec,
1647 // _etext is the first location after the last read-only loadable segment.
1648 // _edata is the first location after the last read-write loadable segment.
1649 // _end is the first location after the uninitialized data region.
1650 PhdrEntry *Last = nullptr;
1651 PhdrEntry *LastRO = nullptr;
1652 PhdrEntry *LastRW = nullptr;
1653 for (PhdrEntry &P : Phdrs) {
1654 if (P.p_type != PT_LOAD)
1657 if (P.p_flags & PF_W)
1663 Set(ElfSym::End1, ElfSym::End2, Last->First, Last->p_memsz);
1665 Set(ElfSym::Etext1, ElfSym::Etext2, LastRO->First, LastRO->p_filesz);
1667 Set(ElfSym::Edata1, ElfSym::Edata2, LastRW->First, LastRW->p_filesz);
1670 ElfSym::Bss->Section = findSection(".bss");
1672 // Setup MIPS _gp_disp/__gnu_local_gp symbols which should
1673 // be equal to the _gp symbol's value.
1674 if (Config->EMachine == EM_MIPS) {
1675 if (!ElfSym::MipsGp->Value) {
1676 // Find GP-relative section with the lowest address
1677 // and use this address to calculate default _gp value.
1679 for (const OutputSection *OS : OutputSections)
1680 if ((OS->Flags & SHF_MIPS_GPREL) && OS->Addr < Gp)
1682 if (Gp != (uint64_t)-1)
1683 ElfSym::MipsGp->Value = Gp + 0x7ff0;
1688 template <class ELFT> void Writer<ELFT>::writeHeader() {
1689 uint8_t *Buf = Buffer->getBufferStart();
1690 memcpy(Buf, "\177ELF", 4);
1692 // Write the ELF header.
1693 auto *EHdr = reinterpret_cast<Elf_Ehdr *>(Buf);
1694 EHdr->e_ident[EI_CLASS] = Config->Is64 ? ELFCLASS64 : ELFCLASS32;
1695 EHdr->e_ident[EI_DATA] = Config->IsLE ? ELFDATA2LSB : ELFDATA2MSB;
1696 EHdr->e_ident[EI_VERSION] = EV_CURRENT;
1697 EHdr->e_ident[EI_OSABI] = Config->OSABI;
1698 EHdr->e_type = getELFType();
1699 EHdr->e_machine = Config->EMachine;
1700 EHdr->e_version = EV_CURRENT;
1701 EHdr->e_entry = getEntryAddr();
1702 EHdr->e_shoff = SectionHeaderOff;
1703 EHdr->e_ehsize = sizeof(Elf_Ehdr);
1704 EHdr->e_phnum = Phdrs.size();
1705 EHdr->e_shentsize = sizeof(Elf_Shdr);
1706 EHdr->e_shnum = OutputSections.size() + 1;
1707 EHdr->e_shstrndx = In<ELFT>::ShStrTab->OutSec->SectionIndex;
1709 if (Config->EMachine == EM_ARM)
1710 // We don't currently use any features incompatible with EF_ARM_EABI_VER5,
1711 // but we don't have any firm guarantees of conformance. Linux AArch64
1712 // kernels (as of 2016) require an EABI version to be set.
1713 EHdr->e_flags = EF_ARM_EABI_VER5;
1714 else if (Config->EMachine == EM_MIPS)
1715 EHdr->e_flags = getMipsEFlags<ELFT>();
1717 if (!Config->Relocatable) {
1718 EHdr->e_phoff = sizeof(Elf_Ehdr);
1719 EHdr->e_phentsize = sizeof(Elf_Phdr);
1722 // Write the program header table.
1723 auto *HBuf = reinterpret_cast<Elf_Phdr *>(Buf + EHdr->e_phoff);
1724 for (PhdrEntry &P : Phdrs) {
1725 HBuf->p_type = P.p_type;
1726 HBuf->p_flags = P.p_flags;
1727 HBuf->p_offset = P.p_offset;
1728 HBuf->p_vaddr = P.p_vaddr;
1729 HBuf->p_paddr = P.p_paddr;
1730 HBuf->p_filesz = P.p_filesz;
1731 HBuf->p_memsz = P.p_memsz;
1732 HBuf->p_align = P.p_align;
1736 // Write the section header table. Note that the first table entry is null.
1737 auto *SHdrs = reinterpret_cast<Elf_Shdr *>(Buf + EHdr->e_shoff);
1738 for (OutputSection *Sec : OutputSections)
1739 Sec->writeHeaderTo<ELFT>(++SHdrs);
1742 // Open a result file.
1743 template <class ELFT> void Writer<ELFT>::openFile() {
1744 if (!Config->Is64 && FileSize > UINT32_MAX) {
1745 error("output file too large: " + Twine(FileSize) + " bytes");
1749 unlinkAsync(Config->OutputFile);
1750 ErrorOr<std::unique_ptr<FileOutputBuffer>> BufferOrErr =
1751 FileOutputBuffer::create(Config->OutputFile, FileSize,
1752 FileOutputBuffer::F_executable);
1754 if (auto EC = BufferOrErr.getError())
1755 error("failed to open " + Config->OutputFile + ": " + EC.message());
1757 Buffer = std::move(*BufferOrErr);
1760 template <class ELFT> void Writer<ELFT>::writeSectionsBinary() {
1761 uint8_t *Buf = Buffer->getBufferStart();
1762 for (OutputSection *Sec : OutputSections)
1763 if (Sec->Flags & SHF_ALLOC)
1764 Sec->writeTo<ELFT>(Buf + Sec->Offset);
1767 // Write section contents to a mmap'ed file.
1768 template <class ELFT> void Writer<ELFT>::writeSections() {
1769 uint8_t *Buf = Buffer->getBufferStart();
1771 // PPC64 needs to process relocations in the .opd section
1772 // before processing relocations in code-containing sections.
1773 Out::Opd = findSection(".opd");
1775 Out::OpdBuf = Buf + Out::Opd->Offset;
1776 Out::Opd->template writeTo<ELFT>(Buf + Out::Opd->Offset);
1779 OutputSection *EhFrameHdr =
1780 In<ELFT>::EhFrameHdr ? In<ELFT>::EhFrameHdr->OutSec : nullptr;
1782 // In -r or -emit-relocs mode, write the relocation sections first as in
1783 // ELf_Rel targets we might find out that we need to modify the relocated
1784 // section while doing it.
1785 for (OutputSection *Sec : OutputSections)
1786 if (Sec->Type == SHT_REL || Sec->Type == SHT_RELA)
1787 Sec->writeTo<ELFT>(Buf + Sec->Offset);
1789 for (OutputSection *Sec : OutputSections)
1790 if (Sec != Out::Opd && Sec != EhFrameHdr && Sec->Type != SHT_REL &&
1791 Sec->Type != SHT_RELA)
1792 Sec->writeTo<ELFT>(Buf + Sec->Offset);
1794 // The .eh_frame_hdr depends on .eh_frame section contents, therefore
1795 // it should be written after .eh_frame is written.
1796 if (EhFrameHdr && !EhFrameHdr->Sections.empty())
1797 EhFrameHdr->writeTo<ELFT>(Buf + EhFrameHdr->Offset);
1800 template <class ELFT> void Writer<ELFT>::writeBuildId() {
1801 if (!In<ELFT>::BuildId || !In<ELFT>::BuildId->OutSec)
1804 // Compute a hash of all sections of the output file.
1805 uint8_t *Start = Buffer->getBufferStart();
1806 uint8_t *End = Start + FileSize;
1807 In<ELFT>::BuildId->writeBuildId({Start, End});
1810 template void elf::writeResult<ELF32LE>();
1811 template void elf::writeResult<ELF32BE>();
1812 template void elf::writeResult<ELF64LE>();
1813 template void elf::writeResult<ELF64BE>();
1815 template bool elf::isRelroSection<ELF32LE>(const OutputSection *);
1816 template bool elf::isRelroSection<ELF32BE>(const OutputSection *);
1817 template bool elf::isRelroSection<ELF64LE>(const OutputSection *);
1818 template bool elf::isRelroSection<ELF64BE>(const OutputSection *);