xref: /freebsd/contrib/llvm-project/lld/ELF/Writer.cpp (revision fe6060f10f634930ff71b7c50291ddc610da2475)
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"
140b57cec5SDimitry Andric #include "LinkerScript.h"
150b57cec5SDimitry Andric #include "MapFile.h"
160b57cec5SDimitry Andric #include "OutputSections.h"
170b57cec5SDimitry Andric #include "Relocations.h"
180b57cec5SDimitry Andric #include "SymbolTable.h"
190b57cec5SDimitry Andric #include "Symbols.h"
200b57cec5SDimitry Andric #include "SyntheticSections.h"
210b57cec5SDimitry Andric #include "Target.h"
22*fe6060f1SDimitry Andric #include "lld/Common/Arrays.h"
230b57cec5SDimitry Andric #include "lld/Common/Filesystem.h"
240b57cec5SDimitry Andric #include "lld/Common/Memory.h"
250b57cec5SDimitry Andric #include "lld/Common/Strings.h"
260b57cec5SDimitry Andric #include "llvm/ADT/StringMap.h"
270b57cec5SDimitry Andric #include "llvm/ADT/StringSwitch.h"
285ffd83dbSDimitry Andric #include "llvm/Support/Parallel.h"
290b57cec5SDimitry Andric #include "llvm/Support/RandomNumberGenerator.h"
300b57cec5SDimitry Andric #include "llvm/Support/SHA1.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 copyLocalSymbols();
570b57cec5SDimitry Andric   void addSectionSymbols();
580b57cec5SDimitry Andric   void forEachRelSec(llvm::function_ref<void(InputSectionBase &)> fn);
590b57cec5SDimitry Andric   void sortSections();
600b57cec5SDimitry Andric   void resolveShfLinkOrder();
610b57cec5SDimitry Andric   void finalizeAddressDependentContent();
625ffd83dbSDimitry Andric   void optimizeBasicBlockJumps();
630b57cec5SDimitry Andric   void sortInputSections();
640b57cec5SDimitry Andric   void finalizeSections();
650b57cec5SDimitry Andric   void checkExecuteOnly();
660b57cec5SDimitry Andric   void setReservedSymbolSections();
670b57cec5SDimitry Andric 
680b57cec5SDimitry Andric   std::vector<PhdrEntry *> createPhdrs(Partition &part);
690b57cec5SDimitry Andric   void addPhdrForSection(Partition &part, unsigned shType, unsigned pType,
700b57cec5SDimitry Andric                          unsigned pFlags);
710b57cec5SDimitry Andric   void assignFileOffsets();
720b57cec5SDimitry Andric   void assignFileOffsetsBinary();
730b57cec5SDimitry Andric   void setPhdrs(Partition &part);
740b57cec5SDimitry Andric   void checkSections();
750b57cec5SDimitry Andric   void fixSectionAlignments();
760b57cec5SDimitry Andric   void openFile();
770b57cec5SDimitry Andric   void writeTrapInstr();
780b57cec5SDimitry Andric   void writeHeader();
790b57cec5SDimitry Andric   void writeSections();
800b57cec5SDimitry Andric   void writeSectionsBinary();
810b57cec5SDimitry Andric   void writeBuildId();
820b57cec5SDimitry Andric 
830b57cec5SDimitry Andric   std::unique_ptr<FileOutputBuffer> &buffer;
840b57cec5SDimitry Andric 
850b57cec5SDimitry Andric   void addRelIpltSymbols();
860b57cec5SDimitry Andric   void addStartEndSymbols();
870b57cec5SDimitry Andric   void addStartStopSymbols(OutputSection *sec);
880b57cec5SDimitry Andric 
890b57cec5SDimitry Andric   uint64_t fileSize;
900b57cec5SDimitry Andric   uint64_t sectionHeaderOff;
910b57cec5SDimitry Andric };
920b57cec5SDimitry Andric } // anonymous namespace
930b57cec5SDimitry Andric 
940b57cec5SDimitry Andric static bool isSectionPrefix(StringRef prefix, StringRef name) {
950b57cec5SDimitry Andric   return name.startswith(prefix) || name == prefix.drop_back();
960b57cec5SDimitry Andric }
970b57cec5SDimitry Andric 
985ffd83dbSDimitry Andric StringRef elf::getOutputSectionName(const InputSectionBase *s) {
990b57cec5SDimitry Andric   if (config->relocatable)
1000b57cec5SDimitry Andric     return s->name;
1010b57cec5SDimitry Andric 
1020b57cec5SDimitry Andric   // This is for --emit-relocs. If .text.foo is emitted as .text.bar, we want
1030b57cec5SDimitry Andric   // to emit .rela.text.foo as .rela.text.bar for consistency (this is not
1040b57cec5SDimitry Andric   // technically required, but not doing it is odd). This code guarantees that.
1050b57cec5SDimitry Andric   if (auto *isec = dyn_cast<InputSection>(s)) {
1060b57cec5SDimitry Andric     if (InputSectionBase *rel = isec->getRelocatedSection()) {
1070b57cec5SDimitry Andric       OutputSection *out = rel->getOutputSection();
1080b57cec5SDimitry Andric       if (s->type == SHT_RELA)
1090b57cec5SDimitry Andric         return saver.save(".rela" + out->name);
1100b57cec5SDimitry Andric       return saver.save(".rel" + out->name);
1110b57cec5SDimitry Andric     }
1120b57cec5SDimitry Andric   }
1130b57cec5SDimitry Andric 
1145ffd83dbSDimitry Andric   // A BssSection created for a common symbol is identified as "COMMON" in
1155ffd83dbSDimitry Andric   // linker scripts. It should go to .bss section.
1165ffd83dbSDimitry Andric   if (s->name == "COMMON")
1175ffd83dbSDimitry Andric     return ".bss";
1185ffd83dbSDimitry Andric 
1195ffd83dbSDimitry Andric   if (script->hasSectionsCommand)
1205ffd83dbSDimitry Andric     return s->name;
1215ffd83dbSDimitry Andric 
1225ffd83dbSDimitry Andric   // When no SECTIONS is specified, emulate GNU ld's internal linker scripts
1235ffd83dbSDimitry Andric   // by grouping sections with certain prefixes.
1245ffd83dbSDimitry Andric 
1255ffd83dbSDimitry Andric   // GNU ld places text sections with prefix ".text.hot.", ".text.unknown.",
1265ffd83dbSDimitry Andric   // ".text.unlikely.", ".text.startup." or ".text.exit." before others.
1275ffd83dbSDimitry Andric   // We provide an option -z keep-text-section-prefix to group such sections
1285ffd83dbSDimitry Andric   // into separate output sections. This is more flexible. See also
1295ffd83dbSDimitry Andric   // sortISDBySectionOrder().
1305ffd83dbSDimitry Andric   // ".text.unknown" means the hotness of the section is unknown. When
1315ffd83dbSDimitry Andric   // SampleFDO is used, if a function doesn't have sample, it could be very
1325ffd83dbSDimitry Andric   // cold or it could be a new function never being sampled. Those functions
1335ffd83dbSDimitry Andric   // will be kept in the ".text.unknown" section.
134e8d8bef9SDimitry Andric   // ".text.split." holds symbols which are split out from functions in other
135e8d8bef9SDimitry Andric   // input sections. For example, with -fsplit-machine-functions, placing the
136e8d8bef9SDimitry Andric   // cold parts in .text.split instead of .text.unlikely mitigates against poor
137e8d8bef9SDimitry Andric   // profile inaccuracy. Techniques such as hugepage remapping can make
138e8d8bef9SDimitry Andric   // conservative decisions at the section granularity.
1390b57cec5SDimitry Andric   if (config->zKeepTextSectionPrefix)
1405ffd83dbSDimitry Andric     for (StringRef v : {".text.hot.", ".text.unknown.", ".text.unlikely.",
141e8d8bef9SDimitry Andric                         ".text.startup.", ".text.exit.", ".text.split."})
1420b57cec5SDimitry Andric       if (isSectionPrefix(v, s->name))
1430b57cec5SDimitry Andric         return v.drop_back();
1440b57cec5SDimitry Andric 
1450b57cec5SDimitry Andric   for (StringRef v :
1460b57cec5SDimitry Andric        {".text.", ".rodata.", ".data.rel.ro.", ".data.", ".bss.rel.ro.",
1470b57cec5SDimitry Andric         ".bss.", ".init_array.", ".fini_array.", ".ctors.", ".dtors.", ".tbss.",
1480b57cec5SDimitry Andric         ".gcc_except_table.", ".tdata.", ".ARM.exidx.", ".ARM.extab."})
1490b57cec5SDimitry Andric     if (isSectionPrefix(v, s->name))
1500b57cec5SDimitry Andric       return v.drop_back();
1510b57cec5SDimitry Andric 
1520b57cec5SDimitry Andric   return s->name;
1530b57cec5SDimitry Andric }
1540b57cec5SDimitry Andric 
1550b57cec5SDimitry Andric static bool needsInterpSection() {
15655e4f9d5SDimitry Andric   return !config->relocatable && !config->shared &&
15755e4f9d5SDimitry Andric          !config->dynamicLinker.empty() && script->needsInterpSection();
1580b57cec5SDimitry Andric }
1590b57cec5SDimitry Andric 
1605ffd83dbSDimitry Andric template <class ELFT> void elf::writeResult() {
1615ffd83dbSDimitry Andric   Writer<ELFT>().run();
1620b57cec5SDimitry Andric }
1630b57cec5SDimitry Andric 
1645ffd83dbSDimitry Andric static void removeEmptyPTLoad(std::vector<PhdrEntry *> &phdrs) {
1655ffd83dbSDimitry Andric   auto it = std::stable_partition(
1665ffd83dbSDimitry Andric       phdrs.begin(), phdrs.end(), [&](const PhdrEntry *p) {
1675ffd83dbSDimitry Andric         if (p->p_type != PT_LOAD)
1685ffd83dbSDimitry Andric           return true;
1695ffd83dbSDimitry Andric         if (!p->firstSec)
1705ffd83dbSDimitry Andric           return false;
1715ffd83dbSDimitry Andric         uint64_t size = p->lastSec->addr + p->lastSec->size - p->firstSec->addr;
1725ffd83dbSDimitry Andric         return size != 0;
1735ffd83dbSDimitry Andric       });
1745ffd83dbSDimitry Andric 
1755ffd83dbSDimitry Andric   // Clear OutputSection::ptLoad for sections contained in removed
1765ffd83dbSDimitry Andric   // segments.
1775ffd83dbSDimitry Andric   DenseSet<PhdrEntry *> removed(it, phdrs.end());
1785ffd83dbSDimitry Andric   for (OutputSection *sec : outputSections)
1795ffd83dbSDimitry Andric     if (removed.count(sec->ptLoad))
1805ffd83dbSDimitry Andric       sec->ptLoad = nullptr;
1815ffd83dbSDimitry Andric   phdrs.erase(it, phdrs.end());
1825ffd83dbSDimitry Andric }
1835ffd83dbSDimitry Andric 
1845ffd83dbSDimitry Andric void elf::copySectionsIntoPartitions() {
1850b57cec5SDimitry Andric   std::vector<InputSectionBase *> newSections;
1860b57cec5SDimitry Andric   for (unsigned part = 2; part != partitions.size() + 1; ++part) {
1870b57cec5SDimitry Andric     for (InputSectionBase *s : inputSections) {
1880b57cec5SDimitry Andric       if (!(s->flags & SHF_ALLOC) || !s->isLive())
1890b57cec5SDimitry Andric         continue;
1900b57cec5SDimitry Andric       InputSectionBase *copy;
1910b57cec5SDimitry Andric       if (s->type == SHT_NOTE)
1920b57cec5SDimitry Andric         copy = make<InputSection>(cast<InputSection>(*s));
1930b57cec5SDimitry Andric       else if (auto *es = dyn_cast<EhInputSection>(s))
1940b57cec5SDimitry Andric         copy = make<EhInputSection>(*es);
1950b57cec5SDimitry Andric       else
1960b57cec5SDimitry Andric         continue;
1970b57cec5SDimitry Andric       copy->partition = part;
1980b57cec5SDimitry Andric       newSections.push_back(copy);
1990b57cec5SDimitry Andric     }
2000b57cec5SDimitry Andric   }
2010b57cec5SDimitry Andric 
2020b57cec5SDimitry Andric   inputSections.insert(inputSections.end(), newSections.begin(),
2030b57cec5SDimitry Andric                        newSections.end());
2040b57cec5SDimitry Andric }
2050b57cec5SDimitry Andric 
2065ffd83dbSDimitry Andric void elf::combineEhSections() {
207e8d8bef9SDimitry Andric   llvm::TimeTraceScope timeScope("Combine EH sections");
2080b57cec5SDimitry Andric   for (InputSectionBase *&s : inputSections) {
2090b57cec5SDimitry Andric     // Ignore dead sections and the partition end marker (.part.end),
2100b57cec5SDimitry Andric     // whose partition number is out of bounds.
2110b57cec5SDimitry Andric     if (!s->isLive() || s->partition == 255)
2120b57cec5SDimitry Andric       continue;
2130b57cec5SDimitry Andric 
2140b57cec5SDimitry Andric     Partition &part = s->getPartition();
2150b57cec5SDimitry Andric     if (auto *es = dyn_cast<EhInputSection>(s)) {
21685868e8aSDimitry Andric       part.ehFrame->addSection(es);
2170b57cec5SDimitry Andric       s = nullptr;
2180b57cec5SDimitry Andric     } else if (s->kind() == SectionBase::Regular && part.armExidx &&
2190b57cec5SDimitry Andric                part.armExidx->addSection(cast<InputSection>(s))) {
2200b57cec5SDimitry Andric       s = nullptr;
2210b57cec5SDimitry Andric     }
2220b57cec5SDimitry Andric   }
2230b57cec5SDimitry Andric 
2240b57cec5SDimitry Andric   std::vector<InputSectionBase *> &v = inputSections;
2250b57cec5SDimitry Andric   v.erase(std::remove(v.begin(), v.end(), nullptr), v.end());
2260b57cec5SDimitry Andric }
2270b57cec5SDimitry Andric 
2280b57cec5SDimitry Andric static Defined *addOptionalRegular(StringRef name, SectionBase *sec,
2290b57cec5SDimitry Andric                                    uint64_t val, uint8_t stOther = STV_HIDDEN,
2300b57cec5SDimitry Andric                                    uint8_t binding = STB_GLOBAL) {
2310b57cec5SDimitry Andric   Symbol *s = symtab->find(name);
2320b57cec5SDimitry Andric   if (!s || s->isDefined())
2330b57cec5SDimitry Andric     return nullptr;
2340b57cec5SDimitry Andric 
2350b57cec5SDimitry Andric   s->resolve(Defined{/*file=*/nullptr, name, binding, stOther, STT_NOTYPE, val,
2360b57cec5SDimitry Andric                      /*size=*/0, sec});
2370b57cec5SDimitry Andric   return cast<Defined>(s);
2380b57cec5SDimitry Andric }
2390b57cec5SDimitry Andric 
2400b57cec5SDimitry Andric static Defined *addAbsolute(StringRef name) {
2410b57cec5SDimitry Andric   Symbol *sym = symtab->addSymbol(Defined{nullptr, name, STB_GLOBAL, STV_HIDDEN,
2420b57cec5SDimitry Andric                                           STT_NOTYPE, 0, 0, nullptr});
2430b57cec5SDimitry Andric   return cast<Defined>(sym);
2440b57cec5SDimitry Andric }
2450b57cec5SDimitry Andric 
2460b57cec5SDimitry Andric // The linker is expected to define some symbols depending on
2470b57cec5SDimitry Andric // the linking result. This function defines such symbols.
2485ffd83dbSDimitry Andric void elf::addReservedSymbols() {
2490b57cec5SDimitry Andric   if (config->emachine == EM_MIPS) {
2500b57cec5SDimitry Andric     // Define _gp for MIPS. st_value of _gp symbol will be updated by Writer
2510b57cec5SDimitry Andric     // so that it points to an absolute address which by default is relative
2520b57cec5SDimitry Andric     // to GOT. Default offset is 0x7ff0.
2530b57cec5SDimitry Andric     // See "Global Data Symbols" in Chapter 6 in the following document:
2540b57cec5SDimitry Andric     // ftp://www.linux-mips.org/pub/linux/mips/doc/ABI/mipsabi.pdf
2550b57cec5SDimitry Andric     ElfSym::mipsGp = addAbsolute("_gp");
2560b57cec5SDimitry Andric 
2570b57cec5SDimitry Andric     // On MIPS O32 ABI, _gp_disp is a magic symbol designates offset between
2580b57cec5SDimitry Andric     // start of function and 'gp' pointer into GOT.
2590b57cec5SDimitry Andric     if (symtab->find("_gp_disp"))
2600b57cec5SDimitry Andric       ElfSym::mipsGpDisp = addAbsolute("_gp_disp");
2610b57cec5SDimitry Andric 
2620b57cec5SDimitry Andric     // The __gnu_local_gp is a magic symbol equal to the current value of 'gp'
2630b57cec5SDimitry Andric     // pointer. This symbol is used in the code generated by .cpload pseudo-op
2640b57cec5SDimitry Andric     // in case of using -mno-shared option.
2650b57cec5SDimitry Andric     // https://sourceware.org/ml/binutils/2004-12/msg00094.html
2660b57cec5SDimitry Andric     if (symtab->find("__gnu_local_gp"))
2670b57cec5SDimitry Andric       ElfSym::mipsLocalGp = addAbsolute("__gnu_local_gp");
2680b57cec5SDimitry Andric   } else if (config->emachine == EM_PPC) {
2690b57cec5SDimitry Andric     // glibc *crt1.o has a undefined reference to _SDA_BASE_. Since we don't
2700b57cec5SDimitry Andric     // support Small Data Area, define it arbitrarily as 0.
2710b57cec5SDimitry Andric     addOptionalRegular("_SDA_BASE_", nullptr, 0, STV_HIDDEN);
2725ffd83dbSDimitry Andric   } else if (config->emachine == EM_PPC64) {
2735ffd83dbSDimitry Andric     addPPC64SaveRestore();
2740b57cec5SDimitry Andric   }
2750b57cec5SDimitry Andric 
2760b57cec5SDimitry Andric   // The Power Architecture 64-bit v2 ABI defines a TableOfContents (TOC) which
2770b57cec5SDimitry Andric   // combines the typical ELF GOT with the small data sections. It commonly
2780b57cec5SDimitry Andric   // includes .got .toc .sdata .sbss. The .TOC. symbol replaces both
2790b57cec5SDimitry Andric   // _GLOBAL_OFFSET_TABLE_ and _SDA_BASE_ from the 32-bit ABI. It is used to
2800b57cec5SDimitry Andric   // represent the TOC base which is offset by 0x8000 bytes from the start of
2810b57cec5SDimitry Andric   // the .got section.
2820b57cec5SDimitry Andric   // We do not allow _GLOBAL_OFFSET_TABLE_ to be defined by input objects as the
2830b57cec5SDimitry Andric   // correctness of some relocations depends on its value.
2840b57cec5SDimitry Andric   StringRef gotSymName =
2850b57cec5SDimitry Andric       (config->emachine == EM_PPC64) ? ".TOC." : "_GLOBAL_OFFSET_TABLE_";
2860b57cec5SDimitry Andric 
2870b57cec5SDimitry Andric   if (Symbol *s = symtab->find(gotSymName)) {
2880b57cec5SDimitry Andric     if (s->isDefined()) {
2890b57cec5SDimitry Andric       error(toString(s->file) + " cannot redefine linker defined symbol '" +
2900b57cec5SDimitry Andric             gotSymName + "'");
2910b57cec5SDimitry Andric       return;
2920b57cec5SDimitry Andric     }
2930b57cec5SDimitry Andric 
2940b57cec5SDimitry Andric     uint64_t gotOff = 0;
2950b57cec5SDimitry Andric     if (config->emachine == EM_PPC64)
2960b57cec5SDimitry Andric       gotOff = 0x8000;
2970b57cec5SDimitry Andric 
2980b57cec5SDimitry Andric     s->resolve(Defined{/*file=*/nullptr, gotSymName, STB_GLOBAL, STV_HIDDEN,
2990b57cec5SDimitry Andric                        STT_NOTYPE, gotOff, /*size=*/0, Out::elfHeader});
3000b57cec5SDimitry Andric     ElfSym::globalOffsetTable = cast<Defined>(s);
3010b57cec5SDimitry Andric   }
3020b57cec5SDimitry Andric 
3030b57cec5SDimitry Andric   // __ehdr_start is the location of ELF file headers. Note that we define
3040b57cec5SDimitry Andric   // this symbol unconditionally even when using a linker script, which
3050b57cec5SDimitry Andric   // differs from the behavior implemented by GNU linker which only define
3060b57cec5SDimitry Andric   // this symbol if ELF headers are in the memory mapped segment.
3070b57cec5SDimitry Andric   addOptionalRegular("__ehdr_start", Out::elfHeader, 0, STV_HIDDEN);
3080b57cec5SDimitry Andric 
3090b57cec5SDimitry Andric   // __executable_start is not documented, but the expectation of at
3100b57cec5SDimitry Andric   // least the Android libc is that it points to the ELF header.
3110b57cec5SDimitry Andric   addOptionalRegular("__executable_start", Out::elfHeader, 0, STV_HIDDEN);
3120b57cec5SDimitry Andric 
3130b57cec5SDimitry Andric   // __dso_handle symbol is passed to cxa_finalize as a marker to identify
3140b57cec5SDimitry Andric   // each DSO. The address of the symbol doesn't matter as long as they are
3150b57cec5SDimitry Andric   // different in different DSOs, so we chose the start address of the DSO.
3160b57cec5SDimitry Andric   addOptionalRegular("__dso_handle", Out::elfHeader, 0, STV_HIDDEN);
3170b57cec5SDimitry Andric 
318480093f4SDimitry Andric   // If linker script do layout we do not need to create any standard symbols.
3190b57cec5SDimitry Andric   if (script->hasSectionsCommand)
3200b57cec5SDimitry Andric     return;
3210b57cec5SDimitry Andric 
3220b57cec5SDimitry Andric   auto add = [](StringRef s, int64_t pos) {
3230b57cec5SDimitry Andric     return addOptionalRegular(s, Out::elfHeader, pos, STV_DEFAULT);
3240b57cec5SDimitry Andric   };
3250b57cec5SDimitry Andric 
3260b57cec5SDimitry Andric   ElfSym::bss = add("__bss_start", 0);
3270b57cec5SDimitry Andric   ElfSym::end1 = add("end", -1);
3280b57cec5SDimitry Andric   ElfSym::end2 = add("_end", -1);
3290b57cec5SDimitry Andric   ElfSym::etext1 = add("etext", -1);
3300b57cec5SDimitry Andric   ElfSym::etext2 = add("_etext", -1);
3310b57cec5SDimitry Andric   ElfSym::edata1 = add("edata", -1);
3320b57cec5SDimitry Andric   ElfSym::edata2 = add("_edata", -1);
3330b57cec5SDimitry Andric }
3340b57cec5SDimitry Andric 
3350b57cec5SDimitry Andric static OutputSection *findSection(StringRef name, unsigned partition = 1) {
3360b57cec5SDimitry Andric   for (BaseCommand *base : script->sectionCommands)
3370b57cec5SDimitry Andric     if (auto *sec = dyn_cast<OutputSection>(base))
3380b57cec5SDimitry Andric       if (sec->name == name && sec->partition == partition)
3390b57cec5SDimitry Andric         return sec;
3400b57cec5SDimitry Andric   return nullptr;
3410b57cec5SDimitry Andric }
3420b57cec5SDimitry Andric 
3435ffd83dbSDimitry Andric template <class ELFT> void elf::createSyntheticSections() {
3440b57cec5SDimitry Andric   // Initialize all pointers with NULL. This is needed because
3450b57cec5SDimitry Andric   // you can call lld::elf::main more than once as a library.
3460b57cec5SDimitry Andric   memset(&Out::first, 0, sizeof(Out));
3470b57cec5SDimitry Andric 
34885868e8aSDimitry Andric   // Add the .interp section first because it is not a SyntheticSection.
34985868e8aSDimitry Andric   // The removeUnusedSyntheticSections() function relies on the
35085868e8aSDimitry Andric   // SyntheticSections coming last.
35185868e8aSDimitry Andric   if (needsInterpSection()) {
35285868e8aSDimitry Andric     for (size_t i = 1; i <= partitions.size(); ++i) {
35385868e8aSDimitry Andric       InputSection *sec = createInterpSection();
35485868e8aSDimitry Andric       sec->partition = i;
35585868e8aSDimitry Andric       inputSections.push_back(sec);
35685868e8aSDimitry Andric     }
35785868e8aSDimitry Andric   }
35885868e8aSDimitry Andric 
35985868e8aSDimitry Andric   auto add = [](SyntheticSection *sec) { inputSections.push_back(sec); };
3600b57cec5SDimitry Andric 
3610b57cec5SDimitry Andric   in.shStrTab = make<StringTableSection>(".shstrtab", false);
3620b57cec5SDimitry Andric 
3630b57cec5SDimitry Andric   Out::programHeaders = make<OutputSection>("", 0, SHF_ALLOC);
3640b57cec5SDimitry Andric   Out::programHeaders->alignment = config->wordsize;
3650b57cec5SDimitry Andric 
3660b57cec5SDimitry Andric   if (config->strip != StripPolicy::All) {
3670b57cec5SDimitry Andric     in.strTab = make<StringTableSection>(".strtab", false);
3680b57cec5SDimitry Andric     in.symTab = make<SymbolTableSection<ELFT>>(*in.strTab);
3690b57cec5SDimitry Andric     in.symTabShndx = make<SymtabShndxSection>();
3700b57cec5SDimitry Andric   }
3710b57cec5SDimitry Andric 
3720b57cec5SDimitry Andric   in.bss = make<BssSection>(".bss", 0, 1);
3730b57cec5SDimitry Andric   add(in.bss);
3740b57cec5SDimitry Andric 
3750b57cec5SDimitry Andric   // If there is a SECTIONS command and a .data.rel.ro section name use name
3760b57cec5SDimitry Andric   // .data.rel.ro.bss so that we match in the .data.rel.ro output section.
3770b57cec5SDimitry Andric   // This makes sure our relro is contiguous.
3780b57cec5SDimitry Andric   bool hasDataRelRo =
3790b57cec5SDimitry Andric       script->hasSectionsCommand && findSection(".data.rel.ro", 0);
3800b57cec5SDimitry Andric   in.bssRelRo =
3810b57cec5SDimitry Andric       make<BssSection>(hasDataRelRo ? ".data.rel.ro.bss" : ".bss.rel.ro", 0, 1);
3820b57cec5SDimitry Andric   add(in.bssRelRo);
3830b57cec5SDimitry Andric 
3840b57cec5SDimitry Andric   // Add MIPS-specific sections.
3850b57cec5SDimitry Andric   if (config->emachine == EM_MIPS) {
3860b57cec5SDimitry Andric     if (!config->shared && config->hasDynSymTab) {
3870b57cec5SDimitry Andric       in.mipsRldMap = make<MipsRldMapSection>();
3880b57cec5SDimitry Andric       add(in.mipsRldMap);
3890b57cec5SDimitry Andric     }
3900b57cec5SDimitry Andric     if (auto *sec = MipsAbiFlagsSection<ELFT>::create())
3910b57cec5SDimitry Andric       add(sec);
3920b57cec5SDimitry Andric     if (auto *sec = MipsOptionsSection<ELFT>::create())
3930b57cec5SDimitry Andric       add(sec);
3940b57cec5SDimitry Andric     if (auto *sec = MipsReginfoSection<ELFT>::create())
3950b57cec5SDimitry Andric       add(sec);
3960b57cec5SDimitry Andric   }
3970b57cec5SDimitry Andric 
39885868e8aSDimitry Andric   StringRef relaDynName = config->isRela ? ".rela.dyn" : ".rel.dyn";
39985868e8aSDimitry Andric 
4000b57cec5SDimitry Andric   for (Partition &part : partitions) {
40185868e8aSDimitry Andric     auto add = [&](SyntheticSection *sec) {
4020b57cec5SDimitry Andric       sec->partition = part.getNumber();
4030b57cec5SDimitry Andric       inputSections.push_back(sec);
4040b57cec5SDimitry Andric     };
4050b57cec5SDimitry Andric 
4060b57cec5SDimitry Andric     if (!part.name.empty()) {
4070b57cec5SDimitry Andric       part.elfHeader = make<PartitionElfHeaderSection<ELFT>>();
4080b57cec5SDimitry Andric       part.elfHeader->name = part.name;
4090b57cec5SDimitry Andric       add(part.elfHeader);
4100b57cec5SDimitry Andric 
4110b57cec5SDimitry Andric       part.programHeaders = make<PartitionProgramHeadersSection<ELFT>>();
4120b57cec5SDimitry Andric       add(part.programHeaders);
4130b57cec5SDimitry Andric     }
4140b57cec5SDimitry Andric 
4150b57cec5SDimitry Andric     if (config->buildId != BuildIdKind::None) {
4160b57cec5SDimitry Andric       part.buildId = make<BuildIdSection>();
4170b57cec5SDimitry Andric       add(part.buildId);
4180b57cec5SDimitry Andric     }
4190b57cec5SDimitry Andric 
4200b57cec5SDimitry Andric     part.dynStrTab = make<StringTableSection>(".dynstr", true);
4210b57cec5SDimitry Andric     part.dynSymTab = make<SymbolTableSection<ELFT>>(*part.dynStrTab);
4220b57cec5SDimitry Andric     part.dynamic = make<DynamicSection<ELFT>>();
42385868e8aSDimitry Andric     if (config->androidPackDynRelocs)
42485868e8aSDimitry Andric       part.relaDyn = make<AndroidPackedRelocationSection<ELFT>>(relaDynName);
42585868e8aSDimitry Andric     else
42685868e8aSDimitry Andric       part.relaDyn =
42785868e8aSDimitry Andric           make<RelocationSection<ELFT>>(relaDynName, config->zCombreloc);
4280b57cec5SDimitry Andric 
4290b57cec5SDimitry Andric     if (config->hasDynSymTab) {
4300b57cec5SDimitry Andric       part.dynSymTab = make<SymbolTableSection<ELFT>>(*part.dynStrTab);
4310b57cec5SDimitry Andric       add(part.dynSymTab);
4320b57cec5SDimitry Andric 
4330b57cec5SDimitry Andric       part.verSym = make<VersionTableSection>();
4340b57cec5SDimitry Andric       add(part.verSym);
4350b57cec5SDimitry Andric 
43685868e8aSDimitry Andric       if (!namedVersionDefs().empty()) {
4370b57cec5SDimitry Andric         part.verDef = make<VersionDefinitionSection>();
4380b57cec5SDimitry Andric         add(part.verDef);
4390b57cec5SDimitry Andric       }
4400b57cec5SDimitry Andric 
4410b57cec5SDimitry Andric       part.verNeed = make<VersionNeedSection<ELFT>>();
4420b57cec5SDimitry Andric       add(part.verNeed);
4430b57cec5SDimitry Andric 
4440b57cec5SDimitry Andric       if (config->gnuHash) {
4450b57cec5SDimitry Andric         part.gnuHashTab = make<GnuHashTableSection>();
4460b57cec5SDimitry Andric         add(part.gnuHashTab);
4470b57cec5SDimitry Andric       }
4480b57cec5SDimitry Andric 
4490b57cec5SDimitry Andric       if (config->sysvHash) {
4500b57cec5SDimitry Andric         part.hashTab = make<HashTableSection>();
4510b57cec5SDimitry Andric         add(part.hashTab);
4520b57cec5SDimitry Andric       }
4530b57cec5SDimitry Andric 
4540b57cec5SDimitry Andric       add(part.dynamic);
4550b57cec5SDimitry Andric       add(part.dynStrTab);
4560b57cec5SDimitry Andric       add(part.relaDyn);
4570b57cec5SDimitry Andric     }
4580b57cec5SDimitry Andric 
4590b57cec5SDimitry Andric     if (config->relrPackDynRelocs) {
4600b57cec5SDimitry Andric       part.relrDyn = make<RelrSection<ELFT>>();
4610b57cec5SDimitry Andric       add(part.relrDyn);
4620b57cec5SDimitry Andric     }
4630b57cec5SDimitry Andric 
4640b57cec5SDimitry Andric     if (!config->relocatable) {
4650b57cec5SDimitry Andric       if (config->ehFrameHdr) {
4660b57cec5SDimitry Andric         part.ehFrameHdr = make<EhFrameHeader>();
4670b57cec5SDimitry Andric         add(part.ehFrameHdr);
4680b57cec5SDimitry Andric       }
4690b57cec5SDimitry Andric       part.ehFrame = make<EhFrameSection>();
4700b57cec5SDimitry Andric       add(part.ehFrame);
4710b57cec5SDimitry Andric     }
4720b57cec5SDimitry Andric 
4730b57cec5SDimitry Andric     if (config->emachine == EM_ARM && !config->relocatable) {
4740b57cec5SDimitry Andric       // The ARMExidxsyntheticsection replaces all the individual .ARM.exidx
4750b57cec5SDimitry Andric       // InputSections.
4760b57cec5SDimitry Andric       part.armExidx = make<ARMExidxSyntheticSection>();
4770b57cec5SDimitry Andric       add(part.armExidx);
4780b57cec5SDimitry Andric     }
4790b57cec5SDimitry Andric   }
4800b57cec5SDimitry Andric 
4810b57cec5SDimitry Andric   if (partitions.size() != 1) {
4820b57cec5SDimitry Andric     // Create the partition end marker. This needs to be in partition number 255
4830b57cec5SDimitry Andric     // so that it is sorted after all other partitions. It also has other
4840b57cec5SDimitry Andric     // special handling (see createPhdrs() and combineEhSections()).
4850b57cec5SDimitry Andric     in.partEnd = make<BssSection>(".part.end", config->maxPageSize, 1);
4860b57cec5SDimitry Andric     in.partEnd->partition = 255;
4870b57cec5SDimitry Andric     add(in.partEnd);
4880b57cec5SDimitry Andric 
4890b57cec5SDimitry Andric     in.partIndex = make<PartitionIndexSection>();
4900b57cec5SDimitry Andric     addOptionalRegular("__part_index_begin", in.partIndex, 0);
4910b57cec5SDimitry Andric     addOptionalRegular("__part_index_end", in.partIndex,
4920b57cec5SDimitry Andric                        in.partIndex->getSize());
4930b57cec5SDimitry Andric     add(in.partIndex);
4940b57cec5SDimitry Andric   }
4950b57cec5SDimitry Andric 
4960b57cec5SDimitry Andric   // Add .got. MIPS' .got is so different from the other archs,
4970b57cec5SDimitry Andric   // it has its own class.
4980b57cec5SDimitry Andric   if (config->emachine == EM_MIPS) {
4990b57cec5SDimitry Andric     in.mipsGot = make<MipsGotSection>();
5000b57cec5SDimitry Andric     add(in.mipsGot);
5010b57cec5SDimitry Andric   } else {
5020b57cec5SDimitry Andric     in.got = make<GotSection>();
5030b57cec5SDimitry Andric     add(in.got);
5040b57cec5SDimitry Andric   }
5050b57cec5SDimitry Andric 
5060b57cec5SDimitry Andric   if (config->emachine == EM_PPC) {
5070b57cec5SDimitry Andric     in.ppc32Got2 = make<PPC32Got2Section>();
5080b57cec5SDimitry Andric     add(in.ppc32Got2);
5090b57cec5SDimitry Andric   }
5100b57cec5SDimitry Andric 
5110b57cec5SDimitry Andric   if (config->emachine == EM_PPC64) {
5120b57cec5SDimitry Andric     in.ppc64LongBranchTarget = make<PPC64LongBranchTargetSection>();
5130b57cec5SDimitry Andric     add(in.ppc64LongBranchTarget);
5140b57cec5SDimitry Andric   }
5150b57cec5SDimitry Andric 
5160b57cec5SDimitry Andric   in.gotPlt = make<GotPltSection>();
5170b57cec5SDimitry Andric   add(in.gotPlt);
5180b57cec5SDimitry Andric   in.igotPlt = make<IgotPltSection>();
5190b57cec5SDimitry Andric   add(in.igotPlt);
5200b57cec5SDimitry Andric 
5210b57cec5SDimitry Andric   // _GLOBAL_OFFSET_TABLE_ is defined relative to either .got.plt or .got. Treat
5220b57cec5SDimitry Andric   // it as a relocation and ensure the referenced section is created.
5230b57cec5SDimitry Andric   if (ElfSym::globalOffsetTable && config->emachine != EM_MIPS) {
5240b57cec5SDimitry Andric     if (target->gotBaseSymInGotPlt)
5250b57cec5SDimitry Andric       in.gotPlt->hasGotPltOffRel = true;
5260b57cec5SDimitry Andric     else
5270b57cec5SDimitry Andric       in.got->hasGotOffRel = true;
5280b57cec5SDimitry Andric   }
5290b57cec5SDimitry Andric 
5300b57cec5SDimitry Andric   if (config->gdbIndex)
5310b57cec5SDimitry Andric     add(GdbIndexSection::create<ELFT>());
5320b57cec5SDimitry Andric 
5330b57cec5SDimitry Andric   // We always need to add rel[a].plt to output if it has entries.
5340b57cec5SDimitry Andric   // Even for static linking it can contain R_[*]_IRELATIVE relocations.
5350b57cec5SDimitry Andric   in.relaPlt = make<RelocationSection<ELFT>>(
5360b57cec5SDimitry Andric       config->isRela ? ".rela.plt" : ".rel.plt", /*sort=*/false);
5370b57cec5SDimitry Andric   add(in.relaPlt);
5380b57cec5SDimitry Andric 
53985868e8aSDimitry Andric   // The relaIplt immediately follows .rel[a].dyn to ensure that the IRelative
54085868e8aSDimitry Andric   // relocations are processed last by the dynamic loader. We cannot place the
54185868e8aSDimitry Andric   // iplt section in .rel.dyn when Android relocation packing is enabled because
54285868e8aSDimitry Andric   // that would cause a section type mismatch. However, because the Android
54385868e8aSDimitry Andric   // dynamic loader reads .rel.plt after .rel.dyn, we can get the desired
54485868e8aSDimitry Andric   // behaviour by placing the iplt section in .rel.plt.
5450b57cec5SDimitry Andric   in.relaIplt = make<RelocationSection<ELFT>>(
54685868e8aSDimitry Andric       config->androidPackDynRelocs ? in.relaPlt->name : relaDynName,
5470b57cec5SDimitry Andric       /*sort=*/false);
5480b57cec5SDimitry Andric   add(in.relaIplt);
5490b57cec5SDimitry Andric 
550480093f4SDimitry Andric   if ((config->emachine == EM_386 || config->emachine == EM_X86_64) &&
551480093f4SDimitry Andric       (config->andFeatures & GNU_PROPERTY_X86_FEATURE_1_IBT)) {
552480093f4SDimitry Andric     in.ibtPlt = make<IBTPltSection>();
553480093f4SDimitry Andric     add(in.ibtPlt);
554480093f4SDimitry Andric   }
555480093f4SDimitry Andric 
55692c0d181SDimitry Andric   in.plt = config->emachine == EM_PPC ? make<PPC32GlinkSection>()
55792c0d181SDimitry Andric                                       : make<PltSection>();
5580b57cec5SDimitry Andric   add(in.plt);
559480093f4SDimitry Andric   in.iplt = make<IpltSection>();
5600b57cec5SDimitry Andric   add(in.iplt);
5610b57cec5SDimitry Andric 
5620b57cec5SDimitry Andric   if (config->andFeatures)
5630b57cec5SDimitry Andric     add(make<GnuPropertySection>());
5640b57cec5SDimitry Andric 
5650b57cec5SDimitry Andric   // .note.GNU-stack is always added when we are creating a re-linkable
5660b57cec5SDimitry Andric   // object file. Other linkers are using the presence of this marker
5670b57cec5SDimitry Andric   // section to control the executable-ness of the stack area, but that
5680b57cec5SDimitry Andric   // is irrelevant these days. Stack area should always be non-executable
5690b57cec5SDimitry Andric   // by default. So we emit this section unconditionally.
5700b57cec5SDimitry Andric   if (config->relocatable)
5710b57cec5SDimitry Andric     add(make<GnuStackSection>());
5720b57cec5SDimitry Andric 
5730b57cec5SDimitry Andric   if (in.symTab)
5740b57cec5SDimitry Andric     add(in.symTab);
5750b57cec5SDimitry Andric   if (in.symTabShndx)
5760b57cec5SDimitry Andric     add(in.symTabShndx);
5770b57cec5SDimitry Andric   add(in.shStrTab);
5780b57cec5SDimitry Andric   if (in.strTab)
5790b57cec5SDimitry Andric     add(in.strTab);
5800b57cec5SDimitry Andric }
5810b57cec5SDimitry Andric 
5820b57cec5SDimitry Andric // The main function of the writer.
5830b57cec5SDimitry Andric template <class ELFT> void Writer<ELFT>::run() {
5840b57cec5SDimitry Andric   copyLocalSymbols();
5850b57cec5SDimitry Andric 
5860b57cec5SDimitry Andric   if (config->copyRelocs)
5870b57cec5SDimitry Andric     addSectionSymbols();
5880b57cec5SDimitry Andric 
5890b57cec5SDimitry Andric   // Now that we have a complete set of output sections. This function
5900b57cec5SDimitry Andric   // completes section contents. For example, we need to add strings
5910b57cec5SDimitry Andric   // to the string table, and add entries to .got and .plt.
5920b57cec5SDimitry Andric   // finalizeSections does that.
5930b57cec5SDimitry Andric   finalizeSections();
5940b57cec5SDimitry Andric   checkExecuteOnly();
5950b57cec5SDimitry Andric   if (errorCount())
5960b57cec5SDimitry Andric     return;
5970b57cec5SDimitry Andric 
5980b57cec5SDimitry Andric   // If -compressed-debug-sections is specified, we need to compress
5990b57cec5SDimitry Andric   // .debug_* sections. Do it right now because it changes the size of
6000b57cec5SDimitry Andric   // output sections.
6010b57cec5SDimitry Andric   for (OutputSection *sec : outputSections)
6020b57cec5SDimitry Andric     sec->maybeCompress<ELFT>();
6030b57cec5SDimitry Andric 
60469660011SDimitry Andric   if (script->hasSectionsCommand)
6050b57cec5SDimitry Andric     script->allocateHeaders(mainPart->phdrs);
6060b57cec5SDimitry Andric 
6070b57cec5SDimitry Andric   // Remove empty PT_LOAD to avoid causing the dynamic linker to try to mmap a
6080b57cec5SDimitry Andric   // 0 sized region. This has to be done late since only after assignAddresses
6090b57cec5SDimitry Andric   // we know the size of the sections.
6100b57cec5SDimitry Andric   for (Partition &part : partitions)
6110b57cec5SDimitry Andric     removeEmptyPTLoad(part.phdrs);
6120b57cec5SDimitry Andric 
6130b57cec5SDimitry Andric   if (!config->oFormatBinary)
6140b57cec5SDimitry Andric     assignFileOffsets();
6150b57cec5SDimitry Andric   else
6160b57cec5SDimitry Andric     assignFileOffsetsBinary();
6170b57cec5SDimitry Andric 
6180b57cec5SDimitry Andric   for (Partition &part : partitions)
6190b57cec5SDimitry Andric     setPhdrs(part);
6200b57cec5SDimitry Andric 
6210b57cec5SDimitry Andric   if (config->relocatable)
6220b57cec5SDimitry Andric     for (OutputSection *sec : outputSections)
6230b57cec5SDimitry Andric       sec->addr = 0;
6240b57cec5SDimitry Andric 
6255ffd83dbSDimitry Andric   // Handle --print-map(-M)/--Map, --cref and --print-archive-stats=. Dump them
6265ffd83dbSDimitry Andric   // before checkSections() because the files may be useful in case
6275ffd83dbSDimitry Andric   // checkSections() or openFile() fails, for example, due to an erroneous file
6285ffd83dbSDimitry Andric   // size.
6295ffd83dbSDimitry Andric   writeMapFile();
6305ffd83dbSDimitry Andric   writeCrossReferenceTable();
6315ffd83dbSDimitry Andric   writeArchiveStats();
6325ffd83dbSDimitry Andric 
6330b57cec5SDimitry Andric   if (config->checkSections)
6340b57cec5SDimitry Andric     checkSections();
6350b57cec5SDimitry Andric 
6360b57cec5SDimitry Andric   // It does not make sense try to open the file if we have error already.
6370b57cec5SDimitry Andric   if (errorCount())
6380b57cec5SDimitry Andric     return;
639e8d8bef9SDimitry Andric 
640e8d8bef9SDimitry Andric   {
641e8d8bef9SDimitry Andric     llvm::TimeTraceScope timeScope("Write output file");
6420b57cec5SDimitry Andric     // Write the result down to a file.
6430b57cec5SDimitry Andric     openFile();
6440b57cec5SDimitry Andric     if (errorCount())
6450b57cec5SDimitry Andric       return;
6460b57cec5SDimitry Andric 
6470b57cec5SDimitry Andric     if (!config->oFormatBinary) {
64885868e8aSDimitry Andric       if (config->zSeparate != SeparateSegmentKind::None)
6490b57cec5SDimitry Andric         writeTrapInstr();
6500b57cec5SDimitry Andric       writeHeader();
6510b57cec5SDimitry Andric       writeSections();
6520b57cec5SDimitry Andric     } else {
6530b57cec5SDimitry Andric       writeSectionsBinary();
6540b57cec5SDimitry Andric     }
6550b57cec5SDimitry Andric 
6560b57cec5SDimitry Andric     // Backfill .note.gnu.build-id section content. This is done at last
6570b57cec5SDimitry Andric     // because the content is usually a hash value of the entire output file.
6580b57cec5SDimitry Andric     writeBuildId();
6590b57cec5SDimitry Andric     if (errorCount())
6600b57cec5SDimitry Andric       return;
6610b57cec5SDimitry Andric 
6620b57cec5SDimitry Andric     if (auto e = buffer->commit())
6630b57cec5SDimitry Andric       error("failed to write to the output file: " + toString(std::move(e)));
6640b57cec5SDimitry Andric   }
665e8d8bef9SDimitry Andric }
6660b57cec5SDimitry Andric 
6675ffd83dbSDimitry Andric template <class ELFT, class RelTy>
6685ffd83dbSDimitry Andric static void markUsedLocalSymbolsImpl(ObjFile<ELFT> *file,
6695ffd83dbSDimitry Andric                                      llvm::ArrayRef<RelTy> rels) {
6705ffd83dbSDimitry Andric   for (const RelTy &rel : rels) {
6715ffd83dbSDimitry Andric     Symbol &sym = file->getRelocTargetSym(rel);
6725ffd83dbSDimitry Andric     if (sym.isLocal())
6735ffd83dbSDimitry Andric       sym.used = true;
6745ffd83dbSDimitry Andric   }
6755ffd83dbSDimitry Andric }
6765ffd83dbSDimitry Andric 
6775ffd83dbSDimitry Andric // The function ensures that the "used" field of local symbols reflects the fact
6785ffd83dbSDimitry Andric // that the symbol is used in a relocation from a live section.
6795ffd83dbSDimitry Andric template <class ELFT> static void markUsedLocalSymbols() {
6805ffd83dbSDimitry Andric   // With --gc-sections, the field is already filled.
6815ffd83dbSDimitry Andric   // See MarkLive<ELFT>::resolveReloc().
6825ffd83dbSDimitry Andric   if (config->gcSections)
6835ffd83dbSDimitry Andric     return;
6845ffd83dbSDimitry Andric   // Without --gc-sections, the field is initialized with "true".
6855ffd83dbSDimitry Andric   // Drop the flag first and then rise for symbols referenced in relocations.
6865ffd83dbSDimitry Andric   for (InputFile *file : objectFiles) {
6875ffd83dbSDimitry Andric     ObjFile<ELFT> *f = cast<ObjFile<ELFT>>(file);
6885ffd83dbSDimitry Andric     for (Symbol *b : f->getLocalSymbols())
6895ffd83dbSDimitry Andric       b->used = false;
6905ffd83dbSDimitry Andric     for (InputSectionBase *s : f->getSections()) {
6915ffd83dbSDimitry Andric       InputSection *isec = dyn_cast_or_null<InputSection>(s);
6925ffd83dbSDimitry Andric       if (!isec)
6935ffd83dbSDimitry Andric         continue;
6945ffd83dbSDimitry Andric       if (isec->type == SHT_REL)
6955ffd83dbSDimitry Andric         markUsedLocalSymbolsImpl(f, isec->getDataAs<typename ELFT::Rel>());
6965ffd83dbSDimitry Andric       else if (isec->type == SHT_RELA)
6975ffd83dbSDimitry Andric         markUsedLocalSymbolsImpl(f, isec->getDataAs<typename ELFT::Rela>());
6985ffd83dbSDimitry Andric     }
6995ffd83dbSDimitry Andric   }
7005ffd83dbSDimitry Andric }
7015ffd83dbSDimitry Andric 
7020b57cec5SDimitry Andric static bool shouldKeepInSymtab(const Defined &sym) {
7030b57cec5SDimitry Andric   if (sym.isSection())
7040b57cec5SDimitry Andric     return false;
7050b57cec5SDimitry Andric 
7065ffd83dbSDimitry Andric   // If --emit-reloc or -r is given, preserve symbols referenced by relocations
7075ffd83dbSDimitry Andric   // from live sections.
7085ffd83dbSDimitry Andric   if (config->copyRelocs && sym.used)
7090b57cec5SDimitry Andric     return true;
7100b57cec5SDimitry Andric 
7115ffd83dbSDimitry Andric   // Exclude local symbols pointing to .ARM.exidx sections.
7125ffd83dbSDimitry Andric   // They are probably mapping symbols "$d", which are optional for these
7135ffd83dbSDimitry Andric   // sections. After merging the .ARM.exidx sections, some of these symbols
7145ffd83dbSDimitry Andric   // may become dangling. The easiest way to avoid the issue is not to add
7155ffd83dbSDimitry Andric   // them to the symbol table from the beginning.
7165ffd83dbSDimitry Andric   if (config->emachine == EM_ARM && sym.section &&
7175ffd83dbSDimitry Andric       sym.section->type == SHT_ARM_EXIDX)
7185ffd83dbSDimitry Andric     return false;
7195ffd83dbSDimitry Andric 
7205ffd83dbSDimitry Andric   if (config->discard == DiscardPolicy::None)
7210b57cec5SDimitry Andric     return true;
7225ffd83dbSDimitry Andric   if (config->discard == DiscardPolicy::All)
7235ffd83dbSDimitry Andric     return false;
7240b57cec5SDimitry Andric 
7250b57cec5SDimitry Andric   // In ELF assembly .L symbols are normally discarded by the assembler.
7260b57cec5SDimitry Andric   // If the assembler fails to do so, the linker discards them if
7270b57cec5SDimitry Andric   // * --discard-locals is used.
7280b57cec5SDimitry Andric   // * The symbol is in a SHF_MERGE section, which is normally the reason for
7290b57cec5SDimitry Andric   //   the assembler keeping the .L symbol.
7300b57cec5SDimitry Andric   StringRef name = sym.getName();
7310b57cec5SDimitry Andric   bool isLocal = name.startswith(".L") || name.empty();
7320b57cec5SDimitry Andric   if (!isLocal)
7330b57cec5SDimitry Andric     return true;
7340b57cec5SDimitry Andric 
7350b57cec5SDimitry Andric   if (config->discard == DiscardPolicy::Locals)
7360b57cec5SDimitry Andric     return false;
7370b57cec5SDimitry Andric 
7380b57cec5SDimitry Andric   SectionBase *sec = sym.section;
7390b57cec5SDimitry Andric   return !sec || !(sec->flags & SHF_MERGE);
7400b57cec5SDimitry Andric }
7410b57cec5SDimitry Andric 
7420b57cec5SDimitry Andric static bool includeInSymtab(const Symbol &b) {
7430b57cec5SDimitry Andric   if (!b.isLocal() && !b.isUsedInRegularObj)
7440b57cec5SDimitry Andric     return false;
7450b57cec5SDimitry Andric 
7460b57cec5SDimitry Andric   if (auto *d = dyn_cast<Defined>(&b)) {
7470b57cec5SDimitry Andric     // Always include absolute symbols.
7480b57cec5SDimitry Andric     SectionBase *sec = d->section;
7490b57cec5SDimitry Andric     if (!sec)
7500b57cec5SDimitry Andric       return true;
7510b57cec5SDimitry Andric     sec = sec->repl;
7520b57cec5SDimitry Andric 
7530b57cec5SDimitry Andric     // Exclude symbols pointing to garbage-collected sections.
7540b57cec5SDimitry Andric     if (isa<InputSectionBase>(sec) && !sec->isLive())
7550b57cec5SDimitry Andric       return false;
7560b57cec5SDimitry Andric 
7570b57cec5SDimitry Andric     if (auto *s = dyn_cast<MergeInputSection>(sec))
7580b57cec5SDimitry Andric       if (!s->getSectionPiece(d->value)->live)
7590b57cec5SDimitry Andric         return false;
7600b57cec5SDimitry Andric     return true;
7610b57cec5SDimitry Andric   }
7620b57cec5SDimitry Andric   return b.used;
7630b57cec5SDimitry Andric }
7640b57cec5SDimitry Andric 
7650b57cec5SDimitry Andric // Local symbols are not in the linker's symbol table. This function scans
7660b57cec5SDimitry Andric // each object file's symbol table to copy local symbols to the output.
7670b57cec5SDimitry Andric template <class ELFT> void Writer<ELFT>::copyLocalSymbols() {
7680b57cec5SDimitry Andric   if (!in.symTab)
7690b57cec5SDimitry Andric     return;
770e8d8bef9SDimitry Andric   llvm::TimeTraceScope timeScope("Add local symbols");
7715ffd83dbSDimitry Andric   if (config->copyRelocs && config->discard != DiscardPolicy::None)
7725ffd83dbSDimitry Andric     markUsedLocalSymbols<ELFT>();
7730b57cec5SDimitry Andric   for (InputFile *file : objectFiles) {
7740b57cec5SDimitry Andric     ObjFile<ELFT> *f = cast<ObjFile<ELFT>>(file);
7750b57cec5SDimitry Andric     for (Symbol *b : f->getLocalSymbols()) {
7765ffd83dbSDimitry Andric       assert(b->isLocal() && "should have been caught in initializeSymbols()");
7770b57cec5SDimitry Andric       auto *dr = dyn_cast<Defined>(b);
7780b57cec5SDimitry Andric 
7790b57cec5SDimitry Andric       // No reason to keep local undefined symbol in symtab.
7800b57cec5SDimitry Andric       if (!dr)
7810b57cec5SDimitry Andric         continue;
7820b57cec5SDimitry Andric       if (!includeInSymtab(*b))
7830b57cec5SDimitry Andric         continue;
7840b57cec5SDimitry Andric       if (!shouldKeepInSymtab(*dr))
7850b57cec5SDimitry Andric         continue;
7860b57cec5SDimitry Andric       in.symTab->addSymbol(b);
7870b57cec5SDimitry Andric     }
7880b57cec5SDimitry Andric   }
7890b57cec5SDimitry Andric }
7900b57cec5SDimitry Andric 
7910b57cec5SDimitry Andric // Create a section symbol for each output section so that we can represent
7920b57cec5SDimitry Andric // relocations that point to the section. If we know that no relocation is
7930b57cec5SDimitry Andric // referring to a section (that happens if the section is a synthetic one), we
7940b57cec5SDimitry Andric // don't create a section symbol for that section.
7950b57cec5SDimitry Andric template <class ELFT> void Writer<ELFT>::addSectionSymbols() {
7960b57cec5SDimitry Andric   for (BaseCommand *base : script->sectionCommands) {
7970b57cec5SDimitry Andric     auto *sec = dyn_cast<OutputSection>(base);
7980b57cec5SDimitry Andric     if (!sec)
7990b57cec5SDimitry Andric       continue;
8000b57cec5SDimitry Andric     auto i = llvm::find_if(sec->sectionCommands, [](BaseCommand *base) {
8010b57cec5SDimitry Andric       if (auto *isd = dyn_cast<InputSectionDescription>(base))
8020b57cec5SDimitry Andric         return !isd->sections.empty();
8030b57cec5SDimitry Andric       return false;
8040b57cec5SDimitry Andric     });
8050b57cec5SDimitry Andric     if (i == sec->sectionCommands.end())
8060b57cec5SDimitry Andric       continue;
80785868e8aSDimitry Andric     InputSectionBase *isec = cast<InputSectionDescription>(*i)->sections[0];
8080b57cec5SDimitry Andric 
8090b57cec5SDimitry Andric     // Relocations are not using REL[A] section symbols.
8100b57cec5SDimitry Andric     if (isec->type == SHT_REL || isec->type == SHT_RELA)
8110b57cec5SDimitry Andric       continue;
8120b57cec5SDimitry Andric 
8130b57cec5SDimitry Andric     // Unlike other synthetic sections, mergeable output sections contain data
8140b57cec5SDimitry Andric     // copied from input sections, and there may be a relocation pointing to its
8150b57cec5SDimitry Andric     // contents if -r or -emit-reloc are given.
8160b57cec5SDimitry Andric     if (isa<SyntheticSection>(isec) && !(isec->flags & SHF_MERGE))
8170b57cec5SDimitry Andric       continue;
8180b57cec5SDimitry Andric 
819e8d8bef9SDimitry Andric     // Set the symbol to be relative to the output section so that its st_value
820e8d8bef9SDimitry Andric     // equals the output section address. Note, there may be a gap between the
821e8d8bef9SDimitry Andric     // start of the output section and isec.
8220b57cec5SDimitry Andric     auto *sym =
8230b57cec5SDimitry Andric         make<Defined>(isec->file, "", STB_LOCAL, /*stOther=*/0, STT_SECTION,
824e8d8bef9SDimitry Andric                       /*value=*/0, /*size=*/0, isec->getOutputSection());
8250b57cec5SDimitry Andric     in.symTab->addSymbol(sym);
8260b57cec5SDimitry Andric   }
8270b57cec5SDimitry Andric }
8280b57cec5SDimitry Andric 
8290b57cec5SDimitry Andric // Today's loaders have a feature to make segments read-only after
8300b57cec5SDimitry Andric // processing dynamic relocations to enhance security. PT_GNU_RELRO
8310b57cec5SDimitry Andric // is defined for that.
8320b57cec5SDimitry Andric //
8330b57cec5SDimitry Andric // This function returns true if a section needs to be put into a
8340b57cec5SDimitry Andric // PT_GNU_RELRO segment.
8350b57cec5SDimitry Andric static bool isRelroSection(const OutputSection *sec) {
8360b57cec5SDimitry Andric   if (!config->zRelro)
8370b57cec5SDimitry Andric     return false;
8380b57cec5SDimitry Andric 
8390b57cec5SDimitry Andric   uint64_t flags = sec->flags;
8400b57cec5SDimitry Andric 
8410b57cec5SDimitry Andric   // Non-allocatable or non-writable sections don't need RELRO because
8420b57cec5SDimitry Andric   // they are not writable or not even mapped to memory in the first place.
8430b57cec5SDimitry Andric   // RELRO is for sections that are essentially read-only but need to
8440b57cec5SDimitry Andric   // be writable only at process startup to allow dynamic linker to
8450b57cec5SDimitry Andric   // apply relocations.
8460b57cec5SDimitry Andric   if (!(flags & SHF_ALLOC) || !(flags & SHF_WRITE))
8470b57cec5SDimitry Andric     return false;
8480b57cec5SDimitry Andric 
8490b57cec5SDimitry Andric   // Once initialized, TLS data segments are used as data templates
8500b57cec5SDimitry Andric   // for a thread-local storage. For each new thread, runtime
8510b57cec5SDimitry Andric   // allocates memory for a TLS and copy templates there. No thread
8520b57cec5SDimitry Andric   // are supposed to use templates directly. Thus, it can be in RELRO.
8530b57cec5SDimitry Andric   if (flags & SHF_TLS)
8540b57cec5SDimitry Andric     return true;
8550b57cec5SDimitry Andric 
8560b57cec5SDimitry Andric   // .init_array, .preinit_array and .fini_array contain pointers to
8570b57cec5SDimitry Andric   // functions that are executed on process startup or exit. These
8580b57cec5SDimitry Andric   // pointers are set by the static linker, and they are not expected
8590b57cec5SDimitry Andric   // to change at runtime. But if you are an attacker, you could do
8600b57cec5SDimitry Andric   // interesting things by manipulating pointers in .fini_array, for
8610b57cec5SDimitry Andric   // example. So they are put into RELRO.
8620b57cec5SDimitry Andric   uint32_t type = sec->type;
8630b57cec5SDimitry Andric   if (type == SHT_INIT_ARRAY || type == SHT_FINI_ARRAY ||
8640b57cec5SDimitry Andric       type == SHT_PREINIT_ARRAY)
8650b57cec5SDimitry Andric     return true;
8660b57cec5SDimitry Andric 
8670b57cec5SDimitry Andric   // .got contains pointers to external symbols. They are resolved by
8680b57cec5SDimitry Andric   // the dynamic linker when a module is loaded into memory, and after
8690b57cec5SDimitry Andric   // that they are not expected to change. So, it can be in RELRO.
8700b57cec5SDimitry Andric   if (in.got && sec == in.got->getParent())
8710b57cec5SDimitry Andric     return true;
8720b57cec5SDimitry Andric 
8730b57cec5SDimitry Andric   // .toc is a GOT-ish section for PowerPC64. Their contents are accessed
8740b57cec5SDimitry Andric   // through r2 register, which is reserved for that purpose. Since r2 is used
8750b57cec5SDimitry Andric   // for accessing .got as well, .got and .toc need to be close enough in the
8760b57cec5SDimitry Andric   // virtual address space. Usually, .toc comes just after .got. Since we place
8770b57cec5SDimitry Andric   // .got into RELRO, .toc needs to be placed into RELRO too.
8780b57cec5SDimitry Andric   if (sec->name.equals(".toc"))
8790b57cec5SDimitry Andric     return true;
8800b57cec5SDimitry Andric 
8810b57cec5SDimitry Andric   // .got.plt contains pointers to external function symbols. They are
8820b57cec5SDimitry Andric   // by default resolved lazily, so we usually cannot put it into RELRO.
8830b57cec5SDimitry Andric   // However, if "-z now" is given, the lazy symbol resolution is
8840b57cec5SDimitry Andric   // disabled, which enables us to put it into RELRO.
8850b57cec5SDimitry Andric   if (sec == in.gotPlt->getParent())
8860b57cec5SDimitry Andric     return config->zNow;
8870b57cec5SDimitry Andric 
8880b57cec5SDimitry Andric   // .dynamic section contains data for the dynamic linker, and
8890b57cec5SDimitry Andric   // there's no need to write to it at runtime, so it's better to put
8900b57cec5SDimitry Andric   // it into RELRO.
8910b57cec5SDimitry Andric   if (sec->name == ".dynamic")
8920b57cec5SDimitry Andric     return true;
8930b57cec5SDimitry Andric 
8940b57cec5SDimitry Andric   // Sections with some special names are put into RELRO. This is a
8950b57cec5SDimitry Andric   // bit unfortunate because section names shouldn't be significant in
8960b57cec5SDimitry Andric   // ELF in spirit. But in reality many linker features depend on
8970b57cec5SDimitry Andric   // magic section names.
8980b57cec5SDimitry Andric   StringRef s = sec->name;
8990b57cec5SDimitry Andric   return s == ".data.rel.ro" || s == ".bss.rel.ro" || s == ".ctors" ||
9000b57cec5SDimitry Andric          s == ".dtors" || s == ".jcr" || s == ".eh_frame" ||
9015ffd83dbSDimitry Andric          s == ".fini_array" || s == ".init_array" ||
9025ffd83dbSDimitry Andric          s == ".openbsd.randomdata" || s == ".preinit_array";
9030b57cec5SDimitry Andric }
9040b57cec5SDimitry Andric 
9050b57cec5SDimitry Andric // We compute a rank for each section. The rank indicates where the
9060b57cec5SDimitry Andric // section should be placed in the file.  Instead of using simple
9070b57cec5SDimitry Andric // numbers (0,1,2...), we use a series of flags. One for each decision
9080b57cec5SDimitry Andric // point when placing the section.
9090b57cec5SDimitry Andric // Using flags has two key properties:
9100b57cec5SDimitry Andric // * It is easy to check if a give branch was taken.
9110b57cec5SDimitry Andric // * It is easy two see how similar two ranks are (see getRankProximity).
9120b57cec5SDimitry Andric enum RankFlags {
9130b57cec5SDimitry Andric   RF_NOT_ADDR_SET = 1 << 27,
9140b57cec5SDimitry Andric   RF_NOT_ALLOC = 1 << 26,
9150b57cec5SDimitry Andric   RF_PARTITION = 1 << 18, // Partition number (8 bits)
9160b57cec5SDimitry Andric   RF_NOT_PART_EHDR = 1 << 17,
9170b57cec5SDimitry Andric   RF_NOT_PART_PHDR = 1 << 16,
9180b57cec5SDimitry Andric   RF_NOT_INTERP = 1 << 15,
9190b57cec5SDimitry Andric   RF_NOT_NOTE = 1 << 14,
9200b57cec5SDimitry Andric   RF_WRITE = 1 << 13,
9210b57cec5SDimitry Andric   RF_EXEC_WRITE = 1 << 12,
9220b57cec5SDimitry Andric   RF_EXEC = 1 << 11,
9230b57cec5SDimitry Andric   RF_RODATA = 1 << 10,
9240b57cec5SDimitry Andric   RF_NOT_RELRO = 1 << 9,
9250b57cec5SDimitry Andric   RF_NOT_TLS = 1 << 8,
9260b57cec5SDimitry Andric   RF_BSS = 1 << 7,
9270b57cec5SDimitry Andric   RF_PPC_NOT_TOCBSS = 1 << 6,
9280b57cec5SDimitry Andric   RF_PPC_TOCL = 1 << 5,
9290b57cec5SDimitry Andric   RF_PPC_TOC = 1 << 4,
9300b57cec5SDimitry Andric   RF_PPC_GOT = 1 << 3,
9310b57cec5SDimitry Andric   RF_PPC_BRANCH_LT = 1 << 2,
9320b57cec5SDimitry Andric   RF_MIPS_GPREL = 1 << 1,
9330b57cec5SDimitry Andric   RF_MIPS_NOT_GOT = 1 << 0
9340b57cec5SDimitry Andric };
9350b57cec5SDimitry Andric 
9360b57cec5SDimitry Andric static unsigned getSectionRank(const OutputSection *sec) {
9370b57cec5SDimitry Andric   unsigned rank = sec->partition * RF_PARTITION;
9380b57cec5SDimitry Andric 
9390b57cec5SDimitry Andric   // We want to put section specified by -T option first, so we
9400b57cec5SDimitry Andric   // can start assigning VA starting from them later.
9410b57cec5SDimitry Andric   if (config->sectionStartMap.count(sec->name))
9420b57cec5SDimitry Andric     return rank;
9430b57cec5SDimitry Andric   rank |= RF_NOT_ADDR_SET;
9440b57cec5SDimitry Andric 
9450b57cec5SDimitry Andric   // Allocatable sections go first to reduce the total PT_LOAD size and
9460b57cec5SDimitry Andric   // so debug info doesn't change addresses in actual code.
9470b57cec5SDimitry Andric   if (!(sec->flags & SHF_ALLOC))
9480b57cec5SDimitry Andric     return rank | RF_NOT_ALLOC;
9490b57cec5SDimitry Andric 
9500b57cec5SDimitry Andric   if (sec->type == SHT_LLVM_PART_EHDR)
9510b57cec5SDimitry Andric     return rank;
9520b57cec5SDimitry Andric   rank |= RF_NOT_PART_EHDR;
9530b57cec5SDimitry Andric 
9540b57cec5SDimitry Andric   if (sec->type == SHT_LLVM_PART_PHDR)
9550b57cec5SDimitry Andric     return rank;
9560b57cec5SDimitry Andric   rank |= RF_NOT_PART_PHDR;
9570b57cec5SDimitry Andric 
9580b57cec5SDimitry Andric   // Put .interp first because some loaders want to see that section
9590b57cec5SDimitry Andric   // on the first page of the executable file when loaded into memory.
9600b57cec5SDimitry Andric   if (sec->name == ".interp")
9610b57cec5SDimitry Andric     return rank;
9620b57cec5SDimitry Andric   rank |= RF_NOT_INTERP;
9630b57cec5SDimitry Andric 
9640b57cec5SDimitry Andric   // Put .note sections (which make up one PT_NOTE) at the beginning so that
9650b57cec5SDimitry Andric   // they are likely to be included in a core file even if core file size is
9660b57cec5SDimitry Andric   // limited. In particular, we want a .note.gnu.build-id and a .note.tag to be
9670b57cec5SDimitry Andric   // included in a core to match core files with executables.
9680b57cec5SDimitry Andric   if (sec->type == SHT_NOTE)
9690b57cec5SDimitry Andric     return rank;
9700b57cec5SDimitry Andric   rank |= RF_NOT_NOTE;
9710b57cec5SDimitry Andric 
9720b57cec5SDimitry Andric   // Sort sections based on their access permission in the following
9730b57cec5SDimitry Andric   // order: R, RX, RWX, RW.  This order is based on the following
9740b57cec5SDimitry Andric   // considerations:
9750b57cec5SDimitry Andric   // * Read-only sections come first such that they go in the
9760b57cec5SDimitry Andric   //   PT_LOAD covering the program headers at the start of the file.
9770b57cec5SDimitry Andric   // * Read-only, executable sections come next.
9780b57cec5SDimitry Andric   // * Writable, executable sections follow such that .plt on
9790b57cec5SDimitry Andric   //   architectures where it needs to be writable will be placed
9800b57cec5SDimitry Andric   //   between .text and .data.
9810b57cec5SDimitry Andric   // * Writable sections come last, such that .bss lands at the very
9820b57cec5SDimitry Andric   //   end of the last PT_LOAD.
9830b57cec5SDimitry Andric   bool isExec = sec->flags & SHF_EXECINSTR;
9840b57cec5SDimitry Andric   bool isWrite = sec->flags & SHF_WRITE;
9850b57cec5SDimitry Andric 
9860b57cec5SDimitry Andric   if (isExec) {
9870b57cec5SDimitry Andric     if (isWrite)
9880b57cec5SDimitry Andric       rank |= RF_EXEC_WRITE;
9890b57cec5SDimitry Andric     else
9900b57cec5SDimitry Andric       rank |= RF_EXEC;
9910b57cec5SDimitry Andric   } else if (isWrite) {
9920b57cec5SDimitry Andric     rank |= RF_WRITE;
9930b57cec5SDimitry Andric   } else if (sec->type == SHT_PROGBITS) {
9940b57cec5SDimitry Andric     // Make non-executable and non-writable PROGBITS sections (e.g .rodata
9950b57cec5SDimitry Andric     // .eh_frame) closer to .text. They likely contain PC or GOT relative
9960b57cec5SDimitry Andric     // relocations and there could be relocation overflow if other huge sections
9970b57cec5SDimitry Andric     // (.dynstr .dynsym) were placed in between.
9980b57cec5SDimitry Andric     rank |= RF_RODATA;
9990b57cec5SDimitry Andric   }
10000b57cec5SDimitry Andric 
10010b57cec5SDimitry Andric   // Place RelRo sections first. After considering SHT_NOBITS below, the
10020b57cec5SDimitry Andric   // ordering is PT_LOAD(PT_GNU_RELRO(.data.rel.ro .bss.rel.ro) | .data .bss),
10030b57cec5SDimitry Andric   // where | marks where page alignment happens. An alternative ordering is
10040b57cec5SDimitry Andric   // PT_LOAD(.data | PT_GNU_RELRO( .data.rel.ro .bss.rel.ro) | .bss), but it may
10050b57cec5SDimitry Andric   // waste more bytes due to 2 alignment places.
10060b57cec5SDimitry Andric   if (!isRelroSection(sec))
10070b57cec5SDimitry Andric     rank |= RF_NOT_RELRO;
10080b57cec5SDimitry Andric 
10090b57cec5SDimitry Andric   // If we got here we know that both A and B are in the same PT_LOAD.
10100b57cec5SDimitry Andric 
10110b57cec5SDimitry Andric   // The TLS initialization block needs to be a single contiguous block in a R/W
10120b57cec5SDimitry Andric   // PT_LOAD, so stick TLS sections directly before the other RelRo R/W
10130b57cec5SDimitry Andric   // sections. Since p_filesz can be less than p_memsz, place NOBITS sections
10140b57cec5SDimitry Andric   // after PROGBITS.
10150b57cec5SDimitry Andric   if (!(sec->flags & SHF_TLS))
10160b57cec5SDimitry Andric     rank |= RF_NOT_TLS;
10170b57cec5SDimitry Andric 
10180b57cec5SDimitry Andric   // Within TLS sections, or within other RelRo sections, or within non-RelRo
10190b57cec5SDimitry Andric   // sections, place non-NOBITS sections first.
10200b57cec5SDimitry Andric   if (sec->type == SHT_NOBITS)
10210b57cec5SDimitry Andric     rank |= RF_BSS;
10220b57cec5SDimitry Andric 
10230b57cec5SDimitry Andric   // Some architectures have additional ordering restrictions for sections
10240b57cec5SDimitry Andric   // within the same PT_LOAD.
10250b57cec5SDimitry Andric   if (config->emachine == EM_PPC64) {
10260b57cec5SDimitry Andric     // PPC64 has a number of special SHT_PROGBITS+SHF_ALLOC+SHF_WRITE sections
10270b57cec5SDimitry Andric     // that we would like to make sure appear is a specific order to maximize
10280b57cec5SDimitry Andric     // their coverage by a single signed 16-bit offset from the TOC base
10290b57cec5SDimitry Andric     // pointer. Conversely, the special .tocbss section should be first among
10300b57cec5SDimitry Andric     // all SHT_NOBITS sections. This will put it next to the loaded special
10310b57cec5SDimitry Andric     // PPC64 sections (and, thus, within reach of the TOC base pointer).
10320b57cec5SDimitry Andric     StringRef name = sec->name;
10330b57cec5SDimitry Andric     if (name != ".tocbss")
10340b57cec5SDimitry Andric       rank |= RF_PPC_NOT_TOCBSS;
10350b57cec5SDimitry Andric 
10360b57cec5SDimitry Andric     if (name == ".toc1")
10370b57cec5SDimitry Andric       rank |= RF_PPC_TOCL;
10380b57cec5SDimitry Andric 
10390b57cec5SDimitry Andric     if (name == ".toc")
10400b57cec5SDimitry Andric       rank |= RF_PPC_TOC;
10410b57cec5SDimitry Andric 
10420b57cec5SDimitry Andric     if (name == ".got")
10430b57cec5SDimitry Andric       rank |= RF_PPC_GOT;
10440b57cec5SDimitry Andric 
10450b57cec5SDimitry Andric     if (name == ".branch_lt")
10460b57cec5SDimitry Andric       rank |= RF_PPC_BRANCH_LT;
10470b57cec5SDimitry Andric   }
10480b57cec5SDimitry Andric 
10490b57cec5SDimitry Andric   if (config->emachine == EM_MIPS) {
10500b57cec5SDimitry Andric     // All sections with SHF_MIPS_GPREL flag should be grouped together
10510b57cec5SDimitry Andric     // because data in these sections is addressable with a gp relative address.
10520b57cec5SDimitry Andric     if (sec->flags & SHF_MIPS_GPREL)
10530b57cec5SDimitry Andric       rank |= RF_MIPS_GPREL;
10540b57cec5SDimitry Andric 
10550b57cec5SDimitry Andric     if (sec->name != ".got")
10560b57cec5SDimitry Andric       rank |= RF_MIPS_NOT_GOT;
10570b57cec5SDimitry Andric   }
10580b57cec5SDimitry Andric 
10590b57cec5SDimitry Andric   return rank;
10600b57cec5SDimitry Andric }
10610b57cec5SDimitry Andric 
10620b57cec5SDimitry Andric static bool compareSections(const BaseCommand *aCmd, const BaseCommand *bCmd) {
10630b57cec5SDimitry Andric   const OutputSection *a = cast<OutputSection>(aCmd);
10640b57cec5SDimitry Andric   const OutputSection *b = cast<OutputSection>(bCmd);
10650b57cec5SDimitry Andric 
10660b57cec5SDimitry Andric   if (a->sortRank != b->sortRank)
10670b57cec5SDimitry Andric     return a->sortRank < b->sortRank;
10680b57cec5SDimitry Andric 
10690b57cec5SDimitry Andric   if (!(a->sortRank & RF_NOT_ADDR_SET))
10700b57cec5SDimitry Andric     return config->sectionStartMap.lookup(a->name) <
10710b57cec5SDimitry Andric            config->sectionStartMap.lookup(b->name);
10720b57cec5SDimitry Andric   return false;
10730b57cec5SDimitry Andric }
10740b57cec5SDimitry Andric 
10750b57cec5SDimitry Andric void PhdrEntry::add(OutputSection *sec) {
10760b57cec5SDimitry Andric   lastSec = sec;
10770b57cec5SDimitry Andric   if (!firstSec)
10780b57cec5SDimitry Andric     firstSec = sec;
10790b57cec5SDimitry Andric   p_align = std::max(p_align, sec->alignment);
10800b57cec5SDimitry Andric   if (p_type == PT_LOAD)
10810b57cec5SDimitry Andric     sec->ptLoad = this;
10820b57cec5SDimitry Andric }
10830b57cec5SDimitry Andric 
10840b57cec5SDimitry Andric // The beginning and the ending of .rel[a].plt section are marked
10850b57cec5SDimitry Andric // with __rel[a]_iplt_{start,end} symbols if it is a statically linked
10860b57cec5SDimitry Andric // executable. The runtime needs these symbols in order to resolve
10870b57cec5SDimitry Andric // all IRELATIVE relocs on startup. For dynamic executables, we don't
10880b57cec5SDimitry Andric // need these symbols, since IRELATIVE relocs are resolved through GOT
10890b57cec5SDimitry Andric // and PLT. For details, see http://www.airs.com/blog/archives/403.
10900b57cec5SDimitry Andric template <class ELFT> void Writer<ELFT>::addRelIpltSymbols() {
1091*fe6060f1SDimitry Andric   if (config->relocatable || config->isPic)
10920b57cec5SDimitry Andric     return;
10930b57cec5SDimitry Andric 
10940b57cec5SDimitry Andric   // By default, __rela_iplt_{start,end} belong to a dummy section 0
10950b57cec5SDimitry Andric   // because .rela.plt might be empty and thus removed from output.
10960b57cec5SDimitry Andric   // We'll override Out::elfHeader with In.relaIplt later when we are
10970b57cec5SDimitry Andric   // sure that .rela.plt exists in output.
10980b57cec5SDimitry Andric   ElfSym::relaIpltStart = addOptionalRegular(
10990b57cec5SDimitry Andric       config->isRela ? "__rela_iplt_start" : "__rel_iplt_start",
11000b57cec5SDimitry Andric       Out::elfHeader, 0, STV_HIDDEN, STB_WEAK);
11010b57cec5SDimitry Andric 
11020b57cec5SDimitry Andric   ElfSym::relaIpltEnd = addOptionalRegular(
11030b57cec5SDimitry Andric       config->isRela ? "__rela_iplt_end" : "__rel_iplt_end",
11040b57cec5SDimitry Andric       Out::elfHeader, 0, STV_HIDDEN, STB_WEAK);
11050b57cec5SDimitry Andric }
11060b57cec5SDimitry Andric 
11070b57cec5SDimitry Andric template <class ELFT>
11080b57cec5SDimitry Andric void Writer<ELFT>::forEachRelSec(
11090b57cec5SDimitry Andric     llvm::function_ref<void(InputSectionBase &)> fn) {
11100b57cec5SDimitry Andric   // Scan all relocations. Each relocation goes through a series
11110b57cec5SDimitry Andric   // of tests to determine if it needs special treatment, such as
11120b57cec5SDimitry Andric   // creating GOT, PLT, copy relocations, etc.
11130b57cec5SDimitry Andric   // Note that relocations for non-alloc sections are directly
11140b57cec5SDimitry Andric   // processed by InputSection::relocateNonAlloc.
11150b57cec5SDimitry Andric   for (InputSectionBase *isec : inputSections)
11160b57cec5SDimitry Andric     if (isec->isLive() && isa<InputSection>(isec) && (isec->flags & SHF_ALLOC))
11170b57cec5SDimitry Andric       fn(*isec);
11180b57cec5SDimitry Andric   for (Partition &part : partitions) {
11190b57cec5SDimitry Andric     for (EhInputSection *es : part.ehFrame->sections)
11200b57cec5SDimitry Andric       fn(*es);
11210b57cec5SDimitry Andric     if (part.armExidx && part.armExidx->isLive())
11220b57cec5SDimitry Andric       for (InputSection *ex : part.armExidx->exidxSections)
11230b57cec5SDimitry Andric         fn(*ex);
11240b57cec5SDimitry Andric   }
11250b57cec5SDimitry Andric }
11260b57cec5SDimitry Andric 
11270b57cec5SDimitry Andric // This function generates assignments for predefined symbols (e.g. _end or
11280b57cec5SDimitry Andric // _etext) and inserts them into the commands sequence to be processed at the
11290b57cec5SDimitry Andric // appropriate time. This ensures that the value is going to be correct by the
11300b57cec5SDimitry Andric // time any references to these symbols are processed and is equivalent to
11310b57cec5SDimitry Andric // defining these symbols explicitly in the linker script.
11320b57cec5SDimitry Andric template <class ELFT> void Writer<ELFT>::setReservedSymbolSections() {
11330b57cec5SDimitry Andric   if (ElfSym::globalOffsetTable) {
11340b57cec5SDimitry Andric     // The _GLOBAL_OFFSET_TABLE_ symbol is defined by target convention usually
11350b57cec5SDimitry Andric     // to the start of the .got or .got.plt section.
11360b57cec5SDimitry Andric     InputSection *gotSection = in.gotPlt;
11370b57cec5SDimitry Andric     if (!target->gotBaseSymInGotPlt)
11380b57cec5SDimitry Andric       gotSection = in.mipsGot ? cast<InputSection>(in.mipsGot)
11390b57cec5SDimitry Andric                               : cast<InputSection>(in.got);
11400b57cec5SDimitry Andric     ElfSym::globalOffsetTable->section = gotSection;
11410b57cec5SDimitry Andric   }
11420b57cec5SDimitry Andric 
114385868e8aSDimitry Andric   // .rela_iplt_{start,end} mark the start and the end of in.relaIplt.
11440b57cec5SDimitry Andric   if (ElfSym::relaIpltStart && in.relaIplt->isNeeded()) {
11450b57cec5SDimitry Andric     ElfSym::relaIpltStart->section = in.relaIplt;
11460b57cec5SDimitry Andric     ElfSym::relaIpltEnd->section = in.relaIplt;
11470b57cec5SDimitry Andric     ElfSym::relaIpltEnd->value = in.relaIplt->getSize();
11480b57cec5SDimitry Andric   }
11490b57cec5SDimitry Andric 
11500b57cec5SDimitry Andric   PhdrEntry *last = nullptr;
11510b57cec5SDimitry Andric   PhdrEntry *lastRO = nullptr;
11520b57cec5SDimitry Andric 
11530b57cec5SDimitry Andric   for (Partition &part : partitions) {
11540b57cec5SDimitry Andric     for (PhdrEntry *p : part.phdrs) {
11550b57cec5SDimitry Andric       if (p->p_type != PT_LOAD)
11560b57cec5SDimitry Andric         continue;
11570b57cec5SDimitry Andric       last = p;
11580b57cec5SDimitry Andric       if (!(p->p_flags & PF_W))
11590b57cec5SDimitry Andric         lastRO = p;
11600b57cec5SDimitry Andric     }
11610b57cec5SDimitry Andric   }
11620b57cec5SDimitry Andric 
11630b57cec5SDimitry Andric   if (lastRO) {
11640b57cec5SDimitry Andric     // _etext is the first location after the last read-only loadable segment.
11650b57cec5SDimitry Andric     if (ElfSym::etext1)
11660b57cec5SDimitry Andric       ElfSym::etext1->section = lastRO->lastSec;
11670b57cec5SDimitry Andric     if (ElfSym::etext2)
11680b57cec5SDimitry Andric       ElfSym::etext2->section = lastRO->lastSec;
11690b57cec5SDimitry Andric   }
11700b57cec5SDimitry Andric 
11710b57cec5SDimitry Andric   if (last) {
11720b57cec5SDimitry Andric     // _edata points to the end of the last mapped initialized section.
11730b57cec5SDimitry Andric     OutputSection *edata = nullptr;
11740b57cec5SDimitry Andric     for (OutputSection *os : outputSections) {
11750b57cec5SDimitry Andric       if (os->type != SHT_NOBITS)
11760b57cec5SDimitry Andric         edata = os;
11770b57cec5SDimitry Andric       if (os == last->lastSec)
11780b57cec5SDimitry Andric         break;
11790b57cec5SDimitry Andric     }
11800b57cec5SDimitry Andric 
11810b57cec5SDimitry Andric     if (ElfSym::edata1)
11820b57cec5SDimitry Andric       ElfSym::edata1->section = edata;
11830b57cec5SDimitry Andric     if (ElfSym::edata2)
11840b57cec5SDimitry Andric       ElfSym::edata2->section = edata;
11850b57cec5SDimitry Andric 
11860b57cec5SDimitry Andric     // _end is the first location after the uninitialized data region.
11870b57cec5SDimitry Andric     if (ElfSym::end1)
11880b57cec5SDimitry Andric       ElfSym::end1->section = last->lastSec;
11890b57cec5SDimitry Andric     if (ElfSym::end2)
11900b57cec5SDimitry Andric       ElfSym::end2->section = last->lastSec;
11910b57cec5SDimitry Andric   }
11920b57cec5SDimitry Andric 
11930b57cec5SDimitry Andric   if (ElfSym::bss)
11940b57cec5SDimitry Andric     ElfSym::bss->section = findSection(".bss");
11950b57cec5SDimitry Andric 
11960b57cec5SDimitry Andric   // Setup MIPS _gp_disp/__gnu_local_gp symbols which should
11970b57cec5SDimitry Andric   // be equal to the _gp symbol's value.
11980b57cec5SDimitry Andric   if (ElfSym::mipsGp) {
11990b57cec5SDimitry Andric     // Find GP-relative section with the lowest address
12000b57cec5SDimitry Andric     // and use this address to calculate default _gp value.
12010b57cec5SDimitry Andric     for (OutputSection *os : outputSections) {
12020b57cec5SDimitry Andric       if (os->flags & SHF_MIPS_GPREL) {
12030b57cec5SDimitry Andric         ElfSym::mipsGp->section = os;
12040b57cec5SDimitry Andric         ElfSym::mipsGp->value = 0x7ff0;
12050b57cec5SDimitry Andric         break;
12060b57cec5SDimitry Andric       }
12070b57cec5SDimitry Andric     }
12080b57cec5SDimitry Andric   }
12090b57cec5SDimitry Andric }
12100b57cec5SDimitry Andric 
12110b57cec5SDimitry Andric // We want to find how similar two ranks are.
12120b57cec5SDimitry Andric // The more branches in getSectionRank that match, the more similar they are.
12130b57cec5SDimitry Andric // Since each branch corresponds to a bit flag, we can just use
12140b57cec5SDimitry Andric // countLeadingZeros.
12150b57cec5SDimitry Andric static int getRankProximityAux(OutputSection *a, OutputSection *b) {
12160b57cec5SDimitry Andric   return countLeadingZeros(a->sortRank ^ b->sortRank);
12170b57cec5SDimitry Andric }
12180b57cec5SDimitry Andric 
12190b57cec5SDimitry Andric static int getRankProximity(OutputSection *a, BaseCommand *b) {
12200b57cec5SDimitry Andric   auto *sec = dyn_cast<OutputSection>(b);
12210b57cec5SDimitry Andric   return (sec && sec->hasInputSections) ? getRankProximityAux(a, sec) : -1;
12220b57cec5SDimitry Andric }
12230b57cec5SDimitry Andric 
12240b57cec5SDimitry Andric // When placing orphan sections, we want to place them after symbol assignments
12250b57cec5SDimitry Andric // so that an orphan after
12260b57cec5SDimitry Andric //   begin_foo = .;
12270b57cec5SDimitry Andric //   foo : { *(foo) }
12280b57cec5SDimitry Andric //   end_foo = .;
12290b57cec5SDimitry Andric // doesn't break the intended meaning of the begin/end symbols.
12300b57cec5SDimitry Andric // We don't want to go over sections since findOrphanPos is the
12310b57cec5SDimitry Andric // one in charge of deciding the order of the sections.
12320b57cec5SDimitry Andric // We don't want to go over changes to '.', since doing so in
12330b57cec5SDimitry Andric //  rx_sec : { *(rx_sec) }
12340b57cec5SDimitry Andric //  . = ALIGN(0x1000);
12350b57cec5SDimitry Andric //  /* The RW PT_LOAD starts here*/
12360b57cec5SDimitry Andric //  rw_sec : { *(rw_sec) }
12370b57cec5SDimitry Andric // would mean that the RW PT_LOAD would become unaligned.
12380b57cec5SDimitry Andric static bool shouldSkip(BaseCommand *cmd) {
12390b57cec5SDimitry Andric   if (auto *assign = dyn_cast<SymbolAssignment>(cmd))
12400b57cec5SDimitry Andric     return assign->name != ".";
12410b57cec5SDimitry Andric   return false;
12420b57cec5SDimitry Andric }
12430b57cec5SDimitry Andric 
12440b57cec5SDimitry Andric // We want to place orphan sections so that they share as much
12450b57cec5SDimitry Andric // characteristics with their neighbors as possible. For example, if
12460b57cec5SDimitry Andric // both are rw, or both are tls.
12470b57cec5SDimitry Andric static std::vector<BaseCommand *>::iterator
12480b57cec5SDimitry Andric findOrphanPos(std::vector<BaseCommand *>::iterator b,
12490b57cec5SDimitry Andric               std::vector<BaseCommand *>::iterator e) {
12500b57cec5SDimitry Andric   OutputSection *sec = cast<OutputSection>(*e);
12510b57cec5SDimitry Andric 
12520b57cec5SDimitry Andric   // Find the first element that has as close a rank as possible.
12530b57cec5SDimitry Andric   auto i = std::max_element(b, e, [=](BaseCommand *a, BaseCommand *b) {
12540b57cec5SDimitry Andric     return getRankProximity(sec, a) < getRankProximity(sec, b);
12550b57cec5SDimitry Andric   });
12560b57cec5SDimitry Andric   if (i == e)
12570b57cec5SDimitry Andric     return e;
12580b57cec5SDimitry Andric 
12590b57cec5SDimitry Andric   // Consider all existing sections with the same proximity.
12600b57cec5SDimitry Andric   int proximity = getRankProximity(sec, *i);
12610b57cec5SDimitry Andric   for (; i != e; ++i) {
12620b57cec5SDimitry Andric     auto *curSec = dyn_cast<OutputSection>(*i);
12630b57cec5SDimitry Andric     if (!curSec || !curSec->hasInputSections)
12640b57cec5SDimitry Andric       continue;
12650b57cec5SDimitry Andric     if (getRankProximity(sec, curSec) != proximity ||
12660b57cec5SDimitry Andric         sec->sortRank < curSec->sortRank)
12670b57cec5SDimitry Andric       break;
12680b57cec5SDimitry Andric   }
12690b57cec5SDimitry Andric 
12700b57cec5SDimitry Andric   auto isOutputSecWithInputSections = [](BaseCommand *cmd) {
12710b57cec5SDimitry Andric     auto *os = dyn_cast<OutputSection>(cmd);
12720b57cec5SDimitry Andric     return os && os->hasInputSections;
12730b57cec5SDimitry Andric   };
12740b57cec5SDimitry Andric   auto j = std::find_if(llvm::make_reverse_iterator(i),
12750b57cec5SDimitry Andric                         llvm::make_reverse_iterator(b),
12760b57cec5SDimitry Andric                         isOutputSecWithInputSections);
12770b57cec5SDimitry Andric   i = j.base();
12780b57cec5SDimitry Andric 
12790b57cec5SDimitry Andric   // As a special case, if the orphan section is the last section, put
12800b57cec5SDimitry Andric   // it at the very end, past any other commands.
12810b57cec5SDimitry Andric   // This matches bfd's behavior and is convenient when the linker script fully
12820b57cec5SDimitry Andric   // specifies the start of the file, but doesn't care about the end (the non
12830b57cec5SDimitry Andric   // alloc sections for example).
12840b57cec5SDimitry Andric   auto nextSec = std::find_if(i, e, isOutputSecWithInputSections);
12850b57cec5SDimitry Andric   if (nextSec == e)
12860b57cec5SDimitry Andric     return e;
12870b57cec5SDimitry Andric 
12880b57cec5SDimitry Andric   while (i != e && shouldSkip(*i))
12890b57cec5SDimitry Andric     ++i;
12900b57cec5SDimitry Andric   return i;
12910b57cec5SDimitry Andric }
12920b57cec5SDimitry Andric 
12935ffd83dbSDimitry Andric // Adds random priorities to sections not already in the map.
12945ffd83dbSDimitry Andric static void maybeShuffle(DenseMap<const InputSectionBase *, int> &order) {
1295*fe6060f1SDimitry Andric   if (config->shuffleSections.empty())
12965ffd83dbSDimitry Andric     return;
12975ffd83dbSDimitry Andric 
1298*fe6060f1SDimitry Andric   std::vector<InputSectionBase *> matched, sections = inputSections;
1299*fe6060f1SDimitry Andric   matched.reserve(sections.size());
1300*fe6060f1SDimitry Andric   for (const auto &patAndSeed : config->shuffleSections) {
1301*fe6060f1SDimitry Andric     matched.clear();
1302*fe6060f1SDimitry Andric     for (InputSectionBase *sec : sections)
1303*fe6060f1SDimitry Andric       if (patAndSeed.first.match(sec->name))
1304*fe6060f1SDimitry Andric         matched.push_back(sec);
1305*fe6060f1SDimitry Andric     const uint32_t seed = patAndSeed.second;
1306*fe6060f1SDimitry Andric     if (seed == UINT32_MAX) {
1307*fe6060f1SDimitry Andric       // If --shuffle-sections <section-glob>=-1, reverse the section order. The
1308*fe6060f1SDimitry Andric       // section order is stable even if the number of sections changes. This is
1309*fe6060f1SDimitry Andric       // useful to catch issues like static initialization order fiasco
1310*fe6060f1SDimitry Andric       // reliably.
1311*fe6060f1SDimitry Andric       std::reverse(matched.begin(), matched.end());
1312*fe6060f1SDimitry Andric     } else {
1313*fe6060f1SDimitry Andric       std::mt19937 g(seed ? seed : std::random_device()());
1314*fe6060f1SDimitry Andric       llvm::shuffle(matched.begin(), matched.end(), g);
1315*fe6060f1SDimitry Andric     }
1316*fe6060f1SDimitry Andric     size_t i = 0;
1317*fe6060f1SDimitry Andric     for (InputSectionBase *&sec : sections)
1318*fe6060f1SDimitry Andric       if (patAndSeed.first.match(sec->name))
1319*fe6060f1SDimitry Andric         sec = matched[i++];
1320*fe6060f1SDimitry Andric   }
1321*fe6060f1SDimitry Andric 
13225ffd83dbSDimitry Andric   // Existing priorities are < 0, so use priorities >= 0 for the missing
13235ffd83dbSDimitry Andric   // sections.
1324*fe6060f1SDimitry Andric   int prio = 0;
1325*fe6060f1SDimitry Andric   for (InputSectionBase *sec : sections) {
1326*fe6060f1SDimitry Andric     if (order.try_emplace(sec, prio).second)
1327*fe6060f1SDimitry Andric       ++prio;
13285ffd83dbSDimitry Andric   }
13295ffd83dbSDimitry Andric }
13305ffd83dbSDimitry Andric 
13310b57cec5SDimitry Andric // Builds section order for handling --symbol-ordering-file.
13320b57cec5SDimitry Andric static DenseMap<const InputSectionBase *, int> buildSectionOrder() {
13330b57cec5SDimitry Andric   DenseMap<const InputSectionBase *, int> sectionOrder;
13340b57cec5SDimitry Andric   // Use the rarely used option -call-graph-ordering-file to sort sections.
13350b57cec5SDimitry Andric   if (!config->callGraphProfile.empty())
13360b57cec5SDimitry Andric     return computeCallGraphProfileOrder();
13370b57cec5SDimitry Andric 
13380b57cec5SDimitry Andric   if (config->symbolOrderingFile.empty())
13390b57cec5SDimitry Andric     return sectionOrder;
13400b57cec5SDimitry Andric 
13410b57cec5SDimitry Andric   struct SymbolOrderEntry {
13420b57cec5SDimitry Andric     int priority;
13430b57cec5SDimitry Andric     bool present;
13440b57cec5SDimitry Andric   };
13450b57cec5SDimitry Andric 
13460b57cec5SDimitry Andric   // Build a map from symbols to their priorities. Symbols that didn't
13470b57cec5SDimitry Andric   // appear in the symbol ordering file have the lowest priority 0.
13480b57cec5SDimitry Andric   // All explicitly mentioned symbols have negative (higher) priorities.
13490b57cec5SDimitry Andric   DenseMap<StringRef, SymbolOrderEntry> symbolOrder;
13500b57cec5SDimitry Andric   int priority = -config->symbolOrderingFile.size();
13510b57cec5SDimitry Andric   for (StringRef s : config->symbolOrderingFile)
13520b57cec5SDimitry Andric     symbolOrder.insert({s, {priority++, false}});
13530b57cec5SDimitry Andric 
13540b57cec5SDimitry Andric   // Build a map from sections to their priorities.
13550b57cec5SDimitry Andric   auto addSym = [&](Symbol &sym) {
13560b57cec5SDimitry Andric     auto it = symbolOrder.find(sym.getName());
13570b57cec5SDimitry Andric     if (it == symbolOrder.end())
13580b57cec5SDimitry Andric       return;
13590b57cec5SDimitry Andric     SymbolOrderEntry &ent = it->second;
13600b57cec5SDimitry Andric     ent.present = true;
13610b57cec5SDimitry Andric 
13620b57cec5SDimitry Andric     maybeWarnUnorderableSymbol(&sym);
13630b57cec5SDimitry Andric 
13640b57cec5SDimitry Andric     if (auto *d = dyn_cast<Defined>(&sym)) {
13650b57cec5SDimitry Andric       if (auto *sec = dyn_cast_or_null<InputSectionBase>(d->section)) {
13660b57cec5SDimitry Andric         int &priority = sectionOrder[cast<InputSectionBase>(sec->repl)];
13670b57cec5SDimitry Andric         priority = std::min(priority, ent.priority);
13680b57cec5SDimitry Andric       }
13690b57cec5SDimitry Andric     }
13700b57cec5SDimitry Andric   };
13710b57cec5SDimitry Andric 
13720b57cec5SDimitry Andric   // We want both global and local symbols. We get the global ones from the
13730b57cec5SDimitry Andric   // symbol table and iterate the object files for the local ones.
1374480093f4SDimitry Andric   for (Symbol *sym : symtab->symbols())
13750b57cec5SDimitry Andric     if (!sym->isLazy())
13760b57cec5SDimitry Andric       addSym(*sym);
13770b57cec5SDimitry Andric 
13780b57cec5SDimitry Andric   for (InputFile *file : objectFiles)
1379e8d8bef9SDimitry Andric     for (Symbol *sym : file->getSymbols()) {
1380e8d8bef9SDimitry Andric       if (!sym->isLocal())
1381e8d8bef9SDimitry Andric         break;
13820b57cec5SDimitry Andric       addSym(*sym);
1383e8d8bef9SDimitry Andric     }
13840b57cec5SDimitry Andric 
13850b57cec5SDimitry Andric   if (config->warnSymbolOrdering)
13860b57cec5SDimitry Andric     for (auto orderEntry : symbolOrder)
13870b57cec5SDimitry Andric       if (!orderEntry.second.present)
13880b57cec5SDimitry Andric         warn("symbol ordering file: no such symbol: " + orderEntry.first);
13890b57cec5SDimitry Andric 
13900b57cec5SDimitry Andric   return sectionOrder;
13910b57cec5SDimitry Andric }
13920b57cec5SDimitry Andric 
13930b57cec5SDimitry Andric // Sorts the sections in ISD according to the provided section order.
13940b57cec5SDimitry Andric static void
13950b57cec5SDimitry Andric sortISDBySectionOrder(InputSectionDescription *isd,
13960b57cec5SDimitry Andric                       const DenseMap<const InputSectionBase *, int> &order) {
13970b57cec5SDimitry Andric   std::vector<InputSection *> unorderedSections;
13980b57cec5SDimitry Andric   std::vector<std::pair<InputSection *, int>> orderedSections;
13990b57cec5SDimitry Andric   uint64_t unorderedSize = 0;
14000b57cec5SDimitry Andric 
14010b57cec5SDimitry Andric   for (InputSection *isec : isd->sections) {
14020b57cec5SDimitry Andric     auto i = order.find(isec);
14030b57cec5SDimitry Andric     if (i == order.end()) {
14040b57cec5SDimitry Andric       unorderedSections.push_back(isec);
14050b57cec5SDimitry Andric       unorderedSize += isec->getSize();
14060b57cec5SDimitry Andric       continue;
14070b57cec5SDimitry Andric     }
14080b57cec5SDimitry Andric     orderedSections.push_back({isec, i->second});
14090b57cec5SDimitry Andric   }
141085868e8aSDimitry Andric   llvm::sort(orderedSections, llvm::less_second());
14110b57cec5SDimitry Andric 
14120b57cec5SDimitry Andric   // Find an insertion point for the ordered section list in the unordered
14130b57cec5SDimitry Andric   // section list. On targets with limited-range branches, this is the mid-point
14140b57cec5SDimitry Andric   // of the unordered section list. This decreases the likelihood that a range
14150b57cec5SDimitry Andric   // extension thunk will be needed to enter or exit the ordered region. If the
14160b57cec5SDimitry Andric   // ordered section list is a list of hot functions, we can generally expect
14170b57cec5SDimitry Andric   // the ordered functions to be called more often than the unordered functions,
14180b57cec5SDimitry Andric   // making it more likely that any particular call will be within range, and
14190b57cec5SDimitry Andric   // therefore reducing the number of thunks required.
14200b57cec5SDimitry Andric   //
14210b57cec5SDimitry Andric   // For example, imagine that you have 8MB of hot code and 32MB of cold code.
14220b57cec5SDimitry Andric   // If the layout is:
14230b57cec5SDimitry Andric   //
14240b57cec5SDimitry Andric   // 8MB hot
14250b57cec5SDimitry Andric   // 32MB cold
14260b57cec5SDimitry Andric   //
14270b57cec5SDimitry Andric   // only the first 8-16MB of the cold code (depending on which hot function it
14280b57cec5SDimitry Andric   // is actually calling) can call the hot code without a range extension thunk.
14290b57cec5SDimitry Andric   // However, if we use this layout:
14300b57cec5SDimitry Andric   //
14310b57cec5SDimitry Andric   // 16MB cold
14320b57cec5SDimitry Andric   // 8MB hot
14330b57cec5SDimitry Andric   // 16MB cold
14340b57cec5SDimitry Andric   //
14350b57cec5SDimitry Andric   // both the last 8-16MB of the first block of cold code and the first 8-16MB
14360b57cec5SDimitry Andric   // of the second block of cold code can call the hot code without a thunk. So
14370b57cec5SDimitry Andric   // we effectively double the amount of code that could potentially call into
14380b57cec5SDimitry Andric   // the hot code without a thunk.
14390b57cec5SDimitry Andric   size_t insPt = 0;
14400b57cec5SDimitry Andric   if (target->getThunkSectionSpacing() && !orderedSections.empty()) {
14410b57cec5SDimitry Andric     uint64_t unorderedPos = 0;
14420b57cec5SDimitry Andric     for (; insPt != unorderedSections.size(); ++insPt) {
14430b57cec5SDimitry Andric       unorderedPos += unorderedSections[insPt]->getSize();
14440b57cec5SDimitry Andric       if (unorderedPos > unorderedSize / 2)
14450b57cec5SDimitry Andric         break;
14460b57cec5SDimitry Andric     }
14470b57cec5SDimitry Andric   }
14480b57cec5SDimitry Andric 
14490b57cec5SDimitry Andric   isd->sections.clear();
14500b57cec5SDimitry Andric   for (InputSection *isec : makeArrayRef(unorderedSections).slice(0, insPt))
14510b57cec5SDimitry Andric     isd->sections.push_back(isec);
14520b57cec5SDimitry Andric   for (std::pair<InputSection *, int> p : orderedSections)
14530b57cec5SDimitry Andric     isd->sections.push_back(p.first);
14540b57cec5SDimitry Andric   for (InputSection *isec : makeArrayRef(unorderedSections).slice(insPt))
14550b57cec5SDimitry Andric     isd->sections.push_back(isec);
14560b57cec5SDimitry Andric }
14570b57cec5SDimitry Andric 
14580b57cec5SDimitry Andric static void sortSection(OutputSection *sec,
14590b57cec5SDimitry Andric                         const DenseMap<const InputSectionBase *, int> &order) {
14600b57cec5SDimitry Andric   StringRef name = sec->name;
14610b57cec5SDimitry Andric 
14625ffd83dbSDimitry Andric   // Never sort these.
14635ffd83dbSDimitry Andric   if (name == ".init" || name == ".fini")
14645ffd83dbSDimitry Andric     return;
14655ffd83dbSDimitry Andric 
1466e8d8bef9SDimitry Andric   // IRelative relocations that usually live in the .rel[a].dyn section should
1467*fe6060f1SDimitry Andric   // be processed last by the dynamic loader. To achieve that we add synthetic
1468*fe6060f1SDimitry Andric   // sections in the required order from the beginning so that the in.relaIplt
1469e8d8bef9SDimitry Andric   // section is placed last in an output section. Here we just do not apply
1470e8d8bef9SDimitry Andric   // sorting for an output section which holds the in.relaIplt section.
1471e8d8bef9SDimitry Andric   if (in.relaIplt->getParent() == sec)
1472e8d8bef9SDimitry Andric     return;
1473e8d8bef9SDimitry Andric 
14745ffd83dbSDimitry Andric   // Sort input sections by priority using the list provided by
14755ffd83dbSDimitry Andric   // --symbol-ordering-file or --shuffle-sections=. This is a least significant
14765ffd83dbSDimitry Andric   // digit radix sort. The sections may be sorted stably again by a more
14775ffd83dbSDimitry Andric   // significant key.
14785ffd83dbSDimitry Andric   if (!order.empty())
14795ffd83dbSDimitry Andric     for (BaseCommand *b : sec->sectionCommands)
14805ffd83dbSDimitry Andric       if (auto *isd = dyn_cast<InputSectionDescription>(b))
14815ffd83dbSDimitry Andric         sortISDBySectionOrder(isd, order);
14825ffd83dbSDimitry Andric 
14830b57cec5SDimitry Andric   // Sort input sections by section name suffixes for
14840b57cec5SDimitry Andric   // __attribute__((init_priority(N))).
14850b57cec5SDimitry Andric   if (name == ".init_array" || name == ".fini_array") {
14860b57cec5SDimitry Andric     if (!script->hasSectionsCommand)
14870b57cec5SDimitry Andric       sec->sortInitFini();
14880b57cec5SDimitry Andric     return;
14890b57cec5SDimitry Andric   }
14900b57cec5SDimitry Andric 
14910b57cec5SDimitry Andric   // Sort input sections by the special rule for .ctors and .dtors.
14920b57cec5SDimitry Andric   if (name == ".ctors" || name == ".dtors") {
14930b57cec5SDimitry Andric     if (!script->hasSectionsCommand)
14940b57cec5SDimitry Andric       sec->sortCtorsDtors();
14950b57cec5SDimitry Andric     return;
14960b57cec5SDimitry Andric   }
14970b57cec5SDimitry Andric 
14980b57cec5SDimitry Andric   // .toc is allocated just after .got and is accessed using GOT-relative
14990b57cec5SDimitry Andric   // relocations. Object files compiled with small code model have an
15000b57cec5SDimitry Andric   // addressable range of [.got, .got + 0xFFFC] for GOT-relative relocations.
15010b57cec5SDimitry Andric   // To reduce the risk of relocation overflow, .toc contents are sorted so that
15020b57cec5SDimitry Andric   // sections having smaller relocation offsets are at beginning of .toc
15030b57cec5SDimitry Andric   if (config->emachine == EM_PPC64 && name == ".toc") {
15040b57cec5SDimitry Andric     if (script->hasSectionsCommand)
15050b57cec5SDimitry Andric       return;
15060b57cec5SDimitry Andric     assert(sec->sectionCommands.size() == 1);
15070b57cec5SDimitry Andric     auto *isd = cast<InputSectionDescription>(sec->sectionCommands[0]);
15080b57cec5SDimitry Andric     llvm::stable_sort(isd->sections,
15090b57cec5SDimitry Andric                       [](const InputSection *a, const InputSection *b) -> bool {
15100b57cec5SDimitry Andric                         return a->file->ppc64SmallCodeModelTocRelocs &&
15110b57cec5SDimitry Andric                                !b->file->ppc64SmallCodeModelTocRelocs;
15120b57cec5SDimitry Andric                       });
15130b57cec5SDimitry Andric     return;
15140b57cec5SDimitry Andric   }
15150b57cec5SDimitry Andric }
15160b57cec5SDimitry Andric 
15170b57cec5SDimitry Andric // If no layout was provided by linker script, we want to apply default
15180b57cec5SDimitry Andric // sorting for special input sections. This also handles --symbol-ordering-file.
15190b57cec5SDimitry Andric template <class ELFT> void Writer<ELFT>::sortInputSections() {
15200b57cec5SDimitry Andric   // Build the order once since it is expensive.
15210b57cec5SDimitry Andric   DenseMap<const InputSectionBase *, int> order = buildSectionOrder();
15225ffd83dbSDimitry Andric   maybeShuffle(order);
15230b57cec5SDimitry Andric   for (BaseCommand *base : script->sectionCommands)
15240b57cec5SDimitry Andric     if (auto *sec = dyn_cast<OutputSection>(base))
15250b57cec5SDimitry Andric       sortSection(sec, order);
15260b57cec5SDimitry Andric }
15270b57cec5SDimitry Andric 
15280b57cec5SDimitry Andric template <class ELFT> void Writer<ELFT>::sortSections() {
1529e8d8bef9SDimitry Andric   llvm::TimeTraceScope timeScope("Sort sections");
15300b57cec5SDimitry Andric   script->adjustSectionsBeforeSorting();
15310b57cec5SDimitry Andric 
15320b57cec5SDimitry Andric   // Don't sort if using -r. It is not necessary and we want to preserve the
15330b57cec5SDimitry Andric   // relative order for SHF_LINK_ORDER sections.
15340b57cec5SDimitry Andric   if (config->relocatable)
15350b57cec5SDimitry Andric     return;
15360b57cec5SDimitry Andric 
15370b57cec5SDimitry Andric   sortInputSections();
15380b57cec5SDimitry Andric 
15390b57cec5SDimitry Andric   for (BaseCommand *base : script->sectionCommands) {
15400b57cec5SDimitry Andric     auto *os = dyn_cast<OutputSection>(base);
15410b57cec5SDimitry Andric     if (!os)
15420b57cec5SDimitry Andric       continue;
15430b57cec5SDimitry Andric     os->sortRank = getSectionRank(os);
15440b57cec5SDimitry Andric 
15450b57cec5SDimitry Andric     // We want to assign rude approximation values to outSecOff fields
15460b57cec5SDimitry Andric     // to know the relative order of the input sections. We use it for
15470b57cec5SDimitry Andric     // sorting SHF_LINK_ORDER sections. See resolveShfLinkOrder().
15480b57cec5SDimitry Andric     uint64_t i = 0;
15490b57cec5SDimitry Andric     for (InputSection *sec : getInputSections(os))
15500b57cec5SDimitry Andric       sec->outSecOff = i++;
15510b57cec5SDimitry Andric   }
15520b57cec5SDimitry Andric 
15530b57cec5SDimitry Andric   if (!script->hasSectionsCommand) {
15540b57cec5SDimitry Andric     // We know that all the OutputSections are contiguous in this case.
15550b57cec5SDimitry Andric     auto isSection = [](BaseCommand *base) { return isa<OutputSection>(base); };
15560b57cec5SDimitry Andric     std::stable_sort(
15570b57cec5SDimitry Andric         llvm::find_if(script->sectionCommands, isSection),
15580b57cec5SDimitry Andric         llvm::find_if(llvm::reverse(script->sectionCommands), isSection).base(),
15590b57cec5SDimitry Andric         compareSections);
15605ffd83dbSDimitry Andric 
15615ffd83dbSDimitry Andric     // Process INSERT commands. From this point onwards the order of
15625ffd83dbSDimitry Andric     // script->sectionCommands is fixed.
15635ffd83dbSDimitry Andric     script->processInsertCommands();
15640b57cec5SDimitry Andric     return;
15650b57cec5SDimitry Andric   }
15660b57cec5SDimitry Andric 
15675ffd83dbSDimitry Andric   script->processInsertCommands();
15685ffd83dbSDimitry Andric 
15690b57cec5SDimitry Andric   // Orphan sections are sections present in the input files which are
15700b57cec5SDimitry Andric   // not explicitly placed into the output file by the linker script.
15710b57cec5SDimitry Andric   //
15720b57cec5SDimitry Andric   // The sections in the linker script are already in the correct
15730b57cec5SDimitry Andric   // order. We have to figuere out where to insert the orphan
15740b57cec5SDimitry Andric   // sections.
15750b57cec5SDimitry Andric   //
15760b57cec5SDimitry Andric   // The order of the sections in the script is arbitrary and may not agree with
15770b57cec5SDimitry Andric   // compareSections. This means that we cannot easily define a strict weak
15780b57cec5SDimitry Andric   // ordering. To see why, consider a comparison of a section in the script and
15790b57cec5SDimitry Andric   // one not in the script. We have a two simple options:
15800b57cec5SDimitry Andric   // * Make them equivalent (a is not less than b, and b is not less than a).
15810b57cec5SDimitry Andric   //   The problem is then that equivalence has to be transitive and we can
15820b57cec5SDimitry Andric   //   have sections a, b and c with only b in a script and a less than c
15830b57cec5SDimitry Andric   //   which breaks this property.
15840b57cec5SDimitry Andric   // * Use compareSectionsNonScript. Given that the script order doesn't have
15850b57cec5SDimitry Andric   //   to match, we can end up with sections a, b, c, d where b and c are in the
15860b57cec5SDimitry Andric   //   script and c is compareSectionsNonScript less than b. In which case d
15870b57cec5SDimitry Andric   //   can be equivalent to c, a to b and d < a. As a concrete example:
15880b57cec5SDimitry Andric   //   .a (rx) # not in script
15890b57cec5SDimitry Andric   //   .b (rx) # in script
15900b57cec5SDimitry Andric   //   .c (ro) # in script
15910b57cec5SDimitry Andric   //   .d (ro) # not in script
15920b57cec5SDimitry Andric   //
15930b57cec5SDimitry Andric   // The way we define an order then is:
15940b57cec5SDimitry Andric   // *  Sort only the orphan sections. They are in the end right now.
15950b57cec5SDimitry Andric   // *  Move each orphan section to its preferred position. We try
15960b57cec5SDimitry Andric   //    to put each section in the last position where it can share
15970b57cec5SDimitry Andric   //    a PT_LOAD.
15980b57cec5SDimitry Andric   //
15990b57cec5SDimitry Andric   // There is some ambiguity as to where exactly a new entry should be
16000b57cec5SDimitry Andric   // inserted, because Commands contains not only output section
16010b57cec5SDimitry Andric   // commands but also other types of commands such as symbol assignment
16020b57cec5SDimitry Andric   // expressions. There's no correct answer here due to the lack of the
16030b57cec5SDimitry Andric   // formal specification of the linker script. We use heuristics to
16040b57cec5SDimitry Andric   // determine whether a new output command should be added before or
16050b57cec5SDimitry Andric   // after another commands. For the details, look at shouldSkip
16060b57cec5SDimitry Andric   // function.
16070b57cec5SDimitry Andric 
16080b57cec5SDimitry Andric   auto i = script->sectionCommands.begin();
16090b57cec5SDimitry Andric   auto e = script->sectionCommands.end();
16100b57cec5SDimitry Andric   auto nonScriptI = std::find_if(i, e, [](BaseCommand *base) {
16110b57cec5SDimitry Andric     if (auto *sec = dyn_cast<OutputSection>(base))
16120b57cec5SDimitry Andric       return sec->sectionIndex == UINT32_MAX;
16130b57cec5SDimitry Andric     return false;
16140b57cec5SDimitry Andric   });
16150b57cec5SDimitry Andric 
16160b57cec5SDimitry Andric   // Sort the orphan sections.
16170b57cec5SDimitry Andric   std::stable_sort(nonScriptI, e, compareSections);
16180b57cec5SDimitry Andric 
16190b57cec5SDimitry Andric   // As a horrible special case, skip the first . assignment if it is before any
16200b57cec5SDimitry Andric   // section. We do this because it is common to set a load address by starting
16210b57cec5SDimitry Andric   // the script with ". = 0xabcd" and the expectation is that every section is
16220b57cec5SDimitry Andric   // after that.
16230b57cec5SDimitry Andric   auto firstSectionOrDotAssignment =
16240b57cec5SDimitry Andric       std::find_if(i, e, [](BaseCommand *cmd) { return !shouldSkip(cmd); });
16250b57cec5SDimitry Andric   if (firstSectionOrDotAssignment != e &&
16260b57cec5SDimitry Andric       isa<SymbolAssignment>(**firstSectionOrDotAssignment))
16270b57cec5SDimitry Andric     ++firstSectionOrDotAssignment;
16280b57cec5SDimitry Andric   i = firstSectionOrDotAssignment;
16290b57cec5SDimitry Andric 
16300b57cec5SDimitry Andric   while (nonScriptI != e) {
16310b57cec5SDimitry Andric     auto pos = findOrphanPos(i, nonScriptI);
16320b57cec5SDimitry Andric     OutputSection *orphan = cast<OutputSection>(*nonScriptI);
16330b57cec5SDimitry Andric 
16340b57cec5SDimitry Andric     // As an optimization, find all sections with the same sort rank
16350b57cec5SDimitry Andric     // and insert them with one rotate.
16360b57cec5SDimitry Andric     unsigned rank = orphan->sortRank;
16370b57cec5SDimitry Andric     auto end = std::find_if(nonScriptI + 1, e, [=](BaseCommand *cmd) {
16380b57cec5SDimitry Andric       return cast<OutputSection>(cmd)->sortRank != rank;
16390b57cec5SDimitry Andric     });
16400b57cec5SDimitry Andric     std::rotate(pos, nonScriptI, end);
16410b57cec5SDimitry Andric     nonScriptI = end;
16420b57cec5SDimitry Andric   }
16430b57cec5SDimitry Andric 
16440b57cec5SDimitry Andric   script->adjustSectionsAfterSorting();
16450b57cec5SDimitry Andric }
16460b57cec5SDimitry Andric 
16470b57cec5SDimitry Andric static bool compareByFilePosition(InputSection *a, InputSection *b) {
1648e8d8bef9SDimitry Andric   InputSection *la = a->flags & SHF_LINK_ORDER ? a->getLinkOrderDep() : nullptr;
1649e8d8bef9SDimitry Andric   InputSection *lb = b->flags & SHF_LINK_ORDER ? b->getLinkOrderDep() : nullptr;
1650e8d8bef9SDimitry Andric   // SHF_LINK_ORDER sections with non-zero sh_link are ordered before
1651e8d8bef9SDimitry Andric   // non-SHF_LINK_ORDER sections and SHF_LINK_ORDER sections with zero sh_link.
1652e8d8bef9SDimitry Andric   if (!la || !lb)
1653e8d8bef9SDimitry Andric     return la && !lb;
16540b57cec5SDimitry Andric   OutputSection *aOut = la->getParent();
16550b57cec5SDimitry Andric   OutputSection *bOut = lb->getParent();
16560b57cec5SDimitry Andric 
16570b57cec5SDimitry Andric   if (aOut != bOut)
16585ffd83dbSDimitry Andric     return aOut->addr < bOut->addr;
16590b57cec5SDimitry Andric   return la->outSecOff < lb->outSecOff;
16600b57cec5SDimitry Andric }
16610b57cec5SDimitry Andric 
16620b57cec5SDimitry Andric template <class ELFT> void Writer<ELFT>::resolveShfLinkOrder() {
1663e8d8bef9SDimitry Andric   llvm::TimeTraceScope timeScope("Resolve SHF_LINK_ORDER");
16640b57cec5SDimitry Andric   for (OutputSection *sec : outputSections) {
16650b57cec5SDimitry Andric     if (!(sec->flags & SHF_LINK_ORDER))
16660b57cec5SDimitry Andric       continue;
16670b57cec5SDimitry Andric 
166885868e8aSDimitry Andric     // The ARM.exidx section use SHF_LINK_ORDER, but we have consolidated
166985868e8aSDimitry Andric     // this processing inside the ARMExidxsyntheticsection::finalizeContents().
167085868e8aSDimitry Andric     if (!config->relocatable && config->emachine == EM_ARM &&
167185868e8aSDimitry Andric         sec->type == SHT_ARM_EXIDX)
167285868e8aSDimitry Andric       continue;
167385868e8aSDimitry Andric 
1674e8d8bef9SDimitry Andric     // Link order may be distributed across several InputSectionDescriptions.
1675e8d8bef9SDimitry Andric     // Sorting is performed separately.
16760b57cec5SDimitry Andric     std::vector<InputSection **> scriptSections;
16770b57cec5SDimitry Andric     std::vector<InputSection *> sections;
16780b57cec5SDimitry Andric     for (BaseCommand *base : sec->sectionCommands) {
1679e8d8bef9SDimitry Andric       auto *isd = dyn_cast<InputSectionDescription>(base);
1680e8d8bef9SDimitry Andric       if (!isd)
1681e8d8bef9SDimitry Andric         continue;
1682e8d8bef9SDimitry Andric       bool hasLinkOrder = false;
1683e8d8bef9SDimitry Andric       scriptSections.clear();
1684e8d8bef9SDimitry Andric       sections.clear();
16850b57cec5SDimitry Andric       for (InputSection *&isec : isd->sections) {
1686e8d8bef9SDimitry Andric         if (isec->flags & SHF_LINK_ORDER) {
168785868e8aSDimitry Andric           InputSection *link = isec->getLinkOrderDep();
1688e8d8bef9SDimitry Andric           if (link && !link->getParent())
168985868e8aSDimitry Andric             error(toString(isec) + ": sh_link points to discarded section " +
169085868e8aSDimitry Andric                   toString(link));
1691e8d8bef9SDimitry Andric           hasLinkOrder = true;
16920b57cec5SDimitry Andric         }
1693e8d8bef9SDimitry Andric         scriptSections.push_back(&isec);
1694e8d8bef9SDimitry Andric         sections.push_back(isec);
16950b57cec5SDimitry Andric       }
1696e8d8bef9SDimitry Andric       if (hasLinkOrder && errorCount() == 0) {
16970b57cec5SDimitry Andric         llvm::stable_sort(sections, compareByFilePosition);
1698e8d8bef9SDimitry Andric         for (int i = 0, n = sections.size(); i != n; ++i)
16990b57cec5SDimitry Andric           *scriptSections[i] = sections[i];
17000b57cec5SDimitry Andric       }
17010b57cec5SDimitry Andric     }
1702e8d8bef9SDimitry Andric   }
1703e8d8bef9SDimitry Andric }
17040b57cec5SDimitry Andric 
17055ffd83dbSDimitry Andric static void finalizeSynthetic(SyntheticSection *sec) {
1706e8d8bef9SDimitry Andric   if (sec && sec->isNeeded() && sec->getParent()) {
1707e8d8bef9SDimitry Andric     llvm::TimeTraceScope timeScope("Finalize synthetic sections", sec->name);
17085ffd83dbSDimitry Andric     sec->finalizeContents();
17095ffd83dbSDimitry Andric   }
1710e8d8bef9SDimitry Andric }
17115ffd83dbSDimitry Andric 
17120b57cec5SDimitry Andric // We need to generate and finalize the content that depends on the address of
17130b57cec5SDimitry Andric // InputSections. As the generation of the content may also alter InputSection
17140b57cec5SDimitry Andric // addresses we must converge to a fixed point. We do that here. See the comment
17150b57cec5SDimitry Andric // in Writer<ELFT>::finalizeSections().
17160b57cec5SDimitry Andric template <class ELFT> void Writer<ELFT>::finalizeAddressDependentContent() {
1717e8d8bef9SDimitry Andric   llvm::TimeTraceScope timeScope("Finalize address dependent content");
17180b57cec5SDimitry Andric   ThunkCreator tc;
17190b57cec5SDimitry Andric   AArch64Err843419Patcher a64p;
172085868e8aSDimitry Andric   ARMErr657417Patcher a32p;
17210b57cec5SDimitry Andric   script->assignAddresses();
17225ffd83dbSDimitry Andric   // .ARM.exidx and SHF_LINK_ORDER do not require precise addresses, but they
17235ffd83dbSDimitry Andric   // do require the relative addresses of OutputSections because linker scripts
17245ffd83dbSDimitry Andric   // can assign Virtual Addresses to OutputSections that are not monotonically
17255ffd83dbSDimitry Andric   // increasing.
17265ffd83dbSDimitry Andric   for (Partition &part : partitions)
17275ffd83dbSDimitry Andric     finalizeSynthetic(part.armExidx);
17285ffd83dbSDimitry Andric   resolveShfLinkOrder();
17295ffd83dbSDimitry Andric 
17305ffd83dbSDimitry Andric   // Converts call x@GDPLT to call __tls_get_addr
17315ffd83dbSDimitry Andric   if (config->emachine == EM_HEXAGON)
17325ffd83dbSDimitry Andric     hexagonTLSSymbolUpdate(outputSections);
17330b57cec5SDimitry Andric 
173485868e8aSDimitry Andric   int assignPasses = 0;
173585868e8aSDimitry Andric   for (;;) {
173685868e8aSDimitry Andric     bool changed = target->needsThunks && tc.createThunks(outputSections);
173785868e8aSDimitry Andric 
173885868e8aSDimitry Andric     // With Thunk Size much smaller than branch range we expect to
1739e8d8bef9SDimitry Andric     // converge quickly; if we get to 15 something has gone wrong.
1740e8d8bef9SDimitry Andric     if (changed && tc.pass >= 15) {
174185868e8aSDimitry Andric       error("thunk creation not converged");
174285868e8aSDimitry Andric       break;
174385868e8aSDimitry Andric     }
17440b57cec5SDimitry Andric 
17450b57cec5SDimitry Andric     if (config->fixCortexA53Errata843419) {
17460b57cec5SDimitry Andric       if (changed)
17470b57cec5SDimitry Andric         script->assignAddresses();
17480b57cec5SDimitry Andric       changed |= a64p.createFixes();
17490b57cec5SDimitry Andric     }
175085868e8aSDimitry Andric     if (config->fixCortexA8) {
175185868e8aSDimitry Andric       if (changed)
175285868e8aSDimitry Andric         script->assignAddresses();
175385868e8aSDimitry Andric       changed |= a32p.createFixes();
175485868e8aSDimitry Andric     }
17550b57cec5SDimitry Andric 
17560b57cec5SDimitry Andric     if (in.mipsGot)
17570b57cec5SDimitry Andric       in.mipsGot->updateAllocSize();
17580b57cec5SDimitry Andric 
17590b57cec5SDimitry Andric     for (Partition &part : partitions) {
17600b57cec5SDimitry Andric       changed |= part.relaDyn->updateAllocSize();
17610b57cec5SDimitry Andric       if (part.relrDyn)
17620b57cec5SDimitry Andric         changed |= part.relrDyn->updateAllocSize();
17630b57cec5SDimitry Andric     }
17640b57cec5SDimitry Andric 
176585868e8aSDimitry Andric     const Defined *changedSym = script->assignAddresses();
176685868e8aSDimitry Andric     if (!changed) {
176785868e8aSDimitry Andric       // Some symbols may be dependent on section addresses. When we break the
176885868e8aSDimitry Andric       // loop, the symbol values are finalized because a previous
176985868e8aSDimitry Andric       // assignAddresses() finalized section addresses.
177085868e8aSDimitry Andric       if (!changedSym)
177185868e8aSDimitry Andric         break;
177285868e8aSDimitry Andric       if (++assignPasses == 5) {
177385868e8aSDimitry Andric         errorOrWarn("assignment to symbol " + toString(*changedSym) +
177485868e8aSDimitry Andric                     " does not converge");
177585868e8aSDimitry Andric         break;
177685868e8aSDimitry Andric       }
177785868e8aSDimitry Andric     }
17780b57cec5SDimitry Andric   }
17795ffd83dbSDimitry Andric 
17805ffd83dbSDimitry Andric   // If addrExpr is set, the address may not be a multiple of the alignment.
17815ffd83dbSDimitry Andric   // Warn because this is error-prone.
17825ffd83dbSDimitry Andric   for (BaseCommand *cmd : script->sectionCommands)
17835ffd83dbSDimitry Andric     if (auto *os = dyn_cast<OutputSection>(cmd))
17845ffd83dbSDimitry Andric       if (os->addr % os->alignment != 0)
17855ffd83dbSDimitry Andric         warn("address (0x" + Twine::utohexstr(os->addr) + ") of section " +
17865ffd83dbSDimitry Andric              os->name + " is not a multiple of alignment (" +
17875ffd83dbSDimitry Andric              Twine(os->alignment) + ")");
17880b57cec5SDimitry Andric }
17890b57cec5SDimitry Andric 
1790*fe6060f1SDimitry Andric // If Input Sections have been shrunk (basic block sections) then
17915ffd83dbSDimitry Andric // update symbol values and sizes associated with these sections.  With basic
17925ffd83dbSDimitry Andric // block sections, input sections can shrink when the jump instructions at
17935ffd83dbSDimitry Andric // the end of the section are relaxed.
17945ffd83dbSDimitry Andric static void fixSymbolsAfterShrinking() {
17955ffd83dbSDimitry Andric   for (InputFile *File : objectFiles) {
17965ffd83dbSDimitry Andric     parallelForEach(File->getSymbols(), [&](Symbol *Sym) {
17975ffd83dbSDimitry Andric       auto *def = dyn_cast<Defined>(Sym);
17985ffd83dbSDimitry Andric       if (!def)
17995ffd83dbSDimitry Andric         return;
18005ffd83dbSDimitry Andric 
18015ffd83dbSDimitry Andric       const SectionBase *sec = def->section;
18025ffd83dbSDimitry Andric       if (!sec)
18035ffd83dbSDimitry Andric         return;
18045ffd83dbSDimitry Andric 
18055ffd83dbSDimitry Andric       const InputSectionBase *inputSec = dyn_cast<InputSectionBase>(sec->repl);
18065ffd83dbSDimitry Andric       if (!inputSec || !inputSec->bytesDropped)
18075ffd83dbSDimitry Andric         return;
18085ffd83dbSDimitry Andric 
18095ffd83dbSDimitry Andric       const size_t OldSize = inputSec->data().size();
18105ffd83dbSDimitry Andric       const size_t NewSize = OldSize - inputSec->bytesDropped;
18115ffd83dbSDimitry Andric 
18125ffd83dbSDimitry Andric       if (def->value > NewSize && def->value <= OldSize) {
18135ffd83dbSDimitry Andric         LLVM_DEBUG(llvm::dbgs()
18145ffd83dbSDimitry Andric                    << "Moving symbol " << Sym->getName() << " from "
18155ffd83dbSDimitry Andric                    << def->value << " to "
18165ffd83dbSDimitry Andric                    << def->value - inputSec->bytesDropped << " bytes\n");
18175ffd83dbSDimitry Andric         def->value -= inputSec->bytesDropped;
18185ffd83dbSDimitry Andric         return;
18195ffd83dbSDimitry Andric       }
18205ffd83dbSDimitry Andric 
18215ffd83dbSDimitry Andric       if (def->value + def->size > NewSize && def->value <= OldSize &&
18225ffd83dbSDimitry Andric           def->value + def->size <= OldSize) {
18235ffd83dbSDimitry Andric         LLVM_DEBUG(llvm::dbgs()
18245ffd83dbSDimitry Andric                    << "Shrinking symbol " << Sym->getName() << " from "
18255ffd83dbSDimitry Andric                    << def->size << " to " << def->size - inputSec->bytesDropped
18265ffd83dbSDimitry Andric                    << " bytes\n");
18275ffd83dbSDimitry Andric         def->size -= inputSec->bytesDropped;
18285ffd83dbSDimitry Andric       }
18295ffd83dbSDimitry Andric     });
18305ffd83dbSDimitry Andric   }
18315ffd83dbSDimitry Andric }
18325ffd83dbSDimitry Andric 
18335ffd83dbSDimitry Andric // If basic block sections exist, there are opportunities to delete fall thru
18345ffd83dbSDimitry Andric // jumps and shrink jump instructions after basic block reordering.  This
18355ffd83dbSDimitry Andric // relaxation pass does that.  It is only enabled when --optimize-bb-jumps
18365ffd83dbSDimitry Andric // option is used.
18375ffd83dbSDimitry Andric template <class ELFT> void Writer<ELFT>::optimizeBasicBlockJumps() {
18385ffd83dbSDimitry Andric   assert(config->optimizeBBJumps);
18395ffd83dbSDimitry Andric 
18405ffd83dbSDimitry Andric   script->assignAddresses();
18415ffd83dbSDimitry Andric   // For every output section that has executable input sections, this
18425ffd83dbSDimitry Andric   // does the following:
18435ffd83dbSDimitry Andric   //   1. Deletes all direct jump instructions in input sections that
18445ffd83dbSDimitry Andric   //      jump to the following section as it is not required.
18455ffd83dbSDimitry Andric   //   2. If there are two consecutive jump instructions, it checks
18465ffd83dbSDimitry Andric   //      if they can be flipped and one can be deleted.
18475ffd83dbSDimitry Andric   for (OutputSection *os : outputSections) {
18485ffd83dbSDimitry Andric     if (!(os->flags & SHF_EXECINSTR))
18495ffd83dbSDimitry Andric       continue;
18505ffd83dbSDimitry Andric     std::vector<InputSection *> sections = getInputSections(os);
18515ffd83dbSDimitry Andric     std::vector<unsigned> result(sections.size());
18525ffd83dbSDimitry Andric     // Delete all fall through jump instructions.  Also, check if two
18535ffd83dbSDimitry Andric     // consecutive jump instructions can be flipped so that a fall
18545ffd83dbSDimitry Andric     // through jmp instruction can be deleted.
18555ffd83dbSDimitry Andric     parallelForEachN(0, sections.size(), [&](size_t i) {
18565ffd83dbSDimitry Andric       InputSection *next = i + 1 < sections.size() ? sections[i + 1] : nullptr;
18575ffd83dbSDimitry Andric       InputSection &is = *sections[i];
18585ffd83dbSDimitry Andric       result[i] =
18595ffd83dbSDimitry Andric           target->deleteFallThruJmpInsn(is, is.getFile<ELFT>(), next) ? 1 : 0;
18605ffd83dbSDimitry Andric     });
18615ffd83dbSDimitry Andric     size_t numDeleted = std::count(result.begin(), result.end(), 1);
18625ffd83dbSDimitry Andric     if (numDeleted > 0) {
18635ffd83dbSDimitry Andric       script->assignAddresses();
18645ffd83dbSDimitry Andric       LLVM_DEBUG(llvm::dbgs()
18655ffd83dbSDimitry Andric                  << "Removing " << numDeleted << " fall through jumps\n");
18665ffd83dbSDimitry Andric     }
18675ffd83dbSDimitry Andric   }
18685ffd83dbSDimitry Andric 
18695ffd83dbSDimitry Andric   fixSymbolsAfterShrinking();
18705ffd83dbSDimitry Andric 
18715ffd83dbSDimitry Andric   for (OutputSection *os : outputSections) {
18725ffd83dbSDimitry Andric     std::vector<InputSection *> sections = getInputSections(os);
18735ffd83dbSDimitry Andric     for (InputSection *is : sections)
18745ffd83dbSDimitry Andric       is->trim();
18755ffd83dbSDimitry Andric   }
18760b57cec5SDimitry Andric }
18770b57cec5SDimitry Andric 
18780b57cec5SDimitry Andric // In order to allow users to manipulate linker-synthesized sections,
18790b57cec5SDimitry Andric // we had to add synthetic sections to the input section list early,
18800b57cec5SDimitry Andric // even before we make decisions whether they are needed. This allows
18810b57cec5SDimitry Andric // users to write scripts like this: ".mygot : { .got }".
18820b57cec5SDimitry Andric //
18830b57cec5SDimitry Andric // Doing it has an unintended side effects. If it turns out that we
18840b57cec5SDimitry Andric // don't need a .got (for example) at all because there's no
18850b57cec5SDimitry Andric // relocation that needs a .got, we don't want to emit .got.
18860b57cec5SDimitry Andric //
18870b57cec5SDimitry Andric // To deal with the above problem, this function is called after
18880b57cec5SDimitry Andric // scanRelocations is called to remove synthetic sections that turn
18890b57cec5SDimitry Andric // out to be empty.
18900b57cec5SDimitry Andric static void removeUnusedSyntheticSections() {
18910b57cec5SDimitry Andric   // All input synthetic sections that can be empty are placed after
1892*fe6060f1SDimitry Andric   // all regular ones. Reverse iterate to find the first synthetic section
1893*fe6060f1SDimitry Andric   // after a non-synthetic one which will be our starting point.
1894*fe6060f1SDimitry Andric   auto start = std::find_if(inputSections.rbegin(), inputSections.rend(),
1895*fe6060f1SDimitry Andric                             [](InputSectionBase *s) {
1896*fe6060f1SDimitry Andric                               return !isa<SyntheticSection>(s);
1897*fe6060f1SDimitry Andric                             })
1898*fe6060f1SDimitry Andric                    .base();
1899*fe6060f1SDimitry Andric 
1900*fe6060f1SDimitry Andric   DenseSet<InputSectionDescription *> isdSet;
1901*fe6060f1SDimitry Andric   // Mark unused synthetic sections for deletion
1902*fe6060f1SDimitry Andric   auto end = std::stable_partition(
1903*fe6060f1SDimitry Andric       start, inputSections.end(), [&](InputSectionBase *s) {
19040b57cec5SDimitry Andric         SyntheticSection *ss = dyn_cast<SyntheticSection>(s);
19050b57cec5SDimitry Andric         OutputSection *os = ss->getParent();
19060b57cec5SDimitry Andric         if (!os || ss->isNeeded())
1907*fe6060f1SDimitry Andric           return true;
19080b57cec5SDimitry Andric 
1909*fe6060f1SDimitry Andric         // If we reach here, then ss is an unused synthetic section and we want
1910*fe6060f1SDimitry Andric         // to remove it from the corresponding input section description, and
19115ffd83dbSDimitry Andric         // orphanSections.
19120b57cec5SDimitry Andric         for (BaseCommand *b : os->sectionCommands)
19130b57cec5SDimitry Andric           if (auto *isd = dyn_cast<InputSectionDescription>(b))
1914*fe6060f1SDimitry Andric             isdSet.insert(isd);
1915*fe6060f1SDimitry Andric 
1916*fe6060f1SDimitry Andric         llvm::erase_if(
1917*fe6060f1SDimitry Andric             script->orphanSections,
19185ffd83dbSDimitry Andric             [=](const InputSectionBase *isec) { return isec == ss; });
1919*fe6060f1SDimitry Andric 
1920*fe6060f1SDimitry Andric         return false;
1921*fe6060f1SDimitry Andric       });
1922*fe6060f1SDimitry Andric 
1923*fe6060f1SDimitry Andric   DenseSet<InputSectionBase *> unused(end, inputSections.end());
1924*fe6060f1SDimitry Andric   for (auto *isd : isdSet)
1925*fe6060f1SDimitry Andric     llvm::erase_if(isd->sections,
1926*fe6060f1SDimitry Andric                    [=](InputSection *isec) { return unused.count(isec); });
1927*fe6060f1SDimitry Andric 
1928*fe6060f1SDimitry Andric   // Erase unused synthetic sections.
1929*fe6060f1SDimitry Andric   inputSections.erase(end, inputSections.end());
19300b57cec5SDimitry Andric }
19310b57cec5SDimitry Andric 
19320b57cec5SDimitry Andric // Create output section objects and add them to OutputSections.
19330b57cec5SDimitry Andric template <class ELFT> void Writer<ELFT>::finalizeSections() {
19340b57cec5SDimitry Andric   Out::preinitArray = findSection(".preinit_array");
19350b57cec5SDimitry Andric   Out::initArray = findSection(".init_array");
19360b57cec5SDimitry Andric   Out::finiArray = findSection(".fini_array");
19370b57cec5SDimitry Andric 
19380b57cec5SDimitry Andric   // The linker needs to define SECNAME_start, SECNAME_end and SECNAME_stop
19390b57cec5SDimitry Andric   // symbols for sections, so that the runtime can get the start and end
19400b57cec5SDimitry Andric   // addresses of each section by section name. Add such symbols.
19410b57cec5SDimitry Andric   if (!config->relocatable) {
19420b57cec5SDimitry Andric     addStartEndSymbols();
19430b57cec5SDimitry Andric     for (BaseCommand *base : script->sectionCommands)
19440b57cec5SDimitry Andric       if (auto *sec = dyn_cast<OutputSection>(base))
19450b57cec5SDimitry Andric         addStartStopSymbols(sec);
19460b57cec5SDimitry Andric   }
19470b57cec5SDimitry Andric 
19480b57cec5SDimitry Andric   // Add _DYNAMIC symbol. Unlike GNU gold, our _DYNAMIC symbol has no type.
19490b57cec5SDimitry Andric   // It should be okay as no one seems to care about the type.
19500b57cec5SDimitry Andric   // Even the author of gold doesn't remember why gold behaves that way.
19510b57cec5SDimitry Andric   // https://sourceware.org/ml/binutils/2002-03/msg00360.html
19520b57cec5SDimitry Andric   if (mainPart->dynamic->parent)
19530b57cec5SDimitry Andric     symtab->addSymbol(Defined{/*file=*/nullptr, "_DYNAMIC", STB_WEAK,
19540b57cec5SDimitry Andric                               STV_HIDDEN, STT_NOTYPE,
19550b57cec5SDimitry Andric                               /*value=*/0, /*size=*/0, mainPart->dynamic});
19560b57cec5SDimitry Andric 
19570b57cec5SDimitry Andric   // Define __rel[a]_iplt_{start,end} symbols if needed.
19580b57cec5SDimitry Andric   addRelIpltSymbols();
19590b57cec5SDimitry Andric 
196085868e8aSDimitry Andric   // RISC-V's gp can address +/- 2 KiB, set it to .sdata + 0x800. This symbol
196185868e8aSDimitry Andric   // should only be defined in an executable. If .sdata does not exist, its
196285868e8aSDimitry Andric   // value/section does not matter but it has to be relative, so set its
196385868e8aSDimitry Andric   // st_shndx arbitrarily to 1 (Out::elfHeader).
196485868e8aSDimitry Andric   if (config->emachine == EM_RISCV && !config->shared) {
196585868e8aSDimitry Andric     OutputSection *sec = findSection(".sdata");
19660b57cec5SDimitry Andric     ElfSym::riscvGlobalPointer =
196785868e8aSDimitry Andric         addOptionalRegular("__global_pointer$", sec ? sec : Out::elfHeader,
196885868e8aSDimitry Andric                            0x800, STV_DEFAULT, STB_GLOBAL);
196985868e8aSDimitry Andric   }
19700b57cec5SDimitry Andric 
19710b57cec5SDimitry Andric   if (config->emachine == EM_X86_64) {
19720b57cec5SDimitry Andric     // On targets that support TLSDESC, _TLS_MODULE_BASE_ is defined in such a
19730b57cec5SDimitry Andric     // way that:
19740b57cec5SDimitry Andric     //
19750b57cec5SDimitry Andric     // 1) Without relaxation: it produces a dynamic TLSDESC relocation that
19760b57cec5SDimitry Andric     // computes 0.
19770b57cec5SDimitry Andric     // 2) With LD->LE relaxation: _TLS_MODULE_BASE_@tpoff = 0 (lowest address in
19780b57cec5SDimitry Andric     // the TLS block).
19790b57cec5SDimitry Andric     //
19800b57cec5SDimitry Andric     // 2) is special cased in @tpoff computation. To satisfy 1), we define it as
19810b57cec5SDimitry Andric     // an absolute symbol of zero. This is different from GNU linkers which
19820b57cec5SDimitry Andric     // define _TLS_MODULE_BASE_ relative to the first TLS section.
19830b57cec5SDimitry Andric     Symbol *s = symtab->find("_TLS_MODULE_BASE_");
19840b57cec5SDimitry Andric     if (s && s->isUndefined()) {
19850b57cec5SDimitry Andric       s->resolve(Defined{/*file=*/nullptr, s->getName(), STB_GLOBAL, STV_HIDDEN,
19860b57cec5SDimitry Andric                          STT_TLS, /*value=*/0, 0,
19870b57cec5SDimitry Andric                          /*section=*/nullptr});
19880b57cec5SDimitry Andric       ElfSym::tlsModuleBase = cast<Defined>(s);
19890b57cec5SDimitry Andric     }
19900b57cec5SDimitry Andric   }
19910b57cec5SDimitry Andric 
1992e8d8bef9SDimitry Andric   {
1993e8d8bef9SDimitry Andric     llvm::TimeTraceScope timeScope("Finalize .eh_frame");
19940b57cec5SDimitry Andric     // This responsible for splitting up .eh_frame section into
19950b57cec5SDimitry Andric     // pieces. The relocation scan uses those pieces, so this has to be
19960b57cec5SDimitry Andric     // earlier.
19970b57cec5SDimitry Andric     for (Partition &part : partitions)
19980b57cec5SDimitry Andric       finalizeSynthetic(part.ehFrame);
1999e8d8bef9SDimitry Andric   }
20000b57cec5SDimitry Andric 
2001480093f4SDimitry Andric   for (Symbol *sym : symtab->symbols())
2002480093f4SDimitry Andric     sym->isPreemptible = computeIsPreemptible(*sym);
200385868e8aSDimitry Andric 
200485868e8aSDimitry Andric   // Change values of linker-script-defined symbols from placeholders (assigned
200585868e8aSDimitry Andric   // by declareSymbols) to actual definitions.
200685868e8aSDimitry Andric   script->processSymbolAssignments();
20070b57cec5SDimitry Andric 
2008e8d8bef9SDimitry Andric   {
2009e8d8bef9SDimitry Andric     llvm::TimeTraceScope timeScope("Scan relocations");
2010e8d8bef9SDimitry Andric     // Scan relocations. This must be done after every symbol is declared so
2011e8d8bef9SDimitry Andric     // that we can correctly decide if a dynamic relocation is needed. This is
2012e8d8bef9SDimitry Andric     // called after processSymbolAssignments() because it needs to know whether
2013e8d8bef9SDimitry Andric     // a linker-script-defined symbol is absolute.
20145ffd83dbSDimitry Andric     ppc64noTocRelax.clear();
20150b57cec5SDimitry Andric     if (!config->relocatable) {
20160b57cec5SDimitry Andric       forEachRelSec(scanRelocations<ELFT>);
20170b57cec5SDimitry Andric       reportUndefinedSymbols<ELFT>();
20180b57cec5SDimitry Andric     }
2019e8d8bef9SDimitry Andric   }
20200b57cec5SDimitry Andric 
20210b57cec5SDimitry Andric   if (in.plt && in.plt->isNeeded())
20220b57cec5SDimitry Andric     in.plt->addSymbols();
20230b57cec5SDimitry Andric   if (in.iplt && in.iplt->isNeeded())
20240b57cec5SDimitry Andric     in.iplt->addSymbols();
20250b57cec5SDimitry Andric 
2026e8d8bef9SDimitry Andric   if (config->unresolvedSymbolsInShlib != UnresolvedPolicy::Ignore) {
2027*fe6060f1SDimitry Andric     auto diagnose =
2028*fe6060f1SDimitry Andric         config->unresolvedSymbolsInShlib == UnresolvedPolicy::ReportError
2029*fe6060f1SDimitry Andric             ? errorOrWarn
2030*fe6060f1SDimitry Andric             : warn;
20310b57cec5SDimitry Andric     // Error on undefined symbols in a shared object, if all of its DT_NEEDED
2032480093f4SDimitry Andric     // entries are seen. These cases would otherwise lead to runtime errors
20330b57cec5SDimitry Andric     // reported by the dynamic linker.
20340b57cec5SDimitry Andric     //
20350b57cec5SDimitry Andric     // ld.bfd traces all DT_NEEDED to emulate the logic of the dynamic linker to
20360b57cec5SDimitry Andric     // catch more cases. That is too much for us. Our approach resembles the one
20370b57cec5SDimitry Andric     // used in ld.gold, achieves a good balance to be useful but not too smart.
2038*fe6060f1SDimitry Andric     for (SharedFile *file : sharedFiles) {
2039*fe6060f1SDimitry Andric       bool allNeededIsKnown =
20400b57cec5SDimitry Andric           llvm::all_of(file->dtNeeded, [&](StringRef needed) {
20410b57cec5SDimitry Andric             return symtab->soNames.count(needed);
20420b57cec5SDimitry Andric           });
2043*fe6060f1SDimitry Andric       if (!allNeededIsKnown)
2044*fe6060f1SDimitry Andric         continue;
2045*fe6060f1SDimitry Andric       for (Symbol *sym : file->requiredSymbols)
20460b57cec5SDimitry Andric         if (sym->isUndefined() && !sym->isWeak())
2047*fe6060f1SDimitry Andric           diagnose(toString(file) + ": undefined reference to " +
20485ffd83dbSDimitry Andric                    toString(*sym) + " [--no-allow-shlib-undefined]");
20490b57cec5SDimitry Andric     }
2050e8d8bef9SDimitry Andric   }
20510b57cec5SDimitry Andric 
2052e8d8bef9SDimitry Andric   {
2053e8d8bef9SDimitry Andric     llvm::TimeTraceScope timeScope("Add symbols to symtabs");
20540b57cec5SDimitry Andric     // Now that we have defined all possible global symbols including linker-
20550b57cec5SDimitry Andric     // synthesized ones. Visit all symbols to give the finishing touches.
2056480093f4SDimitry Andric     for (Symbol *sym : symtab->symbols()) {
20570b57cec5SDimitry Andric       if (!includeInSymtab(*sym))
2058480093f4SDimitry Andric         continue;
20590b57cec5SDimitry Andric       if (in.symTab)
20600b57cec5SDimitry Andric         in.symTab->addSymbol(sym);
20610b57cec5SDimitry Andric 
20620b57cec5SDimitry Andric       if (sym->includeInDynsym()) {
20630b57cec5SDimitry Andric         partitions[sym->partition - 1].dynSymTab->addSymbol(sym);
20640b57cec5SDimitry Andric         if (auto *file = dyn_cast_or_null<SharedFile>(sym->file))
20650b57cec5SDimitry Andric           if (file->isNeeded && !sym->isUndefined())
20660b57cec5SDimitry Andric             addVerneed(sym);
20670b57cec5SDimitry Andric       }
2068480093f4SDimitry Andric     }
20690b57cec5SDimitry Andric 
2070e8d8bef9SDimitry Andric     // We also need to scan the dynamic relocation tables of the other
2071e8d8bef9SDimitry Andric     // partitions and add any referenced symbols to the partition's dynsym.
20720b57cec5SDimitry Andric     for (Partition &part : MutableArrayRef<Partition>(partitions).slice(1)) {
20730b57cec5SDimitry Andric       DenseSet<Symbol *> syms;
20740b57cec5SDimitry Andric       for (const SymbolTableEntry &e : part.dynSymTab->getSymbols())
20750b57cec5SDimitry Andric         syms.insert(e.sym);
20760b57cec5SDimitry Andric       for (DynamicReloc &reloc : part.relaDyn->relocs)
2077*fe6060f1SDimitry Andric         if (reloc.sym && reloc.needsDynSymIndex() &&
2078*fe6060f1SDimitry Andric             syms.insert(reloc.sym).second)
20790b57cec5SDimitry Andric           part.dynSymTab->addSymbol(reloc.sym);
20800b57cec5SDimitry Andric     }
2081e8d8bef9SDimitry Andric   }
20820b57cec5SDimitry Andric 
20830b57cec5SDimitry Andric   // Do not proceed if there was an undefined symbol.
20840b57cec5SDimitry Andric   if (errorCount())
20850b57cec5SDimitry Andric     return;
20860b57cec5SDimitry Andric 
20870b57cec5SDimitry Andric   if (in.mipsGot)
20880b57cec5SDimitry Andric     in.mipsGot->build();
20890b57cec5SDimitry Andric 
20900b57cec5SDimitry Andric   removeUnusedSyntheticSections();
20915ffd83dbSDimitry Andric   script->diagnoseOrphanHandling();
20920b57cec5SDimitry Andric 
20930b57cec5SDimitry Andric   sortSections();
20940b57cec5SDimitry Andric 
20950b57cec5SDimitry Andric   // Now that we have the final list, create a list of all the
20960b57cec5SDimitry Andric   // OutputSections for convenience.
20970b57cec5SDimitry Andric   for (BaseCommand *base : script->sectionCommands)
20980b57cec5SDimitry Andric     if (auto *sec = dyn_cast<OutputSection>(base))
20990b57cec5SDimitry Andric       outputSections.push_back(sec);
21000b57cec5SDimitry Andric 
21010b57cec5SDimitry Andric   // Prefer command line supplied address over other constraints.
21020b57cec5SDimitry Andric   for (OutputSection *sec : outputSections) {
21030b57cec5SDimitry Andric     auto i = config->sectionStartMap.find(sec->name);
21040b57cec5SDimitry Andric     if (i != config->sectionStartMap.end())
21050b57cec5SDimitry Andric       sec->addrExpr = [=] { return i->second; };
21060b57cec5SDimitry Andric   }
21070b57cec5SDimitry Andric 
21085ffd83dbSDimitry Andric   // With the outputSections available check for GDPLT relocations
21095ffd83dbSDimitry Andric   // and add __tls_get_addr symbol if needed.
21105ffd83dbSDimitry Andric   if (config->emachine == EM_HEXAGON && hexagonNeedsTLSSymbol(outputSections)) {
21115ffd83dbSDimitry Andric     Symbol *sym = symtab->addSymbol(Undefined{
21125ffd83dbSDimitry Andric         nullptr, "__tls_get_addr", STB_GLOBAL, STV_DEFAULT, STT_NOTYPE});
21135ffd83dbSDimitry Andric     sym->isPreemptible = true;
21145ffd83dbSDimitry Andric     partitions[0].dynSymTab->addSymbol(sym);
21155ffd83dbSDimitry Andric   }
21165ffd83dbSDimitry Andric 
21170b57cec5SDimitry Andric   // This is a bit of a hack. A value of 0 means undef, so we set it
21180b57cec5SDimitry Andric   // to 1 to make __ehdr_start defined. The section number is not
21190b57cec5SDimitry Andric   // particularly relevant.
21200b57cec5SDimitry Andric   Out::elfHeader->sectionIndex = 1;
21210b57cec5SDimitry Andric 
21220b57cec5SDimitry Andric   for (size_t i = 0, e = outputSections.size(); i != e; ++i) {
21230b57cec5SDimitry Andric     OutputSection *sec = outputSections[i];
21240b57cec5SDimitry Andric     sec->sectionIndex = i + 1;
21250b57cec5SDimitry Andric     sec->shName = in.shStrTab->addString(sec->name);
21260b57cec5SDimitry Andric   }
21270b57cec5SDimitry Andric 
21280b57cec5SDimitry Andric   // Binary and relocatable output does not have PHDRS.
21290b57cec5SDimitry Andric   // The headers have to be created before finalize as that can influence the
21300b57cec5SDimitry Andric   // image base and the dynamic section on mips includes the image base.
21310b57cec5SDimitry Andric   if (!config->relocatable && !config->oFormatBinary) {
21320b57cec5SDimitry Andric     for (Partition &part : partitions) {
21330b57cec5SDimitry Andric       part.phdrs = script->hasPhdrsCommands() ? script->createPhdrs()
21340b57cec5SDimitry Andric                                               : createPhdrs(part);
21350b57cec5SDimitry Andric       if (config->emachine == EM_ARM) {
21360b57cec5SDimitry Andric         // PT_ARM_EXIDX is the ARM EHABI equivalent of PT_GNU_EH_FRAME
21370b57cec5SDimitry Andric         addPhdrForSection(part, SHT_ARM_EXIDX, PT_ARM_EXIDX, PF_R);
21380b57cec5SDimitry Andric       }
21390b57cec5SDimitry Andric       if (config->emachine == EM_MIPS) {
21400b57cec5SDimitry Andric         // Add separate segments for MIPS-specific sections.
21410b57cec5SDimitry Andric         addPhdrForSection(part, SHT_MIPS_REGINFO, PT_MIPS_REGINFO, PF_R);
21420b57cec5SDimitry Andric         addPhdrForSection(part, SHT_MIPS_OPTIONS, PT_MIPS_OPTIONS, PF_R);
21430b57cec5SDimitry Andric         addPhdrForSection(part, SHT_MIPS_ABIFLAGS, PT_MIPS_ABIFLAGS, PF_R);
21440b57cec5SDimitry Andric       }
21450b57cec5SDimitry Andric     }
21460b57cec5SDimitry Andric     Out::programHeaders->size = sizeof(Elf_Phdr) * mainPart->phdrs.size();
21470b57cec5SDimitry Andric 
21480b57cec5SDimitry Andric     // Find the TLS segment. This happens before the section layout loop so that
21490b57cec5SDimitry Andric     // Android relocation packing can look up TLS symbol addresses. We only need
21500b57cec5SDimitry Andric     // to care about the main partition here because all TLS symbols were moved
21510b57cec5SDimitry Andric     // to the main partition (see MarkLive.cpp).
21520b57cec5SDimitry Andric     for (PhdrEntry *p : mainPart->phdrs)
21530b57cec5SDimitry Andric       if (p->p_type == PT_TLS)
21540b57cec5SDimitry Andric         Out::tlsPhdr = p;
21550b57cec5SDimitry Andric   }
21560b57cec5SDimitry Andric 
21570b57cec5SDimitry Andric   // Some symbols are defined in term of program headers. Now that we
21580b57cec5SDimitry Andric   // have the headers, we can find out which sections they point to.
21590b57cec5SDimitry Andric   setReservedSymbolSections();
21600b57cec5SDimitry Andric 
2161e8d8bef9SDimitry Andric   {
2162e8d8bef9SDimitry Andric     llvm::TimeTraceScope timeScope("Finalize synthetic sections");
2163e8d8bef9SDimitry Andric 
21640b57cec5SDimitry Andric     finalizeSynthetic(in.bss);
21650b57cec5SDimitry Andric     finalizeSynthetic(in.bssRelRo);
21660b57cec5SDimitry Andric     finalizeSynthetic(in.symTabShndx);
21670b57cec5SDimitry Andric     finalizeSynthetic(in.shStrTab);
21680b57cec5SDimitry Andric     finalizeSynthetic(in.strTab);
21690b57cec5SDimitry Andric     finalizeSynthetic(in.got);
21700b57cec5SDimitry Andric     finalizeSynthetic(in.mipsGot);
21710b57cec5SDimitry Andric     finalizeSynthetic(in.igotPlt);
21720b57cec5SDimitry Andric     finalizeSynthetic(in.gotPlt);
21730b57cec5SDimitry Andric     finalizeSynthetic(in.relaIplt);
21740b57cec5SDimitry Andric     finalizeSynthetic(in.relaPlt);
21750b57cec5SDimitry Andric     finalizeSynthetic(in.plt);
21760b57cec5SDimitry Andric     finalizeSynthetic(in.iplt);
21770b57cec5SDimitry Andric     finalizeSynthetic(in.ppc32Got2);
21780b57cec5SDimitry Andric     finalizeSynthetic(in.partIndex);
21790b57cec5SDimitry Andric 
21800b57cec5SDimitry Andric     // Dynamic section must be the last one in this list and dynamic
21810b57cec5SDimitry Andric     // symbol table section (dynSymTab) must be the first one.
21820b57cec5SDimitry Andric     for (Partition &part : partitions) {
21830b57cec5SDimitry Andric       finalizeSynthetic(part.dynSymTab);
21840b57cec5SDimitry Andric       finalizeSynthetic(part.gnuHashTab);
21850b57cec5SDimitry Andric       finalizeSynthetic(part.hashTab);
21860b57cec5SDimitry Andric       finalizeSynthetic(part.verDef);
21870b57cec5SDimitry Andric       finalizeSynthetic(part.relaDyn);
21880b57cec5SDimitry Andric       finalizeSynthetic(part.relrDyn);
21890b57cec5SDimitry Andric       finalizeSynthetic(part.ehFrameHdr);
21900b57cec5SDimitry Andric       finalizeSynthetic(part.verSym);
21910b57cec5SDimitry Andric       finalizeSynthetic(part.verNeed);
21920b57cec5SDimitry Andric       finalizeSynthetic(part.dynamic);
21930b57cec5SDimitry Andric     }
2194e8d8bef9SDimitry Andric   }
21950b57cec5SDimitry Andric 
21960b57cec5SDimitry Andric   if (!script->hasSectionsCommand && !config->relocatable)
21970b57cec5SDimitry Andric     fixSectionAlignments();
21980b57cec5SDimitry Andric 
21990b57cec5SDimitry Andric   // This is used to:
22000b57cec5SDimitry Andric   // 1) Create "thunks":
22010b57cec5SDimitry Andric   //    Jump instructions in many ISAs have small displacements, and therefore
22020b57cec5SDimitry Andric   //    they cannot jump to arbitrary addresses in memory. For example, RISC-V
22030b57cec5SDimitry Andric   //    JAL instruction can target only +-1 MiB from PC. It is a linker's
22040b57cec5SDimitry Andric   //    responsibility to create and insert small pieces of code between
22050b57cec5SDimitry Andric   //    sections to extend the ranges if jump targets are out of range. Such
22060b57cec5SDimitry Andric   //    code pieces are called "thunks".
22070b57cec5SDimitry Andric   //
22080b57cec5SDimitry Andric   //    We add thunks at this stage. We couldn't do this before this point
22090b57cec5SDimitry Andric   //    because this is the earliest point where we know sizes of sections and
22100b57cec5SDimitry Andric   //    their layouts (that are needed to determine if jump targets are in
22110b57cec5SDimitry Andric   //    range).
22120b57cec5SDimitry Andric   //
22130b57cec5SDimitry Andric   // 2) Update the sections. We need to generate content that depends on the
22140b57cec5SDimitry Andric   //    address of InputSections. For example, MIPS GOT section content or
22150b57cec5SDimitry Andric   //    android packed relocations sections content.
22160b57cec5SDimitry Andric   //
22170b57cec5SDimitry Andric   // 3) Assign the final values for the linker script symbols. Linker scripts
22180b57cec5SDimitry Andric   //    sometimes using forward symbol declarations. We want to set the correct
22190b57cec5SDimitry Andric   //    values. They also might change after adding the thunks.
22200b57cec5SDimitry Andric   finalizeAddressDependentContent();
22215ffd83dbSDimitry Andric   if (errorCount())
22225ffd83dbSDimitry Andric     return;
22230b57cec5SDimitry Andric 
2224e8d8bef9SDimitry Andric   {
2225e8d8bef9SDimitry Andric     llvm::TimeTraceScope timeScope("Finalize synthetic sections");
2226e8d8bef9SDimitry Andric     // finalizeAddressDependentContent may have added local symbols to the
2227e8d8bef9SDimitry Andric     // static symbol table.
22280b57cec5SDimitry Andric     finalizeSynthetic(in.symTab);
22290b57cec5SDimitry Andric     finalizeSynthetic(in.ppc64LongBranchTarget);
2230e8d8bef9SDimitry Andric   }
22310b57cec5SDimitry Andric 
22325ffd83dbSDimitry Andric   // Relaxation to delete inter-basic block jumps created by basic block
22335ffd83dbSDimitry Andric   // sections. Run after in.symTab is finalized as optimizeBasicBlockJumps
22345ffd83dbSDimitry Andric   // can relax jump instructions based on symbol offset.
22355ffd83dbSDimitry Andric   if (config->optimizeBBJumps)
22365ffd83dbSDimitry Andric     optimizeBasicBlockJumps();
22375ffd83dbSDimitry Andric 
22380b57cec5SDimitry Andric   // Fill other section headers. The dynamic table is finalized
22390b57cec5SDimitry Andric   // at the end because some tags like RELSZ depend on result
22400b57cec5SDimitry Andric   // of finalizing other sections.
22410b57cec5SDimitry Andric   for (OutputSection *sec : outputSections)
22420b57cec5SDimitry Andric     sec->finalize();
22430b57cec5SDimitry Andric }
22440b57cec5SDimitry Andric 
22450b57cec5SDimitry Andric // Ensure data sections are not mixed with executable sections when
22460b57cec5SDimitry Andric // -execute-only is used. -execute-only is a feature to make pages executable
22470b57cec5SDimitry Andric // but not readable, and the feature is currently supported only on AArch64.
22480b57cec5SDimitry Andric template <class ELFT> void Writer<ELFT>::checkExecuteOnly() {
22490b57cec5SDimitry Andric   if (!config->executeOnly)
22500b57cec5SDimitry Andric     return;
22510b57cec5SDimitry Andric 
22520b57cec5SDimitry Andric   for (OutputSection *os : outputSections)
22530b57cec5SDimitry Andric     if (os->flags & SHF_EXECINSTR)
22540b57cec5SDimitry Andric       for (InputSection *isec : getInputSections(os))
22550b57cec5SDimitry Andric         if (!(isec->flags & SHF_EXECINSTR))
22560b57cec5SDimitry Andric           error("cannot place " + toString(isec) + " into " + toString(os->name) +
22570b57cec5SDimitry Andric                 ": -execute-only does not support intermingling data and code");
22580b57cec5SDimitry Andric }
22590b57cec5SDimitry Andric 
22600b57cec5SDimitry Andric // The linker is expected to define SECNAME_start and SECNAME_end
22610b57cec5SDimitry Andric // symbols for a few sections. This function defines them.
22620b57cec5SDimitry Andric template <class ELFT> void Writer<ELFT>::addStartEndSymbols() {
22630b57cec5SDimitry Andric   // If a section does not exist, there's ambiguity as to how we
22640b57cec5SDimitry Andric   // define _start and _end symbols for an init/fini section. Since
22650b57cec5SDimitry Andric   // the loader assume that the symbols are always defined, we need to
22660b57cec5SDimitry Andric   // always define them. But what value? The loader iterates over all
22670b57cec5SDimitry Andric   // pointers between _start and _end to run global ctors/dtors, so if
22680b57cec5SDimitry Andric   // the section is empty, their symbol values don't actually matter
22690b57cec5SDimitry Andric   // as long as _start and _end point to the same location.
22700b57cec5SDimitry Andric   //
22710b57cec5SDimitry Andric   // That said, we don't want to set the symbols to 0 (which is
22720b57cec5SDimitry Andric   // probably the simplest value) because that could cause some
22730b57cec5SDimitry Andric   // program to fail to link due to relocation overflow, if their
22740b57cec5SDimitry Andric   // program text is above 2 GiB. We use the address of the .text
22750b57cec5SDimitry Andric   // section instead to prevent that failure.
22760b57cec5SDimitry Andric   //
2277480093f4SDimitry Andric   // In rare situations, the .text section may not exist. If that's the
22780b57cec5SDimitry Andric   // case, use the image base address as a last resort.
22790b57cec5SDimitry Andric   OutputSection *Default = findSection(".text");
22800b57cec5SDimitry Andric   if (!Default)
22810b57cec5SDimitry Andric     Default = Out::elfHeader;
22820b57cec5SDimitry Andric 
22830b57cec5SDimitry Andric   auto define = [=](StringRef start, StringRef end, OutputSection *os) {
22840b57cec5SDimitry Andric     if (os) {
22850b57cec5SDimitry Andric       addOptionalRegular(start, os, 0);
22860b57cec5SDimitry Andric       addOptionalRegular(end, os, -1);
22870b57cec5SDimitry Andric     } else {
22880b57cec5SDimitry Andric       addOptionalRegular(start, Default, 0);
22890b57cec5SDimitry Andric       addOptionalRegular(end, Default, 0);
22900b57cec5SDimitry Andric     }
22910b57cec5SDimitry Andric   };
22920b57cec5SDimitry Andric 
22930b57cec5SDimitry Andric   define("__preinit_array_start", "__preinit_array_end", Out::preinitArray);
22940b57cec5SDimitry Andric   define("__init_array_start", "__init_array_end", Out::initArray);
22950b57cec5SDimitry Andric   define("__fini_array_start", "__fini_array_end", Out::finiArray);
22960b57cec5SDimitry Andric 
22970b57cec5SDimitry Andric   if (OutputSection *sec = findSection(".ARM.exidx"))
22980b57cec5SDimitry Andric     define("__exidx_start", "__exidx_end", sec);
22990b57cec5SDimitry Andric }
23000b57cec5SDimitry Andric 
23010b57cec5SDimitry Andric // If a section name is valid as a C identifier (which is rare because of
23020b57cec5SDimitry Andric // the leading '.'), linkers are expected to define __start_<secname> and
23030b57cec5SDimitry Andric // __stop_<secname> symbols. They are at beginning and end of the section,
23040b57cec5SDimitry Andric // respectively. This is not requested by the ELF standard, but GNU ld and
23050b57cec5SDimitry Andric // gold provide the feature, and used by many programs.
23060b57cec5SDimitry Andric template <class ELFT>
23070b57cec5SDimitry Andric void Writer<ELFT>::addStartStopSymbols(OutputSection *sec) {
23080b57cec5SDimitry Andric   StringRef s = sec->name;
23090b57cec5SDimitry Andric   if (!isValidCIdentifier(s))
23100b57cec5SDimitry Andric     return;
23115ffd83dbSDimitry Andric   addOptionalRegular(saver.save("__start_" + s), sec, 0,
23125ffd83dbSDimitry Andric                      config->zStartStopVisibility);
23135ffd83dbSDimitry Andric   addOptionalRegular(saver.save("__stop_" + s), sec, -1,
23145ffd83dbSDimitry Andric                      config->zStartStopVisibility);
23150b57cec5SDimitry Andric }
23160b57cec5SDimitry Andric 
23170b57cec5SDimitry Andric static bool needsPtLoad(OutputSection *sec) {
2318*fe6060f1SDimitry Andric   if (!(sec->flags & SHF_ALLOC))
23190b57cec5SDimitry Andric     return false;
23200b57cec5SDimitry Andric 
23210b57cec5SDimitry Andric   // Don't allocate VA space for TLS NOBITS sections. The PT_TLS PHDR is
23220b57cec5SDimitry Andric   // responsible for allocating space for them, not the PT_LOAD that
23230b57cec5SDimitry Andric   // contains the TLS initialization image.
23240b57cec5SDimitry Andric   if ((sec->flags & SHF_TLS) && sec->type == SHT_NOBITS)
23250b57cec5SDimitry Andric     return false;
23260b57cec5SDimitry Andric   return true;
23270b57cec5SDimitry Andric }
23280b57cec5SDimitry Andric 
23290b57cec5SDimitry Andric // Linker scripts are responsible for aligning addresses. Unfortunately, most
23300b57cec5SDimitry Andric // linker scripts are designed for creating two PT_LOADs only, one RX and one
23310b57cec5SDimitry Andric // RW. This means that there is no alignment in the RO to RX transition and we
23320b57cec5SDimitry Andric // cannot create a PT_LOAD there.
23330b57cec5SDimitry Andric static uint64_t computeFlags(uint64_t flags) {
23340b57cec5SDimitry Andric   if (config->omagic)
23350b57cec5SDimitry Andric     return PF_R | PF_W | PF_X;
23360b57cec5SDimitry Andric   if (config->executeOnly && (flags & PF_X))
23370b57cec5SDimitry Andric     return flags & ~PF_R;
23380b57cec5SDimitry Andric   if (config->singleRoRx && !(flags & PF_W))
23390b57cec5SDimitry Andric     return flags | PF_X;
23400b57cec5SDimitry Andric   return flags;
23410b57cec5SDimitry Andric }
23420b57cec5SDimitry Andric 
23430b57cec5SDimitry Andric // Decide which program headers to create and which sections to include in each
23440b57cec5SDimitry Andric // one.
23450b57cec5SDimitry Andric template <class ELFT>
23460b57cec5SDimitry Andric std::vector<PhdrEntry *> Writer<ELFT>::createPhdrs(Partition &part) {
23470b57cec5SDimitry Andric   std::vector<PhdrEntry *> ret;
23480b57cec5SDimitry Andric   auto addHdr = [&](unsigned type, unsigned flags) -> PhdrEntry * {
23490b57cec5SDimitry Andric     ret.push_back(make<PhdrEntry>(type, flags));
23500b57cec5SDimitry Andric     return ret.back();
23510b57cec5SDimitry Andric   };
23520b57cec5SDimitry Andric 
23530b57cec5SDimitry Andric   unsigned partNo = part.getNumber();
23540b57cec5SDimitry Andric   bool isMain = partNo == 1;
23550b57cec5SDimitry Andric 
235685868e8aSDimitry Andric   // Add the first PT_LOAD segment for regular output sections.
235785868e8aSDimitry Andric   uint64_t flags = computeFlags(PF_R);
235885868e8aSDimitry Andric   PhdrEntry *load = nullptr;
235985868e8aSDimitry Andric 
236085868e8aSDimitry Andric   // nmagic or omagic output does not have PT_PHDR, PT_INTERP, or the readonly
236185868e8aSDimitry Andric   // PT_LOAD.
236285868e8aSDimitry Andric   if (!config->nmagic && !config->omagic) {
236385868e8aSDimitry Andric     // The first phdr entry is PT_PHDR which describes the program header
236485868e8aSDimitry Andric     // itself.
23650b57cec5SDimitry Andric     if (isMain)
23660b57cec5SDimitry Andric       addHdr(PT_PHDR, PF_R)->add(Out::programHeaders);
23670b57cec5SDimitry Andric     else
23680b57cec5SDimitry Andric       addHdr(PT_PHDR, PF_R)->add(part.programHeaders->getParent());
23690b57cec5SDimitry Andric 
23700b57cec5SDimitry Andric     // PT_INTERP must be the second entry if exists.
23710b57cec5SDimitry Andric     if (OutputSection *cmd = findSection(".interp", partNo))
23720b57cec5SDimitry Andric       addHdr(PT_INTERP, cmd->getPhdrFlags())->add(cmd);
23730b57cec5SDimitry Andric 
23740b57cec5SDimitry Andric     // Add the headers. We will remove them if they don't fit.
23750b57cec5SDimitry Andric     // In the other partitions the headers are ordinary sections, so they don't
23760b57cec5SDimitry Andric     // need to be added here.
23770b57cec5SDimitry Andric     if (isMain) {
23780b57cec5SDimitry Andric       load = addHdr(PT_LOAD, flags);
23790b57cec5SDimitry Andric       load->add(Out::elfHeader);
23800b57cec5SDimitry Andric       load->add(Out::programHeaders);
23810b57cec5SDimitry Andric     }
238285868e8aSDimitry Andric   }
23830b57cec5SDimitry Andric 
23840b57cec5SDimitry Andric   // PT_GNU_RELRO includes all sections that should be marked as
2385480093f4SDimitry Andric   // read-only by dynamic linker after processing relocations.
23860b57cec5SDimitry Andric   // Current dynamic loaders only support one PT_GNU_RELRO PHDR, give
23870b57cec5SDimitry Andric   // an error message if more than one PT_GNU_RELRO PHDR is required.
23880b57cec5SDimitry Andric   PhdrEntry *relRo = make<PhdrEntry>(PT_GNU_RELRO, PF_R);
23890b57cec5SDimitry Andric   bool inRelroPhdr = false;
23900b57cec5SDimitry Andric   OutputSection *relroEnd = nullptr;
23910b57cec5SDimitry Andric   for (OutputSection *sec : outputSections) {
23920b57cec5SDimitry Andric     if (sec->partition != partNo || !needsPtLoad(sec))
23930b57cec5SDimitry Andric       continue;
23940b57cec5SDimitry Andric     if (isRelroSection(sec)) {
23950b57cec5SDimitry Andric       inRelroPhdr = true;
23960b57cec5SDimitry Andric       if (!relroEnd)
23970b57cec5SDimitry Andric         relRo->add(sec);
23980b57cec5SDimitry Andric       else
23990b57cec5SDimitry Andric         error("section: " + sec->name + " is not contiguous with other relro" +
24000b57cec5SDimitry Andric               " sections");
24010b57cec5SDimitry Andric     } else if (inRelroPhdr) {
24020b57cec5SDimitry Andric       inRelroPhdr = false;
24030b57cec5SDimitry Andric       relroEnd = sec;
24040b57cec5SDimitry Andric     }
24050b57cec5SDimitry Andric   }
24060b57cec5SDimitry Andric 
24070b57cec5SDimitry Andric   for (OutputSection *sec : outputSections) {
24080b57cec5SDimitry Andric     if (!needsPtLoad(sec))
24090b57cec5SDimitry Andric       continue;
24100b57cec5SDimitry Andric 
24110b57cec5SDimitry Andric     // Normally, sections in partitions other than the current partition are
24120b57cec5SDimitry Andric     // ignored. But partition number 255 is a special case: it contains the
24130b57cec5SDimitry Andric     // partition end marker (.part.end). It needs to be added to the main
24140b57cec5SDimitry Andric     // partition so that a segment is created for it in the main partition,
24150b57cec5SDimitry Andric     // which will cause the dynamic loader to reserve space for the other
24160b57cec5SDimitry Andric     // partitions.
24170b57cec5SDimitry Andric     if (sec->partition != partNo) {
24180b57cec5SDimitry Andric       if (isMain && sec->partition == 255)
24190b57cec5SDimitry Andric         addHdr(PT_LOAD, computeFlags(sec->getPhdrFlags()))->add(sec);
24200b57cec5SDimitry Andric       continue;
24210b57cec5SDimitry Andric     }
24220b57cec5SDimitry Andric 
24230b57cec5SDimitry Andric     // Segments are contiguous memory regions that has the same attributes
24240b57cec5SDimitry Andric     // (e.g. executable or writable). There is one phdr for each segment.
24250b57cec5SDimitry Andric     // Therefore, we need to create a new phdr when the next section has
24260b57cec5SDimitry Andric     // different flags or is loaded at a discontiguous address or memory
24270b57cec5SDimitry Andric     // region using AT or AT> linker script command, respectively. At the same
24280b57cec5SDimitry Andric     // time, we don't want to create a separate load segment for the headers,
24290b57cec5SDimitry Andric     // even if the first output section has an AT or AT> attribute.
24300b57cec5SDimitry Andric     uint64_t newFlags = computeFlags(sec->getPhdrFlags());
24315ffd83dbSDimitry Andric     bool sameLMARegion =
24325ffd83dbSDimitry Andric         load && !sec->lmaExpr && sec->lmaRegion == load->firstSec->lmaRegion;
24335ffd83dbSDimitry Andric     if (!(load && newFlags == flags && sec != relroEnd &&
24345ffd83dbSDimitry Andric           sec->memRegion == load->firstSec->memRegion &&
24355ffd83dbSDimitry Andric           (sameLMARegion || load->lastSec == Out::programHeaders))) {
24360b57cec5SDimitry Andric       load = addHdr(PT_LOAD, newFlags);
24370b57cec5SDimitry Andric       flags = newFlags;
24380b57cec5SDimitry Andric     }
24390b57cec5SDimitry Andric 
24400b57cec5SDimitry Andric     load->add(sec);
24410b57cec5SDimitry Andric   }
24420b57cec5SDimitry Andric 
24430b57cec5SDimitry Andric   // Add a TLS segment if any.
24440b57cec5SDimitry Andric   PhdrEntry *tlsHdr = make<PhdrEntry>(PT_TLS, PF_R);
24450b57cec5SDimitry Andric   for (OutputSection *sec : outputSections)
24460b57cec5SDimitry Andric     if (sec->partition == partNo && sec->flags & SHF_TLS)
24470b57cec5SDimitry Andric       tlsHdr->add(sec);
24480b57cec5SDimitry Andric   if (tlsHdr->firstSec)
24490b57cec5SDimitry Andric     ret.push_back(tlsHdr);
24500b57cec5SDimitry Andric 
24510b57cec5SDimitry Andric   // Add an entry for .dynamic.
24520b57cec5SDimitry Andric   if (OutputSection *sec = part.dynamic->getParent())
24530b57cec5SDimitry Andric     addHdr(PT_DYNAMIC, sec->getPhdrFlags())->add(sec);
24540b57cec5SDimitry Andric 
24550b57cec5SDimitry Andric   if (relRo->firstSec)
24560b57cec5SDimitry Andric     ret.push_back(relRo);
24570b57cec5SDimitry Andric 
24580b57cec5SDimitry Andric   // PT_GNU_EH_FRAME is a special section pointing on .eh_frame_hdr.
24590b57cec5SDimitry Andric   if (part.ehFrame->isNeeded() && part.ehFrameHdr &&
24600b57cec5SDimitry Andric       part.ehFrame->getParent() && part.ehFrameHdr->getParent())
24610b57cec5SDimitry Andric     addHdr(PT_GNU_EH_FRAME, part.ehFrameHdr->getParent()->getPhdrFlags())
24620b57cec5SDimitry Andric         ->add(part.ehFrameHdr->getParent());
24630b57cec5SDimitry Andric 
24640b57cec5SDimitry Andric   // PT_OPENBSD_RANDOMIZE is an OpenBSD-specific feature. That makes
24650b57cec5SDimitry Andric   // the dynamic linker fill the segment with random data.
24660b57cec5SDimitry Andric   if (OutputSection *cmd = findSection(".openbsd.randomdata", partNo))
24670b57cec5SDimitry Andric     addHdr(PT_OPENBSD_RANDOMIZE, cmd->getPhdrFlags())->add(cmd);
24680b57cec5SDimitry Andric 
2469480093f4SDimitry Andric   if (config->zGnustack != GnuStackKind::None) {
24700b57cec5SDimitry Andric     // PT_GNU_STACK is a special section to tell the loader to make the
24710b57cec5SDimitry Andric     // pages for the stack non-executable. If you really want an executable
24720b57cec5SDimitry Andric     // stack, you can pass -z execstack, but that's not recommended for
24730b57cec5SDimitry Andric     // security reasons.
24740b57cec5SDimitry Andric     unsigned perm = PF_R | PF_W;
2475480093f4SDimitry Andric     if (config->zGnustack == GnuStackKind::Exec)
24760b57cec5SDimitry Andric       perm |= PF_X;
24770b57cec5SDimitry Andric     addHdr(PT_GNU_STACK, perm)->p_memsz = config->zStackSize;
2478480093f4SDimitry Andric   }
24790b57cec5SDimitry Andric 
24800b57cec5SDimitry Andric   // PT_OPENBSD_WXNEEDED is a OpenBSD-specific header to mark the executable
24810b57cec5SDimitry Andric   // is expected to perform W^X violations, such as calling mprotect(2) or
24820b57cec5SDimitry Andric   // mmap(2) with PROT_WRITE | PROT_EXEC, which is prohibited by default on
24830b57cec5SDimitry Andric   // OpenBSD.
24840b57cec5SDimitry Andric   if (config->zWxneeded)
24850b57cec5SDimitry Andric     addHdr(PT_OPENBSD_WXNEEDED, PF_X);
24860b57cec5SDimitry Andric 
2487480093f4SDimitry Andric   if (OutputSection *cmd = findSection(".note.gnu.property", partNo))
2488480093f4SDimitry Andric     addHdr(PT_GNU_PROPERTY, PF_R)->add(cmd);
2489480093f4SDimitry Andric 
24900b57cec5SDimitry Andric   // Create one PT_NOTE per a group of contiguous SHT_NOTE sections with the
24910b57cec5SDimitry Andric   // same alignment.
24920b57cec5SDimitry Andric   PhdrEntry *note = nullptr;
24930b57cec5SDimitry Andric   for (OutputSection *sec : outputSections) {
24940b57cec5SDimitry Andric     if (sec->partition != partNo)
24950b57cec5SDimitry Andric       continue;
24960b57cec5SDimitry Andric     if (sec->type == SHT_NOTE && (sec->flags & SHF_ALLOC)) {
24970b57cec5SDimitry Andric       if (!note || sec->lmaExpr || note->lastSec->alignment != sec->alignment)
24980b57cec5SDimitry Andric         note = addHdr(PT_NOTE, PF_R);
24990b57cec5SDimitry Andric       note->add(sec);
25000b57cec5SDimitry Andric     } else {
25010b57cec5SDimitry Andric       note = nullptr;
25020b57cec5SDimitry Andric     }
25030b57cec5SDimitry Andric   }
25040b57cec5SDimitry Andric   return ret;
25050b57cec5SDimitry Andric }
25060b57cec5SDimitry Andric 
25070b57cec5SDimitry Andric template <class ELFT>
25080b57cec5SDimitry Andric void Writer<ELFT>::addPhdrForSection(Partition &part, unsigned shType,
25090b57cec5SDimitry Andric                                      unsigned pType, unsigned pFlags) {
25100b57cec5SDimitry Andric   unsigned partNo = part.getNumber();
25110b57cec5SDimitry Andric   auto i = llvm::find_if(outputSections, [=](OutputSection *cmd) {
25120b57cec5SDimitry Andric     return cmd->partition == partNo && cmd->type == shType;
25130b57cec5SDimitry Andric   });
25140b57cec5SDimitry Andric   if (i == outputSections.end())
25150b57cec5SDimitry Andric     return;
25160b57cec5SDimitry Andric 
25170b57cec5SDimitry Andric   PhdrEntry *entry = make<PhdrEntry>(pType, pFlags);
25180b57cec5SDimitry Andric   entry->add(*i);
25190b57cec5SDimitry Andric   part.phdrs.push_back(entry);
25200b57cec5SDimitry Andric }
25210b57cec5SDimitry Andric 
252285868e8aSDimitry Andric // Place the first section of each PT_LOAD to a different page (of maxPageSize).
252385868e8aSDimitry Andric // This is achieved by assigning an alignment expression to addrExpr of each
252485868e8aSDimitry Andric // such section.
25250b57cec5SDimitry Andric template <class ELFT> void Writer<ELFT>::fixSectionAlignments() {
252685868e8aSDimitry Andric   const PhdrEntry *prev;
252785868e8aSDimitry Andric   auto pageAlign = [&](const PhdrEntry *p) {
252885868e8aSDimitry Andric     OutputSection *cmd = p->firstSec;
25295ffd83dbSDimitry Andric     if (!cmd)
25305ffd83dbSDimitry Andric       return;
25315ffd83dbSDimitry Andric     cmd->alignExpr = [align = cmd->alignment]() { return align; };
25325ffd83dbSDimitry Andric     if (!cmd->addrExpr) {
253385868e8aSDimitry Andric       // Prefer advancing to align(dot, maxPageSize) + dot%maxPageSize to avoid
253485868e8aSDimitry Andric       // padding in the file contents.
253585868e8aSDimitry Andric       //
253685868e8aSDimitry Andric       // When -z separate-code is used we must not have any overlap in pages
253785868e8aSDimitry Andric       // between an executable segment and a non-executable segment. We align to
253885868e8aSDimitry Andric       // the next maximum page size boundary on transitions between executable
253985868e8aSDimitry Andric       // and non-executable segments.
254085868e8aSDimitry Andric       //
254185868e8aSDimitry Andric       // SHT_LLVM_PART_EHDR marks the start of a partition. The partition
254285868e8aSDimitry Andric       // sections will be extracted to a separate file. Align to the next
254385868e8aSDimitry Andric       // maximum page size boundary so that we can find the ELF header at the
254485868e8aSDimitry Andric       // start. We cannot benefit from overlapping p_offset ranges with the
254585868e8aSDimitry Andric       // previous segment anyway.
254685868e8aSDimitry Andric       if (config->zSeparate == SeparateSegmentKind::Loadable ||
254785868e8aSDimitry Andric           (config->zSeparate == SeparateSegmentKind::Code && prev &&
254885868e8aSDimitry Andric            (prev->p_flags & PF_X) != (p->p_flags & PF_X)) ||
254985868e8aSDimitry Andric           cmd->type == SHT_LLVM_PART_EHDR)
255085868e8aSDimitry Andric         cmd->addrExpr = [] {
25510b57cec5SDimitry Andric           return alignTo(script->getDot(), config->maxPageSize);
25520b57cec5SDimitry Andric         };
255385868e8aSDimitry Andric       // PT_TLS is at the start of the first RW PT_LOAD. If `p` includes PT_TLS,
255485868e8aSDimitry Andric       // it must be the RW. Align to p_align(PT_TLS) to make sure
255585868e8aSDimitry Andric       // p_vaddr(PT_LOAD)%p_align(PT_LOAD) = 0. Otherwise, if
255685868e8aSDimitry Andric       // sh_addralign(.tdata) < sh_addralign(.tbss), we will set p_align(PT_TLS)
255785868e8aSDimitry Andric       // to sh_addralign(.tbss), while p_vaddr(PT_TLS)=p_vaddr(PT_LOAD) may not
255885868e8aSDimitry Andric       // be congruent to 0 modulo p_align(PT_TLS).
255985868e8aSDimitry Andric       //
256085868e8aSDimitry Andric       // Technically this is not required, but as of 2019, some dynamic loaders
256185868e8aSDimitry Andric       // don't handle p_vaddr%p_align != 0 correctly, e.g. glibc (i386 and
256285868e8aSDimitry Andric       // x86-64) doesn't make runtime address congruent to p_vaddr modulo
256385868e8aSDimitry Andric       // p_align for dynamic TLS blocks (PR/24606), FreeBSD rtld has the same
256485868e8aSDimitry Andric       // bug, musl (TLS Variant 1 architectures) before 1.1.23 handled TLS
256585868e8aSDimitry Andric       // blocks correctly. We need to keep the workaround for a while.
256685868e8aSDimitry Andric       else if (Out::tlsPhdr && Out::tlsPhdr->firstSec == p->firstSec)
256785868e8aSDimitry Andric         cmd->addrExpr = [] {
256885868e8aSDimitry Andric           return alignTo(script->getDot(), config->maxPageSize) +
256985868e8aSDimitry Andric                  alignTo(script->getDot() % config->maxPageSize,
257085868e8aSDimitry Andric                          Out::tlsPhdr->p_align);
257185868e8aSDimitry Andric         };
257285868e8aSDimitry Andric       else
257385868e8aSDimitry Andric         cmd->addrExpr = [] {
257485868e8aSDimitry Andric           return alignTo(script->getDot(), config->maxPageSize) +
257585868e8aSDimitry Andric                  script->getDot() % config->maxPageSize;
257685868e8aSDimitry Andric         };
257785868e8aSDimitry Andric     }
25780b57cec5SDimitry Andric   };
25790b57cec5SDimitry Andric 
25800b57cec5SDimitry Andric   for (Partition &part : partitions) {
258185868e8aSDimitry Andric     prev = nullptr;
25820b57cec5SDimitry Andric     for (const PhdrEntry *p : part.phdrs)
258385868e8aSDimitry Andric       if (p->p_type == PT_LOAD && p->firstSec) {
258485868e8aSDimitry Andric         pageAlign(p);
258585868e8aSDimitry Andric         prev = p;
258685868e8aSDimitry Andric       }
25870b57cec5SDimitry Andric   }
25880b57cec5SDimitry Andric }
25890b57cec5SDimitry Andric 
25900b57cec5SDimitry Andric // Compute an in-file position for a given section. The file offset must be the
25910b57cec5SDimitry Andric // same with its virtual address modulo the page size, so that the loader can
25920b57cec5SDimitry Andric // load executables without any address adjustment.
25930b57cec5SDimitry Andric static uint64_t computeFileOffset(OutputSection *os, uint64_t off) {
25940b57cec5SDimitry Andric   // The first section in a PT_LOAD has to have congruent offset and address
259585868e8aSDimitry Andric   // modulo the maximum page size.
259685868e8aSDimitry Andric   if (os->ptLoad && os->ptLoad->firstSec == os)
259785868e8aSDimitry Andric     return alignTo(off, os->ptLoad->p_align, os->addr);
25980b57cec5SDimitry Andric 
25990b57cec5SDimitry Andric   // File offsets are not significant for .bss sections other than the first one
26000b57cec5SDimitry Andric   // in a PT_LOAD. By convention, we keep section offsets monotonically
26010b57cec5SDimitry Andric   // increasing rather than setting to zero.
26020b57cec5SDimitry Andric    if (os->type == SHT_NOBITS)
26030b57cec5SDimitry Andric      return off;
26040b57cec5SDimitry Andric 
26050b57cec5SDimitry Andric   // If the section is not in a PT_LOAD, we just have to align it.
26060b57cec5SDimitry Andric   if (!os->ptLoad)
26070b57cec5SDimitry Andric     return alignTo(off, os->alignment);
26080b57cec5SDimitry Andric 
26090b57cec5SDimitry Andric   // If two sections share the same PT_LOAD the file offset is calculated
26100b57cec5SDimitry Andric   // using this formula: Off2 = Off1 + (VA2 - VA1).
26110b57cec5SDimitry Andric   OutputSection *first = os->ptLoad->firstSec;
26120b57cec5SDimitry Andric   return first->offset + os->addr - first->addr;
26130b57cec5SDimitry Andric }
26140b57cec5SDimitry Andric 
26150b57cec5SDimitry Andric // Set an in-file position to a given section and returns the end position of
26160b57cec5SDimitry Andric // the section.
26170b57cec5SDimitry Andric static uint64_t setFileOffset(OutputSection *os, uint64_t off) {
26180b57cec5SDimitry Andric   off = computeFileOffset(os, off);
26190b57cec5SDimitry Andric   os->offset = off;
26200b57cec5SDimitry Andric 
26210b57cec5SDimitry Andric   if (os->type == SHT_NOBITS)
26220b57cec5SDimitry Andric     return off;
26230b57cec5SDimitry Andric   return off + os->size;
26240b57cec5SDimitry Andric }
26250b57cec5SDimitry Andric 
26260b57cec5SDimitry Andric template <class ELFT> void Writer<ELFT>::assignFileOffsetsBinary() {
2627e8d8bef9SDimitry Andric   // Compute the minimum LMA of all non-empty non-NOBITS sections as minAddr.
2628e8d8bef9SDimitry Andric   auto needsOffset = [](OutputSection &sec) {
2629e8d8bef9SDimitry Andric     return sec.type != SHT_NOBITS && (sec.flags & SHF_ALLOC) && sec.size > 0;
2630e8d8bef9SDimitry Andric   };
2631e8d8bef9SDimitry Andric   uint64_t minAddr = UINT64_MAX;
26320b57cec5SDimitry Andric   for (OutputSection *sec : outputSections)
2633e8d8bef9SDimitry Andric     if (needsOffset(*sec)) {
2634e8d8bef9SDimitry Andric       sec->offset = sec->getLMA();
2635e8d8bef9SDimitry Andric       minAddr = std::min(minAddr, sec->offset);
2636e8d8bef9SDimitry Andric     }
2637e8d8bef9SDimitry Andric 
2638e8d8bef9SDimitry Andric   // Sections are laid out at LMA minus minAddr.
2639e8d8bef9SDimitry Andric   fileSize = 0;
2640e8d8bef9SDimitry Andric   for (OutputSection *sec : outputSections)
2641e8d8bef9SDimitry Andric     if (needsOffset(*sec)) {
2642e8d8bef9SDimitry Andric       sec->offset -= minAddr;
2643e8d8bef9SDimitry Andric       fileSize = std::max(fileSize, sec->offset + sec->size);
2644e8d8bef9SDimitry Andric     }
26450b57cec5SDimitry Andric }
26460b57cec5SDimitry Andric 
26470b57cec5SDimitry Andric static std::string rangeToString(uint64_t addr, uint64_t len) {
26480b57cec5SDimitry Andric   return "[0x" + utohexstr(addr) + ", 0x" + utohexstr(addr + len - 1) + "]";
26490b57cec5SDimitry Andric }
26500b57cec5SDimitry Andric 
26510b57cec5SDimitry Andric // Assign file offsets to output sections.
26520b57cec5SDimitry Andric template <class ELFT> void Writer<ELFT>::assignFileOffsets() {
26530b57cec5SDimitry Andric   uint64_t off = 0;
26540b57cec5SDimitry Andric   off = setFileOffset(Out::elfHeader, off);
26550b57cec5SDimitry Andric   off = setFileOffset(Out::programHeaders, off);
26560b57cec5SDimitry Andric 
26570b57cec5SDimitry Andric   PhdrEntry *lastRX = nullptr;
26580b57cec5SDimitry Andric   for (Partition &part : partitions)
26590b57cec5SDimitry Andric     for (PhdrEntry *p : part.phdrs)
26600b57cec5SDimitry Andric       if (p->p_type == PT_LOAD && (p->p_flags & PF_X))
26610b57cec5SDimitry Andric         lastRX = p;
26620b57cec5SDimitry Andric 
2663e8d8bef9SDimitry Andric   // Layout SHF_ALLOC sections before non-SHF_ALLOC sections. A non-SHF_ALLOC
2664e8d8bef9SDimitry Andric   // will not occupy file offsets contained by a PT_LOAD.
26650b57cec5SDimitry Andric   for (OutputSection *sec : outputSections) {
2666e8d8bef9SDimitry Andric     if (!(sec->flags & SHF_ALLOC))
2667e8d8bef9SDimitry Andric       continue;
26680b57cec5SDimitry Andric     off = setFileOffset(sec, off);
26690b57cec5SDimitry Andric 
26700b57cec5SDimitry Andric     // If this is a last section of the last executable segment and that
26710b57cec5SDimitry Andric     // segment is the last loadable segment, align the offset of the
26720b57cec5SDimitry Andric     // following section to avoid loading non-segments parts of the file.
267385868e8aSDimitry Andric     if (config->zSeparate != SeparateSegmentKind::None && lastRX &&
267485868e8aSDimitry Andric         lastRX->lastSec == sec)
26750b57cec5SDimitry Andric       off = alignTo(off, config->commonPageSize);
26760b57cec5SDimitry Andric   }
2677e8d8bef9SDimitry Andric   for (OutputSection *sec : outputSections)
2678e8d8bef9SDimitry Andric     if (!(sec->flags & SHF_ALLOC))
2679e8d8bef9SDimitry Andric       off = setFileOffset(sec, off);
26800b57cec5SDimitry Andric 
26810b57cec5SDimitry Andric   sectionHeaderOff = alignTo(off, config->wordsize);
26820b57cec5SDimitry Andric   fileSize = sectionHeaderOff + (outputSections.size() + 1) * sizeof(Elf_Shdr);
26830b57cec5SDimitry Andric 
26840b57cec5SDimitry Andric   // Our logic assumes that sections have rising VA within the same segment.
26850b57cec5SDimitry Andric   // With use of linker scripts it is possible to violate this rule and get file
26860b57cec5SDimitry Andric   // offset overlaps or overflows. That should never happen with a valid script
26870b57cec5SDimitry Andric   // which does not move the location counter backwards and usually scripts do
26880b57cec5SDimitry Andric   // not do that. Unfortunately, there are apps in the wild, for example, Linux
26890b57cec5SDimitry Andric   // kernel, which control segment distribution explicitly and move the counter
26900b57cec5SDimitry Andric   // backwards, so we have to allow doing that to support linking them. We
26910b57cec5SDimitry Andric   // perform non-critical checks for overlaps in checkSectionOverlap(), but here
26920b57cec5SDimitry Andric   // we want to prevent file size overflows because it would crash the linker.
26930b57cec5SDimitry Andric   for (OutputSection *sec : outputSections) {
26940b57cec5SDimitry Andric     if (sec->type == SHT_NOBITS)
26950b57cec5SDimitry Andric       continue;
26960b57cec5SDimitry Andric     if ((sec->offset > fileSize) || (sec->offset + sec->size > fileSize))
26970b57cec5SDimitry Andric       error("unable to place section " + sec->name + " at file offset " +
26980b57cec5SDimitry Andric             rangeToString(sec->offset, sec->size) +
26990b57cec5SDimitry Andric             "; check your linker script for overflows");
27000b57cec5SDimitry Andric   }
27010b57cec5SDimitry Andric }
27020b57cec5SDimitry Andric 
27030b57cec5SDimitry Andric // Finalize the program headers. We call this function after we assign
27040b57cec5SDimitry Andric // file offsets and VAs to all sections.
27050b57cec5SDimitry Andric template <class ELFT> void Writer<ELFT>::setPhdrs(Partition &part) {
27060b57cec5SDimitry Andric   for (PhdrEntry *p : part.phdrs) {
27070b57cec5SDimitry Andric     OutputSection *first = p->firstSec;
27080b57cec5SDimitry Andric     OutputSection *last = p->lastSec;
27090b57cec5SDimitry Andric 
27100b57cec5SDimitry Andric     if (first) {
27110b57cec5SDimitry Andric       p->p_filesz = last->offset - first->offset;
27120b57cec5SDimitry Andric       if (last->type != SHT_NOBITS)
27130b57cec5SDimitry Andric         p->p_filesz += last->size;
27140b57cec5SDimitry Andric 
27150b57cec5SDimitry Andric       p->p_memsz = last->addr + last->size - first->addr;
27160b57cec5SDimitry Andric       p->p_offset = first->offset;
27170b57cec5SDimitry Andric       p->p_vaddr = first->addr;
27180b57cec5SDimitry Andric 
27190b57cec5SDimitry Andric       // File offsets in partitions other than the main partition are relative
27200b57cec5SDimitry Andric       // to the offset of the ELF headers. Perform that adjustment now.
27210b57cec5SDimitry Andric       if (part.elfHeader)
27220b57cec5SDimitry Andric         p->p_offset -= part.elfHeader->getParent()->offset;
27230b57cec5SDimitry Andric 
27240b57cec5SDimitry Andric       if (!p->hasLMA)
27250b57cec5SDimitry Andric         p->p_paddr = first->getLMA();
27260b57cec5SDimitry Andric     }
27270b57cec5SDimitry Andric 
272885868e8aSDimitry Andric     if (p->p_type == PT_GNU_RELRO) {
27290b57cec5SDimitry Andric       p->p_align = 1;
273085868e8aSDimitry Andric       // musl/glibc ld.so rounds the size down, so we need to round up
27310b57cec5SDimitry Andric       // to protect the last page. This is a no-op on FreeBSD which always
27320b57cec5SDimitry Andric       // rounds up.
273385868e8aSDimitry Andric       p->p_memsz = alignTo(p->p_offset + p->p_memsz, config->commonPageSize) -
273485868e8aSDimitry Andric                    p->p_offset;
27350b57cec5SDimitry Andric     }
27360b57cec5SDimitry Andric   }
27370b57cec5SDimitry Andric }
27380b57cec5SDimitry Andric 
27390b57cec5SDimitry Andric // A helper struct for checkSectionOverlap.
27400b57cec5SDimitry Andric namespace {
27410b57cec5SDimitry Andric struct SectionOffset {
27420b57cec5SDimitry Andric   OutputSection *sec;
27430b57cec5SDimitry Andric   uint64_t offset;
27440b57cec5SDimitry Andric };
27450b57cec5SDimitry Andric } // namespace
27460b57cec5SDimitry Andric 
27470b57cec5SDimitry Andric // Check whether sections overlap for a specific address range (file offsets,
2748480093f4SDimitry Andric // load and virtual addresses).
27490b57cec5SDimitry Andric static void checkOverlap(StringRef name, std::vector<SectionOffset> &sections,
27500b57cec5SDimitry Andric                          bool isVirtualAddr) {
27510b57cec5SDimitry Andric   llvm::sort(sections, [=](const SectionOffset &a, const SectionOffset &b) {
27520b57cec5SDimitry Andric     return a.offset < b.offset;
27530b57cec5SDimitry Andric   });
27540b57cec5SDimitry Andric 
27550b57cec5SDimitry Andric   // Finding overlap is easy given a vector is sorted by start position.
27560b57cec5SDimitry Andric   // If an element starts before the end of the previous element, they overlap.
27570b57cec5SDimitry Andric   for (size_t i = 1, end = sections.size(); i < end; ++i) {
27580b57cec5SDimitry Andric     SectionOffset a = sections[i - 1];
27590b57cec5SDimitry Andric     SectionOffset b = sections[i];
27600b57cec5SDimitry Andric     if (b.offset >= a.offset + a.sec->size)
27610b57cec5SDimitry Andric       continue;
27620b57cec5SDimitry Andric 
27630b57cec5SDimitry Andric     // If both sections are in OVERLAY we allow the overlapping of virtual
27640b57cec5SDimitry Andric     // addresses, because it is what OVERLAY was designed for.
27650b57cec5SDimitry Andric     if (isVirtualAddr && a.sec->inOverlay && b.sec->inOverlay)
27660b57cec5SDimitry Andric       continue;
27670b57cec5SDimitry Andric 
27680b57cec5SDimitry Andric     errorOrWarn("section " + a.sec->name + " " + name +
27690b57cec5SDimitry Andric                 " range overlaps with " + b.sec->name + "\n>>> " + a.sec->name +
27700b57cec5SDimitry Andric                 " range is " + rangeToString(a.offset, a.sec->size) + "\n>>> " +
27710b57cec5SDimitry Andric                 b.sec->name + " range is " +
27720b57cec5SDimitry Andric                 rangeToString(b.offset, b.sec->size));
27730b57cec5SDimitry Andric   }
27740b57cec5SDimitry Andric }
27750b57cec5SDimitry Andric 
27760b57cec5SDimitry Andric // Check for overlapping sections and address overflows.
27770b57cec5SDimitry Andric //
27780b57cec5SDimitry Andric // In this function we check that none of the output sections have overlapping
27790b57cec5SDimitry Andric // file offsets. For SHF_ALLOC sections we also check that the load address
27800b57cec5SDimitry Andric // ranges and the virtual address ranges don't overlap
27810b57cec5SDimitry Andric template <class ELFT> void Writer<ELFT>::checkSections() {
27820b57cec5SDimitry Andric   // First, check that section's VAs fit in available address space for target.
27830b57cec5SDimitry Andric   for (OutputSection *os : outputSections)
27840b57cec5SDimitry Andric     if ((os->addr + os->size < os->addr) ||
27850b57cec5SDimitry Andric         (!ELFT::Is64Bits && os->addr + os->size > UINT32_MAX))
27860b57cec5SDimitry Andric       errorOrWarn("section " + os->name + " at 0x" + utohexstr(os->addr) +
27870b57cec5SDimitry Andric                   " of size 0x" + utohexstr(os->size) +
27880b57cec5SDimitry Andric                   " exceeds available address space");
27890b57cec5SDimitry Andric 
27900b57cec5SDimitry Andric   // Check for overlapping file offsets. In this case we need to skip any
27910b57cec5SDimitry Andric   // section marked as SHT_NOBITS. These sections don't actually occupy space in
27920b57cec5SDimitry Andric   // the file so Sec->Offset + Sec->Size can overlap with others. If --oformat
27930b57cec5SDimitry Andric   // binary is specified only add SHF_ALLOC sections are added to the output
27940b57cec5SDimitry Andric   // file so we skip any non-allocated sections in that case.
27950b57cec5SDimitry Andric   std::vector<SectionOffset> fileOffs;
27960b57cec5SDimitry Andric   for (OutputSection *sec : outputSections)
27970b57cec5SDimitry Andric     if (sec->size > 0 && sec->type != SHT_NOBITS &&
27980b57cec5SDimitry Andric         (!config->oFormatBinary || (sec->flags & SHF_ALLOC)))
27990b57cec5SDimitry Andric       fileOffs.push_back({sec, sec->offset});
28000b57cec5SDimitry Andric   checkOverlap("file", fileOffs, false);
28010b57cec5SDimitry Andric 
28020b57cec5SDimitry Andric   // When linking with -r there is no need to check for overlapping virtual/load
28030b57cec5SDimitry Andric   // addresses since those addresses will only be assigned when the final
28040b57cec5SDimitry Andric   // executable/shared object is created.
28050b57cec5SDimitry Andric   if (config->relocatable)
28060b57cec5SDimitry Andric     return;
28070b57cec5SDimitry Andric 
28080b57cec5SDimitry Andric   // Checking for overlapping virtual and load addresses only needs to take
28090b57cec5SDimitry Andric   // into account SHF_ALLOC sections since others will not be loaded.
28100b57cec5SDimitry Andric   // Furthermore, we also need to skip SHF_TLS sections since these will be
28110b57cec5SDimitry Andric   // mapped to other addresses at runtime and can therefore have overlapping
28120b57cec5SDimitry Andric   // ranges in the file.
28130b57cec5SDimitry Andric   std::vector<SectionOffset> vmas;
28140b57cec5SDimitry Andric   for (OutputSection *sec : outputSections)
28150b57cec5SDimitry Andric     if (sec->size > 0 && (sec->flags & SHF_ALLOC) && !(sec->flags & SHF_TLS))
28160b57cec5SDimitry Andric       vmas.push_back({sec, sec->addr});
28170b57cec5SDimitry Andric   checkOverlap("virtual address", vmas, true);
28180b57cec5SDimitry Andric 
28190b57cec5SDimitry Andric   // Finally, check that the load addresses don't overlap. This will usually be
28200b57cec5SDimitry Andric   // the same as the virtual addresses but can be different when using a linker
28210b57cec5SDimitry Andric   // script with AT().
28220b57cec5SDimitry Andric   std::vector<SectionOffset> lmas;
28230b57cec5SDimitry Andric   for (OutputSection *sec : outputSections)
28240b57cec5SDimitry Andric     if (sec->size > 0 && (sec->flags & SHF_ALLOC) && !(sec->flags & SHF_TLS))
28250b57cec5SDimitry Andric       lmas.push_back({sec, sec->getLMA()});
28260b57cec5SDimitry Andric   checkOverlap("load address", lmas, false);
28270b57cec5SDimitry Andric }
28280b57cec5SDimitry Andric 
28290b57cec5SDimitry Andric // The entry point address is chosen in the following ways.
28300b57cec5SDimitry Andric //
28310b57cec5SDimitry Andric // 1. the '-e' entry command-line option;
28320b57cec5SDimitry Andric // 2. the ENTRY(symbol) command in a linker control script;
28330b57cec5SDimitry Andric // 3. the value of the symbol _start, if present;
28340b57cec5SDimitry Andric // 4. the number represented by the entry symbol, if it is a number;
28350b57cec5SDimitry Andric // 5. the address of the first byte of the .text section, if present;
28360b57cec5SDimitry Andric // 6. the address 0.
28370b57cec5SDimitry Andric static uint64_t getEntryAddr() {
28380b57cec5SDimitry Andric   // Case 1, 2 or 3
28390b57cec5SDimitry Andric   if (Symbol *b = symtab->find(config->entry))
28400b57cec5SDimitry Andric     return b->getVA();
28410b57cec5SDimitry Andric 
28420b57cec5SDimitry Andric   // Case 4
28430b57cec5SDimitry Andric   uint64_t addr;
28440b57cec5SDimitry Andric   if (to_integer(config->entry, addr))
28450b57cec5SDimitry Andric     return addr;
28460b57cec5SDimitry Andric 
28470b57cec5SDimitry Andric   // Case 5
28480b57cec5SDimitry Andric   if (OutputSection *sec = findSection(".text")) {
28490b57cec5SDimitry Andric     if (config->warnMissingEntry)
28500b57cec5SDimitry Andric       warn("cannot find entry symbol " + config->entry + "; defaulting to 0x" +
28510b57cec5SDimitry Andric            utohexstr(sec->addr));
28520b57cec5SDimitry Andric     return sec->addr;
28530b57cec5SDimitry Andric   }
28540b57cec5SDimitry Andric 
28550b57cec5SDimitry Andric   // Case 6
28560b57cec5SDimitry Andric   if (config->warnMissingEntry)
28570b57cec5SDimitry Andric     warn("cannot find entry symbol " + config->entry +
28580b57cec5SDimitry Andric          "; not setting start address");
28590b57cec5SDimitry Andric   return 0;
28600b57cec5SDimitry Andric }
28610b57cec5SDimitry Andric 
28620b57cec5SDimitry Andric static uint16_t getELFType() {
28630b57cec5SDimitry Andric   if (config->isPic)
28640b57cec5SDimitry Andric     return ET_DYN;
28650b57cec5SDimitry Andric   if (config->relocatable)
28660b57cec5SDimitry Andric     return ET_REL;
28670b57cec5SDimitry Andric   return ET_EXEC;
28680b57cec5SDimitry Andric }
28690b57cec5SDimitry Andric 
28700b57cec5SDimitry Andric template <class ELFT> void Writer<ELFT>::writeHeader() {
28710b57cec5SDimitry Andric   writeEhdr<ELFT>(Out::bufferStart, *mainPart);
28720b57cec5SDimitry Andric   writePhdrs<ELFT>(Out::bufferStart + sizeof(Elf_Ehdr), *mainPart);
28730b57cec5SDimitry Andric 
28740b57cec5SDimitry Andric   auto *eHdr = reinterpret_cast<Elf_Ehdr *>(Out::bufferStart);
28750b57cec5SDimitry Andric   eHdr->e_type = getELFType();
28760b57cec5SDimitry Andric   eHdr->e_entry = getEntryAddr();
28770b57cec5SDimitry Andric   eHdr->e_shoff = sectionHeaderOff;
28780b57cec5SDimitry Andric 
28790b57cec5SDimitry Andric   // Write the section header table.
28800b57cec5SDimitry Andric   //
28810b57cec5SDimitry Andric   // The ELF header can only store numbers up to SHN_LORESERVE in the e_shnum
28820b57cec5SDimitry Andric   // and e_shstrndx fields. When the value of one of these fields exceeds
28830b57cec5SDimitry Andric   // SHN_LORESERVE ELF requires us to put sentinel values in the ELF header and
28840b57cec5SDimitry Andric   // use fields in the section header at index 0 to store
28850b57cec5SDimitry Andric   // the value. The sentinel values and fields are:
28860b57cec5SDimitry Andric   // e_shnum = 0, SHdrs[0].sh_size = number of sections.
28870b57cec5SDimitry Andric   // e_shstrndx = SHN_XINDEX, SHdrs[0].sh_link = .shstrtab section index.
28880b57cec5SDimitry Andric   auto *sHdrs = reinterpret_cast<Elf_Shdr *>(Out::bufferStart + eHdr->e_shoff);
28890b57cec5SDimitry Andric   size_t num = outputSections.size() + 1;
28900b57cec5SDimitry Andric   if (num >= SHN_LORESERVE)
28910b57cec5SDimitry Andric     sHdrs->sh_size = num;
28920b57cec5SDimitry Andric   else
28930b57cec5SDimitry Andric     eHdr->e_shnum = num;
28940b57cec5SDimitry Andric 
28950b57cec5SDimitry Andric   uint32_t strTabIndex = in.shStrTab->getParent()->sectionIndex;
28960b57cec5SDimitry Andric   if (strTabIndex >= SHN_LORESERVE) {
28970b57cec5SDimitry Andric     sHdrs->sh_link = strTabIndex;
28980b57cec5SDimitry Andric     eHdr->e_shstrndx = SHN_XINDEX;
28990b57cec5SDimitry Andric   } else {
29000b57cec5SDimitry Andric     eHdr->e_shstrndx = strTabIndex;
29010b57cec5SDimitry Andric   }
29020b57cec5SDimitry Andric 
29030b57cec5SDimitry Andric   for (OutputSection *sec : outputSections)
29040b57cec5SDimitry Andric     sec->writeHeaderTo<ELFT>(++sHdrs);
29050b57cec5SDimitry Andric }
29060b57cec5SDimitry Andric 
29070b57cec5SDimitry Andric // Open a result file.
29080b57cec5SDimitry Andric template <class ELFT> void Writer<ELFT>::openFile() {
29090b57cec5SDimitry Andric   uint64_t maxSize = config->is64 ? INT64_MAX : UINT32_MAX;
29100b57cec5SDimitry Andric   if (fileSize != size_t(fileSize) || maxSize < fileSize) {
2911e8d8bef9SDimitry Andric     std::string msg;
2912e8d8bef9SDimitry Andric     raw_string_ostream s(msg);
2913e8d8bef9SDimitry Andric     s << "output file too large: " << Twine(fileSize) << " bytes\n"
2914e8d8bef9SDimitry Andric       << "section sizes:\n";
2915e8d8bef9SDimitry Andric     for (OutputSection *os : outputSections)
2916e8d8bef9SDimitry Andric       s << os->name << ' ' << os->size << "\n";
2917e8d8bef9SDimitry Andric     error(s.str());
29180b57cec5SDimitry Andric     return;
29190b57cec5SDimitry Andric   }
29200b57cec5SDimitry Andric 
29210b57cec5SDimitry Andric   unlinkAsync(config->outputFile);
29220b57cec5SDimitry Andric   unsigned flags = 0;
29230b57cec5SDimitry Andric   if (!config->relocatable)
2924480093f4SDimitry Andric     flags |= FileOutputBuffer::F_executable;
2925480093f4SDimitry Andric   if (!config->mmapOutputFile)
2926480093f4SDimitry Andric     flags |= FileOutputBuffer::F_no_mmap;
29270b57cec5SDimitry Andric   Expected<std::unique_ptr<FileOutputBuffer>> bufferOrErr =
29280b57cec5SDimitry Andric       FileOutputBuffer::create(config->outputFile, fileSize, flags);
29290b57cec5SDimitry Andric 
29300b57cec5SDimitry Andric   if (!bufferOrErr) {
29310b57cec5SDimitry Andric     error("failed to open " + config->outputFile + ": " +
29320b57cec5SDimitry Andric           llvm::toString(bufferOrErr.takeError()));
29330b57cec5SDimitry Andric     return;
29340b57cec5SDimitry Andric   }
29350b57cec5SDimitry Andric   buffer = std::move(*bufferOrErr);
29360b57cec5SDimitry Andric   Out::bufferStart = buffer->getBufferStart();
29370b57cec5SDimitry Andric }
29380b57cec5SDimitry Andric 
29390b57cec5SDimitry Andric template <class ELFT> void Writer<ELFT>::writeSectionsBinary() {
29400b57cec5SDimitry Andric   for (OutputSection *sec : outputSections)
29410b57cec5SDimitry Andric     if (sec->flags & SHF_ALLOC)
29420b57cec5SDimitry Andric       sec->writeTo<ELFT>(Out::bufferStart + sec->offset);
29430b57cec5SDimitry Andric }
29440b57cec5SDimitry Andric 
29450b57cec5SDimitry Andric static void fillTrap(uint8_t *i, uint8_t *end) {
29460b57cec5SDimitry Andric   for (; i + 4 <= end; i += 4)
29470b57cec5SDimitry Andric     memcpy(i, &target->trapInstr, 4);
29480b57cec5SDimitry Andric }
29490b57cec5SDimitry Andric 
29500b57cec5SDimitry Andric // Fill the last page of executable segments with trap instructions
29510b57cec5SDimitry Andric // instead of leaving them as zero. Even though it is not required by any
29520b57cec5SDimitry Andric // standard, it is in general a good thing to do for security reasons.
29530b57cec5SDimitry Andric //
29540b57cec5SDimitry Andric // We'll leave other pages in segments as-is because the rest will be
29550b57cec5SDimitry Andric // overwritten by output sections.
29560b57cec5SDimitry Andric template <class ELFT> void Writer<ELFT>::writeTrapInstr() {
29570b57cec5SDimitry Andric   for (Partition &part : partitions) {
29580b57cec5SDimitry Andric     // Fill the last page.
29590b57cec5SDimitry Andric     for (PhdrEntry *p : part.phdrs)
29600b57cec5SDimitry Andric       if (p->p_type == PT_LOAD && (p->p_flags & PF_X))
29610b57cec5SDimitry Andric         fillTrap(Out::bufferStart + alignDown(p->firstSec->offset + p->p_filesz,
29620b57cec5SDimitry Andric                                               config->commonPageSize),
29630b57cec5SDimitry Andric                  Out::bufferStart + alignTo(p->firstSec->offset + p->p_filesz,
29640b57cec5SDimitry Andric                                             config->commonPageSize));
29650b57cec5SDimitry Andric 
29660b57cec5SDimitry Andric     // Round up the file size of the last segment to the page boundary iff it is
29670b57cec5SDimitry Andric     // an executable segment to ensure that other tools don't accidentally
29680b57cec5SDimitry Andric     // trim the instruction padding (e.g. when stripping the file).
29690b57cec5SDimitry Andric     PhdrEntry *last = nullptr;
29700b57cec5SDimitry Andric     for (PhdrEntry *p : part.phdrs)
29710b57cec5SDimitry Andric       if (p->p_type == PT_LOAD)
29720b57cec5SDimitry Andric         last = p;
29730b57cec5SDimitry Andric 
29740b57cec5SDimitry Andric     if (last && (last->p_flags & PF_X))
29750b57cec5SDimitry Andric       last->p_memsz = last->p_filesz =
29760b57cec5SDimitry Andric           alignTo(last->p_filesz, config->commonPageSize);
29770b57cec5SDimitry Andric   }
29780b57cec5SDimitry Andric }
29790b57cec5SDimitry Andric 
29800b57cec5SDimitry Andric // Write section contents to a mmap'ed file.
29810b57cec5SDimitry Andric template <class ELFT> void Writer<ELFT>::writeSections() {
29820b57cec5SDimitry Andric   // In -r or -emit-relocs mode, write the relocation sections first as in
29830b57cec5SDimitry Andric   // ELf_Rel targets we might find out that we need to modify the relocated
29840b57cec5SDimitry Andric   // section while doing it.
29850b57cec5SDimitry Andric   for (OutputSection *sec : outputSections)
29860b57cec5SDimitry Andric     if (sec->type == SHT_REL || sec->type == SHT_RELA)
29870b57cec5SDimitry Andric       sec->writeTo<ELFT>(Out::bufferStart + sec->offset);
29880b57cec5SDimitry Andric 
29890b57cec5SDimitry Andric   for (OutputSection *sec : outputSections)
29900b57cec5SDimitry Andric     if (sec->type != SHT_REL && sec->type != SHT_RELA)
29910b57cec5SDimitry Andric       sec->writeTo<ELFT>(Out::bufferStart + sec->offset);
29920b57cec5SDimitry Andric 
2993*fe6060f1SDimitry Andric   // Finally, check that all dynamic relocation addends were written correctly.
2994*fe6060f1SDimitry Andric   if (config->checkDynamicRelocs && config->writeAddends) {
2995*fe6060f1SDimitry Andric     for (OutputSection *sec : outputSections)
2996*fe6060f1SDimitry Andric       if (sec->type == SHT_REL || sec->type == SHT_RELA)
2997*fe6060f1SDimitry Andric         sec->checkDynRelAddends(Out::bufferStart);
29980b57cec5SDimitry Andric   }
29990b57cec5SDimitry Andric }
30000b57cec5SDimitry Andric 
30010b57cec5SDimitry Andric // Computes a hash value of Data using a given hash function.
30020b57cec5SDimitry Andric // In order to utilize multiple cores, we first split data into 1MB
30030b57cec5SDimitry Andric // chunks, compute a hash for each chunk, and then compute a hash value
30040b57cec5SDimitry Andric // of the hash values.
30050b57cec5SDimitry Andric static void
30060b57cec5SDimitry Andric computeHash(llvm::MutableArrayRef<uint8_t> hashBuf,
30070b57cec5SDimitry Andric             llvm::ArrayRef<uint8_t> data,
30080b57cec5SDimitry Andric             std::function<void(uint8_t *dest, ArrayRef<uint8_t> arr)> hashFn) {
30090b57cec5SDimitry Andric   std::vector<ArrayRef<uint8_t>> chunks = split(data, 1024 * 1024);
30100b57cec5SDimitry Andric   std::vector<uint8_t> hashes(chunks.size() * hashBuf.size());
30110b57cec5SDimitry Andric 
30120b57cec5SDimitry Andric   // Compute hash values.
30130b57cec5SDimitry Andric   parallelForEachN(0, chunks.size(), [&](size_t i) {
30140b57cec5SDimitry Andric     hashFn(hashes.data() + i * hashBuf.size(), chunks[i]);
30150b57cec5SDimitry Andric   });
30160b57cec5SDimitry Andric 
30170b57cec5SDimitry Andric   // Write to the final output buffer.
30180b57cec5SDimitry Andric   hashFn(hashBuf.data(), hashes);
30190b57cec5SDimitry Andric }
30200b57cec5SDimitry Andric 
30210b57cec5SDimitry Andric template <class ELFT> void Writer<ELFT>::writeBuildId() {
30220b57cec5SDimitry Andric   if (!mainPart->buildId || !mainPart->buildId->getParent())
30230b57cec5SDimitry Andric     return;
30240b57cec5SDimitry Andric 
30250b57cec5SDimitry Andric   if (config->buildId == BuildIdKind::Hexstring) {
30260b57cec5SDimitry Andric     for (Partition &part : partitions)
30270b57cec5SDimitry Andric       part.buildId->writeBuildId(config->buildIdVector);
30280b57cec5SDimitry Andric     return;
30290b57cec5SDimitry Andric   }
30300b57cec5SDimitry Andric 
30310b57cec5SDimitry Andric   // Compute a hash of all sections of the output file.
30320b57cec5SDimitry Andric   size_t hashSize = mainPart->buildId->hashSize;
30330b57cec5SDimitry Andric   std::vector<uint8_t> buildId(hashSize);
30340b57cec5SDimitry Andric   llvm::ArrayRef<uint8_t> buf{Out::bufferStart, size_t(fileSize)};
30350b57cec5SDimitry Andric 
30360b57cec5SDimitry Andric   switch (config->buildId) {
30370b57cec5SDimitry Andric   case BuildIdKind::Fast:
30380b57cec5SDimitry Andric     computeHash(buildId, buf, [](uint8_t *dest, ArrayRef<uint8_t> arr) {
30390b57cec5SDimitry Andric       write64le(dest, xxHash64(arr));
30400b57cec5SDimitry Andric     });
30410b57cec5SDimitry Andric     break;
30420b57cec5SDimitry Andric   case BuildIdKind::Md5:
30430b57cec5SDimitry Andric     computeHash(buildId, buf, [&](uint8_t *dest, ArrayRef<uint8_t> arr) {
30440b57cec5SDimitry Andric       memcpy(dest, MD5::hash(arr).data(), hashSize);
30450b57cec5SDimitry Andric     });
30460b57cec5SDimitry Andric     break;
30470b57cec5SDimitry Andric   case BuildIdKind::Sha1:
30480b57cec5SDimitry Andric     computeHash(buildId, buf, [&](uint8_t *dest, ArrayRef<uint8_t> arr) {
30490b57cec5SDimitry Andric       memcpy(dest, SHA1::hash(arr).data(), hashSize);
30500b57cec5SDimitry Andric     });
30510b57cec5SDimitry Andric     break;
30520b57cec5SDimitry Andric   case BuildIdKind::Uuid:
30530b57cec5SDimitry Andric     if (auto ec = llvm::getRandomBytes(buildId.data(), hashSize))
30540b57cec5SDimitry Andric       error("entropy source failure: " + ec.message());
30550b57cec5SDimitry Andric     break;
30560b57cec5SDimitry Andric   default:
30570b57cec5SDimitry Andric     llvm_unreachable("unknown BuildIdKind");
30580b57cec5SDimitry Andric   }
30590b57cec5SDimitry Andric   for (Partition &part : partitions)
30600b57cec5SDimitry Andric     part.buildId->writeBuildId(buildId);
30610b57cec5SDimitry Andric }
30620b57cec5SDimitry Andric 
30635ffd83dbSDimitry Andric template void elf::createSyntheticSections<ELF32LE>();
30645ffd83dbSDimitry Andric template void elf::createSyntheticSections<ELF32BE>();
30655ffd83dbSDimitry Andric template void elf::createSyntheticSections<ELF64LE>();
30665ffd83dbSDimitry Andric template void elf::createSyntheticSections<ELF64BE>();
306785868e8aSDimitry Andric 
30685ffd83dbSDimitry Andric template void elf::writeResult<ELF32LE>();
30695ffd83dbSDimitry Andric template void elf::writeResult<ELF32BE>();
30705ffd83dbSDimitry Andric template void elf::writeResult<ELF64LE>();
30715ffd83dbSDimitry Andric template void elf::writeResult<ELF64BE>();
3072