184487f11SMichael J. Spencer //===- Writer.cpp ---------------------------------------------------------===//
284487f11SMichael J. Spencer //
32946cd70SChandler Carruth // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
42946cd70SChandler Carruth // See https://llvm.org/LICENSE.txt for license information.
52946cd70SChandler Carruth // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
684487f11SMichael J. Spencer //
784487f11SMichael J. Spencer //===----------------------------------------------------------------------===//
884487f11SMichael J. Spencer
9f8325410SMichael J. Spencer #include "Writer.h"
10732cd8cbSPeter Smith #include "AArch64ErrataFix.h"
11ea99ce5eSPeter Smith #include "ARMErrataFix.h"
12b842725cSMichael J. Spencer #include "CallGraphSort.h"
13cb8474edSRui Ueyama #include "Config.h"
14b01430a0SFangrui Song #include "InputFiles.h"
15717677afSRui Ueyama #include "LinkerScript.h"
161ebfc59cSRafael Espindola #include "MapFile.h"
175805c4f5SRafael Espindola #include "OutputSections.h"
180fcdc730SRui Ueyama #include "Relocations.h"
19afff74e2SRui Ueyama #include "SymbolTable.h"
20d26b52fdSRafael Espindola #include "Symbols.h"
216dc7fcbeSRui Ueyama #include "SyntheticSections.h"
2201205f79SRafael Espindola #include "Target.h"
239b6dde8aSJez Ng #include "lld/Common/Arrays.h"
2483d59e05SAlexandre Ganea #include "lld/Common/CommonLinkerContext.h"
257fd99fc4SRui Ueyama #include "lld/Common/Filesystem.h"
26ee173718SRui Ueyama #include "lld/Common/Strings.h"
278e3b38abSDenis Protivensky #include "llvm/ADT/StringMap.h"
28d3e5b6f7SFangrui Song #include "llvm/Support/BLAKE3.h"
29932f0276SReid Kleckner #include "llvm/Support/Parallel.h"
3097d25e06SPeter Collingbourne #include "llvm/Support/RandomNumberGenerator.h"
31e7cb3744SRussell Gallop #include "llvm/Support/TimeProfiler.h"
3297d25e06SPeter Collingbourne #include "llvm/Support/xxhash.h"
33e8b2df47SRui Ueyama #include <climits>
3484487f11SMichael J. Spencer
3594317878SSriraman Tallam #define DEBUG_TYPE "lld"
3694317878SSriraman Tallam
3784487f11SMichael J. Spencer using namespace llvm;
3884487f11SMichael J. Spencer using namespace llvm::ELF;
3984487f11SMichael J. Spencer using namespace llvm::object;
400a259f3bSPeter Smith using namespace llvm::support;
410a259f3bSPeter Smith using namespace llvm::support::endian;
4207837b8fSFangrui Song using namespace lld;
4307837b8fSFangrui Song using namespace lld::elf;
4484487f11SMichael J. Spencer
45afff74e2SRui Ueyama namespace {
46afff74e2SRui Ueyama // The writer writes a SymbolTable result to a file.
47afff74e2SRui Ueyama template <class ELFT> class Writer {
48afff74e2SRui Ueyama public:
49ed146d62SGeorgii Rymar LLVM_ELF_IMPORT_TYPES_ELFT(ELFT)
50ed146d62SGeorgii Rymar
Writer()51136d27abSRui Ueyama Writer() : buffer(errorHandler().outputBuffer) {}
520ce388beSRui Ueyama
53afff74e2SRui Ueyama void run();
54afff74e2SRui Ueyama
55afff74e2SRui Ueyama private:
565a9640beSRui Ueyama void copyLocalSymbols();
5708d6a3f1SRafael Espindola void addSectionSymbols();
5824c073d1SRafael Espindola void sortSections();
5903cbf468SPeter Smith void resolveShfLinkOrder();
60da49faf1SGeorge Rimar void finalizeAddressDependentContent();
6194317878SSriraman Tallam void optimizeBasicBlockJumps();
62a6b1fbecSGeorge Rimar void sortInputSections();
63e63d81bdSEugene Leviant void finalizeSections();
646d26ed92SRui Ueyama void checkExecuteOnly();
654d560160SRafael Espindola void setReservedSymbolSections();
662df0fd86SRui Ueyama
67a1c2ee01SFangrui Song SmallVector<PhdrEntry *, 0> createPhdrs(Partition &part);
683837f427SRui Ueyama void addPhdrForSection(Partition &part, unsigned shType, unsigned pType,
693837f427SRui Ueyama unsigned pFlags);
70e044e9cfSRui Ueyama void assignFileOffsets();
7186ce267aSGeorge Rimar void assignFileOffsetsBinary();
723837f427SRui Ueyama void setPhdrs(Partition &part);
7394e148c8SGeorge Rimar void checkSections();
7447091903SRui Ueyama void fixSectionAlignments();
75f7f52ef6SRui Ueyama void openFile();
76edd6c358SPetr Hosek void writeTrapInstr();
77afff74e2SRui Ueyama void writeHeader();
78afff74e2SRui Ueyama void writeSections();
7986ce267aSGeorge Rimar void writeSectionsBinary();
80634ddf0bSRui Ueyama void writeBuildId();
81afff74e2SRui Ueyama
823837f427SRui Ueyama std::unique_ptr<FileOutputBuffer> &buffer;
832f008242SMichael J. Spencer
840168722eSRui Ueyama void addRelIpltSymbols();
85a5d79d16SRui Ueyama void addStartEndSymbols();
869e9c86fdSFangrui Song void addStartStopSymbols(OutputSection &osec);
872f1b79fcSRui Ueyama
883837f427SRui Ueyama uint64_t fileSize;
893837f427SRui Ueyama uint64_t sectionHeaderOff;
90afff74e2SRui Ueyama };
91afff74e2SRui Ueyama } // anonymous namespace
92afff74e2SRui Ueyama
needsInterpSection()93696a7f9aSGeorge Rimar static bool needsInterpSection() {
942d7a8cf9SFangrui Song return !config->relocatable && !config->shared &&
952d7a8cf9SFangrui Song !config->dynamicLinker.empty() && script->needsInterpSection();
96fd03cfd2SRui Ueyama }
97fd03cfd2SRui Ueyama
writeResult()9807837b8fSFangrui Song template <class ELFT> void elf::writeResult() {
99e7cb3744SRussell Gallop Writer<ELFT>().run();
100e7cb3744SRussell Gallop }
1014fc6044aSRafael Espindola
removeEmptyPTLoad(SmallVector<PhdrEntry *,0> & phdrs)102a1c2ee01SFangrui Song static void removeEmptyPTLoad(SmallVector<PhdrEntry *, 0> &phdrs) {
103c49f83b6SFangrui Song auto it = std::stable_partition(
104c49f83b6SFangrui Song phdrs.begin(), phdrs.end(), [&](const PhdrEntry *p) {
1053837f427SRui Ueyama if (p->p_type != PT_LOAD)
10641217616SRafael Espindola return true;
107c49f83b6SFangrui Song if (!p->firstSec)
108c49f83b6SFangrui Song return false;
1093837f427SRui Ueyama uint64_t size = p->lastSec->addr + p->lastSec->size - p->firstSec->addr;
110c49f83b6SFangrui Song return size != 0;
111074ba93cSRafael Espindola });
112c49f83b6SFangrui Song
113c49f83b6SFangrui Song // Clear OutputSection::ptLoad for sections contained in removed
114c49f83b6SFangrui Song // segments.
115c49f83b6SFangrui Song DenseSet<PhdrEntry *> removed(it, phdrs.end());
116c49f83b6SFangrui Song for (OutputSection *sec : outputSections)
117c49f83b6SFangrui Song if (removed.count(sec->ptLoad))
118c49f83b6SFangrui Song sec->ptLoad = nullptr;
119c49f83b6SFangrui Song phdrs.erase(it, phdrs.end());
120074ba93cSRafael Espindola }
121074ba93cSRafael Espindola
copySectionsIntoPartitions()12207837b8fSFangrui Song void elf::copySectionsIntoPartitions() {
123ac0986f8SFangrui Song SmallVector<InputSectionBase *, 0> newSections;
1243837f427SRui Ueyama for (unsigned part = 2; part != partitions.size() + 1; ++part) {
1253837f427SRui Ueyama for (InputSectionBase *s : inputSections) {
1263837f427SRui Ueyama if (!(s->flags & SHF_ALLOC) || !s->isLive())
12702828985SPeter Collingbourne continue;
1283837f427SRui Ueyama InputSectionBase *copy;
1293837f427SRui Ueyama if (s->type == SHT_NOTE)
1303837f427SRui Ueyama copy = make<InputSection>(cast<InputSection>(*s));
1313837f427SRui Ueyama else if (auto *es = dyn_cast<EhInputSection>(s))
1323837f427SRui Ueyama copy = make<EhInputSection>(*es);
13302828985SPeter Collingbourne else
13402828985SPeter Collingbourne continue;
1353837f427SRui Ueyama copy->partition = part;
1363837f427SRui Ueyama newSections.push_back(copy);
13702828985SPeter Collingbourne }
13802828985SPeter Collingbourne }
13902828985SPeter Collingbourne
1403837f427SRui Ueyama inputSections.insert(inputSections.end(), newSections.begin(),
1413837f427SRui Ueyama newSections.end());
14202828985SPeter Collingbourne }
14302828985SPeter Collingbourne
combineEhSections()14407837b8fSFangrui Song void elf::combineEhSections() {
145439341b9SJames Henderson llvm::TimeTraceScope timeScope("Combine EH sections");
1463837f427SRui Ueyama for (InputSectionBase *&s : inputSections) {
147eaf3f569SPeter Collingbourne // Ignore dead sections and the partition end marker (.part.end),
148eaf3f569SPeter Collingbourne // whose partition number is out of bounds.
1493837f427SRui Ueyama if (!s->isLive() || s->partition == 255)
1507b79321eSPetr Hosek continue;
1517b79321eSPetr Hosek
1523837f427SRui Ueyama Partition &part = s->getPartition();
1533837f427SRui Ueyama if (auto *es = dyn_cast<EhInputSection>(s)) {
1541681ceb2SFangrui Song part.ehFrame->addSection(es);
1553837f427SRui Ueyama s = nullptr;
1563837f427SRui Ueyama } else if (s->kind() == SectionBase::Regular && part.armExidx &&
1573837f427SRui Ueyama part.armExidx->addSection(cast<InputSection>(s))) {
1583837f427SRui Ueyama s = nullptr;
159a9e84723SPeter Collingbourne }
1607b79321eSPetr Hosek }
1617b79321eSPetr Hosek
16210726992SKazu Hirata llvm::erase_value(inputSections, nullptr);
1637b79321eSPetr Hosek }
1647b79321eSPetr Hosek
addOptionalRegular(StringRef name,SectionBase * sec,uint64_t val,uint8_t stOther=STV_HIDDEN)1653837f427SRui Ueyama static Defined *addOptionalRegular(StringRef name, SectionBase *sec,
166db6a00daSFangrui Song uint64_t val, uint8_t stOther = STV_HIDDEN) {
1673837f427SRui Ueyama Symbol *s = symtab->find(name);
168fc0aa842SFangrui Song if (!s || s->isDefined() || s->isCommon())
1691dd30dddSRafael Espindola return nullptr;
1707d476192SRui Ueyama
171977a1a52SFangrui Song s->resolve(Defined{nullptr, StringRef(), STB_GLOBAL, stOther, STT_NOTYPE, val,
172bfaf64aeSRui Ueyama /*size=*/0, sec});
1738ca46bbaSFangrui Song s->isUsedInRegularObj = true;
1743837f427SRui Ueyama return cast<Defined>(s);
1751dd30dddSRafael Espindola }
1761dd30dddSRafael Espindola
addAbsolute(StringRef name)1773837f427SRui Ueyama static Defined *addAbsolute(StringRef name) {
1783837f427SRui Ueyama Symbol *sym = symtab->addSymbol(Defined{nullptr, name, STB_GLOBAL, STV_HIDDEN,
179bbf154cfSRui Ueyama STT_NOTYPE, 0, 0, nullptr});
1808ca46bbaSFangrui Song sym->isUsedInRegularObj = true;
1813837f427SRui Ueyama return cast<Defined>(sym);
182c3095aceSMartin Storsjo }
18339adc6dfSAlexander Richardson
1841dd30dddSRafael Espindola // The linker is expected to define some symbols depending on
1851dd30dddSRafael Espindola // the linking result. This function defines such symbols.
addReservedSymbols()18607837b8fSFangrui Song void elf::addReservedSymbols() {
1873837f427SRui Ueyama if (config->emachine == EM_MIPS) {
1881dd30dddSRafael Espindola // Define _gp for MIPS. st_value of _gp symbol will be updated by Writer
1891dd30dddSRafael Espindola // so that it points to an absolute address which by default is relative
1901dd30dddSRafael Espindola // to GOT. Default offset is 0x7ff0.
1911dd30dddSRafael Espindola // See "Global Data Symbols" in Chapter 6 in the following document:
1921dd30dddSRafael Espindola // ftp://www.linux-mips.org/pub/linux/mips/doc/ABI/mipsabi.pdf
1933837f427SRui Ueyama ElfSym::mipsGp = addAbsolute("_gp");
1941dd30dddSRafael Espindola
1951dd30dddSRafael Espindola // On MIPS O32 ABI, _gp_disp is a magic symbol designates offset between
1961dd30dddSRafael Espindola // start of function and 'gp' pointer into GOT.
1973837f427SRui Ueyama if (symtab->find("_gp_disp"))
1983837f427SRui Ueyama ElfSym::mipsGpDisp = addAbsolute("_gp_disp");
1991dd30dddSRafael Espindola
2001dd30dddSRafael Espindola // The __gnu_local_gp is a magic symbol equal to the current value of 'gp'
2011dd30dddSRafael Espindola // pointer. This symbol is used in the code generated by .cpload pseudo-op
2021dd30dddSRafael Espindola // in case of using -mno-shared option.
2031dd30dddSRafael Espindola // https://sourceware.org/ml/binutils/2004-12/msg00094.html
2043837f427SRui Ueyama if (symtab->find("__gnu_local_gp"))
2053837f427SRui Ueyama ElfSym::mipsLocalGp = addAbsolute("__gnu_local_gp");
2063837f427SRui Ueyama } else if (config->emachine == EM_PPC) {
20782442adfSFangrui Song // glibc *crt1.o has a undefined reference to _SDA_BASE_. Since we don't
20882442adfSFangrui Song // support Small Data Area, define it arbitrarily as 0.
20982442adfSFangrui Song addOptionalRegular("_SDA_BASE_", nullptr, 0, STV_HIDDEN);
210bae7cf67SFangrui Song } else if (config->emachine == EM_PPC64) {
211bae7cf67SFangrui Song addPPC64SaveRestore();
2121dd30dddSRafael Espindola }
2131dd30dddSRafael Espindola
214121078beSFangrui Song // The Power Architecture 64-bit v2 ABI defines a TableOfContents (TOC) which
215121078beSFangrui Song // combines the typical ELF GOT with the small data sections. It commonly
216121078beSFangrui Song // includes .got .toc .sdata .sbss. The .TOC. symbol replaces both
217121078beSFangrui Song // _GLOBAL_OFFSET_TABLE_ and _SDA_BASE_ from the 32-bit ABI. It is used to
218121078beSFangrui Song // represent the TOC base which is offset by 0x8000 bytes from the start of
219121078beSFangrui Song // the .got section.
220fd8aeb2cSPeter Smith // We do not allow _GLOBAL_OFFSET_TABLE_ to be defined by input objects as the
221fd8aeb2cSPeter Smith // correctness of some relocations depends on its value.
2223837f427SRui Ueyama StringRef gotSymName =
2233837f427SRui Ueyama (config->emachine == EM_PPC64) ? ".TOC." : "_GLOBAL_OFFSET_TABLE_";
224f28825bcSRui Ueyama
2253837f427SRui Ueyama if (Symbol *s = symtab->find(gotSymName)) {
2263837f427SRui Ueyama if (s->isDefined()) {
2273837f427SRui Ueyama error(toString(s->file) + " cannot redefine linker defined symbol '" +
2283837f427SRui Ueyama gotSymName + "'");
229f28825bcSRui Ueyama return;
230f28825bcSRui Ueyama }
231f28825bcSRui Ueyama
2323837f427SRui Ueyama uint64_t gotOff = 0;
2333837f427SRui Ueyama if (config->emachine == EM_PPC64)
2343837f427SRui Ueyama gotOff = 0x8000;
235f28825bcSRui Ueyama
236977a1a52SFangrui Song s->resolve(Defined{/*file=*/nullptr, StringRef(), STB_GLOBAL, STV_HIDDEN,
237bfaf64aeSRui Ueyama STT_NOTYPE, gotOff, /*size=*/0, Out::elfHeader});
2383837f427SRui Ueyama ElfSym::globalOffsetTable = cast<Defined>(s);
239fd8aeb2cSPeter Smith }
2401dd30dddSRafael Espindola
2411dd30dddSRafael Espindola // __ehdr_start is the location of ELF file headers. Note that we define
2421dd30dddSRafael Espindola // this symbol unconditionally even when using a linker script, which
2431dd30dddSRafael Espindola // differs from the behavior implemented by GNU linker which only define
2441dd30dddSRafael Espindola // this symbol if ELF headers are in the memory mapped segment.
2453837f427SRui Ueyama addOptionalRegular("__ehdr_start", Out::elfHeader, 0, STV_HIDDEN);
2462ce74015SRui Ueyama
2471dd30dddSRafael Espindola // __executable_start is not documented, but the expectation of at
2482ce74015SRui Ueyama // least the Android libc is that it points to the ELF header.
2493837f427SRui Ueyama addOptionalRegular("__executable_start", Out::elfHeader, 0, STV_HIDDEN);
2502ce74015SRui Ueyama
2511dd30dddSRafael Espindola // __dso_handle symbol is passed to cxa_finalize as a marker to identify
2521dd30dddSRafael Espindola // each DSO. The address of the symbol doesn't matter as long as they are
2531dd30dddSRafael Espindola // different in different DSOs, so we chose the start address of the DSO.
2543837f427SRui Ueyama addOptionalRegular("__dso_handle", Out::elfHeader, 0, STV_HIDDEN);
2551dd30dddSRafael Espindola
2565976a3f5SNico Weber // If linker script do layout we do not need to create any standard symbols.
2573837f427SRui Ueyama if (script->hasSectionsCommand)
2581dd30dddSRafael Espindola return;
2591dd30dddSRafael Espindola
2603837f427SRui Ueyama auto add = [](StringRef s, int64_t pos) {
2613837f427SRui Ueyama return addOptionalRegular(s, Out::elfHeader, pos, STV_DEFAULT);
2621dd30dddSRafael Espindola };
2631dd30dddSRafael Espindola
2643837f427SRui Ueyama ElfSym::bss = add("__bss_start", 0);
2653837f427SRui Ueyama ElfSym::end1 = add("end", -1);
2663837f427SRui Ueyama ElfSym::end2 = add("_end", -1);
2673837f427SRui Ueyama ElfSym::etext1 = add("etext", -1);
2683837f427SRui Ueyama ElfSym::etext2 = add("_etext", -1);
2693837f427SRui Ueyama ElfSym::edata1 = add("edata", -1);
2703837f427SRui Ueyama ElfSym::edata2 = add("_edata", -1);
2711dd30dddSRafael Espindola }
2721dd30dddSRafael Espindola
findSection(StringRef name,unsigned partition=1)2733837f427SRui Ueyama static OutputSection *findSection(StringRef name, unsigned partition = 1) {
2747051aeefSFangrui Song for (SectionCommand *cmd : script->sectionCommands)
2756c814931SFangrui Song if (auto *osd = dyn_cast<OutputDesc>(cmd))
2766c814931SFangrui Song if (osd->osec.name == name && osd->osec.partition == partition)
2776c814931SFangrui Song return &osd->osec;
278471001a7SRafael Espindola return nullptr;
279471001a7SRafael Espindola }
280471001a7SRafael Espindola
createSyntheticSections()28107837b8fSFangrui Song template <class ELFT> void elf::createSyntheticSections() {
282f83aca42SRui Ueyama // Initialize all pointers with NULL. This is needed because
283f83aca42SRui Ueyama // you can call lld::elf::main more than once as a library.
284d060cc1fSFangrui Song Out::tlsPhdr = nullptr;
285d060cc1fSFangrui Song Out::preinitArray = nullptr;
286d060cc1fSFangrui Song Out::initArray = nullptr;
287d060cc1fSFangrui Song Out::finiArray = nullptr;
288cfadbd94SRui Ueyama
2890bb825d2SPeter Collingbourne // Add the .interp section first because it is not a SyntheticSection.
2900bb825d2SPeter Collingbourne // The removeUnusedSyntheticSections() function relies on the
2910bb825d2SPeter Collingbourne // SyntheticSections coming last.
2920bb825d2SPeter Collingbourne if (needsInterpSection()) {
2930bb825d2SPeter Collingbourne for (size_t i = 1; i <= partitions.size(); ++i) {
2940bb825d2SPeter Collingbourne InputSection *sec = createInterpSection();
2950bb825d2SPeter Collingbourne sec->partition = i;
2960bb825d2SPeter Collingbourne inputSections.push_back(sec);
2970bb825d2SPeter Collingbourne }
2980bb825d2SPeter Collingbourne }
2990bb825d2SPeter Collingbourne
300baa3eb0dSFangrui Song auto add = [](SyntheticSection &sec) { inputSections.push_back(&sec); };
3017ddd7a8bSRui Ueyama
302cb203f3fSFangrui Song in.shStrTab = std::make_unique<StringTableSection>(".shstrtab", false);
3034197a6abSRui Ueyama
3043837f427SRui Ueyama Out::programHeaders = make<OutputSection>("", 0, SHF_ALLOC);
3053837f427SRui Ueyama Out::programHeaders->alignment = config->wordsize;
3064197a6abSRui Ueyama
3073837f427SRui Ueyama if (config->strip != StripPolicy::All) {
308cb203f3fSFangrui Song in.strTab = std::make_unique<StringTableSection>(".strtab", false);
309cb203f3fSFangrui Song in.symTab = std::make_unique<SymbolTableSection<ELFT>>(*in.strTab);
310cb203f3fSFangrui Song in.symTabShndx = std::make_unique<SymtabShndxSection>();
3114197a6abSRui Ueyama }
312cfadbd94SRui Ueyama
313cb203f3fSFangrui Song in.bss = std::make_unique<BssSection>(".bss", 0, 1);
314baa3eb0dSFangrui Song add(*in.bss);
31580e4be7eSPeter Smith
31680e4be7eSPeter Smith // If there is a SECTIONS command and a .data.rel.ro section name use name
31780e4be7eSPeter Smith // .data.rel.ro.bss so that we match in the .data.rel.ro output section.
31880e4be7eSPeter Smith // This makes sure our relro is contiguous.
3197cd0c453SFangrui Song bool hasDataRelRo = script->hasSectionsCommand && findSection(".data.rel.ro");
320cb203f3fSFangrui Song in.bssRelRo = std::make_unique<BssSection>(
321cb203f3fSFangrui Song hasDataRelRo ? ".data.rel.ro.bss" : ".bss.rel.ro", 0, 1);
322baa3eb0dSFangrui Song add(*in.bssRelRo);
3231ab9cf49SGeorge Rimar
3241d75de00SRui Ueyama // Add MIPS-specific sections.
3253837f427SRui Ueyama if (config->emachine == EM_MIPS) {
3263837f427SRui Ueyama if (!config->shared && config->hasDynSymTab) {
327cb203f3fSFangrui Song in.mipsRldMap = std::make_unique<MipsRldMapSection>();
328baa3eb0dSFangrui Song add(*in.mipsRldMap);
32917b7a575SEugene Leviant }
330988a03c5SFangrui Song if ((in.mipsAbiFlags = MipsAbiFlagsSection<ELFT>::create()))
331988a03c5SFangrui Song add(*in.mipsAbiFlags);
332988a03c5SFangrui Song if ((in.mipsOptions = MipsOptionsSection<ELFT>::create()))
333988a03c5SFangrui Song add(*in.mipsOptions);
334988a03c5SFangrui Song if ((in.mipsReginfo = MipsReginfoSection<ELFT>::create()))
335988a03c5SFangrui Song add(*in.mipsReginfo);
336ce02cf00SSimon Atanasyan }
33741ca327bSEugene Leviant
33825ab1c64SFangrui Song StringRef relaDynName = config->isRela ? ".rela.dyn" : ".rel.dyn";
33925ab1c64SFangrui Song
3403837f427SRui Ueyama for (Partition &part : partitions) {
341baa3eb0dSFangrui Song auto add = [&](SyntheticSection &sec) {
342baa3eb0dSFangrui Song sec.partition = part.getNumber();
343baa3eb0dSFangrui Song inputSections.push_back(&sec);
34402828985SPeter Collingbourne };
34511992c86SGeorge Rimar
3463837f427SRui Ueyama if (!part.name.empty()) {
347cb203f3fSFangrui Song part.elfHeader = std::make_unique<PartitionElfHeaderSection<ELFT>>();
3483837f427SRui Ueyama part.elfHeader->name = part.name;
349baa3eb0dSFangrui Song add(*part.elfHeader);
35011992c86SGeorge Rimar
351cb203f3fSFangrui Song part.programHeaders =
352cb203f3fSFangrui Song std::make_unique<PartitionProgramHeadersSection<ELFT>>();
353baa3eb0dSFangrui Song add(*part.programHeaders);
35411992c86SGeorge Rimar }
35511992c86SGeorge Rimar
3563837f427SRui Ueyama if (config->buildId != BuildIdKind::None) {
357cb203f3fSFangrui Song part.buildId = std::make_unique<BuildIdSection>();
358baa3eb0dSFangrui Song add(*part.buildId);
35902828985SPeter Collingbourne }
36002828985SPeter Collingbourne
361cb203f3fSFangrui Song part.dynStrTab = std::make_unique<StringTableSection>(".dynstr", true);
362cb203f3fSFangrui Song part.dynSymTab =
363cb203f3fSFangrui Song std::make_unique<SymbolTableSection<ELFT>>(*part.dynStrTab);
364cb203f3fSFangrui Song part.dynamic = std::make_unique<DynamicSection<ELFT>>();
365786c89feSMitch Phillips
366786c89feSMitch Phillips if (config->emachine == EM_AARCH64 &&
367786c89feSMitch Phillips config->androidMemtagMode != ELF::NT_MEMTAG_LEVEL_NONE) {
368786c89feSMitch Phillips part.memtagAndroidNote = std::make_unique<MemtagAndroidNote>();
369786c89feSMitch Phillips add(*part.memtagAndroidNote);
370786c89feSMitch Phillips }
371786c89feSMitch Phillips
37225ab1c64SFangrui Song if (config->androidPackDynRelocs)
373ad26b0b2SFangrui Song part.relaDyn =
374cb203f3fSFangrui Song std::make_unique<AndroidPackedRelocationSection<ELFT>>(relaDynName);
375cb203f3fSFangrui Song else
376cb203f3fSFangrui Song part.relaDyn = std::make_unique<RelocationSection<ELFT>>(
377cb203f3fSFangrui Song relaDynName, config->zCombreloc);
37802828985SPeter Collingbourne
3793837f427SRui Ueyama if (config->hasDynSymTab) {
380baa3eb0dSFangrui Song add(*part.dynSymTab);
38102828985SPeter Collingbourne
382cb203f3fSFangrui Song part.verSym = std::make_unique<VersionTableSection>();
383baa3eb0dSFangrui Song add(*part.verSym);
38402828985SPeter Collingbourne
385e28a70daSFangrui Song if (!namedVersionDefs().empty()) {
386cb203f3fSFangrui Song part.verDef = std::make_unique<VersionDefinitionSection>();
387baa3eb0dSFangrui Song add(*part.verDef);
38802828985SPeter Collingbourne }
38902828985SPeter Collingbourne
390cb203f3fSFangrui Song part.verNeed = std::make_unique<VersionNeedSection<ELFT>>();
391baa3eb0dSFangrui Song add(*part.verNeed);
39211992c86SGeorge Rimar
3933837f427SRui Ueyama if (config->gnuHash) {
394a5249c2dSFangrui Song part.gnuHashTab = std::make_unique<GnuHashTableSection>();
395baa3eb0dSFangrui Song add(*part.gnuHashTab);
39611992c86SGeorge Rimar }
39711992c86SGeorge Rimar
3983837f427SRui Ueyama if (config->sysvHash) {
399a5249c2dSFangrui Song part.hashTab = std::make_unique<HashTableSection>();
400baa3eb0dSFangrui Song add(*part.hashTab);
40111992c86SGeorge Rimar }
40211992c86SGeorge Rimar
403baa3eb0dSFangrui Song add(*part.dynamic);
404baa3eb0dSFangrui Song add(*part.dynStrTab);
405baa3eb0dSFangrui Song add(*part.relaDyn);
40611992c86SGeorge Rimar }
40711992c86SGeorge Rimar
4083837f427SRui Ueyama if (config->relrPackDynRelocs) {
409cb203f3fSFangrui Song part.relrDyn = std::make_unique<RelrSection<ELFT>>();
410baa3eb0dSFangrui Song add(*part.relrDyn);
41102828985SPeter Collingbourne }
41202828985SPeter Collingbourne
4133837f427SRui Ueyama if (!config->relocatable) {
4143837f427SRui Ueyama if (config->ehFrameHdr) {
415cb203f3fSFangrui Song part.ehFrameHdr = std::make_unique<EhFrameHeader>();
416baa3eb0dSFangrui Song add(*part.ehFrameHdr);
41702828985SPeter Collingbourne }
418cb203f3fSFangrui Song part.ehFrame = std::make_unique<EhFrameSection>();
419baa3eb0dSFangrui Song add(*part.ehFrame);
42002828985SPeter Collingbourne }
42102828985SPeter Collingbourne
4223837f427SRui Ueyama if (config->emachine == EM_ARM && !config->relocatable) {
42302828985SPeter Collingbourne // The ARMExidxsyntheticsection replaces all the individual .ARM.exidx
42402828985SPeter Collingbourne // InputSections.
425cb203f3fSFangrui Song part.armExidx = std::make_unique<ARMExidxSyntheticSection>();
426baa3eb0dSFangrui Song add(*part.armExidx);
42702828985SPeter Collingbourne }
428*525ffb05SAlex Brachet
429*525ffb05SAlex Brachet if (!config->packageMetadata.empty()) {
430*525ffb05SAlex Brachet part.packageMetadataNote = std::make_unique<PackageMetadataNote>();
431*525ffb05SAlex Brachet add(*part.packageMetadataNote);
432*525ffb05SAlex Brachet }
43302828985SPeter Collingbourne }
43402828985SPeter Collingbourne
4353837f427SRui Ueyama if (partitions.size() != 1) {
43602828985SPeter Collingbourne // Create the partition end marker. This needs to be in partition number 255
43702828985SPeter Collingbourne // so that it is sorted after all other partitions. It also has other
438eaf3f569SPeter Collingbourne // special handling (see createPhdrs() and combineEhSections()).
439cb203f3fSFangrui Song in.partEnd =
440cb203f3fSFangrui Song std::make_unique<BssSection>(".part.end", config->maxPageSize, 1);
4413837f427SRui Ueyama in.partEnd->partition = 255;
442baa3eb0dSFangrui Song add(*in.partEnd);
44302828985SPeter Collingbourne
444cb203f3fSFangrui Song in.partIndex = std::make_unique<PartitionIndexSection>();
445cb203f3fSFangrui Song addOptionalRegular("__part_index_begin", in.partIndex.get(), 0);
446cb203f3fSFangrui Song addOptionalRegular("__part_index_end", in.partIndex.get(),
4473837f427SRui Ueyama in.partIndex->getSize());
448baa3eb0dSFangrui Song add(*in.partIndex);
44911479dafSRui Ueyama }
45011479dafSRui Ueyama
4511d75de00SRui Ueyama // Add .got. MIPS' .got is so different from the other archs,
4521d75de00SRui Ueyama // it has its own class.
4533837f427SRui Ueyama if (config->emachine == EM_MIPS) {
454cb203f3fSFangrui Song in.mipsGot = std::make_unique<MipsGotSection>();
455baa3eb0dSFangrui Song add(*in.mipsGot);
45611992c86SGeorge Rimar } else {
457cb203f3fSFangrui Song in.got = std::make_unique<GotSection>();
458baa3eb0dSFangrui Song add(*in.got);
45911992c86SGeorge Rimar }
460725dc14bSSimon Atanasyan
4613837f427SRui Ueyama if (config->emachine == EM_PPC) {
462cb203f3fSFangrui Song in.ppc32Got2 = std::make_unique<PPC32Got2Section>();
463baa3eb0dSFangrui Song add(*in.ppc32Got2);
46482442adfSFangrui Song }
46582442adfSFangrui Song
4663837f427SRui Ueyama if (config->emachine == EM_PPC64) {
467cb203f3fSFangrui Song in.ppc64LongBranchTarget = std::make_unique<PPC64LongBranchTargetSection>();
468baa3eb0dSFangrui Song add(*in.ppc64LongBranchTarget);
469614dc11cSSean Fertile }
470614dc11cSSean Fertile
471cb203f3fSFangrui Song in.gotPlt = std::make_unique<GotPltSection>();
472baa3eb0dSFangrui Song add(*in.gotPlt);
473cb203f3fSFangrui Song in.igotPlt = std::make_unique<IgotPltSection>();
474baa3eb0dSFangrui Song add(*in.igotPlt);
4753fb5a6dcSGeorge Rimar
476210949a2SFangrui Song // _GLOBAL_OFFSET_TABLE_ is defined relative to either .got.plt or .got. Treat
477210949a2SFangrui Song // it as a relocation and ensure the referenced section is created.
4783837f427SRui Ueyama if (ElfSym::globalOffsetTable && config->emachine != EM_MIPS) {
4793837f427SRui Ueyama if (target->gotBaseSymInGotPlt)
4803837f427SRui Ueyama in.gotPlt->hasGotPltOffRel = true;
481210949a2SFangrui Song else
4823837f427SRui Ueyama in.got->hasGotOffRel = true;
483210949a2SFangrui Song }
484210949a2SFangrui Song
4853837f427SRui Ueyama if (config->gdbIndex)
486baa3eb0dSFangrui Song add(*GdbIndexSection::create<ELFT>());
4873fb5a6dcSGeorge Rimar
4883fb5a6dcSGeorge Rimar // We always need to add rel[a].plt to output if it has entries.
4893fb5a6dcSGeorge Rimar // Even for static linking it can contain R_[*]_IRELATIVE relocations.
490cb203f3fSFangrui Song in.relaPlt = std::make_unique<RelocationSection<ELFT>>(
491bfaf64aeSRui Ueyama config->isRela ? ".rela.plt" : ".rel.plt", /*sort=*/false);
492baa3eb0dSFangrui Song add(*in.relaPlt);
4933fb5a6dcSGeorge Rimar
49425ab1c64SFangrui Song // The relaIplt immediately follows .rel[a].dyn to ensure that the IRelative
49525ab1c64SFangrui Song // relocations are processed last by the dynamic loader. We cannot place the
49625ab1c64SFangrui Song // iplt section in .rel.dyn when Android relocation packing is enabled because
49725ab1c64SFangrui Song // that would cause a section type mismatch. However, because the Android
49825ab1c64SFangrui Song // dynamic loader reads .rel.plt after .rel.dyn, we can get the desired
49925ab1c64SFangrui Song // behaviour by placing the iplt section in .rel.plt.
500cb203f3fSFangrui Song in.relaIplt = std::make_unique<RelocationSection<ELFT>>(
50125ab1c64SFangrui Song config->androidPackDynRelocs ? in.relaPlt->name : relaDynName,
502bfaf64aeSRui Ueyama /*sort=*/false);
503baa3eb0dSFangrui Song add(*in.relaIplt);
504baffdb8bSPeter Smith
5057cd429f2SFangrui Song if ((config->emachine == EM_386 || config->emachine == EM_X86_64) &&
5067cd429f2SFangrui Song (config->andFeatures & GNU_PROPERTY_X86_FEATURE_1_IBT)) {
507cb203f3fSFangrui Song in.ibtPlt = std::make_unique<IBTPltSection>();
508baa3eb0dSFangrui Song add(*in.ibtPlt);
5097cd429f2SFangrui Song }
5107cd429f2SFangrui Song
511cb203f3fSFangrui Song if (config->emachine == EM_PPC)
512cb203f3fSFangrui Song in.plt = std::make_unique<PPC32GlinkSection>();
513cb203f3fSFangrui Song else
514cb203f3fSFangrui Song in.plt = std::make_unique<PltSection>();
515baa3eb0dSFangrui Song add(*in.plt);
516cb203f3fSFangrui Song in.iplt = std::make_unique<IpltSection>();
517baa3eb0dSFangrui Song add(*in.iplt);
5183fb5a6dcSGeorge Rimar
5193837f427SRui Ueyama if (config->andFeatures)
520baa3eb0dSFangrui Song add(*make<GnuPropertySection>());
5212057f836SRui Ueyama
522025bb56aSRui Ueyama // .note.GNU-stack is always added when we are creating a re-linkable
523025bb56aSRui Ueyama // object file. Other linkers are using the presence of this marker
524025bb56aSRui Ueyama // section to control the executable-ness of the stack area, but that
525025bb56aSRui Ueyama // is irrelevant these days. Stack area should always be non-executable
526025bb56aSRui Ueyama // by default. So we emit this section unconditionally.
5273837f427SRui Ueyama if (config->relocatable)
528baa3eb0dSFangrui Song add(*make<GnuStackSection>());
529025bb56aSRui Ueyama
5303837f427SRui Ueyama if (in.symTab)
531baa3eb0dSFangrui Song add(*in.symTab);
5323837f427SRui Ueyama if (in.symTabShndx)
533baa3eb0dSFangrui Song add(*in.symTabShndx);
534baa3eb0dSFangrui Song add(*in.shStrTab);
5353837f427SRui Ueyama if (in.strTab)
536baa3eb0dSFangrui Song add(*in.strTab);
53784487f11SMichael J. Spencer }
53884487f11SMichael J. Spencer
5395af073e3SRafael Espindola // The main function of the writer.
run()5405af073e3SRafael Espindola template <class ELFT> void Writer<ELFT>::run() {
5415af073e3SRafael Espindola copyLocalSymbols();
5425af073e3SRafael Espindola
5433837f427SRui Ueyama if (config->copyRelocs)
5445af073e3SRafael Espindola addSectionSymbols();
5455af073e3SRafael Espindola
5465af073e3SRafael Espindola // Now that we have a complete set of output sections. This function
5475af073e3SRafael Espindola // completes section contents. For example, we need to add strings
5485af073e3SRafael Espindola // to the string table, and add entries to .got and .plt.
5495af073e3SRafael Espindola // finalizeSections does that.
5505af073e3SRafael Espindola finalizeSections();
5516d26ed92SRui Ueyama checkExecuteOnly();
5525af073e3SRafael Espindola
553bf6e259bSFangrui Song // If --compressed-debug-sections is specified, compress .debug_* sections.
554bf6e259bSFangrui Song // Do it right now because it changes the size of output sections.
5553837f427SRui Ueyama for (OutputSection *sec : outputSections)
5563837f427SRui Ueyama sec->maybeCompress<ELFT>();
5575af073e3SRafael Espindola
55806bb7dfbSFangrui Song if (script->hasSectionsCommand)
5593837f427SRui Ueyama script->allocateHeaders(mainPart->phdrs);
5605af073e3SRafael Espindola
5615af073e3SRafael Espindola // Remove empty PT_LOAD to avoid causing the dynamic linker to try to mmap a
5625af073e3SRafael Espindola // 0 sized region. This has to be done late since only after assignAddresses
5635af073e3SRafael Espindola // we know the size of the sections.
5643837f427SRui Ueyama for (Partition &part : partitions)
5653837f427SRui Ueyama removeEmptyPTLoad(part.phdrs);
5665af073e3SRafael Espindola
5673837f427SRui Ueyama if (!config->oFormatBinary)
5685af073e3SRafael Espindola assignFileOffsets();
5695af073e3SRafael Espindola else
5705af073e3SRafael Espindola assignFileOffsetsBinary();
5715af073e3SRafael Espindola
5723837f427SRui Ueyama for (Partition &part : partitions)
5733837f427SRui Ueyama setPhdrs(part);
5745af073e3SRafael Espindola
5757fd3849bSFangrui Song // Handle --print-map(-M)/--Map and --cref. Dump them before checkSections()
5767fd3849bSFangrui Song // because the files may be useful in case checkSections() or openFile()
5777fd3849bSFangrui Song // fails, for example, due to an erroneous file size.
5781ce51a5fSFangrui Song writeMapAndCref();
579eb4b5a36SFangrui Song
5803837f427SRui Ueyama if (config->checkSections)
58194e148c8SGeorge Rimar checkSections();
5826b367faaSAlexander Richardson
5835af073e3SRafael Espindola // It does not make sense try to open the file if we have error already.
5845af073e3SRafael Espindola if (errorCount())
5855af073e3SRafael Espindola return;
586439341b9SJames Henderson
587439341b9SJames Henderson {
588439341b9SJames Henderson llvm::TimeTraceScope timeScope("Write output file");
5895af073e3SRafael Espindola // Write the result down to a file.
5905af073e3SRafael Espindola openFile();
5915af073e3SRafael Espindola if (errorCount())
5925af073e3SRafael Espindola return;
5935af073e3SRafael Espindola
5943837f427SRui Ueyama if (!config->oFormatBinary) {
59502649506SFangrui Song if (config->zSeparate != SeparateSegmentKind::None)
5965af073e3SRafael Espindola writeTrapInstr();
5975af073e3SRafael Espindola writeHeader();
5985af073e3SRafael Espindola writeSections();
5995af073e3SRafael Espindola } else {
6005af073e3SRafael Espindola writeSectionsBinary();
6015af073e3SRafael Espindola }
6025af073e3SRafael Espindola
6035af073e3SRafael Espindola // Backfill .note.gnu.build-id section content. This is done at last
6045af073e3SRafael Espindola // because the content is usually a hash value of the entire output file.
6055af073e3SRafael Espindola writeBuildId();
6065af073e3SRafael Espindola if (errorCount())
6075af073e3SRafael Espindola return;
6085af073e3SRafael Espindola
6093837f427SRui Ueyama if (auto e = buffer->commit())
6103837f427SRui Ueyama error("failed to write to the output file: " + toString(std::move(e)));
6115af073e3SRafael Espindola }
612439341b9SJames Henderson }
6135af073e3SRafael Espindola
61466e4eb9cSIgor Kudrin template <class ELFT, class RelTy>
markUsedLocalSymbolsImpl(ObjFile<ELFT> * file,llvm::ArrayRef<RelTy> rels)61566e4eb9cSIgor Kudrin static void markUsedLocalSymbolsImpl(ObjFile<ELFT> *file,
61666e4eb9cSIgor Kudrin llvm::ArrayRef<RelTy> rels) {
61766e4eb9cSIgor Kudrin for (const RelTy &rel : rels) {
61866e4eb9cSIgor Kudrin Symbol &sym = file->getRelocTargetSym(rel);
61966e4eb9cSIgor Kudrin if (sym.isLocal())
62066e4eb9cSIgor Kudrin sym.used = true;
62166e4eb9cSIgor Kudrin }
62266e4eb9cSIgor Kudrin }
62366e4eb9cSIgor Kudrin
62466e4eb9cSIgor Kudrin // The function ensures that the "used" field of local symbols reflects the fact
62566e4eb9cSIgor Kudrin // that the symbol is used in a relocation from a live section.
markUsedLocalSymbols()62666e4eb9cSIgor Kudrin template <class ELFT> static void markUsedLocalSymbols() {
62766e4eb9cSIgor Kudrin // With --gc-sections, the field is already filled.
62866e4eb9cSIgor Kudrin // See MarkLive<ELFT>::resolveReloc().
62966e4eb9cSIgor Kudrin if (config->gcSections)
63066e4eb9cSIgor Kudrin return;
6319a572164SFangrui Song for (ELFFileBase *file : ctx->objectFiles) {
63266e4eb9cSIgor Kudrin ObjFile<ELFT> *f = cast<ObjFile<ELFT>>(file);
63366e4eb9cSIgor Kudrin for (InputSectionBase *s : f->getSections()) {
63466e4eb9cSIgor Kudrin InputSection *isec = dyn_cast_or_null<InputSection>(s);
63566e4eb9cSIgor Kudrin if (!isec)
63666e4eb9cSIgor Kudrin continue;
63766e4eb9cSIgor Kudrin if (isec->type == SHT_REL)
63866e4eb9cSIgor Kudrin markUsedLocalSymbolsImpl(f, isec->getDataAs<typename ELFT::Rel>());
63966e4eb9cSIgor Kudrin else if (isec->type == SHT_RELA)
64066e4eb9cSIgor Kudrin markUsedLocalSymbolsImpl(f, isec->getDataAs<typename ELFT::Rela>());
64166e4eb9cSIgor Kudrin }
64266e4eb9cSIgor Kudrin }
64366e4eb9cSIgor Kudrin }
64466e4eb9cSIgor Kudrin
shouldKeepInSymtab(const Defined & sym)6453837f427SRui Ueyama static bool shouldKeepInSymtab(const Defined &sym) {
6463837f427SRui Ueyama if (sym.isSection())
64710d71ffcSRafael Espindola return false;
64810d71ffcSRafael Espindola
64966e4eb9cSIgor Kudrin // If --emit-reloc or -r is given, preserve symbols referenced by relocations
65066e4eb9cSIgor Kudrin // from live sections.
651be01af4aSFangrui Song if (sym.used && config->copyRelocs)
65210d71ffcSRafael Espindola return true;
65310d71ffcSRafael Espindola
6549f65f5acSIgor Kudrin // Exclude local symbols pointing to .ARM.exidx sections.
6559f65f5acSIgor Kudrin // They are probably mapping symbols "$d", which are optional for these
6569f65f5acSIgor Kudrin // sections. After merging the .ARM.exidx sections, some of these symbols
6579f65f5acSIgor Kudrin // may become dangling. The easiest way to avoid the issue is not to add
6589f65f5acSIgor Kudrin // them to the symbol table from the beginning.
6599f65f5acSIgor Kudrin if (config->emachine == EM_ARM && sym.section &&
6609f65f5acSIgor Kudrin sym.section->type == SHT_ARM_EXIDX)
6619f65f5acSIgor Kudrin return false;
6629f65f5acSIgor Kudrin
663497c76e9SFangrui Song if (config->discard == DiscardPolicy::None)
6644af8d47dSRui Ueyama return true;
665497c76e9SFangrui Song if (config->discard == DiscardPolicy::All)
666497c76e9SFangrui Song return false;
6674af8d47dSRui Ueyama
66810d71ffcSRafael Espindola // In ELF assembly .L symbols are normally discarded by the assembler.
66910d71ffcSRafael Espindola // If the assembler fails to do so, the linker discards them if
67010d71ffcSRafael Espindola // * --discard-locals is used.
67110d71ffcSRafael Espindola // * The symbol is in a SHF_MERGE section, which is normally the reason for
67210d71ffcSRafael Espindola // the assembler keeping the .L symbol.
673c977564fSFangrui Song if (sym.getName().startswith(".L") &&
6744ae1c2c6SFangrui Song (config->discard == DiscardPolicy::Locals ||
6754ae1c2c6SFangrui Song (sym.section && (sym.section->flags & SHF_MERGE))))
67610d71ffcSRafael Espindola return false;
6774ae1c2c6SFangrui Song return true;
67810d71ffcSRafael Espindola }
67910d71ffcSRafael Espindola
includeInSymtab(const Symbol & b)6803837f427SRui Ueyama static bool includeInSymtab(const Symbol &b) {
6813837f427SRui Ueyama if (auto *d = dyn_cast<Defined>(&b)) {
682474eb019SRafael Espindola // Always include absolute symbols.
6833837f427SRui Ueyama SectionBase *sec = d->section;
6843837f427SRui Ueyama if (!sec)
685474eb019SRafael Espindola return true;
6864664280aSRui Ueyama
6873837f427SRui Ueyama if (auto *s = dyn_cast<MergeInputSection>(sec))
6888565a87fSFangrui Song return s->getSectionPiece(d->value).live;
68938e6361dSFangrui Song return sec->isLive();
69040007586SIgor Kudrin }
691bb4eacdbSFangrui Song return b.used || !config->gcSections;
6921d4b3023SRafael Espindola }
693462220deSRafael Espindola
6945a9640beSRui Ueyama // Local symbols are not in the linker's symbol table. This function scans
6955a9640beSRui Ueyama // each object file's symbol table to copy local symbols to the output.
copyLocalSymbols()6965a9640beSRui Ueyama template <class ELFT> void Writer<ELFT>::copyLocalSymbols() {
6973837f427SRui Ueyama if (!in.symTab)
69890f76fbbSRui Ueyama return;
699439341b9SJames Henderson llvm::TimeTraceScope timeScope("Add local symbols");
70066e4eb9cSIgor Kudrin if (config->copyRelocs && config->discard != DiscardPolicy::None)
70166e4eb9cSIgor Kudrin markUsedLocalSymbols<ELFT>();
7029a572164SFangrui Song for (ELFFileBase *file : ctx->objectFiles) {
7037a54ae9cSFangrui Song for (Symbol *b : file->getLocalSymbols()) {
704c4d13f72SFangrui Song assert(b->isLocal() && "should have been caught in initializeSymbols()");
7053837f427SRui Ueyama auto *dr = dyn_cast<Defined>(b);
7063fc0f7e5SRui Ueyama
707ccfe3cb3SRafael Espindola // No reason to keep local undefined symbol in symtab.
7083837f427SRui Ueyama if (!dr)
709444576d4SRafael Espindola continue;
7105d0be553SFangrui Song if (includeInSymtab(*b) && shouldKeepInSymtab(*dr))
7113837f427SRui Ueyama in.symTab->addSymbol(b);
7125a9640beSRui Ueyama }
7135a9640beSRui Ueyama }
7145a9640beSRui Ueyama }
7155a9640beSRui Ueyama
716661a2c19SGeorge Rimar // Create a section symbol for each output section so that we can represent
717661a2c19SGeorge Rimar // relocations that point to the section. If we know that no relocation is
718661a2c19SGeorge Rimar // referring to a section (that happens if the section is a synthetic one), we
719661a2c19SGeorge Rimar // don't create a section symbol for that section.
addSectionSymbols()7204664280aSRui Ueyama template <class ELFT> void Writer<ELFT>::addSectionSymbols() {
7217051aeefSFangrui Song for (SectionCommand *cmd : script->sectionCommands) {
7226c814931SFangrui Song auto *osd = dyn_cast<OutputDesc>(cmd);
7236c814931SFangrui Song if (!osd)
72473d29ab3SRui Ueyama continue;
725940bd4c7SFangrui Song OutputSection &osec = osd->osec;
7267370a489SFangrui Song InputSectionBase *isec = nullptr;
7277370a489SFangrui Song // Iterate over all input sections and add a STT_SECTION symbol if any input
7287370a489SFangrui Song // section may be a relocation target.
7297370a489SFangrui Song for (SectionCommand *cmd : osec.commands) {
7307370a489SFangrui Song auto *isd = dyn_cast<InputSectionDescription>(cmd);
7317370a489SFangrui Song if (!isd)
732d48b2088SRafael Espindola continue;
7337370a489SFangrui Song for (InputSectionBase *s : isd->sections) {
734661a2c19SGeorge Rimar // Relocations are not using REL[A] section symbols.
7357370a489SFangrui Song if (s->type == SHT_REL || s->type == SHT_RELA)
736661a2c19SGeorge Rimar continue;
737661a2c19SGeorge Rimar
7387370a489SFangrui Song // Unlike other synthetic sections, mergeable output sections contain
7397370a489SFangrui Song // data copied from input sections, and there may be a relocation
7407370a489SFangrui Song // pointing to its contents if -r or --emit-reloc is given.
7417370a489SFangrui Song if (isa<SyntheticSection>(s) && !(s->flags & SHF_MERGE))
7427370a489SFangrui Song continue;
7437370a489SFangrui Song
7447370a489SFangrui Song isec = s;
7457370a489SFangrui Song break;
7467370a489SFangrui Song }
7477370a489SFangrui Song }
7487370a489SFangrui Song if (!isec)
74908d6a3f1SRafael Espindola continue;
75008d6a3f1SRafael Espindola
7512fc704a0SFangrui Song // Set the symbol to be relative to the output section so that its st_value
7522fc704a0SFangrui Song // equals the output section address. Note, there may be a gap between the
7532fc704a0SFangrui Song // start of the output section and isec.
754940bd4c7SFangrui Song in.symTab->addSymbol(makeDefined(isec->file, "", STB_LOCAL, /*stOther=*/0,
755940bd4c7SFangrui Song STT_SECTION,
756940bd4c7SFangrui Song /*value=*/0, /*size=*/0, &osec));
75708d6a3f1SRafael Espindola }
75808d6a3f1SRafael Espindola }
75908d6a3f1SRafael Espindola
76026ad0570SRui Ueyama // Today's loaders have a feature to make segments read-only after
76126ad0570SRui Ueyama // processing dynamic relocations to enhance security. PT_GNU_RELRO
76226ad0570SRui Ueyama // is defined for that.
76326ad0570SRui Ueyama //
76426ad0570SRui Ueyama // This function returns true if a section needs to be put into a
76526ad0570SRui Ueyama // PT_GNU_RELRO segment.
isRelroSection(const OutputSection * sec)7663837f427SRui Ueyama static bool isRelroSection(const OutputSection *sec) {
7673837f427SRui Ueyama if (!config->zRelro)
7684fc6044aSRafael Espindola return false;
7695a44980fSFangrui Song if (sec->relro)
7705a44980fSFangrui Song return true;
77126ad0570SRui Ueyama
7723837f427SRui Ueyama uint64_t flags = sec->flags;
7739d773f3cSRui Ueyama
7749d773f3cSRui Ueyama // Non-allocatable or non-writable sections don't need RELRO because
7759d773f3cSRui Ueyama // they are not writable or not even mapped to memory in the first place.
7769d773f3cSRui Ueyama // RELRO is for sections that are essentially read-only but need to
7779d773f3cSRui Ueyama // be writable only at process startup to allow dynamic linker to
7789d773f3cSRui Ueyama // apply relocations.
7793837f427SRui Ueyama if (!(flags & SHF_ALLOC) || !(flags & SHF_WRITE))
780e3336c0bSGeorge Rimar return false;
7819d773f3cSRui Ueyama
7829d773f3cSRui Ueyama // Once initialized, TLS data segments are used as data templates
7839d773f3cSRui Ueyama // for a thread-local storage. For each new thread, runtime
7849d773f3cSRui Ueyama // allocates memory for a TLS and copy templates there. No thread
7859d773f3cSRui Ueyama // are supposed to use templates directly. Thus, it can be in RELRO.
7863837f427SRui Ueyama if (flags & SHF_TLS)
787ccfc3261SRui Ueyama return true;
78826ad0570SRui Ueyama
7899d773f3cSRui Ueyama // .init_array, .preinit_array and .fini_array contain pointers to
7909d773f3cSRui Ueyama // functions that are executed on process startup or exit. These
7919d773f3cSRui Ueyama // pointers are set by the static linker, and they are not expected
7929d773f3cSRui Ueyama // to change at runtime. But if you are an attacker, you could do
7939d773f3cSRui Ueyama // interesting things by manipulating pointers in .fini_array, for
7949d773f3cSRui Ueyama // example. So they are put into RELRO.
7953837f427SRui Ueyama uint32_t type = sec->type;
7963837f427SRui Ueyama if (type == SHT_INIT_ARRAY || type == SHT_FINI_ARRAY ||
7973837f427SRui Ueyama type == SHT_PREINIT_ARRAY)
798e3336c0bSGeorge Rimar return true;
79926ad0570SRui Ueyama
8009d773f3cSRui Ueyama // .got contains pointers to external symbols. They are resolved by
8019d773f3cSRui Ueyama // the dynamic linker when a module is loaded into memory, and after
8029d773f3cSRui Ueyama // that they are not expected to change. So, it can be in RELRO.
8033837f427SRui Ueyama if (in.got && sec == in.got->getParent())
804725dc14bSSimon Atanasyan return true;
8059d773f3cSRui Ueyama
8066e33f7acSRui Ueyama // .toc is a GOT-ish section for PowerPC64. Their contents are accessed
8076e33f7acSRui Ueyama // through r2 register, which is reserved for that purpose. Since r2 is used
8086e33f7acSRui Ueyama // for accessing .got as well, .got and .toc need to be close enough in the
8096e33f7acSRui Ueyama // virtual address space. Usually, .toc comes just after .got. Since we place
8106e33f7acSRui Ueyama // .got into RELRO, .toc needs to be placed into RELRO too.
8113837f427SRui Ueyama if (sec->name.equals(".toc"))
81285197a08SZaara Syeda return true;
81385197a08SZaara Syeda
8149d773f3cSRui Ueyama // .got.plt contains pointers to external function symbols. They are
8159d773f3cSRui Ueyama // by default resolved lazily, so we usually cannot put it into RELRO.
8169d773f3cSRui Ueyama // However, if "-z now" is given, the lazy symbol resolution is
8179d773f3cSRui Ueyama // disabled, which enables us to put it into RELRO.
8183837f427SRui Ueyama if (sec == in.gotPlt->getParent())
8193837f427SRui Ueyama return config->zNow;
8209d773f3cSRui Ueyama
8219d773f3cSRui Ueyama // .dynamic section contains data for the dynamic linker, and
8229d773f3cSRui Ueyama // there's no need to write to it at runtime, so it's better to put
8239d773f3cSRui Ueyama // it into RELRO.
8243837f427SRui Ueyama if (sec->name == ".dynamic")
8259d773f3cSRui Ueyama return true;
8269d773f3cSRui Ueyama
8279d773f3cSRui Ueyama // Sections with some special names are put into RELRO. This is a
8289d773f3cSRui Ueyama // bit unfortunate because section names shouldn't be significant in
8299d773f3cSRui Ueyama // ELF in spirit. But in reality many linker features depend on
8309d773f3cSRui Ueyama // magic section names.
8313837f427SRui Ueyama StringRef s = sec->name;
8323837f427SRui Ueyama return s == ".data.rel.ro" || s == ".bss.rel.ro" || s == ".ctors" ||
8333837f427SRui Ueyama s == ".dtors" || s == ".jcr" || s == ".eh_frame" ||
8342539b4aeSPeter Smith s == ".fini_array" || s == ".init_array" ||
8352539b4aeSPeter Smith s == ".openbsd.randomdata" || s == ".preinit_array";
836e3336c0bSGeorge Rimar }
837e3336c0bSGeorge Rimar
8385210141bSRafael Espindola // We compute a rank for each section. The rank indicates where the
8395210141bSRafael Espindola // section should be placed in the file. Instead of using simple
8405210141bSRafael Espindola // numbers (0,1,2...), we use a series of flags. One for each decision
8415210141bSRafael Espindola // point when placing the section.
8425210141bSRafael Espindola // Using flags has two key properties:
8435210141bSRafael Espindola // * It is easy to check if a give branch was taken.
8445210141bSRafael Espindola // * It is easy two see how similar two ranks are (see getRankProximity).
8455210141bSRafael Espindola enum RankFlags {
84602828985SPeter Collingbourne RF_NOT_ADDR_SET = 1 << 27,
84702828985SPeter Collingbourne RF_NOT_ALLOC = 1 << 26,
84802828985SPeter Collingbourne RF_PARTITION = 1 << 18, // Partition number (8 bits)
84902828985SPeter Collingbourne RF_NOT_PART_EHDR = 1 << 17,
85002828985SPeter Collingbourne RF_NOT_PART_PHDR = 1 << 16,
85107f8daf0SFangrui Song RF_NOT_INTERP = 1 << 15,
85207f8daf0SFangrui Song RF_NOT_NOTE = 1 << 14,
85307f8daf0SFangrui Song RF_WRITE = 1 << 13,
85407f8daf0SFangrui Song RF_EXEC_WRITE = 1 << 12,
85507f8daf0SFangrui Song RF_EXEC = 1 << 11,
85607f8daf0SFangrui Song RF_RODATA = 1 << 10,
85707f8daf0SFangrui Song RF_NOT_RELRO = 1 << 9,
85841031d97SFangrui Song RF_NOT_TLS = 1 << 8,
85941031d97SFangrui Song RF_BSS = 1 << 7,
860246c1c47SRafael Espindola RF_PPC_NOT_TOCBSS = 1 << 6,
86185197a08SZaara Syeda RF_PPC_TOCL = 1 << 5,
86285197a08SZaara Syeda RF_PPC_TOC = 1 << 4,
86385197a08SZaara Syeda RF_PPC_GOT = 1 << 3,
864246c1c47SRafael Espindola RF_PPC_BRANCH_LT = 1 << 2,
865246c1c47SRafael Espindola RF_MIPS_GPREL = 1 << 1,
866246c1c47SRafael Espindola RF_MIPS_NOT_GOT = 1 << 0
8675210141bSRafael Espindola };
868e288eef3SRui Ueyama
getSectionRank(const OutputSection & osec)8699e9c86fdSFangrui Song static unsigned getSectionRank(const OutputSection &osec) {
8709e9c86fdSFangrui Song unsigned rank = osec.partition * RF_PARTITION;
8715a9640beSRui Ueyama
8725967c973SRafael Espindola // We want to put section specified by -T option first, so we
8735967c973SRafael Espindola // can start assigning VA starting from them later.
8749e9c86fdSFangrui Song if (config->sectionStartMap.count(osec.name))
8753837f427SRui Ueyama return rank;
8763837f427SRui Ueyama rank |= RF_NOT_ADDR_SET;
8775210141bSRafael Espindola
87841031d97SFangrui Song // Allocatable sections go first to reduce the total PT_LOAD size and
87941031d97SFangrui Song // so debug info doesn't change addresses in actual code.
8809e9c86fdSFangrui Song if (!(osec.flags & SHF_ALLOC))
8813837f427SRui Ueyama return rank | RF_NOT_ALLOC;
88241031d97SFangrui Song
8839e9c86fdSFangrui Song if (osec.type == SHT_LLVM_PART_EHDR)
8843837f427SRui Ueyama return rank;
8853837f427SRui Ueyama rank |= RF_NOT_PART_EHDR;
88602828985SPeter Collingbourne
8879e9c86fdSFangrui Song if (osec.type == SHT_LLVM_PART_PHDR)
8883837f427SRui Ueyama return rank;
8893837f427SRui Ueyama rank |= RF_NOT_PART_PHDR;
89002828985SPeter Collingbourne
8915210141bSRafael Espindola // Put .interp first because some loaders want to see that section
8925210141bSRafael Espindola // on the first page of the executable file when loaded into memory.
8939e9c86fdSFangrui Song if (osec.name == ".interp")
8943837f427SRui Ueyama return rank;
8953837f427SRui Ueyama rank |= RF_NOT_INTERP;
8965210141bSRafael Espindola
89741031d97SFangrui Song // Put .note sections (which make up one PT_NOTE) at the beginning so that
89841031d97SFangrui Song // they are likely to be included in a core file even if core file size is
89941031d97SFangrui Song // limited. In particular, we want a .note.gnu.build-id and a .note.tag to be
90041031d97SFangrui Song // included in a core to match core files with executables.
9019e9c86fdSFangrui Song if (osec.type == SHT_NOTE)
9023837f427SRui Ueyama return rank;
9033837f427SRui Ueyama rank |= RF_NOT_NOTE;
9045967c973SRafael Espindola
905d23e9267SRafael Espindola // Sort sections based on their access permission in the following
906d23e9267SRafael Espindola // order: R, RX, RWX, RW. This order is based on the following
907d23e9267SRafael Espindola // considerations:
908d23e9267SRafael Espindola // * Read-only sections come first such that they go in the
909d23e9267SRafael Espindola // PT_LOAD covering the program headers at the start of the file.
91068fa4e8cSFangrui Song // * Read-only, executable sections come next.
911d23e9267SRafael Espindola // * Writable, executable sections follow such that .plt on
912d23e9267SRafael Espindola // architectures where it needs to be writable will be placed
913d23e9267SRafael Espindola // between .text and .data.
914d23e9267SRafael Espindola // * Writable sections come last, such that .bss lands at the very
915d23e9267SRafael Espindola // end of the last PT_LOAD.
9169e9c86fdSFangrui Song bool isExec = osec.flags & SHF_EXECINSTR;
9179e9c86fdSFangrui Song bool isWrite = osec.flags & SHF_WRITE;
9185a9640beSRui Ueyama
9193837f427SRui Ueyama if (isExec) {
9203837f427SRui Ueyama if (isWrite)
9213837f427SRui Ueyama rank |= RF_EXEC_WRITE;
92268fa4e8cSFangrui Song else
9233837f427SRui Ueyama rank |= RF_EXEC;
9243837f427SRui Ueyama } else if (isWrite) {
9253837f427SRui Ueyama rank |= RF_WRITE;
9269e9c86fdSFangrui Song } else if (osec.type == SHT_PROGBITS) {
9273d87323aSFangrui Song // Make non-executable and non-writable PROGBITS sections (e.g .rodata
9283d87323aSFangrui Song // .eh_frame) closer to .text. They likely contain PC or GOT relative
9293d87323aSFangrui Song // relocations and there could be relocation overflow if other huge sections
9303d87323aSFangrui Song // (.dynstr .dynsym) were placed in between.
9313837f427SRui Ueyama rank |= RF_RODATA;
932e979fd1bSRafael Espindola }
9335a9640beSRui Ueyama
93407f8daf0SFangrui Song // Place RelRo sections first. After considering SHT_NOBITS below, the
93507f8daf0SFangrui Song // ordering is PT_LOAD(PT_GNU_RELRO(.data.rel.ro .bss.rel.ro) | .data .bss),
93607f8daf0SFangrui Song // where | marks where page alignment happens. An alternative ordering is
93707f8daf0SFangrui Song // PT_LOAD(.data | PT_GNU_RELRO( .data.rel.ro .bss.rel.ro) | .bss), but it may
93807f8daf0SFangrui Song // waste more bytes due to 2 alignment places.
9399e9c86fdSFangrui Song if (!isRelroSection(&osec))
9403837f427SRui Ueyama rank |= RF_NOT_RELRO;
94107f8daf0SFangrui Song
9420d7e83bbSHal Finkel // If we got here we know that both A and B are in the same PT_LOAD.
9431bf73000SMichael J. Spencer
944628ec9f1SPeter Collingbourne // The TLS initialization block needs to be a single contiguous block in a R/W
945628ec9f1SPeter Collingbourne // PT_LOAD, so stick TLS sections directly before the other RelRo R/W
94607f8daf0SFangrui Song // sections. Since p_filesz can be less than p_memsz, place NOBITS sections
94707f8daf0SFangrui Song // after PROGBITS.
9489e9c86fdSFangrui Song if (!(osec.flags & SHF_TLS))
9493837f427SRui Ueyama rank |= RF_NOT_TLS;
950628ec9f1SPeter Collingbourne
95107f8daf0SFangrui Song // Within TLS sections, or within other RelRo sections, or within non-RelRo
95207f8daf0SFangrui Song // sections, place non-NOBITS sections first.
9539e9c86fdSFangrui Song if (osec.type == SHT_NOBITS)
9543837f427SRui Ueyama rank |= RF_BSS;
955e3336c0bSGeorge Rimar
95695637558SBen Dunbobbin // Some architectures have additional ordering restrictions for sections
95795637558SBen Dunbobbin // within the same PT_LOAD.
9583837f427SRui Ueyama if (config->emachine == EM_PPC64) {
9595210141bSRafael Espindola // PPC64 has a number of special SHT_PROGBITS+SHF_ALLOC+SHF_WRITE sections
9605210141bSRafael Espindola // that we would like to make sure appear is a specific order to maximize
9615210141bSRafael Espindola // their coverage by a single signed 16-bit offset from the TOC base
9625210141bSRafael Espindola // pointer. Conversely, the special .tocbss section should be first among
9635210141bSRafael Espindola // all SHT_NOBITS sections. This will put it next to the loaded special
9645210141bSRafael Espindola // PPC64 sections (and, thus, within reach of the TOC base pointer).
9659e9c86fdSFangrui Song StringRef name = osec.name;
9663837f427SRui Ueyama if (name != ".tocbss")
9673837f427SRui Ueyama rank |= RF_PPC_NOT_TOCBSS;
9689abc2a50SHal Finkel
9693837f427SRui Ueyama if (name == ".toc1")
9703837f427SRui Ueyama rank |= RF_PPC_TOCL;
9715210141bSRafael Espindola
9723837f427SRui Ueyama if (name == ".toc")
9733837f427SRui Ueyama rank |= RF_PPC_TOC;
9745210141bSRafael Espindola
9753837f427SRui Ueyama if (name == ".got")
9763837f427SRui Ueyama rank |= RF_PPC_GOT;
97785197a08SZaara Syeda
9783837f427SRui Ueyama if (name == ".branch_lt")
9793837f427SRui Ueyama rank |= RF_PPC_BRANCH_LT;
9805210141bSRafael Espindola }
98137ad8161SRui Ueyama
9823837f427SRui Ueyama if (config->emachine == EM_MIPS) {
9835210141bSRafael Espindola // All sections with SHF_MIPS_GPREL flag should be grouped together
9845210141bSRafael Espindola // because data in these sections is addressable with a gp relative address.
9859e9c86fdSFangrui Song if (osec.flags & SHF_MIPS_GPREL)
9863837f427SRui Ueyama rank |= RF_MIPS_GPREL;
9875210141bSRafael Espindola
9889e9c86fdSFangrui Song if (osec.name != ".got")
9893837f427SRui Ueyama rank |= RF_MIPS_NOT_GOT;
9905210141bSRafael Espindola }
9915210141bSRafael Espindola
9923837f427SRui Ueyama return rank;
9935210141bSRafael Espindola }
9945210141bSRafael Espindola
compareSections(const SectionCommand * aCmd,const SectionCommand * bCmd)9957051aeefSFangrui Song static bool compareSections(const SectionCommand *aCmd,
9967051aeefSFangrui Song const SectionCommand *bCmd) {
9976c814931SFangrui Song const OutputSection *a = &cast<OutputDesc>(aCmd)->osec;
9986c814931SFangrui Song const OutputSection *b = &cast<OutputDesc>(bCmd)->osec;
9994664280aSRui Ueyama
10003837f427SRui Ueyama if (a->sortRank != b->sortRank)
10013837f427SRui Ueyama return a->sortRank < b->sortRank;
10024664280aSRui Ueyama
10033837f427SRui Ueyama if (!(a->sortRank & RF_NOT_ADDR_SET))
10043837f427SRui Ueyama return config->sectionStartMap.lookup(a->name) <
10053837f427SRui Ueyama config->sectionStartMap.lookup(b->name);
100624c073d1SRafael Espindola return false;
100724c073d1SRafael Espindola }
100824c073d1SRafael Espindola
add(OutputSection * sec)10093837f427SRui Ueyama void PhdrEntry::add(OutputSection *sec) {
10103837f427SRui Ueyama lastSec = sec;
10113837f427SRui Ueyama if (!firstSec)
10123837f427SRui Ueyama firstSec = sec;
10133837f427SRui Ueyama p_align = std::max(p_align, sec->alignment);
101417cb7c0aSRafael Espindola if (p_type == PT_LOAD)
10153837f427SRui Ueyama sec->ptLoad = this;
1016bbe38602SEugene Leviant }
1017bbe38602SEugene Leviant
10180168722eSRui Ueyama // The beginning and the ending of .rel[a].plt section are marked
10190168722eSRui Ueyama // with __rel[a]_iplt_{start,end} symbols if it is a statically linked
10200168722eSRui Ueyama // executable. The runtime needs these symbols in order to resolve
10210168722eSRui Ueyama // all IRELATIVE relocs on startup. For dynamic executables, we don't
10220168722eSRui Ueyama // need these symbols, since IRELATIVE relocs are resolved through GOT
10230168722eSRui Ueyama // and PLT. For details, see http://www.airs.com/blog/archives/403.
addRelIpltSymbols()1024ee741cfaSGeorge Rimar template <class ELFT> void Writer<ELFT>::addRelIpltSymbols() {
1025f8cb78e9SFangrui Song if (config->relocatable || config->isPic)
1026a07ff661SGeorge Rimar return;
10270168722eSRui Ueyama
10286f9d49cdSRui Ueyama // By default, __rela_iplt_{start,end} belong to a dummy section 0
10296f9d49cdSRui Ueyama // because .rela.plt might be empty and thus removed from output.
103047cfe8f3SFangrui Song // We'll override Out::elfHeader with In.relaIplt later when we are
10316f9d49cdSRui Ueyama // sure that .rela.plt exists in output.
10323837f427SRui Ueyama ElfSym::relaIpltStart = addOptionalRegular(
10333837f427SRui Ueyama config->isRela ? "__rela_iplt_start" : "__rel_iplt_start",
1034db6a00daSFangrui Song Out::elfHeader, 0, STV_HIDDEN);
10356f9d49cdSRui Ueyama
10363837f427SRui Ueyama ElfSym::relaIpltEnd = addOptionalRegular(
10373837f427SRui Ueyama config->isRela ? "__rela_iplt_end" : "__rel_iplt_end",
1038db6a00daSFangrui Song Out::elfHeader, 0, STV_HIDDEN);
1039a07ff661SGeorge Rimar }
1040a07ff661SGeorge Rimar
104118821b60SPetr Hosek // This function generates assignments for predefined symbols (e.g. _end or
104218821b60SPetr Hosek // _etext) and inserts them into the commands sequence to be processed at the
104318821b60SPetr Hosek // appropriate time. This ensures that the value is going to be correct by the
104418821b60SPetr Hosek // time any references to these symbols are processed and is equivalent to
104518821b60SPetr Hosek // defining these symbols explicitly in the linker script.
setReservedSymbolSections()10464d560160SRafael Espindola template <class ELFT> void Writer<ELFT>::setReservedSymbolSections() {
10473837f427SRui Ueyama if (ElfSym::globalOffsetTable) {
10483d044f57SPeter Smith // The _GLOBAL_OFFSET_TABLE_ symbol is defined by target convention usually
10493d044f57SPeter Smith // to the start of the .got or .got.plt section.
1050cb203f3fSFangrui Song InputSection *sec = in.gotPlt.get();
10513837f427SRui Ueyama if (!target->gotBaseSymInGotPlt)
1052cb203f3fSFangrui Song sec = in.mipsGot.get() ? cast<InputSection>(in.mipsGot.get())
1053cb203f3fSFangrui Song : cast<InputSection>(in.got.get());
1054cb203f3fSFangrui Song ElfSym::globalOffsetTable->section = sec;
105563fcc5ccSRafael Espindola }
105663fcc5ccSRafael Espindola
105725ab1c64SFangrui Song // .rela_iplt_{start,end} mark the start and the end of in.relaIplt.
10583837f427SRui Ueyama if (ElfSym::relaIpltStart && in.relaIplt->isNeeded()) {
1059cb203f3fSFangrui Song ElfSym::relaIpltStart->section = in.relaIplt.get();
1060cb203f3fSFangrui Song ElfSym::relaIpltEnd->section = in.relaIplt.get();
10613837f427SRui Ueyama ElfSym::relaIpltEnd->value = in.relaIplt->getSize();
10626f9d49cdSRui Ueyama }
1063aded4093SRafael Espindola
10643837f427SRui Ueyama PhdrEntry *last = nullptr;
10653837f427SRui Ueyama PhdrEntry *lastRO = nullptr;
1066888da8c2SRui Ueyama
10673837f427SRui Ueyama for (Partition &part : partitions) {
10683837f427SRui Ueyama for (PhdrEntry *p : part.phdrs) {
10693837f427SRui Ueyama if (p->p_type != PT_LOAD)
107018821b60SPetr Hosek continue;
10713837f427SRui Ueyama last = p;
10723837f427SRui Ueyama if (!(p->p_flags & PF_W))
10733837f427SRui Ueyama lastRO = p;
107418821b60SPetr Hosek }
107502828985SPeter Collingbourne }
107618821b60SPetr Hosek
10773837f427SRui Ueyama if (lastRO) {
1078e6624361SPeter Collingbourne // _etext is the first location after the last read-only loadable segment.
10793837f427SRui Ueyama if (ElfSym::etext1)
10803837f427SRui Ueyama ElfSym::etext1->section = lastRO->lastSec;
10813837f427SRui Ueyama if (ElfSym::etext2)
10823837f427SRui Ueyama ElfSym::etext2->section = lastRO->lastSec;
108318821b60SPetr Hosek }
1084888da8c2SRui Ueyama
10853837f427SRui Ueyama if (last) {
1086e6624361SPeter Collingbourne // _edata points to the end of the last mapped initialized section.
10873837f427SRui Ueyama OutputSection *edata = nullptr;
10883837f427SRui Ueyama for (OutputSection *os : outputSections) {
10893837f427SRui Ueyama if (os->type != SHT_NOBITS)
10903837f427SRui Ueyama edata = os;
10913837f427SRui Ueyama if (os == last->lastSec)
1092888da8c2SRui Ueyama break;
1093e6624361SPeter Collingbourne }
1094d30bd132SGeorge Rimar
10953837f427SRui Ueyama if (ElfSym::edata1)
10963837f427SRui Ueyama ElfSym::edata1->section = edata;
10973837f427SRui Ueyama if (ElfSym::edata2)
10983837f427SRui Ueyama ElfSym::edata2->section = edata;
1099e6624361SPeter Collingbourne
1100e6624361SPeter Collingbourne // _end is the first location after the uninitialized data region.
11013837f427SRui Ueyama if (ElfSym::end1)
11023837f427SRui Ueyama ElfSym::end1->section = last->lastSec;
11033837f427SRui Ueyama if (ElfSym::end2)
11043837f427SRui Ueyama ElfSym::end2->section = last->lastSec;
11054d560160SRafael Espindola }
11064d560160SRafael Espindola
11073837f427SRui Ueyama if (ElfSym::bss)
11083837f427SRui Ueyama ElfSym::bss->section = findSection(".bss");
11094d560160SRafael Espindola
11104d560160SRafael Espindola // Setup MIPS _gp_disp/__gnu_local_gp symbols which should
11114d560160SRafael Espindola // be equal to the _gp symbol's value.
11123837f427SRui Ueyama if (ElfSym::mipsGp) {
11134d560160SRafael Espindola // Find GP-relative section with the lowest address
11144d560160SRafael Espindola // and use this address to calculate default _gp value.
11153837f427SRui Ueyama for (OutputSection *os : outputSections) {
11163837f427SRui Ueyama if (os->flags & SHF_MIPS_GPREL) {
11173837f427SRui Ueyama ElfSym::mipsGp->section = os;
11183837f427SRui Ueyama ElfSym::mipsGp->value = 0x7ff0;
11194d560160SRafael Espindola break;
11204d560160SRafael Espindola }
11214d560160SRafael Espindola }
112218821b60SPetr Hosek }
112318821b60SPetr Hosek }
112418821b60SPetr Hosek
11255210141bSRafael Espindola // We want to find how similar two ranks are.
11265210141bSRafael Espindola // The more branches in getSectionRank that match, the more similar they are.
11275210141bSRafael Espindola // Since each branch corresponds to a bit flag, we can just use
11285210141bSRafael Espindola // countLeadingZeros.
getRankProximityAux(const OutputSection & a,const OutputSection & b)11296c814931SFangrui Song static int getRankProximityAux(const OutputSection &a, const OutputSection &b) {
11306c814931SFangrui Song return countLeadingZeros(a.sortRank ^ b.sortRank);
1131bae1c656SEugene Leviant }
1132bae1c656SEugene Leviant
getRankProximity(OutputSection * a,SectionCommand * b)11337051aeefSFangrui Song static int getRankProximity(OutputSection *a, SectionCommand *b) {
11346c814931SFangrui Song auto *osd = dyn_cast<OutputDesc>(b);
11356c814931SFangrui Song return (osd && osd->osec.hasInputSections)
11366c814931SFangrui Song ? getRankProximityAux(*a, osd->osec)
11376c814931SFangrui Song : -1;
1138383971d2SRafael Espindola }
1139383971d2SRafael Espindola
1140383971d2SRafael Espindola // When placing orphan sections, we want to place them after symbol assignments
1141383971d2SRafael Espindola // so that an orphan after
1142383971d2SRafael Espindola // begin_foo = .;
1143383971d2SRafael Espindola // foo : { *(foo) }
1144383971d2SRafael Espindola // end_foo = .;
1145383971d2SRafael Espindola // doesn't break the intended meaning of the begin/end symbols.
1146383971d2SRafael Espindola // We don't want to go over sections since findOrphanPos is the
1147383971d2SRafael Espindola // one in charge of deciding the order of the sections.
1148383971d2SRafael Espindola // We don't want to go over changes to '.', since doing so in
1149383971d2SRafael Espindola // rx_sec : { *(rx_sec) }
1150383971d2SRafael Espindola // . = ALIGN(0x1000);
1151383971d2SRafael Espindola // /* The RW PT_LOAD starts here*/
1152383971d2SRafael Espindola // rw_sec : { *(rw_sec) }
1153383971d2SRafael Espindola // would mean that the RW PT_LOAD would become unaligned.
shouldSkip(SectionCommand * cmd)11547051aeefSFangrui Song static bool shouldSkip(SectionCommand *cmd) {
11553837f427SRui Ueyama if (auto *assign = dyn_cast<SymbolAssignment>(cmd))
11563837f427SRui Ueyama return assign->name != ".";
11576a7c7f39SGeorge Rimar return false;
1158383971d2SRafael Espindola }
1159383971d2SRafael Espindola
11605210141bSRafael Espindola // We want to place orphan sections so that they share as much
11615210141bSRafael Espindola // characteristics with their neighbors as possible. For example, if
11625210141bSRafael Espindola // both are rw, or both are tls.
1163a1c2ee01SFangrui Song static SmallVectorImpl<SectionCommand *>::iterator
findOrphanPos(SmallVectorImpl<SectionCommand * >::iterator b,SmallVectorImpl<SectionCommand * >::iterator e)1164a1c2ee01SFangrui Song findOrphanPos(SmallVectorImpl<SectionCommand *>::iterator b,
1165a1c2ee01SFangrui Song SmallVectorImpl<SectionCommand *>::iterator e) {
11666c814931SFangrui Song OutputSection *sec = &cast<OutputDesc>(*e)->osec;
11670ca37127SRafael Espindola
11685210141bSRafael Espindola // Find the first element that has as close a rank as possible.
11697051aeefSFangrui Song auto i = std::max_element(b, e, [=](SectionCommand *a, SectionCommand *b) {
11703837f427SRui Ueyama return getRankProximity(sec, a) < getRankProximity(sec, b);
11715210141bSRafael Espindola });
11723837f427SRui Ueyama if (i == e)
11733837f427SRui Ueyama return e;
11746c814931SFangrui Song if (!isa<OutputDesc>(*i))
11751302fdc2SIgor Kudrin return e;
11766c814931SFangrui Song auto foundSec = &cast<OutputDesc>(*i)->osec;
11770ca37127SRafael Espindola
11785210141bSRafael Espindola // Consider all existing sections with the same proximity.
11793837f427SRui Ueyama int proximity = getRankProximity(sec, *i);
11801302fdc2SIgor Kudrin unsigned sortRank = sec->sortRank;
1181d2dd36bbSIgor Kudrin if (script->hasPhdrsCommands() || !script->memoryRegions.empty())
1182d2dd36bbSIgor Kudrin // Prevent the orphan section to be placed before the found section. If
1183d2dd36bbSIgor Kudrin // custom program headers are defined, that helps to avoid adding it to a
1184d2dd36bbSIgor Kudrin // previous segment and changing flags of that segment, for example, making
1185d2dd36bbSIgor Kudrin // a read-only segment writable. If memory regions are defined, an orphan
1186d2dd36bbSIgor Kudrin // section should continue the same region as the found section to better
1187d2dd36bbSIgor Kudrin // resemble the behavior of GNU ld.
11881302fdc2SIgor Kudrin sortRank = std::max(sortRank, foundSec->sortRank);
11893837f427SRui Ueyama for (; i != e; ++i) {
11906c814931SFangrui Song auto *curSecDesc = dyn_cast<OutputDesc>(*i);
11916c814931SFangrui Song if (!curSecDesc || !curSecDesc->osec.hasInputSections)
1192383971d2SRafael Espindola continue;
11936c814931SFangrui Song if (getRankProximity(sec, curSecDesc) != proximity ||
11946c814931SFangrui Song sortRank < curSecDesc->osec.sortRank)
1195383971d2SRafael Espindola break;
1196383971d2SRafael Espindola }
11972fd533dbSPetr Hosek
11987051aeefSFangrui Song auto isOutputSecWithInputSections = [](SectionCommand *cmd) {
11996c814931SFangrui Song auto *osd = dyn_cast<OutputDesc>(cmd);
12006c814931SFangrui Song return osd && osd->osec.hasInputSections;
120198c858a2SAndrew Ng };
1202afc14a0dSBenjamin Kramer auto j =
1203afc14a0dSBenjamin Kramer std::find_if(std::make_reverse_iterator(i), std::make_reverse_iterator(b),
12043837f427SRui Ueyama isOutputSecWithInputSections);
12053837f427SRui Ueyama i = j.base();
1206aad64e0aSRafael Espindola
1207aad64e0aSRafael Espindola // As a special case, if the orphan section is the last section, put
1208aad64e0aSRafael Espindola // it at the very end, past any other commands.
1209aad64e0aSRafael Espindola // This matches bfd's behavior and is convenient when the linker script fully
1210aad64e0aSRafael Espindola // specifies the start of the file, but doesn't care about the end (the non
1211aad64e0aSRafael Espindola // alloc sections for example).
12123837f427SRui Ueyama auto nextSec = std::find_if(i, e, isOutputSecWithInputSections);
12133837f427SRui Ueyama if (nextSec == e)
12143837f427SRui Ueyama return e;
1215aad64e0aSRafael Espindola
12163837f427SRui Ueyama while (i != e && shouldSkip(*i))
12173837f427SRui Ueyama ++i;
12183837f427SRui Ueyama return i;
12190ca37127SRafael Espindola }
12200ca37127SRafael Espindola
1221d48d3391SRafael Ávila de Espíndola // Adds random priorities to sections not already in the map.
maybeShuffle(DenseMap<const InputSectionBase *,int> & order)1222d48d3391SRafael Ávila de Espíndola static void maybeShuffle(DenseMap<const InputSectionBase *, int> &order) {
122316c30c3cSFangrui Song if (config->shuffleSections.empty())
1224d48d3391SRafael Ávila de Espíndola return;
1225d48d3391SRafael Ávila de Espíndola
1226ba948c5aSFangrui Song SmallVector<InputSectionBase *, 0> matched, sections = inputSections;
122716c30c3cSFangrui Song matched.reserve(sections.size());
122816c30c3cSFangrui Song for (const auto &patAndSeed : config->shuffleSections) {
122916c30c3cSFangrui Song matched.clear();
123016c30c3cSFangrui Song for (InputSectionBase *sec : sections)
123116c30c3cSFangrui Song if (patAndSeed.first.match(sec->name))
123216c30c3cSFangrui Song matched.push_back(sec);
123316c30c3cSFangrui Song const uint32_t seed = patAndSeed.second;
1234423cb321SFangrui Song if (seed == UINT32_MAX) {
123516c30c3cSFangrui Song // If --shuffle-sections <section-glob>=-1, reverse the section order. The
123616c30c3cSFangrui Song // section order is stable even if the number of sections changes. This is
123716c30c3cSFangrui Song // useful to catch issues like static initialization order fiasco
123816c30c3cSFangrui Song // reliably.
123916c30c3cSFangrui Song std::reverse(matched.begin(), matched.end());
1240423cb321SFangrui Song } else {
1241d48d3391SRafael Ávila de Espíndola std::mt19937 g(seed ? seed : std::random_device()());
124216c30c3cSFangrui Song llvm::shuffle(matched.begin(), matched.end(), g);
1243423cb321SFangrui Song }
124416c30c3cSFangrui Song size_t i = 0;
124516c30c3cSFangrui Song for (InputSectionBase *&sec : sections)
124616c30c3cSFangrui Song if (patAndSeed.first.match(sec->name))
124716c30c3cSFangrui Song sec = matched[i++];
124816c30c3cSFangrui Song }
124916c30c3cSFangrui Song
125016c30c3cSFangrui Song // Existing priorities are < 0, so use priorities >= 0 for the missing
125116c30c3cSFangrui Song // sections.
125216c30c3cSFangrui Song int prio = 0;
125316c30c3cSFangrui Song for (InputSectionBase *sec : sections) {
125416c30c3cSFangrui Song if (order.try_emplace(sec, prio).second)
125516c30c3cSFangrui Song ++prio;
1256d48d3391SRafael Ávila de Espíndola }
1257d48d3391SRafael Ávila de Espíndola }
1258d48d3391SRafael Ávila de Espíndola
1259c7945c82SRafael Espindola // Builds section order for handling --symbol-ordering-file.
buildSectionOrder()12601d76120dSRafael Espindola static DenseMap<const InputSectionBase *, int> buildSectionOrder() {
12613837f427SRui Ueyama DenseMap<const InputSectionBase *, int> sectionOrder;
1262bf6e259bSFangrui Song // Use the rarely used option --call-graph-ordering-file to sort sections.
12633837f427SRui Ueyama if (!config->callGraphProfile.empty())
1264b842725cSMichael J. Spencer return computeCallGraphProfileOrder();
1265b842725cSMichael J. Spencer
12663837f427SRui Ueyama if (config->symbolOrderingFile.empty())
12673837f427SRui Ueyama return sectionOrder;
1268c7945c82SRafael Espindola
1269de300e66SJames Henderson struct SymbolOrderEntry {
12703837f427SRui Ueyama int priority;
12713837f427SRui Ueyama bool present;
1272de300e66SJames Henderson };
1273de300e66SJames Henderson
1274c7945c82SRafael Espindola // Build a map from symbols to their priorities. Symbols that didn't
1275c7945c82SRafael Espindola // appear in the symbol ordering file have the lowest priority 0.
1276c7945c82SRafael Espindola // All explicitly mentioned symbols have negative (higher) priorities.
1277769057a5SFangrui Song DenseMap<CachedHashStringRef, SymbolOrderEntry> symbolOrder;
12783837f427SRui Ueyama int priority = -config->symbolOrderingFile.size();
12793837f427SRui Ueyama for (StringRef s : config->symbolOrderingFile)
1280769057a5SFangrui Song symbolOrder.insert({CachedHashStringRef(s), {priority++, false}});
1281c7945c82SRafael Espindola
1282c7945c82SRafael Espindola // Build a map from sections to their priorities.
12833837f427SRui Ueyama auto addSym = [&](Symbol &sym) {
1284769057a5SFangrui Song auto it = symbolOrder.find(CachedHashStringRef(sym.getName()));
12853837f427SRui Ueyama if (it == symbolOrder.end())
12863bd98b10SRafael Espindola return;
12873837f427SRui Ueyama SymbolOrderEntry &ent = it->second;
12883837f427SRui Ueyama ent.present = true;
1289de300e66SJames Henderson
12903837f427SRui Ueyama maybeWarnUnorderableSymbol(&sym);
1291de300e66SJames Henderson
12923837f427SRui Ueyama if (auto *d = dyn_cast<Defined>(&sym)) {
12933837f427SRui Ueyama if (auto *sec = dyn_cast_or_null<InputSectionBase>(d->section)) {
1294e1b6b5beSFangrui Song int &priority = sectionOrder[cast<InputSectionBase>(sec)];
12953837f427SRui Ueyama priority = std::min(priority, ent.priority);
1296c7945c82SRafael Espindola }
1297c7945c82SRafael Espindola }
12983bd98b10SRafael Espindola };
12994664280aSRui Ueyama
13003bd98b10SRafael Espindola // We want both global and local symbols. We get the global ones from the
13013bd98b10SRafael Espindola // symbol table and iterate the object files for the local ones.
1302a2fc9644SFangrui Song for (Symbol *sym : symtab->symbols())
13033837f427SRui Ueyama addSym(*sym);
1304d8f8abbdSRui Ueyama
13059a572164SFangrui Song for (ELFFileBase *file : ctx->objectFiles)
13060940cd18SFangrui Song for (Symbol *sym : file->getLocalSymbols())
13073837f427SRui Ueyama addSym(*sym);
1308de300e66SJames Henderson
13093837f427SRui Ueyama if (config->warnSymbolOrdering)
13103837f427SRui Ueyama for (auto orderEntry : symbolOrder)
13113837f427SRui Ueyama if (!orderEntry.second.present)
1312769057a5SFangrui Song warn("symbol ordering file: no such symbol: " + orderEntry.first.val());
1313de300e66SJames Henderson
13143837f427SRui Ueyama return sectionOrder;
1315c7945c82SRafael Espindola }
1316c7945c82SRafael Espindola
13175ea6d50aSPeter Collingbourne // Sorts the sections in ISD according to the provided section order.
13185ea6d50aSPeter Collingbourne static void
sortISDBySectionOrder(InputSectionDescription * isd,const DenseMap<const InputSectionBase *,int> & order,bool executableOutputSection)13193837f427SRui Ueyama sortISDBySectionOrder(InputSectionDescription *isd,
13202324c2e3SYongKang Zhu const DenseMap<const InputSectionBase *, int> &order,
13212324c2e3SYongKang Zhu bool executableOutputSection) {
1322a1c2ee01SFangrui Song SmallVector<InputSection *, 0> unorderedSections;
1323a1c2ee01SFangrui Song SmallVector<std::pair<InputSection *, int>, 0> orderedSections;
13243837f427SRui Ueyama uint64_t unorderedSize = 0;
13252324c2e3SYongKang Zhu uint64_t totalSize = 0;
13265ea6d50aSPeter Collingbourne
13273837f427SRui Ueyama for (InputSection *isec : isd->sections) {
13282324c2e3SYongKang Zhu if (executableOutputSection)
13292324c2e3SYongKang Zhu totalSize += isec->getSize();
13303837f427SRui Ueyama auto i = order.find(isec);
13313837f427SRui Ueyama if (i == order.end()) {
13323837f427SRui Ueyama unorderedSections.push_back(isec);
13333837f427SRui Ueyama unorderedSize += isec->getSize();
13345ea6d50aSPeter Collingbourne continue;
13355ea6d50aSPeter Collingbourne }
13363837f427SRui Ueyama orderedSections.push_back({isec, i->second});
13375ea6d50aSPeter Collingbourne }
13386d5a8c92SFangrui Song llvm::sort(orderedSections, llvm::less_second());
13395ea6d50aSPeter Collingbourne
13405ea6d50aSPeter Collingbourne // Find an insertion point for the ordered section list in the unordered
13415ea6d50aSPeter Collingbourne // section list. On targets with limited-range branches, this is the mid-point
13425ea6d50aSPeter Collingbourne // of the unordered section list. This decreases the likelihood that a range
13435ea6d50aSPeter Collingbourne // extension thunk will be needed to enter or exit the ordered region. If the
13445ea6d50aSPeter Collingbourne // ordered section list is a list of hot functions, we can generally expect
13455ea6d50aSPeter Collingbourne // the ordered functions to be called more often than the unordered functions,
13465ea6d50aSPeter Collingbourne // making it more likely that any particular call will be within range, and
13475ea6d50aSPeter Collingbourne // therefore reducing the number of thunks required.
13485ea6d50aSPeter Collingbourne //
13495ea6d50aSPeter Collingbourne // For example, imagine that you have 8MB of hot code and 32MB of cold code.
13505ea6d50aSPeter Collingbourne // If the layout is:
13515ea6d50aSPeter Collingbourne //
13525ea6d50aSPeter Collingbourne // 8MB hot
13535ea6d50aSPeter Collingbourne // 32MB cold
13545ea6d50aSPeter Collingbourne //
13555ea6d50aSPeter Collingbourne // only the first 8-16MB of the cold code (depending on which hot function it
13565ea6d50aSPeter Collingbourne // is actually calling) can call the hot code without a range extension thunk.
13575ea6d50aSPeter Collingbourne // However, if we use this layout:
13585ea6d50aSPeter Collingbourne //
13595ea6d50aSPeter Collingbourne // 16MB cold
13605ea6d50aSPeter Collingbourne // 8MB hot
13615ea6d50aSPeter Collingbourne // 16MB cold
13625ea6d50aSPeter Collingbourne //
13635ea6d50aSPeter Collingbourne // both the last 8-16MB of the first block of cold code and the first 8-16MB
13645ea6d50aSPeter Collingbourne // of the second block of cold code can call the hot code without a thunk. So
13655ea6d50aSPeter Collingbourne // we effectively double the amount of code that could potentially call into
13665ea6d50aSPeter Collingbourne // the hot code without a thunk.
13672324c2e3SYongKang Zhu //
13682324c2e3SYongKang Zhu // The above is not necessary if total size of input sections in this "isd"
13692324c2e3SYongKang Zhu // is small. Note that we assume all input sections are executable if the
13702324c2e3SYongKang Zhu // output section is executable (which is not always true but supposed to
13712324c2e3SYongKang Zhu // cover most cases).
13723837f427SRui Ueyama size_t insPt = 0;
13732324c2e3SYongKang Zhu if (executableOutputSection && !orderedSections.empty() &&
13742324c2e3SYongKang Zhu target->getThunkSectionSpacing() &&
13752324c2e3SYongKang Zhu totalSize >= target->getThunkSectionSpacing()) {
13763837f427SRui Ueyama uint64_t unorderedPos = 0;
13773837f427SRui Ueyama for (; insPt != unorderedSections.size(); ++insPt) {
13783837f427SRui Ueyama unorderedPos += unorderedSections[insPt]->getSize();
13793837f427SRui Ueyama if (unorderedPos > unorderedSize / 2)
13805ea6d50aSPeter Collingbourne break;
13815ea6d50aSPeter Collingbourne }
13825ea6d50aSPeter Collingbourne }
13835ea6d50aSPeter Collingbourne
13843837f427SRui Ueyama isd->sections.clear();
13853837f427SRui Ueyama for (InputSection *isec : makeArrayRef(unorderedSections).slice(0, insPt))
13863837f427SRui Ueyama isd->sections.push_back(isec);
13873837f427SRui Ueyama for (std::pair<InputSection *, int> p : orderedSections)
13883837f427SRui Ueyama isd->sections.push_back(p.first);
13893837f427SRui Ueyama for (InputSection *isec : makeArrayRef(unorderedSections).slice(insPt))
13903837f427SRui Ueyama isd->sections.push_back(isec);
13915ea6d50aSPeter Collingbourne }
13925ea6d50aSPeter Collingbourne
sortSection(OutputSection & osec,const DenseMap<const InputSectionBase *,int> & order)13939e9c86fdSFangrui Song static void sortSection(OutputSection &osec,
13943837f427SRui Ueyama const DenseMap<const InputSectionBase *, int> &order) {
13959e9c86fdSFangrui Song StringRef name = osec.name;
1396ecfd7345SRafael Espindola
1397dbd7281aSFangrui Song // Never sort these.
1398dbd7281aSFangrui Song if (name == ".init" || name == ".fini")
1399dbd7281aSFangrui Song return;
1400dbd7281aSFangrui Song
1401c135a68dSGeorgii Rymar // IRelative relocations that usually live in the .rel[a].dyn section should
1402cb4df6ebSNico Weber // be processed last by the dynamic loader. To achieve that we add synthetic
1403cb4df6ebSNico Weber // sections in the required order from the beginning so that the in.relaIplt
1404c135a68dSGeorgii Rymar // section is placed last in an output section. Here we just do not apply
1405c135a68dSGeorgii Rymar // sorting for an output section which holds the in.relaIplt section.
14069e9c86fdSFangrui Song if (in.relaIplt->getParent() == &osec)
1407c135a68dSGeorgii Rymar return;
1408c135a68dSGeorgii Rymar
1409dbd7281aSFangrui Song // Sort input sections by priority using the list provided by
1410dbd7281aSFangrui Song // --symbol-ordering-file or --shuffle-sections=. This is a least significant
1411dbd7281aSFangrui Song // digit radix sort. The sections may be sorted stably again by a more
1412dbd7281aSFangrui Song // significant key.
1413dbd7281aSFangrui Song if (!order.empty())
14149e9c86fdSFangrui Song for (SectionCommand *b : osec.commands)
1415dbd7281aSFangrui Song if (auto *isd = dyn_cast<InputSectionDescription>(b))
14162324c2e3SYongKang Zhu sortISDBySectionOrder(isd, order, osec.flags & SHF_EXECINSTR);
1417dbd7281aSFangrui Song
14183b42fc8aSFangrui Song if (script->hasSectionsCommand)
14193b42fc8aSFangrui Song return;
14203b42fc8aSFangrui Song
14213837f427SRui Ueyama if (name == ".init_array" || name == ".fini_array") {
14229e9c86fdSFangrui Song osec.sortInitFini();
14233b42fc8aSFangrui Song } else if (name == ".ctors" || name == ".dtors") {
14249e9c86fdSFangrui Song osec.sortCtorsDtors();
14253b42fc8aSFangrui Song } else if (config->emachine == EM_PPC64 && name == ".toc") {
142683cb2528SSean Fertile // .toc is allocated just after .got and is accessed using GOT-relative
142783cb2528SSean Fertile // relocations. Object files compiled with small code model have an
142883cb2528SSean Fertile // addressable range of [.got, .got + 0xFFFC] for GOT-relative relocations.
14293b42fc8aSFangrui Song // To reduce the risk of relocation overflow, .toc contents are sorted so
14303b42fc8aSFangrui Song // that sections having smaller relocation offsets are at beginning of .toc
14319e9c86fdSFangrui Song assert(osec.commands.size() == 1);
14329e9c86fdSFangrui Song auto *isd = cast<InputSectionDescription>(osec.commands[0]);
14333837f427SRui Ueyama llvm::stable_sort(isd->sections,
14343837f427SRui Ueyama [](const InputSection *a, const InputSection *b) -> bool {
14353837f427SRui Ueyama return a->file->ppc64SmallCodeModelTocRelocs &&
14363837f427SRui Ueyama !b->file->ppc64SmallCodeModelTocRelocs;
143783cb2528SSean Fertile });
143883cb2528SSean Fertile }
1439e43db0e8SMichael J. Spencer }
1440e43db0e8SMichael J. Spencer
1441a6b1fbecSGeorge Rimar // If no layout was provided by linker script, we want to apply default
144222d53356SRafael Espindola // sorting for special input sections. This also handles --symbol-ordering-file.
sortInputSections()1443a6b1fbecSGeorge Rimar template <class ELFT> void Writer<ELFT>::sortInputSections() {
1444ecfd7345SRafael Espindola // Build the order once since it is expensive.
14453837f427SRui Ueyama DenseMap<const InputSectionBase *, int> order = buildSectionOrder();
1446d48d3391SRafael Ávila de Espíndola maybeShuffle(order);
14477051aeefSFangrui Song for (SectionCommand *cmd : script->sectionCommands)
14486c814931SFangrui Song if (auto *osd = dyn_cast<OutputDesc>(cmd))
14496c814931SFangrui Song sortSection(osd->osec, order);
1450a6b1fbecSGeorge Rimar }
1451a6b1fbecSGeorge Rimar
sortSections()145224c073d1SRafael Espindola template <class ELFT> void Writer<ELFT>::sortSections() {
1453439341b9SJames Henderson llvm::TimeTraceScope timeScope("Sort sections");
1454e0b43df3SGeorge Rimar
14551960bcd7SRafael Espindola // Don't sort if using -r. It is not necessary and we want to preserve the
14561960bcd7SRafael Espindola // relative order for SHF_LINK_ORDER sections.
1457a0318711SFangrui Song if (config->relocatable) {
1458a0318711SFangrui Song script->adjustOutputSections();
14591960bcd7SRafael Espindola return;
1460a0318711SFangrui Song }
14615210141bSRafael Espindola
1462a6b1fbecSGeorge Rimar sortInputSections();
1463a6b1fbecSGeorge Rimar
1464a0318711SFangrui Song for (SectionCommand *cmd : script->sectionCommands)
14656c814931SFangrui Song if (auto *osd = dyn_cast<OutputDesc>(cmd))
14666c814931SFangrui Song osd->osec.sortRank = getSectionRank(osd->osec);
14673837f427SRui Ueyama if (!script->hasSectionsCommand) {
1468a6b1fbecSGeorge Rimar // We know that all the OutputSections are contiguous in this case.
14696c814931SFangrui Song auto isSection = [](SectionCommand *cmd) { return isa<OutputDesc>(cmd); };
14709f0b8e80SGeorge Rimar std::stable_sort(
14713837f427SRui Ueyama llvm::find_if(script->sectionCommands, isSection),
14723837f427SRui Ueyama llvm::find_if(llvm::reverse(script->sectionCommands), isSection).base(),
14739f0b8e80SGeorge Rimar compareSections);
147424c073d1SRafael Espindola }
147524c073d1SRafael Espindola
1476a0318711SFangrui Song // Process INSERT commands and update output section attributes. From this
1477a0318711SFangrui Song // point onwards the order of script->sectionCommands is fixed.
14787c426fb1SFangrui Song script->processInsertCommands();
1479a0318711SFangrui Song script->adjustOutputSections();
1480a0318711SFangrui Song
1481a0318711SFangrui Song if (!script->hasSectionsCommand)
1482a0318711SFangrui Song return;
14837c426fb1SFangrui Song
1484383971d2SRafael Espindola // Orphan sections are sections present in the input files which are
1485383971d2SRafael Espindola // not explicitly placed into the output file by the linker script.
1486383971d2SRafael Espindola //
1487383971d2SRafael Espindola // The sections in the linker script are already in the correct
1488383971d2SRafael Espindola // order. We have to figuere out where to insert the orphan
1489383971d2SRafael Espindola // sections.
1490383971d2SRafael Espindola //
149124c073d1SRafael Espindola // The order of the sections in the script is arbitrary and may not agree with
1492383971d2SRafael Espindola // compareSections. This means that we cannot easily define a strict weak
1493383971d2SRafael Espindola // ordering. To see why, consider a comparison of a section in the script and
1494383971d2SRafael Espindola // one not in the script. We have a two simple options:
149524c073d1SRafael Espindola // * Make them equivalent (a is not less than b, and b is not less than a).
149624c073d1SRafael Espindola // The problem is then that equivalence has to be transitive and we can
149724c073d1SRafael Espindola // have sections a, b and c with only b in a script and a less than c
149824c073d1SRafael Espindola // which breaks this property.
149924c073d1SRafael Espindola // * Use compareSectionsNonScript. Given that the script order doesn't have
150024c073d1SRafael Espindola // to match, we can end up with sections a, b, c, d where b and c are in the
150124c073d1SRafael Espindola // script and c is compareSectionsNonScript less than b. In which case d
150224c073d1SRafael Espindola // can be equivalent to c, a to b and d < a. As a concrete example:
150324c073d1SRafael Espindola // .a (rx) # not in script
150424c073d1SRafael Espindola // .b (rx) # in script
150524c073d1SRafael Espindola // .c (ro) # in script
150624c073d1SRafael Espindola // .d (ro) # not in script
150724c073d1SRafael Espindola //
150824c073d1SRafael Espindola // The way we define an order then is:
1509383971d2SRafael Espindola // * Sort only the orphan sections. They are in the end right now.
1510383971d2SRafael Espindola // * Move each orphan section to its preferred position. We try
15119cd5df0eSFangrui Song // to put each section in the last position where it can share
1512bae1c656SEugene Leviant // a PT_LOAD.
1513383971d2SRafael Espindola //
1514383971d2SRafael Espindola // There is some ambiguity as to where exactly a new entry should be
1515ac27de9dSRui Ueyama // inserted, because Commands contains not only output section
1516383971d2SRafael Espindola // commands but also other types of commands such as symbol assignment
1517383971d2SRafael Espindola // expressions. There's no correct answer here due to the lack of the
1518383971d2SRafael Espindola // formal specification of the linker script. We use heuristics to
1519383971d2SRafael Espindola // determine whether a new output command should be added before or
1520383971d2SRafael Espindola // after another commands. For the details, look at shouldSkip
1521383971d2SRafael Espindola // function.
152224c073d1SRafael Espindola
15233837f427SRui Ueyama auto i = script->sectionCommands.begin();
15243837f427SRui Ueyama auto e = script->sectionCommands.end();
15257051aeefSFangrui Song auto nonScriptI = std::find_if(i, e, [](SectionCommand *cmd) {
15266c814931SFangrui Song if (auto *osd = dyn_cast<OutputDesc>(cmd))
15276c814931SFangrui Song return osd->osec.sectionIndex == UINT32_MAX;
1528383971d2SRafael Espindola return false;
1529383971d2SRafael Espindola });
153024c073d1SRafael Espindola
1531383971d2SRafael Espindola // Sort the orphan sections.
15323837f427SRui Ueyama std::stable_sort(nonScriptI, e, compareSections);
1533383971d2SRafael Espindola
1534383971d2SRafael Espindola // As a horrible special case, skip the first . assignment if it is before any
1535383971d2SRafael Espindola // section. We do this because it is common to set a load address by starting
1536383971d2SRafael Espindola // the script with ". = 0xabcd" and the expectation is that every section is
1537383971d2SRafael Espindola // after that.
15383837f427SRui Ueyama auto firstSectionOrDotAssignment =
15397051aeefSFangrui Song std::find_if(i, e, [](SectionCommand *cmd) { return !shouldSkip(cmd); });
15403837f427SRui Ueyama if (firstSectionOrDotAssignment != e &&
15413837f427SRui Ueyama isa<SymbolAssignment>(**firstSectionOrDotAssignment))
15423837f427SRui Ueyama ++firstSectionOrDotAssignment;
15433837f427SRui Ueyama i = firstSectionOrDotAssignment;
1544383971d2SRafael Espindola
15453837f427SRui Ueyama while (nonScriptI != e) {
15463837f427SRui Ueyama auto pos = findOrphanPos(i, nonScriptI);
15476c814931SFangrui Song OutputSection *orphan = &cast<OutputDesc>(*nonScriptI)->osec;
15485210141bSRafael Espindola
15495210141bSRafael Espindola // As an optimization, find all sections with the same sort rank
15505210141bSRafael Espindola // and insert them with one rotate.
15513837f427SRui Ueyama unsigned rank = orphan->sortRank;
15527051aeefSFangrui Song auto end = std::find_if(nonScriptI + 1, e, [=](SectionCommand *cmd) {
15536c814931SFangrui Song return cast<OutputDesc>(cmd)->osec.sortRank != rank;
15545210141bSRafael Espindola });
15553837f427SRui Ueyama std::rotate(pos, nonScriptI, end);
15563837f427SRui Ueyama nonScriptI = end;
15575210141bSRafael Espindola }
1558f7a17448SRafael Espindola
15593837f427SRui Ueyama script->adjustSectionsAfterSorting();
156024c073d1SRafael Espindola }
156124c073d1SRafael Espindola
compareByFilePosition(InputSection * a,InputSection * b)15623837f427SRui Ueyama static bool compareByFilePosition(InputSection *a, InputSection *b) {
1563e8a11c05SFangrui Song InputSection *la = a->flags & SHF_LINK_ORDER ? a->getLinkOrderDep() : nullptr;
1564e8a11c05SFangrui Song InputSection *lb = b->flags & SHF_LINK_ORDER ? b->getLinkOrderDep() : nullptr;
1565e8a11c05SFangrui Song // SHF_LINK_ORDER sections with non-zero sh_link are ordered before
1566e8a11c05SFangrui Song // non-SHF_LINK_ORDER sections and SHF_LINK_ORDER sections with zero sh_link.
1567b216c80cSFangrui Song if (!la || !lb)
1568b216c80cSFangrui Song return la && !lb;
15693837f427SRui Ueyama OutputSection *aOut = la->getParent();
15703837f427SRui Ueyama OutputSection *bOut = lb->getParent();
15714664280aSRui Ueyama
15723837f427SRui Ueyama if (aOut != bOut)
15733834385fSPeter Smith return aOut->addr < bOut->addr;
15743837f427SRui Ueyama return la->outSecOff < lb->outSecOff;
157503cbf468SPeter Smith }
157603cbf468SPeter Smith
resolveShfLinkOrder()157703cbf468SPeter Smith template <class ELFT> void Writer<ELFT>::resolveShfLinkOrder() {
1578439341b9SJames Henderson llvm::TimeTraceScope timeScope("Resolve SHF_LINK_ORDER");
15793837f427SRui Ueyama for (OutputSection *sec : outputSections) {
15803837f427SRui Ueyama if (!(sec->flags & SHF_LINK_ORDER))
158103cbf468SPeter Smith continue;
158203cbf468SPeter Smith
158326720514SFangrui Song // The ARM.exidx section use SHF_LINK_ORDER, but we have consolidated
158426720514SFangrui Song // this processing inside the ARMExidxsyntheticsection::finalizeContents().
158526720514SFangrui Song if (!config->relocatable && config->emachine == EM_ARM &&
158626720514SFangrui Song sec->type == SHT_ARM_EXIDX)
158726720514SFangrui Song continue;
158826720514SFangrui Song
1589e8a11c05SFangrui Song // Link order may be distributed across several InputSectionDescriptions.
1590e8a11c05SFangrui Song // Sorting is performed separately.
1591196aedb8SFangrui Song SmallVector<InputSection **, 0> scriptSections;
1592196aedb8SFangrui Song SmallVector<InputSection *, 0> sections;
15937051aeefSFangrui Song for (SectionCommand *cmd : sec->commands) {
15947051aeefSFangrui Song auto *isd = dyn_cast<InputSectionDescription>(cmd);
1595e8a11c05SFangrui Song if (!isd)
1596e8a11c05SFangrui Song continue;
1597e8a11c05SFangrui Song bool hasLinkOrder = false;
1598e8a11c05SFangrui Song scriptSections.clear();
1599e8a11c05SFangrui Song sections.clear();
16003837f427SRui Ueyama for (InputSection *&isec : isd->sections) {
1601e8a11c05SFangrui Song if (isec->flags & SHF_LINK_ORDER) {
160226720514SFangrui Song InputSection *link = isec->getLinkOrderDep();
1603b216c80cSFangrui Song if (link && !link->getParent())
160426720514SFangrui Song error(toString(isec) + ": sh_link points to discarded section " +
160526720514SFangrui Song toString(link));
1606e8a11c05SFangrui Song hasLinkOrder = true;
160703cbf468SPeter Smith }
1608e8a11c05SFangrui Song scriptSections.push_back(&isec);
1609e8a11c05SFangrui Song sections.push_back(isec);
161003cbf468SPeter Smith }
1611e8a11c05SFangrui Song if (hasLinkOrder && errorCount() == 0) {
16123837f427SRui Ueyama llvm::stable_sort(sections, compareByFilePosition);
1613e8a11c05SFangrui Song for (int i = 0, n = sections.size(); i != n; ++i)
16143837f427SRui Ueyama *scriptSections[i] = sections[i];
161503cbf468SPeter Smith }
161603cbf468SPeter Smith }
1617e8a11c05SFangrui Song }
1618e8a11c05SFangrui Song }
161903cbf468SPeter Smith
finalizeSynthetic(SyntheticSection * sec)16203b1622d6SPeter Smith static void finalizeSynthetic(SyntheticSection *sec) {
1621439341b9SJames Henderson if (sec && sec->isNeeded() && sec->getParent()) {
1622439341b9SJames Henderson llvm::TimeTraceScope timeScope("Finalize synthetic sections", sec->name);
16233b1622d6SPeter Smith sec->finalizeContents();
16243b1622d6SPeter Smith }
1625439341b9SJames Henderson }
16263b1622d6SPeter Smith
1627da49faf1SGeorge Rimar // We need to generate and finalize the content that depends on the address of
1628da49faf1SGeorge Rimar // InputSections. As the generation of the content may also alter InputSection
1629da49faf1SGeorge Rimar // addresses we must converge to a fixed point. We do that here. See the comment
1630da49faf1SGeorge Rimar // in Writer<ELFT>::finalizeSections().
finalizeAddressDependentContent()1631da49faf1SGeorge Rimar template <class ELFT> void Writer<ELFT>::finalizeAddressDependentContent() {
1632439341b9SJames Henderson llvm::TimeTraceScope timeScope("Finalize address dependent content");
16333837f427SRui Ueyama ThunkCreator tc;
16343837f427SRui Ueyama AArch64Err843419Patcher a64p;
1635ea99ce5eSPeter Smith ARMErr657417Patcher a32p;
16363837f427SRui Ueyama script->assignAddresses();
16373834385fSPeter Smith // .ARM.exidx and SHF_LINK_ORDER do not require precise addresses, but they
16383834385fSPeter Smith // do require the relative addresses of OutputSections because linker scripts
16393834385fSPeter Smith // can assign Virtual Addresses to OutputSections that are not monotonically
16403834385fSPeter Smith // increasing.
16413b1622d6SPeter Smith for (Partition &part : partitions)
1642cb203f3fSFangrui Song finalizeSynthetic(part.armExidx.get());
16433834385fSPeter Smith resolveShfLinkOrder();
16445a30177eSRui Ueyama
16455a5a075cSSid Manning // Converts call x@GDPLT to call __tls_get_addr
16465a5a075cSSid Manning if (config->emachine == EM_HEXAGON)
16475a5a075cSSid Manning hexagonTLSSymbolUpdate(outputSections);
16485a5a075cSSid Manning
16496611d58fSFangrui Song uint32_t pass = 0, assignPasses = 0;
1650debcac9fSFangrui Song for (;;) {
16516611d58fSFangrui Song bool changed = target->needsThunks ? tc.createThunks(pass, outputSections)
16526611d58fSFangrui Song : target->relaxOnce(pass);
16536611d58fSFangrui Song ++pass;
1654debcac9fSFangrui Song
1655debcac9fSFangrui Song // With Thunk Size much smaller than branch range we expect to
165615f0ad2fSFangrui Song // converge quickly; if we get to 15 something has gone wrong.
16576611d58fSFangrui Song if (changed && pass >= 15) {
16586611d58fSFangrui Song error(target->needsThunks ? "thunk creation not converged"
16596611d58fSFangrui Song : "relaxation not converged");
1660debcac9fSFangrui Song break;
1661debcac9fSFangrui Song }
16625a30177eSRui Ueyama
16633837f427SRui Ueyama if (config->fixCortexA53Errata843419) {
16643837f427SRui Ueyama if (changed)
16653837f427SRui Ueyama script->assignAddresses();
16663837f427SRui Ueyama changed |= a64p.createFixes();
16675a30177eSRui Ueyama }
1668ea99ce5eSPeter Smith if (config->fixCortexA8) {
1669ea99ce5eSPeter Smith if (changed)
1670ea99ce5eSPeter Smith script->assignAddresses();
1671ea99ce5eSPeter Smith changed |= a32p.createFixes();
1672ea99ce5eSPeter Smith }
16735a30177eSRui Ueyama
16743837f427SRui Ueyama if (in.mipsGot)
16753837f427SRui Ueyama in.mipsGot->updateAllocSize();
16765a30177eSRui Ueyama
16773837f427SRui Ueyama for (Partition &part : partitions) {
16783837f427SRui Ueyama changed |= part.relaDyn->updateAllocSize();
16793837f427SRui Ueyama if (part.relrDyn)
16803837f427SRui Ueyama changed |= part.relrDyn->updateAllocSize();
168102828985SPeter Collingbourne }
16825a30177eSRui Ueyama
1683debcac9fSFangrui Song const Defined *changedSym = script->assignAddresses();
1684debcac9fSFangrui Song if (!changed) {
1685debcac9fSFangrui Song // Some symbols may be dependent on section addresses. When we break the
1686debcac9fSFangrui Song // loop, the symbol values are finalized because a previous
1687debcac9fSFangrui Song // assignAddresses() finalized section addresses.
1688debcac9fSFangrui Song if (!changedSym)
1689debcac9fSFangrui Song break;
1690debcac9fSFangrui Song if (++assignPasses == 5) {
1691debcac9fSFangrui Song errorOrWarn("assignment to symbol " + toString(*changedSym) +
1692debcac9fSFangrui Song " does not converge");
1693debcac9fSFangrui Song break;
1694debcac9fSFangrui Song }
1695debcac9fSFangrui Song }
16965a30177eSRui Ueyama }
16976611d58fSFangrui Song if (!config->relocatable && config->emachine == EM_RISCV)
16986611d58fSFangrui Song riscvFinalizeRelax(pass);
1699de0dda54SFangrui Song
1700e590c9bcSFangrui Song if (config->relocatable)
1701e590c9bcSFangrui Song for (OutputSection *sec : outputSections)
1702e590c9bcSFangrui Song sec->addr = 0;
1703e590c9bcSFangrui Song
1704fbf41b52SFangrui Song // If addrExpr is set, the address may not be a multiple of the alignment.
1705fbf41b52SFangrui Song // Warn because this is error-prone.
17067051aeefSFangrui Song for (SectionCommand *cmd : script->sectionCommands)
17076c814931SFangrui Song if (auto *osd = dyn_cast<OutputDesc>(cmd)) {
17086c814931SFangrui Song OutputSection *osec = &osd->osec;
17096c814931SFangrui Song if (osec->addr % osec->alignment != 0)
17106c814931SFangrui Song warn("address (0x" + Twine::utohexstr(osec->addr) + ") of section " +
17116c814931SFangrui Song osec->name + " is not a multiple of alignment (" +
17126c814931SFangrui Song Twine(osec->alignment) + ")");
17136c814931SFangrui Song }
17145a30177eSRui Ueyama }
17155a30177eSRui Ueyama
1716cb4df6ebSNico Weber // If Input Sections have been shrunk (basic block sections) then
171794317878SSriraman Tallam // update symbol values and sizes associated with these sections. With basic
171894317878SSriraman Tallam // block sections, input sections can shrink when the jump instructions at
171994317878SSriraman Tallam // the end of the section are relaxed.
fixSymbolsAfterShrinking()172094317878SSriraman Tallam static void fixSymbolsAfterShrinking() {
17219a572164SFangrui Song for (InputFile *File : ctx->objectFiles) {
172294317878SSriraman Tallam parallelForEach(File->getSymbols(), [&](Symbol *Sym) {
172394317878SSriraman Tallam auto *def = dyn_cast<Defined>(Sym);
172494317878SSriraman Tallam if (!def)
172594317878SSriraman Tallam return;
172694317878SSriraman Tallam
172794317878SSriraman Tallam const SectionBase *sec = def->section;
172894317878SSriraman Tallam if (!sec)
172994317878SSriraman Tallam return;
173094317878SSriraman Tallam
1731e1b6b5beSFangrui Song const InputSectionBase *inputSec = dyn_cast<InputSectionBase>(sec);
173294317878SSriraman Tallam if (!inputSec || !inputSec->bytesDropped)
173394317878SSriraman Tallam return;
173494317878SSriraman Tallam
1735ae1ba619SFangrui Song const size_t OldSize = inputSec->rawData.size();
173694317878SSriraman Tallam const size_t NewSize = OldSize - inputSec->bytesDropped;
173794317878SSriraman Tallam
173894317878SSriraman Tallam if (def->value > NewSize && def->value <= OldSize) {
173994317878SSriraman Tallam LLVM_DEBUG(llvm::dbgs()
174094317878SSriraman Tallam << "Moving symbol " << Sym->getName() << " from "
174194317878SSriraman Tallam << def->value << " to "
174294317878SSriraman Tallam << def->value - inputSec->bytesDropped << " bytes\n");
174394317878SSriraman Tallam def->value -= inputSec->bytesDropped;
174494317878SSriraman Tallam return;
174594317878SSriraman Tallam }
174694317878SSriraman Tallam
174794317878SSriraman Tallam if (def->value + def->size > NewSize && def->value <= OldSize &&
174894317878SSriraman Tallam def->value + def->size <= OldSize) {
174994317878SSriraman Tallam LLVM_DEBUG(llvm::dbgs()
175094317878SSriraman Tallam << "Shrinking symbol " << Sym->getName() << " from "
175194317878SSriraman Tallam << def->size << " to " << def->size - inputSec->bytesDropped
175294317878SSriraman Tallam << " bytes\n");
175394317878SSriraman Tallam def->size -= inputSec->bytesDropped;
175494317878SSriraman Tallam }
175594317878SSriraman Tallam });
175694317878SSriraman Tallam }
175794317878SSriraman Tallam }
175894317878SSriraman Tallam
175994317878SSriraman Tallam // If basic block sections exist, there are opportunities to delete fall thru
176094317878SSriraman Tallam // jumps and shrink jump instructions after basic block reordering. This
176194317878SSriraman Tallam // relaxation pass does that. It is only enabled when --optimize-bb-jumps
176294317878SSriraman Tallam // option is used.
optimizeBasicBlockJumps()176394317878SSriraman Tallam template <class ELFT> void Writer<ELFT>::optimizeBasicBlockJumps() {
176494317878SSriraman Tallam assert(config->optimizeBBJumps);
1765e0612c91SFangrui Song SmallVector<InputSection *, 0> storage;
176694317878SSriraman Tallam
176794317878SSriraman Tallam script->assignAddresses();
176894317878SSriraman Tallam // For every output section that has executable input sections, this
176994317878SSriraman Tallam // does the following:
177094317878SSriraman Tallam // 1. Deletes all direct jump instructions in input sections that
177194317878SSriraman Tallam // jump to the following section as it is not required.
177294317878SSriraman Tallam // 2. If there are two consecutive jump instructions, it checks
177394317878SSriraman Tallam // if they can be flipped and one can be deleted.
1774a1c2ee01SFangrui Song for (OutputSection *osec : outputSections) {
1775a1c2ee01SFangrui Song if (!(osec->flags & SHF_EXECINSTR))
177694317878SSriraman Tallam continue;
1777e0612c91SFangrui Song ArrayRef<InputSection *> sections = getInputSections(*osec, storage);
17781372d536SFangrui Song size_t numDeleted = 0;
177994317878SSriraman Tallam // Delete all fall through jump instructions. Also, check if two
178094317878SSriraman Tallam // consecutive jump instructions can be flipped so that a fall
178194317878SSriraman Tallam // through jmp instruction can be deleted.
1782b07292f7SFangrui Song for (size_t i = 0, e = sections.size(); i != e; ++i) {
178394317878SSriraman Tallam InputSection *next = i + 1 < sections.size() ? sections[i + 1] : nullptr;
1784b07292f7SFangrui Song InputSection &sec = *sections[i];
17851372d536SFangrui Song numDeleted += target->deleteFallThruJmpInsn(sec, sec.file, next);
1786b07292f7SFangrui Song }
178794317878SSriraman Tallam if (numDeleted > 0) {
178894317878SSriraman Tallam script->assignAddresses();
178994317878SSriraman Tallam LLVM_DEBUG(llvm::dbgs()
179094317878SSriraman Tallam << "Removing " << numDeleted << " fall through jumps\n");
179194317878SSriraman Tallam }
179294317878SSriraman Tallam }
179394317878SSriraman Tallam
179494317878SSriraman Tallam fixSymbolsAfterShrinking();
179594317878SSriraman Tallam
1796a1c2ee01SFangrui Song for (OutputSection *osec : outputSections)
1797e0612c91SFangrui Song for (InputSection *is : getInputSections(*osec, storage))
179894317878SSriraman Tallam is->trim();
179994317878SSriraman Tallam }
180094317878SSriraman Tallam
18016aeea183SRui Ueyama // In order to allow users to manipulate linker-synthesized sections,
18026aeea183SRui Ueyama // we had to add synthetic sections to the input section list early,
18036aeea183SRui Ueyama // even before we make decisions whether they are needed. This allows
18046aeea183SRui Ueyama // users to write scripts like this: ".mygot : { .got }".
18056aeea183SRui Ueyama //
18066aeea183SRui Ueyama // Doing it has an unintended side effects. If it turns out that we
18076aeea183SRui Ueyama // don't need a .got (for example) at all because there's no
18086aeea183SRui Ueyama // relocation that needs a .got, we don't want to emit .got.
18096aeea183SRui Ueyama //
18106aeea183SRui Ueyama // To deal with the above problem, this function is called after
18116aeea183SRui Ueyama // scanRelocations is called to remove synthetic sections that turn
18126aeea183SRui Ueyama // out to be empty.
removeUnusedSyntheticSections()1813c080ff64SRafael Espindola static void removeUnusedSyntheticSections() {
18149e9754b5SRafael Espindola // All input synthetic sections that can be empty are placed after
1815b9cf1769SAmilendra Kodithuwakku // all regular ones. Reverse iterate to find the first synthetic section
1816b9cf1769SAmilendra Kodithuwakku // after a non-synthetic one which will be our starting point.
1817b9cf1769SAmilendra Kodithuwakku auto start = std::find_if(inputSections.rbegin(), inputSections.rend(),
1818b9cf1769SAmilendra Kodithuwakku [](InputSectionBase *s) {
1819b9cf1769SAmilendra Kodithuwakku return !isa<SyntheticSection>(s);
1820b9cf1769SAmilendra Kodithuwakku })
1821b9cf1769SAmilendra Kodithuwakku .base();
1822b9cf1769SAmilendra Kodithuwakku
18234709bacfSFangrui Song // Remove unused synthetic sections from inputSections;
18244709bacfSFangrui Song DenseSet<InputSectionBase *> unused;
18254709bacfSFangrui Song auto end =
18264709bacfSFangrui Song std::remove_if(start, inputSections.end(), [&](InputSectionBase *s) {
182799a2d940SFangrui Song auto *sec = cast<SyntheticSection>(s);
18284709bacfSFangrui Song if (sec->getParent() && sec->isNeeded())
18294709bacfSFangrui Song return false;
18304709bacfSFangrui Song unused.insert(sec);
18314709bacfSFangrui Song return true;
1832b9cf1769SAmilendra Kodithuwakku });
18334709bacfSFangrui Song inputSections.erase(end, inputSections.end());
1834b9cf1769SAmilendra Kodithuwakku
183599a2d940SFangrui Song // Remove unused synthetic sections from the corresponding input section
183699a2d940SFangrui Song // description and orphanSections.
18374709bacfSFangrui Song for (auto *sec : unused)
18384709bacfSFangrui Song if (OutputSection *osec = cast<SyntheticSection>(sec)->getParent())
183999a2d940SFangrui Song for (SectionCommand *cmd : osec->commands)
184099a2d940SFangrui Song if (auto *isd = dyn_cast<InputSectionDescription>(cmd))
184199a2d940SFangrui Song llvm::erase_if(isd->sections, [&](InputSection *isec) {
184299a2d940SFangrui Song return unused.count(isec);
184399a2d940SFangrui Song });
184499a2d940SFangrui Song llvm::erase_if(script->orphanSections, [&](const InputSectionBase *sec) {
184599a2d940SFangrui Song return unused.count(sec);
184699a2d940SFangrui Song });
184711992c86SGeorge Rimar }
184811992c86SGeorge Rimar
1849e63d81bdSEugene Leviant // Create output section objects and add them to OutputSections.
finalizeSections()1850e63d81bdSEugene Leviant template <class ELFT> void Writer<ELFT>::finalizeSections() {
18513837f427SRui Ueyama Out::preinitArray = findSection(".preinit_array");
18523837f427SRui Ueyama Out::initArray = findSection(".init_array");
18533837f427SRui Ueyama Out::finiArray = findSection(".fini_array");
185477572244SRafael Espindola
1855a5d79d16SRui Ueyama // The linker needs to define SECNAME_start, SECNAME_end and SECNAME_stop
1856a5d79d16SRui Ueyama // symbols for sections, so that the runtime can get the start and end
1857a5d79d16SRui Ueyama // addresses of each section by section name. Add such symbols.
18583837f427SRui Ueyama if (!config->relocatable) {
1859a5d79d16SRui Ueyama addStartEndSymbols();
18607051aeefSFangrui Song for (SectionCommand *cmd : script->sectionCommands)
18616c814931SFangrui Song if (auto *osd = dyn_cast<OutputDesc>(cmd))
18626c814931SFangrui Song addStartStopSymbols(osd->osec);
1863c1034a85SGeorge Rimar }
1864d4530c6eSRui Ueyama
1865d4530c6eSRui Ueyama // Add _DYNAMIC symbol. Unlike GNU gold, our _DYNAMIC symbol has no type.
1866d4530c6eSRui Ueyama // It should be okay as no one seems to care about the type.
1867d4530c6eSRui Ueyama // Even the author of gold doesn't remember why gold behaves that way.
1868d4530c6eSRui Ueyama // https://sourceware.org/ml/binutils/2002-03/msg00360.html
18693837f427SRui Ueyama if (mainPart->dynamic->parent)
18708ca46bbaSFangrui Song symtab->addSymbol(Defined{/*file=*/nullptr, "_DYNAMIC", STB_WEAK, STV_HIDDEN, STT_NOTYPE,
18718ca46bbaSFangrui Song /*value=*/0, /*size=*/0, mainPart->dynamic.get()})->isUsedInRegularObj = true;
1872334c3e11SRafael Espindola
1873de9857e3SRafael Espindola // Define __rel[a]_iplt_{start,end} symbols if needed.
1874de9857e3SRafael Espindola addRelIpltSymbols();
1875de9857e3SRafael Espindola
18768fbe81fbSFangrui Song // RISC-V's gp can address +/- 2 KiB, set it to .sdata + 0x800. This symbol
18778fbe81fbSFangrui Song // should only be defined in an executable. If .sdata does not exist, its
18788fbe81fbSFangrui Song // value/section does not matter but it has to be relative, so set its
18798fbe81fbSFangrui Song // st_shndx arbitrarily to 1 (Out::elfHeader).
18808fbe81fbSFangrui Song if (config->emachine == EM_RISCV && !config->shared) {
18818fbe81fbSFangrui Song OutputSection *sec = findSection(".sdata");
18823837f427SRui Ueyama ElfSym::riscvGlobalPointer =
18838fbe81fbSFangrui Song addOptionalRegular("__global_pointer$", sec ? sec : Out::elfHeader,
1884db6a00daSFangrui Song 0x800, STV_DEFAULT);
18858fbe81fbSFangrui Song }
18865cd9c6bcSRui Ueyama
1887e39c138fSFangrui Song if (config->emachine == EM_386 || config->emachine == EM_X86_64) {
18880526c0cdSFangrui Song // On targets that support TLSDESC, _TLS_MODULE_BASE_ is defined in such a
18890526c0cdSFangrui Song // way that:
18900526c0cdSFangrui Song //
18910526c0cdSFangrui Song // 1) Without relaxation: it produces a dynamic TLSDESC relocation that
18920526c0cdSFangrui Song // computes 0.
18930526c0cdSFangrui Song // 2) With LD->LE relaxation: _TLS_MODULE_BASE_@tpoff = 0 (lowest address in
18940526c0cdSFangrui Song // the TLS block).
18950526c0cdSFangrui Song //
18960526c0cdSFangrui Song // 2) is special cased in @tpoff computation. To satisfy 1), we define it as
18970526c0cdSFangrui Song // an absolute symbol of zero. This is different from GNU linkers which
18980526c0cdSFangrui Song // define _TLS_MODULE_BASE_ relative to the first TLS section.
18993837f427SRui Ueyama Symbol *s = symtab->find("_TLS_MODULE_BASE_");
19003837f427SRui Ueyama if (s && s->isUndefined()) {
1901977a1a52SFangrui Song s->resolve(Defined{/*file=*/nullptr, StringRef(), STB_GLOBAL, STV_HIDDEN,
1902bfaf64aeSRui Ueyama STT_TLS, /*value=*/0, 0,
1903bfaf64aeSRui Ueyama /*section=*/nullptr});
19043837f427SRui Ueyama ElfSym::tlsModuleBase = cast<Defined>(s);
19050526c0cdSFangrui Song }
19060526c0cdSFangrui Song }
19070526c0cdSFangrui Song
1908439341b9SJames Henderson {
1909439341b9SJames Henderson llvm::TimeTraceScope timeScope("Finalize .eh_frame");
191066b4e215SRafael Espindola // This responsible for splitting up .eh_frame section into
19111ec42d9aSPeter Smith // pieces. The relocation scan uses those pieces, so this has to be
191266b4e215SRafael Espindola // earlier.
19133837f427SRui Ueyama for (Partition &part : partitions)
1914cb203f3fSFangrui Song finalizeSynthetic(part.ehFrame.get());
1915439341b9SJames Henderson }
191656004c57SRafael Espindola
19173bc15276SFangrui Song if (config->hasDynSymTab) {
19183bc15276SFangrui Song parallelForEach(symtab->symbols(), [](Symbol *sym) {
1919a2fc9644SFangrui Song sym->isPreemptible = computeIsPreemptible(*sym);
19203bc15276SFangrui Song });
19213bc15276SFangrui Song }
192235c908f2SRafael Espindola
19235d9f419aSFangrui Song // Change values of linker-script-defined symbols from placeholders (assigned
19245d9f419aSFangrui Song // by declareSymbols) to actual definitions.
19255d9f419aSFangrui Song script->processSymbolAssignments();
19265d9f419aSFangrui Song
1927439341b9SJames Henderson {
1928439341b9SJames Henderson llvm::TimeTraceScope timeScope("Scan relocations");
1929439341b9SJames Henderson // Scan relocations. This must be done after every symbol is declared so
1930439341b9SJames Henderson // that we can correctly decide if a dynamic relocation is needed. This is
1931439341b9SJames Henderson // called after processSymbolAssignments() because it needs to know whether
1932439341b9SJames Henderson // a linker-script-defined symbol is absolute.
1933b257d3c8SFangrui Song ppc64noTocRelax.clear();
19343837f427SRui Ueyama if (!config->relocatable) {
1935e7c8cd4aSFangrui Song // Scan all relocations. Each relocation goes through a series of tests to
1936e7c8cd4aSFangrui Song // determine if it needs special treatment, such as creating GOT, PLT,
1937e7c8cd4aSFangrui Song // copy relocations, etc. Note that relocations for non-alloc sections are
1938e7c8cd4aSFangrui Song // directly processed by InputSection::relocateNonAlloc.
1939e7c8cd4aSFangrui Song for (InputSectionBase *sec : inputSections)
1940e7c8cd4aSFangrui Song if (sec->isLive() && isa<InputSection>(sec) && (sec->flags & SHF_ALLOC))
1941e7c8cd4aSFangrui Song scanRelocations<ELFT>(*sec);
1942e7c8cd4aSFangrui Song for (Partition &part : partitions) {
1943e7c8cd4aSFangrui Song for (EhInputSection *sec : part.ehFrame->sections)
1944e7c8cd4aSFangrui Song scanRelocations<ELFT>(*sec);
1945e7c8cd4aSFangrui Song if (part.armExidx && part.armExidx->isLive())
1946e7c8cd4aSFangrui Song for (InputSection *sec : part.armExidx->exidxSections)
1947e7c8cd4aSFangrui Song scanRelocations<ELFT>(*sec);
1948e7c8cd4aSFangrui Song }
1949e7c8cd4aSFangrui Song
19509af90e20SFangrui Song reportUndefinedSymbols();
1951cf783be8SFangrui Song postScanRelocations();
19522c450434SNico Weber }
1953439341b9SJames Henderson }
19540f7cedaaSRafael Espindola
19553837f427SRui Ueyama if (in.plt && in.plt->isNeeded())
19563837f427SRui Ueyama in.plt->addSymbols();
19573837f427SRui Ueyama if (in.iplt && in.iplt->isNeeded())
19583837f427SRui Ueyama in.iplt->addSymbols();
19599694376aSPeter Smith
196055d310adSFangrui Song if (config->unresolvedSymbolsInShlib != UnresolvedPolicy::Ignore) {
196170c23e23SIgor Kudrin auto diagnose =
196270c23e23SIgor Kudrin config->unresolvedSymbolsInShlib == UnresolvedPolicy::ReportError
196370c23e23SIgor Kudrin ? errorOrWarn
196470c23e23SIgor Kudrin : warn;
1965b4744d30SFangrui Song // Error on undefined symbols in a shared object, if all of its DT_NEEDED
19665976a3f5SNico Weber // entries are seen. These cases would otherwise lead to runtime errors
1967b4744d30SFangrui Song // reported by the dynamic linker.
1968b4744d30SFangrui Song //
1969b4744d30SFangrui Song // ld.bfd traces all DT_NEEDED to emulate the logic of the dynamic linker to
1970b4744d30SFangrui Song // catch more cases. That is too much for us. Our approach resembles the one
1971b4744d30SFangrui Song // used in ld.gold, achieves a good balance to be useful but not too smart.
19729a572164SFangrui Song for (SharedFile *file : ctx->sharedFiles) {
197370c23e23SIgor Kudrin bool allNeededIsKnown =
19743837f427SRui Ueyama llvm::all_of(file->dtNeeded, [&](StringRef needed) {
1975769057a5SFangrui Song return symtab->soNames.count(CachedHashStringRef(needed));
1976b4744d30SFangrui Song });
197770c23e23SIgor Kudrin if (!allNeededIsKnown)
197870c23e23SIgor Kudrin continue;
197970c23e23SIgor Kudrin for (Symbol *sym : file->requiredSymbols)
19803837f427SRui Ueyama if (sym->isUndefined() && !sym->isWeak())
198151b9e099SFangrui Song diagnose("undefined reference due to --no-allow-shlib-undefined: " +
198251b9e099SFangrui Song toString(*sym) + "\n>>> referenced by " + toString(file));
1983b4744d30SFangrui Song }
198455d310adSFangrui Song }
1985b4744d30SFangrui Song
1986439341b9SJames Henderson {
1987439341b9SJames Henderson llvm::TimeTraceScope timeScope("Add symbols to symtabs");
198855865432SPeter Smith // Now that we have defined all possible global symbols including linker-
19891b2a8bf6SRui Ueyama // synthesized ones. Visit all symbols to give the finishing touches.
1990a2fc9644SFangrui Song for (Symbol *sym : symtab->symbols()) {
19915d0be553SFangrui Song if (!sym->isUsedInRegularObj || !includeInSymtab(*sym))
1992a2fc9644SFangrui Song continue;
1993c0fc09abSFangrui Song if (!config->relocatable)
1994aabe901dSFangrui Song sym->binding = sym->computeBinding();
19953837f427SRui Ueyama if (in.symTab)
19963837f427SRui Ueyama in.symTab->addSymbol(sym);
199705a3dd2cSRafael Espindola
19983837f427SRui Ueyama if (sym->includeInDynsym()) {
19993837f427SRui Ueyama partitions[sym->partition - 1].dynSymTab->addSymbol(sym);
20003837f427SRui Ueyama if (auto *file = dyn_cast_or_null<SharedFile>(sym->file))
20013837f427SRui Ueyama if (file->isNeeded && !sym->isUndefined())
20023837f427SRui Ueyama addVerneed(sym);
200321a12fc6SPeter Collingbourne }
2004a2fc9644SFangrui Song }
2005c2a0d7e3SRui Ueyama
2006439341b9SJames Henderson // We also need to scan the dynamic relocation tables of the other
2007439341b9SJames Henderson // partitions and add any referenced symbols to the partition's dynsym.
20083837f427SRui Ueyama for (Partition &part : MutableArrayRef<Partition>(partitions).slice(1)) {
20093837f427SRui Ueyama DenseSet<Symbol *> syms;
20103837f427SRui Ueyama for (const SymbolTableEntry &e : part.dynSymTab->getSymbols())
20113837f427SRui Ueyama syms.insert(e.sym);
20123837f427SRui Ueyama for (DynamicReloc &reloc : part.relaDyn->relocs)
20136d87ca08SAlex Richardson if (reloc.sym && reloc.needsDynSymIndex() &&
20146d87ca08SAlex Richardson syms.insert(reloc.sym).second)
20153837f427SRui Ueyama part.dynSymTab->addSymbol(reloc.sym);
201602828985SPeter Collingbourne }
2017439341b9SJames Henderson }
201802828985SPeter Collingbourne
20193837f427SRui Ueyama if (in.mipsGot)
20203837f427SRui Ueyama in.mipsGot->build();
2021ed9ee69cSSimon Atanasyan
2022c080ff64SRafael Espindola removeUnusedSyntheticSections();
202342319409SFangrui Song script->diagnoseOrphanHandling();
2024c080ff64SRafael Espindola
202524c073d1SRafael Espindola sortSections();
2026383971d2SRafael Espindola
202725c7ec4fSFangrui Song // Create a list of OutputSections, assign sectionIndex, and populate
202825c7ec4fSFangrui Song // in.shStrTab.
20297051aeefSFangrui Song for (SectionCommand *cmd : script->sectionCommands)
20306c814931SFangrui Song if (auto *osd = dyn_cast<OutputDesc>(cmd)) {
20316c814931SFangrui Song OutputSection *osec = &osd->osec;
203225c7ec4fSFangrui Song outputSections.push_back(osec);
203325c7ec4fSFangrui Song osec->sectionIndex = outputSections.size();
203425c7ec4fSFangrui Song osec->shName = in.shStrTab->addString(osec->name);
203525c7ec4fSFangrui Song }
203684417f83SRui Ueyama
20379c0395e3SRafael Espindola // Prefer command line supplied address over other constraints.
20383837f427SRui Ueyama for (OutputSection *sec : outputSections) {
20393837f427SRui Ueyama auto i = config->sectionStartMap.find(sec->name);
20403837f427SRui Ueyama if (i != config->sectionStartMap.end())
20413837f427SRui Ueyama sec->addrExpr = [=] { return i->second; };
20429c0395e3SRafael Espindola }
20439c0395e3SRafael Espindola
20445a5a075cSSid Manning // With the outputSections available check for GDPLT relocations
20455a5a075cSSid Manning // and add __tls_get_addr symbol if needed.
20465a5a075cSSid Manning if (config->emachine == EM_HEXAGON && hexagonNeedsTLSSymbol(outputSections)) {
20475a5a075cSSid Manning Symbol *sym = symtab->addSymbol(Undefined{
20485a5a075cSSid Manning nullptr, "__tls_get_addr", STB_GLOBAL, STV_DEFAULT, STT_NOTYPE});
20495a5a075cSSid Manning sym->isPreemptible = true;
20505a5a075cSSid Manning partitions[0].dynSymTab->addSymbol(sym);
20515a5a075cSSid Manning }
20525a5a075cSSid Manning
205378493a25SRafael Espindola // This is a bit of a hack. A value of 0 means undef, so we set it
20541f3e2b29SFangrui Song // to 1 to make __ehdr_start defined. The section number is not
205578493a25SRafael Espindola // particularly relevant.
20563837f427SRui Ueyama Out::elfHeader->sectionIndex = 1;
2057d060cc1fSFangrui Song Out::elfHeader->size = sizeof(typename ELFT::Ehdr);
205878493a25SRafael Espindola
20595967c973SRafael Espindola // Binary and relocatable output does not have PHDRS.
20605967c973SRafael Espindola // The headers have to be created before finalize as that can influence the
20615967c973SRafael Espindola // image base and the dynamic section on mips includes the image base.
20623837f427SRui Ueyama if (!config->relocatable && !config->oFormatBinary) {
20633837f427SRui Ueyama for (Partition &part : partitions) {
20643837f427SRui Ueyama part.phdrs = script->hasPhdrsCommands() ? script->createPhdrs()
20653837f427SRui Ueyama : createPhdrs(part);
20663837f427SRui Ueyama if (config->emachine == EM_ARM) {
206714b09810SSimon Atanasyan // PT_ARM_EXIDX is the ARM EHABI equivalent of PT_GNU_EH_FRAME
20683837f427SRui Ueyama addPhdrForSection(part, SHT_ARM_EXIDX, PT_ARM_EXIDX, PF_R);
206914b09810SSimon Atanasyan }
20703837f427SRui Ueyama if (config->emachine == EM_MIPS) {
207114b09810SSimon Atanasyan // Add separate segments for MIPS-specific sections.
20723837f427SRui Ueyama addPhdrForSection(part, SHT_MIPS_REGINFO, PT_MIPS_REGINFO, PF_R);
20733837f427SRui Ueyama addPhdrForSection(part, SHT_MIPS_OPTIONS, PT_MIPS_OPTIONS, PF_R);
20743837f427SRui Ueyama addPhdrForSection(part, SHT_MIPS_ABIFLAGS, PT_MIPS_ABIFLAGS, PF_R);
207514b09810SSimon Atanasyan }
207602828985SPeter Collingbourne }
20773837f427SRui Ueyama Out::programHeaders->size = sizeof(Elf_Phdr) * mainPart->phdrs.size();
20781c33d14bSRyan Prichard
20791c33d14bSRyan Prichard // Find the TLS segment. This happens before the section layout loop so that
208002828985SPeter Collingbourne // Android relocation packing can look up TLS symbol addresses. We only need
208102828985SPeter Collingbourne // to care about the main partition here because all TLS symbols were moved
208202828985SPeter Collingbourne // to the main partition (see MarkLive.cpp).
20833837f427SRui Ueyama for (PhdrEntry *p : mainPart->phdrs)
20843837f427SRui Ueyama if (p->p_type == PT_TLS)
20853837f427SRui Ueyama Out::tlsPhdr = p;
20865967c973SRafael Espindola }
20875967c973SRafael Espindola
20884d560160SRafael Espindola // Some symbols are defined in term of program headers. Now that we
20894d560160SRafael Espindola // have the headers, we can find out which sections they point to.
20904d560160SRafael Espindola setReservedSymbolSections();
20914d560160SRafael Espindola
2092439341b9SJames Henderson {
2093439341b9SJames Henderson llvm::TimeTraceScope timeScope("Finalize synthetic sections");
2094439341b9SJames Henderson
2095cb203f3fSFangrui Song finalizeSynthetic(in.bss.get());
2096cb203f3fSFangrui Song finalizeSynthetic(in.bssRelRo.get());
2097cb203f3fSFangrui Song finalizeSynthetic(in.symTabShndx.get());
2098cb203f3fSFangrui Song finalizeSynthetic(in.shStrTab.get());
2099cb203f3fSFangrui Song finalizeSynthetic(in.strTab.get());
2100cb203f3fSFangrui Song finalizeSynthetic(in.got.get());
2101cb203f3fSFangrui Song finalizeSynthetic(in.mipsGot.get());
2102cb203f3fSFangrui Song finalizeSynthetic(in.igotPlt.get());
2103cb203f3fSFangrui Song finalizeSynthetic(in.gotPlt.get());
2104cb203f3fSFangrui Song finalizeSynthetic(in.relaIplt.get());
2105cb203f3fSFangrui Song finalizeSynthetic(in.relaPlt.get());
2106cb203f3fSFangrui Song finalizeSynthetic(in.plt.get());
2107cb203f3fSFangrui Song finalizeSynthetic(in.iplt.get());
2108cb203f3fSFangrui Song finalizeSynthetic(in.ppc32Got2.get());
2109cb203f3fSFangrui Song finalizeSynthetic(in.partIndex.get());
211002828985SPeter Collingbourne
211102828985SPeter Collingbourne // Dynamic section must be the last one in this list and dynamic
211247cfe8f3SFangrui Song // symbol table section (dynSymTab) must be the first one.
21133837f427SRui Ueyama for (Partition &part : partitions) {
2114da0e5b88SFangrui Song if (part.relaDyn) {
2115da0e5b88SFangrui Song // Compute DT_RELACOUNT to be used by part.dynamic.
2116da0e5b88SFangrui Song part.relaDyn->partitionRels();
2117da0e5b88SFangrui Song finalizeSynthetic(part.relaDyn.get());
2118da0e5b88SFangrui Song }
2119da0e5b88SFangrui Song
2120cb203f3fSFangrui Song finalizeSynthetic(part.dynSymTab.get());
2121a5249c2dSFangrui Song finalizeSynthetic(part.gnuHashTab.get());
2122a5249c2dSFangrui Song finalizeSynthetic(part.hashTab.get());
2123cb203f3fSFangrui Song finalizeSynthetic(part.verDef.get());
2124cb203f3fSFangrui Song finalizeSynthetic(part.relrDyn.get());
2125cb203f3fSFangrui Song finalizeSynthetic(part.ehFrameHdr.get());
2126cb203f3fSFangrui Song finalizeSynthetic(part.verSym.get());
2127cb203f3fSFangrui Song finalizeSynthetic(part.verNeed.get());
2128cb203f3fSFangrui Song finalizeSynthetic(part.dynamic.get());
212902828985SPeter Collingbourne }
2130439341b9SJames Henderson }
21311ec42d9aSPeter Smith
21323837f427SRui Ueyama if (!script->hasSectionsCommand && !config->relocatable)
21333ef89b0fSPeter Smith fixSectionAlignments();
21343ef89b0fSPeter Smith
2135da49faf1SGeorge Rimar // This is used to:
2136da49faf1SGeorge Rimar // 1) Create "thunks":
2137da49faf1SGeorge Rimar // Jump instructions in many ISAs have small displacements, and therefore
2138da49faf1SGeorge Rimar // they cannot jump to arbitrary addresses in memory. For example, RISC-V
2139da49faf1SGeorge Rimar // JAL instruction can target only +-1 MiB from PC. It is a linker's
2140da49faf1SGeorge Rimar // responsibility to create and insert small pieces of code between
2141da49faf1SGeorge Rimar // sections to extend the ranges if jump targets are out of range. Such
2142da49faf1SGeorge Rimar // code pieces are called "thunks".
21435a30177eSRui Ueyama //
2144da49faf1SGeorge Rimar // We add thunks at this stage. We couldn't do this before this point
2145da49faf1SGeorge Rimar // because this is the earliest point where we know sizes of sections and
2146da49faf1SGeorge Rimar // their layouts (that are needed to determine if jump targets are in
2147da49faf1SGeorge Rimar // range).
2148da49faf1SGeorge Rimar //
2149da49faf1SGeorge Rimar // 2) Update the sections. We need to generate content that depends on the
2150da49faf1SGeorge Rimar // address of InputSections. For example, MIPS GOT section content or
2151da49faf1SGeorge Rimar // android packed relocations sections content.
2152da49faf1SGeorge Rimar //
2153da49faf1SGeorge Rimar // 3) Assign the final values for the linker script symbols. Linker scripts
2154da49faf1SGeorge Rimar // sometimes using forward symbol declarations. We want to set the correct
2155da49faf1SGeorge Rimar // values. They also might change after adding the thunks.
2156da49faf1SGeorge Rimar finalizeAddressDependentContent();
21575014d6fcSFangrui Song
21585014d6fcSFangrui Song // All information needed for OutputSection part of Map file is available.
21593834385fSPeter Smith if (errorCount())
21603834385fSPeter Smith return;
216143e852fbSPeter Smith
2162439341b9SJames Henderson {
2163439341b9SJames Henderson llvm::TimeTraceScope timeScope("Finalize synthetic sections");
2164439341b9SJames Henderson // finalizeAddressDependentContent may have added local symbols to the
2165439341b9SJames Henderson // static symbol table.
2166cb203f3fSFangrui Song finalizeSynthetic(in.symTab.get());
2167cb203f3fSFangrui Song finalizeSynthetic(in.ppc64LongBranchTarget.get());
2168439341b9SJames Henderson }
2169c552619fSGeorge Rimar
217094317878SSriraman Tallam // Relaxation to delete inter-basic block jumps created by basic block
217194317878SSriraman Tallam // sections. Run after in.symTab is finalized as optimizeBasicBlockJumps
217294317878SSriraman Tallam // can relax jump instructions based on symbol offset.
217394317878SSriraman Tallam if (config->optimizeBBJumps)
217494317878SSriraman Tallam optimizeBasicBlockJumps();
217594317878SSriraman Tallam
21761ec42d9aSPeter Smith // Fill other section headers. The dynamic table is finalized
21771ec42d9aSPeter Smith // at the end because some tags like RELSZ depend on result
21781ec42d9aSPeter Smith // of finalizing other sections.
21793837f427SRui Ueyama for (OutputSection *sec : outputSections)
21803837f427SRui Ueyama sec->finalize();
218184417f83SRui Ueyama }
218284417f83SRui Ueyama
21836d26ed92SRui Ueyama // Ensure data sections are not mixed with executable sections when
2184bf6e259bSFangrui Song // --execute-only is used. --execute-only make pages executable but not
2185bf6e259bSFangrui Song // readable.
checkExecuteOnly()21866d26ed92SRui Ueyama template <class ELFT> void Writer<ELFT>::checkExecuteOnly() {
21873837f427SRui Ueyama if (!config->executeOnly)
21886d26ed92SRui Ueyama return;
21896d26ed92SRui Ueyama
2190e0612c91SFangrui Song SmallVector<InputSection *, 0> storage;
2191a1c2ee01SFangrui Song for (OutputSection *osec : outputSections)
2192a1c2ee01SFangrui Song if (osec->flags & SHF_EXECINSTR)
2193e0612c91SFangrui Song for (InputSection *isec : getInputSections(*osec, storage))
21943837f427SRui Ueyama if (!(isec->flags & SHF_EXECINSTR))
2195a1c2ee01SFangrui Song error("cannot place " + toString(isec) + " into " +
2196a1c2ee01SFangrui Song toString(osec->name) +
2197bfc2f4b1SFangrui Song ": --execute-only does not support intermingling data and code");
21986d26ed92SRui Ueyama }
21996d26ed92SRui Ueyama
2200a5d79d16SRui Ueyama // The linker is expected to define SECNAME_start and SECNAME_end
2201a5d79d16SRui Ueyama // symbols for a few sections. This function defines them.
addStartEndSymbols()2202a5d79d16SRui Ueyama template <class ELFT> void Writer<ELFT>::addStartEndSymbols() {
22033aede928SHan Shen // If a section does not exist, there's ambiguity as to how we
22043aede928SHan Shen // define _start and _end symbols for an init/fini section. Since
22053aede928SHan Shen // the loader assume that the symbols are always defined, we need to
22063aede928SHan Shen // always define them. But what value? The loader iterates over all
22073aede928SHan Shen // pointers between _start and _end to run global ctors/dtors, so if
22083aede928SHan Shen // the section is empty, their symbol values don't actually matter
22093aede928SHan Shen // as long as _start and _end point to the same location.
22103aede928SHan Shen //
22113aede928SHan Shen // That said, we don't want to set the symbols to 0 (which is
22123aede928SHan Shen // probably the simplest value) because that could cause some
22133aede928SHan Shen // program to fail to link due to relocation overflow, if their
22143aede928SHan Shen // program text is above 2 GiB. We use the address of the .text
22153aede928SHan Shen // section instead to prevent that failure.
221629d86397SRui Ueyama //
22175976a3f5SNico Weber // In rare situations, the .text section may not exist. If that's the
221829d86397SRui Ueyama // case, use the image base address as a last resort.
22193aede928SHan Shen OutputSection *Default = findSection(".text");
22203aede928SHan Shen if (!Default)
22213837f427SRui Ueyama Default = Out::elfHeader;
222229d86397SRui Ueyama
22233837f427SRui Ueyama auto define = [=](StringRef start, StringRef end, OutputSection *os) {
222447eb3f15SAndrew Ng if (os && !script->isDiscarded(os)) {
22253837f427SRui Ueyama addOptionalRegular(start, os, 0);
22263837f427SRui Ueyama addOptionalRegular(end, os, -1);
2227f8435a9bSPeter Collingbourne } else {
22283837f427SRui Ueyama addOptionalRegular(start, Default, 0);
22293837f427SRui Ueyama addOptionalRegular(end, Default, 0);
2230f8435a9bSPeter Collingbourne }
2231a5d79d16SRui Ueyama };
2232a5d79d16SRui Ueyama
22333837f427SRui Ueyama define("__preinit_array_start", "__preinit_array_end", Out::preinitArray);
22343837f427SRui Ueyama define("__init_array_start", "__init_array_end", Out::initArray);
22353837f427SRui Ueyama define("__fini_array_start", "__fini_array_end", Out::finiArray);
223617cd3752SPeter Smith
22373837f427SRui Ueyama if (OutputSection *sec = findSection(".ARM.exidx"))
22383837f427SRui Ueyama define("__exidx_start", "__exidx_end", sec);
2239a5d79d16SRui Ueyama }
2240a5d79d16SRui Ueyama
2241d9189cebSRui Ueyama // If a section name is valid as a C identifier (which is rare because of
2242d9189cebSRui Ueyama // the leading '.'), linkers are expected to define __start_<secname> and
2243d9189cebSRui Ueyama // __stop_<secname> symbols. They are at beginning and end of the section,
2244d9189cebSRui Ueyama // respectively. This is not requested by the ELF standard, but GNU ld and
2245d9189cebSRui Ueyama // gold provide the feature, and used by many programs.
2246d9189cebSRui Ueyama template <class ELFT>
addStartStopSymbols(OutputSection & osec)22479e9c86fdSFangrui Song void Writer<ELFT>::addStartStopSymbols(OutputSection &osec) {
22489e9c86fdSFangrui Song StringRef s = osec.name;
22493837f427SRui Ueyama if (!isValidCIdentifier(s))
2250d9189cebSRui Ueyama return;
22519e9c86fdSFangrui Song addOptionalRegular(saver().save("__start_" + s), &osec, 0,
2252fffd05d5SPetr Hosek config->zStartStopVisibility);
22539e9c86fdSFangrui Song addOptionalRegular(saver().save("__stop_" + s), &osec, -1,
2254fffd05d5SPetr Hosek config->zStartStopVisibility);
2255d9189cebSRui Ueyama }
2256d9189cebSRui Ueyama
needsPtLoad(OutputSection * sec)22573837f427SRui Ueyama static bool needsPtLoad(OutputSection *sec) {
22585d621ed8SKonstantin Schwarz if (!(sec->flags & SHF_ALLOC))
2259ef762f26SRafael Espindola return false;
2260ef762f26SRafael Espindola
2261ef762f26SRafael Espindola // Don't allocate VA space for TLS NOBITS sections. The PT_TLS PHDR is
2262ef762f26SRafael Espindola // responsible for allocating space for them, not the PT_LOAD that
2263ef762f26SRafael Espindola // contains the TLS initialization image.
22643837f427SRui Ueyama if ((sec->flags & SHF_TLS) && sec->type == SHT_NOBITS)
2265ef762f26SRafael Espindola return false;
2266ef762f26SRafael Espindola return true;
22671d299a8aSMichael J. Spencer }
22681d299a8aSMichael J. Spencer
2269b45fd70eSRafael Espindola // Linker scripts are responsible for aligning addresses. Unfortunately, most
2270b45fd70eSRafael Espindola // linker scripts are designed for creating two PT_LOADs only, one RX and one
2271b45fd70eSRafael Espindola // RW. This means that there is no alignment in the RO to RX transition and we
2272b45fd70eSRafael Espindola // cannot create a PT_LOAD there.
computeFlags(uint64_t flags)22733837f427SRui Ueyama static uint64_t computeFlags(uint64_t flags) {
22743837f427SRui Ueyama if (config->omagic)
2275595a763fSGeorge Rimar return PF_R | PF_W | PF_X;
22763837f427SRui Ueyama if (config->executeOnly && (flags & PF_X))
22773837f427SRui Ueyama return flags & ~PF_R;
22783837f427SRui Ueyama if (config->singleRoRx && !(flags & PF_W))
22793837f427SRui Ueyama return flags | PF_X;
22803837f427SRui Ueyama return flags;
2281b45fd70eSRafael Espindola }
2282b45fd70eSRafael Espindola
22834fc6044aSRafael Espindola // Decide which program headers to create and which sections to include in each
22844fc6044aSRafael Espindola // one.
228502828985SPeter Collingbourne template <class ELFT>
createPhdrs(Partition & part)2286a1c2ee01SFangrui Song SmallVector<PhdrEntry *, 0> Writer<ELFT>::createPhdrs(Partition &part) {
2287a1c2ee01SFangrui Song SmallVector<PhdrEntry *, 0> ret;
22883837f427SRui Ueyama auto addHdr = [&](unsigned type, unsigned flags) -> PhdrEntry * {
22893837f427SRui Ueyama ret.push_back(make<PhdrEntry>(type, flags));
22903837f427SRui Ueyama return ret.back();
22914fc6044aSRafael Espindola };
2292e3336c0bSGeorge Rimar
22933837f427SRui Ueyama unsigned partNo = part.getNumber();
22943837f427SRui Ueyama bool isMain = partNo == 1;
229502828985SPeter Collingbourne
2296e8c0d933SFangrui Song // Add the first PT_LOAD segment for regular output sections.
2297e8c0d933SFangrui Song uint64_t flags = computeFlags(PF_R);
2298e8c0d933SFangrui Song PhdrEntry *load = nullptr;
2299e8c0d933SFangrui Song
2300e8c0d933SFangrui Song // nmagic or omagic output does not have PT_PHDR, PT_INTERP, or the readonly
2301e8c0d933SFangrui Song // PT_LOAD.
2302e8c0d933SFangrui Song if (!config->nmagic && !config->omagic) {
2303e8c0d933SFangrui Song // The first phdr entry is PT_PHDR which describes the program header
2304e8c0d933SFangrui Song // itself.
23053837f427SRui Ueyama if (isMain)
23063837f427SRui Ueyama addHdr(PT_PHDR, PF_R)->add(Out::programHeaders);
230702828985SPeter Collingbourne else
23083837f427SRui Ueyama addHdr(PT_PHDR, PF_R)->add(part.programHeaders->getParent());
2309953c2c4bSRui Ueyama
2310803195edSRui Ueyama // PT_INTERP must be the second entry if exists.
23113837f427SRui Ueyama if (OutputSection *cmd = findSection(".interp", partNo))
23123837f427SRui Ueyama addHdr(PT_INTERP, cmd->getPhdrFlags())->add(cmd);
23137010776dSRafael Espindola
231402ed7575SRafael Espindola // Add the headers. We will remove them if they don't fit.
231502828985SPeter Collingbourne // In the other partitions the headers are ordinary sections, so they don't
231602828985SPeter Collingbourne // need to be added here.
23173837f427SRui Ueyama if (isMain) {
23183837f427SRui Ueyama load = addHdr(PT_LOAD, flags);
23193837f427SRui Ueyama load->add(Out::elfHeader);
23203837f427SRui Ueyama load->add(Out::programHeaders);
232102828985SPeter Collingbourne }
2322e8c0d933SFangrui Song }
232302ed7575SRafael Espindola
2324e8710ef1SFangrui Song // PT_GNU_RELRO includes all sections that should be marked as
23255976a3f5SNico Weber // read-only by dynamic linker after processing relocations.
2326e8710ef1SFangrui Song // Current dynamic loaders only support one PT_GNU_RELRO PHDR, give
2327e8710ef1SFangrui Song // an error message if more than one PT_GNU_RELRO PHDR is required.
23283837f427SRui Ueyama PhdrEntry *relRo = make<PhdrEntry>(PT_GNU_RELRO, PF_R);
23293837f427SRui Ueyama bool inRelroPhdr = false;
23303837f427SRui Ueyama OutputSection *relroEnd = nullptr;
23313837f427SRui Ueyama for (OutputSection *sec : outputSections) {
23323837f427SRui Ueyama if (sec->partition != partNo || !needsPtLoad(sec))
2333e8710ef1SFangrui Song continue;
23343837f427SRui Ueyama if (isRelroSection(sec)) {
23353837f427SRui Ueyama inRelroPhdr = true;
23363837f427SRui Ueyama if (!relroEnd)
23373837f427SRui Ueyama relRo->add(sec);
2338e8710ef1SFangrui Song else
23393837f427SRui Ueyama error("section: " + sec->name + " is not contiguous with other relro" +
2340e8710ef1SFangrui Song " sections");
23413837f427SRui Ueyama } else if (inRelroPhdr) {
23423837f427SRui Ueyama inRelroPhdr = false;
23433837f427SRui Ueyama relroEnd = sec;
2344e8710ef1SFangrui Song }
2345e8710ef1SFangrui Song }
2346e8710ef1SFangrui Song
23473837f427SRui Ueyama for (OutputSection *sec : outputSections) {
23483837f427SRui Ueyama if (!needsPtLoad(sec))
2349ef762f26SRafael Espindola continue;
2350ef762f26SRafael Espindola
235102828985SPeter Collingbourne // Normally, sections in partitions other than the current partition are
235202828985SPeter Collingbourne // ignored. But partition number 255 is a special case: it contains the
235302828985SPeter Collingbourne // partition end marker (.part.end). It needs to be added to the main
235402828985SPeter Collingbourne // partition so that a segment is created for it in the main partition,
235502828985SPeter Collingbourne // which will cause the dynamic loader to reserve space for the other
235602828985SPeter Collingbourne // partitions.
23573837f427SRui Ueyama if (sec->partition != partNo) {
23583837f427SRui Ueyama if (isMain && sec->partition == 255)
23593837f427SRui Ueyama addHdr(PT_LOAD, computeFlags(sec->getPhdrFlags()))->add(sec);
236002828985SPeter Collingbourne continue;
236102828985SPeter Collingbourne }
236202828985SPeter Collingbourne
23638ceadb38SGeorge Rimar // Segments are contiguous memory regions that has the same attributes
23648ceadb38SGeorge Rimar // (e.g. executable or writable). There is one phdr for each segment.
23658ceadb38SGeorge Rimar // Therefore, we need to create a new phdr when the next section has
236634bdf27eSGeorge Rimar // different flags or is loaded at a discontiguous address or memory
236734bdf27eSGeorge Rimar // region using AT or AT> linker script command, respectively. At the same
236834bdf27eSGeorge Rimar // time, we don't want to create a separate load segment for the headers,
236934bdf27eSGeorge Rimar // even if the first output section has an AT or AT> attribute.
23703837f427SRui Ueyama uint64_t newFlags = computeFlags(sec->getPhdrFlags());
2371b498d993SFangrui Song bool sameLMARegion =
2372b498d993SFangrui Song load && !sec->lmaExpr && sec->lmaRegion == load->firstSec->lmaRegion;
2373b498d993SFangrui Song if (!(load && newFlags == flags && sec != relroEnd &&
2374b498d993SFangrui Song sec->memRegion == load->firstSec->memRegion &&
2375b498d993SFangrui Song (sameLMARegion || load->lastSec == Out::programHeaders))) {
23763837f427SRui Ueyama load = addHdr(PT_LOAD, newFlags);
23773837f427SRui Ueyama flags = newFlags;
23782f008242SMichael J. Spencer }
237978aa1de3SMichael J. Spencer
23803837f427SRui Ueyama load->add(sec);
238184487f11SMichael J. Spencer }
23826b83b90bSRafael Espindola
2383db00b618SRui Ueyama // Add a TLS segment if any.
23843837f427SRui Ueyama PhdrEntry *tlsHdr = make<PhdrEntry>(PT_TLS, PF_R);
23853837f427SRui Ueyama for (OutputSection *sec : outputSections)
23863837f427SRui Ueyama if (sec->partition == partNo && sec->flags & SHF_TLS)
23873837f427SRui Ueyama tlsHdr->add(sec);
23883837f427SRui Ueyama if (tlsHdr->firstSec)
23893837f427SRui Ueyama ret.push_back(tlsHdr);
239078aa1de3SMichael J. Spencer
2391803195edSRui Ueyama // Add an entry for .dynamic.
23923837f427SRui Ueyama if (OutputSection *sec = part.dynamic->getParent())
23933837f427SRui Ueyama addHdr(PT_DYNAMIC, sec->getPhdrFlags())->add(sec);
239491009b38SRafael Espindola
23953837f427SRui Ueyama if (relRo->firstSec)
23963837f427SRui Ueyama ret.push_back(relRo);
2397e3336c0bSGeorge Rimar
23984fc6044aSRafael Espindola // PT_GNU_EH_FRAME is a special section pointing on .eh_frame_hdr.
23993837f427SRui Ueyama if (part.ehFrame->isNeeded() && part.ehFrameHdr &&
24003837f427SRui Ueyama part.ehFrame->getParent() && part.ehFrameHdr->getParent())
24013837f427SRui Ueyama addHdr(PT_GNU_EH_FRAME, part.ehFrameHdr->getParent()->getPhdrFlags())
24023837f427SRui Ueyama ->add(part.ehFrameHdr->getParent());
2403f6bc65a3SGeorge Rimar
240481cb7107SRui Ueyama // PT_OPENBSD_RANDOMIZE is an OpenBSD-specific feature. That makes
240581cb7107SRui Ueyama // the dynamic linker fill the segment with random data.
24063837f427SRui Ueyama if (OutputSection *cmd = findSection(".openbsd.randomdata", partNo))
24073837f427SRui Ueyama addHdr(PT_OPENBSD_RANDOMIZE, cmd->getPhdrFlags())->add(cmd);
2408270173f2SGeorge Rimar
24092a0fcae3SMichał Górny if (config->zGnustack != GnuStackKind::None) {
2410e79b09a6SRui Ueyama // PT_GNU_STACK is a special section to tell the loader to make the
2411a7e87252SRui Ueyama // pages for the stack non-executable. If you really want an executable
2412a7e87252SRui Ueyama // stack, you can pass -z execstack, but that's not recommended for
2413a7e87252SRui Ueyama // security reasons.
24143837f427SRui Ueyama unsigned perm = PF_R | PF_W;
24152a0fcae3SMichał Górny if (config->zGnustack == GnuStackKind::Exec)
24163837f427SRui Ueyama perm |= PF_X;
24173837f427SRui Ueyama addHdr(PT_GNU_STACK, perm)->p_memsz = config->zStackSize;
24182a0fcae3SMichał Górny }
24199907eb0bSRafael Espindola
2420cc6e567cSGeorge Rimar // PT_OPENBSD_WXNEEDED is a OpenBSD-specific header to mark the executable
2421cc6e567cSGeorge Rimar // is expected to perform W^X violations, such as calling mprotect(2) or
2422cc6e567cSGeorge Rimar // mmap(2) with PROT_WRITE | PROT_EXEC, which is prohibited by default on
2423cc6e567cSGeorge Rimar // OpenBSD.
24243837f427SRui Ueyama if (config->zWxneeded)
24253837f427SRui Ueyama addHdr(PT_OPENBSD_WXNEEDED, PF_X);
2426cc6e567cSGeorge Rimar
24274d6c4cb4SPeter Smith if (OutputSection *cmd = findSection(".note.gnu.property", partNo))
24284d6c4cb4SPeter Smith addHdr(PT_GNU_PROPERTY, PF_R)->add(cmd);
24294d6c4cb4SPeter Smith
2430d45df094SFangrui Song // Create one PT_NOTE per a group of contiguous SHT_NOTE sections with the
2431d45df094SFangrui Song // same alignment.
24323837f427SRui Ueyama PhdrEntry *note = nullptr;
24333837f427SRui Ueyama for (OutputSection *sec : outputSections) {
24343837f427SRui Ueyama if (sec->partition != partNo)
243502828985SPeter Collingbourne continue;
24363837f427SRui Ueyama if (sec->type == SHT_NOTE && (sec->flags & SHF_ALLOC)) {
24373837f427SRui Ueyama if (!note || sec->lmaExpr || note->lastSec->alignment != sec->alignment)
24383837f427SRui Ueyama note = addHdr(PT_NOTE, PF_R);
24393837f427SRui Ueyama note->add(sec);
24404d65ef3bSPetr Hosek } else {
24413837f427SRui Ueyama note = nullptr;
24424d65ef3bSPetr Hosek }
24434d65ef3bSPetr Hosek }
24443837f427SRui Ueyama return ret;
2445a46566f2SRui Ueyama }
244603220309SGeorge Rimar
24478e67000fSRafael Espindola template <class ELFT>
addPhdrForSection(Partition & part,unsigned shType,unsigned pType,unsigned pFlags)24483837f427SRui Ueyama void Writer<ELFT>::addPhdrForSection(Partition &part, unsigned shType,
24493837f427SRui Ueyama unsigned pType, unsigned pFlags) {
24503837f427SRui Ueyama unsigned partNo = part.getNumber();
24513837f427SRui Ueyama auto i = llvm::find_if(outputSections, [=](OutputSection *cmd) {
24523837f427SRui Ueyama return cmd->partition == partNo && cmd->type == shType;
245302828985SPeter Collingbourne });
24543837f427SRui Ueyama if (i == outputSections.end())
24558e67000fSRafael Espindola return;
24568e67000fSRafael Espindola
24573837f427SRui Ueyama PhdrEntry *entry = make<PhdrEntry>(pType, pFlags);
24583837f427SRui Ueyama entry->add(*i);
24593837f427SRui Ueyama part.phdrs.push_back(entry);
24608e67000fSRafael Espindola }
24618e67000fSRafael Espindola
246201c7f4b6SFangrui Song // Place the first section of each PT_LOAD to a different page (of maxPageSize).
246301c7f4b6SFangrui Song // This is achieved by assigning an alignment expression to addrExpr of each
246401c7f4b6SFangrui Song // such section.
fixSectionAlignments()246547091903SRui Ueyama template <class ELFT> void Writer<ELFT>::fixSectionAlignments() {
246601c7f4b6SFangrui Song const PhdrEntry *prev;
246701c7f4b6SFangrui Song auto pageAlign = [&](const PhdrEntry *p) {
246801c7f4b6SFangrui Song OutputSection *cmd = p->firstSec;
24696ed8e201SFangrui Song if (!cmd)
24706ed8e201SFangrui Song return;
24716ed8e201SFangrui Song cmd->alignExpr = [align = cmd->alignment]() { return align; };
24726ed8e201SFangrui Song if (!cmd->addrExpr) {
247301c7f4b6SFangrui Song // Prefer advancing to align(dot, maxPageSize) + dot%maxPageSize to avoid
247401c7f4b6SFangrui Song // padding in the file contents.
247501c7f4b6SFangrui Song //
247601c7f4b6SFangrui Song // When -z separate-code is used we must not have any overlap in pages
247701c7f4b6SFangrui Song // between an executable segment and a non-executable segment. We align to
247801c7f4b6SFangrui Song // the next maximum page size boundary on transitions between executable
247901c7f4b6SFangrui Song // and non-executable segments.
248001c7f4b6SFangrui Song //
24814514ac7cSFangrui Song // SHT_LLVM_PART_EHDR marks the start of a partition. The partition
24824514ac7cSFangrui Song // sections will be extracted to a separate file. Align to the next
24834514ac7cSFangrui Song // maximum page size boundary so that we can find the ELF header at the
24844514ac7cSFangrui Song // start. We cannot benefit from overlapping p_offset ranges with the
24854514ac7cSFangrui Song // previous segment anyway.
248602649506SFangrui Song if (config->zSeparate == SeparateSegmentKind::Loadable ||
248702649506SFangrui Song (config->zSeparate == SeparateSegmentKind::Code && prev &&
24884514ac7cSFangrui Song (prev->p_flags & PF_X) != (p->p_flags & PF_X)) ||
24894514ac7cSFangrui Song cmd->type == SHT_LLVM_PART_EHDR)
249001c7f4b6SFangrui Song cmd->addrExpr = [] {
249185cfd917SFangrui Song return alignToPowerOf2(script->getDot(), config->maxPageSize);
24920f7dc0e2SRafael Espindola };
249301c7f4b6SFangrui Song // PT_TLS is at the start of the first RW PT_LOAD. If `p` includes PT_TLS,
249401c7f4b6SFangrui Song // it must be the RW. Align to p_align(PT_TLS) to make sure
249501c7f4b6SFangrui Song // p_vaddr(PT_LOAD)%p_align(PT_LOAD) = 0. Otherwise, if
249601c7f4b6SFangrui Song // sh_addralign(.tdata) < sh_addralign(.tbss), we will set p_align(PT_TLS)
249701c7f4b6SFangrui Song // to sh_addralign(.tbss), while p_vaddr(PT_TLS)=p_vaddr(PT_LOAD) may not
249801c7f4b6SFangrui Song // be congruent to 0 modulo p_align(PT_TLS).
249901c7f4b6SFangrui Song //
250001c7f4b6SFangrui Song // Technically this is not required, but as of 2019, some dynamic loaders
250101c7f4b6SFangrui Song // don't handle p_vaddr%p_align != 0 correctly, e.g. glibc (i386 and
250201c7f4b6SFangrui Song // x86-64) doesn't make runtime address congruent to p_vaddr modulo
250301c7f4b6SFangrui Song // p_align for dynamic TLS blocks (PR/24606), FreeBSD rtld has the same
250401c7f4b6SFangrui Song // bug, musl (TLS Variant 1 architectures) before 1.1.23 handled TLS
250501c7f4b6SFangrui Song // blocks correctly. We need to keep the workaround for a while.
250601c7f4b6SFangrui Song else if (Out::tlsPhdr && Out::tlsPhdr->firstSec == p->firstSec)
250701c7f4b6SFangrui Song cmd->addrExpr = [] {
250885cfd917SFangrui Song return alignToPowerOf2(script->getDot(), config->maxPageSize) +
250985cfd917SFangrui Song alignToPowerOf2(script->getDot() % config->maxPageSize,
251001c7f4b6SFangrui Song Out::tlsPhdr->p_align);
251101c7f4b6SFangrui Song };
251201c7f4b6SFangrui Song else
251301c7f4b6SFangrui Song cmd->addrExpr = [] {
251485cfd917SFangrui Song return alignToPowerOf2(script->getDot(), config->maxPageSize) +
251501c7f4b6SFangrui Song script->getDot() % config->maxPageSize;
251601c7f4b6SFangrui Song };
251701c7f4b6SFangrui Song }
25180f7dc0e2SRafael Espindola };
25190f7dc0e2SRafael Espindola
25203837f427SRui Ueyama for (Partition &part : partitions) {
252101c7f4b6SFangrui Song prev = nullptr;
25223837f427SRui Ueyama for (const PhdrEntry *p : part.phdrs)
252301c7f4b6SFangrui Song if (p->p_type == PT_LOAD && p->firstSec) {
252401c7f4b6SFangrui Song pageAlign(p);
252501c7f4b6SFangrui Song prev = p;
252601c7f4b6SFangrui Song }
252747091903SRui Ueyama }
252802828985SPeter Collingbourne }
252947091903SRui Ueyama
25308d760249SRui Ueyama // Compute an in-file position for a given section. The file offset must be the
25318d760249SRui Ueyama // same with its virtual address modulo the page size, so that the loader can
25328d760249SRui Ueyama // load executables without any address adjustment.
computeFileOffset(OutputSection * os,uint64_t off)25333837f427SRui Ueyama static uint64_t computeFileOffset(OutputSection *os, uint64_t off) {
2534af47d002SFangrui Song // The first section in a PT_LOAD has to have congruent offset and address
25356dc2bd70SFangrui Song // modulo the maximum page size.
25366dc2bd70SFangrui Song if (os->ptLoad && os->ptLoad->firstSec == os)
25376dc2bd70SFangrui Song return alignTo(off, os->ptLoad->p_align, os->addr);
2538af47d002SFangrui Song
2539af47d002SFangrui Song // File offsets are not significant for .bss sections other than the first one
2540cfaa5bf4SJessica Clarke // in a PT_LOAD/PT_TLS. By convention, we keep section offsets monotonically
2541af47d002SFangrui Song // increasing rather than setting to zero.
2542cfaa5bf4SJessica Clarke if (os->type == SHT_NOBITS &&
2543cfaa5bf4SJessica Clarke (!Out::tlsPhdr || Out::tlsPhdr->firstSec != os))
25443837f427SRui Ueyama return off;
25452640a0a5SRafael Espindola
25462640a0a5SRafael Espindola // If the section is not in a PT_LOAD, we just have to align it.
25473837f427SRui Ueyama if (!os->ptLoad)
254885cfd917SFangrui Song return alignToPowerOf2(off, os->alignment);
25498d760249SRui Ueyama
25508b8f74f6SRafael Espindola // If two sections share the same PT_LOAD the file offset is calculated
25518b8f74f6SRafael Espindola // using this formula: Off2 = Off1 + (VA2 - VA1).
2552af47d002SFangrui Song OutputSection *first = os->ptLoad->firstSec;
25533837f427SRui Ueyama return first->offset + os->addr - first->addr;
25545f857327SGeorge Rimar }
25555f857327SGeorge Rimar
assignFileOffsetsBinary()255686ce267aSGeorge Rimar template <class ELFT> void Writer<ELFT>::assignFileOffsetsBinary() {
2557acb66b91SFangrui Song // Compute the minimum LMA of all non-empty non-NOBITS sections as minAddr.
2558acb66b91SFangrui Song auto needsOffset = [](OutputSection &sec) {
2559acb66b91SFangrui Song return sec.type != SHT_NOBITS && (sec.flags & SHF_ALLOC) && sec.size > 0;
2560acb66b91SFangrui Song };
2561acb66b91SFangrui Song uint64_t minAddr = UINT64_MAX;
25623837f427SRui Ueyama for (OutputSection *sec : outputSections)
2563acb66b91SFangrui Song if (needsOffset(*sec)) {
2564acb66b91SFangrui Song sec->offset = sec->getLMA();
2565acb66b91SFangrui Song minAddr = std::min(minAddr, sec->offset);
2566acb66b91SFangrui Song }
2567acb66b91SFangrui Song
2568acb66b91SFangrui Song // Sections are laid out at LMA minus minAddr.
2569acb66b91SFangrui Song fileSize = 0;
2570acb66b91SFangrui Song for (OutputSection *sec : outputSections)
2571acb66b91SFangrui Song if (needsOffset(*sec)) {
2572acb66b91SFangrui Song sec->offset -= minAddr;
2573acb66b91SFangrui Song fileSize = std::max(fileSize, sec->offset + sec->size);
2574acb66b91SFangrui Song }
257586ce267aSGeorge Rimar }
257686ce267aSGeorge Rimar
rangeToString(uint64_t addr,uint64_t len)25773837f427SRui Ueyama static std::string rangeToString(uint64_t addr, uint64_t len) {
25783837f427SRui Ueyama return "[0x" + utohexstr(addr) + ", 0x" + utohexstr(addr + len - 1) + "]";
257923130867SGeorge Rimar }
258023130867SGeorge Rimar
258186ce267aSGeorge Rimar // Assign file offsets to output sections.
assignFileOffsets()258286ce267aSGeorge Rimar template <class ELFT> void Writer<ELFT>::assignFileOffsets() {
25836c1c2313SFangrui Song Out::programHeaders->offset = Out::elfHeader->size;
25846c1c2313SFangrui Song uint64_t off = Out::elfHeader->size + Out::programHeaders->size;
258586ce267aSGeorge Rimar
25863837f427SRui Ueyama PhdrEntry *lastRX = nullptr;
25873837f427SRui Ueyama for (Partition &part : partitions)
25883837f427SRui Ueyama for (PhdrEntry *p : part.phdrs)
25893837f427SRui Ueyama if (p->p_type == PT_LOAD && (p->p_flags & PF_X))
25903837f427SRui Ueyama lastRX = p;
2591edd6c358SPetr Hosek
2592ec29538aSFangrui Song // Layout SHF_ALLOC sections before non-SHF_ALLOC sections. A non-SHF_ALLOC
2593ec29538aSFangrui Song // will not occupy file offsets contained by a PT_LOAD.
25943837f427SRui Ueyama for (OutputSection *sec : outputSections) {
2595ec29538aSFangrui Song if (!(sec->flags & SHF_ALLOC))
2596ec29538aSFangrui Song continue;
2597cecc6893SFangrui Song off = computeFileOffset(sec, off);
2598cecc6893SFangrui Song sec->offset = off;
2599cecc6893SFangrui Song if (sec->type != SHT_NOBITS)
2600cecc6893SFangrui Song off += sec->size;
26014664280aSRui Ueyama
2602edd6c358SPetr Hosek // If this is a last section of the last executable segment and that
2603edd6c358SPetr Hosek // segment is the last loadable segment, align the offset of the
2604edd6c358SPetr Hosek // following section to avoid loading non-segments parts of the file.
260502649506SFangrui Song if (config->zSeparate != SeparateSegmentKind::None && lastRX &&
260602649506SFangrui Song lastRX->lastSec == sec)
260785cfd917SFangrui Song off = alignToPowerOf2(off, config->maxPageSize);
2608edd6c358SPetr Hosek }
2609cecc6893SFangrui Song for (OutputSection *osec : outputSections)
2610cecc6893SFangrui Song if (!(osec->flags & SHF_ALLOC)) {
261185cfd917SFangrui Song osec->offset = alignToPowerOf2(off, osec->alignment);
2612cecc6893SFangrui Song off = osec->offset + osec->size;
2613cecc6893SFangrui Song }
2614467c4d55SEugene Leviant
261585cfd917SFangrui Song sectionHeaderOff = alignToPowerOf2(off, config->wordsize);
26163837f427SRui Ueyama fileSize = sectionHeaderOff + (outputSections.size() + 1) * sizeof(Elf_Shdr);
261723130867SGeorge Rimar
261823130867SGeorge Rimar // Our logic assumes that sections have rising VA within the same segment.
261923130867SGeorge Rimar // With use of linker scripts it is possible to violate this rule and get file
262023130867SGeorge Rimar // offset overlaps or overflows. That should never happen with a valid script
262123130867SGeorge Rimar // which does not move the location counter backwards and usually scripts do
262223130867SGeorge Rimar // not do that. Unfortunately, there are apps in the wild, for example, Linux
262323130867SGeorge Rimar // kernel, which control segment distribution explicitly and move the counter
262423130867SGeorge Rimar // backwards, so we have to allow doing that to support linking them. We
26258fcd04b7SGeorge Rimar // perform non-critical checks for overlaps in checkSectionOverlap(), but here
26268fcd04b7SGeorge Rimar // we want to prevent file size overflows because it would crash the linker.
26273837f427SRui Ueyama for (OutputSection *sec : outputSections) {
26283837f427SRui Ueyama if (sec->type == SHT_NOBITS)
262923130867SGeorge Rimar continue;
26303837f427SRui Ueyama if ((sec->offset > fileSize) || (sec->offset + sec->size > fileSize))
26313837f427SRui Ueyama error("unable to place section " + sec->name + " at file offset " +
26323837f427SRui Ueyama rangeToString(sec->offset, sec->size) +
263323130867SGeorge Rimar "; check your linker script for overflows");
263423130867SGeorge Rimar }
2635e044e9cfSRui Ueyama }
2636e044e9cfSRui Ueyama
2637e044e9cfSRui Ueyama // Finalize the program headers. We call this function after we assign
2638e044e9cfSRui Ueyama // file offsets and VAs to all sections.
setPhdrs(Partition & part)26393837f427SRui Ueyama template <class ELFT> void Writer<ELFT>::setPhdrs(Partition &part) {
26403837f427SRui Ueyama for (PhdrEntry *p : part.phdrs) {
26413837f427SRui Ueyama OutputSection *first = p->firstSec;
26423837f427SRui Ueyama OutputSection *last = p->lastSec;
26434664280aSRui Ueyama
26443837f427SRui Ueyama if (first) {
26453837f427SRui Ueyama p->p_filesz = last->offset - first->offset;
26463837f427SRui Ueyama if (last->type != SHT_NOBITS)
26473837f427SRui Ueyama p->p_filesz += last->size;
26484664280aSRui Ueyama
26493837f427SRui Ueyama p->p_memsz = last->addr + last->size - first->addr;
26503837f427SRui Ueyama p->p_offset = first->offset;
26513837f427SRui Ueyama p->p_vaddr = first->addr;
26524664280aSRui Ueyama
265302828985SPeter Collingbourne // File offsets in partitions other than the main partition are relative
265402828985SPeter Collingbourne // to the offset of the ELF headers. Perform that adjustment now.
26553837f427SRui Ueyama if (part.elfHeader)
26563837f427SRui Ueyama p->p_offset -= part.elfHeader->getParent()->offset;
265702828985SPeter Collingbourne
26583837f427SRui Ueyama if (!p->hasLMA)
26593837f427SRui Ueyama p->p_paddr = first->getLMA();
26604fc6044aSRafael Espindola }
26614664280aSRui Ueyama
26626dc2bd70SFangrui Song if (p->p_type == PT_GNU_RELRO) {
26633837f427SRui Ueyama p->p_align = 1;
26642ac8ce5dSFangrui Song // musl/glibc ld.so rounds the size down, so we need to round up
26652ac8ce5dSFangrui Song // to protect the last page. This is a no-op on FreeBSD which always
26662ac8ce5dSFangrui Song // rounds up.
266785cfd917SFangrui Song p->p_memsz =
266885cfd917SFangrui Song alignToPowerOf2(p->p_offset + p->p_memsz, config->commonPageSize) -
26692ac8ce5dSFangrui Song p->p_offset;
26707b5088b3SPeter Collingbourne }
2671803195edSRui Ueyama }
2672d7d2369cSRyan Prichard }
267384487f11SMichael J. Spencer
267409fcdbc1SRui Ueyama // A helper struct for checkSectionOverlap.
267509fcdbc1SRui Ueyama namespace {
267609fcdbc1SRui Ueyama struct SectionOffset {
26773837f427SRui Ueyama OutputSection *sec;
26783837f427SRui Ueyama uint64_t offset;
267909fcdbc1SRui Ueyama };
268009fcdbc1SRui Ueyama } // namespace
268109fcdbc1SRui Ueyama
26826b367faaSAlexander Richardson // Check whether sections overlap for a specific address range (file offsets,
26835976a3f5SNico Weber // load and virtual addresses).
checkOverlap(StringRef name,std::vector<SectionOffset> & sections,bool isVirtualAddr)26843837f427SRui Ueyama static void checkOverlap(StringRef name, std::vector<SectionOffset> §ions,
26853837f427SRui Ueyama bool isVirtualAddr) {
26863837f427SRui Ueyama llvm::sort(sections, [=](const SectionOffset &a, const SectionOffset &b) {
26873837f427SRui Ueyama return a.offset < b.offset;
26886b367faaSAlexander Richardson });
268909fcdbc1SRui Ueyama
269057061618SRui Ueyama // Finding overlap is easy given a vector is sorted by start position.
269157061618SRui Ueyama // If an element starts before the end of the previous element, they overlap.
26923837f427SRui Ueyama for (size_t i = 1, end = sections.size(); i < end; ++i) {
26933837f427SRui Ueyama SectionOffset a = sections[i - 1];
26943837f427SRui Ueyama SectionOffset b = sections[i];
26953837f427SRui Ueyama if (b.offset >= a.offset + a.sec->size)
2696a582419aSGeorge Rimar continue;
2697a582419aSGeorge Rimar
2698a582419aSGeorge Rimar // If both sections are in OVERLAY we allow the overlapping of virtual
2699a582419aSGeorge Rimar // addresses, because it is what OVERLAY was designed for.
27003837f427SRui Ueyama if (isVirtualAddr && a.sec->inOverlay && b.sec->inOverlay)
2701a582419aSGeorge Rimar continue;
2702a582419aSGeorge Rimar
27033837f427SRui Ueyama errorOrWarn("section " + a.sec->name + " " + name +
27043837f427SRui Ueyama " range overlaps with " + b.sec->name + "\n>>> " + a.sec->name +
27053837f427SRui Ueyama " range is " + rangeToString(a.offset, a.sec->size) + "\n>>> " +
27063837f427SRui Ueyama b.sec->name + " range is " +
27073837f427SRui Ueyama rangeToString(b.offset, b.sec->size));
27086b367faaSAlexander Richardson }
27096b367faaSAlexander Richardson }
27106b367faaSAlexander Richardson
27111fc9f39bSGeorge Rimar // Check for overlapping sections and address overflows.
27126b367faaSAlexander Richardson //
27136b367faaSAlexander Richardson // In this function we check that none of the output sections have overlapping
27146b367faaSAlexander Richardson // file offsets. For SHF_ALLOC sections we also check that the load address
27156b367faaSAlexander Richardson // ranges and the virtual address ranges don't overlap
checkSections()271694e148c8SGeorge Rimar template <class ELFT> void Writer<ELFT>::checkSections() {
27171fc9f39bSGeorge Rimar // First, check that section's VAs fit in available address space for target.
27183837f427SRui Ueyama for (OutputSection *os : outputSections)
27193837f427SRui Ueyama if ((os->addr + os->size < os->addr) ||
27203837f427SRui Ueyama (!ELFT::Is64Bits && os->addr + os->size > UINT32_MAX))
27213837f427SRui Ueyama errorOrWarn("section " + os->name + " at 0x" + utohexstr(os->addr) +
27223837f427SRui Ueyama " of size 0x" + utohexstr(os->size) +
27231fc9f39bSGeorge Rimar " exceeds available address space");
27241fc9f39bSGeorge Rimar
27251fc9f39bSGeorge Rimar // Check for overlapping file offsets. In this case we need to skip any
27261fc9f39bSGeorge Rimar // section marked as SHT_NOBITS. These sections don't actually occupy space in
27271fc9f39bSGeorge Rimar // the file so Sec->Offset + Sec->Size can overlap with others. If --oformat
27281fc9f39bSGeorge Rimar // binary is specified only add SHF_ALLOC sections are added to the output
27291fc9f39bSGeorge Rimar // file so we skip any non-allocated sections in that case.
27303837f427SRui Ueyama std::vector<SectionOffset> fileOffs;
27313837f427SRui Ueyama for (OutputSection *sec : outputSections)
27323837f427SRui Ueyama if (sec->size > 0 && sec->type != SHT_NOBITS &&
27333837f427SRui Ueyama (!config->oFormatBinary || (sec->flags & SHF_ALLOC)))
27343837f427SRui Ueyama fileOffs.push_back({sec, sec->offset});
27353837f427SRui Ueyama checkOverlap("file", fileOffs, false);
27366b367faaSAlexander Richardson
27376b367faaSAlexander Richardson // When linking with -r there is no need to check for overlapping virtual/load
27386b367faaSAlexander Richardson // addresses since those addresses will only be assigned when the final
27396b367faaSAlexander Richardson // executable/shared object is created.
27403837f427SRui Ueyama if (config->relocatable)
27416b367faaSAlexander Richardson return;
27426b367faaSAlexander Richardson
27436b367faaSAlexander Richardson // Checking for overlapping virtual and load addresses only needs to take
27449cd5df0eSFangrui Song // into account SHF_ALLOC sections since others will not be loaded.
27456b367faaSAlexander Richardson // Furthermore, we also need to skip SHF_TLS sections since these will be
27466b367faaSAlexander Richardson // mapped to other addresses at runtime and can therefore have overlapping
27476b367faaSAlexander Richardson // ranges in the file.
27483837f427SRui Ueyama std::vector<SectionOffset> vmas;
27493837f427SRui Ueyama for (OutputSection *sec : outputSections)
27503837f427SRui Ueyama if (sec->size > 0 && (sec->flags & SHF_ALLOC) && !(sec->flags & SHF_TLS))
27513837f427SRui Ueyama vmas.push_back({sec, sec->addr});
27523837f427SRui Ueyama checkOverlap("virtual address", vmas, true);
27536b367faaSAlexander Richardson
27546b367faaSAlexander Richardson // Finally, check that the load addresses don't overlap. This will usually be
27556b367faaSAlexander Richardson // the same as the virtual addresses but can be different when using a linker
27566b367faaSAlexander Richardson // script with AT().
27573837f427SRui Ueyama std::vector<SectionOffset> lmas;
27583837f427SRui Ueyama for (OutputSection *sec : outputSections)
27593837f427SRui Ueyama if (sec->size > 0 && (sec->flags & SHF_ALLOC) && !(sec->flags & SHF_TLS))
27603837f427SRui Ueyama lmas.push_back({sec, sec->getLMA()});
27613837f427SRui Ueyama checkOverlap("load address", lmas, false);
27626b367faaSAlexander Richardson }
27636b367faaSAlexander Richardson
27642eda6d16SRui Ueyama // The entry point address is chosen in the following ways.
27652eda6d16SRui Ueyama //
27662eda6d16SRui Ueyama // 1. the '-e' entry command-line option;
27672eda6d16SRui Ueyama // 2. the ENTRY(symbol) command in a linker control script;
27688ed548a4SRui Ueyama // 3. the value of the symbol _start, if present;
27697b4eddf0SRui Ueyama // 4. the number represented by the entry symbol, if it is a number;
2770d001ab82SFangrui Song // 5. the address 0.
getEntryAddr()2771180cd071SRui Ueyama static uint64_t getEntryAddr() {
27727b4eddf0SRui Ueyama // Case 1, 2 or 3
27733837f427SRui Ueyama if (Symbol *b = symtab->find(config->entry))
27743837f427SRui Ueyama return b->getVA();
27757b4eddf0SRui Ueyama
27767b4eddf0SRui Ueyama // Case 4
27773837f427SRui Ueyama uint64_t addr;
27783837f427SRui Ueyama if (to_integer(config->entry, addr))
27793837f427SRui Ueyama return addr;
27802eda6d16SRui Ueyama
27817b4eddf0SRui Ueyama // Case 5
27823837f427SRui Ueyama if (config->warnMissingEntry)
27833837f427SRui Ueyama warn("cannot find entry symbol " + config->entry +
27842eda6d16SRui Ueyama "; not setting start address");
27858da7aa08SRui Ueyama return 0;
27863bfaba92SRui Ueyama }
27873bfaba92SRui Ueyama
getELFType()27884cea4e81SRui Ueyama static uint16_t getELFType() {
27893837f427SRui Ueyama if (config->isPic)
27904cea4e81SRui Ueyama return ET_DYN;
27913837f427SRui Ueyama if (config->relocatable)
27924cea4e81SRui Ueyama return ET_REL;
27934cea4e81SRui Ueyama return ET_EXEC;
27944cea4e81SRui Ueyama }
27954cea4e81SRui Ueyama
writeHeader()279684487f11SMichael J. Spencer template <class ELFT> void Writer<ELFT>::writeHeader() {
27973837f427SRui Ueyama writeEhdr<ELFT>(Out::bufferStart, *mainPart);
27983837f427SRui Ueyama writePhdrs<ELFT>(Out::bufferStart + sizeof(Elf_Ehdr), *mainPart);
2799e08cd678SRui Ueyama
28003837f427SRui Ueyama auto *eHdr = reinterpret_cast<Elf_Ehdr *>(Out::bufferStart);
28013837f427SRui Ueyama eHdr->e_type = getELFType();
28023837f427SRui Ueyama eHdr->e_entry = getEntryAddr();
28033837f427SRui Ueyama eHdr->e_shoff = sectionHeaderOff;
2804e438e078SRafael Espindola
2805b9f3ea3eSGeorge Rimar // Write the section header table.
2806b9f3ea3eSGeorge Rimar //
2807b9f3ea3eSGeorge Rimar // The ELF header can only store numbers up to SHN_LORESERVE in the e_shnum
2808b9f3ea3eSGeorge Rimar // and e_shstrndx fields. When the value of one of these fields exceeds
2809b9f3ea3eSGeorge Rimar // SHN_LORESERVE ELF requires us to put sentinel values in the ELF header and
2810b9f3ea3eSGeorge Rimar // use fields in the section header at index 0 to store
2811b9f3ea3eSGeorge Rimar // the value. The sentinel values and fields are:
2812b9f3ea3eSGeorge Rimar // e_shnum = 0, SHdrs[0].sh_size = number of sections.
2813b9f3ea3eSGeorge Rimar // e_shstrndx = SHN_XINDEX, SHdrs[0].sh_link = .shstrtab section index.
28143837f427SRui Ueyama auto *sHdrs = reinterpret_cast<Elf_Shdr *>(Out::bufferStart + eHdr->e_shoff);
28153837f427SRui Ueyama size_t num = outputSections.size() + 1;
28163837f427SRui Ueyama if (num >= SHN_LORESERVE)
28173837f427SRui Ueyama sHdrs->sh_size = num;
2818b9f3ea3eSGeorge Rimar else
28193837f427SRui Ueyama eHdr->e_shnum = num;
2820b9f3ea3eSGeorge Rimar
28213837f427SRui Ueyama uint32_t strTabIndex = in.shStrTab->getParent()->sectionIndex;
28223837f427SRui Ueyama if (strTabIndex >= SHN_LORESERVE) {
28233837f427SRui Ueyama sHdrs->sh_link = strTabIndex;
28243837f427SRui Ueyama eHdr->e_shstrndx = SHN_XINDEX;
2825b9f3ea3eSGeorge Rimar } else {
28263837f427SRui Ueyama eHdr->e_shstrndx = strTabIndex;
2827b9f3ea3eSGeorge Rimar }
2828b9f3ea3eSGeorge Rimar
28293837f427SRui Ueyama for (OutputSection *sec : outputSections)
28303837f427SRui Ueyama sec->writeHeaderTo<ELFT>(++sHdrs);
283184487f11SMichael J. Spencer }
28326b83b90bSRafael Espindola
28336d12eaeeSRui Ueyama // Open a result file.
openFile()2834f7f52ef6SRui Ueyama template <class ELFT> void Writer<ELFT>::openFile() {
28353837f427SRui Ueyama uint64_t maxSize = config->is64 ? INT64_MAX : UINT32_MAX;
28363837f427SRui Ueyama if (fileSize != size_t(fileSize) || maxSize < fileSize) {
28378e0b1793SBob Haarman std::string msg;
28388e0b1793SBob Haarman raw_string_ostream s(msg);
28398e0b1793SBob Haarman s << "output file too large: " << Twine(fileSize) << " bytes\n"
28408e0b1793SBob Haarman << "section sizes:\n";
28418e0b1793SBob Haarman for (OutputSection *os : outputSections)
28428e0b1793SBob Haarman s << os->name << ' ' << os->size << "\n";
28438e0b1793SBob Haarman error(s.str());
28446bd38220SRui Ueyama return;
28456bd38220SRui Ueyama }
28466bd38220SRui Ueyama
28473837f427SRui Ueyama unlinkAsync(config->outputFile);
28483837f427SRui Ueyama unsigned flags = 0;
28493837f427SRui Ueyama if (!config->relocatable)
285068142324SNick Terrell flags |= FileOutputBuffer::F_executable;
285168142324SNick Terrell if (!config->mmapOutputFile)
285268142324SNick Terrell flags |= FileOutputBuffer::F_no_mmap;
28533837f427SRui Ueyama Expected<std::unique_ptr<FileOutputBuffer>> bufferOrErr =
28543837f427SRui Ueyama FileOutputBuffer::create(config->outputFile, fileSize, flags);
28556d12eaeeSRui Ueyama
28563837f427SRui Ueyama if (!bufferOrErr) {
28573837f427SRui Ueyama error("failed to open " + config->outputFile + ": " +
28583837f427SRui Ueyama llvm::toString(bufferOrErr.takeError()));
28597fb9eabdSPeter Collingbourne return;
28607fb9eabdSPeter Collingbourne }
28613837f427SRui Ueyama buffer = std::move(*bufferOrErr);
28623837f427SRui Ueyama Out::bufferStart = buffer->getBufferStart();
286384487f11SMichael J. Spencer }
286484487f11SMichael J. Spencer
writeSectionsBinary()286586ce267aSGeorge Rimar template <class ELFT> void Writer<ELFT>::writeSectionsBinary() {
28663837f427SRui Ueyama for (OutputSection *sec : outputSections)
28673837f427SRui Ueyama if (sec->flags & SHF_ALLOC)
28683837f427SRui Ueyama sec->writeTo<ELFT>(Out::bufferStart + sec->offset);
286986ce267aSGeorge Rimar }
287086ce267aSGeorge Rimar
fillTrap(uint8_t * i,uint8_t * end)28713837f427SRui Ueyama static void fillTrap(uint8_t *i, uint8_t *end) {
28723837f427SRui Ueyama for (; i + 4 <= end; i += 4)
28733837f427SRui Ueyama memcpy(i, &target->trapInstr, 4);
2874edd6c358SPetr Hosek }
2875edd6c358SPetr Hosek
28766238ed24SRui Ueyama // Fill the last page of executable segments with trap instructions
28776238ed24SRui Ueyama // instead of leaving them as zero. Even though it is not required by any
28786238ed24SRui Ueyama // standard, it is in general a good thing to do for security reasons.
28796238ed24SRui Ueyama //
28806238ed24SRui Ueyama // We'll leave other pages in segments as-is because the rest will be
28816238ed24SRui Ueyama // overwritten by output sections.
writeTrapInstr()2882edd6c358SPetr Hosek template <class ELFT> void Writer<ELFT>::writeTrapInstr() {
28833837f427SRui Ueyama for (Partition &part : partitions) {
28846238ed24SRui Ueyama // Fill the last page.
28853837f427SRui Ueyama for (PhdrEntry *p : part.phdrs)
28863837f427SRui Ueyama if (p->p_type == PT_LOAD && (p->p_flags & PF_X))
2887a4c5db30SFangrui Song fillTrap(Out::bufferStart +
2888a4c5db30SFangrui Song alignDown(p->firstSec->offset + p->p_filesz, 4),
288985cfd917SFangrui Song Out::bufferStart +
289085cfd917SFangrui Song alignToPowerOf2(p->firstSec->offset + p->p_filesz,
2891f9a4d9aaSFangrui Song config->maxPageSize));
2892edd6c358SPetr Hosek
28936238ed24SRui Ueyama // Round up the file size of the last segment to the page boundary iff it is
2894df15559cSRui Ueyama // an executable segment to ensure that other tools don't accidentally
28956238ed24SRui Ueyama // trim the instruction padding (e.g. when stripping the file).
28963837f427SRui Ueyama PhdrEntry *last = nullptr;
28973837f427SRui Ueyama for (PhdrEntry *p : part.phdrs)
28983837f427SRui Ueyama if (p->p_type == PT_LOAD)
28993837f427SRui Ueyama last = p;
29000f46e45bSRui Ueyama
29013837f427SRui Ueyama if (last && (last->p_flags & PF_X))
29023837f427SRui Ueyama last->p_memsz = last->p_filesz =
290385cfd917SFangrui Song alignToPowerOf2(last->p_filesz, config->maxPageSize);
2904edd6c358SPetr Hosek }
290502828985SPeter Collingbourne }
2906edd6c358SPetr Hosek
290784487f11SMichael J. Spencer // Write section contents to a mmap'ed file.
writeSections()290884487f11SMichael J. Spencer template <class ELFT> void Writer<ELFT>::writeSections() {
29098825ffdbSFangrui Song llvm::TimeTraceScope timeScope("Write sections");
29108825ffdbSFangrui Song
2911bf6e259bSFangrui Song // In -r or --emit-relocs mode, write the relocation sections first as in
291208d6a3f1SRafael Espindola // ELf_Rel targets we might find out that we need to modify the relocated
291308d6a3f1SRafael Espindola // section while doing it.
29143837f427SRui Ueyama for (OutputSection *sec : outputSections)
29153837f427SRui Ueyama if (sec->type == SHT_REL || sec->type == SHT_RELA)
29163837f427SRui Ueyama sec->writeTo<ELFT>(Out::bufferStart + sec->offset);
291708d6a3f1SRafael Espindola
29183837f427SRui Ueyama for (OutputSection *sec : outputSections)
29193837f427SRui Ueyama if (sec->type != SHT_REL && sec->type != SHT_RELA)
29203837f427SRui Ueyama sec->writeTo<ELFT>(Out::bufferStart + sec->offset);
292135c5e564SAlex Richardson
292235c5e564SAlex Richardson // Finally, check that all dynamic relocation addends were written correctly.
292335c5e564SAlex Richardson if (config->checkDynamicRelocs && config->writeAddends) {
292435c5e564SAlex Richardson for (OutputSection *sec : outputSections)
292535c5e564SAlex Richardson if (sec->type == SHT_REL || sec->type == SHT_RELA)
292635c5e564SAlex Richardson sec->checkDynRelAddends(Out::bufferStart);
292735c5e564SAlex Richardson }
292884487f11SMichael J. Spencer }
29293ce825edSRui Ueyama
293097d25e06SPeter Collingbourne // Computes a hash value of Data using a given hash function.
293197d25e06SPeter Collingbourne // In order to utilize multiple cores, we first split data into 1MB
293297d25e06SPeter Collingbourne // chunks, compute a hash for each chunk, and then compute a hash value
293397d25e06SPeter Collingbourne // of the hash values.
293497d25e06SPeter Collingbourne static void
computeHash(llvm::MutableArrayRef<uint8_t> hashBuf,llvm::ArrayRef<uint8_t> data,std::function<void (uint8_t * dest,ArrayRef<uint8_t> arr)> hashFn)29353837f427SRui Ueyama computeHash(llvm::MutableArrayRef<uint8_t> hashBuf,
29363837f427SRui Ueyama llvm::ArrayRef<uint8_t> data,
29373837f427SRui Ueyama std::function<void(uint8_t *dest, ArrayRef<uint8_t> arr)> hashFn) {
29383837f427SRui Ueyama std::vector<ArrayRef<uint8_t>> chunks = split(data, 1024 * 1024);
293907bd4676SFangrui Song const size_t hashesSize = chunks.size() * hashBuf.size();
294007bd4676SFangrui Song std::unique_ptr<uint8_t[]> hashes(new uint8_t[hashesSize]);
294197d25e06SPeter Collingbourne
294297d25e06SPeter Collingbourne // Compute hash values.
29437effcbdaSNico Weber parallelFor(0, chunks.size(), [&](size_t i) {
294407bd4676SFangrui Song hashFn(hashes.get() + i * hashBuf.size(), chunks[i]);
294597d25e06SPeter Collingbourne });
294697d25e06SPeter Collingbourne
294797d25e06SPeter Collingbourne // Write to the final output buffer.
294807bd4676SFangrui Song hashFn(hashBuf.data(), makeArrayRef(hashes.get(), hashesSize));
294997d25e06SPeter Collingbourne }
295097d25e06SPeter Collingbourne
writeBuildId()29513edca1acSBen Dunbobbin template <class ELFT> void Writer<ELFT>::writeBuildId() {
29523837f427SRui Ueyama if (!mainPart->buildId || !mainPart->buildId->getParent())
29533edca1acSBen Dunbobbin return;
29543edca1acSBen Dunbobbin
29553837f427SRui Ueyama if (config->buildId == BuildIdKind::Hexstring) {
29563837f427SRui Ueyama for (Partition &part : partitions)
29573837f427SRui Ueyama part.buildId->writeBuildId(config->buildIdVector);
29583edca1acSBen Dunbobbin return;
29593edca1acSBen Dunbobbin }
29603edca1acSBen Dunbobbin
29613edca1acSBen Dunbobbin // Compute a hash of all sections of the output file.
29623837f427SRui Ueyama size_t hashSize = mainPart->buildId->hashSize;
296307bd4676SFangrui Song std::unique_ptr<uint8_t[]> buildId(new uint8_t[hashSize]);
296407bd4676SFangrui Song MutableArrayRef<uint8_t> output(buildId.get(), hashSize);
296507bd4676SFangrui Song llvm::ArrayRef<uint8_t> input{Out::bufferStart, size_t(fileSize)};
29663edca1acSBen Dunbobbin
2967d3e5b6f7SFangrui Song // Fedora introduced build ID as "approximation of true uniqueness across all
2968d3e5b6f7SFangrui Song // binaries that might be used by overlapping sets of people". It does not
2969d3e5b6f7SFangrui Song // need some security goals that some hash algorithms strive to provide, e.g.
2970d3e5b6f7SFangrui Song // (second-)preimage and collision resistance. In practice people use 'md5'
2971d3e5b6f7SFangrui Song // and 'sha1' just for different lengths. Implement them with the more
2972d3e5b6f7SFangrui Song // efficient BLAKE3.
29733837f427SRui Ueyama switch (config->buildId) {
297497d25e06SPeter Collingbourne case BuildIdKind::Fast:
297507bd4676SFangrui Song computeHash(output, input, [](uint8_t *dest, ArrayRef<uint8_t> arr) {
29763837f427SRui Ueyama write64le(dest, xxHash64(arr));
297797d25e06SPeter Collingbourne });
297897d25e06SPeter Collingbourne break;
297997d25e06SPeter Collingbourne case BuildIdKind::Md5:
298007bd4676SFangrui Song computeHash(output, input, [&](uint8_t *dest, ArrayRef<uint8_t> arr) {
2981d3e5b6f7SFangrui Song memcpy(dest, BLAKE3::hash<16>(arr).data(), hashSize);
298297d25e06SPeter Collingbourne });
298397d25e06SPeter Collingbourne break;
298497d25e06SPeter Collingbourne case BuildIdKind::Sha1:
298507bd4676SFangrui Song computeHash(output, input, [&](uint8_t *dest, ArrayRef<uint8_t> arr) {
2986d3e5b6f7SFangrui Song memcpy(dest, BLAKE3::hash<20>(arr).data(), hashSize);
298797d25e06SPeter Collingbourne });
298897d25e06SPeter Collingbourne break;
298997d25e06SPeter Collingbourne case BuildIdKind::Uuid:
299007bd4676SFangrui Song if (auto ec = llvm::getRandomBytes(buildId.get(), hashSize))
29913837f427SRui Ueyama error("entropy source failure: " + ec.message());
299297d25e06SPeter Collingbourne break;
299397d25e06SPeter Collingbourne default:
299497d25e06SPeter Collingbourne llvm_unreachable("unknown BuildIdKind");
299597d25e06SPeter Collingbourne }
29963837f427SRui Ueyama for (Partition &part : partitions)
299707bd4676SFangrui Song part.buildId->writeBuildId(output);
2998634ddf0bSRui Ueyama }
2999634ddf0bSRui Ueyama
300007837b8fSFangrui Song template void elf::createSyntheticSections<ELF32LE>();
300107837b8fSFangrui Song template void elf::createSyntheticSections<ELF32BE>();
300207837b8fSFangrui Song template void elf::createSyntheticSections<ELF64LE>();
300307837b8fSFangrui Song template void elf::createSyntheticSections<ELF64BE>();
30045d9f419aSFangrui Song
300507837b8fSFangrui Song template void elf::writeResult<ELF32LE>();
300607837b8fSFangrui Song template void elf::writeResult<ELF32BE>();
300707837b8fSFangrui Song template void elf::writeResult<ELF64LE>();
300807837b8fSFangrui Song template void elf::writeResult<ELF64BE>();
3009