brintos

brintos / llvm-project-archived public Read only

0
0
Text · 119.6 KiB · 083b4fb Raw
3109 lines · cpp
1//===- Writer.cpp ---------------------------------------------------------===//2//3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.4// See https://llvm.org/LICENSE.txt for license information.5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception6//7//===----------------------------------------------------------------------===//8 9#include "Writer.h"10#include "AArch64ErrataFix.h"11#include "ARMErrataFix.h"12#include "BPSectionOrderer.h"13#include "CallGraphSort.h"14#include "Config.h"15#include "InputFiles.h"16#include "LinkerScript.h"17#include "MapFile.h"18#include "OutputSections.h"19#include "Relocations.h"20#include "SymbolTable.h"21#include "Symbols.h"22#include "SyntheticSections.h"23#include "Target.h"24#include "lld/Common/Arrays.h"25#include "lld/Common/CommonLinkerContext.h"26#include "lld/Common/Filesystem.h"27#include "lld/Common/Strings.h"28#include "llvm/ADT/STLExtras.h"29#include "llvm/ADT/StringMap.h"30#include "llvm/Support/BLAKE3.h"31#include "llvm/Support/Parallel.h"32#include "llvm/Support/RandomNumberGenerator.h"33#include "llvm/Support/TimeProfiler.h"34#include "llvm/Support/xxhash.h"35#include <climits>36 37#define DEBUG_TYPE "lld"38 39using namespace llvm;40using namespace llvm::ELF;41using namespace llvm::object;42using namespace llvm::support;43using namespace llvm::support::endian;44using namespace lld;45using namespace lld::elf;46 47namespace {48// The writer writes a SymbolTable result to a file.49template <class ELFT> class Writer {50public:51  LLVM_ELF_IMPORT_TYPES_ELFT(ELFT)52 53  Writer(Ctx &ctx) : ctx(ctx), buffer(ctx.e.outputBuffer), tc(ctx) {}54 55  void run();56 57private:58  void addSectionSymbols();59  void sortSections();60  void resolveShfLinkOrder();61  void finalizeAddressDependentContent();62  void optimizeBasicBlockJumps();63  void sortInputSections();64  void sortOrphanSections();65  void finalizeSections();66  void checkExecuteOnly();67  void checkExecuteOnlyReport();68  void setReservedSymbolSections();69 70  SmallVector<std::unique_ptr<PhdrEntry>, 0> createPhdrs(Partition &part);71  void addPhdrForSection(Partition &part, unsigned shType, unsigned pType,72                         unsigned pFlags);73  void assignFileOffsets();74  void assignFileOffsetsBinary();75  void setPhdrs(Partition &part);76  void checkSections();77  void fixSectionAlignments();78  void openFile();79  void writeTrapInstr();80  void writeHeader();81  void writeSections();82  void writeSectionsBinary();83  void writeBuildId();84 85  Ctx &ctx;86  std::unique_ptr<FileOutputBuffer> &buffer;87  // ThunkCreator holds Thunks that are used at writeTo time.88  ThunkCreator tc;89 90  void addRelIpltSymbols();91  void addStartEndSymbols();92  void addStartStopSymbols(OutputSection &osec);93 94  uint64_t fileSize;95  uint64_t sectionHeaderOff;96};97} // anonymous namespace98 99template <class ELFT> void elf::writeResult(Ctx &ctx) {100  Writer<ELFT>(ctx).run();101}102 103static void104removeEmptyPTLoad(Ctx &ctx, SmallVector<std::unique_ptr<PhdrEntry>, 0> &phdrs) {105  auto it = std::stable_partition(phdrs.begin(), phdrs.end(), [&](auto &p) {106    if (p->p_type != PT_LOAD)107      return true;108    if (!p->firstSec)109      return false;110    uint64_t size = p->lastSec->addr + p->lastSec->size - p->firstSec->addr;111    return size != 0;112  });113 114  // Clear OutputSection::ptLoad for sections contained in removed115  // segments.116  DenseSet<PhdrEntry *> removed;117  for (auto it2 = it; it2 != phdrs.end(); ++it2)118    removed.insert(it2->get());119  for (OutputSection *sec : ctx.outputSections)120    if (removed.count(sec->ptLoad))121      sec->ptLoad = nullptr;122  phdrs.erase(it, phdrs.end());123}124 125void elf::copySectionsIntoPartitions(Ctx &ctx) {126  SmallVector<InputSectionBase *, 0> newSections;127  const size_t ehSize = ctx.ehInputSections.size();128  for (unsigned part = 2; part != ctx.partitions.size() + 1; ++part) {129    for (InputSectionBase *s : ctx.inputSections) {130      if (!(s->flags & SHF_ALLOC) || !s->isLive() || s->type != SHT_NOTE)131        continue;132      auto *copy = make<InputSection>(cast<InputSection>(*s));133      copy->partition = part;134      newSections.push_back(copy);135    }136    for (size_t i = 0; i != ehSize; ++i) {137      assert(ctx.ehInputSections[i]->isLive());138      auto *copy = make<EhInputSection>(*ctx.ehInputSections[i]);139      copy->partition = part;140      ctx.ehInputSections.push_back(copy);141    }142  }143 144  ctx.inputSections.insert(ctx.inputSections.end(), newSections.begin(),145                           newSections.end());146}147 148static Defined *addOptionalRegular(Ctx &ctx, StringRef name, SectionBase *sec,149                                   uint64_t val, uint8_t stOther = STV_HIDDEN) {150  Symbol *s = ctx.symtab->find(name);151  if (!s || s->isDefined() || s->isCommon())152    return nullptr;153 154  ctx.synthesizedSymbols.push_back(s);155  s->resolve(ctx, Defined{ctx, ctx.internalFile, StringRef(), STB_GLOBAL,156                          stOther, STT_NOTYPE, val,157                          /*size=*/0, sec});158  s->isUsedInRegularObj = true;159  return cast<Defined>(s);160}161 162// The linker is expected to define some symbols depending on163// the linking result. This function defines such symbols.164void elf::addReservedSymbols(Ctx &ctx) {165  if (ctx.arg.emachine == EM_MIPS) {166    auto addAbsolute = [&](StringRef name) {167      Symbol *sym =168          ctx.symtab->addSymbol(Defined{ctx, ctx.internalFile, name, STB_GLOBAL,169                                        STV_HIDDEN, STT_NOTYPE, 0, 0, nullptr});170      sym->isUsedInRegularObj = true;171      return cast<Defined>(sym);172    };173    // Define _gp for MIPS. st_value of _gp symbol will be updated by Writer174    // so that it points to an absolute address which by default is relative175    // to GOT. Default offset is 0x7ff0.176    // See "Global Data Symbols" in Chapter 6 in the following document:177    // ftp://www.linux-mips.org/pub/linux/mips/doc/ABI/mipsabi.pdf178    ctx.sym.mipsGp = addAbsolute("_gp");179 180    // On MIPS O32 ABI, _gp_disp is a magic symbol designates offset between181    // start of function and 'gp' pointer into GOT.182    if (ctx.symtab->find("_gp_disp"))183      ctx.sym.mipsGpDisp = addAbsolute("_gp_disp");184 185    // The __gnu_local_gp is a magic symbol equal to the current value of 'gp'186    // pointer. This symbol is used in the code generated by .cpload pseudo-op187    // in case of using -mno-shared option.188    // https://sourceware.org/ml/binutils/2004-12/msg00094.html189    if (ctx.symtab->find("__gnu_local_gp"))190      ctx.sym.mipsLocalGp = addAbsolute("__gnu_local_gp");191  } else if (ctx.arg.emachine == EM_PPC) {192    // glibc *crt1.o has a undefined reference to _SDA_BASE_. Since we don't193    // support Small Data Area, define it arbitrarily as 0.194    addOptionalRegular(ctx, "_SDA_BASE_", nullptr, 0, STV_HIDDEN);195  } else if (ctx.arg.emachine == EM_PPC64) {196    addPPC64SaveRestore(ctx);197  }198 199  // The Power Architecture 64-bit v2 ABI defines a TableOfContents (TOC) which200  // combines the typical ELF GOT with the small data sections. It commonly201  // includes .got .toc .sdata .sbss. The .TOC. symbol replaces both202  // _GLOBAL_OFFSET_TABLE_ and _SDA_BASE_ from the 32-bit ABI. It is used to203  // represent the TOC base which is offset by 0x8000 bytes from the start of204  // the .got section.205  // We do not allow _GLOBAL_OFFSET_TABLE_ to be defined by input objects as the206  // correctness of some relocations depends on its value.207  StringRef gotSymName =208      (ctx.arg.emachine == EM_PPC64) ? ".TOC." : "_GLOBAL_OFFSET_TABLE_";209 210  if (Symbol *s = ctx.symtab->find(gotSymName)) {211    if (s->isDefined()) {212      ErrAlways(ctx) << s->file << " cannot redefine linker defined symbol '"213                     << gotSymName << "'";214      return;215    }216 217    uint64_t gotOff = 0;218    if (ctx.arg.emachine == EM_PPC64)219      gotOff = 0x8000;220 221    s->resolve(ctx, Defined{ctx, ctx.internalFile, StringRef(), STB_GLOBAL,222                            STV_HIDDEN, STT_NOTYPE, gotOff, /*size=*/0,223                            ctx.out.elfHeader.get()});224    ctx.sym.globalOffsetTable = cast<Defined>(s);225  }226 227  // __ehdr_start is the location of ELF file headers. Note that we define228  // this symbol unconditionally even when using a linker script, which229  // differs from the behavior implemented by GNU linker which only define230  // this symbol if ELF headers are in the memory mapped segment.231  addOptionalRegular(ctx, "__ehdr_start", ctx.out.elfHeader.get(), 0,232                     STV_HIDDEN);233 234  // __executable_start is not documented, but the expectation of at235  // least the Android libc is that it points to the ELF header.236  addOptionalRegular(ctx, "__executable_start", ctx.out.elfHeader.get(), 0,237                     STV_HIDDEN);238 239  // __dso_handle symbol is passed to cxa_finalize as a marker to identify240  // each DSO. The address of the symbol doesn't matter as long as they are241  // different in different DSOs, so we chose the start address of the DSO.242  addOptionalRegular(ctx, "__dso_handle", ctx.out.elfHeader.get(), 0,243                     STV_HIDDEN);244 245  // If linker script do layout we do not need to create any standard symbols.246  if (ctx.script->hasSectionsCommand)247    return;248 249  auto add = [&](StringRef s, int64_t pos) {250    return addOptionalRegular(ctx, s, ctx.out.elfHeader.get(), pos,251                              STV_DEFAULT);252  };253 254  ctx.sym.bss = add("__bss_start", 0);255  ctx.sym.end1 = add("end", -1);256  ctx.sym.end2 = add("_end", -1);257  ctx.sym.etext1 = add("etext", -1);258  ctx.sym.etext2 = add("_etext", -1);259  ctx.sym.edata1 = add("edata", -1);260  ctx.sym.edata2 = add("_edata", -1);261}262 263static void demoteDefined(Defined &sym, DenseMap<SectionBase *, size_t> &map) {264  if (map.empty())265    for (auto [i, sec] : llvm::enumerate(sym.file->getSections()))266      map.try_emplace(sec, i);267  // Change WEAK to GLOBAL so that if a scanned relocation references sym,268  // maybeReportUndefined will report an error.269  uint8_t binding = sym.isWeak() ? uint8_t(STB_GLOBAL) : sym.binding;270  Undefined(sym.file, sym.getName(), binding, sym.stOther, sym.type,271            /*discardedSecIdx=*/map.lookup(sym.section))272      .overwrite(sym);273  // Eliminate from the symbol table, otherwise we would leave an undefined274  // symbol if the symbol is unreferenced in the absence of GC.275  sym.isUsedInRegularObj = false;276}277 278// If all references to a DSO happen to be weak, the DSO is not added to279// DT_NEEDED. If that happens, replace ShardSymbol with Undefined to avoid280// dangling references to an unneeded DSO. Use a weak binding to avoid281// --no-allow-shlib-undefined diagnostics. Similarly, demote lazy symbols.282//283// In addition, demote symbols defined in discarded sections, so that284// references to /DISCARD/ discarded symbols will lead to errors.285static void demoteSymbolsAndComputeIsPreemptible(Ctx &ctx) {286  llvm::TimeTraceScope timeScope("Demote symbols");287  DenseMap<InputFile *, DenseMap<SectionBase *, size_t>> sectionIndexMap;288  for (Symbol *sym : ctx.symtab->getSymbols()) {289    if (auto *d = dyn_cast<Defined>(sym)) {290      if (d->section && !d->section->isLive())291        demoteDefined(*d, sectionIndexMap[d->file]);292    } else {293      auto *s = dyn_cast<SharedSymbol>(sym);294      if (sym->isLazy() || (s && !cast<SharedFile>(s->file)->isNeeded)) {295        uint8_t binding = sym->isLazy() ? sym->binding : uint8_t(STB_WEAK);296        Undefined(ctx.internalFile, sym->getName(), binding, sym->stOther,297                  sym->type)298            .overwrite(*sym);299        sym->versionId = VER_NDX_GLOBAL;300      }301    }302 303    sym->isPreemptible = (sym->isUndefined() || sym->isExported) &&304                         computeIsPreemptible(ctx, *sym);305  }306}307 308static OutputSection *findSection(Ctx &ctx, StringRef name,309                                  unsigned partition = 1) {310  for (SectionCommand *cmd : ctx.script->sectionCommands)311    if (auto *osd = dyn_cast<OutputDesc>(cmd))312      if (osd->osec.name == name && osd->osec.partition == partition)313        return &osd->osec;314  return nullptr;315}316 317// The main function of the writer.318template <class ELFT> void Writer<ELFT>::run() {319  // Now that we have a complete set of output sections. This function320  // completes section contents. For example, we need to add strings321  // to the string table, and add entries to .got and .plt.322  // finalizeSections does that.323  finalizeSections();324  checkExecuteOnly();325  checkExecuteOnlyReport();326 327  // If --compressed-debug-sections is specified, compress .debug_* sections.328  // Do it right now because it changes the size of output sections.329  for (OutputSection *sec : ctx.outputSections)330    sec->maybeCompress<ELFT>(ctx);331 332  if (ctx.script->hasSectionsCommand)333    ctx.script->allocateHeaders(ctx.mainPart->phdrs);334 335  // Remove empty PT_LOAD to avoid causing the dynamic linker to try to mmap a336  // 0 sized region. This has to be done late since only after assignAddresses337  // we know the size of the sections.338  for (Partition &part : ctx.partitions)339    removeEmptyPTLoad(ctx, part.phdrs);340 341  if (!ctx.arg.oFormatBinary)342    assignFileOffsets();343  else344    assignFileOffsetsBinary();345 346  for (Partition &part : ctx.partitions)347    setPhdrs(part);348 349  // Handle --print-map(-M)/--Map and --cref. Dump them before checkSections()350  // because the files may be useful in case checkSections() or openFile()351  // fails, for example, due to an erroneous file size.352  writeMapAndCref(ctx);353 354  // Handle --print-memory-usage option.355  if (ctx.arg.printMemoryUsage)356    ctx.script->printMemoryUsage(ctx.e.outs());357 358  if (ctx.arg.checkSections)359    checkSections();360 361  // It does not make sense try to open the file if we have error already.362  if (errCount(ctx))363    return;364 365  {366    llvm::TimeTraceScope timeScope("Write output file");367    // Write the result down to a file.368    openFile();369    if (errCount(ctx))370      return;371 372    if (!ctx.arg.oFormatBinary) {373      if (ctx.arg.zSeparate != SeparateSegmentKind::None)374        writeTrapInstr();375      writeHeader();376      writeSections();377    } else {378      writeSectionsBinary();379    }380 381    // Backfill .note.gnu.build-id section content. This is done at last382    // because the content is usually a hash value of the entire output file.383    writeBuildId();384    if (errCount(ctx))385      return;386 387    if (!ctx.e.disableOutput) {388      if (auto e = buffer->commit())389        Err(ctx) << "failed to write output '" << buffer->getPath()390                 << "': " << std::move(e);391    }392 393    if (!ctx.arg.cmseOutputLib.empty())394      writeARMCmseImportLib<ELFT>(ctx);395  }396}397 398template <class ELFT, class RelTy>399static void markUsedLocalSymbolsImpl(ObjFile<ELFT> *file,400                                     llvm::ArrayRef<RelTy> rels) {401  for (const RelTy &rel : rels) {402    Symbol &sym = file->getRelocTargetSym(rel);403    if (sym.isLocal())404      sym.used = true;405  }406}407 408// The function ensures that the "used" field of local symbols reflects the fact409// that the symbol is used in a relocation from a live section.410template <class ELFT> static void markUsedLocalSymbols(Ctx &ctx) {411  // With --gc-sections, the field is already filled.412  // See MarkLive<ELFT>::resolveReloc().413  if (ctx.arg.gcSections)414    return;415  for (ELFFileBase *file : ctx.objectFiles) {416    ObjFile<ELFT> *f = cast<ObjFile<ELFT>>(file);417    for (InputSectionBase *s : f->getSections()) {418      InputSection *isec = dyn_cast_or_null<InputSection>(s);419      if (!isec)420        continue;421      if (isec->type == SHT_REL) {422        markUsedLocalSymbolsImpl(f, isec->getDataAs<typename ELFT::Rel>());423      } else if (isec->type == SHT_RELA) {424        markUsedLocalSymbolsImpl(f, isec->getDataAs<typename ELFT::Rela>());425      } else if (isec->type == SHT_CREL) {426        // The is64=true variant also works with ELF32 since only the r_symidx427        // member is used.428        for (Elf_Crel_Impl<true> r : RelocsCrel<true>(isec->content_)) {429          Symbol &sym = file->getSymbol(r.r_symidx);430          if (sym.isLocal())431            sym.used = true;432        }433      }434    }435  }436}437 438static bool shouldKeepInSymtab(Ctx &ctx, const Defined &sym) {439  if (sym.isSection())440    return false;441 442  // If --emit-reloc or -r is given, preserve symbols referenced by relocations443  // from live sections.444  if (sym.used && ctx.arg.copyRelocs)445    return true;446 447  // Exclude local symbols pointing to .ARM.exidx sections.448  // They are probably mapping symbols "$d", which are optional for these449  // sections. After merging the .ARM.exidx sections, some of these symbols450  // may become dangling. The easiest way to avoid the issue is not to add451  // them to the symbol table from the beginning.452  if (ctx.arg.emachine == EM_ARM && sym.section &&453      sym.section->type == SHT_ARM_EXIDX)454    return false;455 456  if (ctx.arg.discard == DiscardPolicy::None)457    return true;458  if (ctx.arg.discard == DiscardPolicy::All)459    return false;460 461  // In ELF assembly .L symbols are normally discarded by the assembler.462  // If the assembler fails to do so, the linker discards them if463  // * --discard-locals is used.464  // * The symbol is in a SHF_MERGE section, which is normally the reason for465  //   the assembler keeping the .L symbol.466  if (sym.getName().starts_with(".L") &&467      (ctx.arg.discard == DiscardPolicy::Locals ||468       (sym.section && (sym.section->flags & SHF_MERGE))))469    return false;470  return true;471}472 473bool elf::includeInSymtab(Ctx &ctx, const Symbol &b) {474  if (auto *d = dyn_cast<Defined>(&b)) {475    // Always include absolute symbols.476    SectionBase *sec = d->section;477    if (!sec)478      return true;479    assert(sec->isLive());480 481    if (auto *s = dyn_cast<MergeInputSection>(sec))482      return s->getSectionPiece(d->value).live;483    return true;484  }485  return b.used || !ctx.arg.gcSections;486}487 488// Scan local symbols to:489//490// - demote symbols defined relative to /DISCARD/ discarded input sections so491//   that relocations referencing them will lead to errors.492// - copy eligible symbols to .symTab493static void demoteAndCopyLocalSymbols(Ctx &ctx) {494  llvm::TimeTraceScope timeScope("Add local symbols");495  for (ELFFileBase *file : ctx.objectFiles) {496    DenseMap<SectionBase *, size_t> sectionIndexMap;497    for (Symbol *b : file->getLocalSymbols()) {498      assert(b->isLocal() && "should have been caught in initializeSymbols()");499      auto *dr = dyn_cast<Defined>(b);500      if (!dr)501        continue;502 503      if (dr->section && !dr->section->isLive())504        demoteDefined(*dr, sectionIndexMap);505      else if (ctx.in.symTab && includeInSymtab(ctx, *b) &&506               shouldKeepInSymtab(ctx, *dr))507        ctx.in.symTab->addSymbol(b);508    }509  }510}511 512// Create a section symbol for each output section so that we can represent513// relocations that point to the section. If we know that no relocation is514// referring to a section (that happens if the section is a synthetic one), we515// don't create a section symbol for that section.516template <class ELFT> void Writer<ELFT>::addSectionSymbols() {517  for (SectionCommand *cmd : ctx.script->sectionCommands) {518    auto *osd = dyn_cast<OutputDesc>(cmd);519    if (!osd)520      continue;521    OutputSection &osec = osd->osec;522    InputSectionBase *isec = nullptr;523    // Iterate over all input sections and add a STT_SECTION symbol if any input524    // section may be a relocation target.525    for (SectionCommand *cmd : osec.commands) {526      auto *isd = dyn_cast<InputSectionDescription>(cmd);527      if (!isd)528        continue;529      for (InputSectionBase *s : isd->sections) {530        // Relocations are not using REL[A] section symbols.531        if (isStaticRelSecType(s->type))532          continue;533 534        // Unlike other synthetic sections, mergeable output sections contain535        // data copied from input sections, and there may be a relocation536        // pointing to its contents if -r or --emit-reloc is given.537        if (isa<SyntheticSection>(s) && !(s->flags & SHF_MERGE))538          continue;539 540        isec = s;541        break;542      }543    }544    if (!isec)545      continue;546 547    // Set the symbol to be relative to the output section so that its st_value548    // equals the output section address. Note, there may be a gap between the549    // start of the output section and isec.550    ctx.in.symTab->addSymbol(makeDefined(ctx, isec->file, "", STB_LOCAL,551                                         /*stOther=*/0, STT_SECTION,552                                         /*value=*/0, /*size=*/0, &osec));553  }554}555 556// Returns true if this is a variant of .data.rel.ro.557static bool isRelRoDataSection(Ctx &ctx, StringRef secName) {558  if (!secName.consume_front(".data.rel.ro"))559    return false;560  if (secName.empty())561    return true;562  // If -z keep-data-section-prefix is specified, additionally allow563  // '.data.rel.ro.hot' and '.data.rel.ro.unlikely'.564  if (ctx.arg.zKeepDataSectionPrefix)565    return secName == ".hot" || secName == ".unlikely";566  return false;567}568 569// Today's loaders have a feature to make segments read-only after570// processing dynamic relocations to enhance security. PT_GNU_RELRO571// is defined for that.572//573// This function returns true if a section needs to be put into a574// PT_GNU_RELRO segment.575static bool isRelroSection(Ctx &ctx, const OutputSection *sec) {576  if (!ctx.arg.zRelro)577    return false;578  if (sec->relro)579    return true;580 581  uint64_t flags = sec->flags;582 583  // Non-allocatable or non-writable sections don't need RELRO because584  // they are not writable or not even mapped to memory in the first place.585  // RELRO is for sections that are essentially read-only but need to586  // be writable only at process startup to allow dynamic linker to587  // apply relocations.588  if (!(flags & SHF_ALLOC) || !(flags & SHF_WRITE))589    return false;590 591  // Once initialized, TLS data segments are used as data templates592  // for a thread-local storage. For each new thread, runtime593  // allocates memory for a TLS and copy templates there. No thread594  // are supposed to use templates directly. Thus, it can be in RELRO.595  if (flags & SHF_TLS)596    return true;597 598  // .init_array, .preinit_array and .fini_array contain pointers to599  // functions that are executed on process startup or exit. These600  // pointers are set by the static linker, and they are not expected601  // to change at runtime. But if you are an attacker, you could do602  // interesting things by manipulating pointers in .fini_array, for603  // example. So they are put into RELRO.604  uint32_t type = sec->type;605  if (type == SHT_INIT_ARRAY || type == SHT_FINI_ARRAY ||606      type == SHT_PREINIT_ARRAY)607    return true;608 609  // .got contains pointers to external symbols. They are resolved by610  // the dynamic linker when a module is loaded into memory, and after611  // that they are not expected to change. So, it can be in RELRO.612  if (ctx.in.got && sec == ctx.in.got->getParent())613    return true;614 615  // .toc is a GOT-ish section for PowerPC64. Their contents are accessed616  // through r2 register, which is reserved for that purpose. Since r2 is used617  // for accessing .got as well, .got and .toc need to be close enough in the618  // virtual address space. Usually, .toc comes just after .got. Since we place619  // .got into RELRO, .toc needs to be placed into RELRO too.620  if (sec->name == ".toc")621    return true;622 623  // .got.plt contains pointers to external function symbols. They are624  // by default resolved lazily, so we usually cannot put it into RELRO.625  // However, if "-z now" is given, the lazy symbol resolution is626  // disabled, which enables us to put it into RELRO.627  if (sec == ctx.in.gotPlt->getParent())628    return ctx.arg.zNow;629 630  if (ctx.in.relroPadding && sec == ctx.in.relroPadding->getParent())631    return true;632 633  // .dynamic section contains data for the dynamic linker, and634  // there's no need to write to it at runtime, so it's better to put635  // it into RELRO.636  if (sec->name == ".dynamic")637    return true;638 639  // Sections with some special names are put into RELRO. This is a640  // bit unfortunate because section names shouldn't be significant in641  // ELF in spirit. But in reality many linker features depend on642  // magic section names.643  StringRef s = sec->name;644 645  bool abiAgnostic = isRelRoDataSection(ctx, s) || s == ".bss.rel.ro" ||646                     s == ".ctors" || s == ".dtors" || s == ".jcr" ||647                     s == ".eh_frame" || s == ".fini_array" ||648                     s == ".init_array" || s == ".preinit_array";649 650  bool abiSpecific =651      ctx.arg.osabi == ELFOSABI_OPENBSD && s == ".openbsd.randomdata";652 653  return abiAgnostic || abiSpecific;654}655 656// We compute a rank for each section. The rank indicates where the657// section should be placed in the file.  Instead of using simple658// numbers (0,1,2...), we use a series of flags. One for each decision659// point when placing the section.660// Using flags has two key properties:661// * It is easy to check if a give branch was taken.662// * It is easy two see how similar two ranks are (see getRankProximity).663enum RankFlags {664  RF_NOT_ADDR_SET = 1 << 27,665  RF_NOT_ALLOC = 1 << 26,666  RF_PARTITION = 1 << 18, // Partition number (8 bits)667  RF_LARGE_EXEC_WRITE = 1 << 16,668  RF_LARGE_ALT = 1 << 15,669  RF_WRITE = 1 << 14,670  RF_EXEC_WRITE = 1 << 13,671  RF_EXEC = 1 << 12,672  RF_RODATA = 1 << 11,673  RF_LARGE_EXEC = 1 << 10,674  RF_LARGE = 1 << 9,675  RF_NOT_RELRO = 1 << 8,676  RF_NOT_TLS = 1 << 7,677  RF_BSS = 1 << 6,678};679 680unsigned elf::getSectionRank(Ctx &ctx, OutputSection &osec) {681  unsigned rank = osec.partition * RF_PARTITION;682 683  // We want to put section specified by -T option first, so we684  // can start assigning VA starting from them later.685  if (ctx.arg.sectionStartMap.count(osec.name))686    return rank;687  rank |= RF_NOT_ADDR_SET;688 689  // Allocatable sections go first to reduce the total PT_LOAD size and690  // so debug info doesn't change addresses in actual code.691  if (!(osec.flags & SHF_ALLOC))692    return rank | RF_NOT_ALLOC;693 694  // Sort sections based on their access permission in the following695  // order: R, RX, RXW, RW(RELRO), RW(non-RELRO).696  //697  // Read-only sections come first such that they go in the PT_LOAD covering the698  // program headers at the start of the file.699  //700  // The layout for writable sections is PT_LOAD(PT_GNU_RELRO(.data.rel.ro701  // .bss.rel.ro) | .data .bss), where | marks where page alignment happens.702  // An alternative ordering is PT_LOAD(.data | PT_GNU_RELRO( .data.rel.ro703  // .bss.rel.ro) | .bss), but it may waste more bytes due to 2 alignment704  // places.705  bool isExec = osec.flags & SHF_EXECINSTR;706  bool isWrite = osec.flags & SHF_WRITE;707  bool isLarge = osec.flags & SHF_X86_64_LARGE && ctx.arg.emachine == EM_X86_64;708 709  if (!isWrite && !isExec) {710    // Among PROGBITS sections, place .lrodata further from .text.711    // For -z lrodata-after-bss, place .lrodata after .lbss like GNU ld. This712    // layout has one extra PT_LOAD, but alleviates relocation overflow713    // pressure for absolute relocations referencing small data from -fno-pic714    // relocatable files.715    if (isLarge)716      rank |= ctx.arg.zLrodataAfterBss ? RF_LARGE_ALT : 0;717    else718      rank |= ctx.arg.zLrodataAfterBss ? 0 : RF_LARGE;719 720    if (osec.type == SHT_LLVM_PART_EHDR)721      ;722    else if (osec.type == SHT_LLVM_PART_PHDR)723      rank |= 1;724    else if (osec.name == ".interp")725      rank |= 2;726    // Put .note sections at the beginning so that they are likely to be727    // included in a truncate core file. In particular, .note.gnu.build-id, if728    // available, can identify the object file.729    else if (osec.type == SHT_NOTE)730      rank |= 3;731    // Make PROGBITS sections (e.g .rodata .eh_frame) closer to .text to732    // alleviate relocation overflow pressure. Large special sections such as733    // .dynstr and .dynsym can be away from .text.734    else if (osec.type != SHT_PROGBITS)735      rank |= 4;736    else737      rank |= RF_RODATA;738  } else if (isExec) {739    // Place readonly .ltext before .lrodata and writable .ltext after .lbss to740    // keep writable and readonly segments separate.741    if (isLarge) {742      rank |= isWrite ? RF_LARGE_EXEC_WRITE : RF_LARGE_EXEC;743    } else {744      rank |= isWrite ? RF_EXEC_WRITE : RF_EXEC;745    }746  } else {747    rank |= RF_WRITE;748    // The TLS initialization block needs to be a single contiguous block. Place749    // TLS sections directly before the other RELRO sections.750    if (!(osec.flags & SHF_TLS))751      rank |= RF_NOT_TLS;752    if (isRelroSection(ctx, &osec))753      osec.relro = true;754    else755      rank |= RF_NOT_RELRO;756    // Place .ldata and .lbss after .bss. Making .bss closer to .text757    // alleviates relocation overflow pressure.758    // For -z lrodata-after-bss, place .lbss/.lrodata/.ldata after .bss.759    // .bss/.lbss being adjacent reuses the NOBITS size optimization.760    if (isLarge) {761      rank |= ctx.arg.zLrodataAfterBss762                  ? (osec.type == SHT_NOBITS ? 1 : RF_LARGE_ALT)763                  : RF_LARGE;764    }765  }766 767  // Within TLS sections, or within other RelRo sections, or within non-RelRo768  // sections, place non-NOBITS sections first.769  if (osec.type == SHT_NOBITS)770    rank |= RF_BSS;771 772  // Some architectures have additional ordering restrictions for sections773  // within the same PT_LOAD.774  if (ctx.arg.emachine == EM_PPC64) {775    // PPC64 has a number of special SHT_PROGBITS+SHF_ALLOC+SHF_WRITE sections776    // that we would like to make sure appear is a specific order to maximize777    // their coverage by a single signed 16-bit offset from the TOC base778    // pointer.779    StringRef name = osec.name;780    if (name == ".got")781      rank |= 1;782    else if (name == ".toc")783      rank |= 2;784  }785 786  if (ctx.arg.emachine == EM_MIPS) {787    if (osec.name != ".got")788      rank |= 1;789    // All sections with SHF_MIPS_GPREL flag should be grouped together790    // because data in these sections is addressable with a gp relative address.791    if (osec.flags & SHF_MIPS_GPREL)792      rank |= 2;793  }794 795  if (ctx.arg.emachine == EM_RISCV) {796    // .sdata and .sbss are placed closer to make GP relaxation more profitable797    // and match GNU ld.798    StringRef name = osec.name;799    if (name == ".sdata" || (osec.type == SHT_NOBITS && name != ".sbss"))800      rank |= 1;801  }802 803  return rank;804}805 806static bool compareSections(Ctx &ctx, const SectionCommand *aCmd,807                            const SectionCommand *bCmd) {808  const OutputSection *a = &cast<OutputDesc>(aCmd)->osec;809  const OutputSection *b = &cast<OutputDesc>(bCmd)->osec;810 811  if (a->sortRank != b->sortRank)812    return a->sortRank < b->sortRank;813 814  if (!(a->sortRank & RF_NOT_ADDR_SET))815    return ctx.arg.sectionStartMap.lookup(a->name) <816           ctx.arg.sectionStartMap.lookup(b->name);817  return false;818}819 820void PhdrEntry::add(OutputSection *sec) {821  lastSec = sec;822  if (!firstSec)823    firstSec = sec;824  p_align = std::max(p_align, sec->addralign);825  if (p_type == PT_LOAD)826    sec->ptLoad = this;827}828 829// A statically linked position-dependent executable should only contain830// IRELATIVE relocations and no other dynamic relocations. Encapsulation symbols831// __rel[a]_iplt_{start,end} will be defined for .rel[a].dyn, to be832// processed by the libc runtime. Other executables or DSOs use dynamic tags833// instead.834template <class ELFT> void Writer<ELFT>::addRelIpltSymbols() {835  if (ctx.arg.isPic)836    return;837 838  // __rela_iplt_{start,end} are initially defined relative to dummy section 0.839  // We'll override ctx.out.elfHeader with relaDyn later when we are sure that840  // .rela.dyn will be present in the output.841  std::string name = ctx.arg.isRela ? "__rela_iplt_start" : "__rel_iplt_start";842  ctx.sym.relaIpltStart =843      addOptionalRegular(ctx, name, ctx.out.elfHeader.get(), 0, STV_HIDDEN);844  name.replace(name.size() - 5, 5, "end");845  ctx.sym.relaIpltEnd =846      addOptionalRegular(ctx, name, ctx.out.elfHeader.get(), 0, STV_HIDDEN);847}848 849// This function generates assignments for predefined symbols (e.g. _end or850// _etext) and inserts them into the commands sequence to be processed at the851// appropriate time. This ensures that the value is going to be correct by the852// time any references to these symbols are processed and is equivalent to853// defining these symbols explicitly in the linker script.854template <class ELFT> void Writer<ELFT>::setReservedSymbolSections() {855  if (ctx.sym.globalOffsetTable) {856    // The _GLOBAL_OFFSET_TABLE_ symbol is defined by target convention usually857    // to the start of the .got or .got.plt section.858    InputSection *sec = ctx.in.gotPlt.get();859    if (!ctx.target->gotBaseSymInGotPlt)860      sec = ctx.in.mipsGot ? cast<InputSection>(ctx.in.mipsGot.get())861                           : cast<InputSection>(ctx.in.got.get());862    ctx.sym.globalOffsetTable->section = sec;863  }864 865  // .rela_iplt_{start,end} mark the start and the end of the section containing866  // IRELATIVE relocations.867  if (ctx.sym.relaIpltStart) {868    auto &dyn = getIRelativeSection(ctx);869    if (dyn.isNeeded()) {870      ctx.sym.relaIpltStart->section = &dyn;871      ctx.sym.relaIpltEnd->section = &dyn;872      ctx.sym.relaIpltEnd->value = dyn.getSize();873    }874  }875 876  PhdrEntry *last = nullptr;877  OutputSection *lastRO = nullptr;878  auto isLarge = [&ctx = ctx](OutputSection *osec) {879    return ctx.arg.emachine == EM_X86_64 && osec->flags & SHF_X86_64_LARGE;880  };881  for (Partition &part : ctx.partitions) {882    for (auto &p : part.phdrs) {883      if (p->p_type != PT_LOAD)884        continue;885      last = p.get();886      if (!(p->p_flags & PF_W) && p->lastSec && !isLarge(p->lastSec))887        lastRO = p->lastSec;888    }889  }890 891  if (lastRO) {892    // _etext is the first location after the last read-only loadable segment893    // that does not contain large sections.894    if (ctx.sym.etext1)895      ctx.sym.etext1->section = lastRO;896    if (ctx.sym.etext2)897      ctx.sym.etext2->section = lastRO;898  }899 900  if (last) {901    // _edata points to the end of the last non-large mapped initialized902    // section.903    OutputSection *edata = nullptr;904    for (OutputSection *os : ctx.outputSections) {905      if (os->type != SHT_NOBITS && !isLarge(os))906        edata = os;907      if (os == last->lastSec)908        break;909    }910 911    if (ctx.sym.edata1)912      ctx.sym.edata1->section = edata;913    if (ctx.sym.edata2)914      ctx.sym.edata2->section = edata;915 916    // _end is the first location after the uninitialized data region.917    if (ctx.sym.end1)918      ctx.sym.end1->section = last->lastSec;919    if (ctx.sym.end2)920      ctx.sym.end2->section = last->lastSec;921  }922 923  if (ctx.sym.bss) {924    // On RISC-V, set __bss_start to the start of .sbss if present.925    OutputSection *sbss =926        ctx.arg.emachine == EM_RISCV ? findSection(ctx, ".sbss") : nullptr;927    ctx.sym.bss->section = sbss ? sbss : findSection(ctx, ".bss");928  }929 930  // Setup MIPS _gp_disp/__gnu_local_gp symbols which should931  // be equal to the _gp symbol's value.932  if (ctx.sym.mipsGp) {933    // Find GP-relative section with the lowest address934    // and use this address to calculate default _gp value.935    for (OutputSection *os : ctx.outputSections) {936      if (os->flags & SHF_MIPS_GPREL) {937        ctx.sym.mipsGp->section = os;938        ctx.sym.mipsGp->value = 0x7ff0;939        break;940      }941    }942  }943}944 945// We want to find how similar two ranks are.946// The more branches in getSectionRank that match, the more similar they are.947// Since each branch corresponds to a bit flag, we can just use948// countLeadingZeros.949static int getRankProximity(OutputSection *a, SectionCommand *b) {950  auto *osd = dyn_cast<OutputDesc>(b);951  return (osd && osd->osec.hasInputSections)952             ? llvm::countl_zero(a->sortRank ^ osd->osec.sortRank)953             : -1;954}955 956// When placing orphan sections, we want to place them after symbol assignments957// so that an orphan after958//   begin_foo = .;959//   foo : { *(foo) }960//   end_foo = .;961// doesn't break the intended meaning of the begin/end symbols.962// We don't want to go over sections since findOrphanPos is the963// one in charge of deciding the order of the sections.964// We don't want to go over changes to '.', since doing so in965//  rx_sec : { *(rx_sec) }966//  . = ALIGN(0x1000);967//  /* The RW PT_LOAD starts here*/968//  rw_sec : { *(rw_sec) }969// would mean that the RW PT_LOAD would become unaligned.970static bool shouldSkip(SectionCommand *cmd) {971  if (auto *assign = dyn_cast<SymbolAssignment>(cmd))972    return assign->name != ".";973  return false;974}975 976// We want to place orphan sections so that they share as much977// characteristics with their neighbors as possible. For example, if978// both are rw, or both are tls.979static SmallVectorImpl<SectionCommand *>::iterator980findOrphanPos(Ctx &ctx, SmallVectorImpl<SectionCommand *>::iterator b,981              SmallVectorImpl<SectionCommand *>::iterator e) {982  // Place non-alloc orphan sections at the end. This matches how we assign file983  // offsets to non-alloc sections.984  OutputSection *sec = &cast<OutputDesc>(*e)->osec;985  if (!(sec->flags & SHF_ALLOC))986    return e;987 988  // As a special case, place .relro_padding before the SymbolAssignment using989  // DATA_SEGMENT_RELRO_END, if present.990  if (ctx.in.relroPadding && sec == ctx.in.relroPadding->getParent()) {991    auto i = std::find_if(b, e, [=](SectionCommand *a) {992      if (auto *assign = dyn_cast<SymbolAssignment>(a))993        return assign->dataSegmentRelroEnd;994      return false;995    });996    if (i != e)997      return i;998  }999 1000  // Find the most similar output section as the anchor. Rank Proximity is a1001  // value in the range [-1, 32] where [0, 32] indicates potential anchors (0:1002  // least similar; 32: identical). -1 means not an anchor.1003  //1004  // In the event of proximity ties, we select the first or last section1005  // depending on whether the orphan's rank is smaller.1006  int maxP = 0;1007  auto i = e;1008  for (auto j = b; j != e; ++j) {1009    int p = getRankProximity(sec, *j);1010    if (p > maxP ||1011        (p == maxP && cast<OutputDesc>(*j)->osec.sortRank <= sec->sortRank)) {1012      maxP = p;1013      i = j;1014    }1015  }1016  if (i == e)1017    return e;1018 1019  auto isOutputSecWithInputSections = [](SectionCommand *cmd) {1020    auto *osd = dyn_cast<OutputDesc>(cmd);1021    return osd && osd->osec.hasInputSections;1022  };1023 1024  // Then, scan backward or forward through the script for a suitable insertion1025  // point. If i's rank is larger, the orphan section can be placed before i.1026  //1027  // However, don't do this if custom program headers are defined. Otherwise,1028  // adding the orphan to a previous segment can change its flags, for example,1029  // making a read-only segment writable. If memory regions are defined, an1030  // orphan section should continue the same region as the found section to1031  // better resemble the behavior of GNU ld.1032  bool mustAfter =1033      ctx.script->hasPhdrsCommands() || !ctx.script->memoryRegions.empty();1034  if (cast<OutputDesc>(*i)->osec.sortRank <= sec->sortRank || mustAfter) {1035    for (auto j = ++i; j != e; ++j) {1036      if (!isOutputSecWithInputSections(*j))1037        continue;1038      if (getRankProximity(sec, *j) != maxP)1039        break;1040      i = j + 1;1041    }1042  } else {1043    for (; i != b; --i)1044      if (isOutputSecWithInputSections(i[-1]))1045        break;1046  }1047 1048  // As a special case, if the orphan section is the last section, put1049  // it at the very end, past any other commands.1050  // This matches bfd's behavior and is convenient when the linker script fully1051  // specifies the start of the file, but doesn't care about the end (the non1052  // alloc sections for example).1053  if (std::none_of(i, e, isOutputSecWithInputSections))1054    return e;1055 1056  while (i != e && shouldSkip(*i))1057    ++i;1058  return i;1059}1060 1061// Adds random priorities to sections not already in the map.1062static void maybeShuffle(Ctx &ctx,1063                         DenseMap<const InputSectionBase *, int> &order) {1064  if (ctx.arg.shuffleSections.empty())1065    return;1066 1067  SmallVector<InputSectionBase *, 0> matched, sections = ctx.inputSections;1068  matched.reserve(sections.size());1069  for (const auto &patAndSeed : ctx.arg.shuffleSections) {1070    matched.clear();1071    for (InputSectionBase *sec : sections)1072      if (patAndSeed.first.match(sec->name))1073        matched.push_back(sec);1074    const uint32_t seed = patAndSeed.second;1075    if (seed == UINT32_MAX) {1076      // If --shuffle-sections <section-glob>=-1, reverse the section order. The1077      // section order is stable even if the number of sections changes. This is1078      // useful to catch issues like static initialization order fiasco1079      // reliably.1080      std::reverse(matched.begin(), matched.end());1081    } else {1082      std::mt19937 g(seed ? seed : std::random_device()());1083      llvm::shuffle(matched.begin(), matched.end(), g);1084    }1085    size_t i = 0;1086    for (InputSectionBase *&sec : sections)1087      if (patAndSeed.first.match(sec->name))1088        sec = matched[i++];1089  }1090 1091  // Existing priorities are < 0, so use priorities >= 0 for the missing1092  // sections.1093  int prio = 0;1094  for (InputSectionBase *sec : sections) {1095    if (order.try_emplace(sec, prio).second)1096      ++prio;1097  }1098}1099 1100// Return section order within an InputSectionDescription.1101// If both --symbol-ordering-file and call graph profile are present, the order1102// file takes precedence, but the call graph profile is still used for symbols1103// that don't appear in the order file.1104static DenseMap<const InputSectionBase *, int> buildSectionOrder(Ctx &ctx) {1105  DenseMap<const InputSectionBase *, int> sectionOrder;1106  if (ctx.arg.bpStartupFunctionSort || ctx.arg.bpFunctionOrderForCompression ||1107      ctx.arg.bpDataOrderForCompression) {1108    TimeTraceScope timeScope("Balanced Partitioning Section Orderer");1109    sectionOrder = runBalancedPartitioning(1110        ctx, ctx.arg.bpStartupFunctionSort ? ctx.arg.irpgoProfilePath : "",1111        ctx.arg.bpFunctionOrderForCompression,1112        ctx.arg.bpDataOrderForCompression,1113        ctx.arg.bpCompressionSortStartupFunctions,1114        ctx.arg.bpVerboseSectionOrderer);1115  } else if (!ctx.arg.callGraphProfile.empty()) {1116    sectionOrder = computeCallGraphProfileOrder(ctx);1117  }1118 1119  if (ctx.arg.symbolOrderingFile.empty())1120    return sectionOrder;1121 1122  struct SymbolOrderEntry {1123    int priority;1124    bool present;1125  };1126 1127  // Build a map from symbols to their priorities. Symbols that didn't1128  // appear in the symbol ordering file have the lowest priority 0.1129  // All explicitly mentioned symbols have negative (higher) priorities.1130  DenseMap<CachedHashStringRef, SymbolOrderEntry> symbolOrder;1131  int priority = -sectionOrder.size() - ctx.arg.symbolOrderingFile.size();1132  for (StringRef s : ctx.arg.symbolOrderingFile)1133    symbolOrder.insert({CachedHashStringRef(s), {priority++, false}});1134 1135  // Build a map from sections to their priorities.1136  auto addSym = [&](Symbol &sym) {1137    auto it = symbolOrder.find(CachedHashStringRef(sym.getName()));1138    if (it == symbolOrder.end())1139      return;1140    SymbolOrderEntry &ent = it->second;1141    ent.present = true;1142 1143    maybeWarnUnorderableSymbol(ctx, &sym);1144 1145    if (auto *d = dyn_cast<Defined>(&sym)) {1146      if (auto *sec = dyn_cast_or_null<InputSectionBase>(d->section)) {1147        int &priority = sectionOrder[cast<InputSectionBase>(sec)];1148        priority = std::min(priority, ent.priority);1149      }1150    }1151  };1152 1153  // We want both global and local symbols. We get the global ones from the1154  // symbol table and iterate the object files for the local ones.1155  for (Symbol *sym : ctx.symtab->getSymbols())1156    addSym(*sym);1157 1158  for (ELFFileBase *file : ctx.objectFiles)1159    for (Symbol *sym : file->getLocalSymbols())1160      addSym(*sym);1161 1162  if (ctx.arg.warnSymbolOrdering)1163    for (auto orderEntry : symbolOrder)1164      if (!orderEntry.second.present)1165        Warn(ctx) << "symbol ordering file: no such symbol: "1166                  << orderEntry.first.val();1167 1168  return sectionOrder;1169}1170 1171// Sorts the sections in ISD according to the provided section order.1172static void1173sortISDBySectionOrder(Ctx &ctx, InputSectionDescription *isd,1174                      const DenseMap<const InputSectionBase *, int> &order,1175                      bool executableOutputSection) {1176  SmallVector<InputSection *, 0> unorderedSections;1177  SmallVector<std::pair<InputSection *, int>, 0> orderedSections;1178  uint64_t unorderedSize = 0;1179  uint64_t totalSize = 0;1180 1181  for (InputSection *isec : isd->sections) {1182    if (executableOutputSection)1183      totalSize += isec->getSize();1184    auto i = order.find(isec);1185    if (i == order.end()) {1186      unorderedSections.push_back(isec);1187      unorderedSize += isec->getSize();1188      continue;1189    }1190    orderedSections.push_back({isec, i->second});1191  }1192  llvm::sort(orderedSections, llvm::less_second());1193 1194  // Find an insertion point for the ordered section list in the unordered1195  // section list. On targets with limited-range branches, this is the mid-point1196  // of the unordered section list. This decreases the likelihood that a range1197  // extension thunk will be needed to enter or exit the ordered region. If the1198  // ordered section list is a list of hot functions, we can generally expect1199  // the ordered functions to be called more often than the unordered functions,1200  // making it more likely that any particular call will be within range, and1201  // therefore reducing the number of thunks required.1202  //1203  // For example, imagine that you have 8MB of hot code and 32MB of cold code.1204  // If the layout is:1205  //1206  // 8MB hot1207  // 32MB cold1208  //1209  // only the first 8-16MB of the cold code (depending on which hot function it1210  // is actually calling) can call the hot code without a range extension thunk.1211  // However, if we use this layout:1212  //1213  // 16MB cold1214  // 8MB hot1215  // 16MB cold1216  //1217  // both the last 8-16MB of the first block of cold code and the first 8-16MB1218  // of the second block of cold code can call the hot code without a thunk. So1219  // we effectively double the amount of code that could potentially call into1220  // the hot code without a thunk.1221  //1222  // The above is not necessary if total size of input sections in this "isd"1223  // is small. Note that we assume all input sections are executable if the1224  // output section is executable (which is not always true but supposed to1225  // cover most cases).1226  size_t insPt = 0;1227  if (executableOutputSection && !orderedSections.empty() &&1228      ctx.target->getThunkSectionSpacing() &&1229      totalSize >= ctx.target->getThunkSectionSpacing()) {1230    uint64_t unorderedPos = 0;1231    for (; insPt != unorderedSections.size(); ++insPt) {1232      unorderedPos += unorderedSections[insPt]->getSize();1233      if (unorderedPos > unorderedSize / 2)1234        break;1235    }1236  }1237 1238  isd->sections.clear();1239  for (InputSection *isec : ArrayRef(unorderedSections).slice(0, insPt))1240    isd->sections.push_back(isec);1241  for (std::pair<InputSection *, int> p : orderedSections)1242    isd->sections.push_back(p.first);1243  for (InputSection *isec : ArrayRef(unorderedSections).slice(insPt))1244    isd->sections.push_back(isec);1245}1246 1247static void sortSection(Ctx &ctx, OutputSection &osec,1248                        const DenseMap<const InputSectionBase *, int> &order) {1249  StringRef name = osec.name;1250 1251  // Never sort these.1252  if (name == ".init" || name == ".fini")1253    return;1254 1255  // Sort input sections by priority using the list provided by1256  // --symbol-ordering-file or --shuffle-sections=. This is a least significant1257  // digit radix sort. The sections may be sorted stably again by a more1258  // significant key.1259  if (!order.empty())1260    for (SectionCommand *b : osec.commands)1261      if (auto *isd = dyn_cast<InputSectionDescription>(b))1262        sortISDBySectionOrder(ctx, isd, order, osec.flags & SHF_EXECINSTR);1263 1264  if (ctx.script->hasSectionsCommand)1265    return;1266 1267  if (name == ".init_array" || name == ".fini_array") {1268    osec.sortInitFini();1269  } else if (name == ".ctors" || name == ".dtors") {1270    osec.sortCtorsDtors();1271  } else if (ctx.arg.emachine == EM_PPC64 && name == ".toc") {1272    // .toc is allocated just after .got and is accessed using GOT-relative1273    // relocations. Object files compiled with small code model have an1274    // addressable range of [.got, .got + 0xFFFC] for GOT-relative relocations.1275    // To reduce the risk of relocation overflow, .toc contents are sorted so1276    // that sections having smaller relocation offsets are at beginning of .toc1277    assert(osec.commands.size() == 1);1278    auto *isd = cast<InputSectionDescription>(osec.commands[0]);1279    llvm::stable_sort(isd->sections,1280                      [](const InputSection *a, const InputSection *b) -> bool {1281                        return a->file->ppc64SmallCodeModelTocRelocs &&1282                               !b->file->ppc64SmallCodeModelTocRelocs;1283                      });1284  }1285}1286 1287// Sort sections within each InputSectionDescription.1288template <class ELFT> void Writer<ELFT>::sortInputSections() {1289  // Assign negative priorities.1290  DenseMap<const InputSectionBase *, int> order = buildSectionOrder(ctx);1291  // Assign non-negative priorities due to --shuffle-sections.1292  maybeShuffle(ctx, order);1293  for (SectionCommand *cmd : ctx.script->sectionCommands)1294    if (auto *osd = dyn_cast<OutputDesc>(cmd))1295      sortSection(ctx, osd->osec, order);1296}1297 1298template <class ELFT> void Writer<ELFT>::sortSections() {1299  llvm::TimeTraceScope timeScope("Sort sections");1300 1301  // Don't sort if using -r. It is not necessary and we want to preserve the1302  // relative order for SHF_LINK_ORDER sections.1303  if (ctx.arg.relocatable) {1304    ctx.script->adjustOutputSections();1305    return;1306  }1307 1308  sortInputSections();1309 1310  for (SectionCommand *cmd : ctx.script->sectionCommands)1311    if (auto *osd = dyn_cast<OutputDesc>(cmd))1312      osd->osec.sortRank = getSectionRank(ctx, osd->osec);1313  if (!ctx.script->hasSectionsCommand) {1314    // OutputDescs are mostly contiguous, but may be interleaved with1315    // SymbolAssignments in the presence of INSERT commands.1316    auto mid = std::stable_partition(1317        ctx.script->sectionCommands.begin(), ctx.script->sectionCommands.end(),1318        [](SectionCommand *cmd) { return isa<OutputDesc>(cmd); });1319    std::stable_sort(1320        ctx.script->sectionCommands.begin(), mid,1321        [&ctx = ctx](auto *l, auto *r) { return compareSections(ctx, l, r); });1322  }1323 1324  // Process INSERT commands and update output section attributes. From this1325  // point onwards the order of script->sectionCommands is fixed.1326  ctx.script->processInsertCommands();1327  ctx.script->adjustOutputSections();1328 1329  if (ctx.script->hasSectionsCommand)1330    sortOrphanSections();1331 1332  ctx.script->adjustSectionsAfterSorting();1333}1334 1335template <class ELFT> void Writer<ELFT>::sortOrphanSections() {1336  // Orphan sections are sections present in the input files which are1337  // not explicitly placed into the output file by the linker script.1338  //1339  // The sections in the linker script are already in the correct1340  // order. We have to figuere out where to insert the orphan1341  // sections.1342  //1343  // The order of the sections in the script is arbitrary and may not agree with1344  // compareSections. This means that we cannot easily define a strict weak1345  // ordering. To see why, consider a comparison of a section in the script and1346  // one not in the script. We have a two simple options:1347  // * Make them equivalent (a is not less than b, and b is not less than a).1348  //   The problem is then that equivalence has to be transitive and we can1349  //   have sections a, b and c with only b in a script and a less than c1350  //   which breaks this property.1351  // * Use compareSectionsNonScript. Given that the script order doesn't have1352  //   to match, we can end up with sections a, b, c, d where b and c are in the1353  //   script and c is compareSectionsNonScript less than b. In which case d1354  //   can be equivalent to c, a to b and d < a. As a concrete example:1355  //   .a (rx) # not in script1356  //   .b (rx) # in script1357  //   .c (ro) # in script1358  //   .d (ro) # not in script1359  //1360  // The way we define an order then is:1361  // *  Sort only the orphan sections. They are in the end right now.1362  // *  Move each orphan section to its preferred position. We try1363  //    to put each section in the last position where it can share1364  //    a PT_LOAD.1365  //1366  // There is some ambiguity as to where exactly a new entry should be1367  // inserted, because Commands contains not only output section1368  // commands but also other types of commands such as symbol assignment1369  // expressions. There's no correct answer here due to the lack of the1370  // formal specification of the linker script. We use heuristics to1371  // determine whether a new output command should be added before or1372  // after another commands. For the details, look at shouldSkip1373  // function.1374 1375  auto i = ctx.script->sectionCommands.begin();1376  auto e = ctx.script->sectionCommands.end();1377  auto nonScriptI = std::find_if(i, e, [](SectionCommand *cmd) {1378    if (auto *osd = dyn_cast<OutputDesc>(cmd))1379      return osd->osec.sectionIndex == UINT32_MAX;1380    return false;1381  });1382 1383  // Sort the orphan sections.1384  std::stable_sort(nonScriptI, e, [&ctx = ctx](auto *l, auto *r) {1385    return compareSections(ctx, l, r);1386  });1387 1388  // As a horrible special case, skip the first . assignment if it is before any1389  // section. We do this because it is common to set a load address by starting1390  // the script with ". = 0xabcd" and the expectation is that every section is1391  // after that.1392  auto firstSectionOrDotAssignment =1393      std::find_if(i, e, [](SectionCommand *cmd) { return !shouldSkip(cmd); });1394  if (firstSectionOrDotAssignment != e &&1395      isa<SymbolAssignment>(**firstSectionOrDotAssignment))1396    ++firstSectionOrDotAssignment;1397  i = firstSectionOrDotAssignment;1398 1399  while (nonScriptI != e) {1400    auto pos = findOrphanPos(ctx, i, nonScriptI);1401    OutputSection *orphan = &cast<OutputDesc>(*nonScriptI)->osec;1402 1403    // As an optimization, find all sections with the same sort rank1404    // and insert them with one rotate.1405    unsigned rank = orphan->sortRank;1406    auto end = std::find_if(nonScriptI + 1, e, [=](SectionCommand *cmd) {1407      return cast<OutputDesc>(cmd)->osec.sortRank != rank;1408    });1409    std::rotate(pos, nonScriptI, end);1410    nonScriptI = end;1411  }1412}1413 1414static bool compareByFilePosition(InputSection *a, InputSection *b) {1415  InputSection *la = a->flags & SHF_LINK_ORDER ? a->getLinkOrderDep() : nullptr;1416  InputSection *lb = b->flags & SHF_LINK_ORDER ? b->getLinkOrderDep() : nullptr;1417  // SHF_LINK_ORDER sections with non-zero sh_link are ordered before1418  // non-SHF_LINK_ORDER sections and SHF_LINK_ORDER sections with zero sh_link.1419  if (!la || !lb)1420    return la && !lb;1421  OutputSection *aOut = la->getParent();1422  OutputSection *bOut = lb->getParent();1423 1424  if (aOut == bOut)1425    return la->outSecOff < lb->outSecOff;1426  if (aOut->addr == bOut->addr)1427    return aOut->sectionIndex < bOut->sectionIndex;1428  return aOut->addr < bOut->addr;1429}1430 1431template <class ELFT> void Writer<ELFT>::resolveShfLinkOrder() {1432  llvm::TimeTraceScope timeScope("Resolve SHF_LINK_ORDER");1433  for (OutputSection *sec : ctx.outputSections) {1434    if (!(sec->flags & SHF_LINK_ORDER))1435      continue;1436 1437    // The ARM.exidx section use SHF_LINK_ORDER, but we have consolidated1438    // this processing inside the ARMExidxsyntheticsection::finalizeContents().1439    if (!ctx.arg.relocatable && ctx.arg.emachine == EM_ARM &&1440        sec->type == SHT_ARM_EXIDX)1441      continue;1442 1443    // Link order may be distributed across several InputSectionDescriptions.1444    // Sorting is performed separately.1445    SmallVector<InputSection **, 0> scriptSections;1446    SmallVector<InputSection *, 0> sections;1447    for (SectionCommand *cmd : sec->commands) {1448      auto *isd = dyn_cast<InputSectionDescription>(cmd);1449      if (!isd)1450        continue;1451      bool hasLinkOrder = false;1452      scriptSections.clear();1453      sections.clear();1454      for (InputSection *&isec : isd->sections) {1455        if (isec->flags & SHF_LINK_ORDER) {1456          InputSection *link = isec->getLinkOrderDep();1457          if (link && !link->getParent())1458            ErrAlways(ctx) << isec << ": sh_link points to discarded section "1459                           << link;1460          hasLinkOrder = true;1461        }1462        scriptSections.push_back(&isec);1463        sections.push_back(isec);1464      }1465      if (hasLinkOrder && errCount(ctx) == 0) {1466        llvm::stable_sort(sections, compareByFilePosition);1467        for (int i = 0, n = sections.size(); i != n; ++i)1468          *scriptSections[i] = sections[i];1469      }1470    }1471  }1472}1473 1474static void finalizeSynthetic(Ctx &ctx, SyntheticSection *sec) {1475  if (sec && sec->isNeeded() && sec->getParent()) {1476    llvm::TimeTraceScope timeScope("Finalize synthetic sections", sec->name);1477    sec->finalizeContents();1478  }1479}1480 1481static bool canInsertPadding(OutputSection *sec) {1482  StringRef s = sec->name;1483  return s == ".bss" || s == ".data" || s == ".data.rel.ro" || s == ".lbss" ||1484         s == ".ldata" || s == ".lrodata" || s == ".ltext" || s == ".rodata" ||1485         s.starts_with(".text");1486}1487 1488static void randomizeSectionPadding(Ctx &ctx) {1489  std::mt19937 g(*ctx.arg.randomizeSectionPadding);1490  PhdrEntry *curPtLoad = nullptr;1491  for (OutputSection *os : ctx.outputSections) {1492    if (!canInsertPadding(os))1493      continue;1494    for (SectionCommand *bc : os->commands) {1495      if (auto *isd = dyn_cast<InputSectionDescription>(bc)) {1496        SmallVector<InputSection *, 0> tmp;1497        if (os->ptLoad != curPtLoad) {1498          tmp.push_back(1499              make<PaddingSection>(ctx, g() % ctx.arg.maxPageSize, os));1500          curPtLoad = os->ptLoad;1501        }1502        for (InputSection *isec : isd->sections) {1503          // Probability of inserting padding is 1 in 16.1504          if (g() % 16 == 0)1505            tmp.push_back(make<PaddingSection>(ctx, isec->addralign, os));1506          tmp.push_back(isec);1507        }1508        isd->sections = std::move(tmp);1509      }1510    }1511  }1512}1513 1514// We need to generate and finalize the content that depends on the address of1515// InputSections. As the generation of the content may also alter InputSection1516// addresses we must converge to a fixed point. We do that here. See the comment1517// in Writer<ELFT>::finalizeSections().1518template <class ELFT> void Writer<ELFT>::finalizeAddressDependentContent() {1519  llvm::TimeTraceScope timeScope("Finalize address dependent content");1520  AArch64Err843419Patcher a64p(ctx);1521  ARMErr657417Patcher a32p(ctx);1522  ctx.script->assignAddresses();1523 1524  // .ARM.exidx and SHF_LINK_ORDER do not require precise addresses, but they1525  // do require the relative addresses of OutputSections because linker scripts1526  // can assign Virtual Addresses to OutputSections that are not monotonically1527  // increasing. Anything here must be repeatable, since spilling may change1528  // section order.1529  const auto finalizeOrderDependentContent = [this] {1530    for (Partition &part : ctx.partitions)1531      finalizeSynthetic(ctx, part.armExidx.get());1532    resolveShfLinkOrder();1533  };1534  finalizeOrderDependentContent();1535 1536  // Converts call x@GDPLT to call __tls_get_addr1537  if (ctx.arg.emachine == EM_HEXAGON)1538    hexagonTLSSymbolUpdate(ctx);1539 1540  if (ctx.arg.randomizeSectionPadding)1541    randomizeSectionPadding(ctx);1542 1543  // Iterate until a fixed point is reached, skipping relocatable links since1544  // the final addresses are unavailable.1545  uint32_t pass = 0, assignPasses = 0;1546  while (!ctx.arg.relocatable) {1547    bool changed = ctx.target->needsThunks1548                       ? tc.createThunks(pass, ctx.outputSections)1549                       : ctx.target->relaxOnce(pass);1550    bool spilled = ctx.script->spillSections();1551    changed |= spilled;1552    ++pass;1553 1554    // With Thunk Size much smaller than branch range we expect to1555    // converge quickly; if we get to 30 something has gone wrong.1556    if (changed && pass >= 30) {1557      Err(ctx) << "address assignment did not converge";1558      break;1559    }1560 1561    if (ctx.arg.fixCortexA53Errata843419) {1562      if (changed)1563        ctx.script->assignAddresses();1564      changed |= a64p.createFixes();1565    }1566    if (ctx.arg.fixCortexA8) {1567      if (changed)1568        ctx.script->assignAddresses();1569      changed |= a32p.createFixes();1570    }1571 1572    finalizeSynthetic(ctx, ctx.in.got.get());1573    if (ctx.in.mipsGot)1574      ctx.in.mipsGot->updateAllocSize(ctx);1575 1576    for (Partition &part : ctx.partitions) {1577      // The R_AARCH64_AUTH_RELATIVE has a smaller addend field as bits [63:32]1578      // encode the signing schema. We've put relocations in .relr.auth.dyn1579      // during RelocationScanner::processAux, but the target VA for some of1580      // them might be wider than 32 bits. We can only know the final VA at this1581      // point, so move relocations with large values from .relr.auth.dyn to1582      // .rela.dyn. See also AArch64::relocate.1583      if (part.relrAuthDyn) {1584        auto it = llvm::remove_if(1585            part.relrAuthDyn->relocs, [this, &part](const RelativeReloc &elem) {1586              const Relocation &reloc = elem.inputSec->relocs()[elem.relocIdx];1587              if (isInt<32>(reloc.sym->getVA(ctx, reloc.addend)))1588                return false;1589              part.relaDyn->addReloc({R_AARCH64_AUTH_RELATIVE, elem.inputSec,1590                                      reloc.offset, false, *reloc.sym,1591                                      reloc.addend, R_ABS});1592              return true;1593            });1594        changed |= (it != part.relrAuthDyn->relocs.end());1595        part.relrAuthDyn->relocs.erase(it, part.relrAuthDyn->relocs.end());1596      }1597      if (part.relaDyn)1598        changed |= part.relaDyn->updateAllocSize(ctx);1599      if (part.relrDyn)1600        changed |= part.relrDyn->updateAllocSize(ctx);1601      if (part.relrAuthDyn)1602        changed |= part.relrAuthDyn->updateAllocSize(ctx);1603      if (part.memtagGlobalDescriptors)1604        changed |= part.memtagGlobalDescriptors->updateAllocSize(ctx);1605    }1606 1607    std::pair<const OutputSection *, const Defined *> changes =1608        ctx.script->assignAddresses();1609    if (!changed) {1610      // Some symbols may be dependent on section addresses. When we break the1611      // loop, the symbol values are finalized because a previous1612      // assignAddresses() finalized section addresses.1613      if (!changes.first && !changes.second)1614        break;1615      if (++assignPasses == 5) {1616        if (changes.first)1617          Err(ctx) << "address (0x" << Twine::utohexstr(changes.first->addr)1618                   << ") of section '" << changes.first->name1619                   << "' does not converge";1620        if (changes.second)1621          Err(ctx) << "assignment to symbol " << changes.second1622                   << " does not converge";1623        break;1624      }1625    } else if (spilled) {1626      // Spilling can change relative section order.1627      finalizeOrderDependentContent();1628    }1629  }1630  if (!ctx.arg.relocatable)1631    ctx.target->finalizeRelax(pass);1632 1633  if (ctx.arg.relocatable)1634    for (OutputSection *sec : ctx.outputSections)1635      sec->addr = 0;1636 1637  uint64_t imageBase = ctx.script->hasSectionsCommand || ctx.arg.relocatable1638                           ? 01639                           : ctx.target->getImageBase();1640  for (SectionCommand *cmd : ctx.script->sectionCommands) {1641    auto *osd = dyn_cast<OutputDesc>(cmd);1642    if (!osd)1643      continue;1644    OutputSection *osec = &osd->osec;1645    // Error if the address is below the image base when SECTIONS is absent1646    // (e.g. when -Ttext is specified and smaller than the default target image1647    // base for no-pie).1648    if (osec->addr < imageBase && (osec->flags & SHF_ALLOC)) {1649      Err(ctx) << "section '" << osec->name << "' address (0x"1650               << Twine::utohexstr(osec->addr)1651               << ") is smaller than image base (0x"1652               << Twine::utohexstr(imageBase) << "); specify --image-base";1653    }1654 1655    // If addrExpr is set, the address may not be a multiple of the alignment.1656    // Warn because this is error-prone.1657    if (osec->addr % osec->addralign != 0)1658      Warn(ctx) << "address (0x" << Twine::utohexstr(osec->addr)1659                << ") of section " << osec->name1660                << " is not a multiple of alignment (" << osec->addralign1661                << ")";1662  }1663 1664  // Sizes are no longer allowed to grow, so all allowable spills have been1665  // taken. Remove any leftover potential spills.1666  ctx.script->erasePotentialSpillSections();1667}1668 1669// If Input Sections have been shrunk (basic block sections) then1670// update symbol values and sizes associated with these sections.  With basic1671// block sections, input sections can shrink when the jump instructions at1672// the end of the section are relaxed.1673static void fixSymbolsAfterShrinking(Ctx &ctx) {1674  for (InputFile *File : ctx.objectFiles) {1675    parallelForEach(File->getSymbols(), [&](Symbol *Sym) {1676      auto *def = dyn_cast<Defined>(Sym);1677      if (!def)1678        return;1679 1680      const SectionBase *sec = def->section;1681      if (!sec)1682        return;1683 1684      const InputSectionBase *inputSec = dyn_cast<InputSectionBase>(sec);1685      if (!inputSec || !inputSec->bytesDropped)1686        return;1687 1688      const size_t OldSize = inputSec->content().size();1689      const size_t NewSize = OldSize - inputSec->bytesDropped;1690 1691      if (def->value > NewSize && def->value <= OldSize) {1692        LLVM_DEBUG(llvm::dbgs()1693                   << "Moving symbol " << Sym->getName() << " from "1694                   << def->value << " to "1695                   << def->value - inputSec->bytesDropped << " bytes\n");1696        def->value -= inputSec->bytesDropped;1697        return;1698      }1699 1700      if (def->value + def->size > NewSize && def->value <= OldSize &&1701          def->value + def->size <= OldSize) {1702        LLVM_DEBUG(llvm::dbgs()1703                   << "Shrinking symbol " << Sym->getName() << " from "1704                   << def->size << " to " << def->size - inputSec->bytesDropped1705                   << " bytes\n");1706        def->size -= inputSec->bytesDropped;1707      }1708    });1709  }1710}1711 1712// If basic block sections exist, there are opportunities to delete fall thru1713// jumps and shrink jump instructions after basic block reordering.  This1714// relaxation pass does that.  It is only enabled when --optimize-bb-jumps1715// option is used.1716template <class ELFT> void Writer<ELFT>::optimizeBasicBlockJumps() {1717  assert(ctx.arg.optimizeBBJumps);1718  SmallVector<InputSection *, 0> storage;1719 1720  ctx.script->assignAddresses();1721  // For every output section that has executable input sections, this1722  // does the following:1723  //   1. Deletes all direct jump instructions in input sections that1724  //      jump to the following section as it is not required.1725  //   2. If there are two consecutive jump instructions, it checks1726  //      if they can be flipped and one can be deleted.1727  for (OutputSection *osec : ctx.outputSections) {1728    if (!(osec->flags & SHF_EXECINSTR))1729      continue;1730    ArrayRef<InputSection *> sections = getInputSections(*osec, storage);1731    size_t numDeleted = 0;1732    // Delete all fall through jump instructions.  Also, check if two1733    // consecutive jump instructions can be flipped so that a fall1734    // through jmp instruction can be deleted.1735    for (size_t i = 0, e = sections.size(); i != e; ++i) {1736      InputSection *next = i + 1 < sections.size() ? sections[i + 1] : nullptr;1737      InputSection &sec = *sections[i];1738      numDeleted += ctx.target->deleteFallThruJmpInsn(sec, sec.file, next);1739    }1740    if (numDeleted > 0) {1741      ctx.script->assignAddresses();1742      LLVM_DEBUG(llvm::dbgs()1743                 << "Removing " << numDeleted << " fall through jumps\n");1744    }1745  }1746 1747  fixSymbolsAfterShrinking(ctx);1748 1749  for (OutputSection *osec : ctx.outputSections)1750    for (InputSection *is : getInputSections(*osec, storage))1751      is->trim();1752}1753 1754// In order to allow users to manipulate linker-synthesized sections,1755// we had to add synthetic sections to the input section list early,1756// even before we make decisions whether they are needed. This allows1757// users to write scripts like this: ".mygot : { .got }".1758//1759// Doing it has an unintended side effects. If it turns out that we1760// don't need a .got (for example) at all because there's no1761// relocation that needs a .got, we don't want to emit .got.1762//1763// To deal with the above problem, this function is called after1764// scanRelocations is called to remove synthetic sections that turn1765// out to be empty.1766static void removeUnusedSyntheticSections(Ctx &ctx) {1767  // All input synthetic sections that can be empty are placed after1768  // all regular ones. Reverse iterate to find the first synthetic section1769  // after a non-synthetic one which will be our starting point.1770  auto start =1771      llvm::find_if(llvm::reverse(ctx.inputSections), [](InputSectionBase *s) {1772        return !isa<SyntheticSection>(s);1773      }).base();1774 1775  // Remove unused synthetic sections from ctx.inputSections;1776  DenseSet<InputSectionBase *> unused;1777  auto end =1778      std::remove_if(start, ctx.inputSections.end(), [&](InputSectionBase *s) {1779        auto *sec = cast<SyntheticSection>(s);1780        if (sec->getParent() && sec->isNeeded())1781          return false;1782        // .relr.auth.dyn relocations may be moved to .rela.dyn in1783        // finalizeAddressDependentContent, making .rela.dyn no longer empty.1784        // Conservatively keep .rela.dyn. .relr.auth.dyn can be made empty, but1785        // we would fail to remove it here.1786        if (ctx.arg.emachine == EM_AARCH64 && ctx.arg.relrPackDynRelocs &&1787            sec == ctx.mainPart->relaDyn.get())1788          return false;1789        unused.insert(sec);1790        return true;1791      });1792  ctx.inputSections.erase(end, ctx.inputSections.end());1793 1794  // Remove unused synthetic sections from the corresponding input section1795  // description and orphanSections.1796  for (auto *sec : unused)1797    if (OutputSection *osec = cast<SyntheticSection>(sec)->getParent())1798      for (SectionCommand *cmd : osec->commands)1799        if (auto *isd = dyn_cast<InputSectionDescription>(cmd))1800          llvm::erase_if(isd->sections, [&](InputSection *isec) {1801            return unused.count(isec);1802          });1803  llvm::erase_if(ctx.script->orphanSections, [&](const InputSectionBase *sec) {1804    return unused.count(sec);1805  });1806}1807 1808// Create output section objects and add them to OutputSections.1809template <class ELFT> void Writer<ELFT>::finalizeSections() {1810  if (!ctx.arg.relocatable) {1811    ctx.out.preinitArray = findSection(ctx, ".preinit_array");1812    ctx.out.initArray = findSection(ctx, ".init_array");1813    ctx.out.finiArray = findSection(ctx, ".fini_array");1814 1815    // The linker needs to define SECNAME_start, SECNAME_end and SECNAME_stop1816    // symbols for sections, so that the runtime can get the start and end1817    // addresses of each section by section name. Add such symbols.1818    addStartEndSymbols();1819    for (SectionCommand *cmd : ctx.script->sectionCommands)1820      if (auto *osd = dyn_cast<OutputDesc>(cmd))1821        addStartStopSymbols(osd->osec);1822 1823    // Add _DYNAMIC symbol. Unlike GNU gold, our _DYNAMIC symbol has no type.1824    // It should be okay as no one seems to care about the type.1825    // Even the author of gold doesn't remember why gold behaves that way.1826    // https://sourceware.org/ml/binutils/2002-03/msg00360.html1827    if (ctx.mainPart->dynamic->parent) {1828      Symbol *s = ctx.symtab->addSymbol(Defined{1829          ctx, ctx.internalFile, "_DYNAMIC", STB_WEAK, STV_HIDDEN, STT_NOTYPE,1830          /*value=*/0, /*size=*/0, ctx.mainPart->dynamic.get()});1831      s->isUsedInRegularObj = true;1832    }1833 1834    // Define __rel[a]_iplt_{start,end} symbols if needed.1835    addRelIpltSymbols();1836 1837    // RISC-V's gp can address +/- 2 KiB, set it to .sdata + 0x800. This symbol1838    // should only be defined in an executable. If .sdata does not exist, its1839    // value/section does not matter but it has to be relative, so set its1840    // st_shndx arbitrarily to 1 (ctx.out.elfHeader).1841    if (ctx.arg.emachine == EM_RISCV) {1842      if (!ctx.arg.shared) {1843        OutputSection *sec = findSection(ctx, ".sdata");1844        addOptionalRegular(ctx, "__global_pointer$",1845                           sec ? sec : ctx.out.elfHeader.get(), 0x800,1846                           STV_DEFAULT);1847        // Set riscvGlobalPointer to be used by the optional global pointer1848        // relaxation.1849        if (ctx.arg.relaxGP) {1850          Symbol *s = ctx.symtab->find("__global_pointer$");1851          if (s && s->isDefined())1852            ctx.sym.riscvGlobalPointer = cast<Defined>(s);1853        }1854      }1855    }1856 1857    if (ctx.arg.emachine == EM_386 || ctx.arg.emachine == EM_X86_64) {1858      // On targets that support TLSDESC, _TLS_MODULE_BASE_ is defined in such a1859      // way that:1860      //1861      // 1) Without relaxation: it produces a dynamic TLSDESC relocation that1862      // computes 0.1863      // 2) With LD->LE relaxation: _TLS_MODULE_BASE_@tpoff = 0 (lowest address1864      // in the TLS block).1865      //1866      // 2) is special cased in @tpoff computation. To satisfy 1), we define it1867      // as an absolute symbol of zero. This is different from GNU linkers which1868      // define _TLS_MODULE_BASE_ relative to the first TLS section.1869      Symbol *s = ctx.symtab->find("_TLS_MODULE_BASE_");1870      if (s && s->isUndefined()) {1871        s->resolve(ctx, Defined{ctx, ctx.internalFile, StringRef(), STB_GLOBAL,1872                                STV_HIDDEN, STT_TLS, /*value=*/0, 0,1873                                /*section=*/nullptr});1874        ctx.sym.tlsModuleBase = cast<Defined>(s);1875      }1876    }1877 1878    // This responsible for splitting up .eh_frame section into1879    // pieces. The relocation scan uses those pieces, so this has to be1880    // earlier.1881    {1882      llvm::TimeTraceScope timeScope("Finalize .eh_frame");1883      for (Partition &part : ctx.partitions)1884        finalizeSynthetic(ctx, part.ehFrame.get());1885    }1886  }1887 1888  // If the previous code block defines any non-hidden symbols (e.g.1889  // __global_pointer$), they may be exported.1890  if (ctx.arg.exportDynamic)1891    for (Symbol *sym : ctx.synthesizedSymbols)1892      if (sym->computeBinding(ctx) != STB_LOCAL)1893        sym->isExported = true;1894 1895  demoteSymbolsAndComputeIsPreemptible(ctx);1896 1897  if (ctx.arg.copyRelocs && ctx.arg.discard != DiscardPolicy::None)1898    markUsedLocalSymbols<ELFT>(ctx);1899  demoteAndCopyLocalSymbols(ctx);1900 1901  if (ctx.arg.copyRelocs)1902    addSectionSymbols();1903 1904  // Change values of linker-script-defined symbols from placeholders (assigned1905  // by declareSymbols) to actual definitions.1906  ctx.script->processSymbolAssignments();1907 1908  if (!ctx.arg.relocatable) {1909    llvm::TimeTraceScope timeScope("Scan relocations");1910    // Scan relocations. This must be done after every symbol is declared so1911    // that we can correctly decide if a dynamic relocation is needed. This is1912    // called after processSymbolAssignments() because it needs to know whether1913    // a linker-script-defined symbol is absolute.1914    scanRelocations<ELFT>(ctx);1915    reportUndefinedSymbols(ctx);1916    postScanRelocations(ctx);1917 1918    if (ctx.in.plt && ctx.in.plt->isNeeded())1919      ctx.in.plt->addSymbols();1920    if (ctx.in.iplt && ctx.in.iplt->isNeeded())1921      ctx.in.iplt->addSymbols();1922 1923    if (ctx.arg.unresolvedSymbolsInShlib != UnresolvedPolicy::Ignore) {1924      auto diag =1925          ctx.arg.unresolvedSymbolsInShlib == UnresolvedPolicy::ReportError &&1926                  !ctx.arg.noinhibitExec1927              ? DiagLevel::Err1928              : DiagLevel::Warn;1929      // Error on undefined symbols in a shared object, if all of its DT_NEEDED1930      // entries are seen. These cases would otherwise lead to runtime errors1931      // reported by the dynamic linker.1932      //1933      // ld.bfd traces all DT_NEEDED to emulate the logic of the dynamic linker1934      // to catch more cases. That is too much for us. Our approach resembles1935      // the one used in ld.gold, achieves a good balance to be useful but not1936      // too smart.1937      //1938      // If a DSO reference is resolved by a SharedSymbol, but the SharedSymbol1939      // is overridden by a hidden visibility Defined (which is later discarded1940      // due to GC), don't report the diagnostic. However, this may indicate an1941      // unintended SharedSymbol.1942      for (SharedFile *file : ctx.sharedFiles) {1943        bool allNeededIsKnown =1944            llvm::all_of(file->dtNeeded, [&](StringRef needed) {1945              return ctx.symtab->soNames.count(CachedHashStringRef(needed));1946            });1947        if (!allNeededIsKnown)1948          continue;1949        for (Symbol *sym : file->requiredSymbols) {1950          if (sym->dsoDefined)1951            continue;1952          if (sym->isUndefined() && !sym->isWeak()) {1953            ELFSyncStream(ctx, diag)1954                << "undefined reference: " << sym << "\n>>> referenced by "1955                << file << " (disallowed by --no-allow-shlib-undefined)";1956          } else if (sym->isDefined() &&1957                     sym->computeBinding(ctx) == STB_LOCAL) {1958            ELFSyncStream(ctx, diag)1959                << "non-exported symbol '" << sym << "' in '" << sym->file1960                << "' is referenced by DSO '" << file << "'";1961          }1962        }1963      }1964    }1965  }1966 1967  {1968    llvm::TimeTraceScope timeScope("Add symbols to symtabs");1969    // Now that we have defined all possible global symbols including linker-1970    // synthesized ones. Visit all symbols to give the finishing touches.1971    for (Symbol *sym : ctx.symtab->getSymbols()) {1972      if (!sym->isUsedInRegularObj || !includeInSymtab(ctx, *sym))1973        continue;1974      if (!ctx.arg.relocatable)1975        sym->binding = sym->computeBinding(ctx);1976      if (ctx.in.symTab)1977        ctx.in.symTab->addSymbol(sym);1978 1979      // computeBinding might localize a symbol that was considered exported1980      // but then synthesized as hidden (e.g. _DYNAMIC).1981      if ((sym->isExported || sym->isPreemptible) && !sym->isLocal()) {1982        ctx.partitions[sym->partition - 1].dynSymTab->addSymbol(sym);1983        if (auto *file = dyn_cast<SharedFile>(sym->file))1984          if (file->isNeeded && !sym->isUndefined())1985            addVerneed(ctx, *sym);1986      }1987    }1988 1989    // We also need to scan the dynamic relocation tables of the other1990    // partitions and add any referenced symbols to the partition's dynsym.1991    for (Partition &part :1992         MutableArrayRef<Partition>(ctx.partitions).slice(1)) {1993      DenseSet<Symbol *> syms;1994      for (const SymbolTableEntry &e : part.dynSymTab->getSymbols())1995        syms.insert(e.sym);1996      for (DynamicReloc &reloc : part.relaDyn->relocs)1997        if (reloc.sym && reloc.needsDynSymIndex() &&1998            syms.insert(reloc.sym).second)1999          part.dynSymTab->addSymbol(reloc.sym);2000    }2001  }2002 2003  if (ctx.in.mipsGot)2004    ctx.in.mipsGot->build();2005 2006  removeUnusedSyntheticSections(ctx);2007  ctx.script->diagnoseOrphanHandling();2008  ctx.script->diagnoseMissingSGSectionAddress();2009 2010  sortSections();2011 2012  // Create a list of OutputSections, assign sectionIndex, and populate2013  // ctx.in.shStrTab. If -z nosectionheader is specified, drop non-ALLOC2014  // sections.2015  for (SectionCommand *cmd : ctx.script->sectionCommands)2016    if (auto *osd = dyn_cast<OutputDesc>(cmd)) {2017      OutputSection *osec = &osd->osec;2018      if (!ctx.in.shStrTab && !(osec->flags & SHF_ALLOC))2019        continue;2020      ctx.outputSections.push_back(osec);2021      osec->sectionIndex = ctx.outputSections.size();2022      if (ctx.in.shStrTab)2023        osec->shName = ctx.in.shStrTab->addString(osec->name);2024    }2025 2026  // Prefer command line supplied address over other constraints.2027  for (OutputSection *sec : ctx.outputSections) {2028    auto i = ctx.arg.sectionStartMap.find(sec->name);2029    if (i != ctx.arg.sectionStartMap.end())2030      sec->addrExpr = [=] { return i->second; };2031  }2032 2033  // With the ctx.outputSections available check for GDPLT relocations2034  // and add __tls_get_addr symbol if needed.2035  if (ctx.arg.emachine == EM_HEXAGON &&2036      hexagonNeedsTLSSymbol(ctx.outputSections)) {2037    Symbol *sym =2038        ctx.symtab->addSymbol(Undefined{ctx.internalFile, "__tls_get_addr",2039                                        STB_GLOBAL, STV_DEFAULT, STT_NOTYPE});2040    sym->isPreemptible = true;2041    ctx.partitions[0].dynSymTab->addSymbol(sym);2042  }2043 2044  // This is a bit of a hack. A value of 0 means undef, so we set it2045  // to 1 to make __ehdr_start defined. The section number is not2046  // particularly relevant.2047  ctx.out.elfHeader->sectionIndex = 1;2048  ctx.out.elfHeader->size = sizeof(typename ELFT::Ehdr);2049 2050  // Binary and relocatable output does not have PHDRS.2051  // The headers have to be created before finalize as that can influence the2052  // image base and the dynamic section on mips includes the image base.2053  if (!ctx.arg.relocatable && !ctx.arg.oFormatBinary) {2054    for (Partition &part : ctx.partitions) {2055      part.phdrs = ctx.script->hasPhdrsCommands() ? ctx.script->createPhdrs()2056                                                  : createPhdrs(part);2057      if (ctx.arg.emachine == EM_ARM) {2058        // PT_ARM_EXIDX is the ARM EHABI equivalent of PT_GNU_EH_FRAME2059        addPhdrForSection(part, SHT_ARM_EXIDX, PT_ARM_EXIDX, PF_R);2060      }2061      if (ctx.arg.emachine == EM_MIPS) {2062        // Add separate segments for MIPS-specific sections.2063        addPhdrForSection(part, SHT_MIPS_REGINFO, PT_MIPS_REGINFO, PF_R);2064        addPhdrForSection(part, SHT_MIPS_OPTIONS, PT_MIPS_OPTIONS, PF_R);2065        addPhdrForSection(part, SHT_MIPS_ABIFLAGS, PT_MIPS_ABIFLAGS, PF_R);2066      }2067      if (ctx.arg.emachine == EM_RISCV)2068        addPhdrForSection(part, SHT_RISCV_ATTRIBUTES, PT_RISCV_ATTRIBUTES,2069                          PF_R);2070    }2071    ctx.out.programHeaders->size =2072        sizeof(Elf_Phdr) * ctx.mainPart->phdrs.size();2073 2074    // Find the TLS segment. This happens before the section layout loop so that2075    // Android relocation packing can look up TLS symbol addresses. We only need2076    // to care about the main partition here because all TLS symbols were moved2077    // to the main partition (see MarkLive.cpp).2078    for (auto &p : ctx.mainPart->phdrs)2079      if (p->p_type == PT_TLS)2080        ctx.tlsPhdr = p.get();2081  }2082 2083  // Some symbols are defined in term of program headers. Now that we2084  // have the headers, we can find out which sections they point to.2085  setReservedSymbolSections();2086 2087  if (ctx.script->noCrossRefs.size()) {2088    llvm::TimeTraceScope timeScope("Check NOCROSSREFS");2089    checkNoCrossRefs<ELFT>(ctx);2090  }2091 2092  {2093    llvm::TimeTraceScope timeScope("Finalize synthetic sections");2094 2095    finalizeSynthetic(ctx, ctx.in.bss.get());2096    finalizeSynthetic(ctx, ctx.in.bssRelRo.get());2097    finalizeSynthetic(ctx, ctx.in.symTabShndx.get());2098    finalizeSynthetic(ctx, ctx.in.shStrTab.get());2099    finalizeSynthetic(ctx, ctx.in.strTab.get());2100    finalizeSynthetic(ctx, ctx.in.got.get());2101    finalizeSynthetic(ctx, ctx.in.mipsGot.get());2102    finalizeSynthetic(ctx, ctx.in.igotPlt.get());2103    finalizeSynthetic(ctx, ctx.in.gotPlt.get());2104    finalizeSynthetic(ctx, ctx.in.relaPlt.get());2105    finalizeSynthetic(ctx, ctx.in.plt.get());2106    finalizeSynthetic(ctx, ctx.in.iplt.get());2107    finalizeSynthetic(ctx, ctx.in.ppc32Got2.get());2108    finalizeSynthetic(ctx, ctx.in.partIndex.get());2109 2110    // Dynamic section must be the last one in this list and dynamic2111    // symbol table section (dynSymTab) must be the first one.2112    for (Partition &part : ctx.partitions) {2113      if (part.relaDyn) {2114        part.relaDyn->mergeRels();2115        // Compute DT_RELACOUNT to be used by part.dynamic.2116        part.relaDyn->partitionRels();2117        finalizeSynthetic(ctx, part.relaDyn.get());2118      }2119      if (part.relrDyn) {2120        part.relrDyn->mergeRels();2121        finalizeSynthetic(ctx, part.relrDyn.get());2122      }2123      if (part.relrAuthDyn) {2124        part.relrAuthDyn->mergeRels();2125        finalizeSynthetic(ctx, part.relrAuthDyn.get());2126      }2127 2128      finalizeSynthetic(ctx, part.dynSymTab.get());2129      finalizeSynthetic(ctx, part.gnuHashTab.get());2130      finalizeSynthetic(ctx, part.hashTab.get());2131      finalizeSynthetic(ctx, part.verDef.get());2132      finalizeSynthetic(ctx, part.ehFrameHdr.get());2133      finalizeSynthetic(ctx, part.verSym.get());2134      finalizeSynthetic(ctx, part.verNeed.get());2135      finalizeSynthetic(ctx, part.dynamic.get());2136    }2137  }2138 2139  if (!ctx.script->hasSectionsCommand && !ctx.arg.relocatable)2140    fixSectionAlignments();2141 2142  // This is used to:2143  // 1) Create "thunks":2144  //    Jump instructions in many ISAs have small displacements, and therefore2145  //    they cannot jump to arbitrary addresses in memory. For example, RISC-V2146  //    JAL instruction can target only +-1 MiB from PC. It is a linker's2147  //    responsibility to create and insert small pieces of code between2148  //    sections to extend the ranges if jump targets are out of range. Such2149  //    code pieces are called "thunks".2150  //2151  //    We add thunks at this stage. We couldn't do this before this point2152  //    because this is the earliest point where we know sizes of sections and2153  //    their layouts (that are needed to determine if jump targets are in2154  //    range).2155  //2156  // 2) Update the sections. We need to generate content that depends on the2157  //    address of InputSections. For example, MIPS GOT section content or2158  //    android packed relocations sections content.2159  //2160  // 3) Assign the final values for the linker script symbols. Linker scripts2161  //    sometimes using forward symbol declarations. We want to set the correct2162  //    values. They also might change after adding the thunks.2163  finalizeAddressDependentContent();2164 2165  // All information needed for OutputSection part of Map file is available.2166  if (errCount(ctx))2167    return;2168 2169  {2170    llvm::TimeTraceScope timeScope("Finalize synthetic sections");2171    // finalizeAddressDependentContent may have added local symbols to the2172    // static symbol table.2173    finalizeSynthetic(ctx, ctx.in.symTab.get());2174    finalizeSynthetic(ctx, ctx.in.debugNames.get());2175    finalizeSynthetic(ctx, ctx.in.ppc64LongBranchTarget.get());2176    finalizeSynthetic(ctx, ctx.in.armCmseSGSection.get());2177  }2178 2179  // Relaxation to delete inter-basic block jumps created by basic block2180  // sections. Run after ctx.in.symTab is finalized as optimizeBasicBlockJumps2181  // can relax jump instructions based on symbol offset.2182  if (ctx.arg.optimizeBBJumps)2183    optimizeBasicBlockJumps();2184 2185  // Fill other section headers. The dynamic table is finalized2186  // at the end because some tags like RELSZ depend on result2187  // of finalizing other sections.2188  for (OutputSection *sec : ctx.outputSections)2189    sec->finalize(ctx);2190 2191  ctx.script->checkFinalScriptConditions();2192 2193  if (ctx.arg.emachine == EM_ARM && !ctx.arg.isLE && ctx.arg.armBe8) {2194    addArmInputSectionMappingSymbols(ctx);2195    sortArmMappingSymbols(ctx);2196  }2197}2198 2199// Ensure data sections are not mixed with executable sections when2200// --execute-only is used. --execute-only make pages executable but not2201// readable.2202template <class ELFT> void Writer<ELFT>::checkExecuteOnly() {2203  if (!ctx.arg.executeOnly)2204    return;2205 2206  SmallVector<InputSection *, 0> storage;2207  for (OutputSection *osec : ctx.outputSections)2208    if (osec->flags & SHF_EXECINSTR)2209      for (InputSection *isec : getInputSections(*osec, storage))2210        if (!(isec->flags & SHF_EXECINSTR))2211          ErrAlways(ctx) << "cannot place " << isec << " into " << osec->name2212                         << ": --execute-only does not support intermingling "2213                            "data and code";2214}2215 2216// Check which input sections of RX output sections don't have the2217// SHF_AARCH64_PURECODE or SHF_ARM_PURECODE flag set.2218template <class ELFT> void Writer<ELFT>::checkExecuteOnlyReport() {2219  if (ctx.arg.zExecuteOnlyReport == ReportPolicy::None)2220    return;2221 2222  auto reportUnless = [&](bool cond) -> ELFSyncStream {2223    if (cond)2224      return {ctx, DiagLevel::None};2225    return {ctx, toDiagLevel(ctx.arg.zExecuteOnlyReport)};2226  };2227 2228  uint64_t purecodeFlag =2229      ctx.arg.emachine == EM_AARCH64 ? SHF_AARCH64_PURECODE : SHF_ARM_PURECODE;2230  StringRef purecodeFlagName = ctx.arg.emachine == EM_AARCH642231                                   ? "SHF_AARCH64_PURECODE"2232                                   : "SHF_ARM_PURECODE";2233  SmallVector<InputSection *, 0> storage;2234  for (OutputSection *osec : ctx.outputSections) {2235    if (osec->getPhdrFlags() != (PF_R | PF_X))2236      continue;2237    for (InputSection *sec : getInputSections(*osec, storage)) {2238      if (isa<SyntheticSection>(sec))2239        continue;2240      reportUnless(sec->flags & purecodeFlag)2241          << "-z execute-only-report: " << sec << " does not have "2242          << purecodeFlagName << " flag set";2243    }2244  }2245}2246 2247// The linker is expected to define SECNAME_start and SECNAME_end2248// symbols for a few sections. This function defines them.2249template <class ELFT> void Writer<ELFT>::addStartEndSymbols() {2250  // If the associated output section does not exist, there is ambiguity as to2251  // how we define _start and _end symbols for an init/fini section. Users2252  // expect no "undefined symbol" linker errors and loaders expect equal2253  // st_value but do not particularly care whether the symbols are defined or2254  // not. We retain the output section so that the section indexes will be2255  // correct.2256  auto define = [=](StringRef start, StringRef end, OutputSection *os) {2257    if (os) {2258      Defined *startSym = addOptionalRegular(ctx, start, os, 0);2259      Defined *stopSym = addOptionalRegular(ctx, end, os, -1);2260      if (startSym || stopSym)2261        os->usedInExpression = true;2262    } else {2263      addOptionalRegular(ctx, start, ctx.out.elfHeader.get(), 0);2264      addOptionalRegular(ctx, end, ctx.out.elfHeader.get(), 0);2265    }2266  };2267 2268  define("__preinit_array_start", "__preinit_array_end", ctx.out.preinitArray);2269  define("__init_array_start", "__init_array_end", ctx.out.initArray);2270  define("__fini_array_start", "__fini_array_end", ctx.out.finiArray);2271 2272  // As a special case, don't unnecessarily retain .ARM.exidx, which would2273  // create an empty PT_ARM_EXIDX.2274  if (OutputSection *sec = findSection(ctx, ".ARM.exidx"))2275    define("__exidx_start", "__exidx_end", sec);2276}2277 2278// If a section name is valid as a C identifier (which is rare because of2279// the leading '.'), linkers are expected to define __start_<secname> and2280// __stop_<secname> symbols. They are at beginning and end of the section,2281// respectively. This is not requested by the ELF standard, but GNU ld and2282// gold provide the feature, and used by many programs.2283template <class ELFT>2284void Writer<ELFT>::addStartStopSymbols(OutputSection &osec) {2285  StringRef s = osec.name;2286  if (!isValidCIdentifier(s))2287    return;2288  StringSaver &ss = ctx.saver;2289  Defined *startSym = addOptionalRegular(ctx, ss.save("__start_" + s), &osec, 0,2290                                         ctx.arg.zStartStopVisibility);2291  Defined *stopSym = addOptionalRegular(ctx, ss.save("__stop_" + s), &osec, -1,2292                                        ctx.arg.zStartStopVisibility);2293  if (startSym || stopSym)2294    osec.usedInExpression = true;2295}2296 2297static bool needsPtLoad(OutputSection *sec) {2298  if (!(sec->flags & SHF_ALLOC))2299    return false;2300 2301  // Don't allocate VA space for TLS NOBITS sections. The PT_TLS PHDR is2302  // responsible for allocating space for them, not the PT_LOAD that2303  // contains the TLS initialization image.2304  if ((sec->flags & SHF_TLS) && sec->type == SHT_NOBITS)2305    return false;2306  return true;2307}2308 2309// Adjust phdr flags according to certain options.2310static uint64_t computeFlags(Ctx &ctx, uint64_t flags) {2311  if (ctx.arg.omagic)2312    return PF_R | PF_W | PF_X;2313  if (ctx.arg.executeOnly && (flags & PF_X))2314    return flags & ~PF_R;2315  return flags;2316}2317 2318// Decide which program headers to create and which sections to include in each2319// one.2320template <class ELFT>2321SmallVector<std::unique_ptr<PhdrEntry>, 0>2322Writer<ELFT>::createPhdrs(Partition &part) {2323  SmallVector<std::unique_ptr<PhdrEntry>, 0> ret;2324  auto addHdr = [&, &ctx = ctx](unsigned type, unsigned flags) -> PhdrEntry * {2325    ret.push_back(std::make_unique<PhdrEntry>(ctx, type, flags));2326    return ret.back().get();2327  };2328 2329  unsigned partNo = part.getNumber(ctx);2330  bool isMain = partNo == 1;2331 2332  // Add the first PT_LOAD segment for regular output sections.2333  uint64_t flags = computeFlags(ctx, PF_R);2334  PhdrEntry *load = nullptr;2335 2336  // nmagic or omagic output does not have PT_PHDR, PT_INTERP, or the readonly2337  // PT_LOAD.2338  if (!ctx.arg.nmagic && !ctx.arg.omagic) {2339    // The first phdr entry is PT_PHDR which describes the program header2340    // itself.2341    if (isMain)2342      addHdr(PT_PHDR, PF_R)->add(ctx.out.programHeaders.get());2343    else2344      addHdr(PT_PHDR, PF_R)->add(part.programHeaders->getParent());2345 2346    // PT_INTERP must be the second entry if exists.2347    if (OutputSection *cmd = findSection(ctx, ".interp", partNo))2348      addHdr(PT_INTERP, cmd->getPhdrFlags())->add(cmd);2349 2350    // Add the headers. We will remove them if they don't fit.2351    // In the other partitions the headers are ordinary sections, so they don't2352    // need to be added here.2353    if (isMain) {2354      load = addHdr(PT_LOAD, flags);2355      load->add(ctx.out.elfHeader.get());2356      load->add(ctx.out.programHeaders.get());2357    }2358  }2359 2360  // PT_GNU_RELRO includes all sections that should be marked as2361  // read-only by dynamic linker after processing relocations.2362  // Current dynamic loaders only support one PT_GNU_RELRO PHDR, give2363  // an error message if more than one PT_GNU_RELRO PHDR is required.2364  auto relRo = std::make_unique<PhdrEntry>(ctx, PT_GNU_RELRO, PF_R);2365  bool inRelroPhdr = false;2366  OutputSection *relroEnd = nullptr;2367  for (OutputSection *sec : ctx.outputSections) {2368    if (sec->partition != partNo || !needsPtLoad(sec))2369      continue;2370    if (isRelroSection(ctx, sec)) {2371      inRelroPhdr = true;2372      if (!relroEnd)2373        relRo->add(sec);2374      else2375        ErrAlways(ctx) << "section: " << sec->name2376                       << " is not contiguous with other relro" << " sections";2377    } else if (inRelroPhdr) {2378      inRelroPhdr = false;2379      relroEnd = sec;2380    }2381  }2382  relRo->p_align = 1;2383 2384  for (OutputSection *sec : ctx.outputSections) {2385    if (!needsPtLoad(sec))2386      continue;2387 2388    // Normally, sections in partitions other than the current partition are2389    // ignored. But partition number 255 is a special case: it contains the2390    // partition end marker (.part.end). It needs to be added to the main2391    // partition so that a segment is created for it in the main partition,2392    // which will cause the dynamic loader to reserve space for the other2393    // partitions.2394    if (sec->partition != partNo) {2395      if (isMain && sec->partition == 255)2396        addHdr(PT_LOAD, computeFlags(ctx, sec->getPhdrFlags()))->add(sec);2397      continue;2398    }2399 2400    // Segments are contiguous memory regions that has the same attributes2401    // (e.g. executable or writable). There is one phdr for each segment.2402    // Therefore, we need to create a new phdr when the next section has2403    // incompatible flags or is loaded at a discontiguous address or memory2404    // region using AT or AT> linker script command, respectively.2405    //2406    // As an exception, we don't create a separate load segment for the ELF2407    // headers, even if the first "real" output has an AT or AT> attribute.2408    //2409    // In addition, NOBITS sections should only be placed at the end of a LOAD2410    // segment (since it's represented as p_filesz < p_memsz). If we have a2411    // not-NOBITS section after a NOBITS, we create a new LOAD for the latter2412    // even if flags match, so as not to require actually writing the2413    // supposed-to-be-NOBITS section to the output file. (However, we cannot do2414    // so when hasSectionsCommand, since we cannot introduce the extra alignment2415    // needed to create a new LOAD)2416    uint64_t newFlags = computeFlags(ctx, sec->getPhdrFlags());2417    uint64_t incompatible = flags ^ newFlags;2418    if (!(newFlags & PF_W)) {2419      // When --no-rosegment is specified, RO and RX sections are compatible.2420      if (ctx.arg.singleRoRx)2421        incompatible &= ~PF_X;2422      // When --no-xosegment is specified (the default), XO and RX sections are2423      // compatible.2424      if (ctx.arg.singleXoRx)2425        incompatible &= ~PF_R;2426    }2427    if (incompatible)2428      load = nullptr;2429 2430    bool sameLMARegion =2431        load && !sec->lmaExpr && sec->lmaRegion == load->firstSec->lmaRegion;2432    if (load && sec != relroEnd &&2433        sec->memRegion == load->firstSec->memRegion &&2434        (sameLMARegion || load->lastSec == ctx.out.programHeaders.get()) &&2435        (ctx.script->hasSectionsCommand || sec->type == SHT_NOBITS ||2436         load->lastSec->type != SHT_NOBITS)) {2437      load->p_flags |= newFlags;2438    } else {2439      load = addHdr(PT_LOAD, newFlags);2440      flags = newFlags;2441    }2442 2443    load->add(sec);2444  }2445 2446  // Add a TLS segment if any.2447  auto tlsHdr = std::make_unique<PhdrEntry>(ctx, PT_TLS, PF_R);2448  for (OutputSection *sec : ctx.outputSections)2449    if (sec->partition == partNo && sec->flags & SHF_TLS)2450      tlsHdr->add(sec);2451  if (tlsHdr->firstSec)2452    ret.push_back(std::move(tlsHdr));2453 2454  // Add an entry for .dynamic.2455  if (OutputSection *sec = part.dynamic->getParent())2456    addHdr(PT_DYNAMIC, sec->getPhdrFlags())->add(sec);2457 2458  if (relRo->firstSec)2459    ret.push_back(std::move(relRo));2460 2461  // PT_GNU_EH_FRAME is a special section pointing on .eh_frame_hdr.2462  if (part.ehFrame->isNeeded() && part.ehFrameHdr &&2463      part.ehFrame->getParent() && part.ehFrameHdr->getParent())2464    addHdr(PT_GNU_EH_FRAME, part.ehFrameHdr->getParent()->getPhdrFlags())2465        ->add(part.ehFrameHdr->getParent());2466 2467  if (ctx.arg.osabi == ELFOSABI_OPENBSD) {2468    // PT_OPENBSD_MUTABLE makes the dynamic linker fill the segment with2469    // zero data, like bss, but it can be treated differently.2470    if (OutputSection *cmd = findSection(ctx, ".openbsd.mutable", partNo))2471      addHdr(PT_OPENBSD_MUTABLE, cmd->getPhdrFlags())->add(cmd);2472 2473    // PT_OPENBSD_RANDOMIZE makes the dynamic linker fill the segment2474    // with random data.2475    if (OutputSection *cmd = findSection(ctx, ".openbsd.randomdata", partNo))2476      addHdr(PT_OPENBSD_RANDOMIZE, cmd->getPhdrFlags())->add(cmd);2477 2478    // PT_OPENBSD_SYSCALLS makes the kernel and dynamic linker register2479    // system call sites.2480    if (OutputSection *cmd = findSection(ctx, ".openbsd.syscalls", partNo))2481      addHdr(PT_OPENBSD_SYSCALLS, cmd->getPhdrFlags())->add(cmd);2482  }2483 2484  if (ctx.arg.zGnustack != GnuStackKind::None) {2485    // PT_GNU_STACK is a special section to tell the loader to make the2486    // pages for the stack non-executable. If you really want an executable2487    // stack, you can pass -z execstack, but that's not recommended for2488    // security reasons.2489    unsigned perm = PF_R | PF_W;2490    if (ctx.arg.zGnustack == GnuStackKind::Exec)2491      perm |= PF_X;2492    addHdr(PT_GNU_STACK, perm)->p_memsz = ctx.arg.zStackSize;2493  }2494 2495  // PT_OPENBSD_NOBTCFI is an OpenBSD-specific header to mark that the2496  // executable is expected to violate branch-target CFI checks.2497  if (ctx.arg.zNoBtCfi)2498    addHdr(PT_OPENBSD_NOBTCFI, PF_X);2499 2500  // PT_OPENBSD_WXNEEDED is a OpenBSD-specific header to mark the executable2501  // is expected to perform W^X violations, such as calling mprotect(2) or2502  // mmap(2) with PROT_WRITE | PROT_EXEC, which is prohibited by default on2503  // OpenBSD.2504  if (ctx.arg.zWxneeded)2505    addHdr(PT_OPENBSD_WXNEEDED, PF_X);2506 2507  if (OutputSection *cmd = findSection(ctx, ".note.gnu.property", partNo))2508    addHdr(PT_GNU_PROPERTY, PF_R)->add(cmd);2509 2510  // Create one PT_NOTE per a group of contiguous SHT_NOTE sections with the2511  // same alignment.2512  PhdrEntry *note = nullptr;2513  for (OutputSection *sec : ctx.outputSections) {2514    if (sec->partition != partNo)2515      continue;2516    if (sec->type == SHT_NOTE && (sec->flags & SHF_ALLOC)) {2517      if (!note || sec->lmaExpr || note->lastSec->addralign != sec->addralign)2518        note = addHdr(PT_NOTE, PF_R);2519      note->add(sec);2520    } else {2521      note = nullptr;2522    }2523  }2524  return ret;2525}2526 2527template <class ELFT>2528void Writer<ELFT>::addPhdrForSection(Partition &part, unsigned shType,2529                                     unsigned pType, unsigned pFlags) {2530  unsigned partNo = part.getNumber(ctx);2531  auto i = llvm::find_if(ctx.outputSections, [=](OutputSection *cmd) {2532    return cmd->partition == partNo && cmd->type == shType;2533  });2534  if (i == ctx.outputSections.end())2535    return;2536 2537  auto entry = std::make_unique<PhdrEntry>(ctx, pType, pFlags);2538  entry->add(*i);2539  part.phdrs.push_back(std::move(entry));2540}2541 2542// Place the first section of each PT_LOAD to a different page (of maxPageSize).2543// This is achieved by assigning an alignment expression to addrExpr of each2544// such section.2545template <class ELFT> void Writer<ELFT>::fixSectionAlignments() {2546  const PhdrEntry *prev;2547  auto pageAlign = [&, &ctx = this->ctx](const PhdrEntry *p) {2548    OutputSection *cmd = p->firstSec;2549    if (!cmd)2550      return;2551    cmd->alignExpr = [align = cmd->addralign]() { return align; };2552    if (!cmd->addrExpr) {2553      // Prefer advancing to align(dot, maxPageSize) + dot%maxPageSize to avoid2554      // padding in the file contents.2555      //2556      // When -z separate-code is used we must not have any overlap in pages2557      // between an executable segment and a non-executable segment. We align to2558      // the next maximum page size boundary on transitions between executable2559      // and non-executable segments.2560      //2561      // SHT_LLVM_PART_EHDR marks the start of a partition. The partition2562      // sections will be extracted to a separate file. Align to the next2563      // maximum page size boundary so that we can find the ELF header at the2564      // start. We cannot benefit from overlapping p_offset ranges with the2565      // previous segment anyway.2566      if (ctx.arg.zSeparate == SeparateSegmentKind::Loadable ||2567          (ctx.arg.zSeparate == SeparateSegmentKind::Code && prev &&2568           (prev->p_flags & PF_X) != (p->p_flags & PF_X)) ||2569          cmd->type == SHT_LLVM_PART_EHDR)2570        cmd->addrExpr = [&ctx = this->ctx] {2571          return alignToPowerOf2(ctx.script->getDot(), ctx.arg.maxPageSize);2572        };2573      // PT_TLS is at the start of the first RW PT_LOAD. If `p` includes PT_TLS,2574      // it must be the RW. Align to p_align(PT_TLS) to make sure2575      // p_vaddr(PT_LOAD)%p_align(PT_LOAD) = 0. Otherwise, if2576      // sh_addralign(.tdata) < sh_addralign(.tbss), we will set p_align(PT_TLS)2577      // to sh_addralign(.tbss), while p_vaddr(PT_TLS)=p_vaddr(PT_LOAD) may not2578      // be congruent to 0 modulo p_align(PT_TLS).2579      //2580      // Technically this is not required, but as of 2019, some dynamic loaders2581      // don't handle p_vaddr%p_align != 0 correctly, e.g. glibc (i386 and2582      // x86-64) doesn't make runtime address congruent to p_vaddr modulo2583      // p_align for dynamic TLS blocks (PR/24606), FreeBSD rtld has the same2584      // bug, musl (TLS Variant 1 architectures) before 1.1.23 handled TLS2585      // blocks correctly. We need to keep the workaround for a while.2586      else if (ctx.tlsPhdr && ctx.tlsPhdr->firstSec == p->firstSec)2587        cmd->addrExpr = [&ctx] {2588          return alignToPowerOf2(ctx.script->getDot(), ctx.arg.maxPageSize) +2589                 alignToPowerOf2(ctx.script->getDot() % ctx.arg.maxPageSize,2590                                 ctx.tlsPhdr->p_align);2591        };2592      else2593        cmd->addrExpr = [&ctx] {2594          return alignToPowerOf2(ctx.script->getDot(), ctx.arg.maxPageSize) +2595                 ctx.script->getDot() % ctx.arg.maxPageSize;2596        };2597    }2598  };2599 2600  for (Partition &part : ctx.partitions) {2601    prev = nullptr;2602    for (auto &p : part.phdrs)2603      if (p->p_type == PT_LOAD && p->firstSec) {2604        pageAlign(p.get());2605        prev = p.get();2606      }2607  }2608}2609 2610// Compute an in-file position for a given section. The file offset must be the2611// same with its virtual address modulo the page size, so that the loader can2612// load executables without any address adjustment.2613static uint64_t computeFileOffset(Ctx &ctx, OutputSection *os, uint64_t off) {2614  // The first section in a PT_LOAD has to have congruent offset and address2615  // modulo the maximum page size.2616  if (os->ptLoad && os->ptLoad->firstSec == os)2617    return alignTo(off, os->ptLoad->p_align, os->addr);2618 2619  // File offsets are not significant for .bss sections other than the first one2620  // in a PT_LOAD/PT_TLS. By convention, we keep section offsets monotonically2621  // increasing rather than setting to zero.2622  if (os->type == SHT_NOBITS && (!ctx.tlsPhdr || ctx.tlsPhdr->firstSec != os))2623    return off;2624 2625  // If the section is not in a PT_LOAD, we just have to align it.2626  if (!os->ptLoad)2627     return alignToPowerOf2(off, os->addralign);2628 2629  // If two sections share the same PT_LOAD the file offset is calculated2630  // using this formula: Off2 = Off1 + (VA2 - VA1).2631  OutputSection *first = os->ptLoad->firstSec;2632  return first->offset + os->addr - first->addr;2633}2634 2635template <class ELFT> void Writer<ELFT>::assignFileOffsetsBinary() {2636  // Compute the minimum LMA of all non-empty non-NOBITS sections as minAddr.2637  auto needsOffset = [](OutputSection &sec) {2638    return sec.type != SHT_NOBITS && (sec.flags & SHF_ALLOC) && sec.size > 0;2639  };2640  uint64_t minAddr = UINT64_MAX;2641  for (OutputSection *sec : ctx.outputSections)2642    if (needsOffset(*sec)) {2643      sec->offset = sec->getLMA();2644      minAddr = std::min(minAddr, sec->offset);2645    }2646 2647  // Sections are laid out at LMA minus minAddr.2648  fileSize = 0;2649  for (OutputSection *sec : ctx.outputSections)2650    if (needsOffset(*sec)) {2651      sec->offset -= minAddr;2652      fileSize = std::max(fileSize, sec->offset + sec->size);2653    }2654}2655 2656static std::string rangeToString(uint64_t addr, uint64_t len) {2657  return "[0x" + utohexstr(addr) + ", 0x" + utohexstr(addr + len - 1) + "]";2658}2659 2660// Assign file offsets to output sections.2661template <class ELFT> void Writer<ELFT>::assignFileOffsets() {2662  ctx.out.programHeaders->offset = ctx.out.elfHeader->size;2663  uint64_t off = ctx.out.elfHeader->size + ctx.out.programHeaders->size;2664 2665  PhdrEntry *lastRX = nullptr;2666  for (Partition &part : ctx.partitions)2667    for (auto &p : part.phdrs)2668      if (p->p_type == PT_LOAD && (p->p_flags & PF_X))2669        lastRX = p.get();2670 2671  // Layout SHF_ALLOC sections before non-SHF_ALLOC sections. A non-SHF_ALLOC2672  // will not occupy file offsets contained by a PT_LOAD.2673  for (OutputSection *sec : ctx.outputSections) {2674    if (!(sec->flags & SHF_ALLOC))2675      continue;2676    off = computeFileOffset(ctx, sec, off);2677    sec->offset = off;2678    if (sec->type != SHT_NOBITS)2679      off += sec->size;2680 2681    // If this is a last section of the last executable segment and that2682    // segment is the last loadable segment, align the offset of the2683    // following section to avoid loading non-segments parts of the file.2684    if (ctx.arg.zSeparate != SeparateSegmentKind::None && lastRX &&2685        lastRX->lastSec == sec)2686      off = alignToPowerOf2(off, ctx.arg.maxPageSize);2687  }2688  for (OutputSection *osec : ctx.outputSections) {2689    if (osec->flags & SHF_ALLOC)2690      continue;2691    osec->offset = alignToPowerOf2(off, osec->addralign);2692    off = osec->offset + osec->size;2693  }2694 2695  sectionHeaderOff = alignToPowerOf2(off, ctx.arg.wordsize);2696  fileSize =2697      sectionHeaderOff + (ctx.outputSections.size() + 1) * sizeof(Elf_Shdr);2698 2699  // Our logic assumes that sections have rising VA within the same segment.2700  // With use of linker scripts it is possible to violate this rule and get file2701  // offset overlaps or overflows. That should never happen with a valid script2702  // which does not move the location counter backwards and usually scripts do2703  // not do that. Unfortunately, there are apps in the wild, for example, Linux2704  // kernel, which control segment distribution explicitly and move the counter2705  // backwards, so we have to allow doing that to support linking them. We2706  // perform non-critical checks for overlaps in checkSectionOverlap(), but here2707  // we want to prevent file size overflows because it would crash the linker.2708  for (OutputSection *sec : ctx.outputSections) {2709    if (sec->type == SHT_NOBITS)2710      continue;2711    if ((sec->offset > fileSize) || (sec->offset + sec->size > fileSize))2712      ErrAlways(ctx) << "unable to place section " << sec->name2713                     << " at file offset "2714                     << rangeToString(sec->offset, sec->size)2715                     << "; check your linker script for overflows";2716  }2717}2718 2719// Finalize the program headers. We call this function after we assign2720// file offsets and VAs to all sections.2721template <class ELFT> void Writer<ELFT>::setPhdrs(Partition &part) {2722  for (std::unique_ptr<PhdrEntry> &p : part.phdrs) {2723    OutputSection *first = p->firstSec;2724    OutputSection *last = p->lastSec;2725 2726    // .ARM.exidx sections may not be within a single .ARM.exidx2727    // output section. We always want to describe just the2728    // SyntheticSection.2729    if (part.armExidx && p->p_type == PT_ARM_EXIDX) {2730      p->p_filesz = part.armExidx->getSize();2731      p->p_memsz = p->p_filesz;2732      p->p_offset = first->offset + part.armExidx->outSecOff;2733      p->p_vaddr = first->addr + part.armExidx->outSecOff;2734      p->p_align = part.armExidx->addralign;2735      if (part.elfHeader)2736        p->p_offset -= part.elfHeader->getParent()->offset;2737 2738      if (!p->hasLMA)2739        p->p_paddr = first->getLMA() + part.armExidx->outSecOff;2740      return;2741    }2742 2743    if (first) {2744      p->p_filesz = last->offset - first->offset;2745      if (last->type != SHT_NOBITS)2746        p->p_filesz += last->size;2747 2748      p->p_memsz = last->addr + last->size - first->addr;2749      p->p_offset = first->offset;2750      p->p_vaddr = first->addr;2751 2752      // File offsets in partitions other than the main partition are relative2753      // to the offset of the ELF headers. Perform that adjustment now.2754      if (part.elfHeader)2755        p->p_offset -= part.elfHeader->getParent()->offset;2756 2757      if (!p->hasLMA)2758        p->p_paddr = first->getLMA();2759    }2760  }2761}2762 2763// A helper struct for checkSectionOverlap.2764namespace {2765struct SectionOffset {2766  OutputSection *sec;2767  uint64_t offset;2768};2769} // namespace2770 2771// Check whether sections overlap for a specific address range (file offsets,2772// load and virtual addresses).2773static void checkOverlap(Ctx &ctx, StringRef name,2774                         std::vector<SectionOffset> &sections,2775                         bool isVirtualAddr) {2776  llvm::sort(sections, [=](const SectionOffset &a, const SectionOffset &b) {2777    return a.offset < b.offset;2778  });2779 2780  // Finding overlap is easy given a vector is sorted by start position.2781  // If an element starts before the end of the previous element, they overlap.2782  for (size_t i = 1, end = sections.size(); i < end; ++i) {2783    SectionOffset a = sections[i - 1];2784    SectionOffset b = sections[i];2785    if (b.offset >= a.offset + a.sec->size)2786      continue;2787 2788    // If both sections are in OVERLAY we allow the overlapping of virtual2789    // addresses, because it is what OVERLAY was designed for.2790    if (isVirtualAddr && a.sec->inOverlay && b.sec->inOverlay)2791      continue;2792 2793    Err(ctx) << "section " << a.sec->name << " " << name2794             << " range overlaps with " << b.sec->name << "\n>>> "2795             << a.sec->name << " range is "2796             << rangeToString(a.offset, a.sec->size) << "\n>>> " << b.sec->name2797             << " range is " << rangeToString(b.offset, b.sec->size);2798  }2799}2800 2801// Check for overlapping sections and address overflows.2802//2803// In this function we check that none of the output sections have overlapping2804// file offsets. For SHF_ALLOC sections we also check that the load address2805// ranges and the virtual address ranges don't overlap2806template <class ELFT> void Writer<ELFT>::checkSections() {2807  // First, check that section's VAs fit in available address space for target.2808  for (OutputSection *os : ctx.outputSections)2809    if ((os->addr + os->size < os->addr) ||2810        (!ELFT::Is64Bits && os->addr + os->size > uint64_t(UINT32_MAX) + 1))2811      Err(ctx) << "section " << os->name << " at 0x"2812               << utohexstr(os->addr, true) << " of size 0x"2813               << utohexstr(os->size, true)2814               << " exceeds available address space";2815 2816  // Check for overlapping file offsets. In this case we need to skip any2817  // section marked as SHT_NOBITS. These sections don't actually occupy space in2818  // the file so Sec->Offset + Sec->Size can overlap with others. If --oformat2819  // binary is specified only add SHF_ALLOC sections are added to the output2820  // file so we skip any non-allocated sections in that case.2821  std::vector<SectionOffset> fileOffs;2822  for (OutputSection *sec : ctx.outputSections)2823    if (sec->size > 0 && sec->type != SHT_NOBITS &&2824        (!ctx.arg.oFormatBinary || (sec->flags & SHF_ALLOC)))2825      fileOffs.push_back({sec, sec->offset});2826  checkOverlap(ctx, "file", fileOffs, false);2827 2828  // When linking with -r there is no need to check for overlapping virtual/load2829  // addresses since those addresses will only be assigned when the final2830  // executable/shared object is created.2831  if (ctx.arg.relocatable)2832    return;2833 2834  // Checking for overlapping virtual and load addresses only needs to take2835  // into account SHF_ALLOC sections since others will not be loaded.2836  // Furthermore, we also need to skip SHF_TLS sections since these will be2837  // mapped to other addresses at runtime and can therefore have overlapping2838  // ranges in the file.2839  std::vector<SectionOffset> vmas;2840  for (OutputSection *sec : ctx.outputSections)2841    if (sec->size > 0 && (sec->flags & SHF_ALLOC) && !(sec->flags & SHF_TLS))2842      vmas.push_back({sec, sec->addr});2843  checkOverlap(ctx, "virtual address", vmas, true);2844 2845  // Finally, check that the load addresses don't overlap. This will usually be2846  // the same as the virtual addresses but can be different when using a linker2847  // script with AT().2848  std::vector<SectionOffset> lmas;2849  for (OutputSection *sec : ctx.outputSections)2850    if (sec->size > 0 && (sec->flags & SHF_ALLOC) && !(sec->flags & SHF_TLS))2851      lmas.push_back({sec, sec->getLMA()});2852  checkOverlap(ctx, "load address", lmas, false);2853}2854 2855// The entry point address is chosen in the following ways.2856//2857// 1. the '-e' entry command-line option;2858// 2. the ENTRY(symbol) command in a linker control script;2859// 3. the value of the symbol _start, if present;2860// 4. the number represented by the entry symbol, if it is a number;2861// 5. the address 0.2862static uint64_t getEntryAddr(Ctx &ctx) {2863  // Case 1, 2 or 32864  if (Symbol *b = ctx.symtab->find(ctx.arg.entry))2865    return b->getVA(ctx);2866 2867  // Case 42868  uint64_t addr;2869  if (to_integer(ctx.arg.entry, addr))2870    return addr;2871 2872  // Case 52873  if (ctx.arg.warnMissingEntry)2874    Warn(ctx) << "cannot find entry symbol " << ctx.arg.entry2875              << "; not setting start address";2876  return 0;2877}2878 2879static uint16_t getELFType(Ctx &ctx) {2880  if (ctx.arg.isPic)2881    return ET_DYN;2882  if (ctx.arg.relocatable)2883    return ET_REL;2884  return ET_EXEC;2885}2886 2887template <class ELFT> void Writer<ELFT>::writeHeader() {2888  writeEhdr<ELFT>(ctx, ctx.bufferStart, *ctx.mainPart);2889  writePhdrs<ELFT>(ctx.bufferStart + sizeof(Elf_Ehdr), *ctx.mainPart);2890 2891  auto *eHdr = reinterpret_cast<Elf_Ehdr *>(ctx.bufferStart);2892  eHdr->e_type = getELFType(ctx);2893  eHdr->e_entry = getEntryAddr(ctx);2894 2895  // If -z nosectionheader is specified, omit the section header table.2896  if (!ctx.in.shStrTab)2897    return;2898  eHdr->e_shoff = sectionHeaderOff;2899 2900  // Write the section header table.2901  //2902  // The ELF header can only store numbers up to SHN_LORESERVE in the e_shnum2903  // and e_shstrndx fields. When the value of one of these fields exceeds2904  // SHN_LORESERVE ELF requires us to put sentinel values in the ELF header and2905  // use fields in the section header at index 0 to store2906  // the value. The sentinel values and fields are:2907  // e_shnum = 0, SHdrs[0].sh_size = number of sections.2908  // e_shstrndx = SHN_XINDEX, SHdrs[0].sh_link = .shstrtab section index.2909  auto *sHdrs = reinterpret_cast<Elf_Shdr *>(ctx.bufferStart + eHdr->e_shoff);2910  size_t num = ctx.outputSections.size() + 1;2911  if (num >= SHN_LORESERVE)2912    sHdrs->sh_size = num;2913  else2914    eHdr->e_shnum = num;2915 2916  uint32_t strTabIndex = ctx.in.shStrTab->getParent()->sectionIndex;2917  if (strTabIndex >= SHN_LORESERVE) {2918    sHdrs->sh_link = strTabIndex;2919    eHdr->e_shstrndx = SHN_XINDEX;2920  } else {2921    eHdr->e_shstrndx = strTabIndex;2922  }2923 2924  for (OutputSection *sec : ctx.outputSections)2925    sec->writeHeaderTo<ELFT>(++sHdrs);2926}2927 2928// Open a result file.2929template <class ELFT> void Writer<ELFT>::openFile() {2930  uint64_t maxSize = ctx.arg.is64 ? INT64_MAX : UINT32_MAX;2931  if (fileSize != size_t(fileSize) || maxSize < fileSize) {2932    std::string msg;2933    raw_string_ostream s(msg);2934    s << "output file too large: " << fileSize << " bytes\n"2935      << "section sizes:\n";2936    for (OutputSection *os : ctx.outputSections)2937      s << os->name << ' ' << os->size << "\n";2938    ErrAlways(ctx) << msg;2939    return;2940  }2941 2942  unlinkAsync(ctx.arg.outputFile);2943  unsigned flags = 0;2944  if (!ctx.arg.relocatable)2945    flags |= FileOutputBuffer::F_executable;2946  if (ctx.arg.mmapOutputFile)2947    flags |= FileOutputBuffer::F_mmap;2948  Expected<std::unique_ptr<FileOutputBuffer>> bufferOrErr =2949      FileOutputBuffer::create(ctx.arg.outputFile, fileSize, flags);2950 2951  if (!bufferOrErr) {2952    ErrAlways(ctx) << "failed to open " << ctx.arg.outputFile << ": "2953                   << bufferOrErr.takeError();2954    return;2955  }2956  buffer = std::move(*bufferOrErr);2957  ctx.bufferStart = buffer->getBufferStart();2958}2959 2960template <class ELFT> void Writer<ELFT>::writeSectionsBinary() {2961  parallel::TaskGroup tg;2962  for (OutputSection *sec : ctx.outputSections)2963    if (sec->flags & SHF_ALLOC)2964      sec->writeTo<ELFT>(ctx, ctx.bufferStart + sec->offset, tg);2965}2966 2967static void fillTrap(std::array<uint8_t, 4> trapInstr, uint8_t *i,2968                     uint8_t *end) {2969  for (; i + 4 <= end; i += 4)2970    memcpy(i, trapInstr.data(), 4);2971}2972 2973// Fill the last page of executable segments with trap instructions2974// instead of leaving them as zero. Even though it is not required by any2975// standard, it is in general a good thing to do for security reasons.2976//2977// We'll leave other pages in segments as-is because the rest will be2978// overwritten by output sections.2979template <class ELFT> void Writer<ELFT>::writeTrapInstr() {2980  for (Partition &part : ctx.partitions) {2981    // Fill the last page.2982    for (std::unique_ptr<PhdrEntry> &p : part.phdrs)2983      if (p->p_type == PT_LOAD && (p->p_flags & PF_X))2984        fillTrap(2985            ctx.target->trapInstr,2986            ctx.bufferStart + alignDown(p->firstSec->offset + p->p_filesz, 4),2987            ctx.bufferStart + alignToPowerOf2(p->firstSec->offset + p->p_filesz,2988                                              ctx.arg.maxPageSize));2989 2990    // Round up the file size of the last segment to the page boundary iff it is2991    // an executable segment to ensure that other tools don't accidentally2992    // trim the instruction padding (e.g. when stripping the file).2993    PhdrEntry *last = nullptr;2994    for (std::unique_ptr<PhdrEntry> &p : part.phdrs)2995      if (p->p_type == PT_LOAD)2996        last = p.get();2997 2998    if (last && (last->p_flags & PF_X)) {2999      last->p_filesz = alignToPowerOf2(last->p_filesz, ctx.arg.maxPageSize);3000      // p_memsz might be larger than the aligned p_filesz due to trailing BSS3001      // sections. Don't decrease it.3002      last->p_memsz = std::max(last->p_memsz, last->p_filesz);3003    }3004  }3005}3006 3007// Write section contents to a mmap'ed file.3008template <class ELFT> void Writer<ELFT>::writeSections() {3009  llvm::TimeTraceScope timeScope("Write sections");3010 3011  {3012    // In -r or --emit-relocs mode, write the relocation sections first as in3013    // ELf_Rel targets we might find out that we need to modify the relocated3014    // section while doing it.3015    parallel::TaskGroup tg;3016    for (OutputSection *sec : ctx.outputSections)3017      if (isStaticRelSecType(sec->type))3018        sec->writeTo<ELFT>(ctx, ctx.bufferStart + sec->offset, tg);3019  }3020  {3021    parallel::TaskGroup tg;3022    for (OutputSection *sec : ctx.outputSections)3023      if (!isStaticRelSecType(sec->type))3024        sec->writeTo<ELFT>(ctx, ctx.bufferStart + sec->offset, tg);3025  }3026 3027  // Finally, check that all dynamic relocation addends were written correctly.3028  if (ctx.arg.checkDynamicRelocs && ctx.arg.writeAddends) {3029    for (OutputSection *sec : ctx.outputSections)3030      if (isStaticRelSecType(sec->type))3031        sec->checkDynRelAddends(ctx);3032  }3033}3034 3035// Computes a hash value of Data using a given hash function.3036// In order to utilize multiple cores, we first split data into 1MB3037// chunks, compute a hash for each chunk, and then compute a hash value3038// of the hash values.3039static void3040computeHash(llvm::MutableArrayRef<uint8_t> hashBuf,3041            llvm::ArrayRef<uint8_t> data,3042            std::function<void(uint8_t *dest, ArrayRef<uint8_t> arr)> hashFn) {3043  std::vector<ArrayRef<uint8_t>> chunks = split(data, 1024 * 1024);3044  const size_t hashesSize = chunks.size() * hashBuf.size();3045  std::unique_ptr<uint8_t[]> hashes(new uint8_t[hashesSize]);3046 3047  // Compute hash values.3048  parallelFor(0, chunks.size(), [&](size_t i) {3049    hashFn(hashes.get() + i * hashBuf.size(), chunks[i]);3050  });3051 3052  // Write to the final output buffer.3053  hashFn(hashBuf.data(), ArrayRef(hashes.get(), hashesSize));3054}3055 3056template <class ELFT> void Writer<ELFT>::writeBuildId() {3057  if (!ctx.mainPart->buildId || !ctx.mainPart->buildId->getParent())3058    return;3059 3060  if (ctx.arg.buildId == BuildIdKind::Hexstring) {3061    for (Partition &part : ctx.partitions)3062      part.buildId->writeBuildId(ctx.arg.buildIdVector);3063    return;3064  }3065 3066  // Compute a hash of all sections of the output file.3067  size_t hashSize = ctx.mainPart->buildId->hashSize;3068  std::unique_ptr<uint8_t[]> buildId(new uint8_t[hashSize]);3069  MutableArrayRef<uint8_t> output(buildId.get(), hashSize);3070  llvm::ArrayRef<uint8_t> input{ctx.bufferStart, size_t(fileSize)};3071 3072  // Fedora introduced build ID as "approximation of true uniqueness across all3073  // binaries that might be used by overlapping sets of people". It does not3074  // need some security goals that some hash algorithms strive to provide, e.g.3075  // (second-)preimage and collision resistance. In practice people use 'md5'3076  // and 'sha1' just for different lengths. Implement them with the more3077  // efficient BLAKE3.3078  switch (ctx.arg.buildId) {3079  case BuildIdKind::Fast:3080    computeHash(output, input, [](uint8_t *dest, ArrayRef<uint8_t> arr) {3081      write64le(dest, xxh3_64bits(arr));3082    });3083    break;3084  case BuildIdKind::Md5:3085    computeHash(output, input, [&](uint8_t *dest, ArrayRef<uint8_t> arr) {3086      memcpy(dest, BLAKE3::hash<16>(arr).data(), hashSize);3087    });3088    break;3089  case BuildIdKind::Sha1:3090    computeHash(output, input, [&](uint8_t *dest, ArrayRef<uint8_t> arr) {3091      memcpy(dest, BLAKE3::hash<20>(arr).data(), hashSize);3092    });3093    break;3094  case BuildIdKind::Uuid:3095    if (auto ec = llvm::getRandomBytes(buildId.get(), hashSize))3096      ErrAlways(ctx) << "entropy source failure: " << ec.message();3097    break;3098  default:3099    llvm_unreachable("unknown BuildIdKind");3100  }3101  for (Partition &part : ctx.partitions)3102    part.buildId->writeBuildId(output);3103}3104 3105template void elf::writeResult<ELF32LE>(Ctx &);3106template void elf::writeResult<ELF32BE>(Ctx &);3107template void elf::writeResult<ELF64LE>(Ctx &);3108template void elf::writeResult<ELF64BE>(Ctx &);3109