AMDGPU: Fix warnings introduced by r310336
[llvm-project.git] / lld / ELF / Writer.cpp
blobe82be4f86f22235fc2f8344d15e13efa45743900
1 //===- Writer.cpp ---------------------------------------------------------===//
2 //
3 // The LLVM Linker
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
10 #include "Writer.h"
11 #include "Config.h"
12 #include "Filesystem.h"
13 #include "LinkerScript.h"
14 #include "MapFile.h"
15 #include "Memory.h"
16 #include "OutputSections.h"
17 #include "Relocations.h"
18 #include "Strings.h"
19 #include "SymbolTable.h"
20 #include "SyntheticSections.h"
21 #include "Target.h"
22 #include "Threads.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"
27 #include <climits>
29 using namespace llvm;
30 using namespace llvm::ELF;
31 using namespace llvm::object;
32 using namespace llvm::support;
33 using namespace llvm::support::endian;
35 using namespace lld;
36 using namespace lld::elf;
38 namespace {
39 // The writer writes a SymbolTable result to a file.
40 template <class ELFT> class Writer {
41 public:
42 typedef typename ELFT::Shdr Elf_Shdr;
43 typedef typename ELFT::Ehdr Elf_Ehdr;
44 typedef typename ELFT::Phdr Elf_Phdr;
46 void run();
48 private:
49 void createSyntheticSections();
50 void copyLocalSymbols();
51 void addSectionSymbols();
52 void addReservedSymbols();
53 void createSections();
54 void forEachRelSec(std::function<void(InputSectionBase &)> Fn);
55 void sortSections();
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();
64 void setPhdrs();
65 void fixSectionAlignments();
66 void fixPredefinedSymbols();
67 void openFile();
68 void writeTrapInstr();
69 void writeHeader();
70 void writeSections();
71 void writeSectionsBinary();
72 void writeBuildId();
74 std::unique_ptr<FileOutputBuffer> Buffer;
76 OutputSectionFactory Factory;
78 void addRelIpltSymbols();
79 void addStartEndSymbols();
80 void addStartStopSymbols(OutputSection *Sec);
81 uint64_t getEntryAddr();
82 OutputSection *findSection(StringRef Name);
84 std::vector<PhdrEntry *> Phdrs;
86 uint64_t FileSize;
87 uint64_t SectionHeaderOff;
89 bool HasGotBaseSym = false;
91 } // anonymous namespace
93 StringRef elf::getOutputSectionName(StringRef Name) {
94 // ".zdebug_" is a prefix for ZLIB-compressed sections.
95 // Because we decompressed input sections, we want to remove 'z'.
96 if (Name.startswith(".zdebug_"))
97 return Saver.save("." + Name.substr(2));
99 if (Config->Relocatable)
100 return Name;
102 for (StringRef V :
103 {".text.", ".rodata.", ".data.rel.ro.", ".data.", ".bss.rel.ro.",
104 ".bss.", ".init_array.", ".fini_array.", ".ctors.", ".dtors.", ".tbss.",
105 ".gcc_except_table.", ".tdata.", ".ARM.exidx.", ".ARM.extab."}) {
106 StringRef Prefix = V.drop_back();
107 if (Name.startswith(V) || Name == Prefix)
108 return Prefix;
111 // CommonSection is identified as "COMMON" in linker scripts.
112 // By default, it should go to .bss section.
113 if (Name == "COMMON")
114 return ".bss";
116 return Name;
119 template <class ELFT> static bool needsInterpSection() {
120 return !SharedFile<ELFT>::Instances.empty() &&
121 !Config->DynamicLinker.empty() && !Script->ignoreInterpSection();
124 template <class ELFT> void elf::writeResult() { Writer<ELFT>().run(); }
126 template <class ELFT> void Writer<ELFT>::removeEmptyPTLoad() {
127 auto I = llvm::remove_if(Phdrs, [&](const PhdrEntry *P) {
128 if (P->p_type != PT_LOAD)
129 return false;
130 if (!P->First)
131 return true;
132 uint64_t Size = P->Last->Addr + P->Last->Size - P->First->Addr;
133 return Size == 0;
135 Phdrs.erase(I, Phdrs.end());
138 template <class ELFT> static void combineEhFrameSections() {
139 for (InputSectionBase *&S : InputSections) {
140 EhInputSection *ES = dyn_cast<EhInputSection>(S);
141 if (!ES || !ES->Live)
142 continue;
144 In<ELFT>::EhFrame->addSection(ES);
145 S = nullptr;
148 std::vector<InputSectionBase *> &V = InputSections;
149 V.erase(std::remove(V.begin(), V.end(), nullptr), V.end());
152 // The main function of the writer.
153 template <class ELFT> void Writer<ELFT>::run() {
154 // Create linker-synthesized sections such as .got or .plt.
155 // Such sections are of type input section.
156 createSyntheticSections();
158 if (!Config->Relocatable)
159 combineEhFrameSections<ELFT>();
161 // We need to create some reserved symbols such as _end. Create them.
162 if (!Config->Relocatable)
163 addReservedSymbols();
165 // Create output sections.
166 if (Script->Opt.HasSections) {
167 // If linker script contains SECTIONS commands, let it create sections.
168 Script->processCommands(Factory);
170 // Linker scripts may have left some input sections unassigned.
171 // Assign such sections using the default rule.
172 Script->addOrphanSections(Factory);
173 } else {
174 // If linker script does not contain SECTIONS commands, create
175 // output sections by default rules. We still need to give the
176 // linker script a chance to run, because it might contain
177 // non-SECTIONS commands such as ASSERT.
178 Script->processCommands(Factory);
179 createSections();
182 if (Config->Discard != DiscardPolicy::All)
183 copyLocalSymbols();
185 if (Config->CopyRelocs)
186 addSectionSymbols();
188 // Now that we have a complete set of output sections. This function
189 // completes section contents. For example, we need to add strings
190 // to the string table, and add entries to .got and .plt.
191 // finalizeSections does that.
192 finalizeSections();
193 if (ErrorCount)
194 return;
196 if (!Script->Opt.HasSections && !Config->Relocatable)
197 fixSectionAlignments();
199 // If -compressed-debug-sections is specified, we need to compress
200 // .debug_* sections. Do it right now because it changes the size of
201 // output sections.
202 parallelForEach(OutputSections,
203 [](OutputSection *Sec) { Sec->maybeCompress<ELFT>(); });
205 Script->assignAddresses();
206 Script->allocateHeaders(Phdrs);
208 // Remove empty PT_LOAD to avoid causing the dynamic linker to try to mmap a
209 // 0 sized region. This has to be done late since only after assignAddresses
210 // we know the size of the sections.
211 removeEmptyPTLoad();
213 if (!Config->OFormatBinary)
214 assignFileOffsets();
215 else
216 assignFileOffsetsBinary();
218 setPhdrs();
220 if (Config->Relocatable) {
221 for (OutputSection *Sec : OutputSections)
222 Sec->Addr = 0;
223 } else {
224 fixPredefinedSymbols();
227 // It does not make sense try to open the file if we have error already.
228 if (ErrorCount)
229 return;
230 // Write the result down to a file.
231 openFile();
232 if (ErrorCount)
233 return;
235 if (!Config->OFormatBinary) {
236 writeTrapInstr();
237 writeHeader();
238 writeSections();
239 } else {
240 writeSectionsBinary();
243 // Backfill .note.gnu.build-id section content. This is done at last
244 // because the content is usually a hash value of the entire output file.
245 writeBuildId();
246 if (ErrorCount)
247 return;
249 // Handle -Map option.
250 writeMapFile<ELFT>();
251 if (ErrorCount)
252 return;
254 if (auto EC = Buffer->commit())
255 error("failed to write to the output file: " + EC.message());
257 // Flush the output streams and exit immediately. A full shutdown
258 // is a good test that we are keeping track of all allocated memory,
259 // but actually freeing it is a waste of time in a regular linker run.
260 if (Config->ExitEarly)
261 exitLld(0);
264 // Initialize Out members.
265 template <class ELFT> void Writer<ELFT>::createSyntheticSections() {
266 // Initialize all pointers with NULL. This is needed because
267 // you can call lld::elf::main more than once as a library.
268 memset(&Out::First, 0, sizeof(Out));
270 auto Add = [](InputSectionBase *Sec) { InputSections.push_back(Sec); };
272 InX::DynStrTab = make<StringTableSection>(".dynstr", true);
273 InX::Dynamic = make<DynamicSection<ELFT>>();
274 In<ELFT>::RelaDyn = make<RelocationSection<ELFT>>(
275 Config->IsRela ? ".rela.dyn" : ".rel.dyn", Config->ZCombreloc);
276 InX::ShStrTab = make<StringTableSection>(".shstrtab", false);
278 Out::ElfHeader = make<OutputSection>("", 0, SHF_ALLOC);
279 Out::ElfHeader->Size = sizeof(Elf_Ehdr);
280 Out::ProgramHeaders = make<OutputSection>("", 0, SHF_ALLOC);
281 Out::ProgramHeaders->updateAlignment(Config->Wordsize);
283 if (needsInterpSection<ELFT>()) {
284 InX::Interp = createInterpSection();
285 Add(InX::Interp);
286 } else {
287 InX::Interp = nullptr;
290 if (Config->Strip != StripPolicy::All) {
291 InX::StrTab = make<StringTableSection>(".strtab", false);
292 InX::SymTab = make<SymbolTableSection<ELFT>>(*InX::StrTab);
295 if (Config->BuildId != BuildIdKind::None) {
296 InX::BuildId = make<BuildIdSection>();
297 Add(InX::BuildId);
300 InX::Common = createCommonSection<ELFT>();
301 if (InX::Common)
302 Add(InX::Common);
304 InX::Bss = make<BssSection>(".bss");
305 Add(InX::Bss);
306 InX::BssRelRo = make<BssSection>(".bss.rel.ro");
307 Add(InX::BssRelRo);
309 // Add MIPS-specific sections.
310 bool HasDynSymTab = !SharedFile<ELFT>::Instances.empty() || Config->Pic ||
311 Config->ExportDynamic;
312 if (Config->EMachine == EM_MIPS) {
313 if (!Config->Shared && HasDynSymTab) {
314 InX::MipsRldMap = make<MipsRldMapSection>();
315 Add(InX::MipsRldMap);
317 if (auto *Sec = MipsAbiFlagsSection<ELFT>::create())
318 Add(Sec);
319 if (auto *Sec = MipsOptionsSection<ELFT>::create())
320 Add(Sec);
321 if (auto *Sec = MipsReginfoSection<ELFT>::create())
322 Add(Sec);
325 if (HasDynSymTab) {
326 InX::DynSymTab = make<SymbolTableSection<ELFT>>(*InX::DynStrTab);
327 Add(InX::DynSymTab);
329 In<ELFT>::VerSym = make<VersionTableSection<ELFT>>();
330 Add(In<ELFT>::VerSym);
332 if (!Config->VersionDefinitions.empty()) {
333 In<ELFT>::VerDef = make<VersionDefinitionSection<ELFT>>();
334 Add(In<ELFT>::VerDef);
337 In<ELFT>::VerNeed = make<VersionNeedSection<ELFT>>();
338 Add(In<ELFT>::VerNeed);
340 if (Config->GnuHash) {
341 InX::GnuHashTab = make<GnuHashTableSection>();
342 Add(InX::GnuHashTab);
345 if (Config->SysvHash) {
346 In<ELFT>::HashTab = make<HashTableSection<ELFT>>();
347 Add(In<ELFT>::HashTab);
350 Add(InX::Dynamic);
351 Add(InX::DynStrTab);
352 Add(In<ELFT>::RelaDyn);
355 // Add .got. MIPS' .got is so different from the other archs,
356 // it has its own class.
357 if (Config->EMachine == EM_MIPS) {
358 InX::MipsGot = make<MipsGotSection>();
359 Add(InX::MipsGot);
360 } else {
361 InX::Got = make<GotSection>();
362 Add(InX::Got);
365 InX::GotPlt = make<GotPltSection>();
366 Add(InX::GotPlt);
367 InX::IgotPlt = make<IgotPltSection>();
368 Add(InX::IgotPlt);
370 if (Config->GdbIndex) {
371 InX::GdbIndex = createGdbIndex<ELFT>();
372 Add(InX::GdbIndex);
375 // We always need to add rel[a].plt to output if it has entries.
376 // Even for static linking it can contain R_[*]_IRELATIVE relocations.
377 In<ELFT>::RelaPlt = make<RelocationSection<ELFT>>(
378 Config->IsRela ? ".rela.plt" : ".rel.plt", false /*Sort*/);
379 Add(In<ELFT>::RelaPlt);
381 // The RelaIplt immediately follows .rel.plt (.rel.dyn for ARM) to ensure
382 // that the IRelative relocations are processed last by the dynamic loader
383 In<ELFT>::RelaIplt = make<RelocationSection<ELFT>>(
384 (Config->EMachine == EM_ARM) ? ".rel.dyn" : In<ELFT>::RelaPlt->Name,
385 false /*Sort*/);
386 Add(In<ELFT>::RelaIplt);
388 InX::Plt = make<PltSection>(Target->PltHeaderSize);
389 Add(InX::Plt);
390 InX::Iplt = make<PltSection>(0);
391 Add(InX::Iplt);
393 if (!Config->Relocatable) {
394 if (Config->EhFrameHdr) {
395 In<ELFT>::EhFrameHdr = make<EhFrameHeader<ELFT>>();
396 Add(In<ELFT>::EhFrameHdr);
398 In<ELFT>::EhFrame = make<EhFrameSection<ELFT>>();
399 Add(In<ELFT>::EhFrame);
402 if (InX::SymTab)
403 Add(InX::SymTab);
404 Add(InX::ShStrTab);
405 if (InX::StrTab)
406 Add(InX::StrTab);
409 static bool shouldKeepInSymtab(SectionBase *Sec, StringRef SymName,
410 const SymbolBody &B) {
411 if (B.isFile() || B.isSection())
412 return false;
414 // If sym references a section in a discarded group, don't keep it.
415 if (Sec == &InputSection::Discarded)
416 return false;
418 if (Config->Discard == DiscardPolicy::None)
419 return true;
421 // In ELF assembly .L symbols are normally discarded by the assembler.
422 // If the assembler fails to do so, the linker discards them if
423 // * --discard-locals is used.
424 // * The symbol is in a SHF_MERGE section, which is normally the reason for
425 // the assembler keeping the .L symbol.
426 if (!SymName.startswith(".L") && !SymName.empty())
427 return true;
429 if (Config->Discard == DiscardPolicy::Locals)
430 return false;
432 return !Sec || !(Sec->Flags & SHF_MERGE);
435 static bool includeInSymtab(const SymbolBody &B) {
436 if (!B.isLocal() && !B.symbol()->IsUsedInRegularObj)
437 return false;
439 if (auto *D = dyn_cast<DefinedRegular>(&B)) {
440 // Always include absolute symbols.
441 SectionBase *Sec = D->Section;
442 if (!Sec)
443 return true;
444 if (auto *IS = dyn_cast<InputSectionBase>(Sec)) {
445 Sec = IS->Repl;
446 IS = cast<InputSectionBase>(Sec);
447 // Exclude symbols pointing to garbage-collected sections.
448 if (!IS->Live)
449 return false;
451 if (auto *S = dyn_cast<MergeInputSection>(Sec))
452 if (!S->getSectionPiece(D->Value)->Live)
453 return false;
455 return true;
458 // Local symbols are not in the linker's symbol table. This function scans
459 // each object file's symbol table to copy local symbols to the output.
460 template <class ELFT> void Writer<ELFT>::copyLocalSymbols() {
461 if (!InX::SymTab)
462 return;
463 for (ObjFile<ELFT> *F : ObjFile<ELFT>::Instances) {
464 for (SymbolBody *B : F->getLocalSymbols()) {
465 if (!B->IsLocal)
466 fatal(toString(F) +
467 ": broken object: getLocalSymbols returns a non-local symbol");
468 auto *DR = dyn_cast<DefinedRegular>(B);
470 // No reason to keep local undefined symbol in symtab.
471 if (!DR)
472 continue;
473 if (!includeInSymtab(*B))
474 continue;
476 SectionBase *Sec = DR->Section;
477 if (!shouldKeepInSymtab(Sec, B->getName(), *B))
478 continue;
479 InX::SymTab->addSymbol(B);
484 template <class ELFT> void Writer<ELFT>::addSectionSymbols() {
485 // Create one STT_SECTION symbol for each output section we might
486 // have a relocation with.
487 for (BaseCommand *Base : Script->Opt.Commands) {
488 auto *Sec = dyn_cast<OutputSection>(Base);
489 if (!Sec)
490 continue;
491 auto I = llvm::find_if(Sec->Commands, [](BaseCommand *Base) {
492 if (auto *ISD = dyn_cast<InputSectionDescription>(Base))
493 return !ISD->Sections.empty();
494 return false;
496 if (I == Sec->Commands.end())
497 continue;
498 InputSection *IS = cast<InputSectionDescription>(*I)->Sections[0];
499 if (isa<SyntheticSection>(IS) || IS->Type == SHT_REL ||
500 IS->Type == SHT_RELA)
501 continue;
503 auto *Sym =
504 make<DefinedRegular>("", /*IsLocal=*/true, /*StOther=*/0, STT_SECTION,
505 /*Value=*/0, /*Size=*/0, IS);
506 InX::SymTab->addSymbol(Sym);
510 // Today's loaders have a feature to make segments read-only after
511 // processing dynamic relocations to enhance security. PT_GNU_RELRO
512 // is defined for that.
514 // This function returns true if a section needs to be put into a
515 // PT_GNU_RELRO segment.
516 static bool isRelroSection(const OutputSection *Sec) {
517 if (!Config->ZRelro)
518 return false;
520 uint64_t Flags = Sec->Flags;
522 // Non-allocatable or non-writable sections don't need RELRO because
523 // they are not writable or not even mapped to memory in the first place.
524 // RELRO is for sections that are essentially read-only but need to
525 // be writable only at process startup to allow dynamic linker to
526 // apply relocations.
527 if (!(Flags & SHF_ALLOC) || !(Flags & SHF_WRITE))
528 return false;
530 // Once initialized, TLS data segments are used as data templates
531 // for a thread-local storage. For each new thread, runtime
532 // allocates memory for a TLS and copy templates there. No thread
533 // are supposed to use templates directly. Thus, it can be in RELRO.
534 if (Flags & SHF_TLS)
535 return true;
537 // .init_array, .preinit_array and .fini_array contain pointers to
538 // functions that are executed on process startup or exit. These
539 // pointers are set by the static linker, and they are not expected
540 // to change at runtime. But if you are an attacker, you could do
541 // interesting things by manipulating pointers in .fini_array, for
542 // example. So they are put into RELRO.
543 uint32_t Type = Sec->Type;
544 if (Type == SHT_INIT_ARRAY || Type == SHT_FINI_ARRAY ||
545 Type == SHT_PREINIT_ARRAY)
546 return true;
548 // .got contains pointers to external symbols. They are resolved by
549 // the dynamic linker when a module is loaded into memory, and after
550 // that they are not expected to change. So, it can be in RELRO.
551 if (InX::Got && Sec == InX::Got->getParent())
552 return true;
554 // .got.plt contains pointers to external function symbols. They are
555 // by default resolved lazily, so we usually cannot put it into RELRO.
556 // However, if "-z now" is given, the lazy symbol resolution is
557 // disabled, which enables us to put it into RELRO.
558 if (Sec == InX::GotPlt->getParent())
559 return Config->ZNow;
561 // .dynamic section contains data for the dynamic linker, and
562 // there's no need to write to it at runtime, so it's better to put
563 // it into RELRO.
564 if (Sec == InX::Dynamic->getParent())
565 return true;
567 // .bss.rel.ro is used for copy relocations for read-only symbols.
568 // Since the dynamic linker needs to process copy relocations, the
569 // section cannot be read-only, but once initialized, they shouldn't
570 // change.
571 if (Sec == InX::BssRelRo->getParent())
572 return true;
574 // Sections with some special names are put into RELRO. This is a
575 // bit unfortunate because section names shouldn't be significant in
576 // ELF in spirit. But in reality many linker features depend on
577 // magic section names.
578 StringRef S = Sec->Name;
579 return S == ".data.rel.ro" || S == ".ctors" || S == ".dtors" || S == ".jcr" ||
580 S == ".eh_frame" || S == ".openbsd.randomdata";
583 // We compute a rank for each section. The rank indicates where the
584 // section should be placed in the file. Instead of using simple
585 // numbers (0,1,2...), we use a series of flags. One for each decision
586 // point when placing the section.
587 // Using flags has two key properties:
588 // * It is easy to check if a give branch was taken.
589 // * It is easy two see how similar two ranks are (see getRankProximity).
590 enum RankFlags {
591 RF_NOT_ADDR_SET = 1 << 16,
592 RF_NOT_INTERP = 1 << 15,
593 RF_NOT_ALLOC = 1 << 14,
594 RF_WRITE = 1 << 13,
595 RF_EXEC_WRITE = 1 << 12,
596 RF_EXEC = 1 << 11,
597 RF_NON_TLS_BSS = 1 << 10,
598 RF_NON_TLS_BSS_RO = 1 << 9,
599 RF_NOT_TLS = 1 << 8,
600 RF_BSS = 1 << 7,
601 RF_PPC_NOT_TOCBSS = 1 << 6,
602 RF_PPC_OPD = 1 << 5,
603 RF_PPC_TOCL = 1 << 4,
604 RF_PPC_TOC = 1 << 3,
605 RF_PPC_BRANCH_LT = 1 << 2,
606 RF_MIPS_GPREL = 1 << 1,
607 RF_MIPS_NOT_GOT = 1 << 0
610 static unsigned getSectionRank(const OutputSection *Sec) {
611 unsigned Rank = 0;
613 // We want to put section specified by -T option first, so we
614 // can start assigning VA starting from them later.
615 if (Config->SectionStartMap.count(Sec->Name))
616 return Rank;
617 Rank |= RF_NOT_ADDR_SET;
619 // Put .interp first because some loaders want to see that section
620 // on the first page of the executable file when loaded into memory.
621 if (Sec->Name == ".interp")
622 return Rank;
623 Rank |= RF_NOT_INTERP;
625 // Allocatable sections go first to reduce the total PT_LOAD size and
626 // so debug info doesn't change addresses in actual code.
627 if (!(Sec->Flags & SHF_ALLOC))
628 return Rank | RF_NOT_ALLOC;
630 // Sort sections based on their access permission in the following
631 // order: R, RX, RWX, RW. This order is based on the following
632 // considerations:
633 // * Read-only sections come first such that they go in the
634 // PT_LOAD covering the program headers at the start of the file.
635 // * Read-only, executable sections come next, unless the
636 // -no-rosegment option is used.
637 // * Writable, executable sections follow such that .plt on
638 // architectures where it needs to be writable will be placed
639 // between .text and .data.
640 // * Writable sections come last, such that .bss lands at the very
641 // end of the last PT_LOAD.
642 bool IsExec = Sec->Flags & SHF_EXECINSTR;
643 bool IsWrite = Sec->Flags & SHF_WRITE;
645 if (IsExec) {
646 if (IsWrite)
647 Rank |= RF_EXEC_WRITE;
648 else if (!Config->SingleRoRx)
649 Rank |= RF_EXEC;
650 } else {
651 if (IsWrite)
652 Rank |= RF_WRITE;
655 // If we got here we know that both A and B are in the same PT_LOAD.
657 bool IsTls = Sec->Flags & SHF_TLS;
658 bool IsNoBits = Sec->Type == SHT_NOBITS;
660 // The first requirement we have is to put (non-TLS) nobits sections last. The
661 // reason is that the only thing the dynamic linker will see about them is a
662 // p_memsz that is larger than p_filesz. Seeing that it zeros the end of the
663 // PT_LOAD, so that has to correspond to the nobits sections.
664 bool IsNonTlsNoBits = IsNoBits && !IsTls;
665 if (IsNonTlsNoBits)
666 Rank |= RF_NON_TLS_BSS;
668 // We place nobits RelRo sections before plain r/w ones, and non-nobits RelRo
669 // sections after r/w ones, so that the RelRo sections are contiguous.
670 bool IsRelRo = isRelroSection(Sec);
671 if (IsNonTlsNoBits && !IsRelRo)
672 Rank |= RF_NON_TLS_BSS_RO;
673 if (!IsNonTlsNoBits && IsRelRo)
674 Rank |= RF_NON_TLS_BSS_RO;
676 // The TLS initialization block needs to be a single contiguous block in a R/W
677 // PT_LOAD, so stick TLS sections directly before the other RelRo R/W
678 // sections. The TLS NOBITS sections are placed here as they don't take up
679 // virtual address space in the PT_LOAD.
680 if (!IsTls)
681 Rank |= RF_NOT_TLS;
683 // Within the TLS initialization block, the non-nobits sections need to appear
684 // first.
685 if (IsNoBits)
686 Rank |= RF_BSS;
688 // // Some architectures have additional ordering restrictions for sections
689 // // within the same PT_LOAD.
690 if (Config->EMachine == EM_PPC64) {
691 // PPC64 has a number of special SHT_PROGBITS+SHF_ALLOC+SHF_WRITE sections
692 // that we would like to make sure appear is a specific order to maximize
693 // their coverage by a single signed 16-bit offset from the TOC base
694 // pointer. Conversely, the special .tocbss section should be first among
695 // all SHT_NOBITS sections. This will put it next to the loaded special
696 // PPC64 sections (and, thus, within reach of the TOC base pointer).
697 StringRef Name = Sec->Name;
698 if (Name != ".tocbss")
699 Rank |= RF_PPC_NOT_TOCBSS;
701 if (Name == ".opd")
702 Rank |= RF_PPC_OPD;
704 if (Name == ".toc1")
705 Rank |= RF_PPC_TOCL;
707 if (Name == ".toc")
708 Rank |= RF_PPC_TOC;
710 if (Name == ".branch_lt")
711 Rank |= RF_PPC_BRANCH_LT;
713 if (Config->EMachine == EM_MIPS) {
714 // All sections with SHF_MIPS_GPREL flag should be grouped together
715 // because data in these sections is addressable with a gp relative address.
716 if (Sec->Flags & SHF_MIPS_GPREL)
717 Rank |= RF_MIPS_GPREL;
719 if (Sec->Name != ".got")
720 Rank |= RF_MIPS_NOT_GOT;
723 return Rank;
726 static bool compareSections(const BaseCommand *ACmd, const BaseCommand *BCmd) {
727 const OutputSection *A = cast<OutputSection>(ACmd);
728 const OutputSection *B = cast<OutputSection>(BCmd);
729 if (A->SortRank != B->SortRank)
730 return A->SortRank < B->SortRank;
731 if (!(A->SortRank & RF_NOT_ADDR_SET))
732 return Config->SectionStartMap.lookup(A->Name) <
733 Config->SectionStartMap.lookup(B->Name);
734 return false;
737 void PhdrEntry::add(OutputSection *Sec) {
738 Last = Sec;
739 if (!First)
740 First = Sec;
741 p_align = std::max(p_align, Sec->Alignment);
742 if (p_type == PT_LOAD)
743 Sec->FirstInPtLoad = First;
746 template <class ELFT>
747 static Symbol *addRegular(StringRef Name, SectionBase *Sec, uint64_t Value,
748 uint8_t StOther = STV_HIDDEN,
749 uint8_t Binding = STB_WEAK) {
750 // The linker generated symbols are added as STB_WEAK to allow user defined
751 // ones to override them.
752 return Symtab->addRegular<ELFT>(Name, StOther, STT_NOTYPE, Value,
753 /*Size=*/0, Binding, Sec,
754 /*File=*/nullptr);
757 template <class ELFT>
758 static DefinedRegular *
759 addOptionalRegular(StringRef Name, SectionBase *Sec, uint64_t Val,
760 uint8_t StOther = STV_HIDDEN, uint8_t Binding = STB_GLOBAL) {
761 SymbolBody *S = Symtab->find(Name);
762 if (!S)
763 return nullptr;
764 if (S->isInCurrentDSO())
765 return nullptr;
766 return cast<DefinedRegular>(
767 addRegular<ELFT>(Name, Sec, Val, StOther, Binding)->body());
770 // The beginning and the ending of .rel[a].plt section are marked
771 // with __rel[a]_iplt_{start,end} symbols if it is a statically linked
772 // executable. The runtime needs these symbols in order to resolve
773 // all IRELATIVE relocs on startup. For dynamic executables, we don't
774 // need these symbols, since IRELATIVE relocs are resolved through GOT
775 // and PLT. For details, see http://www.airs.com/blog/archives/403.
776 template <class ELFT> void Writer<ELFT>::addRelIpltSymbols() {
777 if (!Config->Static)
778 return;
779 StringRef S = Config->IsRela ? "__rela_iplt_start" : "__rel_iplt_start";
780 addOptionalRegular<ELFT>(S, In<ELFT>::RelaIplt, 0, STV_HIDDEN, STB_WEAK);
782 S = Config->IsRela ? "__rela_iplt_end" : "__rel_iplt_end";
783 addOptionalRegular<ELFT>(S, In<ELFT>::RelaIplt, -1, STV_HIDDEN, STB_WEAK);
786 // The linker is expected to define some symbols depending on
787 // the linking result. This function defines such symbols.
788 template <class ELFT> void Writer<ELFT>::addReservedSymbols() {
789 if (Config->EMachine == EM_MIPS) {
790 // Define _gp for MIPS. st_value of _gp symbol will be updated by Writer
791 // so that it points to an absolute address which by default is relative
792 // to GOT. Default offset is 0x7ff0.
793 // See "Global Data Symbols" in Chapter 6 in the following document:
794 // ftp://www.linux-mips.org/pub/linux/mips/doc/ABI/mipsabi.pdf
795 ElfSym::MipsGp = Symtab->addAbsolute<ELFT>("_gp", STV_HIDDEN, STB_LOCAL);
797 // On MIPS O32 ABI, _gp_disp is a magic symbol designates offset between
798 // start of function and 'gp' pointer into GOT.
799 if (Symtab->find("_gp_disp"))
800 ElfSym::MipsGpDisp =
801 Symtab->addAbsolute<ELFT>("_gp_disp", STV_HIDDEN, STB_LOCAL);
803 // The __gnu_local_gp is a magic symbol equal to the current value of 'gp'
804 // pointer. This symbol is used in the code generated by .cpload pseudo-op
805 // in case of using -mno-shared option.
806 // https://sourceware.org/ml/binutils/2004-12/msg00094.html
807 if (Symtab->find("__gnu_local_gp"))
808 ElfSym::MipsLocalGp =
809 Symtab->addAbsolute<ELFT>("__gnu_local_gp", STV_HIDDEN, STB_LOCAL);
812 // The _GLOBAL_OFFSET_TABLE_ symbol is defined by target convention to
813 // be at some offset from the base of the .got section, usually 0 or the end
814 // of the .got
815 InputSection *GotSection = InX::MipsGot ? cast<InputSection>(InX::MipsGot)
816 : cast<InputSection>(InX::Got);
817 ElfSym::GlobalOffsetTable = addOptionalRegular<ELFT>(
818 "_GLOBAL_OFFSET_TABLE_", GotSection, Target->GotBaseSymOff);
820 // __tls_get_addr is defined by the dynamic linker for dynamic ELFs. For
821 // static linking the linker is required to optimize away any references to
822 // __tls_get_addr, so it's not defined anywhere. Create a hidden definition
823 // to avoid the undefined symbol error.
824 if (!InX::DynSymTab)
825 Symtab->addIgnored<ELFT>("__tls_get_addr");
827 // __ehdr_start is the location of ELF file headers. Note that we define
828 // this symbol unconditionally even when using a linker script, which
829 // differs from the behavior implemented by GNU linker which only define
830 // this symbol if ELF headers are in the memory mapped segment.
831 // __executable_start is not documented, but the expectation of at
832 // least the android libc is that it points to the elf header too.
833 // __dso_handle symbol is passed to cxa_finalize as a marker to identify
834 // each DSO. The address of the symbol doesn't matter as long as they are
835 // different in different DSOs, so we chose the start address of the DSO.
836 for (const char *Name :
837 {"__ehdr_start", "__executable_start", "__dso_handle"})
838 addOptionalRegular<ELFT>(Name, Out::ElfHeader, 0, STV_HIDDEN);
840 // If linker script do layout we do not need to create any standart symbols.
841 if (Script->Opt.HasSections)
842 return;
844 auto Add = [](StringRef S) {
845 return addOptionalRegular<ELFT>(S, Out::ElfHeader, 0, STV_DEFAULT);
848 ElfSym::Bss = Add("__bss_start");
849 ElfSym::End1 = Add("end");
850 ElfSym::End2 = Add("_end");
851 ElfSym::Etext1 = Add("etext");
852 ElfSym::Etext2 = Add("_etext");
853 ElfSym::Edata1 = Add("edata");
854 ElfSym::Edata2 = Add("_edata");
857 // Sort input sections by section name suffixes for
858 // __attribute__((init_priority(N))).
859 static void sortInitFini(OutputSection *Cmd) {
860 if (Cmd)
861 Cmd->sortInitFini();
864 // Sort input sections by the special rule for .ctors and .dtors.
865 static void sortCtorsDtors(OutputSection *Cmd) {
866 if (Cmd)
867 Cmd->sortCtorsDtors();
870 // Sort input sections using the list provided by --symbol-ordering-file.
871 template <class ELFT> static void sortBySymbolsOrder() {
872 if (Config->SymbolOrderingFile.empty())
873 return;
875 // Sort sections by priority.
876 DenseMap<SectionBase *, int> SectionOrder = buildSectionOrder<ELFT>();
877 for (BaseCommand *Base : Script->Opt.Commands)
878 if (auto *Sec = dyn_cast<OutputSection>(Base))
879 Sec->sort([&](InputSectionBase *S) { return SectionOrder.lookup(S); });
882 template <class ELFT>
883 void Writer<ELFT>::forEachRelSec(std::function<void(InputSectionBase &)> Fn) {
884 for (InputSectionBase *IS : InputSections) {
885 if (!IS->Live)
886 continue;
887 // Scan all relocations. Each relocation goes through a series
888 // of tests to determine if it needs special treatment, such as
889 // creating GOT, PLT, copy relocations, etc.
890 // Note that relocations for non-alloc sections are directly
891 // processed by InputSection::relocateNonAlloc.
892 if (!(IS->Flags & SHF_ALLOC))
893 continue;
894 if (isa<InputSection>(IS) || isa<EhInputSection>(IS))
895 Fn(*IS);
898 if (!Config->Relocatable) {
899 for (EhInputSection *ES : In<ELFT>::EhFrame->Sections)
900 Fn(*ES);
904 template <class ELFT> void Writer<ELFT>::createSections() {
905 std::vector<BaseCommand *> Old = Script->Opt.Commands;
906 Script->Opt.Commands.clear();
907 for (InputSectionBase *IS : InputSections)
908 if (IS)
909 Factory.addInputSec(IS, getOutputSectionName(IS->Name));
910 Script->Opt.Commands.insert(Script->Opt.Commands.end(), Old.begin(),
911 Old.end());
913 Script->fabricateDefaultCommands();
914 sortBySymbolsOrder<ELFT>();
915 sortInitFini(findSection(".init_array"));
916 sortInitFini(findSection(".fini_array"));
917 sortCtorsDtors(findSection(".ctors"));
918 sortCtorsDtors(findSection(".dtors"));
921 // We want to find how similar two ranks are.
922 // The more branches in getSectionRank that match, the more similar they are.
923 // Since each branch corresponds to a bit flag, we can just use
924 // countLeadingZeros.
925 static int getRankProximityAux(OutputSection *A, OutputSection *B) {
926 return countLeadingZeros(A->SortRank ^ B->SortRank);
929 static int getRankProximity(OutputSection *A, BaseCommand *B) {
930 if (auto *Sec = dyn_cast<OutputSection>(B))
931 if (Sec->Live)
932 return getRankProximityAux(A, Sec);
933 return -1;
936 // When placing orphan sections, we want to place them after symbol assignments
937 // so that an orphan after
938 // begin_foo = .;
939 // foo : { *(foo) }
940 // end_foo = .;
941 // doesn't break the intended meaning of the begin/end symbols.
942 // We don't want to go over sections since findOrphanPos is the
943 // one in charge of deciding the order of the sections.
944 // We don't want to go over changes to '.', since doing so in
945 // rx_sec : { *(rx_sec) }
946 // . = ALIGN(0x1000);
947 // /* The RW PT_LOAD starts here*/
948 // rw_sec : { *(rw_sec) }
949 // would mean that the RW PT_LOAD would become unaligned.
950 static bool shouldSkip(BaseCommand *Cmd) {
951 if (isa<OutputSection>(Cmd))
952 return false;
953 if (auto *Assign = dyn_cast<SymbolAssignment>(Cmd))
954 return Assign->Name != ".";
955 return true;
958 // We want to place orphan sections so that they share as much
959 // characteristics with their neighbors as possible. For example, if
960 // both are rw, or both are tls.
961 template <typename ELFT>
962 static std::vector<BaseCommand *>::iterator
963 findOrphanPos(std::vector<BaseCommand *>::iterator B,
964 std::vector<BaseCommand *>::iterator E) {
965 OutputSection *Sec = cast<OutputSection>(*E);
967 // Find the first element that has as close a rank as possible.
968 auto I = std::max_element(B, E, [=](BaseCommand *A, BaseCommand *B) {
969 return getRankProximity(Sec, A) < getRankProximity(Sec, B);
971 if (I == E)
972 return E;
974 // Consider all existing sections with the same proximity.
975 int Proximity = getRankProximity(Sec, *I);
976 for (; I != E; ++I) {
977 auto *CurSec = dyn_cast<OutputSection>(*I);
978 if (!CurSec || !CurSec->Live)
979 continue;
980 if (getRankProximity(Sec, CurSec) != Proximity ||
981 Sec->SortRank < CurSec->SortRank)
982 break;
984 auto J = std::find_if(
985 llvm::make_reverse_iterator(I), llvm::make_reverse_iterator(B),
986 [](BaseCommand *Cmd) { return isa<OutputSection>(Cmd); });
987 I = J.base();
988 while (I != E && shouldSkip(*I))
989 ++I;
990 return I;
993 template <class ELFT> void Writer<ELFT>::sortSections() {
994 if (Script->Opt.HasSections)
995 Script->adjustSectionsBeforeSorting();
997 // Don't sort if using -r. It is not necessary and we want to preserve the
998 // relative order for SHF_LINK_ORDER sections.
999 if (Config->Relocatable)
1000 return;
1002 for (BaseCommand *Base : Script->Opt.Commands)
1003 if (auto *Sec = dyn_cast<OutputSection>(Base))
1004 Sec->SortRank = getSectionRank(Sec);
1006 if (!Script->Opt.HasSections) {
1007 // We know that all the OutputSections are contiguous in
1008 // this case.
1009 auto E = Script->Opt.Commands.end();
1010 auto I = Script->Opt.Commands.begin();
1011 auto IsSection = [](BaseCommand *Base) { return isa<OutputSection>(Base); };
1012 I = std::find_if(I, E, IsSection);
1013 E = std::find_if(llvm::make_reverse_iterator(E),
1014 llvm::make_reverse_iterator(I), IsSection)
1015 .base();
1016 std::stable_sort(I, E, compareSections);
1017 return;
1020 // Orphan sections are sections present in the input files which are
1021 // not explicitly placed into the output file by the linker script.
1023 // The sections in the linker script are already in the correct
1024 // order. We have to figuere out where to insert the orphan
1025 // sections.
1027 // The order of the sections in the script is arbitrary and may not agree with
1028 // compareSections. This means that we cannot easily define a strict weak
1029 // ordering. To see why, consider a comparison of a section in the script and
1030 // one not in the script. We have a two simple options:
1031 // * Make them equivalent (a is not less than b, and b is not less than a).
1032 // The problem is then that equivalence has to be transitive and we can
1033 // have sections a, b and c with only b in a script and a less than c
1034 // which breaks this property.
1035 // * Use compareSectionsNonScript. Given that the script order doesn't have
1036 // to match, we can end up with sections a, b, c, d where b and c are in the
1037 // script and c is compareSectionsNonScript less than b. In which case d
1038 // can be equivalent to c, a to b and d < a. As a concrete example:
1039 // .a (rx) # not in script
1040 // .b (rx) # in script
1041 // .c (ro) # in script
1042 // .d (ro) # not in script
1044 // The way we define an order then is:
1045 // * Sort only the orphan sections. They are in the end right now.
1046 // * Move each orphan section to its preferred position. We try
1047 // to put each section in the last position where it it can share
1048 // a PT_LOAD.
1050 // There is some ambiguity as to where exactly a new entry should be
1051 // inserted, because Opt.Commands contains not only output section
1052 // commands but also other types of commands such as symbol assignment
1053 // expressions. There's no correct answer here due to the lack of the
1054 // formal specification of the linker script. We use heuristics to
1055 // determine whether a new output command should be added before or
1056 // after another commands. For the details, look at shouldSkip
1057 // function.
1059 auto I = Script->Opt.Commands.begin();
1060 auto E = Script->Opt.Commands.end();
1061 auto NonScriptI = std::find_if(I, E, [](BaseCommand *Base) {
1062 if (auto *Sec = dyn_cast<OutputSection>(Base))
1063 return Sec->Live && Sec->SectionIndex == INT_MAX;
1064 return false;
1067 // Sort the orphan sections.
1068 std::stable_sort(NonScriptI, E, compareSections);
1070 // As a horrible special case, skip the first . assignment if it is before any
1071 // section. We do this because it is common to set a load address by starting
1072 // the script with ". = 0xabcd" and the expectation is that every section is
1073 // after that.
1074 auto FirstSectionOrDotAssignment =
1075 std::find_if(I, E, [](BaseCommand *Cmd) { return !shouldSkip(Cmd); });
1076 if (FirstSectionOrDotAssignment != E &&
1077 isa<SymbolAssignment>(**FirstSectionOrDotAssignment))
1078 ++FirstSectionOrDotAssignment;
1079 I = FirstSectionOrDotAssignment;
1081 while (NonScriptI != E) {
1082 auto Pos = findOrphanPos<ELFT>(I, NonScriptI);
1083 OutputSection *Orphan = cast<OutputSection>(*NonScriptI);
1085 // As an optimization, find all sections with the same sort rank
1086 // and insert them with one rotate.
1087 unsigned Rank = Orphan->SortRank;
1088 auto End = std::find_if(NonScriptI + 1, E, [=](BaseCommand *Cmd) {
1089 return cast<OutputSection>(Cmd)->SortRank != Rank;
1091 std::rotate(Pos, NonScriptI, End);
1092 NonScriptI = End;
1095 Script->adjustSectionsAfterSorting();
1098 static void applySynthetic(const std::vector<SyntheticSection *> &Sections,
1099 std::function<void(SyntheticSection *)> Fn) {
1100 for (SyntheticSection *SS : Sections)
1101 if (SS && SS->getParent() && !SS->empty())
1102 Fn(SS);
1105 // We need to add input synthetic sections early in createSyntheticSections()
1106 // to make them visible from linkescript side. But not all sections are always
1107 // required to be in output. For example we don't need dynamic section content
1108 // sometimes. This function filters out such unused sections from the output.
1109 static void removeUnusedSyntheticSections() {
1110 // All input synthetic sections that can be empty are placed after
1111 // all regular ones. We iterate over them all and exit at first
1112 // non-synthetic.
1113 for (InputSectionBase *S : llvm::reverse(InputSections)) {
1114 SyntheticSection *SS = dyn_cast<SyntheticSection>(S);
1115 if (!SS)
1116 return;
1117 OutputSection *OS = SS->getParent();
1118 if (!SS->empty() || !OS)
1119 continue;
1120 if ((SS == InX::Got || SS == InX::MipsGot) && ElfSym::GlobalOffsetTable)
1121 continue;
1123 std::vector<BaseCommand *>::iterator Empty = OS->Commands.end();
1124 for (auto I = OS->Commands.begin(), E = OS->Commands.end(); I != E; ++I) {
1125 BaseCommand *B = *I;
1126 if (auto *ISD = dyn_cast<InputSectionDescription>(B)) {
1127 auto P = std::find(ISD->Sections.begin(), ISD->Sections.end(), SS);
1128 if (P != ISD->Sections.end())
1129 ISD->Sections.erase(P);
1130 if (ISD->Sections.empty())
1131 Empty = I;
1134 if (Empty != OS->Commands.end())
1135 OS->Commands.erase(Empty);
1137 // If there are no other sections in the output section, remove it from the
1138 // output.
1139 if (OS->Commands.empty()) {
1140 // Also remove script commands matching the output section.
1141 auto &Cmds = Script->Opt.Commands;
1142 auto I = std::remove_if(Cmds.begin(), Cmds.end(), [&](BaseCommand *Cmd2) {
1143 if (auto *Sec = dyn_cast<OutputSection>(Cmd2))
1144 return Sec == OS;
1145 return false;
1147 Cmds.erase(I, Cmds.end());
1152 // Create output section objects and add them to OutputSections.
1153 template <class ELFT> void Writer<ELFT>::finalizeSections() {
1154 Out::DebugInfo = findSection(".debug_info");
1155 Out::PreinitArray = findSection(".preinit_array");
1156 Out::InitArray = findSection(".init_array");
1157 Out::FiniArray = findSection(".fini_array");
1159 // The linker needs to define SECNAME_start, SECNAME_end and SECNAME_stop
1160 // symbols for sections, so that the runtime can get the start and end
1161 // addresses of each section by section name. Add such symbols.
1162 if (!Config->Relocatable) {
1163 addStartEndSymbols();
1164 for (BaseCommand *Base : Script->Opt.Commands)
1165 if (auto *Sec = dyn_cast<OutputSection>(Base))
1166 addStartStopSymbols(Sec);
1169 // Add _DYNAMIC symbol. Unlike GNU gold, our _DYNAMIC symbol has no type.
1170 // It should be okay as no one seems to care about the type.
1171 // Even the author of gold doesn't remember why gold behaves that way.
1172 // https://sourceware.org/ml/binutils/2002-03/msg00360.html
1173 if (InX::DynSymTab)
1174 addRegular<ELFT>("_DYNAMIC", InX::Dynamic, 0);
1176 // Define __rel[a]_iplt_{start,end} symbols if needed.
1177 addRelIpltSymbols();
1179 // This responsible for splitting up .eh_frame section into
1180 // pieces. The relocation scan uses those pieces, so this has to be
1181 // earlier.
1182 applySynthetic({In<ELFT>::EhFrame},
1183 [](SyntheticSection *SS) { SS->finalizeContents(); });
1185 // Scan relocations. This must be done after every symbol is declared so that
1186 // we can correctly decide if a dynamic relocation is needed.
1187 forEachRelSec(scanRelocations<ELFT>);
1189 if (InX::Plt && !InX::Plt->empty())
1190 InX::Plt->addSymbols();
1191 if (InX::Iplt && !InX::Iplt->empty())
1192 InX::Iplt->addSymbols();
1194 // Now that we have defined all possible global symbols including linker-
1195 // synthesized ones. Visit all symbols to give the finishing touches.
1196 for (Symbol *S : Symtab->getSymbols()) {
1197 SymbolBody *Body = S->body();
1199 if (!includeInSymtab(*Body))
1200 continue;
1201 if (InX::SymTab)
1202 InX::SymTab->addSymbol(Body);
1204 if (InX::DynSymTab && S->includeInDynsym()) {
1205 InX::DynSymTab->addSymbol(Body);
1206 if (auto *SS = dyn_cast<SharedSymbol>(Body))
1207 if (cast<SharedFile<ELFT>>(S->File)->isNeeded())
1208 In<ELFT>::VerNeed->addSymbol(SS);
1212 // Do not proceed if there was an undefined symbol.
1213 if (ErrorCount)
1214 return;
1216 addPredefinedSections();
1217 removeUnusedSyntheticSections();
1219 sortSections();
1221 // Now that we have the final list, create a list of all the
1222 // OutputSections for convenience.
1223 for (BaseCommand *Base : Script->Opt.Commands)
1224 if (auto *Sec = dyn_cast<OutputSection>(Base))
1225 OutputSections.push_back(Sec);
1227 // Prefer command line supplied address over other constraints.
1228 for (OutputSection *Sec : OutputSections) {
1229 auto I = Config->SectionStartMap.find(Sec->Name);
1230 if (I != Config->SectionStartMap.end())
1231 Sec->AddrExpr = [=] { return I->second; };
1234 // This is a bit of a hack. A value of 0 means undef, so we set it
1235 // to 1 t make __ehdr_start defined. The section number is not
1236 // particularly relevant.
1237 Out::ElfHeader->SectionIndex = 1;
1239 unsigned I = 1;
1240 for (OutputSection *Sec : OutputSections) {
1241 Sec->SectionIndex = I++;
1242 Sec->ShName = InX::ShStrTab->addString(Sec->Name);
1245 // Binary and relocatable output does not have PHDRS.
1246 // The headers have to be created before finalize as that can influence the
1247 // image base and the dynamic section on mips includes the image base.
1248 if (!Config->Relocatable && !Config->OFormatBinary) {
1249 Phdrs = Script->hasPhdrsCommands() ? Script->createPhdrs() : createPhdrs();
1250 addPtArmExid(Phdrs);
1251 Out::ProgramHeaders->Size = sizeof(Elf_Phdr) * Phdrs.size();
1254 // Dynamic section must be the last one in this list and dynamic
1255 // symbol table section (DynSymTab) must be the first one.
1256 applySynthetic({InX::DynSymTab, InX::Bss, InX::BssRelRo,
1257 InX::GnuHashTab, In<ELFT>::HashTab, InX::SymTab,
1258 InX::ShStrTab, InX::StrTab, In<ELFT>::VerDef,
1259 InX::DynStrTab, InX::GdbIndex, InX::Got,
1260 InX::MipsGot, InX::IgotPlt, InX::GotPlt,
1261 In<ELFT>::RelaDyn, In<ELFT>::RelaIplt, In<ELFT>::RelaPlt,
1262 InX::Plt, InX::Iplt, In<ELFT>::EhFrameHdr,
1263 In<ELFT>::VerSym, In<ELFT>::VerNeed, InX::Dynamic},
1264 [](SyntheticSection *SS) { SS->finalizeContents(); });
1266 // Some architectures use small displacements for jump instructions.
1267 // It is linker's responsibility to create thunks containing long
1268 // jump instructions if jump targets are too far. Create thunks.
1269 if (Target->NeedsThunks) {
1270 // FIXME: only ARM Interworking and Mips LA25 Thunks are implemented,
1271 // these
1272 // do not require address information. To support range extension Thunks
1273 // we need to assign addresses so that we can tell if jump instructions
1274 // are out of range. This will need to turn into a loop that converges
1275 // when no more Thunks are added
1276 ThunkCreator TC;
1277 Script->assignAddresses();
1278 if (TC.createThunks(OutputSections)) {
1279 applySynthetic({InX::MipsGot},
1280 [](SyntheticSection *SS) { SS->updateAllocSize(); });
1281 if (TC.createThunks(OutputSections))
1282 fatal("All non-range thunks should be created in first call");
1286 // Fill other section headers. The dynamic table is finalized
1287 // at the end because some tags like RELSZ depend on result
1288 // of finalizing other sections.
1289 for (OutputSection *Sec : OutputSections)
1290 Sec->finalize<ELFT>();
1292 // createThunks may have added local symbols to the static symbol table
1293 applySynthetic({InX::SymTab, InX::ShStrTab, InX::StrTab},
1294 [](SyntheticSection *SS) { SS->postThunkContents(); });
1297 template <class ELFT> void Writer<ELFT>::addPredefinedSections() {
1298 // ARM ABI requires .ARM.exidx to be terminated by some piece of data.
1299 // We have the terminater synthetic section class. Add that at the end.
1300 OutputSection *Cmd = findSection(".ARM.exidx");
1301 if (!Cmd || !Cmd->Live || Config->Relocatable)
1302 return;
1304 auto *Sentinel = make<ARMExidxSentinelSection>();
1305 Cmd->addSection(Sentinel);
1308 // The linker is expected to define SECNAME_start and SECNAME_end
1309 // symbols for a few sections. This function defines them.
1310 template <class ELFT> void Writer<ELFT>::addStartEndSymbols() {
1311 auto Define = [&](StringRef Start, StringRef End, OutputSection *OS) {
1312 // These symbols resolve to the image base if the section does not exist.
1313 // A special value -1 indicates end of the section.
1314 if (OS) {
1315 addOptionalRegular<ELFT>(Start, OS, 0);
1316 addOptionalRegular<ELFT>(End, OS, -1);
1317 } else {
1318 if (Config->Pic)
1319 OS = Out::ElfHeader;
1320 addOptionalRegular<ELFT>(Start, OS, 0);
1321 addOptionalRegular<ELFT>(End, OS, 0);
1325 Define("__preinit_array_start", "__preinit_array_end", Out::PreinitArray);
1326 Define("__init_array_start", "__init_array_end", Out::InitArray);
1327 Define("__fini_array_start", "__fini_array_end", Out::FiniArray);
1329 if (OutputSection *Sec = findSection(".ARM.exidx"))
1330 Define("__exidx_start", "__exidx_end", Sec);
1333 // If a section name is valid as a C identifier (which is rare because of
1334 // the leading '.'), linkers are expected to define __start_<secname> and
1335 // __stop_<secname> symbols. They are at beginning and end of the section,
1336 // respectively. This is not requested by the ELF standard, but GNU ld and
1337 // gold provide the feature, and used by many programs.
1338 template <class ELFT>
1339 void Writer<ELFT>::addStartStopSymbols(OutputSection *Sec) {
1340 StringRef S = Sec->Name;
1341 if (!isValidCIdentifier(S))
1342 return;
1343 addOptionalRegular<ELFT>(Saver.save("__start_" + S), Sec, 0, STV_DEFAULT);
1344 addOptionalRegular<ELFT>(Saver.save("__stop_" + S), Sec, -1, STV_DEFAULT);
1347 template <class ELFT> OutputSection *Writer<ELFT>::findSection(StringRef Name) {
1348 for (BaseCommand *Base : Script->Opt.Commands)
1349 if (auto *Sec = dyn_cast<OutputSection>(Base))
1350 if (Sec->Name == Name)
1351 return Sec;
1352 return nullptr;
1355 static bool needsPtLoad(OutputSection *Sec) {
1356 if (!(Sec->Flags & SHF_ALLOC))
1357 return false;
1359 // Don't allocate VA space for TLS NOBITS sections. The PT_TLS PHDR is
1360 // responsible for allocating space for them, not the PT_LOAD that
1361 // contains the TLS initialization image.
1362 if (Sec->Flags & SHF_TLS && Sec->Type == SHT_NOBITS)
1363 return false;
1364 return true;
1367 // Linker scripts are responsible for aligning addresses. Unfortunately, most
1368 // linker scripts are designed for creating two PT_LOADs only, one RX and one
1369 // RW. This means that there is no alignment in the RO to RX transition and we
1370 // cannot create a PT_LOAD there.
1371 static uint64_t computeFlags(uint64_t Flags) {
1372 if (Config->Omagic)
1373 return PF_R | PF_W | PF_X;
1374 if (Config->SingleRoRx && !(Flags & PF_W))
1375 return Flags | PF_X;
1376 return Flags;
1379 // Decide which program headers to create and which sections to include in each
1380 // one.
1381 template <class ELFT> std::vector<PhdrEntry *> Writer<ELFT>::createPhdrs() {
1382 std::vector<PhdrEntry *> Ret;
1383 auto AddHdr = [&](unsigned Type, unsigned Flags) -> PhdrEntry * {
1384 Ret.push_back(make<PhdrEntry>(Type, Flags));
1385 return Ret.back();
1388 // The first phdr entry is PT_PHDR which describes the program header itself.
1389 AddHdr(PT_PHDR, PF_R)->add(Out::ProgramHeaders);
1391 // PT_INTERP must be the second entry if exists.
1392 if (OutputSection *Cmd = findSection(".interp"))
1393 AddHdr(PT_INTERP, Cmd->getPhdrFlags())->add(Cmd);
1395 // Add the first PT_LOAD segment for regular output sections.
1396 uint64_t Flags = computeFlags(PF_R);
1397 PhdrEntry *Load = AddHdr(PT_LOAD, Flags);
1399 // Add the headers. We will remove them if they don't fit.
1400 Load->add(Out::ElfHeader);
1401 Load->add(Out::ProgramHeaders);
1403 for (OutputSection *Sec : OutputSections) {
1404 if (!(Sec->Flags & SHF_ALLOC))
1405 break;
1406 if (!needsPtLoad(Sec))
1407 continue;
1409 // Segments are contiguous memory regions that has the same attributes
1410 // (e.g. executable or writable). There is one phdr for each segment.
1411 // Therefore, we need to create a new phdr when the next section has
1412 // different flags or is loaded at a discontiguous address using AT linker
1413 // script command.
1414 uint64_t NewFlags = computeFlags(Sec->getPhdrFlags());
1415 if (Sec->LMAExpr || Flags != NewFlags) {
1416 Load = AddHdr(PT_LOAD, NewFlags);
1417 Flags = NewFlags;
1420 Load->add(Sec);
1423 // Add a TLS segment if any.
1424 PhdrEntry *TlsHdr = make<PhdrEntry>(PT_TLS, PF_R);
1425 for (OutputSection *Sec : OutputSections)
1426 if (Sec->Flags & SHF_TLS)
1427 TlsHdr->add(Sec);
1428 if (TlsHdr->First)
1429 Ret.push_back(TlsHdr);
1431 // Add an entry for .dynamic.
1432 if (InX::DynSymTab)
1433 AddHdr(PT_DYNAMIC, InX::Dynamic->getParent()->getPhdrFlags())
1434 ->add(InX::Dynamic->getParent());
1436 // PT_GNU_RELRO includes all sections that should be marked as
1437 // read-only by dynamic linker after proccessing relocations.
1438 PhdrEntry *RelRo = make<PhdrEntry>(PT_GNU_RELRO, PF_R);
1439 for (OutputSection *Sec : OutputSections)
1440 if (needsPtLoad(Sec) && isRelroSection(Sec))
1441 RelRo->add(Sec);
1442 if (RelRo->First)
1443 Ret.push_back(RelRo);
1445 // PT_GNU_EH_FRAME is a special section pointing on .eh_frame_hdr.
1446 if (!In<ELFT>::EhFrame->empty() && In<ELFT>::EhFrameHdr &&
1447 In<ELFT>::EhFrame->getParent() && In<ELFT>::EhFrameHdr->getParent())
1448 AddHdr(PT_GNU_EH_FRAME, In<ELFT>::EhFrameHdr->getParent()->getPhdrFlags())
1449 ->add(In<ELFT>::EhFrameHdr->getParent());
1451 // PT_OPENBSD_RANDOMIZE is an OpenBSD-specific feature. That makes
1452 // the dynamic linker fill the segment with random data.
1453 if (OutputSection *Cmd = findSection(".openbsd.randomdata"))
1454 AddHdr(PT_OPENBSD_RANDOMIZE, Cmd->getPhdrFlags())->add(Cmd);
1456 // PT_GNU_STACK is a special section to tell the loader to make the
1457 // pages for the stack non-executable. If you really want an executable
1458 // stack, you can pass -z execstack, but that's not recommended for
1459 // security reasons.
1460 unsigned Perm;
1461 if (Config->ZExecstack)
1462 Perm = PF_R | PF_W | PF_X;
1463 else
1464 Perm = PF_R | PF_W;
1465 AddHdr(PT_GNU_STACK, Perm)->p_memsz = Config->ZStackSize;
1467 // PT_OPENBSD_WXNEEDED is a OpenBSD-specific header to mark the executable
1468 // is expected to perform W^X violations, such as calling mprotect(2) or
1469 // mmap(2) with PROT_WRITE | PROT_EXEC, which is prohibited by default on
1470 // OpenBSD.
1471 if (Config->ZWxneeded)
1472 AddHdr(PT_OPENBSD_WXNEEDED, PF_X);
1474 // Create one PT_NOTE per a group of contiguous .note sections.
1475 PhdrEntry *Note = nullptr;
1476 for (OutputSection *Sec : OutputSections) {
1477 if (Sec->Type == SHT_NOTE) {
1478 if (!Note || Sec->LMAExpr)
1479 Note = AddHdr(PT_NOTE, PF_R);
1480 Note->add(Sec);
1481 } else {
1482 Note = nullptr;
1485 return Ret;
1488 template <class ELFT>
1489 void Writer<ELFT>::addPtArmExid(std::vector<PhdrEntry *> &Phdrs) {
1490 if (Config->EMachine != EM_ARM)
1491 return;
1492 auto I = llvm::find_if(OutputSections, [](OutputSection *Cmd) {
1493 return Cmd->Type == SHT_ARM_EXIDX;
1495 if (I == OutputSections.end())
1496 return;
1498 // PT_ARM_EXIDX is the ARM EHABI equivalent of PT_GNU_EH_FRAME
1499 PhdrEntry *ARMExidx = make<PhdrEntry>(PT_ARM_EXIDX, PF_R);
1500 ARMExidx->add(*I);
1501 Phdrs.push_back(ARMExidx);
1504 // The first section of each PT_LOAD, the first section in PT_GNU_RELRO and the
1505 // first section after PT_GNU_RELRO have to be page aligned so that the dynamic
1506 // linker can set the permissions.
1507 template <class ELFT> void Writer<ELFT>::fixSectionAlignments() {
1508 auto PageAlign = [](OutputSection *Cmd) {
1509 if (Cmd && !Cmd->AddrExpr)
1510 Cmd->AddrExpr = [=] {
1511 return alignTo(Script->getDot(), Config->MaxPageSize);
1515 for (const PhdrEntry *P : Phdrs)
1516 if (P->p_type == PT_LOAD && P->First)
1517 PageAlign(P->First);
1519 for (const PhdrEntry *P : Phdrs) {
1520 if (P->p_type != PT_GNU_RELRO)
1521 continue;
1522 if (P->First)
1523 PageAlign(P->First);
1524 // Find the first section after PT_GNU_RELRO. If it is in a PT_LOAD we
1525 // have to align it to a page.
1526 auto End = OutputSections.end();
1527 auto I = std::find(OutputSections.begin(), End, P->Last);
1528 if (I == End || (I + 1) == End)
1529 continue;
1530 OutputSection *Cmd = (*(I + 1));
1531 if (needsPtLoad(Cmd))
1532 PageAlign(Cmd);
1536 // Adjusts the file alignment for a given output section and returns
1537 // its new file offset. The file offset must be the same with its
1538 // virtual address (modulo the page size) so that the loader can load
1539 // executables without any address adjustment.
1540 static uint64_t getFileAlignment(uint64_t Off, OutputSection *Cmd) {
1541 OutputSection *First = Cmd->FirstInPtLoad;
1542 // If the section is not in a PT_LOAD, we just have to align it.
1543 if (!First)
1544 return alignTo(Off, Cmd->Alignment);
1546 // The first section in a PT_LOAD has to have congruent offset and address
1547 // module the page size.
1548 if (Cmd == First)
1549 return alignTo(Off, std::max<uint64_t>(Cmd->Alignment, Config->MaxPageSize),
1550 Cmd->Addr);
1552 // If two sections share the same PT_LOAD the file offset is calculated
1553 // using this formula: Off2 = Off1 + (VA2 - VA1).
1554 return First->Offset + Cmd->Addr - First->Addr;
1557 static uint64_t setOffset(OutputSection *Cmd, uint64_t Off) {
1558 if (Cmd->Type == SHT_NOBITS) {
1559 Cmd->Offset = Off;
1560 return Off;
1563 Off = getFileAlignment(Off, Cmd);
1564 Cmd->Offset = Off;
1565 return Off + Cmd->Size;
1568 template <class ELFT> void Writer<ELFT>::assignFileOffsetsBinary() {
1569 uint64_t Off = 0;
1570 for (OutputSection *Sec : OutputSections)
1571 if (Sec->Flags & SHF_ALLOC)
1572 Off = setOffset(Sec, Off);
1573 FileSize = alignTo(Off, Config->Wordsize);
1576 // Assign file offsets to output sections.
1577 template <class ELFT> void Writer<ELFT>::assignFileOffsets() {
1578 uint64_t Off = 0;
1579 Off = setOffset(Out::ElfHeader, Off);
1580 Off = setOffset(Out::ProgramHeaders, Off);
1582 PhdrEntry *LastRX = nullptr;
1583 for (PhdrEntry *P : Phdrs)
1584 if (P->p_type == PT_LOAD && (P->p_flags & PF_X))
1585 LastRX = P;
1587 for (OutputSection *Sec : OutputSections) {
1588 Off = setOffset(Sec, Off);
1589 if (Script->Opt.HasSections)
1590 continue;
1591 // If this is a last section of the last executable segment and that
1592 // segment is the last loadable segment, align the offset of the
1593 // following section to avoid loading non-segments parts of the file.
1594 if (LastRX && LastRX->Last == Sec)
1595 Off = alignTo(Off, Target->PageSize);
1598 SectionHeaderOff = alignTo(Off, Config->Wordsize);
1599 FileSize = SectionHeaderOff + (OutputSections.size() + 1) * sizeof(Elf_Shdr);
1602 // Finalize the program headers. We call this function after we assign
1603 // file offsets and VAs to all sections.
1604 template <class ELFT> void Writer<ELFT>::setPhdrs() {
1605 for (PhdrEntry *P : Phdrs) {
1606 OutputSection *First = P->First;
1607 OutputSection *Last = P->Last;
1608 if (First) {
1609 P->p_filesz = Last->Offset - First->Offset;
1610 if (Last->Type != SHT_NOBITS)
1611 P->p_filesz += Last->Size;
1612 P->p_memsz = Last->Addr + Last->Size - First->Addr;
1613 P->p_offset = First->Offset;
1614 P->p_vaddr = First->Addr;
1615 if (!P->HasLMA)
1616 P->p_paddr = First->getLMA();
1618 if (P->p_type == PT_LOAD)
1619 P->p_align = std::max<uint64_t>(P->p_align, Config->MaxPageSize);
1620 else if (P->p_type == PT_GNU_RELRO) {
1621 P->p_align = 1;
1622 // The glibc dynamic loader rounds the size down, so we need to round up
1623 // to protect the last page. This is a no-op on FreeBSD which always
1624 // rounds up.
1625 P->p_memsz = alignTo(P->p_memsz, Target->PageSize);
1628 // The TLS pointer goes after PT_TLS. At least glibc will align it,
1629 // so round up the size to make sure the offsets are correct.
1630 if (P->p_type == PT_TLS) {
1631 Out::TlsPhdr = P;
1632 if (P->p_memsz)
1633 P->p_memsz = alignTo(P->p_memsz, P->p_align);
1638 // The entry point address is chosen in the following ways.
1640 // 1. the '-e' entry command-line option;
1641 // 2. the ENTRY(symbol) command in a linker control script;
1642 // 3. the value of the symbol start, if present;
1643 // 4. the address of the first byte of the .text section, if present;
1644 // 5. the address 0.
1645 template <class ELFT> uint64_t Writer<ELFT>::getEntryAddr() {
1646 // Case 1, 2 or 3. As a special case, if the symbol is actually
1647 // a number, we'll use that number as an address.
1648 if (SymbolBody *B = Symtab->find(Config->Entry))
1649 return B->getVA();
1650 uint64_t Addr;
1651 if (to_integer(Config->Entry, Addr))
1652 return Addr;
1654 // Case 4
1655 if (OutputSection *Sec = findSection(".text")) {
1656 if (Config->WarnMissingEntry)
1657 warn("cannot find entry symbol " + Config->Entry + "; defaulting to 0x" +
1658 utohexstr(Sec->Addr));
1659 return Sec->Addr;
1662 // Case 5
1663 if (Config->WarnMissingEntry)
1664 warn("cannot find entry symbol " + Config->Entry +
1665 "; not setting start address");
1666 return 0;
1669 static uint16_t getELFType() {
1670 if (Config->Pic)
1671 return ET_DYN;
1672 if (Config->Relocatable)
1673 return ET_REL;
1674 return ET_EXEC;
1677 // This function is called after we have assigned address and size
1678 // to each section. This function fixes some predefined
1679 // symbol values that depend on section address and size.
1680 template <class ELFT> void Writer<ELFT>::fixPredefinedSymbols() {
1681 // _etext is the first location after the last read-only loadable segment.
1682 // _edata is the first location after the last read-write loadable segment.
1683 // _end is the first location after the uninitialized data region.
1684 PhdrEntry *Last = nullptr;
1685 PhdrEntry *LastRO = nullptr;
1686 PhdrEntry *LastRW = nullptr;
1687 for (PhdrEntry *P : Phdrs) {
1688 if (P->p_type != PT_LOAD)
1689 continue;
1690 Last = P;
1691 if (P->p_flags & PF_W)
1692 LastRW = P;
1693 else
1694 LastRO = P;
1697 auto Set = [](DefinedRegular *S, OutputSection *Cmd, uint64_t Value) {
1698 if (S) {
1699 S->Section = Cmd;
1700 S->Value = Value;
1704 if (Last) {
1705 Set(ElfSym::End1, Last->First, Last->p_memsz);
1706 Set(ElfSym::End2, Last->First, Last->p_memsz);
1708 if (LastRO) {
1709 Set(ElfSym::Etext1, LastRO->First, LastRO->p_filesz);
1710 Set(ElfSym::Etext2, LastRO->First, LastRO->p_filesz);
1712 if (LastRW) {
1713 Set(ElfSym::Edata1, LastRW->First, LastRW->p_filesz);
1714 Set(ElfSym::Edata2, LastRW->First, LastRW->p_filesz);
1717 if (ElfSym::Bss)
1718 ElfSym::Bss->Section = findSection(".bss");
1720 // Setup MIPS _gp_disp/__gnu_local_gp symbols which should
1721 // be equal to the _gp symbol's value.
1722 if (Config->EMachine == EM_MIPS && !ElfSym::MipsGp->Value) {
1723 // Find GP-relative section with the lowest address
1724 // and use this address to calculate default _gp value.
1725 for (const OutputSection *Cmd : OutputSections) {
1726 const OutputSection *OS = Cmd;
1727 if (OS->Flags & SHF_MIPS_GPREL) {
1728 ElfSym::MipsGp->Value = OS->Addr + 0x7ff0;
1729 break;
1735 template <class ELFT> void Writer<ELFT>::writeHeader() {
1736 uint8_t *Buf = Buffer->getBufferStart();
1737 memcpy(Buf, "\177ELF", 4);
1739 // Write the ELF header.
1740 auto *EHdr = reinterpret_cast<Elf_Ehdr *>(Buf);
1741 EHdr->e_ident[EI_CLASS] = Config->Is64 ? ELFCLASS64 : ELFCLASS32;
1742 EHdr->e_ident[EI_DATA] = Config->IsLE ? ELFDATA2LSB : ELFDATA2MSB;
1743 EHdr->e_ident[EI_VERSION] = EV_CURRENT;
1744 EHdr->e_ident[EI_OSABI] = Config->OSABI;
1745 EHdr->e_type = getELFType();
1746 EHdr->e_machine = Config->EMachine;
1747 EHdr->e_version = EV_CURRENT;
1748 EHdr->e_entry = getEntryAddr();
1749 EHdr->e_shoff = SectionHeaderOff;
1750 EHdr->e_ehsize = sizeof(Elf_Ehdr);
1751 EHdr->e_phnum = Phdrs.size();
1752 EHdr->e_shentsize = sizeof(Elf_Shdr);
1753 EHdr->e_shnum = OutputSections.size() + 1;
1754 EHdr->e_shstrndx = InX::ShStrTab->getParent()->SectionIndex;
1756 if (Config->EMachine == EM_ARM)
1757 // We don't currently use any features incompatible with EF_ARM_EABI_VER5,
1758 // but we don't have any firm guarantees of conformance. Linux AArch64
1759 // kernels (as of 2016) require an EABI version to be set.
1760 EHdr->e_flags = EF_ARM_EABI_VER5;
1761 else if (Config->EMachine == EM_MIPS)
1762 EHdr->e_flags = getMipsEFlags<ELFT>();
1764 if (!Config->Relocatable) {
1765 EHdr->e_phoff = sizeof(Elf_Ehdr);
1766 EHdr->e_phentsize = sizeof(Elf_Phdr);
1769 // Write the program header table.
1770 auto *HBuf = reinterpret_cast<Elf_Phdr *>(Buf + EHdr->e_phoff);
1771 for (PhdrEntry *P : Phdrs) {
1772 HBuf->p_type = P->p_type;
1773 HBuf->p_flags = P->p_flags;
1774 HBuf->p_offset = P->p_offset;
1775 HBuf->p_vaddr = P->p_vaddr;
1776 HBuf->p_paddr = P->p_paddr;
1777 HBuf->p_filesz = P->p_filesz;
1778 HBuf->p_memsz = P->p_memsz;
1779 HBuf->p_align = P->p_align;
1780 ++HBuf;
1783 // Write the section header table. Note that the first table entry is null.
1784 auto *SHdrs = reinterpret_cast<Elf_Shdr *>(Buf + EHdr->e_shoff);
1785 for (OutputSection *Sec : OutputSections)
1786 Sec->writeHeaderTo<ELFT>(++SHdrs);
1789 // Open a result file.
1790 template <class ELFT> void Writer<ELFT>::openFile() {
1791 if (!Config->Is64 && FileSize > UINT32_MAX) {
1792 error("output file too large: " + Twine(FileSize) + " bytes");
1793 return;
1796 unlinkAsync(Config->OutputFile);
1797 ErrorOr<std::unique_ptr<FileOutputBuffer>> BufferOrErr =
1798 FileOutputBuffer::create(Config->OutputFile, FileSize,
1799 FileOutputBuffer::F_executable);
1801 if (auto EC = BufferOrErr.getError())
1802 error("failed to open " + Config->OutputFile + ": " + EC.message());
1803 else
1804 Buffer = std::move(*BufferOrErr);
1807 template <class ELFT> void Writer<ELFT>::writeSectionsBinary() {
1808 uint8_t *Buf = Buffer->getBufferStart();
1809 for (OutputSection *Sec : OutputSections)
1810 if (Sec->Flags & SHF_ALLOC)
1811 Sec->writeTo<ELFT>(Buf + Sec->Offset);
1814 static void fillTrapInstr(uint8_t *I, uint8_t *End) {
1815 for (; I + 4 < End; I += 4)
1816 memcpy(I, &Target->TrapInstr, 4);
1820 // Fill the first and the last page of executable segments with trap
1821 // instructions instead of leaving them as zero. Even though it is not required
1822 // by any standard , it is in general a good thing to do for security reasons.
1823 template <class ELFT> void Writer<ELFT>::writeTrapInstr() {
1824 if (Script->Opt.HasSections)
1825 return;
1827 uint8_t *Buf = Buffer->getBufferStart();
1829 for (PhdrEntry *P : Phdrs) {
1830 if (P->p_type != PT_LOAD || !(P->p_flags & PF_X))
1831 continue;
1833 // We only fill the first and the last page of the segment because the
1834 // middle part will be overwritten by output sections.
1835 fillTrapInstr(Buf + alignDown(P->p_offset, Target->PageSize),
1836 Buf + alignTo(P->p_offset, Target->PageSize));
1837 fillTrapInstr(Buf + alignDown(P->p_offset + P->p_filesz, Target->PageSize),
1838 Buf + alignTo(P->p_offset + P->p_filesz, Target->PageSize));
1841 PhdrEntry *LastRX = nullptr;
1842 for (PhdrEntry *P : Phdrs) {
1843 if (P->p_type != PT_LOAD)
1844 continue;
1845 if (P->p_flags & PF_X)
1846 LastRX = P;
1847 else
1848 LastRX = nullptr;
1851 // Round up the file size of the last segment to the page boundary iff it is
1852 // an executable segment to ensure that other other tools don't accidentally
1853 // trim the instruction padding (e.g. when stripping the file).
1854 if (LastRX)
1855 LastRX->p_filesz = alignTo(LastRX->p_filesz, Target->PageSize);
1858 // Write section contents to a mmap'ed file.
1859 template <class ELFT> void Writer<ELFT>::writeSections() {
1860 uint8_t *Buf = Buffer->getBufferStart();
1862 // PPC64 needs to process relocations in the .opd section
1863 // before processing relocations in code-containing sections.
1864 if (auto *OpdCmd = findSection(".opd")) {
1865 Out::Opd = OpdCmd;
1866 Out::OpdBuf = Buf + Out::Opd->Offset;
1867 OpdCmd->template writeTo<ELFT>(Buf + Out::Opd->Offset);
1870 OutputSection *EhFrameHdr =
1871 (In<ELFT>::EhFrameHdr && !In<ELFT>::EhFrameHdr->empty())
1872 ? In<ELFT>::EhFrameHdr->getParent()
1873 : nullptr;
1875 // In -r or -emit-relocs mode, write the relocation sections first as in
1876 // ELf_Rel targets we might find out that we need to modify the relocated
1877 // section while doing it.
1878 for (OutputSection *Sec : OutputSections)
1879 if (Sec->Type == SHT_REL || Sec->Type == SHT_RELA)
1880 Sec->writeTo<ELFT>(Buf + Sec->Offset);
1882 for (OutputSection *Sec : OutputSections)
1883 if (Sec != Out::Opd && Sec != EhFrameHdr && Sec->Type != SHT_REL &&
1884 Sec->Type != SHT_RELA)
1885 Sec->writeTo<ELFT>(Buf + Sec->Offset);
1887 // The .eh_frame_hdr depends on .eh_frame section contents, therefore
1888 // it should be written after .eh_frame is written.
1889 if (EhFrameHdr)
1890 EhFrameHdr->writeTo<ELFT>(Buf + EhFrameHdr->Offset);
1893 template <class ELFT> void Writer<ELFT>::writeBuildId() {
1894 if (!InX::BuildId || !InX::BuildId->getParent())
1895 return;
1897 // Compute a hash of all sections of the output file.
1898 uint8_t *Start = Buffer->getBufferStart();
1899 uint8_t *End = Start + FileSize;
1900 InX::BuildId->writeBuildId({Start, End});
1903 template void elf::writeResult<ELF32LE>();
1904 template void elf::writeResult<ELF32BE>();
1905 template void elf::writeResult<ELF64LE>();
1906 template void elf::writeResult<ELF64BE>();