10b57cec5SDimitry Andric //===- Writer.cpp ---------------------------------------------------------===// 20b57cec5SDimitry Andric // 30b57cec5SDimitry Andric // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 40b57cec5SDimitry Andric // See https://llvm.org/LICENSE.txt for license information. 50b57cec5SDimitry Andric // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 60b57cec5SDimitry Andric // 70b57cec5SDimitry Andric //===----------------------------------------------------------------------===// 80b57cec5SDimitry Andric 90b57cec5SDimitry Andric #include "Writer.h" 100b57cec5SDimitry Andric #include "AArch64ErrataFix.h" 1185868e8aSDimitry Andric #include "ARMErrataFix.h" 120b57cec5SDimitry Andric #include "CallGraphSort.h" 130b57cec5SDimitry Andric #include "Config.h" 1481ad6265SDimitry Andric #include "InputFiles.h" 150b57cec5SDimitry Andric #include "LinkerScript.h" 160b57cec5SDimitry Andric #include "MapFile.h" 170b57cec5SDimitry Andric #include "OutputSections.h" 180b57cec5SDimitry Andric #include "Relocations.h" 190b57cec5SDimitry Andric #include "SymbolTable.h" 200b57cec5SDimitry Andric #include "Symbols.h" 210b57cec5SDimitry Andric #include "SyntheticSections.h" 220b57cec5SDimitry Andric #include "Target.h" 23fe6060f1SDimitry Andric #include "lld/Common/Arrays.h" 2404eeddc0SDimitry Andric #include "lld/Common/CommonLinkerContext.h" 250b57cec5SDimitry Andric #include "lld/Common/Filesystem.h" 260b57cec5SDimitry Andric #include "lld/Common/Strings.h" 270b57cec5SDimitry Andric #include "llvm/ADT/StringMap.h" 2881ad6265SDimitry Andric #include "llvm/Support/BLAKE3.h" 295ffd83dbSDimitry Andric #include "llvm/Support/Parallel.h" 300b57cec5SDimitry Andric #include "llvm/Support/RandomNumberGenerator.h" 315ffd83dbSDimitry Andric #include "llvm/Support/TimeProfiler.h" 320b57cec5SDimitry Andric #include "llvm/Support/xxhash.h" 330b57cec5SDimitry Andric #include <climits> 340b57cec5SDimitry Andric 355ffd83dbSDimitry Andric #define DEBUG_TYPE "lld" 365ffd83dbSDimitry Andric 370b57cec5SDimitry Andric using namespace llvm; 380b57cec5SDimitry Andric using namespace llvm::ELF; 390b57cec5SDimitry Andric using namespace llvm::object; 400b57cec5SDimitry Andric using namespace llvm::support; 410b57cec5SDimitry Andric using namespace llvm::support::endian; 425ffd83dbSDimitry Andric using namespace lld; 435ffd83dbSDimitry Andric using namespace lld::elf; 440b57cec5SDimitry Andric 450b57cec5SDimitry Andric namespace { 460b57cec5SDimitry Andric // The writer writes a SymbolTable result to a file. 470b57cec5SDimitry Andric template <class ELFT> class Writer { 480b57cec5SDimitry Andric public: 49e8d8bef9SDimitry Andric LLVM_ELF_IMPORT_TYPES_ELFT(ELFT) 50e8d8bef9SDimitry Andric 510b57cec5SDimitry Andric Writer() : buffer(errorHandler().outputBuffer) {} 520b57cec5SDimitry Andric 530b57cec5SDimitry Andric void run(); 540b57cec5SDimitry Andric 550b57cec5SDimitry Andric private: 560b57cec5SDimitry Andric void copyLocalSymbols(); 570b57cec5SDimitry Andric void addSectionSymbols(); 580b57cec5SDimitry Andric void sortSections(); 590b57cec5SDimitry Andric void resolveShfLinkOrder(); 600b57cec5SDimitry Andric void finalizeAddressDependentContent(); 615ffd83dbSDimitry Andric void optimizeBasicBlockJumps(); 620b57cec5SDimitry Andric void sortInputSections(); 630b57cec5SDimitry Andric void finalizeSections(); 640b57cec5SDimitry Andric void checkExecuteOnly(); 650b57cec5SDimitry Andric void setReservedSymbolSections(); 660b57cec5SDimitry Andric 6704eeddc0SDimitry Andric SmallVector<PhdrEntry *, 0> createPhdrs(Partition &part); 680b57cec5SDimitry Andric void addPhdrForSection(Partition &part, unsigned shType, unsigned pType, 690b57cec5SDimitry Andric unsigned pFlags); 700b57cec5SDimitry Andric void assignFileOffsets(); 710b57cec5SDimitry Andric void assignFileOffsetsBinary(); 720b57cec5SDimitry Andric void setPhdrs(Partition &part); 730b57cec5SDimitry Andric void checkSections(); 740b57cec5SDimitry Andric void fixSectionAlignments(); 750b57cec5SDimitry Andric void openFile(); 760b57cec5SDimitry Andric void writeTrapInstr(); 770b57cec5SDimitry Andric void writeHeader(); 780b57cec5SDimitry Andric void writeSections(); 790b57cec5SDimitry Andric void writeSectionsBinary(); 800b57cec5SDimitry Andric void writeBuildId(); 810b57cec5SDimitry Andric 820b57cec5SDimitry Andric std::unique_ptr<FileOutputBuffer> &buffer; 830b57cec5SDimitry Andric 840b57cec5SDimitry Andric void addRelIpltSymbols(); 850b57cec5SDimitry Andric void addStartEndSymbols(); 8681ad6265SDimitry Andric void addStartStopSymbols(OutputSection &osec); 870b57cec5SDimitry Andric 880b57cec5SDimitry Andric uint64_t fileSize; 890b57cec5SDimitry Andric uint64_t sectionHeaderOff; 900b57cec5SDimitry Andric }; 910b57cec5SDimitry Andric } // anonymous namespace 920b57cec5SDimitry Andric 930b57cec5SDimitry Andric static bool needsInterpSection() { 9455e4f9d5SDimitry Andric return !config->relocatable && !config->shared && 9555e4f9d5SDimitry Andric !config->dynamicLinker.empty() && script->needsInterpSection(); 960b57cec5SDimitry Andric } 970b57cec5SDimitry Andric 985ffd83dbSDimitry Andric template <class ELFT> void elf::writeResult() { 995ffd83dbSDimitry Andric Writer<ELFT>().run(); 1000b57cec5SDimitry Andric } 1010b57cec5SDimitry Andric 10204eeddc0SDimitry Andric static void removeEmptyPTLoad(SmallVector<PhdrEntry *, 0> &phdrs) { 1035ffd83dbSDimitry Andric auto it = std::stable_partition( 1045ffd83dbSDimitry Andric phdrs.begin(), phdrs.end(), [&](const PhdrEntry *p) { 1055ffd83dbSDimitry Andric if (p->p_type != PT_LOAD) 1065ffd83dbSDimitry Andric return true; 1075ffd83dbSDimitry Andric if (!p->firstSec) 1085ffd83dbSDimitry Andric return false; 1095ffd83dbSDimitry Andric uint64_t size = p->lastSec->addr + p->lastSec->size - p->firstSec->addr; 1105ffd83dbSDimitry Andric return size != 0; 1115ffd83dbSDimitry Andric }); 1125ffd83dbSDimitry Andric 1135ffd83dbSDimitry Andric // Clear OutputSection::ptLoad for sections contained in removed 1145ffd83dbSDimitry Andric // segments. 1155ffd83dbSDimitry Andric DenseSet<PhdrEntry *> removed(it, phdrs.end()); 1165ffd83dbSDimitry Andric for (OutputSection *sec : outputSections) 1175ffd83dbSDimitry Andric if (removed.count(sec->ptLoad)) 1185ffd83dbSDimitry Andric sec->ptLoad = nullptr; 1195ffd83dbSDimitry Andric phdrs.erase(it, phdrs.end()); 1205ffd83dbSDimitry Andric } 1215ffd83dbSDimitry Andric 1225ffd83dbSDimitry Andric void elf::copySectionsIntoPartitions() { 12304eeddc0SDimitry Andric SmallVector<InputSectionBase *, 0> newSections; 1240b57cec5SDimitry Andric for (unsigned part = 2; part != partitions.size() + 1; ++part) { 1250b57cec5SDimitry Andric for (InputSectionBase *s : inputSections) { 1260b57cec5SDimitry Andric if (!(s->flags & SHF_ALLOC) || !s->isLive()) 1270b57cec5SDimitry Andric continue; 1280b57cec5SDimitry Andric InputSectionBase *copy; 1290b57cec5SDimitry Andric if (s->type == SHT_NOTE) 1300b57cec5SDimitry Andric copy = make<InputSection>(cast<InputSection>(*s)); 1310b57cec5SDimitry Andric else if (auto *es = dyn_cast<EhInputSection>(s)) 1320b57cec5SDimitry Andric copy = make<EhInputSection>(*es); 1330b57cec5SDimitry Andric else 1340b57cec5SDimitry Andric continue; 1350b57cec5SDimitry Andric copy->partition = part; 1360b57cec5SDimitry Andric newSections.push_back(copy); 1370b57cec5SDimitry Andric } 1380b57cec5SDimitry Andric } 1390b57cec5SDimitry Andric 1400b57cec5SDimitry Andric inputSections.insert(inputSections.end(), newSections.begin(), 1410b57cec5SDimitry Andric newSections.end()); 1420b57cec5SDimitry Andric } 1430b57cec5SDimitry Andric 1445ffd83dbSDimitry Andric void elf::combineEhSections() { 145e8d8bef9SDimitry Andric llvm::TimeTraceScope timeScope("Combine EH sections"); 1460b57cec5SDimitry Andric for (InputSectionBase *&s : inputSections) { 1470b57cec5SDimitry Andric // Ignore dead sections and the partition end marker (.part.end), 1480b57cec5SDimitry Andric // whose partition number is out of bounds. 1490b57cec5SDimitry Andric if (!s->isLive() || s->partition == 255) 1500b57cec5SDimitry Andric continue; 1510b57cec5SDimitry Andric 1520b57cec5SDimitry Andric Partition &part = s->getPartition(); 1530b57cec5SDimitry Andric if (auto *es = dyn_cast<EhInputSection>(s)) { 15485868e8aSDimitry Andric part.ehFrame->addSection(es); 1550b57cec5SDimitry Andric s = nullptr; 1560b57cec5SDimitry Andric } else if (s->kind() == SectionBase::Regular && part.armExidx && 1570b57cec5SDimitry Andric part.armExidx->addSection(cast<InputSection>(s))) { 1580b57cec5SDimitry Andric s = nullptr; 1590b57cec5SDimitry Andric } 1600b57cec5SDimitry Andric } 1610b57cec5SDimitry Andric 162349cc55cSDimitry Andric llvm::erase_value(inputSections, nullptr); 1630b57cec5SDimitry Andric } 1640b57cec5SDimitry Andric 1650b57cec5SDimitry Andric static Defined *addOptionalRegular(StringRef name, SectionBase *sec, 166349cc55cSDimitry Andric uint64_t val, uint8_t stOther = STV_HIDDEN) { 1670b57cec5SDimitry Andric Symbol *s = symtab->find(name); 16881ad6265SDimitry Andric if (!s || s->isDefined() || s->isCommon()) 1690b57cec5SDimitry Andric return nullptr; 1700b57cec5SDimitry Andric 17181ad6265SDimitry Andric s->resolve(Defined{nullptr, StringRef(), STB_GLOBAL, stOther, STT_NOTYPE, val, 1720b57cec5SDimitry Andric /*size=*/0, sec}); 17381ad6265SDimitry Andric s->isUsedInRegularObj = true; 1740b57cec5SDimitry Andric return cast<Defined>(s); 1750b57cec5SDimitry Andric } 1760b57cec5SDimitry Andric 1770b57cec5SDimitry Andric static Defined *addAbsolute(StringRef name) { 1780b57cec5SDimitry Andric Symbol *sym = symtab->addSymbol(Defined{nullptr, name, STB_GLOBAL, STV_HIDDEN, 1790b57cec5SDimitry Andric STT_NOTYPE, 0, 0, nullptr}); 18081ad6265SDimitry Andric sym->isUsedInRegularObj = true; 1810b57cec5SDimitry Andric return cast<Defined>(sym); 1820b57cec5SDimitry Andric } 1830b57cec5SDimitry Andric 1840b57cec5SDimitry Andric // The linker is expected to define some symbols depending on 1850b57cec5SDimitry Andric // the linking result. This function defines such symbols. 1865ffd83dbSDimitry Andric void elf::addReservedSymbols() { 1870b57cec5SDimitry Andric if (config->emachine == EM_MIPS) { 1880b57cec5SDimitry Andric // Define _gp for MIPS. st_value of _gp symbol will be updated by Writer 1890b57cec5SDimitry Andric // so that it points to an absolute address which by default is relative 1900b57cec5SDimitry Andric // to GOT. Default offset is 0x7ff0. 1910b57cec5SDimitry Andric // See "Global Data Symbols" in Chapter 6 in the following document: 1920b57cec5SDimitry Andric // ftp://www.linux-mips.org/pub/linux/mips/doc/ABI/mipsabi.pdf 1930b57cec5SDimitry Andric ElfSym::mipsGp = addAbsolute("_gp"); 1940b57cec5SDimitry Andric 1950b57cec5SDimitry Andric // On MIPS O32 ABI, _gp_disp is a magic symbol designates offset between 1960b57cec5SDimitry Andric // start of function and 'gp' pointer into GOT. 1970b57cec5SDimitry Andric if (symtab->find("_gp_disp")) 1980b57cec5SDimitry Andric ElfSym::mipsGpDisp = addAbsolute("_gp_disp"); 1990b57cec5SDimitry Andric 2000b57cec5SDimitry Andric // The __gnu_local_gp is a magic symbol equal to the current value of 'gp' 2010b57cec5SDimitry Andric // pointer. This symbol is used in the code generated by .cpload pseudo-op 2020b57cec5SDimitry Andric // in case of using -mno-shared option. 2030b57cec5SDimitry Andric // https://sourceware.org/ml/binutils/2004-12/msg00094.html 2040b57cec5SDimitry Andric if (symtab->find("__gnu_local_gp")) 2050b57cec5SDimitry Andric ElfSym::mipsLocalGp = addAbsolute("__gnu_local_gp"); 2060b57cec5SDimitry Andric } else if (config->emachine == EM_PPC) { 2070b57cec5SDimitry Andric // glibc *crt1.o has a undefined reference to _SDA_BASE_. Since we don't 2080b57cec5SDimitry Andric // support Small Data Area, define it arbitrarily as 0. 2090b57cec5SDimitry Andric addOptionalRegular("_SDA_BASE_", nullptr, 0, STV_HIDDEN); 2105ffd83dbSDimitry Andric } else if (config->emachine == EM_PPC64) { 2115ffd83dbSDimitry Andric addPPC64SaveRestore(); 2120b57cec5SDimitry Andric } 2130b57cec5SDimitry Andric 2140b57cec5SDimitry Andric // The Power Architecture 64-bit v2 ABI defines a TableOfContents (TOC) which 2150b57cec5SDimitry Andric // combines the typical ELF GOT with the small data sections. It commonly 2160b57cec5SDimitry Andric // includes .got .toc .sdata .sbss. The .TOC. symbol replaces both 2170b57cec5SDimitry Andric // _GLOBAL_OFFSET_TABLE_ and _SDA_BASE_ from the 32-bit ABI. It is used to 2180b57cec5SDimitry Andric // represent the TOC base which is offset by 0x8000 bytes from the start of 2190b57cec5SDimitry Andric // the .got section. 2200b57cec5SDimitry Andric // We do not allow _GLOBAL_OFFSET_TABLE_ to be defined by input objects as the 2210b57cec5SDimitry Andric // correctness of some relocations depends on its value. 2220b57cec5SDimitry Andric StringRef gotSymName = 2230b57cec5SDimitry Andric (config->emachine == EM_PPC64) ? ".TOC." : "_GLOBAL_OFFSET_TABLE_"; 2240b57cec5SDimitry Andric 2250b57cec5SDimitry Andric if (Symbol *s = symtab->find(gotSymName)) { 2260b57cec5SDimitry Andric if (s->isDefined()) { 2270b57cec5SDimitry Andric error(toString(s->file) + " cannot redefine linker defined symbol '" + 2280b57cec5SDimitry Andric gotSymName + "'"); 2290b57cec5SDimitry Andric return; 2300b57cec5SDimitry Andric } 2310b57cec5SDimitry Andric 2320b57cec5SDimitry Andric uint64_t gotOff = 0; 2330b57cec5SDimitry Andric if (config->emachine == EM_PPC64) 2340b57cec5SDimitry Andric gotOff = 0x8000; 2350b57cec5SDimitry Andric 23681ad6265SDimitry Andric s->resolve(Defined{/*file=*/nullptr, StringRef(), STB_GLOBAL, STV_HIDDEN, 2370b57cec5SDimitry Andric STT_NOTYPE, gotOff, /*size=*/0, Out::elfHeader}); 2380b57cec5SDimitry Andric ElfSym::globalOffsetTable = cast<Defined>(s); 2390b57cec5SDimitry Andric } 2400b57cec5SDimitry Andric 2410b57cec5SDimitry Andric // __ehdr_start is the location of ELF file headers. Note that we define 2420b57cec5SDimitry Andric // this symbol unconditionally even when using a linker script, which 2430b57cec5SDimitry Andric // differs from the behavior implemented by GNU linker which only define 2440b57cec5SDimitry Andric // this symbol if ELF headers are in the memory mapped segment. 2450b57cec5SDimitry Andric addOptionalRegular("__ehdr_start", Out::elfHeader, 0, STV_HIDDEN); 2460b57cec5SDimitry Andric 2470b57cec5SDimitry Andric // __executable_start is not documented, but the expectation of at 2480b57cec5SDimitry Andric // least the Android libc is that it points to the ELF header. 2490b57cec5SDimitry Andric addOptionalRegular("__executable_start", Out::elfHeader, 0, STV_HIDDEN); 2500b57cec5SDimitry Andric 2510b57cec5SDimitry Andric // __dso_handle symbol is passed to cxa_finalize as a marker to identify 2520b57cec5SDimitry Andric // each DSO. The address of the symbol doesn't matter as long as they are 2530b57cec5SDimitry Andric // different in different DSOs, so we chose the start address of the DSO. 2540b57cec5SDimitry Andric addOptionalRegular("__dso_handle", Out::elfHeader, 0, STV_HIDDEN); 2550b57cec5SDimitry Andric 256480093f4SDimitry Andric // If linker script do layout we do not need to create any standard symbols. 2570b57cec5SDimitry Andric if (script->hasSectionsCommand) 2580b57cec5SDimitry Andric return; 2590b57cec5SDimitry Andric 2600b57cec5SDimitry Andric auto add = [](StringRef s, int64_t pos) { 2610b57cec5SDimitry Andric return addOptionalRegular(s, Out::elfHeader, pos, STV_DEFAULT); 2620b57cec5SDimitry Andric }; 2630b57cec5SDimitry Andric 2640b57cec5SDimitry Andric ElfSym::bss = add("__bss_start", 0); 2650b57cec5SDimitry Andric ElfSym::end1 = add("end", -1); 2660b57cec5SDimitry Andric ElfSym::end2 = add("_end", -1); 2670b57cec5SDimitry Andric ElfSym::etext1 = add("etext", -1); 2680b57cec5SDimitry Andric ElfSym::etext2 = add("_etext", -1); 2690b57cec5SDimitry Andric ElfSym::edata1 = add("edata", -1); 2700b57cec5SDimitry Andric ElfSym::edata2 = add("_edata", -1); 2710b57cec5SDimitry Andric } 2720b57cec5SDimitry Andric 2730b57cec5SDimitry Andric static OutputSection *findSection(StringRef name, unsigned partition = 1) { 2744824e7fdSDimitry Andric for (SectionCommand *cmd : script->sectionCommands) 27581ad6265SDimitry Andric if (auto *osd = dyn_cast<OutputDesc>(cmd)) 27681ad6265SDimitry Andric if (osd->osec.name == name && osd->osec.partition == partition) 27781ad6265SDimitry Andric return &osd->osec; 2780b57cec5SDimitry Andric return nullptr; 2790b57cec5SDimitry Andric } 2800b57cec5SDimitry Andric 2815ffd83dbSDimitry Andric template <class ELFT> void elf::createSyntheticSections() { 2820b57cec5SDimitry Andric // Initialize all pointers with NULL. This is needed because 2830b57cec5SDimitry Andric // you can call lld::elf::main more than once as a library. 2844824e7fdSDimitry Andric Out::tlsPhdr = nullptr; 2854824e7fdSDimitry Andric Out::preinitArray = nullptr; 2864824e7fdSDimitry Andric Out::initArray = nullptr; 2874824e7fdSDimitry Andric Out::finiArray = nullptr; 2880b57cec5SDimitry Andric 28985868e8aSDimitry Andric // Add the .interp section first because it is not a SyntheticSection. 29085868e8aSDimitry Andric // The removeUnusedSyntheticSections() function relies on the 29185868e8aSDimitry Andric // SyntheticSections coming last. 29285868e8aSDimitry Andric if (needsInterpSection()) { 29385868e8aSDimitry Andric for (size_t i = 1; i <= partitions.size(); ++i) { 29485868e8aSDimitry Andric InputSection *sec = createInterpSection(); 29585868e8aSDimitry Andric sec->partition = i; 29685868e8aSDimitry Andric inputSections.push_back(sec); 29785868e8aSDimitry Andric } 29885868e8aSDimitry Andric } 29985868e8aSDimitry Andric 3000eae32dcSDimitry Andric auto add = [](SyntheticSection &sec) { inputSections.push_back(&sec); }; 3010b57cec5SDimitry Andric 30204eeddc0SDimitry Andric in.shStrTab = std::make_unique<StringTableSection>(".shstrtab", false); 3030b57cec5SDimitry Andric 3040b57cec5SDimitry Andric Out::programHeaders = make<OutputSection>("", 0, SHF_ALLOC); 3050b57cec5SDimitry Andric Out::programHeaders->alignment = config->wordsize; 3060b57cec5SDimitry Andric 3070b57cec5SDimitry Andric if (config->strip != StripPolicy::All) { 30804eeddc0SDimitry Andric in.strTab = std::make_unique<StringTableSection>(".strtab", false); 30904eeddc0SDimitry Andric in.symTab = std::make_unique<SymbolTableSection<ELFT>>(*in.strTab); 31004eeddc0SDimitry Andric in.symTabShndx = std::make_unique<SymtabShndxSection>(); 3110b57cec5SDimitry Andric } 3120b57cec5SDimitry Andric 31304eeddc0SDimitry Andric in.bss = std::make_unique<BssSection>(".bss", 0, 1); 3140eae32dcSDimitry Andric add(*in.bss); 3150b57cec5SDimitry Andric 3160b57cec5SDimitry Andric // If there is a SECTIONS command and a .data.rel.ro section name use name 3170b57cec5SDimitry Andric // .data.rel.ro.bss so that we match in the .data.rel.ro output section. 3180b57cec5SDimitry Andric // This makes sure our relro is contiguous. 3191fd87a68SDimitry Andric bool hasDataRelRo = script->hasSectionsCommand && findSection(".data.rel.ro"); 32004eeddc0SDimitry Andric in.bssRelRo = std::make_unique<BssSection>( 32104eeddc0SDimitry Andric hasDataRelRo ? ".data.rel.ro.bss" : ".bss.rel.ro", 0, 1); 3220eae32dcSDimitry Andric add(*in.bssRelRo); 3230b57cec5SDimitry Andric 3240b57cec5SDimitry Andric // Add MIPS-specific sections. 3250b57cec5SDimitry Andric if (config->emachine == EM_MIPS) { 3260b57cec5SDimitry Andric if (!config->shared && config->hasDynSymTab) { 32704eeddc0SDimitry Andric in.mipsRldMap = std::make_unique<MipsRldMapSection>(); 3280eae32dcSDimitry Andric add(*in.mipsRldMap); 3290b57cec5SDimitry Andric } 3301fd87a68SDimitry Andric if ((in.mipsAbiFlags = MipsAbiFlagsSection<ELFT>::create())) 3311fd87a68SDimitry Andric add(*in.mipsAbiFlags); 3321fd87a68SDimitry Andric if ((in.mipsOptions = MipsOptionsSection<ELFT>::create())) 3331fd87a68SDimitry Andric add(*in.mipsOptions); 3341fd87a68SDimitry Andric if ((in.mipsReginfo = MipsReginfoSection<ELFT>::create())) 3351fd87a68SDimitry Andric add(*in.mipsReginfo); 3360b57cec5SDimitry Andric } 3370b57cec5SDimitry Andric 33885868e8aSDimitry Andric StringRef relaDynName = config->isRela ? ".rela.dyn" : ".rel.dyn"; 33985868e8aSDimitry Andric 3400b57cec5SDimitry Andric for (Partition &part : partitions) { 3410eae32dcSDimitry Andric auto add = [&](SyntheticSection &sec) { 3420eae32dcSDimitry Andric sec.partition = part.getNumber(); 3430eae32dcSDimitry Andric inputSections.push_back(&sec); 3440b57cec5SDimitry Andric }; 3450b57cec5SDimitry Andric 3460b57cec5SDimitry Andric if (!part.name.empty()) { 34704eeddc0SDimitry Andric part.elfHeader = std::make_unique<PartitionElfHeaderSection<ELFT>>(); 3480b57cec5SDimitry Andric part.elfHeader->name = part.name; 3490eae32dcSDimitry Andric add(*part.elfHeader); 3500b57cec5SDimitry Andric 35104eeddc0SDimitry Andric part.programHeaders = 35204eeddc0SDimitry Andric std::make_unique<PartitionProgramHeadersSection<ELFT>>(); 3530eae32dcSDimitry Andric add(*part.programHeaders); 3540b57cec5SDimitry Andric } 3550b57cec5SDimitry Andric 3560b57cec5SDimitry Andric if (config->buildId != BuildIdKind::None) { 35704eeddc0SDimitry Andric part.buildId = std::make_unique<BuildIdSection>(); 3580eae32dcSDimitry Andric add(*part.buildId); 3590b57cec5SDimitry Andric } 3600b57cec5SDimitry Andric 36104eeddc0SDimitry Andric part.dynStrTab = std::make_unique<StringTableSection>(".dynstr", true); 36204eeddc0SDimitry Andric part.dynSymTab = 36304eeddc0SDimitry Andric std::make_unique<SymbolTableSection<ELFT>>(*part.dynStrTab); 36404eeddc0SDimitry Andric part.dynamic = std::make_unique<DynamicSection<ELFT>>(); 36581ad6265SDimitry Andric 36681ad6265SDimitry Andric if (config->emachine == EM_AARCH64 && 36781ad6265SDimitry Andric config->androidMemtagMode != ELF::NT_MEMTAG_LEVEL_NONE) { 36881ad6265SDimitry Andric part.memtagAndroidNote = std::make_unique<MemtagAndroidNote>(); 36981ad6265SDimitry Andric add(*part.memtagAndroidNote); 37081ad6265SDimitry Andric } 37181ad6265SDimitry Andric 37285868e8aSDimitry Andric if (config->androidPackDynRelocs) 37385868e8aSDimitry Andric part.relaDyn = 37404eeddc0SDimitry Andric std::make_unique<AndroidPackedRelocationSection<ELFT>>(relaDynName); 37504eeddc0SDimitry Andric else 37604eeddc0SDimitry Andric part.relaDyn = std::make_unique<RelocationSection<ELFT>>( 37704eeddc0SDimitry Andric relaDynName, config->zCombreloc); 3780b57cec5SDimitry Andric 3790b57cec5SDimitry Andric if (config->hasDynSymTab) { 3800eae32dcSDimitry Andric add(*part.dynSymTab); 3810b57cec5SDimitry Andric 38204eeddc0SDimitry Andric part.verSym = std::make_unique<VersionTableSection>(); 3830eae32dcSDimitry Andric add(*part.verSym); 3840b57cec5SDimitry Andric 38585868e8aSDimitry Andric if (!namedVersionDefs().empty()) { 38604eeddc0SDimitry Andric part.verDef = std::make_unique<VersionDefinitionSection>(); 3870eae32dcSDimitry Andric add(*part.verDef); 3880b57cec5SDimitry Andric } 3890b57cec5SDimitry Andric 39004eeddc0SDimitry Andric part.verNeed = std::make_unique<VersionNeedSection<ELFT>>(); 3910eae32dcSDimitry Andric add(*part.verNeed); 3920b57cec5SDimitry Andric 3930b57cec5SDimitry Andric if (config->gnuHash) { 39404eeddc0SDimitry Andric part.gnuHashTab = std::make_unique<GnuHashTableSection>(); 3950eae32dcSDimitry Andric add(*part.gnuHashTab); 3960b57cec5SDimitry Andric } 3970b57cec5SDimitry Andric 3980b57cec5SDimitry Andric if (config->sysvHash) { 39904eeddc0SDimitry Andric part.hashTab = std::make_unique<HashTableSection>(); 4000eae32dcSDimitry Andric add(*part.hashTab); 4010b57cec5SDimitry Andric } 4020b57cec5SDimitry Andric 4030eae32dcSDimitry Andric add(*part.dynamic); 4040eae32dcSDimitry Andric add(*part.dynStrTab); 4050eae32dcSDimitry Andric add(*part.relaDyn); 4060b57cec5SDimitry Andric } 4070b57cec5SDimitry Andric 4080b57cec5SDimitry Andric if (config->relrPackDynRelocs) { 40904eeddc0SDimitry Andric part.relrDyn = std::make_unique<RelrSection<ELFT>>(); 4100eae32dcSDimitry Andric add(*part.relrDyn); 4110b57cec5SDimitry Andric } 4120b57cec5SDimitry Andric 4130b57cec5SDimitry Andric if (!config->relocatable) { 4140b57cec5SDimitry Andric if (config->ehFrameHdr) { 41504eeddc0SDimitry Andric part.ehFrameHdr = std::make_unique<EhFrameHeader>(); 4160eae32dcSDimitry Andric add(*part.ehFrameHdr); 4170b57cec5SDimitry Andric } 41804eeddc0SDimitry Andric part.ehFrame = std::make_unique<EhFrameSection>(); 4190eae32dcSDimitry Andric add(*part.ehFrame); 4200b57cec5SDimitry Andric } 4210b57cec5SDimitry Andric 4220b57cec5SDimitry Andric if (config->emachine == EM_ARM && !config->relocatable) { 4230b57cec5SDimitry Andric // The ARMExidxsyntheticsection replaces all the individual .ARM.exidx 4240b57cec5SDimitry Andric // InputSections. 42504eeddc0SDimitry Andric part.armExidx = std::make_unique<ARMExidxSyntheticSection>(); 4260eae32dcSDimitry Andric add(*part.armExidx); 4270b57cec5SDimitry Andric } 4280b57cec5SDimitry Andric } 4290b57cec5SDimitry Andric 4300b57cec5SDimitry Andric if (partitions.size() != 1) { 4310b57cec5SDimitry Andric // Create the partition end marker. This needs to be in partition number 255 4320b57cec5SDimitry Andric // so that it is sorted after all other partitions. It also has other 4330b57cec5SDimitry Andric // special handling (see createPhdrs() and combineEhSections()). 43404eeddc0SDimitry Andric in.partEnd = 43504eeddc0SDimitry Andric std::make_unique<BssSection>(".part.end", config->maxPageSize, 1); 4360b57cec5SDimitry Andric in.partEnd->partition = 255; 4370eae32dcSDimitry Andric add(*in.partEnd); 4380b57cec5SDimitry Andric 43904eeddc0SDimitry Andric in.partIndex = std::make_unique<PartitionIndexSection>(); 44004eeddc0SDimitry Andric addOptionalRegular("__part_index_begin", in.partIndex.get(), 0); 44104eeddc0SDimitry Andric addOptionalRegular("__part_index_end", in.partIndex.get(), 4420b57cec5SDimitry Andric in.partIndex->getSize()); 4430eae32dcSDimitry Andric add(*in.partIndex); 4440b57cec5SDimitry Andric } 4450b57cec5SDimitry Andric 4460b57cec5SDimitry Andric // Add .got. MIPS' .got is so different from the other archs, 4470b57cec5SDimitry Andric // it has its own class. 4480b57cec5SDimitry Andric if (config->emachine == EM_MIPS) { 44904eeddc0SDimitry Andric in.mipsGot = std::make_unique<MipsGotSection>(); 4500eae32dcSDimitry Andric add(*in.mipsGot); 4510b57cec5SDimitry Andric } else { 45204eeddc0SDimitry Andric in.got = std::make_unique<GotSection>(); 4530eae32dcSDimitry Andric add(*in.got); 4540b57cec5SDimitry Andric } 4550b57cec5SDimitry Andric 4560b57cec5SDimitry Andric if (config->emachine == EM_PPC) { 45704eeddc0SDimitry Andric in.ppc32Got2 = std::make_unique<PPC32Got2Section>(); 4580eae32dcSDimitry Andric add(*in.ppc32Got2); 4590b57cec5SDimitry Andric } 4600b57cec5SDimitry Andric 4610b57cec5SDimitry Andric if (config->emachine == EM_PPC64) { 46204eeddc0SDimitry Andric in.ppc64LongBranchTarget = std::make_unique<PPC64LongBranchTargetSection>(); 4630eae32dcSDimitry Andric add(*in.ppc64LongBranchTarget); 4640b57cec5SDimitry Andric } 4650b57cec5SDimitry Andric 46604eeddc0SDimitry Andric in.gotPlt = std::make_unique<GotPltSection>(); 4670eae32dcSDimitry Andric add(*in.gotPlt); 46804eeddc0SDimitry Andric in.igotPlt = std::make_unique<IgotPltSection>(); 4690eae32dcSDimitry Andric add(*in.igotPlt); 4700b57cec5SDimitry Andric 4710b57cec5SDimitry Andric // _GLOBAL_OFFSET_TABLE_ is defined relative to either .got.plt or .got. Treat 4720b57cec5SDimitry Andric // it as a relocation and ensure the referenced section is created. 4730b57cec5SDimitry Andric if (ElfSym::globalOffsetTable && config->emachine != EM_MIPS) { 4740b57cec5SDimitry Andric if (target->gotBaseSymInGotPlt) 4750b57cec5SDimitry Andric in.gotPlt->hasGotPltOffRel = true; 4760b57cec5SDimitry Andric else 4770b57cec5SDimitry Andric in.got->hasGotOffRel = true; 4780b57cec5SDimitry Andric } 4790b57cec5SDimitry Andric 4800b57cec5SDimitry Andric if (config->gdbIndex) 4810eae32dcSDimitry Andric add(*GdbIndexSection::create<ELFT>()); 4820b57cec5SDimitry Andric 4830b57cec5SDimitry Andric // We always need to add rel[a].plt to output if it has entries. 4840b57cec5SDimitry Andric // Even for static linking it can contain R_[*]_IRELATIVE relocations. 48504eeddc0SDimitry Andric in.relaPlt = std::make_unique<RelocationSection<ELFT>>( 4860b57cec5SDimitry Andric config->isRela ? ".rela.plt" : ".rel.plt", /*sort=*/false); 4870eae32dcSDimitry Andric add(*in.relaPlt); 4880b57cec5SDimitry Andric 48985868e8aSDimitry Andric // The relaIplt immediately follows .rel[a].dyn to ensure that the IRelative 49085868e8aSDimitry Andric // relocations are processed last by the dynamic loader. We cannot place the 49185868e8aSDimitry Andric // iplt section in .rel.dyn when Android relocation packing is enabled because 49285868e8aSDimitry Andric // that would cause a section type mismatch. However, because the Android 49385868e8aSDimitry Andric // dynamic loader reads .rel.plt after .rel.dyn, we can get the desired 49485868e8aSDimitry Andric // behaviour by placing the iplt section in .rel.plt. 49504eeddc0SDimitry Andric in.relaIplt = std::make_unique<RelocationSection<ELFT>>( 49685868e8aSDimitry Andric config->androidPackDynRelocs ? in.relaPlt->name : relaDynName, 4970b57cec5SDimitry Andric /*sort=*/false); 4980eae32dcSDimitry Andric add(*in.relaIplt); 4990b57cec5SDimitry Andric 500480093f4SDimitry Andric if ((config->emachine == EM_386 || config->emachine == EM_X86_64) && 501480093f4SDimitry Andric (config->andFeatures & GNU_PROPERTY_X86_FEATURE_1_IBT)) { 50204eeddc0SDimitry Andric in.ibtPlt = std::make_unique<IBTPltSection>(); 5030eae32dcSDimitry Andric add(*in.ibtPlt); 504480093f4SDimitry Andric } 505480093f4SDimitry Andric 50604eeddc0SDimitry Andric if (config->emachine == EM_PPC) 50704eeddc0SDimitry Andric in.plt = std::make_unique<PPC32GlinkSection>(); 50804eeddc0SDimitry Andric else 50904eeddc0SDimitry Andric in.plt = std::make_unique<PltSection>(); 5100eae32dcSDimitry Andric add(*in.plt); 51104eeddc0SDimitry Andric in.iplt = std::make_unique<IpltSection>(); 5120eae32dcSDimitry Andric add(*in.iplt); 5130b57cec5SDimitry Andric 5140b57cec5SDimitry Andric if (config->andFeatures) 5150eae32dcSDimitry Andric add(*make<GnuPropertySection>()); 5160b57cec5SDimitry Andric 5170b57cec5SDimitry Andric // .note.GNU-stack is always added when we are creating a re-linkable 5180b57cec5SDimitry Andric // object file. Other linkers are using the presence of this marker 5190b57cec5SDimitry Andric // section to control the executable-ness of the stack area, but that 5200b57cec5SDimitry Andric // is irrelevant these days. Stack area should always be non-executable 5210b57cec5SDimitry Andric // by default. So we emit this section unconditionally. 5220b57cec5SDimitry Andric if (config->relocatable) 5230eae32dcSDimitry Andric add(*make<GnuStackSection>()); 5240b57cec5SDimitry Andric 5250b57cec5SDimitry Andric if (in.symTab) 5260eae32dcSDimitry Andric add(*in.symTab); 5270b57cec5SDimitry Andric if (in.symTabShndx) 5280eae32dcSDimitry Andric add(*in.symTabShndx); 5290eae32dcSDimitry Andric add(*in.shStrTab); 5300b57cec5SDimitry Andric if (in.strTab) 5310eae32dcSDimitry Andric add(*in.strTab); 5320b57cec5SDimitry Andric } 5330b57cec5SDimitry Andric 5340b57cec5SDimitry Andric // The main function of the writer. 5350b57cec5SDimitry Andric template <class ELFT> void Writer<ELFT>::run() { 5360b57cec5SDimitry Andric copyLocalSymbols(); 5370b57cec5SDimitry Andric 5380b57cec5SDimitry Andric if (config->copyRelocs) 5390b57cec5SDimitry Andric addSectionSymbols(); 5400b57cec5SDimitry Andric 5410b57cec5SDimitry Andric // Now that we have a complete set of output sections. This function 5420b57cec5SDimitry Andric // completes section contents. For example, we need to add strings 5430b57cec5SDimitry Andric // to the string table, and add entries to .got and .plt. 5440b57cec5SDimitry Andric // finalizeSections does that. 5450b57cec5SDimitry Andric finalizeSections(); 5460b57cec5SDimitry Andric checkExecuteOnly(); 5470b57cec5SDimitry Andric 548349cc55cSDimitry Andric // If --compressed-debug-sections is specified, compress .debug_* sections. 549349cc55cSDimitry Andric // Do it right now because it changes the size of output sections. 5500b57cec5SDimitry Andric for (OutputSection *sec : outputSections) 5510b57cec5SDimitry Andric sec->maybeCompress<ELFT>(); 5520b57cec5SDimitry Andric 55369660011SDimitry Andric if (script->hasSectionsCommand) 5540b57cec5SDimitry Andric script->allocateHeaders(mainPart->phdrs); 5550b57cec5SDimitry Andric 5560b57cec5SDimitry Andric // Remove empty PT_LOAD to avoid causing the dynamic linker to try to mmap a 5570b57cec5SDimitry Andric // 0 sized region. This has to be done late since only after assignAddresses 5580b57cec5SDimitry Andric // we know the size of the sections. 5590b57cec5SDimitry Andric for (Partition &part : partitions) 5600b57cec5SDimitry Andric removeEmptyPTLoad(part.phdrs); 5610b57cec5SDimitry Andric 5620b57cec5SDimitry Andric if (!config->oFormatBinary) 5630b57cec5SDimitry Andric assignFileOffsets(); 5640b57cec5SDimitry Andric else 5650b57cec5SDimitry Andric assignFileOffsetsBinary(); 5660b57cec5SDimitry Andric 5670b57cec5SDimitry Andric for (Partition &part : partitions) 5680b57cec5SDimitry Andric setPhdrs(part); 5690b57cec5SDimitry Andric 57081ad6265SDimitry Andric // Handle --print-map(-M)/--Map and --cref. Dump them before checkSections() 57181ad6265SDimitry Andric // because the files may be useful in case checkSections() or openFile() 57281ad6265SDimitry Andric // fails, for example, due to an erroneous file size. 5734824e7fdSDimitry Andric writeMapAndCref(); 5745ffd83dbSDimitry Andric 5750b57cec5SDimitry Andric if (config->checkSections) 5760b57cec5SDimitry Andric checkSections(); 5770b57cec5SDimitry Andric 5780b57cec5SDimitry Andric // It does not make sense try to open the file if we have error already. 5790b57cec5SDimitry Andric if (errorCount()) 5800b57cec5SDimitry Andric return; 581e8d8bef9SDimitry Andric 582e8d8bef9SDimitry Andric { 583e8d8bef9SDimitry Andric llvm::TimeTraceScope timeScope("Write output file"); 5840b57cec5SDimitry Andric // Write the result down to a file. 5850b57cec5SDimitry Andric openFile(); 5860b57cec5SDimitry Andric if (errorCount()) 5870b57cec5SDimitry Andric return; 5880b57cec5SDimitry Andric 5890b57cec5SDimitry Andric if (!config->oFormatBinary) { 59085868e8aSDimitry Andric if (config->zSeparate != SeparateSegmentKind::None) 5910b57cec5SDimitry Andric writeTrapInstr(); 5920b57cec5SDimitry Andric writeHeader(); 5930b57cec5SDimitry Andric writeSections(); 5940b57cec5SDimitry Andric } else { 5950b57cec5SDimitry Andric writeSectionsBinary(); 5960b57cec5SDimitry Andric } 5970b57cec5SDimitry Andric 5980b57cec5SDimitry Andric // Backfill .note.gnu.build-id section content. This is done at last 5990b57cec5SDimitry Andric // because the content is usually a hash value of the entire output file. 6000b57cec5SDimitry Andric writeBuildId(); 6010b57cec5SDimitry Andric if (errorCount()) 6020b57cec5SDimitry Andric return; 6030b57cec5SDimitry Andric 6040b57cec5SDimitry Andric if (auto e = buffer->commit()) 6050b57cec5SDimitry Andric error("failed to write to the output file: " + toString(std::move(e))); 6060b57cec5SDimitry Andric } 607e8d8bef9SDimitry Andric } 6080b57cec5SDimitry Andric 6095ffd83dbSDimitry Andric template <class ELFT, class RelTy> 6105ffd83dbSDimitry Andric static void markUsedLocalSymbolsImpl(ObjFile<ELFT> *file, 6115ffd83dbSDimitry Andric llvm::ArrayRef<RelTy> rels) { 6125ffd83dbSDimitry Andric for (const RelTy &rel : rels) { 6135ffd83dbSDimitry Andric Symbol &sym = file->getRelocTargetSym(rel); 6145ffd83dbSDimitry Andric if (sym.isLocal()) 6155ffd83dbSDimitry Andric sym.used = true; 6165ffd83dbSDimitry Andric } 6175ffd83dbSDimitry Andric } 6185ffd83dbSDimitry Andric 6195ffd83dbSDimitry Andric // The function ensures that the "used" field of local symbols reflects the fact 6205ffd83dbSDimitry Andric // that the symbol is used in a relocation from a live section. 6215ffd83dbSDimitry Andric template <class ELFT> static void markUsedLocalSymbols() { 6225ffd83dbSDimitry Andric // With --gc-sections, the field is already filled. 6235ffd83dbSDimitry Andric // See MarkLive<ELFT>::resolveReloc(). 6245ffd83dbSDimitry Andric if (config->gcSections) 6255ffd83dbSDimitry Andric return; 62681ad6265SDimitry Andric for (ELFFileBase *file : ctx->objectFiles) { 6275ffd83dbSDimitry Andric ObjFile<ELFT> *f = cast<ObjFile<ELFT>>(file); 6285ffd83dbSDimitry Andric for (InputSectionBase *s : f->getSections()) { 6295ffd83dbSDimitry Andric InputSection *isec = dyn_cast_or_null<InputSection>(s); 6305ffd83dbSDimitry Andric if (!isec) 6315ffd83dbSDimitry Andric continue; 6325ffd83dbSDimitry Andric if (isec->type == SHT_REL) 6335ffd83dbSDimitry Andric markUsedLocalSymbolsImpl(f, isec->getDataAs<typename ELFT::Rel>()); 6345ffd83dbSDimitry Andric else if (isec->type == SHT_RELA) 6355ffd83dbSDimitry Andric markUsedLocalSymbolsImpl(f, isec->getDataAs<typename ELFT::Rela>()); 6365ffd83dbSDimitry Andric } 6375ffd83dbSDimitry Andric } 6385ffd83dbSDimitry Andric } 6395ffd83dbSDimitry Andric 6400b57cec5SDimitry Andric static bool shouldKeepInSymtab(const Defined &sym) { 6410b57cec5SDimitry Andric if (sym.isSection()) 6420b57cec5SDimitry Andric return false; 6430b57cec5SDimitry Andric 6445ffd83dbSDimitry Andric // If --emit-reloc or -r is given, preserve symbols referenced by relocations 6455ffd83dbSDimitry Andric // from live sections. 64681ad6265SDimitry Andric if (sym.used && config->copyRelocs) 6470b57cec5SDimitry Andric return true; 6480b57cec5SDimitry Andric 6495ffd83dbSDimitry Andric // Exclude local symbols pointing to .ARM.exidx sections. 6505ffd83dbSDimitry Andric // They are probably mapping symbols "$d", which are optional for these 6515ffd83dbSDimitry Andric // sections. After merging the .ARM.exidx sections, some of these symbols 6525ffd83dbSDimitry Andric // may become dangling. The easiest way to avoid the issue is not to add 6535ffd83dbSDimitry Andric // them to the symbol table from the beginning. 6545ffd83dbSDimitry Andric if (config->emachine == EM_ARM && sym.section && 6555ffd83dbSDimitry Andric sym.section->type == SHT_ARM_EXIDX) 6565ffd83dbSDimitry Andric return false; 6575ffd83dbSDimitry Andric 6585ffd83dbSDimitry Andric if (config->discard == DiscardPolicy::None) 6590b57cec5SDimitry Andric return true; 6605ffd83dbSDimitry Andric if (config->discard == DiscardPolicy::All) 6615ffd83dbSDimitry Andric return false; 6620b57cec5SDimitry Andric 6630b57cec5SDimitry Andric // In ELF assembly .L symbols are normally discarded by the assembler. 6640b57cec5SDimitry Andric // If the assembler fails to do so, the linker discards them if 6650b57cec5SDimitry Andric // * --discard-locals is used. 6660b57cec5SDimitry Andric // * The symbol is in a SHF_MERGE section, which is normally the reason for 6670b57cec5SDimitry Andric // the assembler keeping the .L symbol. 668349cc55cSDimitry Andric if (sym.getName().startswith(".L") && 669349cc55cSDimitry Andric (config->discard == DiscardPolicy::Locals || 670349cc55cSDimitry Andric (sym.section && (sym.section->flags & SHF_MERGE)))) 6710b57cec5SDimitry Andric return false; 672349cc55cSDimitry Andric return true; 6730b57cec5SDimitry Andric } 6740b57cec5SDimitry Andric 6750b57cec5SDimitry Andric static bool includeInSymtab(const Symbol &b) { 6760b57cec5SDimitry Andric if (auto *d = dyn_cast<Defined>(&b)) { 6770b57cec5SDimitry Andric // Always include absolute symbols. 6780b57cec5SDimitry Andric SectionBase *sec = d->section; 6790b57cec5SDimitry Andric if (!sec) 6800b57cec5SDimitry Andric return true; 6810b57cec5SDimitry Andric 6820b57cec5SDimitry Andric if (auto *s = dyn_cast<MergeInputSection>(sec)) 68381ad6265SDimitry Andric return s->getSectionPiece(d->value).live; 68481ad6265SDimitry Andric return sec->isLive(); 6850b57cec5SDimitry Andric } 68681ad6265SDimitry Andric return b.used || !config->gcSections; 6870b57cec5SDimitry Andric } 6880b57cec5SDimitry Andric 6890b57cec5SDimitry Andric // Local symbols are not in the linker's symbol table. This function scans 6900b57cec5SDimitry Andric // each object file's symbol table to copy local symbols to the output. 6910b57cec5SDimitry Andric template <class ELFT> void Writer<ELFT>::copyLocalSymbols() { 6920b57cec5SDimitry Andric if (!in.symTab) 6930b57cec5SDimitry Andric return; 694e8d8bef9SDimitry Andric llvm::TimeTraceScope timeScope("Add local symbols"); 6955ffd83dbSDimitry Andric if (config->copyRelocs && config->discard != DiscardPolicy::None) 6965ffd83dbSDimitry Andric markUsedLocalSymbols<ELFT>(); 69781ad6265SDimitry Andric for (ELFFileBase *file : ctx->objectFiles) { 6980eae32dcSDimitry Andric for (Symbol *b : file->getLocalSymbols()) { 6995ffd83dbSDimitry Andric assert(b->isLocal() && "should have been caught in initializeSymbols()"); 7000b57cec5SDimitry Andric auto *dr = dyn_cast<Defined>(b); 7010b57cec5SDimitry Andric 7020b57cec5SDimitry Andric // No reason to keep local undefined symbol in symtab. 7030b57cec5SDimitry Andric if (!dr) 7040b57cec5SDimitry Andric continue; 7050eae32dcSDimitry Andric if (includeInSymtab(*b) && shouldKeepInSymtab(*dr)) 7060b57cec5SDimitry Andric in.symTab->addSymbol(b); 7070b57cec5SDimitry Andric } 7080b57cec5SDimitry Andric } 7090b57cec5SDimitry Andric } 7100b57cec5SDimitry Andric 7110b57cec5SDimitry Andric // Create a section symbol for each output section so that we can represent 7120b57cec5SDimitry Andric // relocations that point to the section. If we know that no relocation is 7130b57cec5SDimitry Andric // referring to a section (that happens if the section is a synthetic one), we 7140b57cec5SDimitry Andric // don't create a section symbol for that section. 7150b57cec5SDimitry Andric template <class ELFT> void Writer<ELFT>::addSectionSymbols() { 7164824e7fdSDimitry Andric for (SectionCommand *cmd : script->sectionCommands) { 71781ad6265SDimitry Andric auto *osd = dyn_cast<OutputDesc>(cmd); 71881ad6265SDimitry Andric if (!osd) 7190b57cec5SDimitry Andric continue; 72081ad6265SDimitry Andric OutputSection &osec = osd->osec; 7213a9a9c0cSDimitry Andric InputSectionBase *isec = nullptr; 7223a9a9c0cSDimitry Andric // Iterate over all input sections and add a STT_SECTION symbol if any input 7233a9a9c0cSDimitry Andric // section may be a relocation target. 7243a9a9c0cSDimitry Andric for (SectionCommand *cmd : osec.commands) { 7253a9a9c0cSDimitry Andric auto *isd = dyn_cast<InputSectionDescription>(cmd); 7263a9a9c0cSDimitry Andric if (!isd) 7270b57cec5SDimitry Andric continue; 7283a9a9c0cSDimitry Andric for (InputSectionBase *s : isd->sections) { 7290b57cec5SDimitry Andric // Relocations are not using REL[A] section symbols. 7303a9a9c0cSDimitry Andric if (s->type == SHT_REL || s->type == SHT_RELA) 7310b57cec5SDimitry Andric continue; 7320b57cec5SDimitry Andric 7333a9a9c0cSDimitry Andric // Unlike other synthetic sections, mergeable output sections contain 7343a9a9c0cSDimitry Andric // data copied from input sections, and there may be a relocation 7353a9a9c0cSDimitry Andric // pointing to its contents if -r or --emit-reloc is given. 7363a9a9c0cSDimitry Andric if (isa<SyntheticSection>(s) && !(s->flags & SHF_MERGE)) 7373a9a9c0cSDimitry Andric continue; 7383a9a9c0cSDimitry Andric 7393a9a9c0cSDimitry Andric isec = s; 7403a9a9c0cSDimitry Andric break; 7413a9a9c0cSDimitry Andric } 7423a9a9c0cSDimitry Andric } 7433a9a9c0cSDimitry Andric if (!isec) 7440b57cec5SDimitry Andric continue; 7450b57cec5SDimitry Andric 746e8d8bef9SDimitry Andric // Set the symbol to be relative to the output section so that its st_value 747e8d8bef9SDimitry Andric // equals the output section address. Note, there may be a gap between the 748e8d8bef9SDimitry Andric // start of the output section and isec. 74981ad6265SDimitry Andric in.symTab->addSymbol(makeDefined(isec->file, "", STB_LOCAL, /*stOther=*/0, 75081ad6265SDimitry Andric STT_SECTION, 75181ad6265SDimitry Andric /*value=*/0, /*size=*/0, &osec)); 7520b57cec5SDimitry Andric } 7530b57cec5SDimitry Andric } 7540b57cec5SDimitry Andric 7550b57cec5SDimitry Andric // Today's loaders have a feature to make segments read-only after 7560b57cec5SDimitry Andric // processing dynamic relocations to enhance security. PT_GNU_RELRO 7570b57cec5SDimitry Andric // is defined for that. 7580b57cec5SDimitry Andric // 7590b57cec5SDimitry Andric // This function returns true if a section needs to be put into a 7600b57cec5SDimitry Andric // PT_GNU_RELRO segment. 7610b57cec5SDimitry Andric static bool isRelroSection(const OutputSection *sec) { 7620b57cec5SDimitry Andric if (!config->zRelro) 7630b57cec5SDimitry Andric return false; 76481ad6265SDimitry Andric if (sec->relro) 76581ad6265SDimitry Andric return true; 7660b57cec5SDimitry Andric 7670b57cec5SDimitry Andric uint64_t flags = sec->flags; 7680b57cec5SDimitry Andric 7690b57cec5SDimitry Andric // Non-allocatable or non-writable sections don't need RELRO because 7700b57cec5SDimitry Andric // they are not writable or not even mapped to memory in the first place. 7710b57cec5SDimitry Andric // RELRO is for sections that are essentially read-only but need to 7720b57cec5SDimitry Andric // be writable only at process startup to allow dynamic linker to 7730b57cec5SDimitry Andric // apply relocations. 7740b57cec5SDimitry Andric if (!(flags & SHF_ALLOC) || !(flags & SHF_WRITE)) 7750b57cec5SDimitry Andric return false; 7760b57cec5SDimitry Andric 7770b57cec5SDimitry Andric // Once initialized, TLS data segments are used as data templates 7780b57cec5SDimitry Andric // for a thread-local storage. For each new thread, runtime 7790b57cec5SDimitry Andric // allocates memory for a TLS and copy templates there. No thread 7800b57cec5SDimitry Andric // are supposed to use templates directly. Thus, it can be in RELRO. 7810b57cec5SDimitry Andric if (flags & SHF_TLS) 7820b57cec5SDimitry Andric return true; 7830b57cec5SDimitry Andric 7840b57cec5SDimitry Andric // .init_array, .preinit_array and .fini_array contain pointers to 7850b57cec5SDimitry Andric // functions that are executed on process startup or exit. These 7860b57cec5SDimitry Andric // pointers are set by the static linker, and they are not expected 7870b57cec5SDimitry Andric // to change at runtime. But if you are an attacker, you could do 7880b57cec5SDimitry Andric // interesting things by manipulating pointers in .fini_array, for 7890b57cec5SDimitry Andric // example. So they are put into RELRO. 7900b57cec5SDimitry Andric uint32_t type = sec->type; 7910b57cec5SDimitry Andric if (type == SHT_INIT_ARRAY || type == SHT_FINI_ARRAY || 7920b57cec5SDimitry Andric type == SHT_PREINIT_ARRAY) 7930b57cec5SDimitry Andric return true; 7940b57cec5SDimitry Andric 7950b57cec5SDimitry Andric // .got contains pointers to external symbols. They are resolved by 7960b57cec5SDimitry Andric // the dynamic linker when a module is loaded into memory, and after 7970b57cec5SDimitry Andric // that they are not expected to change. So, it can be in RELRO. 7980b57cec5SDimitry Andric if (in.got && sec == in.got->getParent()) 7990b57cec5SDimitry Andric return true; 8000b57cec5SDimitry Andric 8010b57cec5SDimitry Andric // .toc is a GOT-ish section for PowerPC64. Their contents are accessed 8020b57cec5SDimitry Andric // through r2 register, which is reserved for that purpose. Since r2 is used 8030b57cec5SDimitry Andric // for accessing .got as well, .got and .toc need to be close enough in the 8040b57cec5SDimitry Andric // virtual address space. Usually, .toc comes just after .got. Since we place 8050b57cec5SDimitry Andric // .got into RELRO, .toc needs to be placed into RELRO too. 8060b57cec5SDimitry Andric if (sec->name.equals(".toc")) 8070b57cec5SDimitry Andric return true; 8080b57cec5SDimitry Andric 8090b57cec5SDimitry Andric // .got.plt contains pointers to external function symbols. They are 8100b57cec5SDimitry Andric // by default resolved lazily, so we usually cannot put it into RELRO. 8110b57cec5SDimitry Andric // However, if "-z now" is given, the lazy symbol resolution is 8120b57cec5SDimitry Andric // disabled, which enables us to put it into RELRO. 8130b57cec5SDimitry Andric if (sec == in.gotPlt->getParent()) 8140b57cec5SDimitry Andric return config->zNow; 8150b57cec5SDimitry Andric 8160b57cec5SDimitry Andric // .dynamic section contains data for the dynamic linker, and 8170b57cec5SDimitry Andric // there's no need to write to it at runtime, so it's better to put 8180b57cec5SDimitry Andric // it into RELRO. 8190b57cec5SDimitry Andric if (sec->name == ".dynamic") 8200b57cec5SDimitry Andric return true; 8210b57cec5SDimitry Andric 8220b57cec5SDimitry Andric // Sections with some special names are put into RELRO. This is a 8230b57cec5SDimitry Andric // bit unfortunate because section names shouldn't be significant in 8240b57cec5SDimitry Andric // ELF in spirit. But in reality many linker features depend on 8250b57cec5SDimitry Andric // magic section names. 8260b57cec5SDimitry Andric StringRef s = sec->name; 8270b57cec5SDimitry Andric return s == ".data.rel.ro" || s == ".bss.rel.ro" || s == ".ctors" || 8280b57cec5SDimitry Andric s == ".dtors" || s == ".jcr" || s == ".eh_frame" || 8295ffd83dbSDimitry Andric s == ".fini_array" || s == ".init_array" || 8305ffd83dbSDimitry Andric s == ".openbsd.randomdata" || s == ".preinit_array"; 8310b57cec5SDimitry Andric } 8320b57cec5SDimitry Andric 8330b57cec5SDimitry Andric // We compute a rank for each section. The rank indicates where the 8340b57cec5SDimitry Andric // section should be placed in the file. Instead of using simple 8350b57cec5SDimitry Andric // numbers (0,1,2...), we use a series of flags. One for each decision 8360b57cec5SDimitry Andric // point when placing the section. 8370b57cec5SDimitry Andric // Using flags has two key properties: 8380b57cec5SDimitry Andric // * It is easy to check if a give branch was taken. 8390b57cec5SDimitry Andric // * It is easy two see how similar two ranks are (see getRankProximity). 8400b57cec5SDimitry Andric enum RankFlags { 8410b57cec5SDimitry Andric RF_NOT_ADDR_SET = 1 << 27, 8420b57cec5SDimitry Andric RF_NOT_ALLOC = 1 << 26, 8430b57cec5SDimitry Andric RF_PARTITION = 1 << 18, // Partition number (8 bits) 8440b57cec5SDimitry Andric RF_NOT_PART_EHDR = 1 << 17, 8450b57cec5SDimitry Andric RF_NOT_PART_PHDR = 1 << 16, 8460b57cec5SDimitry Andric RF_NOT_INTERP = 1 << 15, 8470b57cec5SDimitry Andric RF_NOT_NOTE = 1 << 14, 8480b57cec5SDimitry Andric RF_WRITE = 1 << 13, 8490b57cec5SDimitry Andric RF_EXEC_WRITE = 1 << 12, 8500b57cec5SDimitry Andric RF_EXEC = 1 << 11, 8510b57cec5SDimitry Andric RF_RODATA = 1 << 10, 8520b57cec5SDimitry Andric RF_NOT_RELRO = 1 << 9, 8530b57cec5SDimitry Andric RF_NOT_TLS = 1 << 8, 8540b57cec5SDimitry Andric RF_BSS = 1 << 7, 8550b57cec5SDimitry Andric RF_PPC_NOT_TOCBSS = 1 << 6, 8560b57cec5SDimitry Andric RF_PPC_TOCL = 1 << 5, 8570b57cec5SDimitry Andric RF_PPC_TOC = 1 << 4, 8580b57cec5SDimitry Andric RF_PPC_GOT = 1 << 3, 8590b57cec5SDimitry Andric RF_PPC_BRANCH_LT = 1 << 2, 8600b57cec5SDimitry Andric RF_MIPS_GPREL = 1 << 1, 8610b57cec5SDimitry Andric RF_MIPS_NOT_GOT = 1 << 0 8620b57cec5SDimitry Andric }; 8630b57cec5SDimitry Andric 86481ad6265SDimitry Andric static unsigned getSectionRank(const OutputSection &osec) { 86581ad6265SDimitry Andric unsigned rank = osec.partition * RF_PARTITION; 8660b57cec5SDimitry Andric 8670b57cec5SDimitry Andric // We want to put section specified by -T option first, so we 8680b57cec5SDimitry Andric // can start assigning VA starting from them later. 86981ad6265SDimitry Andric if (config->sectionStartMap.count(osec.name)) 8700b57cec5SDimitry Andric return rank; 8710b57cec5SDimitry Andric rank |= RF_NOT_ADDR_SET; 8720b57cec5SDimitry Andric 8730b57cec5SDimitry Andric // Allocatable sections go first to reduce the total PT_LOAD size and 8740b57cec5SDimitry Andric // so debug info doesn't change addresses in actual code. 87581ad6265SDimitry Andric if (!(osec.flags & SHF_ALLOC)) 8760b57cec5SDimitry Andric return rank | RF_NOT_ALLOC; 8770b57cec5SDimitry Andric 87881ad6265SDimitry Andric if (osec.type == SHT_LLVM_PART_EHDR) 8790b57cec5SDimitry Andric return rank; 8800b57cec5SDimitry Andric rank |= RF_NOT_PART_EHDR; 8810b57cec5SDimitry Andric 88281ad6265SDimitry Andric if (osec.type == SHT_LLVM_PART_PHDR) 8830b57cec5SDimitry Andric return rank; 8840b57cec5SDimitry Andric rank |= RF_NOT_PART_PHDR; 8850b57cec5SDimitry Andric 8860b57cec5SDimitry Andric // Put .interp first because some loaders want to see that section 8870b57cec5SDimitry Andric // on the first page of the executable file when loaded into memory. 88881ad6265SDimitry Andric if (osec.name == ".interp") 8890b57cec5SDimitry Andric return rank; 8900b57cec5SDimitry Andric rank |= RF_NOT_INTERP; 8910b57cec5SDimitry Andric 8920b57cec5SDimitry Andric // Put .note sections (which make up one PT_NOTE) at the beginning so that 8930b57cec5SDimitry Andric // they are likely to be included in a core file even if core file size is 8940b57cec5SDimitry Andric // limited. In particular, we want a .note.gnu.build-id and a .note.tag to be 8950b57cec5SDimitry Andric // included in a core to match core files with executables. 89681ad6265SDimitry Andric if (osec.type == SHT_NOTE) 8970b57cec5SDimitry Andric return rank; 8980b57cec5SDimitry Andric rank |= RF_NOT_NOTE; 8990b57cec5SDimitry Andric 9000b57cec5SDimitry Andric // Sort sections based on their access permission in the following 9010b57cec5SDimitry Andric // order: R, RX, RWX, RW. This order is based on the following 9020b57cec5SDimitry Andric // considerations: 9030b57cec5SDimitry Andric // * Read-only sections come first such that they go in the 9040b57cec5SDimitry Andric // PT_LOAD covering the program headers at the start of the file. 9050b57cec5SDimitry Andric // * Read-only, executable sections come next. 9060b57cec5SDimitry Andric // * Writable, executable sections follow such that .plt on 9070b57cec5SDimitry Andric // architectures where it needs to be writable will be placed 9080b57cec5SDimitry Andric // between .text and .data. 9090b57cec5SDimitry Andric // * Writable sections come last, such that .bss lands at the very 9100b57cec5SDimitry Andric // end of the last PT_LOAD. 91181ad6265SDimitry Andric bool isExec = osec.flags & SHF_EXECINSTR; 91281ad6265SDimitry Andric bool isWrite = osec.flags & SHF_WRITE; 9130b57cec5SDimitry Andric 9140b57cec5SDimitry Andric if (isExec) { 9150b57cec5SDimitry Andric if (isWrite) 9160b57cec5SDimitry Andric rank |= RF_EXEC_WRITE; 9170b57cec5SDimitry Andric else 9180b57cec5SDimitry Andric rank |= RF_EXEC; 9190b57cec5SDimitry Andric } else if (isWrite) { 9200b57cec5SDimitry Andric rank |= RF_WRITE; 92181ad6265SDimitry Andric } else if (osec.type == SHT_PROGBITS) { 9220b57cec5SDimitry Andric // Make non-executable and non-writable PROGBITS sections (e.g .rodata 9230b57cec5SDimitry Andric // .eh_frame) closer to .text. They likely contain PC or GOT relative 9240b57cec5SDimitry Andric // relocations and there could be relocation overflow if other huge sections 9250b57cec5SDimitry Andric // (.dynstr .dynsym) were placed in between. 9260b57cec5SDimitry Andric rank |= RF_RODATA; 9270b57cec5SDimitry Andric } 9280b57cec5SDimitry Andric 9290b57cec5SDimitry Andric // Place RelRo sections first. After considering SHT_NOBITS below, the 9300b57cec5SDimitry Andric // ordering is PT_LOAD(PT_GNU_RELRO(.data.rel.ro .bss.rel.ro) | .data .bss), 9310b57cec5SDimitry Andric // where | marks where page alignment happens. An alternative ordering is 9320b57cec5SDimitry Andric // PT_LOAD(.data | PT_GNU_RELRO( .data.rel.ro .bss.rel.ro) | .bss), but it may 9330b57cec5SDimitry Andric // waste more bytes due to 2 alignment places. 93481ad6265SDimitry Andric if (!isRelroSection(&osec)) 9350b57cec5SDimitry Andric rank |= RF_NOT_RELRO; 9360b57cec5SDimitry Andric 9370b57cec5SDimitry Andric // If we got here we know that both A and B are in the same PT_LOAD. 9380b57cec5SDimitry Andric 9390b57cec5SDimitry Andric // The TLS initialization block needs to be a single contiguous block in a R/W 9400b57cec5SDimitry Andric // PT_LOAD, so stick TLS sections directly before the other RelRo R/W 9410b57cec5SDimitry Andric // sections. Since p_filesz can be less than p_memsz, place NOBITS sections 9420b57cec5SDimitry Andric // after PROGBITS. 94381ad6265SDimitry Andric if (!(osec.flags & SHF_TLS)) 9440b57cec5SDimitry Andric rank |= RF_NOT_TLS; 9450b57cec5SDimitry Andric 9460b57cec5SDimitry Andric // Within TLS sections, or within other RelRo sections, or within non-RelRo 9470b57cec5SDimitry Andric // sections, place non-NOBITS sections first. 94881ad6265SDimitry Andric if (osec.type == SHT_NOBITS) 9490b57cec5SDimitry Andric rank |= RF_BSS; 9500b57cec5SDimitry Andric 9510b57cec5SDimitry Andric // Some architectures have additional ordering restrictions for sections 9520b57cec5SDimitry Andric // within the same PT_LOAD. 9530b57cec5SDimitry Andric if (config->emachine == EM_PPC64) { 9540b57cec5SDimitry Andric // PPC64 has a number of special SHT_PROGBITS+SHF_ALLOC+SHF_WRITE sections 9550b57cec5SDimitry Andric // that we would like to make sure appear is a specific order to maximize 9560b57cec5SDimitry Andric // their coverage by a single signed 16-bit offset from the TOC base 9570b57cec5SDimitry Andric // pointer. Conversely, the special .tocbss section should be first among 9580b57cec5SDimitry Andric // all SHT_NOBITS sections. This will put it next to the loaded special 9590b57cec5SDimitry Andric // PPC64 sections (and, thus, within reach of the TOC base pointer). 96081ad6265SDimitry Andric StringRef name = osec.name; 9610b57cec5SDimitry Andric if (name != ".tocbss") 9620b57cec5SDimitry Andric rank |= RF_PPC_NOT_TOCBSS; 9630b57cec5SDimitry Andric 9640b57cec5SDimitry Andric if (name == ".toc1") 9650b57cec5SDimitry Andric rank |= RF_PPC_TOCL; 9660b57cec5SDimitry Andric 9670b57cec5SDimitry Andric if (name == ".toc") 9680b57cec5SDimitry Andric rank |= RF_PPC_TOC; 9690b57cec5SDimitry Andric 9700b57cec5SDimitry Andric if (name == ".got") 9710b57cec5SDimitry Andric rank |= RF_PPC_GOT; 9720b57cec5SDimitry Andric 9730b57cec5SDimitry Andric if (name == ".branch_lt") 9740b57cec5SDimitry Andric rank |= RF_PPC_BRANCH_LT; 9750b57cec5SDimitry Andric } 9760b57cec5SDimitry Andric 9770b57cec5SDimitry Andric if (config->emachine == EM_MIPS) { 9780b57cec5SDimitry Andric // All sections with SHF_MIPS_GPREL flag should be grouped together 9790b57cec5SDimitry Andric // because data in these sections is addressable with a gp relative address. 98081ad6265SDimitry Andric if (osec.flags & SHF_MIPS_GPREL) 9810b57cec5SDimitry Andric rank |= RF_MIPS_GPREL; 9820b57cec5SDimitry Andric 98381ad6265SDimitry Andric if (osec.name != ".got") 9840b57cec5SDimitry Andric rank |= RF_MIPS_NOT_GOT; 9850b57cec5SDimitry Andric } 9860b57cec5SDimitry Andric 9870b57cec5SDimitry Andric return rank; 9880b57cec5SDimitry Andric } 9890b57cec5SDimitry Andric 9904824e7fdSDimitry Andric static bool compareSections(const SectionCommand *aCmd, 9914824e7fdSDimitry Andric const SectionCommand *bCmd) { 99281ad6265SDimitry Andric const OutputSection *a = &cast<OutputDesc>(aCmd)->osec; 99381ad6265SDimitry Andric const OutputSection *b = &cast<OutputDesc>(bCmd)->osec; 9940b57cec5SDimitry Andric 9950b57cec5SDimitry Andric if (a->sortRank != b->sortRank) 9960b57cec5SDimitry Andric return a->sortRank < b->sortRank; 9970b57cec5SDimitry Andric 9980b57cec5SDimitry Andric if (!(a->sortRank & RF_NOT_ADDR_SET)) 9990b57cec5SDimitry Andric return config->sectionStartMap.lookup(a->name) < 10000b57cec5SDimitry Andric config->sectionStartMap.lookup(b->name); 10010b57cec5SDimitry Andric return false; 10020b57cec5SDimitry Andric } 10030b57cec5SDimitry Andric 10040b57cec5SDimitry Andric void PhdrEntry::add(OutputSection *sec) { 10050b57cec5SDimitry Andric lastSec = sec; 10060b57cec5SDimitry Andric if (!firstSec) 10070b57cec5SDimitry Andric firstSec = sec; 10080b57cec5SDimitry Andric p_align = std::max(p_align, sec->alignment); 10090b57cec5SDimitry Andric if (p_type == PT_LOAD) 10100b57cec5SDimitry Andric sec->ptLoad = this; 10110b57cec5SDimitry Andric } 10120b57cec5SDimitry Andric 10130b57cec5SDimitry Andric // The beginning and the ending of .rel[a].plt section are marked 10140b57cec5SDimitry Andric // with __rel[a]_iplt_{start,end} symbols if it is a statically linked 10150b57cec5SDimitry Andric // executable. The runtime needs these symbols in order to resolve 10160b57cec5SDimitry Andric // all IRELATIVE relocs on startup. For dynamic executables, we don't 10170b57cec5SDimitry Andric // need these symbols, since IRELATIVE relocs are resolved through GOT 10180b57cec5SDimitry Andric // and PLT. For details, see http://www.airs.com/blog/archives/403. 10190b57cec5SDimitry Andric template <class ELFT> void Writer<ELFT>::addRelIpltSymbols() { 1020fe6060f1SDimitry Andric if (config->relocatable || config->isPic) 10210b57cec5SDimitry Andric return; 10220b57cec5SDimitry Andric 10230b57cec5SDimitry Andric // By default, __rela_iplt_{start,end} belong to a dummy section 0 10240b57cec5SDimitry Andric // because .rela.plt might be empty and thus removed from output. 10250b57cec5SDimitry Andric // We'll override Out::elfHeader with In.relaIplt later when we are 10260b57cec5SDimitry Andric // sure that .rela.plt exists in output. 10270b57cec5SDimitry Andric ElfSym::relaIpltStart = addOptionalRegular( 10280b57cec5SDimitry Andric config->isRela ? "__rela_iplt_start" : "__rel_iplt_start", 1029349cc55cSDimitry Andric Out::elfHeader, 0, STV_HIDDEN); 10300b57cec5SDimitry Andric 10310b57cec5SDimitry Andric ElfSym::relaIpltEnd = addOptionalRegular( 10320b57cec5SDimitry Andric config->isRela ? "__rela_iplt_end" : "__rel_iplt_end", 1033349cc55cSDimitry Andric Out::elfHeader, 0, STV_HIDDEN); 10340b57cec5SDimitry Andric } 10350b57cec5SDimitry Andric 10360b57cec5SDimitry Andric // This function generates assignments for predefined symbols (e.g. _end or 10370b57cec5SDimitry Andric // _etext) and inserts them into the commands sequence to be processed at the 10380b57cec5SDimitry Andric // appropriate time. This ensures that the value is going to be correct by the 10390b57cec5SDimitry Andric // time any references to these symbols are processed and is equivalent to 10400b57cec5SDimitry Andric // defining these symbols explicitly in the linker script. 10410b57cec5SDimitry Andric template <class ELFT> void Writer<ELFT>::setReservedSymbolSections() { 10420b57cec5SDimitry Andric if (ElfSym::globalOffsetTable) { 10430b57cec5SDimitry Andric // The _GLOBAL_OFFSET_TABLE_ symbol is defined by target convention usually 10440b57cec5SDimitry Andric // to the start of the .got or .got.plt section. 104504eeddc0SDimitry Andric InputSection *sec = in.gotPlt.get(); 10460b57cec5SDimitry Andric if (!target->gotBaseSymInGotPlt) 104704eeddc0SDimitry Andric sec = in.mipsGot.get() ? cast<InputSection>(in.mipsGot.get()) 104804eeddc0SDimitry Andric : cast<InputSection>(in.got.get()); 104904eeddc0SDimitry Andric ElfSym::globalOffsetTable->section = sec; 10500b57cec5SDimitry Andric } 10510b57cec5SDimitry Andric 105285868e8aSDimitry Andric // .rela_iplt_{start,end} mark the start and the end of in.relaIplt. 10530b57cec5SDimitry Andric if (ElfSym::relaIpltStart && in.relaIplt->isNeeded()) { 105404eeddc0SDimitry Andric ElfSym::relaIpltStart->section = in.relaIplt.get(); 105504eeddc0SDimitry Andric ElfSym::relaIpltEnd->section = in.relaIplt.get(); 10560b57cec5SDimitry Andric ElfSym::relaIpltEnd->value = in.relaIplt->getSize(); 10570b57cec5SDimitry Andric } 10580b57cec5SDimitry Andric 10590b57cec5SDimitry Andric PhdrEntry *last = nullptr; 10600b57cec5SDimitry Andric PhdrEntry *lastRO = nullptr; 10610b57cec5SDimitry Andric 10620b57cec5SDimitry Andric for (Partition &part : partitions) { 10630b57cec5SDimitry Andric for (PhdrEntry *p : part.phdrs) { 10640b57cec5SDimitry Andric if (p->p_type != PT_LOAD) 10650b57cec5SDimitry Andric continue; 10660b57cec5SDimitry Andric last = p; 10670b57cec5SDimitry Andric if (!(p->p_flags & PF_W)) 10680b57cec5SDimitry Andric lastRO = p; 10690b57cec5SDimitry Andric } 10700b57cec5SDimitry Andric } 10710b57cec5SDimitry Andric 10720b57cec5SDimitry Andric if (lastRO) { 10730b57cec5SDimitry Andric // _etext is the first location after the last read-only loadable segment. 10740b57cec5SDimitry Andric if (ElfSym::etext1) 10750b57cec5SDimitry Andric ElfSym::etext1->section = lastRO->lastSec; 10760b57cec5SDimitry Andric if (ElfSym::etext2) 10770b57cec5SDimitry Andric ElfSym::etext2->section = lastRO->lastSec; 10780b57cec5SDimitry Andric } 10790b57cec5SDimitry Andric 10800b57cec5SDimitry Andric if (last) { 10810b57cec5SDimitry Andric // _edata points to the end of the last mapped initialized section. 10820b57cec5SDimitry Andric OutputSection *edata = nullptr; 10830b57cec5SDimitry Andric for (OutputSection *os : outputSections) { 10840b57cec5SDimitry Andric if (os->type != SHT_NOBITS) 10850b57cec5SDimitry Andric edata = os; 10860b57cec5SDimitry Andric if (os == last->lastSec) 10870b57cec5SDimitry Andric break; 10880b57cec5SDimitry Andric } 10890b57cec5SDimitry Andric 10900b57cec5SDimitry Andric if (ElfSym::edata1) 10910b57cec5SDimitry Andric ElfSym::edata1->section = edata; 10920b57cec5SDimitry Andric if (ElfSym::edata2) 10930b57cec5SDimitry Andric ElfSym::edata2->section = edata; 10940b57cec5SDimitry Andric 10950b57cec5SDimitry Andric // _end is the first location after the uninitialized data region. 10960b57cec5SDimitry Andric if (ElfSym::end1) 10970b57cec5SDimitry Andric ElfSym::end1->section = last->lastSec; 10980b57cec5SDimitry Andric if (ElfSym::end2) 10990b57cec5SDimitry Andric ElfSym::end2->section = last->lastSec; 11000b57cec5SDimitry Andric } 11010b57cec5SDimitry Andric 11020b57cec5SDimitry Andric if (ElfSym::bss) 11030b57cec5SDimitry Andric ElfSym::bss->section = findSection(".bss"); 11040b57cec5SDimitry Andric 11050b57cec5SDimitry Andric // Setup MIPS _gp_disp/__gnu_local_gp symbols which should 11060b57cec5SDimitry Andric // be equal to the _gp symbol's value. 11070b57cec5SDimitry Andric if (ElfSym::mipsGp) { 11080b57cec5SDimitry Andric // Find GP-relative section with the lowest address 11090b57cec5SDimitry Andric // and use this address to calculate default _gp value. 11100b57cec5SDimitry Andric for (OutputSection *os : outputSections) { 11110b57cec5SDimitry Andric if (os->flags & SHF_MIPS_GPREL) { 11120b57cec5SDimitry Andric ElfSym::mipsGp->section = os; 11130b57cec5SDimitry Andric ElfSym::mipsGp->value = 0x7ff0; 11140b57cec5SDimitry Andric break; 11150b57cec5SDimitry Andric } 11160b57cec5SDimitry Andric } 11170b57cec5SDimitry Andric } 11180b57cec5SDimitry Andric } 11190b57cec5SDimitry Andric 11200b57cec5SDimitry Andric // We want to find how similar two ranks are. 11210b57cec5SDimitry Andric // The more branches in getSectionRank that match, the more similar they are. 11220b57cec5SDimitry Andric // Since each branch corresponds to a bit flag, we can just use 11230b57cec5SDimitry Andric // countLeadingZeros. 112481ad6265SDimitry Andric static int getRankProximityAux(const OutputSection &a, const OutputSection &b) { 112581ad6265SDimitry Andric return countLeadingZeros(a.sortRank ^ b.sortRank); 11260b57cec5SDimitry Andric } 11270b57cec5SDimitry Andric 11284824e7fdSDimitry Andric static int getRankProximity(OutputSection *a, SectionCommand *b) { 112981ad6265SDimitry Andric auto *osd = dyn_cast<OutputDesc>(b); 113081ad6265SDimitry Andric return (osd && osd->osec.hasInputSections) 113181ad6265SDimitry Andric ? getRankProximityAux(*a, osd->osec) 113281ad6265SDimitry Andric : -1; 11330b57cec5SDimitry Andric } 11340b57cec5SDimitry Andric 11350b57cec5SDimitry Andric // When placing orphan sections, we want to place them after symbol assignments 11360b57cec5SDimitry Andric // so that an orphan after 11370b57cec5SDimitry Andric // begin_foo = .; 11380b57cec5SDimitry Andric // foo : { *(foo) } 11390b57cec5SDimitry Andric // end_foo = .; 11400b57cec5SDimitry Andric // doesn't break the intended meaning of the begin/end symbols. 11410b57cec5SDimitry Andric // We don't want to go over sections since findOrphanPos is the 11420b57cec5SDimitry Andric // one in charge of deciding the order of the sections. 11430b57cec5SDimitry Andric // We don't want to go over changes to '.', since doing so in 11440b57cec5SDimitry Andric // rx_sec : { *(rx_sec) } 11450b57cec5SDimitry Andric // . = ALIGN(0x1000); 11460b57cec5SDimitry Andric // /* The RW PT_LOAD starts here*/ 11470b57cec5SDimitry Andric // rw_sec : { *(rw_sec) } 11480b57cec5SDimitry Andric // would mean that the RW PT_LOAD would become unaligned. 11494824e7fdSDimitry Andric static bool shouldSkip(SectionCommand *cmd) { 11500b57cec5SDimitry Andric if (auto *assign = dyn_cast<SymbolAssignment>(cmd)) 11510b57cec5SDimitry Andric return assign->name != "."; 11520b57cec5SDimitry Andric return false; 11530b57cec5SDimitry Andric } 11540b57cec5SDimitry Andric 11550b57cec5SDimitry Andric // We want to place orphan sections so that they share as much 11560b57cec5SDimitry Andric // characteristics with their neighbors as possible. For example, if 11570b57cec5SDimitry Andric // both are rw, or both are tls. 115804eeddc0SDimitry Andric static SmallVectorImpl<SectionCommand *>::iterator 115904eeddc0SDimitry Andric findOrphanPos(SmallVectorImpl<SectionCommand *>::iterator b, 116004eeddc0SDimitry Andric SmallVectorImpl<SectionCommand *>::iterator e) { 116181ad6265SDimitry Andric OutputSection *sec = &cast<OutputDesc>(*e)->osec; 11620b57cec5SDimitry Andric 11630b57cec5SDimitry Andric // Find the first element that has as close a rank as possible. 11644824e7fdSDimitry Andric auto i = std::max_element(b, e, [=](SectionCommand *a, SectionCommand *b) { 11650b57cec5SDimitry Andric return getRankProximity(sec, a) < getRankProximity(sec, b); 11660b57cec5SDimitry Andric }); 11670b57cec5SDimitry Andric if (i == e) 11680b57cec5SDimitry Andric return e; 116981ad6265SDimitry Andric if (!isa<OutputDesc>(*i)) 1170349cc55cSDimitry Andric return e; 117181ad6265SDimitry Andric auto foundSec = &cast<OutputDesc>(*i)->osec; 11720b57cec5SDimitry Andric 11730b57cec5SDimitry Andric // Consider all existing sections with the same proximity. 11740b57cec5SDimitry Andric int proximity = getRankProximity(sec, *i); 1175349cc55cSDimitry Andric unsigned sortRank = sec->sortRank; 1176349cc55cSDimitry Andric if (script->hasPhdrsCommands() || !script->memoryRegions.empty()) 1177349cc55cSDimitry Andric // Prevent the orphan section to be placed before the found section. If 1178349cc55cSDimitry Andric // custom program headers are defined, that helps to avoid adding it to a 1179349cc55cSDimitry Andric // previous segment and changing flags of that segment, for example, making 1180349cc55cSDimitry Andric // a read-only segment writable. If memory regions are defined, an orphan 1181349cc55cSDimitry Andric // section should continue the same region as the found section to better 1182349cc55cSDimitry Andric // resemble the behavior of GNU ld. 1183349cc55cSDimitry Andric sortRank = std::max(sortRank, foundSec->sortRank); 11840b57cec5SDimitry Andric for (; i != e; ++i) { 118581ad6265SDimitry Andric auto *curSecDesc = dyn_cast<OutputDesc>(*i); 118681ad6265SDimitry Andric if (!curSecDesc || !curSecDesc->osec.hasInputSections) 11870b57cec5SDimitry Andric continue; 118881ad6265SDimitry Andric if (getRankProximity(sec, curSecDesc) != proximity || 118981ad6265SDimitry Andric sortRank < curSecDesc->osec.sortRank) 11900b57cec5SDimitry Andric break; 11910b57cec5SDimitry Andric } 11920b57cec5SDimitry Andric 11934824e7fdSDimitry Andric auto isOutputSecWithInputSections = [](SectionCommand *cmd) { 119481ad6265SDimitry Andric auto *osd = dyn_cast<OutputDesc>(cmd); 119581ad6265SDimitry Andric return osd && osd->osec.hasInputSections; 11960b57cec5SDimitry Andric }; 119704eeddc0SDimitry Andric auto j = 119804eeddc0SDimitry Andric std::find_if(std::make_reverse_iterator(i), std::make_reverse_iterator(b), 11990b57cec5SDimitry Andric isOutputSecWithInputSections); 12000b57cec5SDimitry Andric i = j.base(); 12010b57cec5SDimitry Andric 12020b57cec5SDimitry Andric // As a special case, if the orphan section is the last section, put 12030b57cec5SDimitry Andric // it at the very end, past any other commands. 12040b57cec5SDimitry Andric // This matches bfd's behavior and is convenient when the linker script fully 12050b57cec5SDimitry Andric // specifies the start of the file, but doesn't care about the end (the non 12060b57cec5SDimitry Andric // alloc sections for example). 12070b57cec5SDimitry Andric auto nextSec = std::find_if(i, e, isOutputSecWithInputSections); 12080b57cec5SDimitry Andric if (nextSec == e) 12090b57cec5SDimitry Andric return e; 12100b57cec5SDimitry Andric 12110b57cec5SDimitry Andric while (i != e && shouldSkip(*i)) 12120b57cec5SDimitry Andric ++i; 12130b57cec5SDimitry Andric return i; 12140b57cec5SDimitry Andric } 12150b57cec5SDimitry Andric 12165ffd83dbSDimitry Andric // Adds random priorities to sections not already in the map. 12175ffd83dbSDimitry Andric static void maybeShuffle(DenseMap<const InputSectionBase *, int> &order) { 1218fe6060f1SDimitry Andric if (config->shuffleSections.empty()) 12195ffd83dbSDimitry Andric return; 12205ffd83dbSDimitry Andric 12210eae32dcSDimitry Andric SmallVector<InputSectionBase *, 0> matched, sections = inputSections; 1222fe6060f1SDimitry Andric matched.reserve(sections.size()); 1223fe6060f1SDimitry Andric for (const auto &patAndSeed : config->shuffleSections) { 1224fe6060f1SDimitry Andric matched.clear(); 1225fe6060f1SDimitry Andric for (InputSectionBase *sec : sections) 1226fe6060f1SDimitry Andric if (patAndSeed.first.match(sec->name)) 1227fe6060f1SDimitry Andric matched.push_back(sec); 1228fe6060f1SDimitry Andric const uint32_t seed = patAndSeed.second; 1229fe6060f1SDimitry Andric if (seed == UINT32_MAX) { 1230fe6060f1SDimitry Andric // If --shuffle-sections <section-glob>=-1, reverse the section order. The 1231fe6060f1SDimitry Andric // section order is stable even if the number of sections changes. This is 1232fe6060f1SDimitry Andric // useful to catch issues like static initialization order fiasco 1233fe6060f1SDimitry Andric // reliably. 1234fe6060f1SDimitry Andric std::reverse(matched.begin(), matched.end()); 1235fe6060f1SDimitry Andric } else { 1236fe6060f1SDimitry Andric std::mt19937 g(seed ? seed : std::random_device()()); 1237fe6060f1SDimitry Andric llvm::shuffle(matched.begin(), matched.end(), g); 1238fe6060f1SDimitry Andric } 1239fe6060f1SDimitry Andric size_t i = 0; 1240fe6060f1SDimitry Andric for (InputSectionBase *&sec : sections) 1241fe6060f1SDimitry Andric if (patAndSeed.first.match(sec->name)) 1242fe6060f1SDimitry Andric sec = matched[i++]; 1243fe6060f1SDimitry Andric } 1244fe6060f1SDimitry Andric 12455ffd83dbSDimitry Andric // Existing priorities are < 0, so use priorities >= 0 for the missing 12465ffd83dbSDimitry Andric // sections. 1247fe6060f1SDimitry Andric int prio = 0; 1248fe6060f1SDimitry Andric for (InputSectionBase *sec : sections) { 1249fe6060f1SDimitry Andric if (order.try_emplace(sec, prio).second) 1250fe6060f1SDimitry Andric ++prio; 12515ffd83dbSDimitry Andric } 12525ffd83dbSDimitry Andric } 12535ffd83dbSDimitry Andric 12540b57cec5SDimitry Andric // Builds section order for handling --symbol-ordering-file. 12550b57cec5SDimitry Andric static DenseMap<const InputSectionBase *, int> buildSectionOrder() { 12560b57cec5SDimitry Andric DenseMap<const InputSectionBase *, int> sectionOrder; 1257349cc55cSDimitry Andric // Use the rarely used option --call-graph-ordering-file to sort sections. 12580b57cec5SDimitry Andric if (!config->callGraphProfile.empty()) 12590b57cec5SDimitry Andric return computeCallGraphProfileOrder(); 12600b57cec5SDimitry Andric 12610b57cec5SDimitry Andric if (config->symbolOrderingFile.empty()) 12620b57cec5SDimitry Andric return sectionOrder; 12630b57cec5SDimitry Andric 12640b57cec5SDimitry Andric struct SymbolOrderEntry { 12650b57cec5SDimitry Andric int priority; 12660b57cec5SDimitry Andric bool present; 12670b57cec5SDimitry Andric }; 12680b57cec5SDimitry Andric 12690b57cec5SDimitry Andric // Build a map from symbols to their priorities. Symbols that didn't 12700b57cec5SDimitry Andric // appear in the symbol ordering file have the lowest priority 0. 12710b57cec5SDimitry Andric // All explicitly mentioned symbols have negative (higher) priorities. 127204eeddc0SDimitry Andric DenseMap<CachedHashStringRef, SymbolOrderEntry> symbolOrder; 12730b57cec5SDimitry Andric int priority = -config->symbolOrderingFile.size(); 12740b57cec5SDimitry Andric for (StringRef s : config->symbolOrderingFile) 127504eeddc0SDimitry Andric symbolOrder.insert({CachedHashStringRef(s), {priority++, false}}); 12760b57cec5SDimitry Andric 12770b57cec5SDimitry Andric // Build a map from sections to their priorities. 12780b57cec5SDimitry Andric auto addSym = [&](Symbol &sym) { 127904eeddc0SDimitry Andric auto it = symbolOrder.find(CachedHashStringRef(sym.getName())); 12800b57cec5SDimitry Andric if (it == symbolOrder.end()) 12810b57cec5SDimitry Andric return; 12820b57cec5SDimitry Andric SymbolOrderEntry &ent = it->second; 12830b57cec5SDimitry Andric ent.present = true; 12840b57cec5SDimitry Andric 12850b57cec5SDimitry Andric maybeWarnUnorderableSymbol(&sym); 12860b57cec5SDimitry Andric 12870b57cec5SDimitry Andric if (auto *d = dyn_cast<Defined>(&sym)) { 12880b57cec5SDimitry Andric if (auto *sec = dyn_cast_or_null<InputSectionBase>(d->section)) { 12890eae32dcSDimitry Andric int &priority = sectionOrder[cast<InputSectionBase>(sec)]; 12900b57cec5SDimitry Andric priority = std::min(priority, ent.priority); 12910b57cec5SDimitry Andric } 12920b57cec5SDimitry Andric } 12930b57cec5SDimitry Andric }; 12940b57cec5SDimitry Andric 12950b57cec5SDimitry Andric // We want both global and local symbols. We get the global ones from the 12960b57cec5SDimitry Andric // symbol table and iterate the object files for the local ones. 1297480093f4SDimitry Andric for (Symbol *sym : symtab->symbols()) 12980b57cec5SDimitry Andric addSym(*sym); 12990b57cec5SDimitry Andric 130081ad6265SDimitry Andric for (ELFFileBase *file : ctx->objectFiles) 130104eeddc0SDimitry Andric for (Symbol *sym : file->getLocalSymbols()) 13020b57cec5SDimitry Andric addSym(*sym); 13030b57cec5SDimitry Andric 13040b57cec5SDimitry Andric if (config->warnSymbolOrdering) 13050b57cec5SDimitry Andric for (auto orderEntry : symbolOrder) 13060b57cec5SDimitry Andric if (!orderEntry.second.present) 130704eeddc0SDimitry Andric warn("symbol ordering file: no such symbol: " + orderEntry.first.val()); 13080b57cec5SDimitry Andric 13090b57cec5SDimitry Andric return sectionOrder; 13100b57cec5SDimitry Andric } 13110b57cec5SDimitry Andric 13120b57cec5SDimitry Andric // Sorts the sections in ISD according to the provided section order. 13130b57cec5SDimitry Andric static void 13140b57cec5SDimitry Andric sortISDBySectionOrder(InputSectionDescription *isd, 1315753f127fSDimitry Andric const DenseMap<const InputSectionBase *, int> &order, 1316753f127fSDimitry Andric bool executableOutputSection) { 131704eeddc0SDimitry Andric SmallVector<InputSection *, 0> unorderedSections; 131804eeddc0SDimitry Andric SmallVector<std::pair<InputSection *, int>, 0> orderedSections; 13190b57cec5SDimitry Andric uint64_t unorderedSize = 0; 1320753f127fSDimitry Andric uint64_t totalSize = 0; 13210b57cec5SDimitry Andric 13220b57cec5SDimitry Andric for (InputSection *isec : isd->sections) { 1323753f127fSDimitry Andric if (executableOutputSection) 1324753f127fSDimitry Andric totalSize += isec->getSize(); 13250b57cec5SDimitry Andric auto i = order.find(isec); 13260b57cec5SDimitry Andric if (i == order.end()) { 13270b57cec5SDimitry Andric unorderedSections.push_back(isec); 13280b57cec5SDimitry Andric unorderedSize += isec->getSize(); 13290b57cec5SDimitry Andric continue; 13300b57cec5SDimitry Andric } 13310b57cec5SDimitry Andric orderedSections.push_back({isec, i->second}); 13320b57cec5SDimitry Andric } 133385868e8aSDimitry Andric llvm::sort(orderedSections, llvm::less_second()); 13340b57cec5SDimitry Andric 13350b57cec5SDimitry Andric // Find an insertion point for the ordered section list in the unordered 13360b57cec5SDimitry Andric // section list. On targets with limited-range branches, this is the mid-point 13370b57cec5SDimitry Andric // of the unordered section list. This decreases the likelihood that a range 13380b57cec5SDimitry Andric // extension thunk will be needed to enter or exit the ordered region. If the 13390b57cec5SDimitry Andric // ordered section list is a list of hot functions, we can generally expect 13400b57cec5SDimitry Andric // the ordered functions to be called more often than the unordered functions, 13410b57cec5SDimitry Andric // making it more likely that any particular call will be within range, and 13420b57cec5SDimitry Andric // therefore reducing the number of thunks required. 13430b57cec5SDimitry Andric // 13440b57cec5SDimitry Andric // For example, imagine that you have 8MB of hot code and 32MB of cold code. 13450b57cec5SDimitry Andric // If the layout is: 13460b57cec5SDimitry Andric // 13470b57cec5SDimitry Andric // 8MB hot 13480b57cec5SDimitry Andric // 32MB cold 13490b57cec5SDimitry Andric // 13500b57cec5SDimitry Andric // only the first 8-16MB of the cold code (depending on which hot function it 13510b57cec5SDimitry Andric // is actually calling) can call the hot code without a range extension thunk. 13520b57cec5SDimitry Andric // However, if we use this layout: 13530b57cec5SDimitry Andric // 13540b57cec5SDimitry Andric // 16MB cold 13550b57cec5SDimitry Andric // 8MB hot 13560b57cec5SDimitry Andric // 16MB cold 13570b57cec5SDimitry Andric // 13580b57cec5SDimitry Andric // both the last 8-16MB of the first block of cold code and the first 8-16MB 13590b57cec5SDimitry Andric // of the second block of cold code can call the hot code without a thunk. So 13600b57cec5SDimitry Andric // we effectively double the amount of code that could potentially call into 13610b57cec5SDimitry Andric // the hot code without a thunk. 1362753f127fSDimitry Andric // 1363753f127fSDimitry Andric // The above is not necessary if total size of input sections in this "isd" 1364753f127fSDimitry Andric // is small. Note that we assume all input sections are executable if the 1365753f127fSDimitry Andric // output section is executable (which is not always true but supposed to 1366753f127fSDimitry Andric // cover most cases). 13670b57cec5SDimitry Andric size_t insPt = 0; 1368753f127fSDimitry Andric if (executableOutputSection && !orderedSections.empty() && 1369753f127fSDimitry Andric target->getThunkSectionSpacing() && 1370753f127fSDimitry Andric totalSize >= target->getThunkSectionSpacing()) { 13710b57cec5SDimitry Andric uint64_t unorderedPos = 0; 13720b57cec5SDimitry Andric for (; insPt != unorderedSections.size(); ++insPt) { 13730b57cec5SDimitry Andric unorderedPos += unorderedSections[insPt]->getSize(); 13740b57cec5SDimitry Andric if (unorderedPos > unorderedSize / 2) 13750b57cec5SDimitry Andric break; 13760b57cec5SDimitry Andric } 13770b57cec5SDimitry Andric } 13780b57cec5SDimitry Andric 13790b57cec5SDimitry Andric isd->sections.clear(); 13800b57cec5SDimitry Andric for (InputSection *isec : makeArrayRef(unorderedSections).slice(0, insPt)) 13810b57cec5SDimitry Andric isd->sections.push_back(isec); 13820b57cec5SDimitry Andric for (std::pair<InputSection *, int> p : orderedSections) 13830b57cec5SDimitry Andric isd->sections.push_back(p.first); 13840b57cec5SDimitry Andric for (InputSection *isec : makeArrayRef(unorderedSections).slice(insPt)) 13850b57cec5SDimitry Andric isd->sections.push_back(isec); 13860b57cec5SDimitry Andric } 13870b57cec5SDimitry Andric 138881ad6265SDimitry Andric static void sortSection(OutputSection &osec, 13890b57cec5SDimitry Andric const DenseMap<const InputSectionBase *, int> &order) { 139081ad6265SDimitry Andric StringRef name = osec.name; 13910b57cec5SDimitry Andric 13925ffd83dbSDimitry Andric // Never sort these. 13935ffd83dbSDimitry Andric if (name == ".init" || name == ".fini") 13945ffd83dbSDimitry Andric return; 13955ffd83dbSDimitry Andric 1396e8d8bef9SDimitry Andric // IRelative relocations that usually live in the .rel[a].dyn section should 1397fe6060f1SDimitry Andric // be processed last by the dynamic loader. To achieve that we add synthetic 1398fe6060f1SDimitry Andric // sections in the required order from the beginning so that the in.relaIplt 1399e8d8bef9SDimitry Andric // section is placed last in an output section. Here we just do not apply 1400e8d8bef9SDimitry Andric // sorting for an output section which holds the in.relaIplt section. 140181ad6265SDimitry Andric if (in.relaIplt->getParent() == &osec) 1402e8d8bef9SDimitry Andric return; 1403e8d8bef9SDimitry Andric 14045ffd83dbSDimitry Andric // Sort input sections by priority using the list provided by 14055ffd83dbSDimitry Andric // --symbol-ordering-file or --shuffle-sections=. This is a least significant 14065ffd83dbSDimitry Andric // digit radix sort. The sections may be sorted stably again by a more 14075ffd83dbSDimitry Andric // significant key. 14085ffd83dbSDimitry Andric if (!order.empty()) 140981ad6265SDimitry Andric for (SectionCommand *b : osec.commands) 14105ffd83dbSDimitry Andric if (auto *isd = dyn_cast<InputSectionDescription>(b)) 1411753f127fSDimitry Andric sortISDBySectionOrder(isd, order, osec.flags & SHF_EXECINSTR); 14125ffd83dbSDimitry Andric 1413349cc55cSDimitry Andric if (script->hasSectionsCommand) 1414349cc55cSDimitry Andric return; 1415349cc55cSDimitry Andric 14160b57cec5SDimitry Andric if (name == ".init_array" || name == ".fini_array") { 141781ad6265SDimitry Andric osec.sortInitFini(); 1418349cc55cSDimitry Andric } else if (name == ".ctors" || name == ".dtors") { 141981ad6265SDimitry Andric osec.sortCtorsDtors(); 1420349cc55cSDimitry Andric } else if (config->emachine == EM_PPC64 && name == ".toc") { 14210b57cec5SDimitry Andric // .toc is allocated just after .got and is accessed using GOT-relative 14220b57cec5SDimitry Andric // relocations. Object files compiled with small code model have an 14230b57cec5SDimitry Andric // addressable range of [.got, .got + 0xFFFC] for GOT-relative relocations. 1424349cc55cSDimitry Andric // To reduce the risk of relocation overflow, .toc contents are sorted so 1425349cc55cSDimitry Andric // that sections having smaller relocation offsets are at beginning of .toc 142681ad6265SDimitry Andric assert(osec.commands.size() == 1); 142781ad6265SDimitry Andric auto *isd = cast<InputSectionDescription>(osec.commands[0]); 14280b57cec5SDimitry Andric llvm::stable_sort(isd->sections, 14290b57cec5SDimitry Andric [](const InputSection *a, const InputSection *b) -> bool { 14300b57cec5SDimitry Andric return a->file->ppc64SmallCodeModelTocRelocs && 14310b57cec5SDimitry Andric !b->file->ppc64SmallCodeModelTocRelocs; 14320b57cec5SDimitry Andric }); 14330b57cec5SDimitry Andric } 14340b57cec5SDimitry Andric } 14350b57cec5SDimitry Andric 14360b57cec5SDimitry Andric // If no layout was provided by linker script, we want to apply default 14370b57cec5SDimitry Andric // sorting for special input sections. This also handles --symbol-ordering-file. 14380b57cec5SDimitry Andric template <class ELFT> void Writer<ELFT>::sortInputSections() { 14390b57cec5SDimitry Andric // Build the order once since it is expensive. 14400b57cec5SDimitry Andric DenseMap<const InputSectionBase *, int> order = buildSectionOrder(); 14415ffd83dbSDimitry Andric maybeShuffle(order); 14424824e7fdSDimitry Andric for (SectionCommand *cmd : script->sectionCommands) 144381ad6265SDimitry Andric if (auto *osd = dyn_cast<OutputDesc>(cmd)) 144481ad6265SDimitry Andric sortSection(osd->osec, order); 14450b57cec5SDimitry Andric } 14460b57cec5SDimitry Andric 14470b57cec5SDimitry Andric template <class ELFT> void Writer<ELFT>::sortSections() { 1448e8d8bef9SDimitry Andric llvm::TimeTraceScope timeScope("Sort sections"); 14490b57cec5SDimitry Andric 14500b57cec5SDimitry Andric // Don't sort if using -r. It is not necessary and we want to preserve the 14510b57cec5SDimitry Andric // relative order for SHF_LINK_ORDER sections. 14521fd87a68SDimitry Andric if (config->relocatable) { 14531fd87a68SDimitry Andric script->adjustOutputSections(); 14540b57cec5SDimitry Andric return; 14551fd87a68SDimitry Andric } 14560b57cec5SDimitry Andric 14570b57cec5SDimitry Andric sortInputSections(); 14580b57cec5SDimitry Andric 14591fd87a68SDimitry Andric for (SectionCommand *cmd : script->sectionCommands) 146081ad6265SDimitry Andric if (auto *osd = dyn_cast<OutputDesc>(cmd)) 146181ad6265SDimitry Andric osd->osec.sortRank = getSectionRank(osd->osec); 14620b57cec5SDimitry Andric if (!script->hasSectionsCommand) { 14630b57cec5SDimitry Andric // We know that all the OutputSections are contiguous in this case. 146481ad6265SDimitry Andric auto isSection = [](SectionCommand *cmd) { return isa<OutputDesc>(cmd); }; 14650b57cec5SDimitry Andric std::stable_sort( 14660b57cec5SDimitry Andric llvm::find_if(script->sectionCommands, isSection), 14670b57cec5SDimitry Andric llvm::find_if(llvm::reverse(script->sectionCommands), isSection).base(), 14680b57cec5SDimitry Andric compareSections); 14690b57cec5SDimitry Andric } 14700b57cec5SDimitry Andric 14711fd87a68SDimitry Andric // Process INSERT commands and update output section attributes. From this 14721fd87a68SDimitry Andric // point onwards the order of script->sectionCommands is fixed. 14735ffd83dbSDimitry Andric script->processInsertCommands(); 14741fd87a68SDimitry Andric script->adjustOutputSections(); 14751fd87a68SDimitry Andric 14761fd87a68SDimitry Andric if (!script->hasSectionsCommand) 14771fd87a68SDimitry Andric return; 14785ffd83dbSDimitry Andric 14790b57cec5SDimitry Andric // Orphan sections are sections present in the input files which are 14800b57cec5SDimitry Andric // not explicitly placed into the output file by the linker script. 14810b57cec5SDimitry Andric // 14820b57cec5SDimitry Andric // The sections in the linker script are already in the correct 14830b57cec5SDimitry Andric // order. We have to figuere out where to insert the orphan 14840b57cec5SDimitry Andric // sections. 14850b57cec5SDimitry Andric // 14860b57cec5SDimitry Andric // The order of the sections in the script is arbitrary and may not agree with 14870b57cec5SDimitry Andric // compareSections. This means that we cannot easily define a strict weak 14880b57cec5SDimitry Andric // ordering. To see why, consider a comparison of a section in the script and 14890b57cec5SDimitry Andric // one not in the script. We have a two simple options: 14900b57cec5SDimitry Andric // * Make them equivalent (a is not less than b, and b is not less than a). 14910b57cec5SDimitry Andric // The problem is then that equivalence has to be transitive and we can 14920b57cec5SDimitry Andric // have sections a, b and c with only b in a script and a less than c 14930b57cec5SDimitry Andric // which breaks this property. 14940b57cec5SDimitry Andric // * Use compareSectionsNonScript. Given that the script order doesn't have 14950b57cec5SDimitry Andric // to match, we can end up with sections a, b, c, d where b and c are in the 14960b57cec5SDimitry Andric // script and c is compareSectionsNonScript less than b. In which case d 14970b57cec5SDimitry Andric // can be equivalent to c, a to b and d < a. As a concrete example: 14980b57cec5SDimitry Andric // .a (rx) # not in script 14990b57cec5SDimitry Andric // .b (rx) # in script 15000b57cec5SDimitry Andric // .c (ro) # in script 15010b57cec5SDimitry Andric // .d (ro) # not in script 15020b57cec5SDimitry Andric // 15030b57cec5SDimitry Andric // The way we define an order then is: 15040b57cec5SDimitry Andric // * Sort only the orphan sections. They are in the end right now. 15050b57cec5SDimitry Andric // * Move each orphan section to its preferred position. We try 15060b57cec5SDimitry Andric // to put each section in the last position where it can share 15070b57cec5SDimitry Andric // a PT_LOAD. 15080b57cec5SDimitry Andric // 15090b57cec5SDimitry Andric // There is some ambiguity as to where exactly a new entry should be 15100b57cec5SDimitry Andric // inserted, because Commands contains not only output section 15110b57cec5SDimitry Andric // commands but also other types of commands such as symbol assignment 15120b57cec5SDimitry Andric // expressions. There's no correct answer here due to the lack of the 15130b57cec5SDimitry Andric // formal specification of the linker script. We use heuristics to 15140b57cec5SDimitry Andric // determine whether a new output command should be added before or 15150b57cec5SDimitry Andric // after another commands. For the details, look at shouldSkip 15160b57cec5SDimitry Andric // function. 15170b57cec5SDimitry Andric 15180b57cec5SDimitry Andric auto i = script->sectionCommands.begin(); 15190b57cec5SDimitry Andric auto e = script->sectionCommands.end(); 15204824e7fdSDimitry Andric auto nonScriptI = std::find_if(i, e, [](SectionCommand *cmd) { 152181ad6265SDimitry Andric if (auto *osd = dyn_cast<OutputDesc>(cmd)) 152281ad6265SDimitry Andric return osd->osec.sectionIndex == UINT32_MAX; 15230b57cec5SDimitry Andric return false; 15240b57cec5SDimitry Andric }); 15250b57cec5SDimitry Andric 15260b57cec5SDimitry Andric // Sort the orphan sections. 15270b57cec5SDimitry Andric std::stable_sort(nonScriptI, e, compareSections); 15280b57cec5SDimitry Andric 15290b57cec5SDimitry Andric // As a horrible special case, skip the first . assignment if it is before any 15300b57cec5SDimitry Andric // section. We do this because it is common to set a load address by starting 15310b57cec5SDimitry Andric // the script with ". = 0xabcd" and the expectation is that every section is 15320b57cec5SDimitry Andric // after that. 15330b57cec5SDimitry Andric auto firstSectionOrDotAssignment = 15344824e7fdSDimitry Andric std::find_if(i, e, [](SectionCommand *cmd) { return !shouldSkip(cmd); }); 15350b57cec5SDimitry Andric if (firstSectionOrDotAssignment != e && 15360b57cec5SDimitry Andric isa<SymbolAssignment>(**firstSectionOrDotAssignment)) 15370b57cec5SDimitry Andric ++firstSectionOrDotAssignment; 15380b57cec5SDimitry Andric i = firstSectionOrDotAssignment; 15390b57cec5SDimitry Andric 15400b57cec5SDimitry Andric while (nonScriptI != e) { 15410b57cec5SDimitry Andric auto pos = findOrphanPos(i, nonScriptI); 154281ad6265SDimitry Andric OutputSection *orphan = &cast<OutputDesc>(*nonScriptI)->osec; 15430b57cec5SDimitry Andric 15440b57cec5SDimitry Andric // As an optimization, find all sections with the same sort rank 15450b57cec5SDimitry Andric // and insert them with one rotate. 15460b57cec5SDimitry Andric unsigned rank = orphan->sortRank; 15474824e7fdSDimitry Andric auto end = std::find_if(nonScriptI + 1, e, [=](SectionCommand *cmd) { 154881ad6265SDimitry Andric return cast<OutputDesc>(cmd)->osec.sortRank != rank; 15490b57cec5SDimitry Andric }); 15500b57cec5SDimitry Andric std::rotate(pos, nonScriptI, end); 15510b57cec5SDimitry Andric nonScriptI = end; 15520b57cec5SDimitry Andric } 15530b57cec5SDimitry Andric 15540b57cec5SDimitry Andric script->adjustSectionsAfterSorting(); 15550b57cec5SDimitry Andric } 15560b57cec5SDimitry Andric 15570b57cec5SDimitry Andric static bool compareByFilePosition(InputSection *a, InputSection *b) { 1558e8d8bef9SDimitry Andric InputSection *la = a->flags & SHF_LINK_ORDER ? a->getLinkOrderDep() : nullptr; 1559e8d8bef9SDimitry Andric InputSection *lb = b->flags & SHF_LINK_ORDER ? b->getLinkOrderDep() : nullptr; 1560e8d8bef9SDimitry Andric // SHF_LINK_ORDER sections with non-zero sh_link are ordered before 1561e8d8bef9SDimitry Andric // non-SHF_LINK_ORDER sections and SHF_LINK_ORDER sections with zero sh_link. 1562e8d8bef9SDimitry Andric if (!la || !lb) 1563e8d8bef9SDimitry Andric return la && !lb; 15640b57cec5SDimitry Andric OutputSection *aOut = la->getParent(); 15650b57cec5SDimitry Andric OutputSection *bOut = lb->getParent(); 15660b57cec5SDimitry Andric 15670b57cec5SDimitry Andric if (aOut != bOut) 15685ffd83dbSDimitry Andric return aOut->addr < bOut->addr; 15690b57cec5SDimitry Andric return la->outSecOff < lb->outSecOff; 15700b57cec5SDimitry Andric } 15710b57cec5SDimitry Andric 15720b57cec5SDimitry Andric template <class ELFT> void Writer<ELFT>::resolveShfLinkOrder() { 1573e8d8bef9SDimitry Andric llvm::TimeTraceScope timeScope("Resolve SHF_LINK_ORDER"); 15740b57cec5SDimitry Andric for (OutputSection *sec : outputSections) { 15750b57cec5SDimitry Andric if (!(sec->flags & SHF_LINK_ORDER)) 15760b57cec5SDimitry Andric continue; 15770b57cec5SDimitry Andric 157885868e8aSDimitry Andric // The ARM.exidx section use SHF_LINK_ORDER, but we have consolidated 157985868e8aSDimitry Andric // this processing inside the ARMExidxsyntheticsection::finalizeContents(). 158085868e8aSDimitry Andric if (!config->relocatable && config->emachine == EM_ARM && 158185868e8aSDimitry Andric sec->type == SHT_ARM_EXIDX) 158285868e8aSDimitry Andric continue; 158385868e8aSDimitry Andric 1584e8d8bef9SDimitry Andric // Link order may be distributed across several InputSectionDescriptions. 1585e8d8bef9SDimitry Andric // Sorting is performed separately. 15861fd87a68SDimitry Andric SmallVector<InputSection **, 0> scriptSections; 15871fd87a68SDimitry Andric SmallVector<InputSection *, 0> sections; 15884824e7fdSDimitry Andric for (SectionCommand *cmd : sec->commands) { 15894824e7fdSDimitry Andric auto *isd = dyn_cast<InputSectionDescription>(cmd); 1590e8d8bef9SDimitry Andric if (!isd) 1591e8d8bef9SDimitry Andric continue; 1592e8d8bef9SDimitry Andric bool hasLinkOrder = false; 1593e8d8bef9SDimitry Andric scriptSections.clear(); 1594e8d8bef9SDimitry Andric sections.clear(); 15950b57cec5SDimitry Andric for (InputSection *&isec : isd->sections) { 1596e8d8bef9SDimitry Andric if (isec->flags & SHF_LINK_ORDER) { 159785868e8aSDimitry Andric InputSection *link = isec->getLinkOrderDep(); 1598e8d8bef9SDimitry Andric if (link && !link->getParent()) 159985868e8aSDimitry Andric error(toString(isec) + ": sh_link points to discarded section " + 160085868e8aSDimitry Andric toString(link)); 1601e8d8bef9SDimitry Andric hasLinkOrder = true; 16020b57cec5SDimitry Andric } 1603e8d8bef9SDimitry Andric scriptSections.push_back(&isec); 1604e8d8bef9SDimitry Andric sections.push_back(isec); 16050b57cec5SDimitry Andric } 1606e8d8bef9SDimitry Andric if (hasLinkOrder && errorCount() == 0) { 16070b57cec5SDimitry Andric llvm::stable_sort(sections, compareByFilePosition); 1608e8d8bef9SDimitry Andric for (int i = 0, n = sections.size(); i != n; ++i) 16090b57cec5SDimitry Andric *scriptSections[i] = sections[i]; 16100b57cec5SDimitry Andric } 16110b57cec5SDimitry Andric } 1612e8d8bef9SDimitry Andric } 1613e8d8bef9SDimitry Andric } 16140b57cec5SDimitry Andric 16155ffd83dbSDimitry Andric static void finalizeSynthetic(SyntheticSection *sec) { 1616e8d8bef9SDimitry Andric if (sec && sec->isNeeded() && sec->getParent()) { 1617e8d8bef9SDimitry Andric llvm::TimeTraceScope timeScope("Finalize synthetic sections", sec->name); 16185ffd83dbSDimitry Andric sec->finalizeContents(); 16195ffd83dbSDimitry Andric } 1620e8d8bef9SDimitry Andric } 16215ffd83dbSDimitry Andric 16220b57cec5SDimitry Andric // We need to generate and finalize the content that depends on the address of 16230b57cec5SDimitry Andric // InputSections. As the generation of the content may also alter InputSection 16240b57cec5SDimitry Andric // addresses we must converge to a fixed point. We do that here. See the comment 16250b57cec5SDimitry Andric // in Writer<ELFT>::finalizeSections(). 16260b57cec5SDimitry Andric template <class ELFT> void Writer<ELFT>::finalizeAddressDependentContent() { 1627e8d8bef9SDimitry Andric llvm::TimeTraceScope timeScope("Finalize address dependent content"); 16280b57cec5SDimitry Andric ThunkCreator tc; 16290b57cec5SDimitry Andric AArch64Err843419Patcher a64p; 163085868e8aSDimitry Andric ARMErr657417Patcher a32p; 16310b57cec5SDimitry Andric script->assignAddresses(); 16325ffd83dbSDimitry Andric // .ARM.exidx and SHF_LINK_ORDER do not require precise addresses, but they 16335ffd83dbSDimitry Andric // do require the relative addresses of OutputSections because linker scripts 16345ffd83dbSDimitry Andric // can assign Virtual Addresses to OutputSections that are not monotonically 16355ffd83dbSDimitry Andric // increasing. 16365ffd83dbSDimitry Andric for (Partition &part : partitions) 163704eeddc0SDimitry Andric finalizeSynthetic(part.armExidx.get()); 16385ffd83dbSDimitry Andric resolveShfLinkOrder(); 16395ffd83dbSDimitry Andric 16405ffd83dbSDimitry Andric // Converts call x@GDPLT to call __tls_get_addr 16415ffd83dbSDimitry Andric if (config->emachine == EM_HEXAGON) 16425ffd83dbSDimitry Andric hexagonTLSSymbolUpdate(outputSections); 16430b57cec5SDimitry Andric 1644753f127fSDimitry Andric uint32_t pass = 0, assignPasses = 0; 164585868e8aSDimitry Andric for (;;) { 1646753f127fSDimitry Andric bool changed = target->needsThunks ? tc.createThunks(pass, outputSections) 1647753f127fSDimitry Andric : target->relaxOnce(pass); 1648753f127fSDimitry Andric ++pass; 164985868e8aSDimitry Andric 165085868e8aSDimitry Andric // With Thunk Size much smaller than branch range we expect to 1651e8d8bef9SDimitry Andric // converge quickly; if we get to 15 something has gone wrong. 1652753f127fSDimitry Andric if (changed && pass >= 15) { 1653753f127fSDimitry Andric error(target->needsThunks ? "thunk creation not converged" 1654753f127fSDimitry Andric : "relaxation not converged"); 165585868e8aSDimitry Andric break; 165685868e8aSDimitry Andric } 16570b57cec5SDimitry Andric 16580b57cec5SDimitry Andric if (config->fixCortexA53Errata843419) { 16590b57cec5SDimitry Andric if (changed) 16600b57cec5SDimitry Andric script->assignAddresses(); 16610b57cec5SDimitry Andric changed |= a64p.createFixes(); 16620b57cec5SDimitry Andric } 166385868e8aSDimitry Andric if (config->fixCortexA8) { 166485868e8aSDimitry Andric if (changed) 166585868e8aSDimitry Andric script->assignAddresses(); 166685868e8aSDimitry Andric changed |= a32p.createFixes(); 166785868e8aSDimitry Andric } 16680b57cec5SDimitry Andric 16690b57cec5SDimitry Andric if (in.mipsGot) 16700b57cec5SDimitry Andric in.mipsGot->updateAllocSize(); 16710b57cec5SDimitry Andric 16720b57cec5SDimitry Andric for (Partition &part : partitions) { 16730b57cec5SDimitry Andric changed |= part.relaDyn->updateAllocSize(); 16740b57cec5SDimitry Andric if (part.relrDyn) 16750b57cec5SDimitry Andric changed |= part.relrDyn->updateAllocSize(); 16760b57cec5SDimitry Andric } 16770b57cec5SDimitry Andric 167885868e8aSDimitry Andric const Defined *changedSym = script->assignAddresses(); 167985868e8aSDimitry Andric if (!changed) { 168085868e8aSDimitry Andric // Some symbols may be dependent on section addresses. When we break the 168185868e8aSDimitry Andric // loop, the symbol values are finalized because a previous 168285868e8aSDimitry Andric // assignAddresses() finalized section addresses. 168385868e8aSDimitry Andric if (!changedSym) 168485868e8aSDimitry Andric break; 168585868e8aSDimitry Andric if (++assignPasses == 5) { 168685868e8aSDimitry Andric errorOrWarn("assignment to symbol " + toString(*changedSym) + 168785868e8aSDimitry Andric " does not converge"); 168885868e8aSDimitry Andric break; 168985868e8aSDimitry Andric } 169085868e8aSDimitry Andric } 16910b57cec5SDimitry Andric } 1692753f127fSDimitry Andric if (!config->relocatable && config->emachine == EM_RISCV) 1693753f127fSDimitry Andric riscvFinalizeRelax(pass); 16945ffd83dbSDimitry Andric 169504eeddc0SDimitry Andric if (config->relocatable) 169604eeddc0SDimitry Andric for (OutputSection *sec : outputSections) 169704eeddc0SDimitry Andric sec->addr = 0; 169804eeddc0SDimitry Andric 16995ffd83dbSDimitry Andric // If addrExpr is set, the address may not be a multiple of the alignment. 17005ffd83dbSDimitry Andric // Warn because this is error-prone. 17014824e7fdSDimitry Andric for (SectionCommand *cmd : script->sectionCommands) 170281ad6265SDimitry Andric if (auto *osd = dyn_cast<OutputDesc>(cmd)) { 170381ad6265SDimitry Andric OutputSection *osec = &osd->osec; 170481ad6265SDimitry Andric if (osec->addr % osec->alignment != 0) 170581ad6265SDimitry Andric warn("address (0x" + Twine::utohexstr(osec->addr) + ") of section " + 170681ad6265SDimitry Andric osec->name + " is not a multiple of alignment (" + 170781ad6265SDimitry Andric Twine(osec->alignment) + ")"); 170881ad6265SDimitry Andric } 17090b57cec5SDimitry Andric } 17100b57cec5SDimitry Andric 1711fe6060f1SDimitry Andric // If Input Sections have been shrunk (basic block sections) then 17125ffd83dbSDimitry Andric // update symbol values and sizes associated with these sections. With basic 17135ffd83dbSDimitry Andric // block sections, input sections can shrink when the jump instructions at 17145ffd83dbSDimitry Andric // the end of the section are relaxed. 17155ffd83dbSDimitry Andric static void fixSymbolsAfterShrinking() { 171681ad6265SDimitry Andric for (InputFile *File : ctx->objectFiles) { 17175ffd83dbSDimitry Andric parallelForEach(File->getSymbols(), [&](Symbol *Sym) { 17185ffd83dbSDimitry Andric auto *def = dyn_cast<Defined>(Sym); 17195ffd83dbSDimitry Andric if (!def) 17205ffd83dbSDimitry Andric return; 17215ffd83dbSDimitry Andric 17225ffd83dbSDimitry Andric const SectionBase *sec = def->section; 17235ffd83dbSDimitry Andric if (!sec) 17245ffd83dbSDimitry Andric return; 17255ffd83dbSDimitry Andric 17260eae32dcSDimitry Andric const InputSectionBase *inputSec = dyn_cast<InputSectionBase>(sec); 17275ffd83dbSDimitry Andric if (!inputSec || !inputSec->bytesDropped) 17285ffd83dbSDimitry Andric return; 17295ffd83dbSDimitry Andric 173081ad6265SDimitry Andric const size_t OldSize = inputSec->rawData.size(); 17315ffd83dbSDimitry Andric const size_t NewSize = OldSize - inputSec->bytesDropped; 17325ffd83dbSDimitry Andric 17335ffd83dbSDimitry Andric if (def->value > NewSize && def->value <= OldSize) { 17345ffd83dbSDimitry Andric LLVM_DEBUG(llvm::dbgs() 17355ffd83dbSDimitry Andric << "Moving symbol " << Sym->getName() << " from " 17365ffd83dbSDimitry Andric << def->value << " to " 17375ffd83dbSDimitry Andric << def->value - inputSec->bytesDropped << " bytes\n"); 17385ffd83dbSDimitry Andric def->value -= inputSec->bytesDropped; 17395ffd83dbSDimitry Andric return; 17405ffd83dbSDimitry Andric } 17415ffd83dbSDimitry Andric 17425ffd83dbSDimitry Andric if (def->value + def->size > NewSize && def->value <= OldSize && 17435ffd83dbSDimitry Andric def->value + def->size <= OldSize) { 17445ffd83dbSDimitry Andric LLVM_DEBUG(llvm::dbgs() 17455ffd83dbSDimitry Andric << "Shrinking symbol " << Sym->getName() << " from " 17465ffd83dbSDimitry Andric << def->size << " to " << def->size - inputSec->bytesDropped 17475ffd83dbSDimitry Andric << " bytes\n"); 17485ffd83dbSDimitry Andric def->size -= inputSec->bytesDropped; 17495ffd83dbSDimitry Andric } 17505ffd83dbSDimitry Andric }); 17515ffd83dbSDimitry Andric } 17525ffd83dbSDimitry Andric } 17535ffd83dbSDimitry Andric 17545ffd83dbSDimitry Andric // If basic block sections exist, there are opportunities to delete fall thru 17555ffd83dbSDimitry Andric // jumps and shrink jump instructions after basic block reordering. This 17565ffd83dbSDimitry Andric // relaxation pass does that. It is only enabled when --optimize-bb-jumps 17575ffd83dbSDimitry Andric // option is used. 17585ffd83dbSDimitry Andric template <class ELFT> void Writer<ELFT>::optimizeBasicBlockJumps() { 17595ffd83dbSDimitry Andric assert(config->optimizeBBJumps); 1760753f127fSDimitry Andric SmallVector<InputSection *, 0> storage; 17615ffd83dbSDimitry Andric 17625ffd83dbSDimitry Andric script->assignAddresses(); 17635ffd83dbSDimitry Andric // For every output section that has executable input sections, this 17645ffd83dbSDimitry Andric // does the following: 17655ffd83dbSDimitry Andric // 1. Deletes all direct jump instructions in input sections that 17665ffd83dbSDimitry Andric // jump to the following section as it is not required. 17675ffd83dbSDimitry Andric // 2. If there are two consecutive jump instructions, it checks 17685ffd83dbSDimitry Andric // if they can be flipped and one can be deleted. 176904eeddc0SDimitry Andric for (OutputSection *osec : outputSections) { 177004eeddc0SDimitry Andric if (!(osec->flags & SHF_EXECINSTR)) 17715ffd83dbSDimitry Andric continue; 1772753f127fSDimitry Andric ArrayRef<InputSection *> sections = getInputSections(*osec, storage); 177304eeddc0SDimitry Andric size_t numDeleted = 0; 17745ffd83dbSDimitry Andric // Delete all fall through jump instructions. Also, check if two 17755ffd83dbSDimitry Andric // consecutive jump instructions can be flipped so that a fall 17765ffd83dbSDimitry Andric // through jmp instruction can be deleted. 177704eeddc0SDimitry Andric for (size_t i = 0, e = sections.size(); i != e; ++i) { 17785ffd83dbSDimitry Andric InputSection *next = i + 1 < sections.size() ? sections[i + 1] : nullptr; 177904eeddc0SDimitry Andric InputSection &sec = *sections[i]; 178004eeddc0SDimitry Andric numDeleted += target->deleteFallThruJmpInsn(sec, sec.file, next); 178104eeddc0SDimitry Andric } 17825ffd83dbSDimitry Andric if (numDeleted > 0) { 17835ffd83dbSDimitry Andric script->assignAddresses(); 17845ffd83dbSDimitry Andric LLVM_DEBUG(llvm::dbgs() 17855ffd83dbSDimitry Andric << "Removing " << numDeleted << " fall through jumps\n"); 17865ffd83dbSDimitry Andric } 17875ffd83dbSDimitry Andric } 17885ffd83dbSDimitry Andric 17895ffd83dbSDimitry Andric fixSymbolsAfterShrinking(); 17905ffd83dbSDimitry Andric 179104eeddc0SDimitry Andric for (OutputSection *osec : outputSections) 1792753f127fSDimitry Andric for (InputSection *is : getInputSections(*osec, storage)) 17935ffd83dbSDimitry Andric is->trim(); 17945ffd83dbSDimitry Andric } 17950b57cec5SDimitry Andric 17960b57cec5SDimitry Andric // In order to allow users to manipulate linker-synthesized sections, 17970b57cec5SDimitry Andric // we had to add synthetic sections to the input section list early, 17980b57cec5SDimitry Andric // even before we make decisions whether they are needed. This allows 17990b57cec5SDimitry Andric // users to write scripts like this: ".mygot : { .got }". 18000b57cec5SDimitry Andric // 18010b57cec5SDimitry Andric // Doing it has an unintended side effects. If it turns out that we 18020b57cec5SDimitry Andric // don't need a .got (for example) at all because there's no 18030b57cec5SDimitry Andric // relocation that needs a .got, we don't want to emit .got. 18040b57cec5SDimitry Andric // 18050b57cec5SDimitry Andric // To deal with the above problem, this function is called after 18060b57cec5SDimitry Andric // scanRelocations is called to remove synthetic sections that turn 18070b57cec5SDimitry Andric // out to be empty. 18080b57cec5SDimitry Andric static void removeUnusedSyntheticSections() { 18090b57cec5SDimitry Andric // All input synthetic sections that can be empty are placed after 1810fe6060f1SDimitry Andric // all regular ones. Reverse iterate to find the first synthetic section 1811fe6060f1SDimitry Andric // after a non-synthetic one which will be our starting point. 1812fe6060f1SDimitry Andric auto start = std::find_if(inputSections.rbegin(), inputSections.rend(), 1813fe6060f1SDimitry Andric [](InputSectionBase *s) { 1814fe6060f1SDimitry Andric return !isa<SyntheticSection>(s); 1815fe6060f1SDimitry Andric }) 1816fe6060f1SDimitry Andric .base(); 1817fe6060f1SDimitry Andric 18184824e7fdSDimitry Andric // Remove unused synthetic sections from inputSections; 18194824e7fdSDimitry Andric DenseSet<InputSectionBase *> unused; 18204824e7fdSDimitry Andric auto end = 18214824e7fdSDimitry Andric std::remove_if(start, inputSections.end(), [&](InputSectionBase *s) { 18224824e7fdSDimitry Andric auto *sec = cast<SyntheticSection>(s); 18234824e7fdSDimitry Andric if (sec->getParent() && sec->isNeeded()) 1824fe6060f1SDimitry Andric return false; 18254824e7fdSDimitry Andric unused.insert(sec); 18264824e7fdSDimitry Andric return true; 1827fe6060f1SDimitry Andric }); 1828fe6060f1SDimitry Andric inputSections.erase(end, inputSections.end()); 18294824e7fdSDimitry Andric 18304824e7fdSDimitry Andric // Remove unused synthetic sections from the corresponding input section 18314824e7fdSDimitry Andric // description and orphanSections. 18324824e7fdSDimitry Andric for (auto *sec : unused) 18334824e7fdSDimitry Andric if (OutputSection *osec = cast<SyntheticSection>(sec)->getParent()) 18344824e7fdSDimitry Andric for (SectionCommand *cmd : osec->commands) 18354824e7fdSDimitry Andric if (auto *isd = dyn_cast<InputSectionDescription>(cmd)) 18364824e7fdSDimitry Andric llvm::erase_if(isd->sections, [&](InputSection *isec) { 18374824e7fdSDimitry Andric return unused.count(isec); 18384824e7fdSDimitry Andric }); 18394824e7fdSDimitry Andric llvm::erase_if(script->orphanSections, [&](const InputSectionBase *sec) { 18404824e7fdSDimitry Andric return unused.count(sec); 18414824e7fdSDimitry Andric }); 18420b57cec5SDimitry Andric } 18430b57cec5SDimitry Andric 18440b57cec5SDimitry Andric // Create output section objects and add them to OutputSections. 18450b57cec5SDimitry Andric template <class ELFT> void Writer<ELFT>::finalizeSections() { 18460b57cec5SDimitry Andric Out::preinitArray = findSection(".preinit_array"); 18470b57cec5SDimitry Andric Out::initArray = findSection(".init_array"); 18480b57cec5SDimitry Andric Out::finiArray = findSection(".fini_array"); 18490b57cec5SDimitry Andric 18500b57cec5SDimitry Andric // The linker needs to define SECNAME_start, SECNAME_end and SECNAME_stop 18510b57cec5SDimitry Andric // symbols for sections, so that the runtime can get the start and end 18520b57cec5SDimitry Andric // addresses of each section by section name. Add such symbols. 18530b57cec5SDimitry Andric if (!config->relocatable) { 18540b57cec5SDimitry Andric addStartEndSymbols(); 18554824e7fdSDimitry Andric for (SectionCommand *cmd : script->sectionCommands) 185681ad6265SDimitry Andric if (auto *osd = dyn_cast<OutputDesc>(cmd)) 185781ad6265SDimitry Andric addStartStopSymbols(osd->osec); 18580b57cec5SDimitry Andric } 18590b57cec5SDimitry Andric 18600b57cec5SDimitry Andric // Add _DYNAMIC symbol. Unlike GNU gold, our _DYNAMIC symbol has no type. 18610b57cec5SDimitry Andric // It should be okay as no one seems to care about the type. 18620b57cec5SDimitry Andric // Even the author of gold doesn't remember why gold behaves that way. 18630b57cec5SDimitry Andric // https://sourceware.org/ml/binutils/2002-03/msg00360.html 18640b57cec5SDimitry Andric if (mainPart->dynamic->parent) 186581ad6265SDimitry Andric symtab->addSymbol(Defined{/*file=*/nullptr, "_DYNAMIC", STB_WEAK, STV_HIDDEN, STT_NOTYPE, 186681ad6265SDimitry Andric /*value=*/0, /*size=*/0, mainPart->dynamic.get()})->isUsedInRegularObj = true; 18670b57cec5SDimitry Andric 18680b57cec5SDimitry Andric // Define __rel[a]_iplt_{start,end} symbols if needed. 18690b57cec5SDimitry Andric addRelIpltSymbols(); 18700b57cec5SDimitry Andric 187185868e8aSDimitry Andric // RISC-V's gp can address +/- 2 KiB, set it to .sdata + 0x800. This symbol 187285868e8aSDimitry Andric // should only be defined in an executable. If .sdata does not exist, its 187385868e8aSDimitry Andric // value/section does not matter but it has to be relative, so set its 187485868e8aSDimitry Andric // st_shndx arbitrarily to 1 (Out::elfHeader). 187585868e8aSDimitry Andric if (config->emachine == EM_RISCV && !config->shared) { 187685868e8aSDimitry Andric OutputSection *sec = findSection(".sdata"); 18770b57cec5SDimitry Andric ElfSym::riscvGlobalPointer = 187885868e8aSDimitry Andric addOptionalRegular("__global_pointer$", sec ? sec : Out::elfHeader, 1879349cc55cSDimitry Andric 0x800, STV_DEFAULT); 188085868e8aSDimitry Andric } 18810b57cec5SDimitry Andric 1882349cc55cSDimitry Andric if (config->emachine == EM_386 || config->emachine == EM_X86_64) { 18830b57cec5SDimitry Andric // On targets that support TLSDESC, _TLS_MODULE_BASE_ is defined in such a 18840b57cec5SDimitry Andric // way that: 18850b57cec5SDimitry Andric // 18860b57cec5SDimitry Andric // 1) Without relaxation: it produces a dynamic TLSDESC relocation that 18870b57cec5SDimitry Andric // computes 0. 18880b57cec5SDimitry Andric // 2) With LD->LE relaxation: _TLS_MODULE_BASE_@tpoff = 0 (lowest address in 18890b57cec5SDimitry Andric // the TLS block). 18900b57cec5SDimitry Andric // 18910b57cec5SDimitry Andric // 2) is special cased in @tpoff computation. To satisfy 1), we define it as 18920b57cec5SDimitry Andric // an absolute symbol of zero. This is different from GNU linkers which 18930b57cec5SDimitry Andric // define _TLS_MODULE_BASE_ relative to the first TLS section. 18940b57cec5SDimitry Andric Symbol *s = symtab->find("_TLS_MODULE_BASE_"); 18950b57cec5SDimitry Andric if (s && s->isUndefined()) { 189681ad6265SDimitry Andric s->resolve(Defined{/*file=*/nullptr, StringRef(), STB_GLOBAL, STV_HIDDEN, 18970b57cec5SDimitry Andric STT_TLS, /*value=*/0, 0, 18980b57cec5SDimitry Andric /*section=*/nullptr}); 18990b57cec5SDimitry Andric ElfSym::tlsModuleBase = cast<Defined>(s); 19000b57cec5SDimitry Andric } 19010b57cec5SDimitry Andric } 19020b57cec5SDimitry Andric 1903e8d8bef9SDimitry Andric { 1904e8d8bef9SDimitry Andric llvm::TimeTraceScope timeScope("Finalize .eh_frame"); 19050b57cec5SDimitry Andric // This responsible for splitting up .eh_frame section into 19060b57cec5SDimitry Andric // pieces. The relocation scan uses those pieces, so this has to be 19070b57cec5SDimitry Andric // earlier. 19080b57cec5SDimitry Andric for (Partition &part : partitions) 190904eeddc0SDimitry Andric finalizeSynthetic(part.ehFrame.get()); 1910e8d8bef9SDimitry Andric } 19110b57cec5SDimitry Andric 191204eeddc0SDimitry Andric if (config->hasDynSymTab) { 191304eeddc0SDimitry Andric parallelForEach(symtab->symbols(), [](Symbol *sym) { 1914480093f4SDimitry Andric sym->isPreemptible = computeIsPreemptible(*sym); 191504eeddc0SDimitry Andric }); 191604eeddc0SDimitry Andric } 191785868e8aSDimitry Andric 191885868e8aSDimitry Andric // Change values of linker-script-defined symbols from placeholders (assigned 191985868e8aSDimitry Andric // by declareSymbols) to actual definitions. 192085868e8aSDimitry Andric script->processSymbolAssignments(); 19210b57cec5SDimitry Andric 1922e8d8bef9SDimitry Andric { 1923e8d8bef9SDimitry Andric llvm::TimeTraceScope timeScope("Scan relocations"); 1924e8d8bef9SDimitry Andric // Scan relocations. This must be done after every symbol is declared so 1925e8d8bef9SDimitry Andric // that we can correctly decide if a dynamic relocation is needed. This is 1926e8d8bef9SDimitry Andric // called after processSymbolAssignments() because it needs to know whether 1927e8d8bef9SDimitry Andric // a linker-script-defined symbol is absolute. 19285ffd83dbSDimitry Andric ppc64noTocRelax.clear(); 19290b57cec5SDimitry Andric if (!config->relocatable) { 193004eeddc0SDimitry Andric // Scan all relocations. Each relocation goes through a series of tests to 193104eeddc0SDimitry Andric // determine if it needs special treatment, such as creating GOT, PLT, 193204eeddc0SDimitry Andric // copy relocations, etc. Note that relocations for non-alloc sections are 193304eeddc0SDimitry Andric // directly processed by InputSection::relocateNonAlloc. 193404eeddc0SDimitry Andric for (InputSectionBase *sec : inputSections) 193504eeddc0SDimitry Andric if (sec->isLive() && isa<InputSection>(sec) && (sec->flags & SHF_ALLOC)) 193604eeddc0SDimitry Andric scanRelocations<ELFT>(*sec); 193704eeddc0SDimitry Andric for (Partition &part : partitions) { 193804eeddc0SDimitry Andric for (EhInputSection *sec : part.ehFrame->sections) 193904eeddc0SDimitry Andric scanRelocations<ELFT>(*sec); 194004eeddc0SDimitry Andric if (part.armExidx && part.armExidx->isLive()) 194104eeddc0SDimitry Andric for (InputSection *sec : part.armExidx->exidxSections) 194204eeddc0SDimitry Andric scanRelocations<ELFT>(*sec); 194304eeddc0SDimitry Andric } 194404eeddc0SDimitry Andric 194581ad6265SDimitry Andric reportUndefinedSymbols(); 19460eae32dcSDimitry Andric postScanRelocations(); 19470b57cec5SDimitry Andric } 1948e8d8bef9SDimitry Andric } 19490b57cec5SDimitry Andric 19500b57cec5SDimitry Andric if (in.plt && in.plt->isNeeded()) 19510b57cec5SDimitry Andric in.plt->addSymbols(); 19520b57cec5SDimitry Andric if (in.iplt && in.iplt->isNeeded()) 19530b57cec5SDimitry Andric in.iplt->addSymbols(); 19540b57cec5SDimitry Andric 1955e8d8bef9SDimitry Andric if (config->unresolvedSymbolsInShlib != UnresolvedPolicy::Ignore) { 1956fe6060f1SDimitry Andric auto diagnose = 1957fe6060f1SDimitry Andric config->unresolvedSymbolsInShlib == UnresolvedPolicy::ReportError 1958fe6060f1SDimitry Andric ? errorOrWarn 1959fe6060f1SDimitry Andric : warn; 19600b57cec5SDimitry Andric // Error on undefined symbols in a shared object, if all of its DT_NEEDED 1961480093f4SDimitry Andric // entries are seen. These cases would otherwise lead to runtime errors 19620b57cec5SDimitry Andric // reported by the dynamic linker. 19630b57cec5SDimitry Andric // 19640b57cec5SDimitry Andric // ld.bfd traces all DT_NEEDED to emulate the logic of the dynamic linker to 19650b57cec5SDimitry Andric // catch more cases. That is too much for us. Our approach resembles the one 19660b57cec5SDimitry Andric // used in ld.gold, achieves a good balance to be useful but not too smart. 196781ad6265SDimitry Andric for (SharedFile *file : ctx->sharedFiles) { 1968fe6060f1SDimitry Andric bool allNeededIsKnown = 19690b57cec5SDimitry Andric llvm::all_of(file->dtNeeded, [&](StringRef needed) { 197004eeddc0SDimitry Andric return symtab->soNames.count(CachedHashStringRef(needed)); 19710b57cec5SDimitry Andric }); 1972fe6060f1SDimitry Andric if (!allNeededIsKnown) 1973fe6060f1SDimitry Andric continue; 1974fe6060f1SDimitry Andric for (Symbol *sym : file->requiredSymbols) 19750b57cec5SDimitry Andric if (sym->isUndefined() && !sym->isWeak()) 1976fcaf7f86SDimitry Andric diagnose("undefined reference due to --no-allow-shlib-undefined: " + 1977fcaf7f86SDimitry Andric toString(*sym) + "\n>>> referenced by " + toString(file)); 19780b57cec5SDimitry Andric } 1979e8d8bef9SDimitry Andric } 19800b57cec5SDimitry Andric 1981e8d8bef9SDimitry Andric { 1982e8d8bef9SDimitry Andric llvm::TimeTraceScope timeScope("Add symbols to symtabs"); 19830b57cec5SDimitry Andric // Now that we have defined all possible global symbols including linker- 19840b57cec5SDimitry Andric // synthesized ones. Visit all symbols to give the finishing touches. 1985480093f4SDimitry Andric for (Symbol *sym : symtab->symbols()) { 19860eae32dcSDimitry Andric if (!sym->isUsedInRegularObj || !includeInSymtab(*sym)) 1987480093f4SDimitry Andric continue; 198804eeddc0SDimitry Andric if (!config->relocatable) 198904eeddc0SDimitry Andric sym->binding = sym->computeBinding(); 19900b57cec5SDimitry Andric if (in.symTab) 19910b57cec5SDimitry Andric in.symTab->addSymbol(sym); 19920b57cec5SDimitry Andric 19930b57cec5SDimitry Andric if (sym->includeInDynsym()) { 19940b57cec5SDimitry Andric partitions[sym->partition - 1].dynSymTab->addSymbol(sym); 19950b57cec5SDimitry Andric if (auto *file = dyn_cast_or_null<SharedFile>(sym->file)) 19960b57cec5SDimitry Andric if (file->isNeeded && !sym->isUndefined()) 19970b57cec5SDimitry Andric addVerneed(sym); 19980b57cec5SDimitry Andric } 1999480093f4SDimitry Andric } 20000b57cec5SDimitry Andric 2001e8d8bef9SDimitry Andric // We also need to scan the dynamic relocation tables of the other 2002e8d8bef9SDimitry Andric // partitions and add any referenced symbols to the partition's dynsym. 20030b57cec5SDimitry Andric for (Partition &part : MutableArrayRef<Partition>(partitions).slice(1)) { 20040b57cec5SDimitry Andric DenseSet<Symbol *> syms; 20050b57cec5SDimitry Andric for (const SymbolTableEntry &e : part.dynSymTab->getSymbols()) 20060b57cec5SDimitry Andric syms.insert(e.sym); 20070b57cec5SDimitry Andric for (DynamicReloc &reloc : part.relaDyn->relocs) 2008fe6060f1SDimitry Andric if (reloc.sym && reloc.needsDynSymIndex() && 2009fe6060f1SDimitry Andric syms.insert(reloc.sym).second) 20100b57cec5SDimitry Andric part.dynSymTab->addSymbol(reloc.sym); 20110b57cec5SDimitry Andric } 2012e8d8bef9SDimitry Andric } 20130b57cec5SDimitry Andric 20140b57cec5SDimitry Andric if (in.mipsGot) 20150b57cec5SDimitry Andric in.mipsGot->build(); 20160b57cec5SDimitry Andric 20170b57cec5SDimitry Andric removeUnusedSyntheticSections(); 20185ffd83dbSDimitry Andric script->diagnoseOrphanHandling(); 20190b57cec5SDimitry Andric 20200b57cec5SDimitry Andric sortSections(); 20210b57cec5SDimitry Andric 20224824e7fdSDimitry Andric // Create a list of OutputSections, assign sectionIndex, and populate 20234824e7fdSDimitry Andric // in.shStrTab. 20244824e7fdSDimitry Andric for (SectionCommand *cmd : script->sectionCommands) 202581ad6265SDimitry Andric if (auto *osd = dyn_cast<OutputDesc>(cmd)) { 202681ad6265SDimitry Andric OutputSection *osec = &osd->osec; 20274824e7fdSDimitry Andric outputSections.push_back(osec); 20284824e7fdSDimitry Andric osec->sectionIndex = outputSections.size(); 20294824e7fdSDimitry Andric osec->shName = in.shStrTab->addString(osec->name); 20304824e7fdSDimitry Andric } 20310b57cec5SDimitry Andric 20320b57cec5SDimitry Andric // Prefer command line supplied address over other constraints. 20330b57cec5SDimitry Andric for (OutputSection *sec : outputSections) { 20340b57cec5SDimitry Andric auto i = config->sectionStartMap.find(sec->name); 20350b57cec5SDimitry Andric if (i != config->sectionStartMap.end()) 20360b57cec5SDimitry Andric sec->addrExpr = [=] { return i->second; }; 20370b57cec5SDimitry Andric } 20380b57cec5SDimitry Andric 20395ffd83dbSDimitry Andric // With the outputSections available check for GDPLT relocations 20405ffd83dbSDimitry Andric // and add __tls_get_addr symbol if needed. 20415ffd83dbSDimitry Andric if (config->emachine == EM_HEXAGON && hexagonNeedsTLSSymbol(outputSections)) { 20425ffd83dbSDimitry Andric Symbol *sym = symtab->addSymbol(Undefined{ 20435ffd83dbSDimitry Andric nullptr, "__tls_get_addr", STB_GLOBAL, STV_DEFAULT, STT_NOTYPE}); 20445ffd83dbSDimitry Andric sym->isPreemptible = true; 20455ffd83dbSDimitry Andric partitions[0].dynSymTab->addSymbol(sym); 20465ffd83dbSDimitry Andric } 20475ffd83dbSDimitry Andric 20480b57cec5SDimitry Andric // This is a bit of a hack. A value of 0 means undef, so we set it 20490b57cec5SDimitry Andric // to 1 to make __ehdr_start defined. The section number is not 20500b57cec5SDimitry Andric // particularly relevant. 20510b57cec5SDimitry Andric Out::elfHeader->sectionIndex = 1; 20524824e7fdSDimitry Andric Out::elfHeader->size = sizeof(typename ELFT::Ehdr); 20530b57cec5SDimitry Andric 20540b57cec5SDimitry Andric // Binary and relocatable output does not have PHDRS. 20550b57cec5SDimitry Andric // The headers have to be created before finalize as that can influence the 20560b57cec5SDimitry Andric // image base and the dynamic section on mips includes the image base. 20570b57cec5SDimitry Andric if (!config->relocatable && !config->oFormatBinary) { 20580b57cec5SDimitry Andric for (Partition &part : partitions) { 20590b57cec5SDimitry Andric part.phdrs = script->hasPhdrsCommands() ? script->createPhdrs() 20600b57cec5SDimitry Andric : createPhdrs(part); 20610b57cec5SDimitry Andric if (config->emachine == EM_ARM) { 20620b57cec5SDimitry Andric // PT_ARM_EXIDX is the ARM EHABI equivalent of PT_GNU_EH_FRAME 20630b57cec5SDimitry Andric addPhdrForSection(part, SHT_ARM_EXIDX, PT_ARM_EXIDX, PF_R); 20640b57cec5SDimitry Andric } 20650b57cec5SDimitry Andric if (config->emachine == EM_MIPS) { 20660b57cec5SDimitry Andric // Add separate segments for MIPS-specific sections. 20670b57cec5SDimitry Andric addPhdrForSection(part, SHT_MIPS_REGINFO, PT_MIPS_REGINFO, PF_R); 20680b57cec5SDimitry Andric addPhdrForSection(part, SHT_MIPS_OPTIONS, PT_MIPS_OPTIONS, PF_R); 20690b57cec5SDimitry Andric addPhdrForSection(part, SHT_MIPS_ABIFLAGS, PT_MIPS_ABIFLAGS, PF_R); 20700b57cec5SDimitry Andric } 20710b57cec5SDimitry Andric } 20720b57cec5SDimitry Andric Out::programHeaders->size = sizeof(Elf_Phdr) * mainPart->phdrs.size(); 20730b57cec5SDimitry Andric 20740b57cec5SDimitry Andric // Find the TLS segment. This happens before the section layout loop so that 20750b57cec5SDimitry Andric // Android relocation packing can look up TLS symbol addresses. We only need 20760b57cec5SDimitry Andric // to care about the main partition here because all TLS symbols were moved 20770b57cec5SDimitry Andric // to the main partition (see MarkLive.cpp). 20780b57cec5SDimitry Andric for (PhdrEntry *p : mainPart->phdrs) 20790b57cec5SDimitry Andric if (p->p_type == PT_TLS) 20800b57cec5SDimitry Andric Out::tlsPhdr = p; 20810b57cec5SDimitry Andric } 20820b57cec5SDimitry Andric 20830b57cec5SDimitry Andric // Some symbols are defined in term of program headers. Now that we 20840b57cec5SDimitry Andric // have the headers, we can find out which sections they point to. 20850b57cec5SDimitry Andric setReservedSymbolSections(); 20860b57cec5SDimitry Andric 2087e8d8bef9SDimitry Andric { 2088e8d8bef9SDimitry Andric llvm::TimeTraceScope timeScope("Finalize synthetic sections"); 2089e8d8bef9SDimitry Andric 209004eeddc0SDimitry Andric finalizeSynthetic(in.bss.get()); 209104eeddc0SDimitry Andric finalizeSynthetic(in.bssRelRo.get()); 209204eeddc0SDimitry Andric finalizeSynthetic(in.symTabShndx.get()); 209304eeddc0SDimitry Andric finalizeSynthetic(in.shStrTab.get()); 209404eeddc0SDimitry Andric finalizeSynthetic(in.strTab.get()); 209504eeddc0SDimitry Andric finalizeSynthetic(in.got.get()); 209604eeddc0SDimitry Andric finalizeSynthetic(in.mipsGot.get()); 209704eeddc0SDimitry Andric finalizeSynthetic(in.igotPlt.get()); 209804eeddc0SDimitry Andric finalizeSynthetic(in.gotPlt.get()); 209904eeddc0SDimitry Andric finalizeSynthetic(in.relaIplt.get()); 210004eeddc0SDimitry Andric finalizeSynthetic(in.relaPlt.get()); 210104eeddc0SDimitry Andric finalizeSynthetic(in.plt.get()); 210204eeddc0SDimitry Andric finalizeSynthetic(in.iplt.get()); 210304eeddc0SDimitry Andric finalizeSynthetic(in.ppc32Got2.get()); 210404eeddc0SDimitry Andric finalizeSynthetic(in.partIndex.get()); 21050b57cec5SDimitry Andric 21060b57cec5SDimitry Andric // Dynamic section must be the last one in this list and dynamic 21070b57cec5SDimitry Andric // symbol table section (dynSymTab) must be the first one. 21080b57cec5SDimitry Andric for (Partition &part : partitions) { 21091fd87a68SDimitry Andric if (part.relaDyn) { 21101fd87a68SDimitry Andric // Compute DT_RELACOUNT to be used by part.dynamic. 21111fd87a68SDimitry Andric part.relaDyn->partitionRels(); 21121fd87a68SDimitry Andric finalizeSynthetic(part.relaDyn.get()); 21131fd87a68SDimitry Andric } 21141fd87a68SDimitry Andric 211504eeddc0SDimitry Andric finalizeSynthetic(part.dynSymTab.get()); 211604eeddc0SDimitry Andric finalizeSynthetic(part.gnuHashTab.get()); 211704eeddc0SDimitry Andric finalizeSynthetic(part.hashTab.get()); 211804eeddc0SDimitry Andric finalizeSynthetic(part.verDef.get()); 211904eeddc0SDimitry Andric finalizeSynthetic(part.relrDyn.get()); 212004eeddc0SDimitry Andric finalizeSynthetic(part.ehFrameHdr.get()); 212104eeddc0SDimitry Andric finalizeSynthetic(part.verSym.get()); 212204eeddc0SDimitry Andric finalizeSynthetic(part.verNeed.get()); 212304eeddc0SDimitry Andric finalizeSynthetic(part.dynamic.get()); 21240b57cec5SDimitry Andric } 2125e8d8bef9SDimitry Andric } 21260b57cec5SDimitry Andric 21270b57cec5SDimitry Andric if (!script->hasSectionsCommand && !config->relocatable) 21280b57cec5SDimitry Andric fixSectionAlignments(); 21290b57cec5SDimitry Andric 21300b57cec5SDimitry Andric // This is used to: 21310b57cec5SDimitry Andric // 1) Create "thunks": 21320b57cec5SDimitry Andric // Jump instructions in many ISAs have small displacements, and therefore 21330b57cec5SDimitry Andric // they cannot jump to arbitrary addresses in memory. For example, RISC-V 21340b57cec5SDimitry Andric // JAL instruction can target only +-1 MiB from PC. It is a linker's 21350b57cec5SDimitry Andric // responsibility to create and insert small pieces of code between 21360b57cec5SDimitry Andric // sections to extend the ranges if jump targets are out of range. Such 21370b57cec5SDimitry Andric // code pieces are called "thunks". 21380b57cec5SDimitry Andric // 21390b57cec5SDimitry Andric // We add thunks at this stage. We couldn't do this before this point 21400b57cec5SDimitry Andric // because this is the earliest point where we know sizes of sections and 21410b57cec5SDimitry Andric // their layouts (that are needed to determine if jump targets are in 21420b57cec5SDimitry Andric // range). 21430b57cec5SDimitry Andric // 21440b57cec5SDimitry Andric // 2) Update the sections. We need to generate content that depends on the 21450b57cec5SDimitry Andric // address of InputSections. For example, MIPS GOT section content or 21460b57cec5SDimitry Andric // android packed relocations sections content. 21470b57cec5SDimitry Andric // 21480b57cec5SDimitry Andric // 3) Assign the final values for the linker script symbols. Linker scripts 21490b57cec5SDimitry Andric // sometimes using forward symbol declarations. We want to set the correct 21500b57cec5SDimitry Andric // values. They also might change after adding the thunks. 21510b57cec5SDimitry Andric finalizeAddressDependentContent(); 215204eeddc0SDimitry Andric 215304eeddc0SDimitry Andric // All information needed for OutputSection part of Map file is available. 21545ffd83dbSDimitry Andric if (errorCount()) 21555ffd83dbSDimitry Andric return; 21560b57cec5SDimitry Andric 2157e8d8bef9SDimitry Andric { 2158e8d8bef9SDimitry Andric llvm::TimeTraceScope timeScope("Finalize synthetic sections"); 2159e8d8bef9SDimitry Andric // finalizeAddressDependentContent may have added local symbols to the 2160e8d8bef9SDimitry Andric // static symbol table. 216104eeddc0SDimitry Andric finalizeSynthetic(in.symTab.get()); 216204eeddc0SDimitry Andric finalizeSynthetic(in.ppc64LongBranchTarget.get()); 2163e8d8bef9SDimitry Andric } 21640b57cec5SDimitry Andric 21655ffd83dbSDimitry Andric // Relaxation to delete inter-basic block jumps created by basic block 21665ffd83dbSDimitry Andric // sections. Run after in.symTab is finalized as optimizeBasicBlockJumps 21675ffd83dbSDimitry Andric // can relax jump instructions based on symbol offset. 21685ffd83dbSDimitry Andric if (config->optimizeBBJumps) 21695ffd83dbSDimitry Andric optimizeBasicBlockJumps(); 21705ffd83dbSDimitry Andric 21710b57cec5SDimitry Andric // Fill other section headers. The dynamic table is finalized 21720b57cec5SDimitry Andric // at the end because some tags like RELSZ depend on result 21730b57cec5SDimitry Andric // of finalizing other sections. 21740b57cec5SDimitry Andric for (OutputSection *sec : outputSections) 21750b57cec5SDimitry Andric sec->finalize(); 21760b57cec5SDimitry Andric } 21770b57cec5SDimitry Andric 21780b57cec5SDimitry Andric // Ensure data sections are not mixed with executable sections when 2179349cc55cSDimitry Andric // --execute-only is used. --execute-only make pages executable but not 2180349cc55cSDimitry Andric // readable. 21810b57cec5SDimitry Andric template <class ELFT> void Writer<ELFT>::checkExecuteOnly() { 21820b57cec5SDimitry Andric if (!config->executeOnly) 21830b57cec5SDimitry Andric return; 21840b57cec5SDimitry Andric 2185753f127fSDimitry Andric SmallVector<InputSection *, 0> storage; 218604eeddc0SDimitry Andric for (OutputSection *osec : outputSections) 218704eeddc0SDimitry Andric if (osec->flags & SHF_EXECINSTR) 2188753f127fSDimitry Andric for (InputSection *isec : getInputSections(*osec, storage)) 21890b57cec5SDimitry Andric if (!(isec->flags & SHF_EXECINSTR)) 219004eeddc0SDimitry Andric error("cannot place " + toString(isec) + " into " + 219104eeddc0SDimitry Andric toString(osec->name) + 219204eeddc0SDimitry Andric ": --execute-only does not support intermingling data and code"); 21930b57cec5SDimitry Andric } 21940b57cec5SDimitry Andric 21950b57cec5SDimitry Andric // The linker is expected to define SECNAME_start and SECNAME_end 21960b57cec5SDimitry Andric // symbols for a few sections. This function defines them. 21970b57cec5SDimitry Andric template <class ELFT> void Writer<ELFT>::addStartEndSymbols() { 21980b57cec5SDimitry Andric // If a section does not exist, there's ambiguity as to how we 21990b57cec5SDimitry Andric // define _start and _end symbols for an init/fini section. Since 22000b57cec5SDimitry Andric // the loader assume that the symbols are always defined, we need to 22010b57cec5SDimitry Andric // always define them. But what value? The loader iterates over all 22020b57cec5SDimitry Andric // pointers between _start and _end to run global ctors/dtors, so if 22030b57cec5SDimitry Andric // the section is empty, their symbol values don't actually matter 22040b57cec5SDimitry Andric // as long as _start and _end point to the same location. 22050b57cec5SDimitry Andric // 22060b57cec5SDimitry Andric // That said, we don't want to set the symbols to 0 (which is 22070b57cec5SDimitry Andric // probably the simplest value) because that could cause some 22080b57cec5SDimitry Andric // program to fail to link due to relocation overflow, if their 22090b57cec5SDimitry Andric // program text is above 2 GiB. We use the address of the .text 22100b57cec5SDimitry Andric // section instead to prevent that failure. 22110b57cec5SDimitry Andric // 2212480093f4SDimitry Andric // In rare situations, the .text section may not exist. If that's the 22130b57cec5SDimitry Andric // case, use the image base address as a last resort. 22140b57cec5SDimitry Andric OutputSection *Default = findSection(".text"); 22150b57cec5SDimitry Andric if (!Default) 22160b57cec5SDimitry Andric Default = Out::elfHeader; 22170b57cec5SDimitry Andric 22180b57cec5SDimitry Andric auto define = [=](StringRef start, StringRef end, OutputSection *os) { 2219349cc55cSDimitry Andric if (os && !script->isDiscarded(os)) { 22200b57cec5SDimitry Andric addOptionalRegular(start, os, 0); 22210b57cec5SDimitry Andric addOptionalRegular(end, os, -1); 22220b57cec5SDimitry Andric } else { 22230b57cec5SDimitry Andric addOptionalRegular(start, Default, 0); 22240b57cec5SDimitry Andric addOptionalRegular(end, Default, 0); 22250b57cec5SDimitry Andric } 22260b57cec5SDimitry Andric }; 22270b57cec5SDimitry Andric 22280b57cec5SDimitry Andric define("__preinit_array_start", "__preinit_array_end", Out::preinitArray); 22290b57cec5SDimitry Andric define("__init_array_start", "__init_array_end", Out::initArray); 22300b57cec5SDimitry Andric define("__fini_array_start", "__fini_array_end", Out::finiArray); 22310b57cec5SDimitry Andric 22320b57cec5SDimitry Andric if (OutputSection *sec = findSection(".ARM.exidx")) 22330b57cec5SDimitry Andric define("__exidx_start", "__exidx_end", sec); 22340b57cec5SDimitry Andric } 22350b57cec5SDimitry Andric 22360b57cec5SDimitry Andric // If a section name is valid as a C identifier (which is rare because of 22370b57cec5SDimitry Andric // the leading '.'), linkers are expected to define __start_<secname> and 22380b57cec5SDimitry Andric // __stop_<secname> symbols. They are at beginning and end of the section, 22390b57cec5SDimitry Andric // respectively. This is not requested by the ELF standard, but GNU ld and 22400b57cec5SDimitry Andric // gold provide the feature, and used by many programs. 22410b57cec5SDimitry Andric template <class ELFT> 224281ad6265SDimitry Andric void Writer<ELFT>::addStartStopSymbols(OutputSection &osec) { 224381ad6265SDimitry Andric StringRef s = osec.name; 22440b57cec5SDimitry Andric if (!isValidCIdentifier(s)) 22450b57cec5SDimitry Andric return; 224681ad6265SDimitry Andric addOptionalRegular(saver().save("__start_" + s), &osec, 0, 22475ffd83dbSDimitry Andric config->zStartStopVisibility); 224881ad6265SDimitry Andric addOptionalRegular(saver().save("__stop_" + s), &osec, -1, 22495ffd83dbSDimitry Andric config->zStartStopVisibility); 22500b57cec5SDimitry Andric } 22510b57cec5SDimitry Andric 22520b57cec5SDimitry Andric static bool needsPtLoad(OutputSection *sec) { 2253fe6060f1SDimitry Andric if (!(sec->flags & SHF_ALLOC)) 22540b57cec5SDimitry Andric return false; 22550b57cec5SDimitry Andric 22560b57cec5SDimitry Andric // Don't allocate VA space for TLS NOBITS sections. The PT_TLS PHDR is 22570b57cec5SDimitry Andric // responsible for allocating space for them, not the PT_LOAD that 22580b57cec5SDimitry Andric // contains the TLS initialization image. 22590b57cec5SDimitry Andric if ((sec->flags & SHF_TLS) && sec->type == SHT_NOBITS) 22600b57cec5SDimitry Andric return false; 22610b57cec5SDimitry Andric return true; 22620b57cec5SDimitry Andric } 22630b57cec5SDimitry Andric 22640b57cec5SDimitry Andric // Linker scripts are responsible for aligning addresses. Unfortunately, most 22650b57cec5SDimitry Andric // linker scripts are designed for creating two PT_LOADs only, one RX and one 22660b57cec5SDimitry Andric // RW. This means that there is no alignment in the RO to RX transition and we 22670b57cec5SDimitry Andric // cannot create a PT_LOAD there. 22680b57cec5SDimitry Andric static uint64_t computeFlags(uint64_t flags) { 22690b57cec5SDimitry Andric if (config->omagic) 22700b57cec5SDimitry Andric return PF_R | PF_W | PF_X; 22710b57cec5SDimitry Andric if (config->executeOnly && (flags & PF_X)) 22720b57cec5SDimitry Andric return flags & ~PF_R; 22730b57cec5SDimitry Andric if (config->singleRoRx && !(flags & PF_W)) 22740b57cec5SDimitry Andric return flags | PF_X; 22750b57cec5SDimitry Andric return flags; 22760b57cec5SDimitry Andric } 22770b57cec5SDimitry Andric 22780b57cec5SDimitry Andric // Decide which program headers to create and which sections to include in each 22790b57cec5SDimitry Andric // one. 22800b57cec5SDimitry Andric template <class ELFT> 228104eeddc0SDimitry Andric SmallVector<PhdrEntry *, 0> Writer<ELFT>::createPhdrs(Partition &part) { 228204eeddc0SDimitry Andric SmallVector<PhdrEntry *, 0> ret; 22830b57cec5SDimitry Andric auto addHdr = [&](unsigned type, unsigned flags) -> PhdrEntry * { 22840b57cec5SDimitry Andric ret.push_back(make<PhdrEntry>(type, flags)); 22850b57cec5SDimitry Andric return ret.back(); 22860b57cec5SDimitry Andric }; 22870b57cec5SDimitry Andric 22880b57cec5SDimitry Andric unsigned partNo = part.getNumber(); 22890b57cec5SDimitry Andric bool isMain = partNo == 1; 22900b57cec5SDimitry Andric 229185868e8aSDimitry Andric // Add the first PT_LOAD segment for regular output sections. 229285868e8aSDimitry Andric uint64_t flags = computeFlags(PF_R); 229385868e8aSDimitry Andric PhdrEntry *load = nullptr; 229485868e8aSDimitry Andric 229585868e8aSDimitry Andric // nmagic or omagic output does not have PT_PHDR, PT_INTERP, or the readonly 229685868e8aSDimitry Andric // PT_LOAD. 229785868e8aSDimitry Andric if (!config->nmagic && !config->omagic) { 229885868e8aSDimitry Andric // The first phdr entry is PT_PHDR which describes the program header 229985868e8aSDimitry Andric // itself. 23000b57cec5SDimitry Andric if (isMain) 23010b57cec5SDimitry Andric addHdr(PT_PHDR, PF_R)->add(Out::programHeaders); 23020b57cec5SDimitry Andric else 23030b57cec5SDimitry Andric addHdr(PT_PHDR, PF_R)->add(part.programHeaders->getParent()); 23040b57cec5SDimitry Andric 23050b57cec5SDimitry Andric // PT_INTERP must be the second entry if exists. 23060b57cec5SDimitry Andric if (OutputSection *cmd = findSection(".interp", partNo)) 23070b57cec5SDimitry Andric addHdr(PT_INTERP, cmd->getPhdrFlags())->add(cmd); 23080b57cec5SDimitry Andric 23090b57cec5SDimitry Andric // Add the headers. We will remove them if they don't fit. 23100b57cec5SDimitry Andric // In the other partitions the headers are ordinary sections, so they don't 23110b57cec5SDimitry Andric // need to be added here. 23120b57cec5SDimitry Andric if (isMain) { 23130b57cec5SDimitry Andric load = addHdr(PT_LOAD, flags); 23140b57cec5SDimitry Andric load->add(Out::elfHeader); 23150b57cec5SDimitry Andric load->add(Out::programHeaders); 23160b57cec5SDimitry Andric } 231785868e8aSDimitry Andric } 23180b57cec5SDimitry Andric 23190b57cec5SDimitry Andric // PT_GNU_RELRO includes all sections that should be marked as 2320480093f4SDimitry Andric // read-only by dynamic linker after processing relocations. 23210b57cec5SDimitry Andric // Current dynamic loaders only support one PT_GNU_RELRO PHDR, give 23220b57cec5SDimitry Andric // an error message if more than one PT_GNU_RELRO PHDR is required. 23230b57cec5SDimitry Andric PhdrEntry *relRo = make<PhdrEntry>(PT_GNU_RELRO, PF_R); 23240b57cec5SDimitry Andric bool inRelroPhdr = false; 23250b57cec5SDimitry Andric OutputSection *relroEnd = nullptr; 23260b57cec5SDimitry Andric for (OutputSection *sec : outputSections) { 23270b57cec5SDimitry Andric if (sec->partition != partNo || !needsPtLoad(sec)) 23280b57cec5SDimitry Andric continue; 23290b57cec5SDimitry Andric if (isRelroSection(sec)) { 23300b57cec5SDimitry Andric inRelroPhdr = true; 23310b57cec5SDimitry Andric if (!relroEnd) 23320b57cec5SDimitry Andric relRo->add(sec); 23330b57cec5SDimitry Andric else 23340b57cec5SDimitry Andric error("section: " + sec->name + " is not contiguous with other relro" + 23350b57cec5SDimitry Andric " sections"); 23360b57cec5SDimitry Andric } else if (inRelroPhdr) { 23370b57cec5SDimitry Andric inRelroPhdr = false; 23380b57cec5SDimitry Andric relroEnd = sec; 23390b57cec5SDimitry Andric } 23400b57cec5SDimitry Andric } 23410b57cec5SDimitry Andric 23420b57cec5SDimitry Andric for (OutputSection *sec : outputSections) { 23430b57cec5SDimitry Andric if (!needsPtLoad(sec)) 23440b57cec5SDimitry Andric continue; 23450b57cec5SDimitry Andric 23460b57cec5SDimitry Andric // Normally, sections in partitions other than the current partition are 23470b57cec5SDimitry Andric // ignored. But partition number 255 is a special case: it contains the 23480b57cec5SDimitry Andric // partition end marker (.part.end). It needs to be added to the main 23490b57cec5SDimitry Andric // partition so that a segment is created for it in the main partition, 23500b57cec5SDimitry Andric // which will cause the dynamic loader to reserve space for the other 23510b57cec5SDimitry Andric // partitions. 23520b57cec5SDimitry Andric if (sec->partition != partNo) { 23530b57cec5SDimitry Andric if (isMain && sec->partition == 255) 23540b57cec5SDimitry Andric addHdr(PT_LOAD, computeFlags(sec->getPhdrFlags()))->add(sec); 23550b57cec5SDimitry Andric continue; 23560b57cec5SDimitry Andric } 23570b57cec5SDimitry Andric 23580b57cec5SDimitry Andric // Segments are contiguous memory regions that has the same attributes 23590b57cec5SDimitry Andric // (e.g. executable or writable). There is one phdr for each segment. 23600b57cec5SDimitry Andric // Therefore, we need to create a new phdr when the next section has 23610b57cec5SDimitry Andric // different flags or is loaded at a discontiguous address or memory 23620b57cec5SDimitry Andric // region using AT or AT> linker script command, respectively. At the same 23630b57cec5SDimitry Andric // time, we don't want to create a separate load segment for the headers, 23640b57cec5SDimitry Andric // even if the first output section has an AT or AT> attribute. 23650b57cec5SDimitry Andric uint64_t newFlags = computeFlags(sec->getPhdrFlags()); 23665ffd83dbSDimitry Andric bool sameLMARegion = 23675ffd83dbSDimitry Andric load && !sec->lmaExpr && sec->lmaRegion == load->firstSec->lmaRegion; 23685ffd83dbSDimitry Andric if (!(load && newFlags == flags && sec != relroEnd && 23695ffd83dbSDimitry Andric sec->memRegion == load->firstSec->memRegion && 23705ffd83dbSDimitry Andric (sameLMARegion || load->lastSec == Out::programHeaders))) { 23710b57cec5SDimitry Andric load = addHdr(PT_LOAD, newFlags); 23720b57cec5SDimitry Andric flags = newFlags; 23730b57cec5SDimitry Andric } 23740b57cec5SDimitry Andric 23750b57cec5SDimitry Andric load->add(sec); 23760b57cec5SDimitry Andric } 23770b57cec5SDimitry Andric 23780b57cec5SDimitry Andric // Add a TLS segment if any. 23790b57cec5SDimitry Andric PhdrEntry *tlsHdr = make<PhdrEntry>(PT_TLS, PF_R); 23800b57cec5SDimitry Andric for (OutputSection *sec : outputSections) 23810b57cec5SDimitry Andric if (sec->partition == partNo && sec->flags & SHF_TLS) 23820b57cec5SDimitry Andric tlsHdr->add(sec); 23830b57cec5SDimitry Andric if (tlsHdr->firstSec) 23840b57cec5SDimitry Andric ret.push_back(tlsHdr); 23850b57cec5SDimitry Andric 23860b57cec5SDimitry Andric // Add an entry for .dynamic. 23870b57cec5SDimitry Andric if (OutputSection *sec = part.dynamic->getParent()) 23880b57cec5SDimitry Andric addHdr(PT_DYNAMIC, sec->getPhdrFlags())->add(sec); 23890b57cec5SDimitry Andric 23900b57cec5SDimitry Andric if (relRo->firstSec) 23910b57cec5SDimitry Andric ret.push_back(relRo); 23920b57cec5SDimitry Andric 23930b57cec5SDimitry Andric // PT_GNU_EH_FRAME is a special section pointing on .eh_frame_hdr. 23940b57cec5SDimitry Andric if (part.ehFrame->isNeeded() && part.ehFrameHdr && 23950b57cec5SDimitry Andric part.ehFrame->getParent() && part.ehFrameHdr->getParent()) 23960b57cec5SDimitry Andric addHdr(PT_GNU_EH_FRAME, part.ehFrameHdr->getParent()->getPhdrFlags()) 23970b57cec5SDimitry Andric ->add(part.ehFrameHdr->getParent()); 23980b57cec5SDimitry Andric 23990b57cec5SDimitry Andric // PT_OPENBSD_RANDOMIZE is an OpenBSD-specific feature. That makes 24000b57cec5SDimitry Andric // the dynamic linker fill the segment with random data. 24010b57cec5SDimitry Andric if (OutputSection *cmd = findSection(".openbsd.randomdata", partNo)) 24020b57cec5SDimitry Andric addHdr(PT_OPENBSD_RANDOMIZE, cmd->getPhdrFlags())->add(cmd); 24030b57cec5SDimitry Andric 2404480093f4SDimitry Andric if (config->zGnustack != GnuStackKind::None) { 24050b57cec5SDimitry Andric // PT_GNU_STACK is a special section to tell the loader to make the 24060b57cec5SDimitry Andric // pages for the stack non-executable. If you really want an executable 24070b57cec5SDimitry Andric // stack, you can pass -z execstack, but that's not recommended for 24080b57cec5SDimitry Andric // security reasons. 24090b57cec5SDimitry Andric unsigned perm = PF_R | PF_W; 2410480093f4SDimitry Andric if (config->zGnustack == GnuStackKind::Exec) 24110b57cec5SDimitry Andric perm |= PF_X; 24120b57cec5SDimitry Andric addHdr(PT_GNU_STACK, perm)->p_memsz = config->zStackSize; 2413480093f4SDimitry Andric } 24140b57cec5SDimitry Andric 24150b57cec5SDimitry Andric // PT_OPENBSD_WXNEEDED is a OpenBSD-specific header to mark the executable 24160b57cec5SDimitry Andric // is expected to perform W^X violations, such as calling mprotect(2) or 24170b57cec5SDimitry Andric // mmap(2) with PROT_WRITE | PROT_EXEC, which is prohibited by default on 24180b57cec5SDimitry Andric // OpenBSD. 24190b57cec5SDimitry Andric if (config->zWxneeded) 24200b57cec5SDimitry Andric addHdr(PT_OPENBSD_WXNEEDED, PF_X); 24210b57cec5SDimitry Andric 2422480093f4SDimitry Andric if (OutputSection *cmd = findSection(".note.gnu.property", partNo)) 2423480093f4SDimitry Andric addHdr(PT_GNU_PROPERTY, PF_R)->add(cmd); 2424480093f4SDimitry Andric 24250b57cec5SDimitry Andric // Create one PT_NOTE per a group of contiguous SHT_NOTE sections with the 24260b57cec5SDimitry Andric // same alignment. 24270b57cec5SDimitry Andric PhdrEntry *note = nullptr; 24280b57cec5SDimitry Andric for (OutputSection *sec : outputSections) { 24290b57cec5SDimitry Andric if (sec->partition != partNo) 24300b57cec5SDimitry Andric continue; 24310b57cec5SDimitry Andric if (sec->type == SHT_NOTE && (sec->flags & SHF_ALLOC)) { 24320b57cec5SDimitry Andric if (!note || sec->lmaExpr || note->lastSec->alignment != sec->alignment) 24330b57cec5SDimitry Andric note = addHdr(PT_NOTE, PF_R); 24340b57cec5SDimitry Andric note->add(sec); 24350b57cec5SDimitry Andric } else { 24360b57cec5SDimitry Andric note = nullptr; 24370b57cec5SDimitry Andric } 24380b57cec5SDimitry Andric } 24390b57cec5SDimitry Andric return ret; 24400b57cec5SDimitry Andric } 24410b57cec5SDimitry Andric 24420b57cec5SDimitry Andric template <class ELFT> 24430b57cec5SDimitry Andric void Writer<ELFT>::addPhdrForSection(Partition &part, unsigned shType, 24440b57cec5SDimitry Andric unsigned pType, unsigned pFlags) { 24450b57cec5SDimitry Andric unsigned partNo = part.getNumber(); 24460b57cec5SDimitry Andric auto i = llvm::find_if(outputSections, [=](OutputSection *cmd) { 24470b57cec5SDimitry Andric return cmd->partition == partNo && cmd->type == shType; 24480b57cec5SDimitry Andric }); 24490b57cec5SDimitry Andric if (i == outputSections.end()) 24500b57cec5SDimitry Andric return; 24510b57cec5SDimitry Andric 24520b57cec5SDimitry Andric PhdrEntry *entry = make<PhdrEntry>(pType, pFlags); 24530b57cec5SDimitry Andric entry->add(*i); 24540b57cec5SDimitry Andric part.phdrs.push_back(entry); 24550b57cec5SDimitry Andric } 24560b57cec5SDimitry Andric 245785868e8aSDimitry Andric // Place the first section of each PT_LOAD to a different page (of maxPageSize). 245885868e8aSDimitry Andric // This is achieved by assigning an alignment expression to addrExpr of each 245985868e8aSDimitry Andric // such section. 24600b57cec5SDimitry Andric template <class ELFT> void Writer<ELFT>::fixSectionAlignments() { 246185868e8aSDimitry Andric const PhdrEntry *prev; 246285868e8aSDimitry Andric auto pageAlign = [&](const PhdrEntry *p) { 246385868e8aSDimitry Andric OutputSection *cmd = p->firstSec; 24645ffd83dbSDimitry Andric if (!cmd) 24655ffd83dbSDimitry Andric return; 24665ffd83dbSDimitry Andric cmd->alignExpr = [align = cmd->alignment]() { return align; }; 24675ffd83dbSDimitry Andric if (!cmd->addrExpr) { 246885868e8aSDimitry Andric // Prefer advancing to align(dot, maxPageSize) + dot%maxPageSize to avoid 246985868e8aSDimitry Andric // padding in the file contents. 247085868e8aSDimitry Andric // 247185868e8aSDimitry Andric // When -z separate-code is used we must not have any overlap in pages 247285868e8aSDimitry Andric // between an executable segment and a non-executable segment. We align to 247385868e8aSDimitry Andric // the next maximum page size boundary on transitions between executable 247485868e8aSDimitry Andric // and non-executable segments. 247585868e8aSDimitry Andric // 247685868e8aSDimitry Andric // SHT_LLVM_PART_EHDR marks the start of a partition. The partition 247785868e8aSDimitry Andric // sections will be extracted to a separate file. Align to the next 247885868e8aSDimitry Andric // maximum page size boundary so that we can find the ELF header at the 247985868e8aSDimitry Andric // start. We cannot benefit from overlapping p_offset ranges with the 248085868e8aSDimitry Andric // previous segment anyway. 248185868e8aSDimitry Andric if (config->zSeparate == SeparateSegmentKind::Loadable || 248285868e8aSDimitry Andric (config->zSeparate == SeparateSegmentKind::Code && prev && 248385868e8aSDimitry Andric (prev->p_flags & PF_X) != (p->p_flags & PF_X)) || 248485868e8aSDimitry Andric cmd->type == SHT_LLVM_PART_EHDR) 248585868e8aSDimitry Andric cmd->addrExpr = [] { 2486*972a253aSDimitry Andric return alignToPowerOf2(script->getDot(), config->maxPageSize); 24870b57cec5SDimitry Andric }; 248885868e8aSDimitry Andric // PT_TLS is at the start of the first RW PT_LOAD. If `p` includes PT_TLS, 248985868e8aSDimitry Andric // it must be the RW. Align to p_align(PT_TLS) to make sure 249085868e8aSDimitry Andric // p_vaddr(PT_LOAD)%p_align(PT_LOAD) = 0. Otherwise, if 249185868e8aSDimitry Andric // sh_addralign(.tdata) < sh_addralign(.tbss), we will set p_align(PT_TLS) 249285868e8aSDimitry Andric // to sh_addralign(.tbss), while p_vaddr(PT_TLS)=p_vaddr(PT_LOAD) may not 249385868e8aSDimitry Andric // be congruent to 0 modulo p_align(PT_TLS). 249485868e8aSDimitry Andric // 249585868e8aSDimitry Andric // Technically this is not required, but as of 2019, some dynamic loaders 249685868e8aSDimitry Andric // don't handle p_vaddr%p_align != 0 correctly, e.g. glibc (i386 and 249785868e8aSDimitry Andric // x86-64) doesn't make runtime address congruent to p_vaddr modulo 249885868e8aSDimitry Andric // p_align for dynamic TLS blocks (PR/24606), FreeBSD rtld has the same 249985868e8aSDimitry Andric // bug, musl (TLS Variant 1 architectures) before 1.1.23 handled TLS 250085868e8aSDimitry Andric // blocks correctly. We need to keep the workaround for a while. 250185868e8aSDimitry Andric else if (Out::tlsPhdr && Out::tlsPhdr->firstSec == p->firstSec) 250285868e8aSDimitry Andric cmd->addrExpr = [] { 2503*972a253aSDimitry Andric return alignToPowerOf2(script->getDot(), config->maxPageSize) + 2504*972a253aSDimitry Andric alignToPowerOf2(script->getDot() % config->maxPageSize, 250585868e8aSDimitry Andric Out::tlsPhdr->p_align); 250685868e8aSDimitry Andric }; 250785868e8aSDimitry Andric else 250885868e8aSDimitry Andric cmd->addrExpr = [] { 2509*972a253aSDimitry Andric return alignToPowerOf2(script->getDot(), config->maxPageSize) + 251085868e8aSDimitry Andric script->getDot() % config->maxPageSize; 251185868e8aSDimitry Andric }; 251285868e8aSDimitry Andric } 25130b57cec5SDimitry Andric }; 25140b57cec5SDimitry Andric 25150b57cec5SDimitry Andric for (Partition &part : partitions) { 251685868e8aSDimitry Andric prev = nullptr; 25170b57cec5SDimitry Andric for (const PhdrEntry *p : part.phdrs) 251885868e8aSDimitry Andric if (p->p_type == PT_LOAD && p->firstSec) { 251985868e8aSDimitry Andric pageAlign(p); 252085868e8aSDimitry Andric prev = p; 252185868e8aSDimitry Andric } 25220b57cec5SDimitry Andric } 25230b57cec5SDimitry Andric } 25240b57cec5SDimitry Andric 25250b57cec5SDimitry Andric // Compute an in-file position for a given section. The file offset must be the 25260b57cec5SDimitry Andric // same with its virtual address modulo the page size, so that the loader can 25270b57cec5SDimitry Andric // load executables without any address adjustment. 25280b57cec5SDimitry Andric static uint64_t computeFileOffset(OutputSection *os, uint64_t off) { 25290b57cec5SDimitry Andric // The first section in a PT_LOAD has to have congruent offset and address 253085868e8aSDimitry Andric // modulo the maximum page size. 253185868e8aSDimitry Andric if (os->ptLoad && os->ptLoad->firstSec == os) 253285868e8aSDimitry Andric return alignTo(off, os->ptLoad->p_align, os->addr); 25330b57cec5SDimitry Andric 25340b57cec5SDimitry Andric // File offsets are not significant for .bss sections other than the first one 2535349cc55cSDimitry Andric // in a PT_LOAD/PT_TLS. By convention, we keep section offsets monotonically 25360b57cec5SDimitry Andric // increasing rather than setting to zero. 2537349cc55cSDimitry Andric if (os->type == SHT_NOBITS && 2538349cc55cSDimitry Andric (!Out::tlsPhdr || Out::tlsPhdr->firstSec != os)) 25390b57cec5SDimitry Andric return off; 25400b57cec5SDimitry Andric 25410b57cec5SDimitry Andric // If the section is not in a PT_LOAD, we just have to align it. 25420b57cec5SDimitry Andric if (!os->ptLoad) 2543*972a253aSDimitry Andric return alignToPowerOf2(off, os->alignment); 25440b57cec5SDimitry Andric 25450b57cec5SDimitry Andric // If two sections share the same PT_LOAD the file offset is calculated 25460b57cec5SDimitry Andric // using this formula: Off2 = Off1 + (VA2 - VA1). 25470b57cec5SDimitry Andric OutputSection *first = os->ptLoad->firstSec; 25480b57cec5SDimitry Andric return first->offset + os->addr - first->addr; 25490b57cec5SDimitry Andric } 25500b57cec5SDimitry Andric 25510b57cec5SDimitry Andric template <class ELFT> void Writer<ELFT>::assignFileOffsetsBinary() { 2552e8d8bef9SDimitry Andric // Compute the minimum LMA of all non-empty non-NOBITS sections as minAddr. 2553e8d8bef9SDimitry Andric auto needsOffset = [](OutputSection &sec) { 2554e8d8bef9SDimitry Andric return sec.type != SHT_NOBITS && (sec.flags & SHF_ALLOC) && sec.size > 0; 2555e8d8bef9SDimitry Andric }; 2556e8d8bef9SDimitry Andric uint64_t minAddr = UINT64_MAX; 25570b57cec5SDimitry Andric for (OutputSection *sec : outputSections) 2558e8d8bef9SDimitry Andric if (needsOffset(*sec)) { 2559e8d8bef9SDimitry Andric sec->offset = sec->getLMA(); 2560e8d8bef9SDimitry Andric minAddr = std::min(minAddr, sec->offset); 2561e8d8bef9SDimitry Andric } 2562e8d8bef9SDimitry Andric 2563e8d8bef9SDimitry Andric // Sections are laid out at LMA minus minAddr. 2564e8d8bef9SDimitry Andric fileSize = 0; 2565e8d8bef9SDimitry Andric for (OutputSection *sec : outputSections) 2566e8d8bef9SDimitry Andric if (needsOffset(*sec)) { 2567e8d8bef9SDimitry Andric sec->offset -= minAddr; 2568e8d8bef9SDimitry Andric fileSize = std::max(fileSize, sec->offset + sec->size); 2569e8d8bef9SDimitry Andric } 25700b57cec5SDimitry Andric } 25710b57cec5SDimitry Andric 25720b57cec5SDimitry Andric static std::string rangeToString(uint64_t addr, uint64_t len) { 25730b57cec5SDimitry Andric return "[0x" + utohexstr(addr) + ", 0x" + utohexstr(addr + len - 1) + "]"; 25740b57cec5SDimitry Andric } 25750b57cec5SDimitry Andric 25760b57cec5SDimitry Andric // Assign file offsets to output sections. 25770b57cec5SDimitry Andric template <class ELFT> void Writer<ELFT>::assignFileOffsets() { 25784824e7fdSDimitry Andric Out::programHeaders->offset = Out::elfHeader->size; 25794824e7fdSDimitry Andric uint64_t off = Out::elfHeader->size + Out::programHeaders->size; 25800b57cec5SDimitry Andric 25810b57cec5SDimitry Andric PhdrEntry *lastRX = nullptr; 25820b57cec5SDimitry Andric for (Partition &part : partitions) 25830b57cec5SDimitry Andric for (PhdrEntry *p : part.phdrs) 25840b57cec5SDimitry Andric if (p->p_type == PT_LOAD && (p->p_flags & PF_X)) 25850b57cec5SDimitry Andric lastRX = p; 25860b57cec5SDimitry Andric 2587e8d8bef9SDimitry Andric // Layout SHF_ALLOC sections before non-SHF_ALLOC sections. A non-SHF_ALLOC 2588e8d8bef9SDimitry Andric // will not occupy file offsets contained by a PT_LOAD. 25890b57cec5SDimitry Andric for (OutputSection *sec : outputSections) { 2590e8d8bef9SDimitry Andric if (!(sec->flags & SHF_ALLOC)) 2591e8d8bef9SDimitry Andric continue; 25924824e7fdSDimitry Andric off = computeFileOffset(sec, off); 25934824e7fdSDimitry Andric sec->offset = off; 25944824e7fdSDimitry Andric if (sec->type != SHT_NOBITS) 25954824e7fdSDimitry Andric off += sec->size; 25960b57cec5SDimitry Andric 25970b57cec5SDimitry Andric // If this is a last section of the last executable segment and that 25980b57cec5SDimitry Andric // segment is the last loadable segment, align the offset of the 25990b57cec5SDimitry Andric // following section to avoid loading non-segments parts of the file. 260085868e8aSDimitry Andric if (config->zSeparate != SeparateSegmentKind::None && lastRX && 260185868e8aSDimitry Andric lastRX->lastSec == sec) 2602*972a253aSDimitry Andric off = alignToPowerOf2(off, config->maxPageSize); 26030b57cec5SDimitry Andric } 26044824e7fdSDimitry Andric for (OutputSection *osec : outputSections) 26054824e7fdSDimitry Andric if (!(osec->flags & SHF_ALLOC)) { 2606*972a253aSDimitry Andric osec->offset = alignToPowerOf2(off, osec->alignment); 26074824e7fdSDimitry Andric off = osec->offset + osec->size; 26084824e7fdSDimitry Andric } 26090b57cec5SDimitry Andric 2610*972a253aSDimitry Andric sectionHeaderOff = alignToPowerOf2(off, config->wordsize); 26110b57cec5SDimitry Andric fileSize = sectionHeaderOff + (outputSections.size() + 1) * sizeof(Elf_Shdr); 26120b57cec5SDimitry Andric 26130b57cec5SDimitry Andric // Our logic assumes that sections have rising VA within the same segment. 26140b57cec5SDimitry Andric // With use of linker scripts it is possible to violate this rule and get file 26150b57cec5SDimitry Andric // offset overlaps or overflows. That should never happen with a valid script 26160b57cec5SDimitry Andric // which does not move the location counter backwards and usually scripts do 26170b57cec5SDimitry Andric // not do that. Unfortunately, there are apps in the wild, for example, Linux 26180b57cec5SDimitry Andric // kernel, which control segment distribution explicitly and move the counter 26190b57cec5SDimitry Andric // backwards, so we have to allow doing that to support linking them. We 26200b57cec5SDimitry Andric // perform non-critical checks for overlaps in checkSectionOverlap(), but here 26210b57cec5SDimitry Andric // we want to prevent file size overflows because it would crash the linker. 26220b57cec5SDimitry Andric for (OutputSection *sec : outputSections) { 26230b57cec5SDimitry Andric if (sec->type == SHT_NOBITS) 26240b57cec5SDimitry Andric continue; 26250b57cec5SDimitry Andric if ((sec->offset > fileSize) || (sec->offset + sec->size > fileSize)) 26260b57cec5SDimitry Andric error("unable to place section " + sec->name + " at file offset " + 26270b57cec5SDimitry Andric rangeToString(sec->offset, sec->size) + 26280b57cec5SDimitry Andric "; check your linker script for overflows"); 26290b57cec5SDimitry Andric } 26300b57cec5SDimitry Andric } 26310b57cec5SDimitry Andric 26320b57cec5SDimitry Andric // Finalize the program headers. We call this function after we assign 26330b57cec5SDimitry Andric // file offsets and VAs to all sections. 26340b57cec5SDimitry Andric template <class ELFT> void Writer<ELFT>::setPhdrs(Partition &part) { 26350b57cec5SDimitry Andric for (PhdrEntry *p : part.phdrs) { 26360b57cec5SDimitry Andric OutputSection *first = p->firstSec; 26370b57cec5SDimitry Andric OutputSection *last = p->lastSec; 26380b57cec5SDimitry Andric 26390b57cec5SDimitry Andric if (first) { 26400b57cec5SDimitry Andric p->p_filesz = last->offset - first->offset; 26410b57cec5SDimitry Andric if (last->type != SHT_NOBITS) 26420b57cec5SDimitry Andric p->p_filesz += last->size; 26430b57cec5SDimitry Andric 26440b57cec5SDimitry Andric p->p_memsz = last->addr + last->size - first->addr; 26450b57cec5SDimitry Andric p->p_offset = first->offset; 26460b57cec5SDimitry Andric p->p_vaddr = first->addr; 26470b57cec5SDimitry Andric 26480b57cec5SDimitry Andric // File offsets in partitions other than the main partition are relative 26490b57cec5SDimitry Andric // to the offset of the ELF headers. Perform that adjustment now. 26500b57cec5SDimitry Andric if (part.elfHeader) 26510b57cec5SDimitry Andric p->p_offset -= part.elfHeader->getParent()->offset; 26520b57cec5SDimitry Andric 26530b57cec5SDimitry Andric if (!p->hasLMA) 26540b57cec5SDimitry Andric p->p_paddr = first->getLMA(); 26550b57cec5SDimitry Andric } 26560b57cec5SDimitry Andric 265785868e8aSDimitry Andric if (p->p_type == PT_GNU_RELRO) { 26580b57cec5SDimitry Andric p->p_align = 1; 265985868e8aSDimitry Andric // musl/glibc ld.so rounds the size down, so we need to round up 26600b57cec5SDimitry Andric // to protect the last page. This is a no-op on FreeBSD which always 26610b57cec5SDimitry Andric // rounds up. 2662*972a253aSDimitry Andric p->p_memsz = 2663*972a253aSDimitry Andric alignToPowerOf2(p->p_offset + p->p_memsz, config->commonPageSize) - 266485868e8aSDimitry Andric p->p_offset; 26650b57cec5SDimitry Andric } 26660b57cec5SDimitry Andric } 26670b57cec5SDimitry Andric } 26680b57cec5SDimitry Andric 26690b57cec5SDimitry Andric // A helper struct for checkSectionOverlap. 26700b57cec5SDimitry Andric namespace { 26710b57cec5SDimitry Andric struct SectionOffset { 26720b57cec5SDimitry Andric OutputSection *sec; 26730b57cec5SDimitry Andric uint64_t offset; 26740b57cec5SDimitry Andric }; 26750b57cec5SDimitry Andric } // namespace 26760b57cec5SDimitry Andric 26770b57cec5SDimitry Andric // Check whether sections overlap for a specific address range (file offsets, 2678480093f4SDimitry Andric // load and virtual addresses). 26790b57cec5SDimitry Andric static void checkOverlap(StringRef name, std::vector<SectionOffset> §ions, 26800b57cec5SDimitry Andric bool isVirtualAddr) { 26810b57cec5SDimitry Andric llvm::sort(sections, [=](const SectionOffset &a, const SectionOffset &b) { 26820b57cec5SDimitry Andric return a.offset < b.offset; 26830b57cec5SDimitry Andric }); 26840b57cec5SDimitry Andric 26850b57cec5SDimitry Andric // Finding overlap is easy given a vector is sorted by start position. 26860b57cec5SDimitry Andric // If an element starts before the end of the previous element, they overlap. 26870b57cec5SDimitry Andric for (size_t i = 1, end = sections.size(); i < end; ++i) { 26880b57cec5SDimitry Andric SectionOffset a = sections[i - 1]; 26890b57cec5SDimitry Andric SectionOffset b = sections[i]; 26900b57cec5SDimitry Andric if (b.offset >= a.offset + a.sec->size) 26910b57cec5SDimitry Andric continue; 26920b57cec5SDimitry Andric 26930b57cec5SDimitry Andric // If both sections are in OVERLAY we allow the overlapping of virtual 26940b57cec5SDimitry Andric // addresses, because it is what OVERLAY was designed for. 26950b57cec5SDimitry Andric if (isVirtualAddr && a.sec->inOverlay && b.sec->inOverlay) 26960b57cec5SDimitry Andric continue; 26970b57cec5SDimitry Andric 26980b57cec5SDimitry Andric errorOrWarn("section " + a.sec->name + " " + name + 26990b57cec5SDimitry Andric " range overlaps with " + b.sec->name + "\n>>> " + a.sec->name + 27000b57cec5SDimitry Andric " range is " + rangeToString(a.offset, a.sec->size) + "\n>>> " + 27010b57cec5SDimitry Andric b.sec->name + " range is " + 27020b57cec5SDimitry Andric rangeToString(b.offset, b.sec->size)); 27030b57cec5SDimitry Andric } 27040b57cec5SDimitry Andric } 27050b57cec5SDimitry Andric 27060b57cec5SDimitry Andric // Check for overlapping sections and address overflows. 27070b57cec5SDimitry Andric // 27080b57cec5SDimitry Andric // In this function we check that none of the output sections have overlapping 27090b57cec5SDimitry Andric // file offsets. For SHF_ALLOC sections we also check that the load address 27100b57cec5SDimitry Andric // ranges and the virtual address ranges don't overlap 27110b57cec5SDimitry Andric template <class ELFT> void Writer<ELFT>::checkSections() { 27120b57cec5SDimitry Andric // First, check that section's VAs fit in available address space for target. 27130b57cec5SDimitry Andric for (OutputSection *os : outputSections) 27140b57cec5SDimitry Andric if ((os->addr + os->size < os->addr) || 27150b57cec5SDimitry Andric (!ELFT::Is64Bits && os->addr + os->size > UINT32_MAX)) 27160b57cec5SDimitry Andric errorOrWarn("section " + os->name + " at 0x" + utohexstr(os->addr) + 27170b57cec5SDimitry Andric " of size 0x" + utohexstr(os->size) + 27180b57cec5SDimitry Andric " exceeds available address space"); 27190b57cec5SDimitry Andric 27200b57cec5SDimitry Andric // Check for overlapping file offsets. In this case we need to skip any 27210b57cec5SDimitry Andric // section marked as SHT_NOBITS. These sections don't actually occupy space in 27220b57cec5SDimitry Andric // the file so Sec->Offset + Sec->Size can overlap with others. If --oformat 27230b57cec5SDimitry Andric // binary is specified only add SHF_ALLOC sections are added to the output 27240b57cec5SDimitry Andric // file so we skip any non-allocated sections in that case. 27250b57cec5SDimitry Andric std::vector<SectionOffset> fileOffs; 27260b57cec5SDimitry Andric for (OutputSection *sec : outputSections) 27270b57cec5SDimitry Andric if (sec->size > 0 && sec->type != SHT_NOBITS && 27280b57cec5SDimitry Andric (!config->oFormatBinary || (sec->flags & SHF_ALLOC))) 27290b57cec5SDimitry Andric fileOffs.push_back({sec, sec->offset}); 27300b57cec5SDimitry Andric checkOverlap("file", fileOffs, false); 27310b57cec5SDimitry Andric 27320b57cec5SDimitry Andric // When linking with -r there is no need to check for overlapping virtual/load 27330b57cec5SDimitry Andric // addresses since those addresses will only be assigned when the final 27340b57cec5SDimitry Andric // executable/shared object is created. 27350b57cec5SDimitry Andric if (config->relocatable) 27360b57cec5SDimitry Andric return; 27370b57cec5SDimitry Andric 27380b57cec5SDimitry Andric // Checking for overlapping virtual and load addresses only needs to take 27390b57cec5SDimitry Andric // into account SHF_ALLOC sections since others will not be loaded. 27400b57cec5SDimitry Andric // Furthermore, we also need to skip SHF_TLS sections since these will be 27410b57cec5SDimitry Andric // mapped to other addresses at runtime and can therefore have overlapping 27420b57cec5SDimitry Andric // ranges in the file. 27430b57cec5SDimitry Andric std::vector<SectionOffset> vmas; 27440b57cec5SDimitry Andric for (OutputSection *sec : outputSections) 27450b57cec5SDimitry Andric if (sec->size > 0 && (sec->flags & SHF_ALLOC) && !(sec->flags & SHF_TLS)) 27460b57cec5SDimitry Andric vmas.push_back({sec, sec->addr}); 27470b57cec5SDimitry Andric checkOverlap("virtual address", vmas, true); 27480b57cec5SDimitry Andric 27490b57cec5SDimitry Andric // Finally, check that the load addresses don't overlap. This will usually be 27500b57cec5SDimitry Andric // the same as the virtual addresses but can be different when using a linker 27510b57cec5SDimitry Andric // script with AT(). 27520b57cec5SDimitry Andric std::vector<SectionOffset> lmas; 27530b57cec5SDimitry Andric for (OutputSection *sec : outputSections) 27540b57cec5SDimitry Andric if (sec->size > 0 && (sec->flags & SHF_ALLOC) && !(sec->flags & SHF_TLS)) 27550b57cec5SDimitry Andric lmas.push_back({sec, sec->getLMA()}); 27560b57cec5SDimitry Andric checkOverlap("load address", lmas, false); 27570b57cec5SDimitry Andric } 27580b57cec5SDimitry Andric 27590b57cec5SDimitry Andric // The entry point address is chosen in the following ways. 27600b57cec5SDimitry Andric // 27610b57cec5SDimitry Andric // 1. the '-e' entry command-line option; 27620b57cec5SDimitry Andric // 2. the ENTRY(symbol) command in a linker control script; 27630b57cec5SDimitry Andric // 3. the value of the symbol _start, if present; 27640b57cec5SDimitry Andric // 4. the number represented by the entry symbol, if it is a number; 2765349cc55cSDimitry Andric // 5. the address 0. 27660b57cec5SDimitry Andric static uint64_t getEntryAddr() { 27670b57cec5SDimitry Andric // Case 1, 2 or 3 27680b57cec5SDimitry Andric if (Symbol *b = symtab->find(config->entry)) 27690b57cec5SDimitry Andric return b->getVA(); 27700b57cec5SDimitry Andric 27710b57cec5SDimitry Andric // Case 4 27720b57cec5SDimitry Andric uint64_t addr; 27730b57cec5SDimitry Andric if (to_integer(config->entry, addr)) 27740b57cec5SDimitry Andric return addr; 27750b57cec5SDimitry Andric 27760b57cec5SDimitry Andric // Case 5 27770b57cec5SDimitry Andric if (config->warnMissingEntry) 27780b57cec5SDimitry Andric warn("cannot find entry symbol " + config->entry + 27790b57cec5SDimitry Andric "; not setting start address"); 27800b57cec5SDimitry Andric return 0; 27810b57cec5SDimitry Andric } 27820b57cec5SDimitry Andric 27830b57cec5SDimitry Andric static uint16_t getELFType() { 27840b57cec5SDimitry Andric if (config->isPic) 27850b57cec5SDimitry Andric return ET_DYN; 27860b57cec5SDimitry Andric if (config->relocatable) 27870b57cec5SDimitry Andric return ET_REL; 27880b57cec5SDimitry Andric return ET_EXEC; 27890b57cec5SDimitry Andric } 27900b57cec5SDimitry Andric 27910b57cec5SDimitry Andric template <class ELFT> void Writer<ELFT>::writeHeader() { 27920b57cec5SDimitry Andric writeEhdr<ELFT>(Out::bufferStart, *mainPart); 27930b57cec5SDimitry Andric writePhdrs<ELFT>(Out::bufferStart + sizeof(Elf_Ehdr), *mainPart); 27940b57cec5SDimitry Andric 27950b57cec5SDimitry Andric auto *eHdr = reinterpret_cast<Elf_Ehdr *>(Out::bufferStart); 27960b57cec5SDimitry Andric eHdr->e_type = getELFType(); 27970b57cec5SDimitry Andric eHdr->e_entry = getEntryAddr(); 27980b57cec5SDimitry Andric eHdr->e_shoff = sectionHeaderOff; 27990b57cec5SDimitry Andric 28000b57cec5SDimitry Andric // Write the section header table. 28010b57cec5SDimitry Andric // 28020b57cec5SDimitry Andric // The ELF header can only store numbers up to SHN_LORESERVE in the e_shnum 28030b57cec5SDimitry Andric // and e_shstrndx fields. When the value of one of these fields exceeds 28040b57cec5SDimitry Andric // SHN_LORESERVE ELF requires us to put sentinel values in the ELF header and 28050b57cec5SDimitry Andric // use fields in the section header at index 0 to store 28060b57cec5SDimitry Andric // the value. The sentinel values and fields are: 28070b57cec5SDimitry Andric // e_shnum = 0, SHdrs[0].sh_size = number of sections. 28080b57cec5SDimitry Andric // e_shstrndx = SHN_XINDEX, SHdrs[0].sh_link = .shstrtab section index. 28090b57cec5SDimitry Andric auto *sHdrs = reinterpret_cast<Elf_Shdr *>(Out::bufferStart + eHdr->e_shoff); 28100b57cec5SDimitry Andric size_t num = outputSections.size() + 1; 28110b57cec5SDimitry Andric if (num >= SHN_LORESERVE) 28120b57cec5SDimitry Andric sHdrs->sh_size = num; 28130b57cec5SDimitry Andric else 28140b57cec5SDimitry Andric eHdr->e_shnum = num; 28150b57cec5SDimitry Andric 28160b57cec5SDimitry Andric uint32_t strTabIndex = in.shStrTab->getParent()->sectionIndex; 28170b57cec5SDimitry Andric if (strTabIndex >= SHN_LORESERVE) { 28180b57cec5SDimitry Andric sHdrs->sh_link = strTabIndex; 28190b57cec5SDimitry Andric eHdr->e_shstrndx = SHN_XINDEX; 28200b57cec5SDimitry Andric } else { 28210b57cec5SDimitry Andric eHdr->e_shstrndx = strTabIndex; 28220b57cec5SDimitry Andric } 28230b57cec5SDimitry Andric 28240b57cec5SDimitry Andric for (OutputSection *sec : outputSections) 28250b57cec5SDimitry Andric sec->writeHeaderTo<ELFT>(++sHdrs); 28260b57cec5SDimitry Andric } 28270b57cec5SDimitry Andric 28280b57cec5SDimitry Andric // Open a result file. 28290b57cec5SDimitry Andric template <class ELFT> void Writer<ELFT>::openFile() { 28300b57cec5SDimitry Andric uint64_t maxSize = config->is64 ? INT64_MAX : UINT32_MAX; 28310b57cec5SDimitry Andric if (fileSize != size_t(fileSize) || maxSize < fileSize) { 2832e8d8bef9SDimitry Andric std::string msg; 2833e8d8bef9SDimitry Andric raw_string_ostream s(msg); 2834e8d8bef9SDimitry Andric s << "output file too large: " << Twine(fileSize) << " bytes\n" 2835e8d8bef9SDimitry Andric << "section sizes:\n"; 2836e8d8bef9SDimitry Andric for (OutputSection *os : outputSections) 2837e8d8bef9SDimitry Andric s << os->name << ' ' << os->size << "\n"; 2838e8d8bef9SDimitry Andric error(s.str()); 28390b57cec5SDimitry Andric return; 28400b57cec5SDimitry Andric } 28410b57cec5SDimitry Andric 28420b57cec5SDimitry Andric unlinkAsync(config->outputFile); 28430b57cec5SDimitry Andric unsigned flags = 0; 28440b57cec5SDimitry Andric if (!config->relocatable) 2845480093f4SDimitry Andric flags |= FileOutputBuffer::F_executable; 2846480093f4SDimitry Andric if (!config->mmapOutputFile) 2847480093f4SDimitry Andric flags |= FileOutputBuffer::F_no_mmap; 28480b57cec5SDimitry Andric Expected<std::unique_ptr<FileOutputBuffer>> bufferOrErr = 28490b57cec5SDimitry Andric FileOutputBuffer::create(config->outputFile, fileSize, flags); 28500b57cec5SDimitry Andric 28510b57cec5SDimitry Andric if (!bufferOrErr) { 28520b57cec5SDimitry Andric error("failed to open " + config->outputFile + ": " + 28530b57cec5SDimitry Andric llvm::toString(bufferOrErr.takeError())); 28540b57cec5SDimitry Andric return; 28550b57cec5SDimitry Andric } 28560b57cec5SDimitry Andric buffer = std::move(*bufferOrErr); 28570b57cec5SDimitry Andric Out::bufferStart = buffer->getBufferStart(); 28580b57cec5SDimitry Andric } 28590b57cec5SDimitry Andric 28600b57cec5SDimitry Andric template <class ELFT> void Writer<ELFT>::writeSectionsBinary() { 28610b57cec5SDimitry Andric for (OutputSection *sec : outputSections) 28620b57cec5SDimitry Andric if (sec->flags & SHF_ALLOC) 28630b57cec5SDimitry Andric sec->writeTo<ELFT>(Out::bufferStart + sec->offset); 28640b57cec5SDimitry Andric } 28650b57cec5SDimitry Andric 28660b57cec5SDimitry Andric static void fillTrap(uint8_t *i, uint8_t *end) { 28670b57cec5SDimitry Andric for (; i + 4 <= end; i += 4) 28680b57cec5SDimitry Andric memcpy(i, &target->trapInstr, 4); 28690b57cec5SDimitry Andric } 28700b57cec5SDimitry Andric 28710b57cec5SDimitry Andric // Fill the last page of executable segments with trap instructions 28720b57cec5SDimitry Andric // instead of leaving them as zero. Even though it is not required by any 28730b57cec5SDimitry Andric // standard, it is in general a good thing to do for security reasons. 28740b57cec5SDimitry Andric // 28750b57cec5SDimitry Andric // We'll leave other pages in segments as-is because the rest will be 28760b57cec5SDimitry Andric // overwritten by output sections. 28770b57cec5SDimitry Andric template <class ELFT> void Writer<ELFT>::writeTrapInstr() { 28780b57cec5SDimitry Andric for (Partition &part : partitions) { 28790b57cec5SDimitry Andric // Fill the last page. 28800b57cec5SDimitry Andric for (PhdrEntry *p : part.phdrs) 28810b57cec5SDimitry Andric if (p->p_type == PT_LOAD && (p->p_flags & PF_X)) 288204eeddc0SDimitry Andric fillTrap(Out::bufferStart + 288304eeddc0SDimitry Andric alignDown(p->firstSec->offset + p->p_filesz, 4), 2884*972a253aSDimitry Andric Out::bufferStart + 2885*972a253aSDimitry Andric alignToPowerOf2(p->firstSec->offset + p->p_filesz, 28864824e7fdSDimitry Andric config->maxPageSize)); 28870b57cec5SDimitry Andric 28880b57cec5SDimitry Andric // Round up the file size of the last segment to the page boundary iff it is 28890b57cec5SDimitry Andric // an executable segment to ensure that other tools don't accidentally 28900b57cec5SDimitry Andric // trim the instruction padding (e.g. when stripping the file). 28910b57cec5SDimitry Andric PhdrEntry *last = nullptr; 28920b57cec5SDimitry Andric for (PhdrEntry *p : part.phdrs) 28930b57cec5SDimitry Andric if (p->p_type == PT_LOAD) 28940b57cec5SDimitry Andric last = p; 28950b57cec5SDimitry Andric 28960b57cec5SDimitry Andric if (last && (last->p_flags & PF_X)) 28970b57cec5SDimitry Andric last->p_memsz = last->p_filesz = 2898*972a253aSDimitry Andric alignToPowerOf2(last->p_filesz, config->maxPageSize); 28990b57cec5SDimitry Andric } 29000b57cec5SDimitry Andric } 29010b57cec5SDimitry Andric 29020b57cec5SDimitry Andric // Write section contents to a mmap'ed file. 29030b57cec5SDimitry Andric template <class ELFT> void Writer<ELFT>::writeSections() { 29040eae32dcSDimitry Andric llvm::TimeTraceScope timeScope("Write sections"); 29050eae32dcSDimitry Andric 2906349cc55cSDimitry Andric // In -r or --emit-relocs mode, write the relocation sections first as in 29070b57cec5SDimitry Andric // ELf_Rel targets we might find out that we need to modify the relocated 29080b57cec5SDimitry Andric // section while doing it. 29090b57cec5SDimitry Andric for (OutputSection *sec : outputSections) 29100b57cec5SDimitry Andric if (sec->type == SHT_REL || sec->type == SHT_RELA) 29110b57cec5SDimitry Andric sec->writeTo<ELFT>(Out::bufferStart + sec->offset); 29120b57cec5SDimitry Andric 29130b57cec5SDimitry Andric for (OutputSection *sec : outputSections) 29140b57cec5SDimitry Andric if (sec->type != SHT_REL && sec->type != SHT_RELA) 29150b57cec5SDimitry Andric sec->writeTo<ELFT>(Out::bufferStart + sec->offset); 29160b57cec5SDimitry Andric 2917fe6060f1SDimitry Andric // Finally, check that all dynamic relocation addends were written correctly. 2918fe6060f1SDimitry Andric if (config->checkDynamicRelocs && config->writeAddends) { 2919fe6060f1SDimitry Andric for (OutputSection *sec : outputSections) 2920fe6060f1SDimitry Andric if (sec->type == SHT_REL || sec->type == SHT_RELA) 2921fe6060f1SDimitry Andric sec->checkDynRelAddends(Out::bufferStart); 29220b57cec5SDimitry Andric } 29230b57cec5SDimitry Andric } 29240b57cec5SDimitry Andric 29250b57cec5SDimitry Andric // Computes a hash value of Data using a given hash function. 29260b57cec5SDimitry Andric // In order to utilize multiple cores, we first split data into 1MB 29270b57cec5SDimitry Andric // chunks, compute a hash for each chunk, and then compute a hash value 29280b57cec5SDimitry Andric // of the hash values. 29290b57cec5SDimitry Andric static void 29300b57cec5SDimitry Andric computeHash(llvm::MutableArrayRef<uint8_t> hashBuf, 29310b57cec5SDimitry Andric llvm::ArrayRef<uint8_t> data, 29320b57cec5SDimitry Andric std::function<void(uint8_t *dest, ArrayRef<uint8_t> arr)> hashFn) { 29330b57cec5SDimitry Andric std::vector<ArrayRef<uint8_t>> chunks = split(data, 1024 * 1024); 293404eeddc0SDimitry Andric const size_t hashesSize = chunks.size() * hashBuf.size(); 293504eeddc0SDimitry Andric std::unique_ptr<uint8_t[]> hashes(new uint8_t[hashesSize]); 29360b57cec5SDimitry Andric 29370b57cec5SDimitry Andric // Compute hash values. 293881ad6265SDimitry Andric parallelFor(0, chunks.size(), [&](size_t i) { 293904eeddc0SDimitry Andric hashFn(hashes.get() + i * hashBuf.size(), chunks[i]); 29400b57cec5SDimitry Andric }); 29410b57cec5SDimitry Andric 29420b57cec5SDimitry Andric // Write to the final output buffer. 294304eeddc0SDimitry Andric hashFn(hashBuf.data(), makeArrayRef(hashes.get(), hashesSize)); 29440b57cec5SDimitry Andric } 29450b57cec5SDimitry Andric 29460b57cec5SDimitry Andric template <class ELFT> void Writer<ELFT>::writeBuildId() { 29470b57cec5SDimitry Andric if (!mainPart->buildId || !mainPart->buildId->getParent()) 29480b57cec5SDimitry Andric return; 29490b57cec5SDimitry Andric 29500b57cec5SDimitry Andric if (config->buildId == BuildIdKind::Hexstring) { 29510b57cec5SDimitry Andric for (Partition &part : partitions) 29520b57cec5SDimitry Andric part.buildId->writeBuildId(config->buildIdVector); 29530b57cec5SDimitry Andric return; 29540b57cec5SDimitry Andric } 29550b57cec5SDimitry Andric 29560b57cec5SDimitry Andric // Compute a hash of all sections of the output file. 29570b57cec5SDimitry Andric size_t hashSize = mainPart->buildId->hashSize; 295804eeddc0SDimitry Andric std::unique_ptr<uint8_t[]> buildId(new uint8_t[hashSize]); 295904eeddc0SDimitry Andric MutableArrayRef<uint8_t> output(buildId.get(), hashSize); 296004eeddc0SDimitry Andric llvm::ArrayRef<uint8_t> input{Out::bufferStart, size_t(fileSize)}; 29610b57cec5SDimitry Andric 296281ad6265SDimitry Andric // Fedora introduced build ID as "approximation of true uniqueness across all 296381ad6265SDimitry Andric // binaries that might be used by overlapping sets of people". It does not 296481ad6265SDimitry Andric // need some security goals that some hash algorithms strive to provide, e.g. 296581ad6265SDimitry Andric // (second-)preimage and collision resistance. In practice people use 'md5' 296681ad6265SDimitry Andric // and 'sha1' just for different lengths. Implement them with the more 296781ad6265SDimitry Andric // efficient BLAKE3. 29680b57cec5SDimitry Andric switch (config->buildId) { 29690b57cec5SDimitry Andric case BuildIdKind::Fast: 297004eeddc0SDimitry Andric computeHash(output, input, [](uint8_t *dest, ArrayRef<uint8_t> arr) { 29710b57cec5SDimitry Andric write64le(dest, xxHash64(arr)); 29720b57cec5SDimitry Andric }); 29730b57cec5SDimitry Andric break; 29740b57cec5SDimitry Andric case BuildIdKind::Md5: 297504eeddc0SDimitry Andric computeHash(output, input, [&](uint8_t *dest, ArrayRef<uint8_t> arr) { 297681ad6265SDimitry Andric memcpy(dest, BLAKE3::hash<16>(arr).data(), hashSize); 29770b57cec5SDimitry Andric }); 29780b57cec5SDimitry Andric break; 29790b57cec5SDimitry Andric case BuildIdKind::Sha1: 298004eeddc0SDimitry Andric computeHash(output, input, [&](uint8_t *dest, ArrayRef<uint8_t> arr) { 298181ad6265SDimitry Andric memcpy(dest, BLAKE3::hash<20>(arr).data(), hashSize); 29820b57cec5SDimitry Andric }); 29830b57cec5SDimitry Andric break; 29840b57cec5SDimitry Andric case BuildIdKind::Uuid: 298504eeddc0SDimitry Andric if (auto ec = llvm::getRandomBytes(buildId.get(), hashSize)) 29860b57cec5SDimitry Andric error("entropy source failure: " + ec.message()); 29870b57cec5SDimitry Andric break; 29880b57cec5SDimitry Andric default: 29890b57cec5SDimitry Andric llvm_unreachable("unknown BuildIdKind"); 29900b57cec5SDimitry Andric } 29910b57cec5SDimitry Andric for (Partition &part : partitions) 299204eeddc0SDimitry Andric part.buildId->writeBuildId(output); 29930b57cec5SDimitry Andric } 29940b57cec5SDimitry Andric 29955ffd83dbSDimitry Andric template void elf::createSyntheticSections<ELF32LE>(); 29965ffd83dbSDimitry Andric template void elf::createSyntheticSections<ELF32BE>(); 29975ffd83dbSDimitry Andric template void elf::createSyntheticSections<ELF64LE>(); 29985ffd83dbSDimitry Andric template void elf::createSyntheticSections<ELF64BE>(); 299985868e8aSDimitry Andric 30005ffd83dbSDimitry Andric template void elf::writeResult<ELF32LE>(); 30015ffd83dbSDimitry Andric template void elf::writeResult<ELF32BE>(); 30025ffd83dbSDimitry Andric template void elf::writeResult<ELF64LE>(); 30035ffd83dbSDimitry Andric template void elf::writeResult<ELF64BE>(); 3004