brintos

brintos / llvm-project-archived public Read only

0
0
Text · 62.1 KiB · ff7ef2d Raw
1585 lines · cpp
1//===- InputSection.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 "InputSection.h"10#include "Config.h"11#include "InputFiles.h"12#include "OutputSections.h"13#include "Relocations.h"14#include "SymbolTable.h"15#include "Symbols.h"16#include "SyntheticSections.h"17#include "Target.h"18#include "lld/Common/DWARF.h"19#include "llvm/Support/Compiler.h"20#include "llvm/Support/Compression.h"21#include "llvm/Support/Endian.h"22#include "llvm/Support/LEB128.h"23#include "llvm/Support/xxhash.h"24#include <algorithm>25#include <optional>26#include <vector>27 28using namespace llvm;29using namespace llvm::ELF;30using namespace llvm::object;31using namespace llvm::support;32using namespace llvm::support::endian;33using namespace llvm::sys;34using namespace lld;35using namespace lld::elf;36 37// Returns a string to construct an error message.38std::string elf::toStr(Ctx &ctx, const InputSectionBase *sec) {39  return (toStr(ctx, sec->file) + ":(" + sec->name + ")").str();40}41 42const ELFSyncStream &elf::operator<<(const ELFSyncStream &s,43                                     const InputSectionBase *sec) {44  return s << toStr(s.ctx, sec);45}46 47template <class ELFT>48static ArrayRef<uint8_t> getSectionContents(ObjFile<ELFT> &file,49                                            const typename ELFT::Shdr &hdr) {50  if (hdr.sh_type == SHT_NOBITS)51    return ArrayRef<uint8_t>(nullptr, hdr.sh_size);52  return check(file.getObj().getSectionContents(hdr));53}54 55InputSectionBase::InputSectionBase(InputFile *file, StringRef name,56                                   uint32_t type, uint64_t flags, uint32_t link,57                                   uint32_t info, uint32_t addralign,58                                   uint32_t entsize, ArrayRef<uint8_t> data,59                                   Kind sectionKind)60    : SectionBase(sectionKind, file, name, type, flags, link, info, addralign,61                  entsize),62      bss(0), decodedCrel(0), keepUnique(0), nopFiller(0),63      content_(data.data()), size(data.size()) {64  // In order to reduce memory allocation, we assume that mergeable65  // sections are smaller than 4 GiB, which is not an unreasonable66  // assumption as of 2017.67  if (sectionKind == SectionBase::Merge && content().size() > UINT32_MAX)68    ErrAlways(getCtx()) << this << ": section too large";69 70  // The ELF spec states that a value of 0 means the section has71  // no alignment constraints.72  uint32_t v = std::max<uint32_t>(addralign, 1);73  if (!isPowerOf2_64(v)) {74    Err(getCtx()) << this << ": sh_addralign is not a power of 2";75    v = 1;76  }77  this->addralign = v;78 79  // If SHF_COMPRESSED is set, parse the header. The legacy .zdebug format is no80  // longer supported.81  if (flags & SHF_COMPRESSED) {82    Ctx &ctx = file->ctx;83    invokeELFT(parseCompressedHeader, ctx);84  }85}86 87// SHF_INFO_LINK and SHF_GROUP are normally resolved and not copied to the88// output section. However, for relocatable linking without89// --force-group-allocation, the SHF_GROUP flag and section groups are retained.90static uint64_t getFlags(Ctx &ctx, uint64_t flags) {91  flags &= ~(uint64_t)SHF_INFO_LINK;92  if (ctx.arg.resolveGroups)93    flags &= ~(uint64_t)SHF_GROUP;94  return flags;95}96 97template <class ELFT>98InputSectionBase::InputSectionBase(ObjFile<ELFT> &file,99                                   const typename ELFT::Shdr &hdr,100                                   StringRef name, Kind sectionKind)101    : InputSectionBase(&file, name, hdr.sh_type,102                       getFlags(file.ctx, hdr.sh_flags), hdr.sh_link,103                       hdr.sh_info, hdr.sh_addralign, hdr.sh_entsize,104                       getSectionContents(file, hdr), sectionKind) {105  // We reject object files having insanely large alignments even though106  // they are allowed by the spec. I think 4GB is a reasonable limitation.107  // We might want to relax this in the future.108  if (hdr.sh_addralign > UINT32_MAX) {109    Err(getCtx()) << &file << ": section sh_addralign is too large";110    addralign = 1;111  }112}113 114size_t InputSectionBase::getSize() const {115  if (auto *s = dyn_cast<SyntheticSection>(this))116    return s->getSize();117  return size - bytesDropped;118}119 120template <class ELFT>121static void decompressAux(Ctx &ctx, const InputSectionBase &sec, uint8_t *out,122                          size_t size) {123  auto *hdr = reinterpret_cast<const typename ELFT::Chdr *>(sec.content_);124  auto compressed = ArrayRef<uint8_t>(sec.content_, sec.compressedSize)125                        .slice(sizeof(typename ELFT::Chdr));126  if (Error e = hdr->ch_type == ELFCOMPRESS_ZLIB127                    ? compression::zlib::decompress(compressed, out, size)128                    : compression::zstd::decompress(compressed, out, size))129    Err(ctx) << &sec << ": decompress failed: " << std::move(e);130}131 132void InputSectionBase::decompress() const {133  Ctx &ctx = getCtx();134  uint8_t *buf = makeThreadLocalN<uint8_t>(size);135  invokeELFT(decompressAux, ctx, *this, buf, size);136  content_ = buf;137  compressed = false;138}139 140template <class ELFT>141RelsOrRelas<ELFT> InputSectionBase::relsOrRelas(bool supportsCrel) const {142  if (relSecIdx == 0)143    return {};144  RelsOrRelas<ELFT> ret;145  auto *f = cast<ObjFile<ELFT>>(file);146  typename ELFT::Shdr shdr = f->template getELFShdrs<ELFT>()[relSecIdx];147  if (shdr.sh_type == SHT_CREL) {148    // Return an iterator if supported by caller.149    if (supportsCrel) {150      ret.crels = Relocs<typename ELFT::Crel>(151          (const uint8_t *)f->mb.getBufferStart() + shdr.sh_offset);152      return ret;153    }154    InputSectionBase *const &relSec = f->getSections()[relSecIdx];155    // Otherwise, allocate a buffer to hold the decoded RELA relocations. When156    // called for the first time, relSec is null (without --emit-relocs) or an157    // InputSection with false decodedCrel.158    if (!relSec || !cast<InputSection>(relSec)->decodedCrel) {159      auto *sec = makeThreadLocal<InputSection>(*f, shdr, name);160      f->cacheDecodedCrel(relSecIdx, sec);161      sec->type = SHT_RELA;162      sec->decodedCrel = true;163 164      RelocsCrel<ELFT::Is64Bits> entries(sec->content_);165      sec->size = entries.size() * sizeof(typename ELFT::Rela);166      auto *relas = makeThreadLocalN<typename ELFT::Rela>(entries.size());167      sec->content_ = reinterpret_cast<uint8_t *>(relas);168      for (auto [i, r] : llvm::enumerate(entries)) {169        relas[i].r_offset = r.r_offset;170        relas[i].setSymbolAndType(r.r_symidx, r.r_type, false);171        relas[i].r_addend = r.r_addend;172      }173    }174    ret.relas = {ArrayRef(175        reinterpret_cast<const typename ELFT::Rela *>(relSec->content_),176        relSec->size / sizeof(typename ELFT::Rela))};177    return ret;178  }179 180  const void *content = f->mb.getBufferStart() + shdr.sh_offset;181  size_t size = shdr.sh_size;182  if (shdr.sh_type == SHT_REL) {183    ret.rels = {ArrayRef(reinterpret_cast<const typename ELFT::Rel *>(content),184                         size / sizeof(typename ELFT::Rel))};185  } else {186    assert(shdr.sh_type == SHT_RELA);187    ret.relas = {188        ArrayRef(reinterpret_cast<const typename ELFT::Rela *>(content),189                 size / sizeof(typename ELFT::Rela))};190  }191  return ret;192}193 194Ctx &SectionBase::getCtx() const { return file->ctx; }195 196uint64_t SectionBase::getOffset(uint64_t offset) const {197  switch (kind()) {198  case Output: {199    auto *os = cast<OutputSection>(this);200    // For output sections we treat offset -1 as the end of the section.201    return offset == uint64_t(-1) ? os->size : offset;202  }203  case Class:204    llvm_unreachable("section classes do not have offsets");205  case Regular:206  case Synthetic:207  case Spill:208    return cast<InputSection>(this)->outSecOff + offset;209  case EHFrame: {210    // Two code paths may reach here. First, clang_rt.crtbegin.o and GCC211    // crtbeginT.o may reference the start of an empty .eh_frame to identify the212    // start of the output .eh_frame. Just return offset.213    //214    // Second, InputSection::copyRelocations on .eh_frame. Some pieces may be215    // discarded due to GC/ICF. We should compute the output section offset.216    const EhInputSection *es = cast<EhInputSection>(this);217    if (!es->content().empty())218      if (InputSection *isec = es->getParent())219        return isec->outSecOff + es->getParentOffset(offset);220    return offset;221  }222  case Merge:223    const MergeInputSection *ms = cast<MergeInputSection>(this);224    if (InputSection *isec = ms->getParent())225      return isec->outSecOff + ms->getParentOffset(offset);226    return ms->getParentOffset(offset);227  }228  llvm_unreachable("invalid section kind");229}230 231uint64_t SectionBase::getVA(uint64_t offset) const {232  const OutputSection *out = getOutputSection();233  return (out ? out->addr : 0) + getOffset(offset);234}235 236OutputSection *SectionBase::getOutputSection() {237  InputSection *sec;238  if (auto *isec = dyn_cast<InputSection>(this))239    sec = isec;240  else if (auto *ms = dyn_cast<MergeInputSection>(this))241    sec = ms->getParent();242  else if (auto *eh = dyn_cast<EhInputSection>(this))243    sec = eh->getParent();244  else245    return cast<OutputSection>(this);246  return sec ? sec->getParent() : nullptr;247}248 249// When a section is compressed, `rawData` consists with a header followed250// by zlib-compressed data. This function parses a header to initialize251// `uncompressedSize` member and remove the header from `rawData`.252template <typename ELFT>253void InputSectionBase::parseCompressedHeader(Ctx &ctx) {254  flags &= ~(uint64_t)SHF_COMPRESSED;255 256  // New-style header257  if (content().size() < sizeof(typename ELFT::Chdr)) {258    ErrAlways(ctx) << this << ": corrupted compressed section";259    return;260  }261 262  auto *hdr = reinterpret_cast<const typename ELFT::Chdr *>(content().data());263  if (hdr->ch_type == ELFCOMPRESS_ZLIB) {264    if (!compression::zlib::isAvailable())265      ErrAlways(ctx) << this266                     << " is compressed with ELFCOMPRESS_ZLIB, but lld is "267                        "not built with zlib support";268  } else if (hdr->ch_type == ELFCOMPRESS_ZSTD) {269    if (!compression::zstd::isAvailable())270      ErrAlways(ctx) << this271                     << " is compressed with ELFCOMPRESS_ZSTD, but lld is "272                        "not built with zstd support";273  } else {274    ErrAlways(ctx) << this << ": unsupported compression type ("275                   << uint32_t(hdr->ch_type) << ")";276    return;277  }278 279  compressed = true;280  compressedSize = size;281  size = hdr->ch_size;282  addralign = std::max<uint32_t>(hdr->ch_addralign, 1);283}284 285InputSection *InputSectionBase::getLinkOrderDep() const {286  assert(flags & SHF_LINK_ORDER);287  if (!link)288    return nullptr;289  return cast<InputSection>(file->getSections()[link]);290}291 292// Find a symbol that encloses a given location.293Defined *InputSectionBase::getEnclosingSymbol(uint64_t offset,294                                              uint8_t type) const {295  if (file->isInternal())296    return nullptr;297  for (Symbol *b : file->getSymbols())298    if (Defined *d = dyn_cast<Defined>(b))299      if (d->section == this && d->value <= offset &&300          offset < d->value + d->size && (type == 0 || type == d->type))301        return d;302  return nullptr;303}304 305// Returns an object file location string. Used to construct an error message.306std::string InputSectionBase::getLocation(uint64_t offset) const {307  std::string secAndOffset =308      (name + "+0x" + Twine::utohexstr(offset) + ")").str();309 310  std::string filename = toStr(getCtx(), file);311  if (Defined *d = getEnclosingFunction(offset))312    return filename + ":(function " + toStr(getCtx(), *d) + ": " + secAndOffset;313 314  return filename + ":(" + secAndOffset;315}316 317static void printFileLine(const ELFSyncStream &s, StringRef path,318                          unsigned line) {319  StringRef filename = path::filename(path);320  s << filename << ':' << line;321  if (filename != path)322    s << " (" << path << ':' << line << ')';323}324 325// Print an error message that looks like this:326//327//   foo.c:42 (/home/alice/possibly/very/long/path/foo.c:42)328const ELFSyncStream &elf::operator<<(const ELFSyncStream &s,329                                     InputSectionBase::SrcMsg &&msg) {330  auto &sec = msg.sec;331  if (sec.file->kind() != InputFile::ObjKind)332    return s;333  auto &file = cast<ELFFileBase>(*sec.file);334 335  // First, look up the DWARF line table.336  ArrayRef<InputSectionBase *> sections = file.getSections();337  auto it = llvm::find(sections, &sec);338  uint64_t sectionIndex = it != sections.end()339                              ? it - sections.begin()340                              : object::SectionedAddress::UndefSection;341  DWARFCache *dwarf = file.getDwarf();342  if (auto info = dwarf->getDILineInfo(msg.offset, sectionIndex))343    printFileLine(s, info->FileName, info->Line);344  else if (auto fileLine = dwarf->getVariableLoc(msg.sym.getName()))345    // If it failed, look up again as a variable.346    printFileLine(s, fileLine->first, fileLine->second);347  else348    // File.sourceFile contains STT_FILE symbol, and that is a last resort.349    s << file.sourceFile;350  return s;351}352 353// Returns a filename string along with an optional section name. This354// function is intended to be used for constructing an error355// message. The returned message looks like this:356//357//   path/to/foo.o:(function bar)358//359// or360//361//   path/to/foo.o:(function bar) in archive path/to/bar.a362const ELFSyncStream &elf::operator<<(const ELFSyncStream &s,363                                     InputSectionBase::ObjMsg &&msg) {364  auto *sec = msg.sec;365  s << sec->file->getName() << ":(";366 367  // Find a symbol that encloses a given location. getObjMsg may be called368  // before ObjFile::initSectionsAndLocalSyms where local symbols are369  // initialized.370  if (Defined *d = sec->getEnclosingSymbol(msg.offset))371    s << d;372  else373    s << sec->name << "+0x" << Twine::utohexstr(msg.offset);374  s << ')';375  if (!sec->file->archiveName.empty())376    s << (" in archive " + sec->file->archiveName).str();377  return s;378}379 380PotentialSpillSection::PotentialSpillSection(const InputSectionBase &source,381                                             InputSectionDescription &isd)382    : InputSection(source.file, source.name, source.type, source.flags,383                   source.addralign, source.addralign, {}, SectionBase::Spill),384      isd(&isd) {}385 386InputSection InputSection::discarded(nullptr, "", 0, 0, 0, 0,387                                     ArrayRef<uint8_t>());388 389InputSection::InputSection(InputFile *f, StringRef name, uint32_t type,390                           uint64_t flags, uint32_t addralign, uint32_t entsize,391                           ArrayRef<uint8_t> data, Kind k)392    : InputSectionBase(f, name, type, flags,393                       /*link=*/0, /*info=*/0, addralign, /*entsize=*/entsize,394                       data, k) {395  assert(f || this == &InputSection::discarded);396}397 398template <class ELFT>399InputSection::InputSection(ObjFile<ELFT> &f, const typename ELFT::Shdr &header,400                           StringRef name)401    : InputSectionBase(f, header, name, InputSectionBase::Regular) {}402 403// Copy SHT_GROUP section contents. Used only for the -r option.404template <class ELFT> void InputSection::copyShtGroup(uint8_t *buf) {405  // ELFT::Word is the 32-bit integral type in the target endianness.406  using u32 = typename ELFT::Word;407  ArrayRef<u32> from = getDataAs<u32>();408  auto *to = reinterpret_cast<u32 *>(buf);409 410  // The first entry is not a section number but a flag.411  *to++ = from[0];412 413  // Adjust section numbers because section numbers in an input object files are414  // different in the output. We also need to handle combined or discarded415  // members.416  ArrayRef<InputSectionBase *> sections = file->getSections();417  DenseSet<uint32_t> seen;418  for (uint32_t idx : from.slice(1)) {419    OutputSection *osec = sections[idx]->getOutputSection();420    if (osec && seen.insert(osec->sectionIndex).second)421      *to++ = osec->sectionIndex;422  }423}424 425InputSectionBase *InputSection::getRelocatedSection() const {426  if (file->isInternal() || !isStaticRelSecType(type))427    return nullptr;428  ArrayRef<InputSectionBase *> sections = file->getSections();429  return sections[info];430}431 432template <class ELFT, class RelTy>433void InputSection::copyRelocations(Ctx &ctx, uint8_t *buf) {434  bool linkerRelax =435      ctx.arg.relax && is_contained({EM_RISCV, EM_LOONGARCH}, ctx.arg.emachine);436  if (!ctx.arg.relocatable && (linkerRelax || ctx.arg.branchToBranch)) {437    // On LoongArch and RISC-V, relaxation might change relocations: copy438    // from internal ones that are updated by relaxation.439    InputSectionBase *sec = getRelocatedSection();440    copyRelocations<ELFT, RelTy>(441        ctx, buf,442        llvm::make_range(sec->relocations.begin(), sec->relocations.end()));443  } else {444    // Convert the raw relocations in the input section into Relocation objects445    // suitable to be used by copyRelocations below.446    struct MapRel {447      Ctx &ctx;448      const ObjFile<ELFT> &file;449      Relocation operator()(const RelTy &rel) const {450        // RelExpr is not used so set to a dummy value.451        return Relocation{R_NONE, rel.getType(ctx.arg.isMips64EL), rel.r_offset,452                          getAddend<ELFT>(rel), &file.getRelocTargetSym(rel)};453      }454    };455 456    using RawRels = ArrayRef<RelTy>;457    using MapRelIter =458        llvm::mapped_iterator<typename RawRels::iterator, MapRel>;459    auto mapRel = MapRel{ctx, *getFile<ELFT>()};460    RawRels rawRels = getDataAs<RelTy>();461    auto rels = llvm::make_range(MapRelIter(rawRels.begin(), mapRel),462                                 MapRelIter(rawRels.end(), mapRel));463    copyRelocations<ELFT, RelTy>(ctx, buf, rels);464  }465}466 467// This is used for -r and --emit-relocs. We can't use memcpy to copy468// relocations because we need to update symbol table offset and section index469// for each relocation. So we copy relocations one by one.470template <class ELFT, class RelTy, class RelIt>471void InputSection::copyRelocations(Ctx &ctx, uint8_t *buf,472                                   llvm::iterator_range<RelIt> rels) {473  const TargetInfo &target = *ctx.target;474  InputSectionBase *sec = getRelocatedSection();475  (void)sec->contentMaybeDecompress(); // uncompress if needed476 477  for (const Relocation &rel : rels) {478    RelType type = rel.type;479    const ObjFile<ELFT> *file = getFile<ELFT>();480    Symbol &sym = *rel.sym;481 482    auto *p = reinterpret_cast<typename ELFT::Rela *>(buf);483    buf += sizeof(RelTy);484 485    if (RelTy::HasAddend)486      p->r_addend = rel.addend;487 488    // Output section VA is zero for -r, so r_offset is an offset within the489    // section, but for --emit-relocs it is a virtual address.490    p->r_offset = sec->getVA(rel.offset);491    p->setSymbolAndType(ctx.in.symTab->getSymbolIndex(sym), type,492                        ctx.arg.isMips64EL);493 494    if (sym.type == STT_SECTION) {495      // We combine multiple section symbols into only one per496      // section. This means we have to update the addend. That is497      // trivial for Elf_Rela, but for Elf_Rel we have to write to the498      // section data. We do that by adding to the Relocation vector.499 500      // .eh_frame is horribly special and can reference discarded sections. To501      // avoid having to parse and recreate .eh_frame, we just replace any502      // relocation in it pointing to discarded sections with R_*_NONE, which503      // hopefully creates a frame that is ignored at runtime. Also, don't warn504      // on .gcc_except_table and debug sections.505      //506      // See the comment in maybeReportUndefined for PPC32 .got2 and PPC64 .toc507      auto *d = dyn_cast<Defined>(&sym);508      if (!d) {509        if (!isDebugSection(*sec) && sec->name != ".eh_frame" &&510            sec->name != ".gcc_except_table" && sec->name != ".got2" &&511            sec->name != ".toc") {512          uint32_t secIdx = cast<Undefined>(sym).discardedSecIdx;513          Elf_Shdr_Impl<ELFT> sec = file->template getELFShdrs<ELFT>()[secIdx];514          Warn(ctx) << "relocation refers to a discarded section: "515                    << CHECK2(file->getObj().getSectionName(sec), file)516                    << "\n>>> referenced by " << getObjMsg(p->r_offset);517        }518        p->setSymbolAndType(0, 0, false);519        continue;520      }521      SectionBase *section = d->section;522      assert(section->isLive());523 524      int64_t addend = rel.addend;525      const uint8_t *bufLoc = sec->content().begin() + rel.offset;526      if (!RelTy::HasAddend)527        addend = target.getImplicitAddend(bufLoc, type);528 529      if (ctx.arg.emachine == EM_MIPS &&530          target.getRelExpr(type, sym, bufLoc) == RE_MIPS_GOTREL) {531        // Some MIPS relocations depend on "gp" value. By default,532        // this value has 0x7ff0 offset from a .got section. But533        // relocatable files produced by a compiler or a linker534        // might redefine this default value and we must use it535        // for a calculation of the relocation result. When we536        // generate EXE or DSO it's trivial. Generating a relocatable537        // output is more difficult case because the linker does538        // not calculate relocations in this mode and loses539        // individual "gp" values used by each input object file.540        // As a workaround we add the "gp" value to the relocation541        // addend and save it back to the file.542        addend += sec->getFile<ELFT>()->mipsGp0;543      }544 545      if (RelTy::HasAddend)546        p->r_addend =547            sym.getVA(ctx, addend) - section->getOutputSection()->addr;548      // For SHF_ALLOC sections relocated by REL, append a relocation to549      // sec->relocations so that relocateAlloc transitively called by550      // writeSections will update the implicit addend. Non-SHF_ALLOC sections551      // utilize relocateNonAlloc to process raw relocations and do not need552      // this sec->relocations change.553      else if (ctx.arg.relocatable && (sec->flags & SHF_ALLOC) &&554               type != target.noneRel)555        sec->addReloc({R_ABS, type, rel.offset, addend, &sym});556    } else if (ctx.arg.emachine == EM_PPC && type == R_PPC_PLTREL24 &&557               p->r_addend >= 0x8000 && sec->file->ppc32Got2) {558      // Similar to R_MIPS_GPREL{16,32}. If the addend of R_PPC_PLTREL24559      // indicates that r30 is relative to the input section .got2560      // (r_addend>=0x8000), after linking, r30 should be relative to the output561      // section .got2 . To compensate for the shift, adjust r_addend by562      // ppc32Got->outSecOff.563      p->r_addend += sec->file->ppc32Got2->outSecOff;564    }565  }566}567 568// The ARM and AArch64 ABI handle pc-relative relocations to undefined weak569// references specially. The general rule is that the value of the symbol in570// this context is the address of the place P. A further special case is that571// branch relocations to an undefined weak reference resolve to the next572// instruction.573static uint32_t getARMUndefinedRelativeWeakVA(RelType type, uint32_t a,574                                              uint32_t p) {575  switch (type) {576  // Unresolved branch relocations to weak references resolve to next577  // instruction, this will be either 2 or 4 bytes on from P.578  case R_ARM_THM_JUMP8:579  case R_ARM_THM_JUMP11:580    return p + 2 + a;581  case R_ARM_CALL:582  case R_ARM_JUMP24:583  case R_ARM_PC24:584  case R_ARM_PLT32:585  case R_ARM_PREL31:586  case R_ARM_THM_JUMP19:587  case R_ARM_THM_JUMP24:588    return p + 4 + a;589  case R_ARM_THM_CALL:590    // We don't want an interworking BLX to ARM591    return p + 5 + a;592  // Unresolved non branch pc-relative relocations593  // R_ARM_TARGET2 which can be resolved relatively is not present as it never594  // targets a weak-reference.595  case R_ARM_MOVW_PREL_NC:596  case R_ARM_MOVT_PREL:597  case R_ARM_REL32:598  case R_ARM_THM_ALU_PREL_11_0:599  case R_ARM_THM_MOVW_PREL_NC:600  case R_ARM_THM_MOVT_PREL:601  case R_ARM_THM_PC12:602    return p + a;603  // p + a is unrepresentable as negative immediates can't be encoded.604  case R_ARM_THM_PC8:605    return p;606  }607  llvm_unreachable("ARM pc-relative relocation expected\n");608}609 610// The comment above getARMUndefinedRelativeWeakVA applies to this function.611static uint64_t getAArch64UndefinedRelativeWeakVA(uint64_t type, uint64_t p) {612  switch (type) {613  // Unresolved branch relocations to weak references resolve to next614  // instruction, this is 4 bytes on from P.615  case R_AARCH64_CALL26:616  case R_AARCH64_CONDBR19:617  case R_AARCH64_JUMP26:618  case R_AARCH64_TSTBR14:619    return p + 4;620  // Unresolved non branch pc-relative relocations621  case R_AARCH64_PREL16:622  case R_AARCH64_PREL32:623  case R_AARCH64_PREL64:624  case R_AARCH64_ADR_PREL_LO21:625  case R_AARCH64_LD_PREL_LO19:626  case R_AARCH64_PLT32:627    return p;628  }629  llvm_unreachable("AArch64 pc-relative relocation expected\n");630}631 632static uint64_t getRISCVUndefinedRelativeWeakVA(uint64_t type, uint64_t p) {633  switch (type) {634  case R_RISCV_BRANCH:635  case R_RISCV_JAL:636  case R_RISCV_CALL:637  case R_RISCV_CALL_PLT:638  case R_RISCV_RVC_BRANCH:639  case R_RISCV_RVC_JUMP:640  case R_RISCV_PLT32:641    return p;642  default:643    return 0;644  }645}646 647// ARM SBREL relocations are of the form S + A - B where B is the static base648// The ARM ABI defines base to be "addressing origin of the output segment649// defining the symbol S". We defined the "addressing origin"/static base to be650// the base of the PT_LOAD segment containing the Sym.651// The procedure call standard only defines a Read Write Position Independent652// RWPI variant so in practice we should expect the static base to be the base653// of the RW segment.654static uint64_t getARMStaticBase(const Symbol &sym) {655  OutputSection *os = sym.getOutputSection();656  if (!os || !os->ptLoad || !os->ptLoad->firstSec) {657    Err(os->ctx) << "SBREL relocation to " << sym.getName()658                 << " without static base";659    return 0;660  }661  return os->ptLoad->firstSec->addr;662}663 664// For RE_RISCV_PC_INDIRECT (R_RISCV_PCREL_LO12_{I,S}), the symbol actually665// points the corresponding R_RISCV_PCREL_HI20 relocation, and the target VA666// is calculated using PCREL_HI20's symbol.667//668// This function returns the R_RISCV_PCREL_HI20 relocation from the669// R_RISCV_PCREL_LO12 relocation.670static Relocation *getRISCVPCRelHi20(Ctx &ctx, const InputSectionBase *loSec,671                                     const Relocation &loReloc) {672  uint64_t addend = loReloc.addend;673  Symbol *sym = loReloc.sym;674 675  const Defined *d = cast<Defined>(sym);676  if (!d->section) {677    Err(ctx) << loSec->getLocation(loReloc.offset)678             << ": R_RISCV_PCREL_LO12 relocation points to an absolute symbol: "679             << sym->getName();680    return nullptr;681  }682  InputSection *hiSec = cast<InputSection>(d->section);683 684  if (hiSec != loSec)685    Err(ctx) << loSec->getLocation(loReloc.offset)686             << ": R_RISCV_PCREL_LO12 relocation points to a symbol '"687             << sym->getName() << "' in a different section '" << hiSec->name688             << "'";689 690  if (addend != 0)691    Warn(ctx) << loSec->getLocation(loReloc.offset)692              << ": non-zero addend in R_RISCV_PCREL_LO12 relocation to "693              << hiSec->getObjMsg(d->value) << " is ignored";694 695  // Relocations are sorted by offset, so we can use std::equal_range to do696  // binary search.697  Relocation hiReloc;698  hiReloc.offset = d->value;699  auto range =700      std::equal_range(hiSec->relocs().begin(), hiSec->relocs().end(), hiReloc,701                       [](const Relocation &lhs, const Relocation &rhs) {702                         return lhs.offset < rhs.offset;703                       });704 705  for (auto it = range.first; it != range.second; ++it)706    if (it->type == R_RISCV_PCREL_HI20 || it->type == R_RISCV_GOT_HI20 ||707        it->type == R_RISCV_TLS_GD_HI20 || it->type == R_RISCV_TLS_GOT_HI20)708      return &*it;709 710  Err(ctx) << loSec->getLocation(loReloc.offset)711           << ": R_RISCV_PCREL_LO12 relocation points to "712           << hiSec->getObjMsg(d->value)713           << " without an associated R_RISCV_PCREL_HI20 relocation";714  return nullptr;715}716 717// A TLS symbol's virtual address is relative to the TLS segment. Add a718// target-specific adjustment to produce a thread-pointer-relative offset.719static int64_t getTlsTpOffset(Ctx &ctx, const Symbol &s) {720  // On targets that support TLSDESC, _TLS_MODULE_BASE_@tpoff = 0.721  if (&s == ctx.sym.tlsModuleBase)722    return 0;723 724  // There are 2 TLS layouts. Among targets we support, x86 uses TLS Variant 2725  // while most others use Variant 1. At run time TP will be aligned to p_align.726 727  // Variant 1. TP will be followed by an optional gap (which is the size of 2728  // pointers on ARM/AArch64, 0 on other targets), followed by alignment729  // padding, then the static TLS blocks. The alignment padding is added so that730  // (TP + gap + padding) is congruent to p_vaddr modulo p_align.731  //732  // Variant 2. Static TLS blocks, followed by alignment padding are placed733  // before TP. The alignment padding is added so that (TP - padding -734  // p_memsz) is congruent to p_vaddr modulo p_align.735  PhdrEntry *tls = ctx.tlsPhdr;736  if (!tls) // Reported an error in getSymVA737    return 0;738  switch (ctx.arg.emachine) {739    // Variant 1.740  case EM_ARM:741  case EM_AARCH64:742    return s.getVA(ctx, 0) + ctx.arg.wordsize * 2 +743           ((tls->p_vaddr - ctx.arg.wordsize * 2) & (tls->p_align - 1));744  case EM_MIPS:745  case EM_PPC:746  case EM_PPC64:747    // Adjusted Variant 1. TP is placed with a displacement of 0x7000, which is748    // to allow a signed 16-bit offset to reach 0x1000 of TCB/thread-library749    // data and 0xf000 of the program's TLS segment.750    return s.getVA(ctx, 0) + (tls->p_vaddr & (tls->p_align - 1)) - 0x7000;751  case EM_LOONGARCH:752  case EM_RISCV:753    // See the comment in handleTlsRelocation. For TLSDESC=>IE,754    // R_RISCV_TLSDESC_{LOAD_LO12,ADD_LO12_I,CALL} also reach here. While755    // `tls` may be null, the return value is ignored.756    if (s.type != STT_TLS)757      return 0;758    return s.getVA(ctx, 0) + (tls->p_vaddr & (tls->p_align - 1));759 760    // Variant 2.761  case EM_HEXAGON:762  case EM_S390:763  case EM_SPARCV9:764  case EM_386:765  case EM_X86_64:766    return s.getVA(ctx, 0) - tls->p_memsz -767           ((-tls->p_vaddr - tls->p_memsz) & (tls->p_align - 1));768  default:769    llvm_unreachable("unhandled ctx.arg.emachine");770  }771}772 773uint64_t InputSectionBase::getRelocTargetVA(Ctx &ctx, const Relocation &r,774                                            uint64_t p) const {775  int64_t a = r.addend;776  switch (r.expr) {777  case R_ABS:778  case R_DTPREL:779  case R_RELAX_TLS_LD_TO_LE_ABS:780  case R_RELAX_GOT_PC_NOPIC:781  case RE_AARCH64_AUTH:782  case RE_RISCV_ADD:783  case RE_RISCV_LEB128:784    return r.sym->getVA(ctx, a);785  case R_ADDEND:786    return a;787  case R_RELAX_HINT:788    return 0;789  case RE_ARM_SBREL:790    return r.sym->getVA(ctx, a) - getARMStaticBase(*r.sym);791  case R_GOT:792  case RE_AARCH64_AUTH_GOT:793  case R_RELAX_TLS_GD_TO_IE_ABS:794    return r.sym->getGotVA(ctx) + a;795  case RE_LOONGARCH_GOT:796    // The LoongArch TLS GD relocs reuse the R_LARCH_GOT_PC_LO12 reloc r.type797    // for their page offsets. The arithmetics are different in the TLS case798    // so we have to duplicate some logic here.799    if (r.sym->hasFlag(NEEDS_TLSGD) && r.type != R_LARCH_TLS_IE_PC_LO12)800      // Like RE_LOONGARCH_TLSGD_PAGE_PC but taking the absolute value.801      return ctx.in.got->getGlobalDynAddr(*r.sym) + a;802    return r.sym->getGotVA(ctx) + a;803  case R_GOTONLY_PC:804    return ctx.in.got->getVA() + a - p;805  case R_GOTPLTONLY_PC:806    return ctx.in.gotPlt->getVA() + a - p;807  case R_GOTREL:808  case RE_PPC64_RELAX_TOC:809    return r.sym->getVA(ctx, a) - ctx.in.got->getVA();810  case R_GOTPLTREL:811    return r.sym->getVA(ctx, a) - ctx.in.gotPlt->getVA();812  case R_GOTPLT:813  case R_RELAX_TLS_GD_TO_IE_GOTPLT:814    return r.sym->getGotVA(ctx) + a - ctx.in.gotPlt->getVA();815  case R_TLSLD_GOT_OFF:816  case R_GOT_OFF:817  case R_RELAX_TLS_GD_TO_IE_GOT_OFF:818    return r.sym->getGotOffset(ctx) + a;819  case RE_AARCH64_GOT_PAGE_PC:820  case RE_AARCH64_AUTH_GOT_PAGE_PC:821  case RE_AARCH64_RELAX_TLS_GD_TO_IE_PAGE_PC:822    return getAArch64Page(r.sym->getGotVA(ctx) + a) - getAArch64Page(p);823  case RE_AARCH64_GOT_PAGE:824    return r.sym->getGotVA(ctx) + a - getAArch64Page(ctx.in.got->getVA());825  case R_GOT_PC:826  case RE_AARCH64_AUTH_GOT_PC:827  case R_RELAX_TLS_GD_TO_IE:828    return r.sym->getGotVA(ctx) + a - p;829  case R_GOTPLT_GOTREL:830    return r.sym->getGotPltVA(ctx) + a - ctx.in.got->getVA();831  case R_GOTPLT_PC:832    return r.sym->getGotPltVA(ctx) + a - p;833  case RE_LOONGARCH_GOT_PAGE_PC:834  case RE_LOONGARCH_RELAX_TLS_GD_TO_IE_PAGE_PC:835    if (r.sym->hasFlag(NEEDS_TLSGD))836      return getLoongArchPageDelta(ctx.in.got->getGlobalDynAddr(*r.sym) + a, p,837                                   r.type);838    return getLoongArchPageDelta(r.sym->getGotVA(ctx) + a, p, r.type);839  case RE_MIPS_GOTREL:840    return r.sym->getVA(ctx, a) - ctx.in.mipsGot->getGp(file);841  case RE_MIPS_GOT_GP:842    return ctx.in.mipsGot->getGp(file) + a;843  case RE_MIPS_GOT_GP_PC: {844    // R_MIPS_LO16 expression has RE_MIPS_GOT_GP_PC r.type iif the target845    // is _gp_disp symbol. In that case we should use the following846    // formula for calculation "AHL + GP - P + 4". For details see p. 4-19 at847    // ftp://www.linux-mips.org/pub/linux/mips/doc/ABI/mipsabi.pdf848    // microMIPS variants of these relocations use slightly different849    // expressions: AHL + GP - P + 3 for %lo() and AHL + GP - P - 1 for %hi()850    // to correctly handle less-significant bit of the microMIPS symbol.851    uint64_t v = ctx.in.mipsGot->getGp(file) + a - p;852    if (r.type == R_MIPS_LO16 || r.type == R_MICROMIPS_LO16)853      v += 4;854    if (r.type == R_MICROMIPS_LO16 || r.type == R_MICROMIPS_HI16)855      v -= 1;856    return v;857  }858  case RE_MIPS_GOT_LOCAL_PAGE:859    // If relocation against MIPS local symbol requires GOT entry, this entry860    // should be initialized by 'page address'. This address is high 16-bits861    // of sum the symbol's value and the addend.862    return ctx.in.mipsGot->getVA() +863           ctx.in.mipsGot->getPageEntryOffset(file, *r.sym, a) -864           ctx.in.mipsGot->getGp(file);865  case RE_MIPS_OSEC_LOCAL_PAGE:866    // This is used by the MIPS multi-GOT implementation. It relocates867    // addresses of 64kb pages that lie inside the output section that sym is868    // a representative for.869    return getMipsPageAddr(r.sym->getOutputSection()->addr) + a;870  case RE_MIPS_GOT_OFF:871  case RE_MIPS_GOT_OFF32:872    // In case of MIPS if a GOT relocation has non-zero addend this addend873    // should be applied to the GOT entry content not to the GOT entry offset.874    // That is why we use separate expression r.type.875    return ctx.in.mipsGot->getVA() +876           ctx.in.mipsGot->getSymEntryOffset(file, *r.sym, a) -877           ctx.in.mipsGot->getGp(file);878  case RE_MIPS_TLSGD:879    return ctx.in.mipsGot->getVA() +880           ctx.in.mipsGot->getGlobalDynOffset(file, *r.sym) -881           ctx.in.mipsGot->getGp(file);882  case RE_MIPS_TLSLD:883    return ctx.in.mipsGot->getVA() + ctx.in.mipsGot->getTlsIndexOffset(file) -884           ctx.in.mipsGot->getGp(file);885  case RE_AARCH64_PAGE_PC: {886    uint64_t val = r.sym->isUndefWeak() ? p + a : r.sym->getVA(ctx, a);887    return getAArch64Page(val) - getAArch64Page(p);888  }889  case RE_RISCV_PC_INDIRECT: {890    if (const Relocation *hiRel = getRISCVPCRelHi20(ctx, this, r))891      return getRelocTargetVA(ctx, *hiRel, r.sym->getVA(ctx));892    return 0;893  }894  case RE_LOONGARCH_PAGE_PC:895    return getLoongArchPageDelta(r.sym->getVA(ctx, a), p, r.type);896  case R_PC:897  case RE_ARM_PCA: {898    uint64_t dest;899    if (r.expr == RE_ARM_PCA)900      // Some PC relative ARM (Thumb) relocations align down the place.901      p = p & 0xfffffffc;902    if (r.sym->isUndefined()) {903      // On ARM and AArch64 a branch to an undefined weak resolves to the next904      // instruction, otherwise the place. On RISC-V, resolve an undefined weak905      // to the same instruction to cause an infinite loop (making the user906      // aware of the issue) while ensuring no overflow.907      // Note: if the symbol is hidden, its binding has been converted to local,908      // so we just check isUndefined() here.909      if (ctx.arg.emachine == EM_ARM)910        dest = getARMUndefinedRelativeWeakVA(r.type, a, p);911      else if (ctx.arg.emachine == EM_AARCH64)912        dest = getAArch64UndefinedRelativeWeakVA(r.type, p) + a;913      else if (ctx.arg.emachine == EM_PPC)914        dest = p;915      else if (ctx.arg.emachine == EM_RISCV)916        dest = getRISCVUndefinedRelativeWeakVA(r.type, p) + a;917      else918        dest = r.sym->getVA(ctx, a);919    } else {920      dest = r.sym->getVA(ctx, a);921    }922    return dest - p;923  }924  case R_PLT:925    return r.sym->getPltVA(ctx) + a;926  case R_PLT_PC:927  case RE_PPC64_CALL_PLT:928    return r.sym->getPltVA(ctx) + a - p;929  case RE_LOONGARCH_PLT_PAGE_PC:930    return getLoongArchPageDelta(r.sym->getPltVA(ctx) + a, p, r.type);931  case R_PLT_GOTPLT:932    return r.sym->getPltVA(ctx) + a - ctx.in.gotPlt->getVA();933  case R_PLT_GOTREL:934    return r.sym->getPltVA(ctx) + a - ctx.in.got->getVA();935  case RE_PPC32_PLTREL:936    // R_PPC_PLTREL24 uses the addend (usually 0 or 0x8000) to indicate r30937    // stores _GLOBAL_OFFSET_TABLE_ or .got2+0x8000. The addend is ignored for938    // target VA computation.939    return r.sym->getPltVA(ctx) - p;940  case RE_PPC64_CALL: {941    uint64_t symVA = r.sym->getVA(ctx, a);942    // If we have an undefined weak symbol, we might get here with a symbol943    // address of zero. That could overflow, but the code must be unreachable,944    // so don't bother doing anything at all.945    if (!symVA)946      return 0;947 948    // PPC64 V2 ABI describes two entry points to a function. The global entry949    // point is used for calls where the caller and callee (may) have different950    // TOC base pointers and r2 needs to be modified to hold the TOC base for951    // the callee. For local calls the caller and callee share the same952    // TOC base and so the TOC pointer initialization code should be skipped by953    // branching to the local entry point.954    return symVA - p +955           getPPC64GlobalEntryToLocalEntryOffset(ctx, r.sym->stOther);956  }957  case RE_PPC64_TOCBASE:958    return getPPC64TocBase(ctx) + a;959  case R_RELAX_GOT_PC:960  case RE_PPC64_RELAX_GOT_PC:961    return r.sym->getVA(ctx, a) - p;962  case R_RELAX_TLS_GD_TO_LE:963  case R_RELAX_TLS_IE_TO_LE:964  case R_RELAX_TLS_LD_TO_LE:965  case R_TPREL:966    // It is not very clear what to return if the symbol is undefined. With967    // --noinhibit-exec, even a non-weak undefined reference may reach here.968    // Just return A, which matches R_ABS, and the behavior of some dynamic969    // loaders.970    if (r.sym->isUndefined())971      return a;972    return getTlsTpOffset(ctx, *r.sym) + a;973  case R_RELAX_TLS_GD_TO_LE_NEG:974  case R_TPREL_NEG:975    if (r.sym->isUndefined())976      return a;977    return -getTlsTpOffset(ctx, *r.sym) + a;978  case R_SIZE:979    return r.sym->getSize() + a;980  case R_TLSDESC:981  case RE_AARCH64_AUTH_TLSDESC:982    return ctx.in.got->getTlsDescAddr(*r.sym) + a;983  case R_TLSDESC_PC:984    return ctx.in.got->getTlsDescAddr(*r.sym) + a - p;985  case R_TLSDESC_GOTPLT:986    return ctx.in.got->getTlsDescAddr(*r.sym) + a - ctx.in.gotPlt->getVA();987  case RE_AARCH64_TLSDESC_PAGE:988  case RE_AARCH64_AUTH_TLSDESC_PAGE:989    return getAArch64Page(ctx.in.got->getTlsDescAddr(*r.sym) + a) -990           getAArch64Page(p);991  case RE_LOONGARCH_TLSDESC_PAGE_PC:992    return getLoongArchPageDelta(ctx.in.got->getTlsDescAddr(*r.sym) + a, p,993                                 r.type);994  case R_TLSGD_GOT:995    return ctx.in.got->getGlobalDynOffset(*r.sym) + a;996  case R_TLSGD_GOTPLT:997    return ctx.in.got->getGlobalDynAddr(*r.sym) + a - ctx.in.gotPlt->getVA();998  case R_TLSGD_PC:999    return ctx.in.got->getGlobalDynAddr(*r.sym) + a - p;1000  case RE_LOONGARCH_TLSGD_PAGE_PC:1001    return getLoongArchPageDelta(ctx.in.got->getGlobalDynAddr(*r.sym) + a, p,1002                                 r.type);1003  case R_TLSLD_GOTPLT:1004    return ctx.in.got->getVA() + ctx.in.got->getTlsIndexOff() + a -1005           ctx.in.gotPlt->getVA();1006  case R_TLSLD_GOT:1007    return ctx.in.got->getTlsIndexOff() + a;1008  case R_TLSLD_PC:1009    return ctx.in.got->getTlsIndexVA() + a - p;1010  default:1011    llvm_unreachable("invalid expression");1012  }1013}1014 1015// This function applies relocations to sections without SHF_ALLOC bit.1016// Such sections are never mapped to memory at runtime. Debug sections are1017// an example. Relocations in non-alloc sections are much easier to1018// handle than in allocated sections because it will never need complex1019// treatment such as GOT or PLT (because at runtime no one refers them).1020// So, we handle relocations for non-alloc sections directly in this1021// function as a performance optimization.1022template <class ELFT, class RelTy>1023void InputSection::relocateNonAlloc(Ctx &ctx, uint8_t *buf,1024                                    Relocs<RelTy> rels) {1025  const unsigned bits = sizeof(typename ELFT::uint) * 8;1026  const TargetInfo &target = *ctx.target;1027  const auto emachine = ctx.arg.emachine;1028  const bool isDebug = isDebugSection(*this);1029  const bool isDebugLine = isDebug && name == ".debug_line";1030  std::optional<uint64_t> tombstone;1031  if (isDebug) {1032    if (name == ".debug_loc" || name == ".debug_ranges")1033      tombstone = 1;1034    else if (name == ".debug_names")1035      tombstone = UINT64_MAX; // tombstone value1036    else1037      tombstone = 0;1038  }1039  for (const auto &patAndValue : llvm::reverse(ctx.arg.deadRelocInNonAlloc))1040    if (patAndValue.first.match(this->name)) {1041      tombstone = patAndValue.second;1042      break;1043    }1044 1045  const InputFile *f = this->file;1046  for (auto it = rels.begin(), end = rels.end(); it != end; ++it) {1047    const RelTy &rel = *it;1048    const RelType type = rel.getType(ctx.arg.isMips64EL);1049    const uint64_t offset = rel.r_offset;1050    uint8_t *bufLoc = buf + offset;1051    int64_t addend = getAddend<ELFT>(rel);1052    if (!RelTy::HasAddend)1053      addend += target.getImplicitAddend(bufLoc, type);1054 1055    Symbol &sym = f->getRelocTargetSym(rel);1056    RelExpr expr = target.getRelExpr(type, sym, bufLoc);1057    if (expr == R_NONE)1058      continue;1059    auto *ds = dyn_cast<Defined>(&sym);1060 1061    if (emachine == EM_RISCV && type == R_RISCV_SET_ULEB128) {1062      if (++it != end &&1063          it->getType(/*isMips64EL=*/false) == R_RISCV_SUB_ULEB128 &&1064          it->r_offset == offset) {1065        uint64_t val;1066        if (!ds && tombstone) {1067          val = *tombstone;1068        } else {1069          val = sym.getVA(ctx, addend) -1070                (f->getRelocTargetSym(*it).getVA(ctx) + getAddend<ELFT>(*it));1071        }1072        if (overwriteULEB128(bufLoc, val) >= 0x80)1073          Err(ctx) << getLocation(offset) << ": ULEB128 value " << val1074                   << " exceeds available space; references '" << &sym << "'";1075        continue;1076      }1077      Err(ctx) << getLocation(offset)1078               << ": R_RISCV_SET_ULEB128 not paired with R_RISCV_SUB_SET128";1079      return;1080    }1081 1082    if (tombstone && (expr == R_ABS || expr == R_DTPREL)) {1083      // Resolve relocations in .debug_* referencing (discarded symbols or ICF1084      // folded section symbols) to a tombstone value. Resolving to addend is1085      // unsatisfactory because the result address range may collide with a1086      // valid range of low address, or leave multiple CUs claiming ownership of1087      // the same range of code, which may confuse consumers.1088      //1089      // To address the problems, we use -1 as a tombstone value for most1090      // .debug_* sections. We have to ignore the addend because we don't want1091      // to resolve an address attribute (which may have a non-zero addend) to1092      // -1+addend (wrap around to a low address).1093      //1094      // R_DTPREL type relocations represent an offset into the dynamic thread1095      // vector. The computed value is st_value plus a non-negative offset.1096      // Negative values are invalid, so -1 can be used as the tombstone value.1097      //1098      // If the referenced symbol is relative to a discarded section (due to1099      // --gc-sections, COMDAT, etc), it has been converted to a Undefined.1100      // `ds->folded` catches the ICF folded case. However, resolving a1101      // relocation in .debug_line to -1 would stop debugger users from setting1102      // breakpoints on the folded-in function, so exclude .debug_line.1103      //1104      // For pre-DWARF-v5 .debug_loc and .debug_ranges, -1 is a reserved value1105      // (base address selection entry), use 1 (which is used by GNU ld for1106      // .debug_ranges).1107      //1108      // TODO To reduce disruption, we use 0 instead of -1 as the tombstone1109      // value. Enable -1 in a future release.1110      if (!ds || (ds->folded && !isDebugLine)) {1111        // If -z dead-reloc-in-nonalloc= is specified, respect it.1112        uint64_t value = SignExtend64<bits>(*tombstone);1113        // For a 32-bit local TU reference in .debug_names, X86_64::relocate1114        // requires that the unsigned value for R_X86_64_32 is truncated to1115        // 32-bit. Other 64-bit targets's don't discern signed/unsigned 32-bit1116        // absolute relocations and do not need this change.1117        if (emachine == EM_X86_64 && type == R_X86_64_32)1118          value = static_cast<uint32_t>(value);1119        target.relocateNoSym(bufLoc, type, value);1120        continue;1121      }1122    }1123 1124    // For a relocatable link, content relocated by relocation types with an1125    // explicit addend, such as RELA, remain unchanged and we can stop here.1126    // While content relocated by relocation types with an implicit addend, such1127    // as REL, needs the implicit addend updated.1128    if (ctx.arg.relocatable && (RelTy::HasAddend || sym.type != STT_SECTION))1129      continue;1130 1131    // R_ABS/R_DTPREL and some other relocations can be used from non-SHF_ALLOC1132    // sections.1133    if (LLVM_LIKELY(expr == R_ABS) || expr == R_DTPREL || expr == R_GOTPLTREL ||1134        expr == RE_RISCV_ADD || expr == RE_ARM_SBREL) {1135      target.relocateNoSym(bufLoc, type,1136                           SignExtend64<bits>(sym.getVA(ctx, addend)));1137      continue;1138    }1139 1140    if (expr == R_SIZE) {1141      target.relocateNoSym(bufLoc, type,1142                           SignExtend64<bits>(sym.getSize() + addend));1143      continue;1144    }1145 1146    // If the control reaches here, we found a PC-relative relocation in a1147    // non-ALLOC section. Since non-ALLOC section is not loaded into memory1148    // at runtime, the notion of PC-relative doesn't make sense here. So,1149    // this is a usage error. However, GNU linkers historically accept such1150    // relocations without any errors and relocate them as if they were at1151    // address 0. For bug-compatibility, we accept them with warnings. We1152    // know Steel Bank Common Lisp as of 2018 have this bug.1153    //1154    // GCC 8.0 or earlier have a bug that they emit R_386_GOTPC relocations1155    // against _GLOBAL_OFFSET_TABLE_ for .debug_info. The bug has been fixed in1156    // 2017 (https://gcc.gnu.org/bugzilla/show_bug.cgi?id=82630), but we need to1157    // keep this bug-compatible code for a while.1158    bool isErr = expr != R_PC && !(emachine == EM_386 && type == R_386_GOTPC);1159    {1160      ELFSyncStream diag(ctx, isErr && !ctx.arg.noinhibitExec1161                                  ? DiagLevel::Err1162                                  : DiagLevel::Warn);1163      diag << getLocation(offset) << ": has non-ABS relocation " << type1164           << " against symbol '" << &sym << "'";1165    }1166    if (!isErr)1167      target.relocateNoSym(1168          bufLoc, type,1169          SignExtend64<bits>(sym.getVA(ctx, addend - offset - outSecOff)));1170  }1171}1172 1173template <class ELFT>1174void InputSection::relocate(Ctx &ctx, uint8_t *buf, uint8_t *bufEnd) {1175  if ((flags & SHF_EXECINSTR) && LLVM_UNLIKELY(getFile<ELFT>()->splitStack))1176    adjustSplitStackFunctionPrologues<ELFT>(ctx, buf, bufEnd);1177 1178  if (flags & SHF_ALLOC) {1179    ctx.target->relocateAlloc(*this, buf);1180    return;1181  }1182 1183  auto *sec = cast<InputSection>(this);1184  // For a relocatable link, also call relocateNonAlloc() to rewrite applicable1185  // locations with tombstone values.1186  invokeOnRelocs(*sec, sec->relocateNonAlloc<ELFT>, ctx, buf);1187}1188 1189// For each function-defining prologue, find any calls to __morestack,1190// and replace them with calls to __morestack_non_split.1191static void switchMorestackCallsToMorestackNonSplit(1192    Ctx &ctx, DenseSet<Defined *> &prologues,1193    SmallVector<Relocation *, 0> &morestackCalls) {1194 1195  // If the target adjusted a function's prologue, all calls to1196  // __morestack inside that function should be switched to1197  // __morestack_non_split.1198  Symbol *moreStackNonSplit = ctx.symtab->find("__morestack_non_split");1199  if (!moreStackNonSplit) {1200    ErrAlways(ctx) << "mixing split-stack objects requires a definition of "1201                      "__morestack_non_split";1202    return;1203  }1204 1205  // Sort both collections to compare addresses efficiently.1206  llvm::sort(morestackCalls, [](const Relocation *l, const Relocation *r) {1207    return l->offset < r->offset;1208  });1209  std::vector<Defined *> functions(prologues.begin(), prologues.end());1210  llvm::sort(functions, [](const Defined *l, const Defined *r) {1211    return l->value < r->value;1212  });1213 1214  auto it = morestackCalls.begin();1215  for (Defined *f : functions) {1216    // Find the first call to __morestack within the function.1217    while (it != morestackCalls.end() && (*it)->offset < f->value)1218      ++it;1219    // Adjust all calls inside the function.1220    while (it != morestackCalls.end() && (*it)->offset < f->value + f->size) {1221      (*it)->sym = moreStackNonSplit;1222      ++it;1223    }1224  }1225}1226 1227static bool enclosingPrologueAttempted(uint64_t offset,1228                                       const DenseSet<Defined *> &prologues) {1229  for (Defined *f : prologues)1230    if (f->value <= offset && offset < f->value + f->size)1231      return true;1232  return false;1233}1234 1235// If a function compiled for split stack calls a function not1236// compiled for split stack, then the caller needs its prologue1237// adjusted to ensure that the called function will have enough stack1238// available. Find those functions, and adjust their prologues.1239template <class ELFT>1240void InputSectionBase::adjustSplitStackFunctionPrologues(Ctx &ctx, uint8_t *buf,1241                                                         uint8_t *end) {1242  DenseSet<Defined *> prologues;1243  SmallVector<Relocation *, 0> morestackCalls;1244 1245  for (Relocation &rel : relocs()) {1246    // Ignore calls into the split-stack api.1247    if (rel.sym->getName().starts_with("__morestack")) {1248      if (rel.sym->getName() == "__morestack")1249        morestackCalls.push_back(&rel);1250      continue;1251    }1252 1253    // A relocation to non-function isn't relevant. Sometimes1254    // __morestack is not marked as a function, so this check comes1255    // after the name check.1256    if (rel.sym->type != STT_FUNC)1257      continue;1258 1259    // If the callee's-file was compiled with split stack, nothing to do.  In1260    // this context, a "Defined" symbol is one "defined by the binary currently1261    // being produced". So an "undefined" symbol might be provided by a shared1262    // library. It is not possible to tell how such symbols were compiled, so be1263    // conservative.1264    if (Defined *d = dyn_cast<Defined>(rel.sym))1265      if (InputSection *isec = cast_or_null<InputSection>(d->section))1266        if (!isec || !isec->getFile<ELFT>() || isec->getFile<ELFT>()->splitStack)1267          continue;1268 1269    if (enclosingPrologueAttempted(rel.offset, prologues))1270      continue;1271 1272    if (Defined *f = getEnclosingFunction(rel.offset)) {1273      prologues.insert(f);1274      if (ctx.target->adjustPrologueForCrossSplitStack(buf + f->value, end,1275                                                       f->stOther))1276        continue;1277      if (!getFile<ELFT>()->someNoSplitStack)1278        Err(ctx)1279            << this << ": " << f->getName() << " (with -fsplit-stack) calls "1280            << rel.sym->getName()1281            << " (without -fsplit-stack), but couldn't adjust its prologue";1282    }1283  }1284 1285  if (ctx.target->needsMoreStackNonSplit)1286    switchMorestackCallsToMorestackNonSplit(ctx, prologues, morestackCalls);1287}1288 1289template <class ELFT> void InputSection::writeTo(Ctx &ctx, uint8_t *buf) {1290  if (LLVM_UNLIKELY(type == SHT_NOBITS))1291    return;1292  // If -r or --emit-relocs is given, then an InputSection1293  // may be a relocation section.1294  if (LLVM_UNLIKELY(type == SHT_RELA)) {1295    copyRelocations<ELFT, typename ELFT::Rela>(ctx, buf);1296    return;1297  }1298  if (LLVM_UNLIKELY(type == SHT_REL)) {1299    copyRelocations<ELFT, typename ELFT::Rel>(ctx, buf);1300    return;1301  }1302 1303  // If -r is given, we may have a SHT_GROUP section.1304  if (LLVM_UNLIKELY(type == SHT_GROUP)) {1305    copyShtGroup<ELFT>(buf);1306    return;1307  }1308 1309  // If this is a compressed section, uncompress section contents directly1310  // to the buffer.1311  if (compressed) {1312    auto *hdr = reinterpret_cast<const typename ELFT::Chdr *>(content_);1313    auto compressed = ArrayRef<uint8_t>(content_, compressedSize)1314                          .slice(sizeof(typename ELFT::Chdr));1315    size_t size = this->size;1316    if (Error e = hdr->ch_type == ELFCOMPRESS_ZLIB1317                      ? compression::zlib::decompress(compressed, buf, size)1318                      : compression::zstd::decompress(compressed, buf, size))1319      Err(ctx) << this << ": decompress failed: " << std::move(e);1320    uint8_t *bufEnd = buf + size;1321    relocate<ELFT>(ctx, buf, bufEnd);1322    return;1323  }1324 1325  // Copy section contents from source object file to output file1326  // and then apply relocations.1327  memcpy(buf, content().data(), content().size());1328  relocate<ELFT>(ctx, buf, buf + content().size());1329}1330 1331void InputSection::replace(InputSection *other) {1332  addralign = std::max(addralign, other->addralign);1333 1334  // When a section is replaced with another section that was allocated to1335  // another partition, the replacement section (and its associated sections)1336  // need to be placed in the main partition so that both partitions will be1337  // able to access it.1338  if (partition != other->partition) {1339    partition = 1;1340    for (InputSection *isec : dependentSections)1341      isec->partition = 1;1342  }1343 1344  other->repl = repl;1345  other->markDead();1346}1347 1348template <class ELFT>1349EhInputSection::EhInputSection(ObjFile<ELFT> &f,1350                               const typename ELFT::Shdr &header,1351                               StringRef name)1352    : InputSectionBase(f, header, name, InputSectionBase::EHFrame) {}1353 1354SyntheticSection *EhInputSection::getParent() const {1355  return cast_or_null<SyntheticSection>(parent);1356}1357 1358// .eh_frame is a sequence of CIE or FDE records.1359// This function splits an input section into records and returns them.1360// In rare cases (.eh_frame pieces are reordered by a linker script), the1361// relocations may be unordered.1362template <class ELFT> void EhInputSection::split() {1363  const RelsOrRelas<ELFT> elfRels = relsOrRelas<ELFT>();1364  if (elfRels.areRelocsCrel())1365    preprocessRelocs<ELFT>(elfRels.crels);1366  else if (elfRels.areRelocsRel())1367    preprocessRelocs<ELFT>(elfRels.rels);1368  else1369    preprocessRelocs<ELFT>(elfRels.relas);1370 1371  // The loop below expects the relocations to be sorted by offset.1372  auto cmp = [](const Relocation &a, const Relocation &b) {1373    return a.offset < b.offset;1374  };1375  if (!llvm::is_sorted(rels, cmp))1376    llvm::stable_sort(rels, cmp);1377 1378  ArrayRef<uint8_t> d = content();1379  const char *msg = nullptr;1380  unsigned relI = 0;1381  while (!d.empty()) {1382    if (d.size() < 4) {1383      msg = "CIE/FDE too small";1384      break;1385    }1386    uint64_t size = endian::read32<ELFT::Endianness>(d.data());1387    if (size == 0) // ZERO terminator1388      break;1389    uint32_t id = endian::read32<ELFT::Endianness>(d.data() + 4);1390    size += 4;1391    if (LLVM_UNLIKELY(size > d.size())) {1392      // If it is 0xFFFFFFFF, the next 8 bytes contain the size instead,1393      // but we do not support that format yet.1394      msg = size == UINT32_MAX + uint64_t(4)1395                ? "CIE/FDE too large"1396                : "CIE/FDE ends past the end of the section";1397      break;1398    }1399 1400    // Find the first relocation that points to [off,off+size). Relocations1401    // have been sorted by r_offset.1402    const uint64_t off = d.data() - content().data();1403    while (relI != rels.size() && rels[relI].offset < off)1404      ++relI;1405    unsigned firstRel = -1;1406    if (relI != rels.size() && rels[relI].offset < off + size)1407      firstRel = relI;1408    (id == 0 ? cies : fdes).emplace_back(off, this, size, firstRel);1409    d = d.slice(size);1410  }1411  if (msg)1412    Err(file->ctx) << "corrupted .eh_frame: " << msg << "\n>>> defined in "1413                   << getObjMsg(d.data() - content().data());1414}1415 1416template <class ELFT, class RelTy>1417void EhInputSection::preprocessRelocs(Relocs<RelTy> elfRels) {1418  Ctx &ctx = file->ctx;1419  rels.reserve(elfRels.size());1420  for (auto rel : elfRels) {1421    uint64_t offset = rel.r_offset;1422    Symbol &sym = file->getSymbol(rel.getSymbol(ctx.arg.isMips64EL));1423    RelType type = rel.getType(ctx.arg.isMips64EL);1424    RelExpr expr = ctx.target->getRelExpr(type, sym, content().data() + offset);1425    int64_t addend =1426        RelTy::HasAddend1427            ? getAddend<ELFT>(rel)1428            : ctx.target->getImplicitAddend(content().data() + offset, type);1429    rels.push_back({expr, type, offset, addend, &sym});1430  }1431}1432 1433// Return the offset in an output section for a given input offset.1434uint64_t EhInputSection::getParentOffset(uint64_t offset) const {1435  auto it = partition_point(1436      fdes, [=](EhSectionPiece p) { return p.inputOff <= offset; });1437  if (it == fdes.begin() || it[-1].inputOff + it[-1].size <= offset) {1438    it = partition_point(1439        cies, [=](EhSectionPiece p) { return p.inputOff <= offset; });1440    if (it == cies.begin()) // invalid piece1441      return offset;1442  }1443  if (it[-1].outputOff == -1) // invalid piece1444    return offset - it[-1].inputOff;1445  return it[-1].outputOff + (offset - it[-1].inputOff);1446}1447 1448static size_t findNull(StringRef s, size_t entSize) {1449  for (unsigned i = 0, n = s.size(); i != n; i += entSize) {1450    const char *b = s.begin() + i;1451    if (std::all_of(b, b + entSize, [](char c) { return c == 0; }))1452      return i;1453  }1454  llvm_unreachable("");1455}1456 1457// Split SHF_STRINGS section. Such section is a sequence of1458// null-terminated strings.1459void MergeInputSection::splitStrings(StringRef s, size_t entSize) {1460  const bool live = !(flags & SHF_ALLOC) || !getCtx().arg.gcSections;1461  const char *p = s.data(), *end = s.data() + s.size();1462  if (!std::all_of(end - entSize, end, [](char c) { return c == 0; })) {1463    Err(getCtx()) << this << ": string is not null terminated";1464    pieces.emplace_back(entSize, 0, false);1465    return;1466  }1467  if (entSize == 1) {1468    // Optimize the common case.1469    do {1470      size_t size = strlen(p);1471      pieces.emplace_back(p - s.begin(), xxh3_64bits(StringRef(p, size)), live);1472      p += size + 1;1473    } while (p != end);1474  } else {1475    do {1476      size_t size = findNull(StringRef(p, end - p), entSize);1477      pieces.emplace_back(p - s.begin(), xxh3_64bits(StringRef(p, size)), live);1478      p += size + entSize;1479    } while (p != end);1480  }1481}1482 1483// Split non-SHF_STRINGS section. Such section is a sequence of1484// fixed size records.1485void MergeInputSection::splitNonStrings(ArrayRef<uint8_t> data,1486                                        size_t entSize) {1487  size_t size = data.size();1488  assert((size % entSize) == 0);1489  const bool live = !(flags & SHF_ALLOC) || !getCtx().arg.gcSections;1490 1491  pieces.resize_for_overwrite(size / entSize);1492  for (size_t i = 0, j = 0; i != size; i += entSize, j++)1493    pieces[j] = {i, (uint32_t)xxh3_64bits(data.slice(i, entSize)), live};1494}1495 1496template <class ELFT>1497MergeInputSection::MergeInputSection(ObjFile<ELFT> &f,1498                                     const typename ELFT::Shdr &header,1499                                     StringRef name)1500    : InputSectionBase(f, header, name, InputSectionBase::Merge) {}1501 1502MergeInputSection::MergeInputSection(Ctx &ctx, StringRef name, uint32_t type,1503                                     uint64_t flags, uint64_t entsize,1504                                     ArrayRef<uint8_t> data)1505    : InputSectionBase(ctx.internalFile, name, type, flags, /*link=*/0,1506                       /*info=*/0,1507                       /*addralign=*/entsize, entsize, data,1508                       SectionBase::Merge) {}1509 1510// This function is called after we obtain a complete list of input sections1511// that need to be linked. This is responsible to split section contents1512// into small chunks for further processing.1513//1514// Note that this function is called from parallelForEach. This must be1515// thread-safe (i.e. no memory allocation from the pools).1516void MergeInputSection::splitIntoPieces() {1517  assert(pieces.empty());1518 1519  if (flags & SHF_STRINGS)1520    splitStrings(toStringRef(contentMaybeDecompress()), entsize);1521  else1522    splitNonStrings(contentMaybeDecompress(), entsize);1523}1524 1525SectionPiece &MergeInputSection::getSectionPiece(uint64_t offset) {1526  if (content().size() <= offset) {1527    Err(getCtx()) << this << ": offset is outside the section";1528    return pieces[0];1529  }1530  return partition_point(1531      pieces, [=](SectionPiece p) { return p.inputOff <= offset; })[-1];1532}1533 1534// Return the offset in an output section for a given input offset.1535uint64_t MergeInputSection::getParentOffset(uint64_t offset) const {1536  const SectionPiece &piece = getSectionPiece(offset);1537  return piece.outputOff + (offset - piece.inputOff);1538}1539 1540template InputSection::InputSection(ObjFile<ELF32LE> &, const ELF32LE::Shdr &,1541                                    StringRef);1542template InputSection::InputSection(ObjFile<ELF32BE> &, const ELF32BE::Shdr &,1543                                    StringRef);1544template InputSection::InputSection(ObjFile<ELF64LE> &, const ELF64LE::Shdr &,1545                                    StringRef);1546template InputSection::InputSection(ObjFile<ELF64BE> &, const ELF64BE::Shdr &,1547                                    StringRef);1548 1549template void InputSection::writeTo<ELF32LE>(Ctx &, uint8_t *);1550template void InputSection::writeTo<ELF32BE>(Ctx &, uint8_t *);1551template void InputSection::writeTo<ELF64LE>(Ctx &, uint8_t *);1552template void InputSection::writeTo<ELF64BE>(Ctx &, uint8_t *);1553 1554template RelsOrRelas<ELF32LE>1555InputSectionBase::relsOrRelas<ELF32LE>(bool) const;1556template RelsOrRelas<ELF32BE>1557InputSectionBase::relsOrRelas<ELF32BE>(bool) const;1558template RelsOrRelas<ELF64LE>1559InputSectionBase::relsOrRelas<ELF64LE>(bool) const;1560template RelsOrRelas<ELF64BE>1561InputSectionBase::relsOrRelas<ELF64BE>(bool) const;1562 1563template MergeInputSection::MergeInputSection(ObjFile<ELF32LE> &,1564                                              const ELF32LE::Shdr &, StringRef);1565template MergeInputSection::MergeInputSection(ObjFile<ELF32BE> &,1566                                              const ELF32BE::Shdr &, StringRef);1567template MergeInputSection::MergeInputSection(ObjFile<ELF64LE> &,1568                                              const ELF64LE::Shdr &, StringRef);1569template MergeInputSection::MergeInputSection(ObjFile<ELF64BE> &,1570                                              const ELF64BE::Shdr &, StringRef);1571 1572template EhInputSection::EhInputSection(ObjFile<ELF32LE> &,1573                                        const ELF32LE::Shdr &, StringRef);1574template EhInputSection::EhInputSection(ObjFile<ELF32BE> &,1575                                        const ELF32BE::Shdr &, StringRef);1576template EhInputSection::EhInputSection(ObjFile<ELF64LE> &,1577                                        const ELF64LE::Shdr &, StringRef);1578template EhInputSection::EhInputSection(ObjFile<ELF64BE> &,1579                                        const ELF64BE::Shdr &, StringRef);1580 1581template void EhInputSection::split<ELF32LE>();1582template void EhInputSection::split<ELF32BE>();1583template void EhInputSection::split<ELF64LE>();1584template void EhInputSection::split<ELF64BE>();1585