brintos

brintos / llvm-project-archived public Read only

0
0
Text · 10.6 KiB · deb623b Raw
256 lines · c
1//===-- RuntimeDyldELF.h - Run-time dynamic linker for MC-JIT ---*- 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// ELF support for MC-JIT runtime dynamic linker.10//11//===----------------------------------------------------------------------===//12 13#ifndef LLVM_LIB_EXECUTIONENGINE_RUNTIMEDYLD_RUNTIMEDYLDELF_H14#define LLVM_LIB_EXECUTIONENGINE_RUNTIMEDYLD_RUNTIMEDYLDELF_H15 16#include "RuntimeDyldImpl.h"17#include "llvm/ADT/DenseMap.h"18 19namespace llvm {20namespace object {21class ELFObjectFileBase;22}23 24class RuntimeDyldELF : public RuntimeDyldImpl {25 26  void resolveRelocation(const SectionEntry &Section, uint64_t Offset,27                         uint64_t Value, uint32_t Type, int64_t Addend,28                         uint64_t SymOffset = 0, SID SectionID = 0);29 30  void resolveX86_64Relocation(const SectionEntry &Section, uint64_t Offset,31                               uint64_t Value, uint32_t Type, int64_t Addend,32                               uint64_t SymOffset);33 34  void resolveX86Relocation(const SectionEntry &Section, uint64_t Offset,35                            uint32_t Value, uint32_t Type, int32_t Addend);36 37  void resolveAArch64Relocation(const SectionEntry &Section, uint64_t Offset,38                                uint64_t Value, uint32_t Type, int64_t Addend);39 40  bool resolveAArch64ShortBranch(unsigned SectionID, relocation_iterator RelI,41                                 const RelocationValueRef &Value);42 43  void resolveAArch64Branch(unsigned SectionID, const RelocationValueRef &Value,44                            relocation_iterator RelI, StubMap &Stubs);45 46  void resolveARMRelocation(const SectionEntry &Section, uint64_t Offset,47                            uint32_t Value, uint32_t Type, int32_t Addend);48 49  void resolveLoongArch64Relocation(const SectionEntry &Section,50                                    uint64_t Offset, uint64_t Value,51                                    uint32_t Type, int64_t Addend);52 53  bool resolveLoongArch64ShortBranch(unsigned SectionID,54                                     relocation_iterator RelI,55                                     const RelocationValueRef &Value);56 57  void resolveLoongArch64Branch(unsigned SectionID,58                                const RelocationValueRef &Value,59                                relocation_iterator RelI, StubMap &Stubs);60 61  void resolvePPC32Relocation(const SectionEntry &Section, uint64_t Offset,62                              uint64_t Value, uint32_t Type, int64_t Addend);63 64  void resolvePPC64Relocation(const SectionEntry &Section, uint64_t Offset,65                              uint64_t Value, uint32_t Type, int64_t Addend);66 67  void resolveSystemZRelocation(const SectionEntry &Section, uint64_t Offset,68                                uint64_t Value, uint32_t Type, int64_t Addend);69 70  void resolveBPFRelocation(const SectionEntry &Section, uint64_t Offset,71                            uint64_t Value, uint32_t Type, int64_t Addend);72 73  void resolveRISCVRelocation(const SectionEntry &Section, uint64_t Offset,74                              uint64_t Value, uint32_t Type, int64_t Addend,75                              SID SectionID);76 77  unsigned getMaxStubSize() const override {78    if (Arch == Triple::aarch64 || Arch == Triple::aarch64_be)79      return 20; // movz; movk; movk; movk; br80    if (Arch == Triple::arm || Arch == Triple::thumb)81      return 8; // 32-bit instruction and 32-bit address82    else if (IsMipsO32ABI || IsMipsN32ABI)83      return 16;84    else if (IsMipsN64ABI)85      return 32;86    if (Arch == Triple::loongarch64)87      return 20; // lu12i.w; ori; lu32i.d; lu52i.d; jr88    else if (Arch == Triple::ppc64 || Arch == Triple::ppc64le)89      return 44;90    else if (Arch == Triple::x86_64)91      return 6; // 2-byte jmp instruction + 32-bit relative address92    else if (Arch == Triple::systemz)93      return 16;94    else95      return 0;96  }97 98  Align getStubAlignment() override {99    if (Arch == Triple::systemz)100      return Align(8);101    else102      return Align(1);103  }104 105  void setMipsABI(const ObjectFile &Obj) override;106 107  Error findPPC64TOCSection(const object::ELFObjectFileBase &Obj,108                            ObjSectionToIDMap &LocalSections,109                            RelocationValueRef &Rel);110  Error findOPDEntrySection(const object::ELFObjectFileBase &Obj,111                            ObjSectionToIDMap &LocalSections,112                            RelocationValueRef &Rel);113 114protected:115  size_t getGOTEntrySize() override;116 117private:118  SectionEntry &getSection(unsigned SectionID) { return Sections[SectionID]; }119 120  // Allocate no GOT entries for use in the given section.121  uint64_t allocateGOTEntries(unsigned no);122 123  // Find GOT entry corresponding to relocation or create new one.124  uint64_t findOrAllocGOTEntry(const RelocationValueRef &Value,125                               unsigned GOTRelType);126 127  // Resolve the relative address of GOTOffset in Section ID and place128  // it at the given Offset129  void resolveGOTOffsetRelocation(unsigned SectionID, uint64_t Offset,130                                  uint64_t GOTOffset, uint32_t Type);131 132  // For a GOT entry referenced from SectionID, compute a relocation entry133  // that will place the final resolved value in the GOT slot134  RelocationEntry computeGOTOffsetRE(uint64_t GOTOffset, uint64_t SymbolOffset,135                                     unsigned Type);136 137  // Compute the address in memory where we can find the placeholder138  void *computePlaceholderAddress(unsigned SectionID, uint64_t Offset) const;139 140  // Split out common case for creating the RelocationEntry for when the141  // relocation requires no particular advanced processing.142  void processSimpleRelocation(unsigned SectionID, uint64_t Offset, unsigned RelType, RelocationValueRef Value);143 144  // Return matching *LO16 relocation (Mips specific)145  uint32_t getMatchingLoRelocation(uint32_t RelType,146                                   bool IsLocal = false) const;147 148  // The tentative ID for the GOT section149  unsigned GOTSectionID;150 151  // Records the current number of allocated slots in the GOT152  // (This would be equivalent to GOTEntries.size() were it not for relocations153  // that consume more than one slot)154  unsigned CurrentGOTIndex;155 156protected:157  // A map from section to a GOT section that has entries for section's GOT158  // relocations. (Mips64 specific)159  DenseMap<SID, SID> SectionToGOTMap;160 161private:162  // A map to avoid duplicate got entries (Mips64 specific)163  StringMap<uint64_t> GOTSymbolOffsets;164 165  // *HI16 relocations will be added for resolving when we find matching166  // *LO16 part. (Mips specific)167  //168  // *HI20 relocations will be added for resolving when we find matching169  // *LO12 part. (RISC-V specific)170  SmallVector<std::pair<RelocationValueRef, RelocationEntry>, 8> PendingRelocs;171 172  // When a module is loaded we save the SectionID of the EH frame section173  // in a table until we receive a request to register all unregistered174  // EH frame sections with the memory manager.175  SmallVector<SID, 2> UnregisteredEHFrameSections;176 177  // Map between GOT relocation value and corresponding GOT offset178  std::map<RelocationValueRef, uint64_t> GOTOffsetMap;179 180  /// The ID of the current IFunc stub section181  unsigned IFuncStubSectionID = 0;182  /// The current offset into the IFunc stub section183  uint64_t IFuncStubOffset = 0;184 185  /// A IFunc stub and its original symbol186  struct IFuncStub {187    /// The offset of this stub in the IFunc stub section188    uint64_t StubOffset;189    /// The symbol table entry of the original symbol190    SymbolTableEntry OriginalSymbol;191  };192 193  /// The IFunc stubs194  SmallVector<IFuncStub, 2> IFuncStubs;195 196  /// Create the code for the IFunc resolver at the given address. This code197  /// works together with the stubs created in createIFuncStub() to call the198  /// resolver function and then jump to the real function address.199  /// It must not be larger than 64B.200  void createIFuncResolver(uint8_t *Addr) const;201  /// Create the code for an IFunc stub for the IFunc that is defined in202  /// section IFuncSectionID at offset IFuncOffset. The IFunc resolver created203  /// by createIFuncResolver() is defined in the section IFuncStubSectionID at204  /// offset IFuncResolverOffset. The code should be written into the section205  /// with the id IFuncStubSectionID at the offset IFuncStubOffset.206  void createIFuncStub(unsigned IFuncStubSectionID,207                       uint64_t IFuncResolverOffset, uint64_t IFuncStubOffset,208                       unsigned IFuncSectionID, uint64_t IFuncOffset);209  /// Return the maximum size of a stub created by createIFuncStub()210  unsigned getMaxIFuncStubSize() const;211 212  void processNewSymbol(const SymbolRef &ObjSymbol,213                        SymbolTableEntry &Entry) override;214  bool relocationNeedsGot(const RelocationRef &R) const override;215  bool relocationNeedsStub(const RelocationRef &R) const override;216 217  // Process a GOTTPOFF TLS relocation for x86-64218  // NOLINTNEXTLINE(readability-identifier-naming)219  void processX86_64GOTTPOFFRelocation(unsigned SectionID, uint64_t Offset,220                                       RelocationValueRef Value,221                                       int64_t Addend);222  // Process a TLSLD/TLSGD relocation for x86-64223  // NOLINTNEXTLINE(readability-identifier-naming)224  void processX86_64TLSRelocation(unsigned SectionID, uint64_t Offset,225                                  uint64_t RelType, RelocationValueRef Value,226                                  int64_t Addend,227                                  const RelocationRef &GetAddrRelocation);228 229public:230  RuntimeDyldELF(RuntimeDyld::MemoryManager &MemMgr,231                 JITSymbolResolver &Resolver);232  ~RuntimeDyldELF() override;233 234  static std::unique_ptr<RuntimeDyldELF>235  create(Triple::ArchType Arch, RuntimeDyld::MemoryManager &MemMgr,236         JITSymbolResolver &Resolver);237 238  std::unique_ptr<RuntimeDyld::LoadedObjectInfo>239  loadObject(const object::ObjectFile &O) override;240 241  void resolveRelocation(const RelocationEntry &RE, uint64_t Value) override;242  Expected<relocation_iterator>243  processRelocationRef(unsigned SectionID, relocation_iterator RelI,244                       const ObjectFile &Obj,245                       ObjSectionToIDMap &ObjSectionToID,246                       StubMap &Stubs) override;247  bool isCompatibleFile(const object::ObjectFile &Obj) const override;248  void registerEHFrames() override;249  Error finalizeLoad(const ObjectFile &Obj,250                     ObjSectionToIDMap &SectionMap) override;251};252 253} // end namespace llvm254 255#endif // LLVM_LIB_EXECUTIONENGINE_RUNTIMEDYLD_RUNTIMEDYLDELF_H256