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 | c41ab78 | 2011-04-06 01:11:05 +0000 | [diff] [blame] | 44 | |
| 45 | // For each function, we have a MemoryBlock of it's instruction data. |
| 46 | StringMap<sys::MemoryBlock> Functions; |
| 47 | |
Jim Grosbach | 6e56331 | 2011-03-21 22:15:52 +0000 | [diff] [blame] | 48 | // Master symbol table. As modules are loaded and external symbols are |
| 49 | // resolved, their addresses are stored here. |
Jim Grosbach | fcbe5b7 | 2011-04-04 23:04:39 +0000 | [diff] [blame] | 50 | StringMap<uint64_t> SymbolTable; |
Jim Grosbach | 6e56331 | 2011-03-21 22:15:52 +0000 | [diff] [blame] | 51 | |
| 52 | // FIXME: Should have multiple data blocks, one for each loaded chunk of |
| 53 | // compiled code. |
Jim Grosbach | 7cbf92d | 2011-04-12 00:23:32 +0000 | [diff] [blame^] | 54 | // sys::MemoryBlock Data; |
Jim Grosbach | 6e56331 | 2011-03-21 22:15:52 +0000 | [diff] [blame] | 55 | |
| 56 | bool HasError; |
| 57 | std::string ErrorStr; |
| 58 | |
| 59 | // Set the error state and record an error string. |
| 60 | bool Error(const Twine &Msg) { |
| 61 | ErrorStr = Msg.str(); |
| 62 | HasError = true; |
| 63 | return true; |
| 64 | } |
| 65 | |
Jim Grosbach | c41ab78 | 2011-04-06 01:11:05 +0000 | [diff] [blame] | 66 | void extractFunction(StringRef Name, uint8_t *StartAddress, |
| 67 | uint8_t *EndAddress); |
Jim Grosbach | 8b54dca | 2011-03-23 19:52:00 +0000 | [diff] [blame] | 68 | bool resolveRelocation(uint32_t BaseSection, macho::RelocationEntry RE, |
| 69 | SmallVectorImpl<void *> &SectionBases, |
| 70 | SmallVectorImpl<StringRef> &SymbolNames); |
Jim Grosbach | a8287e3 | 2011-03-23 22:06:06 +0000 | [diff] [blame] | 71 | bool resolveX86_64Relocation(intptr_t Address, intptr_t Value, bool isPCRel, |
| 72 | unsigned Type, unsigned Size); |
| 73 | bool resolveARMRelocation(intptr_t Address, intptr_t Value, bool isPCRel, |
| 74 | unsigned Type, unsigned Size); |
Jim Grosbach | 8b54dca | 2011-03-23 19:52:00 +0000 | [diff] [blame] | 75 | |
Jim Grosbach | 6e56331 | 2011-03-21 22:15:52 +0000 | [diff] [blame] | 76 | bool loadSegment32(const MachOObject *Obj, |
| 77 | const MachOObject::LoadCommandInfo *SegmentLCI, |
| 78 | const InMemoryStruct<macho::SymtabLoadCommand> &SymtabLC); |
| 79 | bool loadSegment64(const MachOObject *Obj, |
| 80 | const MachOObject::LoadCommandInfo *SegmentLCI, |
| 81 | const InMemoryStruct<macho::SymtabLoadCommand> &SymtabLC); |
| 82 | |
| 83 | public: |
Jim Grosbach | fcbe5b7 | 2011-04-04 23:04:39 +0000 | [diff] [blame] | 84 | RuntimeDyldImpl(RTDyldMemoryManager *mm) : MemMgr(mm), HasError(false) {} |
Jim Grosbach | 8371c89 | 2011-03-22 00:42:19 +0000 | [diff] [blame] | 85 | |
Jim Grosbach | 6e56331 | 2011-03-21 22:15:52 +0000 | [diff] [blame] | 86 | bool loadObject(MemoryBuffer *InputBuffer); |
| 87 | |
Jim Grosbach | b027105 | 2011-04-08 17:31:24 +0000 | [diff] [blame] | 88 | void *getSymbolAddress(StringRef Name) { |
Jim Grosbach | c41ab78 | 2011-04-06 01:11:05 +0000 | [diff] [blame] | 89 | // FIXME: Just look up as a function for now. Overly simple of course. |
| 90 | // Work in progress. |
Jim Grosbach | b027105 | 2011-04-08 17:31:24 +0000 | [diff] [blame] | 91 | return Functions.lookup(Name).base(); |
Jim Grosbach | 6e56331 | 2011-03-21 22:15:52 +0000 | [diff] [blame] | 92 | } |
| 93 | |
Jim Grosbach | 6e56331 | 2011-03-21 22:15:52 +0000 | [diff] [blame] | 94 | // Is the linker in an error state? |
| 95 | bool hasError() { return HasError; } |
| 96 | |
| 97 | // Mark the error condition as handled and continue. |
| 98 | void clearError() { HasError = false; } |
| 99 | |
| 100 | // Get the error message. |
| 101 | StringRef getErrorString() { return ErrorStr; } |
| 102 | }; |
| 103 | |
Jim Grosbach | c41ab78 | 2011-04-06 01:11:05 +0000 | [diff] [blame] | 104 | void RuntimeDyldImpl::extractFunction(StringRef Name, uint8_t *StartAddress, |
Jim Grosbach | 01ccab4 | 2011-04-06 22:13:52 +0000 | [diff] [blame] | 105 | uint8_t *EndAddress) { |
Jim Grosbach | c41ab78 | 2011-04-06 01:11:05 +0000 | [diff] [blame] | 106 | // Allocate memory for the function via the memory manager. |
| 107 | uintptr_t Size = EndAddress - StartAddress + 1; |
| 108 | uint8_t *Mem = MemMgr->startFunctionBody(Name.data(), Size); |
| 109 | assert(Size >= (uint64_t)(EndAddress - StartAddress + 1) && |
| 110 | "Memory manager failed to allocate enough memory!"); |
| 111 | // Copy the function payload into the memory block. |
| 112 | memcpy(Mem, StartAddress, EndAddress - StartAddress + 1); |
| 113 | MemMgr->endFunctionBody(Name.data(), Mem, Mem + Size); |
| 114 | // Remember where we put it. |
| 115 | Functions[Name] = sys::MemoryBlock(Mem, Size); |
| 116 | DEBUG(dbgs() << " allocated to " << Mem << "\n"); |
| 117 | } |
| 118 | |
Jim Grosbach | 8b54dca | 2011-03-23 19:52:00 +0000 | [diff] [blame] | 119 | bool RuntimeDyldImpl:: |
| 120 | resolveRelocation(uint32_t BaseSection, macho::RelocationEntry RE, |
| 121 | SmallVectorImpl<void *> &SectionBases, |
| 122 | SmallVectorImpl<StringRef> &SymbolNames) { |
| 123 | // struct relocation_info { |
| 124 | // int32_t r_address; |
| 125 | // uint32_t r_symbolnum:24, |
| 126 | // r_pcrel:1, |
| 127 | // r_length:2, |
| 128 | // r_extern:1, |
| 129 | // r_type:4; |
| 130 | // }; |
| 131 | uint32_t SymbolNum = RE.Word1 & 0xffffff; // 24-bit value |
| 132 | bool isPCRel = (RE.Word1 >> 24) & 1; |
| 133 | unsigned Log2Size = (RE.Word1 >> 25) & 3; |
| 134 | bool isExtern = (RE.Word1 >> 27) & 1; |
| 135 | unsigned Type = (RE.Word1 >> 28) & 0xf; |
| 136 | if (RE.Word0 & macho::RF_Scattered) |
| 137 | return Error("NOT YET IMPLEMENTED: scattered relocations."); |
Jim Grosbach | 6e56331 | 2011-03-21 22:15:52 +0000 | [diff] [blame] | 138 | |
Jim Grosbach | 8b54dca | 2011-03-23 19:52:00 +0000 | [diff] [blame] | 139 | // The address requiring a relocation. |
| 140 | intptr_t Address = (intptr_t)SectionBases[BaseSection] + RE.Word0; |
| 141 | |
| 142 | // Figure out the target address of the relocation. If isExtern is true, |
| 143 | // this relocation references the symbol table, otherwise it references |
| 144 | // a section in the same object, numbered from 1 through NumSections |
| 145 | // (SectionBases is [0, NumSections-1]). |
| 146 | intptr_t Value; |
| 147 | if (isExtern) { |
| 148 | StringRef Name = SymbolNames[SymbolNum]; |
| 149 | if (SymbolTable.lookup(Name)) { |
| 150 | // The symbol is in our symbol table, so we can resolve it directly. |
| 151 | Value = (intptr_t)SymbolTable[Name]; |
| 152 | } else { |
| 153 | return Error("NOT YET IMPLEMENTED: relocations to pre-compiled code."); |
| 154 | } |
| 155 | DEBUG(dbgs() << "Resolve relocation(" << Type << ") from '" << Name |
| 156 | << "' to " << format("0x%x", Address) << ".\n"); |
| 157 | } else { |
| 158 | // For non-external relocations, the SymbolNum is actual a section number |
| 159 | // as described above. |
| 160 | Value = (intptr_t)SectionBases[SymbolNum - 1]; |
| 161 | } |
| 162 | |
Jim Grosbach | a8287e3 | 2011-03-23 22:06:06 +0000 | [diff] [blame] | 163 | unsigned Size = 1 << Log2Size; |
| 164 | switch (CPUType) { |
| 165 | default: assert(0 && "Unsupported CPU type!"); |
| 166 | case mach::CTM_x86_64: |
| 167 | return resolveX86_64Relocation(Address, Value, isPCRel, Type, Size); |
| 168 | case mach::CTM_ARM: |
| 169 | return resolveARMRelocation(Address, Value, isPCRel, Type, Size); |
| 170 | } |
| 171 | llvm_unreachable(""); |
| 172 | } |
| 173 | |
| 174 | bool RuntimeDyldImpl::resolveX86_64Relocation(intptr_t Address, intptr_t Value, |
| 175 | bool isPCRel, unsigned Type, |
| 176 | unsigned Size) { |
Jim Grosbach | 8b54dca | 2011-03-23 19:52:00 +0000 | [diff] [blame] | 177 | // If the relocation is PC-relative, the value to be encoded is the |
| 178 | // pointer difference. |
| 179 | if (isPCRel) |
| 180 | // FIXME: It seems this value needs to be adjusted by 4 for an effective PC |
| 181 | // address. Is that expected? Only for branches, perhaps? |
| 182 | Value -= Address + 4; |
| 183 | |
| 184 | switch(Type) { |
| 185 | default: |
| 186 | llvm_unreachable("Invalid relocation type!"); |
| 187 | case macho::RIT_X86_64_Unsigned: |
| 188 | case macho::RIT_X86_64_Branch: { |
| 189 | // Mask in the target value a byte at a time (we don't have an alignment |
| 190 | // guarantee for the target address, so this is safest). |
Jim Grosbach | 8b54dca | 2011-03-23 19:52:00 +0000 | [diff] [blame] | 191 | uint8_t *p = (uint8_t*)Address; |
Jim Grosbach | a8287e3 | 2011-03-23 22:06:06 +0000 | [diff] [blame] | 192 | for (unsigned i = 0; i < Size; ++i) { |
Jim Grosbach | 8b54dca | 2011-03-23 19:52:00 +0000 | [diff] [blame] | 193 | *p++ = (uint8_t)Value; |
| 194 | Value >>= 8; |
| 195 | } |
| 196 | return false; |
| 197 | } |
| 198 | case macho::RIT_X86_64_Signed: |
| 199 | case macho::RIT_X86_64_GOTLoad: |
| 200 | case macho::RIT_X86_64_GOT: |
| 201 | case macho::RIT_X86_64_Subtractor: |
| 202 | case macho::RIT_X86_64_Signed1: |
| 203 | case macho::RIT_X86_64_Signed2: |
| 204 | case macho::RIT_X86_64_Signed4: |
| 205 | case macho::RIT_X86_64_TLV: |
| 206 | return Error("Relocation type not implemented yet!"); |
| 207 | } |
| 208 | return false; |
| 209 | } |
Jim Grosbach | 6e56331 | 2011-03-21 22:15:52 +0000 | [diff] [blame] | 210 | |
Jim Grosbach | a8287e3 | 2011-03-23 22:06:06 +0000 | [diff] [blame] | 211 | bool RuntimeDyldImpl::resolveARMRelocation(intptr_t Address, intptr_t Value, |
| 212 | bool isPCRel, unsigned Type, |
| 213 | unsigned Size) { |
| 214 | // If the relocation is PC-relative, the value to be encoded is the |
| 215 | // pointer difference. |
| 216 | if (isPCRel) { |
| 217 | Value -= Address; |
| 218 | // ARM PCRel relocations have an effective-PC offset of two instructions |
| 219 | // (four bytes in Thumb mode, 8 bytes in ARM mode). |
| 220 | // FIXME: For now, assume ARM mode. |
| 221 | Value -= 8; |
| 222 | } |
| 223 | |
| 224 | switch(Type) { |
| 225 | default: |
| 226 | case macho::RIT_Vanilla: { |
| 227 | llvm_unreachable("Invalid relocation type!"); |
| 228 | // Mask in the target value a byte at a time (we don't have an alignment |
| 229 | // guarantee for the target address, so this is safest). |
| 230 | uint8_t *p = (uint8_t*)Address; |
| 231 | for (unsigned i = 0; i < Size; ++i) { |
| 232 | *p++ = (uint8_t)Value; |
| 233 | Value >>= 8; |
| 234 | } |
Jim Grosbach | 5ffe37f | 2011-03-23 23:35:17 +0000 | [diff] [blame] | 235 | break; |
Jim Grosbach | a8287e3 | 2011-03-23 22:06:06 +0000 | [diff] [blame] | 236 | } |
| 237 | case macho::RIT_Pair: |
| 238 | case macho::RIT_Difference: |
| 239 | case macho::RIT_ARM_LocalDifference: |
| 240 | case macho::RIT_ARM_PreboundLazyPointer: |
Jim Grosbach | 5ffe37f | 2011-03-23 23:35:17 +0000 | [diff] [blame] | 241 | case macho::RIT_ARM_Branch24Bit: { |
| 242 | // Mask the value into the target address. We know instructions are |
| 243 | // 32-bit aligned, so we can do it all at once. |
| 244 | uint32_t *p = (uint32_t*)Address; |
| 245 | // The low two bits of the value are not encoded. |
| 246 | Value >>= 2; |
| 247 | // Mask the value to 24 bits. |
| 248 | Value &= 0xffffff; |
| 249 | // FIXME: If the destination is a Thumb function (and the instruction |
| 250 | // is a non-predicated BL instruction), we need to change it to a BLX |
| 251 | // instruction instead. |
| 252 | |
| 253 | // Insert the value into the instruction. |
| 254 | *p = (*p & ~0xffffff) | Value; |
| 255 | break; |
| 256 | } |
Jim Grosbach | a8287e3 | 2011-03-23 22:06:06 +0000 | [diff] [blame] | 257 | case macho::RIT_ARM_ThumbBranch22Bit: |
| 258 | case macho::RIT_ARM_ThumbBranch32Bit: |
| 259 | case macho::RIT_ARM_Half: |
| 260 | case macho::RIT_ARM_HalfDifference: |
| 261 | return Error("Relocation type not implemented yet!"); |
| 262 | } |
| 263 | return false; |
| 264 | } |
| 265 | |
Jim Grosbach | 6e56331 | 2011-03-21 22:15:52 +0000 | [diff] [blame] | 266 | bool RuntimeDyldImpl:: |
| 267 | loadSegment32(const MachOObject *Obj, |
| 268 | const MachOObject::LoadCommandInfo *SegmentLCI, |
| 269 | const InMemoryStruct<macho::SymtabLoadCommand> &SymtabLC) { |
| 270 | InMemoryStruct<macho::SegmentLoadCommand> Segment32LC; |
| 271 | Obj->ReadSegmentLoadCommand(*SegmentLCI, Segment32LC); |
| 272 | if (!Segment32LC) |
| 273 | return Error("unable to load segment load command"); |
| 274 | |
Jim Grosbach | b027105 | 2011-04-08 17:31:24 +0000 | [diff] [blame] | 275 | for (unsigned SectNum = 0; SectNum != Segment32LC->NumSections; ++SectNum) { |
Jim Grosbach | 6e56331 | 2011-03-21 22:15:52 +0000 | [diff] [blame] | 276 | InMemoryStruct<macho::Section> Sect; |
Jim Grosbach | b027105 | 2011-04-08 17:31:24 +0000 | [diff] [blame] | 277 | Obj->ReadSection(*SegmentLCI, SectNum, Sect); |
| 278 | if (!Sect) |
| 279 | return Error("unable to load section: '" + Twine(SectNum) + "'"); |
Jim Grosbach | 6e56331 | 2011-03-21 22:15:52 +0000 | [diff] [blame] | 280 | |
| 281 | // FIXME: Improve check. |
Jim Grosbach | b027105 | 2011-04-08 17:31:24 +0000 | [diff] [blame] | 282 | if (Sect->Flags != 0x80000400) |
| 283 | return Error("unsupported section type!"); |
Jim Grosbach | 6e56331 | 2011-03-21 22:15:52 +0000 | [diff] [blame] | 284 | |
Jim Grosbach | b027105 | 2011-04-08 17:31:24 +0000 | [diff] [blame] | 285 | // Address and names of symbols in the section. |
| 286 | typedef std::pair<uint64_t, StringRef> SymbolEntry; |
| 287 | SmallVector<SymbolEntry, 32> Symbols; |
| 288 | for (unsigned i = 0; i != SymtabLC->NumSymbolTableEntries; ++i) { |
| 289 | InMemoryStruct<macho::SymbolTableEntry> STE; |
| 290 | Obj->ReadSymbolTableEntry(SymtabLC->SymbolTableOffset, i, STE); |
| 291 | if (!STE) |
| 292 | return Error("unable to read symbol: '" + Twine(i) + "'"); |
| 293 | if (STE->SectionIndex > Segment32LC->NumSections) |
Benjamin Kramer | cc513e1c | 2011-04-09 10:10:35 +0000 | [diff] [blame] | 294 | return Error("invalid section index for symbol: '" + Twine(i) + "'"); |
Jim Grosbach | b027105 | 2011-04-08 17:31:24 +0000 | [diff] [blame] | 295 | |
| 296 | // Just skip symbols not defined in this section. |
Jim Grosbach | e2e777b | 2011-04-08 21:11:20 +0000 | [diff] [blame] | 297 | if ((unsigned)STE->SectionIndex - 1 != SectNum) |
Jim Grosbach | b027105 | 2011-04-08 17:31:24 +0000 | [diff] [blame] | 298 | continue; |
| 299 | |
| 300 | // Get the symbol name. |
| 301 | StringRef Name = Obj->getStringAtIndex(STE->StringIndex); |
| 302 | |
| 303 | // FIXME: Check the symbol type and flags. |
| 304 | if (STE->Type != 0xF) // external, defined in this section. |
| 305 | return Error("unexpected symbol type!"); |
| 306 | if (STE->Flags != 0x0) |
| 307 | return Error("unexpected symbol type!"); |
| 308 | |
| 309 | uint64_t BaseAddress = Sect->Address; |
| 310 | uint64_t Address = BaseAddress + STE->Value; |
| 311 | |
| 312 | // Remember the symbol. |
| 313 | Symbols.push_back(SymbolEntry(Address, Name)); |
| 314 | |
| 315 | DEBUG(dbgs() << "Function sym: '" << Name << "' @ " << Address << "\n"); |
| 316 | } |
| 317 | // Sort the symbols by address, just in case they didn't come in that |
| 318 | // way. |
| 319 | array_pod_sort(Symbols.begin(), Symbols.end()); |
| 320 | |
| 321 | // Extract the function data. |
| 322 | uint8_t *Base = (uint8_t*)Obj->getData(Segment32LC->FileOffset, |
| 323 | Segment32LC->FileSize).data(); |
| 324 | for (unsigned i = 0, e = Symbols.size() - 1; i != e; ++i) { |
| 325 | uint64_t StartOffset = Symbols[i].first; |
| 326 | uint64_t EndOffset = Symbols[i + 1].first - 1; |
| 327 | DEBUG(dbgs() << "Extracting function: " << Symbols[i].second |
| 328 | << " from [" << StartOffset << ", " << EndOffset << "]\n"); |
| 329 | extractFunction(Symbols[i].second, Base + StartOffset, Base + EndOffset); |
| 330 | } |
| 331 | // The last symbol we do after since the end address is calculated |
| 332 | // differently because there is no next symbol to reference. |
| 333 | uint64_t StartOffset = Symbols[Symbols.size() - 1].first; |
| 334 | uint64_t EndOffset = Sect->Size - 1; |
| 335 | DEBUG(dbgs() << "Extracting function: " << Symbols[Symbols.size()-1].second |
| 336 | << " from [" << StartOffset << ", " << EndOffset << "]\n"); |
| 337 | extractFunction(Symbols[Symbols.size()-1].second, |
| 338 | Base + StartOffset, Base + EndOffset); |
Jim Grosbach | 6e56331 | 2011-03-21 22:15:52 +0000 | [diff] [blame] | 339 | } |
| 340 | |
Jim Grosbach | 6e56331 | 2011-03-21 22:15:52 +0000 | [diff] [blame] | 341 | return false; |
| 342 | } |
| 343 | |
| 344 | |
| 345 | bool RuntimeDyldImpl:: |
| 346 | loadSegment64(const MachOObject *Obj, |
| 347 | const MachOObject::LoadCommandInfo *SegmentLCI, |
| 348 | const InMemoryStruct<macho::SymtabLoadCommand> &SymtabLC) { |
| 349 | InMemoryStruct<macho::Segment64LoadCommand> Segment64LC; |
| 350 | Obj->ReadSegment64LoadCommand(*SegmentLCI, Segment64LC); |
| 351 | if (!Segment64LC) |
| 352 | return Error("unable to load segment load command"); |
| 353 | |
Jim Grosbach | c41ab78 | 2011-04-06 01:11:05 +0000 | [diff] [blame] | 354 | for (unsigned SectNum = 0; SectNum != Segment64LC->NumSections; ++SectNum) { |
Jim Grosbach | 6e56331 | 2011-03-21 22:15:52 +0000 | [diff] [blame] | 355 | InMemoryStruct<macho::Section64> Sect; |
Jim Grosbach | c41ab78 | 2011-04-06 01:11:05 +0000 | [diff] [blame] | 356 | Obj->ReadSection64(*SegmentLCI, SectNum, Sect); |
Jim Grosbach | 6e56331 | 2011-03-21 22:15:52 +0000 | [diff] [blame] | 357 | if (!Sect) |
Jim Grosbach | c41ab78 | 2011-04-06 01:11:05 +0000 | [diff] [blame] | 358 | return Error("unable to load section: '" + Twine(SectNum) + "'"); |
Jim Grosbach | 6e56331 | 2011-03-21 22:15:52 +0000 | [diff] [blame] | 359 | |
| 360 | // FIXME: Improve check. |
| 361 | if (Sect->Flags != 0x80000400) |
| 362 | return Error("unsupported section type!"); |
| 363 | |
Jim Grosbach | c41ab78 | 2011-04-06 01:11:05 +0000 | [diff] [blame] | 364 | // Address and names of symbols in the section. |
| 365 | typedef std::pair<uint64_t, StringRef> SymbolEntry; |
| 366 | SmallVector<SymbolEntry, 64> Symbols; |
| 367 | for (unsigned i = 0; i != SymtabLC->NumSymbolTableEntries; ++i) { |
| 368 | InMemoryStruct<macho::Symbol64TableEntry> STE; |
| 369 | Obj->ReadSymbol64TableEntry(SymtabLC->SymbolTableOffset, i, STE); |
| 370 | if (!STE) |
| 371 | return Error("unable to read symbol: '" + Twine(i) + "'"); |
| 372 | if (STE->SectionIndex > Segment64LC->NumSections) |
Benjamin Kramer | cc513e1c | 2011-04-09 10:10:35 +0000 | [diff] [blame] | 373 | return Error("invalid section index for symbol: '" + Twine(i) + "'"); |
Jim Grosbach | c41ab78 | 2011-04-06 01:11:05 +0000 | [diff] [blame] | 374 | |
| 375 | // Just skip symbols not defined in this section. |
Jim Grosbach | e2e777b | 2011-04-08 21:11:20 +0000 | [diff] [blame] | 376 | if ((unsigned)STE->SectionIndex - 1 != SectNum) |
Jim Grosbach | c41ab78 | 2011-04-06 01:11:05 +0000 | [diff] [blame] | 377 | continue; |
| 378 | |
| 379 | // Get the symbol name. |
| 380 | StringRef Name = Obj->getStringAtIndex(STE->StringIndex); |
| 381 | |
| 382 | // FIXME: Check the symbol type and flags. |
| 383 | if (STE->Type != 0xF) // external, defined in this section. |
| 384 | return Error("unexpected symbol type!"); |
| 385 | if (STE->Flags != 0x0) |
| 386 | return Error("unexpected symbol type!"); |
| 387 | |
| 388 | uint64_t BaseAddress = Sect->Address; |
| 389 | uint64_t Address = BaseAddress + STE->Value; |
| 390 | |
| 391 | // Remember the symbol. |
| 392 | Symbols.push_back(SymbolEntry(Address, Name)); |
| 393 | |
| 394 | DEBUG(dbgs() << "Function sym: '" << Name << "' @ " << Address << "\n"); |
| 395 | } |
| 396 | // Sort the symbols by address, just in case they didn't come in that |
| 397 | // way. |
| 398 | array_pod_sort(Symbols.begin(), Symbols.end()); |
| 399 | |
| 400 | // Extract the function data. |
| 401 | uint8_t *Base = (uint8_t*)Obj->getData(Segment64LC->FileOffset, |
| 402 | Segment64LC->FileSize).data(); |
| 403 | for (unsigned i = 0, e = Symbols.size() - 1; i != e; ++i) { |
| 404 | uint64_t StartOffset = Symbols[i].first; |
| 405 | uint64_t EndOffset = Symbols[i + 1].first - 1; |
| 406 | DEBUG(dbgs() << "Extracting function: " << Symbols[i].second |
| 407 | << " from [" << StartOffset << ", " << EndOffset << "]\n"); |
| 408 | extractFunction(Symbols[i].second, Base + StartOffset, Base + EndOffset); |
| 409 | } |
| 410 | // The last symbol we do after since the end address is calculated |
| 411 | // differently because there is no next symbol to reference. |
| 412 | uint64_t StartOffset = Symbols[Symbols.size() - 1].first; |
| 413 | uint64_t EndOffset = Sect->Size - 1; |
| 414 | DEBUG(dbgs() << "Extracting function: " << Symbols[Symbols.size()-1].second |
| 415 | << " from [" << StartOffset << ", " << EndOffset << "]\n"); |
| 416 | extractFunction(Symbols[Symbols.size()-1].second, |
| 417 | Base + StartOffset, Base + EndOffset); |
Jim Grosbach | 6e56331 | 2011-03-21 22:15:52 +0000 | [diff] [blame] | 418 | } |
| 419 | |
Jim Grosbach | 6e56331 | 2011-03-21 22:15:52 +0000 | [diff] [blame] | 420 | return false; |
| 421 | } |
| 422 | |
Jim Grosbach | 6e56331 | 2011-03-21 22:15:52 +0000 | [diff] [blame] | 423 | bool RuntimeDyldImpl::loadObject(MemoryBuffer *InputBuffer) { |
| 424 | // If the linker is in an error state, don't do anything. |
| 425 | if (hasError()) |
| 426 | return true; |
| 427 | // Load the Mach-O wrapper object. |
| 428 | std::string ErrorStr; |
| 429 | OwningPtr<MachOObject> Obj( |
| 430 | MachOObject::LoadFromBuffer(InputBuffer, &ErrorStr)); |
| 431 | if (!Obj) |
| 432 | return Error("unable to load object: '" + ErrorStr + "'"); |
| 433 | |
Jim Grosbach | a8287e3 | 2011-03-23 22:06:06 +0000 | [diff] [blame] | 434 | // Get the CPU type information from the header. |
| 435 | const macho::Header &Header = Obj->getHeader(); |
| 436 | |
| 437 | // FIXME: Error checking that the loaded object is compatible with |
| 438 | // the system we're running on. |
| 439 | CPUType = Header.CPUType; |
| 440 | CPUSubtype = Header.CPUSubtype; |
| 441 | |
Jim Grosbach | 6e56331 | 2011-03-21 22:15:52 +0000 | [diff] [blame] | 442 | // Validate that the load commands match what we expect. |
| 443 | const MachOObject::LoadCommandInfo *SegmentLCI = 0, *SymtabLCI = 0, |
| 444 | *DysymtabLCI = 0; |
Jim Grosbach | a8287e3 | 2011-03-23 22:06:06 +0000 | [diff] [blame] | 445 | for (unsigned i = 0; i != Header.NumLoadCommands; ++i) { |
Jim Grosbach | 6e56331 | 2011-03-21 22:15:52 +0000 | [diff] [blame] | 446 | const MachOObject::LoadCommandInfo &LCI = Obj->getLoadCommandInfo(i); |
| 447 | switch (LCI.Command.Type) { |
| 448 | case macho::LCT_Segment: |
| 449 | case macho::LCT_Segment64: |
| 450 | if (SegmentLCI) |
| 451 | return Error("unexpected input object (multiple segments)"); |
| 452 | SegmentLCI = &LCI; |
| 453 | break; |
| 454 | case macho::LCT_Symtab: |
| 455 | if (SymtabLCI) |
| 456 | return Error("unexpected input object (multiple symbol tables)"); |
| 457 | SymtabLCI = &LCI; |
| 458 | break; |
| 459 | case macho::LCT_Dysymtab: |
| 460 | if (DysymtabLCI) |
| 461 | return Error("unexpected input object (multiple symbol tables)"); |
| 462 | DysymtabLCI = &LCI; |
| 463 | break; |
| 464 | default: |
| 465 | return Error("unexpected input object (unexpected load command"); |
| 466 | } |
| 467 | } |
| 468 | |
| 469 | if (!SymtabLCI) |
| 470 | return Error("no symbol table found in object"); |
| 471 | if (!SegmentLCI) |
| 472 | return Error("no symbol table found in object"); |
| 473 | |
| 474 | // Read and register the symbol table data. |
| 475 | InMemoryStruct<macho::SymtabLoadCommand> SymtabLC; |
| 476 | Obj->ReadSymtabLoadCommand(*SymtabLCI, SymtabLC); |
| 477 | if (!SymtabLC) |
| 478 | return Error("unable to load symbol table load command"); |
| 479 | Obj->RegisterStringTable(*SymtabLC); |
| 480 | |
| 481 | // Read the dynamic link-edit information, if present (not present in static |
| 482 | // objects). |
| 483 | if (DysymtabLCI) { |
| 484 | InMemoryStruct<macho::DysymtabLoadCommand> DysymtabLC; |
| 485 | Obj->ReadDysymtabLoadCommand(*DysymtabLCI, DysymtabLC); |
| 486 | if (!DysymtabLC) |
| 487 | return Error("unable to load dynamic link-exit load command"); |
| 488 | |
| 489 | // FIXME: We don't support anything interesting yet. |
Jim Grosbach | 8b54dca | 2011-03-23 19:52:00 +0000 | [diff] [blame] | 490 | // if (DysymtabLC->LocalSymbolsIndex != 0) |
| 491 | // return Error("NOT YET IMPLEMENTED: local symbol entries"); |
| 492 | // if (DysymtabLC->ExternalSymbolsIndex != 0) |
| 493 | // return Error("NOT YET IMPLEMENTED: non-external symbol entries"); |
| 494 | // if (DysymtabLC->UndefinedSymbolsIndex != SymtabLC->NumSymbolTableEntries) |
| 495 | // return Error("NOT YET IMPLEMENTED: undefined symbol entries"); |
Jim Grosbach | 6e56331 | 2011-03-21 22:15:52 +0000 | [diff] [blame] | 496 | } |
| 497 | |
| 498 | // Load the segment load command. |
| 499 | if (SegmentLCI->Command.Type == macho::LCT_Segment) { |
| 500 | if (loadSegment32(Obj.get(), SegmentLCI, SymtabLC)) |
| 501 | return true; |
| 502 | } else { |
| 503 | if (loadSegment64(Obj.get(), SegmentLCI, SymtabLC)) |
| 504 | return true; |
| 505 | } |
| 506 | |
| 507 | return false; |
| 508 | } |
| 509 | |
| 510 | |
| 511 | //===----------------------------------------------------------------------===// |
| 512 | // RuntimeDyld class implementation |
Jim Grosbach | fcbe5b7 | 2011-04-04 23:04:39 +0000 | [diff] [blame] | 513 | RuntimeDyld::RuntimeDyld(RTDyldMemoryManager *MM) { |
| 514 | Dyld = new RuntimeDyldImpl(MM); |
Jim Grosbach | 6e56331 | 2011-03-21 22:15:52 +0000 | [diff] [blame] | 515 | } |
| 516 | |
| 517 | RuntimeDyld::~RuntimeDyld() { |
| 518 | delete Dyld; |
| 519 | } |
| 520 | |
| 521 | bool RuntimeDyld::loadObject(MemoryBuffer *InputBuffer) { |
| 522 | return Dyld->loadObject(InputBuffer); |
| 523 | } |
| 524 | |
Jim Grosbach | b027105 | 2011-04-08 17:31:24 +0000 | [diff] [blame] | 525 | void *RuntimeDyld::getSymbolAddress(StringRef Name) { |
Jim Grosbach | 6e56331 | 2011-03-21 22:15:52 +0000 | [diff] [blame] | 526 | return Dyld->getSymbolAddress(Name); |
| 527 | } |
| 528 | |
Jim Grosbach | 91dde15 | 2011-03-22 18:22:27 +0000 | [diff] [blame] | 529 | StringRef RuntimeDyld::getErrorString() { |
Jim Grosbach | b3eecaf | 2011-03-22 18:19:42 +0000 | [diff] [blame] | 530 | return Dyld->getErrorString(); |
| 531 | } |
| 532 | |
Jim Grosbach | 6e56331 | 2011-03-21 22:15:52 +0000 | [diff] [blame] | 533 | } // end namespace llvm |