10b57cec5SDimitry Andric //===- Writer.cpp ---------------------------------------------------------===//
20b57cec5SDimitry Andric //
30b57cec5SDimitry Andric // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
40b57cec5SDimitry Andric // See https://llvm.org/LICENSE.txt for license information.
50b57cec5SDimitry Andric // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
60b57cec5SDimitry Andric //
70b57cec5SDimitry Andric //===----------------------------------------------------------------------===//
80b57cec5SDimitry Andric
90b57cec5SDimitry Andric #include "Writer.h"
100b57cec5SDimitry Andric #include "AArch64ErrataFix.h"
1185868e8aSDimitry Andric #include "ARMErrataFix.h"
120b57cec5SDimitry Andric #include "CallGraphSort.h"
130b57cec5SDimitry Andric #include "Config.h"
1481ad6265SDimitry Andric #include "InputFiles.h"
150b57cec5SDimitry Andric #include "LinkerScript.h"
160b57cec5SDimitry Andric #include "MapFile.h"
170b57cec5SDimitry Andric #include "OutputSections.h"
180b57cec5SDimitry Andric #include "Relocations.h"
190b57cec5SDimitry Andric #include "SymbolTable.h"
200b57cec5SDimitry Andric #include "Symbols.h"
210b57cec5SDimitry Andric #include "SyntheticSections.h"
220b57cec5SDimitry Andric #include "Target.h"
23fe6060f1SDimitry Andric #include "lld/Common/Arrays.h"
2404eeddc0SDimitry Andric #include "lld/Common/CommonLinkerContext.h"
250b57cec5SDimitry Andric #include "lld/Common/Filesystem.h"
260b57cec5SDimitry Andric #include "lld/Common/Strings.h"
270fca6ea1SDimitry Andric #include "llvm/ADT/STLExtras.h"
280b57cec5SDimitry Andric #include "llvm/ADT/StringMap.h"
2981ad6265SDimitry Andric #include "llvm/Support/BLAKE3.h"
305ffd83dbSDimitry Andric #include "llvm/Support/Parallel.h"
310b57cec5SDimitry Andric #include "llvm/Support/RandomNumberGenerator.h"
325ffd83dbSDimitry Andric #include "llvm/Support/TimeProfiler.h"
330b57cec5SDimitry Andric #include "llvm/Support/xxhash.h"
340b57cec5SDimitry Andric #include <climits>
350b57cec5SDimitry Andric
365ffd83dbSDimitry Andric #define DEBUG_TYPE "lld"
375ffd83dbSDimitry Andric
380b57cec5SDimitry Andric using namespace llvm;
390b57cec5SDimitry Andric using namespace llvm::ELF;
400b57cec5SDimitry Andric using namespace llvm::object;
410b57cec5SDimitry Andric using namespace llvm::support;
420b57cec5SDimitry Andric using namespace llvm::support::endian;
435ffd83dbSDimitry Andric using namespace lld;
445ffd83dbSDimitry Andric using namespace lld::elf;
450b57cec5SDimitry Andric
460b57cec5SDimitry Andric namespace {
470b57cec5SDimitry Andric // The writer writes a SymbolTable result to a file.
480b57cec5SDimitry Andric template <class ELFT> class Writer {
490b57cec5SDimitry Andric public:
50e8d8bef9SDimitry Andric LLVM_ELF_IMPORT_TYPES_ELFT(ELFT)
51e8d8bef9SDimitry Andric
Writer()520b57cec5SDimitry Andric Writer() : buffer(errorHandler().outputBuffer) {}
530b57cec5SDimitry Andric
540b57cec5SDimitry Andric void run();
550b57cec5SDimitry Andric
560b57cec5SDimitry Andric private:
570b57cec5SDimitry Andric void addSectionSymbols();
580b57cec5SDimitry Andric void sortSections();
590b57cec5SDimitry Andric void resolveShfLinkOrder();
600b57cec5SDimitry Andric void finalizeAddressDependentContent();
615ffd83dbSDimitry Andric void optimizeBasicBlockJumps();
620b57cec5SDimitry Andric void sortInputSections();
6306c3fb27SDimitry Andric void sortOrphanSections();
640b57cec5SDimitry Andric void finalizeSections();
650b57cec5SDimitry Andric void checkExecuteOnly();
660b57cec5SDimitry Andric void setReservedSymbolSections();
670b57cec5SDimitry Andric
6804eeddc0SDimitry Andric SmallVector<PhdrEntry *, 0> createPhdrs(Partition &part);
690b57cec5SDimitry Andric void addPhdrForSection(Partition &part, unsigned shType, unsigned pType,
700b57cec5SDimitry Andric unsigned pFlags);
710b57cec5SDimitry Andric void assignFileOffsets();
720b57cec5SDimitry Andric void assignFileOffsetsBinary();
730b57cec5SDimitry Andric void setPhdrs(Partition &part);
740b57cec5SDimitry Andric void checkSections();
750b57cec5SDimitry Andric void fixSectionAlignments();
760b57cec5SDimitry Andric void openFile();
770b57cec5SDimitry Andric void writeTrapInstr();
780b57cec5SDimitry Andric void writeHeader();
790b57cec5SDimitry Andric void writeSections();
800b57cec5SDimitry Andric void writeSectionsBinary();
810b57cec5SDimitry Andric void writeBuildId();
820b57cec5SDimitry Andric
830b57cec5SDimitry Andric std::unique_ptr<FileOutputBuffer> &buffer;
840b57cec5SDimitry Andric
850b57cec5SDimitry Andric void addRelIpltSymbols();
860b57cec5SDimitry Andric void addStartEndSymbols();
8781ad6265SDimitry Andric void addStartStopSymbols(OutputSection &osec);
880b57cec5SDimitry Andric
890b57cec5SDimitry Andric uint64_t fileSize;
900b57cec5SDimitry Andric uint64_t sectionHeaderOff;
910b57cec5SDimitry Andric };
920b57cec5SDimitry Andric } // anonymous namespace
930b57cec5SDimitry Andric
writeResult()945ffd83dbSDimitry Andric template <class ELFT> void elf::writeResult() {
955ffd83dbSDimitry Andric Writer<ELFT>().run();
960b57cec5SDimitry Andric }
970b57cec5SDimitry Andric
removeEmptyPTLoad(SmallVector<PhdrEntry *,0> & phdrs)9804eeddc0SDimitry Andric static void removeEmptyPTLoad(SmallVector<PhdrEntry *, 0> &phdrs) {
995ffd83dbSDimitry Andric auto it = std::stable_partition(
1005ffd83dbSDimitry Andric phdrs.begin(), phdrs.end(), [&](const PhdrEntry *p) {
1015ffd83dbSDimitry Andric if (p->p_type != PT_LOAD)
1025ffd83dbSDimitry Andric return true;
1035ffd83dbSDimitry Andric if (!p->firstSec)
1045ffd83dbSDimitry Andric return false;
1055ffd83dbSDimitry Andric uint64_t size = p->lastSec->addr + p->lastSec->size - p->firstSec->addr;
1065ffd83dbSDimitry Andric return size != 0;
1075ffd83dbSDimitry Andric });
1085ffd83dbSDimitry Andric
1095ffd83dbSDimitry Andric // Clear OutputSection::ptLoad for sections contained in removed
1105ffd83dbSDimitry Andric // segments.
1115ffd83dbSDimitry Andric DenseSet<PhdrEntry *> removed(it, phdrs.end());
1125ffd83dbSDimitry Andric for (OutputSection *sec : outputSections)
1135ffd83dbSDimitry Andric if (removed.count(sec->ptLoad))
1145ffd83dbSDimitry Andric sec->ptLoad = nullptr;
1155ffd83dbSDimitry Andric phdrs.erase(it, phdrs.end());
1165ffd83dbSDimitry Andric }
1175ffd83dbSDimitry Andric
copySectionsIntoPartitions()1185ffd83dbSDimitry Andric void elf::copySectionsIntoPartitions() {
11904eeddc0SDimitry Andric SmallVector<InputSectionBase *, 0> newSections;
120bdd1243dSDimitry Andric const size_t ehSize = ctx.ehInputSections.size();
1210b57cec5SDimitry Andric for (unsigned part = 2; part != partitions.size() + 1; ++part) {
122bdd1243dSDimitry Andric for (InputSectionBase *s : ctx.inputSections) {
123bdd1243dSDimitry Andric if (!(s->flags & SHF_ALLOC) || !s->isLive() || s->type != SHT_NOTE)
1240b57cec5SDimitry Andric continue;
125bdd1243dSDimitry Andric auto *copy = make<InputSection>(cast<InputSection>(*s));
1260b57cec5SDimitry Andric copy->partition = part;
1270b57cec5SDimitry Andric newSections.push_back(copy);
1280b57cec5SDimitry Andric }
129bdd1243dSDimitry Andric for (size_t i = 0; i != ehSize; ++i) {
130bdd1243dSDimitry Andric assert(ctx.ehInputSections[i]->isLive());
131bdd1243dSDimitry Andric auto *copy = make<EhInputSection>(*ctx.ehInputSections[i]);
132bdd1243dSDimitry Andric copy->partition = part;
133bdd1243dSDimitry Andric ctx.ehInputSections.push_back(copy);
134bdd1243dSDimitry Andric }
1350b57cec5SDimitry Andric }
1360b57cec5SDimitry Andric
137bdd1243dSDimitry Andric ctx.inputSections.insert(ctx.inputSections.end(), newSections.begin(),
1380b57cec5SDimitry Andric newSections.end());
1390b57cec5SDimitry Andric }
1400b57cec5SDimitry Andric
addOptionalRegular(StringRef name,SectionBase * sec,uint64_t val,uint8_t stOther=STV_HIDDEN)1410b57cec5SDimitry Andric static Defined *addOptionalRegular(StringRef name, SectionBase *sec,
142349cc55cSDimitry Andric uint64_t val, uint8_t stOther = STV_HIDDEN) {
143bdd1243dSDimitry Andric Symbol *s = symtab.find(name);
14481ad6265SDimitry Andric if (!s || s->isDefined() || s->isCommon())
1450b57cec5SDimitry Andric return nullptr;
1460b57cec5SDimitry Andric
1477a6dacacSDimitry Andric s->resolve(Defined{ctx.internalFile, StringRef(), STB_GLOBAL, stOther,
1487a6dacacSDimitry Andric STT_NOTYPE, val,
1490b57cec5SDimitry Andric /*size=*/0, sec});
15081ad6265SDimitry Andric s->isUsedInRegularObj = true;
1510b57cec5SDimitry Andric return cast<Defined>(s);
1520b57cec5SDimitry Andric }
1530b57cec5SDimitry Andric
1540b57cec5SDimitry Andric // The linker is expected to define some symbols depending on
1550b57cec5SDimitry Andric // the linking result. This function defines such symbols.
addReservedSymbols()1565ffd83dbSDimitry Andric void elf::addReservedSymbols() {
1570b57cec5SDimitry Andric if (config->emachine == EM_MIPS) {
1587a6dacacSDimitry Andric auto addAbsolute = [](StringRef name) {
1597a6dacacSDimitry Andric Symbol *sym =
1607a6dacacSDimitry Andric symtab.addSymbol(Defined{ctx.internalFile, name, STB_GLOBAL,
1617a6dacacSDimitry Andric STV_HIDDEN, STT_NOTYPE, 0, 0, nullptr});
1627a6dacacSDimitry Andric sym->isUsedInRegularObj = true;
1637a6dacacSDimitry Andric return cast<Defined>(sym);
1647a6dacacSDimitry Andric };
1650b57cec5SDimitry Andric // Define _gp for MIPS. st_value of _gp symbol will be updated by Writer
1660b57cec5SDimitry Andric // so that it points to an absolute address which by default is relative
1670b57cec5SDimitry Andric // to GOT. Default offset is 0x7ff0.
1680b57cec5SDimitry Andric // See "Global Data Symbols" in Chapter 6 in the following document:
1690b57cec5SDimitry Andric // ftp://www.linux-mips.org/pub/linux/mips/doc/ABI/mipsabi.pdf
1700b57cec5SDimitry Andric ElfSym::mipsGp = addAbsolute("_gp");
1710b57cec5SDimitry Andric
1720b57cec5SDimitry Andric // On MIPS O32 ABI, _gp_disp is a magic symbol designates offset between
1730b57cec5SDimitry Andric // start of function and 'gp' pointer into GOT.
174bdd1243dSDimitry Andric if (symtab.find("_gp_disp"))
1750b57cec5SDimitry Andric ElfSym::mipsGpDisp = addAbsolute("_gp_disp");
1760b57cec5SDimitry Andric
1770b57cec5SDimitry Andric // The __gnu_local_gp is a magic symbol equal to the current value of 'gp'
1780b57cec5SDimitry Andric // pointer. This symbol is used in the code generated by .cpload pseudo-op
1790b57cec5SDimitry Andric // in case of using -mno-shared option.
1800b57cec5SDimitry Andric // https://sourceware.org/ml/binutils/2004-12/msg00094.html
181bdd1243dSDimitry Andric if (symtab.find("__gnu_local_gp"))
1820b57cec5SDimitry Andric ElfSym::mipsLocalGp = addAbsolute("__gnu_local_gp");
1830b57cec5SDimitry Andric } else if (config->emachine == EM_PPC) {
1840b57cec5SDimitry Andric // glibc *crt1.o has a undefined reference to _SDA_BASE_. Since we don't
1850b57cec5SDimitry Andric // support Small Data Area, define it arbitrarily as 0.
1860b57cec5SDimitry Andric addOptionalRegular("_SDA_BASE_", nullptr, 0, STV_HIDDEN);
1875ffd83dbSDimitry Andric } else if (config->emachine == EM_PPC64) {
1885ffd83dbSDimitry Andric addPPC64SaveRestore();
1890b57cec5SDimitry Andric }
1900b57cec5SDimitry Andric
1910b57cec5SDimitry Andric // The Power Architecture 64-bit v2 ABI defines a TableOfContents (TOC) which
1920b57cec5SDimitry Andric // combines the typical ELF GOT with the small data sections. It commonly
1930b57cec5SDimitry Andric // includes .got .toc .sdata .sbss. The .TOC. symbol replaces both
1940b57cec5SDimitry Andric // _GLOBAL_OFFSET_TABLE_ and _SDA_BASE_ from the 32-bit ABI. It is used to
1950b57cec5SDimitry Andric // represent the TOC base which is offset by 0x8000 bytes from the start of
1960b57cec5SDimitry Andric // the .got section.
1970b57cec5SDimitry Andric // We do not allow _GLOBAL_OFFSET_TABLE_ to be defined by input objects as the
1980b57cec5SDimitry Andric // correctness of some relocations depends on its value.
1990b57cec5SDimitry Andric StringRef gotSymName =
2000b57cec5SDimitry Andric (config->emachine == EM_PPC64) ? ".TOC." : "_GLOBAL_OFFSET_TABLE_";
2010b57cec5SDimitry Andric
202bdd1243dSDimitry Andric if (Symbol *s = symtab.find(gotSymName)) {
2030b57cec5SDimitry Andric if (s->isDefined()) {
2040b57cec5SDimitry Andric error(toString(s->file) + " cannot redefine linker defined symbol '" +
2050b57cec5SDimitry Andric gotSymName + "'");
2060b57cec5SDimitry Andric return;
2070b57cec5SDimitry Andric }
2080b57cec5SDimitry Andric
2090b57cec5SDimitry Andric uint64_t gotOff = 0;
2100b57cec5SDimitry Andric if (config->emachine == EM_PPC64)
2110b57cec5SDimitry Andric gotOff = 0x8000;
2120b57cec5SDimitry Andric
2137a6dacacSDimitry Andric s->resolve(Defined{ctx.internalFile, StringRef(), STB_GLOBAL, STV_HIDDEN,
2140b57cec5SDimitry Andric STT_NOTYPE, gotOff, /*size=*/0, Out::elfHeader});
2150b57cec5SDimitry Andric ElfSym::globalOffsetTable = cast<Defined>(s);
2160b57cec5SDimitry Andric }
2170b57cec5SDimitry Andric
2180b57cec5SDimitry Andric // __ehdr_start is the location of ELF file headers. Note that we define
2190b57cec5SDimitry Andric // this symbol unconditionally even when using a linker script, which
2200b57cec5SDimitry Andric // differs from the behavior implemented by GNU linker which only define
2210b57cec5SDimitry Andric // this symbol if ELF headers are in the memory mapped segment.
2220b57cec5SDimitry Andric addOptionalRegular("__ehdr_start", Out::elfHeader, 0, STV_HIDDEN);
2230b57cec5SDimitry Andric
2240b57cec5SDimitry Andric // __executable_start is not documented, but the expectation of at
2250b57cec5SDimitry Andric // least the Android libc is that it points to the ELF header.
2260b57cec5SDimitry Andric addOptionalRegular("__executable_start", Out::elfHeader, 0, STV_HIDDEN);
2270b57cec5SDimitry Andric
2280b57cec5SDimitry Andric // __dso_handle symbol is passed to cxa_finalize as a marker to identify
2290b57cec5SDimitry Andric // each DSO. The address of the symbol doesn't matter as long as they are
2300b57cec5SDimitry Andric // different in different DSOs, so we chose the start address of the DSO.
2310b57cec5SDimitry Andric addOptionalRegular("__dso_handle", Out::elfHeader, 0, STV_HIDDEN);
2320b57cec5SDimitry Andric
233480093f4SDimitry Andric // If linker script do layout we do not need to create any standard symbols.
2340b57cec5SDimitry Andric if (script->hasSectionsCommand)
2350b57cec5SDimitry Andric return;
2360b57cec5SDimitry Andric
2370b57cec5SDimitry Andric auto add = [](StringRef s, int64_t pos) {
2380b57cec5SDimitry Andric return addOptionalRegular(s, Out::elfHeader, pos, STV_DEFAULT);
2390b57cec5SDimitry Andric };
2400b57cec5SDimitry Andric
2410b57cec5SDimitry Andric ElfSym::bss = add("__bss_start", 0);
2420b57cec5SDimitry Andric ElfSym::end1 = add("end", -1);
2430b57cec5SDimitry Andric ElfSym::end2 = add("_end", -1);
2440b57cec5SDimitry Andric ElfSym::etext1 = add("etext", -1);
2450b57cec5SDimitry Andric ElfSym::etext2 = add("_etext", -1);
2460b57cec5SDimitry Andric ElfSym::edata1 = add("edata", -1);
2470b57cec5SDimitry Andric ElfSym::edata2 = add("_edata", -1);
2480b57cec5SDimitry Andric }
2490b57cec5SDimitry Andric
demoteDefined(Defined & sym,DenseMap<SectionBase *,size_t> & map)2505f757f3fSDimitry Andric static void demoteDefined(Defined &sym, DenseMap<SectionBase *, size_t> &map) {
2515f757f3fSDimitry Andric if (map.empty())
2525f757f3fSDimitry Andric for (auto [i, sec] : llvm::enumerate(sym.file->getSections()))
2535f757f3fSDimitry Andric map.try_emplace(sec, i);
2545f757f3fSDimitry Andric // Change WEAK to GLOBAL so that if a scanned relocation references sym,
2555f757f3fSDimitry Andric // maybeReportUndefined will report an error.
2565f757f3fSDimitry Andric uint8_t binding = sym.isWeak() ? uint8_t(STB_GLOBAL) : sym.binding;
2575f757f3fSDimitry Andric Undefined(sym.file, sym.getName(), binding, sym.stOther, sym.type,
2585f757f3fSDimitry Andric /*discardedSecIdx=*/map.lookup(sym.section))
2595f757f3fSDimitry Andric .overwrite(sym);
260439352acSDimitry Andric // Eliminate from the symbol table, otherwise we would leave an undefined
261439352acSDimitry Andric // symbol if the symbol is unreferenced in the absence of GC.
262439352acSDimitry Andric sym.isUsedInRegularObj = false;
2635f757f3fSDimitry Andric }
2645f757f3fSDimitry Andric
2655f757f3fSDimitry Andric // If all references to a DSO happen to be weak, the DSO is not added to
2665f757f3fSDimitry Andric // DT_NEEDED. If that happens, replace ShardSymbol with Undefined to avoid
2675f757f3fSDimitry Andric // dangling references to an unneeded DSO. Use a weak binding to avoid
2685f757f3fSDimitry Andric // --no-allow-shlib-undefined diagnostics. Similarly, demote lazy symbols.
2695f757f3fSDimitry Andric //
2705f757f3fSDimitry Andric // In addition, demote symbols defined in discarded sections, so that
2715f757f3fSDimitry Andric // references to /DISCARD/ discarded symbols will lead to errors.
demoteSymbolsAndComputeIsPreemptible()2725f757f3fSDimitry Andric static void demoteSymbolsAndComputeIsPreemptible() {
2735f757f3fSDimitry Andric llvm::TimeTraceScope timeScope("Demote symbols");
2745f757f3fSDimitry Andric DenseMap<InputFile *, DenseMap<SectionBase *, size_t>> sectionIndexMap;
2755f757f3fSDimitry Andric for (Symbol *sym : symtab.getSymbols()) {
2765f757f3fSDimitry Andric if (auto *d = dyn_cast<Defined>(sym)) {
2775f757f3fSDimitry Andric if (d->section && !d->section->isLive())
2785f757f3fSDimitry Andric demoteDefined(*d, sectionIndexMap[d->file]);
2795f757f3fSDimitry Andric } else {
2805f757f3fSDimitry Andric auto *s = dyn_cast<SharedSymbol>(sym);
2815f757f3fSDimitry Andric if (sym->isLazy() || (s && !cast<SharedFile>(s->file)->isNeeded)) {
2825f757f3fSDimitry Andric uint8_t binding = sym->isLazy() ? sym->binding : uint8_t(STB_WEAK);
2837a6dacacSDimitry Andric Undefined(ctx.internalFile, sym->getName(), binding, sym->stOther,
2847a6dacacSDimitry Andric sym->type)
2855f757f3fSDimitry Andric .overwrite(*sym);
2865f757f3fSDimitry Andric sym->versionId = VER_NDX_GLOBAL;
2875f757f3fSDimitry Andric }
2885f757f3fSDimitry Andric }
2895f757f3fSDimitry Andric
2905f757f3fSDimitry Andric if (config->hasDynSymTab)
2915f757f3fSDimitry Andric sym->isPreemptible = computeIsPreemptible(*sym);
2925f757f3fSDimitry Andric }
2935f757f3fSDimitry Andric }
2945f757f3fSDimitry Andric
findSection(StringRef name,unsigned partition=1)2950b57cec5SDimitry Andric static OutputSection *findSection(StringRef name, unsigned partition = 1) {
2964824e7fdSDimitry Andric for (SectionCommand *cmd : script->sectionCommands)
29781ad6265SDimitry Andric if (auto *osd = dyn_cast<OutputDesc>(cmd))
29881ad6265SDimitry Andric if (osd->osec.name == name && osd->osec.partition == partition)
29981ad6265SDimitry Andric return &osd->osec;
3000b57cec5SDimitry Andric return nullptr;
3010b57cec5SDimitry Andric }
3020b57cec5SDimitry Andric
3030b57cec5SDimitry Andric // The main function of the writer.
run()3040b57cec5SDimitry Andric template <class ELFT> void Writer<ELFT>::run() {
3050b57cec5SDimitry Andric // Now that we have a complete set of output sections. This function
3060b57cec5SDimitry Andric // completes section contents. For example, we need to add strings
3070b57cec5SDimitry Andric // to the string table, and add entries to .got and .plt.
3080b57cec5SDimitry Andric // finalizeSections does that.
3090b57cec5SDimitry Andric finalizeSections();
3100b57cec5SDimitry Andric checkExecuteOnly();
3110b57cec5SDimitry Andric
312349cc55cSDimitry Andric // If --compressed-debug-sections is specified, compress .debug_* sections.
313349cc55cSDimitry Andric // Do it right now because it changes the size of output sections.
3140b57cec5SDimitry Andric for (OutputSection *sec : outputSections)
3150b57cec5SDimitry Andric sec->maybeCompress<ELFT>();
3160b57cec5SDimitry Andric
31769660011SDimitry Andric if (script->hasSectionsCommand)
3180b57cec5SDimitry Andric script->allocateHeaders(mainPart->phdrs);
3190b57cec5SDimitry Andric
3200b57cec5SDimitry Andric // Remove empty PT_LOAD to avoid causing the dynamic linker to try to mmap a
3210b57cec5SDimitry Andric // 0 sized region. This has to be done late since only after assignAddresses
3220b57cec5SDimitry Andric // we know the size of the sections.
3230b57cec5SDimitry Andric for (Partition &part : partitions)
3240b57cec5SDimitry Andric removeEmptyPTLoad(part.phdrs);
3250b57cec5SDimitry Andric
3260b57cec5SDimitry Andric if (!config->oFormatBinary)
3270b57cec5SDimitry Andric assignFileOffsets();
3280b57cec5SDimitry Andric else
3290b57cec5SDimitry Andric assignFileOffsetsBinary();
3300b57cec5SDimitry Andric
3310b57cec5SDimitry Andric for (Partition &part : partitions)
3320b57cec5SDimitry Andric setPhdrs(part);
3330b57cec5SDimitry Andric
33481ad6265SDimitry Andric // Handle --print-map(-M)/--Map and --cref. Dump them before checkSections()
33581ad6265SDimitry Andric // because the files may be useful in case checkSections() or openFile()
33681ad6265SDimitry Andric // fails, for example, due to an erroneous file size.
3374824e7fdSDimitry Andric writeMapAndCref();
3385ffd83dbSDimitry Andric
33906c3fb27SDimitry Andric // Handle --print-memory-usage option.
34006c3fb27SDimitry Andric if (config->printMemoryUsage)
34106c3fb27SDimitry Andric script->printMemoryUsage(lld::outs());
34206c3fb27SDimitry Andric
3430b57cec5SDimitry Andric if (config->checkSections)
3440b57cec5SDimitry Andric checkSections();
3450b57cec5SDimitry Andric
3460b57cec5SDimitry Andric // It does not make sense try to open the file if we have error already.
3470b57cec5SDimitry Andric if (errorCount())
3480b57cec5SDimitry Andric return;
349e8d8bef9SDimitry Andric
350e8d8bef9SDimitry Andric {
351e8d8bef9SDimitry Andric llvm::TimeTraceScope timeScope("Write output file");
3520b57cec5SDimitry Andric // Write the result down to a file.
3530b57cec5SDimitry Andric openFile();
3540b57cec5SDimitry Andric if (errorCount())
3550b57cec5SDimitry Andric return;
3560b57cec5SDimitry Andric
3570b57cec5SDimitry Andric if (!config->oFormatBinary) {
35885868e8aSDimitry Andric if (config->zSeparate != SeparateSegmentKind::None)
3590b57cec5SDimitry Andric writeTrapInstr();
3600b57cec5SDimitry Andric writeHeader();
3610b57cec5SDimitry Andric writeSections();
3620b57cec5SDimitry Andric } else {
3630b57cec5SDimitry Andric writeSectionsBinary();
3640b57cec5SDimitry Andric }
3650b57cec5SDimitry Andric
3660b57cec5SDimitry Andric // Backfill .note.gnu.build-id section content. This is done at last
3670b57cec5SDimitry Andric // because the content is usually a hash value of the entire output file.
3680b57cec5SDimitry Andric writeBuildId();
3690b57cec5SDimitry Andric if (errorCount())
3700b57cec5SDimitry Andric return;
3710b57cec5SDimitry Andric
3720b57cec5SDimitry Andric if (auto e = buffer->commit())
373bdd1243dSDimitry Andric fatal("failed to write output '" + buffer->getPath() +
374bdd1243dSDimitry Andric "': " + toString(std::move(e)));
37506c3fb27SDimitry Andric
37606c3fb27SDimitry Andric if (!config->cmseOutputLib.empty())
37706c3fb27SDimitry Andric writeARMCmseImportLib<ELFT>();
3780b57cec5SDimitry Andric }
379e8d8bef9SDimitry Andric }
3800b57cec5SDimitry Andric
3815ffd83dbSDimitry Andric template <class ELFT, class RelTy>
markUsedLocalSymbolsImpl(ObjFile<ELFT> * file,llvm::ArrayRef<RelTy> rels)3825ffd83dbSDimitry Andric static void markUsedLocalSymbolsImpl(ObjFile<ELFT> *file,
3835ffd83dbSDimitry Andric llvm::ArrayRef<RelTy> rels) {
3845ffd83dbSDimitry Andric for (const RelTy &rel : rels) {
3855ffd83dbSDimitry Andric Symbol &sym = file->getRelocTargetSym(rel);
3865ffd83dbSDimitry Andric if (sym.isLocal())
3875ffd83dbSDimitry Andric sym.used = true;
3885ffd83dbSDimitry Andric }
3895ffd83dbSDimitry Andric }
3905ffd83dbSDimitry Andric
3915ffd83dbSDimitry Andric // The function ensures that the "used" field of local symbols reflects the fact
3925ffd83dbSDimitry Andric // that the symbol is used in a relocation from a live section.
markUsedLocalSymbols()3935ffd83dbSDimitry Andric template <class ELFT> static void markUsedLocalSymbols() {
3945ffd83dbSDimitry Andric // With --gc-sections, the field is already filled.
3955ffd83dbSDimitry Andric // See MarkLive<ELFT>::resolveReloc().
3965ffd83dbSDimitry Andric if (config->gcSections)
3975ffd83dbSDimitry Andric return;
398bdd1243dSDimitry Andric for (ELFFileBase *file : ctx.objectFiles) {
3995ffd83dbSDimitry Andric ObjFile<ELFT> *f = cast<ObjFile<ELFT>>(file);
4005ffd83dbSDimitry Andric for (InputSectionBase *s : f->getSections()) {
4015ffd83dbSDimitry Andric InputSection *isec = dyn_cast_or_null<InputSection>(s);
4025ffd83dbSDimitry Andric if (!isec)
4035ffd83dbSDimitry Andric continue;
404*52418fc2SDimitry Andric if (isec->type == SHT_REL) {
4055ffd83dbSDimitry Andric markUsedLocalSymbolsImpl(f, isec->getDataAs<typename ELFT::Rel>());
406*52418fc2SDimitry Andric } else if (isec->type == SHT_RELA) {
4075ffd83dbSDimitry Andric markUsedLocalSymbolsImpl(f, isec->getDataAs<typename ELFT::Rela>());
408*52418fc2SDimitry Andric } else if (isec->type == SHT_CREL) {
409*52418fc2SDimitry Andric // The is64=true variant also works with ELF32 since only the r_symidx
410*52418fc2SDimitry Andric // member is used.
411*52418fc2SDimitry Andric for (Elf_Crel_Impl<true> r : RelocsCrel<true>(isec->content_)) {
412*52418fc2SDimitry Andric Symbol &sym = file->getSymbol(r.r_symidx);
413*52418fc2SDimitry Andric if (sym.isLocal())
414*52418fc2SDimitry Andric sym.used = true;
415*52418fc2SDimitry Andric }
416*52418fc2SDimitry Andric }
4175ffd83dbSDimitry Andric }
4185ffd83dbSDimitry Andric }
4195ffd83dbSDimitry Andric }
4205ffd83dbSDimitry Andric
shouldKeepInSymtab(const Defined & sym)4210b57cec5SDimitry Andric static bool shouldKeepInSymtab(const Defined &sym) {
4220b57cec5SDimitry Andric if (sym.isSection())
4230b57cec5SDimitry Andric return false;
4240b57cec5SDimitry Andric
4255ffd83dbSDimitry Andric // If --emit-reloc or -r is given, preserve symbols referenced by relocations
4265ffd83dbSDimitry Andric // from live sections.
42781ad6265SDimitry Andric if (sym.used && config->copyRelocs)
4280b57cec5SDimitry Andric return true;
4290b57cec5SDimitry Andric
4305ffd83dbSDimitry Andric // Exclude local symbols pointing to .ARM.exidx sections.
4315ffd83dbSDimitry Andric // They are probably mapping symbols "$d", which are optional for these
4325ffd83dbSDimitry Andric // sections. After merging the .ARM.exidx sections, some of these symbols
4335ffd83dbSDimitry Andric // may become dangling. The easiest way to avoid the issue is not to add
4345ffd83dbSDimitry Andric // them to the symbol table from the beginning.
4355ffd83dbSDimitry Andric if (config->emachine == EM_ARM && sym.section &&
4365ffd83dbSDimitry Andric sym.section->type == SHT_ARM_EXIDX)
4375ffd83dbSDimitry Andric return false;
4385ffd83dbSDimitry Andric
4395ffd83dbSDimitry Andric if (config->discard == DiscardPolicy::None)
4400b57cec5SDimitry Andric return true;
4415ffd83dbSDimitry Andric if (config->discard == DiscardPolicy::All)
4425ffd83dbSDimitry Andric return false;
4430b57cec5SDimitry Andric
4440b57cec5SDimitry Andric // In ELF assembly .L symbols are normally discarded by the assembler.
4450b57cec5SDimitry Andric // If the assembler fails to do so, the linker discards them if
4460b57cec5SDimitry Andric // * --discard-locals is used.
4470b57cec5SDimitry Andric // * The symbol is in a SHF_MERGE section, which is normally the reason for
4480b57cec5SDimitry Andric // the assembler keeping the .L symbol.
44906c3fb27SDimitry Andric if (sym.getName().starts_with(".L") &&
450349cc55cSDimitry Andric (config->discard == DiscardPolicy::Locals ||
451349cc55cSDimitry Andric (sym.section && (sym.section->flags & SHF_MERGE))))
4520b57cec5SDimitry Andric return false;
453349cc55cSDimitry Andric return true;
4540b57cec5SDimitry Andric }
4550b57cec5SDimitry Andric
includeInSymtab(const Symbol & b)4565f757f3fSDimitry Andric bool lld::elf::includeInSymtab(const Symbol &b) {
4570b57cec5SDimitry Andric if (auto *d = dyn_cast<Defined>(&b)) {
4580b57cec5SDimitry Andric // Always include absolute symbols.
4590b57cec5SDimitry Andric SectionBase *sec = d->section;
4600b57cec5SDimitry Andric if (!sec)
4610b57cec5SDimitry Andric return true;
4625f757f3fSDimitry Andric assert(sec->isLive());
4630b57cec5SDimitry Andric
4640b57cec5SDimitry Andric if (auto *s = dyn_cast<MergeInputSection>(sec))
46581ad6265SDimitry Andric return s->getSectionPiece(d->value).live;
4665f757f3fSDimitry Andric return true;
4670b57cec5SDimitry Andric }
46881ad6265SDimitry Andric return b.used || !config->gcSections;
4690b57cec5SDimitry Andric }
4700b57cec5SDimitry Andric
4715f757f3fSDimitry Andric // Scan local symbols to:
4725f757f3fSDimitry Andric //
4735f757f3fSDimitry Andric // - demote symbols defined relative to /DISCARD/ discarded input sections so
4745f757f3fSDimitry Andric // that relocations referencing them will lead to errors.
4755f757f3fSDimitry Andric // - copy eligible symbols to .symTab
demoteAndCopyLocalSymbols()4765f757f3fSDimitry Andric static void demoteAndCopyLocalSymbols() {
477e8d8bef9SDimitry Andric llvm::TimeTraceScope timeScope("Add local symbols");
478bdd1243dSDimitry Andric for (ELFFileBase *file : ctx.objectFiles) {
4795f757f3fSDimitry Andric DenseMap<SectionBase *, size_t> sectionIndexMap;
4800eae32dcSDimitry Andric for (Symbol *b : file->getLocalSymbols()) {
4815ffd83dbSDimitry Andric assert(b->isLocal() && "should have been caught in initializeSymbols()");
4820b57cec5SDimitry Andric auto *dr = dyn_cast<Defined>(b);
4830b57cec5SDimitry Andric if (!dr)
4840b57cec5SDimitry Andric continue;
4855f757f3fSDimitry Andric
4865f757f3fSDimitry Andric if (dr->section && !dr->section->isLive())
4875f757f3fSDimitry Andric demoteDefined(*dr, sectionIndexMap);
4885f757f3fSDimitry Andric else if (in.symTab && includeInSymtab(*b) && shouldKeepInSymtab(*dr))
4890b57cec5SDimitry Andric in.symTab->addSymbol(b);
4900b57cec5SDimitry Andric }
4910b57cec5SDimitry Andric }
4920b57cec5SDimitry Andric }
4930b57cec5SDimitry Andric
4940b57cec5SDimitry Andric // Create a section symbol for each output section so that we can represent
4950b57cec5SDimitry Andric // relocations that point to the section. If we know that no relocation is
4960b57cec5SDimitry Andric // referring to a section (that happens if the section is a synthetic one), we
4970b57cec5SDimitry Andric // don't create a section symbol for that section.
addSectionSymbols()4980b57cec5SDimitry Andric template <class ELFT> void Writer<ELFT>::addSectionSymbols() {
4994824e7fdSDimitry Andric for (SectionCommand *cmd : script->sectionCommands) {
50081ad6265SDimitry Andric auto *osd = dyn_cast<OutputDesc>(cmd);
50181ad6265SDimitry Andric if (!osd)
5020b57cec5SDimitry Andric continue;
50381ad6265SDimitry Andric OutputSection &osec = osd->osec;
5043a9a9c0cSDimitry Andric InputSectionBase *isec = nullptr;
5053a9a9c0cSDimitry Andric // Iterate over all input sections and add a STT_SECTION symbol if any input
5063a9a9c0cSDimitry Andric // section may be a relocation target.
5073a9a9c0cSDimitry Andric for (SectionCommand *cmd : osec.commands) {
5083a9a9c0cSDimitry Andric auto *isd = dyn_cast<InputSectionDescription>(cmd);
5093a9a9c0cSDimitry Andric if (!isd)
5100b57cec5SDimitry Andric continue;
5113a9a9c0cSDimitry Andric for (InputSectionBase *s : isd->sections) {
5120b57cec5SDimitry Andric // Relocations are not using REL[A] section symbols.
5130fca6ea1SDimitry Andric if (isStaticRelSecType(s->type))
5140b57cec5SDimitry Andric continue;
5150b57cec5SDimitry Andric
5163a9a9c0cSDimitry Andric // Unlike other synthetic sections, mergeable output sections contain
5173a9a9c0cSDimitry Andric // data copied from input sections, and there may be a relocation
5183a9a9c0cSDimitry Andric // pointing to its contents if -r or --emit-reloc is given.
5193a9a9c0cSDimitry Andric if (isa<SyntheticSection>(s) && !(s->flags & SHF_MERGE))
5203a9a9c0cSDimitry Andric continue;
5213a9a9c0cSDimitry Andric
5223a9a9c0cSDimitry Andric isec = s;
5233a9a9c0cSDimitry Andric break;
5243a9a9c0cSDimitry Andric }
5253a9a9c0cSDimitry Andric }
5263a9a9c0cSDimitry Andric if (!isec)
5270b57cec5SDimitry Andric continue;
5280b57cec5SDimitry Andric
529e8d8bef9SDimitry Andric // Set the symbol to be relative to the output section so that its st_value
530e8d8bef9SDimitry Andric // equals the output section address. Note, there may be a gap between the
531e8d8bef9SDimitry Andric // start of the output section and isec.
53281ad6265SDimitry Andric in.symTab->addSymbol(makeDefined(isec->file, "", STB_LOCAL, /*stOther=*/0,
53381ad6265SDimitry Andric STT_SECTION,
53481ad6265SDimitry Andric /*value=*/0, /*size=*/0, &osec));
5350b57cec5SDimitry Andric }
5360b57cec5SDimitry Andric }
5370b57cec5SDimitry Andric
5380b57cec5SDimitry Andric // Today's loaders have a feature to make segments read-only after
5390b57cec5SDimitry Andric // processing dynamic relocations to enhance security. PT_GNU_RELRO
5400b57cec5SDimitry Andric // is defined for that.
5410b57cec5SDimitry Andric //
5420b57cec5SDimitry Andric // This function returns true if a section needs to be put into a
5430b57cec5SDimitry Andric // PT_GNU_RELRO segment.
isRelroSection(const OutputSection * sec)5440b57cec5SDimitry Andric static bool isRelroSection(const OutputSection *sec) {
5450b57cec5SDimitry Andric if (!config->zRelro)
5460b57cec5SDimitry Andric return false;
54781ad6265SDimitry Andric if (sec->relro)
54881ad6265SDimitry Andric return true;
5490b57cec5SDimitry Andric
5500b57cec5SDimitry Andric uint64_t flags = sec->flags;
5510b57cec5SDimitry Andric
5520b57cec5SDimitry Andric // Non-allocatable or non-writable sections don't need RELRO because
5530b57cec5SDimitry Andric // they are not writable or not even mapped to memory in the first place.
5540b57cec5SDimitry Andric // RELRO is for sections that are essentially read-only but need to
5550b57cec5SDimitry Andric // be writable only at process startup to allow dynamic linker to
5560b57cec5SDimitry Andric // apply relocations.
5570b57cec5SDimitry Andric if (!(flags & SHF_ALLOC) || !(flags & SHF_WRITE))
5580b57cec5SDimitry Andric return false;
5590b57cec5SDimitry Andric
5600b57cec5SDimitry Andric // Once initialized, TLS data segments are used as data templates
5610b57cec5SDimitry Andric // for a thread-local storage. For each new thread, runtime
5620b57cec5SDimitry Andric // allocates memory for a TLS and copy templates there. No thread
5630b57cec5SDimitry Andric // are supposed to use templates directly. Thus, it can be in RELRO.
5640b57cec5SDimitry Andric if (flags & SHF_TLS)
5650b57cec5SDimitry Andric return true;
5660b57cec5SDimitry Andric
5670b57cec5SDimitry Andric // .init_array, .preinit_array and .fini_array contain pointers to
5680b57cec5SDimitry Andric // functions that are executed on process startup or exit. These
5690b57cec5SDimitry Andric // pointers are set by the static linker, and they are not expected
5700b57cec5SDimitry Andric // to change at runtime. But if you are an attacker, you could do
5710b57cec5SDimitry Andric // interesting things by manipulating pointers in .fini_array, for
5720b57cec5SDimitry Andric // example. So they are put into RELRO.
5730b57cec5SDimitry Andric uint32_t type = sec->type;
5740b57cec5SDimitry Andric if (type == SHT_INIT_ARRAY || type == SHT_FINI_ARRAY ||
5750b57cec5SDimitry Andric type == SHT_PREINIT_ARRAY)
5760b57cec5SDimitry Andric return true;
5770b57cec5SDimitry Andric
5780b57cec5SDimitry Andric // .got contains pointers to external symbols. They are resolved by
5790b57cec5SDimitry Andric // the dynamic linker when a module is loaded into memory, and after
5800b57cec5SDimitry Andric // that they are not expected to change. So, it can be in RELRO.
5810b57cec5SDimitry Andric if (in.got && sec == in.got->getParent())
5820b57cec5SDimitry Andric return true;
5830b57cec5SDimitry Andric
5840b57cec5SDimitry Andric // .toc is a GOT-ish section for PowerPC64. Their contents are accessed
5850b57cec5SDimitry Andric // through r2 register, which is reserved for that purpose. Since r2 is used
5860b57cec5SDimitry Andric // for accessing .got as well, .got and .toc need to be close enough in the
5870b57cec5SDimitry Andric // virtual address space. Usually, .toc comes just after .got. Since we place
5880b57cec5SDimitry Andric // .got into RELRO, .toc needs to be placed into RELRO too.
5890fca6ea1SDimitry Andric if (sec->name == ".toc")
5900b57cec5SDimitry Andric return true;
5910b57cec5SDimitry Andric
5920b57cec5SDimitry Andric // .got.plt contains pointers to external function symbols. They are
5930b57cec5SDimitry Andric // by default resolved lazily, so we usually cannot put it into RELRO.
5940b57cec5SDimitry Andric // However, if "-z now" is given, the lazy symbol resolution is
5950b57cec5SDimitry Andric // disabled, which enables us to put it into RELRO.
5960b57cec5SDimitry Andric if (sec == in.gotPlt->getParent())
5970b57cec5SDimitry Andric return config->zNow;
5980b57cec5SDimitry Andric
5995f757f3fSDimitry Andric if (in.relroPadding && sec == in.relroPadding->getParent())
6005f757f3fSDimitry Andric return true;
6015f757f3fSDimitry Andric
6020b57cec5SDimitry Andric // .dynamic section contains data for the dynamic linker, and
6030b57cec5SDimitry Andric // there's no need to write to it at runtime, so it's better to put
6040b57cec5SDimitry Andric // it into RELRO.
6050b57cec5SDimitry Andric if (sec->name == ".dynamic")
6060b57cec5SDimitry Andric return true;
6070b57cec5SDimitry Andric
6080b57cec5SDimitry Andric // Sections with some special names are put into RELRO. This is a
6090b57cec5SDimitry Andric // bit unfortunate because section names shouldn't be significant in
6100b57cec5SDimitry Andric // ELF in spirit. But in reality many linker features depend on
6110b57cec5SDimitry Andric // magic section names.
6120b57cec5SDimitry Andric StringRef s = sec->name;
6130fca6ea1SDimitry Andric
6140fca6ea1SDimitry Andric bool abiAgnostic = s == ".data.rel.ro" || s == ".bss.rel.ro" ||
6150fca6ea1SDimitry Andric s == ".ctors" || s == ".dtors" || s == ".jcr" ||
6160fca6ea1SDimitry Andric s == ".eh_frame" || s == ".fini_array" ||
6170fca6ea1SDimitry Andric s == ".init_array" || s == ".preinit_array";
6180fca6ea1SDimitry Andric
6190fca6ea1SDimitry Andric bool abiSpecific =
6200fca6ea1SDimitry Andric config->osabi == ELFOSABI_OPENBSD && s == ".openbsd.randomdata";
6210fca6ea1SDimitry Andric
6220fca6ea1SDimitry Andric return abiAgnostic || abiSpecific;
6230b57cec5SDimitry Andric }
6240b57cec5SDimitry Andric
6250b57cec5SDimitry Andric // We compute a rank for each section. The rank indicates where the
6260b57cec5SDimitry Andric // section should be placed in the file. Instead of using simple
6270b57cec5SDimitry Andric // numbers (0,1,2...), we use a series of flags. One for each decision
6280b57cec5SDimitry Andric // point when placing the section.
6290b57cec5SDimitry Andric // Using flags has two key properties:
6300b57cec5SDimitry Andric // * It is easy to check if a give branch was taken.
6310b57cec5SDimitry Andric // * It is easy two see how similar two ranks are (see getRankProximity).
6320b57cec5SDimitry Andric enum RankFlags {
6330b57cec5SDimitry Andric RF_NOT_ADDR_SET = 1 << 27,
6340b57cec5SDimitry Andric RF_NOT_ALLOC = 1 << 26,
6350b57cec5SDimitry Andric RF_PARTITION = 1 << 18, // Partition number (8 bits)
6360fca6ea1SDimitry Andric RF_LARGE_ALT = 1 << 15,
6370fca6ea1SDimitry Andric RF_WRITE = 1 << 14,
6380fca6ea1SDimitry Andric RF_EXEC_WRITE = 1 << 13,
6390fca6ea1SDimitry Andric RF_EXEC = 1 << 12,
6400fca6ea1SDimitry Andric RF_RODATA = 1 << 11,
6410fca6ea1SDimitry Andric RF_LARGE = 1 << 10,
6420b57cec5SDimitry Andric RF_NOT_RELRO = 1 << 9,
6430b57cec5SDimitry Andric RF_NOT_TLS = 1 << 8,
6440b57cec5SDimitry Andric RF_BSS = 1 << 7,
6450b57cec5SDimitry Andric };
6460b57cec5SDimitry Andric
getSectionRank(OutputSection & osec)6470fca6ea1SDimitry Andric unsigned elf::getSectionRank(OutputSection &osec) {
64881ad6265SDimitry Andric unsigned rank = osec.partition * RF_PARTITION;
6490b57cec5SDimitry Andric
6500b57cec5SDimitry Andric // We want to put section specified by -T option first, so we
6510b57cec5SDimitry Andric // can start assigning VA starting from them later.
65281ad6265SDimitry Andric if (config->sectionStartMap.count(osec.name))
6530b57cec5SDimitry Andric return rank;
6540b57cec5SDimitry Andric rank |= RF_NOT_ADDR_SET;
6550b57cec5SDimitry Andric
6560b57cec5SDimitry Andric // Allocatable sections go first to reduce the total PT_LOAD size and
6570b57cec5SDimitry Andric // so debug info doesn't change addresses in actual code.
65881ad6265SDimitry Andric if (!(osec.flags & SHF_ALLOC))
6590b57cec5SDimitry Andric return rank | RF_NOT_ALLOC;
6600b57cec5SDimitry Andric
6610b57cec5SDimitry Andric // Sort sections based on their access permission in the following
66206c3fb27SDimitry Andric // order: R, RX, RXW, RW(RELRO), RW(non-RELRO).
66306c3fb27SDimitry Andric //
66406c3fb27SDimitry Andric // Read-only sections come first such that they go in the PT_LOAD covering the
66506c3fb27SDimitry Andric // program headers at the start of the file.
66606c3fb27SDimitry Andric //
66706c3fb27SDimitry Andric // The layout for writable sections is PT_LOAD(PT_GNU_RELRO(.data.rel.ro
66806c3fb27SDimitry Andric // .bss.rel.ro) | .data .bss), where | marks where page alignment happens.
66906c3fb27SDimitry Andric // An alternative ordering is PT_LOAD(.data | PT_GNU_RELRO( .data.rel.ro
67006c3fb27SDimitry Andric // .bss.rel.ro) | .bss), but it may waste more bytes due to 2 alignment
67106c3fb27SDimitry Andric // places.
67281ad6265SDimitry Andric bool isExec = osec.flags & SHF_EXECINSTR;
67381ad6265SDimitry Andric bool isWrite = osec.flags & SHF_WRITE;
6740b57cec5SDimitry Andric
67506c3fb27SDimitry Andric if (!isWrite && !isExec) {
6760fca6ea1SDimitry Andric // Among PROGBITS sections, place .lrodata further from .text.
6770fca6ea1SDimitry Andric // For -z lrodata-after-bss, place .lrodata after .lbss like GNU ld. This
6780fca6ea1SDimitry Andric // layout has one extra PT_LOAD, but alleviates relocation overflow
6790fca6ea1SDimitry Andric // pressure for absolute relocations referencing small data from -fno-pic
6800fca6ea1SDimitry Andric // relocatable files.
6810fca6ea1SDimitry Andric if (osec.flags & SHF_X86_64_LARGE && config->emachine == EM_X86_64)
6820fca6ea1SDimitry Andric rank |= config->zLrodataAfterBss ? RF_LARGE_ALT : 0;
6830fca6ea1SDimitry Andric else
6840fca6ea1SDimitry Andric rank |= config->zLrodataAfterBss ? 0 : RF_LARGE;
6850fca6ea1SDimitry Andric
6860fca6ea1SDimitry Andric if (osec.type == SHT_LLVM_PART_EHDR)
6870fca6ea1SDimitry Andric ;
6880fca6ea1SDimitry Andric else if (osec.type == SHT_LLVM_PART_PHDR)
6890fca6ea1SDimitry Andric rank |= 1;
6900fca6ea1SDimitry Andric else if (osec.name == ".interp")
6910fca6ea1SDimitry Andric rank |= 2;
6920fca6ea1SDimitry Andric // Put .note sections at the beginning so that they are likely to be
6930fca6ea1SDimitry Andric // included in a truncate core file. In particular, .note.gnu.build-id, if
6940fca6ea1SDimitry Andric // available, can identify the object file.
6950fca6ea1SDimitry Andric else if (osec.type == SHT_NOTE)
6960fca6ea1SDimitry Andric rank |= 3;
69706c3fb27SDimitry Andric // Make PROGBITS sections (e.g .rodata .eh_frame) closer to .text to
69806c3fb27SDimitry Andric // alleviate relocation overflow pressure. Large special sections such as
69906c3fb27SDimitry Andric // .dynstr and .dynsym can be away from .text.
7000fca6ea1SDimitry Andric else if (osec.type != SHT_PROGBITS)
7010fca6ea1SDimitry Andric rank |= 4;
7020fca6ea1SDimitry Andric else
7030b57cec5SDimitry Andric rank |= RF_RODATA;
70406c3fb27SDimitry Andric } else if (isExec) {
70506c3fb27SDimitry Andric rank |= isWrite ? RF_EXEC_WRITE : RF_EXEC;
70606c3fb27SDimitry Andric } else {
70706c3fb27SDimitry Andric rank |= RF_WRITE;
70806c3fb27SDimitry Andric // The TLS initialization block needs to be a single contiguous block. Place
70906c3fb27SDimitry Andric // TLS sections directly before the other RELRO sections.
71081ad6265SDimitry Andric if (!(osec.flags & SHF_TLS))
7110b57cec5SDimitry Andric rank |= RF_NOT_TLS;
7125f757f3fSDimitry Andric if (isRelroSection(&osec))
7135f757f3fSDimitry Andric osec.relro = true;
7145f757f3fSDimitry Andric else
71506c3fb27SDimitry Andric rank |= RF_NOT_RELRO;
7160fca6ea1SDimitry Andric // Place .ldata and .lbss after .bss. Making .bss closer to .text
7170fca6ea1SDimitry Andric // alleviates relocation overflow pressure.
7180fca6ea1SDimitry Andric // For -z lrodata-after-bss, place .lbss/.lrodata/.ldata after .bss.
7190fca6ea1SDimitry Andric // .bss/.lbss being adjacent reuses the NOBITS size optimization.
7200fca6ea1SDimitry Andric if (osec.flags & SHF_X86_64_LARGE && config->emachine == EM_X86_64) {
7210fca6ea1SDimitry Andric rank |= config->zLrodataAfterBss
7220fca6ea1SDimitry Andric ? (osec.type == SHT_NOBITS ? 1 : RF_LARGE_ALT)
7230fca6ea1SDimitry Andric : RF_LARGE;
7240fca6ea1SDimitry Andric }
72506c3fb27SDimitry Andric }
7260b57cec5SDimitry Andric
7270b57cec5SDimitry Andric // Within TLS sections, or within other RelRo sections, or within non-RelRo
7280b57cec5SDimitry Andric // sections, place non-NOBITS sections first.
72981ad6265SDimitry Andric if (osec.type == SHT_NOBITS)
7300b57cec5SDimitry Andric rank |= RF_BSS;
7310b57cec5SDimitry Andric
7320b57cec5SDimitry Andric // Some architectures have additional ordering restrictions for sections
7330b57cec5SDimitry Andric // within the same PT_LOAD.
7340b57cec5SDimitry Andric if (config->emachine == EM_PPC64) {
7350b57cec5SDimitry Andric // PPC64 has a number of special SHT_PROGBITS+SHF_ALLOC+SHF_WRITE sections
7360b57cec5SDimitry Andric // that we would like to make sure appear is a specific order to maximize
7370b57cec5SDimitry Andric // their coverage by a single signed 16-bit offset from the TOC base
73806c3fb27SDimitry Andric // pointer.
73981ad6265SDimitry Andric StringRef name = osec.name;
7405f757f3fSDimitry Andric if (name == ".got")
74106c3fb27SDimitry Andric rank |= 1;
74206c3fb27SDimitry Andric else if (name == ".toc")
7435f757f3fSDimitry Andric rank |= 2;
7440b57cec5SDimitry Andric }
7450b57cec5SDimitry Andric
7460b57cec5SDimitry Andric if (config->emachine == EM_MIPS) {
74706c3fb27SDimitry Andric if (osec.name != ".got")
74806c3fb27SDimitry Andric rank |= 1;
7490b57cec5SDimitry Andric // All sections with SHF_MIPS_GPREL flag should be grouped together
7500b57cec5SDimitry Andric // because data in these sections is addressable with a gp relative address.
75181ad6265SDimitry Andric if (osec.flags & SHF_MIPS_GPREL)
75206c3fb27SDimitry Andric rank |= 2;
75306c3fb27SDimitry Andric }
7540b57cec5SDimitry Andric
75506c3fb27SDimitry Andric if (config->emachine == EM_RISCV) {
75606c3fb27SDimitry Andric // .sdata and .sbss are placed closer to make GP relaxation more profitable
75706c3fb27SDimitry Andric // and match GNU ld.
75806c3fb27SDimitry Andric StringRef name = osec.name;
75906c3fb27SDimitry Andric if (name == ".sdata" || (osec.type == SHT_NOBITS && name != ".sbss"))
76006c3fb27SDimitry Andric rank |= 1;
7610b57cec5SDimitry Andric }
7620b57cec5SDimitry Andric
7630b57cec5SDimitry Andric return rank;
7640b57cec5SDimitry Andric }
7650b57cec5SDimitry Andric
compareSections(const SectionCommand * aCmd,const SectionCommand * bCmd)7664824e7fdSDimitry Andric static bool compareSections(const SectionCommand *aCmd,
7674824e7fdSDimitry Andric const SectionCommand *bCmd) {
76881ad6265SDimitry Andric const OutputSection *a = &cast<OutputDesc>(aCmd)->osec;
76981ad6265SDimitry Andric const OutputSection *b = &cast<OutputDesc>(bCmd)->osec;
7700b57cec5SDimitry Andric
7710b57cec5SDimitry Andric if (a->sortRank != b->sortRank)
7720b57cec5SDimitry Andric return a->sortRank < b->sortRank;
7730b57cec5SDimitry Andric
7740b57cec5SDimitry Andric if (!(a->sortRank & RF_NOT_ADDR_SET))
7750b57cec5SDimitry Andric return config->sectionStartMap.lookup(a->name) <
7760b57cec5SDimitry Andric config->sectionStartMap.lookup(b->name);
7770b57cec5SDimitry Andric return false;
7780b57cec5SDimitry Andric }
7790b57cec5SDimitry Andric
add(OutputSection * sec)7800b57cec5SDimitry Andric void PhdrEntry::add(OutputSection *sec) {
7810b57cec5SDimitry Andric lastSec = sec;
7820b57cec5SDimitry Andric if (!firstSec)
7830b57cec5SDimitry Andric firstSec = sec;
784bdd1243dSDimitry Andric p_align = std::max(p_align, sec->addralign);
7850b57cec5SDimitry Andric if (p_type == PT_LOAD)
7860b57cec5SDimitry Andric sec->ptLoad = this;
7870b57cec5SDimitry Andric }
7880b57cec5SDimitry Andric
7890fca6ea1SDimitry Andric // A statically linked position-dependent executable should only contain
7900fca6ea1SDimitry Andric // IRELATIVE relocations and no other dynamic relocations. Encapsulation symbols
7910fca6ea1SDimitry Andric // __rel[a]_iplt_{start,end} will be defined for .rel[a].dyn, to be
7920fca6ea1SDimitry Andric // processed by the libc runtime. Other executables or DSOs use dynamic tags
7930fca6ea1SDimitry Andric // instead.
addRelIpltSymbols()7940b57cec5SDimitry Andric template <class ELFT> void Writer<ELFT>::addRelIpltSymbols() {
795bdd1243dSDimitry Andric if (config->isPic)
7960b57cec5SDimitry Andric return;
7970b57cec5SDimitry Andric
7980fca6ea1SDimitry Andric // __rela_iplt_{start,end} are initially defined relative to dummy section 0.
7990fca6ea1SDimitry Andric // We'll override Out::elfHeader with relaDyn later when we are sure that
8000fca6ea1SDimitry Andric // .rela.dyn will be present in the output.
8010fca6ea1SDimitry Andric std::string name = config->isRela ? "__rela_iplt_start" : "__rel_iplt_start";
8020fca6ea1SDimitry Andric ElfSym::relaIpltStart =
8030fca6ea1SDimitry Andric addOptionalRegular(name, Out::elfHeader, 0, STV_HIDDEN);
8040fca6ea1SDimitry Andric name.replace(name.size() - 5, 5, "end");
8050fca6ea1SDimitry Andric ElfSym::relaIpltEnd = addOptionalRegular(name, Out::elfHeader, 0, STV_HIDDEN);
8060b57cec5SDimitry Andric }
8070b57cec5SDimitry Andric
8080b57cec5SDimitry Andric // This function generates assignments for predefined symbols (e.g. _end or
8090b57cec5SDimitry Andric // _etext) and inserts them into the commands sequence to be processed at the
8100b57cec5SDimitry Andric // appropriate time. This ensures that the value is going to be correct by the
8110b57cec5SDimitry Andric // time any references to these symbols are processed and is equivalent to
8120b57cec5SDimitry Andric // defining these symbols explicitly in the linker script.
setReservedSymbolSections()8130b57cec5SDimitry Andric template <class ELFT> void Writer<ELFT>::setReservedSymbolSections() {
8140b57cec5SDimitry Andric if (ElfSym::globalOffsetTable) {
8150b57cec5SDimitry Andric // The _GLOBAL_OFFSET_TABLE_ symbol is defined by target convention usually
8160b57cec5SDimitry Andric // to the start of the .got or .got.plt section.
81704eeddc0SDimitry Andric InputSection *sec = in.gotPlt.get();
8180b57cec5SDimitry Andric if (!target->gotBaseSymInGotPlt)
819bdd1243dSDimitry Andric sec = in.mipsGot ? cast<InputSection>(in.mipsGot.get())
82004eeddc0SDimitry Andric : cast<InputSection>(in.got.get());
82104eeddc0SDimitry Andric ElfSym::globalOffsetTable->section = sec;
8220b57cec5SDimitry Andric }
8230b57cec5SDimitry Andric
8240fca6ea1SDimitry Andric // .rela_iplt_{start,end} mark the start and the end of .rel[a].dyn.
8250fca6ea1SDimitry Andric if (ElfSym::relaIpltStart && mainPart->relaDyn->isNeeded()) {
8260fca6ea1SDimitry Andric ElfSym::relaIpltStart->section = mainPart->relaDyn.get();
8270fca6ea1SDimitry Andric ElfSym::relaIpltEnd->section = mainPart->relaDyn.get();
8280fca6ea1SDimitry Andric ElfSym::relaIpltEnd->value = mainPart->relaDyn->getSize();
8290b57cec5SDimitry Andric }
8300b57cec5SDimitry Andric
8310b57cec5SDimitry Andric PhdrEntry *last = nullptr;
8320fca6ea1SDimitry Andric OutputSection *lastRO = nullptr;
8330fca6ea1SDimitry Andric auto isLarge = [](OutputSection *osec) {
8340fca6ea1SDimitry Andric return config->emachine == EM_X86_64 && osec->flags & SHF_X86_64_LARGE;
8350fca6ea1SDimitry Andric };
8360b57cec5SDimitry Andric for (Partition &part : partitions) {
8370b57cec5SDimitry Andric for (PhdrEntry *p : part.phdrs) {
8380b57cec5SDimitry Andric if (p->p_type != PT_LOAD)
8390b57cec5SDimitry Andric continue;
8400b57cec5SDimitry Andric last = p;
8410fca6ea1SDimitry Andric if (!(p->p_flags & PF_W) && p->lastSec && !isLarge(p->lastSec))
8420fca6ea1SDimitry Andric lastRO = p->lastSec;
8430b57cec5SDimitry Andric }
8440b57cec5SDimitry Andric }
8450b57cec5SDimitry Andric
8460b57cec5SDimitry Andric if (lastRO) {
8470fca6ea1SDimitry Andric // _etext is the first location after the last read-only loadable segment
8480fca6ea1SDimitry Andric // that does not contain large sections.
8490b57cec5SDimitry Andric if (ElfSym::etext1)
8500fca6ea1SDimitry Andric ElfSym::etext1->section = lastRO;
8510b57cec5SDimitry Andric if (ElfSym::etext2)
8520fca6ea1SDimitry Andric ElfSym::etext2->section = lastRO;
8530b57cec5SDimitry Andric }
8540b57cec5SDimitry Andric
8550b57cec5SDimitry Andric if (last) {
8560fca6ea1SDimitry Andric // _edata points to the end of the last non-large mapped initialized
8570fca6ea1SDimitry Andric // section.
8580b57cec5SDimitry Andric OutputSection *edata = nullptr;
8590b57cec5SDimitry Andric for (OutputSection *os : outputSections) {
8600fca6ea1SDimitry Andric if (os->type != SHT_NOBITS && !isLarge(os))
8610b57cec5SDimitry Andric edata = os;
8620b57cec5SDimitry Andric if (os == last->lastSec)
8630b57cec5SDimitry Andric break;
8640b57cec5SDimitry Andric }
8650b57cec5SDimitry Andric
8660b57cec5SDimitry Andric if (ElfSym::edata1)
8670b57cec5SDimitry Andric ElfSym::edata1->section = edata;
8680b57cec5SDimitry Andric if (ElfSym::edata2)
8690b57cec5SDimitry Andric ElfSym::edata2->section = edata;
8700b57cec5SDimitry Andric
8710b57cec5SDimitry Andric // _end is the first location after the uninitialized data region.
8720b57cec5SDimitry Andric if (ElfSym::end1)
8730b57cec5SDimitry Andric ElfSym::end1->section = last->lastSec;
8740b57cec5SDimitry Andric if (ElfSym::end2)
8750b57cec5SDimitry Andric ElfSym::end2->section = last->lastSec;
8760b57cec5SDimitry Andric }
8770b57cec5SDimitry Andric
87806c3fb27SDimitry Andric if (ElfSym::bss) {
87906c3fb27SDimitry Andric // On RISC-V, set __bss_start to the start of .sbss if present.
88006c3fb27SDimitry Andric OutputSection *sbss =
88106c3fb27SDimitry Andric config->emachine == EM_RISCV ? findSection(".sbss") : nullptr;
88206c3fb27SDimitry Andric ElfSym::bss->section = sbss ? sbss : findSection(".bss");
88306c3fb27SDimitry Andric }
8840b57cec5SDimitry Andric
8850b57cec5SDimitry Andric // Setup MIPS _gp_disp/__gnu_local_gp symbols which should
8860b57cec5SDimitry Andric // be equal to the _gp symbol's value.
8870b57cec5SDimitry Andric if (ElfSym::mipsGp) {
8880b57cec5SDimitry Andric // Find GP-relative section with the lowest address
8890b57cec5SDimitry Andric // and use this address to calculate default _gp value.
8900b57cec5SDimitry Andric for (OutputSection *os : outputSections) {
8910b57cec5SDimitry Andric if (os->flags & SHF_MIPS_GPREL) {
8920b57cec5SDimitry Andric ElfSym::mipsGp->section = os;
8930b57cec5SDimitry Andric ElfSym::mipsGp->value = 0x7ff0;
8940b57cec5SDimitry Andric break;
8950b57cec5SDimitry Andric }
8960b57cec5SDimitry Andric }
8970b57cec5SDimitry Andric }
8980b57cec5SDimitry Andric }
8990b57cec5SDimitry Andric
9000b57cec5SDimitry Andric // We want to find how similar two ranks are.
9010b57cec5SDimitry Andric // The more branches in getSectionRank that match, the more similar they are.
9020b57cec5SDimitry Andric // Since each branch corresponds to a bit flag, we can just use
9030b57cec5SDimitry Andric // countLeadingZeros.
getRankProximity(OutputSection * a,SectionCommand * b)9044824e7fdSDimitry Andric static int getRankProximity(OutputSection *a, SectionCommand *b) {
90581ad6265SDimitry Andric auto *osd = dyn_cast<OutputDesc>(b);
90681ad6265SDimitry Andric return (osd && osd->osec.hasInputSections)
90706c3fb27SDimitry Andric ? llvm::countl_zero(a->sortRank ^ osd->osec.sortRank)
90881ad6265SDimitry Andric : -1;
9090b57cec5SDimitry Andric }
9100b57cec5SDimitry Andric
9110b57cec5SDimitry Andric // When placing orphan sections, we want to place them after symbol assignments
9120b57cec5SDimitry Andric // so that an orphan after
9130b57cec5SDimitry Andric // begin_foo = .;
9140b57cec5SDimitry Andric // foo : { *(foo) }
9150b57cec5SDimitry Andric // end_foo = .;
9160b57cec5SDimitry Andric // doesn't break the intended meaning of the begin/end symbols.
9170b57cec5SDimitry Andric // We don't want to go over sections since findOrphanPos is the
9180b57cec5SDimitry Andric // one in charge of deciding the order of the sections.
9190b57cec5SDimitry Andric // We don't want to go over changes to '.', since doing so in
9200b57cec5SDimitry Andric // rx_sec : { *(rx_sec) }
9210b57cec5SDimitry Andric // . = ALIGN(0x1000);
9220b57cec5SDimitry Andric // /* The RW PT_LOAD starts here*/
9230b57cec5SDimitry Andric // rw_sec : { *(rw_sec) }
9240b57cec5SDimitry Andric // would mean that the RW PT_LOAD would become unaligned.
shouldSkip(SectionCommand * cmd)9254824e7fdSDimitry Andric static bool shouldSkip(SectionCommand *cmd) {
9260b57cec5SDimitry Andric if (auto *assign = dyn_cast<SymbolAssignment>(cmd))
9270b57cec5SDimitry Andric return assign->name != ".";
9280b57cec5SDimitry Andric return false;
9290b57cec5SDimitry Andric }
9300b57cec5SDimitry Andric
9310b57cec5SDimitry Andric // We want to place orphan sections so that they share as much
9320b57cec5SDimitry Andric // characteristics with their neighbors as possible. For example, if
9330b57cec5SDimitry Andric // both are rw, or both are tls.
93404eeddc0SDimitry Andric static SmallVectorImpl<SectionCommand *>::iterator
findOrphanPos(SmallVectorImpl<SectionCommand * >::iterator b,SmallVectorImpl<SectionCommand * >::iterator e)93504eeddc0SDimitry Andric findOrphanPos(SmallVectorImpl<SectionCommand *>::iterator b,
93604eeddc0SDimitry Andric SmallVectorImpl<SectionCommand *>::iterator e) {
9370fca6ea1SDimitry Andric // Place non-alloc orphan sections at the end. This matches how we assign file
9380fca6ea1SDimitry Andric // offsets to non-alloc sections.
93981ad6265SDimitry Andric OutputSection *sec = &cast<OutputDesc>(*e)->osec;
9400fca6ea1SDimitry Andric if (!(sec->flags & SHF_ALLOC))
9410fca6ea1SDimitry Andric return e;
9420b57cec5SDimitry Andric
9435f757f3fSDimitry Andric // As a special case, place .relro_padding before the SymbolAssignment using
9445f757f3fSDimitry Andric // DATA_SEGMENT_RELRO_END, if present.
9455f757f3fSDimitry Andric if (in.relroPadding && sec == in.relroPadding->getParent()) {
9465f757f3fSDimitry Andric auto i = std::find_if(b, e, [=](SectionCommand *a) {
9475f757f3fSDimitry Andric if (auto *assign = dyn_cast<SymbolAssignment>(a))
9485f757f3fSDimitry Andric return assign->dataSegmentRelroEnd;
9495f757f3fSDimitry Andric return false;
9505f757f3fSDimitry Andric });
9515f757f3fSDimitry Andric if (i != e)
9525f757f3fSDimitry Andric return i;
9535f757f3fSDimitry Andric }
9545f757f3fSDimitry Andric
9550fca6ea1SDimitry Andric // Find the most similar output section as the anchor. Rank Proximity is a
9560fca6ea1SDimitry Andric // value in the range [-1, 32] where [0, 32] indicates potential anchors (0:
9570fca6ea1SDimitry Andric // least similar; 32: identical). -1 means not an anchor.
9580fca6ea1SDimitry Andric //
9590fca6ea1SDimitry Andric // In the event of proximity ties, we select the first or last section
9600fca6ea1SDimitry Andric // depending on whether the orphan's rank is smaller.
9610fca6ea1SDimitry Andric int maxP = 0;
9620fca6ea1SDimitry Andric auto i = e;
9630fca6ea1SDimitry Andric for (auto j = b; j != e; ++j) {
9640fca6ea1SDimitry Andric int p = getRankProximity(sec, *j);
9650fca6ea1SDimitry Andric if (p > maxP ||
9660fca6ea1SDimitry Andric (p == maxP && cast<OutputDesc>(*j)->osec.sortRank <= sec->sortRank)) {
9670fca6ea1SDimitry Andric maxP = p;
9680fca6ea1SDimitry Andric i = j;
9690fca6ea1SDimitry Andric }
9700fca6ea1SDimitry Andric }
9710b57cec5SDimitry Andric if (i == e)
9720b57cec5SDimitry Andric return e;
9730b57cec5SDimitry Andric
9744824e7fdSDimitry Andric auto isOutputSecWithInputSections = [](SectionCommand *cmd) {
97581ad6265SDimitry Andric auto *osd = dyn_cast<OutputDesc>(cmd);
97681ad6265SDimitry Andric return osd && osd->osec.hasInputSections;
9770b57cec5SDimitry Andric };
9780fca6ea1SDimitry Andric
9790fca6ea1SDimitry Andric // Then, scan backward or forward through the script for a suitable insertion
9800fca6ea1SDimitry Andric // point. If i's rank is larger, the orphan section can be placed before i.
9810fca6ea1SDimitry Andric //
9820fca6ea1SDimitry Andric // However, don't do this if custom program headers are defined. Otherwise,
9830fca6ea1SDimitry Andric // adding the orphan to a previous segment can change its flags, for example,
9840fca6ea1SDimitry Andric // making a read-only segment writable. If memory regions are defined, an
9850fca6ea1SDimitry Andric // orphan section should continue the same region as the found section to
9860fca6ea1SDimitry Andric // better resemble the behavior of GNU ld.
9870fca6ea1SDimitry Andric bool mustAfter = script->hasPhdrsCommands() || !script->memoryRegions.empty();
9880fca6ea1SDimitry Andric if (cast<OutputDesc>(*i)->osec.sortRank <= sec->sortRank || mustAfter) {
9890fca6ea1SDimitry Andric for (auto j = ++i; j != e; ++j) {
9900fca6ea1SDimitry Andric if (!isOutputSecWithInputSections(*j))
9910fca6ea1SDimitry Andric continue;
9920fca6ea1SDimitry Andric if (getRankProximity(sec, *j) != maxP)
9930fca6ea1SDimitry Andric break;
9940fca6ea1SDimitry Andric i = j + 1;
9950fca6ea1SDimitry Andric }
9960fca6ea1SDimitry Andric } else {
9970fca6ea1SDimitry Andric for (; i != b; --i)
9980fca6ea1SDimitry Andric if (isOutputSecWithInputSections(i[-1]))
9990fca6ea1SDimitry Andric break;
10000fca6ea1SDimitry Andric }
10010b57cec5SDimitry Andric
10020b57cec5SDimitry Andric // As a special case, if the orphan section is the last section, put
10030b57cec5SDimitry Andric // it at the very end, past any other commands.
10040b57cec5SDimitry Andric // This matches bfd's behavior and is convenient when the linker script fully
10050b57cec5SDimitry Andric // specifies the start of the file, but doesn't care about the end (the non
10060b57cec5SDimitry Andric // alloc sections for example).
10070fca6ea1SDimitry Andric if (std::find_if(i, e, isOutputSecWithInputSections) == e)
10080b57cec5SDimitry Andric return e;
10090b57cec5SDimitry Andric
10100b57cec5SDimitry Andric while (i != e && shouldSkip(*i))
10110b57cec5SDimitry Andric ++i;
10120b57cec5SDimitry Andric return i;
10130b57cec5SDimitry Andric }
10140b57cec5SDimitry Andric
10155ffd83dbSDimitry Andric // Adds random priorities to sections not already in the map.
maybeShuffle(DenseMap<const InputSectionBase *,int> & order)10165ffd83dbSDimitry Andric static void maybeShuffle(DenseMap<const InputSectionBase *, int> &order) {
1017fe6060f1SDimitry Andric if (config->shuffleSections.empty())
10185ffd83dbSDimitry Andric return;
10195ffd83dbSDimitry Andric
1020bdd1243dSDimitry Andric SmallVector<InputSectionBase *, 0> matched, sections = ctx.inputSections;
1021fe6060f1SDimitry Andric matched.reserve(sections.size());
1022fe6060f1SDimitry Andric for (const auto &patAndSeed : config->shuffleSections) {
1023fe6060f1SDimitry Andric matched.clear();
1024fe6060f1SDimitry Andric for (InputSectionBase *sec : sections)
1025fe6060f1SDimitry Andric if (patAndSeed.first.match(sec->name))
1026fe6060f1SDimitry Andric matched.push_back(sec);
1027fe6060f1SDimitry Andric const uint32_t seed = patAndSeed.second;
1028fe6060f1SDimitry Andric if (seed == UINT32_MAX) {
1029fe6060f1SDimitry Andric // If --shuffle-sections <section-glob>=-1, reverse the section order. The
1030fe6060f1SDimitry Andric // section order is stable even if the number of sections changes. This is
1031fe6060f1SDimitry Andric // useful to catch issues like static initialization order fiasco
1032fe6060f1SDimitry Andric // reliably.
1033fe6060f1SDimitry Andric std::reverse(matched.begin(), matched.end());
1034fe6060f1SDimitry Andric } else {
1035fe6060f1SDimitry Andric std::mt19937 g(seed ? seed : std::random_device()());
1036fe6060f1SDimitry Andric llvm::shuffle(matched.begin(), matched.end(), g);
1037fe6060f1SDimitry Andric }
1038fe6060f1SDimitry Andric size_t i = 0;
1039fe6060f1SDimitry Andric for (InputSectionBase *&sec : sections)
1040fe6060f1SDimitry Andric if (patAndSeed.first.match(sec->name))
1041fe6060f1SDimitry Andric sec = matched[i++];
1042fe6060f1SDimitry Andric }
1043fe6060f1SDimitry Andric
10445ffd83dbSDimitry Andric // Existing priorities are < 0, so use priorities >= 0 for the missing
10455ffd83dbSDimitry Andric // sections.
1046fe6060f1SDimitry Andric int prio = 0;
1047fe6060f1SDimitry Andric for (InputSectionBase *sec : sections) {
1048fe6060f1SDimitry Andric if (order.try_emplace(sec, prio).second)
1049fe6060f1SDimitry Andric ++prio;
10505ffd83dbSDimitry Andric }
10515ffd83dbSDimitry Andric }
10525ffd83dbSDimitry Andric
10530b57cec5SDimitry Andric // Builds section order for handling --symbol-ordering-file.
buildSectionOrder()10540b57cec5SDimitry Andric static DenseMap<const InputSectionBase *, int> buildSectionOrder() {
10550b57cec5SDimitry Andric DenseMap<const InputSectionBase *, int> sectionOrder;
1056349cc55cSDimitry Andric // Use the rarely used option --call-graph-ordering-file to sort sections.
10570b57cec5SDimitry Andric if (!config->callGraphProfile.empty())
10580b57cec5SDimitry Andric return computeCallGraphProfileOrder();
10590b57cec5SDimitry Andric
10600b57cec5SDimitry Andric if (config->symbolOrderingFile.empty())
10610b57cec5SDimitry Andric return sectionOrder;
10620b57cec5SDimitry Andric
10630b57cec5SDimitry Andric struct SymbolOrderEntry {
10640b57cec5SDimitry Andric int priority;
10650b57cec5SDimitry Andric bool present;
10660b57cec5SDimitry Andric };
10670b57cec5SDimitry Andric
10680b57cec5SDimitry Andric // Build a map from symbols to their priorities. Symbols that didn't
10690b57cec5SDimitry Andric // appear in the symbol ordering file have the lowest priority 0.
10700b57cec5SDimitry Andric // All explicitly mentioned symbols have negative (higher) priorities.
107104eeddc0SDimitry Andric DenseMap<CachedHashStringRef, SymbolOrderEntry> symbolOrder;
10720b57cec5SDimitry Andric int priority = -config->symbolOrderingFile.size();
10730b57cec5SDimitry Andric for (StringRef s : config->symbolOrderingFile)
107404eeddc0SDimitry Andric symbolOrder.insert({CachedHashStringRef(s), {priority++, false}});
10750b57cec5SDimitry Andric
10760b57cec5SDimitry Andric // Build a map from sections to their priorities.
10770b57cec5SDimitry Andric auto addSym = [&](Symbol &sym) {
107804eeddc0SDimitry Andric auto it = symbolOrder.find(CachedHashStringRef(sym.getName()));
10790b57cec5SDimitry Andric if (it == symbolOrder.end())
10800b57cec5SDimitry Andric return;
10810b57cec5SDimitry Andric SymbolOrderEntry &ent = it->second;
10820b57cec5SDimitry Andric ent.present = true;
10830b57cec5SDimitry Andric
10840b57cec5SDimitry Andric maybeWarnUnorderableSymbol(&sym);
10850b57cec5SDimitry Andric
10860b57cec5SDimitry Andric if (auto *d = dyn_cast<Defined>(&sym)) {
10870b57cec5SDimitry Andric if (auto *sec = dyn_cast_or_null<InputSectionBase>(d->section)) {
10880eae32dcSDimitry Andric int &priority = sectionOrder[cast<InputSectionBase>(sec)];
10890b57cec5SDimitry Andric priority = std::min(priority, ent.priority);
10900b57cec5SDimitry Andric }
10910b57cec5SDimitry Andric }
10920b57cec5SDimitry Andric };
10930b57cec5SDimitry Andric
10940b57cec5SDimitry Andric // We want both global and local symbols. We get the global ones from the
10950b57cec5SDimitry Andric // symbol table and iterate the object files for the local ones.
1096bdd1243dSDimitry Andric for (Symbol *sym : symtab.getSymbols())
10970b57cec5SDimitry Andric addSym(*sym);
10980b57cec5SDimitry Andric
1099bdd1243dSDimitry Andric for (ELFFileBase *file : ctx.objectFiles)
110004eeddc0SDimitry Andric for (Symbol *sym : file->getLocalSymbols())
11010b57cec5SDimitry Andric addSym(*sym);
11020b57cec5SDimitry Andric
11030b57cec5SDimitry Andric if (config->warnSymbolOrdering)
11040b57cec5SDimitry Andric for (auto orderEntry : symbolOrder)
11050b57cec5SDimitry Andric if (!orderEntry.second.present)
110604eeddc0SDimitry Andric warn("symbol ordering file: no such symbol: " + orderEntry.first.val());
11070b57cec5SDimitry Andric
11080b57cec5SDimitry Andric return sectionOrder;
11090b57cec5SDimitry Andric }
11100b57cec5SDimitry Andric
11110b57cec5SDimitry Andric // Sorts the sections in ISD according to the provided section order.
11120b57cec5SDimitry Andric static void
sortISDBySectionOrder(InputSectionDescription * isd,const DenseMap<const InputSectionBase *,int> & order,bool executableOutputSection)11130b57cec5SDimitry Andric sortISDBySectionOrder(InputSectionDescription *isd,
1114753f127fSDimitry Andric const DenseMap<const InputSectionBase *, int> &order,
1115753f127fSDimitry Andric bool executableOutputSection) {
111604eeddc0SDimitry Andric SmallVector<InputSection *, 0> unorderedSections;
111704eeddc0SDimitry Andric SmallVector<std::pair<InputSection *, int>, 0> orderedSections;
11180b57cec5SDimitry Andric uint64_t unorderedSize = 0;
1119753f127fSDimitry Andric uint64_t totalSize = 0;
11200b57cec5SDimitry Andric
11210b57cec5SDimitry Andric for (InputSection *isec : isd->sections) {
1122753f127fSDimitry Andric if (executableOutputSection)
1123753f127fSDimitry Andric totalSize += isec->getSize();
11240b57cec5SDimitry Andric auto i = order.find(isec);
11250b57cec5SDimitry Andric if (i == order.end()) {
11260b57cec5SDimitry Andric unorderedSections.push_back(isec);
11270b57cec5SDimitry Andric unorderedSize += isec->getSize();
11280b57cec5SDimitry Andric continue;
11290b57cec5SDimitry Andric }
11300b57cec5SDimitry Andric orderedSections.push_back({isec, i->second});
11310b57cec5SDimitry Andric }
113285868e8aSDimitry Andric llvm::sort(orderedSections, llvm::less_second());
11330b57cec5SDimitry Andric
11340b57cec5SDimitry Andric // Find an insertion point for the ordered section list in the unordered
11350b57cec5SDimitry Andric // section list. On targets with limited-range branches, this is the mid-point
11360b57cec5SDimitry Andric // of the unordered section list. This decreases the likelihood that a range
11370b57cec5SDimitry Andric // extension thunk will be needed to enter or exit the ordered region. If the
11380b57cec5SDimitry Andric // ordered section list is a list of hot functions, we can generally expect
11390b57cec5SDimitry Andric // the ordered functions to be called more often than the unordered functions,
11400b57cec5SDimitry Andric // making it more likely that any particular call will be within range, and
11410b57cec5SDimitry Andric // therefore reducing the number of thunks required.
11420b57cec5SDimitry Andric //
11430b57cec5SDimitry Andric // For example, imagine that you have 8MB of hot code and 32MB of cold code.
11440b57cec5SDimitry Andric // If the layout is:
11450b57cec5SDimitry Andric //
11460b57cec5SDimitry Andric // 8MB hot
11470b57cec5SDimitry Andric // 32MB cold
11480b57cec5SDimitry Andric //
11490b57cec5SDimitry Andric // only the first 8-16MB of the cold code (depending on which hot function it
11500b57cec5SDimitry Andric // is actually calling) can call the hot code without a range extension thunk.
11510b57cec5SDimitry Andric // However, if we use this layout:
11520b57cec5SDimitry Andric //
11530b57cec5SDimitry Andric // 16MB cold
11540b57cec5SDimitry Andric // 8MB hot
11550b57cec5SDimitry Andric // 16MB cold
11560b57cec5SDimitry Andric //
11570b57cec5SDimitry Andric // both the last 8-16MB of the first block of cold code and the first 8-16MB
11580b57cec5SDimitry Andric // of the second block of cold code can call the hot code without a thunk. So
11590b57cec5SDimitry Andric // we effectively double the amount of code that could potentially call into
11600b57cec5SDimitry Andric // the hot code without a thunk.
1161753f127fSDimitry Andric //
1162753f127fSDimitry Andric // The above is not necessary if total size of input sections in this "isd"
1163753f127fSDimitry Andric // is small. Note that we assume all input sections are executable if the
1164753f127fSDimitry Andric // output section is executable (which is not always true but supposed to
1165753f127fSDimitry Andric // cover most cases).
11660b57cec5SDimitry Andric size_t insPt = 0;
1167753f127fSDimitry Andric if (executableOutputSection && !orderedSections.empty() &&
1168753f127fSDimitry Andric target->getThunkSectionSpacing() &&
1169753f127fSDimitry Andric totalSize >= target->getThunkSectionSpacing()) {
11700b57cec5SDimitry Andric uint64_t unorderedPos = 0;
11710b57cec5SDimitry Andric for (; insPt != unorderedSections.size(); ++insPt) {
11720b57cec5SDimitry Andric unorderedPos += unorderedSections[insPt]->getSize();
11730b57cec5SDimitry Andric if (unorderedPos > unorderedSize / 2)
11740b57cec5SDimitry Andric break;
11750b57cec5SDimitry Andric }
11760b57cec5SDimitry Andric }
11770b57cec5SDimitry Andric
11780b57cec5SDimitry Andric isd->sections.clear();
1179bdd1243dSDimitry Andric for (InputSection *isec : ArrayRef(unorderedSections).slice(0, insPt))
11800b57cec5SDimitry Andric isd->sections.push_back(isec);
11810b57cec5SDimitry Andric for (std::pair<InputSection *, int> p : orderedSections)
11820b57cec5SDimitry Andric isd->sections.push_back(p.first);
1183bdd1243dSDimitry Andric for (InputSection *isec : ArrayRef(unorderedSections).slice(insPt))
11840b57cec5SDimitry Andric isd->sections.push_back(isec);
11850b57cec5SDimitry Andric }
11860b57cec5SDimitry Andric
sortSection(OutputSection & osec,const DenseMap<const InputSectionBase *,int> & order)118781ad6265SDimitry Andric static void sortSection(OutputSection &osec,
11880b57cec5SDimitry Andric const DenseMap<const InputSectionBase *, int> &order) {
118981ad6265SDimitry Andric StringRef name = osec.name;
11900b57cec5SDimitry Andric
11915ffd83dbSDimitry Andric // Never sort these.
11925ffd83dbSDimitry Andric if (name == ".init" || name == ".fini")
11935ffd83dbSDimitry Andric return;
11945ffd83dbSDimitry Andric
11955ffd83dbSDimitry Andric // Sort input sections by priority using the list provided by
11965ffd83dbSDimitry Andric // --symbol-ordering-file or --shuffle-sections=. This is a least significant
11975ffd83dbSDimitry Andric // digit radix sort. The sections may be sorted stably again by a more
11985ffd83dbSDimitry Andric // significant key.
11995ffd83dbSDimitry Andric if (!order.empty())
120081ad6265SDimitry Andric for (SectionCommand *b : osec.commands)
12015ffd83dbSDimitry Andric if (auto *isd = dyn_cast<InputSectionDescription>(b))
1202753f127fSDimitry Andric sortISDBySectionOrder(isd, order, osec.flags & SHF_EXECINSTR);
12035ffd83dbSDimitry Andric
1204349cc55cSDimitry Andric if (script->hasSectionsCommand)
1205349cc55cSDimitry Andric return;
1206349cc55cSDimitry Andric
12070b57cec5SDimitry Andric if (name == ".init_array" || name == ".fini_array") {
120881ad6265SDimitry Andric osec.sortInitFini();
1209349cc55cSDimitry Andric } else if (name == ".ctors" || name == ".dtors") {
121081ad6265SDimitry Andric osec.sortCtorsDtors();
1211349cc55cSDimitry Andric } else if (config->emachine == EM_PPC64 && name == ".toc") {
12120b57cec5SDimitry Andric // .toc is allocated just after .got and is accessed using GOT-relative
12130b57cec5SDimitry Andric // relocations. Object files compiled with small code model have an
12140b57cec5SDimitry Andric // addressable range of [.got, .got + 0xFFFC] for GOT-relative relocations.
1215349cc55cSDimitry Andric // To reduce the risk of relocation overflow, .toc contents are sorted so
1216349cc55cSDimitry Andric // that sections having smaller relocation offsets are at beginning of .toc
121781ad6265SDimitry Andric assert(osec.commands.size() == 1);
121881ad6265SDimitry Andric auto *isd = cast<InputSectionDescription>(osec.commands[0]);
12190b57cec5SDimitry Andric llvm::stable_sort(isd->sections,
12200b57cec5SDimitry Andric [](const InputSection *a, const InputSection *b) -> bool {
12210b57cec5SDimitry Andric return a->file->ppc64SmallCodeModelTocRelocs &&
12220b57cec5SDimitry Andric !b->file->ppc64SmallCodeModelTocRelocs;
12230b57cec5SDimitry Andric });
12240b57cec5SDimitry Andric }
12250b57cec5SDimitry Andric }
12260b57cec5SDimitry Andric
12270b57cec5SDimitry Andric // If no layout was provided by linker script, we want to apply default
12280b57cec5SDimitry Andric // sorting for special input sections. This also handles --symbol-ordering-file.
sortInputSections()12290b57cec5SDimitry Andric template <class ELFT> void Writer<ELFT>::sortInputSections() {
12300b57cec5SDimitry Andric // Build the order once since it is expensive.
12310b57cec5SDimitry Andric DenseMap<const InputSectionBase *, int> order = buildSectionOrder();
12325ffd83dbSDimitry Andric maybeShuffle(order);
12334824e7fdSDimitry Andric for (SectionCommand *cmd : script->sectionCommands)
123481ad6265SDimitry Andric if (auto *osd = dyn_cast<OutputDesc>(cmd))
123581ad6265SDimitry Andric sortSection(osd->osec, order);
12360b57cec5SDimitry Andric }
12370b57cec5SDimitry Andric
sortSections()12380b57cec5SDimitry Andric template <class ELFT> void Writer<ELFT>::sortSections() {
1239e8d8bef9SDimitry Andric llvm::TimeTraceScope timeScope("Sort sections");
12400b57cec5SDimitry Andric
12410b57cec5SDimitry Andric // Don't sort if using -r. It is not necessary and we want to preserve the
12420b57cec5SDimitry Andric // relative order for SHF_LINK_ORDER sections.
12431fd87a68SDimitry Andric if (config->relocatable) {
12441fd87a68SDimitry Andric script->adjustOutputSections();
12450b57cec5SDimitry Andric return;
12461fd87a68SDimitry Andric }
12470b57cec5SDimitry Andric
12480b57cec5SDimitry Andric sortInputSections();
12490b57cec5SDimitry Andric
12501fd87a68SDimitry Andric for (SectionCommand *cmd : script->sectionCommands)
125181ad6265SDimitry Andric if (auto *osd = dyn_cast<OutputDesc>(cmd))
125281ad6265SDimitry Andric osd->osec.sortRank = getSectionRank(osd->osec);
12530b57cec5SDimitry Andric if (!script->hasSectionsCommand) {
1254b3edf446SDimitry Andric // OutputDescs are mostly contiguous, but may be interleaved with
1255b3edf446SDimitry Andric // SymbolAssignments in the presence of INSERT commands.
1256b3edf446SDimitry Andric auto mid = std::stable_partition(
1257b3edf446SDimitry Andric script->sectionCommands.begin(), script->sectionCommands.end(),
1258b3edf446SDimitry Andric [](SectionCommand *cmd) { return isa<OutputDesc>(cmd); });
1259b3edf446SDimitry Andric std::stable_sort(script->sectionCommands.begin(), mid, compareSections);
12600b57cec5SDimitry Andric }
12610b57cec5SDimitry Andric
12621fd87a68SDimitry Andric // Process INSERT commands and update output section attributes. From this
12631fd87a68SDimitry Andric // point onwards the order of script->sectionCommands is fixed.
12645ffd83dbSDimitry Andric script->processInsertCommands();
12651fd87a68SDimitry Andric script->adjustOutputSections();
12661fd87a68SDimitry Andric
126706c3fb27SDimitry Andric if (script->hasSectionsCommand)
126806c3fb27SDimitry Andric sortOrphanSections();
12695ffd83dbSDimitry Andric
127006c3fb27SDimitry Andric script->adjustSectionsAfterSorting();
127106c3fb27SDimitry Andric }
127206c3fb27SDimitry Andric
sortOrphanSections()127306c3fb27SDimitry Andric template <class ELFT> void Writer<ELFT>::sortOrphanSections() {
12740b57cec5SDimitry Andric // Orphan sections are sections present in the input files which are
12750b57cec5SDimitry Andric // not explicitly placed into the output file by the linker script.
12760b57cec5SDimitry Andric //
12770b57cec5SDimitry Andric // The sections in the linker script are already in the correct
12780b57cec5SDimitry Andric // order. We have to figuere out where to insert the orphan
12790b57cec5SDimitry Andric // sections.
12800b57cec5SDimitry Andric //
12810b57cec5SDimitry Andric // The order of the sections in the script is arbitrary and may not agree with
12820b57cec5SDimitry Andric // compareSections. This means that we cannot easily define a strict weak
12830b57cec5SDimitry Andric // ordering. To see why, consider a comparison of a section in the script and
12840b57cec5SDimitry Andric // one not in the script. We have a two simple options:
12850b57cec5SDimitry Andric // * Make them equivalent (a is not less than b, and b is not less than a).
12860b57cec5SDimitry Andric // The problem is then that equivalence has to be transitive and we can
12870b57cec5SDimitry Andric // have sections a, b and c with only b in a script and a less than c
12880b57cec5SDimitry Andric // which breaks this property.
12890b57cec5SDimitry Andric // * Use compareSectionsNonScript. Given that the script order doesn't have
12900b57cec5SDimitry Andric // to match, we can end up with sections a, b, c, d where b and c are in the
12910b57cec5SDimitry Andric // script and c is compareSectionsNonScript less than b. In which case d
12920b57cec5SDimitry Andric // can be equivalent to c, a to b and d < a. As a concrete example:
12930b57cec5SDimitry Andric // .a (rx) # not in script
12940b57cec5SDimitry Andric // .b (rx) # in script
12950b57cec5SDimitry Andric // .c (ro) # in script
12960b57cec5SDimitry Andric // .d (ro) # not in script
12970b57cec5SDimitry Andric //
12980b57cec5SDimitry Andric // The way we define an order then is:
12990b57cec5SDimitry Andric // * Sort only the orphan sections. They are in the end right now.
13000b57cec5SDimitry Andric // * Move each orphan section to its preferred position. We try
13010b57cec5SDimitry Andric // to put each section in the last position where it can share
13020b57cec5SDimitry Andric // a PT_LOAD.
13030b57cec5SDimitry Andric //
13040b57cec5SDimitry Andric // There is some ambiguity as to where exactly a new entry should be
13050b57cec5SDimitry Andric // inserted, because Commands contains not only output section
13060b57cec5SDimitry Andric // commands but also other types of commands such as symbol assignment
13070b57cec5SDimitry Andric // expressions. There's no correct answer here due to the lack of the
13080b57cec5SDimitry Andric // formal specification of the linker script. We use heuristics to
13090b57cec5SDimitry Andric // determine whether a new output command should be added before or
13100b57cec5SDimitry Andric // after another commands. For the details, look at shouldSkip
13110b57cec5SDimitry Andric // function.
13120b57cec5SDimitry Andric
13130b57cec5SDimitry Andric auto i = script->sectionCommands.begin();
13140b57cec5SDimitry Andric auto e = script->sectionCommands.end();
13154824e7fdSDimitry Andric auto nonScriptI = std::find_if(i, e, [](SectionCommand *cmd) {
131681ad6265SDimitry Andric if (auto *osd = dyn_cast<OutputDesc>(cmd))
131781ad6265SDimitry Andric return osd->osec.sectionIndex == UINT32_MAX;
13180b57cec5SDimitry Andric return false;
13190b57cec5SDimitry Andric });
13200b57cec5SDimitry Andric
13210b57cec5SDimitry Andric // Sort the orphan sections.
13220b57cec5SDimitry Andric std::stable_sort(nonScriptI, e, compareSections);
13230b57cec5SDimitry Andric
13240b57cec5SDimitry Andric // As a horrible special case, skip the first . assignment if it is before any
13250b57cec5SDimitry Andric // section. We do this because it is common to set a load address by starting
13260b57cec5SDimitry Andric // the script with ". = 0xabcd" and the expectation is that every section is
13270b57cec5SDimitry Andric // after that.
13280b57cec5SDimitry Andric auto firstSectionOrDotAssignment =
13294824e7fdSDimitry Andric std::find_if(i, e, [](SectionCommand *cmd) { return !shouldSkip(cmd); });
13300b57cec5SDimitry Andric if (firstSectionOrDotAssignment != e &&
13310b57cec5SDimitry Andric isa<SymbolAssignment>(**firstSectionOrDotAssignment))
13320b57cec5SDimitry Andric ++firstSectionOrDotAssignment;
13330b57cec5SDimitry Andric i = firstSectionOrDotAssignment;
13340b57cec5SDimitry Andric
13350b57cec5SDimitry Andric while (nonScriptI != e) {
13360b57cec5SDimitry Andric auto pos = findOrphanPos(i, nonScriptI);
133781ad6265SDimitry Andric OutputSection *orphan = &cast<OutputDesc>(*nonScriptI)->osec;
13380b57cec5SDimitry Andric
13390b57cec5SDimitry Andric // As an optimization, find all sections with the same sort rank
13400b57cec5SDimitry Andric // and insert them with one rotate.
13410b57cec5SDimitry Andric unsigned rank = orphan->sortRank;
13424824e7fdSDimitry Andric auto end = std::find_if(nonScriptI + 1, e, [=](SectionCommand *cmd) {
134381ad6265SDimitry Andric return cast<OutputDesc>(cmd)->osec.sortRank != rank;
13440b57cec5SDimitry Andric });
13450b57cec5SDimitry Andric std::rotate(pos, nonScriptI, end);
13460b57cec5SDimitry Andric nonScriptI = end;
13470b57cec5SDimitry Andric }
13480b57cec5SDimitry Andric }
13490b57cec5SDimitry Andric
compareByFilePosition(InputSection * a,InputSection * b)13500b57cec5SDimitry Andric static bool compareByFilePosition(InputSection *a, InputSection *b) {
1351e8d8bef9SDimitry Andric InputSection *la = a->flags & SHF_LINK_ORDER ? a->getLinkOrderDep() : nullptr;
1352e8d8bef9SDimitry Andric InputSection *lb = b->flags & SHF_LINK_ORDER ? b->getLinkOrderDep() : nullptr;
1353e8d8bef9SDimitry Andric // SHF_LINK_ORDER sections with non-zero sh_link are ordered before
1354e8d8bef9SDimitry Andric // non-SHF_LINK_ORDER sections and SHF_LINK_ORDER sections with zero sh_link.
1355e8d8bef9SDimitry Andric if (!la || !lb)
1356e8d8bef9SDimitry Andric return la && !lb;
13570b57cec5SDimitry Andric OutputSection *aOut = la->getParent();
13580b57cec5SDimitry Andric OutputSection *bOut = lb->getParent();
13590b57cec5SDimitry Andric
13600fca6ea1SDimitry Andric if (aOut == bOut)
13610b57cec5SDimitry Andric return la->outSecOff < lb->outSecOff;
13620fca6ea1SDimitry Andric if (aOut->addr == bOut->addr)
13630fca6ea1SDimitry Andric return aOut->sectionIndex < bOut->sectionIndex;
13640fca6ea1SDimitry Andric return aOut->addr < bOut->addr;
13650b57cec5SDimitry Andric }
13660b57cec5SDimitry Andric
resolveShfLinkOrder()13670b57cec5SDimitry Andric template <class ELFT> void Writer<ELFT>::resolveShfLinkOrder() {
1368e8d8bef9SDimitry Andric llvm::TimeTraceScope timeScope("Resolve SHF_LINK_ORDER");
13690b57cec5SDimitry Andric for (OutputSection *sec : outputSections) {
13700b57cec5SDimitry Andric if (!(sec->flags & SHF_LINK_ORDER))
13710b57cec5SDimitry Andric continue;
13720b57cec5SDimitry Andric
137385868e8aSDimitry Andric // The ARM.exidx section use SHF_LINK_ORDER, but we have consolidated
137485868e8aSDimitry Andric // this processing inside the ARMExidxsyntheticsection::finalizeContents().
137585868e8aSDimitry Andric if (!config->relocatable && config->emachine == EM_ARM &&
137685868e8aSDimitry Andric sec->type == SHT_ARM_EXIDX)
137785868e8aSDimitry Andric continue;
137885868e8aSDimitry Andric
1379e8d8bef9SDimitry Andric // Link order may be distributed across several InputSectionDescriptions.
1380e8d8bef9SDimitry Andric // Sorting is performed separately.
13811fd87a68SDimitry Andric SmallVector<InputSection **, 0> scriptSections;
13821fd87a68SDimitry Andric SmallVector<InputSection *, 0> sections;
13834824e7fdSDimitry Andric for (SectionCommand *cmd : sec->commands) {
13844824e7fdSDimitry Andric auto *isd = dyn_cast<InputSectionDescription>(cmd);
1385e8d8bef9SDimitry Andric if (!isd)
1386e8d8bef9SDimitry Andric continue;
1387e8d8bef9SDimitry Andric bool hasLinkOrder = false;
1388e8d8bef9SDimitry Andric scriptSections.clear();
1389e8d8bef9SDimitry Andric sections.clear();
13900b57cec5SDimitry Andric for (InputSection *&isec : isd->sections) {
1391e8d8bef9SDimitry Andric if (isec->flags & SHF_LINK_ORDER) {
139285868e8aSDimitry Andric InputSection *link = isec->getLinkOrderDep();
1393e8d8bef9SDimitry Andric if (link && !link->getParent())
139485868e8aSDimitry Andric error(toString(isec) + ": sh_link points to discarded section " +
139585868e8aSDimitry Andric toString(link));
1396e8d8bef9SDimitry Andric hasLinkOrder = true;
13970b57cec5SDimitry Andric }
1398e8d8bef9SDimitry Andric scriptSections.push_back(&isec);
1399e8d8bef9SDimitry Andric sections.push_back(isec);
14000b57cec5SDimitry Andric }
1401e8d8bef9SDimitry Andric if (hasLinkOrder && errorCount() == 0) {
14020b57cec5SDimitry Andric llvm::stable_sort(sections, compareByFilePosition);
1403e8d8bef9SDimitry Andric for (int i = 0, n = sections.size(); i != n; ++i)
14040b57cec5SDimitry Andric *scriptSections[i] = sections[i];
14050b57cec5SDimitry Andric }
14060b57cec5SDimitry Andric }
1407e8d8bef9SDimitry Andric }
1408e8d8bef9SDimitry Andric }
14090b57cec5SDimitry Andric
finalizeSynthetic(SyntheticSection * sec)14105ffd83dbSDimitry Andric static void finalizeSynthetic(SyntheticSection *sec) {
1411e8d8bef9SDimitry Andric if (sec && sec->isNeeded() && sec->getParent()) {
1412e8d8bef9SDimitry Andric llvm::TimeTraceScope timeScope("Finalize synthetic sections", sec->name);
14135ffd83dbSDimitry Andric sec->finalizeContents();
14145ffd83dbSDimitry Andric }
1415e8d8bef9SDimitry Andric }
14165ffd83dbSDimitry Andric
14170b57cec5SDimitry Andric // We need to generate and finalize the content that depends on the address of
14180b57cec5SDimitry Andric // InputSections. As the generation of the content may also alter InputSection
14190b57cec5SDimitry Andric // addresses we must converge to a fixed point. We do that here. See the comment
14200b57cec5SDimitry Andric // in Writer<ELFT>::finalizeSections().
finalizeAddressDependentContent()14210b57cec5SDimitry Andric template <class ELFT> void Writer<ELFT>::finalizeAddressDependentContent() {
1422e8d8bef9SDimitry Andric llvm::TimeTraceScope timeScope("Finalize address dependent content");
14230b57cec5SDimitry Andric ThunkCreator tc;
14240b57cec5SDimitry Andric AArch64Err843419Patcher a64p;
142585868e8aSDimitry Andric ARMErr657417Patcher a32p;
14260b57cec5SDimitry Andric script->assignAddresses();
14270fca6ea1SDimitry Andric
14285ffd83dbSDimitry Andric // .ARM.exidx and SHF_LINK_ORDER do not require precise addresses, but they
14295ffd83dbSDimitry Andric // do require the relative addresses of OutputSections because linker scripts
14305ffd83dbSDimitry Andric // can assign Virtual Addresses to OutputSections that are not monotonically
14310fca6ea1SDimitry Andric // increasing. Anything here must be repeatable, since spilling may change
14320fca6ea1SDimitry Andric // section order.
14330fca6ea1SDimitry Andric const auto finalizeOrderDependentContent = [this] {
14345ffd83dbSDimitry Andric for (Partition &part : partitions)
143504eeddc0SDimitry Andric finalizeSynthetic(part.armExidx.get());
14365ffd83dbSDimitry Andric resolveShfLinkOrder();
14370fca6ea1SDimitry Andric };
14380fca6ea1SDimitry Andric finalizeOrderDependentContent();
14395ffd83dbSDimitry Andric
14405ffd83dbSDimitry Andric // Converts call x@GDPLT to call __tls_get_addr
14415ffd83dbSDimitry Andric if (config->emachine == EM_HEXAGON)
14425ffd83dbSDimitry Andric hexagonTLSSymbolUpdate(outputSections);
14430b57cec5SDimitry Andric
1444753f127fSDimitry Andric uint32_t pass = 0, assignPasses = 0;
144585868e8aSDimitry Andric for (;;) {
1446753f127fSDimitry Andric bool changed = target->needsThunks ? tc.createThunks(pass, outputSections)
1447753f127fSDimitry Andric : target->relaxOnce(pass);
14480fca6ea1SDimitry Andric bool spilled = script->spillSections();
14490fca6ea1SDimitry Andric changed |= spilled;
1450753f127fSDimitry Andric ++pass;
145185868e8aSDimitry Andric
145285868e8aSDimitry Andric // With Thunk Size much smaller than branch range we expect to
145306c3fb27SDimitry Andric // converge quickly; if we get to 30 something has gone wrong.
145406c3fb27SDimitry Andric if (changed && pass >= 30) {
1455753f127fSDimitry Andric error(target->needsThunks ? "thunk creation not converged"
1456753f127fSDimitry Andric : "relaxation not converged");
145785868e8aSDimitry Andric break;
145885868e8aSDimitry Andric }
14590b57cec5SDimitry Andric
14600b57cec5SDimitry Andric if (config->fixCortexA53Errata843419) {
14610b57cec5SDimitry Andric if (changed)
14620b57cec5SDimitry Andric script->assignAddresses();
14630b57cec5SDimitry Andric changed |= a64p.createFixes();
14640b57cec5SDimitry Andric }
146585868e8aSDimitry Andric if (config->fixCortexA8) {
146685868e8aSDimitry Andric if (changed)
146785868e8aSDimitry Andric script->assignAddresses();
146885868e8aSDimitry Andric changed |= a32p.createFixes();
146985868e8aSDimitry Andric }
14700b57cec5SDimitry Andric
14715f757f3fSDimitry Andric finalizeSynthetic(in.got.get());
14720b57cec5SDimitry Andric if (in.mipsGot)
14730b57cec5SDimitry Andric in.mipsGot->updateAllocSize();
14740b57cec5SDimitry Andric
14750b57cec5SDimitry Andric for (Partition &part : partitions) {
14760fca6ea1SDimitry Andric // The R_AARCH64_AUTH_RELATIVE has a smaller addend field as bits [63:32]
14770fca6ea1SDimitry Andric // encode the signing schema. We've put relocations in .relr.auth.dyn
14780fca6ea1SDimitry Andric // during RelocationScanner::processAux, but the target VA for some of
14790fca6ea1SDimitry Andric // them might be wider than 32 bits. We can only know the final VA at this
14800fca6ea1SDimitry Andric // point, so move relocations with large values from .relr.auth.dyn to
14810fca6ea1SDimitry Andric // .rela.dyn. See also AArch64::relocate.
14820fca6ea1SDimitry Andric if (part.relrAuthDyn) {
14830fca6ea1SDimitry Andric auto it = llvm::remove_if(
14840fca6ea1SDimitry Andric part.relrAuthDyn->relocs, [&part](const RelativeReloc &elem) {
14850fca6ea1SDimitry Andric const Relocation &reloc = elem.inputSec->relocs()[elem.relocIdx];
14860fca6ea1SDimitry Andric if (isInt<32>(reloc.sym->getVA(reloc.addend)))
14870fca6ea1SDimitry Andric return false;
14880fca6ea1SDimitry Andric part.relaDyn->addReloc({R_AARCH64_AUTH_RELATIVE, elem.inputSec,
14890fca6ea1SDimitry Andric reloc.offset,
14900fca6ea1SDimitry Andric DynamicReloc::AddendOnlyWithTargetVA,
14910fca6ea1SDimitry Andric *reloc.sym, reloc.addend, R_ABS});
14920fca6ea1SDimitry Andric return true;
14930fca6ea1SDimitry Andric });
14940fca6ea1SDimitry Andric changed |= (it != part.relrAuthDyn->relocs.end());
14950fca6ea1SDimitry Andric part.relrAuthDyn->relocs.erase(it, part.relrAuthDyn->relocs.end());
14960fca6ea1SDimitry Andric }
14970fca6ea1SDimitry Andric if (part.relaDyn)
14980b57cec5SDimitry Andric changed |= part.relaDyn->updateAllocSize();
14990b57cec5SDimitry Andric if (part.relrDyn)
15000b57cec5SDimitry Andric changed |= part.relrDyn->updateAllocSize();
15010fca6ea1SDimitry Andric if (part.relrAuthDyn)
15020fca6ea1SDimitry Andric changed |= part.relrAuthDyn->updateAllocSize();
15031db9f3b2SDimitry Andric if (part.memtagGlobalDescriptors)
15041db9f3b2SDimitry Andric changed |= part.memtagGlobalDescriptors->updateAllocSize();
15050b57cec5SDimitry Andric }
15060b57cec5SDimitry Andric
15070fca6ea1SDimitry Andric std::pair<const OutputSection *, const Defined *> changes =
15080fca6ea1SDimitry Andric script->assignAddresses();
150985868e8aSDimitry Andric if (!changed) {
151085868e8aSDimitry Andric // Some symbols may be dependent on section addresses. When we break the
151185868e8aSDimitry Andric // loop, the symbol values are finalized because a previous
151285868e8aSDimitry Andric // assignAddresses() finalized section addresses.
15130fca6ea1SDimitry Andric if (!changes.first && !changes.second)
151485868e8aSDimitry Andric break;
151585868e8aSDimitry Andric if (++assignPasses == 5) {
15160fca6ea1SDimitry Andric if (changes.first)
15170fca6ea1SDimitry Andric errorOrWarn("address (0x" + Twine::utohexstr(changes.first->addr) +
15180fca6ea1SDimitry Andric ") of section '" + changes.first->name +
15190fca6ea1SDimitry Andric "' does not converge");
15200fca6ea1SDimitry Andric if (changes.second)
15210fca6ea1SDimitry Andric errorOrWarn("assignment to symbol " + toString(*changes.second) +
152285868e8aSDimitry Andric " does not converge");
152385868e8aSDimitry Andric break;
152485868e8aSDimitry Andric }
15250fca6ea1SDimitry Andric } else if (spilled) {
15260fca6ea1SDimitry Andric // Spilling can change relative section order.
15270fca6ea1SDimitry Andric finalizeOrderDependentContent();
152885868e8aSDimitry Andric }
15290b57cec5SDimitry Andric }
153074626c16SDimitry Andric if (!config->relocatable)
153174626c16SDimitry Andric target->finalizeRelax(pass);
15325ffd83dbSDimitry Andric
153304eeddc0SDimitry Andric if (config->relocatable)
153404eeddc0SDimitry Andric for (OutputSection *sec : outputSections)
153504eeddc0SDimitry Andric sec->addr = 0;
153604eeddc0SDimitry Andric
15375ffd83dbSDimitry Andric // If addrExpr is set, the address may not be a multiple of the alignment.
15385ffd83dbSDimitry Andric // Warn because this is error-prone.
15394824e7fdSDimitry Andric for (SectionCommand *cmd : script->sectionCommands)
154081ad6265SDimitry Andric if (auto *osd = dyn_cast<OutputDesc>(cmd)) {
154181ad6265SDimitry Andric OutputSection *osec = &osd->osec;
1542bdd1243dSDimitry Andric if (osec->addr % osec->addralign != 0)
154381ad6265SDimitry Andric warn("address (0x" + Twine::utohexstr(osec->addr) + ") of section " +
154481ad6265SDimitry Andric osec->name + " is not a multiple of alignment (" +
1545bdd1243dSDimitry Andric Twine(osec->addralign) + ")");
154681ad6265SDimitry Andric }
15470fca6ea1SDimitry Andric
15480fca6ea1SDimitry Andric // Sizes are no longer allowed to grow, so all allowable spills have been
15490fca6ea1SDimitry Andric // taken. Remove any leftover potential spills.
15500fca6ea1SDimitry Andric script->erasePotentialSpillSections();
15510b57cec5SDimitry Andric }
15520b57cec5SDimitry Andric
1553fe6060f1SDimitry Andric // If Input Sections have been shrunk (basic block sections) then
15545ffd83dbSDimitry Andric // update symbol values and sizes associated with these sections. With basic
15555ffd83dbSDimitry Andric // block sections, input sections can shrink when the jump instructions at
15565ffd83dbSDimitry Andric // the end of the section are relaxed.
fixSymbolsAfterShrinking()15575ffd83dbSDimitry Andric static void fixSymbolsAfterShrinking() {
1558bdd1243dSDimitry Andric for (InputFile *File : ctx.objectFiles) {
15595ffd83dbSDimitry Andric parallelForEach(File->getSymbols(), [&](Symbol *Sym) {
15605ffd83dbSDimitry Andric auto *def = dyn_cast<Defined>(Sym);
15615ffd83dbSDimitry Andric if (!def)
15625ffd83dbSDimitry Andric return;
15635ffd83dbSDimitry Andric
15645ffd83dbSDimitry Andric const SectionBase *sec = def->section;
15655ffd83dbSDimitry Andric if (!sec)
15665ffd83dbSDimitry Andric return;
15675ffd83dbSDimitry Andric
15680eae32dcSDimitry Andric const InputSectionBase *inputSec = dyn_cast<InputSectionBase>(sec);
15695ffd83dbSDimitry Andric if (!inputSec || !inputSec->bytesDropped)
15705ffd83dbSDimitry Andric return;
15715ffd83dbSDimitry Andric
1572bdd1243dSDimitry Andric const size_t OldSize = inputSec->content().size();
15735ffd83dbSDimitry Andric const size_t NewSize = OldSize - inputSec->bytesDropped;
15745ffd83dbSDimitry Andric
15755ffd83dbSDimitry Andric if (def->value > NewSize && def->value <= OldSize) {
15765ffd83dbSDimitry Andric LLVM_DEBUG(llvm::dbgs()
15775ffd83dbSDimitry Andric << "Moving symbol " << Sym->getName() << " from "
15785ffd83dbSDimitry Andric << def->value << " to "
15795ffd83dbSDimitry Andric << def->value - inputSec->bytesDropped << " bytes\n");
15805ffd83dbSDimitry Andric def->value -= inputSec->bytesDropped;
15815ffd83dbSDimitry Andric return;
15825ffd83dbSDimitry Andric }
15835ffd83dbSDimitry Andric
15845ffd83dbSDimitry Andric if (def->value + def->size > NewSize && def->value <= OldSize &&
15855ffd83dbSDimitry Andric def->value + def->size <= OldSize) {
15865ffd83dbSDimitry Andric LLVM_DEBUG(llvm::dbgs()
15875ffd83dbSDimitry Andric << "Shrinking symbol " << Sym->getName() << " from "
15885ffd83dbSDimitry Andric << def->size << " to " << def->size - inputSec->bytesDropped
15895ffd83dbSDimitry Andric << " bytes\n");
15905ffd83dbSDimitry Andric def->size -= inputSec->bytesDropped;
15915ffd83dbSDimitry Andric }
15925ffd83dbSDimitry Andric });
15935ffd83dbSDimitry Andric }
15945ffd83dbSDimitry Andric }
15955ffd83dbSDimitry Andric
15965ffd83dbSDimitry Andric // If basic block sections exist, there are opportunities to delete fall thru
15975ffd83dbSDimitry Andric // jumps and shrink jump instructions after basic block reordering. This
15985ffd83dbSDimitry Andric // relaxation pass does that. It is only enabled when --optimize-bb-jumps
15995ffd83dbSDimitry Andric // option is used.
optimizeBasicBlockJumps()16005ffd83dbSDimitry Andric template <class ELFT> void Writer<ELFT>::optimizeBasicBlockJumps() {
16015ffd83dbSDimitry Andric assert(config->optimizeBBJumps);
1602753f127fSDimitry Andric SmallVector<InputSection *, 0> storage;
16035ffd83dbSDimitry Andric
16045ffd83dbSDimitry Andric script->assignAddresses();
16055ffd83dbSDimitry Andric // For every output section that has executable input sections, this
16065ffd83dbSDimitry Andric // does the following:
16075ffd83dbSDimitry Andric // 1. Deletes all direct jump instructions in input sections that
16085ffd83dbSDimitry Andric // jump to the following section as it is not required.
16095ffd83dbSDimitry Andric // 2. If there are two consecutive jump instructions, it checks
16105ffd83dbSDimitry Andric // if they can be flipped and one can be deleted.
161104eeddc0SDimitry Andric for (OutputSection *osec : outputSections) {
161204eeddc0SDimitry Andric if (!(osec->flags & SHF_EXECINSTR))
16135ffd83dbSDimitry Andric continue;
1614753f127fSDimitry Andric ArrayRef<InputSection *> sections = getInputSections(*osec, storage);
161504eeddc0SDimitry Andric size_t numDeleted = 0;
16165ffd83dbSDimitry Andric // Delete all fall through jump instructions. Also, check if two
16175ffd83dbSDimitry Andric // consecutive jump instructions can be flipped so that a fall
16185ffd83dbSDimitry Andric // through jmp instruction can be deleted.
161904eeddc0SDimitry Andric for (size_t i = 0, e = sections.size(); i != e; ++i) {
16205ffd83dbSDimitry Andric InputSection *next = i + 1 < sections.size() ? sections[i + 1] : nullptr;
162104eeddc0SDimitry Andric InputSection &sec = *sections[i];
162204eeddc0SDimitry Andric numDeleted += target->deleteFallThruJmpInsn(sec, sec.file, next);
162304eeddc0SDimitry Andric }
16245ffd83dbSDimitry Andric if (numDeleted > 0) {
16255ffd83dbSDimitry Andric script->assignAddresses();
16265ffd83dbSDimitry Andric LLVM_DEBUG(llvm::dbgs()
16275ffd83dbSDimitry Andric << "Removing " << numDeleted << " fall through jumps\n");
16285ffd83dbSDimitry Andric }
16295ffd83dbSDimitry Andric }
16305ffd83dbSDimitry Andric
16315ffd83dbSDimitry Andric fixSymbolsAfterShrinking();
16325ffd83dbSDimitry Andric
163304eeddc0SDimitry Andric for (OutputSection *osec : outputSections)
1634753f127fSDimitry Andric for (InputSection *is : getInputSections(*osec, storage))
16355ffd83dbSDimitry Andric is->trim();
16365ffd83dbSDimitry Andric }
16370b57cec5SDimitry Andric
16380b57cec5SDimitry Andric // In order to allow users to manipulate linker-synthesized sections,
16390b57cec5SDimitry Andric // we had to add synthetic sections to the input section list early,
16400b57cec5SDimitry Andric // even before we make decisions whether they are needed. This allows
16410b57cec5SDimitry Andric // users to write scripts like this: ".mygot : { .got }".
16420b57cec5SDimitry Andric //
16430b57cec5SDimitry Andric // Doing it has an unintended side effects. If it turns out that we
16440b57cec5SDimitry Andric // don't need a .got (for example) at all because there's no
16450b57cec5SDimitry Andric // relocation that needs a .got, we don't want to emit .got.
16460b57cec5SDimitry Andric //
16470b57cec5SDimitry Andric // To deal with the above problem, this function is called after
16480b57cec5SDimitry Andric // scanRelocations is called to remove synthetic sections that turn
16490b57cec5SDimitry Andric // out to be empty.
removeUnusedSyntheticSections()16500b57cec5SDimitry Andric static void removeUnusedSyntheticSections() {
16510b57cec5SDimitry Andric // All input synthetic sections that can be empty are placed after
1652fe6060f1SDimitry Andric // all regular ones. Reverse iterate to find the first synthetic section
1653fe6060f1SDimitry Andric // after a non-synthetic one which will be our starting point.
1654bdd1243dSDimitry Andric auto start =
1655bdd1243dSDimitry Andric llvm::find_if(llvm::reverse(ctx.inputSections), [](InputSectionBase *s) {
1656fe6060f1SDimitry Andric return !isa<SyntheticSection>(s);
1657bdd1243dSDimitry Andric }).base();
1658fe6060f1SDimitry Andric
1659bdd1243dSDimitry Andric // Remove unused synthetic sections from ctx.inputSections;
16604824e7fdSDimitry Andric DenseSet<InputSectionBase *> unused;
16614824e7fdSDimitry Andric auto end =
1662bdd1243dSDimitry Andric std::remove_if(start, ctx.inputSections.end(), [&](InputSectionBase *s) {
16634824e7fdSDimitry Andric auto *sec = cast<SyntheticSection>(s);
16644824e7fdSDimitry Andric if (sec->getParent() && sec->isNeeded())
1665fe6060f1SDimitry Andric return false;
16660fca6ea1SDimitry Andric // .relr.auth.dyn relocations may be moved to .rela.dyn in
16670fca6ea1SDimitry Andric // finalizeAddressDependentContent, making .rela.dyn no longer empty.
16680fca6ea1SDimitry Andric // Conservatively keep .rela.dyn. .relr.auth.dyn can be made empty, but
16690fca6ea1SDimitry Andric // we would fail to remove it here.
16700fca6ea1SDimitry Andric if (config->emachine == EM_AARCH64 && config->relrPackDynRelocs)
16710fca6ea1SDimitry Andric if (auto *relSec = dyn_cast<RelocationBaseSection>(sec))
16720fca6ea1SDimitry Andric if (relSec == mainPart->relaDyn.get())
16730fca6ea1SDimitry Andric return false;
16744824e7fdSDimitry Andric unused.insert(sec);
16754824e7fdSDimitry Andric return true;
1676fe6060f1SDimitry Andric });
1677bdd1243dSDimitry Andric ctx.inputSections.erase(end, ctx.inputSections.end());
16784824e7fdSDimitry Andric
16794824e7fdSDimitry Andric // Remove unused synthetic sections from the corresponding input section
16804824e7fdSDimitry Andric // description and orphanSections.
16814824e7fdSDimitry Andric for (auto *sec : unused)
16824824e7fdSDimitry Andric if (OutputSection *osec = cast<SyntheticSection>(sec)->getParent())
16834824e7fdSDimitry Andric for (SectionCommand *cmd : osec->commands)
16844824e7fdSDimitry Andric if (auto *isd = dyn_cast<InputSectionDescription>(cmd))
16854824e7fdSDimitry Andric llvm::erase_if(isd->sections, [&](InputSection *isec) {
16864824e7fdSDimitry Andric return unused.count(isec);
16874824e7fdSDimitry Andric });
16884824e7fdSDimitry Andric llvm::erase_if(script->orphanSections, [&](const InputSectionBase *sec) {
16894824e7fdSDimitry Andric return unused.count(sec);
16904824e7fdSDimitry Andric });
16910b57cec5SDimitry Andric }
16920b57cec5SDimitry Andric
16930b57cec5SDimitry Andric // Create output section objects and add them to OutputSections.
finalizeSections()16940b57cec5SDimitry Andric template <class ELFT> void Writer<ELFT>::finalizeSections() {
1695bdd1243dSDimitry Andric if (!config->relocatable) {
16960b57cec5SDimitry Andric Out::preinitArray = findSection(".preinit_array");
16970b57cec5SDimitry Andric Out::initArray = findSection(".init_array");
16980b57cec5SDimitry Andric Out::finiArray = findSection(".fini_array");
16990b57cec5SDimitry Andric
17000b57cec5SDimitry Andric // The linker needs to define SECNAME_start, SECNAME_end and SECNAME_stop
17010b57cec5SDimitry Andric // symbols for sections, so that the runtime can get the start and end
17020b57cec5SDimitry Andric // addresses of each section by section name. Add such symbols.
17030b57cec5SDimitry Andric addStartEndSymbols();
17044824e7fdSDimitry Andric for (SectionCommand *cmd : script->sectionCommands)
170581ad6265SDimitry Andric if (auto *osd = dyn_cast<OutputDesc>(cmd))
170681ad6265SDimitry Andric addStartStopSymbols(osd->osec);
17070b57cec5SDimitry Andric
17080b57cec5SDimitry Andric // Add _DYNAMIC symbol. Unlike GNU gold, our _DYNAMIC symbol has no type.
17090b57cec5SDimitry Andric // It should be okay as no one seems to care about the type.
17100b57cec5SDimitry Andric // Even the author of gold doesn't remember why gold behaves that way.
17110b57cec5SDimitry Andric // https://sourceware.org/ml/binutils/2002-03/msg00360.html
1712bdd1243dSDimitry Andric if (mainPart->dynamic->parent) {
1713bdd1243dSDimitry Andric Symbol *s = symtab.addSymbol(Defined{
17147a6dacacSDimitry Andric ctx.internalFile, "_DYNAMIC", STB_WEAK, STV_HIDDEN, STT_NOTYPE,
1715bdd1243dSDimitry Andric /*value=*/0, /*size=*/0, mainPart->dynamic.get()});
1716bdd1243dSDimitry Andric s->isUsedInRegularObj = true;
1717bdd1243dSDimitry Andric }
17180b57cec5SDimitry Andric
17190b57cec5SDimitry Andric // Define __rel[a]_iplt_{start,end} symbols if needed.
17200b57cec5SDimitry Andric addRelIpltSymbols();
17210b57cec5SDimitry Andric
172285868e8aSDimitry Andric // RISC-V's gp can address +/- 2 KiB, set it to .sdata + 0x800. This symbol
172385868e8aSDimitry Andric // should only be defined in an executable. If .sdata does not exist, its
172485868e8aSDimitry Andric // value/section does not matter but it has to be relative, so set its
172585868e8aSDimitry Andric // st_shndx arbitrarily to 1 (Out::elfHeader).
172606c3fb27SDimitry Andric if (config->emachine == EM_RISCV) {
172706c3fb27SDimitry Andric ElfSym::riscvGlobalPointer = nullptr;
172806c3fb27SDimitry Andric if (!config->shared) {
172985868e8aSDimitry Andric OutputSection *sec = findSection(".sdata");
173006c3fb27SDimitry Andric addOptionalRegular(
173106c3fb27SDimitry Andric "__global_pointer$", sec ? sec : Out::elfHeader, 0x800, STV_DEFAULT);
173206c3fb27SDimitry Andric // Set riscvGlobalPointer to be used by the optional global pointer
173306c3fb27SDimitry Andric // relaxation.
173406c3fb27SDimitry Andric if (config->relaxGP) {
173506c3fb27SDimitry Andric Symbol *s = symtab.find("__global_pointer$");
173606c3fb27SDimitry Andric if (s && s->isDefined())
173706c3fb27SDimitry Andric ElfSym::riscvGlobalPointer = cast<Defined>(s);
173806c3fb27SDimitry Andric }
173906c3fb27SDimitry Andric }
174085868e8aSDimitry Andric }
17410b57cec5SDimitry Andric
1742349cc55cSDimitry Andric if (config->emachine == EM_386 || config->emachine == EM_X86_64) {
17430b57cec5SDimitry Andric // On targets that support TLSDESC, _TLS_MODULE_BASE_ is defined in such a
17440b57cec5SDimitry Andric // way that:
17450b57cec5SDimitry Andric //
17460b57cec5SDimitry Andric // 1) Without relaxation: it produces a dynamic TLSDESC relocation that
17470b57cec5SDimitry Andric // computes 0.
1748bdd1243dSDimitry Andric // 2) With LD->LE relaxation: _TLS_MODULE_BASE_@tpoff = 0 (lowest address
1749bdd1243dSDimitry Andric // in the TLS block).
17500b57cec5SDimitry Andric //
1751bdd1243dSDimitry Andric // 2) is special cased in @tpoff computation. To satisfy 1), we define it
1752bdd1243dSDimitry Andric // as an absolute symbol of zero. This is different from GNU linkers which
17530b57cec5SDimitry Andric // define _TLS_MODULE_BASE_ relative to the first TLS section.
1754bdd1243dSDimitry Andric Symbol *s = symtab.find("_TLS_MODULE_BASE_");
17550b57cec5SDimitry Andric if (s && s->isUndefined()) {
17567a6dacacSDimitry Andric s->resolve(Defined{ctx.internalFile, StringRef(), STB_GLOBAL,
1757bdd1243dSDimitry Andric STV_HIDDEN, STT_TLS, /*value=*/0, 0,
17580b57cec5SDimitry Andric /*section=*/nullptr});
17590b57cec5SDimitry Andric ElfSym::tlsModuleBase = cast<Defined>(s);
17600b57cec5SDimitry Andric }
17610b57cec5SDimitry Andric }
17620b57cec5SDimitry Andric
17630b57cec5SDimitry Andric // This responsible for splitting up .eh_frame section into
17640b57cec5SDimitry Andric // pieces. The relocation scan uses those pieces, so this has to be
17650b57cec5SDimitry Andric // earlier.
1766bdd1243dSDimitry Andric {
1767bdd1243dSDimitry Andric llvm::TimeTraceScope timeScope("Finalize .eh_frame");
17680b57cec5SDimitry Andric for (Partition &part : partitions)
176904eeddc0SDimitry Andric finalizeSynthetic(part.ehFrame.get());
1770e8d8bef9SDimitry Andric }
17715f757f3fSDimitry Andric }
17720b57cec5SDimitry Andric
17735f757f3fSDimitry Andric demoteSymbolsAndComputeIsPreemptible();
17745f757f3fSDimitry Andric
17755f757f3fSDimitry Andric if (config->copyRelocs && config->discard != DiscardPolicy::None)
17765f757f3fSDimitry Andric markUsedLocalSymbols<ELFT>();
17775f757f3fSDimitry Andric demoteAndCopyLocalSymbols();
17785f757f3fSDimitry Andric
17795f757f3fSDimitry Andric if (config->copyRelocs)
17805f757f3fSDimitry Andric addSectionSymbols();
178185868e8aSDimitry Andric
178285868e8aSDimitry Andric // Change values of linker-script-defined symbols from placeholders (assigned
178385868e8aSDimitry Andric // by declareSymbols) to actual definitions.
178485868e8aSDimitry Andric script->processSymbolAssignments();
17850b57cec5SDimitry Andric
1786bdd1243dSDimitry Andric if (!config->relocatable) {
1787e8d8bef9SDimitry Andric llvm::TimeTraceScope timeScope("Scan relocations");
1788e8d8bef9SDimitry Andric // Scan relocations. This must be done after every symbol is declared so
1789e8d8bef9SDimitry Andric // that we can correctly decide if a dynamic relocation is needed. This is
1790e8d8bef9SDimitry Andric // called after processSymbolAssignments() because it needs to know whether
1791e8d8bef9SDimitry Andric // a linker-script-defined symbol is absolute.
17925ffd83dbSDimitry Andric ppc64noTocRelax.clear();
1793bdd1243dSDimitry Andric scanRelocations<ELFT>();
179481ad6265SDimitry Andric reportUndefinedSymbols();
17950eae32dcSDimitry Andric postScanRelocations();
17960b57cec5SDimitry Andric
17970b57cec5SDimitry Andric if (in.plt && in.plt->isNeeded())
17980b57cec5SDimitry Andric in.plt->addSymbols();
17990b57cec5SDimitry Andric if (in.iplt && in.iplt->isNeeded())
18000b57cec5SDimitry Andric in.iplt->addSymbols();
18010b57cec5SDimitry Andric
1802e8d8bef9SDimitry Andric if (config->unresolvedSymbolsInShlib != UnresolvedPolicy::Ignore) {
1803fe6060f1SDimitry Andric auto diagnose =
1804fe6060f1SDimitry Andric config->unresolvedSymbolsInShlib == UnresolvedPolicy::ReportError
1805fe6060f1SDimitry Andric ? errorOrWarn
1806fe6060f1SDimitry Andric : warn;
18070b57cec5SDimitry Andric // Error on undefined symbols in a shared object, if all of its DT_NEEDED
1808480093f4SDimitry Andric // entries are seen. These cases would otherwise lead to runtime errors
18090b57cec5SDimitry Andric // reported by the dynamic linker.
18100b57cec5SDimitry Andric //
1811bdd1243dSDimitry Andric // ld.bfd traces all DT_NEEDED to emulate the logic of the dynamic linker
1812bdd1243dSDimitry Andric // to catch more cases. That is too much for us. Our approach resembles
1813bdd1243dSDimitry Andric // the one used in ld.gold, achieves a good balance to be useful but not
1814bdd1243dSDimitry Andric // too smart.
18157a6dacacSDimitry Andric //
18167a6dacacSDimitry Andric // If a DSO reference is resolved by a SharedSymbol, but the SharedSymbol
18177a6dacacSDimitry Andric // is overridden by a hidden visibility Defined (which is later discarded
18187a6dacacSDimitry Andric // due to GC), don't report the diagnostic. However, this may indicate an
18197a6dacacSDimitry Andric // unintended SharedSymbol.
1820bdd1243dSDimitry Andric for (SharedFile *file : ctx.sharedFiles) {
1821fe6060f1SDimitry Andric bool allNeededIsKnown =
18220b57cec5SDimitry Andric llvm::all_of(file->dtNeeded, [&](StringRef needed) {
1823bdd1243dSDimitry Andric return symtab.soNames.count(CachedHashStringRef(needed));
18240b57cec5SDimitry Andric });
1825fe6060f1SDimitry Andric if (!allNeededIsKnown)
1826fe6060f1SDimitry Andric continue;
18275f757f3fSDimitry Andric for (Symbol *sym : file->requiredSymbols) {
18287a6dacacSDimitry Andric if (sym->dsoDefined)
18297a6dacacSDimitry Andric continue;
18305f757f3fSDimitry Andric if (sym->isUndefined() && !sym->isWeak()) {
18310fca6ea1SDimitry Andric diagnose("undefined reference: " + toString(*sym) +
18320fca6ea1SDimitry Andric "\n>>> referenced by " + toString(file) +
18330fca6ea1SDimitry Andric " (disallowed by --no-allow-shlib-undefined)");
18345f757f3fSDimitry Andric } else if (sym->isDefined() && sym->computeBinding() == STB_LOCAL) {
18355f757f3fSDimitry Andric diagnose("non-exported symbol '" + toString(*sym) + "' in '" +
18365f757f3fSDimitry Andric toString(sym->file) + "' is referenced by DSO '" +
18375f757f3fSDimitry Andric toString(file) + "'");
18385f757f3fSDimitry Andric }
18395f757f3fSDimitry Andric }
18400b57cec5SDimitry Andric }
1841e8d8bef9SDimitry Andric }
1842bdd1243dSDimitry Andric }
18430b57cec5SDimitry Andric
1844e8d8bef9SDimitry Andric {
1845e8d8bef9SDimitry Andric llvm::TimeTraceScope timeScope("Add symbols to symtabs");
18460b57cec5SDimitry Andric // Now that we have defined all possible global symbols including linker-
18470b57cec5SDimitry Andric // synthesized ones. Visit all symbols to give the finishing touches.
1848bdd1243dSDimitry Andric for (Symbol *sym : symtab.getSymbols()) {
18490eae32dcSDimitry Andric if (!sym->isUsedInRegularObj || !includeInSymtab(*sym))
1850480093f4SDimitry Andric continue;
185104eeddc0SDimitry Andric if (!config->relocatable)
185204eeddc0SDimitry Andric sym->binding = sym->computeBinding();
18530b57cec5SDimitry Andric if (in.symTab)
18540b57cec5SDimitry Andric in.symTab->addSymbol(sym);
18550b57cec5SDimitry Andric
18560b57cec5SDimitry Andric if (sym->includeInDynsym()) {
18570b57cec5SDimitry Andric partitions[sym->partition - 1].dynSymTab->addSymbol(sym);
18580b57cec5SDimitry Andric if (auto *file = dyn_cast_or_null<SharedFile>(sym->file))
18590b57cec5SDimitry Andric if (file->isNeeded && !sym->isUndefined())
18600b57cec5SDimitry Andric addVerneed(sym);
18610b57cec5SDimitry Andric }
1862480093f4SDimitry Andric }
18630b57cec5SDimitry Andric
1864e8d8bef9SDimitry Andric // We also need to scan the dynamic relocation tables of the other
1865e8d8bef9SDimitry Andric // partitions and add any referenced symbols to the partition's dynsym.
18660b57cec5SDimitry Andric for (Partition &part : MutableArrayRef<Partition>(partitions).slice(1)) {
18670b57cec5SDimitry Andric DenseSet<Symbol *> syms;
18680b57cec5SDimitry Andric for (const SymbolTableEntry &e : part.dynSymTab->getSymbols())
18690b57cec5SDimitry Andric syms.insert(e.sym);
18700b57cec5SDimitry Andric for (DynamicReloc &reloc : part.relaDyn->relocs)
1871fe6060f1SDimitry Andric if (reloc.sym && reloc.needsDynSymIndex() &&
1872fe6060f1SDimitry Andric syms.insert(reloc.sym).second)
18730b57cec5SDimitry Andric part.dynSymTab->addSymbol(reloc.sym);
18740b57cec5SDimitry Andric }
1875e8d8bef9SDimitry Andric }
18760b57cec5SDimitry Andric
18770b57cec5SDimitry Andric if (in.mipsGot)
18780b57cec5SDimitry Andric in.mipsGot->build();
18790b57cec5SDimitry Andric
18800b57cec5SDimitry Andric removeUnusedSyntheticSections();
18815ffd83dbSDimitry Andric script->diagnoseOrphanHandling();
188206c3fb27SDimitry Andric script->diagnoseMissingSGSectionAddress();
18830b57cec5SDimitry Andric
18840b57cec5SDimitry Andric sortSections();
18850b57cec5SDimitry Andric
18864824e7fdSDimitry Andric // Create a list of OutputSections, assign sectionIndex, and populate
18874824e7fdSDimitry Andric // in.shStrTab.
18884824e7fdSDimitry Andric for (SectionCommand *cmd : script->sectionCommands)
188981ad6265SDimitry Andric if (auto *osd = dyn_cast<OutputDesc>(cmd)) {
189081ad6265SDimitry Andric OutputSection *osec = &osd->osec;
18914824e7fdSDimitry Andric outputSections.push_back(osec);
18924824e7fdSDimitry Andric osec->sectionIndex = outputSections.size();
18934824e7fdSDimitry Andric osec->shName = in.shStrTab->addString(osec->name);
18944824e7fdSDimitry Andric }
18950b57cec5SDimitry Andric
18960b57cec5SDimitry Andric // Prefer command line supplied address over other constraints.
18970b57cec5SDimitry Andric for (OutputSection *sec : outputSections) {
18980b57cec5SDimitry Andric auto i = config->sectionStartMap.find(sec->name);
18990b57cec5SDimitry Andric if (i != config->sectionStartMap.end())
19000b57cec5SDimitry Andric sec->addrExpr = [=] { return i->second; };
19010b57cec5SDimitry Andric }
19020b57cec5SDimitry Andric
19035ffd83dbSDimitry Andric // With the outputSections available check for GDPLT relocations
19045ffd83dbSDimitry Andric // and add __tls_get_addr symbol if needed.
19055ffd83dbSDimitry Andric if (config->emachine == EM_HEXAGON && hexagonNeedsTLSSymbol(outputSections)) {
19067a6dacacSDimitry Andric Symbol *sym =
19077a6dacacSDimitry Andric symtab.addSymbol(Undefined{ctx.internalFile, "__tls_get_addr",
19087a6dacacSDimitry Andric STB_GLOBAL, STV_DEFAULT, STT_NOTYPE});
19095ffd83dbSDimitry Andric sym->isPreemptible = true;
19105ffd83dbSDimitry Andric partitions[0].dynSymTab->addSymbol(sym);
19115ffd83dbSDimitry Andric }
19125ffd83dbSDimitry Andric
19130b57cec5SDimitry Andric // This is a bit of a hack. A value of 0 means undef, so we set it
19140b57cec5SDimitry Andric // to 1 to make __ehdr_start defined. The section number is not
19150b57cec5SDimitry Andric // particularly relevant.
19160b57cec5SDimitry Andric Out::elfHeader->sectionIndex = 1;
19174824e7fdSDimitry Andric Out::elfHeader->size = sizeof(typename ELFT::Ehdr);
19180b57cec5SDimitry Andric
19190b57cec5SDimitry Andric // Binary and relocatable output does not have PHDRS.
19200b57cec5SDimitry Andric // The headers have to be created before finalize as that can influence the
19210b57cec5SDimitry Andric // image base and the dynamic section on mips includes the image base.
19220b57cec5SDimitry Andric if (!config->relocatable && !config->oFormatBinary) {
19230b57cec5SDimitry Andric for (Partition &part : partitions) {
19240b57cec5SDimitry Andric part.phdrs = script->hasPhdrsCommands() ? script->createPhdrs()
19250b57cec5SDimitry Andric : createPhdrs(part);
19260b57cec5SDimitry Andric if (config->emachine == EM_ARM) {
19270b57cec5SDimitry Andric // PT_ARM_EXIDX is the ARM EHABI equivalent of PT_GNU_EH_FRAME
19280b57cec5SDimitry Andric addPhdrForSection(part, SHT_ARM_EXIDX, PT_ARM_EXIDX, PF_R);
19290b57cec5SDimitry Andric }
19300b57cec5SDimitry Andric if (config->emachine == EM_MIPS) {
19310b57cec5SDimitry Andric // Add separate segments for MIPS-specific sections.
19320b57cec5SDimitry Andric addPhdrForSection(part, SHT_MIPS_REGINFO, PT_MIPS_REGINFO, PF_R);
19330b57cec5SDimitry Andric addPhdrForSection(part, SHT_MIPS_OPTIONS, PT_MIPS_OPTIONS, PF_R);
19340b57cec5SDimitry Andric addPhdrForSection(part, SHT_MIPS_ABIFLAGS, PT_MIPS_ABIFLAGS, PF_R);
19350b57cec5SDimitry Andric }
193606c3fb27SDimitry Andric if (config->emachine == EM_RISCV)
193706c3fb27SDimitry Andric addPhdrForSection(part, SHT_RISCV_ATTRIBUTES, PT_RISCV_ATTRIBUTES,
193806c3fb27SDimitry Andric PF_R);
19390b57cec5SDimitry Andric }
19400b57cec5SDimitry Andric Out::programHeaders->size = sizeof(Elf_Phdr) * mainPart->phdrs.size();
19410b57cec5SDimitry Andric
19420b57cec5SDimitry Andric // Find the TLS segment. This happens before the section layout loop so that
19430b57cec5SDimitry Andric // Android relocation packing can look up TLS symbol addresses. We only need
19440b57cec5SDimitry Andric // to care about the main partition here because all TLS symbols were moved
19450b57cec5SDimitry Andric // to the main partition (see MarkLive.cpp).
19460b57cec5SDimitry Andric for (PhdrEntry *p : mainPart->phdrs)
19470b57cec5SDimitry Andric if (p->p_type == PT_TLS)
19480b57cec5SDimitry Andric Out::tlsPhdr = p;
19490b57cec5SDimitry Andric }
19500b57cec5SDimitry Andric
19510b57cec5SDimitry Andric // Some symbols are defined in term of program headers. Now that we
19520b57cec5SDimitry Andric // have the headers, we can find out which sections they point to.
19530b57cec5SDimitry Andric setReservedSymbolSections();
19540b57cec5SDimitry Andric
19550fca6ea1SDimitry Andric if (script->noCrossRefs.size()) {
19560fca6ea1SDimitry Andric llvm::TimeTraceScope timeScope("Check NOCROSSREFS");
19570fca6ea1SDimitry Andric checkNoCrossRefs<ELFT>();
19580fca6ea1SDimitry Andric }
19590fca6ea1SDimitry Andric
1960e8d8bef9SDimitry Andric {
1961e8d8bef9SDimitry Andric llvm::TimeTraceScope timeScope("Finalize synthetic sections");
1962e8d8bef9SDimitry Andric
196304eeddc0SDimitry Andric finalizeSynthetic(in.bss.get());
196404eeddc0SDimitry Andric finalizeSynthetic(in.bssRelRo.get());
196504eeddc0SDimitry Andric finalizeSynthetic(in.symTabShndx.get());
196604eeddc0SDimitry Andric finalizeSynthetic(in.shStrTab.get());
196704eeddc0SDimitry Andric finalizeSynthetic(in.strTab.get());
196804eeddc0SDimitry Andric finalizeSynthetic(in.got.get());
196904eeddc0SDimitry Andric finalizeSynthetic(in.mipsGot.get());
197004eeddc0SDimitry Andric finalizeSynthetic(in.igotPlt.get());
197104eeddc0SDimitry Andric finalizeSynthetic(in.gotPlt.get());
197204eeddc0SDimitry Andric finalizeSynthetic(in.relaPlt.get());
197304eeddc0SDimitry Andric finalizeSynthetic(in.plt.get());
197404eeddc0SDimitry Andric finalizeSynthetic(in.iplt.get());
197504eeddc0SDimitry Andric finalizeSynthetic(in.ppc32Got2.get());
197604eeddc0SDimitry Andric finalizeSynthetic(in.partIndex.get());
19770b57cec5SDimitry Andric
19780b57cec5SDimitry Andric // Dynamic section must be the last one in this list and dynamic
19790b57cec5SDimitry Andric // symbol table section (dynSymTab) must be the first one.
19800b57cec5SDimitry Andric for (Partition &part : partitions) {
19811fd87a68SDimitry Andric if (part.relaDyn) {
1982bdd1243dSDimitry Andric part.relaDyn->mergeRels();
19831fd87a68SDimitry Andric // Compute DT_RELACOUNT to be used by part.dynamic.
19841fd87a68SDimitry Andric part.relaDyn->partitionRels();
19851fd87a68SDimitry Andric finalizeSynthetic(part.relaDyn.get());
19861fd87a68SDimitry Andric }
1987bdd1243dSDimitry Andric if (part.relrDyn) {
1988bdd1243dSDimitry Andric part.relrDyn->mergeRels();
1989bdd1243dSDimitry Andric finalizeSynthetic(part.relrDyn.get());
1990bdd1243dSDimitry Andric }
19910fca6ea1SDimitry Andric if (part.relrAuthDyn) {
19920fca6ea1SDimitry Andric part.relrAuthDyn->mergeRels();
19930fca6ea1SDimitry Andric finalizeSynthetic(part.relrAuthDyn.get());
19940fca6ea1SDimitry Andric }
19951fd87a68SDimitry Andric
199604eeddc0SDimitry Andric finalizeSynthetic(part.dynSymTab.get());
199704eeddc0SDimitry Andric finalizeSynthetic(part.gnuHashTab.get());
199804eeddc0SDimitry Andric finalizeSynthetic(part.hashTab.get());
199904eeddc0SDimitry Andric finalizeSynthetic(part.verDef.get());
200004eeddc0SDimitry Andric finalizeSynthetic(part.ehFrameHdr.get());
200104eeddc0SDimitry Andric finalizeSynthetic(part.verSym.get());
200204eeddc0SDimitry Andric finalizeSynthetic(part.verNeed.get());
200304eeddc0SDimitry Andric finalizeSynthetic(part.dynamic.get());
20040b57cec5SDimitry Andric }
2005e8d8bef9SDimitry Andric }
20060b57cec5SDimitry Andric
20070b57cec5SDimitry Andric if (!script->hasSectionsCommand && !config->relocatable)
20080b57cec5SDimitry Andric fixSectionAlignments();
20090b57cec5SDimitry Andric
20100b57cec5SDimitry Andric // This is used to:
20110b57cec5SDimitry Andric // 1) Create "thunks":
20120b57cec5SDimitry Andric // Jump instructions in many ISAs have small displacements, and therefore
20130b57cec5SDimitry Andric // they cannot jump to arbitrary addresses in memory. For example, RISC-V
20140b57cec5SDimitry Andric // JAL instruction can target only +-1 MiB from PC. It is a linker's
20150b57cec5SDimitry Andric // responsibility to create and insert small pieces of code between
20160b57cec5SDimitry Andric // sections to extend the ranges if jump targets are out of range. Such
20170b57cec5SDimitry Andric // code pieces are called "thunks".
20180b57cec5SDimitry Andric //
20190b57cec5SDimitry Andric // We add thunks at this stage. We couldn't do this before this point
20200b57cec5SDimitry Andric // because this is the earliest point where we know sizes of sections and
20210b57cec5SDimitry Andric // their layouts (that are needed to determine if jump targets are in
20220b57cec5SDimitry Andric // range).
20230b57cec5SDimitry Andric //
20240b57cec5SDimitry Andric // 2) Update the sections. We need to generate content that depends on the
20250b57cec5SDimitry Andric // address of InputSections. For example, MIPS GOT section content or
20260b57cec5SDimitry Andric // android packed relocations sections content.
20270b57cec5SDimitry Andric //
20280b57cec5SDimitry Andric // 3) Assign the final values for the linker script symbols. Linker scripts
20290b57cec5SDimitry Andric // sometimes using forward symbol declarations. We want to set the correct
20300b57cec5SDimitry Andric // values. They also might change after adding the thunks.
20310b57cec5SDimitry Andric finalizeAddressDependentContent();
203204eeddc0SDimitry Andric
203304eeddc0SDimitry Andric // All information needed for OutputSection part of Map file is available.
20345ffd83dbSDimitry Andric if (errorCount())
20355ffd83dbSDimitry Andric return;
20360b57cec5SDimitry Andric
2037e8d8bef9SDimitry Andric {
2038e8d8bef9SDimitry Andric llvm::TimeTraceScope timeScope("Finalize synthetic sections");
2039e8d8bef9SDimitry Andric // finalizeAddressDependentContent may have added local symbols to the
2040e8d8bef9SDimitry Andric // static symbol table.
204104eeddc0SDimitry Andric finalizeSynthetic(in.symTab.get());
20420fca6ea1SDimitry Andric finalizeSynthetic(in.debugNames.get());
204304eeddc0SDimitry Andric finalizeSynthetic(in.ppc64LongBranchTarget.get());
204406c3fb27SDimitry Andric finalizeSynthetic(in.armCmseSGSection.get());
2045e8d8bef9SDimitry Andric }
20460b57cec5SDimitry Andric
20475ffd83dbSDimitry Andric // Relaxation to delete inter-basic block jumps created by basic block
20485ffd83dbSDimitry Andric // sections. Run after in.symTab is finalized as optimizeBasicBlockJumps
20495ffd83dbSDimitry Andric // can relax jump instructions based on symbol offset.
20505ffd83dbSDimitry Andric if (config->optimizeBBJumps)
20515ffd83dbSDimitry Andric optimizeBasicBlockJumps();
20525ffd83dbSDimitry Andric
20530b57cec5SDimitry Andric // Fill other section headers. The dynamic table is finalized
20540b57cec5SDimitry Andric // at the end because some tags like RELSZ depend on result
20550b57cec5SDimitry Andric // of finalizing other sections.
20560b57cec5SDimitry Andric for (OutputSection *sec : outputSections)
20570b57cec5SDimitry Andric sec->finalize();
205806c3fb27SDimitry Andric
20595f757f3fSDimitry Andric script->checkFinalScriptConditions();
206006c3fb27SDimitry Andric
206106c3fb27SDimitry Andric if (config->emachine == EM_ARM && !config->isLE && config->armBe8) {
206206c3fb27SDimitry Andric addArmInputSectionMappingSymbols();
206306c3fb27SDimitry Andric sortArmMappingSymbols();
206406c3fb27SDimitry Andric }
20650b57cec5SDimitry Andric }
20660b57cec5SDimitry Andric
20670b57cec5SDimitry Andric // Ensure data sections are not mixed with executable sections when
2068349cc55cSDimitry Andric // --execute-only is used. --execute-only make pages executable but not
2069349cc55cSDimitry Andric // readable.
checkExecuteOnly()20700b57cec5SDimitry Andric template <class ELFT> void Writer<ELFT>::checkExecuteOnly() {
20710b57cec5SDimitry Andric if (!config->executeOnly)
20720b57cec5SDimitry Andric return;
20730b57cec5SDimitry Andric
2074753f127fSDimitry Andric SmallVector<InputSection *, 0> storage;
207504eeddc0SDimitry Andric for (OutputSection *osec : outputSections)
207604eeddc0SDimitry Andric if (osec->flags & SHF_EXECINSTR)
2077753f127fSDimitry Andric for (InputSection *isec : getInputSections(*osec, storage))
20780b57cec5SDimitry Andric if (!(isec->flags & SHF_EXECINSTR))
207904eeddc0SDimitry Andric error("cannot place " + toString(isec) + " into " +
208004eeddc0SDimitry Andric toString(osec->name) +
208104eeddc0SDimitry Andric ": --execute-only does not support intermingling data and code");
20820b57cec5SDimitry Andric }
20830b57cec5SDimitry Andric
20840b57cec5SDimitry Andric // The linker is expected to define SECNAME_start and SECNAME_end
20850b57cec5SDimitry Andric // symbols for a few sections. This function defines them.
addStartEndSymbols()20860b57cec5SDimitry Andric template <class ELFT> void Writer<ELFT>::addStartEndSymbols() {
20870fca6ea1SDimitry Andric // If the associated output section does not exist, there is ambiguity as to
20880fca6ea1SDimitry Andric // how we define _start and _end symbols for an init/fini section. Users
20890fca6ea1SDimitry Andric // expect no "undefined symbol" linker errors and loaders expect equal
20900fca6ea1SDimitry Andric // st_value but do not particularly care whether the symbols are defined or
20910fca6ea1SDimitry Andric // not. We retain the output section so that the section indexes will be
20920fca6ea1SDimitry Andric // correct.
20930b57cec5SDimitry Andric auto define = [=](StringRef start, StringRef end, OutputSection *os) {
20940fca6ea1SDimitry Andric if (os) {
20950fca6ea1SDimitry Andric Defined *startSym = addOptionalRegular(start, os, 0);
20960fca6ea1SDimitry Andric Defined *stopSym = addOptionalRegular(end, os, -1);
20970fca6ea1SDimitry Andric if (startSym || stopSym)
20980fca6ea1SDimitry Andric os->usedInExpression = true;
20990b57cec5SDimitry Andric } else {
21000fca6ea1SDimitry Andric addOptionalRegular(start, Out::elfHeader, 0);
21010fca6ea1SDimitry Andric addOptionalRegular(end, Out::elfHeader, 0);
21020b57cec5SDimitry Andric }
21030b57cec5SDimitry Andric };
21040b57cec5SDimitry Andric
21050b57cec5SDimitry Andric define("__preinit_array_start", "__preinit_array_end", Out::preinitArray);
21060b57cec5SDimitry Andric define("__init_array_start", "__init_array_end", Out::initArray);
21070b57cec5SDimitry Andric define("__fini_array_start", "__fini_array_end", Out::finiArray);
21080b57cec5SDimitry Andric
21090fca6ea1SDimitry Andric // As a special case, don't unnecessarily retain .ARM.exidx, which would
21100fca6ea1SDimitry Andric // create an empty PT_ARM_EXIDX.
21110b57cec5SDimitry Andric if (OutputSection *sec = findSection(".ARM.exidx"))
21120b57cec5SDimitry Andric define("__exidx_start", "__exidx_end", sec);
21130b57cec5SDimitry Andric }
21140b57cec5SDimitry Andric
21150b57cec5SDimitry Andric // If a section name is valid as a C identifier (which is rare because of
21160b57cec5SDimitry Andric // the leading '.'), linkers are expected to define __start_<secname> and
21170b57cec5SDimitry Andric // __stop_<secname> symbols. They are at beginning and end of the section,
21180b57cec5SDimitry Andric // respectively. This is not requested by the ELF standard, but GNU ld and
21190b57cec5SDimitry Andric // gold provide the feature, and used by many programs.
21200b57cec5SDimitry Andric template <class ELFT>
addStartStopSymbols(OutputSection & osec)212181ad6265SDimitry Andric void Writer<ELFT>::addStartStopSymbols(OutputSection &osec) {
212281ad6265SDimitry Andric StringRef s = osec.name;
21230b57cec5SDimitry Andric if (!isValidCIdentifier(s))
21240b57cec5SDimitry Andric return;
21250fca6ea1SDimitry Andric Defined *startSym = addOptionalRegular(saver().save("__start_" + s), &osec, 0,
21265ffd83dbSDimitry Andric config->zStartStopVisibility);
21270fca6ea1SDimitry Andric Defined *stopSym = addOptionalRegular(saver().save("__stop_" + s), &osec, -1,
21285ffd83dbSDimitry Andric config->zStartStopVisibility);
21290fca6ea1SDimitry Andric if (startSym || stopSym)
21300fca6ea1SDimitry Andric osec.usedInExpression = true;
21310b57cec5SDimitry Andric }
21320b57cec5SDimitry Andric
needsPtLoad(OutputSection * sec)21330b57cec5SDimitry Andric static bool needsPtLoad(OutputSection *sec) {
2134fe6060f1SDimitry Andric if (!(sec->flags & SHF_ALLOC))
21350b57cec5SDimitry Andric return false;
21360b57cec5SDimitry Andric
21370b57cec5SDimitry Andric // Don't allocate VA space for TLS NOBITS sections. The PT_TLS PHDR is
21380b57cec5SDimitry Andric // responsible for allocating space for them, not the PT_LOAD that
21390b57cec5SDimitry Andric // contains the TLS initialization image.
21400b57cec5SDimitry Andric if ((sec->flags & SHF_TLS) && sec->type == SHT_NOBITS)
21410b57cec5SDimitry Andric return false;
21420b57cec5SDimitry Andric return true;
21430b57cec5SDimitry Andric }
21440b57cec5SDimitry Andric
21450fca6ea1SDimitry Andric // Adjust phdr flags according to certain options.
computeFlags(uint64_t flags)21460b57cec5SDimitry Andric static uint64_t computeFlags(uint64_t flags) {
21470b57cec5SDimitry Andric if (config->omagic)
21480b57cec5SDimitry Andric return PF_R | PF_W | PF_X;
21490b57cec5SDimitry Andric if (config->executeOnly && (flags & PF_X))
21500b57cec5SDimitry Andric return flags & ~PF_R;
21510b57cec5SDimitry Andric return flags;
21520b57cec5SDimitry Andric }
21530b57cec5SDimitry Andric
21540b57cec5SDimitry Andric // Decide which program headers to create and which sections to include in each
21550b57cec5SDimitry Andric // one.
21560b57cec5SDimitry Andric template <class ELFT>
createPhdrs(Partition & part)215704eeddc0SDimitry Andric SmallVector<PhdrEntry *, 0> Writer<ELFT>::createPhdrs(Partition &part) {
215804eeddc0SDimitry Andric SmallVector<PhdrEntry *, 0> ret;
21590b57cec5SDimitry Andric auto addHdr = [&](unsigned type, unsigned flags) -> PhdrEntry * {
21600b57cec5SDimitry Andric ret.push_back(make<PhdrEntry>(type, flags));
21610b57cec5SDimitry Andric return ret.back();
21620b57cec5SDimitry Andric };
21630b57cec5SDimitry Andric
21640b57cec5SDimitry Andric unsigned partNo = part.getNumber();
21650b57cec5SDimitry Andric bool isMain = partNo == 1;
21660b57cec5SDimitry Andric
216785868e8aSDimitry Andric // Add the first PT_LOAD segment for regular output sections.
216885868e8aSDimitry Andric uint64_t flags = computeFlags(PF_R);
216985868e8aSDimitry Andric PhdrEntry *load = nullptr;
217085868e8aSDimitry Andric
217185868e8aSDimitry Andric // nmagic or omagic output does not have PT_PHDR, PT_INTERP, or the readonly
217285868e8aSDimitry Andric // PT_LOAD.
217385868e8aSDimitry Andric if (!config->nmagic && !config->omagic) {
217485868e8aSDimitry Andric // The first phdr entry is PT_PHDR which describes the program header
217585868e8aSDimitry Andric // itself.
21760b57cec5SDimitry Andric if (isMain)
21770b57cec5SDimitry Andric addHdr(PT_PHDR, PF_R)->add(Out::programHeaders);
21780b57cec5SDimitry Andric else
21790b57cec5SDimitry Andric addHdr(PT_PHDR, PF_R)->add(part.programHeaders->getParent());
21800b57cec5SDimitry Andric
21810b57cec5SDimitry Andric // PT_INTERP must be the second entry if exists.
21820b57cec5SDimitry Andric if (OutputSection *cmd = findSection(".interp", partNo))
21830b57cec5SDimitry Andric addHdr(PT_INTERP, cmd->getPhdrFlags())->add(cmd);
21840b57cec5SDimitry Andric
21850b57cec5SDimitry Andric // Add the headers. We will remove them if they don't fit.
21860b57cec5SDimitry Andric // In the other partitions the headers are ordinary sections, so they don't
21870b57cec5SDimitry Andric // need to be added here.
21880b57cec5SDimitry Andric if (isMain) {
21890b57cec5SDimitry Andric load = addHdr(PT_LOAD, flags);
21900b57cec5SDimitry Andric load->add(Out::elfHeader);
21910b57cec5SDimitry Andric load->add(Out::programHeaders);
21920b57cec5SDimitry Andric }
219385868e8aSDimitry Andric }
21940b57cec5SDimitry Andric
21950b57cec5SDimitry Andric // PT_GNU_RELRO includes all sections that should be marked as
2196480093f4SDimitry Andric // read-only by dynamic linker after processing relocations.
21970b57cec5SDimitry Andric // Current dynamic loaders only support one PT_GNU_RELRO PHDR, give
21980b57cec5SDimitry Andric // an error message if more than one PT_GNU_RELRO PHDR is required.
21990b57cec5SDimitry Andric PhdrEntry *relRo = make<PhdrEntry>(PT_GNU_RELRO, PF_R);
22000b57cec5SDimitry Andric bool inRelroPhdr = false;
22010b57cec5SDimitry Andric OutputSection *relroEnd = nullptr;
22020b57cec5SDimitry Andric for (OutputSection *sec : outputSections) {
22030b57cec5SDimitry Andric if (sec->partition != partNo || !needsPtLoad(sec))
22040b57cec5SDimitry Andric continue;
22050b57cec5SDimitry Andric if (isRelroSection(sec)) {
22060b57cec5SDimitry Andric inRelroPhdr = true;
22070b57cec5SDimitry Andric if (!relroEnd)
22080b57cec5SDimitry Andric relRo->add(sec);
22090b57cec5SDimitry Andric else
22100b57cec5SDimitry Andric error("section: " + sec->name + " is not contiguous with other relro" +
22110b57cec5SDimitry Andric " sections");
22120b57cec5SDimitry Andric } else if (inRelroPhdr) {
22130b57cec5SDimitry Andric inRelroPhdr = false;
22140b57cec5SDimitry Andric relroEnd = sec;
22150b57cec5SDimitry Andric }
22160b57cec5SDimitry Andric }
22175f757f3fSDimitry Andric relRo->p_align = 1;
22180b57cec5SDimitry Andric
22190b57cec5SDimitry Andric for (OutputSection *sec : outputSections) {
22200b57cec5SDimitry Andric if (!needsPtLoad(sec))
22210b57cec5SDimitry Andric continue;
22220b57cec5SDimitry Andric
22230b57cec5SDimitry Andric // Normally, sections in partitions other than the current partition are
22240b57cec5SDimitry Andric // ignored. But partition number 255 is a special case: it contains the
22250b57cec5SDimitry Andric // partition end marker (.part.end). It needs to be added to the main
22260b57cec5SDimitry Andric // partition so that a segment is created for it in the main partition,
22270b57cec5SDimitry Andric // which will cause the dynamic loader to reserve space for the other
22280b57cec5SDimitry Andric // partitions.
22290b57cec5SDimitry Andric if (sec->partition != partNo) {
22300b57cec5SDimitry Andric if (isMain && sec->partition == 255)
22310b57cec5SDimitry Andric addHdr(PT_LOAD, computeFlags(sec->getPhdrFlags()))->add(sec);
22320b57cec5SDimitry Andric continue;
22330b57cec5SDimitry Andric }
22340b57cec5SDimitry Andric
22350b57cec5SDimitry Andric // Segments are contiguous memory regions that has the same attributes
22360b57cec5SDimitry Andric // (e.g. executable or writable). There is one phdr for each segment.
22370b57cec5SDimitry Andric // Therefore, we need to create a new phdr when the next section has
22380fca6ea1SDimitry Andric // incompatible flags or is loaded at a discontiguous address or memory
22390fca6ea1SDimitry Andric // region using AT or AT> linker script command, respectively.
224006c3fb27SDimitry Andric //
224106c3fb27SDimitry Andric // As an exception, we don't create a separate load segment for the ELF
224206c3fb27SDimitry Andric // headers, even if the first "real" output has an AT or AT> attribute.
224306c3fb27SDimitry Andric //
224406c3fb27SDimitry Andric // In addition, NOBITS sections should only be placed at the end of a LOAD
224506c3fb27SDimitry Andric // segment (since it's represented as p_filesz < p_memsz). If we have a
224606c3fb27SDimitry Andric // not-NOBITS section after a NOBITS, we create a new LOAD for the latter
224706c3fb27SDimitry Andric // even if flags match, so as not to require actually writing the
224806c3fb27SDimitry Andric // supposed-to-be-NOBITS section to the output file. (However, we cannot do
224906c3fb27SDimitry Andric // so when hasSectionsCommand, since we cannot introduce the extra alignment
225006c3fb27SDimitry Andric // needed to create a new LOAD)
22510b57cec5SDimitry Andric uint64_t newFlags = computeFlags(sec->getPhdrFlags());
22520fca6ea1SDimitry Andric // When --no-rosegment is specified, RO and RX sections are compatible.
22530fca6ea1SDimitry Andric uint32_t incompatible = flags ^ newFlags;
22540fca6ea1SDimitry Andric if (config->singleRoRx && !(newFlags & PF_W))
22550fca6ea1SDimitry Andric incompatible &= ~PF_X;
22560fca6ea1SDimitry Andric if (incompatible)
22570fca6ea1SDimitry Andric load = nullptr;
22580fca6ea1SDimitry Andric
22595ffd83dbSDimitry Andric bool sameLMARegion =
22605ffd83dbSDimitry Andric load && !sec->lmaExpr && sec->lmaRegion == load->firstSec->lmaRegion;
22610fca6ea1SDimitry Andric if (load && sec != relroEnd &&
22625ffd83dbSDimitry Andric sec->memRegion == load->firstSec->memRegion &&
226306c3fb27SDimitry Andric (sameLMARegion || load->lastSec == Out::programHeaders) &&
226406c3fb27SDimitry Andric (script->hasSectionsCommand || sec->type == SHT_NOBITS ||
22650fca6ea1SDimitry Andric load->lastSec->type != SHT_NOBITS)) {
22660fca6ea1SDimitry Andric load->p_flags |= newFlags;
22670fca6ea1SDimitry Andric } else {
22680b57cec5SDimitry Andric load = addHdr(PT_LOAD, newFlags);
22690b57cec5SDimitry Andric flags = newFlags;
22700b57cec5SDimitry Andric }
22710b57cec5SDimitry Andric
22720b57cec5SDimitry Andric load->add(sec);
22730b57cec5SDimitry Andric }
22740b57cec5SDimitry Andric
22750b57cec5SDimitry Andric // Add a TLS segment if any.
22760b57cec5SDimitry Andric PhdrEntry *tlsHdr = make<PhdrEntry>(PT_TLS, PF_R);
22770b57cec5SDimitry Andric for (OutputSection *sec : outputSections)
22780b57cec5SDimitry Andric if (sec->partition == partNo && sec->flags & SHF_TLS)
22790b57cec5SDimitry Andric tlsHdr->add(sec);
22800b57cec5SDimitry Andric if (tlsHdr->firstSec)
22810b57cec5SDimitry Andric ret.push_back(tlsHdr);
22820b57cec5SDimitry Andric
22830b57cec5SDimitry Andric // Add an entry for .dynamic.
22840b57cec5SDimitry Andric if (OutputSection *sec = part.dynamic->getParent())
22850b57cec5SDimitry Andric addHdr(PT_DYNAMIC, sec->getPhdrFlags())->add(sec);
22860b57cec5SDimitry Andric
22870b57cec5SDimitry Andric if (relRo->firstSec)
22880b57cec5SDimitry Andric ret.push_back(relRo);
22890b57cec5SDimitry Andric
22900b57cec5SDimitry Andric // PT_GNU_EH_FRAME is a special section pointing on .eh_frame_hdr.
22910b57cec5SDimitry Andric if (part.ehFrame->isNeeded() && part.ehFrameHdr &&
22920b57cec5SDimitry Andric part.ehFrame->getParent() && part.ehFrameHdr->getParent())
22930b57cec5SDimitry Andric addHdr(PT_GNU_EH_FRAME, part.ehFrameHdr->getParent()->getPhdrFlags())
22940b57cec5SDimitry Andric ->add(part.ehFrameHdr->getParent());
22950b57cec5SDimitry Andric
22960fca6ea1SDimitry Andric if (config->osabi == ELFOSABI_OPENBSD) {
22970fca6ea1SDimitry Andric // PT_OPENBSD_MUTABLE makes the dynamic linker fill the segment with
22980fca6ea1SDimitry Andric // zero data, like bss, but it can be treated differently.
22990fca6ea1SDimitry Andric if (OutputSection *cmd = findSection(".openbsd.mutable", partNo))
23000fca6ea1SDimitry Andric addHdr(PT_OPENBSD_MUTABLE, cmd->getPhdrFlags())->add(cmd);
23010fca6ea1SDimitry Andric
23020fca6ea1SDimitry Andric // PT_OPENBSD_RANDOMIZE makes the dynamic linker fill the segment
23030fca6ea1SDimitry Andric // with random data.
23040b57cec5SDimitry Andric if (OutputSection *cmd = findSection(".openbsd.randomdata", partNo))
23050b57cec5SDimitry Andric addHdr(PT_OPENBSD_RANDOMIZE, cmd->getPhdrFlags())->add(cmd);
23060b57cec5SDimitry Andric
23070fca6ea1SDimitry Andric // PT_OPENBSD_SYSCALLS makes the kernel and dynamic linker register
23080fca6ea1SDimitry Andric // system call sites.
23090fca6ea1SDimitry Andric if (OutputSection *cmd = findSection(".openbsd.syscalls", partNo))
23100fca6ea1SDimitry Andric addHdr(PT_OPENBSD_SYSCALLS, cmd->getPhdrFlags())->add(cmd);
23110fca6ea1SDimitry Andric }
23120fca6ea1SDimitry Andric
2313480093f4SDimitry Andric if (config->zGnustack != GnuStackKind::None) {
23140b57cec5SDimitry Andric // PT_GNU_STACK is a special section to tell the loader to make the
23150b57cec5SDimitry Andric // pages for the stack non-executable. If you really want an executable
23160b57cec5SDimitry Andric // stack, you can pass -z execstack, but that's not recommended for
23170b57cec5SDimitry Andric // security reasons.
23180b57cec5SDimitry Andric unsigned perm = PF_R | PF_W;
2319480093f4SDimitry Andric if (config->zGnustack == GnuStackKind::Exec)
23200b57cec5SDimitry Andric perm |= PF_X;
23210b57cec5SDimitry Andric addHdr(PT_GNU_STACK, perm)->p_memsz = config->zStackSize;
2322480093f4SDimitry Andric }
23230b57cec5SDimitry Andric
23240b57cec5SDimitry Andric // PT_OPENBSD_WXNEEDED is a OpenBSD-specific header to mark the executable
23250b57cec5SDimitry Andric // is expected to perform W^X violations, such as calling mprotect(2) or
23260b57cec5SDimitry Andric // mmap(2) with PROT_WRITE | PROT_EXEC, which is prohibited by default on
23270b57cec5SDimitry Andric // OpenBSD.
23280b57cec5SDimitry Andric if (config->zWxneeded)
23290b57cec5SDimitry Andric addHdr(PT_OPENBSD_WXNEEDED, PF_X);
23300b57cec5SDimitry Andric
2331480093f4SDimitry Andric if (OutputSection *cmd = findSection(".note.gnu.property", partNo))
2332480093f4SDimitry Andric addHdr(PT_GNU_PROPERTY, PF_R)->add(cmd);
2333480093f4SDimitry Andric
23340b57cec5SDimitry Andric // Create one PT_NOTE per a group of contiguous SHT_NOTE sections with the
23350b57cec5SDimitry Andric // same alignment.
23360b57cec5SDimitry Andric PhdrEntry *note = nullptr;
23370b57cec5SDimitry Andric for (OutputSection *sec : outputSections) {
23380b57cec5SDimitry Andric if (sec->partition != partNo)
23390b57cec5SDimitry Andric continue;
23400b57cec5SDimitry Andric if (sec->type == SHT_NOTE && (sec->flags & SHF_ALLOC)) {
2341bdd1243dSDimitry Andric if (!note || sec->lmaExpr || note->lastSec->addralign != sec->addralign)
23420b57cec5SDimitry Andric note = addHdr(PT_NOTE, PF_R);
23430b57cec5SDimitry Andric note->add(sec);
23440b57cec5SDimitry Andric } else {
23450b57cec5SDimitry Andric note = nullptr;
23460b57cec5SDimitry Andric }
23470b57cec5SDimitry Andric }
23480b57cec5SDimitry Andric return ret;
23490b57cec5SDimitry Andric }
23500b57cec5SDimitry Andric
23510b57cec5SDimitry Andric template <class ELFT>
addPhdrForSection(Partition & part,unsigned shType,unsigned pType,unsigned pFlags)23520b57cec5SDimitry Andric void Writer<ELFT>::addPhdrForSection(Partition &part, unsigned shType,
23530b57cec5SDimitry Andric unsigned pType, unsigned pFlags) {
23540b57cec5SDimitry Andric unsigned partNo = part.getNumber();
23550b57cec5SDimitry Andric auto i = llvm::find_if(outputSections, [=](OutputSection *cmd) {
23560b57cec5SDimitry Andric return cmd->partition == partNo && cmd->type == shType;
23570b57cec5SDimitry Andric });
23580b57cec5SDimitry Andric if (i == outputSections.end())
23590b57cec5SDimitry Andric return;
23600b57cec5SDimitry Andric
23610b57cec5SDimitry Andric PhdrEntry *entry = make<PhdrEntry>(pType, pFlags);
23620b57cec5SDimitry Andric entry->add(*i);
23630b57cec5SDimitry Andric part.phdrs.push_back(entry);
23640b57cec5SDimitry Andric }
23650b57cec5SDimitry Andric
236685868e8aSDimitry Andric // Place the first section of each PT_LOAD to a different page (of maxPageSize).
236785868e8aSDimitry Andric // This is achieved by assigning an alignment expression to addrExpr of each
236885868e8aSDimitry Andric // such section.
fixSectionAlignments()23690b57cec5SDimitry Andric template <class ELFT> void Writer<ELFT>::fixSectionAlignments() {
237085868e8aSDimitry Andric const PhdrEntry *prev;
237185868e8aSDimitry Andric auto pageAlign = [&](const PhdrEntry *p) {
237285868e8aSDimitry Andric OutputSection *cmd = p->firstSec;
23735ffd83dbSDimitry Andric if (!cmd)
23745ffd83dbSDimitry Andric return;
2375bdd1243dSDimitry Andric cmd->alignExpr = [align = cmd->addralign]() { return align; };
23765ffd83dbSDimitry Andric if (!cmd->addrExpr) {
237785868e8aSDimitry Andric // Prefer advancing to align(dot, maxPageSize) + dot%maxPageSize to avoid
237885868e8aSDimitry Andric // padding in the file contents.
237985868e8aSDimitry Andric //
238085868e8aSDimitry Andric // When -z separate-code is used we must not have any overlap in pages
238185868e8aSDimitry Andric // between an executable segment and a non-executable segment. We align to
238285868e8aSDimitry Andric // the next maximum page size boundary on transitions between executable
238385868e8aSDimitry Andric // and non-executable segments.
238485868e8aSDimitry Andric //
238585868e8aSDimitry Andric // SHT_LLVM_PART_EHDR marks the start of a partition. The partition
238685868e8aSDimitry Andric // sections will be extracted to a separate file. Align to the next
238785868e8aSDimitry Andric // maximum page size boundary so that we can find the ELF header at the
238885868e8aSDimitry Andric // start. We cannot benefit from overlapping p_offset ranges with the
238985868e8aSDimitry Andric // previous segment anyway.
239085868e8aSDimitry Andric if (config->zSeparate == SeparateSegmentKind::Loadable ||
239185868e8aSDimitry Andric (config->zSeparate == SeparateSegmentKind::Code && prev &&
239285868e8aSDimitry Andric (prev->p_flags & PF_X) != (p->p_flags & PF_X)) ||
239385868e8aSDimitry Andric cmd->type == SHT_LLVM_PART_EHDR)
239485868e8aSDimitry Andric cmd->addrExpr = [] {
2395972a253aSDimitry Andric return alignToPowerOf2(script->getDot(), config->maxPageSize);
23960b57cec5SDimitry Andric };
239785868e8aSDimitry Andric // PT_TLS is at the start of the first RW PT_LOAD. If `p` includes PT_TLS,
239885868e8aSDimitry Andric // it must be the RW. Align to p_align(PT_TLS) to make sure
239985868e8aSDimitry Andric // p_vaddr(PT_LOAD)%p_align(PT_LOAD) = 0. Otherwise, if
240085868e8aSDimitry Andric // sh_addralign(.tdata) < sh_addralign(.tbss), we will set p_align(PT_TLS)
240185868e8aSDimitry Andric // to sh_addralign(.tbss), while p_vaddr(PT_TLS)=p_vaddr(PT_LOAD) may not
240285868e8aSDimitry Andric // be congruent to 0 modulo p_align(PT_TLS).
240385868e8aSDimitry Andric //
240485868e8aSDimitry Andric // Technically this is not required, but as of 2019, some dynamic loaders
240585868e8aSDimitry Andric // don't handle p_vaddr%p_align != 0 correctly, e.g. glibc (i386 and
240685868e8aSDimitry Andric // x86-64) doesn't make runtime address congruent to p_vaddr modulo
240785868e8aSDimitry Andric // p_align for dynamic TLS blocks (PR/24606), FreeBSD rtld has the same
240885868e8aSDimitry Andric // bug, musl (TLS Variant 1 architectures) before 1.1.23 handled TLS
240985868e8aSDimitry Andric // blocks correctly. We need to keep the workaround for a while.
241085868e8aSDimitry Andric else if (Out::tlsPhdr && Out::tlsPhdr->firstSec == p->firstSec)
241185868e8aSDimitry Andric cmd->addrExpr = [] {
2412972a253aSDimitry Andric return alignToPowerOf2(script->getDot(), config->maxPageSize) +
2413972a253aSDimitry Andric alignToPowerOf2(script->getDot() % config->maxPageSize,
241485868e8aSDimitry Andric Out::tlsPhdr->p_align);
241585868e8aSDimitry Andric };
241685868e8aSDimitry Andric else
241785868e8aSDimitry Andric cmd->addrExpr = [] {
2418972a253aSDimitry Andric return alignToPowerOf2(script->getDot(), config->maxPageSize) +
241985868e8aSDimitry Andric script->getDot() % config->maxPageSize;
242085868e8aSDimitry Andric };
242185868e8aSDimitry Andric }
24220b57cec5SDimitry Andric };
24230b57cec5SDimitry Andric
24240b57cec5SDimitry Andric for (Partition &part : partitions) {
242585868e8aSDimitry Andric prev = nullptr;
24260b57cec5SDimitry Andric for (const PhdrEntry *p : part.phdrs)
242785868e8aSDimitry Andric if (p->p_type == PT_LOAD && p->firstSec) {
242885868e8aSDimitry Andric pageAlign(p);
242985868e8aSDimitry Andric prev = p;
243085868e8aSDimitry Andric }
24310b57cec5SDimitry Andric }
24320b57cec5SDimitry Andric }
24330b57cec5SDimitry Andric
24340b57cec5SDimitry Andric // Compute an in-file position for a given section. The file offset must be the
24350b57cec5SDimitry Andric // same with its virtual address modulo the page size, so that the loader can
24360b57cec5SDimitry Andric // load executables without any address adjustment.
computeFileOffset(OutputSection * os,uint64_t off)24370b57cec5SDimitry Andric static uint64_t computeFileOffset(OutputSection *os, uint64_t off) {
24380b57cec5SDimitry Andric // The first section in a PT_LOAD has to have congruent offset and address
243985868e8aSDimitry Andric // modulo the maximum page size.
244085868e8aSDimitry Andric if (os->ptLoad && os->ptLoad->firstSec == os)
244185868e8aSDimitry Andric return alignTo(off, os->ptLoad->p_align, os->addr);
24420b57cec5SDimitry Andric
24430b57cec5SDimitry Andric // File offsets are not significant for .bss sections other than the first one
2444349cc55cSDimitry Andric // in a PT_LOAD/PT_TLS. By convention, we keep section offsets monotonically
24450b57cec5SDimitry Andric // increasing rather than setting to zero.
2446349cc55cSDimitry Andric if (os->type == SHT_NOBITS &&
2447349cc55cSDimitry Andric (!Out::tlsPhdr || Out::tlsPhdr->firstSec != os))
24480b57cec5SDimitry Andric return off;
24490b57cec5SDimitry Andric
24500b57cec5SDimitry Andric // If the section is not in a PT_LOAD, we just have to align it.
24510b57cec5SDimitry Andric if (!os->ptLoad)
2452bdd1243dSDimitry Andric return alignToPowerOf2(off, os->addralign);
24530b57cec5SDimitry Andric
24540b57cec5SDimitry Andric // If two sections share the same PT_LOAD the file offset is calculated
24550b57cec5SDimitry Andric // using this formula: Off2 = Off1 + (VA2 - VA1).
24560b57cec5SDimitry Andric OutputSection *first = os->ptLoad->firstSec;
24570b57cec5SDimitry Andric return first->offset + os->addr - first->addr;
24580b57cec5SDimitry Andric }
24590b57cec5SDimitry Andric
assignFileOffsetsBinary()24600b57cec5SDimitry Andric template <class ELFT> void Writer<ELFT>::assignFileOffsetsBinary() {
2461e8d8bef9SDimitry Andric // Compute the minimum LMA of all non-empty non-NOBITS sections as minAddr.
2462e8d8bef9SDimitry Andric auto needsOffset = [](OutputSection &sec) {
2463e8d8bef9SDimitry Andric return sec.type != SHT_NOBITS && (sec.flags & SHF_ALLOC) && sec.size > 0;
2464e8d8bef9SDimitry Andric };
2465e8d8bef9SDimitry Andric uint64_t minAddr = UINT64_MAX;
24660b57cec5SDimitry Andric for (OutputSection *sec : outputSections)
2467e8d8bef9SDimitry Andric if (needsOffset(*sec)) {
2468e8d8bef9SDimitry Andric sec->offset = sec->getLMA();
2469e8d8bef9SDimitry Andric minAddr = std::min(minAddr, sec->offset);
2470e8d8bef9SDimitry Andric }
2471e8d8bef9SDimitry Andric
2472e8d8bef9SDimitry Andric // Sections are laid out at LMA minus minAddr.
2473e8d8bef9SDimitry Andric fileSize = 0;
2474e8d8bef9SDimitry Andric for (OutputSection *sec : outputSections)
2475e8d8bef9SDimitry Andric if (needsOffset(*sec)) {
2476e8d8bef9SDimitry Andric sec->offset -= minAddr;
2477e8d8bef9SDimitry Andric fileSize = std::max(fileSize, sec->offset + sec->size);
2478e8d8bef9SDimitry Andric }
24790b57cec5SDimitry Andric }
24800b57cec5SDimitry Andric
rangeToString(uint64_t addr,uint64_t len)24810b57cec5SDimitry Andric static std::string rangeToString(uint64_t addr, uint64_t len) {
24820b57cec5SDimitry Andric return "[0x" + utohexstr(addr) + ", 0x" + utohexstr(addr + len - 1) + "]";
24830b57cec5SDimitry Andric }
24840b57cec5SDimitry Andric
24850b57cec5SDimitry Andric // Assign file offsets to output sections.
assignFileOffsets()24860b57cec5SDimitry Andric template <class ELFT> void Writer<ELFT>::assignFileOffsets() {
24874824e7fdSDimitry Andric Out::programHeaders->offset = Out::elfHeader->size;
24884824e7fdSDimitry Andric uint64_t off = Out::elfHeader->size + Out::programHeaders->size;
24890b57cec5SDimitry Andric
24900b57cec5SDimitry Andric PhdrEntry *lastRX = nullptr;
24910b57cec5SDimitry Andric for (Partition &part : partitions)
24920b57cec5SDimitry Andric for (PhdrEntry *p : part.phdrs)
24930b57cec5SDimitry Andric if (p->p_type == PT_LOAD && (p->p_flags & PF_X))
24940b57cec5SDimitry Andric lastRX = p;
24950b57cec5SDimitry Andric
2496e8d8bef9SDimitry Andric // Layout SHF_ALLOC sections before non-SHF_ALLOC sections. A non-SHF_ALLOC
2497e8d8bef9SDimitry Andric // will not occupy file offsets contained by a PT_LOAD.
24980b57cec5SDimitry Andric for (OutputSection *sec : outputSections) {
2499e8d8bef9SDimitry Andric if (!(sec->flags & SHF_ALLOC))
2500e8d8bef9SDimitry Andric continue;
25014824e7fdSDimitry Andric off = computeFileOffset(sec, off);
25024824e7fdSDimitry Andric sec->offset = off;
25034824e7fdSDimitry Andric if (sec->type != SHT_NOBITS)
25044824e7fdSDimitry Andric off += sec->size;
25050b57cec5SDimitry Andric
25060b57cec5SDimitry Andric // If this is a last section of the last executable segment and that
25070b57cec5SDimitry Andric // segment is the last loadable segment, align the offset of the
25080b57cec5SDimitry Andric // following section to avoid loading non-segments parts of the file.
250985868e8aSDimitry Andric if (config->zSeparate != SeparateSegmentKind::None && lastRX &&
251085868e8aSDimitry Andric lastRX->lastSec == sec)
2511972a253aSDimitry Andric off = alignToPowerOf2(off, config->maxPageSize);
25120b57cec5SDimitry Andric }
25130fca6ea1SDimitry Andric for (OutputSection *osec : outputSections) {
25140fca6ea1SDimitry Andric if (osec->flags & SHF_ALLOC)
25150fca6ea1SDimitry Andric continue;
2516bdd1243dSDimitry Andric osec->offset = alignToPowerOf2(off, osec->addralign);
25174824e7fdSDimitry Andric off = osec->offset + osec->size;
25184824e7fdSDimitry Andric }
25190b57cec5SDimitry Andric
2520972a253aSDimitry Andric sectionHeaderOff = alignToPowerOf2(off, config->wordsize);
25210b57cec5SDimitry Andric fileSize = sectionHeaderOff + (outputSections.size() + 1) * sizeof(Elf_Shdr);
25220b57cec5SDimitry Andric
25230b57cec5SDimitry Andric // Our logic assumes that sections have rising VA within the same segment.
25240b57cec5SDimitry Andric // With use of linker scripts it is possible to violate this rule and get file
25250b57cec5SDimitry Andric // offset overlaps or overflows. That should never happen with a valid script
25260b57cec5SDimitry Andric // which does not move the location counter backwards and usually scripts do
25270b57cec5SDimitry Andric // not do that. Unfortunately, there are apps in the wild, for example, Linux
25280b57cec5SDimitry Andric // kernel, which control segment distribution explicitly and move the counter
25290b57cec5SDimitry Andric // backwards, so we have to allow doing that to support linking them. We
25300b57cec5SDimitry Andric // perform non-critical checks for overlaps in checkSectionOverlap(), but here
25310b57cec5SDimitry Andric // we want to prevent file size overflows because it would crash the linker.
25320b57cec5SDimitry Andric for (OutputSection *sec : outputSections) {
25330b57cec5SDimitry Andric if (sec->type == SHT_NOBITS)
25340b57cec5SDimitry Andric continue;
25350b57cec5SDimitry Andric if ((sec->offset > fileSize) || (sec->offset + sec->size > fileSize))
25360b57cec5SDimitry Andric error("unable to place section " + sec->name + " at file offset " +
25370b57cec5SDimitry Andric rangeToString(sec->offset, sec->size) +
25380b57cec5SDimitry Andric "; check your linker script for overflows");
25390b57cec5SDimitry Andric }
25400b57cec5SDimitry Andric }
25410b57cec5SDimitry Andric
25420b57cec5SDimitry Andric // Finalize the program headers. We call this function after we assign
25430b57cec5SDimitry Andric // file offsets and VAs to all sections.
setPhdrs(Partition & part)25440b57cec5SDimitry Andric template <class ELFT> void Writer<ELFT>::setPhdrs(Partition &part) {
25450b57cec5SDimitry Andric for (PhdrEntry *p : part.phdrs) {
25460b57cec5SDimitry Andric OutputSection *first = p->firstSec;
25470b57cec5SDimitry Andric OutputSection *last = p->lastSec;
25480b57cec5SDimitry Andric
254906c3fb27SDimitry Andric // .ARM.exidx sections may not be within a single .ARM.exidx
255006c3fb27SDimitry Andric // output section. We always want to describe just the
255106c3fb27SDimitry Andric // SyntheticSection.
255206c3fb27SDimitry Andric if (part.armExidx && p->p_type == PT_ARM_EXIDX) {
255306c3fb27SDimitry Andric p->p_filesz = part.armExidx->getSize();
255406c3fb27SDimitry Andric p->p_memsz = part.armExidx->getSize();
255506c3fb27SDimitry Andric p->p_offset = first->offset + part.armExidx->outSecOff;
255606c3fb27SDimitry Andric p->p_vaddr = first->addr + part.armExidx->outSecOff;
255706c3fb27SDimitry Andric p->p_align = part.armExidx->addralign;
255806c3fb27SDimitry Andric if (part.elfHeader)
255906c3fb27SDimitry Andric p->p_offset -= part.elfHeader->getParent()->offset;
256006c3fb27SDimitry Andric
256106c3fb27SDimitry Andric if (!p->hasLMA)
256206c3fb27SDimitry Andric p->p_paddr = first->getLMA() + part.armExidx->outSecOff;
256306c3fb27SDimitry Andric return;
256406c3fb27SDimitry Andric }
256506c3fb27SDimitry Andric
25660b57cec5SDimitry Andric if (first) {
25670b57cec5SDimitry Andric p->p_filesz = last->offset - first->offset;
25680b57cec5SDimitry Andric if (last->type != SHT_NOBITS)
25690b57cec5SDimitry Andric p->p_filesz += last->size;
25700b57cec5SDimitry Andric
25710b57cec5SDimitry Andric p->p_memsz = last->addr + last->size - first->addr;
25720b57cec5SDimitry Andric p->p_offset = first->offset;
25730b57cec5SDimitry Andric p->p_vaddr = first->addr;
25740b57cec5SDimitry Andric
25750b57cec5SDimitry Andric // File offsets in partitions other than the main partition are relative
25760b57cec5SDimitry Andric // to the offset of the ELF headers. Perform that adjustment now.
25770b57cec5SDimitry Andric if (part.elfHeader)
25780b57cec5SDimitry Andric p->p_offset -= part.elfHeader->getParent()->offset;
25790b57cec5SDimitry Andric
25800b57cec5SDimitry Andric if (!p->hasLMA)
25810b57cec5SDimitry Andric p->p_paddr = first->getLMA();
25820b57cec5SDimitry Andric }
25830b57cec5SDimitry Andric }
25840b57cec5SDimitry Andric }
25850b57cec5SDimitry Andric
25860b57cec5SDimitry Andric // A helper struct for checkSectionOverlap.
25870b57cec5SDimitry Andric namespace {
25880b57cec5SDimitry Andric struct SectionOffset {
25890b57cec5SDimitry Andric OutputSection *sec;
25900b57cec5SDimitry Andric uint64_t offset;
25910b57cec5SDimitry Andric };
25920b57cec5SDimitry Andric } // namespace
25930b57cec5SDimitry Andric
25940b57cec5SDimitry Andric // Check whether sections overlap for a specific address range (file offsets,
2595480093f4SDimitry Andric // load and virtual addresses).
checkOverlap(StringRef name,std::vector<SectionOffset> & sections,bool isVirtualAddr)25960b57cec5SDimitry Andric static void checkOverlap(StringRef name, std::vector<SectionOffset> §ions,
25970b57cec5SDimitry Andric bool isVirtualAddr) {
25980b57cec5SDimitry Andric llvm::sort(sections, [=](const SectionOffset &a, const SectionOffset &b) {
25990b57cec5SDimitry Andric return a.offset < b.offset;
26000b57cec5SDimitry Andric });
26010b57cec5SDimitry Andric
26020b57cec5SDimitry Andric // Finding overlap is easy given a vector is sorted by start position.
26030b57cec5SDimitry Andric // If an element starts before the end of the previous element, they overlap.
26040b57cec5SDimitry Andric for (size_t i = 1, end = sections.size(); i < end; ++i) {
26050b57cec5SDimitry Andric SectionOffset a = sections[i - 1];
26060b57cec5SDimitry Andric SectionOffset b = sections[i];
26070b57cec5SDimitry Andric if (b.offset >= a.offset + a.sec->size)
26080b57cec5SDimitry Andric continue;
26090b57cec5SDimitry Andric
26100b57cec5SDimitry Andric // If both sections are in OVERLAY we allow the overlapping of virtual
26110b57cec5SDimitry Andric // addresses, because it is what OVERLAY was designed for.
26120b57cec5SDimitry Andric if (isVirtualAddr && a.sec->inOverlay && b.sec->inOverlay)
26130b57cec5SDimitry Andric continue;
26140b57cec5SDimitry Andric
26150b57cec5SDimitry Andric errorOrWarn("section " + a.sec->name + " " + name +
26160b57cec5SDimitry Andric " range overlaps with " + b.sec->name + "\n>>> " + a.sec->name +
26170b57cec5SDimitry Andric " range is " + rangeToString(a.offset, a.sec->size) + "\n>>> " +
26180b57cec5SDimitry Andric b.sec->name + " range is " +
26190b57cec5SDimitry Andric rangeToString(b.offset, b.sec->size));
26200b57cec5SDimitry Andric }
26210b57cec5SDimitry Andric }
26220b57cec5SDimitry Andric
26230b57cec5SDimitry Andric // Check for overlapping sections and address overflows.
26240b57cec5SDimitry Andric //
26250b57cec5SDimitry Andric // In this function we check that none of the output sections have overlapping
26260b57cec5SDimitry Andric // file offsets. For SHF_ALLOC sections we also check that the load address
26270b57cec5SDimitry Andric // ranges and the virtual address ranges don't overlap
checkSections()26280b57cec5SDimitry Andric template <class ELFT> void Writer<ELFT>::checkSections() {
26290b57cec5SDimitry Andric // First, check that section's VAs fit in available address space for target.
26300b57cec5SDimitry Andric for (OutputSection *os : outputSections)
26310b57cec5SDimitry Andric if ((os->addr + os->size < os->addr) ||
2632bdd1243dSDimitry Andric (!ELFT::Is64Bits && os->addr + os->size > uint64_t(UINT32_MAX) + 1))
26330b57cec5SDimitry Andric errorOrWarn("section " + os->name + " at 0x" + utohexstr(os->addr) +
26340b57cec5SDimitry Andric " of size 0x" + utohexstr(os->size) +
26350b57cec5SDimitry Andric " exceeds available address space");
26360b57cec5SDimitry Andric
26370b57cec5SDimitry Andric // Check for overlapping file offsets. In this case we need to skip any
26380b57cec5SDimitry Andric // section marked as SHT_NOBITS. These sections don't actually occupy space in
26390b57cec5SDimitry Andric // the file so Sec->Offset + Sec->Size can overlap with others. If --oformat
26400b57cec5SDimitry Andric // binary is specified only add SHF_ALLOC sections are added to the output
26410b57cec5SDimitry Andric // file so we skip any non-allocated sections in that case.
26420b57cec5SDimitry Andric std::vector<SectionOffset> fileOffs;
26430b57cec5SDimitry Andric for (OutputSection *sec : outputSections)
26440b57cec5SDimitry Andric if (sec->size > 0 && sec->type != SHT_NOBITS &&
26450b57cec5SDimitry Andric (!config->oFormatBinary || (sec->flags & SHF_ALLOC)))
26460b57cec5SDimitry Andric fileOffs.push_back({sec, sec->offset});
26470b57cec5SDimitry Andric checkOverlap("file", fileOffs, false);
26480b57cec5SDimitry Andric
26490b57cec5SDimitry Andric // When linking with -r there is no need to check for overlapping virtual/load
26500b57cec5SDimitry Andric // addresses since those addresses will only be assigned when the final
26510b57cec5SDimitry Andric // executable/shared object is created.
26520b57cec5SDimitry Andric if (config->relocatable)
26530b57cec5SDimitry Andric return;
26540b57cec5SDimitry Andric
26550b57cec5SDimitry Andric // Checking for overlapping virtual and load addresses only needs to take
26560b57cec5SDimitry Andric // into account SHF_ALLOC sections since others will not be loaded.
26570b57cec5SDimitry Andric // Furthermore, we also need to skip SHF_TLS sections since these will be
26580b57cec5SDimitry Andric // mapped to other addresses at runtime and can therefore have overlapping
26590b57cec5SDimitry Andric // ranges in the file.
26600b57cec5SDimitry Andric std::vector<SectionOffset> vmas;
26610b57cec5SDimitry Andric for (OutputSection *sec : outputSections)
26620b57cec5SDimitry Andric if (sec->size > 0 && (sec->flags & SHF_ALLOC) && !(sec->flags & SHF_TLS))
26630b57cec5SDimitry Andric vmas.push_back({sec, sec->addr});
26640b57cec5SDimitry Andric checkOverlap("virtual address", vmas, true);
26650b57cec5SDimitry Andric
26660b57cec5SDimitry Andric // Finally, check that the load addresses don't overlap. This will usually be
26670b57cec5SDimitry Andric // the same as the virtual addresses but can be different when using a linker
26680b57cec5SDimitry Andric // script with AT().
26690b57cec5SDimitry Andric std::vector<SectionOffset> lmas;
26700b57cec5SDimitry Andric for (OutputSection *sec : outputSections)
26710b57cec5SDimitry Andric if (sec->size > 0 && (sec->flags & SHF_ALLOC) && !(sec->flags & SHF_TLS))
26720b57cec5SDimitry Andric lmas.push_back({sec, sec->getLMA()});
26730b57cec5SDimitry Andric checkOverlap("load address", lmas, false);
26740b57cec5SDimitry Andric }
26750b57cec5SDimitry Andric
26760b57cec5SDimitry Andric // The entry point address is chosen in the following ways.
26770b57cec5SDimitry Andric //
26780b57cec5SDimitry Andric // 1. the '-e' entry command-line option;
26790b57cec5SDimitry Andric // 2. the ENTRY(symbol) command in a linker control script;
26800b57cec5SDimitry Andric // 3. the value of the symbol _start, if present;
26810b57cec5SDimitry Andric // 4. the number represented by the entry symbol, if it is a number;
2682349cc55cSDimitry Andric // 5. the address 0.
getEntryAddr()26830b57cec5SDimitry Andric static uint64_t getEntryAddr() {
26840b57cec5SDimitry Andric // Case 1, 2 or 3
2685bdd1243dSDimitry Andric if (Symbol *b = symtab.find(config->entry))
26860b57cec5SDimitry Andric return b->getVA();
26870b57cec5SDimitry Andric
26880b57cec5SDimitry Andric // Case 4
26890b57cec5SDimitry Andric uint64_t addr;
26900b57cec5SDimitry Andric if (to_integer(config->entry, addr))
26910b57cec5SDimitry Andric return addr;
26920b57cec5SDimitry Andric
26930b57cec5SDimitry Andric // Case 5
26940b57cec5SDimitry Andric if (config->warnMissingEntry)
26950b57cec5SDimitry Andric warn("cannot find entry symbol " + config->entry +
26960b57cec5SDimitry Andric "; not setting start address");
26970b57cec5SDimitry Andric return 0;
26980b57cec5SDimitry Andric }
26990b57cec5SDimitry Andric
getELFType()27000b57cec5SDimitry Andric static uint16_t getELFType() {
27010b57cec5SDimitry Andric if (config->isPic)
27020b57cec5SDimitry Andric return ET_DYN;
27030b57cec5SDimitry Andric if (config->relocatable)
27040b57cec5SDimitry Andric return ET_REL;
27050b57cec5SDimitry Andric return ET_EXEC;
27060b57cec5SDimitry Andric }
27070b57cec5SDimitry Andric
writeHeader()27080b57cec5SDimitry Andric template <class ELFT> void Writer<ELFT>::writeHeader() {
27090b57cec5SDimitry Andric writeEhdr<ELFT>(Out::bufferStart, *mainPart);
27100b57cec5SDimitry Andric writePhdrs<ELFT>(Out::bufferStart + sizeof(Elf_Ehdr), *mainPart);
27110b57cec5SDimitry Andric
27120b57cec5SDimitry Andric auto *eHdr = reinterpret_cast<Elf_Ehdr *>(Out::bufferStart);
27130b57cec5SDimitry Andric eHdr->e_type = getELFType();
27140b57cec5SDimitry Andric eHdr->e_entry = getEntryAddr();
27150b57cec5SDimitry Andric eHdr->e_shoff = sectionHeaderOff;
27160b57cec5SDimitry Andric
27170b57cec5SDimitry Andric // Write the section header table.
27180b57cec5SDimitry Andric //
27190b57cec5SDimitry Andric // The ELF header can only store numbers up to SHN_LORESERVE in the e_shnum
27200b57cec5SDimitry Andric // and e_shstrndx fields. When the value of one of these fields exceeds
27210b57cec5SDimitry Andric // SHN_LORESERVE ELF requires us to put sentinel values in the ELF header and
27220b57cec5SDimitry Andric // use fields in the section header at index 0 to store
27230b57cec5SDimitry Andric // the value. The sentinel values and fields are:
27240b57cec5SDimitry Andric // e_shnum = 0, SHdrs[0].sh_size = number of sections.
27250b57cec5SDimitry Andric // e_shstrndx = SHN_XINDEX, SHdrs[0].sh_link = .shstrtab section index.
27260b57cec5SDimitry Andric auto *sHdrs = reinterpret_cast<Elf_Shdr *>(Out::bufferStart + eHdr->e_shoff);
27270b57cec5SDimitry Andric size_t num = outputSections.size() + 1;
27280b57cec5SDimitry Andric if (num >= SHN_LORESERVE)
27290b57cec5SDimitry Andric sHdrs->sh_size = num;
27300b57cec5SDimitry Andric else
27310b57cec5SDimitry Andric eHdr->e_shnum = num;
27320b57cec5SDimitry Andric
27330b57cec5SDimitry Andric uint32_t strTabIndex = in.shStrTab->getParent()->sectionIndex;
27340b57cec5SDimitry Andric if (strTabIndex >= SHN_LORESERVE) {
27350b57cec5SDimitry Andric sHdrs->sh_link = strTabIndex;
27360b57cec5SDimitry Andric eHdr->e_shstrndx = SHN_XINDEX;
27370b57cec5SDimitry Andric } else {
27380b57cec5SDimitry Andric eHdr->e_shstrndx = strTabIndex;
27390b57cec5SDimitry Andric }
27400b57cec5SDimitry Andric
27410b57cec5SDimitry Andric for (OutputSection *sec : outputSections)
27420b57cec5SDimitry Andric sec->writeHeaderTo<ELFT>(++sHdrs);
27430b57cec5SDimitry Andric }
27440b57cec5SDimitry Andric
27450b57cec5SDimitry Andric // Open a result file.
openFile()27460b57cec5SDimitry Andric template <class ELFT> void Writer<ELFT>::openFile() {
27470b57cec5SDimitry Andric uint64_t maxSize = config->is64 ? INT64_MAX : UINT32_MAX;
27480b57cec5SDimitry Andric if (fileSize != size_t(fileSize) || maxSize < fileSize) {
2749e8d8bef9SDimitry Andric std::string msg;
2750e8d8bef9SDimitry Andric raw_string_ostream s(msg);
2751e8d8bef9SDimitry Andric s << "output file too large: " << Twine(fileSize) << " bytes\n"
2752e8d8bef9SDimitry Andric << "section sizes:\n";
2753e8d8bef9SDimitry Andric for (OutputSection *os : outputSections)
2754e8d8bef9SDimitry Andric s << os->name << ' ' << os->size << "\n";
2755e8d8bef9SDimitry Andric error(s.str());
27560b57cec5SDimitry Andric return;
27570b57cec5SDimitry Andric }
27580b57cec5SDimitry Andric
27590b57cec5SDimitry Andric unlinkAsync(config->outputFile);
27600b57cec5SDimitry Andric unsigned flags = 0;
27610b57cec5SDimitry Andric if (!config->relocatable)
2762480093f4SDimitry Andric flags |= FileOutputBuffer::F_executable;
2763480093f4SDimitry Andric if (!config->mmapOutputFile)
2764480093f4SDimitry Andric flags |= FileOutputBuffer::F_no_mmap;
27650b57cec5SDimitry Andric Expected<std::unique_ptr<FileOutputBuffer>> bufferOrErr =
27660b57cec5SDimitry Andric FileOutputBuffer::create(config->outputFile, fileSize, flags);
27670b57cec5SDimitry Andric
27680b57cec5SDimitry Andric if (!bufferOrErr) {
27690b57cec5SDimitry Andric error("failed to open " + config->outputFile + ": " +
27700b57cec5SDimitry Andric llvm::toString(bufferOrErr.takeError()));
27710b57cec5SDimitry Andric return;
27720b57cec5SDimitry Andric }
27730b57cec5SDimitry Andric buffer = std::move(*bufferOrErr);
27740b57cec5SDimitry Andric Out::bufferStart = buffer->getBufferStart();
27750b57cec5SDimitry Andric }
27760b57cec5SDimitry Andric
writeSectionsBinary()27770b57cec5SDimitry Andric template <class ELFT> void Writer<ELFT>::writeSectionsBinary() {
2778bdd1243dSDimitry Andric parallel::TaskGroup tg;
27790b57cec5SDimitry Andric for (OutputSection *sec : outputSections)
27800b57cec5SDimitry Andric if (sec->flags & SHF_ALLOC)
2781bdd1243dSDimitry Andric sec->writeTo<ELFT>(Out::bufferStart + sec->offset, tg);
27820b57cec5SDimitry Andric }
27830b57cec5SDimitry Andric
fillTrap(uint8_t * i,uint8_t * end)27840b57cec5SDimitry Andric static void fillTrap(uint8_t *i, uint8_t *end) {
27850b57cec5SDimitry Andric for (; i + 4 <= end; i += 4)
27860b57cec5SDimitry Andric memcpy(i, &target->trapInstr, 4);
27870b57cec5SDimitry Andric }
27880b57cec5SDimitry Andric
27890b57cec5SDimitry Andric // Fill the last page of executable segments with trap instructions
27900b57cec5SDimitry Andric // instead of leaving them as zero. Even though it is not required by any
27910b57cec5SDimitry Andric // standard, it is in general a good thing to do for security reasons.
27920b57cec5SDimitry Andric //
27930b57cec5SDimitry Andric // We'll leave other pages in segments as-is because the rest will be
27940b57cec5SDimitry Andric // overwritten by output sections.
writeTrapInstr()27950b57cec5SDimitry Andric template <class ELFT> void Writer<ELFT>::writeTrapInstr() {
27960b57cec5SDimitry Andric for (Partition &part : partitions) {
27970b57cec5SDimitry Andric // Fill the last page.
27980b57cec5SDimitry Andric for (PhdrEntry *p : part.phdrs)
27990b57cec5SDimitry Andric if (p->p_type == PT_LOAD && (p->p_flags & PF_X))
280004eeddc0SDimitry Andric fillTrap(Out::bufferStart +
280104eeddc0SDimitry Andric alignDown(p->firstSec->offset + p->p_filesz, 4),
2802972a253aSDimitry Andric Out::bufferStart +
2803972a253aSDimitry Andric alignToPowerOf2(p->firstSec->offset + p->p_filesz,
28044824e7fdSDimitry Andric config->maxPageSize));
28050b57cec5SDimitry Andric
28060b57cec5SDimitry Andric // Round up the file size of the last segment to the page boundary iff it is
28070b57cec5SDimitry Andric // an executable segment to ensure that other tools don't accidentally
28080b57cec5SDimitry Andric // trim the instruction padding (e.g. when stripping the file).
28090b57cec5SDimitry Andric PhdrEntry *last = nullptr;
28100b57cec5SDimitry Andric for (PhdrEntry *p : part.phdrs)
28110b57cec5SDimitry Andric if (p->p_type == PT_LOAD)
28120b57cec5SDimitry Andric last = p;
28130b57cec5SDimitry Andric
28140b57cec5SDimitry Andric if (last && (last->p_flags & PF_X))
28150b57cec5SDimitry Andric last->p_memsz = last->p_filesz =
2816972a253aSDimitry Andric alignToPowerOf2(last->p_filesz, config->maxPageSize);
28170b57cec5SDimitry Andric }
28180b57cec5SDimitry Andric }
28190b57cec5SDimitry Andric
28200b57cec5SDimitry Andric // Write section contents to a mmap'ed file.
writeSections()28210b57cec5SDimitry Andric template <class ELFT> void Writer<ELFT>::writeSections() {
28220eae32dcSDimitry Andric llvm::TimeTraceScope timeScope("Write sections");
28230eae32dcSDimitry Andric
2824bdd1243dSDimitry Andric {
2825349cc55cSDimitry Andric // In -r or --emit-relocs mode, write the relocation sections first as in
28260b57cec5SDimitry Andric // ELf_Rel targets we might find out that we need to modify the relocated
28270b57cec5SDimitry Andric // section while doing it.
2828bdd1243dSDimitry Andric parallel::TaskGroup tg;
28290b57cec5SDimitry Andric for (OutputSection *sec : outputSections)
28300fca6ea1SDimitry Andric if (isStaticRelSecType(sec->type))
2831bdd1243dSDimitry Andric sec->writeTo<ELFT>(Out::bufferStart + sec->offset, tg);
2832bdd1243dSDimitry Andric }
2833bdd1243dSDimitry Andric {
2834bdd1243dSDimitry Andric parallel::TaskGroup tg;
28350b57cec5SDimitry Andric for (OutputSection *sec : outputSections)
28360fca6ea1SDimitry Andric if (!isStaticRelSecType(sec->type))
2837bdd1243dSDimitry Andric sec->writeTo<ELFT>(Out::bufferStart + sec->offset, tg);
2838bdd1243dSDimitry Andric }
28390b57cec5SDimitry Andric
2840fe6060f1SDimitry Andric // Finally, check that all dynamic relocation addends were written correctly.
2841fe6060f1SDimitry Andric if (config->checkDynamicRelocs && config->writeAddends) {
2842fe6060f1SDimitry Andric for (OutputSection *sec : outputSections)
28430fca6ea1SDimitry Andric if (isStaticRelSecType(sec->type))
2844fe6060f1SDimitry Andric sec->checkDynRelAddends(Out::bufferStart);
28450b57cec5SDimitry Andric }
28460b57cec5SDimitry Andric }
28470b57cec5SDimitry Andric
28480b57cec5SDimitry Andric // Computes a hash value of Data using a given hash function.
28490b57cec5SDimitry Andric // In order to utilize multiple cores, we first split data into 1MB
28500b57cec5SDimitry Andric // chunks, compute a hash for each chunk, and then compute a hash value
28510b57cec5SDimitry Andric // of the hash values.
28520b57cec5SDimitry Andric static void
computeHash(llvm::MutableArrayRef<uint8_t> hashBuf,llvm::ArrayRef<uint8_t> data,std::function<void (uint8_t * dest,ArrayRef<uint8_t> arr)> hashFn)28530b57cec5SDimitry Andric computeHash(llvm::MutableArrayRef<uint8_t> hashBuf,
28540b57cec5SDimitry Andric llvm::ArrayRef<uint8_t> data,
28550b57cec5SDimitry Andric std::function<void(uint8_t *dest, ArrayRef<uint8_t> arr)> hashFn) {
28560b57cec5SDimitry Andric std::vector<ArrayRef<uint8_t>> chunks = split(data, 1024 * 1024);
285704eeddc0SDimitry Andric const size_t hashesSize = chunks.size() * hashBuf.size();
285804eeddc0SDimitry Andric std::unique_ptr<uint8_t[]> hashes(new uint8_t[hashesSize]);
28590b57cec5SDimitry Andric
28600b57cec5SDimitry Andric // Compute hash values.
286181ad6265SDimitry Andric parallelFor(0, chunks.size(), [&](size_t i) {
286204eeddc0SDimitry Andric hashFn(hashes.get() + i * hashBuf.size(), chunks[i]);
28630b57cec5SDimitry Andric });
28640b57cec5SDimitry Andric
28650b57cec5SDimitry Andric // Write to the final output buffer.
2866bdd1243dSDimitry Andric hashFn(hashBuf.data(), ArrayRef(hashes.get(), hashesSize));
28670b57cec5SDimitry Andric }
28680b57cec5SDimitry Andric
writeBuildId()28690b57cec5SDimitry Andric template <class ELFT> void Writer<ELFT>::writeBuildId() {
28700b57cec5SDimitry Andric if (!mainPart->buildId || !mainPart->buildId->getParent())
28710b57cec5SDimitry Andric return;
28720b57cec5SDimitry Andric
28730b57cec5SDimitry Andric if (config->buildId == BuildIdKind::Hexstring) {
28740b57cec5SDimitry Andric for (Partition &part : partitions)
28750b57cec5SDimitry Andric part.buildId->writeBuildId(config->buildIdVector);
28760b57cec5SDimitry Andric return;
28770b57cec5SDimitry Andric }
28780b57cec5SDimitry Andric
28790b57cec5SDimitry Andric // Compute a hash of all sections of the output file.
28800b57cec5SDimitry Andric size_t hashSize = mainPart->buildId->hashSize;
288104eeddc0SDimitry Andric std::unique_ptr<uint8_t[]> buildId(new uint8_t[hashSize]);
288204eeddc0SDimitry Andric MutableArrayRef<uint8_t> output(buildId.get(), hashSize);
288304eeddc0SDimitry Andric llvm::ArrayRef<uint8_t> input{Out::bufferStart, size_t(fileSize)};
28840b57cec5SDimitry Andric
288581ad6265SDimitry Andric // Fedora introduced build ID as "approximation of true uniqueness across all
288681ad6265SDimitry Andric // binaries that might be used by overlapping sets of people". It does not
288781ad6265SDimitry Andric // need some security goals that some hash algorithms strive to provide, e.g.
288881ad6265SDimitry Andric // (second-)preimage and collision resistance. In practice people use 'md5'
288981ad6265SDimitry Andric // and 'sha1' just for different lengths. Implement them with the more
289081ad6265SDimitry Andric // efficient BLAKE3.
28910b57cec5SDimitry Andric switch (config->buildId) {
28920b57cec5SDimitry Andric case BuildIdKind::Fast:
289304eeddc0SDimitry Andric computeHash(output, input, [](uint8_t *dest, ArrayRef<uint8_t> arr) {
289406c3fb27SDimitry Andric write64le(dest, xxh3_64bits(arr));
28950b57cec5SDimitry Andric });
28960b57cec5SDimitry Andric break;
28970b57cec5SDimitry Andric case BuildIdKind::Md5:
289804eeddc0SDimitry Andric computeHash(output, input, [&](uint8_t *dest, ArrayRef<uint8_t> arr) {
289981ad6265SDimitry Andric memcpy(dest, BLAKE3::hash<16>(arr).data(), hashSize);
29000b57cec5SDimitry Andric });
29010b57cec5SDimitry Andric break;
29020b57cec5SDimitry Andric case BuildIdKind::Sha1:
290304eeddc0SDimitry Andric computeHash(output, input, [&](uint8_t *dest, ArrayRef<uint8_t> arr) {
290481ad6265SDimitry Andric memcpy(dest, BLAKE3::hash<20>(arr).data(), hashSize);
29050b57cec5SDimitry Andric });
29060b57cec5SDimitry Andric break;
29070b57cec5SDimitry Andric case BuildIdKind::Uuid:
290804eeddc0SDimitry Andric if (auto ec = llvm::getRandomBytes(buildId.get(), hashSize))
29090b57cec5SDimitry Andric error("entropy source failure: " + ec.message());
29100b57cec5SDimitry Andric break;
29110b57cec5SDimitry Andric default:
29120b57cec5SDimitry Andric llvm_unreachable("unknown BuildIdKind");
29130b57cec5SDimitry Andric }
29140b57cec5SDimitry Andric for (Partition &part : partitions)
291504eeddc0SDimitry Andric part.buildId->writeBuildId(output);
29160b57cec5SDimitry Andric }
29170b57cec5SDimitry Andric
29185ffd83dbSDimitry Andric template void elf::writeResult<ELF32LE>();
29195ffd83dbSDimitry Andric template void elf::writeResult<ELF32BE>();
29205ffd83dbSDimitry Andric template void elf::writeResult<ELF64LE>();
29215ffd83dbSDimitry Andric template void elf::writeResult<ELF64BE>();
2922