brintos

brintos / llvm-project-archived public Read only

0
0
Text · 11.9 KiB · 86ca298 Raw
385 lines · c
1//===- Relocations.h -------------------------------------------*- C++ -*-===//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#ifndef LLD_ELF_RELOCATIONS_H10#define LLD_ELF_RELOCATIONS_H11 12#include "lld/Common/LLVM.h"13#include "llvm/ADT/DenseMap.h"14#include "llvm/ADT/STLExtras.h"15#include "llvm/Object/ELFTypes.h"16#include <vector>17 18namespace lld::elf {19struct Ctx;20struct ELFSyncStream;21class Defined;22class Undefined;23class Symbol;24class InputSection;25class InputSectionBase;26class OutputSection;27class RelocationBaseSection;28class SectionBase;29 30// Represents a relocation type, such as R_X86_64_PC32 or R_ARM_THM_CALL.31struct RelType {32  uint32_t v = 0;33  /*implicit*/ constexpr RelType(uint32_t v = 0) : v(v) {}34  /*implicit*/ operator uint32_t() const { return v; }35};36 37using JumpModType = uint32_t;38 39// List of target-independent relocation types. Relocations read40// from files are converted to these types so that the main code41// doesn't have to know about architecture-specific details.42enum RelExpr {43  R_ABS,44  R_ADDEND,45  R_DTPREL,46  R_GOT,47  R_GOT_OFF,48  R_GOT_PC,49  R_GOTONLY_PC,50  R_GOTPLTONLY_PC,51  R_GOTPLT,52  R_GOTPLTREL,53  R_GOTREL,54  R_GOTPLT_GOTREL,55  R_GOTPLT_PC,56  R_NONE,57  R_PC,58  R_PLT,59  R_PLT_PC,60  R_PLT_GOTPLT,61  R_PLT_GOTREL,62  R_RELAX_HINT,63  R_RELAX_GOT_PC,64  R_RELAX_GOT_PC_NOPIC,65  R_RELAX_TLS_GD_TO_IE,66  R_RELAX_TLS_GD_TO_IE_ABS,67  R_RELAX_TLS_GD_TO_IE_GOT_OFF,68  R_RELAX_TLS_GD_TO_IE_GOTPLT,69  R_RELAX_TLS_GD_TO_LE,70  R_RELAX_TLS_GD_TO_LE_NEG,71  R_RELAX_TLS_IE_TO_LE,72  R_RELAX_TLS_LD_TO_LE,73  R_RELAX_TLS_LD_TO_LE_ABS,74  R_SIZE,75  R_TPREL,76  R_TPREL_NEG,77  R_TLSDESC,78  R_TLSDESC_CALL,79  R_TLSDESC_PC,80  R_TLSDESC_GOTPLT,81  R_TLSGD_GOT,82  R_TLSGD_GOTPLT,83  R_TLSGD_PC,84  R_TLSIE_HINT,85  R_TLSLD_GOT,86  R_TLSLD_GOTPLT,87  R_TLSLD_GOT_OFF,88  R_TLSLD_HINT,89  R_TLSLD_PC,90 91  // The following is abstract relocation types used for only one target.92  //93  // Even though RelExpr is intended to be a target-neutral representation94  // of a relocation type, there are some relocations whose semantics are95  // unique to a target. Such relocation are marked with RE_<TARGET_NAME>.96  RE_AARCH64_GOT_PAGE_PC,97  RE_AARCH64_AUTH_GOT_PAGE_PC,98  RE_AARCH64_GOT_PAGE,99  RE_AARCH64_AUTH_GOT,100  RE_AARCH64_AUTH_GOT_PC,101  RE_AARCH64_PAGE_PC,102  RE_AARCH64_RELAX_TLS_GD_TO_IE_PAGE_PC,103  RE_AARCH64_TLSDESC_PAGE,104  RE_AARCH64_AUTH_TLSDESC_PAGE,105  RE_AARCH64_AUTH_TLSDESC,106  RE_AARCH64_AUTH,107  RE_ARM_PCA,108  RE_ARM_SBREL,109  RE_MIPS_GOTREL,110  RE_MIPS_GOT_GP,111  RE_MIPS_GOT_GP_PC,112  RE_MIPS_GOT_LOCAL_PAGE,113  RE_MIPS_GOT_OFF,114  RE_MIPS_GOT_OFF32,115  RE_MIPS_OSEC_LOCAL_PAGE,116  RE_MIPS_TLSGD,117  RE_MIPS_TLSLD,118  RE_PPC32_PLTREL,119  RE_PPC64_CALL,120  RE_PPC64_CALL_PLT,121  RE_PPC64_RELAX_TOC,122  RE_PPC64_TOCBASE,123  RE_PPC64_RELAX_GOT_PC,124  RE_RISCV_ADD,125  RE_RISCV_LEB128,126  RE_RISCV_PC_INDIRECT,127  // Same as R_PC but with page-aligned semantics.128  RE_LOONGARCH_PAGE_PC,129  // Same as R_PLT_PC but with page-aligned semantics.130  RE_LOONGARCH_PLT_PAGE_PC,131  // In addition to having page-aligned semantics, LoongArch GOT relocs are132  // also reused for TLS, making the semantics differ from other architectures.133  RE_LOONGARCH_GOT,134  RE_LOONGARCH_GOT_PAGE_PC,135  RE_LOONGARCH_TLSGD_PAGE_PC,136  RE_LOONGARCH_TLSDESC_PAGE_PC,137  RE_LOONGARCH_RELAX_TLS_GD_TO_IE_PAGE_PC,138};139 140// Architecture-neutral representation of relocation.141struct Relocation {142  RelExpr expr;143  RelType type;144  uint64_t offset;145  int64_t addend;146  Symbol *sym;147};148 149// Manipulate jump instructions with these modifiers.  These are used to relax150// jump instruction opcodes at basic block boundaries and are particularly151// useful when basic block sections are enabled.152struct JumpInstrMod {153  uint64_t offset;154  JumpModType original;155  unsigned size;156};157 158void printLocation(ELFSyncStream &s, InputSectionBase &sec, const Symbol &sym,159                   uint64_t off);160 161// This function writes undefined symbol diagnostics to an internal buffer.162// Call reportUndefinedSymbols() after calling scanRelocations() to emit163// the diagnostics.164template <class ELFT> void scanRelocations(Ctx &ctx);165template <class ELFT> void checkNoCrossRefs(Ctx &ctx);166void reportUndefinedSymbols(Ctx &);167bool maybeReportUndefined(Ctx &, Undefined &sym, InputSectionBase &sec,168                          uint64_t offset);169void postScanRelocations(Ctx &ctx);170void addGotEntry(Ctx &ctx, Symbol &sym);171 172void hexagonTLSSymbolUpdate(Ctx &ctx);173bool hexagonNeedsTLSSymbol(ArrayRef<OutputSection *> outputSections);174 175bool isAbsolute(const Symbol &sym);176 177class ThunkSection;178class Thunk;179class InputSectionDescription;180 181class ThunkCreator {182public:183  // Thunk may be incomplete. Avoid inline ctor/dtor.184  ThunkCreator(Ctx &ctx);185  ~ThunkCreator();186  // Return true if Thunks have been added to OutputSections187  bool createThunks(uint32_t pass, ArrayRef<OutputSection *> outputSections);188 189private:190  void mergeThunks(ArrayRef<OutputSection *> outputSections);191 192  ThunkSection *getISDThunkSec(OutputSection *os, InputSection *isec,193                               InputSectionDescription *isd,194                               const Relocation &rel, uint64_t src);195 196  ThunkSection *getISThunkSec(InputSection *isec);197 198  void createInitialThunkSections(ArrayRef<OutputSection *> outputSections);199 200  std::pair<Thunk *, bool> getThunk(InputSection *isec, Relocation &rel,201                                    uint64_t src);202 203  std::pair<Thunk *, bool> getSyntheticLandingPad(Defined &d, int64_t a);204 205  ThunkSection *addThunkSection(OutputSection *os, InputSectionDescription *,206                                uint64_t off);207 208  bool normalizeExistingThunk(Relocation &rel, uint64_t src);209 210  bool addSyntheticLandingPads();211 212  Ctx &ctx;213 214  // Record all the available Thunks for a (Symbol, addend) pair, where Symbol215  // is represented as a (section, offset) pair. There may be multiple216  // relocations sharing the same (section, offset + addend) pair. We may revert217  // a relocation back to its original non-Thunk target, and restore the218  // original addend, so we cannot fold offset + addend. A nested pair is used219  // because DenseMapInfo is not specialized for std::tuple.220  llvm::DenseMap<std::pair<std::pair<SectionBase *, uint64_t>, int64_t>,221                 SmallVector<std::unique_ptr<Thunk>, 0>>222      thunkedSymbolsBySectionAndAddend;223  llvm::DenseMap<std::pair<Symbol *, int64_t>,224                 SmallVector<std::unique_ptr<Thunk>, 0>>225      thunkedSymbols;226 227  // Find a Thunk from the Thunks symbol definition, we can use this to find228  // the Thunk from a relocation to the Thunks symbol definition.229  llvm::DenseMap<Symbol *, Thunk *> thunks;230 231  // Track InputSections that have an inline ThunkSection placed in front232  // an inline ThunkSection may have control fall through to the section below233  // so we need to make sure that there is only one of them.234  // The Mips LA25 Thunk is an example of an inline ThunkSection, as is235  // the AArch64BTLandingPadThunk.236  llvm::DenseMap<InputSection *, ThunkSection *> thunkedSections;237 238  // Record landing pads, generated for a section + offset destination.239  // Landling pads are alternative entry points for destinations that need240  // to be reached via thunks that use indirect branches. A destination241  // needs at most one landing pad as that can be reused by all callers.242  llvm::DenseMap<std::pair<std::pair<SectionBase *, uint64_t>, int64_t>,243                 std::unique_ptr<Thunk>>244      landingPadsBySectionAndAddend;245 246  // All the nonLandingPad thunks that have been created, in order of creation.247  std::vector<Thunk *> allThunks;248 249  // The number of completed passes of createThunks this permits us250  // to do one time initialization on Pass 0 and put a limit on the251  // number of times it can be called to prevent infinite loops.252  uint32_t pass = 0;253};254 255// Decode LEB128 without error checking. Only used by performance critical code256// like RelocsCrel.257inline uint64_t readLEB128(const uint8_t *&p, uint64_t leb) {258  uint64_t acc = 0, shift = 0, byte;259  do {260    byte = *p++;261    acc |= (byte - 128 * (byte >= leb)) << shift;262    shift += 7;263  } while (byte >= 128);264  return acc;265}266inline uint64_t readULEB128(const uint8_t *&p) { return readLEB128(p, 128); }267inline int64_t readSLEB128(const uint8_t *&p) { return readLEB128(p, 64); }268 269// This class implements a CREL iterator that does not allocate extra memory.270template <bool is64> struct RelocsCrel {271  using uint = std::conditional_t<is64, uint64_t, uint32_t>;272  struct const_iterator {273    using iterator_category = std::forward_iterator_tag;274    using value_type = llvm::object::Elf_Crel_Impl<is64>;275    using difference_type = ptrdiff_t;276    using pointer = value_type *;277    using reference = const value_type &;278    uint32_t count;279    uint8_t flagBits, shift;280    const uint8_t *p;281    llvm::object::Elf_Crel_Impl<is64> crel{};282    const_iterator(size_t hdr, const uint8_t *p)283        : count(hdr / 8), flagBits(hdr & 4 ? 3 : 2), shift(hdr % 4), p(p) {284      if (count)285        step();286    }287    void step() {288      // See object::decodeCrel.289      const uint8_t b = *p++;290      crel.r_offset += b >> flagBits << shift;291      if (b >= 0x80)292        crel.r_offset +=293            ((readULEB128(p) << (7 - flagBits)) - (0x80 >> flagBits)) << shift;294      if (b & 1)295        crel.r_symidx += readSLEB128(p);296      if (b & 2)297        crel.r_type += readSLEB128(p);298      if (b & 4 && flagBits == 3)299        crel.r_addend += static_cast<uint>(readSLEB128(p));300    }301    llvm::object::Elf_Crel_Impl<is64> operator*() const { return crel; };302    const llvm::object::Elf_Crel_Impl<is64> *operator->() const {303      return &crel;304    }305    // For llvm::enumerate.306    bool operator==(const const_iterator &r) const { return count == r.count; }307    bool operator!=(const const_iterator &r) const { return count != r.count; }308    const_iterator &operator++() {309      if (--count)310        step();311      return *this;312    }313    // For RelocationScanner::scanOne.314    void operator+=(size_t n) {315      for (; n; --n)316        operator++();317    }318  };319 320  size_t hdr = 0;321  const uint8_t *p = nullptr;322 323  constexpr RelocsCrel() = default;324  RelocsCrel(const uint8_t *p) : hdr(readULEB128(p)) { this->p = p; }325  size_t size() const { return hdr / 8; }326  const_iterator begin() const { return {hdr, p}; }327  const_iterator end() const { return {0, nullptr}; }328};329 330template <class RelTy> struct Relocs : ArrayRef<RelTy> {331  Relocs() = default;332  Relocs(ArrayRef<RelTy> a) : ArrayRef<RelTy>(a) {}333};334 335template <bool is64>336struct Relocs<llvm::object::Elf_Crel_Impl<is64>> : RelocsCrel<is64> {337  using RelocsCrel<is64>::RelocsCrel;338};339 340// Return a int64_t to make sure we get the sign extension out of the way as341// early as possible.342template <class ELFT>343static inline int64_t getAddend(const typename ELFT::Rel &rel) {344  return 0;345}346template <class ELFT>347static inline int64_t getAddend(const typename ELFT::Rela &rel) {348  return rel.r_addend;349}350template <class ELFT>351static inline int64_t getAddend(const typename ELFT::Crel &rel) {352  return rel.r_addend;353}354 355template <typename RelTy>356inline Relocs<RelTy> sortRels(Relocs<RelTy> rels,357                              SmallVector<RelTy, 0> &storage) {358  auto cmp = [](const RelTy &a, const RelTy &b) {359    return a.r_offset < b.r_offset;360  };361  if (!llvm::is_sorted(rels, cmp)) {362    storage.assign(rels.begin(), rels.end());363    llvm::stable_sort(storage, cmp);364    rels = Relocs<RelTy>(storage);365  }366  return rels;367}368 369template <bool is64>370inline Relocs<llvm::object::Elf_Crel_Impl<is64>>371sortRels(Relocs<llvm::object::Elf_Crel_Impl<is64>> rels,372         SmallVector<llvm::object::Elf_Crel_Impl<is64>, 0> &storage) {373  return {};374}375 376RelocationBaseSection &getIRelativeSection(Ctx &ctx);377 378// Returns true if Expr refers a GOT entry. Note that this function returns379// false for TLS variables even though they need GOT, because TLS variables uses380// GOT differently than the regular variables.381bool needsGot(RelExpr expr);382} // namespace lld::elf383 384#endif385