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 clearOutputSections();
50 void createSyntheticSections();
51 void copyLocalSymbols();
52 void addSectionSymbols();
53 void addReservedSymbols();
54 void createSections();
55 void forEachRelSec(std::function<void(InputSectionBase &)> Fn);
57 void finalizeSections();
58 void addPredefinedSections();
60 std::vector<PhdrEntry> createPhdrs();
61 void removeEmptyPTLoad();
62 void addPtArmExid(std::vector<PhdrEntry> &Phdrs);
63 void assignFileOffsets();
64 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);
84 OutputSection *findSectionInScript(StringRef Name);
85 OutputSectionCommand *findSectionCommand(StringRef Name);
87 std::vector<PhdrEntry> Phdrs;
90 uint64_t SectionHeaderOff;
92 } // anonymous namespace
94 StringRef elf::getOutputSectionName(StringRef Name) {
95 if (Config->Relocatable)
98 // If -emit-relocs is given (which is rare), we need to copy
99 // relocation sections to the output. If input section .foo is
100 // output as .bar, we want to rename .rel.foo .rel.bar as well.
101 if (Config->EmitRelocs) {
102 for (StringRef V : {".rel.", ".rela."}) {
103 if (Name.startswith(V)) {
104 StringRef Inner = getOutputSectionName(Name.substr(V.size() - 1));
105 return Saver.save(V.drop_back() + Inner);
111 {".text.", ".rodata.", ".data.rel.ro.", ".data.", ".bss.rel.ro.",
112 ".bss.", ".init_array.", ".fini_array.", ".ctors.", ".dtors.", ".tbss.",
113 ".gcc_except_table.", ".tdata.", ".ARM.exidx."}) {
114 StringRef Prefix = V.drop_back();
115 if (Name.startswith(V) || Name == Prefix)
119 // CommonSection is identified as "COMMON" in linker scripts.
120 // By default, it should go to .bss section.
121 if (Name == "COMMON")
124 // ".zdebug_" is a prefix for ZLIB-compressed sections.
125 // Because we decompressed input sections, we want to remove 'z'.
126 if (Name.startswith(".zdebug_"))
127 return Saver.save("." + Name.substr(2));
131 template <class ELFT> static bool needsInterpSection() {
132 return !Symtab<ELFT>::X->getSharedFiles().empty() &&
133 !Config->DynamicLinker.empty() && !Script->ignoreInterpSection();
136 template <class ELFT> void elf::writeResult() { Writer<ELFT>().run(); }
138 template <class ELFT> void Writer<ELFT>::removeEmptyPTLoad() {
139 auto I = std::remove_if(Phdrs.begin(), Phdrs.end(), [&](const PhdrEntry &P) {
140 if (P.p_type != PT_LOAD)
144 uint64_t Size = P.Last->Addr + P.Last->Size - P.First->Addr;
147 Phdrs.erase(I, Phdrs.end());
150 // This function scans over the input sections and creates mergeable
151 // synthetic sections. It removes MergeInputSections from array and
152 // adds new synthetic ones. Each synthetic section is added to the
153 // location of the first input section it replaces.
154 static void combineMergableSections() {
155 std::vector<MergeSyntheticSection *> MergeSections;
156 for (InputSectionBase *&S : InputSections) {
157 MergeInputSection *MS = dyn_cast<MergeInputSection>(S);
161 // We do not want to handle sections that are not alive, so just remove
162 // them instead of trying to merge.
166 StringRef OutsecName = getOutputSectionName(MS->Name);
167 uint64_t Flags = MS->Flags & ~(uint64_t)SHF_GROUP;
168 uint32_t Alignment = std::max<uint32_t>(MS->Alignment, MS->Entsize);
170 auto I = 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 template <class ELFT> void Writer<ELFT>::clearOutputSections() {
205 // Clear the OutputSections to make sure it is not used anymore. Any
206 // code from this point on should be using the linker script
208 for (OutputSection *Sec : OutputSections)
209 Sec->Sections.clear();
210 OutputSections.clear();
213 // The main function of the writer.
214 template <class ELFT> void Writer<ELFT>::run() {
215 // Create linker-synthesized sections such as .got or .plt.
216 // Such sections are of type input section.
217 createSyntheticSections();
218 combineMergableSections();
220 if (!Config->Relocatable)
221 combineEhFrameSections<ELFT>();
223 // We need to create some reserved symbols such as _end. Create them.
224 if (!Config->Relocatable)
225 addReservedSymbols();
227 // Create output sections.
228 Script->OutputSections = &OutputSections;
229 if (Script->Opt.HasSections) {
230 // If linker script contains SECTIONS commands, let it create sections.
231 Script->processCommands(Factory);
233 // Linker scripts may have left some input sections unassigned.
234 // Assign such sections using the default rule.
235 Script->addOrphanSections(Factory);
237 // If linker script does not contain SECTIONS commands, create
238 // output sections by default rules. We still need to give the
239 // linker script a chance to run, because it might contain
240 // non-SECTIONS commands such as ASSERT.
242 Script->processCommands(Factory);
245 if (Config->Discard != DiscardPolicy::All)
248 if (Config->CopyRelocs)
251 // Now that we have a complete set of output sections. This function
252 // completes section contents. For example, we need to add strings
253 // to the string table, and add entries to .got and .plt.
254 // finalizeSections does that.
259 if (!Script->Opt.HasSections) {
260 if (!Config->Relocatable)
261 fixSectionAlignments();
262 Script->fabricateDefaultCommands();
265 // If -compressed-debug-sections is specified, we need to compress
266 // .debug_* sections. Do it right now because it changes the size of
268 parallelForEach(OutputSections.begin(), OutputSections.end(),
269 [](OutputSection *S) { S->maybeCompress<ELFT>(); });
271 if (Config->Relocatable) {
274 Script->synchronize();
275 Script->assignAddresses(Phdrs);
277 // Remove empty PT_LOAD to avoid causing the dynamic linker to try to mmap a
278 // 0 sized region. This has to be done late since only after assignAddresses
279 // we know the size of the sections.
282 if (!Config->OFormatBinary)
285 assignFileOffsetsBinary();
288 fixPredefinedSymbols();
291 // It does not make sense try to open the file if we have error already.
294 // Write the result down to a file.
298 if (!Config->OFormatBinary) {
302 writeSectionsBinary();
304 clearOutputSections();
306 // Backfill .note.gnu.build-id section content. This is done at last
307 // because the content is usually a hash value of the entire output file.
313 // Handle -Map option.
314 writeMapFile<ELFT>(Script->Opt.Commands);
318 if (auto EC = Buffer->commit())
319 error("failed to write to the output file: " + EC.message());
321 // Flush the output streams and exit immediately. A full shutdown
322 // is a good test that we are keeping track of all allocated memory,
323 // but actually freeing it is a waste of time in a regular linker run.
324 if (Config->ExitEarly)
328 // Initialize Out members.
329 template <class ELFT> void Writer<ELFT>::createSyntheticSections() {
330 // Initialize all pointers with NULL. This is needed because
331 // you can call lld::elf::main more than once as a library.
332 memset(&Out::First, 0, sizeof(Out));
334 auto Add = [](InputSectionBase *Sec) { InputSections.push_back(Sec); };
336 InX::DynStrTab = make<StringTableSection>(".dynstr", true);
337 InX::Dynamic = make<DynamicSection<ELFT>>();
338 In<ELFT>::RelaDyn = make<RelocationSection<ELFT>>(
339 Config->IsRela ? ".rela.dyn" : ".rel.dyn", Config->ZCombreloc);
340 InX::ShStrTab = make<StringTableSection>(".shstrtab", false);
342 Out::ElfHeader = make<OutputSection>("", 0, SHF_ALLOC);
343 Out::ElfHeader->Size = sizeof(Elf_Ehdr);
344 Out::ProgramHeaders = make<OutputSection>("", 0, SHF_ALLOC);
345 Out::ProgramHeaders->updateAlignment(Config->Wordsize);
347 if (needsInterpSection<ELFT>()) {
348 InX::Interp = createInterpSection();
351 InX::Interp = nullptr;
354 if (!Config->Relocatable)
355 Add(createCommentSection<ELFT>());
357 if (Config->Strip != StripPolicy::All) {
358 InX::StrTab = make<StringTableSection>(".strtab", false);
359 InX::SymTab = make<SymbolTableSection<ELFT>>(*InX::StrTab);
362 if (Config->BuildId != BuildIdKind::None) {
363 InX::BuildId = make<BuildIdSection>();
367 InX::Common = createCommonSection<ELFT>();
371 InX::Bss = make<BssSection>(".bss");
373 InX::BssRelRo = make<BssSection>(".bss.rel.ro");
376 // Add MIPS-specific sections.
377 bool HasDynSymTab = !Symtab<ELFT>::X->getSharedFiles().empty() ||
378 Config->Pic || Config->ExportDynamic;
379 if (Config->EMachine == EM_MIPS) {
380 if (!Config->Shared && HasDynSymTab) {
381 InX::MipsRldMap = make<MipsRldMapSection>();
382 Add(InX::MipsRldMap);
384 if (auto *Sec = MipsAbiFlagsSection<ELFT>::create())
386 if (auto *Sec = MipsOptionsSection<ELFT>::create())
388 if (auto *Sec = MipsReginfoSection<ELFT>::create())
393 InX::DynSymTab = make<SymbolTableSection<ELFT>>(*InX::DynStrTab);
396 In<ELFT>::VerSym = make<VersionTableSection<ELFT>>();
397 Add(In<ELFT>::VerSym);
399 if (!Config->VersionDefinitions.empty()) {
400 In<ELFT>::VerDef = make<VersionDefinitionSection<ELFT>>();
401 Add(In<ELFT>::VerDef);
404 In<ELFT>::VerNeed = make<VersionNeedSection<ELFT>>();
405 Add(In<ELFT>::VerNeed);
407 if (Config->GnuHash) {
408 InX::GnuHashTab = make<GnuHashTableSection>();
409 Add(InX::GnuHashTab);
412 if (Config->SysvHash) {
413 In<ELFT>::HashTab = make<HashTableSection<ELFT>>();
414 Add(In<ELFT>::HashTab);
419 Add(In<ELFT>::RelaDyn);
422 // Add .got. MIPS' .got is so different from the other archs,
423 // it has its own class.
424 if (Config->EMachine == EM_MIPS) {
425 InX::MipsGot = make<MipsGotSection>();
428 InX::Got = make<GotSection>();
432 InX::GotPlt = make<GotPltSection>();
434 InX::IgotPlt = make<IgotPltSection>();
437 if (Config->GdbIndex) {
438 InX::GdbIndex = make<GdbIndexSection>();
442 // We always need to add rel[a].plt to output if it has entries.
443 // Even for static linking it can contain R_[*]_IRELATIVE relocations.
444 In<ELFT>::RelaPlt = make<RelocationSection<ELFT>>(
445 Config->IsRela ? ".rela.plt" : ".rel.plt", false /*Sort*/);
446 Add(In<ELFT>::RelaPlt);
448 // The RelaIplt immediately follows .rel.plt (.rel.dyn for ARM) to ensure
449 // that the IRelative relocations are processed last by the dynamic loader
450 In<ELFT>::RelaIplt = make<RelocationSection<ELFT>>(
451 (Config->EMachine == EM_ARM) ? ".rel.dyn" : In<ELFT>::RelaPlt->Name,
453 Add(In<ELFT>::RelaIplt);
455 InX::Plt = make<PltSection>(Target->PltHeaderSize);
457 InX::Iplt = make<PltSection>(0);
460 if (!Config->Relocatable) {
461 if (Config->EhFrameHdr) {
462 In<ELFT>::EhFrameHdr = make<EhFrameHeader<ELFT>>();
463 Add(In<ELFT>::EhFrameHdr);
465 In<ELFT>::EhFrame = make<EhFrameSection<ELFT>>();
466 Add(In<ELFT>::EhFrame);
476 static bool shouldKeepInSymtab(SectionBase *Sec, StringRef SymName,
477 const SymbolBody &B) {
478 if (B.isFile() || B.isSection())
481 // If sym references a section in a discarded group, don't keep it.
482 if (Sec == &InputSection::Discarded)
485 if (Config->Discard == DiscardPolicy::None)
488 // In ELF assembly .L symbols are normally discarded by the assembler.
489 // If the assembler fails to do so, the linker discards them if
490 // * --discard-locals is used.
491 // * The symbol is in a SHF_MERGE section, which is normally the reason for
492 // the assembler keeping the .L symbol.
493 if (!SymName.startswith(".L") && !SymName.empty())
496 if (Config->Discard == DiscardPolicy::Locals)
499 return !Sec || !(Sec->Flags & SHF_MERGE);
502 static bool includeInSymtab(const SymbolBody &B) {
503 if (!B.isLocal() && !B.symbol()->IsUsedInRegularObj)
506 if (auto *D = dyn_cast<DefinedRegular>(&B)) {
507 // Always include absolute symbols.
508 SectionBase *Sec = D->Section;
511 if (auto *IS = dyn_cast<InputSectionBase>(Sec)) {
513 IS = cast<InputSectionBase>(Sec);
514 // Exclude symbols pointing to garbage-collected sections.
518 if (auto *S = dyn_cast<MergeInputSection>(Sec))
519 if (!S->getSectionPiece(D->Value)->Live)
525 // Local symbols are not in the linker's symbol table. This function scans
526 // each object file's symbol table to copy local symbols to the output.
527 template <class ELFT> void Writer<ELFT>::copyLocalSymbols() {
530 for (elf::ObjectFile<ELFT> *F : Symtab<ELFT>::X->getObjectFiles()) {
531 for (SymbolBody *B : F->getLocalSymbols()) {
534 ": broken object: getLocalSymbols returns a non-local symbol");
535 auto *DR = dyn_cast<DefinedRegular>(B);
537 // No reason to keep local undefined symbol in symtab.
540 if (!includeInSymtab(*B))
543 SectionBase *Sec = DR->Section;
544 if (!shouldKeepInSymtab(Sec, B->getName(), *B))
546 InX::SymTab->addSymbol(B);
551 template <class ELFT> void Writer<ELFT>::addSectionSymbols() {
552 // Create one STT_SECTION symbol for each output section we might
553 // have a relocation with.
554 for (OutputSection *Sec : OutputSections) {
555 if (Sec->Sections.empty())
558 InputSection *IS = Sec->Sections[0];
559 if (isa<SyntheticSection>(IS) || IS->Type == SHT_REL ||
560 IS->Type == SHT_RELA)
564 make<DefinedRegular>("", /*IsLocal=*/true, /*StOther=*/0, STT_SECTION,
565 /*Value=*/0, /*Size=*/0, IS, nullptr);
566 InX::SymTab->addSymbol(Sym);
570 // Today's loaders have a feature to make segments read-only after
571 // processing dynamic relocations to enhance security. PT_GNU_RELRO
572 // is defined for that.
574 // This function returns true if a section needs to be put into a
575 // PT_GNU_RELRO segment.
576 bool elf::isRelroSection(const OutputSection *Sec) {
580 uint64_t Flags = Sec->Flags;
582 // Non-allocatable or non-writable sections don't need RELRO because
583 // they are not writable or not even mapped to memory in the first place.
584 // RELRO is for sections that are essentially read-only but need to
585 // be writable only at process startup to allow dynamic linker to
586 // apply relocations.
587 if (!(Flags & SHF_ALLOC) || !(Flags & SHF_WRITE))
590 // Once initialized, TLS data segments are used as data templates
591 // for a thread-local storage. For each new thread, runtime
592 // allocates memory for a TLS and copy templates there. No thread
593 // are supposed to use templates directly. Thus, it can be in RELRO.
597 // .init_array, .preinit_array and .fini_array contain pointers to
598 // functions that are executed on process startup or exit. These
599 // pointers are set by the static linker, and they are not expected
600 // to change at runtime. But if you are an attacker, you could do
601 // interesting things by manipulating pointers in .fini_array, for
602 // example. So they are put into RELRO.
603 uint32_t Type = Sec->Type;
604 if (Type == SHT_INIT_ARRAY || Type == SHT_FINI_ARRAY ||
605 Type == SHT_PREINIT_ARRAY)
608 // .got contains pointers to external symbols. They are resolved by
609 // the dynamic linker when a module is loaded into memory, and after
610 // that they are not expected to change. So, it can be in RELRO.
611 if (InX::Got && Sec == InX::Got->OutSec)
614 // .got.plt contains pointers to external function symbols. They are
615 // by default resolved lazily, so we usually cannot put it into RELRO.
616 // However, if "-z now" is given, the lazy symbol resolution is
617 // disabled, which enables us to put it into RELRO.
618 if (Sec == InX::GotPlt->OutSec)
621 // .dynamic section contains data for the dynamic linker, and
622 // there's no need to write to it at runtime, so it's better to put
624 if (Sec == InX::Dynamic->OutSec)
627 // .bss.rel.ro is used for copy relocations for read-only symbols.
628 // Since the dynamic linker needs to process copy relocations, the
629 // section cannot be read-only, but once initialized, they shouldn't
631 if (Sec == InX::BssRelRo->OutSec)
634 // Sections with some special names are put into RELRO. This is a
635 // bit unfortunate because section names shouldn't be significant in
636 // ELF in spirit. But in reality many linker features depend on
637 // magic section names.
638 StringRef S = Sec->Name;
639 return S == ".data.rel.ro" || S == ".ctors" || S == ".dtors" || S == ".jcr" ||
640 S == ".eh_frame" || S == ".openbsd.randomdata";
643 // We compute a rank for each section. The rank indicates where the
644 // section should be placed in the file. Instead of using simple
645 // numbers (0,1,2...), we use a series of flags. One for each decision
646 // point when placing the section.
647 // Using flags has two key properties:
648 // * It is easy to check if a give branch was taken.
649 // * It is easy two see how similar two ranks are (see getRankProximity).
651 RF_NOT_ADDR_SET = 1 << 16,
652 RF_NOT_INTERP = 1 << 15,
653 RF_NOT_ALLOC = 1 << 14,
655 RF_EXEC_WRITE = 1 << 12,
657 RF_NON_TLS_BSS = 1 << 10,
658 RF_NON_TLS_BSS_RO = 1 << 9,
661 RF_PPC_NOT_TOCBSS = 1 << 6,
663 RF_PPC_TOCL = 1 << 4,
665 RF_PPC_BRANCH_LT = 1 << 2,
666 RF_MIPS_GPREL = 1 << 1,
667 RF_MIPS_NOT_GOT = 1 << 0
670 static unsigned getSectionRank(const OutputSection *Sec) {
673 // We want to put section specified by -T option first, so we
674 // can start assigning VA starting from them later.
675 if (Config->SectionStartMap.count(Sec->Name))
677 Rank |= RF_NOT_ADDR_SET;
679 // Put .interp first because some loaders want to see that section
680 // on the first page of the executable file when loaded into memory.
681 if (Sec->Name == ".interp")
683 Rank |= RF_NOT_INTERP;
685 // Allocatable sections go first to reduce the total PT_LOAD size and
686 // so debug info doesn't change addresses in actual code.
687 if (!(Sec->Flags & SHF_ALLOC))
688 return Rank | RF_NOT_ALLOC;
690 // Sort sections based on their access permission in the following
691 // order: R, RX, RWX, RW. This order is based on the following
693 // * Read-only sections come first such that they go in the
694 // PT_LOAD covering the program headers at the start of the file.
695 // * Read-only, executable sections come next, unless the
696 // -no-rosegment option is used.
697 // * Writable, executable sections follow such that .plt on
698 // architectures where it needs to be writable will be placed
699 // between .text and .data.
700 // * Writable sections come last, such that .bss lands at the very
701 // end of the last PT_LOAD.
702 bool IsExec = Sec->Flags & SHF_EXECINSTR;
703 bool IsWrite = Sec->Flags & SHF_WRITE;
707 Rank |= RF_EXEC_WRITE;
708 else if (!Config->SingleRoRx)
715 // If we got here we know that both A and B are in the same PT_LOAD.
717 bool IsTls = Sec->Flags & SHF_TLS;
718 bool IsNoBits = Sec->Type == SHT_NOBITS;
720 // The first requirement we have is to put (non-TLS) nobits sections last. The
721 // reason is that the only thing the dynamic linker will see about them is a
722 // p_memsz that is larger than p_filesz. Seeing that it zeros the end of the
723 // PT_LOAD, so that has to correspond to the nobits sections.
724 bool IsNonTlsNoBits = IsNoBits && !IsTls;
726 Rank |= RF_NON_TLS_BSS;
728 // We place nobits RelRo sections before plain r/w ones, and non-nobits RelRo
729 // sections after r/w ones, so that the RelRo sections are contiguous.
730 bool IsRelRo = isRelroSection(Sec);
731 if (IsNonTlsNoBits && !IsRelRo)
732 Rank |= RF_NON_TLS_BSS_RO;
733 if (!IsNonTlsNoBits && IsRelRo)
734 Rank |= RF_NON_TLS_BSS_RO;
736 // The TLS initialization block needs to be a single contiguous block in a R/W
737 // PT_LOAD, so stick TLS sections directly before the other RelRo R/W
738 // sections. The TLS NOBITS sections are placed here as they don't take up
739 // virtual address space in the PT_LOAD.
743 // Within the TLS initialization block, the non-nobits sections need to appear
748 // // Some architectures have additional ordering restrictions for sections
749 // // within the same PT_LOAD.
750 if (Config->EMachine == EM_PPC64) {
751 // PPC64 has a number of special SHT_PROGBITS+SHF_ALLOC+SHF_WRITE sections
752 // that we would like to make sure appear is a specific order to maximize
753 // their coverage by a single signed 16-bit offset from the TOC base
754 // pointer. Conversely, the special .tocbss section should be first among
755 // all SHT_NOBITS sections. This will put it next to the loaded special
756 // PPC64 sections (and, thus, within reach of the TOC base pointer).
757 StringRef Name = Sec->Name;
758 if (Name != ".tocbss")
759 Rank |= RF_PPC_NOT_TOCBSS;
770 if (Name == ".branch_lt")
771 Rank |= RF_PPC_BRANCH_LT;
773 if (Config->EMachine == EM_MIPS) {
774 // All sections with SHF_MIPS_GPREL flag should be grouped together
775 // because data in these sections is addressable with a gp relative address.
776 if (Sec->Flags & SHF_MIPS_GPREL)
777 Rank |= RF_MIPS_GPREL;
779 if (Sec->Name != ".got")
780 Rank |= RF_MIPS_NOT_GOT;
786 static bool compareSectionsNonScript(const OutputSection *A,
787 const OutputSection *B) {
788 if (A->SortRank != B->SortRank)
789 return A->SortRank < B->SortRank;
790 if (!(A->SortRank & RF_NOT_ADDR_SET))
791 return Config->SectionStartMap.lookup(A->Name) <
792 Config->SectionStartMap.lookup(B->Name);
796 // Output section ordering is determined by this function.
797 static bool compareSections(const OutputSection *A, const OutputSection *B) {
798 // For now, put sections mentioned in a linker script
799 // first. Sections not on linker script will have a SectionIndex of
801 int AIndex = A->SectionIndex;
802 int BIndex = B->SectionIndex;
803 if (AIndex != BIndex)
804 return AIndex < BIndex;
806 return compareSectionsNonScript(A, B);
809 void PhdrEntry::add(OutputSection *Sec) {
813 p_align = std::max(p_align, Sec->Alignment);
814 if (p_type == PT_LOAD)
815 Sec->FirstInPtLoad = First;
818 template <class ELFT>
819 static Symbol *addRegular(StringRef Name, SectionBase *Sec, uint64_t Value,
820 uint8_t StOther = STV_HIDDEN,
821 uint8_t Binding = STB_WEAK) {
822 // The linker generated symbols are added as STB_WEAK to allow user defined
823 // ones to override them.
824 return Symtab<ELFT>::X->addRegular(Name, StOther, STT_NOTYPE, Value,
825 /*Size=*/0, Binding, Sec,
829 template <class ELFT>
830 static DefinedRegular *
831 addOptionalRegular(StringRef Name, SectionBase *Sec, uint64_t Val,
832 uint8_t StOther = STV_HIDDEN, uint8_t Binding = STB_GLOBAL) {
833 SymbolBody *S = Symtab<ELFT>::X->find(Name);
836 if (S->isInCurrentDSO())
838 return cast<DefinedRegular>(
839 addRegular<ELFT>(Name, Sec, Val, StOther, Binding)->body());
842 // The beginning and the ending of .rel[a].plt section are marked
843 // with __rel[a]_iplt_{start,end} symbols if it is a statically linked
844 // executable. The runtime needs these symbols in order to resolve
845 // all IRELATIVE relocs on startup. For dynamic executables, we don't
846 // need these symbols, since IRELATIVE relocs are resolved through GOT
847 // and PLT. For details, see http://www.airs.com/blog/archives/403.
848 template <class ELFT> void Writer<ELFT>::addRelIpltSymbols() {
851 StringRef S = Config->IsRela ? "__rela_iplt_start" : "__rel_iplt_start";
852 addOptionalRegular<ELFT>(S, In<ELFT>::RelaIplt, 0, STV_HIDDEN, STB_WEAK);
854 S = Config->IsRela ? "__rela_iplt_end" : "__rel_iplt_end";
855 addOptionalRegular<ELFT>(S, In<ELFT>::RelaIplt, -1, STV_HIDDEN, STB_WEAK);
858 // The linker is expected to define some symbols depending on
859 // the linking result. This function defines such symbols.
860 template <class ELFT> void Writer<ELFT>::addReservedSymbols() {
861 if (Config->EMachine == EM_MIPS) {
862 // Define _gp for MIPS. st_value of _gp symbol will be updated by Writer
863 // so that it points to an absolute address which by default is relative
864 // to GOT. Default offset is 0x7ff0.
865 // See "Global Data Symbols" in Chapter 6 in the following document:
866 // ftp://www.linux-mips.org/pub/linux/mips/doc/ABI/mipsabi.pdf
867 ElfSym::MipsGp = Symtab<ELFT>::X->addAbsolute("_gp", STV_HIDDEN, STB_LOCAL);
869 // On MIPS O32 ABI, _gp_disp is a magic symbol designates offset between
870 // start of function and 'gp' pointer into GOT.
871 if (Symtab<ELFT>::X->find("_gp_disp"))
873 Symtab<ELFT>::X->addAbsolute("_gp_disp", STV_HIDDEN, STB_LOCAL);
875 // The __gnu_local_gp is a magic symbol equal to the current value of 'gp'
876 // pointer. This symbol is used in the code generated by .cpload pseudo-op
877 // in case of using -mno-shared option.
878 // https://sourceware.org/ml/binutils/2004-12/msg00094.html
879 if (Symtab<ELFT>::X->find("__gnu_local_gp"))
880 ElfSym::MipsLocalGp =
881 Symtab<ELFT>::X->addAbsolute("__gnu_local_gp", STV_HIDDEN, STB_LOCAL);
884 // In the assembly for 32 bit x86 the _GLOBAL_OFFSET_TABLE_ symbol
885 // is magical and is used to produce a R_386_GOTPC relocation.
886 // The R_386_GOTPC relocation value doesn't actually depend on the
887 // symbol value, so it could use an index of STN_UNDEF which, according
888 // to the spec, means the symbol value is 0.
889 // Unfortunately both gas and MC keep the _GLOBAL_OFFSET_TABLE_ symbol in
891 // The situation is even stranger on x86_64 where the assembly doesn't
892 // need the magical symbol, but gas still puts _GLOBAL_OFFSET_TABLE_ as
893 // an undefined symbol in the .o files.
894 // Given that the symbol is effectively unused, we just create a dummy
895 // hidden one to avoid the undefined symbol error.
896 Symtab<ELFT>::X->addIgnored("_GLOBAL_OFFSET_TABLE_");
898 // __tls_get_addr is defined by the dynamic linker for dynamic ELFs. For
899 // static linking the linker is required to optimize away any references to
900 // __tls_get_addr, so it's not defined anywhere. Create a hidden definition
901 // to avoid the undefined symbol error.
903 Symtab<ELFT>::X->addIgnored("__tls_get_addr");
905 // __ehdr_start is the location of ELF file headers. Note that we define
906 // this symbol unconditionally even when using a linker script, which
907 // differs from the behavior implemented by GNU linker which only define
908 // this symbol if ELF headers are in the memory mapped segment.
909 addOptionalRegular<ELFT>("__ehdr_start", Out::ElfHeader, 0, STV_HIDDEN);
911 // If linker script do layout we do not need to create any standart symbols.
912 if (Script->Opt.HasSections)
915 auto Add = [](StringRef S) {
916 return addOptionalRegular<ELFT>(S, Out::ElfHeader, 0, STV_DEFAULT);
919 ElfSym::Bss = Add("__bss_start");
920 ElfSym::End1 = Add("end");
921 ElfSym::End2 = Add("_end");
922 ElfSym::Etext1 = Add("etext");
923 ElfSym::Etext2 = Add("_etext");
924 ElfSym::Edata1 = Add("edata");
925 ElfSym::Edata2 = Add("_edata");
928 // Sort input sections by section name suffixes for
929 // __attribute__((init_priority(N))).
930 static void sortInitFini(OutputSection *S) {
932 reinterpret_cast<OutputSection *>(S)->sortInitFini();
935 // Sort input sections by the special rule for .ctors and .dtors.
936 static void sortCtorsDtors(OutputSection *S) {
938 reinterpret_cast<OutputSection *>(S)->sortCtorsDtors();
941 // Sort input sections using the list provided by --symbol-ordering-file.
942 template <class ELFT>
943 static void sortBySymbolsOrder(ArrayRef<OutputSection *> OutputSections) {
944 if (Config->SymbolOrderingFile.empty())
947 // Build a map from symbols to their priorities. Symbols that didn't
948 // appear in the symbol ordering file have the lowest priority 0.
949 // All explicitly mentioned symbols have negative (higher) priorities.
950 DenseMap<StringRef, int> SymbolOrder;
951 int Priority = -Config->SymbolOrderingFile.size();
952 for (StringRef S : Config->SymbolOrderingFile)
953 SymbolOrder.insert({S, Priority++});
955 // Build a map from sections to their priorities.
956 DenseMap<SectionBase *, int> SectionOrder;
957 for (elf::ObjectFile<ELFT> *File : Symtab<ELFT>::X->getObjectFiles()) {
958 for (SymbolBody *Body : File->getSymbols()) {
959 auto *D = dyn_cast<DefinedRegular>(Body);
960 if (!D || !D->Section)
962 int &Priority = SectionOrder[D->Section];
963 Priority = std::min(Priority, SymbolOrder.lookup(D->getName()));
967 // Sort sections by priority.
968 for (OutputSection *Base : OutputSections)
969 if (auto *Sec = dyn_cast<OutputSection>(Base))
970 Sec->sort([&](InputSectionBase *S) { return SectionOrder.lookup(S); });
973 template <class ELFT>
974 void Writer<ELFT>::forEachRelSec(std::function<void(InputSectionBase &)> Fn) {
975 for (InputSectionBase *IS : InputSections) {
978 // Scan all relocations. Each relocation goes through a series
979 // of tests to determine if it needs special treatment, such as
980 // creating GOT, PLT, copy relocations, etc.
981 // Note that relocations for non-alloc sections are directly
982 // processed by InputSection::relocateNonAlloc.
983 if (!(IS->Flags & SHF_ALLOC))
985 if (isa<InputSection>(IS) || isa<EhInputSection>(IS))
989 if (!Config->Relocatable) {
990 for (EhInputSection *ES : In<ELFT>::EhFrame->Sections)
995 template <class ELFT> void Writer<ELFT>::createSections() {
996 for (InputSectionBase *IS : InputSections)
998 Factory.addInputSec(IS, getOutputSectionName(IS->Name));
1000 sortBySymbolsOrder<ELFT>(OutputSections);
1001 sortInitFini(findSection(".init_array"));
1002 sortInitFini(findSection(".fini_array"));
1003 sortCtorsDtors(findSection(".ctors"));
1004 sortCtorsDtors(findSection(".dtors"));
1006 for (OutputSection *Sec : OutputSections)
1007 Sec->assignOffsets();
1010 // We want to find how similar two ranks are.
1011 // The more branches in getSectionRank that match, the more similar they are.
1012 // Since each branch corresponds to a bit flag, we can just use
1013 // countLeadingZeros.
1014 static unsigned getRankProximity(OutputSection *A, OutputSection *B) {
1015 return countLeadingZeros(A->SortRank ^ B->SortRank);
1018 // We want to place orphan sections so that they share as much
1019 // characteristics with their neighbors as possible. For example, if
1020 // both are rw, or both are tls.
1021 template <typename ELFT>
1022 static std::vector<OutputSection *>::iterator
1023 findOrphanPos(std::vector<OutputSection *>::iterator B,
1024 std::vector<OutputSection *>::iterator E) {
1025 OutputSection *Sec = *E;
1027 // Find the first element that has as close a rank as possible.
1028 auto I = std::max_element(B, E, [=](OutputSection *A, OutputSection *B) {
1029 return getRankProximity(Sec, A) < getRankProximity(Sec, B);
1034 // Consider all existing sections with the same proximity.
1035 unsigned Proximity = getRankProximity(Sec, *I);
1036 while (I != E && getRankProximity(Sec, *I) == Proximity &&
1037 Sec->SortRank >= (*I)->SortRank)
1042 template <class ELFT> void Writer<ELFT>::sortSections() {
1043 // Don't sort if using -r. It is not necessary and we want to preserve the
1044 // relative order for SHF_LINK_ORDER sections.
1045 if (Config->Relocatable)
1048 if (Script->Opt.HasSections)
1049 Script->adjustSectionsBeforeSorting();
1051 for (OutputSection *Sec : OutputSections)
1052 Sec->SortRank = getSectionRank(Sec);
1054 if (!Script->Opt.HasSections) {
1055 std::stable_sort(OutputSections.begin(), OutputSections.end(),
1056 compareSectionsNonScript);
1060 // The order of the sections in the script is arbitrary and may not agree with
1061 // compareSectionsNonScript. This means that we cannot easily define a
1062 // strict weak ordering. To see why, consider a comparison of a section in the
1063 // script and one not in the script. We have a two simple options:
1064 // * Make them equivalent (a is not less than b, and b is not less than a).
1065 // The problem is then that equivalence has to be transitive and we can
1066 // have sections a, b and c with only b in a script and a less than c
1067 // which breaks this property.
1068 // * Use compareSectionsNonScript. Given that the script order doesn't have
1069 // to match, we can end up with sections a, b, c, d where b and c are in the
1070 // script and c is compareSectionsNonScript less than b. In which case d
1071 // can be equivalent to c, a to b and d < a. As a concrete example:
1072 // .a (rx) # not in script
1073 // .b (rx) # in script
1074 // .c (ro) # in script
1075 // .d (ro) # not in script
1077 // The way we define an order then is:
1078 // * First put script sections at the start and sort the script sections.
1079 // * Move each non-script section to its preferred position. We try
1080 // to put each section in the last position where it it can share
1083 std::stable_sort(OutputSections.begin(), OutputSections.end(),
1086 auto I = OutputSections.begin();
1087 auto E = OutputSections.end();
1089 std::find_if(OutputSections.begin(), E,
1090 [](OutputSection *S) { return S->SectionIndex == INT_MAX; });
1091 while (NonScriptI != E) {
1092 auto Pos = findOrphanPos<ELFT>(I, NonScriptI);
1094 // As an optimization, find all sections with the same sort rank
1095 // and insert them with one rotate.
1096 unsigned Rank = (*NonScriptI)->SortRank;
1097 auto End = std::find_if(NonScriptI + 1, E, [=](OutputSection *Sec) {
1098 return Sec->SortRank != Rank;
1100 std::rotate(Pos, NonScriptI, End);
1104 Script->adjustSectionsAfterSorting();
1107 static void applySynthetic(const std::vector<SyntheticSection *> &Sections,
1108 std::function<void(SyntheticSection *)> Fn) {
1109 for (SyntheticSection *SS : Sections)
1110 if (SS && SS->OutSec && !SS->empty()) {
1112 SS->OutSec->assignOffsets();
1116 // We need to add input synthetic sections early in createSyntheticSections()
1117 // to make them visible from linkescript side. But not all sections are always
1118 // required to be in output. For example we don't need dynamic section content
1119 // sometimes. This function filters out such unused sections from the output.
1120 static void removeUnusedSyntheticSections(std::vector<OutputSection *> &V) {
1121 // All input synthetic sections that can be empty are placed after
1122 // all regular ones. We iterate over them all and exit at first
1124 for (InputSectionBase *S : llvm::reverse(InputSections)) {
1125 SyntheticSection *SS = dyn_cast<SyntheticSection>(S);
1128 if (!SS->empty() || !SS->OutSec)
1131 SS->OutSec->Sections.erase(std::find(SS->OutSec->Sections.begin(),
1132 SS->OutSec->Sections.end(), SS));
1134 // If there are no other sections in the output section, remove it from the
1136 if (SS->OutSec->Sections.empty())
1137 V.erase(std::find(V.begin(), V.end(), SS->OutSec));
1141 // Create output section objects and add them to OutputSections.
1142 template <class ELFT> void Writer<ELFT>::finalizeSections() {
1143 Out::DebugInfo = findSection(".debug_info");
1144 Out::PreinitArray = findSection(".preinit_array");
1145 Out::InitArray = findSection(".init_array");
1146 Out::FiniArray = findSection(".fini_array");
1148 // The linker needs to define SECNAME_start, SECNAME_end and SECNAME_stop
1149 // symbols for sections, so that the runtime can get the start and end
1150 // addresses of each section by section name. Add such symbols.
1151 if (!Config->Relocatable) {
1152 addStartEndSymbols();
1153 for (OutputSection *Sec : OutputSections)
1154 addStartStopSymbols(Sec);
1157 // Add _DYNAMIC symbol. Unlike GNU gold, our _DYNAMIC symbol has no type.
1158 // It should be okay as no one seems to care about the type.
1159 // Even the author of gold doesn't remember why gold behaves that way.
1160 // https://sourceware.org/ml/binutils/2002-03/msg00360.html
1162 addRegular<ELFT>("_DYNAMIC", InX::Dynamic, 0);
1164 // Define __rel[a]_iplt_{start,end} symbols if needed.
1165 addRelIpltSymbols();
1167 // This responsible for splitting up .eh_frame section into
1168 // pieces. The relocation scan uses those pieces, so this has to be
1170 applySynthetic({In<ELFT>::EhFrame},
1171 [](SyntheticSection *SS) { SS->finalizeContents(); });
1173 // Scan relocations. This must be done after every symbol is declared so that
1174 // we can correctly decide if a dynamic relocation is needed.
1175 forEachRelSec(scanRelocations<ELFT>);
1177 if (InX::Plt && !InX::Plt->empty())
1178 InX::Plt->addSymbols();
1179 if (InX::Iplt && !InX::Iplt->empty())
1180 InX::Iplt->addSymbols();
1182 // Now that we have defined all possible global symbols including linker-
1183 // synthesized ones. Visit all symbols to give the finishing touches.
1184 for (Symbol *S : Symtab<ELFT>::X->getSymbols()) {
1185 SymbolBody *Body = S->body();
1187 if (!includeInSymtab(*Body))
1190 InX::SymTab->addSymbol(Body);
1192 if (InX::DynSymTab && S->includeInDynsym()) {
1193 InX::DynSymTab->addSymbol(Body);
1194 if (auto *SS = dyn_cast<SharedSymbol>(Body))
1195 if (cast<SharedFile<ELFT>>(SS->File)->isNeeded())
1196 In<ELFT>::VerNeed->addSymbol(SS);
1200 // Do not proceed if there was an undefined symbol.
1204 // So far we have added sections from input object files.
1205 // This function adds linker-created Out::* sections.
1206 addPredefinedSections();
1207 removeUnusedSyntheticSections(OutputSections);
1211 // This is a bit of a hack. A value of 0 means undef, so we set it
1212 // to 1 t make __ehdr_start defined. The section number is not
1213 // particularly relevant.
1214 Out::ElfHeader->SectionIndex = 1;
1217 for (OutputSection *Sec : OutputSections) {
1218 Sec->SectionIndex = I++;
1219 Sec->ShName = InX::ShStrTab->addString(Sec->Name);
1222 // Binary and relocatable output does not have PHDRS.
1223 // The headers have to be created before finalize as that can influence the
1224 // image base and the dynamic section on mips includes the image base.
1225 if (!Config->Relocatable && !Config->OFormatBinary) {
1226 Phdrs = Script->hasPhdrsCommands() ? Script->createPhdrs() : createPhdrs();
1227 addPtArmExid(Phdrs);
1228 Out::ProgramHeaders->Size = sizeof(Elf_Phdr) * Phdrs.size();
1231 // Dynamic section must be the last one in this list and dynamic
1232 // symbol table section (DynSymTab) must be the first one.
1233 applySynthetic({InX::DynSymTab, InX::Bss, InX::BssRelRo,
1234 InX::GnuHashTab, In<ELFT>::HashTab, InX::SymTab,
1235 InX::ShStrTab, InX::StrTab, In<ELFT>::VerDef,
1236 InX::DynStrTab, InX::GdbIndex, InX::Got,
1237 InX::MipsGot, InX::IgotPlt, InX::GotPlt,
1238 In<ELFT>::RelaDyn, In<ELFT>::RelaIplt, In<ELFT>::RelaPlt,
1239 InX::Plt, InX::Iplt, In<ELFT>::EhFrameHdr,
1240 In<ELFT>::VerSym, In<ELFT>::VerNeed, InX::Dynamic},
1241 [](SyntheticSection *SS) { SS->finalizeContents(); });
1243 // Some architectures use small displacements for jump instructions.
1244 // It is linker's responsibility to create thunks containing long
1245 // jump instructions if jump targets are too far. Create thunks.
1246 if (Target->NeedsThunks) {
1247 // FIXME: only ARM Interworking and Mips LA25 Thunks are implemented,
1249 // do not require address information. To support range extension Thunks
1250 // we need to assign addresses so that we can tell if jump instructions
1251 // are out of range. This will need to turn into a loop that converges
1252 // when no more Thunks are added
1254 if (TC.createThunks(OutputSections))
1255 applySynthetic({InX::MipsGot},
1256 [](SyntheticSection *SS) { SS->updateAllocSize(); });
1258 // Fill other section headers. The dynamic table is finalized
1259 // at the end because some tags like RELSZ depend on result
1260 // of finalizing other sections.
1261 for (OutputSection *Sec : OutputSections)
1262 Sec->finalize<ELFT>();
1264 // createThunks may have added local symbols to the static symbol table
1265 applySynthetic({InX::SymTab, InX::ShStrTab, InX::StrTab},
1266 [](SyntheticSection *SS) { SS->postThunkContents(); });
1269 template <class ELFT> void Writer<ELFT>::addPredefinedSections() {
1270 // ARM ABI requires .ARM.exidx to be terminated by some piece of data.
1271 // We have the terminater synthetic section class. Add that at the end.
1272 auto *OS = dyn_cast_or_null<OutputSection>(findSection(".ARM.exidx"));
1273 if (OS && !OS->Sections.empty() && !Config->Relocatable)
1274 OS->addSection(make<ARMExidxSentinelSection>());
1277 // The linker is expected to define SECNAME_start and SECNAME_end
1278 // symbols for a few sections. This function defines them.
1279 template <class ELFT> void Writer<ELFT>::addStartEndSymbols() {
1280 auto Define = [&](StringRef Start, StringRef End, OutputSection *OS) {
1281 // These symbols resolve to the image base if the section does not exist.
1282 // A special value -1 indicates end of the section.
1284 addOptionalRegular<ELFT>(Start, OS, 0);
1285 addOptionalRegular<ELFT>(End, OS, -1);
1288 OS = Out::ElfHeader;
1289 addOptionalRegular<ELFT>(Start, OS, 0);
1290 addOptionalRegular<ELFT>(End, OS, 0);
1294 Define("__preinit_array_start", "__preinit_array_end", Out::PreinitArray);
1295 Define("__init_array_start", "__init_array_end", Out::InitArray);
1296 Define("__fini_array_start", "__fini_array_end", Out::FiniArray);
1298 if (OutputSection *Sec = findSection(".ARM.exidx"))
1299 Define("__exidx_start", "__exidx_end", Sec);
1302 // If a section name is valid as a C identifier (which is rare because of
1303 // the leading '.'), linkers are expected to define __start_<secname> and
1304 // __stop_<secname> symbols. They are at beginning and end of the section,
1305 // respectively. This is not requested by the ELF standard, but GNU ld and
1306 // gold provide the feature, and used by many programs.
1307 template <class ELFT>
1308 void Writer<ELFT>::addStartStopSymbols(OutputSection *Sec) {
1309 StringRef S = Sec->Name;
1310 if (!isValidCIdentifier(S))
1312 addOptionalRegular<ELFT>(Saver.save("__start_" + S), Sec, 0, STV_DEFAULT);
1313 addOptionalRegular<ELFT>(Saver.save("__stop_" + S), Sec, -1, STV_DEFAULT);
1316 template <class ELFT>
1317 OutputSectionCommand *Writer<ELFT>::findSectionCommand(StringRef Name) {
1318 for (BaseCommand *Base : Script->Opt.Commands)
1319 if (auto *Cmd = dyn_cast<OutputSectionCommand>(Base))
1320 if (Cmd->Name == Name)
1325 template <class ELFT> OutputSection *Writer<ELFT>::findSectionInScript(StringRef Name) {
1326 if (OutputSectionCommand *Cmd = findSectionCommand(Name))
1331 template <class ELFT> OutputSection *Writer<ELFT>::findSection(StringRef Name) {
1332 for (OutputSection *Sec : OutputSections)
1333 if (Sec->Name == Name)
1338 static bool needsPtLoad(OutputSection *Sec) {
1339 if (!(Sec->Flags & SHF_ALLOC))
1342 // Don't allocate VA space for TLS NOBITS sections. The PT_TLS PHDR is
1343 // responsible for allocating space for them, not the PT_LOAD that
1344 // contains the TLS initialization image.
1345 if (Sec->Flags & SHF_TLS && Sec->Type == SHT_NOBITS)
1350 // Linker scripts are responsible for aligning addresses. Unfortunately, most
1351 // linker scripts are designed for creating two PT_LOADs only, one RX and one
1352 // RW. This means that there is no alignment in the RO to RX transition and we
1353 // cannot create a PT_LOAD there.
1354 static uint64_t computeFlags(uint64_t Flags) {
1356 return PF_R | PF_W | PF_X;
1357 if (Config->SingleRoRx && !(Flags & PF_W))
1358 return Flags | PF_X;
1362 // Decide which program headers to create and which sections to include in each
1364 template <class ELFT> std::vector<PhdrEntry> Writer<ELFT>::createPhdrs() {
1365 std::vector<PhdrEntry> Ret;
1366 auto AddHdr = [&](unsigned Type, unsigned Flags) -> PhdrEntry * {
1367 Ret.emplace_back(Type, Flags);
1371 // The first phdr entry is PT_PHDR which describes the program header itself.
1372 AddHdr(PT_PHDR, PF_R)->add(Out::ProgramHeaders);
1374 // PT_INTERP must be the second entry if exists.
1375 if (OutputSection *Sec = findSection(".interp"))
1376 AddHdr(PT_INTERP, Sec->getPhdrFlags())->add(Sec);
1378 // Add the first PT_LOAD segment for regular output sections.
1379 uint64_t Flags = computeFlags(PF_R);
1380 PhdrEntry *Load = AddHdr(PT_LOAD, Flags);
1382 // Add the headers. We will remove them if they don't fit.
1383 Load->add(Out::ElfHeader);
1384 Load->add(Out::ProgramHeaders);
1386 for (OutputSection *Sec : OutputSections) {
1387 if (!(Sec->Flags & SHF_ALLOC))
1389 if (!needsPtLoad(Sec))
1392 // Segments are contiguous memory regions that has the same attributes
1393 // (e.g. executable or writable). There is one phdr for each segment.
1394 // Therefore, we need to create a new phdr when the next section has
1395 // different flags or is loaded at a discontiguous address using AT linker
1397 uint64_t NewFlags = computeFlags(Sec->getPhdrFlags());
1398 if (Script->hasLMA(Sec) || Flags != NewFlags) {
1399 Load = AddHdr(PT_LOAD, NewFlags);
1406 // Add a TLS segment if any.
1407 PhdrEntry TlsHdr(PT_TLS, PF_R);
1408 for (OutputSection *Sec : OutputSections)
1409 if (Sec->Flags & SHF_TLS)
1412 Ret.push_back(std::move(TlsHdr));
1414 // Add an entry for .dynamic.
1416 AddHdr(PT_DYNAMIC, InX::Dynamic->OutSec->getPhdrFlags())
1417 ->add(InX::Dynamic->OutSec);
1419 // PT_GNU_RELRO includes all sections that should be marked as
1420 // read-only by dynamic linker after proccessing relocations.
1421 PhdrEntry RelRo(PT_GNU_RELRO, PF_R);
1422 for (OutputSection *Sec : OutputSections)
1423 if (needsPtLoad(Sec) && isRelroSection(Sec))
1426 Ret.push_back(std::move(RelRo));
1428 // PT_GNU_EH_FRAME is a special section pointing on .eh_frame_hdr.
1429 if (!In<ELFT>::EhFrame->empty() && In<ELFT>::EhFrameHdr &&
1430 In<ELFT>::EhFrame->OutSec && In<ELFT>::EhFrameHdr->OutSec)
1431 AddHdr(PT_GNU_EH_FRAME, In<ELFT>::EhFrameHdr->OutSec->getPhdrFlags())
1432 ->add(In<ELFT>::EhFrameHdr->OutSec);
1434 // PT_OPENBSD_RANDOMIZE is an OpenBSD-specific feature. That makes
1435 // the dynamic linker fill the segment with random data.
1436 if (OutputSection *Sec = findSection(".openbsd.randomdata"))
1437 AddHdr(PT_OPENBSD_RANDOMIZE, Sec->getPhdrFlags())->add(Sec);
1439 // PT_GNU_STACK is a special section to tell the loader to make the
1440 // pages for the stack non-executable. If you really want an executable
1441 // stack, you can pass -z execstack, but that's not recommended for
1442 // security reasons.
1444 if (Config->ZExecstack)
1445 Perm = PF_R | PF_W | PF_X;
1448 AddHdr(PT_GNU_STACK, Perm)->p_memsz = Config->ZStackSize;
1450 // PT_OPENBSD_WXNEEDED is a OpenBSD-specific header to mark the executable
1451 // is expected to perform W^X violations, such as calling mprotect(2) or
1452 // mmap(2) with PROT_WRITE | PROT_EXEC, which is prohibited by default on
1454 if (Config->ZWxneeded)
1455 AddHdr(PT_OPENBSD_WXNEEDED, PF_X);
1457 // Create one PT_NOTE per a group of contiguous .note sections.
1458 PhdrEntry *Note = nullptr;
1459 for (OutputSection *Sec : OutputSections) {
1460 if (Sec->Type == SHT_NOTE) {
1461 if (!Note || Script->hasLMA(Sec))
1462 Note = AddHdr(PT_NOTE, PF_R);
1471 template <class ELFT>
1472 void Writer<ELFT>::addPtArmExid(std::vector<PhdrEntry> &Phdrs) {
1473 if (Config->EMachine != EM_ARM)
1475 auto I = std::find_if(
1476 OutputSections.begin(), OutputSections.end(),
1477 [](OutputSection *Sec) { return Sec->Type == SHT_ARM_EXIDX; });
1478 if (I == OutputSections.end())
1481 // PT_ARM_EXIDX is the ARM EHABI equivalent of PT_GNU_EH_FRAME
1482 PhdrEntry ARMExidx(PT_ARM_EXIDX, PF_R);
1484 Phdrs.push_back(ARMExidx);
1487 // The first section of each PT_LOAD, the first section in PT_GNU_RELRO and the
1488 // first section after PT_GNU_RELRO have to be page aligned so that the dynamic
1489 // linker can set the permissions.
1490 template <class ELFT> void Writer<ELFT>::fixSectionAlignments() {
1491 for (const PhdrEntry &P : Phdrs)
1492 if (P.p_type == PT_LOAD && P.First)
1493 P.First->PageAlign = true;
1495 for (const PhdrEntry &P : Phdrs) {
1496 if (P.p_type != PT_GNU_RELRO)
1499 P.First->PageAlign = true;
1500 // Find the first section after PT_GNU_RELRO. If it is in a PT_LOAD we
1501 // have to align it to a page.
1502 auto End = OutputSections.end();
1503 auto I = std::find(OutputSections.begin(), End, P.Last);
1504 if (I == End || (I + 1) == End)
1506 OutputSection *Sec = *(I + 1);
1507 if (needsPtLoad(Sec))
1508 Sec->PageAlign = true;
1512 // Adjusts the file alignment for a given output section and returns
1513 // its new file offset. The file offset must be the same with its
1514 // virtual address (modulo the page size) so that the loader can load
1515 // executables without any address adjustment.
1516 static uint64_t getFileAlignment(uint64_t Off, OutputSection *Sec) {
1517 OutputSection *First = Sec->FirstInPtLoad;
1518 // If the section is not in a PT_LOAD, we just have to align it.
1520 return alignTo(Off, Sec->Alignment);
1522 // The first section in a PT_LOAD has to have congruent offset and address
1523 // module the page size.
1525 return alignTo(Off, Config->MaxPageSize, Sec->Addr);
1527 // If two sections share the same PT_LOAD the file offset is calculated
1528 // using this formula: Off2 = Off1 + (VA2 - VA1).
1529 return First->Offset + Sec->Addr - First->Addr;
1532 static uint64_t setOffset(OutputSection *Sec, uint64_t Off) {
1533 if (Sec->Type == SHT_NOBITS) {
1538 Off = getFileAlignment(Off, Sec);
1540 return Off + Sec->Size;
1543 template <class ELFT> void Writer<ELFT>::assignFileOffsetsBinary() {
1545 for (OutputSection *Sec : OutputSections)
1546 if (Sec->Flags & SHF_ALLOC)
1547 Off = setOffset(Sec, Off);
1548 FileSize = alignTo(Off, Config->Wordsize);
1551 // Assign file offsets to output sections.
1552 template <class ELFT> void Writer<ELFT>::assignFileOffsets() {
1554 Off = setOffset(Out::ElfHeader, Off);
1555 Off = setOffset(Out::ProgramHeaders, Off);
1557 for (OutputSection *Sec : OutputSections)
1558 Off = setOffset(Sec, Off);
1560 SectionHeaderOff = alignTo(Off, Config->Wordsize);
1561 FileSize = SectionHeaderOff + (OutputSections.size() + 1) * sizeof(Elf_Shdr);
1564 // Finalize the program headers. We call this function after we assign
1565 // file offsets and VAs to all sections.
1566 template <class ELFT> void Writer<ELFT>::setPhdrs() {
1567 for (PhdrEntry &P : Phdrs) {
1568 OutputSection *First = P.First;
1569 OutputSection *Last = P.Last;
1571 P.p_filesz = Last->Offset - First->Offset;
1572 if (Last->Type != SHT_NOBITS)
1573 P.p_filesz += Last->Size;
1574 P.p_memsz = Last->Addr + Last->Size - First->Addr;
1575 P.p_offset = First->Offset;
1576 P.p_vaddr = First->Addr;
1578 P.p_paddr = First->getLMA();
1580 if (P.p_type == PT_LOAD)
1581 P.p_align = Config->MaxPageSize;
1582 else if (P.p_type == PT_GNU_RELRO)
1585 // The TLS pointer goes after PT_TLS. At least glibc will align it,
1586 // so round up the size to make sure the offsets are correct.
1587 if (P.p_type == PT_TLS) {
1590 P.p_memsz = alignTo(P.p_memsz, P.p_align);
1595 // The entry point address is chosen in the following ways.
1597 // 1. the '-e' entry command-line option;
1598 // 2. the ENTRY(symbol) command in a linker control script;
1599 // 3. the value of the symbol start, if present;
1600 // 4. the address of the first byte of the .text section, if present;
1601 // 5. the address 0.
1602 template <class ELFT> uint64_t Writer<ELFT>::getEntryAddr() {
1603 // Case 1, 2 or 3. As a special case, if the symbol is actually
1604 // a number, we'll use that number as an address.
1605 if (SymbolBody *B = Symtab<ELFT>::X->find(Config->Entry))
1608 if (to_integer(Config->Entry, Addr))
1612 if (OutputSection *Sec = findSectionInScript(".text")) {
1613 if (Config->WarnMissingEntry)
1614 warn("cannot find entry symbol " + Config->Entry + "; defaulting to 0x" +
1615 utohexstr(Sec->Addr));
1620 if (Config->WarnMissingEntry)
1621 warn("cannot find entry symbol " + Config->Entry +
1622 "; not setting start address");
1626 static uint16_t getELFType() {
1629 if (Config->Relocatable)
1634 // This function is called after we have assigned address and size
1635 // to each section. This function fixes some predefined
1636 // symbol values that depend on section address and size.
1637 template <class ELFT> void Writer<ELFT>::fixPredefinedSymbols() {
1638 // _etext is the first location after the last read-only loadable segment.
1639 // _edata is the first location after the last read-write loadable segment.
1640 // _end is the first location after the uninitialized data region.
1641 PhdrEntry *Last = nullptr;
1642 PhdrEntry *LastRO = nullptr;
1643 PhdrEntry *LastRW = nullptr;
1644 for (PhdrEntry &P : Phdrs) {
1645 if (P.p_type != PT_LOAD)
1648 if (P.p_flags & PF_W)
1654 auto Set = [](DefinedRegular *S, OutputSection *Sec, uint64_t Value) {
1662 Set(ElfSym::End1, Last->First, Last->p_memsz);
1663 Set(ElfSym::End2, Last->First, Last->p_memsz);
1666 Set(ElfSym::Etext1, LastRO->First, LastRO->p_filesz);
1667 Set(ElfSym::Etext2, LastRO->First, LastRO->p_filesz);
1670 Set(ElfSym::Edata1, LastRW->First, LastRW->p_filesz);
1671 Set(ElfSym::Edata2, LastRW->First, LastRW->p_filesz);
1675 ElfSym::Bss->Section = findSectionInScript(".bss");
1677 // Setup MIPS _gp_disp/__gnu_local_gp symbols which should
1678 // be equal to the _gp symbol's value.
1679 if (Config->EMachine == EM_MIPS) {
1680 if (!ElfSym::MipsGp->Value) {
1681 // Find GP-relative section with the lowest address
1682 // and use this address to calculate default _gp value.
1684 for (const OutputSection *OS : OutputSections)
1685 if ((OS->Flags & SHF_MIPS_GPREL) && OS->Addr < Gp)
1687 if (Gp != (uint64_t)-1)
1688 ElfSym::MipsGp->Value = Gp + 0x7ff0;
1693 template <class ELFT> void Writer<ELFT>::writeHeader() {
1694 uint8_t *Buf = Buffer->getBufferStart();
1695 memcpy(Buf, "\177ELF", 4);
1697 // Write the ELF header.
1698 auto *EHdr = reinterpret_cast<Elf_Ehdr *>(Buf);
1699 EHdr->e_ident[EI_CLASS] = Config->Is64 ? ELFCLASS64 : ELFCLASS32;
1700 EHdr->e_ident[EI_DATA] = Config->IsLE ? ELFDATA2LSB : ELFDATA2MSB;
1701 EHdr->e_ident[EI_VERSION] = EV_CURRENT;
1702 EHdr->e_ident[EI_OSABI] = Config->OSABI;
1703 EHdr->e_type = getELFType();
1704 EHdr->e_machine = Config->EMachine;
1705 EHdr->e_version = EV_CURRENT;
1706 EHdr->e_entry = getEntryAddr();
1707 EHdr->e_shoff = SectionHeaderOff;
1708 EHdr->e_ehsize = sizeof(Elf_Ehdr);
1709 EHdr->e_phnum = Phdrs.size();
1710 EHdr->e_shentsize = sizeof(Elf_Shdr);
1711 EHdr->e_shnum = OutputSections.size() + 1;
1712 EHdr->e_shstrndx = InX::ShStrTab->OutSec->SectionIndex;
1714 if (Config->EMachine == EM_ARM)
1715 // We don't currently use any features incompatible with EF_ARM_EABI_VER5,
1716 // but we don't have any firm guarantees of conformance. Linux AArch64
1717 // kernels (as of 2016) require an EABI version to be set.
1718 EHdr->e_flags = EF_ARM_EABI_VER5;
1719 else if (Config->EMachine == EM_MIPS)
1720 EHdr->e_flags = getMipsEFlags<ELFT>();
1722 if (!Config->Relocatable) {
1723 EHdr->e_phoff = sizeof(Elf_Ehdr);
1724 EHdr->e_phentsize = sizeof(Elf_Phdr);
1727 // Write the program header table.
1728 auto *HBuf = reinterpret_cast<Elf_Phdr *>(Buf + EHdr->e_phoff);
1729 for (PhdrEntry &P : Phdrs) {
1730 HBuf->p_type = P.p_type;
1731 HBuf->p_flags = P.p_flags;
1732 HBuf->p_offset = P.p_offset;
1733 HBuf->p_vaddr = P.p_vaddr;
1734 HBuf->p_paddr = P.p_paddr;
1735 HBuf->p_filesz = P.p_filesz;
1736 HBuf->p_memsz = P.p_memsz;
1737 HBuf->p_align = P.p_align;
1741 // Write the section header table. Note that the first table entry is null.
1742 auto *SHdrs = reinterpret_cast<Elf_Shdr *>(Buf + EHdr->e_shoff);
1743 for (OutputSection *Sec : OutputSections)
1744 Sec->writeHeaderTo<ELFT>(++SHdrs);
1747 // Open a result file.
1748 template <class ELFT> void Writer<ELFT>::openFile() {
1749 if (!Config->Is64 && FileSize > UINT32_MAX) {
1750 error("output file too large: " + Twine(FileSize) + " bytes");
1754 unlinkAsync(Config->OutputFile);
1755 ErrorOr<std::unique_ptr<FileOutputBuffer>> BufferOrErr =
1756 FileOutputBuffer::create(Config->OutputFile, FileSize,
1757 FileOutputBuffer::F_executable);
1759 if (auto EC = BufferOrErr.getError())
1760 error("failed to open " + Config->OutputFile + ": " + EC.message());
1762 Buffer = std::move(*BufferOrErr);
1765 template <class ELFT> void Writer<ELFT>::writeSectionsBinary() {
1766 uint8_t *Buf = Buffer->getBufferStart();
1767 for (BaseCommand *Base : Script->Opt.Commands) {
1768 auto *Cmd = dyn_cast<OutputSectionCommand>(Base);
1771 OutputSection *Sec = Cmd->Sec;
1772 if (Sec->Flags & SHF_ALLOC)
1773 Cmd->writeTo<ELFT>(Buf + Sec->Offset);
1777 // Write section contents to a mmap'ed file.
1778 template <class ELFT> void Writer<ELFT>::writeSections() {
1779 uint8_t *Buf = Buffer->getBufferStart();
1781 // PPC64 needs to process relocations in the .opd section
1782 // before processing relocations in code-containing sections.
1783 if (auto *OpdCmd = findSectionCommand(".opd")) {
1784 Out::Opd = OpdCmd->Sec;
1785 Out::OpdBuf = Buf + Out::Opd->Offset;
1786 OpdCmd->template writeTo<ELFT>(Buf + Out::Opd->Offset);
1789 OutputSection *EhFrameHdr =
1790 (In<ELFT>::EhFrameHdr && !In<ELFT>::EhFrameHdr->empty())
1791 ? In<ELFT>::EhFrameHdr->OutSec
1794 // In -r or -emit-relocs mode, write the relocation sections first as in
1795 // ELf_Rel targets we might find out that we need to modify the relocated
1796 // section while doing it.
1797 for (BaseCommand *Base : Script->Opt.Commands) {
1798 auto *Cmd = dyn_cast<OutputSectionCommand>(Base);
1801 OutputSection *Sec = Cmd->Sec;
1802 if (Sec->Type == SHT_REL || Sec->Type == SHT_RELA)
1803 Cmd->writeTo<ELFT>(Buf + Sec->Offset);
1806 for (BaseCommand *Base : Script->Opt.Commands) {
1807 auto *Cmd = dyn_cast<OutputSectionCommand>(Base);
1810 OutputSection *Sec = Cmd->Sec;
1811 if (Sec != Out::Opd && Sec != EhFrameHdr && Sec->Type != SHT_REL &&
1812 Sec->Type != SHT_RELA)
1813 Cmd->writeTo<ELFT>(Buf + Sec->Offset);
1816 // The .eh_frame_hdr depends on .eh_frame section contents, therefore
1817 // it should be written after .eh_frame is written.
1819 OutputSectionCommand *Cmd = Script->getCmd(EhFrameHdr);
1820 Cmd->writeTo<ELFT>(Buf + EhFrameHdr->Offset);
1824 template <class ELFT> void Writer<ELFT>::writeBuildId() {
1825 if (!InX::BuildId || !InX::BuildId->OutSec)
1828 // Compute a hash of all sections of the output file.
1829 uint8_t *Start = Buffer->getBufferStart();
1830 uint8_t *End = Start + FileSize;
1831 InX::BuildId->writeBuildId({Start, End});
1834 template void elf::writeResult<ELF32LE>();
1835 template void elf::writeResult<ELF32BE>();
1836 template void elf::writeResult<ELF64LE>();
1837 template void elf::writeResult<ELF64BE>();