blob: 7e6a1f04f0d4648f21766d3a01c90bd318e163ab [file] [log] [blame]
Jim Grosbach6e563312011-03-21 22:15:52 +00001//===-- 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 Grosbach8b54dca2011-03-23 19:52:00 +000014#define DEBUG_TYPE "dyld"
Jim Grosbach6e563312011-03-21 22:15:52 +000015#include "llvm/ADT/OwningPtr.h"
Jim Grosbach8b54dca2011-03-23 19:52:00 +000016#include "llvm/ADT/SmallVector.h"
Jim Grosbach6e563312011-03-21 22:15:52 +000017#include "llvm/ADT/StringMap.h"
18#include "llvm/ADT/StringRef.h"
19#include "llvm/ADT/Twine.h"
20#include "llvm/ExecutionEngine/RuntimeDyld.h"
21#include "llvm/Object/MachOObject.h"
Jim Grosbach8b54dca2011-03-23 19:52:00 +000022#include "llvm/Support/Debug.h"
23#include "llvm/Support/ErrorHandling.h"
24#include "llvm/Support/Format.h"
Jim Grosbach6e563312011-03-21 22:15:52 +000025#include "llvm/Support/Memory.h"
26#include "llvm/Support/MemoryBuffer.h"
27#include "llvm/Support/system_error.h"
Jim Grosbach8b54dca2011-03-23 19:52:00 +000028#include "llvm/Support/raw_ostream.h"
Jim Grosbach6e563312011-03-21 22:15:52 +000029using namespace llvm;
30using namespace llvm::object;
31
Chandler Carruth53c5e7b2011-04-05 23:54:31 +000032// Empty out-of-line virtual destructor as the key function.
33RTDyldMemoryManager::~RTDyldMemoryManager() {}
34
Jim Grosbach6e563312011-03-21 22:15:52 +000035namespace llvm {
36class RuntimeDyldImpl {
Jim Grosbacha8287e32011-03-23 22:06:06 +000037 unsigned CPUType;
38 unsigned CPUSubtype;
39
Jim Grosbachfcbe5b72011-04-04 23:04:39 +000040 // The MemoryManager to load objects into.
41 RTDyldMemoryManager *MemMgr;
Jim Grosbach5acfa9f2011-03-29 21:03:05 +000042
Jim Grosbach6e563312011-03-21 22:15:52 +000043 // Master symbol table. As modules are loaded and external symbols are
44 // resolved, their addresses are stored here.
Jim Grosbachfcbe5b72011-04-04 23:04:39 +000045 StringMap<uint64_t> SymbolTable;
Jim Grosbach6e563312011-03-21 22:15:52 +000046
47 // FIXME: Should have multiple data blocks, one for each loaded chunk of
48 // compiled code.
49 sys::MemoryBlock Data;
50
51 bool HasError;
52 std::string ErrorStr;
53
54 // Set the error state and record an error string.
55 bool Error(const Twine &Msg) {
56 ErrorStr = Msg.str();
57 HasError = true;
58 return true;
59 }
60
Jim Grosbach8b54dca2011-03-23 19:52:00 +000061 bool resolveRelocation(uint32_t BaseSection, macho::RelocationEntry RE,
62 SmallVectorImpl<void *> &SectionBases,
63 SmallVectorImpl<StringRef> &SymbolNames);
Jim Grosbacha8287e32011-03-23 22:06:06 +000064 bool resolveX86_64Relocation(intptr_t Address, intptr_t Value, bool isPCRel,
65 unsigned Type, unsigned Size);
66 bool resolveARMRelocation(intptr_t Address, intptr_t Value, bool isPCRel,
67 unsigned Type, unsigned Size);
Jim Grosbach8b54dca2011-03-23 19:52:00 +000068
Jim Grosbach6e563312011-03-21 22:15:52 +000069 bool loadSegment32(const MachOObject *Obj,
70 const MachOObject::LoadCommandInfo *SegmentLCI,
71 const InMemoryStruct<macho::SymtabLoadCommand> &SymtabLC);
72 bool loadSegment64(const MachOObject *Obj,
73 const MachOObject::LoadCommandInfo *SegmentLCI,
74 const InMemoryStruct<macho::SymtabLoadCommand> &SymtabLC);
75
76public:
Jim Grosbachfcbe5b72011-04-04 23:04:39 +000077 RuntimeDyldImpl(RTDyldMemoryManager *mm) : MemMgr(mm), HasError(false) {}
Jim Grosbach8371c892011-03-22 00:42:19 +000078
Jim Grosbach6e563312011-03-21 22:15:52 +000079 bool loadObject(MemoryBuffer *InputBuffer);
80
Jim Grosbachfcbe5b72011-04-04 23:04:39 +000081 uint64_t getSymbolAddress(StringRef Name) {
Jim Grosbach6e563312011-03-21 22:15:52 +000082 // Use lookup() rather than [] because we don't want to add an entry
83 // if there isn't one already, which the [] operator does.
84 return SymbolTable.lookup(Name);
85 }
86
87 sys::MemoryBlock getMemoryBlock() { return Data; }
88
89 // Is the linker in an error state?
90 bool hasError() { return HasError; }
91
92 // Mark the error condition as handled and continue.
93 void clearError() { HasError = false; }
94
95 // Get the error message.
96 StringRef getErrorString() { return ErrorStr; }
97};
98
Jim Grosbach8b54dca2011-03-23 19:52:00 +000099// FIXME: Relocations for targets other than x86_64.
100bool RuntimeDyldImpl::
101resolveRelocation(uint32_t BaseSection, macho::RelocationEntry RE,
102 SmallVectorImpl<void *> &SectionBases,
103 SmallVectorImpl<StringRef> &SymbolNames) {
104 // struct relocation_info {
105 // int32_t r_address;
106 // uint32_t r_symbolnum:24,
107 // r_pcrel:1,
108 // r_length:2,
109 // r_extern:1,
110 // r_type:4;
111 // };
112 uint32_t SymbolNum = RE.Word1 & 0xffffff; // 24-bit value
113 bool isPCRel = (RE.Word1 >> 24) & 1;
114 unsigned Log2Size = (RE.Word1 >> 25) & 3;
115 bool isExtern = (RE.Word1 >> 27) & 1;
116 unsigned Type = (RE.Word1 >> 28) & 0xf;
117 if (RE.Word0 & macho::RF_Scattered)
118 return Error("NOT YET IMPLEMENTED: scattered relocations.");
Jim Grosbach6e563312011-03-21 22:15:52 +0000119
Jim Grosbach8b54dca2011-03-23 19:52:00 +0000120 // The address requiring a relocation.
121 intptr_t Address = (intptr_t)SectionBases[BaseSection] + RE.Word0;
122
123 // Figure out the target address of the relocation. If isExtern is true,
124 // this relocation references the symbol table, otherwise it references
125 // a section in the same object, numbered from 1 through NumSections
126 // (SectionBases is [0, NumSections-1]).
127 intptr_t Value;
128 if (isExtern) {
129 StringRef Name = SymbolNames[SymbolNum];
130 if (SymbolTable.lookup(Name)) {
131 // The symbol is in our symbol table, so we can resolve it directly.
132 Value = (intptr_t)SymbolTable[Name];
133 } else {
134 return Error("NOT YET IMPLEMENTED: relocations to pre-compiled code.");
135 }
136 DEBUG(dbgs() << "Resolve relocation(" << Type << ") from '" << Name
137 << "' to " << format("0x%x", Address) << ".\n");
138 } else {
139 // For non-external relocations, the SymbolNum is actual a section number
140 // as described above.
141 Value = (intptr_t)SectionBases[SymbolNum - 1];
142 }
143
Jim Grosbacha8287e32011-03-23 22:06:06 +0000144 unsigned Size = 1 << Log2Size;
145 switch (CPUType) {
146 default: assert(0 && "Unsupported CPU type!");
147 case mach::CTM_x86_64:
148 return resolveX86_64Relocation(Address, Value, isPCRel, Type, Size);
149 case mach::CTM_ARM:
150 return resolveARMRelocation(Address, Value, isPCRel, Type, Size);
151 }
152 llvm_unreachable("");
153}
154
155bool RuntimeDyldImpl::resolveX86_64Relocation(intptr_t Address, intptr_t Value,
156 bool isPCRel, unsigned Type,
157 unsigned Size) {
Jim Grosbach8b54dca2011-03-23 19:52:00 +0000158 // If the relocation is PC-relative, the value to be encoded is the
159 // pointer difference.
160 if (isPCRel)
161 // FIXME: It seems this value needs to be adjusted by 4 for an effective PC
162 // address. Is that expected? Only for branches, perhaps?
163 Value -= Address + 4;
164
165 switch(Type) {
166 default:
167 llvm_unreachable("Invalid relocation type!");
168 case macho::RIT_X86_64_Unsigned:
169 case macho::RIT_X86_64_Branch: {
170 // Mask in the target value a byte at a time (we don't have an alignment
171 // guarantee for the target address, so this is safest).
Jim Grosbach8b54dca2011-03-23 19:52:00 +0000172 uint8_t *p = (uint8_t*)Address;
Jim Grosbacha8287e32011-03-23 22:06:06 +0000173 for (unsigned i = 0; i < Size; ++i) {
Jim Grosbach8b54dca2011-03-23 19:52:00 +0000174 *p++ = (uint8_t)Value;
175 Value >>= 8;
176 }
177 return false;
178 }
179 case macho::RIT_X86_64_Signed:
180 case macho::RIT_X86_64_GOTLoad:
181 case macho::RIT_X86_64_GOT:
182 case macho::RIT_X86_64_Subtractor:
183 case macho::RIT_X86_64_Signed1:
184 case macho::RIT_X86_64_Signed2:
185 case macho::RIT_X86_64_Signed4:
186 case macho::RIT_X86_64_TLV:
187 return Error("Relocation type not implemented yet!");
188 }
189 return false;
190}
Jim Grosbach6e563312011-03-21 22:15:52 +0000191
Jim Grosbacha8287e32011-03-23 22:06:06 +0000192bool RuntimeDyldImpl::resolveARMRelocation(intptr_t Address, intptr_t Value,
193 bool isPCRel, unsigned Type,
194 unsigned Size) {
195 // If the relocation is PC-relative, the value to be encoded is the
196 // pointer difference.
197 if (isPCRel) {
198 Value -= Address;
199 // ARM PCRel relocations have an effective-PC offset of two instructions
200 // (four bytes in Thumb mode, 8 bytes in ARM mode).
201 // FIXME: For now, assume ARM mode.
202 Value -= 8;
203 }
204
205 switch(Type) {
206 default:
207 case macho::RIT_Vanilla: {
208 llvm_unreachable("Invalid relocation type!");
209 // Mask in the target value a byte at a time (we don't have an alignment
210 // guarantee for the target address, so this is safest).
211 uint8_t *p = (uint8_t*)Address;
212 for (unsigned i = 0; i < Size; ++i) {
213 *p++ = (uint8_t)Value;
214 Value >>= 8;
215 }
Jim Grosbach5ffe37f2011-03-23 23:35:17 +0000216 break;
Jim Grosbacha8287e32011-03-23 22:06:06 +0000217 }
218 case macho::RIT_Pair:
219 case macho::RIT_Difference:
220 case macho::RIT_ARM_LocalDifference:
221 case macho::RIT_ARM_PreboundLazyPointer:
Jim Grosbach5ffe37f2011-03-23 23:35:17 +0000222 case macho::RIT_ARM_Branch24Bit: {
223 // Mask the value into the target address. We know instructions are
224 // 32-bit aligned, so we can do it all at once.
225 uint32_t *p = (uint32_t*)Address;
226 // The low two bits of the value are not encoded.
227 Value >>= 2;
228 // Mask the value to 24 bits.
229 Value &= 0xffffff;
230 // FIXME: If the destination is a Thumb function (and the instruction
231 // is a non-predicated BL instruction), we need to change it to a BLX
232 // instruction instead.
233
234 // Insert the value into the instruction.
235 *p = (*p & ~0xffffff) | Value;
236 break;
237 }
Jim Grosbacha8287e32011-03-23 22:06:06 +0000238 case macho::RIT_ARM_ThumbBranch22Bit:
239 case macho::RIT_ARM_ThumbBranch32Bit:
240 case macho::RIT_ARM_Half:
241 case macho::RIT_ARM_HalfDifference:
242 return Error("Relocation type not implemented yet!");
243 }
244 return false;
245}
246
Jim Grosbach6e563312011-03-21 22:15:52 +0000247bool RuntimeDyldImpl::
248loadSegment32(const MachOObject *Obj,
249 const MachOObject::LoadCommandInfo *SegmentLCI,
250 const InMemoryStruct<macho::SymtabLoadCommand> &SymtabLC) {
251 InMemoryStruct<macho::SegmentLoadCommand> Segment32LC;
252 Obj->ReadSegmentLoadCommand(*SegmentLCI, Segment32LC);
253 if (!Segment32LC)
254 return Error("unable to load segment load command");
255
256 // Map the segment into memory.
257 std::string ErrorStr;
258 Data = sys::Memory::AllocateRWX(Segment32LC->VMSize, 0, &ErrorStr);
259 if (!Data.base())
260 return Error("unable to allocate memory block: '" + ErrorStr + "'");
261 memcpy(Data.base(), Obj->getData(Segment32LC->FileOffset,
262 Segment32LC->FileSize).data(),
263 Segment32LC->FileSize);
264 memset((char*)Data.base() + Segment32LC->FileSize, 0,
265 Segment32LC->VMSize - Segment32LC->FileSize);
266
Jim Grosbach5ffe37f2011-03-23 23:35:17 +0000267 // Bind the section indices to addresses and record the relocations we
268 // need to resolve.
269 typedef std::pair<uint32_t, macho::RelocationEntry> RelocationMap;
270 SmallVector<RelocationMap, 64> Relocations;
271
Jim Grosbach8b54dca2011-03-23 19:52:00 +0000272 SmallVector<void *, 16> SectionBases;
Jim Grosbach6e563312011-03-21 22:15:52 +0000273 for (unsigned i = 0; i != Segment32LC->NumSections; ++i) {
274 InMemoryStruct<macho::Section> Sect;
275 Obj->ReadSection(*SegmentLCI, i, Sect);
276 if (!Sect)
277 return Error("unable to load section: '" + Twine(i) + "'");
278
Jim Grosbach5ffe37f2011-03-23 23:35:17 +0000279 // Remember any relocations the section has so we can resolve them later.
280 for (unsigned j = 0; j != Sect->NumRelocationTableEntries; ++j) {
281 InMemoryStruct<macho::RelocationEntry> RE;
282 Obj->ReadRelocationEntry(Sect->RelocationTableOffset, j, RE);
283 Relocations.push_back(RelocationMap(j, *RE));
284 }
Jim Grosbach6e563312011-03-21 22:15:52 +0000285
286 // FIXME: Improve check.
Jim Grosbach5ffe37f2011-03-23 23:35:17 +0000287// if (Sect->Flags != 0x80000400)
288// return Error("unsupported section type!");
Jim Grosbach6e563312011-03-21 22:15:52 +0000289
Jim Grosbach8b54dca2011-03-23 19:52:00 +0000290 SectionBases.push_back((char*) Data.base() + Sect->Address);
Jim Grosbach6e563312011-03-21 22:15:52 +0000291 }
292
Jim Grosbach5ffe37f2011-03-23 23:35:17 +0000293 // Bind all the symbols to address. Keep a record of the names for use
294 // by relocation resolution.
295 SmallVector<StringRef, 64> SymbolNames;
Jim Grosbach6e563312011-03-21 22:15:52 +0000296 for (unsigned i = 0; i != SymtabLC->NumSymbolTableEntries; ++i) {
297 InMemoryStruct<macho::SymbolTableEntry> STE;
298 Obj->ReadSymbolTableEntry(SymtabLC->SymbolTableOffset, i, STE);
299 if (!STE)
300 return Error("unable to read symbol: '" + Twine(i) + "'");
Jim Grosbach5ffe37f2011-03-23 23:35:17 +0000301 // Get the symbol name.
302 StringRef Name = Obj->getStringAtIndex(STE->StringIndex);
303 SymbolNames.push_back(Name);
304
305 // Just skip undefined symbols. They'll be loaded from whatever
306 // module they come from (or system dylib) when we resolve relocations
307 // involving them.
Jim Grosbach6e563312011-03-21 22:15:52 +0000308 if (STE->SectionIndex == 0)
Jim Grosbach5ffe37f2011-03-23 23:35:17 +0000309 continue;
Jim Grosbach6e563312011-03-21 22:15:52 +0000310
311 unsigned Index = STE->SectionIndex - 1;
312 if (Index >= Segment32LC->NumSections)
313 return Error("invalid section index for symbol: '" + Twine() + "'");
314
Jim Grosbach6e563312011-03-21 22:15:52 +0000315 // Get the section base address.
316 void *SectionBase = SectionBases[Index];
317
318 // Get the symbol address.
Jim Grosbachfcbe5b72011-04-04 23:04:39 +0000319 uint64_t Address = (uint64_t)SectionBase + STE->Value;
Jim Grosbach6e563312011-03-21 22:15:52 +0000320
321 // FIXME: Check the symbol type and flags.
322 if (STE->Type != 0xF)
323 return Error("unexpected symbol type!");
324 if (STE->Flags != 0x0)
325 return Error("unexpected symbol type!");
326
Jim Grosbach8b54dca2011-03-23 19:52:00 +0000327 DEBUG(dbgs() << "Symbol: '" << Name << "' @ " << Address << "\n");
328
Jim Grosbach6e563312011-03-21 22:15:52 +0000329 SymbolTable[Name] = Address;
330 }
331
Jim Grosbach5ffe37f2011-03-23 23:35:17 +0000332 // Now resolve any relocations.
333 for (unsigned i = 0, e = Relocations.size(); i != e; ++i) {
334 if (resolveRelocation(Relocations[i].first, Relocations[i].second,
335 SectionBases, SymbolNames))
336 return true;
337 }
338
Jim Grosbachf9229102011-03-22 01:06:42 +0000339 // We've loaded the section; now mark the functions in it as executable.
Jim Grosbachfcbe5b72011-04-04 23:04:39 +0000340 // FIXME: We really should use the MemoryManager for this.
Jim Grosbachf9229102011-03-22 01:06:42 +0000341 sys::Memory::setRangeExecutable(Data.base(), Data.size());
342
Jim Grosbach6e563312011-03-21 22:15:52 +0000343 return false;
344}
345
346
347bool RuntimeDyldImpl::
348loadSegment64(const MachOObject *Obj,
349 const MachOObject::LoadCommandInfo *SegmentLCI,
350 const InMemoryStruct<macho::SymtabLoadCommand> &SymtabLC) {
351 InMemoryStruct<macho::Segment64LoadCommand> Segment64LC;
352 Obj->ReadSegment64LoadCommand(*SegmentLCI, Segment64LC);
353 if (!Segment64LC)
354 return Error("unable to load segment load command");
355
356 // Map the segment into memory.
357 std::string ErrorStr;
358 Data = sys::Memory::AllocateRWX(Segment64LC->VMSize, 0, &ErrorStr);
359 if (!Data.base())
360 return Error("unable to allocate memory block: '" + ErrorStr + "'");
361 memcpy(Data.base(), Obj->getData(Segment64LC->FileOffset,
362 Segment64LC->FileSize).data(),
363 Segment64LC->FileSize);
364 memset((char*)Data.base() + Segment64LC->FileSize, 0,
365 Segment64LC->VMSize - Segment64LC->FileSize);
366
Jim Grosbach8b54dca2011-03-23 19:52:00 +0000367 // Bind the section indices to addresses and record the relocations we
368 // need to resolve.
369 typedef std::pair<uint32_t, macho::RelocationEntry> RelocationMap;
370 SmallVector<RelocationMap, 64> Relocations;
371
372 SmallVector<void *, 16> SectionBases;
Jim Grosbach6e563312011-03-21 22:15:52 +0000373 for (unsigned i = 0; i != Segment64LC->NumSections; ++i) {
374 InMemoryStruct<macho::Section64> Sect;
375 Obj->ReadSection64(*SegmentLCI, i, Sect);
376 if (!Sect)
377 return Error("unable to load section: '" + Twine(i) + "'");
378
Jim Grosbach5ffe37f2011-03-23 23:35:17 +0000379 // Remember any relocations the section has so we can resolve them later.
Jim Grosbach8b54dca2011-03-23 19:52:00 +0000380 for (unsigned j = 0; j != Sect->NumRelocationTableEntries; ++j) {
381 InMemoryStruct<macho::RelocationEntry> RE;
382 Obj->ReadRelocationEntry(Sect->RelocationTableOffset, j, RE);
383 Relocations.push_back(RelocationMap(j, *RE));
384 }
Jim Grosbach6e563312011-03-21 22:15:52 +0000385
386 // FIXME: Improve check.
387 if (Sect->Flags != 0x80000400)
388 return Error("unsupported section type!");
389
Jim Grosbach8b54dca2011-03-23 19:52:00 +0000390 SectionBases.push_back((char*) Data.base() + Sect->Address);
Jim Grosbach6e563312011-03-21 22:15:52 +0000391 }
392
Jim Grosbach8b54dca2011-03-23 19:52:00 +0000393 // Bind all the symbols to address. Keep a record of the names for use
394 // by relocation resolution.
395 SmallVector<StringRef, 64> SymbolNames;
Jim Grosbach6e563312011-03-21 22:15:52 +0000396 for (unsigned i = 0; i != SymtabLC->NumSymbolTableEntries; ++i) {
397 InMemoryStruct<macho::Symbol64TableEntry> STE;
398 Obj->ReadSymbol64TableEntry(SymtabLC->SymbolTableOffset, i, STE);
399 if (!STE)
400 return Error("unable to read symbol: '" + Twine(i) + "'");
Jim Grosbach8b54dca2011-03-23 19:52:00 +0000401 // Get the symbol name.
402 StringRef Name = Obj->getStringAtIndex(STE->StringIndex);
403 SymbolNames.push_back(Name);
404
405 // Just skip undefined symbols. They'll be loaded from whatever
406 // module they come from (or system dylib) when we resolve relocations
407 // involving them.
Jim Grosbach6e563312011-03-21 22:15:52 +0000408 if (STE->SectionIndex == 0)
Jim Grosbach8b54dca2011-03-23 19:52:00 +0000409 continue;
Jim Grosbach6e563312011-03-21 22:15:52 +0000410
411 unsigned Index = STE->SectionIndex - 1;
412 if (Index >= Segment64LC->NumSections)
413 return Error("invalid section index for symbol: '" + Twine() + "'");
414
Jim Grosbach6e563312011-03-21 22:15:52 +0000415 // Get the section base address.
416 void *SectionBase = SectionBases[Index];
417
418 // Get the symbol address.
Jim Grosbachfcbe5b72011-04-04 23:04:39 +0000419 uint64_t Address = (uint64_t) SectionBase + STE->Value;
Jim Grosbach6e563312011-03-21 22:15:52 +0000420
421 // FIXME: Check the symbol type and flags.
422 if (STE->Type != 0xF)
423 return Error("unexpected symbol type!");
424 if (STE->Flags != 0x0)
425 return Error("unexpected symbol type!");
426
Jim Grosbach8b54dca2011-03-23 19:52:00 +0000427 DEBUG(dbgs() << "Symbol: '" << Name << "' @ " << Address << "\n");
Jim Grosbach6e563312011-03-21 22:15:52 +0000428 SymbolTable[Name] = Address;
429 }
430
Jim Grosbach8b54dca2011-03-23 19:52:00 +0000431 // Now resolve any relocations.
432 for (unsigned i = 0, e = Relocations.size(); i != e; ++i) {
433 if (resolveRelocation(Relocations[i].first, Relocations[i].second,
434 SectionBases, SymbolNames))
435 return true;
436 }
437
Jim Grosbachf9229102011-03-22 01:06:42 +0000438 // We've loaded the section; now mark the functions in it as executable.
Jim Grosbachfcbe5b72011-04-04 23:04:39 +0000439 // FIXME: We really should use the MemoryManager for this.
Jim Grosbachf9229102011-03-22 01:06:42 +0000440 sys::Memory::setRangeExecutable(Data.base(), Data.size());
441
Jim Grosbach6e563312011-03-21 22:15:52 +0000442 return false;
443}
444
Jim Grosbach6e563312011-03-21 22:15:52 +0000445bool RuntimeDyldImpl::loadObject(MemoryBuffer *InputBuffer) {
446 // If the linker is in an error state, don't do anything.
447 if (hasError())
448 return true;
449 // Load the Mach-O wrapper object.
450 std::string ErrorStr;
451 OwningPtr<MachOObject> Obj(
452 MachOObject::LoadFromBuffer(InputBuffer, &ErrorStr));
453 if (!Obj)
454 return Error("unable to load object: '" + ErrorStr + "'");
455
Jim Grosbacha8287e32011-03-23 22:06:06 +0000456 // Get the CPU type information from the header.
457 const macho::Header &Header = Obj->getHeader();
458
459 // FIXME: Error checking that the loaded object is compatible with
460 // the system we're running on.
461 CPUType = Header.CPUType;
462 CPUSubtype = Header.CPUSubtype;
463
Jim Grosbach6e563312011-03-21 22:15:52 +0000464 // Validate that the load commands match what we expect.
465 const MachOObject::LoadCommandInfo *SegmentLCI = 0, *SymtabLCI = 0,
466 *DysymtabLCI = 0;
Jim Grosbacha8287e32011-03-23 22:06:06 +0000467 for (unsigned i = 0; i != Header.NumLoadCommands; ++i) {
Jim Grosbach6e563312011-03-21 22:15:52 +0000468 const MachOObject::LoadCommandInfo &LCI = Obj->getLoadCommandInfo(i);
469 switch (LCI.Command.Type) {
470 case macho::LCT_Segment:
471 case macho::LCT_Segment64:
472 if (SegmentLCI)
473 return Error("unexpected input object (multiple segments)");
474 SegmentLCI = &LCI;
475 break;
476 case macho::LCT_Symtab:
477 if (SymtabLCI)
478 return Error("unexpected input object (multiple symbol tables)");
479 SymtabLCI = &LCI;
480 break;
481 case macho::LCT_Dysymtab:
482 if (DysymtabLCI)
483 return Error("unexpected input object (multiple symbol tables)");
484 DysymtabLCI = &LCI;
485 break;
486 default:
487 return Error("unexpected input object (unexpected load command");
488 }
489 }
490
491 if (!SymtabLCI)
492 return Error("no symbol table found in object");
493 if (!SegmentLCI)
494 return Error("no symbol table found in object");
495
496 // Read and register the symbol table data.
497 InMemoryStruct<macho::SymtabLoadCommand> SymtabLC;
498 Obj->ReadSymtabLoadCommand(*SymtabLCI, SymtabLC);
499 if (!SymtabLC)
500 return Error("unable to load symbol table load command");
501 Obj->RegisterStringTable(*SymtabLC);
502
503 // Read the dynamic link-edit information, if present (not present in static
504 // objects).
505 if (DysymtabLCI) {
506 InMemoryStruct<macho::DysymtabLoadCommand> DysymtabLC;
507 Obj->ReadDysymtabLoadCommand(*DysymtabLCI, DysymtabLC);
508 if (!DysymtabLC)
509 return Error("unable to load dynamic link-exit load command");
510
511 // FIXME: We don't support anything interesting yet.
Jim Grosbach8b54dca2011-03-23 19:52:00 +0000512// if (DysymtabLC->LocalSymbolsIndex != 0)
513// return Error("NOT YET IMPLEMENTED: local symbol entries");
514// if (DysymtabLC->ExternalSymbolsIndex != 0)
515// return Error("NOT YET IMPLEMENTED: non-external symbol entries");
516// if (DysymtabLC->UndefinedSymbolsIndex != SymtabLC->NumSymbolTableEntries)
517// return Error("NOT YET IMPLEMENTED: undefined symbol entries");
Jim Grosbach6e563312011-03-21 22:15:52 +0000518 }
519
520 // Load the segment load command.
521 if (SegmentLCI->Command.Type == macho::LCT_Segment) {
522 if (loadSegment32(Obj.get(), SegmentLCI, SymtabLC))
523 return true;
524 } else {
525 if (loadSegment64(Obj.get(), SegmentLCI, SymtabLC))
526 return true;
527 }
528
529 return false;
530}
531
532
533//===----------------------------------------------------------------------===//
534// RuntimeDyld class implementation
Jim Grosbachfcbe5b72011-04-04 23:04:39 +0000535RuntimeDyld::RuntimeDyld(RTDyldMemoryManager *MM) {
536 Dyld = new RuntimeDyldImpl(MM);
Jim Grosbach6e563312011-03-21 22:15:52 +0000537}
538
539RuntimeDyld::~RuntimeDyld() {
540 delete Dyld;
541}
542
543bool RuntimeDyld::loadObject(MemoryBuffer *InputBuffer) {
544 return Dyld->loadObject(InputBuffer);
545}
546
Jim Grosbachfcbe5b72011-04-04 23:04:39 +0000547uint64_t RuntimeDyld::getSymbolAddress(StringRef Name) {
Jim Grosbach6e563312011-03-21 22:15:52 +0000548 return Dyld->getSymbolAddress(Name);
549}
550
551sys::MemoryBlock RuntimeDyld::getMemoryBlock() {
552 return Dyld->getMemoryBlock();
553}
554
Jim Grosbach91dde152011-03-22 18:22:27 +0000555StringRef RuntimeDyld::getErrorString() {
Jim Grosbachb3eecaf2011-03-22 18:19:42 +0000556 return Dyld->getErrorString();
557}
558
Jim Grosbach6e563312011-03-21 22:15:52 +0000559} // end namespace llvm