LCOV - code coverage report
Current view: top level - lib/ExecutionEngine/RuntimeDyld/Targets - RuntimeDyldMachOX86_64.h (source / functions) Hit Total Coverage
Test: llvm-toolchain.info Lines: 106 120 88.3 %
Date: 2017-09-14 15:23:50 Functions: 7 8 87.5 %
Legend: Lines: hit not hit

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

Generated by: LCOV version 1.13