brintos

brintos / llvm-project-archived public Read only

0
0
Text · 8.4 KiB · 0e95369 Raw
238 lines · c
1//===-- RuntimeDyldMachOX86_64.h ---- MachO/X86_64 specific code. -*- 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 LLVM_LIB_EXECUTIONENGINE_RUNTIMEDYLD_TARGETS_RUNTIMEDYLDMACHOX86_64_H10#define LLVM_LIB_EXECUTIONENGINE_RUNTIMEDYLD_TARGETS_RUNTIMEDYLDMACHOX86_64_H11 12#include "../RuntimeDyldMachO.h"13 14#define DEBUG_TYPE "dyld"15 16namespace llvm {17 18class RuntimeDyldMachOX86_6419    : public RuntimeDyldMachOCRTPBase<RuntimeDyldMachOX86_64> {20public:21 22  typedef uint64_t TargetPtrT;23 24  RuntimeDyldMachOX86_64(RuntimeDyld::MemoryManager &MM,25                         JITSymbolResolver &Resolver)26      : RuntimeDyldMachOCRTPBase(MM, Resolver) {}27 28  unsigned getMaxStubSize() const override { return 8; }29 30  Align getStubAlignment() override { return Align(8); }31 32  Expected<relocation_iterator>33  processRelocationRef(unsigned SectionID, relocation_iterator RelI,34                       const ObjectFile &BaseObjT,35                       ObjSectionToIDMap &ObjSectionToID,36                       StubMap &Stubs) override {37    const MachOObjectFile &Obj =38      static_cast<const MachOObjectFile &>(BaseObjT);39    MachO::any_relocation_info RelInfo =40        Obj.getRelocation(RelI->getRawDataRefImpl());41    uint32_t RelType = Obj.getAnyRelocationType(RelInfo);42 43    if (RelType == MachO::X86_64_RELOC_SUBTRACTOR)44      return processSubtractRelocation(SectionID, RelI, Obj, ObjSectionToID);45 46    assert(!Obj.isRelocationScattered(RelInfo) &&47           "Scattered relocations not supported on X86_64");48 49    RelocationEntry RE(getRelocationEntry(SectionID, Obj, RelI));50    RE.Addend = memcpyAddend(RE);51    RelocationValueRef Value;52    if (auto ValueOrErr = getRelocationValueRef(Obj, RelI, RE, ObjSectionToID))53      Value = *ValueOrErr;54    else55      return ValueOrErr.takeError();56 57    bool IsExtern = Obj.getPlainRelocationExternal(RelInfo);58    if (!IsExtern && RE.IsPCRel)59      makeValueAddendPCRel(Value, RelI, 1 << RE.Size);60 61    switch (RelType) {62    UNIMPLEMENTED_RELOC(MachO::X86_64_RELOC_TLV);63    default:64      if (RelType > MachO::X86_64_RELOC_TLV)65        return make_error<RuntimeDyldError>(("MachO X86_64 relocation type " +66                                             Twine(RelType) +67                                             " is out of range").str());68      break;69    }70 71    if (RE.RelType == MachO::X86_64_RELOC_GOT ||72        RE.RelType == MachO::X86_64_RELOC_GOT_LOAD)73      processGOTRelocation(RE, Value, Stubs);74    else {75      RE.Addend = Value.Offset;76      if (Value.SymbolName)77        addRelocationForSymbol(RE, Value.SymbolName);78      else79        addRelocationForSection(RE, Value.SectionID);80    }81 82    return ++RelI;83  }84 85  void resolveRelocation(const RelocationEntry &RE, uint64_t Value) override {86    LLVM_DEBUG(dumpRelocationToResolve(RE, Value));87    const SectionEntry &Section = Sections[RE.SectionID];88    uint8_t *LocalAddress = Section.getAddressWithOffset(RE.Offset);89 90    // If the relocation is PC-relative, the value to be encoded is the91    // pointer difference.92    if (RE.IsPCRel) {93      // FIXME: It seems this value needs to be adjusted by 4 for an effective94      // PC address. Is that expected? Only for branches, perhaps?95      uint64_t FinalAddress = Section.getLoadAddressWithOffset(RE.Offset);96      Value -= FinalAddress + 4;97    }98 99    switch (RE.RelType) {100    default:101      llvm_unreachable("Invalid relocation type!");102    case MachO::X86_64_RELOC_SIGNED_1:103    case MachO::X86_64_RELOC_SIGNED_2:104    case MachO::X86_64_RELOC_SIGNED_4:105    case MachO::X86_64_RELOC_SIGNED:106    case MachO::X86_64_RELOC_UNSIGNED:107    case MachO::X86_64_RELOC_BRANCH:108      writeBytesUnaligned(Value + RE.Addend, LocalAddress, 1 << RE.Size);109      break;110    case MachO::X86_64_RELOC_SUBTRACTOR: {111      uint64_t SectionABase = Sections[RE.Sections.SectionA].getLoadAddress();112      uint64_t SectionBBase = Sections[RE.Sections.SectionB].getLoadAddress();113      assert((Value == SectionABase || Value == SectionBBase) &&114             "Unexpected SUBTRACTOR relocation value.");115      Value = SectionABase - SectionBBase + RE.Addend;116      writeBytesUnaligned(Value, LocalAddress, 1 << RE.Size);117      break;118    }119    }120  }121 122  Error finalizeSection(const ObjectFile &Obj, unsigned SectionID,123                        const SectionRef &Section) {124    return Error::success();125  }126 127private:128  void processGOTRelocation(const RelocationEntry &RE,129                            RelocationValueRef &Value, StubMap &Stubs) {130    SectionEntry &Section = Sections[RE.SectionID];131    assert(RE.IsPCRel);132    assert(RE.Size == 2);133    Value.Offset -= RE.Addend;134    RuntimeDyldMachO::StubMap::const_iterator i = Stubs.find(Value);135    uint8_t *Addr;136    if (i != Stubs.end()) {137      Addr = Section.getAddressWithOffset(i->second);138    } else {139      Stubs[Value] = Section.getStubOffset();140      uint8_t *GOTEntry = Section.getAddressWithOffset(Section.getStubOffset());141      RelocationEntry GOTRE(RE.SectionID, Section.getStubOffset(),142                            MachO::X86_64_RELOC_UNSIGNED, Value.Offset, false,143                            3);144      if (Value.SymbolName)145        addRelocationForSymbol(GOTRE, Value.SymbolName);146      else147        addRelocationForSection(GOTRE, Value.SectionID);148      Section.advanceStubOffset(8);149      Addr = GOTEntry;150    }151    RelocationEntry TargetRE(RE.SectionID, RE.Offset,152                             MachO::X86_64_RELOC_UNSIGNED, RE.Addend, true, 2);153    resolveRelocation(TargetRE, (uint64_t)Addr);154  }155 156  Expected<relocation_iterator>157  processSubtractRelocation(unsigned SectionID, relocation_iterator RelI,158                            const MachOObjectFile &BaseObj,159                            ObjSectionToIDMap &ObjSectionToID) {160    const MachOObjectFile &Obj = BaseObj;161    MachO::any_relocation_info RE =162        Obj.getRelocation(RelI->getRawDataRefImpl());163 164    unsigned Size = Obj.getAnyRelocationLength(RE);165    uint64_t Offset = RelI->getOffset();166    uint8_t *LocalAddress = Sections[SectionID].getAddressWithOffset(Offset);167    unsigned NumBytes = 1 << Size;168    int64_t Addend =169      SignExtend64(readBytesUnaligned(LocalAddress, NumBytes), NumBytes * 8);170 171    unsigned SectionBID = ~0U;172    uint64_t SectionBOffset = 0;173 174    MachO::any_relocation_info RelInfo =175      Obj.getRelocation(RelI->getRawDataRefImpl());176 177    bool AIsExternal = BaseObj.getPlainRelocationExternal(RelInfo);178 179    if (AIsExternal) {180      Expected<StringRef> SubtrahendNameOrErr = RelI->getSymbol()->getName();181      if (!SubtrahendNameOrErr)182        return SubtrahendNameOrErr.takeError();183      auto SubtrahendI = GlobalSymbolTable.find(*SubtrahendNameOrErr);184      SectionBID = SubtrahendI->second.getSectionID();185      SectionBOffset = SubtrahendI->second.getOffset();186    } else {187      SectionRef SecB = Obj.getAnyRelocationSection(RelInfo);188      bool IsCode = SecB.isText();189      Expected<unsigned> SectionBIDOrErr =190        findOrEmitSection(Obj, SecB, IsCode, ObjSectionToID);191      if (!SectionBIDOrErr)192        return SectionBIDOrErr.takeError();193      SectionBID = *SectionBIDOrErr;194      Addend += SecB.getAddress();195    }196 197    ++RelI;198 199    unsigned SectionAID = ~0U;200    uint64_t SectionAOffset = 0;201 202    RelInfo = Obj.getRelocation(RelI->getRawDataRefImpl());203 204    bool BIsExternal = BaseObj.getPlainRelocationExternal(RelInfo);205    if (BIsExternal) {206      Expected<StringRef> MinuendNameOrErr = RelI->getSymbol()->getName();207      if (!MinuendNameOrErr)208        return MinuendNameOrErr.takeError();209      auto MinuendI = GlobalSymbolTable.find(*MinuendNameOrErr);210      SectionAID = MinuendI->second.getSectionID();211      SectionAOffset = MinuendI->second.getOffset();212    } else {213      SectionRef SecA = Obj.getAnyRelocationSection(RelInfo);214      bool IsCode = SecA.isText();215      Expected<unsigned> SectionAIDOrErr =216        findOrEmitSection(Obj, SecA, IsCode, ObjSectionToID);217      if (!SectionAIDOrErr)218        return SectionAIDOrErr.takeError();219      SectionAID = *SectionAIDOrErr;220      Addend -= SecA.getAddress();221    }222 223    RelocationEntry R(SectionID, Offset, MachO::X86_64_RELOC_SUBTRACTOR, (uint64_t)Addend,224                      SectionAID, SectionAOffset, SectionBID, SectionBOffset,225                      false, Size);226 227    addRelocationForSection(R, SectionAID);228 229    return ++RelI;230  }231 232};233}234 235#undef DEBUG_TYPE236 237#endif238