brintos

brintos / llvm-project-archived public Read only

0
0
Text · 93.2 KiB · ef19a2a Raw
2327 lines · cpp
1//===- Relocations.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// This file implements the core relocation processing logic. It analyzes10// relocations and determines what auxiliary data structures (GOT, PLT, copy11// relocations) need to be created during linking.12//13// The main entry point is scanRelocations<ELFT>(), which calls scanSection()14// to process all relocations within an input section. For each relocation,15// scan() analyzes the type and target, and determines whether a synthetic16// section entry or dynamic relocation is needed.17//18// Note: This file analyzes what needs to be done but doesn't apply the19// actual relocations - that happens later in InputSection::writeTo().20// Instead, it populates Relocation objects in InputSectionBase::relocations21// and creates necessary synthetic sections (GOT, PLT, etc.).22//23// In addition, this file implements the core Thunk creation logic, called24// during finalizeAddressDependentContent().25//26//===----------------------------------------------------------------------===//27 28#include "Relocations.h"29#include "Config.h"30#include "InputFiles.h"31#include "LinkerScript.h"32#include "OutputSections.h"33#include "RelocScan.h"34#include "SymbolTable.h"35#include "Symbols.h"36#include "SyntheticSections.h"37#include "Target.h"38#include "Thunks.h"39#include "lld/Common/ErrorHandler.h"40#include "lld/Common/Memory.h"41#include "llvm/ADT/SmallSet.h"42#include "llvm/BinaryFormat/ELF.h"43#include "llvm/Demangle/Demangle.h"44#include <algorithm>45 46using namespace llvm;47using namespace llvm::ELF;48using namespace llvm::object;49using namespace llvm::support::endian;50using namespace lld;51using namespace lld::elf;52 53static void printDefinedLocation(ELFSyncStream &s, const Symbol &sym) {54  s << "\n>>> defined in " << sym.file;55}56 57// Construct a message in the following format.58//59// >>> defined in /home/alice/src/foo.o60// >>> referenced by bar.c:12 (/home/alice/src/bar.c:12)61// >>>               /home/alice/src/bar.o:(.text+0x1)62void elf::printLocation(ELFSyncStream &s, InputSectionBase &sec,63                        const Symbol &sym, uint64_t off) {64  printDefinedLocation(s, sym);65  s << "\n>>> referenced by ";66  auto tell = s.tell();67  s << sec.getSrcMsg(sym, off);68  if (tell != s.tell())69    s << "\n>>>               ";70  s << sec.getObjMsg(off);71}72 73void elf::reportRangeError(Ctx &ctx, uint8_t *loc, const Relocation &rel,74                           const Twine &v, int64_t min, uint64_t max) {75  ErrorPlace errPlace = getErrorPlace(ctx, loc);76  auto diag = Err(ctx);77  diag << errPlace.loc << "relocation " << rel.type78       << " out of range: " << v.str() << " is not in [" << min << ", " << max79       << ']';80 81  if (rel.sym) {82    if (!rel.sym->isSection())83      diag << "; references '" << rel.sym << '\'';84    else if (auto *d = dyn_cast<Defined>(rel.sym))85      diag << "; references section '" << d->section->name << "'";86 87    if (ctx.arg.emachine == EM_X86_64 && rel.type == R_X86_64_PC32 &&88        rel.sym->getOutputSection() &&89        (rel.sym->getOutputSection()->flags & SHF_X86_64_LARGE)) {90      diag << "; R_X86_64_PC32 should not reference a section marked "91              "SHF_X86_64_LARGE";92    }93  }94  if (!errPlace.srcLoc.empty())95    diag << "\n>>> referenced by " << errPlace.srcLoc;96  if (rel.sym && !rel.sym->isSection())97    printDefinedLocation(diag, *rel.sym);98 99  if (errPlace.isec && errPlace.isec->name.starts_with(".debug"))100    diag << "; consider recompiling with -fdebug-types-section to reduce size "101            "of debug sections";102}103 104void elf::reportRangeError(Ctx &ctx, uint8_t *loc, int64_t v, int n,105                           const Symbol &sym, const Twine &msg) {106  auto diag = Err(ctx);107  diag << getErrorPlace(ctx, loc).loc << msg << " is out of range: " << v108       << " is not in [" << llvm::minIntN(n) << ", " << llvm::maxIntN(n) << "]";109  if (!sym.getName().empty()) {110    diag << "; references '" << &sym << '\'';111    printDefinedLocation(diag, sym);112  }113}114 115// True if non-preemptable symbol always has the same value regardless of where116// the DSO is loaded.117bool elf::isAbsolute(const Symbol &sym) {118  if (sym.isUndefined())119    return true;120  if (const auto *dr = dyn_cast<Defined>(&sym))121    return dr->section == nullptr; // Absolute symbol.122  return false;123}124 125static bool isAbsoluteValue(const Symbol &sym) {126  return isAbsolute(sym) || sym.isTls();127}128 129// Returns true if Expr refers a PLT entry.130static bool needsPlt(RelExpr expr) {131  return oneof<R_PLT, R_PLT_PC, R_PLT_GOTREL, R_PLT_GOTPLT, R_GOTPLT_GOTREL,132               R_GOTPLT_PC, RE_LOONGARCH_PLT_PAGE_PC, RE_PPC32_PLTREL,133               RE_PPC64_CALL_PLT>(expr);134}135 136bool lld::elf::needsGot(RelExpr expr) {137  return oneof<R_GOT, RE_AARCH64_AUTH_GOT, RE_AARCH64_AUTH_GOT_PC, R_GOT_OFF,138               RE_MIPS_GOT_LOCAL_PAGE, RE_MIPS_GOT_OFF, RE_MIPS_GOT_OFF32,139               RE_AARCH64_GOT_PAGE_PC, RE_AARCH64_AUTH_GOT_PAGE_PC,140               RE_AARCH64_AUTH_GOT_PAGE_PC, R_GOT_PC, R_GOTPLT,141               RE_AARCH64_GOT_PAGE, RE_LOONGARCH_GOT, RE_LOONGARCH_GOT_PAGE_PC>(142      expr);143}144 145// True if this expression is of the form Sym - X, where X is a position in the146// file (PC, or GOT for example).147static bool isRelExpr(RelExpr expr) {148  return oneof<R_PC, R_GOTREL, R_GOTPLTREL, RE_ARM_PCA, RE_MIPS_GOTREL,149               RE_PPC64_CALL, RE_PPC64_RELAX_TOC, RE_AARCH64_PAGE_PC,150               R_RELAX_GOT_PC, RE_RISCV_PC_INDIRECT, RE_PPC64_RELAX_GOT_PC,151               RE_LOONGARCH_PAGE_PC>(expr);152}153 154static RelExpr toPlt(RelExpr expr) {155  switch (expr) {156  case RE_LOONGARCH_PAGE_PC:157    return RE_LOONGARCH_PLT_PAGE_PC;158  case RE_PPC64_CALL:159    return RE_PPC64_CALL_PLT;160  case R_PC:161    return R_PLT_PC;162  case R_ABS:163    return R_PLT;164  case R_GOTREL:165    return R_PLT_GOTREL;166  default:167    return expr;168  }169}170 171static RelExpr fromPlt(RelExpr expr) {172  // We decided not to use a plt. Optimize a reference to the plt to a173  // reference to the symbol itself.174  switch (expr) {175  case R_PLT_PC:176  case RE_PPC32_PLTREL:177    return R_PC;178  case RE_LOONGARCH_PLT_PAGE_PC:179    return RE_LOONGARCH_PAGE_PC;180  case RE_PPC64_CALL_PLT:181    return RE_PPC64_CALL;182  case R_PLT:183    return R_ABS;184  case R_PLT_GOTPLT:185    return R_GOTPLTREL;186  case R_PLT_GOTREL:187    return R_GOTREL;188  default:189    return expr;190  }191}192 193// Returns true if a given shared symbol is in a read-only segment in a DSO.194template <class ELFT> static bool isReadOnly(SharedSymbol &ss) {195  using Elf_Phdr = typename ELFT::Phdr;196 197  // Determine if the symbol is read-only by scanning the DSO's program headers.198  const auto &file = cast<SharedFile>(*ss.file);199  for (const Elf_Phdr &phdr :200       check(file.template getObj<ELFT>().program_headers()))201    if ((phdr.p_type == ELF::PT_LOAD || phdr.p_type == ELF::PT_GNU_RELRO) &&202        !(phdr.p_flags & ELF::PF_W) && ss.value >= phdr.p_vaddr &&203        ss.value < phdr.p_vaddr + phdr.p_memsz)204      return true;205  return false;206}207 208// Returns symbols at the same offset as a given symbol, including SS itself.209//210// If two or more symbols are at the same offset, and at least one of211// them are copied by a copy relocation, all of them need to be copied.212// Otherwise, they would refer to different places at runtime.213template <class ELFT>214static SmallPtrSet<SharedSymbol *, 4> getSymbolsAt(Ctx &ctx, SharedSymbol &ss) {215  using Elf_Sym = typename ELFT::Sym;216 217  const auto &file = cast<SharedFile>(*ss.file);218 219  SmallPtrSet<SharedSymbol *, 4> ret;220  for (const Elf_Sym &s : file.template getGlobalELFSyms<ELFT>()) {221    if (s.st_shndx == SHN_UNDEF || s.st_shndx == SHN_ABS ||222        s.getType() == STT_TLS || s.st_value != ss.value)223      continue;224    StringRef name = check(s.getName(file.getStringTable()));225    Symbol *sym = ctx.symtab->find(name);226    if (auto *alias = dyn_cast_or_null<SharedSymbol>(sym))227      ret.insert(alias);228  }229 230  // The loop does not check SHT_GNU_verneed, so ret does not contain231  // non-default version symbols. If ss has a non-default version, ret won't232  // contain ss. Just add ss unconditionally. If a non-default version alias is233  // separately copy relocated, it and ss will have different addresses.234  // Fortunately this case is impractical and fails with GNU ld as well.235  ret.insert(&ss);236  return ret;237}238 239// When a symbol is copy relocated or we create a canonical plt entry, it is240// effectively a defined symbol. In the case of copy relocation the symbol is241// in .bss and in the case of a canonical plt entry it is in .plt. This function242// replaces the existing symbol with a Defined pointing to the appropriate243// location.244static void replaceWithDefined(Ctx &ctx, Symbol &sym, SectionBase &sec,245                               uint64_t value, uint64_t size) {246  Symbol old = sym;247  Defined(ctx, sym.file, StringRef(), sym.binding, sym.stOther, sym.type, value,248          size, &sec)249      .overwrite(sym);250 251  sym.versionId = old.versionId;252  sym.isUsedInRegularObj = true;253  // A copy relocated alias may need a GOT entry.254  sym.flags.store(old.flags.load(std::memory_order_relaxed) & NEEDS_GOT,255                  std::memory_order_relaxed);256}257 258// Reserve space in .bss or .bss.rel.ro for copy relocation.259//260// The copy relocation is pretty much a hack. If you use a copy relocation261// in your program, not only the symbol name but the symbol's size, RW/RO262// bit and alignment become part of the ABI. In addition to that, if the263// symbol has aliases, the aliases become part of the ABI. That's subtle,264// but if you violate that implicit ABI, that can cause very counter-265// intuitive consequences.266//267// So, what is the copy relocation? It's for linking non-position268// independent code to DSOs. In an ideal world, all references to data269// exported by DSOs should go indirectly through GOT. But if object files270// are compiled as non-PIC, all data references are direct. There is no271// way for the linker to transform the code to use GOT, as machine272// instructions are already set in stone in object files. This is where273// the copy relocation takes a role.274//275// A copy relocation instructs the dynamic linker to copy data from a DSO276// to a specified address (which is usually in .bss) at load-time. If the277// static linker (that's us) finds a direct data reference to a DSO278// symbol, it creates a copy relocation, so that the symbol can be279// resolved as if it were in .bss rather than in a DSO.280//281// As you can see in this function, we create a copy relocation for the282// dynamic linker, and the relocation contains not only symbol name but283// various other information about the symbol. So, such attributes become a284// part of the ABI.285//286// Note for application developers: I can give you a piece of advice if287// you are writing a shared library. You probably should export only288// functions from your library. You shouldn't export variables.289//290// As an example what can happen when you export variables without knowing291// the semantics of copy relocations, assume that you have an exported292// variable of type T. It is an ABI-breaking change to add new members at293// end of T even though doing that doesn't change the layout of the294// existing members. That's because the space for the new members are not295// reserved in .bss unless you recompile the main program. That means they296// are likely to overlap with other data that happens to be laid out next297// to the variable in .bss. This kind of issue is sometimes very hard to298// debug. What's a solution? Instead of exporting a variable V from a DSO,299// define an accessor getV().300template <class ELFT> static void addCopyRelSymbol(Ctx &ctx, SharedSymbol &ss) {301  // Copy relocation against zero-sized symbol doesn't make sense.302  uint64_t symSize = ss.getSize();303  if (symSize == 0 || ss.alignment == 0)304    Err(ctx) << "cannot create a copy relocation for symbol " << &ss;305 306  // See if this symbol is in a read-only segment. If so, preserve the symbol's307  // memory protection by reserving space in the .bss.rel.ro section.308  bool isRO = isReadOnly<ELFT>(ss);309  BssSection *sec = make<BssSection>(ctx, isRO ? ".bss.rel.ro" : ".bss",310                                     symSize, ss.alignment);311  OutputSection *osec = (isRO ? ctx.in.bssRelRo : ctx.in.bss)->getParent();312 313  // At this point, sectionBases has been migrated to sections. Append sec to314  // sections.315  if (osec->commands.empty() ||316      !isa<InputSectionDescription>(osec->commands.back()))317    osec->commands.push_back(make<InputSectionDescription>(""));318  auto *isd = cast<InputSectionDescription>(osec->commands.back());319  isd->sections.push_back(sec);320  osec->commitSection(sec);321 322  // Look through the DSO's dynamic symbol table for aliases and create a323  // dynamic symbol for each one. This causes the copy relocation to correctly324  // interpose any aliases.325  for (SharedSymbol *sym : getSymbolsAt<ELFT>(ctx, ss))326    replaceWithDefined(ctx, *sym, *sec, 0, sym->size);327 328  ctx.mainPart->relaDyn->addSymbolReloc(ctx.target->copyRel, *sec, 0, ss);329}330 331// .eh_frame sections are mergeable input sections, so their input332// offsets are not linearly mapped to output section. For each input333// offset, we need to find a section piece containing the offset and334// add the piece's base address to the input offset to compute the335// output offset. That isn't cheap.336//337// This class is to speed up the offset computation. When we process338// relocations, we access offsets in the monotonically increasing339// order. So we can optimize for that access pattern.340//341// For sections other than .eh_frame, this class doesn't do anything.342namespace {343class OffsetGetter {344public:345  OffsetGetter() = default;346  explicit OffsetGetter(EhInputSection &sec) {347    cies = sec.cies;348    fdes = sec.fdes;349    i = cies.begin();350    j = fdes.begin();351  }352 353  // Translates offsets in input sections to offsets in output sections.354  // Given offset must increase monotonically. We assume that Piece is355  // sorted by inputOff.356  uint64_t get(Ctx &ctx, uint64_t off) {357    while (j != fdes.end() && j->inputOff <= off)358      ++j;359    auto it = j;360    if (j == fdes.begin() || j[-1].inputOff + j[-1].size <= off) {361      while (i != cies.end() && i->inputOff <= off)362        ++i;363      if (i == cies.begin() || i[-1].inputOff + i[-1].size <= off) {364        Err(ctx) << ".eh_frame: relocation is not in any piece";365        return 0;366      }367      it = i;368    }369 370    // Offset -1 means that the piece is dead (i.e. garbage collected).371    if (it[-1].outputOff == -1)372      return -1;373    return it[-1].outputOff + (off - it[-1].inputOff);374  }375 376private:377  ArrayRef<EhSectionPiece> cies, fdes;378  ArrayRef<EhSectionPiece>::iterator i, j;379};380} // namespace381 382// Custom error message if Sym is defined in a discarded section.383template <class ELFT>384static void maybeReportDiscarded(Ctx &ctx, ELFSyncStream &msg, Undefined &sym) {385  auto *file = dyn_cast<ObjFile<ELFT>>(sym.file);386  if (!file || !sym.discardedSecIdx)387    return;388  ArrayRef<typename ELFT::Shdr> objSections =389      file->template getELFShdrs<ELFT>();390 391  if (sym.type == ELF::STT_SECTION) {392    msg << "relocation refers to a discarded section: ";393    msg << CHECK2(394        file->getObj().getSectionName(objSections[sym.discardedSecIdx]), file);395  } else {396    msg << "relocation refers to a symbol in a discarded section: " << &sym;397  }398  msg << "\n>>> defined in " << file;399 400  Elf_Shdr_Impl<ELFT> elfSec = objSections[sym.discardedSecIdx - 1];401  if (elfSec.sh_type != SHT_GROUP)402    return;403 404  // If the discarded section is a COMDAT.405  StringRef signature = file->getShtGroupSignature(objSections, elfSec);406  if (const InputFile *prevailing =407          ctx.symtab->comdatGroups.lookup(CachedHashStringRef(signature))) {408    msg << "\n>>> section group signature: " << signature409        << "\n>>> prevailing definition is in " << prevailing;410    if (sym.nonPrevailing) {411      msg << "\n>>> or the symbol in the prevailing group had STB_WEAK "412             "binding and the symbol in a non-prevailing group had STB_GLOBAL "413             "binding. Mixing groups with STB_WEAK and STB_GLOBAL binding "414             "signature is not supported";415    }416  }417}418 419// Check whether the definition name def is a mangled function name that matches420// the reference name ref.421static bool canSuggestExternCForCXX(StringRef ref, StringRef def) {422  llvm::ItaniumPartialDemangler d;423  std::string name = def.str();424  if (d.partialDemangle(name.c_str()))425    return false;426  char *buf = d.getFunctionName(nullptr, nullptr);427  if (!buf)428    return false;429  bool ret = ref == buf;430  free(buf);431  return ret;432}433 434// Suggest an alternative spelling of an "undefined symbol" diagnostic. Returns435// the suggested symbol, which is either in the symbol table, or in the same436// file of sym.437static const Symbol *getAlternativeSpelling(Ctx &ctx, const Undefined &sym,438                                            std::string &pre_hint,439                                            std::string &post_hint) {440  DenseMap<StringRef, const Symbol *> map;441  if (sym.file->kind() == InputFile::ObjKind) {442    auto *file = cast<ELFFileBase>(sym.file);443    // If sym is a symbol defined in a discarded section, maybeReportDiscarded()444    // will give an error. Don't suggest an alternative spelling.445    if (sym.discardedSecIdx != 0 &&446        file->getSections()[sym.discardedSecIdx] == &InputSection::discarded)447      return nullptr;448 449    // Build a map of local defined symbols.450    for (const Symbol *s : sym.file->getSymbols())451      if (s->isLocal() && s->isDefined() && !s->getName().empty())452        map.try_emplace(s->getName(), s);453  }454 455  auto suggest = [&](StringRef newName) -> const Symbol * {456    // If defined locally.457    if (const Symbol *s = map.lookup(newName))458      return s;459 460    // If in the symbol table and not undefined.461    if (const Symbol *s = ctx.symtab->find(newName))462      if (!s->isUndefined())463        return s;464 465    return nullptr;466  };467 468  // This loop enumerates all strings of Levenshtein distance 1 as typo469  // correction candidates and suggests the one that exists as a non-undefined470  // symbol.471  StringRef name = sym.getName();472  for (size_t i = 0, e = name.size(); i != e + 1; ++i) {473    // Insert a character before name[i].474    std::string newName = (name.substr(0, i) + "0" + name.substr(i)).str();475    for (char c = '0'; c <= 'z'; ++c) {476      newName[i] = c;477      if (const Symbol *s = suggest(newName))478        return s;479    }480    if (i == e)481      break;482 483    // Substitute name[i].484    newName = std::string(name);485    for (char c = '0'; c <= 'z'; ++c) {486      newName[i] = c;487      if (const Symbol *s = suggest(newName))488        return s;489    }490 491    // Transpose name[i] and name[i+1]. This is of edit distance 2 but it is492    // common.493    if (i + 1 < e) {494      newName[i] = name[i + 1];495      newName[i + 1] = name[i];496      if (const Symbol *s = suggest(newName))497        return s;498    }499 500    // Delete name[i].501    newName = (name.substr(0, i) + name.substr(i + 1)).str();502    if (const Symbol *s = suggest(newName))503      return s;504  }505 506  // Case mismatch, e.g. Foo vs FOO.507  for (auto &it : map)508    if (name.equals_insensitive(it.first))509      return it.second;510  for (Symbol *sym : ctx.symtab->getSymbols())511    if (!sym->isUndefined() && name.equals_insensitive(sym->getName()))512      return sym;513 514  // The reference may be a mangled name while the definition is not. Suggest a515  // missing extern "C".516  if (name.starts_with("_Z")) {517    std::string buf = name.str();518    llvm::ItaniumPartialDemangler d;519    if (!d.partialDemangle(buf.c_str()))520      if (char *buf = d.getFunctionName(nullptr, nullptr)) {521        const Symbol *s = suggest(buf);522        free(buf);523        if (s) {524          pre_hint = ": extern \"C\" ";525          return s;526        }527      }528  } else {529    const Symbol *s = nullptr;530    for (auto &it : map)531      if (canSuggestExternCForCXX(name, it.first)) {532        s = it.second;533        break;534      }535    if (!s)536      for (Symbol *sym : ctx.symtab->getSymbols())537        if (canSuggestExternCForCXX(name, sym->getName())) {538          s = sym;539          break;540        }541    if (s) {542      pre_hint = " to declare ";543      post_hint = " as extern \"C\"?";544      return s;545    }546  }547 548  return nullptr;549}550 551static void reportUndefinedSymbol(Ctx &ctx, const UndefinedDiag &undef,552                                  bool correctSpelling) {553  Undefined &sym = *undef.sym;554  ELFSyncStream msg(ctx, DiagLevel::None);555 556  auto visibility = [&]() {557    switch (sym.visibility()) {558    case STV_INTERNAL:559      return "internal ";560    case STV_HIDDEN:561      return "hidden ";562    case STV_PROTECTED:563      return "protected ";564    default:565      return "";566    }567  };568 569  switch (ctx.arg.ekind) {570  case ELF32LEKind:571    maybeReportDiscarded<ELF32LE>(ctx, msg, sym);572    break;573  case ELF32BEKind:574    maybeReportDiscarded<ELF32BE>(ctx, msg, sym);575    break;576  case ELF64LEKind:577    maybeReportDiscarded<ELF64LE>(ctx, msg, sym);578    break;579  case ELF64BEKind:580    maybeReportDiscarded<ELF64BE>(ctx, msg, sym);581    break;582  default:583    llvm_unreachable("");584  }585  if (msg.str().empty())586    msg << "undefined " << visibility() << "symbol: " << &sym;587 588  const size_t maxUndefReferences = 3;589  for (UndefinedDiag::Loc l :590       ArrayRef(undef.locs).take_front(maxUndefReferences)) {591    InputSectionBase &sec = *l.sec;592    uint64_t offset = l.offset;593 594    msg << "\n>>> referenced by ";595    // In the absence of line number information, utilize DW_TAG_variable (if596    // present) for the enclosing symbol (e.g. var in `int *a[] = {&undef};`).597    Symbol *enclosing = sec.getEnclosingSymbol(offset);598 599    ELFSyncStream msg1(ctx, DiagLevel::None);600    auto tell = msg.tell();601    msg << sec.getSrcMsg(enclosing ? *enclosing : sym, offset);602    if (tell != msg.tell())603      msg << "\n>>>               ";604    msg << sec.getObjMsg(offset);605  }606 607  if (maxUndefReferences < undef.locs.size())608    msg << "\n>>> referenced " << (undef.locs.size() - maxUndefReferences)609        << " more times";610 611  if (correctSpelling) {612    std::string pre_hint = ": ", post_hint;613    if (const Symbol *corrected =614            getAlternativeSpelling(ctx, sym, pre_hint, post_hint)) {615      msg << "\n>>> did you mean" << pre_hint << corrected << post_hint616          << "\n>>> defined in: " << corrected->file;617    }618  }619 620  if (sym.getName().starts_with("_ZTV"))621    msg << "\n>>> the vtable symbol may be undefined because the class is "622           "missing its key function "623           "(see https://lld.llvm.org/missingkeyfunction)";624  if (ctx.arg.gcSections && ctx.arg.zStartStopGC &&625      sym.getName().starts_with("__start_")) {626    msg << "\n>>> the encapsulation symbol needs to be retained under "627           "--gc-sections properly; consider -z nostart-stop-gc "628           "(see https://lld.llvm.org/ELF/start-stop-gc)";629  }630 631  if (undef.isWarning)632    Warn(ctx) << msg.str();633  else634    ctx.e.error(msg.str(), ErrorTag::SymbolNotFound, {sym.getName()});635}636 637void elf::reportUndefinedSymbols(Ctx &ctx) {638  // Find the first "undefined symbol" diagnostic for each diagnostic, and639  // collect all "referenced from" lines at the first diagnostic.640  DenseMap<Symbol *, UndefinedDiag *> firstRef;641  for (UndefinedDiag &undef : ctx.undefErrs) {642    assert(undef.locs.size() == 1);643    if (UndefinedDiag *canon = firstRef.lookup(undef.sym)) {644      canon->locs.push_back(undef.locs[0]);645      undef.locs.clear();646    } else647      firstRef[undef.sym] = &undef;648  }649 650  // Enable spell corrector for the first 2 diagnostics.651  for (auto [i, undef] : llvm::enumerate(ctx.undefErrs))652    if (!undef.locs.empty())653      reportUndefinedSymbol(ctx, undef, i < 2);654}655 656// Report an undefined symbol if necessary.657// Returns true if the undefined symbol will produce an error message.658bool RelocScan::maybeReportUndefined(Undefined &sym, uint64_t offset) {659  std::lock_guard<std::mutex> lock(ctx.relocMutex);660  // If versioned, issue an error (even if the symbol is weak) because we don't661  // know the defining filename which is required to construct a Verneed entry.662  if (sym.hasVersionSuffix) {663    ctx.undefErrs.push_back({&sym, {{sec, offset}}, false});664    return true;665  }666  if (sym.isWeak())667    return false;668 669  bool canBeExternal = !sym.isLocal() && sym.visibility() == STV_DEFAULT;670  if (ctx.arg.unresolvedSymbols == UnresolvedPolicy::Ignore && canBeExternal)671    return false;672 673  // clang (as of 2019-06-12) / gcc (as of 8.2.1) PPC64 may emit a .rela.toc674  // which references a switch table in a discarded .rodata/.text section. The675  // .toc and the .rela.toc are incorrectly not placed in the comdat. The ELF676  // spec says references from outside the group to a STB_LOCAL symbol are not677  // allowed. Work around the bug.678  //679  // PPC32 .got2 is similar but cannot be fixed. Multiple .got2 is infeasible680  // because .LC0-.LTOC is not representable if the two labels are in different681  // .got2682  if (sym.discardedSecIdx != 0 && (sec->name == ".got2" || sec->name == ".toc"))683    return false;684 685  bool isWarning =686      (ctx.arg.unresolvedSymbols == UnresolvedPolicy::Warn && canBeExternal) ||687      ctx.arg.noinhibitExec;688  ctx.undefErrs.push_back({&sym, {{sec, offset}}, isWarning});689  return !isWarning;690}691 692bool RelocScan::checkTlsLe(uint64_t offset, Symbol &sym, RelType type) {693  if (!ctx.arg.shared)694    return false;695  auto diag = Err(ctx);696  diag << "relocation " << type << " against " << &sym697       << " cannot be used with -shared";698  printLocation(diag, *sec, sym, offset);699  return true;700}701 702template <bool shard = false>703static void addRelativeReloc(Ctx &ctx, InputSectionBase &isec,704                             uint64_t offsetInSec, Symbol &sym, int64_t addend,705                             RelExpr expr, RelType type) {706  Partition &part = isec.getPartition(ctx);707 708  if (sym.isTagged()) {709    part.relaDyn->addRelativeReloc<shard>(ctx.target->relativeRel, isec,710                                          offsetInSec, sym, addend, type, expr);711    // With MTE globals, we always want to derive the address tag by `ldg`-ing712    // the symbol. When we have a RELATIVE relocation though, we no longer have713    // a reference to the symbol. Because of this, when we have an addend that714    // puts the result of the RELATIVE relocation out-of-bounds of the symbol715    // (e.g. the addend is outside of [0, sym.getSize()]), the AArch64 MemtagABI716    // says we should store the offset to the start of the symbol in the target717    // field. This is described in further detail in:718    // https://github.com/ARM-software/abi-aa/blob/main/memtagabielf64/memtagabielf64.rst#841extended-semantics-of-r_aarch64_relative719    if (addend < 0 || static_cast<uint64_t>(addend) >= sym.getSize())720      isec.relocations.push_back({expr, type, offsetInSec, addend, &sym});721    return;722  }723 724  // Add a relative relocation. If relrDyn section is enabled, and the725  // relocation offset is guaranteed to be even, add the relocation to726  // the relrDyn section, otherwise add it to the relaDyn section.727  // relrDyn sections don't support odd offsets. Also, relrDyn sections728  // don't store the addend values, so we must write it to the relocated729  // address.730  if (part.relrDyn && isec.addralign >= 2 && offsetInSec % 2 == 0) {731    isec.addReloc({expr, type, offsetInSec, addend, &sym});732    if (shard)733      part.relrDyn->relocsVec[parallel::getThreadIndex()].push_back(734          {&isec, isec.relocs().size() - 1});735    else736      part.relrDyn->relocs.push_back({&isec, isec.relocs().size() - 1});737    return;738  }739  part.relaDyn->addRelativeReloc<shard>(ctx.target->relativeRel, isec,740                                        offsetInSec, sym, addend, type, expr);741}742 743template <class PltSection, class GotPltSection>744static void addPltEntry(Ctx &ctx, PltSection &plt, GotPltSection &gotPlt,745                        RelocationBaseSection &rel, RelType type, Symbol &sym) {746  plt.addEntry(sym);747  gotPlt.addEntry(sym);748  if (sym.isPreemptible)749    rel.addReloc(750        {type, &gotPlt, sym.getGotPltOffset(ctx), true, sym, 0, R_ADDEND});751  else752    rel.addReloc(753        {type, &gotPlt, sym.getGotPltOffset(ctx), false, sym, 0, R_ABS});754}755 756void elf::addGotEntry(Ctx &ctx, Symbol &sym) {757  ctx.in.got->addEntry(sym);758  uint64_t off = sym.getGotOffset(ctx);759 760  // If preemptible, emit a GLOB_DAT relocation.761  if (sym.isPreemptible) {762    ctx.mainPart->relaDyn->addReloc(763        {ctx.target->gotRel, ctx.in.got.get(), off, true, sym, 0, R_ADDEND});764    return;765  }766 767  // Otherwise, the value is either a link-time constant or the load base768  // plus a constant.769  if (!ctx.arg.isPic || isAbsolute(sym))770    ctx.in.got->addConstant({R_ABS, ctx.target->symbolicRel, off, 0, &sym});771  else772    addRelativeReloc(ctx, *ctx.in.got, off, sym, 0, R_ABS,773                     ctx.target->symbolicRel);774}775 776static void addGotAuthEntry(Ctx &ctx, Symbol &sym) {777  ctx.in.got->addEntry(sym);778  ctx.in.got->addAuthEntry(sym);779  uint64_t off = sym.getGotOffset(ctx);780 781  // If preemptible, emit a GLOB_DAT relocation.782  if (sym.isPreemptible) {783    ctx.mainPart->relaDyn->addReloc({R_AARCH64_AUTH_GLOB_DAT, ctx.in.got.get(),784                                     off, true, sym, 0, R_ADDEND});785    return;786  }787 788  // Signed GOT requires dynamic relocation.789  ctx.in.got->getPartition(ctx).relaDyn->addReloc(790      {R_AARCH64_AUTH_RELATIVE, ctx.in.got.get(), off, false, sym, 0, R_ABS});791}792 793static void addTpOffsetGotEntry(Ctx &ctx, Symbol &sym) {794  ctx.in.got->addEntry(sym);795  uint64_t off = sym.getGotOffset(ctx);796  if (!sym.isPreemptible && !ctx.arg.shared) {797    ctx.in.got->addConstant({R_TPREL, ctx.target->symbolicRel, off, 0, &sym});798    return;799  }800  ctx.mainPart->relaDyn->addAddendOnlyRelocIfNonPreemptible(801      ctx.target->tlsGotRel, *ctx.in.got, off, sym, ctx.target->symbolicRel);802}803 804// Return true if we can define a symbol in the executable that805// contains the value/function of a symbol defined in a shared806// library.807static bool canDefineSymbolInExecutable(Ctx &ctx, Symbol &sym) {808  // If the symbol has default visibility the symbol defined in the809  // executable will preempt it.810  // Note that we want the visibility of the shared symbol itself, not811  // the visibility of the symbol in the output file we are producing.812  if (!sym.dsoProtected)813    return true;814 815  // If we are allowed to break address equality of functions, defining816  // a plt entry will allow the program to call the function in the817  // .so, but the .so and the executable will no agree on the address818  // of the function. Similar logic for objects.819  return ((sym.isFunc() && ctx.arg.ignoreFunctionAddressEquality) ||820          (sym.isObject() && ctx.arg.ignoreDataAddressEquality));821}822 823// Returns true if a given relocation can be computed at link-time.824// This only handles relocation types expected in process().825//826// For instance, we know the offset from a relocation to its target at827// link-time if the relocation is PC-relative and refers a828// non-interposable function in the same executable. This function829// will return true for such relocation.830//831// If this function returns false, that means we need to emit a832// dynamic relocation so that the relocation will be fixed at load-time.833bool RelocScan::isStaticLinkTimeConstant(RelExpr e, RelType type,834                                         const Symbol &sym,835                                         uint64_t relOff) const {836  // These expressions always compute a constant837  if (oneof<838          R_GOTPLT, R_GOT_OFF, R_RELAX_HINT, RE_MIPS_GOT_LOCAL_PAGE,839          RE_MIPS_GOTREL, RE_MIPS_GOT_OFF, RE_MIPS_GOT_OFF32, RE_MIPS_GOT_GP_PC,840          RE_AARCH64_GOT_PAGE_PC, RE_AARCH64_AUTH_GOT_PAGE_PC, R_GOT_PC,841          R_GOTONLY_PC, R_GOTPLTONLY_PC, R_PLT_PC, R_PLT_GOTREL, R_PLT_GOTPLT,842          R_GOTPLT_GOTREL, R_GOTPLT_PC, RE_PPC32_PLTREL, RE_PPC64_CALL_PLT,843          RE_PPC64_RELAX_TOC, RE_RISCV_ADD, RE_AARCH64_GOT_PAGE,844          RE_AARCH64_AUTH_GOT, RE_AARCH64_AUTH_GOT_PC, RE_LOONGARCH_PLT_PAGE_PC,845          RE_LOONGARCH_GOT, RE_LOONGARCH_GOT_PAGE_PC>(e))846    return true;847 848  // These never do, except if the entire file is position dependent or if849  // only the low bits are used.850  if (e == R_GOT || e == R_PLT)851    return ctx.target->usesOnlyLowPageBits(type) || !ctx.arg.isPic;852  // R_AARCH64_AUTH_ABS64 and iRelSymbolicRel require a dynamic relocation.853  if (e == RE_AARCH64_AUTH || type == ctx.target->iRelSymbolicRel)854    return false;855 856  // The behavior of an undefined weak reference is implementation defined.857  // (We treat undefined non-weak the same as undefined weak.) For static858  // -no-pie linking, dynamic relocations are generally avoided (except859  // IRELATIVE). Emitting dynamic relocations for -shared aligns with its -z860  // undefs default. Dynamic -no-pie linking and -pie allow flexibility.861  if (sym.isPreemptible)862    return sym.isUndefined() && !ctx.arg.isPic;863  if (!ctx.arg.isPic)864    return true;865 866  // Constant when referencing a non-preemptible symbol.867  if (e == R_SIZE || e == RE_RISCV_LEB128)868    return true;869 870  // For the target and the relocation, we want to know if they are871  // absolute or relative.872  bool absVal = isAbsoluteValue(sym) && e != RE_PPC64_TOCBASE;873  bool relE = isRelExpr(e);874  if (absVal && !relE)875    return true;876  if (!absVal && relE)877    return true;878  if (!absVal && !relE)879    return ctx.target->usesOnlyLowPageBits(type);880 881  assert(absVal && relE);882 883  // Allow R_PLT_PC (optimized to R_PC here) to a hidden undefined weak symbol884  // in PIC mode. This is a little strange, but it allows us to link function885  // calls to such symbols (e.g. glibc/stdlib/exit.c:__run_exit_handlers).886  // Normally such a call will be guarded with a comparison, which will load a887  // zero from the GOT.888  if (sym.isUndefined())889    return true;890 891  // We set the final symbols values for linker script defined symbols later.892  // They always can be computed as a link time constant.893  if (sym.scriptDefined)894    return true;895 896  auto diag = Err(ctx);897  diag << "relocation " << type << " cannot refer to absolute symbol: " << &sym;898  printLocation(diag, *sec, sym, relOff);899  return true;900}901 902// The reason we have to do this early scan is as follows903// * To mmap the output file, we need to know the size904// * For that, we need to know how many dynamic relocs we will have.905// It might be possible to avoid this by outputting the file with write:906// * Write the allocated output sections, computing addresses.907// * Apply relocations, recording which ones require a dynamic reloc.908// * Write the dynamic relocations.909// * Write the rest of the file.910// This would have some drawbacks. For example, we would only know if .rela.dyn911// is needed after applying relocations. If it is, it will go after rw and rx912// sections. Given that it is ro, we will need an extra PT_LOAD. This913// complicates things for the dynamic linker and means we would have to reserve914// space for the extra PT_LOAD even if we end up not using it.915void RelocScan::process(RelExpr expr, RelType type, uint64_t offset,916                        Symbol &sym, int64_t addend) const {917  // If non-ifunc non-preemptible, change PLT to direct call and optimize GOT918  // indirection.919  const bool isIfunc = sym.isGnuIFunc();920  if (!sym.isPreemptible && (!isIfunc || ctx.arg.zIfuncNoplt)) {921    if (expr != R_GOT_PC) {922      // The 0x8000 bit of r_addend of R_PPC_PLTREL24 is used to choose call923      // stub type. It should be ignored if optimized to R_PC.924      if (ctx.arg.emachine == EM_PPC && expr == RE_PPC32_PLTREL)925        addend &= ~0x8000;926      // R_HEX_GD_PLT_B22_PCREL (call a@GDPLT) is transformed into927      // call __tls_get_addr even if the symbol is non-preemptible.928      if (!(ctx.arg.emachine == EM_HEXAGON &&929            (type == R_HEX_GD_PLT_B22_PCREL ||930             type == R_HEX_GD_PLT_B22_PCREL_X ||931             type == R_HEX_GD_PLT_B32_PCREL_X)))932        expr = fromPlt(expr);933    } else if (!isAbsoluteValue(sym) ||934               (type == R_PPC64_PCREL_OPT && ctx.arg.emachine == EM_PPC64)) {935      expr = ctx.target->adjustGotPcExpr(type, addend,936                                         sec->content().data() + offset);937      // If the target adjusted the expression to R_RELAX_GOT_PC, we may end up938      // needing the GOT if we can't relax everything.939      if (expr == R_RELAX_GOT_PC)940        ctx.in.got->hasGotOffRel.store(true, std::memory_order_relaxed);941    }942  }943 944  // We were asked not to generate PLT entries for ifuncs. Instead, pass the945  // direct relocation on through.946  if (LLVM_UNLIKELY(isIfunc) && ctx.arg.zIfuncNoplt) {947    std::lock_guard<std::mutex> lock(ctx.relocMutex);948    sym.isExported = true;949    ctx.mainPart->relaDyn->addSymbolReloc(type, *sec, offset, sym, addend,950                                          type);951    return;952  }953 954  if (needsGot(expr)) {955    if (ctx.arg.emachine == EM_MIPS) {956      // MIPS ABI has special rules to process GOT entries and doesn't957      // require relocation entries for them. A special case is TLS958      // relocations. In that case dynamic loader applies dynamic959      // relocations to initialize TLS GOT entries.960      // See "Global Offset Table" in Chapter 5 in the following document961      // for detailed description:962      // ftp://www.linux-mips.org/pub/linux/mips/doc/ABI/mipsabi.pdf963      ctx.in.mipsGot->addEntry(*sec->file, sym, addend, expr);964    } else if (!sym.isTls() || ctx.arg.emachine != EM_LOONGARCH) {965      // Many LoongArch TLS relocs reuse the RE_LOONGARCH_GOT type, in which966      // case the NEEDS_GOT flag shouldn't get set.967      if (expr == RE_AARCH64_AUTH_GOT || expr == RE_AARCH64_AUTH_GOT_PAGE_PC ||968          expr == RE_AARCH64_AUTH_GOT_PC)969        sym.setFlags(NEEDS_GOT | NEEDS_GOT_AUTH);970      else971        sym.setFlags(NEEDS_GOT | NEEDS_GOT_NONAUTH);972    }973  } else if (needsPlt(expr)) {974    sym.setFlags(NEEDS_PLT);975  } else if (LLVM_UNLIKELY(isIfunc)) {976    sym.setFlags(HAS_DIRECT_RELOC);977  }978 979  // If the relocation is known to be a link-time constant, we know no dynamic980  // relocation will be created, pass the control to relocateAlloc() or981  // relocateNonAlloc() to resolve it.982  if (isStaticLinkTimeConstant(expr, type, sym, offset)) {983    sec->addReloc({expr, type, offset, addend, &sym});984    return;985  }986 987  // Use a simple -z notext rule that treats all sections except .eh_frame as988  // writable. GNU ld does not produce dynamic relocations in .eh_frame (and our989  // SectionBase::getOffset would incorrectly adjust the offset).990  //991  // For MIPS, we don't implement GNU ld's DW_EH_PE_absptr to DW_EH_PE_pcrel992  // conversion. We still emit a dynamic relocation.993  bool canWrite = (sec->flags & SHF_WRITE) ||994                  !(ctx.arg.zText ||995                    (isa<EhInputSection>(sec) && ctx.arg.emachine != EM_MIPS));996  if (canWrite) {997    RelType rel = ctx.target->getDynRel(type);998    if (oneof<R_GOT, RE_LOONGARCH_GOT>(expr) ||999        (rel == ctx.target->symbolicRel && !sym.isPreemptible)) {1000      addRelativeReloc<true>(ctx, *sec, offset, sym, addend, expr, type);1001      return;1002    }1003    if (rel != 0) {1004      if (ctx.arg.emachine == EM_MIPS && rel == ctx.target->symbolicRel)1005        rel = ctx.target->relativeRel;1006      std::lock_guard<std::mutex> lock(ctx.relocMutex);1007      Partition &part = sec->getPartition(ctx);1008      if (ctx.arg.emachine == EM_AARCH64 && type == R_AARCH64_AUTH_ABS64) {1009        // For a preemptible symbol, we can't use a relative relocation. For an1010        // undefined symbol, we can't compute offset at link-time and use a1011        // relative relocation. Use a symbolic relocation instead.1012        if (sym.isPreemptible) {1013          part.relaDyn->addSymbolReloc(type, *sec, offset, sym, addend, type);1014        } else if (part.relrAuthDyn && sec->addralign >= 2 && offset % 2 == 0) {1015          // When symbol values are determined in1016          // finalizeAddressDependentContent, some .relr.auth.dyn relocations1017          // may be moved to .rela.dyn.1018          sec->addReloc({expr, type, offset, addend, &sym});1019          part.relrAuthDyn->relocs.push_back({sec, sec->relocs().size() - 1});1020        } else {1021          part.relaDyn->addReloc({R_AARCH64_AUTH_RELATIVE, sec, offset, false,1022                                  sym, addend, R_ABS});1023        }1024        return;1025      }1026      if (LLVM_UNLIKELY(type == ctx.target->iRelSymbolicRel)) {1027        if (sym.isPreemptible) {1028          auto diag = Err(ctx);1029          diag << "relocation " << type1030               << " cannot be used against preemptible symbol '" << &sym << "'";1031          printLocation(diag, *sec, sym, offset);1032        } else if (isIfunc) {1033          auto diag = Err(ctx);1034          diag << "relocation " << type1035               << " cannot be used against ifunc symbol '" << &sym << "'";1036          printLocation(diag, *sec, sym, offset);1037        } else {1038          part.relaDyn->addReloc({ctx.target->iRelativeRel, sec, offset, false,1039                                  sym, addend, R_ABS});1040          return;1041        }1042      }1043      part.relaDyn->addSymbolReloc(rel, *sec, offset, sym, addend, type);1044 1045      // MIPS ABI turns using of GOT and dynamic relocations inside out.1046      // While regular ABI uses dynamic relocations to fill up GOT entries1047      // MIPS ABI requires dynamic linker to fills up GOT entries using1048      // specially sorted dynamic symbol table. This affects even dynamic1049      // relocations against symbols which do not require GOT entries1050      // creation explicitly, i.e. do not have any GOT-relocations. So if1051      // a preemptible symbol has a dynamic relocation we anyway have1052      // to create a GOT entry for it.1053      // If a non-preemptible symbol has a dynamic relocation against it,1054      // dynamic linker takes it st_value, adds offset and writes down1055      // result of the dynamic relocation. In case of preemptible symbol1056      // dynamic linker performs symbol resolution, writes the symbol value1057      // to the GOT entry and reads the GOT entry when it needs to perform1058      // a dynamic relocation.1059      // ftp://www.linux-mips.org/pub/linux/mips/doc/ABI/mipsabi.pdf p.4-191060      if (ctx.arg.emachine == EM_MIPS)1061        ctx.in.mipsGot->addEntry(*sec->file, sym, addend, expr);1062      return;1063    }1064  }1065 1066  // When producing an executable, we can perform copy relocations (for1067  // STT_OBJECT) and canonical PLT (for STT_FUNC) if sym is defined by a DSO.1068  // Copy relocations/canonical PLT entries are unsupported for1069  // R_AARCH64_AUTH_ABS64.1070  if (!ctx.arg.shared && sym.isShared() &&1071      !(ctx.arg.emachine == EM_AARCH64 && type == R_AARCH64_AUTH_ABS64)) {1072    if (!canDefineSymbolInExecutable(ctx, sym)) {1073      auto diag = Err(ctx);1074      diag << "cannot preempt symbol: " << &sym;1075      printLocation(diag, *sec, sym, offset);1076      return;1077    }1078 1079    if (sym.isObject()) {1080      // Produce a copy relocation.1081      if (auto *ss = dyn_cast<SharedSymbol>(&sym)) {1082        if (!ctx.arg.zCopyreloc) {1083          auto diag = Err(ctx);1084          diag << "unresolvable relocation " << type << " against symbol '"1085               << ss << "'; recompile with -fPIC or remove '-z nocopyreloc'";1086          printLocation(diag, *sec, sym, offset);1087        }1088        sym.setFlags(NEEDS_COPY);1089      }1090      sec->addReloc({expr, type, offset, addend, &sym});1091      return;1092    }1093 1094    // This handles a non PIC program call to function in a shared library. In1095    // an ideal world, we could just report an error saying the relocation can1096    // overflow at runtime. In the real world with glibc, crt1.o has a1097    // R_X86_64_PC32 pointing to libc.so.1098    //1099    // The general idea on how to handle such cases is to create a PLT entry and1100    // use that as the function value.1101    //1102    // For the static linking part, we just return a plt expr and everything1103    // else will use the PLT entry as the address.1104    //1105    // The remaining problem is making sure pointer equality still works. We1106    // need the help of the dynamic linker for that. We let it know that we have1107    // a direct reference to a so symbol by creating an undefined symbol with a1108    // non zero st_value. Seeing that, the dynamic linker resolves the symbol to1109    // the value of the symbol we created. This is true even for got entries, so1110    // pointer equality is maintained. To avoid an infinite loop, the only entry1111    // that points to the real function is a dedicated got entry used by the1112    // plt. That is identified by special relocation types (R_X86_64_JUMP_SLOT,1113    // R_386_JMP_SLOT, etc).1114 1115    // For position independent executable on i386, the plt entry requires ebx1116    // to be set. This causes two problems:1117    // * If some code has a direct reference to a function, it was probably1118    //   compiled without -fPIE/-fPIC and doesn't maintain ebx.1119    // * If a library definition gets preempted to the executable, it will have1120    //   the wrong ebx value.1121    if (sym.isFunc()) {1122      if (ctx.arg.pie && ctx.arg.emachine == EM_386) {1123        auto diag = Err(ctx);1124        diag << "symbol '" << &sym1125             << "' cannot be preempted; recompile with -fPIE";1126        printLocation(diag, *sec, sym, offset);1127      }1128      sym.setFlags(NEEDS_COPY | NEEDS_PLT);1129      sec->addReloc({expr, type, offset, addend, &sym});1130      return;1131    }1132  }1133 1134  auto diag = Err(ctx);1135  diag << "relocation " << type << " cannot be used against ";1136  if (sym.getName().empty())1137    diag << "local symbol";1138  else1139    diag << "symbol '" << &sym << "'";1140  diag << "; recompile with -fPIC";1141  printLocation(diag, *sec, sym, offset);1142}1143 1144static unsigned handleAArch64PAuthTlsRelocation(InputSectionBase *sec,1145                                                RelExpr expr, RelType type,1146                                                uint64_t offset, Symbol &sym,1147                                                int64_t addend) {1148  // Do not optimize signed TLSDESC to LE/IE (as described in pauthabielf64).1149  // https://github.com/ARM-software/abi-aa/blob/main/pauthabielf64/pauthabielf64.rst#general-restrictions1150  // > PAUTHELF64 only supports the descriptor based TLS (TLSDESC).1151  if (oneof<RE_AARCH64_AUTH_TLSDESC_PAGE, RE_AARCH64_AUTH_TLSDESC>(expr)) {1152    sym.setFlags(NEEDS_TLSDESC | NEEDS_TLSDESC_AUTH);1153    sec->addReloc({expr, type, offset, addend, &sym});1154    return 1;1155  }1156 1157  // TLSDESC_CALL hint relocation should not be emitted by compiler with signed1158  // TLSDESC enabled.1159  if (expr == R_TLSDESC_CALL)1160    sym.setFlags(NEEDS_TLSDESC_NONAUTH);1161 1162  return 0;1163}1164 1165// Notes about General Dynamic and Local Dynamic TLS models below. They may1166// require the generation of a pair of GOT entries that have associated dynamic1167// relocations. The pair of GOT entries created are of the form GOT[e0] Module1168// Index (Used to find pointer to TLS block at run-time) GOT[e1] Offset of1169// symbol in TLS block.1170//1171// Returns the number of relocations processed.1172unsigned RelocScan::handleTlsRelocation(RelExpr expr, RelType type,1173                                        uint64_t offset, Symbol &sym,1174                                        int64_t addend) {1175  bool isAArch64 = ctx.arg.emachine == EM_AARCH64;1176 1177  if (isAArch64)1178    if (unsigned processed = handleAArch64PAuthTlsRelocation(1179            sec, expr, type, offset, sym, addend))1180      return processed;1181 1182  if (expr == R_TPREL || expr == R_TPREL_NEG)1183    return checkTlsLe(offset, sym, type) ? 1 : 0;1184 1185  bool isRISCV = ctx.arg.emachine == EM_RISCV;1186 1187  if (oneof<RE_AARCH64_TLSDESC_PAGE, R_TLSDESC, R_TLSDESC_CALL, R_TLSDESC_PC,1188            R_TLSDESC_GOTPLT, RE_LOONGARCH_TLSDESC_PAGE_PC>(expr) &&1189      ctx.arg.shared) {1190    // R_RISCV_TLSDESC_{LOAD_LO12,ADD_LO12_I,CALL} reference a label. Do not1191    // set NEEDS_TLSDESC on the label.1192    if (expr != R_TLSDESC_CALL) {1193      if (isAArch64)1194        sym.setFlags(NEEDS_TLSDESC | NEEDS_TLSDESC_NONAUTH);1195      else if (!isRISCV || type == R_RISCV_TLSDESC_HI20)1196        sym.setFlags(NEEDS_TLSDESC);1197      sec->addReloc({expr, type, offset, addend, &sym});1198    }1199    return 1;1200  }1201 1202  // LoongArch supports IE to LE, DESC GD/LD to IE/LE optimizations in1203  // non-extreme code model.1204  bool execOptimizeInLoongArch =1205      ctx.arg.emachine == EM_LOONGARCH &&1206      (type == R_LARCH_TLS_IE_PC_HI20 || type == R_LARCH_TLS_IE_PC_LO12 ||1207       type == R_LARCH_TLS_DESC_PC_HI20 || type == R_LARCH_TLS_DESC_PC_LO12 ||1208       type == R_LARCH_TLS_DESC_LD || type == R_LARCH_TLS_DESC_CALL ||1209       type == R_LARCH_TLS_DESC_PCREL20_S2);1210 1211  // ARM, Hexagon, LoongArch and RISC-V do not support GD/LD to IE/LE1212  // optimizations.1213  // RISC-V supports TLSDESC to IE/LE optimizations.1214  // For PPC64, if the file has missing R_PPC64_TLSGD/R_PPC64_TLSLD, disable1215  // optimization as well.1216  bool execOptimize =1217      !ctx.arg.shared && ctx.arg.emachine != EM_ARM &&1218      ctx.arg.emachine != EM_HEXAGON &&1219      (ctx.arg.emachine != EM_LOONGARCH || execOptimizeInLoongArch) &&1220      !(isRISCV && expr != R_TLSDESC_PC && expr != R_TLSDESC_CALL) &&1221      !sec->file->ppc64DisableTLSRelax;1222 1223  // If we are producing an executable and the symbol is non-preemptable, it1224  // must be defined and the code sequence can be optimized to use1225  // Local-Exesec->1226  //1227  // ARM and RISC-V do not support any relaxations for TLS relocations, however,1228  // we can omit the DTPMOD dynamic relocations and resolve them at link time1229  // because them are always 1. This may be necessary for static linking as1230  // DTPMOD may not be expected at load time.1231  bool isLocalInExecutable = !sym.isPreemptible && !ctx.arg.shared;1232 1233  // Local Dynamic is for access to module local TLS variables, while still1234  // being suitable for being dynamically loaded via dlopen. GOT[e0] is the1235  // module index, with a special value of 0 for the current module. GOT[e1] is1236  // unused. There only needs to be one module index entry.1237  if (oneof<R_TLSLD_GOT, R_TLSLD_GOTPLT, R_TLSLD_PC, R_TLSLD_HINT>(expr)) {1238    // Local-Dynamic relocs can be optimized to Local-Exesec->1239    if (execOptimize) {1240      sec->addReloc({ctx.target->adjustTlsExpr(type, R_RELAX_TLS_LD_TO_LE),1241                     type, offset, addend, &sym});1242      return ctx.target->getTlsGdRelaxSkip(type);1243    }1244    if (expr == R_TLSLD_HINT)1245      return 1;1246    ctx.needsTlsLd.store(true, std::memory_order_relaxed);1247    sec->addReloc({expr, type, offset, addend, &sym});1248    return 1;1249  }1250 1251  // Local-Dynamic relocs can be optimized to Local-Exesec->1252  if (expr == R_DTPREL) {1253    if (execOptimize)1254      expr = ctx.target->adjustTlsExpr(type, R_RELAX_TLS_LD_TO_LE);1255    sec->addReloc({expr, type, offset, addend, &sym});1256    return 1;1257  }1258 1259  // Local-Dynamic sequence where offset of tls variable relative to dynamic1260  // thread pointer is stored in the got. This cannot be optimized to1261  // Local-Exesec->1262  if (expr == R_TLSLD_GOT_OFF) {1263    sym.setFlags(NEEDS_GOT_DTPREL);1264    sec->addReloc({expr, type, offset, addend, &sym});1265    return 1;1266  }1267 1268  // LoongArch does not support transition from TLSDESC to LE/IE in the extreme1269  // code model, in which NEEDS_TLSDESC should set, rather than NEEDS_TLSGD. So1270  // we check independently.1271  if (ctx.arg.emachine == EM_LOONGARCH &&1272      oneof<RE_LOONGARCH_TLSDESC_PAGE_PC, R_TLSDESC, R_TLSDESC_PC,1273            R_TLSDESC_CALL>(expr) &&1274      !execOptimize) {1275    if (expr != R_TLSDESC_CALL) {1276      sym.setFlags(NEEDS_TLSDESC);1277      sec->addReloc({expr, type, offset, addend, &sym});1278    }1279    return 1;1280  }1281 1282  if (oneof<RE_AARCH64_TLSDESC_PAGE, R_TLSDESC, R_TLSDESC_CALL, R_TLSDESC_PC,1283            R_TLSDESC_GOTPLT, R_TLSGD_GOT, R_TLSGD_GOTPLT, R_TLSGD_PC,1284            RE_LOONGARCH_TLSGD_PAGE_PC, RE_LOONGARCH_TLSDESC_PAGE_PC>(expr)) {1285    if (!execOptimize) {1286      sym.setFlags(NEEDS_TLSGD);1287      sec->addReloc({expr, type, offset, addend, &sym});1288      return 1;1289    }1290 1291    // Global-Dynamic/TLSDESC can be optimized to Initial-Exec or Local-Exec1292    // depending on the symbol being locally defined or not.1293    //1294    // R_RISCV_TLSDESC_{LOAD_LO12,ADD_LO12_I,CALL} reference a non-preemptible1295    // label, so TLSDESC=>IE will be categorized as R_RELAX_TLS_GD_TO_LE. We fix1296    // the categorization in RISCV::relocateAllosec->1297    if (sym.isPreemptible) {1298      sym.setFlags(NEEDS_TLSGD_TO_IE);1299      sec->addReloc({ctx.target->adjustTlsExpr(type, R_RELAX_TLS_GD_TO_IE),1300                     type, offset, addend, &sym});1301    } else {1302      sec->addReloc({ctx.target->adjustTlsExpr(type, R_RELAX_TLS_GD_TO_LE),1303                     type, offset, addend, &sym});1304    }1305    return ctx.target->getTlsGdRelaxSkip(type);1306  }1307 1308  if (oneof<R_GOT, R_GOTPLT, R_GOT_PC, RE_AARCH64_GOT_PAGE_PC,1309            RE_LOONGARCH_GOT_PAGE_PC, R_GOT_OFF, R_TLSIE_HINT>(expr)) {1310    ctx.hasTlsIe.store(true, std::memory_order_relaxed);1311    // Initial-Exec relocs can be optimized to Local-Exec if the symbol is1312    // locally defined.  This is not supported on SystemZ.1313    if (execOptimize && isLocalInExecutable && ctx.arg.emachine != EM_S390) {1314      sec->addReloc({R_RELAX_TLS_IE_TO_LE, type, offset, addend, &sym});1315    } else if (expr != R_TLSIE_HINT) {1316      sym.setFlags(NEEDS_TLSIE);1317      // R_GOT needs a relative relocation for PIC on i386 and Hexagon.1318      if (expr == R_GOT && ctx.arg.isPic &&1319          !ctx.target->usesOnlyLowPageBits(type))1320        addRelativeReloc<true>(ctx, *sec, offset, sym, addend, expr, type);1321      else1322        sec->addReloc({expr, type, offset, addend, &sym});1323    }1324    return 1;1325  }1326 1327  // LoongArch TLS GD/LD relocs reuse the RE_LOONGARCH_GOT, in which1328  // NEEDS_TLSIE shouldn't set. So we check independently.1329  if (ctx.arg.emachine == EM_LOONGARCH && expr == RE_LOONGARCH_GOT &&1330      execOptimize && isLocalInExecutable) {1331    ctx.hasTlsIe.store(true, std::memory_order_relaxed);1332    sec->addReloc({R_RELAX_TLS_IE_TO_LE, type, offset, addend, &sym});1333    return 1;1334  }1335 1336  return 0;1337}1338 1339template <class ELFT, class RelTy>1340void TargetInfo::scanSectionImpl(InputSectionBase &sec, Relocs<RelTy> rels) {1341  RelocScan rs(ctx, &sec);1342  // Many relocations end up in sec.relocations.1343  sec.relocations.reserve(rels.size());1344 1345  // On SystemZ, all sections need to be sorted by r_offset, to allow TLS1346  // relaxation to be handled correctly - see SystemZ::getTlsGdRelaxSkip.1347  SmallVector<RelTy, 0> storage;1348  if (ctx.arg.emachine == EM_S390)1349    rels = sortRels(rels, storage);1350 1351  for (auto it = rels.begin(); it != rels.end(); ++it) {1352    auto type = it->getType(false);1353    rs.scan<ELFT, RelTy>(it, type, rs.getAddend<ELFT>(*it, type));1354  }1355 1356  // Sort relocations by offset for more efficient searching for1357  // R_RISCV_PCREL_HI20, ALIGN relocations, R_PPC64_ADDR64 and the1358  // branch-to-branch optimization.1359  if (is_contained({EM_RISCV, EM_LOONGARCH}, ctx.arg.emachine) ||1360      (ctx.arg.emachine == EM_PPC64 && sec.name == ".toc") ||1361      ctx.arg.branchToBranch)1362    llvm::stable_sort(sec.relocs(),1363                      [](const Relocation &lhs, const Relocation &rhs) {1364                        return lhs.offset < rhs.offset;1365                      });1366}1367 1368template <class ELFT> void TargetInfo::scanSection1(InputSectionBase &sec) {1369  const RelsOrRelas<ELFT> rels = sec.template relsOrRelas<ELFT>();1370  if (rels.areRelocsCrel())1371    scanSectionImpl<ELFT>(sec, rels.crels);1372  else if (rels.areRelocsRel())1373    scanSectionImpl<ELFT>(sec, rels.rels);1374  else1375    scanSectionImpl<ELFT>(sec, rels.relas);1376}1377 1378void TargetInfo::scanSection(InputSectionBase &sec) {1379  invokeELFT(scanSection1, sec);1380}1381 1382void RelocScan::scanEhSection(EhInputSection &s) {1383  sec = &s;1384  OffsetGetter getter(s);1385  auto rels = s.rels;1386  s.relocations.reserve(rels.size());1387  for (auto &r : rels) {1388    // Ignore R_*_NONE and other marker relocations.1389    if (r.expr == R_NONE)1390      continue;1391    uint64_t offset = getter.get(ctx, r.offset);1392    // Skip if the relocation offset is within a dead piece.1393    if (offset == uint64_t(-1))1394      continue;1395    Symbol *sym = r.sym;1396    if (sym->isUndefined() &&1397        maybeReportUndefined(cast<Undefined>(*sym), offset))1398      continue;1399    process(r.expr, r.type, offset, *sym, r.addend);1400  }1401}1402 1403template <class ELFT> void elf::scanRelocations(Ctx &ctx) {1404  // Scan all relocations. Each relocation goes through a series of tests to1405  // determine if it needs special treatment, such as creating GOT, PLT,1406  // copy relocations, etc. Note that relocations for non-alloc sections are1407  // directly processed by InputSection::relocateNonAlloc.1408 1409  // Deterministic parallellism needs sorting relocations which is unsuitable1410  // for -z nocombreloc. MIPS and PPC64 use global states which are not suitable1411  // for parallelism.1412  bool serial = !ctx.arg.zCombreloc || ctx.arg.emachine == EM_MIPS ||1413                ctx.arg.emachine == EM_PPC64;1414  parallel::TaskGroup tg;1415  auto outerFn = [&]() {1416    for (ELFFileBase *f : ctx.objectFiles) {1417      auto fn = [f, &ctx]() {1418        for (InputSectionBase *s : f->getSections()) {1419          if (s && s->kind() == SectionBase::Regular && s->isLive() &&1420              (s->flags & SHF_ALLOC) &&1421              !(s->type == SHT_ARM_EXIDX && ctx.arg.emachine == EM_ARM))1422            ctx.target->scanSection(*s);1423        }1424      };1425      if (serial)1426        fn();1427      else1428        tg.spawn(fn);1429    }1430    auto scanEH = [&] {1431      RelocScan scanner(ctx);1432      for (Partition &part : ctx.partitions) {1433        for (EhInputSection *sec : part.ehFrame->sections)1434          scanner.scanEhSection(*sec);1435        if (part.armExidx && part.armExidx->isLive())1436          for (InputSection *sec : part.armExidx->exidxSections)1437            if (sec->isLive())1438              ctx.target->scanSection(*sec);1439      }1440    };1441    if (serial)1442      scanEH();1443    else1444      tg.spawn(scanEH);1445  };1446  // If `serial` is true, call `spawn` to ensure that `scanner` runs in a thread1447  // with valid getThreadIndex().1448  if (serial)1449    tg.spawn(outerFn);1450  else1451    outerFn();1452}1453 1454RelocationBaseSection &elf::getIRelativeSection(Ctx &ctx) {1455  // Prior to Android V, there was a bug that caused RELR relocations to be1456  // applied after packed relocations. This meant that resolvers referenced by1457  // IRELATIVE relocations in the packed relocation section would read1458  // unrelocated globals with RELR relocations when1459  // --pack-relative-relocs=android+relr is enabled. Work around this by placing1460  // IRELATIVE in .rela.plt.1461  return ctx.arg.androidPackDynRelocs ? *ctx.in.relaPlt1462                                      : *ctx.mainPart->relaDyn;1463}1464 1465static bool handleNonPreemptibleIfunc(Ctx &ctx, Symbol &sym, uint16_t flags) {1466  // Handle a reference to a non-preemptible ifunc. These are special in a1467  // few ways:1468  //1469  // - Unlike most non-preemptible symbols, non-preemptible ifuncs do not have1470  //   a fixed value. But assuming that all references to the ifunc are1471  //   GOT-generating or PLT-generating, the handling of an ifunc is1472  //   relatively straightforward. We create a PLT entry in Iplt, which is1473  //   usually at the end of .plt, which makes an indirect call using a1474  //   matching GOT entry in igotPlt, which is usually at the end of .got.plt.1475  //   The GOT entry is relocated using an IRELATIVE relocation in relaDyn,1476  //   which is usually at the end of .rela.dyn.1477  //1478  // - Despite the fact that an ifunc does not have a fixed value, compilers1479  //   that are not passed -fPIC will assume that they do, and will emit1480  //   direct (non-GOT-generating, non-PLT-generating) relocations to the1481  //   symbol. This means that if a direct relocation to the symbol is1482  //   seen, the linker must set a value for the symbol, and this value must1483  //   be consistent no matter what type of reference is made to the symbol.1484  //   This can be done by creating a PLT entry for the symbol in the way1485  //   described above and making it canonical, that is, making all references1486  //   point to the PLT entry instead of the resolver. In lld we also store1487  //   the address of the PLT entry in the dynamic symbol table, which means1488  //   that the symbol will also have the same value in other modules.1489  //   Because the value loaded from the GOT needs to be consistent with1490  //   the value computed using a direct relocation, a non-preemptible ifunc1491  //   may end up with two GOT entries, one in .got.plt that points to the1492  //   address returned by the resolver and is used only by the PLT entry,1493  //   and another in .got that points to the PLT entry and is used by1494  //   GOT-generating relocations.1495  //1496  // - The fact that these symbols do not have a fixed value makes them an1497  //   exception to the general rule that a statically linked executable does1498  //   not require any form of dynamic relocation. To handle these relocations1499  //   correctly, the IRELATIVE relocations are stored in an array which a1500  //   statically linked executable's startup code must enumerate using the1501  //   linker-defined symbols __rela?_iplt_{start,end}.1502  if (!sym.isGnuIFunc() || sym.isPreemptible || ctx.arg.zIfuncNoplt)1503    return false;1504  // Skip unreferenced non-preemptible ifunc.1505  if (!(flags & (NEEDS_GOT | NEEDS_PLT | HAS_DIRECT_RELOC)))1506    return true;1507 1508  sym.isInIplt = true;1509 1510  // Create an Iplt and the associated IRELATIVE relocation pointing to the1511  // original section/value pairs. For non-GOT non-PLT relocation case below, we1512  // may alter section/value, so create a copy of the symbol to make1513  // section/value fixed.1514  auto *directSym = makeDefined(cast<Defined>(sym));1515  directSym->allocateAux(ctx);1516  auto &dyn = getIRelativeSection(ctx);1517  addPltEntry(ctx, *ctx.in.iplt, *ctx.in.igotPlt, dyn, ctx.target->iRelativeRel,1518              *directSym);1519  sym.allocateAux(ctx);1520  ctx.symAux.back().pltIdx = ctx.symAux[directSym->auxIdx].pltIdx;1521 1522  if (flags & HAS_DIRECT_RELOC) {1523    // Change the value to the IPLT and redirect all references to it.1524    auto &d = cast<Defined>(sym);1525    d.section = ctx.in.iplt.get();1526    d.value = d.getPltIdx(ctx) * ctx.target->ipltEntrySize;1527    d.size = 0;1528    // It's important to set the symbol type here so that dynamic loaders1529    // don't try to call the PLT as if it were an ifunc resolver.1530    d.type = STT_FUNC;1531 1532    if (flags & NEEDS_GOT) {1533      assert(!(flags & NEEDS_GOT_AUTH) &&1534             "R_AARCH64_AUTH_IRELATIVE is not supported yet");1535      addGotEntry(ctx, sym);1536    }1537  } else if (flags & NEEDS_GOT) {1538    // Redirect GOT accesses to point to the Igot.1539    sym.gotInIgot = true;1540  }1541  return true;1542}1543 1544void elf::postScanRelocations(Ctx &ctx) {1545  auto fn = [&](Symbol &sym) {1546    auto flags = sym.flags.load(std::memory_order_relaxed);1547    if (handleNonPreemptibleIfunc(ctx, sym, flags))1548      return;1549 1550    if (sym.isTagged() && sym.isDefined())1551      ctx.mainPart->memtagGlobalDescriptors->addSymbol(sym);1552 1553    if (!sym.needsDynReloc())1554      return;1555    sym.allocateAux(ctx);1556 1557    if (flags & NEEDS_GOT) {1558      if ((flags & NEEDS_GOT_AUTH) && (flags & NEEDS_GOT_NONAUTH)) {1559        auto diag = Err(ctx);1560        diag << "both AUTH and non-AUTH GOT entries for '" << sym.getName()1561             << "' requested, but only one type of GOT entry per symbol is "1562                "supported";1563        return;1564      }1565      if (flags & NEEDS_GOT_AUTH)1566        addGotAuthEntry(ctx, sym);1567      else1568        addGotEntry(ctx, sym);1569    }1570    if (flags & NEEDS_PLT)1571      addPltEntry(ctx, *ctx.in.plt, *ctx.in.gotPlt, *ctx.in.relaPlt,1572                  ctx.target->pltRel, sym);1573    if (flags & NEEDS_COPY) {1574      if (sym.isObject()) {1575        invokeELFT(addCopyRelSymbol, ctx, cast<SharedSymbol>(sym));1576        // NEEDS_COPY is cleared for sym and its aliases so that in1577        // later iterations aliases won't cause redundant copies.1578        assert(!sym.hasFlag(NEEDS_COPY));1579      } else {1580        assert(sym.isFunc() && sym.hasFlag(NEEDS_PLT));1581        if (!sym.isDefined()) {1582          replaceWithDefined(ctx, sym, *ctx.in.plt,1583                             ctx.target->pltHeaderSize +1584                                 ctx.target->pltEntrySize * sym.getPltIdx(ctx),1585                             0);1586          sym.setFlags(NEEDS_COPY);1587          if (ctx.arg.emachine == EM_PPC) {1588            // PPC32 canonical PLT entries are at the beginning of .glink1589            cast<Defined>(sym).value = ctx.in.plt->headerSize;1590            ctx.in.plt->headerSize += 16;1591            cast<PPC32GlinkSection>(*ctx.in.plt).canonical_plts.push_back(&sym);1592          }1593        }1594      }1595    }1596 1597    if (!sym.isTls())1598      return;1599    bool isLocalInExecutable = !sym.isPreemptible && !ctx.arg.shared;1600    GotSection *got = ctx.in.got.get();1601 1602    if (flags & NEEDS_TLSDESC) {1603      if ((flags & NEEDS_TLSDESC_AUTH) && (flags & NEEDS_TLSDESC_NONAUTH)) {1604        Err(ctx)1605            << "both AUTH and non-AUTH TLSDESC entries for '" << sym.getName()1606            << "' requested, but only one type of TLSDESC entry per symbol is "1607               "supported";1608        return;1609      }1610      got->addTlsDescEntry(sym);1611      RelType tlsDescRel = ctx.target->tlsDescRel;1612      if (flags & NEEDS_TLSDESC_AUTH) {1613        got->addTlsDescAuthEntry();1614        tlsDescRel = ELF::R_AARCH64_AUTH_TLSDESC;1615      }1616      ctx.mainPart->relaDyn->addAddendOnlyRelocIfNonPreemptible(1617          tlsDescRel, *got, got->getTlsDescOffset(sym), sym, tlsDescRel);1618    }1619    if (flags & NEEDS_TLSGD) {1620      got->addDynTlsEntry(sym);1621      uint64_t off = got->getGlobalDynOffset(sym);1622      if (isLocalInExecutable)1623        // Write one to the GOT slot.1624        got->addConstant({R_ADDEND, ctx.target->symbolicRel, off, 1, &sym});1625      else1626        ctx.mainPart->relaDyn->addSymbolReloc(ctx.target->tlsModuleIndexRel,1627                                              *got, off, sym);1628 1629      // If the symbol is preemptible we need the dynamic linker to write1630      // the offset too.1631      uint64_t offsetOff = off + ctx.arg.wordsize;1632      if (sym.isPreemptible)1633        ctx.mainPart->relaDyn->addSymbolReloc(ctx.target->tlsOffsetRel, *got,1634                                              offsetOff, sym);1635      else1636        got->addConstant({R_ABS, ctx.target->tlsOffsetRel, offsetOff, 0, &sym});1637    }1638    if (flags & NEEDS_TLSGD_TO_IE) {1639      got->addEntry(sym);1640      ctx.mainPart->relaDyn->addSymbolReloc(ctx.target->tlsGotRel, *got,1641                                            sym.getGotOffset(ctx), sym);1642    }1643    if (flags & NEEDS_GOT_DTPREL) {1644      got->addEntry(sym);1645      got->addConstant(1646          {R_ABS, ctx.target->tlsOffsetRel, sym.getGotOffset(ctx), 0, &sym});1647    }1648 1649    if ((flags & NEEDS_TLSIE) && !(flags & NEEDS_TLSGD_TO_IE))1650      addTpOffsetGotEntry(ctx, sym);1651  };1652 1653  GotSection *got = ctx.in.got.get();1654  if (ctx.needsTlsLd.load(std::memory_order_relaxed) && got->addTlsIndex()) {1655    if (ctx.arg.shared)1656      ctx.mainPart->relaDyn->addReloc(1657          {ctx.target->tlsModuleIndexRel, got, got->getTlsIndexOff()});1658    else1659      got->addConstant({R_ADDEND, ctx.target->symbolicRel,1660                        got->getTlsIndexOff(), 1, ctx.dummySym});1661  }1662 1663  assert(ctx.symAux.size() == 1);1664  for (Symbol *sym : ctx.symtab->getSymbols())1665    fn(*sym);1666 1667  // Local symbols may need the aforementioned non-preemptible ifunc and GOT1668  // handling. They don't need regular PLT.1669  for (ELFFileBase *file : ctx.objectFiles)1670    for (Symbol *sym : file->getLocalSymbols())1671      fn(*sym);1672 1673  if (ctx.arg.branchToBranch)1674    ctx.target->applyBranchToBranchOpt();1675}1676 1677static bool mergeCmp(const InputSection *a, const InputSection *b) {1678  // std::merge requires a strict weak ordering.1679  if (a->outSecOff < b->outSecOff)1680    return true;1681 1682  // FIXME dyn_cast<ThunkSection> is non-null for any SyntheticSection.1683  if (a->outSecOff == b->outSecOff && a != b) {1684    auto *ta = dyn_cast<ThunkSection>(a);1685    auto *tb = dyn_cast<ThunkSection>(b);1686 1687    // Check if Thunk is immediately before any specific Target1688    // InputSection for example Mips LA25 Thunks.1689    if (ta && ta->getTargetInputSection() == b)1690      return true;1691 1692    // Place Thunk Sections without specific targets before1693    // non-Thunk Sections.1694    if (ta && !tb && !ta->getTargetInputSection())1695      return true;1696  }1697 1698  return false;1699}1700 1701// Call Fn on every executable InputSection accessed via the linker script1702// InputSectionDescription::Sections.1703static void forEachInputSectionDescription(1704    ArrayRef<OutputSection *> outputSections,1705    llvm::function_ref<void(OutputSection *, InputSectionDescription *)> fn) {1706  for (OutputSection *os : outputSections) {1707    if (!(os->flags & SHF_ALLOC) || !(os->flags & SHF_EXECINSTR))1708      continue;1709    for (SectionCommand *bc : os->commands)1710      if (auto *isd = dyn_cast<InputSectionDescription>(bc))1711        fn(os, isd);1712  }1713}1714 1715ThunkCreator::ThunkCreator(Ctx &ctx) : ctx(ctx) {}1716 1717ThunkCreator::~ThunkCreator() {}1718 1719// Thunk Implementation1720//1721// Thunks (sometimes called stubs, veneers or branch islands) are small pieces1722// of code that the linker inserts inbetween a caller and a callee. The thunks1723// are added at link time rather than compile time as the decision on whether1724// a thunk is needed, such as the caller and callee being out of range, can only1725// be made at link time.1726//1727// It is straightforward to tell given the current state of the program when a1728// thunk is needed for a particular call. The more difficult part is that1729// the thunk needs to be placed in the program such that the caller can reach1730// the thunk and the thunk can reach the callee; furthermore, adding thunks to1731// the program alters addresses, which can mean more thunks etc.1732//1733// In lld we have a synthetic ThunkSection that can hold many Thunks.1734// The decision to have a ThunkSection act as a container means that we can1735// more easily handle the most common case of a single block of contiguous1736// Thunks by inserting just a single ThunkSection.1737//1738// The implementation of Thunks in lld is split across these areas1739// Relocations.cpp : Framework for creating and placing thunks1740// Thunks.cpp : The code generated for each supported thunk1741// Target.cpp : Target specific hooks that the framework uses to decide when1742//              a thunk is used1743// Synthetic.cpp : Implementation of ThunkSection1744// Writer.cpp : Iteratively call framework until no more Thunks added1745//1746// Thunk placement requirements:1747// Mips LA25 thunks. These must be placed immediately before the callee section1748// We can assume that the caller is in range of the Thunk. These are modelled1749// by Thunks that return the section they must precede with1750// getTargetInputSection().1751//1752// ARM interworking and range extension thunks. These thunks must be placed1753// within range of the caller. All implemented ARM thunks can always reach the1754// callee as they use an indirect jump via a register that has no range1755// restrictions.1756//1757// Thunk placement algorithm:1758// For Mips LA25 ThunkSections; the placement is explicit, it has to be before1759// getTargetInputSection().1760//1761// For thunks that must be placed within range of the caller there are many1762// possible choices given that the maximum range from the caller is usually1763// much larger than the average InputSection size. Desirable properties include:1764// - Maximize reuse of thunks by multiple callers1765// - Minimize number of ThunkSections to simplify insertion1766// - Handle impact of already added Thunks on addresses1767// - Simple to understand and implement1768//1769// In lld for the first pass, we pre-create one or more ThunkSections per1770// InputSectionDescription at Target specific intervals. A ThunkSection is1771// placed so that the estimated end of the ThunkSection is within range of the1772// start of the InputSectionDescription or the previous ThunkSection. For1773// example:1774// InputSectionDescription1775// Section 01776// ...1777// Section N1778// ThunkSection 01779// Section N + 11780// ...1781// Section N + K1782// Thunk Section 11783//1784// The intention is that we can add a Thunk to a ThunkSection that is well1785// spaced enough to service a number of callers without having to do a lot1786// of work. An important principle is that it is not an error if a Thunk cannot1787// be placed in a pre-created ThunkSection; when this happens we create a new1788// ThunkSection placed next to the caller. This allows us to handle the vast1789// majority of thunks simply, but also handle rare cases where the branch range1790// is smaller than the target specific spacing.1791//1792// The algorithm is expected to create all the thunks that are needed in a1793// single pass, with a small number of programs needing a second pass due to1794// the insertion of thunks in the first pass increasing the offset between1795// callers and callees that were only just in range.1796//1797// A consequence of allowing new ThunkSections to be created outside of the1798// pre-created ThunkSections is that in rare cases calls to Thunks that were in1799// range in pass K, are out of range in some pass > K due to the insertion of1800// more Thunks in between the caller and callee. When this happens we retarget1801// the relocation back to the original target and create another Thunk.1802 1803// Remove ThunkSections that are empty, this should only be the initial set1804// precreated on pass 0.1805 1806// Insert the Thunks for OutputSection OS into their designated place1807// in the Sections vector, and recalculate the InputSection output section1808// offsets.1809// This may invalidate any output section offsets stored outside of InputSection1810void ThunkCreator::mergeThunks(ArrayRef<OutputSection *> outputSections) {1811  forEachInputSectionDescription(1812      outputSections, [&](OutputSection *os, InputSectionDescription *isd) {1813        if (isd->thunkSections.empty())1814          return;1815 1816        // Remove any zero sized precreated Thunks.1817        llvm::erase_if(isd->thunkSections,1818                       [](const std::pair<ThunkSection *, uint32_t> &ts) {1819                         return ts.first->getSize() == 0;1820                       });1821 1822        // ISD->ThunkSections contains all created ThunkSections, including1823        // those inserted in previous passes. Extract the Thunks created this1824        // pass and order them in ascending outSecOff.1825        std::vector<ThunkSection *> newThunks;1826        for (std::pair<ThunkSection *, uint32_t> ts : isd->thunkSections)1827          if (ts.second == pass)1828            newThunks.push_back(ts.first);1829        llvm::stable_sort(newThunks,1830                          [](const ThunkSection *a, const ThunkSection *b) {1831                            return a->outSecOff < b->outSecOff;1832                          });1833 1834        // Merge sorted vectors of Thunks and InputSections by outSecOff1835        SmallVector<InputSection *, 0> tmp;1836        tmp.reserve(isd->sections.size() + newThunks.size());1837 1838        std::merge(isd->sections.begin(), isd->sections.end(),1839                   newThunks.begin(), newThunks.end(), std::back_inserter(tmp),1840                   mergeCmp);1841 1842        isd->sections = std::move(tmp);1843      });1844}1845 1846constexpr uint32_t HEXAGON_MASK_END_PACKET = 3 << 14;1847constexpr uint32_t HEXAGON_END_OF_PACKET = 3 << 14;1848constexpr uint32_t HEXAGON_END_OF_DUPLEX = 0 << 14;1849 1850// Return the distance between the packet start and the instruction in the1851// relocation.1852static int getHexagonPacketOffset(const InputSection &isec,1853                                  const Relocation &rel) {1854  const ArrayRef<uint8_t> data = isec.content();1855 1856  // Search back as many as 3 instructions.1857  for (unsigned i = 0;; i++) {1858    if (i == 3 || rel.offset < (i + 1) * 4)1859      return i * 4;1860    uint32_t instWord =1861        read32(isec.getCtx(), data.data() + (rel.offset - (i + 1) * 4));1862    if (((instWord & HEXAGON_MASK_END_PACKET) == HEXAGON_END_OF_PACKET) ||1863        ((instWord & HEXAGON_MASK_END_PACKET) == HEXAGON_END_OF_DUPLEX))1864      return i * 4;1865  }1866}1867 1868static int64_t getPCBias(Ctx &ctx, const InputSection &isec,1869                         const Relocation &rel) {1870  if (ctx.arg.emachine == EM_ARM) {1871    switch (rel.type) {1872    case R_ARM_THM_JUMP19:1873    case R_ARM_THM_JUMP24:1874    case R_ARM_THM_CALL:1875      return 4;1876    default:1877      return 8;1878    }1879  }1880  if (ctx.arg.emachine == EM_HEXAGON)1881    return -getHexagonPacketOffset(isec, rel);1882  return 0;1883}1884 1885// Find or create a ThunkSection within the InputSectionDescription (ISD) that1886// is in range of Src. An ISD maps to a range of InputSections described by a1887// linker script section pattern such as { .text .text.* }.1888ThunkSection *ThunkCreator::getISDThunkSec(OutputSection *os,1889                                           InputSection *isec,1890                                           InputSectionDescription *isd,1891                                           const Relocation &rel,1892                                           uint64_t src) {1893  // See the comment in getThunk for -pcBias below.1894  const int64_t pcBias = getPCBias(ctx, *isec, rel);1895  for (std::pair<ThunkSection *, uint32_t> tp : isd->thunkSections) {1896    ThunkSection *ts = tp.first;1897    uint64_t tsBase = os->addr + ts->outSecOff - pcBias;1898    uint64_t tsLimit = tsBase + ts->getSize();1899    if (ctx.target->inBranchRange(rel.type, src,1900                                  (src > tsLimit) ? tsBase : tsLimit))1901      return ts;1902  }1903 1904  // No suitable ThunkSection exists. This can happen when there is a branch1905  // with lower range than the ThunkSection spacing or when there are too1906  // many Thunks. Create a new ThunkSection as close to the InputSection as1907  // possible. Error if InputSection is so large we cannot place ThunkSection1908  // anywhere in Range.1909  uint64_t thunkSecOff = isec->outSecOff;1910  if (!ctx.target->inBranchRange(rel.type, src,1911                                 os->addr + thunkSecOff + rel.addend)) {1912    thunkSecOff = isec->outSecOff + isec->getSize();1913    if (!ctx.target->inBranchRange(rel.type, src,1914                                   os->addr + thunkSecOff + rel.addend))1915      Fatal(ctx) << "InputSection too large for range extension thunk "1916                 << isec->getObjMsg(src - (os->addr << isec->outSecOff));1917  }1918  return addThunkSection(os, isd, thunkSecOff);1919}1920 1921// Add a Thunk that needs to be placed in a ThunkSection that immediately1922// precedes its Target.1923ThunkSection *ThunkCreator::getISThunkSec(InputSection *isec) {1924  ThunkSection *ts = thunkedSections.lookup(isec);1925  if (ts)1926    return ts;1927 1928  // Find InputSectionRange within Target Output Section (TOS) that the1929  // InputSection (IS) that we need to precede is in.1930  OutputSection *tos = isec->getParent();1931  for (SectionCommand *bc : tos->commands) {1932    auto *isd = dyn_cast<InputSectionDescription>(bc);1933    if (!isd || isd->sections.empty())1934      continue;1935 1936    InputSection *first = isd->sections.front();1937    InputSection *last = isd->sections.back();1938 1939    if (isec->outSecOff < first->outSecOff || last->outSecOff < isec->outSecOff)1940      continue;1941 1942    ts = addThunkSection(tos, isd, isec->outSecOff);1943    thunkedSections[isec] = ts;1944    return ts;1945  }1946 1947  return nullptr;1948}1949 1950// Create one or more ThunkSections per OS that can be used to place Thunks.1951// We attempt to place the ThunkSections using the following desirable1952// properties:1953// - Within range of the maximum number of callers1954// - Minimise the number of ThunkSections1955//1956// We follow a simple but conservative heuristic to place ThunkSections at1957// offsets that are multiples of a Target specific branch range.1958// For an InputSectionDescription that is smaller than the range, a single1959// ThunkSection at the end of the range will do.1960//1961// For an InputSectionDescription that is more than twice the size of the range,1962// we place the last ThunkSection at range bytes from the end of the1963// InputSectionDescription in order to increase the likelihood that the1964// distance from a thunk to its target will be sufficiently small to1965// allow for the creation of a short thunk.1966void ThunkCreator::createInitialThunkSections(1967    ArrayRef<OutputSection *> outputSections) {1968  uint32_t thunkSectionSpacing = ctx.target->getThunkSectionSpacing();1969  forEachInputSectionDescription(1970      outputSections, [&](OutputSection *os, InputSectionDescription *isd) {1971        if (isd->sections.empty())1972          return;1973 1974        uint32_t isdBegin = isd->sections.front()->outSecOff;1975        uint32_t isdEnd =1976            isd->sections.back()->outSecOff + isd->sections.back()->getSize();1977        uint32_t lastThunkLowerBound = -1;1978        if (isdEnd - isdBegin > thunkSectionSpacing * 2)1979          lastThunkLowerBound = isdEnd - thunkSectionSpacing;1980 1981        uint32_t isecLimit;1982        uint32_t prevIsecLimit = isdBegin;1983        uint32_t thunkUpperBound = isdBegin + thunkSectionSpacing;1984 1985        for (const InputSection *isec : isd->sections) {1986          isecLimit = isec->outSecOff + isec->getSize();1987          if (isecLimit > thunkUpperBound) {1988            addThunkSection(os, isd, prevIsecLimit);1989            thunkUpperBound = prevIsecLimit + thunkSectionSpacing;1990          }1991          if (isecLimit > lastThunkLowerBound)1992            break;1993          prevIsecLimit = isecLimit;1994        }1995        addThunkSection(os, isd, isecLimit);1996      });1997}1998 1999ThunkSection *ThunkCreator::addThunkSection(OutputSection *os,2000                                            InputSectionDescription *isd,2001                                            uint64_t off) {2002  auto *ts = make<ThunkSection>(ctx, os, off);2003  ts->partition = os->partition;2004  if ((ctx.arg.fixCortexA53Errata843419 || ctx.arg.fixCortexA8) &&2005      !isd->sections.empty()) {2006    // The errata fixes are sensitive to addresses modulo 4 KiB. When we add2007    // thunks we disturb the base addresses of sections placed after the thunks2008    // this makes patches we have generated redundant, and may cause us to2009    // generate more patches as different instructions are now in sensitive2010    // locations. When we generate more patches we may force more branches to2011    // go out of range, causing more thunks to be generated. In pathological2012    // cases this can cause the address dependent content pass not to converge.2013    // We fix this by rounding up the size of the ThunkSection to 4KiB, this2014    // limits the insertion of a ThunkSection on the addresses modulo 4 KiB,2015    // which means that adding Thunks to the section does not invalidate2016    // errata patches for following code.2017    // Rounding up the size to 4KiB has consequences for code-size and can2018    // trip up linker script defined assertions. For example the linux kernel2019    // has an assertion that what LLD represents as an InputSectionDescription2020    // does not exceed 4 KiB even if the overall OutputSection is > 128 Mib.2021    // We use the heuristic of rounding up the size when both of the following2022    // conditions are true:2023    // 1.) The OutputSection is larger than the ThunkSectionSpacing. This2024    //     accounts for the case where no single InputSectionDescription is2025    //     larger than the OutputSection size. This is conservative but simple.2026    // 2.) The InputSectionDescription is larger than 4 KiB. This will prevent2027    //     any assertion failures that an InputSectionDescription is < 4 KiB2028    //     in size.2029    uint64_t isdSize = isd->sections.back()->outSecOff +2030                       isd->sections.back()->getSize() -2031                       isd->sections.front()->outSecOff;2032    if (os->size > ctx.target->getThunkSectionSpacing() && isdSize > 4096)2033      ts->roundUpSizeForErrata = true;2034  }2035  isd->thunkSections.push_back({ts, pass});2036  return ts;2037}2038 2039static bool isThunkSectionCompatible(InputSection *source,2040                                     SectionBase *target) {2041  // We can't reuse thunks in different loadable partitions because they might2042  // not be loaded. But partition 1 (the main partition) will always be loaded.2043  if (source->partition != target->partition)2044    return target->partition == 1;2045  return true;2046}2047 2048std::pair<Thunk *, bool> ThunkCreator::getThunk(InputSection *isec,2049                                                Relocation &rel, uint64_t src) {2050  SmallVector<std::unique_ptr<Thunk>, 0> *thunkVec = nullptr;2051  // Arm and Thumb have a PC Bias of 8 and 4 respectively, this is cancelled2052  // out in the relocation addend. We compensate for the PC bias so that2053  // an Arm and Thumb relocation to the same destination get the same keyAddend,2054  // which is usually 0.2055  const int64_t pcBias = getPCBias(ctx, *isec, rel);2056  const int64_t keyAddend = rel.addend + pcBias;2057 2058  // We use a ((section, offset), addend) pair to find the thunk position if2059  // possible so that we create only one thunk for aliased symbols or ICFed2060  // sections. There may be multiple relocations sharing the same (section,2061  // offset + addend) pair. We may revert the relocation back to its original2062  // non-Thunk target, so we cannot fold offset + addend.2063  if (auto *d = dyn_cast<Defined>(rel.sym))2064    if (!d->isInPlt(ctx) && d->section)2065      thunkVec = &thunkedSymbolsBySectionAndAddend[{{d->section, d->value},2066                                                    keyAddend}];2067  if (!thunkVec)2068    thunkVec = &thunkedSymbols[{rel.sym, keyAddend}];2069 2070  // Check existing Thunks for Sym to see if they can be reused2071  for (auto &t : *thunkVec)2072    if (isThunkSectionCompatible(isec, t->getThunkTargetSym()->section) &&2073        t->isCompatibleWith(*isec, rel) &&2074        ctx.target->inBranchRange(rel.type, src,2075                                  t->getThunkTargetSym()->getVA(ctx, -pcBias)))2076      return std::make_pair(t.get(), false);2077 2078  // No existing compatible Thunk in range, create a new one2079  thunkVec->push_back(addThunk(ctx, *isec, rel));2080  return std::make_pair(thunkVec->back().get(), true);2081}2082 2083std::pair<Thunk *, bool> ThunkCreator::getSyntheticLandingPad(Defined &d,2084                                                              int64_t a) {2085  auto [it, isNew] = landingPadsBySectionAndAddend.try_emplace(2086      {{d.section, d.value}, a}, nullptr);2087  if (isNew)2088    it->second = addLandingPadThunk(ctx, d, a);2089  return {it->second.get(), isNew};2090}2091 2092// Return true if the relocation target is an in range Thunk.2093// Return false if the relocation is not to a Thunk. If the relocation target2094// was originally to a Thunk, but is no longer in range we revert the2095// relocation back to its original non-Thunk target.2096bool ThunkCreator::normalizeExistingThunk(Relocation &rel, uint64_t src) {2097  if (Thunk *t = thunks.lookup(rel.sym)) {2098    if (ctx.target->inBranchRange(rel.type, src,2099                                  rel.sym->getVA(ctx, rel.addend)))2100      return true;2101    rel.sym = &t->destination;2102    rel.addend = t->addend;2103    if (rel.sym->isInPlt(ctx))2104      rel.expr = toPlt(rel.expr);2105  }2106  return false;2107}2108 2109// When indirect branches are restricted, such as AArch64 BTI Thunks may need2110// to target a linker generated landing pad instead of the target. This needs2111// to be done once per pass as the need for a BTI thunk is dependent whether2112// a thunk is short or long. We iterate over all the thunks to make sure we2113// catch thunks that have been created but are no longer live. Non-live thunks2114// are not reachable via normalizeExistingThunk() but are still written.2115bool ThunkCreator::addSyntheticLandingPads() {2116  bool addressesChanged = false;2117  for (Thunk *t : allThunks) {2118    if (!t->needsSyntheticLandingPad())2119      continue;2120    Thunk *lpt;2121    bool isNew;2122    auto &dr = cast<Defined>(t->destination);2123    std::tie(lpt, isNew) = getSyntheticLandingPad(dr, t->addend);2124    if (isNew) {2125      addressesChanged = true;2126      getISThunkSec(cast<InputSection>(dr.section))->addThunk(lpt);2127    }2128    t->landingPad = lpt->getThunkTargetSym();2129  }2130  return addressesChanged;2131}2132 2133// Process all relocations from the InputSections that have been assigned2134// to InputSectionDescriptions and redirect through Thunks if needed. The2135// function should be called iteratively until it returns false.2136//2137// PreConditions:2138// All InputSections that may need a Thunk are reachable from2139// OutputSectionCommands.2140//2141// All OutputSections have an address and all InputSections have an offset2142// within the OutputSection.2143//2144// The offsets between caller (relocation place) and callee2145// (relocation target) will not be modified outside of createThunks().2146//2147// PostConditions:2148// If return value is true then ThunkSections have been inserted into2149// OutputSections. All relocations that needed a Thunk based on the information2150// available to createThunks() on entry have been redirected to a Thunk. Note2151// that adding Thunks changes offsets between caller and callee so more Thunks2152// may be required.2153//2154// If return value is false then no more Thunks are needed, and createThunks has2155// made no changes. If the target requires range extension thunks, currently2156// ARM, then any future change in offset between caller and callee risks a2157// relocation out of range error.2158bool ThunkCreator::createThunks(uint32_t pass,2159                                ArrayRef<OutputSection *> outputSections) {2160  this->pass = pass;2161  bool addressesChanged = false;2162 2163  if (pass == 0 && ctx.target->getThunkSectionSpacing())2164    createInitialThunkSections(outputSections);2165 2166  if (ctx.arg.emachine == EM_AARCH64)2167    addressesChanged = addSyntheticLandingPads();2168 2169  // Create all the Thunks and insert them into synthetic ThunkSections. The2170  // ThunkSections are later inserted back into InputSectionDescriptions.2171  // We separate the creation of ThunkSections from the insertion of the2172  // ThunkSections as ThunkSections are not always inserted into the same2173  // InputSectionDescription as the caller.2174  forEachInputSectionDescription(2175      outputSections, [&](OutputSection *os, InputSectionDescription *isd) {2176        for (InputSection *isec : isd->sections)2177          for (Relocation &rel : isec->relocs()) {2178            uint64_t src = isec->getVA(rel.offset);2179 2180            // If we are a relocation to an existing Thunk, check if it is2181            // still in range. If not then Rel will be altered to point to its2182            // original target so another Thunk can be generated.2183            if (pass > 0 && normalizeExistingThunk(rel, src))2184              continue;2185 2186            if (!ctx.target->needsThunk(rel.expr, rel.type, isec->file, src,2187                                        *rel.sym, rel.addend))2188              continue;2189 2190            Thunk *t;2191            bool isNew;2192            std::tie(t, isNew) = getThunk(isec, rel, src);2193 2194            if (isNew) {2195              // Find or create a ThunkSection for the new Thunk2196              ThunkSection *ts;2197              if (auto *tis = t->getTargetInputSection())2198                ts = getISThunkSec(tis);2199              else2200                ts = getISDThunkSec(os, isec, isd, rel, src);2201              ts->addThunk(t);2202              thunks[t->getThunkTargetSym()] = t;2203              allThunks.push_back(t);2204            }2205 2206            // Redirect relocation to Thunk, we never go via the PLT to a Thunk2207            rel.sym = t->getThunkTargetSym();2208            rel.expr = fromPlt(rel.expr);2209 2210            // On AArch64 and PPC, a jump/call relocation may be encoded as2211            // STT_SECTION + non-zero addend, clear the addend after2212            // redirection.2213            if (ctx.arg.emachine != EM_MIPS)2214              rel.addend = -getPCBias(ctx, *isec, rel);2215          }2216 2217        for (auto &p : isd->thunkSections)2218          addressesChanged |= p.first->assignOffsets();2219      });2220 2221  for (auto &p : thunkedSections)2222    addressesChanged |= p.second->assignOffsets();2223 2224  // Merge all created synthetic ThunkSections back into OutputSection2225  mergeThunks(outputSections);2226  return addressesChanged;2227}2228 2229// The following aid in the conversion of call x@GDPLT to call __tls_get_addr2230// hexagonNeedsTLSSymbol scans for relocations would require a call to2231// __tls_get_addr.2232// hexagonTLSSymbolUpdate rebinds the relocation to __tls_get_addr.2233bool elf::hexagonNeedsTLSSymbol(ArrayRef<OutputSection *> outputSections) {2234  bool needTlsSymbol = false;2235  forEachInputSectionDescription(2236      outputSections, [&](OutputSection *os, InputSectionDescription *isd) {2237        for (InputSection *isec : isd->sections)2238          for (Relocation &rel : isec->relocs())2239            if (rel.sym->type == llvm::ELF::STT_TLS && rel.expr == R_PLT_PC) {2240              needTlsSymbol = true;2241              return;2242            }2243      });2244  return needTlsSymbol;2245}2246 2247void elf::hexagonTLSSymbolUpdate(Ctx &ctx) {2248  Symbol *sym = ctx.symtab->find("__tls_get_addr");2249  if (!sym)2250    return;2251  bool needEntry = true;2252  forEachInputSectionDescription(2253      ctx.outputSections, [&](OutputSection *os, InputSectionDescription *isd) {2254        for (InputSection *isec : isd->sections)2255          for (Relocation &rel : isec->relocs())2256            if (rel.sym->type == llvm::ELF::STT_TLS && rel.expr == R_PLT_PC) {2257              if (needEntry) {2258                if (sym->auxIdx == 0)2259                  sym->allocateAux(ctx);2260                addPltEntry(ctx, *ctx.in.plt, *ctx.in.gotPlt, *ctx.in.relaPlt,2261                            ctx.target->pltRel, *sym);2262                needEntry = false;2263              }2264              rel.sym = sym;2265            }2266      });2267}2268 2269static bool matchesRefTo(const NoCrossRefCommand &cmd, StringRef osec) {2270  if (cmd.toFirst)2271    return cmd.outputSections[0] == osec;2272  return llvm::is_contained(cmd.outputSections, osec);2273}2274 2275template <class ELFT, class Rels>2276static void scanCrossRefs(Ctx &ctx, const NoCrossRefCommand &cmd,2277                          OutputSection *osec, InputSection *sec, Rels rels) {2278  for (const auto &r : rels) {2279    Symbol &sym = sec->file->getSymbol(r.getSymbol(ctx.arg.isMips64EL));2280    // A legal cross-reference is when the destination output section is2281    // nullptr, osec for a self-reference, or a section that is described by the2282    // NOCROSSREFS/NOCROSSREFS_TO command.2283    auto *dstOsec = sym.getOutputSection();2284    if (!dstOsec || dstOsec == osec || !matchesRefTo(cmd, dstOsec->name))2285      continue;2286 2287    std::string toSymName;2288    if (!sym.isSection())2289      toSymName = toStr(ctx, sym);2290    else if (auto *d = dyn_cast<Defined>(&sym))2291      toSymName = d->section->name;2292    Err(ctx) << sec->getLocation(r.r_offset)2293             << ": prohibited cross reference from '" << osec->name << "' to '"2294             << toSymName << "' in '" << dstOsec->name << "'";2295  }2296}2297 2298// For each output section described by at least one NOCROSSREFS(_TO) command,2299// scan relocations from its input sections for prohibited cross references.2300template <class ELFT> void elf::checkNoCrossRefs(Ctx &ctx) {2301  for (OutputSection *osec : ctx.outputSections) {2302    for (const NoCrossRefCommand &noxref : ctx.script->noCrossRefs) {2303      if (!llvm::is_contained(noxref.outputSections, osec->name) ||2304          (noxref.toFirst && noxref.outputSections[0] == osec->name))2305        continue;2306      for (SectionCommand *cmd : osec->commands) {2307        auto *isd = dyn_cast<InputSectionDescription>(cmd);2308        if (!isd)2309          continue;2310        parallelForEach(isd->sections, [&](InputSection *sec) {2311          invokeOnRelocs(*sec, scanCrossRefs<ELFT>, ctx, noxref, osec, sec);2312        });2313      }2314    }2315  }2316}2317 2318template void elf::scanRelocations<ELF32LE>(Ctx &);2319template void elf::scanRelocations<ELF32BE>(Ctx &);2320template void elf::scanRelocations<ELF64LE>(Ctx &);2321template void elf::scanRelocations<ELF64BE>(Ctx &);2322 2323template void elf::checkNoCrossRefs<ELF32LE>(Ctx &);2324template void elf::checkNoCrossRefs<ELF32BE>(Ctx &);2325template void elf::checkNoCrossRefs<ELF64LE>(Ctx &);2326template void elf::checkNoCrossRefs<ELF64BE>(Ctx &);2327