LLVM API Documentation
00001 //===-- RuntimeDyldMachO.h - Run-time dynamic linker for MC-JIT ---*- 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 // MachO support for MC-JIT runtime dynamic linker. 00011 // 00012 //===----------------------------------------------------------------------===// 00013 00014 #ifndef LLVM_LIB_EXECUTIONENGINE_RUNTIMEDYLD_RUNTIMEDYLDMACHO_H 00015 #define LLVM_LIB_EXECUTIONENGINE_RUNTIMEDYLD_RUNTIMEDYLDMACHO_H 00016 00017 #include "ObjectImageCommon.h" 00018 #include "RuntimeDyldImpl.h" 00019 #include "llvm/Object/MachO.h" 00020 #include "llvm/Support/Format.h" 00021 00022 #define DEBUG_TYPE "dyld" 00023 00024 using namespace llvm; 00025 using namespace llvm::object; 00026 00027 namespace llvm { 00028 class RuntimeDyldMachO : public RuntimeDyldImpl { 00029 protected: 00030 struct SectionOffsetPair { 00031 unsigned SectionID; 00032 uint64_t Offset; 00033 }; 00034 00035 struct EHFrameRelatedSections { 00036 EHFrameRelatedSections() 00037 : EHFrameSID(RTDYLD_INVALID_SECTION_ID), 00038 TextSID(RTDYLD_INVALID_SECTION_ID), 00039 ExceptTabSID(RTDYLD_INVALID_SECTION_ID) {} 00040 00041 EHFrameRelatedSections(SID EH, SID T, SID Ex) 00042 : EHFrameSID(EH), TextSID(T), ExceptTabSID(Ex) {} 00043 SID EHFrameSID; 00044 SID TextSID; 00045 SID ExceptTabSID; 00046 }; 00047 00048 // When a module is loaded we save the SectionID of the EH frame section 00049 // in a table until we receive a request to register all unregistered 00050 // EH frame sections with the memory manager. 00051 SmallVector<EHFrameRelatedSections, 2> UnregisteredEHFrameSections; 00052 00053 RuntimeDyldMachO(RTDyldMemoryManager *mm) : RuntimeDyldImpl(mm) {} 00054 00055 /// This convenience method uses memcpy to extract a contiguous addend (the 00056 /// addend size and offset are taken from the corresponding fields of the RE). 00057 int64_t memcpyAddend(const RelocationEntry &RE) const; 00058 00059 /// Given a relocation_iterator for a non-scattered relocation, construct a 00060 /// RelocationEntry and fill in the common fields. The 'Addend' field is *not* 00061 /// filled in, since immediate encodings are highly target/opcode specific. 00062 /// For targets/opcodes with simple, contiguous immediates (e.g. X86) the 00063 /// memcpyAddend method can be used to read the immediate. 00064 RelocationEntry getRelocationEntry(unsigned SectionID, ObjectImage &ObjImg, 00065 const relocation_iterator &RI) const { 00066 const MachOObjectFile &Obj = 00067 static_cast<const MachOObjectFile &>(*ObjImg.getObjectFile()); 00068 MachO::any_relocation_info RelInfo = 00069 Obj.getRelocation(RI->getRawDataRefImpl()); 00070 00071 bool IsPCRel = Obj.getAnyRelocationPCRel(RelInfo); 00072 unsigned Size = Obj.getAnyRelocationLength(RelInfo); 00073 uint64_t Offset; 00074 RI->getOffset(Offset); 00075 MachO::RelocationInfoType RelType = 00076 static_cast<MachO::RelocationInfoType>(Obj.getAnyRelocationType(RelInfo)); 00077 00078 return RelocationEntry(SectionID, Offset, RelType, 0, IsPCRel, Size); 00079 } 00080 00081 /// Construct a RelocationValueRef representing the relocation target. 00082 /// For Symbols in known sections, this will return a RelocationValueRef 00083 /// representing a (SectionID, Offset) pair. 00084 /// For Symbols whose section is not known, this will return a 00085 /// (SymbolName, Offset) pair, where the Offset is taken from the instruction 00086 /// immediate (held in RE.Addend). 00087 /// In both cases the Addend field is *NOT* fixed up to be PC-relative. That 00088 /// should be done by the caller where appropriate by calling makePCRel on 00089 /// the RelocationValueRef. 00090 RelocationValueRef getRelocationValueRef(ObjectImage &ObjImg, 00091 const relocation_iterator &RI, 00092 const RelocationEntry &RE, 00093 ObjSectionToIDMap &ObjSectionToID, 00094 const SymbolTableMap &Symbols); 00095 00096 /// Make the RelocationValueRef addend PC-relative. 00097 void makeValueAddendPCRel(RelocationValueRef &Value, ObjectImage &ObjImg, 00098 const relocation_iterator &RI, 00099 unsigned OffsetToNextPC); 00100 00101 /// Dump information about the relocation entry (RE) and resolved value. 00102 void dumpRelocationToResolve(const RelocationEntry &RE, uint64_t Value) const; 00103 00104 // Return a section iterator for the section containing the given address. 00105 static section_iterator getSectionByAddress(const MachOObjectFile &Obj, 00106 uint64_t Addr); 00107 00108 00109 // Populate __pointers section. 00110 void populateIndirectSymbolPointersSection(MachOObjectFile &Obj, 00111 const SectionRef &PTSection, 00112 unsigned PTSectionID); 00113 00114 public: 00115 /// Create an ObjectImage from the given ObjectBuffer. 00116 static std::unique_ptr<ObjectImage> 00117 createObjectImage(std::unique_ptr<ObjectBuffer> InputBuffer) { 00118 return llvm::make_unique<ObjectImageCommon>(std::move(InputBuffer)); 00119 } 00120 00121 /// Create an ObjectImage from the given ObjectFile. 00122 static ObjectImage * 00123 createObjectImageFromFile(std::unique_ptr<object::ObjectFile> InputObject) { 00124 return new ObjectImageCommon(std::move(InputObject)); 00125 } 00126 00127 /// Create a RuntimeDyldMachO instance for the given target architecture. 00128 static std::unique_ptr<RuntimeDyldMachO> create(Triple::ArchType Arch, 00129 RTDyldMemoryManager *mm); 00130 00131 SectionEntry &getSection(unsigned SectionID) { return Sections[SectionID]; } 00132 00133 bool isCompatibleFormat(const ObjectBuffer *Buffer) const override; 00134 bool isCompatibleFile(const object::ObjectFile *Obj) const override; 00135 }; 00136 00137 /// RuntimeDyldMachOTarget - Templated base class for generic MachO linker 00138 /// algorithms and data structures. 00139 /// 00140 /// Concrete, target specific sub-classes can be accessed via the impl() 00141 /// methods. (i.e. the RuntimeDyldMachO hierarchy uses the Curiously 00142 /// Recurring Template Idiom). Concrete subclasses for each target 00143 /// can be found in ./Targets. 00144 template <typename Impl> 00145 class RuntimeDyldMachOCRTPBase : public RuntimeDyldMachO { 00146 private: 00147 Impl &impl() { return static_cast<Impl &>(*this); } 00148 const Impl &impl() const { return static_cast<const Impl &>(*this); } 00149 00150 unsigned char *processFDE(unsigned char *P, int64_t DeltaForText, 00151 int64_t DeltaForEH); 00152 00153 public: 00154 RuntimeDyldMachOCRTPBase(RTDyldMemoryManager *mm) : RuntimeDyldMachO(mm) {} 00155 00156 void finalizeLoad(ObjectImage &ObjImg, 00157 ObjSectionToIDMap &SectionMap) override; 00158 void registerEHFrames() override; 00159 }; 00160 00161 } // end namespace llvm 00162 00163 #undef DEBUG_TYPE 00164 00165 #endif