Jim Grosbach | e0934be | 2012-01-16 23:50:58 +0000 | [diff] [blame] | 1 | //===-- RuntimeDyldImpl.h - Run-time dynamic linker for MC-JIT --*- C++ -*-===// |
Danil Malyshev | cf852dc | 2011-07-13 07:57:58 +0000 | [diff] [blame] | 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 | // Interface for the implementations of runtime dynamic linker facilities. |
| 11 | // |
| 12 | //===----------------------------------------------------------------------===// |
| 13 | |
| 14 | #ifndef LLVM_RUNTIME_DYLD_IMPL_H |
| 15 | #define LLVM_RUNTIME_DYLD_IMPL_H |
| 16 | |
| 17 | #include "llvm/ExecutionEngine/RuntimeDyld.h" |
Danil Malyshev | 0e4fa5f | 2012-03-30 16:45:19 +0000 | [diff] [blame] | 18 | #include "llvm/Object/ObjectFile.h" |
Jim Grosbach | 020f4e8 | 2012-01-16 23:50:55 +0000 | [diff] [blame] | 19 | #include "llvm/ADT/DenseMap.h" |
Danil Malyshev | cf852dc | 2011-07-13 07:57:58 +0000 | [diff] [blame] | 20 | #include "llvm/ADT/StringMap.h" |
| 21 | #include "llvm/ADT/Twine.h" |
| 22 | #include "llvm/ADT/SmallVector.h" |
Danil Malyshev | cf852dc | 2011-07-13 07:57:58 +0000 | [diff] [blame] | 23 | #include "llvm/Support/Memory.h" |
| 24 | #include "llvm/Support/MemoryBuffer.h" |
| 25 | #include "llvm/Support/system_error.h" |
| 26 | #include "llvm/Support/raw_ostream.h" |
| 27 | #include "llvm/Support/Debug.h" |
| 28 | #include "llvm/Support/ErrorHandling.h" |
Danil Malyshev | 0e4fa5f | 2012-03-30 16:45:19 +0000 | [diff] [blame] | 29 | #include "llvm/ADT/Triple.h" |
| 30 | #include <map> |
| 31 | #include "llvm/Support/Format.h" |
Preston Gurd | 689ff9c | 2012-04-16 22:12:58 +0000 | [diff] [blame^] | 32 | #include "ObjectImage.h" |
Danil Malyshev | cf852dc | 2011-07-13 07:57:58 +0000 | [diff] [blame] | 33 | |
| 34 | using namespace llvm; |
Danil Malyshev | 0e4fa5f | 2012-03-30 16:45:19 +0000 | [diff] [blame] | 35 | using namespace llvm::object; |
Danil Malyshev | cf852dc | 2011-07-13 07:57:58 +0000 | [diff] [blame] | 36 | |
| 37 | namespace llvm { |
Danil Malyshev | 0e4fa5f | 2012-03-30 16:45:19 +0000 | [diff] [blame] | 38 | |
| 39 | class SectionEntry { |
| 40 | public: |
| 41 | uint8_t* Address; |
| 42 | size_t Size; |
| 43 | uint64_t LoadAddress; // For each section, the address it will be |
| 44 | // considered to live at for relocations. The same |
| 45 | // as the pointer to the above memory block for |
| 46 | // hosted JITs. |
| 47 | uintptr_t StubOffset; // It's used for architecturies with stub |
| 48 | // functions for far relocations like ARM. |
| 49 | uintptr_t ObjAddress; // Section address in object file. It's use for |
| 50 | // calculate MachO relocation addend |
| 51 | SectionEntry(uint8_t* address, size_t size, uintptr_t stubOffset, |
| 52 | uintptr_t objAddress) |
| 53 | : Address(address), Size(size), LoadAddress((uintptr_t)address), |
| 54 | StubOffset(stubOffset), ObjAddress(objAddress) {} |
| 55 | }; |
| 56 | |
| 57 | class RelocationEntry { |
| 58 | public: |
| 59 | unsigned SectionID; // Section the relocation is contained in. |
| 60 | uintptr_t Offset; // Offset into the section for the relocation. |
| 61 | uint32_t Data; // Relocatino data. Including type of relocation |
| 62 | // and another flags and parameners from |
| 63 | intptr_t Addend; // Addend encoded in the instruction itself, if any, |
| 64 | // plus the offset into the source section for |
| 65 | // the symbol once the relocation is resolvable. |
| 66 | RelocationEntry(unsigned id, uint64_t offset, uint32_t data, int64_t addend) |
| 67 | : SectionID(id), Offset(offset), Data(data), Addend(addend) {} |
| 68 | }; |
| 69 | |
| 70 | // Raw relocation data from object file |
| 71 | class ObjRelocationInfo { |
| 72 | public: |
| 73 | unsigned SectionID; |
| 74 | uint64_t Offset; |
| 75 | SymbolRef Symbol; |
| 76 | uint64_t Type; |
| 77 | int64_t AdditionalInfo; |
| 78 | }; |
| 79 | |
| 80 | class RelocationValueRef { |
| 81 | public: |
| 82 | unsigned SectionID; |
| 83 | intptr_t Addend; |
| 84 | const char *SymbolName; |
| 85 | RelocationValueRef(): SectionID(0), Addend(0), SymbolName(0) {} |
| 86 | |
| 87 | inline bool operator==(const RelocationValueRef &Other) const { |
| 88 | return std::memcmp(this, &Other, sizeof(RelocationValueRef)) == 0; |
| 89 | } |
| 90 | inline bool operator <(const RelocationValueRef &Other) const { |
| 91 | return std::memcmp(this, &Other, sizeof(RelocationValueRef)) < 0; |
| 92 | } |
| 93 | }; |
| 94 | |
Danil Malyshev | cf852dc | 2011-07-13 07:57:58 +0000 | [diff] [blame] | 95 | class RuntimeDyldImpl { |
| 96 | protected: |
Danil Malyshev | cf852dc | 2011-07-13 07:57:58 +0000 | [diff] [blame] | 97 | // The MemoryManager to load objects into. |
| 98 | RTDyldMemoryManager *MemMgr; |
| 99 | |
Danil Malyshev | 0e4fa5f | 2012-03-30 16:45:19 +0000 | [diff] [blame] | 100 | // A list of emmitted sections. |
| 101 | typedef SmallVector<SectionEntry, 64> SectionList; |
| 102 | SectionList Sections; |
Danil Malyshev | cf852dc | 2011-07-13 07:57:58 +0000 | [diff] [blame] | 103 | |
Danil Malyshev | 0e4fa5f | 2012-03-30 16:45:19 +0000 | [diff] [blame] | 104 | // Keep a map of sections from object file to the SectionID which |
| 105 | // references it. |
| 106 | typedef std::map<SectionRef, unsigned> ObjSectionToIDMap; |
Jim Grosbach | 020f4e8 | 2012-01-16 23:50:55 +0000 | [diff] [blame] | 107 | |
Danil Malyshev | cf852dc | 2011-07-13 07:57:58 +0000 | [diff] [blame] | 108 | // Master symbol table. As modules are loaded and external symbols are |
Jim Grosbach | 61425c0 | 2012-01-16 22:26:39 +0000 | [diff] [blame] | 109 | // resolved, their addresses are stored here as a SectionID/Offset pair. |
Danil Malyshev | 0e4fa5f | 2012-03-30 16:45:19 +0000 | [diff] [blame] | 110 | typedef std::pair<unsigned, uintptr_t> SymbolLoc; |
Jim Grosbach | 61425c0 | 2012-01-16 22:26:39 +0000 | [diff] [blame] | 111 | StringMap<SymbolLoc> SymbolTable; |
Danil Malyshev | 0e4fa5f | 2012-03-30 16:45:19 +0000 | [diff] [blame] | 112 | typedef DenseMap<const char*, SymbolLoc> LocalSymbolMap; |
| 113 | |
Preston Gurd | c68dda8 | 2012-04-12 20:13:57 +0000 | [diff] [blame] | 114 | // Keep a map of common symbols to their sizes |
| 115 | typedef std::map<SymbolRef, unsigned> CommonSymbolMap; |
| 116 | |
Danil Malyshev | 0e4fa5f | 2012-03-30 16:45:19 +0000 | [diff] [blame] | 117 | // For each symbol, keep a list of relocations based on it. Anytime |
| 118 | // its address is reassigned (the JIT re-compiled the function, e.g.), |
| 119 | // the relocations get re-resolved. |
| 120 | // The symbol (or section) the relocation is sourced from is the Key |
| 121 | // in the relocation list where it's stored. |
| 122 | typedef SmallVector<RelocationEntry, 64> RelocationList; |
| 123 | // Relocations to sections already loaded. Indexed by SectionID which is the |
| 124 | // source of the address. The target where the address will be writen is |
| 125 | // SectionID/Offset in the relocation itself. |
| 126 | DenseMap<unsigned, RelocationList> Relocations; |
| 127 | // Relocations to external symbols that are not yet resolved. |
| 128 | // Indexed by symbol name. |
| 129 | StringMap<RelocationList> SymbolRelocations; |
| 130 | |
| 131 | typedef std::map<RelocationValueRef, uintptr_t> StubMap; |
| 132 | |
| 133 | Triple::ArchType Arch; |
| 134 | |
| 135 | inline unsigned getMaxStubSize() { |
| 136 | if (Arch == Triple::arm || Arch == Triple::thumb) |
| 137 | return 8; // 32-bit instruction and 32-bit address |
| 138 | else |
| 139 | return 0; |
| 140 | } |
Danil Malyshev | cf852dc | 2011-07-13 07:57:58 +0000 | [diff] [blame] | 141 | |
| 142 | bool HasError; |
| 143 | std::string ErrorStr; |
| 144 | |
| 145 | // Set the error state and record an error string. |
| 146 | bool Error(const Twine &Msg) { |
| 147 | ErrorStr = Msg.str(); |
| 148 | HasError = true; |
| 149 | return true; |
| 150 | } |
| 151 | |
Jim Grosbach | 61425c0 | 2012-01-16 22:26:39 +0000 | [diff] [blame] | 152 | uint8_t *getSectionAddress(unsigned SectionID) { |
Danil Malyshev | 0e4fa5f | 2012-03-30 16:45:19 +0000 | [diff] [blame] | 153 | return (uint8_t*)Sections[SectionID].Address; |
Jim Grosbach | 61425c0 | 2012-01-16 22:26:39 +0000 | [diff] [blame] | 154 | } |
Danil Malyshev | cf852dc | 2011-07-13 07:57:58 +0000 | [diff] [blame] | 155 | |
Preston Gurd | c68dda8 | 2012-04-12 20:13:57 +0000 | [diff] [blame] | 156 | /// \brief Emits a section containing common symbols. |
| 157 | /// \return SectionID. |
Preston Gurd | 689ff9c | 2012-04-16 22:12:58 +0000 | [diff] [blame^] | 158 | unsigned emitCommonSymbols(ObjectImage &Obj, |
| 159 | const CommonSymbolMap &Map, |
Preston Gurd | c68dda8 | 2012-04-12 20:13:57 +0000 | [diff] [blame] | 160 | uint64_t TotalSize, |
| 161 | LocalSymbolMap &Symbols); |
| 162 | |
Danil Malyshev | 0e4fa5f | 2012-03-30 16:45:19 +0000 | [diff] [blame] | 163 | /// \brief Emits section data from the object file to the MemoryManager. |
| 164 | /// \param IsCode if it's true then allocateCodeSection() will be |
| 165 | /// used for emmits, else allocateDataSection() will be used. |
| 166 | /// \return SectionID. |
Preston Gurd | 689ff9c | 2012-04-16 22:12:58 +0000 | [diff] [blame^] | 167 | unsigned emitSection(ObjectImage &Obj, |
| 168 | const SectionRef &Section, |
| 169 | bool IsCode); |
Danil Malyshev | 0e4fa5f | 2012-03-30 16:45:19 +0000 | [diff] [blame] | 170 | |
| 171 | /// \brief Find Section in LocalSections. If the secton is not found - emit |
| 172 | /// it and store in LocalSections. |
| 173 | /// \param IsCode if it's true then allocateCodeSection() will be |
| 174 | /// used for emmits, else allocateDataSection() will be used. |
| 175 | /// \return SectionID. |
Preston Gurd | 689ff9c | 2012-04-16 22:12:58 +0000 | [diff] [blame^] | 176 | unsigned findOrEmitSection(ObjectImage &Obj, |
| 177 | const SectionRef &Section, |
| 178 | bool IsCode, |
Danil Malyshev | 0e4fa5f | 2012-03-30 16:45:19 +0000 | [diff] [blame] | 179 | ObjSectionToIDMap &LocalSections); |
| 180 | |
| 181 | /// \brief If Value.SymbolName is NULL then store relocation to the |
| 182 | /// Relocations, else store it in the SymbolRelocations. |
| 183 | void AddRelocation(const RelocationValueRef &Value, unsigned SectionID, |
| 184 | uintptr_t Offset, uint32_t RelType); |
| 185 | |
| 186 | /// \brief Emits long jump instruction to Addr. |
| 187 | /// \return Pointer to the memory area for emitting target address. |
| 188 | uint8_t* createStubFunction(uint8_t *Addr); |
| 189 | |
| 190 | /// \brief Resolves relocations from Relocs list with address from Value. |
| 191 | void resolveRelocationList(const RelocationList &Relocs, uint64_t Value); |
| 192 | void resolveRelocationEntry(const RelocationEntry &RE, uint64_t Value); |
| 193 | |
| 194 | /// \brief A object file specific relocation resolver |
| 195 | /// \param Address Address to apply the relocation action |
| 196 | /// \param Value Target symbol address to apply the relocation action |
| 197 | /// \param Type object file specific relocation type |
| 198 | /// \param Addend A constant addend used to compute the value to be stored |
| 199 | /// into the relocatable field |
| 200 | virtual void resolveRelocation(uint8_t *LocalAddress, |
| 201 | uint64_t FinalAddress, |
| 202 | uint64_t Value, |
| 203 | uint32_t Type, |
| 204 | int64_t Addend) = 0; |
| 205 | |
| 206 | /// \brief Parses the object file relocation and store it to Relocations |
| 207 | /// or SymbolRelocations. Its depend from object file type. |
| 208 | virtual void processRelocationRef(const ObjRelocationInfo &Rel, |
Preston Gurd | 689ff9c | 2012-04-16 22:12:58 +0000 | [diff] [blame^] | 209 | ObjectImage &Obj, |
Danil Malyshev | 0e4fa5f | 2012-03-30 16:45:19 +0000 | [diff] [blame] | 210 | ObjSectionToIDMap &ObjSectionToID, |
| 211 | LocalSymbolMap &Symbols, StubMap &Stubs) = 0; |
| 212 | |
| 213 | void resolveSymbols(); |
Preston Gurd | 689ff9c | 2012-04-16 22:12:58 +0000 | [diff] [blame^] | 214 | virtual ObjectImage *createObjectImage(const MemoryBuffer *InputBuffer); |
| 215 | virtual void handleObjectLoaded(ObjectImage *Obj) |
| 216 | { |
| 217 | // Subclasses may choose to retain this image if they have a use for it |
| 218 | delete Obj; |
| 219 | } |
| 220 | |
Danil Malyshev | cf852dc | 2011-07-13 07:57:58 +0000 | [diff] [blame] | 221 | public: |
| 222 | RuntimeDyldImpl(RTDyldMemoryManager *mm) : MemMgr(mm), HasError(false) {} |
| 223 | |
| 224 | virtual ~RuntimeDyldImpl(); |
| 225 | |
Danil Malyshev | 0e4fa5f | 2012-03-30 16:45:19 +0000 | [diff] [blame] | 226 | bool loadObject(const MemoryBuffer *InputBuffer); |
Danil Malyshev | cf852dc | 2011-07-13 07:57:58 +0000 | [diff] [blame] | 227 | |
| 228 | void *getSymbolAddress(StringRef Name) { |
| 229 | // FIXME: Just look up as a function for now. Overly simple of course. |
| 230 | // Work in progress. |
Jim Grosbach | 61425c0 | 2012-01-16 22:26:39 +0000 | [diff] [blame] | 231 | if (SymbolTable.find(Name) == SymbolTable.end()) |
| 232 | return 0; |
| 233 | SymbolLoc Loc = SymbolTable.lookup(Name); |
| 234 | return getSectionAddress(Loc.first) + Loc.second; |
Danil Malyshev | cf852dc | 2011-07-13 07:57:58 +0000 | [diff] [blame] | 235 | } |
| 236 | |
Danil Malyshev | 0e4fa5f | 2012-03-30 16:45:19 +0000 | [diff] [blame] | 237 | void resolveRelocations(); |
Danil Malyshev | cf852dc | 2011-07-13 07:57:58 +0000 | [diff] [blame] | 238 | |
Danil Malyshev | 0e4fa5f | 2012-03-30 16:45:19 +0000 | [diff] [blame] | 239 | void reassignSectionAddress(unsigned SectionID, uint64_t Addr); |
Danil Malyshev | cf852dc | 2011-07-13 07:57:58 +0000 | [diff] [blame] | 240 | |
Jim Grosbach | 020f4e8 | 2012-01-16 23:50:55 +0000 | [diff] [blame] | 241 | void mapSectionAddress(void *LocalAddress, uint64_t TargetAddress); |
| 242 | |
Danil Malyshev | cf852dc | 2011-07-13 07:57:58 +0000 | [diff] [blame] | 243 | // Is the linker in an error state? |
| 244 | bool hasError() { return HasError; } |
| 245 | |
| 246 | // Mark the error condition as handled and continue. |
| 247 | void clearError() { HasError = false; } |
| 248 | |
| 249 | // Get the error message. |
| 250 | StringRef getErrorString() { return ErrorStr; } |
| 251 | |
| 252 | virtual bool isCompatibleFormat(const MemoryBuffer *InputBuffer) const = 0; |
Danil Malyshev | 0e4fa5f | 2012-03-30 16:45:19 +0000 | [diff] [blame] | 253 | |
Danil Malyshev | cf852dc | 2011-07-13 07:57:58 +0000 | [diff] [blame] | 254 | }; |
| 255 | |
Danil Malyshev | cf852dc | 2011-07-13 07:57:58 +0000 | [diff] [blame] | 256 | } // end namespace llvm |
| 257 | |
| 258 | |
| 259 | #endif |