LLVM API Documentation

RuntimeDyldMachOX86_64.h
Go to the documentation of this file.
00001 //===-- RuntimeDyldMachOX86_64.h ---- MachO/X86_64 specific code. -*- C++ -*-=//
00002 //
00003 //                     The LLVM Compiler Infrastructure
00004 //
00005 // This file is distributed under the University of Illinois Open Source
00006 // License. See LICENSE.TXT for details.
00007 //
00008 //===----------------------------------------------------------------------===//
00009 
00010 #ifndef LLVM_LIB_EXECUTIONENGINE_RUNTIMEDYLD_TARGETS_RUNTIMEDYLDMACHOX86_64_H
00011 #define LLVM_LIB_EXECUTIONENGINE_RUNTIMEDYLD_TARGETS_RUNTIMEDYLDMACHOX86_64_H
00012 
00013 #include "../RuntimeDyldMachO.h"
00014 
00015 #define DEBUG_TYPE "dyld"
00016 
00017 namespace llvm {
00018 
00019 class RuntimeDyldMachOX86_64
00020     : public RuntimeDyldMachOCRTPBase<RuntimeDyldMachOX86_64> {
00021 public:
00022 
00023   typedef uint64_t TargetPtrT;
00024 
00025   RuntimeDyldMachOX86_64(RTDyldMemoryManager *MM)
00026       : RuntimeDyldMachOCRTPBase(MM) {}
00027 
00028   unsigned getMaxStubSize() override { return 8; }
00029 
00030   unsigned getStubAlignment() override { return 1; }
00031 
00032   relocation_iterator
00033   processRelocationRef(unsigned SectionID, relocation_iterator RelI,
00034                        ObjectImage &ObjImg, ObjSectionToIDMap &ObjSectionToID,
00035                        const SymbolTableMap &Symbols, StubMap &Stubs) override {
00036     const MachOObjectFile &Obj =
00037         static_cast<const MachOObjectFile &>(*ObjImg.getObjectFile());
00038     MachO::any_relocation_info RelInfo =
00039         Obj.getRelocation(RelI->getRawDataRefImpl());
00040 
00041     assert(!Obj.isRelocationScattered(RelInfo) &&
00042            "Scattered relocations not supported on X86_64");
00043 
00044     RelocationEntry RE(getRelocationEntry(SectionID, ObjImg, RelI));
00045     RE.Addend = memcpyAddend(RE);
00046     RelocationValueRef Value(
00047         getRelocationValueRef(ObjImg, RelI, RE, ObjSectionToID, Symbols));
00048 
00049     bool IsExtern = Obj.getPlainRelocationExternal(RelInfo);
00050     if (!IsExtern && RE.IsPCRel)
00051       makeValueAddendPCRel(Value, ObjImg, RelI, 1 << RE.Size);
00052 
00053     if (RE.RelType == MachO::X86_64_RELOC_GOT ||
00054         RE.RelType == MachO::X86_64_RELOC_GOT_LOAD)
00055       processGOTRelocation(RE, Value, Stubs);
00056     else {
00057       RE.Addend = Value.Offset;
00058       if (Value.SymbolName)
00059         addRelocationForSymbol(RE, Value.SymbolName);
00060       else
00061         addRelocationForSection(RE, Value.SectionID);
00062     }
00063 
00064     return ++RelI;
00065   }
00066 
00067   void resolveRelocation(const RelocationEntry &RE, uint64_t Value) override {
00068     DEBUG(dumpRelocationToResolve(RE, Value));
00069     const SectionEntry &Section = Sections[RE.SectionID];
00070     uint8_t *LocalAddress = Section.Address + RE.Offset;
00071 
00072     // If the relocation is PC-relative, the value to be encoded is the
00073     // pointer difference.
00074     if (RE.IsPCRel) {
00075       // FIXME: It seems this value needs to be adjusted by 4 for an effective
00076       // PC address. Is that expected? Only for branches, perhaps?
00077       uint64_t FinalAddress = Section.LoadAddress + RE.Offset;
00078       Value -= FinalAddress + 4;
00079     }
00080 
00081     switch (RE.RelType) {
00082     default:
00083       llvm_unreachable("Invalid relocation type!");
00084     case MachO::X86_64_RELOC_SIGNED_1:
00085     case MachO::X86_64_RELOC_SIGNED_2:
00086     case MachO::X86_64_RELOC_SIGNED_4:
00087     case MachO::X86_64_RELOC_SIGNED:
00088     case MachO::X86_64_RELOC_UNSIGNED:
00089     case MachO::X86_64_RELOC_BRANCH:
00090       writeBytesUnaligned(Value + RE.Addend, LocalAddress, 1 << RE.Size);
00091       break;
00092     case MachO::X86_64_RELOC_GOT_LOAD:
00093     case MachO::X86_64_RELOC_GOT:
00094     case MachO::X86_64_RELOC_SUBTRACTOR:
00095     case MachO::X86_64_RELOC_TLV:
00096       Error("Relocation type not implemented yet!");
00097     }
00098   }
00099 
00100   void finalizeSection(ObjectImage &ObjImg, unsigned SectionID,
00101                        const SectionRef &Section) {}
00102 
00103 private:
00104   void processGOTRelocation(const RelocationEntry &RE,
00105                             RelocationValueRef &Value, StubMap &Stubs) {
00106     SectionEntry &Section = Sections[RE.SectionID];
00107     assert(RE.IsPCRel);
00108     assert(RE.Size == 2);
00109     Value.Offset -= RE.Addend;
00110     RuntimeDyldMachO::StubMap::const_iterator i = Stubs.find(Value);
00111     uint8_t *Addr;
00112     if (i != Stubs.end()) {
00113       Addr = Section.Address + i->second;
00114     } else {
00115       Stubs[Value] = Section.StubOffset;
00116       uint8_t *GOTEntry = Section.Address + Section.StubOffset;
00117       RelocationEntry GOTRE(RE.SectionID, Section.StubOffset,
00118                             MachO::X86_64_RELOC_UNSIGNED, Value.Offset, false,
00119                             3);
00120       if (Value.SymbolName)
00121         addRelocationForSymbol(GOTRE, Value.SymbolName);
00122       else
00123         addRelocationForSection(GOTRE, Value.SectionID);
00124       Section.StubOffset += 8;
00125       Addr = GOTEntry;
00126     }
00127     RelocationEntry TargetRE(RE.SectionID, RE.Offset,
00128                              MachO::X86_64_RELOC_UNSIGNED, RE.Addend, true, 2);
00129     resolveRelocation(TargetRE, (uint64_t)Addr);
00130   }
00131 };
00132 }
00133 
00134 #undef DEBUG_TYPE
00135 
00136 #endif