Jim Grosbach | 6e56331 | 2011-03-21 22:15:52 +0000 | [diff] [blame] | 1 | //===-- RuntimeDyld.h - Run-time dynamic linker for MC-JIT ------*- 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 | // Implementation of the MC-JIT runtime dynamic linker. |
| 11 | // |
| 12 | //===----------------------------------------------------------------------===// |
| 13 | |
Jim Grosbach | 8b54dca | 2011-03-23 19:52:00 +0000 | [diff] [blame] | 14 | #define DEBUG_TYPE "dyld" |
Jim Grosbach | 6e56331 | 2011-03-21 22:15:52 +0000 | [diff] [blame] | 15 | #include "llvm/ADT/OwningPtr.h" |
Jim Grosbach | 8b54dca | 2011-03-23 19:52:00 +0000 | [diff] [blame] | 16 | #include "llvm/ADT/SmallVector.h" |
Jim Grosbach | 6e56331 | 2011-03-21 22:15:52 +0000 | [diff] [blame] | 17 | #include "llvm/ADT/StringMap.h" |
| 18 | #include "llvm/ADT/StringRef.h" |
Jim Grosbach | c41ab78 | 2011-04-06 01:11:05 +0000 | [diff] [blame] | 19 | #include "llvm/ADT/STLExtras.h" |
Jim Grosbach | 6e56331 | 2011-03-21 22:15:52 +0000 | [diff] [blame] | 20 | #include "llvm/ADT/Twine.h" |
| 21 | #include "llvm/ExecutionEngine/RuntimeDyld.h" |
| 22 | #include "llvm/Object/MachOObject.h" |
Jim Grosbach | 8b54dca | 2011-03-23 19:52:00 +0000 | [diff] [blame] | 23 | #include "llvm/Support/Debug.h" |
| 24 | #include "llvm/Support/ErrorHandling.h" |
| 25 | #include "llvm/Support/Format.h" |
Jim Grosbach | 6e56331 | 2011-03-21 22:15:52 +0000 | [diff] [blame] | 26 | #include "llvm/Support/Memory.h" |
| 27 | #include "llvm/Support/MemoryBuffer.h" |
| 28 | #include "llvm/Support/system_error.h" |
Jim Grosbach | 8b54dca | 2011-03-23 19:52:00 +0000 | [diff] [blame] | 29 | #include "llvm/Support/raw_ostream.h" |
Jim Grosbach | 6e56331 | 2011-03-21 22:15:52 +0000 | [diff] [blame] | 30 | using namespace llvm; |
| 31 | using namespace llvm::object; |
| 32 | |
Chandler Carruth | 53c5e7b | 2011-04-05 23:54:31 +0000 | [diff] [blame] | 33 | // Empty out-of-line virtual destructor as the key function. |
| 34 | RTDyldMemoryManager::~RTDyldMemoryManager() {} |
| 35 | |
Jim Grosbach | 6e56331 | 2011-03-21 22:15:52 +0000 | [diff] [blame] | 36 | namespace llvm { |
| 37 | class RuntimeDyldImpl { |
Jim Grosbach | a8287e3 | 2011-03-23 22:06:06 +0000 | [diff] [blame] | 38 | unsigned CPUType; |
| 39 | unsigned CPUSubtype; |
| 40 | |
Jim Grosbach | fcbe5b7 | 2011-04-04 23:04:39 +0000 | [diff] [blame] | 41 | // The MemoryManager to load objects into. |
| 42 | RTDyldMemoryManager *MemMgr; |
Jim Grosbach | 5acfa9f | 2011-03-29 21:03:05 +0000 | [diff] [blame] | 43 | |
Jim Grosbach | f8c1c84 | 2011-04-12 21:20:41 +0000 | [diff] [blame] | 44 | // FIXME: This all assumes we're dealing with external symbols for anything |
| 45 | // explicitly referenced. I.e., we can index by name and things |
| 46 | // will work out. In practice, this may not be the case, so we |
| 47 | // should find a way to effectively generalize. |
Jim Grosbach | c41ab78 | 2011-04-06 01:11:05 +0000 | [diff] [blame] | 48 | |
| 49 | // For each function, we have a MemoryBlock of it's instruction data. |
| 50 | StringMap<sys::MemoryBlock> Functions; |
| 51 | |
Jim Grosbach | 6e56331 | 2011-03-21 22:15:52 +0000 | [diff] [blame] | 52 | // Master symbol table. As modules are loaded and external symbols are |
| 53 | // resolved, their addresses are stored here. |
Jim Grosbach | f8c1c84 | 2011-04-12 21:20:41 +0000 | [diff] [blame] | 54 | StringMap<uint8_t*> SymbolTable; |
Jim Grosbach | 6e56331 | 2011-03-21 22:15:52 +0000 | [diff] [blame] | 55 | |
Jim Grosbach | f8c1c84 | 2011-04-12 21:20:41 +0000 | [diff] [blame] | 56 | // For each symbol, keep a list of relocations based on it. Anytime |
| 57 | // its address is reassigned (the JIT re-compiled the function, e.g.), |
| 58 | // the relocations get re-resolved. |
| 59 | struct RelocationEntry { |
| 60 | std::string Target; // Object this relocation is contained in. |
| 61 | uint64_t Offset; // Offset into the object for the relocation. |
| 62 | uint32_t Data; // Second word of the raw macho relocation entry. |
| 63 | int64_t Addend; // Addend encoded in the instruction itself, if any. |
| 64 | bool isResolved; // Has this relocation been resolved previously? |
| 65 | |
| 66 | RelocationEntry(StringRef t, uint64_t offset, uint32_t data, int64_t addend) |
| 67 | : Target(t), Offset(offset), Data(data), Addend(addend), |
| 68 | isResolved(false) {} |
| 69 | }; |
| 70 | typedef SmallVector<RelocationEntry, 4> RelocationList; |
| 71 | StringMap<RelocationList> Relocations; |
| 72 | |
| 73 | // FIXME: Also keep a map of all the relocations contained in an object. Use |
| 74 | // this to dynamically answer whether all of the relocations in it have |
| 75 | // been resolved or not. |
Jim Grosbach | 6e56331 | 2011-03-21 22:15:52 +0000 | [diff] [blame] | 76 | |
| 77 | bool HasError; |
| 78 | std::string ErrorStr; |
| 79 | |
| 80 | // Set the error state and record an error string. |
| 81 | bool Error(const Twine &Msg) { |
| 82 | ErrorStr = Msg.str(); |
| 83 | HasError = true; |
| 84 | return true; |
| 85 | } |
| 86 | |
Jim Grosbach | c41ab78 | 2011-04-06 01:11:05 +0000 | [diff] [blame] | 87 | void extractFunction(StringRef Name, uint8_t *StartAddress, |
| 88 | uint8_t *EndAddress); |
Jim Grosbach | f8c1c84 | 2011-04-12 21:20:41 +0000 | [diff] [blame] | 89 | bool resolveRelocation(uint8_t *Address, uint8_t *Value, bool isPCRel, |
| 90 | unsigned Type, unsigned Size); |
| 91 | bool resolveX86_64Relocation(uintptr_t Address, uintptr_t Value, bool isPCRel, |
Jim Grosbach | a8287e3 | 2011-03-23 22:06:06 +0000 | [diff] [blame] | 92 | unsigned Type, unsigned Size); |
Jim Grosbach | f8c1c84 | 2011-04-12 21:20:41 +0000 | [diff] [blame] | 93 | bool resolveARMRelocation(uintptr_t Address, uintptr_t Value, bool isPCRel, |
Jim Grosbach | a8287e3 | 2011-03-23 22:06:06 +0000 | [diff] [blame] | 94 | unsigned Type, unsigned Size); |
Jim Grosbach | 8b54dca | 2011-03-23 19:52:00 +0000 | [diff] [blame] | 95 | |
Jim Grosbach | 6e56331 | 2011-03-21 22:15:52 +0000 | [diff] [blame] | 96 | bool loadSegment32(const MachOObject *Obj, |
| 97 | const MachOObject::LoadCommandInfo *SegmentLCI, |
| 98 | const InMemoryStruct<macho::SymtabLoadCommand> &SymtabLC); |
| 99 | bool loadSegment64(const MachOObject *Obj, |
| 100 | const MachOObject::LoadCommandInfo *SegmentLCI, |
| 101 | const InMemoryStruct<macho::SymtabLoadCommand> &SymtabLC); |
| 102 | |
| 103 | public: |
Jim Grosbach | fcbe5b7 | 2011-04-04 23:04:39 +0000 | [diff] [blame] | 104 | RuntimeDyldImpl(RTDyldMemoryManager *mm) : MemMgr(mm), HasError(false) {} |
Jim Grosbach | 8371c89 | 2011-03-22 00:42:19 +0000 | [diff] [blame] | 105 | |
Jim Grosbach | 6e56331 | 2011-03-21 22:15:52 +0000 | [diff] [blame] | 106 | bool loadObject(MemoryBuffer *InputBuffer); |
| 107 | |
Jim Grosbach | b027105 | 2011-04-08 17:31:24 +0000 | [diff] [blame] | 108 | void *getSymbolAddress(StringRef Name) { |
Jim Grosbach | c41ab78 | 2011-04-06 01:11:05 +0000 | [diff] [blame] | 109 | // FIXME: Just look up as a function for now. Overly simple of course. |
| 110 | // Work in progress. |
Jim Grosbach | f8c1c84 | 2011-04-12 21:20:41 +0000 | [diff] [blame] | 111 | return SymbolTable.lookup(Name); |
Jim Grosbach | 6e56331 | 2011-03-21 22:15:52 +0000 | [diff] [blame] | 112 | } |
| 113 | |
Jim Grosbach | f8c1c84 | 2011-04-12 21:20:41 +0000 | [diff] [blame] | 114 | void resolveRelocations(); |
| 115 | |
| 116 | void reassignSymbolAddress(StringRef Name, uint8_t *Addr); |
| 117 | |
Jim Grosbach | 6e56331 | 2011-03-21 22:15:52 +0000 | [diff] [blame] | 118 | // Is the linker in an error state? |
| 119 | bool hasError() { return HasError; } |
| 120 | |
| 121 | // Mark the error condition as handled and continue. |
| 122 | void clearError() { HasError = false; } |
| 123 | |
| 124 | // Get the error message. |
| 125 | StringRef getErrorString() { return ErrorStr; } |
| 126 | }; |
| 127 | |
Jim Grosbach | c41ab78 | 2011-04-06 01:11:05 +0000 | [diff] [blame] | 128 | void RuntimeDyldImpl::extractFunction(StringRef Name, uint8_t *StartAddress, |
Jim Grosbach | 01ccab4 | 2011-04-06 22:13:52 +0000 | [diff] [blame] | 129 | uint8_t *EndAddress) { |
Jim Grosbach | c41ab78 | 2011-04-06 01:11:05 +0000 | [diff] [blame] | 130 | // Allocate memory for the function via the memory manager. |
| 131 | uintptr_t Size = EndAddress - StartAddress + 1; |
Jim Grosbach | ffa6250 | 2011-05-13 20:12:14 +0000 | [diff] [blame] | 132 | uintptr_t AllocSize = Size; |
| 133 | uint8_t *Mem = MemMgr->startFunctionBody(Name.data(), AllocSize); |
Jim Grosbach | c41ab78 | 2011-04-06 01:11:05 +0000 | [diff] [blame] | 134 | assert(Size >= (uint64_t)(EndAddress - StartAddress + 1) && |
| 135 | "Memory manager failed to allocate enough memory!"); |
| 136 | // Copy the function payload into the memory block. |
Jim Grosbach | ffa6250 | 2011-05-13 20:12:14 +0000 | [diff] [blame] | 137 | memcpy(Mem, StartAddress, Size); |
Jim Grosbach | c41ab78 | 2011-04-06 01:11:05 +0000 | [diff] [blame] | 138 | MemMgr->endFunctionBody(Name.data(), Mem, Mem + Size); |
| 139 | // Remember where we put it. |
| 140 | Functions[Name] = sys::MemoryBlock(Mem, Size); |
Jim Grosbach | f8c1c84 | 2011-04-12 21:20:41 +0000 | [diff] [blame] | 141 | // Default the assigned address for this symbol to wherever this |
| 142 | // allocated it. |
| 143 | SymbolTable[Name] = Mem; |
Jim Grosbach | ffa6250 | 2011-05-13 20:12:14 +0000 | [diff] [blame] | 144 | DEBUG(dbgs() << " allocated to [" << Mem << ", " << Mem + Size << "]\n"); |
Jim Grosbach | c41ab78 | 2011-04-06 01:11:05 +0000 | [diff] [blame] | 145 | } |
| 146 | |
Jim Grosbach | 8b54dca | 2011-03-23 19:52:00 +0000 | [diff] [blame] | 147 | bool RuntimeDyldImpl:: |
Jim Grosbach | f8c1c84 | 2011-04-12 21:20:41 +0000 | [diff] [blame] | 148 | resolveRelocation(uint8_t *Address, uint8_t *Value, bool isPCRel, |
| 149 | unsigned Type, unsigned Size) { |
| 150 | // This just dispatches to the proper target specific routine. |
Jim Grosbach | a8287e3 | 2011-03-23 22:06:06 +0000 | [diff] [blame] | 151 | switch (CPUType) { |
| 152 | default: assert(0 && "Unsupported CPU type!"); |
| 153 | case mach::CTM_x86_64: |
Jim Grosbach | f8c1c84 | 2011-04-12 21:20:41 +0000 | [diff] [blame] | 154 | return resolveX86_64Relocation((uintptr_t)Address, (uintptr_t)Value, |
| 155 | isPCRel, Type, Size); |
Jim Grosbach | a8287e3 | 2011-03-23 22:06:06 +0000 | [diff] [blame] | 156 | case mach::CTM_ARM: |
Jim Grosbach | f8c1c84 | 2011-04-12 21:20:41 +0000 | [diff] [blame] | 157 | return resolveARMRelocation((uintptr_t)Address, (uintptr_t)Value, |
| 158 | isPCRel, Type, Size); |
Jim Grosbach | a8287e3 | 2011-03-23 22:06:06 +0000 | [diff] [blame] | 159 | } |
| 160 | llvm_unreachable(""); |
| 161 | } |
| 162 | |
Jim Grosbach | f8c1c84 | 2011-04-12 21:20:41 +0000 | [diff] [blame] | 163 | bool RuntimeDyldImpl:: |
| 164 | resolveX86_64Relocation(uintptr_t Address, uintptr_t Value, |
| 165 | bool isPCRel, unsigned Type, |
| 166 | unsigned Size) { |
Jim Grosbach | 8b54dca | 2011-03-23 19:52:00 +0000 | [diff] [blame] | 167 | // If the relocation is PC-relative, the value to be encoded is the |
| 168 | // pointer difference. |
| 169 | if (isPCRel) |
| 170 | // FIXME: It seems this value needs to be adjusted by 4 for an effective PC |
| 171 | // address. Is that expected? Only for branches, perhaps? |
| 172 | Value -= Address + 4; |
| 173 | |
| 174 | switch(Type) { |
| 175 | default: |
| 176 | llvm_unreachable("Invalid relocation type!"); |
| 177 | case macho::RIT_X86_64_Unsigned: |
| 178 | case macho::RIT_X86_64_Branch: { |
| 179 | // Mask in the target value a byte at a time (we don't have an alignment |
| 180 | // guarantee for the target address, so this is safest). |
Jim Grosbach | 8b54dca | 2011-03-23 19:52:00 +0000 | [diff] [blame] | 181 | uint8_t *p = (uint8_t*)Address; |
Jim Grosbach | a8287e3 | 2011-03-23 22:06:06 +0000 | [diff] [blame] | 182 | for (unsigned i = 0; i < Size; ++i) { |
Jim Grosbach | 8b54dca | 2011-03-23 19:52:00 +0000 | [diff] [blame] | 183 | *p++ = (uint8_t)Value; |
| 184 | Value >>= 8; |
| 185 | } |
| 186 | return false; |
| 187 | } |
| 188 | case macho::RIT_X86_64_Signed: |
| 189 | case macho::RIT_X86_64_GOTLoad: |
| 190 | case macho::RIT_X86_64_GOT: |
| 191 | case macho::RIT_X86_64_Subtractor: |
| 192 | case macho::RIT_X86_64_Signed1: |
| 193 | case macho::RIT_X86_64_Signed2: |
| 194 | case macho::RIT_X86_64_Signed4: |
| 195 | case macho::RIT_X86_64_TLV: |
| 196 | return Error("Relocation type not implemented yet!"); |
| 197 | } |
| 198 | return false; |
| 199 | } |
Jim Grosbach | 6e56331 | 2011-03-21 22:15:52 +0000 | [diff] [blame] | 200 | |
Jim Grosbach | f8c1c84 | 2011-04-12 21:20:41 +0000 | [diff] [blame] | 201 | bool RuntimeDyldImpl::resolveARMRelocation(uintptr_t Address, uintptr_t Value, |
Jim Grosbach | a8287e3 | 2011-03-23 22:06:06 +0000 | [diff] [blame] | 202 | bool isPCRel, unsigned Type, |
| 203 | unsigned Size) { |
| 204 | // If the relocation is PC-relative, the value to be encoded is the |
| 205 | // pointer difference. |
| 206 | if (isPCRel) { |
| 207 | Value -= Address; |
| 208 | // ARM PCRel relocations have an effective-PC offset of two instructions |
| 209 | // (four bytes in Thumb mode, 8 bytes in ARM mode). |
| 210 | // FIXME: For now, assume ARM mode. |
| 211 | Value -= 8; |
| 212 | } |
| 213 | |
| 214 | switch(Type) { |
| 215 | default: |
Jim Grosbach | f8c1c84 | 2011-04-12 21:20:41 +0000 | [diff] [blame] | 216 | llvm_unreachable("Invalid relocation type!"); |
Jim Grosbach | a8287e3 | 2011-03-23 22:06:06 +0000 | [diff] [blame] | 217 | case macho::RIT_Vanilla: { |
| 218 | llvm_unreachable("Invalid relocation type!"); |
| 219 | // Mask in the target value a byte at a time (we don't have an alignment |
| 220 | // guarantee for the target address, so this is safest). |
| 221 | uint8_t *p = (uint8_t*)Address; |
| 222 | for (unsigned i = 0; i < Size; ++i) { |
| 223 | *p++ = (uint8_t)Value; |
| 224 | Value >>= 8; |
| 225 | } |
Jim Grosbach | 5ffe37f | 2011-03-23 23:35:17 +0000 | [diff] [blame] | 226 | break; |
Jim Grosbach | a8287e3 | 2011-03-23 22:06:06 +0000 | [diff] [blame] | 227 | } |
Jim Grosbach | 5ffe37f | 2011-03-23 23:35:17 +0000 | [diff] [blame] | 228 | case macho::RIT_ARM_Branch24Bit: { |
| 229 | // Mask the value into the target address. We know instructions are |
| 230 | // 32-bit aligned, so we can do it all at once. |
| 231 | uint32_t *p = (uint32_t*)Address; |
| 232 | // The low two bits of the value are not encoded. |
| 233 | Value >>= 2; |
| 234 | // Mask the value to 24 bits. |
| 235 | Value &= 0xffffff; |
| 236 | // FIXME: If the destination is a Thumb function (and the instruction |
| 237 | // is a non-predicated BL instruction), we need to change it to a BLX |
| 238 | // instruction instead. |
| 239 | |
| 240 | // Insert the value into the instruction. |
| 241 | *p = (*p & ~0xffffff) | Value; |
| 242 | break; |
| 243 | } |
Jim Grosbach | a8287e3 | 2011-03-23 22:06:06 +0000 | [diff] [blame] | 244 | case macho::RIT_ARM_ThumbBranch22Bit: |
| 245 | case macho::RIT_ARM_ThumbBranch32Bit: |
| 246 | case macho::RIT_ARM_Half: |
| 247 | case macho::RIT_ARM_HalfDifference: |
Jim Grosbach | f8c1c84 | 2011-04-12 21:20:41 +0000 | [diff] [blame] | 248 | case macho::RIT_Pair: |
| 249 | case macho::RIT_Difference: |
| 250 | case macho::RIT_ARM_LocalDifference: |
| 251 | case macho::RIT_ARM_PreboundLazyPointer: |
Jim Grosbach | a8287e3 | 2011-03-23 22:06:06 +0000 | [diff] [blame] | 252 | return Error("Relocation type not implemented yet!"); |
| 253 | } |
| 254 | return false; |
| 255 | } |
| 256 | |
Jim Grosbach | 6e56331 | 2011-03-21 22:15:52 +0000 | [diff] [blame] | 257 | bool RuntimeDyldImpl:: |
| 258 | loadSegment32(const MachOObject *Obj, |
| 259 | const MachOObject::LoadCommandInfo *SegmentLCI, |
| 260 | const InMemoryStruct<macho::SymtabLoadCommand> &SymtabLC) { |
Jim Grosbach | f8c1c84 | 2011-04-12 21:20:41 +0000 | [diff] [blame] | 261 | InMemoryStruct<macho::SegmentLoadCommand> SegmentLC; |
| 262 | Obj->ReadSegmentLoadCommand(*SegmentLCI, SegmentLC); |
| 263 | if (!SegmentLC) |
Jim Grosbach | 6e56331 | 2011-03-21 22:15:52 +0000 | [diff] [blame] | 264 | return Error("unable to load segment load command"); |
| 265 | |
Jim Grosbach | f8c1c84 | 2011-04-12 21:20:41 +0000 | [diff] [blame] | 266 | for (unsigned SectNum = 0; SectNum != SegmentLC->NumSections; ++SectNum) { |
Jim Grosbach | 6e56331 | 2011-03-21 22:15:52 +0000 | [diff] [blame] | 267 | InMemoryStruct<macho::Section> Sect; |
Jim Grosbach | b027105 | 2011-04-08 17:31:24 +0000 | [diff] [blame] | 268 | Obj->ReadSection(*SegmentLCI, SectNum, Sect); |
| 269 | if (!Sect) |
| 270 | return Error("unable to load section: '" + Twine(SectNum) + "'"); |
Jim Grosbach | 6e56331 | 2011-03-21 22:15:52 +0000 | [diff] [blame] | 271 | |
Jim Grosbach | 757a142 | 2011-05-12 21:21:16 +0000 | [diff] [blame] | 272 | // FIXME: For the time being, we're only loading text segments. |
Jim Grosbach | b027105 | 2011-04-08 17:31:24 +0000 | [diff] [blame] | 273 | if (Sect->Flags != 0x80000400) |
Jim Grosbach | 757a142 | 2011-05-12 21:21:16 +0000 | [diff] [blame] | 274 | continue; |
Jim Grosbach | 6e56331 | 2011-03-21 22:15:52 +0000 | [diff] [blame] | 275 | |
Jim Grosbach | b027105 | 2011-04-08 17:31:24 +0000 | [diff] [blame] | 276 | // Address and names of symbols in the section. |
| 277 | typedef std::pair<uint64_t, StringRef> SymbolEntry; |
Jim Grosbach | f8c1c84 | 2011-04-12 21:20:41 +0000 | [diff] [blame] | 278 | SmallVector<SymbolEntry, 64> Symbols; |
| 279 | // Index of all the names, in this section or not. Used when we're |
| 280 | // dealing with relocation entries. |
| 281 | SmallVector<StringRef, 64> SymbolNames; |
Jim Grosbach | b027105 | 2011-04-08 17:31:24 +0000 | [diff] [blame] | 282 | for (unsigned i = 0; i != SymtabLC->NumSymbolTableEntries; ++i) { |
| 283 | InMemoryStruct<macho::SymbolTableEntry> STE; |
| 284 | Obj->ReadSymbolTableEntry(SymtabLC->SymbolTableOffset, i, STE); |
| 285 | if (!STE) |
| 286 | return Error("unable to read symbol: '" + Twine(i) + "'"); |
Jim Grosbach | f8c1c84 | 2011-04-12 21:20:41 +0000 | [diff] [blame] | 287 | if (STE->SectionIndex > SegmentLC->NumSections) |
Benjamin Kramer | cc513e1c | 2011-04-09 10:10:35 +0000 | [diff] [blame] | 288 | return Error("invalid section index for symbol: '" + Twine(i) + "'"); |
Jim Grosbach | f8c1c84 | 2011-04-12 21:20:41 +0000 | [diff] [blame] | 289 | // Get the symbol name. |
| 290 | StringRef Name = Obj->getStringAtIndex(STE->StringIndex); |
| 291 | SymbolNames.push_back(Name); |
Jim Grosbach | b027105 | 2011-04-08 17:31:24 +0000 | [diff] [blame] | 292 | |
| 293 | // Just skip symbols not defined in this section. |
Jim Grosbach | e2e777b | 2011-04-08 21:11:20 +0000 | [diff] [blame] | 294 | if ((unsigned)STE->SectionIndex - 1 != SectNum) |
Jim Grosbach | b027105 | 2011-04-08 17:31:24 +0000 | [diff] [blame] | 295 | continue; |
| 296 | |
Jim Grosbach | b027105 | 2011-04-08 17:31:24 +0000 | [diff] [blame] | 297 | // FIXME: Check the symbol type and flags. |
| 298 | if (STE->Type != 0xF) // external, defined in this section. |
Jim Grosbach | 1c3c8ea | 2011-05-13 23:11:30 +0000 | [diff] [blame] | 299 | continue; |
Jim Grosbach | f8c1c84 | 2011-04-12 21:20:41 +0000 | [diff] [blame] | 300 | // Flags == 0x8 marks a thumb function for ARM, which is fine as it |
| 301 | // doesn't require any special handling here. |
| 302 | if (STE->Flags != 0x0 && STE->Flags != 0x8) |
Jim Grosbach | 1c3c8ea | 2011-05-13 23:11:30 +0000 | [diff] [blame] | 303 | continue; |
Jim Grosbach | b027105 | 2011-04-08 17:31:24 +0000 | [diff] [blame] | 304 | |
Jim Grosbach | b027105 | 2011-04-08 17:31:24 +0000 | [diff] [blame] | 305 | // Remember the symbol. |
Jim Grosbach | f8c1c84 | 2011-04-12 21:20:41 +0000 | [diff] [blame] | 306 | Symbols.push_back(SymbolEntry(STE->Value, Name)); |
Jim Grosbach | b027105 | 2011-04-08 17:31:24 +0000 | [diff] [blame] | 307 | |
Jim Grosbach | f8c1c84 | 2011-04-12 21:20:41 +0000 | [diff] [blame] | 308 | DEBUG(dbgs() << "Function sym: '" << Name << "' @ " << |
| 309 | (Sect->Address + STE->Value) << "\n"); |
Jim Grosbach | b027105 | 2011-04-08 17:31:24 +0000 | [diff] [blame] | 310 | } |
Jim Grosbach | f8c1c84 | 2011-04-12 21:20:41 +0000 | [diff] [blame] | 311 | // Sort the symbols by address, just in case they didn't come in that way. |
Jim Grosbach | b027105 | 2011-04-08 17:31:24 +0000 | [diff] [blame] | 312 | array_pod_sort(Symbols.begin(), Symbols.end()); |
| 313 | |
Jim Grosbach | 1c3c8ea | 2011-05-13 23:11:30 +0000 | [diff] [blame] | 314 | // If there weren't any functions (odd, but just in case...) |
| 315 | if (!Symbols.size()) |
| 316 | continue; |
| 317 | |
Jim Grosbach | b027105 | 2011-04-08 17:31:24 +0000 | [diff] [blame] | 318 | // Extract the function data. |
Jim Grosbach | f8c1c84 | 2011-04-12 21:20:41 +0000 | [diff] [blame] | 319 | uint8_t *Base = (uint8_t*)Obj->getData(SegmentLC->FileOffset, |
| 320 | SegmentLC->FileSize).data(); |
Jim Grosbach | b027105 | 2011-04-08 17:31:24 +0000 | [diff] [blame] | 321 | for (unsigned i = 0, e = Symbols.size() - 1; i != e; ++i) { |
Jim Grosbach | f8c1c84 | 2011-04-12 21:20:41 +0000 | [diff] [blame] | 322 | uint64_t StartOffset = Sect->Address + Symbols[i].first; |
Jim Grosbach | b027105 | 2011-04-08 17:31:24 +0000 | [diff] [blame] | 323 | uint64_t EndOffset = Symbols[i + 1].first - 1; |
| 324 | DEBUG(dbgs() << "Extracting function: " << Symbols[i].second |
| 325 | << " from [" << StartOffset << ", " << EndOffset << "]\n"); |
| 326 | extractFunction(Symbols[i].second, Base + StartOffset, Base + EndOffset); |
| 327 | } |
| 328 | // The last symbol we do after since the end address is calculated |
| 329 | // differently because there is no next symbol to reference. |
| 330 | uint64_t StartOffset = Symbols[Symbols.size() - 1].first; |
| 331 | uint64_t EndOffset = Sect->Size - 1; |
| 332 | DEBUG(dbgs() << "Extracting function: " << Symbols[Symbols.size()-1].second |
| 333 | << " from [" << StartOffset << ", " << EndOffset << "]\n"); |
| 334 | extractFunction(Symbols[Symbols.size()-1].second, |
| 335 | Base + StartOffset, Base + EndOffset); |
Jim Grosbach | 6e56331 | 2011-03-21 22:15:52 +0000 | [diff] [blame] | 336 | |
Jim Grosbach | f8c1c84 | 2011-04-12 21:20:41 +0000 | [diff] [blame] | 337 | // Now extract the relocation information for each function and process it. |
| 338 | for (unsigned j = 0; j != Sect->NumRelocationTableEntries; ++j) { |
| 339 | InMemoryStruct<macho::RelocationEntry> RE; |
| 340 | Obj->ReadRelocationEntry(Sect->RelocationTableOffset, j, RE); |
| 341 | if (RE->Word0 & macho::RF_Scattered) |
| 342 | return Error("NOT YET IMPLEMENTED: scattered relocations."); |
| 343 | // Word0 of the relocation is the offset into the section where the |
| 344 | // relocation should be applied. We need to translate that into an |
| 345 | // offset into a function since that's our atom. |
| 346 | uint32_t Offset = RE->Word0; |
| 347 | // Look for the function containing the address. This is used for JIT |
| 348 | // code, so the number of functions in section is almost always going |
| 349 | // to be very small (usually just one), so until we have use cases |
| 350 | // where that's not true, just use a trivial linear search. |
| 351 | unsigned SymbolNum; |
| 352 | unsigned NumSymbols = Symbols.size(); |
| 353 | assert(NumSymbols > 0 && Symbols[0].first <= Offset && |
| 354 | "No symbol containing relocation!"); |
| 355 | for (SymbolNum = 0; SymbolNum < NumSymbols - 1; ++SymbolNum) |
| 356 | if (Symbols[SymbolNum + 1].first > Offset) |
| 357 | break; |
| 358 | // Adjust the offset to be relative to the symbol. |
| 359 | Offset -= Symbols[SymbolNum].first; |
| 360 | // Get the name of the symbol containing the relocation. |
| 361 | StringRef TargetName = SymbolNames[SymbolNum]; |
| 362 | |
| 363 | bool isExtern = (RE->Word1 >> 27) & 1; |
| 364 | // Figure out the source symbol of the relocation. If isExtern is true, |
| 365 | // this relocation references the symbol table, otherwise it references |
| 366 | // a section in the same object, numbered from 1 through NumSections |
| 367 | // (SectionBases is [0, NumSections-1]). |
| 368 | // FIXME: Some targets (ARM) use internal relocations even for |
| 369 | // externally visible symbols, if the definition is in the same |
| 370 | // file as the reference. We need to convert those back to by-name |
| 371 | // references. We can resolve the address based on the section |
| 372 | // offset and see if we have a symbol at that address. If we do, |
| 373 | // use that; otherwise, puke. |
| 374 | if (!isExtern) |
| 375 | return Error("Internal relocations not supported."); |
| 376 | uint32_t SourceNum = RE->Word1 & 0xffffff; // 24-bit value |
| 377 | StringRef SourceName = SymbolNames[SourceNum]; |
| 378 | |
| 379 | // FIXME: Get the relocation addend from the target address. |
| 380 | |
| 381 | // Now store the relocation information. Associate it with the source |
| 382 | // symbol. |
| 383 | Relocations[SourceName].push_back(RelocationEntry(TargetName, |
| 384 | Offset, |
| 385 | RE->Word1, |
| 386 | 0 /*Addend*/)); |
| 387 | DEBUG(dbgs() << "Relocation at '" << TargetName << "' + " << Offset |
| 388 | << " from '" << SourceName << "(Word1: " |
| 389 | << format("0x%x", RE->Word1) << ")\n"); |
| 390 | } |
| 391 | } |
Jim Grosbach | 6e56331 | 2011-03-21 22:15:52 +0000 | [diff] [blame] | 392 | return false; |
| 393 | } |
| 394 | |
| 395 | |
| 396 | bool RuntimeDyldImpl:: |
| 397 | loadSegment64(const MachOObject *Obj, |
| 398 | const MachOObject::LoadCommandInfo *SegmentLCI, |
| 399 | const InMemoryStruct<macho::SymtabLoadCommand> &SymtabLC) { |
| 400 | InMemoryStruct<macho::Segment64LoadCommand> Segment64LC; |
| 401 | Obj->ReadSegment64LoadCommand(*SegmentLCI, Segment64LC); |
| 402 | if (!Segment64LC) |
| 403 | return Error("unable to load segment load command"); |
| 404 | |
Jim Grosbach | c41ab78 | 2011-04-06 01:11:05 +0000 | [diff] [blame] | 405 | for (unsigned SectNum = 0; SectNum != Segment64LC->NumSections; ++SectNum) { |
Jim Grosbach | 6e56331 | 2011-03-21 22:15:52 +0000 | [diff] [blame] | 406 | InMemoryStruct<macho::Section64> Sect; |
Jim Grosbach | c41ab78 | 2011-04-06 01:11:05 +0000 | [diff] [blame] | 407 | Obj->ReadSection64(*SegmentLCI, SectNum, Sect); |
Jim Grosbach | 6e56331 | 2011-03-21 22:15:52 +0000 | [diff] [blame] | 408 | if (!Sect) |
Jim Grosbach | c41ab78 | 2011-04-06 01:11:05 +0000 | [diff] [blame] | 409 | return Error("unable to load section: '" + Twine(SectNum) + "'"); |
Jim Grosbach | 6e56331 | 2011-03-21 22:15:52 +0000 | [diff] [blame] | 410 | |
Jim Grosbach | 757a142 | 2011-05-12 21:21:16 +0000 | [diff] [blame] | 411 | // FIXME: For the time being, we're only loading text segments. |
Jim Grosbach | 6e56331 | 2011-03-21 22:15:52 +0000 | [diff] [blame] | 412 | if (Sect->Flags != 0x80000400) |
Jim Grosbach | 757a142 | 2011-05-12 21:21:16 +0000 | [diff] [blame] | 413 | continue; |
Jim Grosbach | 6e56331 | 2011-03-21 22:15:52 +0000 | [diff] [blame] | 414 | |
Jim Grosbach | c41ab78 | 2011-04-06 01:11:05 +0000 | [diff] [blame] | 415 | // Address and names of symbols in the section. |
| 416 | typedef std::pair<uint64_t, StringRef> SymbolEntry; |
| 417 | SmallVector<SymbolEntry, 64> Symbols; |
Jim Grosbach | f8c1c84 | 2011-04-12 21:20:41 +0000 | [diff] [blame] | 418 | // Index of all the names, in this section or not. Used when we're |
| 419 | // dealing with relocation entries. |
| 420 | SmallVector<StringRef, 64> SymbolNames; |
Jim Grosbach | c41ab78 | 2011-04-06 01:11:05 +0000 | [diff] [blame] | 421 | for (unsigned i = 0; i != SymtabLC->NumSymbolTableEntries; ++i) { |
| 422 | InMemoryStruct<macho::Symbol64TableEntry> STE; |
| 423 | Obj->ReadSymbol64TableEntry(SymtabLC->SymbolTableOffset, i, STE); |
| 424 | if (!STE) |
| 425 | return Error("unable to read symbol: '" + Twine(i) + "'"); |
| 426 | if (STE->SectionIndex > Segment64LC->NumSections) |
Benjamin Kramer | cc513e1c | 2011-04-09 10:10:35 +0000 | [diff] [blame] | 427 | return Error("invalid section index for symbol: '" + Twine(i) + "'"); |
Jim Grosbach | f8c1c84 | 2011-04-12 21:20:41 +0000 | [diff] [blame] | 428 | // Get the symbol name. |
| 429 | StringRef Name = Obj->getStringAtIndex(STE->StringIndex); |
| 430 | SymbolNames.push_back(Name); |
Jim Grosbach | c41ab78 | 2011-04-06 01:11:05 +0000 | [diff] [blame] | 431 | |
| 432 | // Just skip symbols not defined in this section. |
Jim Grosbach | e2e777b | 2011-04-08 21:11:20 +0000 | [diff] [blame] | 433 | if ((unsigned)STE->SectionIndex - 1 != SectNum) |
Jim Grosbach | c41ab78 | 2011-04-06 01:11:05 +0000 | [diff] [blame] | 434 | continue; |
| 435 | |
Jim Grosbach | c41ab78 | 2011-04-06 01:11:05 +0000 | [diff] [blame] | 436 | // FIXME: Check the symbol type and flags. |
| 437 | if (STE->Type != 0xF) // external, defined in this section. |
Jim Grosbach | 1c3c8ea | 2011-05-13 23:11:30 +0000 | [diff] [blame] | 438 | continue; |
Jim Grosbach | c41ab78 | 2011-04-06 01:11:05 +0000 | [diff] [blame] | 439 | if (STE->Flags != 0x0) |
Jim Grosbach | 1c3c8ea | 2011-05-13 23:11:30 +0000 | [diff] [blame] | 440 | continue; |
Jim Grosbach | c41ab78 | 2011-04-06 01:11:05 +0000 | [diff] [blame] | 441 | |
Jim Grosbach | c41ab78 | 2011-04-06 01:11:05 +0000 | [diff] [blame] | 442 | // Remember the symbol. |
Jim Grosbach | f8c1c84 | 2011-04-12 21:20:41 +0000 | [diff] [blame] | 443 | Symbols.push_back(SymbolEntry(STE->Value, Name)); |
Jim Grosbach | c41ab78 | 2011-04-06 01:11:05 +0000 | [diff] [blame] | 444 | |
Jim Grosbach | f8c1c84 | 2011-04-12 21:20:41 +0000 | [diff] [blame] | 445 | DEBUG(dbgs() << "Function sym: '" << Name << "' @ " << |
| 446 | (Sect->Address + STE->Value) << "\n"); |
Jim Grosbach | c41ab78 | 2011-04-06 01:11:05 +0000 | [diff] [blame] | 447 | } |
Jim Grosbach | f8c1c84 | 2011-04-12 21:20:41 +0000 | [diff] [blame] | 448 | // Sort the symbols by address, just in case they didn't come in that way. |
Jim Grosbach | c41ab78 | 2011-04-06 01:11:05 +0000 | [diff] [blame] | 449 | array_pod_sort(Symbols.begin(), Symbols.end()); |
| 450 | |
Jim Grosbach | 1c3c8ea | 2011-05-13 23:11:30 +0000 | [diff] [blame] | 451 | // If there weren't any functions (odd, but just in case...) |
| 452 | if (!Symbols.size()) |
| 453 | continue; |
| 454 | |
Jim Grosbach | c41ab78 | 2011-04-06 01:11:05 +0000 | [diff] [blame] | 455 | // Extract the function data. |
| 456 | uint8_t *Base = (uint8_t*)Obj->getData(Segment64LC->FileOffset, |
| 457 | Segment64LC->FileSize).data(); |
| 458 | for (unsigned i = 0, e = Symbols.size() - 1; i != e; ++i) { |
Jim Grosbach | f8c1c84 | 2011-04-12 21:20:41 +0000 | [diff] [blame] | 459 | uint64_t StartOffset = Sect->Address + Symbols[i].first; |
Jim Grosbach | c41ab78 | 2011-04-06 01:11:05 +0000 | [diff] [blame] | 460 | uint64_t EndOffset = Symbols[i + 1].first - 1; |
| 461 | DEBUG(dbgs() << "Extracting function: " << Symbols[i].second |
| 462 | << " from [" << StartOffset << ", " << EndOffset << "]\n"); |
| 463 | extractFunction(Symbols[i].second, Base + StartOffset, Base + EndOffset); |
| 464 | } |
| 465 | // The last symbol we do after since the end address is calculated |
| 466 | // differently because there is no next symbol to reference. |
| 467 | uint64_t StartOffset = Symbols[Symbols.size() - 1].first; |
| 468 | uint64_t EndOffset = Sect->Size - 1; |
| 469 | DEBUG(dbgs() << "Extracting function: " << Symbols[Symbols.size()-1].second |
| 470 | << " from [" << StartOffset << ", " << EndOffset << "]\n"); |
| 471 | extractFunction(Symbols[Symbols.size()-1].second, |
| 472 | Base + StartOffset, Base + EndOffset); |
Jim Grosbach | 6e56331 | 2011-03-21 22:15:52 +0000 | [diff] [blame] | 473 | |
Jim Grosbach | f8c1c84 | 2011-04-12 21:20:41 +0000 | [diff] [blame] | 474 | // Now extract the relocation information for each function and process it. |
| 475 | for (unsigned j = 0; j != Sect->NumRelocationTableEntries; ++j) { |
| 476 | InMemoryStruct<macho::RelocationEntry> RE; |
| 477 | Obj->ReadRelocationEntry(Sect->RelocationTableOffset, j, RE); |
| 478 | if (RE->Word0 & macho::RF_Scattered) |
| 479 | return Error("NOT YET IMPLEMENTED: scattered relocations."); |
| 480 | // Word0 of the relocation is the offset into the section where the |
| 481 | // relocation should be applied. We need to translate that into an |
| 482 | // offset into a function since that's our atom. |
| 483 | uint32_t Offset = RE->Word0; |
| 484 | // Look for the function containing the address. This is used for JIT |
| 485 | // code, so the number of functions in section is almost always going |
| 486 | // to be very small (usually just one), so until we have use cases |
| 487 | // where that's not true, just use a trivial linear search. |
| 488 | unsigned SymbolNum; |
| 489 | unsigned NumSymbols = Symbols.size(); |
| 490 | assert(NumSymbols > 0 && Symbols[0].first <= Offset && |
| 491 | "No symbol containing relocation!"); |
| 492 | for (SymbolNum = 0; SymbolNum < NumSymbols - 1; ++SymbolNum) |
| 493 | if (Symbols[SymbolNum + 1].first > Offset) |
| 494 | break; |
| 495 | // Adjust the offset to be relative to the symbol. |
| 496 | Offset -= Symbols[SymbolNum].first; |
| 497 | // Get the name of the symbol containing the relocation. |
| 498 | StringRef TargetName = SymbolNames[SymbolNum]; |
| 499 | |
| 500 | bool isExtern = (RE->Word1 >> 27) & 1; |
| 501 | // Figure out the source symbol of the relocation. If isExtern is true, |
| 502 | // this relocation references the symbol table, otherwise it references |
| 503 | // a section in the same object, numbered from 1 through NumSections |
| 504 | // (SectionBases is [0, NumSections-1]). |
| 505 | if (!isExtern) |
| 506 | return Error("Internal relocations not supported."); |
| 507 | uint32_t SourceNum = RE->Word1 & 0xffffff; // 24-bit value |
| 508 | StringRef SourceName = SymbolNames[SourceNum]; |
| 509 | |
| 510 | // FIXME: Get the relocation addend from the target address. |
| 511 | |
| 512 | // Now store the relocation information. Associate it with the source |
| 513 | // symbol. |
| 514 | Relocations[SourceName].push_back(RelocationEntry(TargetName, |
| 515 | Offset, |
| 516 | RE->Word1, |
| 517 | 0 /*Addend*/)); |
| 518 | DEBUG(dbgs() << "Relocation at '" << TargetName << "' + " << Offset |
| 519 | << " from '" << SourceName << "(Word1: " |
| 520 | << format("0x%x", RE->Word1) << ")\n"); |
| 521 | } |
| 522 | } |
Jim Grosbach | 6e56331 | 2011-03-21 22:15:52 +0000 | [diff] [blame] | 523 | return false; |
| 524 | } |
| 525 | |
Jim Grosbach | 6e56331 | 2011-03-21 22:15:52 +0000 | [diff] [blame] | 526 | bool RuntimeDyldImpl::loadObject(MemoryBuffer *InputBuffer) { |
| 527 | // If the linker is in an error state, don't do anything. |
| 528 | if (hasError()) |
| 529 | return true; |
| 530 | // Load the Mach-O wrapper object. |
| 531 | std::string ErrorStr; |
| 532 | OwningPtr<MachOObject> Obj( |
| 533 | MachOObject::LoadFromBuffer(InputBuffer, &ErrorStr)); |
| 534 | if (!Obj) |
| 535 | return Error("unable to load object: '" + ErrorStr + "'"); |
| 536 | |
Jim Grosbach | a8287e3 | 2011-03-23 22:06:06 +0000 | [diff] [blame] | 537 | // Get the CPU type information from the header. |
| 538 | const macho::Header &Header = Obj->getHeader(); |
| 539 | |
| 540 | // FIXME: Error checking that the loaded object is compatible with |
| 541 | // the system we're running on. |
| 542 | CPUType = Header.CPUType; |
| 543 | CPUSubtype = Header.CPUSubtype; |
| 544 | |
Jim Grosbach | 6e56331 | 2011-03-21 22:15:52 +0000 | [diff] [blame] | 545 | // Validate that the load commands match what we expect. |
| 546 | const MachOObject::LoadCommandInfo *SegmentLCI = 0, *SymtabLCI = 0, |
| 547 | *DysymtabLCI = 0; |
Jim Grosbach | a8287e3 | 2011-03-23 22:06:06 +0000 | [diff] [blame] | 548 | for (unsigned i = 0; i != Header.NumLoadCommands; ++i) { |
Jim Grosbach | 6e56331 | 2011-03-21 22:15:52 +0000 | [diff] [blame] | 549 | const MachOObject::LoadCommandInfo &LCI = Obj->getLoadCommandInfo(i); |
| 550 | switch (LCI.Command.Type) { |
| 551 | case macho::LCT_Segment: |
| 552 | case macho::LCT_Segment64: |
| 553 | if (SegmentLCI) |
| 554 | return Error("unexpected input object (multiple segments)"); |
| 555 | SegmentLCI = &LCI; |
| 556 | break; |
| 557 | case macho::LCT_Symtab: |
| 558 | if (SymtabLCI) |
| 559 | return Error("unexpected input object (multiple symbol tables)"); |
| 560 | SymtabLCI = &LCI; |
| 561 | break; |
| 562 | case macho::LCT_Dysymtab: |
| 563 | if (DysymtabLCI) |
| 564 | return Error("unexpected input object (multiple symbol tables)"); |
| 565 | DysymtabLCI = &LCI; |
| 566 | break; |
| 567 | default: |
| 568 | return Error("unexpected input object (unexpected load command"); |
| 569 | } |
| 570 | } |
| 571 | |
| 572 | if (!SymtabLCI) |
| 573 | return Error("no symbol table found in object"); |
| 574 | if (!SegmentLCI) |
| 575 | return Error("no symbol table found in object"); |
| 576 | |
| 577 | // Read and register the symbol table data. |
| 578 | InMemoryStruct<macho::SymtabLoadCommand> SymtabLC; |
| 579 | Obj->ReadSymtabLoadCommand(*SymtabLCI, SymtabLC); |
| 580 | if (!SymtabLC) |
| 581 | return Error("unable to load symbol table load command"); |
| 582 | Obj->RegisterStringTable(*SymtabLC); |
| 583 | |
| 584 | // Read the dynamic link-edit information, if present (not present in static |
| 585 | // objects). |
| 586 | if (DysymtabLCI) { |
| 587 | InMemoryStruct<macho::DysymtabLoadCommand> DysymtabLC; |
| 588 | Obj->ReadDysymtabLoadCommand(*DysymtabLCI, DysymtabLC); |
| 589 | if (!DysymtabLC) |
| 590 | return Error("unable to load dynamic link-exit load command"); |
| 591 | |
| 592 | // FIXME: We don't support anything interesting yet. |
Jim Grosbach | 8b54dca | 2011-03-23 19:52:00 +0000 | [diff] [blame] | 593 | // if (DysymtabLC->LocalSymbolsIndex != 0) |
| 594 | // return Error("NOT YET IMPLEMENTED: local symbol entries"); |
| 595 | // if (DysymtabLC->ExternalSymbolsIndex != 0) |
| 596 | // return Error("NOT YET IMPLEMENTED: non-external symbol entries"); |
| 597 | // if (DysymtabLC->UndefinedSymbolsIndex != SymtabLC->NumSymbolTableEntries) |
| 598 | // return Error("NOT YET IMPLEMENTED: undefined symbol entries"); |
Jim Grosbach | 6e56331 | 2011-03-21 22:15:52 +0000 | [diff] [blame] | 599 | } |
| 600 | |
| 601 | // Load the segment load command. |
| 602 | if (SegmentLCI->Command.Type == macho::LCT_Segment) { |
| 603 | if (loadSegment32(Obj.get(), SegmentLCI, SymtabLC)) |
| 604 | return true; |
| 605 | } else { |
| 606 | if (loadSegment64(Obj.get(), SegmentLCI, SymtabLC)) |
| 607 | return true; |
| 608 | } |
| 609 | |
| 610 | return false; |
| 611 | } |
| 612 | |
Jim Grosbach | f8c1c84 | 2011-04-12 21:20:41 +0000 | [diff] [blame] | 613 | // Resolve the relocations for all symbols we currently know about. |
| 614 | void RuntimeDyldImpl::resolveRelocations() { |
| 615 | // Just iterate over the symbols in our symbol table and assign their |
| 616 | // addresses. |
| 617 | StringMap<uint8_t*>::iterator i = SymbolTable.begin(); |
| 618 | StringMap<uint8_t*>::iterator e = SymbolTable.end(); |
| 619 | for (;i != e; ++i) |
| 620 | reassignSymbolAddress(i->getKey(), i->getValue()); |
| 621 | } |
| 622 | |
| 623 | // Assign an address to a symbol name and resolve all the relocations |
| 624 | // associated with it. |
| 625 | void RuntimeDyldImpl::reassignSymbolAddress(StringRef Name, uint8_t *Addr) { |
| 626 | // Assign the address in our symbol table. |
| 627 | SymbolTable[Name] = Addr; |
| 628 | |
| 629 | RelocationList &Relocs = Relocations[Name]; |
| 630 | for (unsigned i = 0, e = Relocs.size(); i != e; ++i) { |
| 631 | RelocationEntry &RE = Relocs[i]; |
| 632 | uint8_t *Target = SymbolTable[RE.Target] + RE.Offset; |
| 633 | bool isPCRel = (RE.Data >> 24) & 1; |
| 634 | unsigned Type = (RE.Data >> 28) & 0xf; |
| 635 | unsigned Size = 1 << ((RE.Data >> 25) & 3); |
| 636 | |
| 637 | DEBUG(dbgs() << "Resolving relocation at '" << RE.Target |
| 638 | << "' + " << RE.Offset << " (" << format("%p", Target) << ")" |
| 639 | << " from '" << Name << " (" << format("%p", Addr) << ")" |
| 640 | << "(" << (isPCRel ? "pcrel" : "absolute") |
| 641 | << ", type: " << Type << ", Size: " << Size << ").\n"); |
| 642 | |
| 643 | resolveRelocation(Target, Addr, isPCRel, Type, Size); |
| 644 | RE.isResolved = true; |
| 645 | } |
| 646 | } |
Jim Grosbach | 6e56331 | 2011-03-21 22:15:52 +0000 | [diff] [blame] | 647 | |
| 648 | //===----------------------------------------------------------------------===// |
| 649 | // RuntimeDyld class implementation |
Jim Grosbach | fcbe5b7 | 2011-04-04 23:04:39 +0000 | [diff] [blame] | 650 | RuntimeDyld::RuntimeDyld(RTDyldMemoryManager *MM) { |
| 651 | Dyld = new RuntimeDyldImpl(MM); |
Jim Grosbach | 6e56331 | 2011-03-21 22:15:52 +0000 | [diff] [blame] | 652 | } |
| 653 | |
| 654 | RuntimeDyld::~RuntimeDyld() { |
| 655 | delete Dyld; |
| 656 | } |
| 657 | |
| 658 | bool RuntimeDyld::loadObject(MemoryBuffer *InputBuffer) { |
| 659 | return Dyld->loadObject(InputBuffer); |
| 660 | } |
| 661 | |
Jim Grosbach | b027105 | 2011-04-08 17:31:24 +0000 | [diff] [blame] | 662 | void *RuntimeDyld::getSymbolAddress(StringRef Name) { |
Jim Grosbach | 6e56331 | 2011-03-21 22:15:52 +0000 | [diff] [blame] | 663 | return Dyld->getSymbolAddress(Name); |
| 664 | } |
| 665 | |
Jim Grosbach | f8c1c84 | 2011-04-12 21:20:41 +0000 | [diff] [blame] | 666 | void RuntimeDyld::resolveRelocations() { |
| 667 | Dyld->resolveRelocations(); |
| 668 | } |
| 669 | |
| 670 | void RuntimeDyld::reassignSymbolAddress(StringRef Name, uint8_t *Addr) { |
| 671 | Dyld->reassignSymbolAddress(Name, Addr); |
| 672 | } |
| 673 | |
Jim Grosbach | 91dde15 | 2011-03-22 18:22:27 +0000 | [diff] [blame] | 674 | StringRef RuntimeDyld::getErrorString() { |
Jim Grosbach | b3eecaf | 2011-03-22 18:19:42 +0000 | [diff] [blame] | 675 | return Dyld->getErrorString(); |
| 676 | } |
| 677 | |
Jim Grosbach | 6e56331 | 2011-03-21 22:15:52 +0000 | [diff] [blame] | 678 | } // end namespace llvm |