xref: /freebsd/contrib/llvm-project/lld/ELF/Writer.cpp (revision b3edf4467982447620505a28fc82e38a414c07dc)
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"
270b57cec5SDimitry Andric #include "llvm/ADT/StringMap.h"
2881ad6265SDimitry Andric #include "llvm/Support/BLAKE3.h"
295ffd83dbSDimitry Andric #include "llvm/Support/Parallel.h"
300b57cec5SDimitry Andric #include "llvm/Support/RandomNumberGenerator.h"
315ffd83dbSDimitry Andric #include "llvm/Support/TimeProfiler.h"
320b57cec5SDimitry Andric #include "llvm/Support/xxhash.h"
330b57cec5SDimitry Andric #include <climits>
340b57cec5SDimitry Andric 
355ffd83dbSDimitry Andric #define DEBUG_TYPE "lld"
365ffd83dbSDimitry Andric 
370b57cec5SDimitry Andric using namespace llvm;
380b57cec5SDimitry Andric using namespace llvm::ELF;
390b57cec5SDimitry Andric using namespace llvm::object;
400b57cec5SDimitry Andric using namespace llvm::support;
410b57cec5SDimitry Andric using namespace llvm::support::endian;
425ffd83dbSDimitry Andric using namespace lld;
435ffd83dbSDimitry Andric using namespace lld::elf;
440b57cec5SDimitry Andric 
450b57cec5SDimitry Andric namespace {
460b57cec5SDimitry Andric // The writer writes a SymbolTable result to a file.
470b57cec5SDimitry Andric template <class ELFT> class Writer {
480b57cec5SDimitry Andric public:
49e8d8bef9SDimitry Andric   LLVM_ELF_IMPORT_TYPES_ELFT(ELFT)
50e8d8bef9SDimitry Andric 
510b57cec5SDimitry Andric   Writer() : buffer(errorHandler().outputBuffer) {}
520b57cec5SDimitry Andric 
530b57cec5SDimitry Andric   void run();
540b57cec5SDimitry Andric 
550b57cec5SDimitry Andric private:
560b57cec5SDimitry Andric   void addSectionSymbols();
570b57cec5SDimitry Andric   void sortSections();
580b57cec5SDimitry Andric   void resolveShfLinkOrder();
590b57cec5SDimitry Andric   void finalizeAddressDependentContent();
605ffd83dbSDimitry Andric   void optimizeBasicBlockJumps();
610b57cec5SDimitry Andric   void sortInputSections();
6206c3fb27SDimitry Andric   void sortOrphanSections();
630b57cec5SDimitry Andric   void finalizeSections();
640b57cec5SDimitry Andric   void checkExecuteOnly();
650b57cec5SDimitry Andric   void setReservedSymbolSections();
660b57cec5SDimitry Andric 
6704eeddc0SDimitry Andric   SmallVector<PhdrEntry *, 0> createPhdrs(Partition &part);
680b57cec5SDimitry Andric   void addPhdrForSection(Partition &part, unsigned shType, unsigned pType,
690b57cec5SDimitry Andric                          unsigned pFlags);
700b57cec5SDimitry Andric   void assignFileOffsets();
710b57cec5SDimitry Andric   void assignFileOffsetsBinary();
720b57cec5SDimitry Andric   void setPhdrs(Partition &part);
730b57cec5SDimitry Andric   void checkSections();
740b57cec5SDimitry Andric   void fixSectionAlignments();
750b57cec5SDimitry Andric   void openFile();
760b57cec5SDimitry Andric   void writeTrapInstr();
770b57cec5SDimitry Andric   void writeHeader();
780b57cec5SDimitry Andric   void writeSections();
790b57cec5SDimitry Andric   void writeSectionsBinary();
800b57cec5SDimitry Andric   void writeBuildId();
810b57cec5SDimitry Andric 
820b57cec5SDimitry Andric   std::unique_ptr<FileOutputBuffer> &buffer;
830b57cec5SDimitry Andric 
840b57cec5SDimitry Andric   void addRelIpltSymbols();
850b57cec5SDimitry Andric   void addStartEndSymbols();
8681ad6265SDimitry Andric   void addStartStopSymbols(OutputSection &osec);
870b57cec5SDimitry Andric 
880b57cec5SDimitry Andric   uint64_t fileSize;
890b57cec5SDimitry Andric   uint64_t sectionHeaderOff;
900b57cec5SDimitry Andric };
910b57cec5SDimitry Andric } // anonymous namespace
920b57cec5SDimitry Andric 
930b57cec5SDimitry Andric static bool needsInterpSection() {
9455e4f9d5SDimitry Andric   return !config->relocatable && !config->shared &&
9555e4f9d5SDimitry Andric          !config->dynamicLinker.empty() && script->needsInterpSection();
960b57cec5SDimitry Andric }
970b57cec5SDimitry Andric 
985ffd83dbSDimitry Andric template <class ELFT> void elf::writeResult() {
995ffd83dbSDimitry Andric   Writer<ELFT>().run();
1000b57cec5SDimitry Andric }
1010b57cec5SDimitry Andric 
10204eeddc0SDimitry Andric static void removeEmptyPTLoad(SmallVector<PhdrEntry *, 0> &phdrs) {
1035ffd83dbSDimitry Andric   auto it = std::stable_partition(
1045ffd83dbSDimitry Andric       phdrs.begin(), phdrs.end(), [&](const PhdrEntry *p) {
1055ffd83dbSDimitry Andric         if (p->p_type != PT_LOAD)
1065ffd83dbSDimitry Andric           return true;
1075ffd83dbSDimitry Andric         if (!p->firstSec)
1085ffd83dbSDimitry Andric           return false;
1095ffd83dbSDimitry Andric         uint64_t size = p->lastSec->addr + p->lastSec->size - p->firstSec->addr;
1105ffd83dbSDimitry Andric         return size != 0;
1115ffd83dbSDimitry Andric       });
1125ffd83dbSDimitry Andric 
1135ffd83dbSDimitry Andric   // Clear OutputSection::ptLoad for sections contained in removed
1145ffd83dbSDimitry Andric   // segments.
1155ffd83dbSDimitry Andric   DenseSet<PhdrEntry *> removed(it, phdrs.end());
1165ffd83dbSDimitry Andric   for (OutputSection *sec : outputSections)
1175ffd83dbSDimitry Andric     if (removed.count(sec->ptLoad))
1185ffd83dbSDimitry Andric       sec->ptLoad = nullptr;
1195ffd83dbSDimitry Andric   phdrs.erase(it, phdrs.end());
1205ffd83dbSDimitry Andric }
1215ffd83dbSDimitry Andric 
1225ffd83dbSDimitry Andric void elf::copySectionsIntoPartitions() {
12304eeddc0SDimitry Andric   SmallVector<InputSectionBase *, 0> newSections;
124bdd1243dSDimitry Andric   const size_t ehSize = ctx.ehInputSections.size();
1250b57cec5SDimitry Andric   for (unsigned part = 2; part != partitions.size() + 1; ++part) {
126bdd1243dSDimitry Andric     for (InputSectionBase *s : ctx.inputSections) {
127bdd1243dSDimitry Andric       if (!(s->flags & SHF_ALLOC) || !s->isLive() || s->type != SHT_NOTE)
1280b57cec5SDimitry Andric         continue;
129bdd1243dSDimitry Andric       auto *copy = make<InputSection>(cast<InputSection>(*s));
1300b57cec5SDimitry Andric       copy->partition = part;
1310b57cec5SDimitry Andric       newSections.push_back(copy);
1320b57cec5SDimitry Andric     }
133bdd1243dSDimitry Andric     for (size_t i = 0; i != ehSize; ++i) {
134bdd1243dSDimitry Andric       assert(ctx.ehInputSections[i]->isLive());
135bdd1243dSDimitry Andric       auto *copy = make<EhInputSection>(*ctx.ehInputSections[i]);
136bdd1243dSDimitry Andric       copy->partition = part;
137bdd1243dSDimitry Andric       ctx.ehInputSections.push_back(copy);
138bdd1243dSDimitry Andric     }
1390b57cec5SDimitry Andric   }
1400b57cec5SDimitry Andric 
141bdd1243dSDimitry Andric   ctx.inputSections.insert(ctx.inputSections.end(), newSections.begin(),
1420b57cec5SDimitry Andric                            newSections.end());
1430b57cec5SDimitry Andric }
1440b57cec5SDimitry Andric 
1450b57cec5SDimitry Andric static Defined *addOptionalRegular(StringRef name, SectionBase *sec,
146349cc55cSDimitry Andric                                    uint64_t val, uint8_t stOther = STV_HIDDEN) {
147bdd1243dSDimitry Andric   Symbol *s = symtab.find(name);
14881ad6265SDimitry Andric   if (!s || s->isDefined() || s->isCommon())
1490b57cec5SDimitry Andric     return nullptr;
1500b57cec5SDimitry Andric 
1517a6dacacSDimitry Andric   s->resolve(Defined{ctx.internalFile, StringRef(), STB_GLOBAL, stOther,
1527a6dacacSDimitry Andric                      STT_NOTYPE, val,
1530b57cec5SDimitry Andric                      /*size=*/0, sec});
15481ad6265SDimitry Andric   s->isUsedInRegularObj = true;
1550b57cec5SDimitry Andric   return cast<Defined>(s);
1560b57cec5SDimitry Andric }
1570b57cec5SDimitry Andric 
1580b57cec5SDimitry Andric // The linker is expected to define some symbols depending on
1590b57cec5SDimitry Andric // the linking result. This function defines such symbols.
1605ffd83dbSDimitry Andric void elf::addReservedSymbols() {
1610b57cec5SDimitry Andric   if (config->emachine == EM_MIPS) {
1627a6dacacSDimitry Andric     auto addAbsolute = [](StringRef name) {
1637a6dacacSDimitry Andric       Symbol *sym =
1647a6dacacSDimitry Andric           symtab.addSymbol(Defined{ctx.internalFile, name, STB_GLOBAL,
1657a6dacacSDimitry Andric                                    STV_HIDDEN, STT_NOTYPE, 0, 0, nullptr});
1667a6dacacSDimitry Andric       sym->isUsedInRegularObj = true;
1677a6dacacSDimitry Andric       return cast<Defined>(sym);
1687a6dacacSDimitry Andric     };
1690b57cec5SDimitry Andric     // Define _gp for MIPS. st_value of _gp symbol will be updated by Writer
1700b57cec5SDimitry Andric     // so that it points to an absolute address which by default is relative
1710b57cec5SDimitry Andric     // to GOT. Default offset is 0x7ff0.
1720b57cec5SDimitry Andric     // See "Global Data Symbols" in Chapter 6 in the following document:
1730b57cec5SDimitry Andric     // ftp://www.linux-mips.org/pub/linux/mips/doc/ABI/mipsabi.pdf
1740b57cec5SDimitry Andric     ElfSym::mipsGp = addAbsolute("_gp");
1750b57cec5SDimitry Andric 
1760b57cec5SDimitry Andric     // On MIPS O32 ABI, _gp_disp is a magic symbol designates offset between
1770b57cec5SDimitry Andric     // start of function and 'gp' pointer into GOT.
178bdd1243dSDimitry Andric     if (symtab.find("_gp_disp"))
1790b57cec5SDimitry Andric       ElfSym::mipsGpDisp = addAbsolute("_gp_disp");
1800b57cec5SDimitry Andric 
1810b57cec5SDimitry Andric     // The __gnu_local_gp is a magic symbol equal to the current value of 'gp'
1820b57cec5SDimitry Andric     // pointer. This symbol is used in the code generated by .cpload pseudo-op
1830b57cec5SDimitry Andric     // in case of using -mno-shared option.
1840b57cec5SDimitry Andric     // https://sourceware.org/ml/binutils/2004-12/msg00094.html
185bdd1243dSDimitry Andric     if (symtab.find("__gnu_local_gp"))
1860b57cec5SDimitry Andric       ElfSym::mipsLocalGp = addAbsolute("__gnu_local_gp");
1870b57cec5SDimitry Andric   } else if (config->emachine == EM_PPC) {
1880b57cec5SDimitry Andric     // glibc *crt1.o has a undefined reference to _SDA_BASE_. Since we don't
1890b57cec5SDimitry Andric     // support Small Data Area, define it arbitrarily as 0.
1900b57cec5SDimitry Andric     addOptionalRegular("_SDA_BASE_", nullptr, 0, STV_HIDDEN);
1915ffd83dbSDimitry Andric   } else if (config->emachine == EM_PPC64) {
1925ffd83dbSDimitry Andric     addPPC64SaveRestore();
1930b57cec5SDimitry Andric   }
1940b57cec5SDimitry Andric 
1950b57cec5SDimitry Andric   // The Power Architecture 64-bit v2 ABI defines a TableOfContents (TOC) which
1960b57cec5SDimitry Andric   // combines the typical ELF GOT with the small data sections. It commonly
1970b57cec5SDimitry Andric   // includes .got .toc .sdata .sbss. The .TOC. symbol replaces both
1980b57cec5SDimitry Andric   // _GLOBAL_OFFSET_TABLE_ and _SDA_BASE_ from the 32-bit ABI. It is used to
1990b57cec5SDimitry Andric   // represent the TOC base which is offset by 0x8000 bytes from the start of
2000b57cec5SDimitry Andric   // the .got section.
2010b57cec5SDimitry Andric   // We do not allow _GLOBAL_OFFSET_TABLE_ to be defined by input objects as the
2020b57cec5SDimitry Andric   // correctness of some relocations depends on its value.
2030b57cec5SDimitry Andric   StringRef gotSymName =
2040b57cec5SDimitry Andric       (config->emachine == EM_PPC64) ? ".TOC." : "_GLOBAL_OFFSET_TABLE_";
2050b57cec5SDimitry Andric 
206bdd1243dSDimitry Andric   if (Symbol *s = symtab.find(gotSymName)) {
2070b57cec5SDimitry Andric     if (s->isDefined()) {
2080b57cec5SDimitry Andric       error(toString(s->file) + " cannot redefine linker defined symbol '" +
2090b57cec5SDimitry Andric             gotSymName + "'");
2100b57cec5SDimitry Andric       return;
2110b57cec5SDimitry Andric     }
2120b57cec5SDimitry Andric 
2130b57cec5SDimitry Andric     uint64_t gotOff = 0;
2140b57cec5SDimitry Andric     if (config->emachine == EM_PPC64)
2150b57cec5SDimitry Andric       gotOff = 0x8000;
2160b57cec5SDimitry Andric 
2177a6dacacSDimitry Andric     s->resolve(Defined{ctx.internalFile, StringRef(), STB_GLOBAL, STV_HIDDEN,
2180b57cec5SDimitry Andric                        STT_NOTYPE, gotOff, /*size=*/0, Out::elfHeader});
2190b57cec5SDimitry Andric     ElfSym::globalOffsetTable = cast<Defined>(s);
2200b57cec5SDimitry Andric   }
2210b57cec5SDimitry Andric 
2220b57cec5SDimitry Andric   // __ehdr_start is the location of ELF file headers. Note that we define
2230b57cec5SDimitry Andric   // this symbol unconditionally even when using a linker script, which
2240b57cec5SDimitry Andric   // differs from the behavior implemented by GNU linker which only define
2250b57cec5SDimitry Andric   // this symbol if ELF headers are in the memory mapped segment.
2260b57cec5SDimitry Andric   addOptionalRegular("__ehdr_start", Out::elfHeader, 0, STV_HIDDEN);
2270b57cec5SDimitry Andric 
2280b57cec5SDimitry Andric   // __executable_start is not documented, but the expectation of at
2290b57cec5SDimitry Andric   // least the Android libc is that it points to the ELF header.
2300b57cec5SDimitry Andric   addOptionalRegular("__executable_start", Out::elfHeader, 0, STV_HIDDEN);
2310b57cec5SDimitry Andric 
2320b57cec5SDimitry Andric   // __dso_handle symbol is passed to cxa_finalize as a marker to identify
2330b57cec5SDimitry Andric   // each DSO. The address of the symbol doesn't matter as long as they are
2340b57cec5SDimitry Andric   // different in different DSOs, so we chose the start address of the DSO.
2350b57cec5SDimitry Andric   addOptionalRegular("__dso_handle", Out::elfHeader, 0, STV_HIDDEN);
2360b57cec5SDimitry Andric 
237480093f4SDimitry Andric   // If linker script do layout we do not need to create any standard symbols.
2380b57cec5SDimitry Andric   if (script->hasSectionsCommand)
2390b57cec5SDimitry Andric     return;
2400b57cec5SDimitry Andric 
2410b57cec5SDimitry Andric   auto add = [](StringRef s, int64_t pos) {
2420b57cec5SDimitry Andric     return addOptionalRegular(s, Out::elfHeader, pos, STV_DEFAULT);
2430b57cec5SDimitry Andric   };
2440b57cec5SDimitry Andric 
2450b57cec5SDimitry Andric   ElfSym::bss = add("__bss_start", 0);
2460b57cec5SDimitry Andric   ElfSym::end1 = add("end", -1);
2470b57cec5SDimitry Andric   ElfSym::end2 = add("_end", -1);
2480b57cec5SDimitry Andric   ElfSym::etext1 = add("etext", -1);
2490b57cec5SDimitry Andric   ElfSym::etext2 = add("_etext", -1);
2500b57cec5SDimitry Andric   ElfSym::edata1 = add("edata", -1);
2510b57cec5SDimitry Andric   ElfSym::edata2 = add("_edata", -1);
2520b57cec5SDimitry Andric }
2530b57cec5SDimitry Andric 
2545f757f3fSDimitry Andric static void demoteDefined(Defined &sym, DenseMap<SectionBase *, size_t> &map) {
2555f757f3fSDimitry Andric   if (map.empty())
2565f757f3fSDimitry Andric     for (auto [i, sec] : llvm::enumerate(sym.file->getSections()))
2575f757f3fSDimitry Andric       map.try_emplace(sec, i);
2585f757f3fSDimitry Andric   // Change WEAK to GLOBAL so that if a scanned relocation references sym,
2595f757f3fSDimitry Andric   // maybeReportUndefined will report an error.
2605f757f3fSDimitry Andric   uint8_t binding = sym.isWeak() ? uint8_t(STB_GLOBAL) : sym.binding;
2615f757f3fSDimitry Andric   Undefined(sym.file, sym.getName(), binding, sym.stOther, sym.type,
2625f757f3fSDimitry Andric             /*discardedSecIdx=*/map.lookup(sym.section))
2635f757f3fSDimitry Andric       .overwrite(sym);
2645f757f3fSDimitry Andric }
2655f757f3fSDimitry Andric 
2665f757f3fSDimitry Andric // If all references to a DSO happen to be weak, the DSO is not added to
2675f757f3fSDimitry Andric // DT_NEEDED. If that happens, replace ShardSymbol with Undefined to avoid
2685f757f3fSDimitry Andric // dangling references to an unneeded DSO. Use a weak binding to avoid
2695f757f3fSDimitry Andric // --no-allow-shlib-undefined diagnostics. Similarly, demote lazy symbols.
2705f757f3fSDimitry Andric //
2715f757f3fSDimitry Andric // In addition, demote symbols defined in discarded sections, so that
2725f757f3fSDimitry Andric // references to /DISCARD/ discarded symbols will lead to errors.
2735f757f3fSDimitry Andric static void demoteSymbolsAndComputeIsPreemptible() {
2745f757f3fSDimitry Andric   llvm::TimeTraceScope timeScope("Demote symbols");
2755f757f3fSDimitry Andric   DenseMap<InputFile *, DenseMap<SectionBase *, size_t>> sectionIndexMap;
2765f757f3fSDimitry Andric   for (Symbol *sym : symtab.getSymbols()) {
2775f757f3fSDimitry Andric     if (auto *d = dyn_cast<Defined>(sym)) {
2785f757f3fSDimitry Andric       if (d->section && !d->section->isLive())
2795f757f3fSDimitry Andric         demoteDefined(*d, sectionIndexMap[d->file]);
2805f757f3fSDimitry Andric     } else {
2815f757f3fSDimitry Andric       auto *s = dyn_cast<SharedSymbol>(sym);
2825f757f3fSDimitry Andric       if (sym->isLazy() || (s && !cast<SharedFile>(s->file)->isNeeded)) {
2835f757f3fSDimitry Andric         uint8_t binding = sym->isLazy() ? sym->binding : uint8_t(STB_WEAK);
2847a6dacacSDimitry Andric         Undefined(ctx.internalFile, sym->getName(), binding, sym->stOther,
2857a6dacacSDimitry Andric                   sym->type)
2865f757f3fSDimitry Andric             .overwrite(*sym);
2875f757f3fSDimitry Andric         sym->versionId = VER_NDX_GLOBAL;
2885f757f3fSDimitry Andric       }
2895f757f3fSDimitry Andric     }
2905f757f3fSDimitry Andric 
2915f757f3fSDimitry Andric     if (config->hasDynSymTab)
2925f757f3fSDimitry Andric       sym->isPreemptible = computeIsPreemptible(*sym);
2935f757f3fSDimitry Andric   }
2945f757f3fSDimitry Andric }
2955f757f3fSDimitry Andric 
2961db9f3b2SDimitry Andric bool elf::hasMemtag() {
2971db9f3b2SDimitry Andric   return config->emachine == EM_AARCH64 &&
2981db9f3b2SDimitry Andric          config->androidMemtagMode != ELF::NT_MEMTAG_LEVEL_NONE;
2991db9f3b2SDimitry Andric }
3001db9f3b2SDimitry Andric 
3015f757f3fSDimitry Andric // Fully static executables don't support MTE globals at this point in time, as
3025f757f3fSDimitry Andric // we currently rely on:
3035f757f3fSDimitry Andric //   - A dynamic loader to process relocations, and
3045f757f3fSDimitry Andric //   - Dynamic entries.
3055f757f3fSDimitry Andric // This restriction could be removed in future by re-using some of the ideas
3065f757f3fSDimitry Andric // that ifuncs use in fully static executables.
3075f757f3fSDimitry Andric bool elf::canHaveMemtagGlobals() {
3081db9f3b2SDimitry Andric   return hasMemtag() &&
3095f757f3fSDimitry Andric          (config->relocatable || config->shared || needsInterpSection());
3105f757f3fSDimitry Andric }
3115f757f3fSDimitry Andric 
3120b57cec5SDimitry Andric static OutputSection *findSection(StringRef name, unsigned partition = 1) {
3134824e7fdSDimitry Andric   for (SectionCommand *cmd : script->sectionCommands)
31481ad6265SDimitry Andric     if (auto *osd = dyn_cast<OutputDesc>(cmd))
31581ad6265SDimitry Andric       if (osd->osec.name == name && osd->osec.partition == partition)
31681ad6265SDimitry Andric         return &osd->osec;
3170b57cec5SDimitry Andric   return nullptr;
3180b57cec5SDimitry Andric }
3190b57cec5SDimitry Andric 
3205ffd83dbSDimitry Andric template <class ELFT> void elf::createSyntheticSections() {
3210b57cec5SDimitry Andric   // Initialize all pointers with NULL. This is needed because
3220b57cec5SDimitry Andric   // you can call lld::elf::main more than once as a library.
3234824e7fdSDimitry Andric   Out::tlsPhdr = nullptr;
3244824e7fdSDimitry Andric   Out::preinitArray = nullptr;
3254824e7fdSDimitry Andric   Out::initArray = nullptr;
3264824e7fdSDimitry Andric   Out::finiArray = nullptr;
3270b57cec5SDimitry Andric 
32885868e8aSDimitry Andric   // Add the .interp section first because it is not a SyntheticSection.
32985868e8aSDimitry Andric   // The removeUnusedSyntheticSections() function relies on the
33085868e8aSDimitry Andric   // SyntheticSections coming last.
33185868e8aSDimitry Andric   if (needsInterpSection()) {
33285868e8aSDimitry Andric     for (size_t i = 1; i <= partitions.size(); ++i) {
33385868e8aSDimitry Andric       InputSection *sec = createInterpSection();
33485868e8aSDimitry Andric       sec->partition = i;
335bdd1243dSDimitry Andric       ctx.inputSections.push_back(sec);
33685868e8aSDimitry Andric     }
33785868e8aSDimitry Andric   }
33885868e8aSDimitry Andric 
339bdd1243dSDimitry Andric   auto add = [](SyntheticSection &sec) { ctx.inputSections.push_back(&sec); };
3400b57cec5SDimitry Andric 
34104eeddc0SDimitry Andric   in.shStrTab = std::make_unique<StringTableSection>(".shstrtab", false);
3420b57cec5SDimitry Andric 
3430b57cec5SDimitry Andric   Out::programHeaders = make<OutputSection>("", 0, SHF_ALLOC);
344bdd1243dSDimitry Andric   Out::programHeaders->addralign = config->wordsize;
3450b57cec5SDimitry Andric 
3460b57cec5SDimitry Andric   if (config->strip != StripPolicy::All) {
34704eeddc0SDimitry Andric     in.strTab = std::make_unique<StringTableSection>(".strtab", false);
34804eeddc0SDimitry Andric     in.symTab = std::make_unique<SymbolTableSection<ELFT>>(*in.strTab);
34904eeddc0SDimitry Andric     in.symTabShndx = std::make_unique<SymtabShndxSection>();
3500b57cec5SDimitry Andric   }
3510b57cec5SDimitry Andric 
35204eeddc0SDimitry Andric   in.bss = std::make_unique<BssSection>(".bss", 0, 1);
3530eae32dcSDimitry Andric   add(*in.bss);
3540b57cec5SDimitry Andric 
3550b57cec5SDimitry Andric   // If there is a SECTIONS command and a .data.rel.ro section name use name
3560b57cec5SDimitry Andric   // .data.rel.ro.bss so that we match in the .data.rel.ro output section.
3570b57cec5SDimitry Andric   // This makes sure our relro is contiguous.
3581fd87a68SDimitry Andric   bool hasDataRelRo = script->hasSectionsCommand && findSection(".data.rel.ro");
35904eeddc0SDimitry Andric   in.bssRelRo = std::make_unique<BssSection>(
36004eeddc0SDimitry Andric       hasDataRelRo ? ".data.rel.ro.bss" : ".bss.rel.ro", 0, 1);
3610eae32dcSDimitry Andric   add(*in.bssRelRo);
3620b57cec5SDimitry Andric 
3630b57cec5SDimitry Andric   // Add MIPS-specific sections.
3640b57cec5SDimitry Andric   if (config->emachine == EM_MIPS) {
3650b57cec5SDimitry Andric     if (!config->shared && config->hasDynSymTab) {
36604eeddc0SDimitry Andric       in.mipsRldMap = std::make_unique<MipsRldMapSection>();
3670eae32dcSDimitry Andric       add(*in.mipsRldMap);
3680b57cec5SDimitry Andric     }
3691fd87a68SDimitry Andric     if ((in.mipsAbiFlags = MipsAbiFlagsSection<ELFT>::create()))
3701fd87a68SDimitry Andric       add(*in.mipsAbiFlags);
3711fd87a68SDimitry Andric     if ((in.mipsOptions = MipsOptionsSection<ELFT>::create()))
3721fd87a68SDimitry Andric       add(*in.mipsOptions);
3731fd87a68SDimitry Andric     if ((in.mipsReginfo = MipsReginfoSection<ELFT>::create()))
3741fd87a68SDimitry Andric       add(*in.mipsReginfo);
3750b57cec5SDimitry Andric   }
3760b57cec5SDimitry Andric 
37785868e8aSDimitry Andric   StringRef relaDynName = config->isRela ? ".rela.dyn" : ".rel.dyn";
37885868e8aSDimitry Andric 
379bdd1243dSDimitry Andric   const unsigned threadCount = config->threadCount;
3800b57cec5SDimitry Andric   for (Partition &part : partitions) {
3810eae32dcSDimitry Andric     auto add = [&](SyntheticSection &sec) {
3820eae32dcSDimitry Andric       sec.partition = part.getNumber();
383bdd1243dSDimitry Andric       ctx.inputSections.push_back(&sec);
3840b57cec5SDimitry Andric     };
3850b57cec5SDimitry Andric 
3860b57cec5SDimitry Andric     if (!part.name.empty()) {
38704eeddc0SDimitry Andric       part.elfHeader = std::make_unique<PartitionElfHeaderSection<ELFT>>();
3880b57cec5SDimitry Andric       part.elfHeader->name = part.name;
3890eae32dcSDimitry Andric       add(*part.elfHeader);
3900b57cec5SDimitry Andric 
39104eeddc0SDimitry Andric       part.programHeaders =
39204eeddc0SDimitry Andric           std::make_unique<PartitionProgramHeadersSection<ELFT>>();
3930eae32dcSDimitry Andric       add(*part.programHeaders);
3940b57cec5SDimitry Andric     }
3950b57cec5SDimitry Andric 
3960b57cec5SDimitry Andric     if (config->buildId != BuildIdKind::None) {
39704eeddc0SDimitry Andric       part.buildId = std::make_unique<BuildIdSection>();
3980eae32dcSDimitry Andric       add(*part.buildId);
3990b57cec5SDimitry Andric     }
4000b57cec5SDimitry Andric 
40104eeddc0SDimitry Andric     part.dynStrTab = std::make_unique<StringTableSection>(".dynstr", true);
40204eeddc0SDimitry Andric     part.dynSymTab =
40304eeddc0SDimitry Andric         std::make_unique<SymbolTableSection<ELFT>>(*part.dynStrTab);
40404eeddc0SDimitry Andric     part.dynamic = std::make_unique<DynamicSection<ELFT>>();
40581ad6265SDimitry Andric 
4061db9f3b2SDimitry Andric     if (hasMemtag()) {
40781ad6265SDimitry Andric       part.memtagAndroidNote = std::make_unique<MemtagAndroidNote>();
40881ad6265SDimitry Andric       add(*part.memtagAndroidNote);
4091db9f3b2SDimitry Andric       if (canHaveMemtagGlobals()) {
4101db9f3b2SDimitry Andric         part.memtagGlobalDescriptors =
4111db9f3b2SDimitry Andric             std::make_unique<MemtagGlobalDescriptors>();
4121db9f3b2SDimitry Andric         add(*part.memtagGlobalDescriptors);
4131db9f3b2SDimitry Andric       }
41481ad6265SDimitry Andric     }
41581ad6265SDimitry Andric 
41685868e8aSDimitry Andric     if (config->androidPackDynRelocs)
417bdd1243dSDimitry Andric       part.relaDyn = std::make_unique<AndroidPackedRelocationSection<ELFT>>(
418bdd1243dSDimitry Andric           relaDynName, threadCount);
41904eeddc0SDimitry Andric     else
42004eeddc0SDimitry Andric       part.relaDyn = std::make_unique<RelocationSection<ELFT>>(
421bdd1243dSDimitry Andric           relaDynName, config->zCombreloc, threadCount);
4220b57cec5SDimitry Andric 
4230b57cec5SDimitry Andric     if (config->hasDynSymTab) {
4240eae32dcSDimitry Andric       add(*part.dynSymTab);
4250b57cec5SDimitry Andric 
42604eeddc0SDimitry Andric       part.verSym = std::make_unique<VersionTableSection>();
4270eae32dcSDimitry Andric       add(*part.verSym);
4280b57cec5SDimitry Andric 
42985868e8aSDimitry Andric       if (!namedVersionDefs().empty()) {
43004eeddc0SDimitry Andric         part.verDef = std::make_unique<VersionDefinitionSection>();
4310eae32dcSDimitry Andric         add(*part.verDef);
4320b57cec5SDimitry Andric       }
4330b57cec5SDimitry Andric 
43404eeddc0SDimitry Andric       part.verNeed = std::make_unique<VersionNeedSection<ELFT>>();
4350eae32dcSDimitry Andric       add(*part.verNeed);
4360b57cec5SDimitry Andric 
4370b57cec5SDimitry Andric       if (config->gnuHash) {
43804eeddc0SDimitry Andric         part.gnuHashTab = std::make_unique<GnuHashTableSection>();
4390eae32dcSDimitry Andric         add(*part.gnuHashTab);
4400b57cec5SDimitry Andric       }
4410b57cec5SDimitry Andric 
4420b57cec5SDimitry Andric       if (config->sysvHash) {
44304eeddc0SDimitry Andric         part.hashTab = std::make_unique<HashTableSection>();
4440eae32dcSDimitry Andric         add(*part.hashTab);
4450b57cec5SDimitry Andric       }
4460b57cec5SDimitry Andric 
4470eae32dcSDimitry Andric       add(*part.dynamic);
4480eae32dcSDimitry Andric       add(*part.dynStrTab);
4490eae32dcSDimitry Andric       add(*part.relaDyn);
4500b57cec5SDimitry Andric     }
4510b57cec5SDimitry Andric 
4520b57cec5SDimitry Andric     if (config->relrPackDynRelocs) {
453bdd1243dSDimitry Andric       part.relrDyn = std::make_unique<RelrSection<ELFT>>(threadCount);
4540eae32dcSDimitry Andric       add(*part.relrDyn);
4550b57cec5SDimitry Andric     }
4560b57cec5SDimitry Andric 
4570b57cec5SDimitry Andric     if (!config->relocatable) {
4580b57cec5SDimitry Andric       if (config->ehFrameHdr) {
45904eeddc0SDimitry Andric         part.ehFrameHdr = std::make_unique<EhFrameHeader>();
4600eae32dcSDimitry Andric         add(*part.ehFrameHdr);
4610b57cec5SDimitry Andric       }
46204eeddc0SDimitry Andric       part.ehFrame = std::make_unique<EhFrameSection>();
4630eae32dcSDimitry Andric       add(*part.ehFrame);
4640b57cec5SDimitry Andric 
465bdd1243dSDimitry Andric       if (config->emachine == EM_ARM) {
466bdd1243dSDimitry Andric         // This section replaces all the individual .ARM.exidx InputSections.
46704eeddc0SDimitry Andric         part.armExidx = std::make_unique<ARMExidxSyntheticSection>();
4680eae32dcSDimitry Andric         add(*part.armExidx);
4690b57cec5SDimitry Andric       }
470bdd1243dSDimitry Andric     }
47161cfbce3SDimitry Andric 
47261cfbce3SDimitry Andric     if (!config->packageMetadata.empty()) {
47361cfbce3SDimitry Andric       part.packageMetadataNote = std::make_unique<PackageMetadataNote>();
47461cfbce3SDimitry Andric       add(*part.packageMetadataNote);
47561cfbce3SDimitry Andric     }
4760b57cec5SDimitry Andric   }
4770b57cec5SDimitry Andric 
4780b57cec5SDimitry Andric   if (partitions.size() != 1) {
4790b57cec5SDimitry Andric     // Create the partition end marker. This needs to be in partition number 255
4800b57cec5SDimitry Andric     // so that it is sorted after all other partitions. It also has other
4810b57cec5SDimitry Andric     // special handling (see createPhdrs() and combineEhSections()).
48204eeddc0SDimitry Andric     in.partEnd =
48304eeddc0SDimitry Andric         std::make_unique<BssSection>(".part.end", config->maxPageSize, 1);
4840b57cec5SDimitry Andric     in.partEnd->partition = 255;
4850eae32dcSDimitry Andric     add(*in.partEnd);
4860b57cec5SDimitry Andric 
48704eeddc0SDimitry Andric     in.partIndex = std::make_unique<PartitionIndexSection>();
48804eeddc0SDimitry Andric     addOptionalRegular("__part_index_begin", in.partIndex.get(), 0);
48904eeddc0SDimitry Andric     addOptionalRegular("__part_index_end", in.partIndex.get(),
4900b57cec5SDimitry Andric                        in.partIndex->getSize());
4910eae32dcSDimitry Andric     add(*in.partIndex);
4920b57cec5SDimitry Andric   }
4930b57cec5SDimitry Andric 
4940b57cec5SDimitry Andric   // Add .got. MIPS' .got is so different from the other archs,
4950b57cec5SDimitry Andric   // it has its own class.
4960b57cec5SDimitry Andric   if (config->emachine == EM_MIPS) {
49704eeddc0SDimitry Andric     in.mipsGot = std::make_unique<MipsGotSection>();
4980eae32dcSDimitry Andric     add(*in.mipsGot);
4990b57cec5SDimitry Andric   } else {
50004eeddc0SDimitry Andric     in.got = std::make_unique<GotSection>();
5010eae32dcSDimitry Andric     add(*in.got);
5020b57cec5SDimitry Andric   }
5030b57cec5SDimitry Andric 
5040b57cec5SDimitry Andric   if (config->emachine == EM_PPC) {
50504eeddc0SDimitry Andric     in.ppc32Got2 = std::make_unique<PPC32Got2Section>();
5060eae32dcSDimitry Andric     add(*in.ppc32Got2);
5070b57cec5SDimitry Andric   }
5080b57cec5SDimitry Andric 
5090b57cec5SDimitry Andric   if (config->emachine == EM_PPC64) {
51004eeddc0SDimitry Andric     in.ppc64LongBranchTarget = std::make_unique<PPC64LongBranchTargetSection>();
5110eae32dcSDimitry Andric     add(*in.ppc64LongBranchTarget);
5120b57cec5SDimitry Andric   }
5130b57cec5SDimitry Andric 
51404eeddc0SDimitry Andric   in.gotPlt = std::make_unique<GotPltSection>();
5150eae32dcSDimitry Andric   add(*in.gotPlt);
51604eeddc0SDimitry Andric   in.igotPlt = std::make_unique<IgotPltSection>();
5170eae32dcSDimitry Andric   add(*in.igotPlt);
5185f757f3fSDimitry Andric   // Add .relro_padding if DATA_SEGMENT_RELRO_END is used; otherwise, add the
5195f757f3fSDimitry Andric   // section in the absence of PHDRS/SECTIONS commands.
5205f757f3fSDimitry Andric   if (config->zRelro && ((script->phdrsCommands.empty() &&
5215f757f3fSDimitry Andric         !script->hasSectionsCommand) || script->seenRelroEnd)) {
5225f757f3fSDimitry Andric     in.relroPadding = std::make_unique<RelroPaddingSection>();
5235f757f3fSDimitry Andric     add(*in.relroPadding);
5245f757f3fSDimitry Andric   }
5250b57cec5SDimitry Andric 
52606c3fb27SDimitry Andric   if (config->emachine == EM_ARM) {
52706c3fb27SDimitry Andric     in.armCmseSGSection = std::make_unique<ArmCmseSGSection>();
52806c3fb27SDimitry Andric     add(*in.armCmseSGSection);
52906c3fb27SDimitry Andric   }
53006c3fb27SDimitry Andric 
5310b57cec5SDimitry Andric   // _GLOBAL_OFFSET_TABLE_ is defined relative to either .got.plt or .got. Treat
5320b57cec5SDimitry Andric   // it as a relocation and ensure the referenced section is created.
5330b57cec5SDimitry Andric   if (ElfSym::globalOffsetTable && config->emachine != EM_MIPS) {
5340b57cec5SDimitry Andric     if (target->gotBaseSymInGotPlt)
5350b57cec5SDimitry Andric       in.gotPlt->hasGotPltOffRel = true;
5360b57cec5SDimitry Andric     else
5370b57cec5SDimitry Andric       in.got->hasGotOffRel = true;
5380b57cec5SDimitry Andric   }
5390b57cec5SDimitry Andric 
5400b57cec5SDimitry Andric   if (config->gdbIndex)
5410eae32dcSDimitry Andric     add(*GdbIndexSection::create<ELFT>());
5420b57cec5SDimitry Andric 
5430b57cec5SDimitry Andric   // We always need to add rel[a].plt to output if it has entries.
5440b57cec5SDimitry Andric   // Even for static linking it can contain R_[*]_IRELATIVE relocations.
54504eeddc0SDimitry Andric   in.relaPlt = std::make_unique<RelocationSection<ELFT>>(
546bdd1243dSDimitry Andric       config->isRela ? ".rela.plt" : ".rel.plt", /*sort=*/false,
547bdd1243dSDimitry Andric       /*threadCount=*/1);
5480eae32dcSDimitry Andric   add(*in.relaPlt);
5490b57cec5SDimitry Andric 
55085868e8aSDimitry Andric   // The relaIplt immediately follows .rel[a].dyn to ensure that the IRelative
55185868e8aSDimitry Andric   // relocations are processed last by the dynamic loader. We cannot place the
55285868e8aSDimitry Andric   // iplt section in .rel.dyn when Android relocation packing is enabled because
55385868e8aSDimitry Andric   // that would cause a section type mismatch. However, because the Android
55485868e8aSDimitry Andric   // dynamic loader reads .rel.plt after .rel.dyn, we can get the desired
55585868e8aSDimitry Andric   // behaviour by placing the iplt section in .rel.plt.
55604eeddc0SDimitry Andric   in.relaIplt = std::make_unique<RelocationSection<ELFT>>(
55785868e8aSDimitry Andric       config->androidPackDynRelocs ? in.relaPlt->name : relaDynName,
558bdd1243dSDimitry Andric       /*sort=*/false, /*threadCount=*/1);
5590eae32dcSDimitry Andric   add(*in.relaIplt);
5600b57cec5SDimitry Andric 
561480093f4SDimitry Andric   if ((config->emachine == EM_386 || config->emachine == EM_X86_64) &&
562480093f4SDimitry Andric       (config->andFeatures & GNU_PROPERTY_X86_FEATURE_1_IBT)) {
56304eeddc0SDimitry Andric     in.ibtPlt = std::make_unique<IBTPltSection>();
5640eae32dcSDimitry Andric     add(*in.ibtPlt);
565480093f4SDimitry Andric   }
566480093f4SDimitry Andric 
56704eeddc0SDimitry Andric   if (config->emachine == EM_PPC)
56804eeddc0SDimitry Andric     in.plt = std::make_unique<PPC32GlinkSection>();
56904eeddc0SDimitry Andric   else
57004eeddc0SDimitry Andric     in.plt = std::make_unique<PltSection>();
5710eae32dcSDimitry Andric   add(*in.plt);
57204eeddc0SDimitry Andric   in.iplt = std::make_unique<IpltSection>();
5730eae32dcSDimitry Andric   add(*in.iplt);
5740b57cec5SDimitry Andric 
5750b57cec5SDimitry Andric   if (config->andFeatures)
5760eae32dcSDimitry Andric     add(*make<GnuPropertySection>());
5770b57cec5SDimitry Andric 
5780b57cec5SDimitry Andric   // .note.GNU-stack is always added when we are creating a re-linkable
5790b57cec5SDimitry Andric   // object file. Other linkers are using the presence of this marker
5800b57cec5SDimitry Andric   // section to control the executable-ness of the stack area, but that
5810b57cec5SDimitry Andric   // is irrelevant these days. Stack area should always be non-executable
5820b57cec5SDimitry Andric   // by default. So we emit this section unconditionally.
5830b57cec5SDimitry Andric   if (config->relocatable)
5840eae32dcSDimitry Andric     add(*make<GnuStackSection>());
5850b57cec5SDimitry Andric 
5860b57cec5SDimitry Andric   if (in.symTab)
5870eae32dcSDimitry Andric     add(*in.symTab);
5880b57cec5SDimitry Andric   if (in.symTabShndx)
5890eae32dcSDimitry Andric     add(*in.symTabShndx);
5900eae32dcSDimitry Andric   add(*in.shStrTab);
5910b57cec5SDimitry Andric   if (in.strTab)
5920eae32dcSDimitry Andric     add(*in.strTab);
5930b57cec5SDimitry Andric }
5940b57cec5SDimitry Andric 
5950b57cec5SDimitry Andric // The main function of the writer.
5960b57cec5SDimitry Andric template <class ELFT> void Writer<ELFT>::run() {
5970b57cec5SDimitry Andric   // Now that we have a complete set of output sections. This function
5980b57cec5SDimitry Andric   // completes section contents. For example, we need to add strings
5990b57cec5SDimitry Andric   // to the string table, and add entries to .got and .plt.
6000b57cec5SDimitry Andric   // finalizeSections does that.
6010b57cec5SDimitry Andric   finalizeSections();
6020b57cec5SDimitry Andric   checkExecuteOnly();
6030b57cec5SDimitry Andric 
604349cc55cSDimitry Andric   // If --compressed-debug-sections is specified, compress .debug_* sections.
605349cc55cSDimitry Andric   // Do it right now because it changes the size of output sections.
6060b57cec5SDimitry Andric   for (OutputSection *sec : outputSections)
6070b57cec5SDimitry Andric     sec->maybeCompress<ELFT>();
6080b57cec5SDimitry Andric 
60969660011SDimitry Andric   if (script->hasSectionsCommand)
6100b57cec5SDimitry Andric     script->allocateHeaders(mainPart->phdrs);
6110b57cec5SDimitry Andric 
6120b57cec5SDimitry Andric   // Remove empty PT_LOAD to avoid causing the dynamic linker to try to mmap a
6130b57cec5SDimitry Andric   // 0 sized region. This has to be done late since only after assignAddresses
6140b57cec5SDimitry Andric   // we know the size of the sections.
6150b57cec5SDimitry Andric   for (Partition &part : partitions)
6160b57cec5SDimitry Andric     removeEmptyPTLoad(part.phdrs);
6170b57cec5SDimitry Andric 
6180b57cec5SDimitry Andric   if (!config->oFormatBinary)
6190b57cec5SDimitry Andric     assignFileOffsets();
6200b57cec5SDimitry Andric   else
6210b57cec5SDimitry Andric     assignFileOffsetsBinary();
6220b57cec5SDimitry Andric 
6230b57cec5SDimitry Andric   for (Partition &part : partitions)
6240b57cec5SDimitry Andric     setPhdrs(part);
6250b57cec5SDimitry Andric 
62681ad6265SDimitry Andric   // Handle --print-map(-M)/--Map and --cref. Dump them before checkSections()
62781ad6265SDimitry Andric   // because the files may be useful in case checkSections() or openFile()
62881ad6265SDimitry Andric   // fails, for example, due to an erroneous file size.
6294824e7fdSDimitry Andric   writeMapAndCref();
6305ffd83dbSDimitry Andric 
63106c3fb27SDimitry Andric   // Handle --print-memory-usage option.
63206c3fb27SDimitry Andric   if (config->printMemoryUsage)
63306c3fb27SDimitry Andric     script->printMemoryUsage(lld::outs());
63406c3fb27SDimitry Andric 
6350b57cec5SDimitry Andric   if (config->checkSections)
6360b57cec5SDimitry Andric     checkSections();
6370b57cec5SDimitry Andric 
6380b57cec5SDimitry Andric   // It does not make sense try to open the file if we have error already.
6390b57cec5SDimitry Andric   if (errorCount())
6400b57cec5SDimitry Andric     return;
641e8d8bef9SDimitry Andric 
642e8d8bef9SDimitry Andric   {
643e8d8bef9SDimitry Andric     llvm::TimeTraceScope timeScope("Write output file");
6440b57cec5SDimitry Andric     // Write the result down to a file.
6450b57cec5SDimitry Andric     openFile();
6460b57cec5SDimitry Andric     if (errorCount())
6470b57cec5SDimitry Andric       return;
6480b57cec5SDimitry Andric 
6490b57cec5SDimitry Andric     if (!config->oFormatBinary) {
65085868e8aSDimitry Andric       if (config->zSeparate != SeparateSegmentKind::None)
6510b57cec5SDimitry Andric         writeTrapInstr();
6520b57cec5SDimitry Andric       writeHeader();
6530b57cec5SDimitry Andric       writeSections();
6540b57cec5SDimitry Andric     } else {
6550b57cec5SDimitry Andric       writeSectionsBinary();
6560b57cec5SDimitry Andric     }
6570b57cec5SDimitry Andric 
6580b57cec5SDimitry Andric     // Backfill .note.gnu.build-id section content. This is done at last
6590b57cec5SDimitry Andric     // because the content is usually a hash value of the entire output file.
6600b57cec5SDimitry Andric     writeBuildId();
6610b57cec5SDimitry Andric     if (errorCount())
6620b57cec5SDimitry Andric       return;
6630b57cec5SDimitry Andric 
6640b57cec5SDimitry Andric     if (auto e = buffer->commit())
665bdd1243dSDimitry Andric       fatal("failed to write output '" + buffer->getPath() +
666bdd1243dSDimitry Andric             "': " + toString(std::move(e)));
66706c3fb27SDimitry Andric 
66806c3fb27SDimitry Andric     if (!config->cmseOutputLib.empty())
66906c3fb27SDimitry Andric       writeARMCmseImportLib<ELFT>();
6700b57cec5SDimitry Andric   }
671e8d8bef9SDimitry Andric }
6720b57cec5SDimitry Andric 
6735ffd83dbSDimitry Andric template <class ELFT, class RelTy>
6745ffd83dbSDimitry Andric static void markUsedLocalSymbolsImpl(ObjFile<ELFT> *file,
6755ffd83dbSDimitry Andric                                      llvm::ArrayRef<RelTy> rels) {
6765ffd83dbSDimitry Andric   for (const RelTy &rel : rels) {
6775ffd83dbSDimitry Andric     Symbol &sym = file->getRelocTargetSym(rel);
6785ffd83dbSDimitry Andric     if (sym.isLocal())
6795ffd83dbSDimitry Andric       sym.used = true;
6805ffd83dbSDimitry Andric   }
6815ffd83dbSDimitry Andric }
6825ffd83dbSDimitry Andric 
6835ffd83dbSDimitry Andric // The function ensures that the "used" field of local symbols reflects the fact
6845ffd83dbSDimitry Andric // that the symbol is used in a relocation from a live section.
6855ffd83dbSDimitry Andric template <class ELFT> static void markUsedLocalSymbols() {
6865ffd83dbSDimitry Andric   // With --gc-sections, the field is already filled.
6875ffd83dbSDimitry Andric   // See MarkLive<ELFT>::resolveReloc().
6885ffd83dbSDimitry Andric   if (config->gcSections)
6895ffd83dbSDimitry Andric     return;
690bdd1243dSDimitry Andric   for (ELFFileBase *file : ctx.objectFiles) {
6915ffd83dbSDimitry Andric     ObjFile<ELFT> *f = cast<ObjFile<ELFT>>(file);
6925ffd83dbSDimitry Andric     for (InputSectionBase *s : f->getSections()) {
6935ffd83dbSDimitry Andric       InputSection *isec = dyn_cast_or_null<InputSection>(s);
6945ffd83dbSDimitry Andric       if (!isec)
6955ffd83dbSDimitry Andric         continue;
6965ffd83dbSDimitry Andric       if (isec->type == SHT_REL)
6975ffd83dbSDimitry Andric         markUsedLocalSymbolsImpl(f, isec->getDataAs<typename ELFT::Rel>());
6985ffd83dbSDimitry Andric       else if (isec->type == SHT_RELA)
6995ffd83dbSDimitry Andric         markUsedLocalSymbolsImpl(f, isec->getDataAs<typename ELFT::Rela>());
7005ffd83dbSDimitry Andric     }
7015ffd83dbSDimitry Andric   }
7025ffd83dbSDimitry Andric }
7035ffd83dbSDimitry Andric 
7040b57cec5SDimitry Andric static bool shouldKeepInSymtab(const Defined &sym) {
7050b57cec5SDimitry Andric   if (sym.isSection())
7060b57cec5SDimitry Andric     return false;
7070b57cec5SDimitry Andric 
7085ffd83dbSDimitry Andric   // If --emit-reloc or -r is given, preserve symbols referenced by relocations
7095ffd83dbSDimitry Andric   // from live sections.
71081ad6265SDimitry Andric   if (sym.used && config->copyRelocs)
7110b57cec5SDimitry Andric     return true;
7120b57cec5SDimitry Andric 
7135ffd83dbSDimitry Andric   // Exclude local symbols pointing to .ARM.exidx sections.
7145ffd83dbSDimitry Andric   // They are probably mapping symbols "$d", which are optional for these
7155ffd83dbSDimitry Andric   // sections. After merging the .ARM.exidx sections, some of these symbols
7165ffd83dbSDimitry Andric   // may become dangling. The easiest way to avoid the issue is not to add
7175ffd83dbSDimitry Andric   // them to the symbol table from the beginning.
7185ffd83dbSDimitry Andric   if (config->emachine == EM_ARM && sym.section &&
7195ffd83dbSDimitry Andric       sym.section->type == SHT_ARM_EXIDX)
7205ffd83dbSDimitry Andric     return false;
7215ffd83dbSDimitry Andric 
7225ffd83dbSDimitry Andric   if (config->discard == DiscardPolicy::None)
7230b57cec5SDimitry Andric     return true;
7245ffd83dbSDimitry Andric   if (config->discard == DiscardPolicy::All)
7255ffd83dbSDimitry Andric     return false;
7260b57cec5SDimitry Andric 
7270b57cec5SDimitry Andric   // In ELF assembly .L symbols are normally discarded by the assembler.
7280b57cec5SDimitry Andric   // If the assembler fails to do so, the linker discards them if
7290b57cec5SDimitry Andric   // * --discard-locals is used.
7300b57cec5SDimitry Andric   // * The symbol is in a SHF_MERGE section, which is normally the reason for
7310b57cec5SDimitry Andric   //   the assembler keeping the .L symbol.
73206c3fb27SDimitry Andric   if (sym.getName().starts_with(".L") &&
733349cc55cSDimitry Andric       (config->discard == DiscardPolicy::Locals ||
734349cc55cSDimitry Andric        (sym.section && (sym.section->flags & SHF_MERGE))))
7350b57cec5SDimitry Andric     return false;
736349cc55cSDimitry Andric   return true;
7370b57cec5SDimitry Andric }
7380b57cec5SDimitry Andric 
7395f757f3fSDimitry Andric bool lld::elf::includeInSymtab(const Symbol &b) {
7400b57cec5SDimitry Andric   if (auto *d = dyn_cast<Defined>(&b)) {
7410b57cec5SDimitry Andric     // Always include absolute symbols.
7420b57cec5SDimitry Andric     SectionBase *sec = d->section;
7430b57cec5SDimitry Andric     if (!sec)
7440b57cec5SDimitry Andric       return true;
7455f757f3fSDimitry Andric     assert(sec->isLive());
7460b57cec5SDimitry Andric 
7470b57cec5SDimitry Andric     if (auto *s = dyn_cast<MergeInputSection>(sec))
74881ad6265SDimitry Andric       return s->getSectionPiece(d->value).live;
7495f757f3fSDimitry Andric     return true;
7500b57cec5SDimitry Andric   }
75181ad6265SDimitry Andric   return b.used || !config->gcSections;
7520b57cec5SDimitry Andric }
7530b57cec5SDimitry Andric 
7545f757f3fSDimitry Andric // Scan local symbols to:
7555f757f3fSDimitry Andric //
7565f757f3fSDimitry Andric // - demote symbols defined relative to /DISCARD/ discarded input sections so
7575f757f3fSDimitry Andric //   that relocations referencing them will lead to errors.
7585f757f3fSDimitry Andric // - copy eligible symbols to .symTab
7595f757f3fSDimitry Andric static void demoteAndCopyLocalSymbols() {
760e8d8bef9SDimitry Andric   llvm::TimeTraceScope timeScope("Add local symbols");
761bdd1243dSDimitry Andric   for (ELFFileBase *file : ctx.objectFiles) {
7625f757f3fSDimitry Andric     DenseMap<SectionBase *, size_t> sectionIndexMap;
7630eae32dcSDimitry Andric     for (Symbol *b : file->getLocalSymbols()) {
7645ffd83dbSDimitry Andric       assert(b->isLocal() && "should have been caught in initializeSymbols()");
7650b57cec5SDimitry Andric       auto *dr = dyn_cast<Defined>(b);
7660b57cec5SDimitry Andric       if (!dr)
7670b57cec5SDimitry Andric         continue;
7685f757f3fSDimitry Andric 
7695f757f3fSDimitry Andric       if (dr->section && !dr->section->isLive())
7705f757f3fSDimitry Andric         demoteDefined(*dr, sectionIndexMap);
7715f757f3fSDimitry Andric       else if (in.symTab && includeInSymtab(*b) && shouldKeepInSymtab(*dr))
7720b57cec5SDimitry Andric         in.symTab->addSymbol(b);
7730b57cec5SDimitry Andric     }
7740b57cec5SDimitry Andric   }
7750b57cec5SDimitry Andric }
7760b57cec5SDimitry Andric 
7770b57cec5SDimitry Andric // Create a section symbol for each output section so that we can represent
7780b57cec5SDimitry Andric // relocations that point to the section. If we know that no relocation is
7790b57cec5SDimitry Andric // referring to a section (that happens if the section is a synthetic one), we
7800b57cec5SDimitry Andric // don't create a section symbol for that section.
7810b57cec5SDimitry Andric template <class ELFT> void Writer<ELFT>::addSectionSymbols() {
7824824e7fdSDimitry Andric   for (SectionCommand *cmd : script->sectionCommands) {
78381ad6265SDimitry Andric     auto *osd = dyn_cast<OutputDesc>(cmd);
78481ad6265SDimitry Andric     if (!osd)
7850b57cec5SDimitry Andric       continue;
78681ad6265SDimitry Andric     OutputSection &osec = osd->osec;
7873a9a9c0cSDimitry Andric     InputSectionBase *isec = nullptr;
7883a9a9c0cSDimitry Andric     // Iterate over all input sections and add a STT_SECTION symbol if any input
7893a9a9c0cSDimitry Andric     // section may be a relocation target.
7903a9a9c0cSDimitry Andric     for (SectionCommand *cmd : osec.commands) {
7913a9a9c0cSDimitry Andric       auto *isd = dyn_cast<InputSectionDescription>(cmd);
7923a9a9c0cSDimitry Andric       if (!isd)
7930b57cec5SDimitry Andric         continue;
7943a9a9c0cSDimitry Andric       for (InputSectionBase *s : isd->sections) {
7950b57cec5SDimitry Andric         // Relocations are not using REL[A] section symbols.
7963a9a9c0cSDimitry Andric         if (s->type == SHT_REL || s->type == SHT_RELA)
7970b57cec5SDimitry Andric           continue;
7980b57cec5SDimitry Andric 
7993a9a9c0cSDimitry Andric         // Unlike other synthetic sections, mergeable output sections contain
8003a9a9c0cSDimitry Andric         // data copied from input sections, and there may be a relocation
8013a9a9c0cSDimitry Andric         // pointing to its contents if -r or --emit-reloc is given.
8023a9a9c0cSDimitry Andric         if (isa<SyntheticSection>(s) && !(s->flags & SHF_MERGE))
8033a9a9c0cSDimitry Andric           continue;
8043a9a9c0cSDimitry Andric 
8053a9a9c0cSDimitry Andric         isec = s;
8063a9a9c0cSDimitry Andric         break;
8073a9a9c0cSDimitry Andric       }
8083a9a9c0cSDimitry Andric     }
8093a9a9c0cSDimitry Andric     if (!isec)
8100b57cec5SDimitry Andric       continue;
8110b57cec5SDimitry Andric 
812e8d8bef9SDimitry Andric     // Set the symbol to be relative to the output section so that its st_value
813e8d8bef9SDimitry Andric     // equals the output section address. Note, there may be a gap between the
814e8d8bef9SDimitry Andric     // start of the output section and isec.
81581ad6265SDimitry Andric     in.symTab->addSymbol(makeDefined(isec->file, "", STB_LOCAL, /*stOther=*/0,
81681ad6265SDimitry Andric                                      STT_SECTION,
81781ad6265SDimitry Andric                                      /*value=*/0, /*size=*/0, &osec));
8180b57cec5SDimitry Andric   }
8190b57cec5SDimitry Andric }
8200b57cec5SDimitry Andric 
8210b57cec5SDimitry Andric // Today's loaders have a feature to make segments read-only after
8220b57cec5SDimitry Andric // processing dynamic relocations to enhance security. PT_GNU_RELRO
8230b57cec5SDimitry Andric // is defined for that.
8240b57cec5SDimitry Andric //
8250b57cec5SDimitry Andric // This function returns true if a section needs to be put into a
8260b57cec5SDimitry Andric // PT_GNU_RELRO segment.
8270b57cec5SDimitry Andric static bool isRelroSection(const OutputSection *sec) {
8280b57cec5SDimitry Andric   if (!config->zRelro)
8290b57cec5SDimitry Andric     return false;
83081ad6265SDimitry Andric   if (sec->relro)
83181ad6265SDimitry Andric     return true;
8320b57cec5SDimitry Andric 
8330b57cec5SDimitry Andric   uint64_t flags = sec->flags;
8340b57cec5SDimitry Andric 
8350b57cec5SDimitry Andric   // Non-allocatable or non-writable sections don't need RELRO because
8360b57cec5SDimitry Andric   // they are not writable or not even mapped to memory in the first place.
8370b57cec5SDimitry Andric   // RELRO is for sections that are essentially read-only but need to
8380b57cec5SDimitry Andric   // be writable only at process startup to allow dynamic linker to
8390b57cec5SDimitry Andric   // apply relocations.
8400b57cec5SDimitry Andric   if (!(flags & SHF_ALLOC) || !(flags & SHF_WRITE))
8410b57cec5SDimitry Andric     return false;
8420b57cec5SDimitry Andric 
8430b57cec5SDimitry Andric   // Once initialized, TLS data segments are used as data templates
8440b57cec5SDimitry Andric   // for a thread-local storage. For each new thread, runtime
8450b57cec5SDimitry Andric   // allocates memory for a TLS and copy templates there. No thread
8460b57cec5SDimitry Andric   // are supposed to use templates directly. Thus, it can be in RELRO.
8470b57cec5SDimitry Andric   if (flags & SHF_TLS)
8480b57cec5SDimitry Andric     return true;
8490b57cec5SDimitry Andric 
8500b57cec5SDimitry Andric   // .init_array, .preinit_array and .fini_array contain pointers to
8510b57cec5SDimitry Andric   // functions that are executed on process startup or exit. These
8520b57cec5SDimitry Andric   // pointers are set by the static linker, and they are not expected
8530b57cec5SDimitry Andric   // to change at runtime. But if you are an attacker, you could do
8540b57cec5SDimitry Andric   // interesting things by manipulating pointers in .fini_array, for
8550b57cec5SDimitry Andric   // example. So they are put into RELRO.
8560b57cec5SDimitry Andric   uint32_t type = sec->type;
8570b57cec5SDimitry Andric   if (type == SHT_INIT_ARRAY || type == SHT_FINI_ARRAY ||
8580b57cec5SDimitry Andric       type == SHT_PREINIT_ARRAY)
8590b57cec5SDimitry Andric     return true;
8600b57cec5SDimitry Andric 
8610b57cec5SDimitry Andric   // .got contains pointers to external symbols. They are resolved by
8620b57cec5SDimitry Andric   // the dynamic linker when a module is loaded into memory, and after
8630b57cec5SDimitry Andric   // that they are not expected to change. So, it can be in RELRO.
8640b57cec5SDimitry Andric   if (in.got && sec == in.got->getParent())
8650b57cec5SDimitry Andric     return true;
8660b57cec5SDimitry Andric 
8670b57cec5SDimitry Andric   // .toc is a GOT-ish section for PowerPC64. Their contents are accessed
8680b57cec5SDimitry Andric   // through r2 register, which is reserved for that purpose. Since r2 is used
8690b57cec5SDimitry Andric   // for accessing .got as well, .got and .toc need to be close enough in the
8700b57cec5SDimitry Andric   // virtual address space. Usually, .toc comes just after .got. Since we place
8710b57cec5SDimitry Andric   // .got into RELRO, .toc needs to be placed into RELRO too.
8720b57cec5SDimitry Andric   if (sec->name.equals(".toc"))
8730b57cec5SDimitry Andric     return true;
8740b57cec5SDimitry Andric 
8750b57cec5SDimitry Andric   // .got.plt contains pointers to external function symbols. They are
8760b57cec5SDimitry Andric   // by default resolved lazily, so we usually cannot put it into RELRO.
8770b57cec5SDimitry Andric   // However, if "-z now" is given, the lazy symbol resolution is
8780b57cec5SDimitry Andric   // disabled, which enables us to put it into RELRO.
8790b57cec5SDimitry Andric   if (sec == in.gotPlt->getParent())
8800b57cec5SDimitry Andric     return config->zNow;
8810b57cec5SDimitry Andric 
8825f757f3fSDimitry Andric   if (in.relroPadding && sec == in.relroPadding->getParent())
8835f757f3fSDimitry Andric     return true;
8845f757f3fSDimitry Andric 
8850b57cec5SDimitry Andric   // .dynamic section contains data for the dynamic linker, and
8860b57cec5SDimitry Andric   // there's no need to write to it at runtime, so it's better to put
8870b57cec5SDimitry Andric   // it into RELRO.
8880b57cec5SDimitry Andric   if (sec->name == ".dynamic")
8890b57cec5SDimitry Andric     return true;
8900b57cec5SDimitry Andric 
8910b57cec5SDimitry Andric   // Sections with some special names are put into RELRO. This is a
8920b57cec5SDimitry Andric   // bit unfortunate because section names shouldn't be significant in
8930b57cec5SDimitry Andric   // ELF in spirit. But in reality many linker features depend on
8940b57cec5SDimitry Andric   // magic section names.
8950b57cec5SDimitry Andric   StringRef s = sec->name;
8960b57cec5SDimitry Andric   return s == ".data.rel.ro" || s == ".bss.rel.ro" || s == ".ctors" ||
8970b57cec5SDimitry Andric          s == ".dtors" || s == ".jcr" || s == ".eh_frame" ||
8985ffd83dbSDimitry Andric          s == ".fini_array" || s == ".init_array" ||
8995ffd83dbSDimitry Andric          s == ".openbsd.randomdata" || s == ".preinit_array";
9000b57cec5SDimitry Andric }
9010b57cec5SDimitry Andric 
9020b57cec5SDimitry Andric // We compute a rank for each section. The rank indicates where the
9030b57cec5SDimitry Andric // section should be placed in the file.  Instead of using simple
9040b57cec5SDimitry Andric // numbers (0,1,2...), we use a series of flags. One for each decision
9050b57cec5SDimitry Andric // point when placing the section.
9060b57cec5SDimitry Andric // Using flags has two key properties:
9070b57cec5SDimitry Andric // * It is easy to check if a give branch was taken.
9080b57cec5SDimitry Andric // * It is easy two see how similar two ranks are (see getRankProximity).
9090b57cec5SDimitry Andric enum RankFlags {
9100b57cec5SDimitry Andric   RF_NOT_ADDR_SET = 1 << 27,
9110b57cec5SDimitry Andric   RF_NOT_ALLOC = 1 << 26,
9120b57cec5SDimitry Andric   RF_PARTITION = 1 << 18, // Partition number (8 bits)
91306c3fb27SDimitry Andric   RF_NOT_SPECIAL = 1 << 17,
91406c3fb27SDimitry Andric   RF_WRITE = 1 << 16,
91506c3fb27SDimitry Andric   RF_EXEC_WRITE = 1 << 15,
91606c3fb27SDimitry Andric   RF_EXEC = 1 << 14,
91706c3fb27SDimitry Andric   RF_RODATA = 1 << 13,
91806c3fb27SDimitry Andric   RF_LARGE = 1 << 12,
9190b57cec5SDimitry Andric   RF_NOT_RELRO = 1 << 9,
9200b57cec5SDimitry Andric   RF_NOT_TLS = 1 << 8,
9210b57cec5SDimitry Andric   RF_BSS = 1 << 7,
9220b57cec5SDimitry Andric };
9230b57cec5SDimitry Andric 
9245f757f3fSDimitry Andric static unsigned getSectionRank(OutputSection &osec) {
92581ad6265SDimitry Andric   unsigned rank = osec.partition * RF_PARTITION;
9260b57cec5SDimitry Andric 
9270b57cec5SDimitry Andric   // We want to put section specified by -T option first, so we
9280b57cec5SDimitry Andric   // can start assigning VA starting from them later.
92981ad6265SDimitry Andric   if (config->sectionStartMap.count(osec.name))
9300b57cec5SDimitry Andric     return rank;
9310b57cec5SDimitry Andric   rank |= RF_NOT_ADDR_SET;
9320b57cec5SDimitry Andric 
9330b57cec5SDimitry Andric   // Allocatable sections go first to reduce the total PT_LOAD size and
9340b57cec5SDimitry Andric   // so debug info doesn't change addresses in actual code.
93581ad6265SDimitry Andric   if (!(osec.flags & SHF_ALLOC))
9360b57cec5SDimitry Andric     return rank | RF_NOT_ALLOC;
9370b57cec5SDimitry Andric 
93881ad6265SDimitry Andric   if (osec.type == SHT_LLVM_PART_EHDR)
9390b57cec5SDimitry Andric     return rank;
94081ad6265SDimitry Andric   if (osec.type == SHT_LLVM_PART_PHDR)
94106c3fb27SDimitry Andric     return rank | 1;
9420b57cec5SDimitry Andric 
9430b57cec5SDimitry Andric   // Put .interp first because some loaders want to see that section
9440b57cec5SDimitry Andric   // on the first page of the executable file when loaded into memory.
94581ad6265SDimitry Andric   if (osec.name == ".interp")
94606c3fb27SDimitry Andric     return rank | 2;
9470b57cec5SDimitry Andric 
94806c3fb27SDimitry Andric   // Put .note sections at the beginning so that they are likely to be included
94906c3fb27SDimitry Andric   // in a truncate core file. In particular, .note.gnu.build-id, if available,
95006c3fb27SDimitry Andric   // can identify the object file.
95181ad6265SDimitry Andric   if (osec.type == SHT_NOTE)
95206c3fb27SDimitry Andric     return rank | 3;
95306c3fb27SDimitry Andric 
95406c3fb27SDimitry Andric   rank |= RF_NOT_SPECIAL;
9550b57cec5SDimitry Andric 
9560b57cec5SDimitry Andric   // Sort sections based on their access permission in the following
95706c3fb27SDimitry Andric   // order: R, RX, RXW, RW(RELRO), RW(non-RELRO).
95806c3fb27SDimitry Andric   //
95906c3fb27SDimitry Andric   // Read-only sections come first such that they go in the PT_LOAD covering the
96006c3fb27SDimitry Andric   // program headers at the start of the file.
96106c3fb27SDimitry Andric   //
96206c3fb27SDimitry Andric   // The layout for writable sections is PT_LOAD(PT_GNU_RELRO(.data.rel.ro
96306c3fb27SDimitry Andric   // .bss.rel.ro) | .data .bss), where | marks where page alignment happens.
96406c3fb27SDimitry Andric   // An alternative ordering is PT_LOAD(.data | PT_GNU_RELRO( .data.rel.ro
96506c3fb27SDimitry Andric   // .bss.rel.ro) | .bss), but it may waste more bytes due to 2 alignment
96606c3fb27SDimitry Andric   // places.
96781ad6265SDimitry Andric   bool isExec = osec.flags & SHF_EXECINSTR;
96881ad6265SDimitry Andric   bool isWrite = osec.flags & SHF_WRITE;
9690b57cec5SDimitry Andric 
97006c3fb27SDimitry Andric   if (!isWrite && !isExec) {
97106c3fb27SDimitry Andric     // Make PROGBITS sections (e.g .rodata .eh_frame) closer to .text to
97206c3fb27SDimitry Andric     // alleviate relocation overflow pressure. Large special sections such as
97306c3fb27SDimitry Andric     // .dynstr and .dynsym can be away from .text.
97406c3fb27SDimitry Andric     if (osec.type == SHT_PROGBITS)
9750b57cec5SDimitry Andric       rank |= RF_RODATA;
97606c3fb27SDimitry Andric     // Among PROGBITS sections, place .lrodata further from .text.
97706c3fb27SDimitry Andric     if (!(osec.flags & SHF_X86_64_LARGE && config->emachine == EM_X86_64))
97806c3fb27SDimitry Andric       rank |= RF_LARGE;
97906c3fb27SDimitry Andric   } else if (isExec) {
98006c3fb27SDimitry Andric     rank |= isWrite ? RF_EXEC_WRITE : RF_EXEC;
98106c3fb27SDimitry Andric   } else {
98206c3fb27SDimitry Andric     rank |= RF_WRITE;
98306c3fb27SDimitry Andric     // The TLS initialization block needs to be a single contiguous block. Place
98406c3fb27SDimitry Andric     // TLS sections directly before the other RELRO sections.
98581ad6265SDimitry Andric     if (!(osec.flags & SHF_TLS))
9860b57cec5SDimitry Andric       rank |= RF_NOT_TLS;
9875f757f3fSDimitry Andric     if (isRelroSection(&osec))
9885f757f3fSDimitry Andric       osec.relro = true;
9895f757f3fSDimitry Andric     else
99006c3fb27SDimitry Andric       rank |= RF_NOT_RELRO;
99106c3fb27SDimitry Andric     // Place .ldata and .lbss after .bss. Making .bss closer to .text alleviates
99206c3fb27SDimitry Andric     // relocation overflow pressure.
99306c3fb27SDimitry Andric     if (osec.flags & SHF_X86_64_LARGE && config->emachine == EM_X86_64)
99406c3fb27SDimitry Andric       rank |= RF_LARGE;
99506c3fb27SDimitry Andric   }
9960b57cec5SDimitry Andric 
9970b57cec5SDimitry Andric   // Within TLS sections, or within other RelRo sections, or within non-RelRo
9980b57cec5SDimitry Andric   // sections, place non-NOBITS sections first.
99981ad6265SDimitry Andric   if (osec.type == SHT_NOBITS)
10000b57cec5SDimitry Andric     rank |= RF_BSS;
10010b57cec5SDimitry Andric 
10020b57cec5SDimitry Andric   // Some architectures have additional ordering restrictions for sections
10030b57cec5SDimitry Andric   // within the same PT_LOAD.
10040b57cec5SDimitry Andric   if (config->emachine == EM_PPC64) {
10050b57cec5SDimitry Andric     // PPC64 has a number of special SHT_PROGBITS+SHF_ALLOC+SHF_WRITE sections
10060b57cec5SDimitry Andric     // that we would like to make sure appear is a specific order to maximize
10070b57cec5SDimitry Andric     // their coverage by a single signed 16-bit offset from the TOC base
100806c3fb27SDimitry Andric     // pointer.
100981ad6265SDimitry Andric     StringRef name = osec.name;
10105f757f3fSDimitry Andric     if (name == ".got")
101106c3fb27SDimitry Andric       rank |= 1;
101206c3fb27SDimitry Andric     else if (name == ".toc")
10135f757f3fSDimitry Andric       rank |= 2;
10140b57cec5SDimitry Andric   }
10150b57cec5SDimitry Andric 
10160b57cec5SDimitry Andric   if (config->emachine == EM_MIPS) {
101706c3fb27SDimitry Andric     if (osec.name != ".got")
101806c3fb27SDimitry Andric       rank |= 1;
10190b57cec5SDimitry Andric     // All sections with SHF_MIPS_GPREL flag should be grouped together
10200b57cec5SDimitry Andric     // because data in these sections is addressable with a gp relative address.
102181ad6265SDimitry Andric     if (osec.flags & SHF_MIPS_GPREL)
102206c3fb27SDimitry Andric       rank |= 2;
102306c3fb27SDimitry Andric   }
10240b57cec5SDimitry Andric 
102506c3fb27SDimitry Andric   if (config->emachine == EM_RISCV) {
102606c3fb27SDimitry Andric     // .sdata and .sbss are placed closer to make GP relaxation more profitable
102706c3fb27SDimitry Andric     // and match GNU ld.
102806c3fb27SDimitry Andric     StringRef name = osec.name;
102906c3fb27SDimitry Andric     if (name == ".sdata" || (osec.type == SHT_NOBITS && name != ".sbss"))
103006c3fb27SDimitry Andric       rank |= 1;
10310b57cec5SDimitry Andric   }
10320b57cec5SDimitry Andric 
10330b57cec5SDimitry Andric   return rank;
10340b57cec5SDimitry Andric }
10350b57cec5SDimitry Andric 
10364824e7fdSDimitry Andric static bool compareSections(const SectionCommand *aCmd,
10374824e7fdSDimitry Andric                             const SectionCommand *bCmd) {
103881ad6265SDimitry Andric   const OutputSection *a = &cast<OutputDesc>(aCmd)->osec;
103981ad6265SDimitry Andric   const OutputSection *b = &cast<OutputDesc>(bCmd)->osec;
10400b57cec5SDimitry Andric 
10410b57cec5SDimitry Andric   if (a->sortRank != b->sortRank)
10420b57cec5SDimitry Andric     return a->sortRank < b->sortRank;
10430b57cec5SDimitry Andric 
10440b57cec5SDimitry Andric   if (!(a->sortRank & RF_NOT_ADDR_SET))
10450b57cec5SDimitry Andric     return config->sectionStartMap.lookup(a->name) <
10460b57cec5SDimitry Andric            config->sectionStartMap.lookup(b->name);
10470b57cec5SDimitry Andric   return false;
10480b57cec5SDimitry Andric }
10490b57cec5SDimitry Andric 
10500b57cec5SDimitry Andric void PhdrEntry::add(OutputSection *sec) {
10510b57cec5SDimitry Andric   lastSec = sec;
10520b57cec5SDimitry Andric   if (!firstSec)
10530b57cec5SDimitry Andric     firstSec = sec;
1054bdd1243dSDimitry Andric   p_align = std::max(p_align, sec->addralign);
10550b57cec5SDimitry Andric   if (p_type == PT_LOAD)
10560b57cec5SDimitry Andric     sec->ptLoad = this;
10570b57cec5SDimitry Andric }
10580b57cec5SDimitry Andric 
10590b57cec5SDimitry Andric // The beginning and the ending of .rel[a].plt section are marked
10600b57cec5SDimitry Andric // with __rel[a]_iplt_{start,end} symbols if it is a statically linked
10610b57cec5SDimitry Andric // executable. The runtime needs these symbols in order to resolve
10620b57cec5SDimitry Andric // all IRELATIVE relocs on startup. For dynamic executables, we don't
10630b57cec5SDimitry Andric // need these symbols, since IRELATIVE relocs are resolved through GOT
10640b57cec5SDimitry Andric // and PLT. For details, see http://www.airs.com/blog/archives/403.
10650b57cec5SDimitry Andric template <class ELFT> void Writer<ELFT>::addRelIpltSymbols() {
1066bdd1243dSDimitry Andric   if (config->isPic)
10670b57cec5SDimitry Andric     return;
10680b57cec5SDimitry Andric 
10690b57cec5SDimitry Andric   // By default, __rela_iplt_{start,end} belong to a dummy section 0
10700b57cec5SDimitry Andric   // because .rela.plt might be empty and thus removed from output.
10710b57cec5SDimitry Andric   // We'll override Out::elfHeader with In.relaIplt later when we are
10720b57cec5SDimitry Andric   // sure that .rela.plt exists in output.
10730b57cec5SDimitry Andric   ElfSym::relaIpltStart = addOptionalRegular(
10740b57cec5SDimitry Andric       config->isRela ? "__rela_iplt_start" : "__rel_iplt_start",
1075349cc55cSDimitry Andric       Out::elfHeader, 0, STV_HIDDEN);
10760b57cec5SDimitry Andric 
10770b57cec5SDimitry Andric   ElfSym::relaIpltEnd = addOptionalRegular(
10780b57cec5SDimitry Andric       config->isRela ? "__rela_iplt_end" : "__rel_iplt_end",
1079349cc55cSDimitry Andric       Out::elfHeader, 0, STV_HIDDEN);
10800b57cec5SDimitry Andric }
10810b57cec5SDimitry Andric 
10820b57cec5SDimitry Andric // This function generates assignments for predefined symbols (e.g. _end or
10830b57cec5SDimitry Andric // _etext) and inserts them into the commands sequence to be processed at the
10840b57cec5SDimitry Andric // appropriate time. This ensures that the value is going to be correct by the
10850b57cec5SDimitry Andric // time any references to these symbols are processed and is equivalent to
10860b57cec5SDimitry Andric // defining these symbols explicitly in the linker script.
10870b57cec5SDimitry Andric template <class ELFT> void Writer<ELFT>::setReservedSymbolSections() {
10880b57cec5SDimitry Andric   if (ElfSym::globalOffsetTable) {
10890b57cec5SDimitry Andric     // The _GLOBAL_OFFSET_TABLE_ symbol is defined by target convention usually
10900b57cec5SDimitry Andric     // to the start of the .got or .got.plt section.
109104eeddc0SDimitry Andric     InputSection *sec = in.gotPlt.get();
10920b57cec5SDimitry Andric     if (!target->gotBaseSymInGotPlt)
1093bdd1243dSDimitry Andric       sec = in.mipsGot ? cast<InputSection>(in.mipsGot.get())
109404eeddc0SDimitry Andric                        : cast<InputSection>(in.got.get());
109504eeddc0SDimitry Andric     ElfSym::globalOffsetTable->section = sec;
10960b57cec5SDimitry Andric   }
10970b57cec5SDimitry Andric 
109885868e8aSDimitry Andric   // .rela_iplt_{start,end} mark the start and the end of in.relaIplt.
10990b57cec5SDimitry Andric   if (ElfSym::relaIpltStart && in.relaIplt->isNeeded()) {
110004eeddc0SDimitry Andric     ElfSym::relaIpltStart->section = in.relaIplt.get();
110104eeddc0SDimitry Andric     ElfSym::relaIpltEnd->section = in.relaIplt.get();
11020b57cec5SDimitry Andric     ElfSym::relaIpltEnd->value = in.relaIplt->getSize();
11030b57cec5SDimitry Andric   }
11040b57cec5SDimitry Andric 
11050b57cec5SDimitry Andric   PhdrEntry *last = nullptr;
11060b57cec5SDimitry Andric   PhdrEntry *lastRO = nullptr;
11070b57cec5SDimitry Andric 
11080b57cec5SDimitry Andric   for (Partition &part : partitions) {
11090b57cec5SDimitry Andric     for (PhdrEntry *p : part.phdrs) {
11100b57cec5SDimitry Andric       if (p->p_type != PT_LOAD)
11110b57cec5SDimitry Andric         continue;
11120b57cec5SDimitry Andric       last = p;
11130b57cec5SDimitry Andric       if (!(p->p_flags & PF_W))
11140b57cec5SDimitry Andric         lastRO = p;
11150b57cec5SDimitry Andric     }
11160b57cec5SDimitry Andric   }
11170b57cec5SDimitry Andric 
11180b57cec5SDimitry Andric   if (lastRO) {
11190b57cec5SDimitry Andric     // _etext is the first location after the last read-only loadable segment.
11200b57cec5SDimitry Andric     if (ElfSym::etext1)
11210b57cec5SDimitry Andric       ElfSym::etext1->section = lastRO->lastSec;
11220b57cec5SDimitry Andric     if (ElfSym::etext2)
11230b57cec5SDimitry Andric       ElfSym::etext2->section = lastRO->lastSec;
11240b57cec5SDimitry Andric   }
11250b57cec5SDimitry Andric 
11260b57cec5SDimitry Andric   if (last) {
11270b57cec5SDimitry Andric     // _edata points to the end of the last mapped initialized section.
11280b57cec5SDimitry Andric     OutputSection *edata = nullptr;
11290b57cec5SDimitry Andric     for (OutputSection *os : outputSections) {
11300b57cec5SDimitry Andric       if (os->type != SHT_NOBITS)
11310b57cec5SDimitry Andric         edata = os;
11320b57cec5SDimitry Andric       if (os == last->lastSec)
11330b57cec5SDimitry Andric         break;
11340b57cec5SDimitry Andric     }
11350b57cec5SDimitry Andric 
11360b57cec5SDimitry Andric     if (ElfSym::edata1)
11370b57cec5SDimitry Andric       ElfSym::edata1->section = edata;
11380b57cec5SDimitry Andric     if (ElfSym::edata2)
11390b57cec5SDimitry Andric       ElfSym::edata2->section = edata;
11400b57cec5SDimitry Andric 
11410b57cec5SDimitry Andric     // _end is the first location after the uninitialized data region.
11420b57cec5SDimitry Andric     if (ElfSym::end1)
11430b57cec5SDimitry Andric       ElfSym::end1->section = last->lastSec;
11440b57cec5SDimitry Andric     if (ElfSym::end2)
11450b57cec5SDimitry Andric       ElfSym::end2->section = last->lastSec;
11460b57cec5SDimitry Andric   }
11470b57cec5SDimitry Andric 
114806c3fb27SDimitry Andric   if (ElfSym::bss) {
114906c3fb27SDimitry Andric     // On RISC-V, set __bss_start to the start of .sbss if present.
115006c3fb27SDimitry Andric     OutputSection *sbss =
115106c3fb27SDimitry Andric         config->emachine == EM_RISCV ? findSection(".sbss") : nullptr;
115206c3fb27SDimitry Andric     ElfSym::bss->section = sbss ? sbss : findSection(".bss");
115306c3fb27SDimitry Andric   }
11540b57cec5SDimitry Andric 
11550b57cec5SDimitry Andric   // Setup MIPS _gp_disp/__gnu_local_gp symbols which should
11560b57cec5SDimitry Andric   // be equal to the _gp symbol's value.
11570b57cec5SDimitry Andric   if (ElfSym::mipsGp) {
11580b57cec5SDimitry Andric     // Find GP-relative section with the lowest address
11590b57cec5SDimitry Andric     // and use this address to calculate default _gp value.
11600b57cec5SDimitry Andric     for (OutputSection *os : outputSections) {
11610b57cec5SDimitry Andric       if (os->flags & SHF_MIPS_GPREL) {
11620b57cec5SDimitry Andric         ElfSym::mipsGp->section = os;
11630b57cec5SDimitry Andric         ElfSym::mipsGp->value = 0x7ff0;
11640b57cec5SDimitry Andric         break;
11650b57cec5SDimitry Andric       }
11660b57cec5SDimitry Andric     }
11670b57cec5SDimitry Andric   }
11680b57cec5SDimitry Andric }
11690b57cec5SDimitry Andric 
11700b57cec5SDimitry Andric // We want to find how similar two ranks are.
11710b57cec5SDimitry Andric // The more branches in getSectionRank that match, the more similar they are.
11720b57cec5SDimitry Andric // Since each branch corresponds to a bit flag, we can just use
11730b57cec5SDimitry Andric // countLeadingZeros.
11744824e7fdSDimitry Andric static int getRankProximity(OutputSection *a, SectionCommand *b) {
117581ad6265SDimitry Andric   auto *osd = dyn_cast<OutputDesc>(b);
117681ad6265SDimitry Andric   return (osd && osd->osec.hasInputSections)
117706c3fb27SDimitry Andric              ? llvm::countl_zero(a->sortRank ^ osd->osec.sortRank)
117881ad6265SDimitry Andric              : -1;
11790b57cec5SDimitry Andric }
11800b57cec5SDimitry Andric 
11810b57cec5SDimitry Andric // When placing orphan sections, we want to place them after symbol assignments
11820b57cec5SDimitry Andric // so that an orphan after
11830b57cec5SDimitry Andric //   begin_foo = .;
11840b57cec5SDimitry Andric //   foo : { *(foo) }
11850b57cec5SDimitry Andric //   end_foo = .;
11860b57cec5SDimitry Andric // doesn't break the intended meaning of the begin/end symbols.
11870b57cec5SDimitry Andric // We don't want to go over sections since findOrphanPos is the
11880b57cec5SDimitry Andric // one in charge of deciding the order of the sections.
11890b57cec5SDimitry Andric // We don't want to go over changes to '.', since doing so in
11900b57cec5SDimitry Andric //  rx_sec : { *(rx_sec) }
11910b57cec5SDimitry Andric //  . = ALIGN(0x1000);
11920b57cec5SDimitry Andric //  /* The RW PT_LOAD starts here*/
11930b57cec5SDimitry Andric //  rw_sec : { *(rw_sec) }
11940b57cec5SDimitry Andric // would mean that the RW PT_LOAD would become unaligned.
11954824e7fdSDimitry Andric static bool shouldSkip(SectionCommand *cmd) {
11960b57cec5SDimitry Andric   if (auto *assign = dyn_cast<SymbolAssignment>(cmd))
11970b57cec5SDimitry Andric     return assign->name != ".";
11980b57cec5SDimitry Andric   return false;
11990b57cec5SDimitry Andric }
12000b57cec5SDimitry Andric 
12010b57cec5SDimitry Andric // We want to place orphan sections so that they share as much
12020b57cec5SDimitry Andric // characteristics with their neighbors as possible. For example, if
12030b57cec5SDimitry Andric // both are rw, or both are tls.
120404eeddc0SDimitry Andric static SmallVectorImpl<SectionCommand *>::iterator
120504eeddc0SDimitry Andric findOrphanPos(SmallVectorImpl<SectionCommand *>::iterator b,
120604eeddc0SDimitry Andric               SmallVectorImpl<SectionCommand *>::iterator e) {
120781ad6265SDimitry Andric   OutputSection *sec = &cast<OutputDesc>(*e)->osec;
12080b57cec5SDimitry Andric 
12095f757f3fSDimitry Andric   // As a special case, place .relro_padding before the SymbolAssignment using
12105f757f3fSDimitry Andric   // DATA_SEGMENT_RELRO_END, if present.
12115f757f3fSDimitry Andric   if (in.relroPadding && sec == in.relroPadding->getParent()) {
12125f757f3fSDimitry Andric     auto i = std::find_if(b, e, [=](SectionCommand *a) {
12135f757f3fSDimitry Andric       if (auto *assign = dyn_cast<SymbolAssignment>(a))
12145f757f3fSDimitry Andric         return assign->dataSegmentRelroEnd;
12155f757f3fSDimitry Andric       return false;
12165f757f3fSDimitry Andric     });
12175f757f3fSDimitry Andric     if (i != e)
12185f757f3fSDimitry Andric       return i;
12195f757f3fSDimitry Andric   }
12205f757f3fSDimitry Andric 
12210b57cec5SDimitry Andric   // Find the first element that has as close a rank as possible.
12224824e7fdSDimitry Andric   auto i = std::max_element(b, e, [=](SectionCommand *a, SectionCommand *b) {
12230b57cec5SDimitry Andric     return getRankProximity(sec, a) < getRankProximity(sec, b);
12240b57cec5SDimitry Andric   });
12250b57cec5SDimitry Andric   if (i == e)
12260b57cec5SDimitry Andric     return e;
122781ad6265SDimitry Andric   if (!isa<OutputDesc>(*i))
1228349cc55cSDimitry Andric     return e;
122981ad6265SDimitry Andric   auto foundSec = &cast<OutputDesc>(*i)->osec;
12300b57cec5SDimitry Andric 
12310b57cec5SDimitry Andric   // Consider all existing sections with the same proximity.
12320b57cec5SDimitry Andric   int proximity = getRankProximity(sec, *i);
1233349cc55cSDimitry Andric   unsigned sortRank = sec->sortRank;
1234349cc55cSDimitry Andric   if (script->hasPhdrsCommands() || !script->memoryRegions.empty())
1235349cc55cSDimitry Andric     // Prevent the orphan section to be placed before the found section. If
1236349cc55cSDimitry Andric     // custom program headers are defined, that helps to avoid adding it to a
1237349cc55cSDimitry Andric     // previous segment and changing flags of that segment, for example, making
1238349cc55cSDimitry Andric     // a read-only segment writable. If memory regions are defined, an orphan
1239349cc55cSDimitry Andric     // section should continue the same region as the found section to better
1240349cc55cSDimitry Andric     // resemble the behavior of GNU ld.
1241349cc55cSDimitry Andric     sortRank = std::max(sortRank, foundSec->sortRank);
12420b57cec5SDimitry Andric   for (; i != e; ++i) {
124381ad6265SDimitry Andric     auto *curSecDesc = dyn_cast<OutputDesc>(*i);
124481ad6265SDimitry Andric     if (!curSecDesc || !curSecDesc->osec.hasInputSections)
12450b57cec5SDimitry Andric       continue;
124681ad6265SDimitry Andric     if (getRankProximity(sec, curSecDesc) != proximity ||
124781ad6265SDimitry Andric         sortRank < curSecDesc->osec.sortRank)
12480b57cec5SDimitry Andric       break;
12490b57cec5SDimitry Andric   }
12500b57cec5SDimitry Andric 
12514824e7fdSDimitry Andric   auto isOutputSecWithInputSections = [](SectionCommand *cmd) {
125281ad6265SDimitry Andric     auto *osd = dyn_cast<OutputDesc>(cmd);
125381ad6265SDimitry Andric     return osd && osd->osec.hasInputSections;
12540b57cec5SDimitry Andric   };
125504eeddc0SDimitry Andric   auto j =
125604eeddc0SDimitry Andric       std::find_if(std::make_reverse_iterator(i), std::make_reverse_iterator(b),
12570b57cec5SDimitry Andric                    isOutputSecWithInputSections);
12580b57cec5SDimitry Andric   i = j.base();
12590b57cec5SDimitry Andric 
12600b57cec5SDimitry Andric   // As a special case, if the orphan section is the last section, put
12610b57cec5SDimitry Andric   // it at the very end, past any other commands.
12620b57cec5SDimitry Andric   // This matches bfd's behavior and is convenient when the linker script fully
12630b57cec5SDimitry Andric   // specifies the start of the file, but doesn't care about the end (the non
12640b57cec5SDimitry Andric   // alloc sections for example).
12650b57cec5SDimitry Andric   auto nextSec = std::find_if(i, e, isOutputSecWithInputSections);
12660b57cec5SDimitry Andric   if (nextSec == e)
12670b57cec5SDimitry Andric     return e;
12680b57cec5SDimitry Andric 
12690b57cec5SDimitry Andric   while (i != e && shouldSkip(*i))
12700b57cec5SDimitry Andric     ++i;
12710b57cec5SDimitry Andric   return i;
12720b57cec5SDimitry Andric }
12730b57cec5SDimitry Andric 
12745ffd83dbSDimitry Andric // Adds random priorities to sections not already in the map.
12755ffd83dbSDimitry Andric static void maybeShuffle(DenseMap<const InputSectionBase *, int> &order) {
1276fe6060f1SDimitry Andric   if (config->shuffleSections.empty())
12775ffd83dbSDimitry Andric     return;
12785ffd83dbSDimitry Andric 
1279bdd1243dSDimitry Andric   SmallVector<InputSectionBase *, 0> matched, sections = ctx.inputSections;
1280fe6060f1SDimitry Andric   matched.reserve(sections.size());
1281fe6060f1SDimitry Andric   for (const auto &patAndSeed : config->shuffleSections) {
1282fe6060f1SDimitry Andric     matched.clear();
1283fe6060f1SDimitry Andric     for (InputSectionBase *sec : sections)
1284fe6060f1SDimitry Andric       if (patAndSeed.first.match(sec->name))
1285fe6060f1SDimitry Andric         matched.push_back(sec);
1286fe6060f1SDimitry Andric     const uint32_t seed = patAndSeed.second;
1287fe6060f1SDimitry Andric     if (seed == UINT32_MAX) {
1288fe6060f1SDimitry Andric       // If --shuffle-sections <section-glob>=-1, reverse the section order. The
1289fe6060f1SDimitry Andric       // section order is stable even if the number of sections changes. This is
1290fe6060f1SDimitry Andric       // useful to catch issues like static initialization order fiasco
1291fe6060f1SDimitry Andric       // reliably.
1292fe6060f1SDimitry Andric       std::reverse(matched.begin(), matched.end());
1293fe6060f1SDimitry Andric     } else {
1294fe6060f1SDimitry Andric       std::mt19937 g(seed ? seed : std::random_device()());
1295fe6060f1SDimitry Andric       llvm::shuffle(matched.begin(), matched.end(), g);
1296fe6060f1SDimitry Andric     }
1297fe6060f1SDimitry Andric     size_t i = 0;
1298fe6060f1SDimitry Andric     for (InputSectionBase *&sec : sections)
1299fe6060f1SDimitry Andric       if (patAndSeed.first.match(sec->name))
1300fe6060f1SDimitry Andric         sec = matched[i++];
1301fe6060f1SDimitry Andric   }
1302fe6060f1SDimitry Andric 
13035ffd83dbSDimitry Andric   // Existing priorities are < 0, so use priorities >= 0 for the missing
13045ffd83dbSDimitry Andric   // sections.
1305fe6060f1SDimitry Andric   int prio = 0;
1306fe6060f1SDimitry Andric   for (InputSectionBase *sec : sections) {
1307fe6060f1SDimitry Andric     if (order.try_emplace(sec, prio).second)
1308fe6060f1SDimitry Andric       ++prio;
13095ffd83dbSDimitry Andric   }
13105ffd83dbSDimitry Andric }
13115ffd83dbSDimitry Andric 
13120b57cec5SDimitry Andric // Builds section order for handling --symbol-ordering-file.
13130b57cec5SDimitry Andric static DenseMap<const InputSectionBase *, int> buildSectionOrder() {
13140b57cec5SDimitry Andric   DenseMap<const InputSectionBase *, int> sectionOrder;
1315349cc55cSDimitry Andric   // Use the rarely used option --call-graph-ordering-file to sort sections.
13160b57cec5SDimitry Andric   if (!config->callGraphProfile.empty())
13170b57cec5SDimitry Andric     return computeCallGraphProfileOrder();
13180b57cec5SDimitry Andric 
13190b57cec5SDimitry Andric   if (config->symbolOrderingFile.empty())
13200b57cec5SDimitry Andric     return sectionOrder;
13210b57cec5SDimitry Andric 
13220b57cec5SDimitry Andric   struct SymbolOrderEntry {
13230b57cec5SDimitry Andric     int priority;
13240b57cec5SDimitry Andric     bool present;
13250b57cec5SDimitry Andric   };
13260b57cec5SDimitry Andric 
13270b57cec5SDimitry Andric   // Build a map from symbols to their priorities. Symbols that didn't
13280b57cec5SDimitry Andric   // appear in the symbol ordering file have the lowest priority 0.
13290b57cec5SDimitry Andric   // All explicitly mentioned symbols have negative (higher) priorities.
133004eeddc0SDimitry Andric   DenseMap<CachedHashStringRef, SymbolOrderEntry> symbolOrder;
13310b57cec5SDimitry Andric   int priority = -config->symbolOrderingFile.size();
13320b57cec5SDimitry Andric   for (StringRef s : config->symbolOrderingFile)
133304eeddc0SDimitry Andric     symbolOrder.insert({CachedHashStringRef(s), {priority++, false}});
13340b57cec5SDimitry Andric 
13350b57cec5SDimitry Andric   // Build a map from sections to their priorities.
13360b57cec5SDimitry Andric   auto addSym = [&](Symbol &sym) {
133704eeddc0SDimitry Andric     auto it = symbolOrder.find(CachedHashStringRef(sym.getName()));
13380b57cec5SDimitry Andric     if (it == symbolOrder.end())
13390b57cec5SDimitry Andric       return;
13400b57cec5SDimitry Andric     SymbolOrderEntry &ent = it->second;
13410b57cec5SDimitry Andric     ent.present = true;
13420b57cec5SDimitry Andric 
13430b57cec5SDimitry Andric     maybeWarnUnorderableSymbol(&sym);
13440b57cec5SDimitry Andric 
13450b57cec5SDimitry Andric     if (auto *d = dyn_cast<Defined>(&sym)) {
13460b57cec5SDimitry Andric       if (auto *sec = dyn_cast_or_null<InputSectionBase>(d->section)) {
13470eae32dcSDimitry Andric         int &priority = sectionOrder[cast<InputSectionBase>(sec)];
13480b57cec5SDimitry Andric         priority = std::min(priority, ent.priority);
13490b57cec5SDimitry Andric       }
13500b57cec5SDimitry Andric     }
13510b57cec5SDimitry Andric   };
13520b57cec5SDimitry Andric 
13530b57cec5SDimitry Andric   // We want both global and local symbols. We get the global ones from the
13540b57cec5SDimitry Andric   // symbol table and iterate the object files for the local ones.
1355bdd1243dSDimitry Andric   for (Symbol *sym : symtab.getSymbols())
13560b57cec5SDimitry Andric     addSym(*sym);
13570b57cec5SDimitry Andric 
1358bdd1243dSDimitry Andric   for (ELFFileBase *file : ctx.objectFiles)
135904eeddc0SDimitry Andric     for (Symbol *sym : file->getLocalSymbols())
13600b57cec5SDimitry Andric       addSym(*sym);
13610b57cec5SDimitry Andric 
13620b57cec5SDimitry Andric   if (config->warnSymbolOrdering)
13630b57cec5SDimitry Andric     for (auto orderEntry : symbolOrder)
13640b57cec5SDimitry Andric       if (!orderEntry.second.present)
136504eeddc0SDimitry Andric         warn("symbol ordering file: no such symbol: " + orderEntry.first.val());
13660b57cec5SDimitry Andric 
13670b57cec5SDimitry Andric   return sectionOrder;
13680b57cec5SDimitry Andric }
13690b57cec5SDimitry Andric 
13700b57cec5SDimitry Andric // Sorts the sections in ISD according to the provided section order.
13710b57cec5SDimitry Andric static void
13720b57cec5SDimitry Andric sortISDBySectionOrder(InputSectionDescription *isd,
1373753f127fSDimitry Andric                       const DenseMap<const InputSectionBase *, int> &order,
1374753f127fSDimitry Andric                       bool executableOutputSection) {
137504eeddc0SDimitry Andric   SmallVector<InputSection *, 0> unorderedSections;
137604eeddc0SDimitry Andric   SmallVector<std::pair<InputSection *, int>, 0> orderedSections;
13770b57cec5SDimitry Andric   uint64_t unorderedSize = 0;
1378753f127fSDimitry Andric   uint64_t totalSize = 0;
13790b57cec5SDimitry Andric 
13800b57cec5SDimitry Andric   for (InputSection *isec : isd->sections) {
1381753f127fSDimitry Andric     if (executableOutputSection)
1382753f127fSDimitry Andric       totalSize += isec->getSize();
13830b57cec5SDimitry Andric     auto i = order.find(isec);
13840b57cec5SDimitry Andric     if (i == order.end()) {
13850b57cec5SDimitry Andric       unorderedSections.push_back(isec);
13860b57cec5SDimitry Andric       unorderedSize += isec->getSize();
13870b57cec5SDimitry Andric       continue;
13880b57cec5SDimitry Andric     }
13890b57cec5SDimitry Andric     orderedSections.push_back({isec, i->second});
13900b57cec5SDimitry Andric   }
139185868e8aSDimitry Andric   llvm::sort(orderedSections, llvm::less_second());
13920b57cec5SDimitry Andric 
13930b57cec5SDimitry Andric   // Find an insertion point for the ordered section list in the unordered
13940b57cec5SDimitry Andric   // section list. On targets with limited-range branches, this is the mid-point
13950b57cec5SDimitry Andric   // of the unordered section list. This decreases the likelihood that a range
13960b57cec5SDimitry Andric   // extension thunk will be needed to enter or exit the ordered region. If the
13970b57cec5SDimitry Andric   // ordered section list is a list of hot functions, we can generally expect
13980b57cec5SDimitry Andric   // the ordered functions to be called more often than the unordered functions,
13990b57cec5SDimitry Andric   // making it more likely that any particular call will be within range, and
14000b57cec5SDimitry Andric   // therefore reducing the number of thunks required.
14010b57cec5SDimitry Andric   //
14020b57cec5SDimitry Andric   // For example, imagine that you have 8MB of hot code and 32MB of cold code.
14030b57cec5SDimitry Andric   // If the layout is:
14040b57cec5SDimitry Andric   //
14050b57cec5SDimitry Andric   // 8MB hot
14060b57cec5SDimitry Andric   // 32MB cold
14070b57cec5SDimitry Andric   //
14080b57cec5SDimitry Andric   // only the first 8-16MB of the cold code (depending on which hot function it
14090b57cec5SDimitry Andric   // is actually calling) can call the hot code without a range extension thunk.
14100b57cec5SDimitry Andric   // However, if we use this layout:
14110b57cec5SDimitry Andric   //
14120b57cec5SDimitry Andric   // 16MB cold
14130b57cec5SDimitry Andric   // 8MB hot
14140b57cec5SDimitry Andric   // 16MB cold
14150b57cec5SDimitry Andric   //
14160b57cec5SDimitry Andric   // both the last 8-16MB of the first block of cold code and the first 8-16MB
14170b57cec5SDimitry Andric   // of the second block of cold code can call the hot code without a thunk. So
14180b57cec5SDimitry Andric   // we effectively double the amount of code that could potentially call into
14190b57cec5SDimitry Andric   // the hot code without a thunk.
1420753f127fSDimitry Andric   //
1421753f127fSDimitry Andric   // The above is not necessary if total size of input sections in this "isd"
1422753f127fSDimitry Andric   // is small. Note that we assume all input sections are executable if the
1423753f127fSDimitry Andric   // output section is executable (which is not always true but supposed to
1424753f127fSDimitry Andric   // cover most cases).
14250b57cec5SDimitry Andric   size_t insPt = 0;
1426753f127fSDimitry Andric   if (executableOutputSection && !orderedSections.empty() &&
1427753f127fSDimitry Andric       target->getThunkSectionSpacing() &&
1428753f127fSDimitry Andric       totalSize >= target->getThunkSectionSpacing()) {
14290b57cec5SDimitry Andric     uint64_t unorderedPos = 0;
14300b57cec5SDimitry Andric     for (; insPt != unorderedSections.size(); ++insPt) {
14310b57cec5SDimitry Andric       unorderedPos += unorderedSections[insPt]->getSize();
14320b57cec5SDimitry Andric       if (unorderedPos > unorderedSize / 2)
14330b57cec5SDimitry Andric         break;
14340b57cec5SDimitry Andric     }
14350b57cec5SDimitry Andric   }
14360b57cec5SDimitry Andric 
14370b57cec5SDimitry Andric   isd->sections.clear();
1438bdd1243dSDimitry Andric   for (InputSection *isec : ArrayRef(unorderedSections).slice(0, insPt))
14390b57cec5SDimitry Andric     isd->sections.push_back(isec);
14400b57cec5SDimitry Andric   for (std::pair<InputSection *, int> p : orderedSections)
14410b57cec5SDimitry Andric     isd->sections.push_back(p.first);
1442bdd1243dSDimitry Andric   for (InputSection *isec : ArrayRef(unorderedSections).slice(insPt))
14430b57cec5SDimitry Andric     isd->sections.push_back(isec);
14440b57cec5SDimitry Andric }
14450b57cec5SDimitry Andric 
144681ad6265SDimitry Andric static void sortSection(OutputSection &osec,
14470b57cec5SDimitry Andric                         const DenseMap<const InputSectionBase *, int> &order) {
144881ad6265SDimitry Andric   StringRef name = osec.name;
14490b57cec5SDimitry Andric 
14505ffd83dbSDimitry Andric   // Never sort these.
14515ffd83dbSDimitry Andric   if (name == ".init" || name == ".fini")
14525ffd83dbSDimitry Andric     return;
14535ffd83dbSDimitry Andric 
1454e8d8bef9SDimitry Andric   // IRelative relocations that usually live in the .rel[a].dyn section should
1455fe6060f1SDimitry Andric   // be processed last by the dynamic loader. To achieve that we add synthetic
1456fe6060f1SDimitry Andric   // sections in the required order from the beginning so that the in.relaIplt
1457e8d8bef9SDimitry Andric   // section is placed last in an output section. Here we just do not apply
1458e8d8bef9SDimitry Andric   // sorting for an output section which holds the in.relaIplt section.
145981ad6265SDimitry Andric   if (in.relaIplt->getParent() == &osec)
1460e8d8bef9SDimitry Andric     return;
1461e8d8bef9SDimitry Andric 
14625ffd83dbSDimitry Andric   // Sort input sections by priority using the list provided by
14635ffd83dbSDimitry Andric   // --symbol-ordering-file or --shuffle-sections=. This is a least significant
14645ffd83dbSDimitry Andric   // digit radix sort. The sections may be sorted stably again by a more
14655ffd83dbSDimitry Andric   // significant key.
14665ffd83dbSDimitry Andric   if (!order.empty())
146781ad6265SDimitry Andric     for (SectionCommand *b : osec.commands)
14685ffd83dbSDimitry Andric       if (auto *isd = dyn_cast<InputSectionDescription>(b))
1469753f127fSDimitry Andric         sortISDBySectionOrder(isd, order, osec.flags & SHF_EXECINSTR);
14705ffd83dbSDimitry Andric 
1471349cc55cSDimitry Andric   if (script->hasSectionsCommand)
1472349cc55cSDimitry Andric     return;
1473349cc55cSDimitry Andric 
14740b57cec5SDimitry Andric   if (name == ".init_array" || name == ".fini_array") {
147581ad6265SDimitry Andric     osec.sortInitFini();
1476349cc55cSDimitry Andric   } else if (name == ".ctors" || name == ".dtors") {
147781ad6265SDimitry Andric     osec.sortCtorsDtors();
1478349cc55cSDimitry Andric   } else if (config->emachine == EM_PPC64 && name == ".toc") {
14790b57cec5SDimitry Andric     // .toc is allocated just after .got and is accessed using GOT-relative
14800b57cec5SDimitry Andric     // relocations. Object files compiled with small code model have an
14810b57cec5SDimitry Andric     // addressable range of [.got, .got + 0xFFFC] for GOT-relative relocations.
1482349cc55cSDimitry Andric     // To reduce the risk of relocation overflow, .toc contents are sorted so
1483349cc55cSDimitry Andric     // that sections having smaller relocation offsets are at beginning of .toc
148481ad6265SDimitry Andric     assert(osec.commands.size() == 1);
148581ad6265SDimitry Andric     auto *isd = cast<InputSectionDescription>(osec.commands[0]);
14860b57cec5SDimitry Andric     llvm::stable_sort(isd->sections,
14870b57cec5SDimitry Andric                       [](const InputSection *a, const InputSection *b) -> bool {
14880b57cec5SDimitry Andric                         return a->file->ppc64SmallCodeModelTocRelocs &&
14890b57cec5SDimitry Andric                                !b->file->ppc64SmallCodeModelTocRelocs;
14900b57cec5SDimitry Andric                       });
14910b57cec5SDimitry Andric   }
14920b57cec5SDimitry Andric }
14930b57cec5SDimitry Andric 
14940b57cec5SDimitry Andric // If no layout was provided by linker script, we want to apply default
14950b57cec5SDimitry Andric // sorting for special input sections. This also handles --symbol-ordering-file.
14960b57cec5SDimitry Andric template <class ELFT> void Writer<ELFT>::sortInputSections() {
14970b57cec5SDimitry Andric   // Build the order once since it is expensive.
14980b57cec5SDimitry Andric   DenseMap<const InputSectionBase *, int> order = buildSectionOrder();
14995ffd83dbSDimitry Andric   maybeShuffle(order);
15004824e7fdSDimitry Andric   for (SectionCommand *cmd : script->sectionCommands)
150181ad6265SDimitry Andric     if (auto *osd = dyn_cast<OutputDesc>(cmd))
150281ad6265SDimitry Andric       sortSection(osd->osec, order);
15030b57cec5SDimitry Andric }
15040b57cec5SDimitry Andric 
15050b57cec5SDimitry Andric template <class ELFT> void Writer<ELFT>::sortSections() {
1506e8d8bef9SDimitry Andric   llvm::TimeTraceScope timeScope("Sort sections");
15070b57cec5SDimitry Andric 
15080b57cec5SDimitry Andric   // Don't sort if using -r. It is not necessary and we want to preserve the
15090b57cec5SDimitry Andric   // relative order for SHF_LINK_ORDER sections.
15101fd87a68SDimitry Andric   if (config->relocatable) {
15111fd87a68SDimitry Andric     script->adjustOutputSections();
15120b57cec5SDimitry Andric     return;
15131fd87a68SDimitry Andric   }
15140b57cec5SDimitry Andric 
15150b57cec5SDimitry Andric   sortInputSections();
15160b57cec5SDimitry Andric 
15171fd87a68SDimitry Andric   for (SectionCommand *cmd : script->sectionCommands)
151881ad6265SDimitry Andric     if (auto *osd = dyn_cast<OutputDesc>(cmd))
151981ad6265SDimitry Andric       osd->osec.sortRank = getSectionRank(osd->osec);
15200b57cec5SDimitry Andric   if (!script->hasSectionsCommand) {
1521*b3edf446SDimitry Andric     // OutputDescs are mostly contiguous, but may be interleaved with
1522*b3edf446SDimitry Andric     // SymbolAssignments in the presence of INSERT commands.
1523*b3edf446SDimitry Andric     auto mid = std::stable_partition(
1524*b3edf446SDimitry Andric         script->sectionCommands.begin(), script->sectionCommands.end(),
1525*b3edf446SDimitry Andric         [](SectionCommand *cmd) { return isa<OutputDesc>(cmd); });
1526*b3edf446SDimitry Andric     std::stable_sort(script->sectionCommands.begin(), mid, compareSections);
15270b57cec5SDimitry Andric   }
15280b57cec5SDimitry Andric 
15291fd87a68SDimitry Andric   // Process INSERT commands and update output section attributes. From this
15301fd87a68SDimitry Andric   // point onwards the order of script->sectionCommands is fixed.
15315ffd83dbSDimitry Andric   script->processInsertCommands();
15321fd87a68SDimitry Andric   script->adjustOutputSections();
15331fd87a68SDimitry Andric 
153406c3fb27SDimitry Andric   if (script->hasSectionsCommand)
153506c3fb27SDimitry Andric     sortOrphanSections();
15365ffd83dbSDimitry Andric 
153706c3fb27SDimitry Andric   script->adjustSectionsAfterSorting();
153806c3fb27SDimitry Andric }
153906c3fb27SDimitry Andric 
154006c3fb27SDimitry Andric template <class ELFT> void Writer<ELFT>::sortOrphanSections() {
15410b57cec5SDimitry Andric   // Orphan sections are sections present in the input files which are
15420b57cec5SDimitry Andric   // not explicitly placed into the output file by the linker script.
15430b57cec5SDimitry Andric   //
15440b57cec5SDimitry Andric   // The sections in the linker script are already in the correct
15450b57cec5SDimitry Andric   // order. We have to figuere out where to insert the orphan
15460b57cec5SDimitry Andric   // sections.
15470b57cec5SDimitry Andric   //
15480b57cec5SDimitry Andric   // The order of the sections in the script is arbitrary and may not agree with
15490b57cec5SDimitry Andric   // compareSections. This means that we cannot easily define a strict weak
15500b57cec5SDimitry Andric   // ordering. To see why, consider a comparison of a section in the script and
15510b57cec5SDimitry Andric   // one not in the script. We have a two simple options:
15520b57cec5SDimitry Andric   // * Make them equivalent (a is not less than b, and b is not less than a).
15530b57cec5SDimitry Andric   //   The problem is then that equivalence has to be transitive and we can
15540b57cec5SDimitry Andric   //   have sections a, b and c with only b in a script and a less than c
15550b57cec5SDimitry Andric   //   which breaks this property.
15560b57cec5SDimitry Andric   // * Use compareSectionsNonScript. Given that the script order doesn't have
15570b57cec5SDimitry Andric   //   to match, we can end up with sections a, b, c, d where b and c are in the
15580b57cec5SDimitry Andric   //   script and c is compareSectionsNonScript less than b. In which case d
15590b57cec5SDimitry Andric   //   can be equivalent to c, a to b and d < a. As a concrete example:
15600b57cec5SDimitry Andric   //   .a (rx) # not in script
15610b57cec5SDimitry Andric   //   .b (rx) # in script
15620b57cec5SDimitry Andric   //   .c (ro) # in script
15630b57cec5SDimitry Andric   //   .d (ro) # not in script
15640b57cec5SDimitry Andric   //
15650b57cec5SDimitry Andric   // The way we define an order then is:
15660b57cec5SDimitry Andric   // *  Sort only the orphan sections. They are in the end right now.
15670b57cec5SDimitry Andric   // *  Move each orphan section to its preferred position. We try
15680b57cec5SDimitry Andric   //    to put each section in the last position where it can share
15690b57cec5SDimitry Andric   //    a PT_LOAD.
15700b57cec5SDimitry Andric   //
15710b57cec5SDimitry Andric   // There is some ambiguity as to where exactly a new entry should be
15720b57cec5SDimitry Andric   // inserted, because Commands contains not only output section
15730b57cec5SDimitry Andric   // commands but also other types of commands such as symbol assignment
15740b57cec5SDimitry Andric   // expressions. There's no correct answer here due to the lack of the
15750b57cec5SDimitry Andric   // formal specification of the linker script. We use heuristics to
15760b57cec5SDimitry Andric   // determine whether a new output command should be added before or
15770b57cec5SDimitry Andric   // after another commands. For the details, look at shouldSkip
15780b57cec5SDimitry Andric   // function.
15790b57cec5SDimitry Andric 
15800b57cec5SDimitry Andric   auto i = script->sectionCommands.begin();
15810b57cec5SDimitry Andric   auto e = script->sectionCommands.end();
15824824e7fdSDimitry Andric   auto nonScriptI = std::find_if(i, e, [](SectionCommand *cmd) {
158381ad6265SDimitry Andric     if (auto *osd = dyn_cast<OutputDesc>(cmd))
158481ad6265SDimitry Andric       return osd->osec.sectionIndex == UINT32_MAX;
15850b57cec5SDimitry Andric     return false;
15860b57cec5SDimitry Andric   });
15870b57cec5SDimitry Andric 
15880b57cec5SDimitry Andric   // Sort the orphan sections.
15890b57cec5SDimitry Andric   std::stable_sort(nonScriptI, e, compareSections);
15900b57cec5SDimitry Andric 
15910b57cec5SDimitry Andric   // As a horrible special case, skip the first . assignment if it is before any
15920b57cec5SDimitry Andric   // section. We do this because it is common to set a load address by starting
15930b57cec5SDimitry Andric   // the script with ". = 0xabcd" and the expectation is that every section is
15940b57cec5SDimitry Andric   // after that.
15950b57cec5SDimitry Andric   auto firstSectionOrDotAssignment =
15964824e7fdSDimitry Andric       std::find_if(i, e, [](SectionCommand *cmd) { return !shouldSkip(cmd); });
15970b57cec5SDimitry Andric   if (firstSectionOrDotAssignment != e &&
15980b57cec5SDimitry Andric       isa<SymbolAssignment>(**firstSectionOrDotAssignment))
15990b57cec5SDimitry Andric     ++firstSectionOrDotAssignment;
16000b57cec5SDimitry Andric   i = firstSectionOrDotAssignment;
16010b57cec5SDimitry Andric 
16020b57cec5SDimitry Andric   while (nonScriptI != e) {
16030b57cec5SDimitry Andric     auto pos = findOrphanPos(i, nonScriptI);
160481ad6265SDimitry Andric     OutputSection *orphan = &cast<OutputDesc>(*nonScriptI)->osec;
16050b57cec5SDimitry Andric 
16060b57cec5SDimitry Andric     // As an optimization, find all sections with the same sort rank
16070b57cec5SDimitry Andric     // and insert them with one rotate.
16080b57cec5SDimitry Andric     unsigned rank = orphan->sortRank;
16094824e7fdSDimitry Andric     auto end = std::find_if(nonScriptI + 1, e, [=](SectionCommand *cmd) {
161081ad6265SDimitry Andric       return cast<OutputDesc>(cmd)->osec.sortRank != rank;
16110b57cec5SDimitry Andric     });
16120b57cec5SDimitry Andric     std::rotate(pos, nonScriptI, end);
16130b57cec5SDimitry Andric     nonScriptI = end;
16140b57cec5SDimitry Andric   }
16150b57cec5SDimitry Andric }
16160b57cec5SDimitry Andric 
16170b57cec5SDimitry Andric static bool compareByFilePosition(InputSection *a, InputSection *b) {
1618e8d8bef9SDimitry Andric   InputSection *la = a->flags & SHF_LINK_ORDER ? a->getLinkOrderDep() : nullptr;
1619e8d8bef9SDimitry Andric   InputSection *lb = b->flags & SHF_LINK_ORDER ? b->getLinkOrderDep() : nullptr;
1620e8d8bef9SDimitry Andric   // SHF_LINK_ORDER sections with non-zero sh_link are ordered before
1621e8d8bef9SDimitry Andric   // non-SHF_LINK_ORDER sections and SHF_LINK_ORDER sections with zero sh_link.
1622e8d8bef9SDimitry Andric   if (!la || !lb)
1623e8d8bef9SDimitry Andric     return la && !lb;
16240b57cec5SDimitry Andric   OutputSection *aOut = la->getParent();
16250b57cec5SDimitry Andric   OutputSection *bOut = lb->getParent();
16260b57cec5SDimitry Andric 
16270b57cec5SDimitry Andric   if (aOut != bOut)
16285ffd83dbSDimitry Andric     return aOut->addr < bOut->addr;
16290b57cec5SDimitry Andric   return la->outSecOff < lb->outSecOff;
16300b57cec5SDimitry Andric }
16310b57cec5SDimitry Andric 
16320b57cec5SDimitry Andric template <class ELFT> void Writer<ELFT>::resolveShfLinkOrder() {
1633e8d8bef9SDimitry Andric   llvm::TimeTraceScope timeScope("Resolve SHF_LINK_ORDER");
16340b57cec5SDimitry Andric   for (OutputSection *sec : outputSections) {
16350b57cec5SDimitry Andric     if (!(sec->flags & SHF_LINK_ORDER))
16360b57cec5SDimitry Andric       continue;
16370b57cec5SDimitry Andric 
163885868e8aSDimitry Andric     // The ARM.exidx section use SHF_LINK_ORDER, but we have consolidated
163985868e8aSDimitry Andric     // this processing inside the ARMExidxsyntheticsection::finalizeContents().
164085868e8aSDimitry Andric     if (!config->relocatable && config->emachine == EM_ARM &&
164185868e8aSDimitry Andric         sec->type == SHT_ARM_EXIDX)
164285868e8aSDimitry Andric       continue;
164385868e8aSDimitry Andric 
1644e8d8bef9SDimitry Andric     // Link order may be distributed across several InputSectionDescriptions.
1645e8d8bef9SDimitry Andric     // Sorting is performed separately.
16461fd87a68SDimitry Andric     SmallVector<InputSection **, 0> scriptSections;
16471fd87a68SDimitry Andric     SmallVector<InputSection *, 0> sections;
16484824e7fdSDimitry Andric     for (SectionCommand *cmd : sec->commands) {
16494824e7fdSDimitry Andric       auto *isd = dyn_cast<InputSectionDescription>(cmd);
1650e8d8bef9SDimitry Andric       if (!isd)
1651e8d8bef9SDimitry Andric         continue;
1652e8d8bef9SDimitry Andric       bool hasLinkOrder = false;
1653e8d8bef9SDimitry Andric       scriptSections.clear();
1654e8d8bef9SDimitry Andric       sections.clear();
16550b57cec5SDimitry Andric       for (InputSection *&isec : isd->sections) {
1656e8d8bef9SDimitry Andric         if (isec->flags & SHF_LINK_ORDER) {
165785868e8aSDimitry Andric           InputSection *link = isec->getLinkOrderDep();
1658e8d8bef9SDimitry Andric           if (link && !link->getParent())
165985868e8aSDimitry Andric             error(toString(isec) + ": sh_link points to discarded section " +
166085868e8aSDimitry Andric                   toString(link));
1661e8d8bef9SDimitry Andric           hasLinkOrder = true;
16620b57cec5SDimitry Andric         }
1663e8d8bef9SDimitry Andric         scriptSections.push_back(&isec);
1664e8d8bef9SDimitry Andric         sections.push_back(isec);
16650b57cec5SDimitry Andric       }
1666e8d8bef9SDimitry Andric       if (hasLinkOrder && errorCount() == 0) {
16670b57cec5SDimitry Andric         llvm::stable_sort(sections, compareByFilePosition);
1668e8d8bef9SDimitry Andric         for (int i = 0, n = sections.size(); i != n; ++i)
16690b57cec5SDimitry Andric           *scriptSections[i] = sections[i];
16700b57cec5SDimitry Andric       }
16710b57cec5SDimitry Andric     }
1672e8d8bef9SDimitry Andric   }
1673e8d8bef9SDimitry Andric }
16740b57cec5SDimitry Andric 
16755ffd83dbSDimitry Andric static void finalizeSynthetic(SyntheticSection *sec) {
1676e8d8bef9SDimitry Andric   if (sec && sec->isNeeded() && sec->getParent()) {
1677e8d8bef9SDimitry Andric     llvm::TimeTraceScope timeScope("Finalize synthetic sections", sec->name);
16785ffd83dbSDimitry Andric     sec->finalizeContents();
16795ffd83dbSDimitry Andric   }
1680e8d8bef9SDimitry Andric }
16815ffd83dbSDimitry Andric 
16820b57cec5SDimitry Andric // We need to generate and finalize the content that depends on the address of
16830b57cec5SDimitry Andric // InputSections. As the generation of the content may also alter InputSection
16840b57cec5SDimitry Andric // addresses we must converge to a fixed point. We do that here. See the comment
16850b57cec5SDimitry Andric // in Writer<ELFT>::finalizeSections().
16860b57cec5SDimitry Andric template <class ELFT> void Writer<ELFT>::finalizeAddressDependentContent() {
1687e8d8bef9SDimitry Andric   llvm::TimeTraceScope timeScope("Finalize address dependent content");
16880b57cec5SDimitry Andric   ThunkCreator tc;
16890b57cec5SDimitry Andric   AArch64Err843419Patcher a64p;
169085868e8aSDimitry Andric   ARMErr657417Patcher a32p;
16910b57cec5SDimitry Andric   script->assignAddresses();
16925ffd83dbSDimitry Andric   // .ARM.exidx and SHF_LINK_ORDER do not require precise addresses, but they
16935ffd83dbSDimitry Andric   // do require the relative addresses of OutputSections because linker scripts
16945ffd83dbSDimitry Andric   // can assign Virtual Addresses to OutputSections that are not monotonically
16955ffd83dbSDimitry Andric   // increasing.
16965ffd83dbSDimitry Andric   for (Partition &part : partitions)
169704eeddc0SDimitry Andric     finalizeSynthetic(part.armExidx.get());
16985ffd83dbSDimitry Andric   resolveShfLinkOrder();
16995ffd83dbSDimitry Andric 
17005ffd83dbSDimitry Andric   // Converts call x@GDPLT to call __tls_get_addr
17015ffd83dbSDimitry Andric   if (config->emachine == EM_HEXAGON)
17025ffd83dbSDimitry Andric     hexagonTLSSymbolUpdate(outputSections);
17030b57cec5SDimitry Andric 
1704753f127fSDimitry Andric   uint32_t pass = 0, assignPasses = 0;
170585868e8aSDimitry Andric   for (;;) {
1706753f127fSDimitry Andric     bool changed = target->needsThunks ? tc.createThunks(pass, outputSections)
1707753f127fSDimitry Andric                                        : target->relaxOnce(pass);
1708753f127fSDimitry Andric     ++pass;
170985868e8aSDimitry Andric 
171085868e8aSDimitry Andric     // With Thunk Size much smaller than branch range we expect to
171106c3fb27SDimitry Andric     // converge quickly; if we get to 30 something has gone wrong.
171206c3fb27SDimitry Andric     if (changed && pass >= 30) {
1713753f127fSDimitry Andric       error(target->needsThunks ? "thunk creation not converged"
1714753f127fSDimitry Andric                                 : "relaxation not converged");
171585868e8aSDimitry Andric       break;
171685868e8aSDimitry Andric     }
17170b57cec5SDimitry Andric 
17180b57cec5SDimitry Andric     if (config->fixCortexA53Errata843419) {
17190b57cec5SDimitry Andric       if (changed)
17200b57cec5SDimitry Andric         script->assignAddresses();
17210b57cec5SDimitry Andric       changed |= a64p.createFixes();
17220b57cec5SDimitry Andric     }
172385868e8aSDimitry Andric     if (config->fixCortexA8) {
172485868e8aSDimitry Andric       if (changed)
172585868e8aSDimitry Andric         script->assignAddresses();
172685868e8aSDimitry Andric       changed |= a32p.createFixes();
172785868e8aSDimitry Andric     }
17280b57cec5SDimitry Andric 
17295f757f3fSDimitry Andric     finalizeSynthetic(in.got.get());
17300b57cec5SDimitry Andric     if (in.mipsGot)
17310b57cec5SDimitry Andric       in.mipsGot->updateAllocSize();
17320b57cec5SDimitry Andric 
17330b57cec5SDimitry Andric     for (Partition &part : partitions) {
17340b57cec5SDimitry Andric       changed |= part.relaDyn->updateAllocSize();
17350b57cec5SDimitry Andric       if (part.relrDyn)
17360b57cec5SDimitry Andric         changed |= part.relrDyn->updateAllocSize();
17371db9f3b2SDimitry Andric       if (part.memtagGlobalDescriptors)
17381db9f3b2SDimitry Andric         changed |= part.memtagGlobalDescriptors->updateAllocSize();
17390b57cec5SDimitry Andric     }
17400b57cec5SDimitry Andric 
174185868e8aSDimitry Andric     const Defined *changedSym = script->assignAddresses();
174285868e8aSDimitry Andric     if (!changed) {
174385868e8aSDimitry Andric       // Some symbols may be dependent on section addresses. When we break the
174485868e8aSDimitry Andric       // loop, the symbol values are finalized because a previous
174585868e8aSDimitry Andric       // assignAddresses() finalized section addresses.
174685868e8aSDimitry Andric       if (!changedSym)
174785868e8aSDimitry Andric         break;
174885868e8aSDimitry Andric       if (++assignPasses == 5) {
174985868e8aSDimitry Andric         errorOrWarn("assignment to symbol " + toString(*changedSym) +
175085868e8aSDimitry Andric                     " does not converge");
175185868e8aSDimitry Andric         break;
175285868e8aSDimitry Andric       }
175385868e8aSDimitry Andric     }
17540b57cec5SDimitry Andric   }
1755753f127fSDimitry Andric   if (!config->relocatable && config->emachine == EM_RISCV)
1756753f127fSDimitry Andric     riscvFinalizeRelax(pass);
17575ffd83dbSDimitry Andric 
175804eeddc0SDimitry Andric   if (config->relocatable)
175904eeddc0SDimitry Andric     for (OutputSection *sec : outputSections)
176004eeddc0SDimitry Andric       sec->addr = 0;
176104eeddc0SDimitry Andric 
17625ffd83dbSDimitry Andric   // If addrExpr is set, the address may not be a multiple of the alignment.
17635ffd83dbSDimitry Andric   // Warn because this is error-prone.
17644824e7fdSDimitry Andric   for (SectionCommand *cmd : script->sectionCommands)
176581ad6265SDimitry Andric     if (auto *osd = dyn_cast<OutputDesc>(cmd)) {
176681ad6265SDimitry Andric       OutputSection *osec = &osd->osec;
1767bdd1243dSDimitry Andric       if (osec->addr % osec->addralign != 0)
176881ad6265SDimitry Andric         warn("address (0x" + Twine::utohexstr(osec->addr) + ") of section " +
176981ad6265SDimitry Andric              osec->name + " is not a multiple of alignment (" +
1770bdd1243dSDimitry Andric              Twine(osec->addralign) + ")");
177181ad6265SDimitry Andric     }
17720b57cec5SDimitry Andric }
17730b57cec5SDimitry Andric 
1774fe6060f1SDimitry Andric // If Input Sections have been shrunk (basic block sections) then
17755ffd83dbSDimitry Andric // update symbol values and sizes associated with these sections.  With basic
17765ffd83dbSDimitry Andric // block sections, input sections can shrink when the jump instructions at
17775ffd83dbSDimitry Andric // the end of the section are relaxed.
17785ffd83dbSDimitry Andric static void fixSymbolsAfterShrinking() {
1779bdd1243dSDimitry Andric   for (InputFile *File : ctx.objectFiles) {
17805ffd83dbSDimitry Andric     parallelForEach(File->getSymbols(), [&](Symbol *Sym) {
17815ffd83dbSDimitry Andric       auto *def = dyn_cast<Defined>(Sym);
17825ffd83dbSDimitry Andric       if (!def)
17835ffd83dbSDimitry Andric         return;
17845ffd83dbSDimitry Andric 
17855ffd83dbSDimitry Andric       const SectionBase *sec = def->section;
17865ffd83dbSDimitry Andric       if (!sec)
17875ffd83dbSDimitry Andric         return;
17885ffd83dbSDimitry Andric 
17890eae32dcSDimitry Andric       const InputSectionBase *inputSec = dyn_cast<InputSectionBase>(sec);
17905ffd83dbSDimitry Andric       if (!inputSec || !inputSec->bytesDropped)
17915ffd83dbSDimitry Andric         return;
17925ffd83dbSDimitry Andric 
1793bdd1243dSDimitry Andric       const size_t OldSize = inputSec->content().size();
17945ffd83dbSDimitry Andric       const size_t NewSize = OldSize - inputSec->bytesDropped;
17955ffd83dbSDimitry Andric 
17965ffd83dbSDimitry Andric       if (def->value > NewSize && def->value <= OldSize) {
17975ffd83dbSDimitry Andric         LLVM_DEBUG(llvm::dbgs()
17985ffd83dbSDimitry Andric                    << "Moving symbol " << Sym->getName() << " from "
17995ffd83dbSDimitry Andric                    << def->value << " to "
18005ffd83dbSDimitry Andric                    << def->value - inputSec->bytesDropped << " bytes\n");
18015ffd83dbSDimitry Andric         def->value -= inputSec->bytesDropped;
18025ffd83dbSDimitry Andric         return;
18035ffd83dbSDimitry Andric       }
18045ffd83dbSDimitry Andric 
18055ffd83dbSDimitry Andric       if (def->value + def->size > NewSize && def->value <= OldSize &&
18065ffd83dbSDimitry Andric           def->value + def->size <= OldSize) {
18075ffd83dbSDimitry Andric         LLVM_DEBUG(llvm::dbgs()
18085ffd83dbSDimitry Andric                    << "Shrinking symbol " << Sym->getName() << " from "
18095ffd83dbSDimitry Andric                    << def->size << " to " << def->size - inputSec->bytesDropped
18105ffd83dbSDimitry Andric                    << " bytes\n");
18115ffd83dbSDimitry Andric         def->size -= inputSec->bytesDropped;
18125ffd83dbSDimitry Andric       }
18135ffd83dbSDimitry Andric     });
18145ffd83dbSDimitry Andric   }
18155ffd83dbSDimitry Andric }
18165ffd83dbSDimitry Andric 
18175ffd83dbSDimitry Andric // If basic block sections exist, there are opportunities to delete fall thru
18185ffd83dbSDimitry Andric // jumps and shrink jump instructions after basic block reordering.  This
18195ffd83dbSDimitry Andric // relaxation pass does that.  It is only enabled when --optimize-bb-jumps
18205ffd83dbSDimitry Andric // option is used.
18215ffd83dbSDimitry Andric template <class ELFT> void Writer<ELFT>::optimizeBasicBlockJumps() {
18225ffd83dbSDimitry Andric   assert(config->optimizeBBJumps);
1823753f127fSDimitry Andric   SmallVector<InputSection *, 0> storage;
18245ffd83dbSDimitry Andric 
18255ffd83dbSDimitry Andric   script->assignAddresses();
18265ffd83dbSDimitry Andric   // For every output section that has executable input sections, this
18275ffd83dbSDimitry Andric   // does the following:
18285ffd83dbSDimitry Andric   //   1. Deletes all direct jump instructions in input sections that
18295ffd83dbSDimitry Andric   //      jump to the following section as it is not required.
18305ffd83dbSDimitry Andric   //   2. If there are two consecutive jump instructions, it checks
18315ffd83dbSDimitry Andric   //      if they can be flipped and one can be deleted.
183204eeddc0SDimitry Andric   for (OutputSection *osec : outputSections) {
183304eeddc0SDimitry Andric     if (!(osec->flags & SHF_EXECINSTR))
18345ffd83dbSDimitry Andric       continue;
1835753f127fSDimitry Andric     ArrayRef<InputSection *> sections = getInputSections(*osec, storage);
183604eeddc0SDimitry Andric     size_t numDeleted = 0;
18375ffd83dbSDimitry Andric     // Delete all fall through jump instructions.  Also, check if two
18385ffd83dbSDimitry Andric     // consecutive jump instructions can be flipped so that a fall
18395ffd83dbSDimitry Andric     // through jmp instruction can be deleted.
184004eeddc0SDimitry Andric     for (size_t i = 0, e = sections.size(); i != e; ++i) {
18415ffd83dbSDimitry Andric       InputSection *next = i + 1 < sections.size() ? sections[i + 1] : nullptr;
184204eeddc0SDimitry Andric       InputSection &sec = *sections[i];
184304eeddc0SDimitry Andric       numDeleted += target->deleteFallThruJmpInsn(sec, sec.file, next);
184404eeddc0SDimitry Andric     }
18455ffd83dbSDimitry Andric     if (numDeleted > 0) {
18465ffd83dbSDimitry Andric       script->assignAddresses();
18475ffd83dbSDimitry Andric       LLVM_DEBUG(llvm::dbgs()
18485ffd83dbSDimitry Andric                  << "Removing " << numDeleted << " fall through jumps\n");
18495ffd83dbSDimitry Andric     }
18505ffd83dbSDimitry Andric   }
18515ffd83dbSDimitry Andric 
18525ffd83dbSDimitry Andric   fixSymbolsAfterShrinking();
18535ffd83dbSDimitry Andric 
185404eeddc0SDimitry Andric   for (OutputSection *osec : outputSections)
1855753f127fSDimitry Andric     for (InputSection *is : getInputSections(*osec, storage))
18565ffd83dbSDimitry Andric       is->trim();
18575ffd83dbSDimitry Andric }
18580b57cec5SDimitry Andric 
18590b57cec5SDimitry Andric // In order to allow users to manipulate linker-synthesized sections,
18600b57cec5SDimitry Andric // we had to add synthetic sections to the input section list early,
18610b57cec5SDimitry Andric // even before we make decisions whether they are needed. This allows
18620b57cec5SDimitry Andric // users to write scripts like this: ".mygot : { .got }".
18630b57cec5SDimitry Andric //
18640b57cec5SDimitry Andric // Doing it has an unintended side effects. If it turns out that we
18650b57cec5SDimitry Andric // don't need a .got (for example) at all because there's no
18660b57cec5SDimitry Andric // relocation that needs a .got, we don't want to emit .got.
18670b57cec5SDimitry Andric //
18680b57cec5SDimitry Andric // To deal with the above problem, this function is called after
18690b57cec5SDimitry Andric // scanRelocations is called to remove synthetic sections that turn
18700b57cec5SDimitry Andric // out to be empty.
18710b57cec5SDimitry Andric static void removeUnusedSyntheticSections() {
18720b57cec5SDimitry Andric   // All input synthetic sections that can be empty are placed after
1873fe6060f1SDimitry Andric   // all regular ones. Reverse iterate to find the first synthetic section
1874fe6060f1SDimitry Andric   // after a non-synthetic one which will be our starting point.
1875bdd1243dSDimitry Andric   auto start =
1876bdd1243dSDimitry Andric       llvm::find_if(llvm::reverse(ctx.inputSections), [](InputSectionBase *s) {
1877fe6060f1SDimitry Andric         return !isa<SyntheticSection>(s);
1878bdd1243dSDimitry Andric       }).base();
1879fe6060f1SDimitry Andric 
1880bdd1243dSDimitry Andric   // Remove unused synthetic sections from ctx.inputSections;
18814824e7fdSDimitry Andric   DenseSet<InputSectionBase *> unused;
18824824e7fdSDimitry Andric   auto end =
1883bdd1243dSDimitry Andric       std::remove_if(start, ctx.inputSections.end(), [&](InputSectionBase *s) {
18844824e7fdSDimitry Andric         auto *sec = cast<SyntheticSection>(s);
18854824e7fdSDimitry Andric         if (sec->getParent() && sec->isNeeded())
1886fe6060f1SDimitry Andric           return false;
18874824e7fdSDimitry Andric         unused.insert(sec);
18884824e7fdSDimitry Andric         return true;
1889fe6060f1SDimitry Andric       });
1890bdd1243dSDimitry Andric   ctx.inputSections.erase(end, ctx.inputSections.end());
18914824e7fdSDimitry Andric 
18924824e7fdSDimitry Andric   // Remove unused synthetic sections from the corresponding input section
18934824e7fdSDimitry Andric   // description and orphanSections.
18944824e7fdSDimitry Andric   for (auto *sec : unused)
18954824e7fdSDimitry Andric     if (OutputSection *osec = cast<SyntheticSection>(sec)->getParent())
18964824e7fdSDimitry Andric       for (SectionCommand *cmd : osec->commands)
18974824e7fdSDimitry Andric         if (auto *isd = dyn_cast<InputSectionDescription>(cmd))
18984824e7fdSDimitry Andric           llvm::erase_if(isd->sections, [&](InputSection *isec) {
18994824e7fdSDimitry Andric             return unused.count(isec);
19004824e7fdSDimitry Andric           });
19014824e7fdSDimitry Andric   llvm::erase_if(script->orphanSections, [&](const InputSectionBase *sec) {
19024824e7fdSDimitry Andric     return unused.count(sec);
19034824e7fdSDimitry Andric   });
19040b57cec5SDimitry Andric }
19050b57cec5SDimitry Andric 
19060b57cec5SDimitry Andric // Create output section objects and add them to OutputSections.
19070b57cec5SDimitry Andric template <class ELFT> void Writer<ELFT>::finalizeSections() {
1908bdd1243dSDimitry Andric   if (!config->relocatable) {
19090b57cec5SDimitry Andric     Out::preinitArray = findSection(".preinit_array");
19100b57cec5SDimitry Andric     Out::initArray = findSection(".init_array");
19110b57cec5SDimitry Andric     Out::finiArray = findSection(".fini_array");
19120b57cec5SDimitry Andric 
19130b57cec5SDimitry Andric     // The linker needs to define SECNAME_start, SECNAME_end and SECNAME_stop
19140b57cec5SDimitry Andric     // symbols for sections, so that the runtime can get the start and end
19150b57cec5SDimitry Andric     // addresses of each section by section name. Add such symbols.
19160b57cec5SDimitry Andric     addStartEndSymbols();
19174824e7fdSDimitry Andric     for (SectionCommand *cmd : script->sectionCommands)
191881ad6265SDimitry Andric       if (auto *osd = dyn_cast<OutputDesc>(cmd))
191981ad6265SDimitry Andric         addStartStopSymbols(osd->osec);
19200b57cec5SDimitry Andric 
19210b57cec5SDimitry Andric     // Add _DYNAMIC symbol. Unlike GNU gold, our _DYNAMIC symbol has no type.
19220b57cec5SDimitry Andric     // It should be okay as no one seems to care about the type.
19230b57cec5SDimitry Andric     // Even the author of gold doesn't remember why gold behaves that way.
19240b57cec5SDimitry Andric     // https://sourceware.org/ml/binutils/2002-03/msg00360.html
1925bdd1243dSDimitry Andric     if (mainPart->dynamic->parent) {
1926bdd1243dSDimitry Andric       Symbol *s = symtab.addSymbol(Defined{
19277a6dacacSDimitry Andric           ctx.internalFile, "_DYNAMIC", STB_WEAK, STV_HIDDEN, STT_NOTYPE,
1928bdd1243dSDimitry Andric           /*value=*/0, /*size=*/0, mainPart->dynamic.get()});
1929bdd1243dSDimitry Andric       s->isUsedInRegularObj = true;
1930bdd1243dSDimitry Andric     }
19310b57cec5SDimitry Andric 
19320b57cec5SDimitry Andric     // Define __rel[a]_iplt_{start,end} symbols if needed.
19330b57cec5SDimitry Andric     addRelIpltSymbols();
19340b57cec5SDimitry Andric 
193585868e8aSDimitry Andric     // RISC-V's gp can address +/- 2 KiB, set it to .sdata + 0x800. This symbol
193685868e8aSDimitry Andric     // should only be defined in an executable. If .sdata does not exist, its
193785868e8aSDimitry Andric     // value/section does not matter but it has to be relative, so set its
193885868e8aSDimitry Andric     // st_shndx arbitrarily to 1 (Out::elfHeader).
193906c3fb27SDimitry Andric     if (config->emachine == EM_RISCV) {
194006c3fb27SDimitry Andric       ElfSym::riscvGlobalPointer = nullptr;
194106c3fb27SDimitry Andric       if (!config->shared) {
194285868e8aSDimitry Andric         OutputSection *sec = findSection(".sdata");
194306c3fb27SDimitry Andric         addOptionalRegular(
194406c3fb27SDimitry Andric             "__global_pointer$", sec ? sec : Out::elfHeader, 0x800, STV_DEFAULT);
194506c3fb27SDimitry Andric         // Set riscvGlobalPointer to be used by the optional global pointer
194606c3fb27SDimitry Andric         // relaxation.
194706c3fb27SDimitry Andric         if (config->relaxGP) {
194806c3fb27SDimitry Andric           Symbol *s = symtab.find("__global_pointer$");
194906c3fb27SDimitry Andric           if (s && s->isDefined())
195006c3fb27SDimitry Andric             ElfSym::riscvGlobalPointer = cast<Defined>(s);
195106c3fb27SDimitry Andric         }
195206c3fb27SDimitry Andric       }
195385868e8aSDimitry Andric     }
19540b57cec5SDimitry Andric 
1955349cc55cSDimitry Andric     if (config->emachine == EM_386 || config->emachine == EM_X86_64) {
19560b57cec5SDimitry Andric       // On targets that support TLSDESC, _TLS_MODULE_BASE_ is defined in such a
19570b57cec5SDimitry Andric       // way that:
19580b57cec5SDimitry Andric       //
19590b57cec5SDimitry Andric       // 1) Without relaxation: it produces a dynamic TLSDESC relocation that
19600b57cec5SDimitry Andric       // computes 0.
1961bdd1243dSDimitry Andric       // 2) With LD->LE relaxation: _TLS_MODULE_BASE_@tpoff = 0 (lowest address
1962bdd1243dSDimitry Andric       // in the TLS block).
19630b57cec5SDimitry Andric       //
1964bdd1243dSDimitry Andric       // 2) is special cased in @tpoff computation. To satisfy 1), we define it
1965bdd1243dSDimitry Andric       // as an absolute symbol of zero. This is different from GNU linkers which
19660b57cec5SDimitry Andric       // define _TLS_MODULE_BASE_ relative to the first TLS section.
1967bdd1243dSDimitry Andric       Symbol *s = symtab.find("_TLS_MODULE_BASE_");
19680b57cec5SDimitry Andric       if (s && s->isUndefined()) {
19697a6dacacSDimitry Andric         s->resolve(Defined{ctx.internalFile, StringRef(), STB_GLOBAL,
1970bdd1243dSDimitry Andric                            STV_HIDDEN, STT_TLS, /*value=*/0, 0,
19710b57cec5SDimitry Andric                            /*section=*/nullptr});
19720b57cec5SDimitry Andric         ElfSym::tlsModuleBase = cast<Defined>(s);
19730b57cec5SDimitry Andric       }
19740b57cec5SDimitry Andric     }
19750b57cec5SDimitry Andric 
19760b57cec5SDimitry Andric     // This responsible for splitting up .eh_frame section into
19770b57cec5SDimitry Andric     // pieces. The relocation scan uses those pieces, so this has to be
19780b57cec5SDimitry Andric     // earlier.
1979bdd1243dSDimitry Andric     {
1980bdd1243dSDimitry Andric       llvm::TimeTraceScope timeScope("Finalize .eh_frame");
19810b57cec5SDimitry Andric       for (Partition &part : partitions)
198204eeddc0SDimitry Andric         finalizeSynthetic(part.ehFrame.get());
1983e8d8bef9SDimitry Andric     }
19845f757f3fSDimitry Andric   }
19850b57cec5SDimitry Andric 
19865f757f3fSDimitry Andric   demoteSymbolsAndComputeIsPreemptible();
19875f757f3fSDimitry Andric 
19885f757f3fSDimitry Andric   if (config->copyRelocs && config->discard != DiscardPolicy::None)
19895f757f3fSDimitry Andric     markUsedLocalSymbols<ELFT>();
19905f757f3fSDimitry Andric   demoteAndCopyLocalSymbols();
19915f757f3fSDimitry Andric 
19925f757f3fSDimitry Andric   if (config->copyRelocs)
19935f757f3fSDimitry Andric     addSectionSymbols();
199485868e8aSDimitry Andric 
199585868e8aSDimitry Andric   // Change values of linker-script-defined symbols from placeholders (assigned
199685868e8aSDimitry Andric   // by declareSymbols) to actual definitions.
199785868e8aSDimitry Andric   script->processSymbolAssignments();
19980b57cec5SDimitry Andric 
1999bdd1243dSDimitry Andric   if (!config->relocatable) {
2000e8d8bef9SDimitry Andric     llvm::TimeTraceScope timeScope("Scan relocations");
2001e8d8bef9SDimitry Andric     // Scan relocations. This must be done after every symbol is declared so
2002e8d8bef9SDimitry Andric     // that we can correctly decide if a dynamic relocation is needed. This is
2003e8d8bef9SDimitry Andric     // called after processSymbolAssignments() because it needs to know whether
2004e8d8bef9SDimitry Andric     // a linker-script-defined symbol is absolute.
20055ffd83dbSDimitry Andric     ppc64noTocRelax.clear();
2006bdd1243dSDimitry Andric     scanRelocations<ELFT>();
200781ad6265SDimitry Andric     reportUndefinedSymbols();
20080eae32dcSDimitry Andric     postScanRelocations();
20090b57cec5SDimitry Andric 
20100b57cec5SDimitry Andric     if (in.plt && in.plt->isNeeded())
20110b57cec5SDimitry Andric       in.plt->addSymbols();
20120b57cec5SDimitry Andric     if (in.iplt && in.iplt->isNeeded())
20130b57cec5SDimitry Andric       in.iplt->addSymbols();
20140b57cec5SDimitry Andric 
2015e8d8bef9SDimitry Andric     if (config->unresolvedSymbolsInShlib != UnresolvedPolicy::Ignore) {
2016fe6060f1SDimitry Andric       auto diagnose =
2017fe6060f1SDimitry Andric           config->unresolvedSymbolsInShlib == UnresolvedPolicy::ReportError
2018fe6060f1SDimitry Andric               ? errorOrWarn
2019fe6060f1SDimitry Andric               : warn;
20200b57cec5SDimitry Andric       // Error on undefined symbols in a shared object, if all of its DT_NEEDED
2021480093f4SDimitry Andric       // entries are seen. These cases would otherwise lead to runtime errors
20220b57cec5SDimitry Andric       // reported by the dynamic linker.
20230b57cec5SDimitry Andric       //
2024bdd1243dSDimitry Andric       // ld.bfd traces all DT_NEEDED to emulate the logic of the dynamic linker
2025bdd1243dSDimitry Andric       // to catch more cases. That is too much for us. Our approach resembles
2026bdd1243dSDimitry Andric       // the one used in ld.gold, achieves a good balance to be useful but not
2027bdd1243dSDimitry Andric       // too smart.
20287a6dacacSDimitry Andric       //
20297a6dacacSDimitry Andric       // If a DSO reference is resolved by a SharedSymbol, but the SharedSymbol
20307a6dacacSDimitry Andric       // is overridden by a hidden visibility Defined (which is later discarded
20317a6dacacSDimitry Andric       // due to GC), don't report the diagnostic. However, this may indicate an
20327a6dacacSDimitry Andric       // unintended SharedSymbol.
2033bdd1243dSDimitry Andric       for (SharedFile *file : ctx.sharedFiles) {
2034fe6060f1SDimitry Andric         bool allNeededIsKnown =
20350b57cec5SDimitry Andric             llvm::all_of(file->dtNeeded, [&](StringRef needed) {
2036bdd1243dSDimitry Andric               return symtab.soNames.count(CachedHashStringRef(needed));
20370b57cec5SDimitry Andric             });
2038fe6060f1SDimitry Andric         if (!allNeededIsKnown)
2039fe6060f1SDimitry Andric           continue;
20405f757f3fSDimitry Andric         for (Symbol *sym : file->requiredSymbols) {
20417a6dacacSDimitry Andric           if (sym->dsoDefined)
20427a6dacacSDimitry Andric             continue;
20435f757f3fSDimitry Andric           if (sym->isUndefined() && !sym->isWeak()) {
2044fcaf7f86SDimitry Andric             diagnose("undefined reference due to --no-allow-shlib-undefined: " +
2045fcaf7f86SDimitry Andric                      toString(*sym) + "\n>>> referenced by " + toString(file));
20465f757f3fSDimitry Andric           } else if (sym->isDefined() && sym->computeBinding() == STB_LOCAL) {
20475f757f3fSDimitry Andric             diagnose("non-exported symbol '" + toString(*sym) + "' in '" +
20485f757f3fSDimitry Andric                      toString(sym->file) + "' is referenced by DSO '" +
20495f757f3fSDimitry Andric                      toString(file) + "'");
20505f757f3fSDimitry Andric           }
20515f757f3fSDimitry Andric         }
20520b57cec5SDimitry Andric       }
2053e8d8bef9SDimitry Andric     }
2054bdd1243dSDimitry Andric   }
20550b57cec5SDimitry Andric 
2056e8d8bef9SDimitry Andric   {
2057e8d8bef9SDimitry Andric     llvm::TimeTraceScope timeScope("Add symbols to symtabs");
20580b57cec5SDimitry Andric     // Now that we have defined all possible global symbols including linker-
20590b57cec5SDimitry Andric     // synthesized ones. Visit all symbols to give the finishing touches.
2060bdd1243dSDimitry Andric     for (Symbol *sym : symtab.getSymbols()) {
20610eae32dcSDimitry Andric       if (!sym->isUsedInRegularObj || !includeInSymtab(*sym))
2062480093f4SDimitry Andric         continue;
206304eeddc0SDimitry Andric       if (!config->relocatable)
206404eeddc0SDimitry Andric         sym->binding = sym->computeBinding();
20650b57cec5SDimitry Andric       if (in.symTab)
20660b57cec5SDimitry Andric         in.symTab->addSymbol(sym);
20670b57cec5SDimitry Andric 
20680b57cec5SDimitry Andric       if (sym->includeInDynsym()) {
20690b57cec5SDimitry Andric         partitions[sym->partition - 1].dynSymTab->addSymbol(sym);
20700b57cec5SDimitry Andric         if (auto *file = dyn_cast_or_null<SharedFile>(sym->file))
20710b57cec5SDimitry Andric           if (file->isNeeded && !sym->isUndefined())
20720b57cec5SDimitry Andric             addVerneed(sym);
20730b57cec5SDimitry Andric       }
2074480093f4SDimitry Andric     }
20750b57cec5SDimitry Andric 
2076e8d8bef9SDimitry Andric     // We also need to scan the dynamic relocation tables of the other
2077e8d8bef9SDimitry Andric     // partitions and add any referenced symbols to the partition's dynsym.
20780b57cec5SDimitry Andric     for (Partition &part : MutableArrayRef<Partition>(partitions).slice(1)) {
20790b57cec5SDimitry Andric       DenseSet<Symbol *> syms;
20800b57cec5SDimitry Andric       for (const SymbolTableEntry &e : part.dynSymTab->getSymbols())
20810b57cec5SDimitry Andric         syms.insert(e.sym);
20820b57cec5SDimitry Andric       for (DynamicReloc &reloc : part.relaDyn->relocs)
2083fe6060f1SDimitry Andric         if (reloc.sym && reloc.needsDynSymIndex() &&
2084fe6060f1SDimitry Andric             syms.insert(reloc.sym).second)
20850b57cec5SDimitry Andric           part.dynSymTab->addSymbol(reloc.sym);
20860b57cec5SDimitry Andric     }
2087e8d8bef9SDimitry Andric   }
20880b57cec5SDimitry Andric 
20890b57cec5SDimitry Andric   if (in.mipsGot)
20900b57cec5SDimitry Andric     in.mipsGot->build();
20910b57cec5SDimitry Andric 
20920b57cec5SDimitry Andric   removeUnusedSyntheticSections();
20935ffd83dbSDimitry Andric   script->diagnoseOrphanHandling();
209406c3fb27SDimitry Andric   script->diagnoseMissingSGSectionAddress();
20950b57cec5SDimitry Andric 
20960b57cec5SDimitry Andric   sortSections();
20970b57cec5SDimitry Andric 
20984824e7fdSDimitry Andric   // Create a list of OutputSections, assign sectionIndex, and populate
20994824e7fdSDimitry Andric   // in.shStrTab.
21004824e7fdSDimitry Andric   for (SectionCommand *cmd : script->sectionCommands)
210181ad6265SDimitry Andric     if (auto *osd = dyn_cast<OutputDesc>(cmd)) {
210281ad6265SDimitry Andric       OutputSection *osec = &osd->osec;
21034824e7fdSDimitry Andric       outputSections.push_back(osec);
21044824e7fdSDimitry Andric       osec->sectionIndex = outputSections.size();
21054824e7fdSDimitry Andric       osec->shName = in.shStrTab->addString(osec->name);
21064824e7fdSDimitry Andric     }
21070b57cec5SDimitry Andric 
21080b57cec5SDimitry Andric   // Prefer command line supplied address over other constraints.
21090b57cec5SDimitry Andric   for (OutputSection *sec : outputSections) {
21100b57cec5SDimitry Andric     auto i = config->sectionStartMap.find(sec->name);
21110b57cec5SDimitry Andric     if (i != config->sectionStartMap.end())
21120b57cec5SDimitry Andric       sec->addrExpr = [=] { return i->second; };
21130b57cec5SDimitry Andric   }
21140b57cec5SDimitry Andric 
21155ffd83dbSDimitry Andric   // With the outputSections available check for GDPLT relocations
21165ffd83dbSDimitry Andric   // and add __tls_get_addr symbol if needed.
21175ffd83dbSDimitry Andric   if (config->emachine == EM_HEXAGON && hexagonNeedsTLSSymbol(outputSections)) {
21187a6dacacSDimitry Andric     Symbol *sym =
21197a6dacacSDimitry Andric         symtab.addSymbol(Undefined{ctx.internalFile, "__tls_get_addr",
21207a6dacacSDimitry Andric                                    STB_GLOBAL, STV_DEFAULT, STT_NOTYPE});
21215ffd83dbSDimitry Andric     sym->isPreemptible = true;
21225ffd83dbSDimitry Andric     partitions[0].dynSymTab->addSymbol(sym);
21235ffd83dbSDimitry Andric   }
21245ffd83dbSDimitry Andric 
21250b57cec5SDimitry Andric   // This is a bit of a hack. A value of 0 means undef, so we set it
21260b57cec5SDimitry Andric   // to 1 to make __ehdr_start defined. The section number is not
21270b57cec5SDimitry Andric   // particularly relevant.
21280b57cec5SDimitry Andric   Out::elfHeader->sectionIndex = 1;
21294824e7fdSDimitry Andric   Out::elfHeader->size = sizeof(typename ELFT::Ehdr);
21300b57cec5SDimitry Andric 
21310b57cec5SDimitry Andric   // Binary and relocatable output does not have PHDRS.
21320b57cec5SDimitry Andric   // The headers have to be created before finalize as that can influence the
21330b57cec5SDimitry Andric   // image base and the dynamic section on mips includes the image base.
21340b57cec5SDimitry Andric   if (!config->relocatable && !config->oFormatBinary) {
21350b57cec5SDimitry Andric     for (Partition &part : partitions) {
21360b57cec5SDimitry Andric       part.phdrs = script->hasPhdrsCommands() ? script->createPhdrs()
21370b57cec5SDimitry Andric                                               : createPhdrs(part);
21380b57cec5SDimitry Andric       if (config->emachine == EM_ARM) {
21390b57cec5SDimitry Andric         // PT_ARM_EXIDX is the ARM EHABI equivalent of PT_GNU_EH_FRAME
21400b57cec5SDimitry Andric         addPhdrForSection(part, SHT_ARM_EXIDX, PT_ARM_EXIDX, PF_R);
21410b57cec5SDimitry Andric       }
21420b57cec5SDimitry Andric       if (config->emachine == EM_MIPS) {
21430b57cec5SDimitry Andric         // Add separate segments for MIPS-specific sections.
21440b57cec5SDimitry Andric         addPhdrForSection(part, SHT_MIPS_REGINFO, PT_MIPS_REGINFO, PF_R);
21450b57cec5SDimitry Andric         addPhdrForSection(part, SHT_MIPS_OPTIONS, PT_MIPS_OPTIONS, PF_R);
21460b57cec5SDimitry Andric         addPhdrForSection(part, SHT_MIPS_ABIFLAGS, PT_MIPS_ABIFLAGS, PF_R);
21470b57cec5SDimitry Andric       }
214806c3fb27SDimitry Andric       if (config->emachine == EM_RISCV)
214906c3fb27SDimitry Andric         addPhdrForSection(part, SHT_RISCV_ATTRIBUTES, PT_RISCV_ATTRIBUTES,
215006c3fb27SDimitry Andric                           PF_R);
21510b57cec5SDimitry Andric     }
21520b57cec5SDimitry Andric     Out::programHeaders->size = sizeof(Elf_Phdr) * mainPart->phdrs.size();
21530b57cec5SDimitry Andric 
21540b57cec5SDimitry Andric     // Find the TLS segment. This happens before the section layout loop so that
21550b57cec5SDimitry Andric     // Android relocation packing can look up TLS symbol addresses. We only need
21560b57cec5SDimitry Andric     // to care about the main partition here because all TLS symbols were moved
21570b57cec5SDimitry Andric     // to the main partition (see MarkLive.cpp).
21580b57cec5SDimitry Andric     for (PhdrEntry *p : mainPart->phdrs)
21590b57cec5SDimitry Andric       if (p->p_type == PT_TLS)
21600b57cec5SDimitry Andric         Out::tlsPhdr = p;
21610b57cec5SDimitry Andric   }
21620b57cec5SDimitry Andric 
21630b57cec5SDimitry Andric   // Some symbols are defined in term of program headers. Now that we
21640b57cec5SDimitry Andric   // have the headers, we can find out which sections they point to.
21650b57cec5SDimitry Andric   setReservedSymbolSections();
21660b57cec5SDimitry Andric 
2167e8d8bef9SDimitry Andric   {
2168e8d8bef9SDimitry Andric     llvm::TimeTraceScope timeScope("Finalize synthetic sections");
2169e8d8bef9SDimitry Andric 
217004eeddc0SDimitry Andric     finalizeSynthetic(in.bss.get());
217104eeddc0SDimitry Andric     finalizeSynthetic(in.bssRelRo.get());
217204eeddc0SDimitry Andric     finalizeSynthetic(in.symTabShndx.get());
217304eeddc0SDimitry Andric     finalizeSynthetic(in.shStrTab.get());
217404eeddc0SDimitry Andric     finalizeSynthetic(in.strTab.get());
217504eeddc0SDimitry Andric     finalizeSynthetic(in.got.get());
217604eeddc0SDimitry Andric     finalizeSynthetic(in.mipsGot.get());
217704eeddc0SDimitry Andric     finalizeSynthetic(in.igotPlt.get());
217804eeddc0SDimitry Andric     finalizeSynthetic(in.gotPlt.get());
217904eeddc0SDimitry Andric     finalizeSynthetic(in.relaIplt.get());
218004eeddc0SDimitry Andric     finalizeSynthetic(in.relaPlt.get());
218104eeddc0SDimitry Andric     finalizeSynthetic(in.plt.get());
218204eeddc0SDimitry Andric     finalizeSynthetic(in.iplt.get());
218304eeddc0SDimitry Andric     finalizeSynthetic(in.ppc32Got2.get());
218404eeddc0SDimitry Andric     finalizeSynthetic(in.partIndex.get());
21850b57cec5SDimitry Andric 
21860b57cec5SDimitry Andric     // Dynamic section must be the last one in this list and dynamic
21870b57cec5SDimitry Andric     // symbol table section (dynSymTab) must be the first one.
21880b57cec5SDimitry Andric     for (Partition &part : partitions) {
21891fd87a68SDimitry Andric       if (part.relaDyn) {
2190bdd1243dSDimitry Andric         part.relaDyn->mergeRels();
21911fd87a68SDimitry Andric         // Compute DT_RELACOUNT to be used by part.dynamic.
21921fd87a68SDimitry Andric         part.relaDyn->partitionRels();
21931fd87a68SDimitry Andric         finalizeSynthetic(part.relaDyn.get());
21941fd87a68SDimitry Andric       }
2195bdd1243dSDimitry Andric       if (part.relrDyn) {
2196bdd1243dSDimitry Andric         part.relrDyn->mergeRels();
2197bdd1243dSDimitry Andric         finalizeSynthetic(part.relrDyn.get());
2198bdd1243dSDimitry Andric       }
21991fd87a68SDimitry Andric 
220004eeddc0SDimitry Andric       finalizeSynthetic(part.dynSymTab.get());
220104eeddc0SDimitry Andric       finalizeSynthetic(part.gnuHashTab.get());
220204eeddc0SDimitry Andric       finalizeSynthetic(part.hashTab.get());
220304eeddc0SDimitry Andric       finalizeSynthetic(part.verDef.get());
220404eeddc0SDimitry Andric       finalizeSynthetic(part.ehFrameHdr.get());
220504eeddc0SDimitry Andric       finalizeSynthetic(part.verSym.get());
220604eeddc0SDimitry Andric       finalizeSynthetic(part.verNeed.get());
220704eeddc0SDimitry Andric       finalizeSynthetic(part.dynamic.get());
22080b57cec5SDimitry Andric     }
2209e8d8bef9SDimitry Andric   }
22100b57cec5SDimitry Andric 
22110b57cec5SDimitry Andric   if (!script->hasSectionsCommand && !config->relocatable)
22120b57cec5SDimitry Andric     fixSectionAlignments();
22130b57cec5SDimitry Andric 
22140b57cec5SDimitry Andric   // This is used to:
22150b57cec5SDimitry Andric   // 1) Create "thunks":
22160b57cec5SDimitry Andric   //    Jump instructions in many ISAs have small displacements, and therefore
22170b57cec5SDimitry Andric   //    they cannot jump to arbitrary addresses in memory. For example, RISC-V
22180b57cec5SDimitry Andric   //    JAL instruction can target only +-1 MiB from PC. It is a linker's
22190b57cec5SDimitry Andric   //    responsibility to create and insert small pieces of code between
22200b57cec5SDimitry Andric   //    sections to extend the ranges if jump targets are out of range. Such
22210b57cec5SDimitry Andric   //    code pieces are called "thunks".
22220b57cec5SDimitry Andric   //
22230b57cec5SDimitry Andric   //    We add thunks at this stage. We couldn't do this before this point
22240b57cec5SDimitry Andric   //    because this is the earliest point where we know sizes of sections and
22250b57cec5SDimitry Andric   //    their layouts (that are needed to determine if jump targets are in
22260b57cec5SDimitry Andric   //    range).
22270b57cec5SDimitry Andric   //
22280b57cec5SDimitry Andric   // 2) Update the sections. We need to generate content that depends on the
22290b57cec5SDimitry Andric   //    address of InputSections. For example, MIPS GOT section content or
22300b57cec5SDimitry Andric   //    android packed relocations sections content.
22310b57cec5SDimitry Andric   //
22320b57cec5SDimitry Andric   // 3) Assign the final values for the linker script symbols. Linker scripts
22330b57cec5SDimitry Andric   //    sometimes using forward symbol declarations. We want to set the correct
22340b57cec5SDimitry Andric   //    values. They also might change after adding the thunks.
22350b57cec5SDimitry Andric   finalizeAddressDependentContent();
223604eeddc0SDimitry Andric 
223704eeddc0SDimitry Andric   // All information needed for OutputSection part of Map file is available.
22385ffd83dbSDimitry Andric   if (errorCount())
22395ffd83dbSDimitry Andric     return;
22400b57cec5SDimitry Andric 
2241e8d8bef9SDimitry Andric   {
2242e8d8bef9SDimitry Andric     llvm::TimeTraceScope timeScope("Finalize synthetic sections");
2243e8d8bef9SDimitry Andric     // finalizeAddressDependentContent may have added local symbols to the
2244e8d8bef9SDimitry Andric     // static symbol table.
224504eeddc0SDimitry Andric     finalizeSynthetic(in.symTab.get());
224604eeddc0SDimitry Andric     finalizeSynthetic(in.ppc64LongBranchTarget.get());
224706c3fb27SDimitry Andric     finalizeSynthetic(in.armCmseSGSection.get());
2248e8d8bef9SDimitry Andric   }
22490b57cec5SDimitry Andric 
22505ffd83dbSDimitry Andric   // Relaxation to delete inter-basic block jumps created by basic block
22515ffd83dbSDimitry Andric   // sections. Run after in.symTab is finalized as optimizeBasicBlockJumps
22525ffd83dbSDimitry Andric   // can relax jump instructions based on symbol offset.
22535ffd83dbSDimitry Andric   if (config->optimizeBBJumps)
22545ffd83dbSDimitry Andric     optimizeBasicBlockJumps();
22555ffd83dbSDimitry Andric 
22560b57cec5SDimitry Andric   // Fill other section headers. The dynamic table is finalized
22570b57cec5SDimitry Andric   // at the end because some tags like RELSZ depend on result
22580b57cec5SDimitry Andric   // of finalizing other sections.
22590b57cec5SDimitry Andric   for (OutputSection *sec : outputSections)
22600b57cec5SDimitry Andric     sec->finalize();
226106c3fb27SDimitry Andric 
22625f757f3fSDimitry Andric   script->checkFinalScriptConditions();
226306c3fb27SDimitry Andric 
226406c3fb27SDimitry Andric   if (config->emachine == EM_ARM && !config->isLE && config->armBe8) {
226506c3fb27SDimitry Andric     addArmInputSectionMappingSymbols();
226606c3fb27SDimitry Andric     sortArmMappingSymbols();
226706c3fb27SDimitry Andric   }
22680b57cec5SDimitry Andric }
22690b57cec5SDimitry Andric 
22700b57cec5SDimitry Andric // Ensure data sections are not mixed with executable sections when
2271349cc55cSDimitry Andric // --execute-only is used. --execute-only make pages executable but not
2272349cc55cSDimitry Andric // readable.
22730b57cec5SDimitry Andric template <class ELFT> void Writer<ELFT>::checkExecuteOnly() {
22740b57cec5SDimitry Andric   if (!config->executeOnly)
22750b57cec5SDimitry Andric     return;
22760b57cec5SDimitry Andric 
2277753f127fSDimitry Andric   SmallVector<InputSection *, 0> storage;
227804eeddc0SDimitry Andric   for (OutputSection *osec : outputSections)
227904eeddc0SDimitry Andric     if (osec->flags & SHF_EXECINSTR)
2280753f127fSDimitry Andric       for (InputSection *isec : getInputSections(*osec, storage))
22810b57cec5SDimitry Andric         if (!(isec->flags & SHF_EXECINSTR))
228204eeddc0SDimitry Andric           error("cannot place " + toString(isec) + " into " +
228304eeddc0SDimitry Andric                 toString(osec->name) +
228404eeddc0SDimitry Andric                 ": --execute-only does not support intermingling data and code");
22850b57cec5SDimitry Andric }
22860b57cec5SDimitry Andric 
22870b57cec5SDimitry Andric // The linker is expected to define SECNAME_start and SECNAME_end
22880b57cec5SDimitry Andric // symbols for a few sections. This function defines them.
22890b57cec5SDimitry Andric template <class ELFT> void Writer<ELFT>::addStartEndSymbols() {
22900b57cec5SDimitry Andric   // If a section does not exist, there's ambiguity as to how we
22910b57cec5SDimitry Andric   // define _start and _end symbols for an init/fini section. Since
22920b57cec5SDimitry Andric   // the loader assume that the symbols are always defined, we need to
22930b57cec5SDimitry Andric   // always define them. But what value? The loader iterates over all
22940b57cec5SDimitry Andric   // pointers between _start and _end to run global ctors/dtors, so if
22950b57cec5SDimitry Andric   // the section is empty, their symbol values don't actually matter
22960b57cec5SDimitry Andric   // as long as _start and _end point to the same location.
22970b57cec5SDimitry Andric   //
22980b57cec5SDimitry Andric   // That said, we don't want to set the symbols to 0 (which is
22990b57cec5SDimitry Andric   // probably the simplest value) because that could cause some
23000b57cec5SDimitry Andric   // program to fail to link due to relocation overflow, if their
23010b57cec5SDimitry Andric   // program text is above 2 GiB. We use the address of the .text
23020b57cec5SDimitry Andric   // section instead to prevent that failure.
23030b57cec5SDimitry Andric   //
2304480093f4SDimitry Andric   // In rare situations, the .text section may not exist. If that's the
23050b57cec5SDimitry Andric   // case, use the image base address as a last resort.
23060b57cec5SDimitry Andric   OutputSection *Default = findSection(".text");
23070b57cec5SDimitry Andric   if (!Default)
23080b57cec5SDimitry Andric     Default = Out::elfHeader;
23090b57cec5SDimitry Andric 
23100b57cec5SDimitry Andric   auto define = [=](StringRef start, StringRef end, OutputSection *os) {
2311349cc55cSDimitry Andric     if (os && !script->isDiscarded(os)) {
23120b57cec5SDimitry Andric       addOptionalRegular(start, os, 0);
23130b57cec5SDimitry Andric       addOptionalRegular(end, os, -1);
23140b57cec5SDimitry Andric     } else {
23150b57cec5SDimitry Andric       addOptionalRegular(start, Default, 0);
23160b57cec5SDimitry Andric       addOptionalRegular(end, Default, 0);
23170b57cec5SDimitry Andric     }
23180b57cec5SDimitry Andric   };
23190b57cec5SDimitry Andric 
23200b57cec5SDimitry Andric   define("__preinit_array_start", "__preinit_array_end", Out::preinitArray);
23210b57cec5SDimitry Andric   define("__init_array_start", "__init_array_end", Out::initArray);
23220b57cec5SDimitry Andric   define("__fini_array_start", "__fini_array_end", Out::finiArray);
23230b57cec5SDimitry Andric 
23240b57cec5SDimitry Andric   if (OutputSection *sec = findSection(".ARM.exidx"))
23250b57cec5SDimitry Andric     define("__exidx_start", "__exidx_end", sec);
23260b57cec5SDimitry Andric }
23270b57cec5SDimitry Andric 
23280b57cec5SDimitry Andric // If a section name is valid as a C identifier (which is rare because of
23290b57cec5SDimitry Andric // the leading '.'), linkers are expected to define __start_<secname> and
23300b57cec5SDimitry Andric // __stop_<secname> symbols. They are at beginning and end of the section,
23310b57cec5SDimitry Andric // respectively. This is not requested by the ELF standard, but GNU ld and
23320b57cec5SDimitry Andric // gold provide the feature, and used by many programs.
23330b57cec5SDimitry Andric template <class ELFT>
233481ad6265SDimitry Andric void Writer<ELFT>::addStartStopSymbols(OutputSection &osec) {
233581ad6265SDimitry Andric   StringRef s = osec.name;
23360b57cec5SDimitry Andric   if (!isValidCIdentifier(s))
23370b57cec5SDimitry Andric     return;
233881ad6265SDimitry Andric   addOptionalRegular(saver().save("__start_" + s), &osec, 0,
23395ffd83dbSDimitry Andric                      config->zStartStopVisibility);
234081ad6265SDimitry Andric   addOptionalRegular(saver().save("__stop_" + s), &osec, -1,
23415ffd83dbSDimitry Andric                      config->zStartStopVisibility);
23420b57cec5SDimitry Andric }
23430b57cec5SDimitry Andric 
23440b57cec5SDimitry Andric static bool needsPtLoad(OutputSection *sec) {
2345fe6060f1SDimitry Andric   if (!(sec->flags & SHF_ALLOC))
23460b57cec5SDimitry Andric     return false;
23470b57cec5SDimitry Andric 
23480b57cec5SDimitry Andric   // Don't allocate VA space for TLS NOBITS sections. The PT_TLS PHDR is
23490b57cec5SDimitry Andric   // responsible for allocating space for them, not the PT_LOAD that
23500b57cec5SDimitry Andric   // contains the TLS initialization image.
23510b57cec5SDimitry Andric   if ((sec->flags & SHF_TLS) && sec->type == SHT_NOBITS)
23520b57cec5SDimitry Andric     return false;
23530b57cec5SDimitry Andric   return true;
23540b57cec5SDimitry Andric }
23550b57cec5SDimitry Andric 
23560b57cec5SDimitry Andric // Linker scripts are responsible for aligning addresses. Unfortunately, most
23570b57cec5SDimitry Andric // linker scripts are designed for creating two PT_LOADs only, one RX and one
23580b57cec5SDimitry Andric // RW. This means that there is no alignment in the RO to RX transition and we
23590b57cec5SDimitry Andric // cannot create a PT_LOAD there.
23600b57cec5SDimitry Andric static uint64_t computeFlags(uint64_t flags) {
23610b57cec5SDimitry Andric   if (config->omagic)
23620b57cec5SDimitry Andric     return PF_R | PF_W | PF_X;
23630b57cec5SDimitry Andric   if (config->executeOnly && (flags & PF_X))
23640b57cec5SDimitry Andric     return flags & ~PF_R;
23650b57cec5SDimitry Andric   if (config->singleRoRx && !(flags & PF_W))
23660b57cec5SDimitry Andric     return flags | PF_X;
23670b57cec5SDimitry Andric   return flags;
23680b57cec5SDimitry Andric }
23690b57cec5SDimitry Andric 
23700b57cec5SDimitry Andric // Decide which program headers to create and which sections to include in each
23710b57cec5SDimitry Andric // one.
23720b57cec5SDimitry Andric template <class ELFT>
237304eeddc0SDimitry Andric SmallVector<PhdrEntry *, 0> Writer<ELFT>::createPhdrs(Partition &part) {
237404eeddc0SDimitry Andric   SmallVector<PhdrEntry *, 0> ret;
23750b57cec5SDimitry Andric   auto addHdr = [&](unsigned type, unsigned flags) -> PhdrEntry * {
23760b57cec5SDimitry Andric     ret.push_back(make<PhdrEntry>(type, flags));
23770b57cec5SDimitry Andric     return ret.back();
23780b57cec5SDimitry Andric   };
23790b57cec5SDimitry Andric 
23800b57cec5SDimitry Andric   unsigned partNo = part.getNumber();
23810b57cec5SDimitry Andric   bool isMain = partNo == 1;
23820b57cec5SDimitry Andric 
238385868e8aSDimitry Andric   // Add the first PT_LOAD segment for regular output sections.
238485868e8aSDimitry Andric   uint64_t flags = computeFlags(PF_R);
238585868e8aSDimitry Andric   PhdrEntry *load = nullptr;
238685868e8aSDimitry Andric 
238785868e8aSDimitry Andric   // nmagic or omagic output does not have PT_PHDR, PT_INTERP, or the readonly
238885868e8aSDimitry Andric   // PT_LOAD.
238985868e8aSDimitry Andric   if (!config->nmagic && !config->omagic) {
239085868e8aSDimitry Andric     // The first phdr entry is PT_PHDR which describes the program header
239185868e8aSDimitry Andric     // itself.
23920b57cec5SDimitry Andric     if (isMain)
23930b57cec5SDimitry Andric       addHdr(PT_PHDR, PF_R)->add(Out::programHeaders);
23940b57cec5SDimitry Andric     else
23950b57cec5SDimitry Andric       addHdr(PT_PHDR, PF_R)->add(part.programHeaders->getParent());
23960b57cec5SDimitry Andric 
23970b57cec5SDimitry Andric     // PT_INTERP must be the second entry if exists.
23980b57cec5SDimitry Andric     if (OutputSection *cmd = findSection(".interp", partNo))
23990b57cec5SDimitry Andric       addHdr(PT_INTERP, cmd->getPhdrFlags())->add(cmd);
24000b57cec5SDimitry Andric 
24010b57cec5SDimitry Andric     // Add the headers. We will remove them if they don't fit.
24020b57cec5SDimitry Andric     // In the other partitions the headers are ordinary sections, so they don't
24030b57cec5SDimitry Andric     // need to be added here.
24040b57cec5SDimitry Andric     if (isMain) {
24050b57cec5SDimitry Andric       load = addHdr(PT_LOAD, flags);
24060b57cec5SDimitry Andric       load->add(Out::elfHeader);
24070b57cec5SDimitry Andric       load->add(Out::programHeaders);
24080b57cec5SDimitry Andric     }
240985868e8aSDimitry Andric   }
24100b57cec5SDimitry Andric 
24110b57cec5SDimitry Andric   // PT_GNU_RELRO includes all sections that should be marked as
2412480093f4SDimitry Andric   // read-only by dynamic linker after processing relocations.
24130b57cec5SDimitry Andric   // Current dynamic loaders only support one PT_GNU_RELRO PHDR, give
24140b57cec5SDimitry Andric   // an error message if more than one PT_GNU_RELRO PHDR is required.
24150b57cec5SDimitry Andric   PhdrEntry *relRo = make<PhdrEntry>(PT_GNU_RELRO, PF_R);
24160b57cec5SDimitry Andric   bool inRelroPhdr = false;
24170b57cec5SDimitry Andric   OutputSection *relroEnd = nullptr;
24180b57cec5SDimitry Andric   for (OutputSection *sec : outputSections) {
24190b57cec5SDimitry Andric     if (sec->partition != partNo || !needsPtLoad(sec))
24200b57cec5SDimitry Andric       continue;
24210b57cec5SDimitry Andric     if (isRelroSection(sec)) {
24220b57cec5SDimitry Andric       inRelroPhdr = true;
24230b57cec5SDimitry Andric       if (!relroEnd)
24240b57cec5SDimitry Andric         relRo->add(sec);
24250b57cec5SDimitry Andric       else
24260b57cec5SDimitry Andric         error("section: " + sec->name + " is not contiguous with other relro" +
24270b57cec5SDimitry Andric               " sections");
24280b57cec5SDimitry Andric     } else if (inRelroPhdr) {
24290b57cec5SDimitry Andric       inRelroPhdr = false;
24300b57cec5SDimitry Andric       relroEnd = sec;
24310b57cec5SDimitry Andric     }
24320b57cec5SDimitry Andric   }
24335f757f3fSDimitry Andric   relRo->p_align = 1;
24340b57cec5SDimitry Andric 
24350b57cec5SDimitry Andric   for (OutputSection *sec : outputSections) {
24360b57cec5SDimitry Andric     if (!needsPtLoad(sec))
24370b57cec5SDimitry Andric       continue;
24380b57cec5SDimitry Andric 
24390b57cec5SDimitry Andric     // Normally, sections in partitions other than the current partition are
24400b57cec5SDimitry Andric     // ignored. But partition number 255 is a special case: it contains the
24410b57cec5SDimitry Andric     // partition end marker (.part.end). It needs to be added to the main
24420b57cec5SDimitry Andric     // partition so that a segment is created for it in the main partition,
24430b57cec5SDimitry Andric     // which will cause the dynamic loader to reserve space for the other
24440b57cec5SDimitry Andric     // partitions.
24450b57cec5SDimitry Andric     if (sec->partition != partNo) {
24460b57cec5SDimitry Andric       if (isMain && sec->partition == 255)
24470b57cec5SDimitry Andric         addHdr(PT_LOAD, computeFlags(sec->getPhdrFlags()))->add(sec);
24480b57cec5SDimitry Andric       continue;
24490b57cec5SDimitry Andric     }
24500b57cec5SDimitry Andric 
24510b57cec5SDimitry Andric     // Segments are contiguous memory regions that has the same attributes
24520b57cec5SDimitry Andric     // (e.g. executable or writable). There is one phdr for each segment.
24530b57cec5SDimitry Andric     // Therefore, we need to create a new phdr when the next section has
245406c3fb27SDimitry Andric     // different flags or is loaded at a discontiguous address or memory region
245506c3fb27SDimitry Andric     // using AT or AT> linker script command, respectively.
245606c3fb27SDimitry Andric     //
245706c3fb27SDimitry Andric     // As an exception, we don't create a separate load segment for the ELF
245806c3fb27SDimitry Andric     // headers, even if the first "real" output has an AT or AT> attribute.
245906c3fb27SDimitry Andric     //
246006c3fb27SDimitry Andric     // In addition, NOBITS sections should only be placed at the end of a LOAD
246106c3fb27SDimitry Andric     // segment (since it's represented as p_filesz < p_memsz). If we have a
246206c3fb27SDimitry Andric     // not-NOBITS section after a NOBITS, we create a new LOAD for the latter
246306c3fb27SDimitry Andric     // even if flags match, so as not to require actually writing the
246406c3fb27SDimitry Andric     // supposed-to-be-NOBITS section to the output file. (However, we cannot do
246506c3fb27SDimitry Andric     // so when hasSectionsCommand, since we cannot introduce the extra alignment
246606c3fb27SDimitry Andric     // needed to create a new LOAD)
24670b57cec5SDimitry Andric     uint64_t newFlags = computeFlags(sec->getPhdrFlags());
24685ffd83dbSDimitry Andric     bool sameLMARegion =
24695ffd83dbSDimitry Andric         load && !sec->lmaExpr && sec->lmaRegion == load->firstSec->lmaRegion;
24705ffd83dbSDimitry Andric     if (!(load && newFlags == flags && sec != relroEnd &&
24715ffd83dbSDimitry Andric           sec->memRegion == load->firstSec->memRegion &&
247206c3fb27SDimitry Andric           (sameLMARegion || load->lastSec == Out::programHeaders) &&
247306c3fb27SDimitry Andric           (script->hasSectionsCommand || sec->type == SHT_NOBITS ||
247406c3fb27SDimitry Andric            load->lastSec->type != SHT_NOBITS))) {
24750b57cec5SDimitry Andric       load = addHdr(PT_LOAD, newFlags);
24760b57cec5SDimitry Andric       flags = newFlags;
24770b57cec5SDimitry Andric     }
24780b57cec5SDimitry Andric 
24790b57cec5SDimitry Andric     load->add(sec);
24800b57cec5SDimitry Andric   }
24810b57cec5SDimitry Andric 
24820b57cec5SDimitry Andric   // Add a TLS segment if any.
24830b57cec5SDimitry Andric   PhdrEntry *tlsHdr = make<PhdrEntry>(PT_TLS, PF_R);
24840b57cec5SDimitry Andric   for (OutputSection *sec : outputSections)
24850b57cec5SDimitry Andric     if (sec->partition == partNo && sec->flags & SHF_TLS)
24860b57cec5SDimitry Andric       tlsHdr->add(sec);
24870b57cec5SDimitry Andric   if (tlsHdr->firstSec)
24880b57cec5SDimitry Andric     ret.push_back(tlsHdr);
24890b57cec5SDimitry Andric 
24900b57cec5SDimitry Andric   // Add an entry for .dynamic.
24910b57cec5SDimitry Andric   if (OutputSection *sec = part.dynamic->getParent())
24920b57cec5SDimitry Andric     addHdr(PT_DYNAMIC, sec->getPhdrFlags())->add(sec);
24930b57cec5SDimitry Andric 
24940b57cec5SDimitry Andric   if (relRo->firstSec)
24950b57cec5SDimitry Andric     ret.push_back(relRo);
24960b57cec5SDimitry Andric 
24970b57cec5SDimitry Andric   // PT_GNU_EH_FRAME is a special section pointing on .eh_frame_hdr.
24980b57cec5SDimitry Andric   if (part.ehFrame->isNeeded() && part.ehFrameHdr &&
24990b57cec5SDimitry Andric       part.ehFrame->getParent() && part.ehFrameHdr->getParent())
25000b57cec5SDimitry Andric     addHdr(PT_GNU_EH_FRAME, part.ehFrameHdr->getParent()->getPhdrFlags())
25010b57cec5SDimitry Andric         ->add(part.ehFrameHdr->getParent());
25020b57cec5SDimitry Andric 
25030b57cec5SDimitry Andric   // PT_OPENBSD_RANDOMIZE is an OpenBSD-specific feature. That makes
25040b57cec5SDimitry Andric   // the dynamic linker fill the segment with random data.
25050b57cec5SDimitry Andric   if (OutputSection *cmd = findSection(".openbsd.randomdata", partNo))
25060b57cec5SDimitry Andric     addHdr(PT_OPENBSD_RANDOMIZE, cmd->getPhdrFlags())->add(cmd);
25070b57cec5SDimitry Andric 
2508480093f4SDimitry Andric   if (config->zGnustack != GnuStackKind::None) {
25090b57cec5SDimitry Andric     // PT_GNU_STACK is a special section to tell the loader to make the
25100b57cec5SDimitry Andric     // pages for the stack non-executable. If you really want an executable
25110b57cec5SDimitry Andric     // stack, you can pass -z execstack, but that's not recommended for
25120b57cec5SDimitry Andric     // security reasons.
25130b57cec5SDimitry Andric     unsigned perm = PF_R | PF_W;
2514480093f4SDimitry Andric     if (config->zGnustack == GnuStackKind::Exec)
25150b57cec5SDimitry Andric       perm |= PF_X;
25160b57cec5SDimitry Andric     addHdr(PT_GNU_STACK, perm)->p_memsz = config->zStackSize;
2517480093f4SDimitry Andric   }
25180b57cec5SDimitry Andric 
25190b57cec5SDimitry Andric   // PT_OPENBSD_WXNEEDED is a OpenBSD-specific header to mark the executable
25200b57cec5SDimitry Andric   // is expected to perform W^X violations, such as calling mprotect(2) or
25210b57cec5SDimitry Andric   // mmap(2) with PROT_WRITE | PROT_EXEC, which is prohibited by default on
25220b57cec5SDimitry Andric   // OpenBSD.
25230b57cec5SDimitry Andric   if (config->zWxneeded)
25240b57cec5SDimitry Andric     addHdr(PT_OPENBSD_WXNEEDED, PF_X);
25250b57cec5SDimitry Andric 
2526480093f4SDimitry Andric   if (OutputSection *cmd = findSection(".note.gnu.property", partNo))
2527480093f4SDimitry Andric     addHdr(PT_GNU_PROPERTY, PF_R)->add(cmd);
2528480093f4SDimitry Andric 
25290b57cec5SDimitry Andric   // Create one PT_NOTE per a group of contiguous SHT_NOTE sections with the
25300b57cec5SDimitry Andric   // same alignment.
25310b57cec5SDimitry Andric   PhdrEntry *note = nullptr;
25320b57cec5SDimitry Andric   for (OutputSection *sec : outputSections) {
25330b57cec5SDimitry Andric     if (sec->partition != partNo)
25340b57cec5SDimitry Andric       continue;
25350b57cec5SDimitry Andric     if (sec->type == SHT_NOTE && (sec->flags & SHF_ALLOC)) {
2536bdd1243dSDimitry Andric       if (!note || sec->lmaExpr || note->lastSec->addralign != sec->addralign)
25370b57cec5SDimitry Andric         note = addHdr(PT_NOTE, PF_R);
25380b57cec5SDimitry Andric       note->add(sec);
25390b57cec5SDimitry Andric     } else {
25400b57cec5SDimitry Andric       note = nullptr;
25410b57cec5SDimitry Andric     }
25420b57cec5SDimitry Andric   }
25430b57cec5SDimitry Andric   return ret;
25440b57cec5SDimitry Andric }
25450b57cec5SDimitry Andric 
25460b57cec5SDimitry Andric template <class ELFT>
25470b57cec5SDimitry Andric void Writer<ELFT>::addPhdrForSection(Partition &part, unsigned shType,
25480b57cec5SDimitry Andric                                      unsigned pType, unsigned pFlags) {
25490b57cec5SDimitry Andric   unsigned partNo = part.getNumber();
25500b57cec5SDimitry Andric   auto i = llvm::find_if(outputSections, [=](OutputSection *cmd) {
25510b57cec5SDimitry Andric     return cmd->partition == partNo && cmd->type == shType;
25520b57cec5SDimitry Andric   });
25530b57cec5SDimitry Andric   if (i == outputSections.end())
25540b57cec5SDimitry Andric     return;
25550b57cec5SDimitry Andric 
25560b57cec5SDimitry Andric   PhdrEntry *entry = make<PhdrEntry>(pType, pFlags);
25570b57cec5SDimitry Andric   entry->add(*i);
25580b57cec5SDimitry Andric   part.phdrs.push_back(entry);
25590b57cec5SDimitry Andric }
25600b57cec5SDimitry Andric 
256185868e8aSDimitry Andric // Place the first section of each PT_LOAD to a different page (of maxPageSize).
256285868e8aSDimitry Andric // This is achieved by assigning an alignment expression to addrExpr of each
256385868e8aSDimitry Andric // such section.
25640b57cec5SDimitry Andric template <class ELFT> void Writer<ELFT>::fixSectionAlignments() {
256585868e8aSDimitry Andric   const PhdrEntry *prev;
256685868e8aSDimitry Andric   auto pageAlign = [&](const PhdrEntry *p) {
256785868e8aSDimitry Andric     OutputSection *cmd = p->firstSec;
25685ffd83dbSDimitry Andric     if (!cmd)
25695ffd83dbSDimitry Andric       return;
2570bdd1243dSDimitry Andric     cmd->alignExpr = [align = cmd->addralign]() { return align; };
25715ffd83dbSDimitry Andric     if (!cmd->addrExpr) {
257285868e8aSDimitry Andric       // Prefer advancing to align(dot, maxPageSize) + dot%maxPageSize to avoid
257385868e8aSDimitry Andric       // padding in the file contents.
257485868e8aSDimitry Andric       //
257585868e8aSDimitry Andric       // When -z separate-code is used we must not have any overlap in pages
257685868e8aSDimitry Andric       // between an executable segment and a non-executable segment. We align to
257785868e8aSDimitry Andric       // the next maximum page size boundary on transitions between executable
257885868e8aSDimitry Andric       // and non-executable segments.
257985868e8aSDimitry Andric       //
258085868e8aSDimitry Andric       // SHT_LLVM_PART_EHDR marks the start of a partition. The partition
258185868e8aSDimitry Andric       // sections will be extracted to a separate file. Align to the next
258285868e8aSDimitry Andric       // maximum page size boundary so that we can find the ELF header at the
258385868e8aSDimitry Andric       // start. We cannot benefit from overlapping p_offset ranges with the
258485868e8aSDimitry Andric       // previous segment anyway.
258585868e8aSDimitry Andric       if (config->zSeparate == SeparateSegmentKind::Loadable ||
258685868e8aSDimitry Andric           (config->zSeparate == SeparateSegmentKind::Code && prev &&
258785868e8aSDimitry Andric            (prev->p_flags & PF_X) != (p->p_flags & PF_X)) ||
258885868e8aSDimitry Andric           cmd->type == SHT_LLVM_PART_EHDR)
258985868e8aSDimitry Andric         cmd->addrExpr = [] {
2590972a253aSDimitry Andric           return alignToPowerOf2(script->getDot(), config->maxPageSize);
25910b57cec5SDimitry Andric         };
259285868e8aSDimitry Andric       // PT_TLS is at the start of the first RW PT_LOAD. If `p` includes PT_TLS,
259385868e8aSDimitry Andric       // it must be the RW. Align to p_align(PT_TLS) to make sure
259485868e8aSDimitry Andric       // p_vaddr(PT_LOAD)%p_align(PT_LOAD) = 0. Otherwise, if
259585868e8aSDimitry Andric       // sh_addralign(.tdata) < sh_addralign(.tbss), we will set p_align(PT_TLS)
259685868e8aSDimitry Andric       // to sh_addralign(.tbss), while p_vaddr(PT_TLS)=p_vaddr(PT_LOAD) may not
259785868e8aSDimitry Andric       // be congruent to 0 modulo p_align(PT_TLS).
259885868e8aSDimitry Andric       //
259985868e8aSDimitry Andric       // Technically this is not required, but as of 2019, some dynamic loaders
260085868e8aSDimitry Andric       // don't handle p_vaddr%p_align != 0 correctly, e.g. glibc (i386 and
260185868e8aSDimitry Andric       // x86-64) doesn't make runtime address congruent to p_vaddr modulo
260285868e8aSDimitry Andric       // p_align for dynamic TLS blocks (PR/24606), FreeBSD rtld has the same
260385868e8aSDimitry Andric       // bug, musl (TLS Variant 1 architectures) before 1.1.23 handled TLS
260485868e8aSDimitry Andric       // blocks correctly. We need to keep the workaround for a while.
260585868e8aSDimitry Andric       else if (Out::tlsPhdr && Out::tlsPhdr->firstSec == p->firstSec)
260685868e8aSDimitry Andric         cmd->addrExpr = [] {
2607972a253aSDimitry Andric           return alignToPowerOf2(script->getDot(), config->maxPageSize) +
2608972a253aSDimitry Andric                  alignToPowerOf2(script->getDot() % config->maxPageSize,
260985868e8aSDimitry Andric                                  Out::tlsPhdr->p_align);
261085868e8aSDimitry Andric         };
261185868e8aSDimitry Andric       else
261285868e8aSDimitry Andric         cmd->addrExpr = [] {
2613972a253aSDimitry Andric           return alignToPowerOf2(script->getDot(), config->maxPageSize) +
261485868e8aSDimitry Andric                  script->getDot() % config->maxPageSize;
261585868e8aSDimitry Andric         };
261685868e8aSDimitry Andric     }
26170b57cec5SDimitry Andric   };
26180b57cec5SDimitry Andric 
26190b57cec5SDimitry Andric   for (Partition &part : partitions) {
262085868e8aSDimitry Andric     prev = nullptr;
26210b57cec5SDimitry Andric     for (const PhdrEntry *p : part.phdrs)
262285868e8aSDimitry Andric       if (p->p_type == PT_LOAD && p->firstSec) {
262385868e8aSDimitry Andric         pageAlign(p);
262485868e8aSDimitry Andric         prev = p;
262585868e8aSDimitry Andric       }
26260b57cec5SDimitry Andric   }
26270b57cec5SDimitry Andric }
26280b57cec5SDimitry Andric 
26290b57cec5SDimitry Andric // Compute an in-file position for a given section. The file offset must be the
26300b57cec5SDimitry Andric // same with its virtual address modulo the page size, so that the loader can
26310b57cec5SDimitry Andric // load executables without any address adjustment.
26320b57cec5SDimitry Andric static uint64_t computeFileOffset(OutputSection *os, uint64_t off) {
26330b57cec5SDimitry Andric   // The first section in a PT_LOAD has to have congruent offset and address
263485868e8aSDimitry Andric   // modulo the maximum page size.
263585868e8aSDimitry Andric   if (os->ptLoad && os->ptLoad->firstSec == os)
263685868e8aSDimitry Andric     return alignTo(off, os->ptLoad->p_align, os->addr);
26370b57cec5SDimitry Andric 
26380b57cec5SDimitry Andric   // File offsets are not significant for .bss sections other than the first one
2639349cc55cSDimitry Andric   // in a PT_LOAD/PT_TLS. By convention, we keep section offsets monotonically
26400b57cec5SDimitry Andric   // increasing rather than setting to zero.
2641349cc55cSDimitry Andric   if (os->type == SHT_NOBITS &&
2642349cc55cSDimitry Andric       (!Out::tlsPhdr || Out::tlsPhdr->firstSec != os))
26430b57cec5SDimitry Andric      return off;
26440b57cec5SDimitry Andric 
26450b57cec5SDimitry Andric   // If the section is not in a PT_LOAD, we just have to align it.
26460b57cec5SDimitry Andric   if (!os->ptLoad)
2647bdd1243dSDimitry Andric      return alignToPowerOf2(off, os->addralign);
26480b57cec5SDimitry Andric 
26490b57cec5SDimitry Andric   // If two sections share the same PT_LOAD the file offset is calculated
26500b57cec5SDimitry Andric   // using this formula: Off2 = Off1 + (VA2 - VA1).
26510b57cec5SDimitry Andric   OutputSection *first = os->ptLoad->firstSec;
26520b57cec5SDimitry Andric   return first->offset + os->addr - first->addr;
26530b57cec5SDimitry Andric }
26540b57cec5SDimitry Andric 
26550b57cec5SDimitry Andric template <class ELFT> void Writer<ELFT>::assignFileOffsetsBinary() {
2656e8d8bef9SDimitry Andric   // Compute the minimum LMA of all non-empty non-NOBITS sections as minAddr.
2657e8d8bef9SDimitry Andric   auto needsOffset = [](OutputSection &sec) {
2658e8d8bef9SDimitry Andric     return sec.type != SHT_NOBITS && (sec.flags & SHF_ALLOC) && sec.size > 0;
2659e8d8bef9SDimitry Andric   };
2660e8d8bef9SDimitry Andric   uint64_t minAddr = UINT64_MAX;
26610b57cec5SDimitry Andric   for (OutputSection *sec : outputSections)
2662e8d8bef9SDimitry Andric     if (needsOffset(*sec)) {
2663e8d8bef9SDimitry Andric       sec->offset = sec->getLMA();
2664e8d8bef9SDimitry Andric       minAddr = std::min(minAddr, sec->offset);
2665e8d8bef9SDimitry Andric     }
2666e8d8bef9SDimitry Andric 
2667e8d8bef9SDimitry Andric   // Sections are laid out at LMA minus minAddr.
2668e8d8bef9SDimitry Andric   fileSize = 0;
2669e8d8bef9SDimitry Andric   for (OutputSection *sec : outputSections)
2670e8d8bef9SDimitry Andric     if (needsOffset(*sec)) {
2671e8d8bef9SDimitry Andric       sec->offset -= minAddr;
2672e8d8bef9SDimitry Andric       fileSize = std::max(fileSize, sec->offset + sec->size);
2673e8d8bef9SDimitry Andric     }
26740b57cec5SDimitry Andric }
26750b57cec5SDimitry Andric 
26760b57cec5SDimitry Andric static std::string rangeToString(uint64_t addr, uint64_t len) {
26770b57cec5SDimitry Andric   return "[0x" + utohexstr(addr) + ", 0x" + utohexstr(addr + len - 1) + "]";
26780b57cec5SDimitry Andric }
26790b57cec5SDimitry Andric 
26800b57cec5SDimitry Andric // Assign file offsets to output sections.
26810b57cec5SDimitry Andric template <class ELFT> void Writer<ELFT>::assignFileOffsets() {
26824824e7fdSDimitry Andric   Out::programHeaders->offset = Out::elfHeader->size;
26834824e7fdSDimitry Andric   uint64_t off = Out::elfHeader->size + Out::programHeaders->size;
26840b57cec5SDimitry Andric 
26850b57cec5SDimitry Andric   PhdrEntry *lastRX = nullptr;
26860b57cec5SDimitry Andric   for (Partition &part : partitions)
26870b57cec5SDimitry Andric     for (PhdrEntry *p : part.phdrs)
26880b57cec5SDimitry Andric       if (p->p_type == PT_LOAD && (p->p_flags & PF_X))
26890b57cec5SDimitry Andric         lastRX = p;
26900b57cec5SDimitry Andric 
2691e8d8bef9SDimitry Andric   // Layout SHF_ALLOC sections before non-SHF_ALLOC sections. A non-SHF_ALLOC
2692e8d8bef9SDimitry Andric   // will not occupy file offsets contained by a PT_LOAD.
26930b57cec5SDimitry Andric   for (OutputSection *sec : outputSections) {
2694e8d8bef9SDimitry Andric     if (!(sec->flags & SHF_ALLOC))
2695e8d8bef9SDimitry Andric       continue;
26964824e7fdSDimitry Andric     off = computeFileOffset(sec, off);
26974824e7fdSDimitry Andric     sec->offset = off;
26984824e7fdSDimitry Andric     if (sec->type != SHT_NOBITS)
26994824e7fdSDimitry Andric       off += sec->size;
27000b57cec5SDimitry Andric 
27010b57cec5SDimitry Andric     // If this is a last section of the last executable segment and that
27020b57cec5SDimitry Andric     // segment is the last loadable segment, align the offset of the
27030b57cec5SDimitry Andric     // following section to avoid loading non-segments parts of the file.
270485868e8aSDimitry Andric     if (config->zSeparate != SeparateSegmentKind::None && lastRX &&
270585868e8aSDimitry Andric         lastRX->lastSec == sec)
2706972a253aSDimitry Andric       off = alignToPowerOf2(off, config->maxPageSize);
27070b57cec5SDimitry Andric   }
27084824e7fdSDimitry Andric   for (OutputSection *osec : outputSections)
27094824e7fdSDimitry Andric     if (!(osec->flags & SHF_ALLOC)) {
2710bdd1243dSDimitry Andric       osec->offset = alignToPowerOf2(off, osec->addralign);
27114824e7fdSDimitry Andric       off = osec->offset + osec->size;
27124824e7fdSDimitry Andric     }
27130b57cec5SDimitry Andric 
2714972a253aSDimitry Andric   sectionHeaderOff = alignToPowerOf2(off, config->wordsize);
27150b57cec5SDimitry Andric   fileSize = sectionHeaderOff + (outputSections.size() + 1) * sizeof(Elf_Shdr);
27160b57cec5SDimitry Andric 
27170b57cec5SDimitry Andric   // Our logic assumes that sections have rising VA within the same segment.
27180b57cec5SDimitry Andric   // With use of linker scripts it is possible to violate this rule and get file
27190b57cec5SDimitry Andric   // offset overlaps or overflows. That should never happen with a valid script
27200b57cec5SDimitry Andric   // which does not move the location counter backwards and usually scripts do
27210b57cec5SDimitry Andric   // not do that. Unfortunately, there are apps in the wild, for example, Linux
27220b57cec5SDimitry Andric   // kernel, which control segment distribution explicitly and move the counter
27230b57cec5SDimitry Andric   // backwards, so we have to allow doing that to support linking them. We
27240b57cec5SDimitry Andric   // perform non-critical checks for overlaps in checkSectionOverlap(), but here
27250b57cec5SDimitry Andric   // we want to prevent file size overflows because it would crash the linker.
27260b57cec5SDimitry Andric   for (OutputSection *sec : outputSections) {
27270b57cec5SDimitry Andric     if (sec->type == SHT_NOBITS)
27280b57cec5SDimitry Andric       continue;
27290b57cec5SDimitry Andric     if ((sec->offset > fileSize) || (sec->offset + sec->size > fileSize))
27300b57cec5SDimitry Andric       error("unable to place section " + sec->name + " at file offset " +
27310b57cec5SDimitry Andric             rangeToString(sec->offset, sec->size) +
27320b57cec5SDimitry Andric             "; check your linker script for overflows");
27330b57cec5SDimitry Andric   }
27340b57cec5SDimitry Andric }
27350b57cec5SDimitry Andric 
27360b57cec5SDimitry Andric // Finalize the program headers. We call this function after we assign
27370b57cec5SDimitry Andric // file offsets and VAs to all sections.
27380b57cec5SDimitry Andric template <class ELFT> void Writer<ELFT>::setPhdrs(Partition &part) {
27390b57cec5SDimitry Andric   for (PhdrEntry *p : part.phdrs) {
27400b57cec5SDimitry Andric     OutputSection *first = p->firstSec;
27410b57cec5SDimitry Andric     OutputSection *last = p->lastSec;
27420b57cec5SDimitry Andric 
274306c3fb27SDimitry Andric     // .ARM.exidx sections may not be within a single .ARM.exidx
274406c3fb27SDimitry Andric     // output section. We always want to describe just the
274506c3fb27SDimitry Andric     // SyntheticSection.
274606c3fb27SDimitry Andric     if (part.armExidx && p->p_type == PT_ARM_EXIDX) {
274706c3fb27SDimitry Andric       p->p_filesz = part.armExidx->getSize();
274806c3fb27SDimitry Andric       p->p_memsz = part.armExidx->getSize();
274906c3fb27SDimitry Andric       p->p_offset = first->offset + part.armExidx->outSecOff;
275006c3fb27SDimitry Andric       p->p_vaddr = first->addr + part.armExidx->outSecOff;
275106c3fb27SDimitry Andric       p->p_align = part.armExidx->addralign;
275206c3fb27SDimitry Andric       if (part.elfHeader)
275306c3fb27SDimitry Andric         p->p_offset -= part.elfHeader->getParent()->offset;
275406c3fb27SDimitry Andric 
275506c3fb27SDimitry Andric       if (!p->hasLMA)
275606c3fb27SDimitry Andric         p->p_paddr = first->getLMA() + part.armExidx->outSecOff;
275706c3fb27SDimitry Andric       return;
275806c3fb27SDimitry Andric     }
275906c3fb27SDimitry Andric 
27600b57cec5SDimitry Andric     if (first) {
27610b57cec5SDimitry Andric       p->p_filesz = last->offset - first->offset;
27620b57cec5SDimitry Andric       if (last->type != SHT_NOBITS)
27630b57cec5SDimitry Andric         p->p_filesz += last->size;
27640b57cec5SDimitry Andric 
27650b57cec5SDimitry Andric       p->p_memsz = last->addr + last->size - first->addr;
27660b57cec5SDimitry Andric       p->p_offset = first->offset;
27670b57cec5SDimitry Andric       p->p_vaddr = first->addr;
27680b57cec5SDimitry Andric 
27690b57cec5SDimitry Andric       // File offsets in partitions other than the main partition are relative
27700b57cec5SDimitry Andric       // to the offset of the ELF headers. Perform that adjustment now.
27710b57cec5SDimitry Andric       if (part.elfHeader)
27720b57cec5SDimitry Andric         p->p_offset -= part.elfHeader->getParent()->offset;
27730b57cec5SDimitry Andric 
27740b57cec5SDimitry Andric       if (!p->hasLMA)
27750b57cec5SDimitry Andric         p->p_paddr = first->getLMA();
27760b57cec5SDimitry Andric     }
27770b57cec5SDimitry Andric   }
27780b57cec5SDimitry Andric }
27790b57cec5SDimitry Andric 
27800b57cec5SDimitry Andric // A helper struct for checkSectionOverlap.
27810b57cec5SDimitry Andric namespace {
27820b57cec5SDimitry Andric struct SectionOffset {
27830b57cec5SDimitry Andric   OutputSection *sec;
27840b57cec5SDimitry Andric   uint64_t offset;
27850b57cec5SDimitry Andric };
27860b57cec5SDimitry Andric } // namespace
27870b57cec5SDimitry Andric 
27880b57cec5SDimitry Andric // Check whether sections overlap for a specific address range (file offsets,
2789480093f4SDimitry Andric // load and virtual addresses).
27900b57cec5SDimitry Andric static void checkOverlap(StringRef name, std::vector<SectionOffset> &sections,
27910b57cec5SDimitry Andric                          bool isVirtualAddr) {
27920b57cec5SDimitry Andric   llvm::sort(sections, [=](const SectionOffset &a, const SectionOffset &b) {
27930b57cec5SDimitry Andric     return a.offset < b.offset;
27940b57cec5SDimitry Andric   });
27950b57cec5SDimitry Andric 
27960b57cec5SDimitry Andric   // Finding overlap is easy given a vector is sorted by start position.
27970b57cec5SDimitry Andric   // If an element starts before the end of the previous element, they overlap.
27980b57cec5SDimitry Andric   for (size_t i = 1, end = sections.size(); i < end; ++i) {
27990b57cec5SDimitry Andric     SectionOffset a = sections[i - 1];
28000b57cec5SDimitry Andric     SectionOffset b = sections[i];
28010b57cec5SDimitry Andric     if (b.offset >= a.offset + a.sec->size)
28020b57cec5SDimitry Andric       continue;
28030b57cec5SDimitry Andric 
28040b57cec5SDimitry Andric     // If both sections are in OVERLAY we allow the overlapping of virtual
28050b57cec5SDimitry Andric     // addresses, because it is what OVERLAY was designed for.
28060b57cec5SDimitry Andric     if (isVirtualAddr && a.sec->inOverlay && b.sec->inOverlay)
28070b57cec5SDimitry Andric       continue;
28080b57cec5SDimitry Andric 
28090b57cec5SDimitry Andric     errorOrWarn("section " + a.sec->name + " " + name +
28100b57cec5SDimitry Andric                 " range overlaps with " + b.sec->name + "\n>>> " + a.sec->name +
28110b57cec5SDimitry Andric                 " range is " + rangeToString(a.offset, a.sec->size) + "\n>>> " +
28120b57cec5SDimitry Andric                 b.sec->name + " range is " +
28130b57cec5SDimitry Andric                 rangeToString(b.offset, b.sec->size));
28140b57cec5SDimitry Andric   }
28150b57cec5SDimitry Andric }
28160b57cec5SDimitry Andric 
28170b57cec5SDimitry Andric // Check for overlapping sections and address overflows.
28180b57cec5SDimitry Andric //
28190b57cec5SDimitry Andric // In this function we check that none of the output sections have overlapping
28200b57cec5SDimitry Andric // file offsets. For SHF_ALLOC sections we also check that the load address
28210b57cec5SDimitry Andric // ranges and the virtual address ranges don't overlap
28220b57cec5SDimitry Andric template <class ELFT> void Writer<ELFT>::checkSections() {
28230b57cec5SDimitry Andric   // First, check that section's VAs fit in available address space for target.
28240b57cec5SDimitry Andric   for (OutputSection *os : outputSections)
28250b57cec5SDimitry Andric     if ((os->addr + os->size < os->addr) ||
2826bdd1243dSDimitry Andric         (!ELFT::Is64Bits && os->addr + os->size > uint64_t(UINT32_MAX) + 1))
28270b57cec5SDimitry Andric       errorOrWarn("section " + os->name + " at 0x" + utohexstr(os->addr) +
28280b57cec5SDimitry Andric                   " of size 0x" + utohexstr(os->size) +
28290b57cec5SDimitry Andric                   " exceeds available address space");
28300b57cec5SDimitry Andric 
28310b57cec5SDimitry Andric   // Check for overlapping file offsets. In this case we need to skip any
28320b57cec5SDimitry Andric   // section marked as SHT_NOBITS. These sections don't actually occupy space in
28330b57cec5SDimitry Andric   // the file so Sec->Offset + Sec->Size can overlap with others. If --oformat
28340b57cec5SDimitry Andric   // binary is specified only add SHF_ALLOC sections are added to the output
28350b57cec5SDimitry Andric   // file so we skip any non-allocated sections in that case.
28360b57cec5SDimitry Andric   std::vector<SectionOffset> fileOffs;
28370b57cec5SDimitry Andric   for (OutputSection *sec : outputSections)
28380b57cec5SDimitry Andric     if (sec->size > 0 && sec->type != SHT_NOBITS &&
28390b57cec5SDimitry Andric         (!config->oFormatBinary || (sec->flags & SHF_ALLOC)))
28400b57cec5SDimitry Andric       fileOffs.push_back({sec, sec->offset});
28410b57cec5SDimitry Andric   checkOverlap("file", fileOffs, false);
28420b57cec5SDimitry Andric 
28430b57cec5SDimitry Andric   // When linking with -r there is no need to check for overlapping virtual/load
28440b57cec5SDimitry Andric   // addresses since those addresses will only be assigned when the final
28450b57cec5SDimitry Andric   // executable/shared object is created.
28460b57cec5SDimitry Andric   if (config->relocatable)
28470b57cec5SDimitry Andric     return;
28480b57cec5SDimitry Andric 
28490b57cec5SDimitry Andric   // Checking for overlapping virtual and load addresses only needs to take
28500b57cec5SDimitry Andric   // into account SHF_ALLOC sections since others will not be loaded.
28510b57cec5SDimitry Andric   // Furthermore, we also need to skip SHF_TLS sections since these will be
28520b57cec5SDimitry Andric   // mapped to other addresses at runtime and can therefore have overlapping
28530b57cec5SDimitry Andric   // ranges in the file.
28540b57cec5SDimitry Andric   std::vector<SectionOffset> vmas;
28550b57cec5SDimitry Andric   for (OutputSection *sec : outputSections)
28560b57cec5SDimitry Andric     if (sec->size > 0 && (sec->flags & SHF_ALLOC) && !(sec->flags & SHF_TLS))
28570b57cec5SDimitry Andric       vmas.push_back({sec, sec->addr});
28580b57cec5SDimitry Andric   checkOverlap("virtual address", vmas, true);
28590b57cec5SDimitry Andric 
28600b57cec5SDimitry Andric   // Finally, check that the load addresses don't overlap. This will usually be
28610b57cec5SDimitry Andric   // the same as the virtual addresses but can be different when using a linker
28620b57cec5SDimitry Andric   // script with AT().
28630b57cec5SDimitry Andric   std::vector<SectionOffset> lmas;
28640b57cec5SDimitry Andric   for (OutputSection *sec : outputSections)
28650b57cec5SDimitry Andric     if (sec->size > 0 && (sec->flags & SHF_ALLOC) && !(sec->flags & SHF_TLS))
28660b57cec5SDimitry Andric       lmas.push_back({sec, sec->getLMA()});
28670b57cec5SDimitry Andric   checkOverlap("load address", lmas, false);
28680b57cec5SDimitry Andric }
28690b57cec5SDimitry Andric 
28700b57cec5SDimitry Andric // The entry point address is chosen in the following ways.
28710b57cec5SDimitry Andric //
28720b57cec5SDimitry Andric // 1. the '-e' entry command-line option;
28730b57cec5SDimitry Andric // 2. the ENTRY(symbol) command in a linker control script;
28740b57cec5SDimitry Andric // 3. the value of the symbol _start, if present;
28750b57cec5SDimitry Andric // 4. the number represented by the entry symbol, if it is a number;
2876349cc55cSDimitry Andric // 5. the address 0.
28770b57cec5SDimitry Andric static uint64_t getEntryAddr() {
28780b57cec5SDimitry Andric   // Case 1, 2 or 3
2879bdd1243dSDimitry Andric   if (Symbol *b = symtab.find(config->entry))
28800b57cec5SDimitry Andric     return b->getVA();
28810b57cec5SDimitry Andric 
28820b57cec5SDimitry Andric   // Case 4
28830b57cec5SDimitry Andric   uint64_t addr;
28840b57cec5SDimitry Andric   if (to_integer(config->entry, addr))
28850b57cec5SDimitry Andric     return addr;
28860b57cec5SDimitry Andric 
28870b57cec5SDimitry Andric   // Case 5
28880b57cec5SDimitry Andric   if (config->warnMissingEntry)
28890b57cec5SDimitry Andric     warn("cannot find entry symbol " + config->entry +
28900b57cec5SDimitry Andric          "; not setting start address");
28910b57cec5SDimitry Andric   return 0;
28920b57cec5SDimitry Andric }
28930b57cec5SDimitry Andric 
28940b57cec5SDimitry Andric static uint16_t getELFType() {
28950b57cec5SDimitry Andric   if (config->isPic)
28960b57cec5SDimitry Andric     return ET_DYN;
28970b57cec5SDimitry Andric   if (config->relocatable)
28980b57cec5SDimitry Andric     return ET_REL;
28990b57cec5SDimitry Andric   return ET_EXEC;
29000b57cec5SDimitry Andric }
29010b57cec5SDimitry Andric 
29020b57cec5SDimitry Andric template <class ELFT> void Writer<ELFT>::writeHeader() {
29030b57cec5SDimitry Andric   writeEhdr<ELFT>(Out::bufferStart, *mainPart);
29040b57cec5SDimitry Andric   writePhdrs<ELFT>(Out::bufferStart + sizeof(Elf_Ehdr), *mainPart);
29050b57cec5SDimitry Andric 
29060b57cec5SDimitry Andric   auto *eHdr = reinterpret_cast<Elf_Ehdr *>(Out::bufferStart);
29070b57cec5SDimitry Andric   eHdr->e_type = getELFType();
29080b57cec5SDimitry Andric   eHdr->e_entry = getEntryAddr();
29090b57cec5SDimitry Andric   eHdr->e_shoff = sectionHeaderOff;
29100b57cec5SDimitry Andric 
29110b57cec5SDimitry Andric   // Write the section header table.
29120b57cec5SDimitry Andric   //
29130b57cec5SDimitry Andric   // The ELF header can only store numbers up to SHN_LORESERVE in the e_shnum
29140b57cec5SDimitry Andric   // and e_shstrndx fields. When the value of one of these fields exceeds
29150b57cec5SDimitry Andric   // SHN_LORESERVE ELF requires us to put sentinel values in the ELF header and
29160b57cec5SDimitry Andric   // use fields in the section header at index 0 to store
29170b57cec5SDimitry Andric   // the value. The sentinel values and fields are:
29180b57cec5SDimitry Andric   // e_shnum = 0, SHdrs[0].sh_size = number of sections.
29190b57cec5SDimitry Andric   // e_shstrndx = SHN_XINDEX, SHdrs[0].sh_link = .shstrtab section index.
29200b57cec5SDimitry Andric   auto *sHdrs = reinterpret_cast<Elf_Shdr *>(Out::bufferStart + eHdr->e_shoff);
29210b57cec5SDimitry Andric   size_t num = outputSections.size() + 1;
29220b57cec5SDimitry Andric   if (num >= SHN_LORESERVE)
29230b57cec5SDimitry Andric     sHdrs->sh_size = num;
29240b57cec5SDimitry Andric   else
29250b57cec5SDimitry Andric     eHdr->e_shnum = num;
29260b57cec5SDimitry Andric 
29270b57cec5SDimitry Andric   uint32_t strTabIndex = in.shStrTab->getParent()->sectionIndex;
29280b57cec5SDimitry Andric   if (strTabIndex >= SHN_LORESERVE) {
29290b57cec5SDimitry Andric     sHdrs->sh_link = strTabIndex;
29300b57cec5SDimitry Andric     eHdr->e_shstrndx = SHN_XINDEX;
29310b57cec5SDimitry Andric   } else {
29320b57cec5SDimitry Andric     eHdr->e_shstrndx = strTabIndex;
29330b57cec5SDimitry Andric   }
29340b57cec5SDimitry Andric 
29350b57cec5SDimitry Andric   for (OutputSection *sec : outputSections)
29360b57cec5SDimitry Andric     sec->writeHeaderTo<ELFT>(++sHdrs);
29370b57cec5SDimitry Andric }
29380b57cec5SDimitry Andric 
29390b57cec5SDimitry Andric // Open a result file.
29400b57cec5SDimitry Andric template <class ELFT> void Writer<ELFT>::openFile() {
29410b57cec5SDimitry Andric   uint64_t maxSize = config->is64 ? INT64_MAX : UINT32_MAX;
29420b57cec5SDimitry Andric   if (fileSize != size_t(fileSize) || maxSize < fileSize) {
2943e8d8bef9SDimitry Andric     std::string msg;
2944e8d8bef9SDimitry Andric     raw_string_ostream s(msg);
2945e8d8bef9SDimitry Andric     s << "output file too large: " << Twine(fileSize) << " bytes\n"
2946e8d8bef9SDimitry Andric       << "section sizes:\n";
2947e8d8bef9SDimitry Andric     for (OutputSection *os : outputSections)
2948e8d8bef9SDimitry Andric       s << os->name << ' ' << os->size << "\n";
2949e8d8bef9SDimitry Andric     error(s.str());
29500b57cec5SDimitry Andric     return;
29510b57cec5SDimitry Andric   }
29520b57cec5SDimitry Andric 
29530b57cec5SDimitry Andric   unlinkAsync(config->outputFile);
29540b57cec5SDimitry Andric   unsigned flags = 0;
29550b57cec5SDimitry Andric   if (!config->relocatable)
2956480093f4SDimitry Andric     flags |= FileOutputBuffer::F_executable;
2957480093f4SDimitry Andric   if (!config->mmapOutputFile)
2958480093f4SDimitry Andric     flags |= FileOutputBuffer::F_no_mmap;
29590b57cec5SDimitry Andric   Expected<std::unique_ptr<FileOutputBuffer>> bufferOrErr =
29600b57cec5SDimitry Andric       FileOutputBuffer::create(config->outputFile, fileSize, flags);
29610b57cec5SDimitry Andric 
29620b57cec5SDimitry Andric   if (!bufferOrErr) {
29630b57cec5SDimitry Andric     error("failed to open " + config->outputFile + ": " +
29640b57cec5SDimitry Andric           llvm::toString(bufferOrErr.takeError()));
29650b57cec5SDimitry Andric     return;
29660b57cec5SDimitry Andric   }
29670b57cec5SDimitry Andric   buffer = std::move(*bufferOrErr);
29680b57cec5SDimitry Andric   Out::bufferStart = buffer->getBufferStart();
29690b57cec5SDimitry Andric }
29700b57cec5SDimitry Andric 
29710b57cec5SDimitry Andric template <class ELFT> void Writer<ELFT>::writeSectionsBinary() {
2972bdd1243dSDimitry Andric   parallel::TaskGroup tg;
29730b57cec5SDimitry Andric   for (OutputSection *sec : outputSections)
29740b57cec5SDimitry Andric     if (sec->flags & SHF_ALLOC)
2975bdd1243dSDimitry Andric       sec->writeTo<ELFT>(Out::bufferStart + sec->offset, tg);
29760b57cec5SDimitry Andric }
29770b57cec5SDimitry Andric 
29780b57cec5SDimitry Andric static void fillTrap(uint8_t *i, uint8_t *end) {
29790b57cec5SDimitry Andric   for (; i + 4 <= end; i += 4)
29800b57cec5SDimitry Andric     memcpy(i, &target->trapInstr, 4);
29810b57cec5SDimitry Andric }
29820b57cec5SDimitry Andric 
29830b57cec5SDimitry Andric // Fill the last page of executable segments with trap instructions
29840b57cec5SDimitry Andric // instead of leaving them as zero. Even though it is not required by any
29850b57cec5SDimitry Andric // standard, it is in general a good thing to do for security reasons.
29860b57cec5SDimitry Andric //
29870b57cec5SDimitry Andric // We'll leave other pages in segments as-is because the rest will be
29880b57cec5SDimitry Andric // overwritten by output sections.
29890b57cec5SDimitry Andric template <class ELFT> void Writer<ELFT>::writeTrapInstr() {
29900b57cec5SDimitry Andric   for (Partition &part : partitions) {
29910b57cec5SDimitry Andric     // Fill the last page.
29920b57cec5SDimitry Andric     for (PhdrEntry *p : part.phdrs)
29930b57cec5SDimitry Andric       if (p->p_type == PT_LOAD && (p->p_flags & PF_X))
299404eeddc0SDimitry Andric         fillTrap(Out::bufferStart +
299504eeddc0SDimitry Andric                      alignDown(p->firstSec->offset + p->p_filesz, 4),
2996972a253aSDimitry Andric                  Out::bufferStart +
2997972a253aSDimitry Andric                      alignToPowerOf2(p->firstSec->offset + p->p_filesz,
29984824e7fdSDimitry Andric                                      config->maxPageSize));
29990b57cec5SDimitry Andric 
30000b57cec5SDimitry Andric     // Round up the file size of the last segment to the page boundary iff it is
30010b57cec5SDimitry Andric     // an executable segment to ensure that other tools don't accidentally
30020b57cec5SDimitry Andric     // trim the instruction padding (e.g. when stripping the file).
30030b57cec5SDimitry Andric     PhdrEntry *last = nullptr;
30040b57cec5SDimitry Andric     for (PhdrEntry *p : part.phdrs)
30050b57cec5SDimitry Andric       if (p->p_type == PT_LOAD)
30060b57cec5SDimitry Andric         last = p;
30070b57cec5SDimitry Andric 
30080b57cec5SDimitry Andric     if (last && (last->p_flags & PF_X))
30090b57cec5SDimitry Andric       last->p_memsz = last->p_filesz =
3010972a253aSDimitry Andric           alignToPowerOf2(last->p_filesz, config->maxPageSize);
30110b57cec5SDimitry Andric   }
30120b57cec5SDimitry Andric }
30130b57cec5SDimitry Andric 
30140b57cec5SDimitry Andric // Write section contents to a mmap'ed file.
30150b57cec5SDimitry Andric template <class ELFT> void Writer<ELFT>::writeSections() {
30160eae32dcSDimitry Andric   llvm::TimeTraceScope timeScope("Write sections");
30170eae32dcSDimitry Andric 
3018bdd1243dSDimitry Andric   {
3019349cc55cSDimitry Andric     // In -r or --emit-relocs mode, write the relocation sections first as in
30200b57cec5SDimitry Andric     // ELf_Rel targets we might find out that we need to modify the relocated
30210b57cec5SDimitry Andric     // section while doing it.
3022bdd1243dSDimitry Andric     parallel::TaskGroup tg;
30230b57cec5SDimitry Andric     for (OutputSection *sec : outputSections)
30240b57cec5SDimitry Andric       if (sec->type == SHT_REL || sec->type == SHT_RELA)
3025bdd1243dSDimitry Andric         sec->writeTo<ELFT>(Out::bufferStart + sec->offset, tg);
3026bdd1243dSDimitry Andric   }
3027bdd1243dSDimitry Andric   {
3028bdd1243dSDimitry Andric     parallel::TaskGroup tg;
30290b57cec5SDimitry Andric     for (OutputSection *sec : outputSections)
30300b57cec5SDimitry Andric       if (sec->type != SHT_REL && sec->type != SHT_RELA)
3031bdd1243dSDimitry Andric         sec->writeTo<ELFT>(Out::bufferStart + sec->offset, tg);
3032bdd1243dSDimitry Andric   }
30330b57cec5SDimitry Andric 
3034fe6060f1SDimitry Andric   // Finally, check that all dynamic relocation addends were written correctly.
3035fe6060f1SDimitry Andric   if (config->checkDynamicRelocs && config->writeAddends) {
3036fe6060f1SDimitry Andric     for (OutputSection *sec : outputSections)
3037fe6060f1SDimitry Andric       if (sec->type == SHT_REL || sec->type == SHT_RELA)
3038fe6060f1SDimitry Andric         sec->checkDynRelAddends(Out::bufferStart);
30390b57cec5SDimitry Andric   }
30400b57cec5SDimitry Andric }
30410b57cec5SDimitry Andric 
30420b57cec5SDimitry Andric // Computes a hash value of Data using a given hash function.
30430b57cec5SDimitry Andric // In order to utilize multiple cores, we first split data into 1MB
30440b57cec5SDimitry Andric // chunks, compute a hash for each chunk, and then compute a hash value
30450b57cec5SDimitry Andric // of the hash values.
30460b57cec5SDimitry Andric static void
30470b57cec5SDimitry Andric computeHash(llvm::MutableArrayRef<uint8_t> hashBuf,
30480b57cec5SDimitry Andric             llvm::ArrayRef<uint8_t> data,
30490b57cec5SDimitry Andric             std::function<void(uint8_t *dest, ArrayRef<uint8_t> arr)> hashFn) {
30500b57cec5SDimitry Andric   std::vector<ArrayRef<uint8_t>> chunks = split(data, 1024 * 1024);
305104eeddc0SDimitry Andric   const size_t hashesSize = chunks.size() * hashBuf.size();
305204eeddc0SDimitry Andric   std::unique_ptr<uint8_t[]> hashes(new uint8_t[hashesSize]);
30530b57cec5SDimitry Andric 
30540b57cec5SDimitry Andric   // Compute hash values.
305581ad6265SDimitry Andric   parallelFor(0, chunks.size(), [&](size_t i) {
305604eeddc0SDimitry Andric     hashFn(hashes.get() + i * hashBuf.size(), chunks[i]);
30570b57cec5SDimitry Andric   });
30580b57cec5SDimitry Andric 
30590b57cec5SDimitry Andric   // Write to the final output buffer.
3060bdd1243dSDimitry Andric   hashFn(hashBuf.data(), ArrayRef(hashes.get(), hashesSize));
30610b57cec5SDimitry Andric }
30620b57cec5SDimitry Andric 
30630b57cec5SDimitry Andric template <class ELFT> void Writer<ELFT>::writeBuildId() {
30640b57cec5SDimitry Andric   if (!mainPart->buildId || !mainPart->buildId->getParent())
30650b57cec5SDimitry Andric     return;
30660b57cec5SDimitry Andric 
30670b57cec5SDimitry Andric   if (config->buildId == BuildIdKind::Hexstring) {
30680b57cec5SDimitry Andric     for (Partition &part : partitions)
30690b57cec5SDimitry Andric       part.buildId->writeBuildId(config->buildIdVector);
30700b57cec5SDimitry Andric     return;
30710b57cec5SDimitry Andric   }
30720b57cec5SDimitry Andric 
30730b57cec5SDimitry Andric   // Compute a hash of all sections of the output file.
30740b57cec5SDimitry Andric   size_t hashSize = mainPart->buildId->hashSize;
307504eeddc0SDimitry Andric   std::unique_ptr<uint8_t[]> buildId(new uint8_t[hashSize]);
307604eeddc0SDimitry Andric   MutableArrayRef<uint8_t> output(buildId.get(), hashSize);
307704eeddc0SDimitry Andric   llvm::ArrayRef<uint8_t> input{Out::bufferStart, size_t(fileSize)};
30780b57cec5SDimitry Andric 
307981ad6265SDimitry Andric   // Fedora introduced build ID as "approximation of true uniqueness across all
308081ad6265SDimitry Andric   // binaries that might be used by overlapping sets of people". It does not
308181ad6265SDimitry Andric   // need some security goals that some hash algorithms strive to provide, e.g.
308281ad6265SDimitry Andric   // (second-)preimage and collision resistance. In practice people use 'md5'
308381ad6265SDimitry Andric   // and 'sha1' just for different lengths. Implement them with the more
308481ad6265SDimitry Andric   // efficient BLAKE3.
30850b57cec5SDimitry Andric   switch (config->buildId) {
30860b57cec5SDimitry Andric   case BuildIdKind::Fast:
308704eeddc0SDimitry Andric     computeHash(output, input, [](uint8_t *dest, ArrayRef<uint8_t> arr) {
308806c3fb27SDimitry Andric       write64le(dest, xxh3_64bits(arr));
30890b57cec5SDimitry Andric     });
30900b57cec5SDimitry Andric     break;
30910b57cec5SDimitry Andric   case BuildIdKind::Md5:
309204eeddc0SDimitry Andric     computeHash(output, input, [&](uint8_t *dest, ArrayRef<uint8_t> arr) {
309381ad6265SDimitry Andric       memcpy(dest, BLAKE3::hash<16>(arr).data(), hashSize);
30940b57cec5SDimitry Andric     });
30950b57cec5SDimitry Andric     break;
30960b57cec5SDimitry Andric   case BuildIdKind::Sha1:
309704eeddc0SDimitry Andric     computeHash(output, input, [&](uint8_t *dest, ArrayRef<uint8_t> arr) {
309881ad6265SDimitry Andric       memcpy(dest, BLAKE3::hash<20>(arr).data(), hashSize);
30990b57cec5SDimitry Andric     });
31000b57cec5SDimitry Andric     break;
31010b57cec5SDimitry Andric   case BuildIdKind::Uuid:
310204eeddc0SDimitry Andric     if (auto ec = llvm::getRandomBytes(buildId.get(), hashSize))
31030b57cec5SDimitry Andric       error("entropy source failure: " + ec.message());
31040b57cec5SDimitry Andric     break;
31050b57cec5SDimitry Andric   default:
31060b57cec5SDimitry Andric     llvm_unreachable("unknown BuildIdKind");
31070b57cec5SDimitry Andric   }
31080b57cec5SDimitry Andric   for (Partition &part : partitions)
310904eeddc0SDimitry Andric     part.buildId->writeBuildId(output);
31100b57cec5SDimitry Andric }
31110b57cec5SDimitry Andric 
31125ffd83dbSDimitry Andric template void elf::createSyntheticSections<ELF32LE>();
31135ffd83dbSDimitry Andric template void elf::createSyntheticSections<ELF32BE>();
31145ffd83dbSDimitry Andric template void elf::createSyntheticSections<ELF64LE>();
31155ffd83dbSDimitry Andric template void elf::createSyntheticSections<ELF64BE>();
311685868e8aSDimitry Andric 
31175ffd83dbSDimitry Andric template void elf::writeResult<ELF32LE>();
31185ffd83dbSDimitry Andric template void elf::writeResult<ELF32BE>();
31195ffd83dbSDimitry Andric template void elf::writeResult<ELF64LE>();
31205ffd83dbSDimitry Andric template void elf::writeResult<ELF64BE>();
3121