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 | |
Eli Bendersky | 6d15e87 | 2012-04-30 10:06:27 +0000 | [diff] [blame] | 17 | #include "ObjectImage.h" |
Danil Malyshev | cf852dc | 2011-07-13 07:57:58 +0000 | [diff] [blame] | 18 | #include "llvm/ExecutionEngine/RuntimeDyld.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/SmallVector.h" |
Eli Bendersky | 6d15e87 | 2012-04-30 10:06:27 +0000 | [diff] [blame] | 21 | #include "llvm/ADT/StringMap.h" |
| 22 | #include "llvm/ADT/Triple.h" |
| 23 | #include "llvm/Object/ObjectFile.h" |
Danil Malyshev | cf852dc | 2011-07-13 07:57:58 +0000 | [diff] [blame] | 24 | #include "llvm/Support/Debug.h" |
| 25 | #include "llvm/Support/ErrorHandling.h" |
Danil Malyshev | 0e4fa5f | 2012-03-30 16:45:19 +0000 | [diff] [blame] | 26 | #include "llvm/Support/Format.h" |
Eli Bendersky | 6d15e87 | 2012-04-30 10:06:27 +0000 | [diff] [blame] | 27 | #include "llvm/Support/raw_ostream.h" |
| 28 | #include "llvm/Support/system_error.h" |
| 29 | #include <map> |
Danil Malyshev | cf852dc | 2011-07-13 07:57:58 +0000 | [diff] [blame] | 30 | |
| 31 | using namespace llvm; |
Danil Malyshev | 0e4fa5f | 2012-03-30 16:45:19 +0000 | [diff] [blame] | 32 | using namespace llvm::object; |
Danil Malyshev | cf852dc | 2011-07-13 07:57:58 +0000 | [diff] [blame] | 33 | |
| 34 | namespace llvm { |
Danil Malyshev | 0e4fa5f | 2012-03-30 16:45:19 +0000 | [diff] [blame] | 35 | |
Eli Bendersky | 6d15e87 | 2012-04-30 10:06:27 +0000 | [diff] [blame] | 36 | class MemoryBuffer; |
| 37 | class Twine; |
| 38 | |
| 39 | |
| 40 | /// SectionEntry - represents a section emitted into memory by the dynamic |
| 41 | /// linker. |
Danil Malyshev | 0e4fa5f | 2012-03-30 16:45:19 +0000 | [diff] [blame] | 42 | class SectionEntry { |
| 43 | public: |
Eli Bendersky | 6d15e87 | 2012-04-30 10:06:27 +0000 | [diff] [blame] | 44 | /// Address - address in the linker's memory where the section resides. |
| 45 | uint8_t *Address; |
| 46 | |
| 47 | /// Size - section size. |
| 48 | size_t Size; |
| 49 | |
| 50 | /// LoadAddress - the address of the section in the target process's memory. |
| 51 | /// Used for situations in which JIT-ed code is being executed in the address |
| 52 | /// space of a separate process. If the code executes in the same address |
| 53 | /// space where it was JIT-ed, this just equals Address. |
| 54 | uint64_t LoadAddress; |
| 55 | |
| 56 | /// StubOffset - used for architectures with stub functions for far |
| 57 | /// relocations (like ARM). |
| 58 | uintptr_t StubOffset; |
| 59 | |
| 60 | /// ObjAddress - address of the section in the in-memory object file. Used |
| 61 | /// for calculating relocations in some object formats (like MachO). |
| 62 | uintptr_t ObjAddress; |
| 63 | |
Eli Bendersky | 5fe0198 | 2012-04-29 12:40:47 +0000 | [diff] [blame] | 64 | SectionEntry(uint8_t *address, size_t size, uintptr_t stubOffset, |
Danil Malyshev | 0e4fa5f | 2012-03-30 16:45:19 +0000 | [diff] [blame] | 65 | uintptr_t objAddress) |
| 66 | : Address(address), Size(size), LoadAddress((uintptr_t)address), |
| 67 | StubOffset(stubOffset), ObjAddress(objAddress) {} |
| 68 | }; |
| 69 | |
Eli Bendersky | 6d15e87 | 2012-04-30 10:06:27 +0000 | [diff] [blame] | 70 | /// RelocationEntry - used to represent relocations internally in the dynamic |
| 71 | /// linker. |
Danil Malyshev | 0e4fa5f | 2012-03-30 16:45:19 +0000 | [diff] [blame] | 72 | class RelocationEntry { |
| 73 | public: |
Eli Bendersky | 6d15e87 | 2012-04-30 10:06:27 +0000 | [diff] [blame] | 74 | /// SectionID - the section this relocation points to. |
| 75 | unsigned SectionID; |
| 76 | |
| 77 | /// Offset - offset into the section. |
| 78 | uintptr_t Offset; |
| 79 | |
| 80 | /// RelType - relocation type. |
| 81 | uint32_t RelType; |
| 82 | |
| 83 | /// Addend - the relocation addend encoded in the instruction itself. Also |
| 84 | /// used to make a relocation section relative instead of symbol relative. |
| 85 | intptr_t Addend; |
| 86 | |
| 87 | RelocationEntry(unsigned id, uint64_t offset, uint32_t type, int64_t addend) |
| 88 | : SectionID(id), Offset(offset), RelType(type), Addend(addend) {} |
Danil Malyshev | 0e4fa5f | 2012-03-30 16:45:19 +0000 | [diff] [blame] | 89 | }; |
| 90 | |
Eli Bendersky | 6d15e87 | 2012-04-30 10:06:27 +0000 | [diff] [blame] | 91 | /// ObjRelocationInfo - relocation information as read from the object file. |
| 92 | /// Used to pass around data taken from object::RelocationRef, together with |
| 93 | /// the section to which the relocation points (represented by a SectionID). |
Danil Malyshev | 0e4fa5f | 2012-03-30 16:45:19 +0000 | [diff] [blame] | 94 | class ObjRelocationInfo { |
| 95 | public: |
| 96 | unsigned SectionID; |
| 97 | uint64_t Offset; |
| 98 | SymbolRef Symbol; |
| 99 | uint64_t Type; |
| 100 | int64_t AdditionalInfo; |
| 101 | }; |
| 102 | |
| 103 | class RelocationValueRef { |
| 104 | public: |
| 105 | unsigned SectionID; |
| 106 | intptr_t Addend; |
| 107 | const char *SymbolName; |
| 108 | RelocationValueRef(): SectionID(0), Addend(0), SymbolName(0) {} |
| 109 | |
| 110 | inline bool operator==(const RelocationValueRef &Other) const { |
| 111 | return std::memcmp(this, &Other, sizeof(RelocationValueRef)) == 0; |
| 112 | } |
| 113 | inline bool operator <(const RelocationValueRef &Other) const { |
| 114 | return std::memcmp(this, &Other, sizeof(RelocationValueRef)) < 0; |
| 115 | } |
| 116 | }; |
| 117 | |
Danil Malyshev | cf852dc | 2011-07-13 07:57:58 +0000 | [diff] [blame] | 118 | class RuntimeDyldImpl { |
| 119 | protected: |
Danil Malyshev | cf852dc | 2011-07-13 07:57:58 +0000 | [diff] [blame] | 120 | // The MemoryManager to load objects into. |
| 121 | RTDyldMemoryManager *MemMgr; |
| 122 | |
Eli Bendersky | 6d15e87 | 2012-04-30 10:06:27 +0000 | [diff] [blame] | 123 | // A list of all sections emitted by the dynamic linker. These sections are |
| 124 | // referenced in the code by means of their index in this list - SectionID. |
Danil Malyshev | 0e4fa5f | 2012-03-30 16:45:19 +0000 | [diff] [blame] | 125 | typedef SmallVector<SectionEntry, 64> SectionList; |
| 126 | SectionList Sections; |
Danil Malyshev | cf852dc | 2011-07-13 07:57:58 +0000 | [diff] [blame] | 127 | |
Danil Malyshev | 0e4fa5f | 2012-03-30 16:45:19 +0000 | [diff] [blame] | 128 | // Keep a map of sections from object file to the SectionID which |
| 129 | // references it. |
| 130 | typedef std::map<SectionRef, unsigned> ObjSectionToIDMap; |
Jim Grosbach | 020f4e8 | 2012-01-16 23:50:55 +0000 | [diff] [blame] | 131 | |
Eli Bendersky | d98c9e9 | 2012-05-01 06:58:59 +0000 | [diff] [blame] | 132 | // A global symbol table for symbols from all loaded modules. Maps the |
| 133 | // symbol name to a (SectionID, offset in section) pair. |
Danil Malyshev | 0e4fa5f | 2012-03-30 16:45:19 +0000 | [diff] [blame] | 134 | typedef std::pair<unsigned, uintptr_t> SymbolLoc; |
Eli Bendersky | d98c9e9 | 2012-05-01 06:58:59 +0000 | [diff] [blame] | 135 | typedef StringMap<SymbolLoc> SymbolTableMap; |
| 136 | SymbolTableMap GlobalSymbolTable; |
Danil Malyshev | 0e4fa5f | 2012-03-30 16:45:19 +0000 | [diff] [blame] | 137 | |
Preston Gurd | c68dda8 | 2012-04-12 20:13:57 +0000 | [diff] [blame] | 138 | // Keep a map of common symbols to their sizes |
| 139 | typedef std::map<SymbolRef, unsigned> CommonSymbolMap; |
| 140 | |
Danil Malyshev | 0e4fa5f | 2012-03-30 16:45:19 +0000 | [diff] [blame] | 141 | // For each symbol, keep a list of relocations based on it. Anytime |
| 142 | // its address is reassigned (the JIT re-compiled the function, e.g.), |
| 143 | // the relocations get re-resolved. |
| 144 | // The symbol (or section) the relocation is sourced from is the Key |
| 145 | // in the relocation list where it's stored. |
| 146 | typedef SmallVector<RelocationEntry, 64> RelocationList; |
| 147 | // Relocations to sections already loaded. Indexed by SectionID which is the |
Benjamin Kramer | d9b0b02 | 2012-06-02 10:20:22 +0000 | [diff] [blame] | 148 | // source of the address. The target where the address will be written is |
Danil Malyshev | 0e4fa5f | 2012-03-30 16:45:19 +0000 | [diff] [blame] | 149 | // SectionID/Offset in the relocation itself. |
| 150 | DenseMap<unsigned, RelocationList> Relocations; |
Eli Bendersky | 37bc5a2 | 2012-04-30 12:15:58 +0000 | [diff] [blame] | 151 | |
| 152 | // Relocations to external symbols that are not yet resolved. Symbols are |
| 153 | // external when they aren't found in the global symbol table of all loaded |
| 154 | // modules. This map is indexed by symbol name. |
| 155 | StringMap<RelocationList> ExternalSymbolRelocations; |
Danil Malyshev | 0e4fa5f | 2012-03-30 16:45:19 +0000 | [diff] [blame] | 156 | |
| 157 | typedef std::map<RelocationValueRef, uintptr_t> StubMap; |
| 158 | |
| 159 | Triple::ArchType Arch; |
| 160 | |
| 161 | inline unsigned getMaxStubSize() { |
| 162 | if (Arch == Triple::arm || Arch == Triple::thumb) |
| 163 | return 8; // 32-bit instruction and 32-bit address |
Akira Hatanaka | b889e0c | 2012-08-17 21:28:04 +0000 | [diff] [blame] | 164 | else if (Arch == Triple::mipsel) |
| 165 | return 16; |
Danil Malyshev | 0e4fa5f | 2012-03-30 16:45:19 +0000 | [diff] [blame] | 166 | else |
| 167 | return 0; |
| 168 | } |
Danil Malyshev | cf852dc | 2011-07-13 07:57:58 +0000 | [diff] [blame] | 169 | |
| 170 | bool HasError; |
| 171 | std::string ErrorStr; |
| 172 | |
| 173 | // Set the error state and record an error string. |
| 174 | bool Error(const Twine &Msg) { |
| 175 | ErrorStr = Msg.str(); |
| 176 | HasError = true; |
| 177 | return true; |
| 178 | } |
| 179 | |
Jim Grosbach | 35ed842 | 2012-09-05 16:50:40 +0000 | [diff] [blame^] | 180 | uint64_t getSectionLoadAddress(unsigned SectionID) { |
| 181 | return Sections[SectionID].LoadAddress; |
| 182 | } |
| 183 | |
Jim Grosbach | 61425c0 | 2012-01-16 22:26:39 +0000 | [diff] [blame] | 184 | uint8_t *getSectionAddress(unsigned SectionID) { |
Danil Malyshev | 0e4fa5f | 2012-03-30 16:45:19 +0000 | [diff] [blame] | 185 | return (uint8_t*)Sections[SectionID].Address; |
Jim Grosbach | 61425c0 | 2012-01-16 22:26:39 +0000 | [diff] [blame] | 186 | } |
Danil Malyshev | cf852dc | 2011-07-13 07:57:58 +0000 | [diff] [blame] | 187 | |
Eli Bendersky | c201e6e | 2012-05-01 10:41:12 +0000 | [diff] [blame] | 188 | /// \brief Given the common symbols discovered in the object file, emit a |
| 189 | /// new section for them and update the symbol mappings in the object and |
| 190 | /// symbol table. |
| 191 | void emitCommonSymbols(ObjectImage &Obj, |
| 192 | const CommonSymbolMap &CommonSymbols, |
| 193 | uint64_t TotalSize, |
| 194 | SymbolTableMap &SymbolTable); |
Preston Gurd | c68dda8 | 2012-04-12 20:13:57 +0000 | [diff] [blame] | 195 | |
Danil Malyshev | 0e4fa5f | 2012-03-30 16:45:19 +0000 | [diff] [blame] | 196 | /// \brief Emits section data from the object file to the MemoryManager. |
| 197 | /// \param IsCode if it's true then allocateCodeSection() will be |
Eli Bendersky | 5fe0198 | 2012-04-29 12:40:47 +0000 | [diff] [blame] | 198 | /// used for emits, else allocateDataSection() will be used. |
Danil Malyshev | 0e4fa5f | 2012-03-30 16:45:19 +0000 | [diff] [blame] | 199 | /// \return SectionID. |
Preston Gurd | 689ff9c | 2012-04-16 22:12:58 +0000 | [diff] [blame] | 200 | unsigned emitSection(ObjectImage &Obj, |
| 201 | const SectionRef &Section, |
| 202 | bool IsCode); |
Danil Malyshev | 0e4fa5f | 2012-03-30 16:45:19 +0000 | [diff] [blame] | 203 | |
| 204 | /// \brief Find Section in LocalSections. If the secton is not found - emit |
| 205 | /// it and store in LocalSections. |
| 206 | /// \param IsCode if it's true then allocateCodeSection() will be |
| 207 | /// used for emmits, else allocateDataSection() will be used. |
| 208 | /// \return SectionID. |
Preston Gurd | 689ff9c | 2012-04-16 22:12:58 +0000 | [diff] [blame] | 209 | unsigned findOrEmitSection(ObjectImage &Obj, |
| 210 | const SectionRef &Section, |
| 211 | bool IsCode, |
Danil Malyshev | 0e4fa5f | 2012-03-30 16:45:19 +0000 | [diff] [blame] | 212 | ObjSectionToIDMap &LocalSections); |
| 213 | |
Eli Bendersky | c201e6e | 2012-05-01 10:41:12 +0000 | [diff] [blame] | 214 | // \brief Add a relocation entry that uses the given section. |
| 215 | void addRelocationForSection(const RelocationEntry &RE, unsigned SectionID); |
| 216 | |
| 217 | // \brief Add a relocation entry that uses the given symbol. This symbol may |
| 218 | // be found in the global symbol table, or it may be external. |
| 219 | void addRelocationForSymbol(const RelocationEntry &RE, StringRef SymbolName); |
Danil Malyshev | 0e4fa5f | 2012-03-30 16:45:19 +0000 | [diff] [blame] | 220 | |
| 221 | /// \brief Emits long jump instruction to Addr. |
| 222 | /// \return Pointer to the memory area for emitting target address. |
| 223 | uint8_t* createStubFunction(uint8_t *Addr); |
| 224 | |
| 225 | /// \brief Resolves relocations from Relocs list with address from Value. |
| 226 | void resolveRelocationList(const RelocationList &Relocs, uint64_t Value); |
| 227 | void resolveRelocationEntry(const RelocationEntry &RE, uint64_t Value); |
| 228 | |
| 229 | /// \brief A object file specific relocation resolver |
Danil Malyshev | 5229741 | 2012-08-27 15:34:01 +0000 | [diff] [blame] | 230 | /// \param LocalAddress The address to apply the relocation action |
| 231 | /// \param FinalAddress If the linker prepare code for remote executon then |
| 232 | /// FinalAddress has the remote address to apply the |
| 233 | /// relocation action, otherwise is same as LocalAddress |
Danil Malyshev | 0e4fa5f | 2012-03-30 16:45:19 +0000 | [diff] [blame] | 234 | /// \param Value Target symbol address to apply the relocation action |
| 235 | /// \param Type object file specific relocation type |
| 236 | /// \param Addend A constant addend used to compute the value to be stored |
| 237 | /// into the relocatable field |
| 238 | virtual void resolveRelocation(uint8_t *LocalAddress, |
| 239 | uint64_t FinalAddress, |
| 240 | uint64_t Value, |
| 241 | uint32_t Type, |
| 242 | int64_t Addend) = 0; |
| 243 | |
Eli Bendersky | 5fe0198 | 2012-04-29 12:40:47 +0000 | [diff] [blame] | 244 | /// \brief Parses the object file relocation and stores it to Relocations |
| 245 | /// or SymbolRelocations (this depends on the object file type). |
Danil Malyshev | 0e4fa5f | 2012-03-30 16:45:19 +0000 | [diff] [blame] | 246 | virtual void processRelocationRef(const ObjRelocationInfo &Rel, |
Preston Gurd | 689ff9c | 2012-04-16 22:12:58 +0000 | [diff] [blame] | 247 | ObjectImage &Obj, |
Danil Malyshev | 0e4fa5f | 2012-03-30 16:45:19 +0000 | [diff] [blame] | 248 | ObjSectionToIDMap &ObjSectionToID, |
Eli Bendersky | d98c9e9 | 2012-05-01 06:58:59 +0000 | [diff] [blame] | 249 | const SymbolTableMap &Symbols, |
Eli Bendersky | 5fe0198 | 2012-04-29 12:40:47 +0000 | [diff] [blame] | 250 | StubMap &Stubs) = 0; |
Danil Malyshev | 0e4fa5f | 2012-03-30 16:45:19 +0000 | [diff] [blame] | 251 | |
Eli Bendersky | 37bc5a2 | 2012-04-30 12:15:58 +0000 | [diff] [blame] | 252 | /// \brief Resolve relocations to external symbols. |
| 253 | void resolveExternalSymbols(); |
Preston Gurd | 689ff9c | 2012-04-16 22:12:58 +0000 | [diff] [blame] | 254 | virtual ObjectImage *createObjectImage(const MemoryBuffer *InputBuffer); |
| 255 | virtual void handleObjectLoaded(ObjectImage *Obj) |
| 256 | { |
| 257 | // Subclasses may choose to retain this image if they have a use for it |
| 258 | delete Obj; |
| 259 | } |
| 260 | |
Danil Malyshev | cf852dc | 2011-07-13 07:57:58 +0000 | [diff] [blame] | 261 | public: |
| 262 | RuntimeDyldImpl(RTDyldMemoryManager *mm) : MemMgr(mm), HasError(false) {} |
| 263 | |
| 264 | virtual ~RuntimeDyldImpl(); |
| 265 | |
Danil Malyshev | 0e4fa5f | 2012-03-30 16:45:19 +0000 | [diff] [blame] | 266 | bool loadObject(const MemoryBuffer *InputBuffer); |
Danil Malyshev | cf852dc | 2011-07-13 07:57:58 +0000 | [diff] [blame] | 267 | |
| 268 | void *getSymbolAddress(StringRef Name) { |
| 269 | // FIXME: Just look up as a function for now. Overly simple of course. |
| 270 | // Work in progress. |
Eli Bendersky | d98c9e9 | 2012-05-01 06:58:59 +0000 | [diff] [blame] | 271 | if (GlobalSymbolTable.find(Name) == GlobalSymbolTable.end()) |
Jim Grosbach | 61425c0 | 2012-01-16 22:26:39 +0000 | [diff] [blame] | 272 | return 0; |
Eli Bendersky | d98c9e9 | 2012-05-01 06:58:59 +0000 | [diff] [blame] | 273 | SymbolLoc Loc = GlobalSymbolTable.lookup(Name); |
Jim Grosbach | 61425c0 | 2012-01-16 22:26:39 +0000 | [diff] [blame] | 274 | return getSectionAddress(Loc.first) + Loc.second; |
Danil Malyshev | cf852dc | 2011-07-13 07:57:58 +0000 | [diff] [blame] | 275 | } |
| 276 | |
Jim Grosbach | 35ed842 | 2012-09-05 16:50:40 +0000 | [diff] [blame^] | 277 | uint64_t getSymbolLoadAddress(StringRef Name) { |
| 278 | // FIXME: Just look up as a function for now. Overly simple of course. |
| 279 | // Work in progress. |
| 280 | if (GlobalSymbolTable.find(Name) == GlobalSymbolTable.end()) |
| 281 | return 0; |
| 282 | SymbolLoc Loc = GlobalSymbolTable.lookup(Name); |
| 283 | return getSectionLoadAddress(Loc.first) + Loc.second; |
| 284 | } |
| 285 | |
Danil Malyshev | 0e4fa5f | 2012-03-30 16:45:19 +0000 | [diff] [blame] | 286 | void resolveRelocations(); |
Danil Malyshev | cf852dc | 2011-07-13 07:57:58 +0000 | [diff] [blame] | 287 | |
Danil Malyshev | 0e4fa5f | 2012-03-30 16:45:19 +0000 | [diff] [blame] | 288 | void reassignSectionAddress(unsigned SectionID, uint64_t Addr); |
Danil Malyshev | cf852dc | 2011-07-13 07:57:58 +0000 | [diff] [blame] | 289 | |
Jim Grosbach | 020f4e8 | 2012-01-16 23:50:55 +0000 | [diff] [blame] | 290 | void mapSectionAddress(void *LocalAddress, uint64_t TargetAddress); |
| 291 | |
Danil Malyshev | cf852dc | 2011-07-13 07:57:58 +0000 | [diff] [blame] | 292 | // Is the linker in an error state? |
| 293 | bool hasError() { return HasError; } |
| 294 | |
| 295 | // Mark the error condition as handled and continue. |
| 296 | void clearError() { HasError = false; } |
| 297 | |
| 298 | // Get the error message. |
| 299 | StringRef getErrorString() { return ErrorStr; } |
| 300 | |
| 301 | virtual bool isCompatibleFormat(const MemoryBuffer *InputBuffer) const = 0; |
Danil Malyshev | 0e4fa5f | 2012-03-30 16:45:19 +0000 | [diff] [blame] | 302 | |
Danil Malyshev | cf852dc | 2011-07-13 07:57:58 +0000 | [diff] [blame] | 303 | }; |
| 304 | |
Danil Malyshev | cf852dc | 2011-07-13 07:57:58 +0000 | [diff] [blame] | 305 | } // end namespace llvm |
| 306 | |
| 307 | |
| 308 | #endif |