blob: dbb83faaa0146657408fd2e315a04e511ec921fa [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"
Jim Grosbachc41ab782011-04-06 01:11:05 +000019#include "llvm/ADT/STLExtras.h"
Jim Grosbach6e563312011-03-21 22:15:52 +000020#include "llvm/ADT/Twine.h"
21#include "llvm/ExecutionEngine/RuntimeDyld.h"
22#include "llvm/Object/MachOObject.h"
Jim Grosbach8b54dca2011-03-23 19:52:00 +000023#include "llvm/Support/Debug.h"
24#include "llvm/Support/ErrorHandling.h"
25#include "llvm/Support/Format.h"
Jim Grosbach6e563312011-03-21 22:15:52 +000026#include "llvm/Support/Memory.h"
27#include "llvm/Support/MemoryBuffer.h"
28#include "llvm/Support/system_error.h"
Jim Grosbach8b54dca2011-03-23 19:52:00 +000029#include "llvm/Support/raw_ostream.h"
Jim Grosbach6e563312011-03-21 22:15:52 +000030using namespace llvm;
31using namespace llvm::object;
32
Chandler Carruth53c5e7b2011-04-05 23:54:31 +000033// Empty out-of-line virtual destructor as the key function.
34RTDyldMemoryManager::~RTDyldMemoryManager() {}
35
Jim Grosbach6e563312011-03-21 22:15:52 +000036namespace llvm {
37class RuntimeDyldImpl {
Jim Grosbacha8287e32011-03-23 22:06:06 +000038 unsigned CPUType;
39 unsigned CPUSubtype;
40
Jim Grosbachfcbe5b72011-04-04 23:04:39 +000041 // The MemoryManager to load objects into.
42 RTDyldMemoryManager *MemMgr;
Jim Grosbach5acfa9f2011-03-29 21:03:05 +000043
Jim Grosbachc41ab782011-04-06 01:11:05 +000044
45 // For each function, we have a MemoryBlock of it's instruction data.
46 StringMap<sys::MemoryBlock> Functions;
47
Jim Grosbach6e563312011-03-21 22:15:52 +000048 // Master symbol table. As modules are loaded and external symbols are
49 // resolved, their addresses are stored here.
Jim Grosbachfcbe5b72011-04-04 23:04:39 +000050 StringMap<uint64_t> SymbolTable;
Jim Grosbach6e563312011-03-21 22:15:52 +000051
52 // FIXME: Should have multiple data blocks, one for each loaded chunk of
53 // compiled code.
Jim Grosbach7cbf92d2011-04-12 00:23:32 +000054// sys::MemoryBlock Data;
Jim Grosbach6e563312011-03-21 22:15:52 +000055
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 Grosbachc41ab782011-04-06 01:11:05 +000066 void extractFunction(StringRef Name, uint8_t *StartAddress,
67 uint8_t *EndAddress);
Jim Grosbach8b54dca2011-03-23 19:52:00 +000068 bool resolveRelocation(uint32_t BaseSection, macho::RelocationEntry RE,
69 SmallVectorImpl<void *> &SectionBases,
70 SmallVectorImpl<StringRef> &SymbolNames);
Jim Grosbacha8287e32011-03-23 22:06:06 +000071 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 Grosbach8b54dca2011-03-23 19:52:00 +000075
Jim Grosbach6e563312011-03-21 22:15:52 +000076 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
83public:
Jim Grosbachfcbe5b72011-04-04 23:04:39 +000084 RuntimeDyldImpl(RTDyldMemoryManager *mm) : MemMgr(mm), HasError(false) {}
Jim Grosbach8371c892011-03-22 00:42:19 +000085
Jim Grosbach6e563312011-03-21 22:15:52 +000086 bool loadObject(MemoryBuffer *InputBuffer);
87
Jim Grosbachb0271052011-04-08 17:31:24 +000088 void *getSymbolAddress(StringRef Name) {
Jim Grosbachc41ab782011-04-06 01:11:05 +000089 // FIXME: Just look up as a function for now. Overly simple of course.
90 // Work in progress.
Jim Grosbachb0271052011-04-08 17:31:24 +000091 return Functions.lookup(Name).base();
Jim Grosbach6e563312011-03-21 22:15:52 +000092 }
93
Jim Grosbach6e563312011-03-21 22:15:52 +000094 // 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 Grosbachc41ab782011-04-06 01:11:05 +0000104void RuntimeDyldImpl::extractFunction(StringRef Name, uint8_t *StartAddress,
Jim Grosbach01ccab42011-04-06 22:13:52 +0000105 uint8_t *EndAddress) {
Jim Grosbachc41ab782011-04-06 01:11:05 +0000106 // 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 Grosbach8b54dca2011-03-23 19:52:00 +0000119bool RuntimeDyldImpl::
120resolveRelocation(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 Grosbach6e563312011-03-21 22:15:52 +0000138
Jim Grosbach8b54dca2011-03-23 19:52:00 +0000139 // 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 Grosbacha8287e32011-03-23 22:06:06 +0000163 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
174bool RuntimeDyldImpl::resolveX86_64Relocation(intptr_t Address, intptr_t Value,
175 bool isPCRel, unsigned Type,
176 unsigned Size) {
Jim Grosbach8b54dca2011-03-23 19:52:00 +0000177 // 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 Grosbach8b54dca2011-03-23 19:52:00 +0000191 uint8_t *p = (uint8_t*)Address;
Jim Grosbacha8287e32011-03-23 22:06:06 +0000192 for (unsigned i = 0; i < Size; ++i) {
Jim Grosbach8b54dca2011-03-23 19:52:00 +0000193 *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 Grosbach6e563312011-03-21 22:15:52 +0000210
Jim Grosbacha8287e32011-03-23 22:06:06 +0000211bool 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 Grosbach5ffe37f2011-03-23 23:35:17 +0000235 break;
Jim Grosbacha8287e32011-03-23 22:06:06 +0000236 }
237 case macho::RIT_Pair:
238 case macho::RIT_Difference:
239 case macho::RIT_ARM_LocalDifference:
240 case macho::RIT_ARM_PreboundLazyPointer:
Jim Grosbach5ffe37f2011-03-23 23:35:17 +0000241 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 Grosbacha8287e32011-03-23 22:06:06 +0000257 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 Grosbach6e563312011-03-21 22:15:52 +0000266bool RuntimeDyldImpl::
267loadSegment32(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 Grosbachb0271052011-04-08 17:31:24 +0000275 for (unsigned SectNum = 0; SectNum != Segment32LC->NumSections; ++SectNum) {
Jim Grosbach6e563312011-03-21 22:15:52 +0000276 InMemoryStruct<macho::Section> Sect;
Jim Grosbachb0271052011-04-08 17:31:24 +0000277 Obj->ReadSection(*SegmentLCI, SectNum, Sect);
278 if (!Sect)
279 return Error("unable to load section: '" + Twine(SectNum) + "'");
Jim Grosbach6e563312011-03-21 22:15:52 +0000280
281 // FIXME: Improve check.
Jim Grosbachb0271052011-04-08 17:31:24 +0000282 if (Sect->Flags != 0x80000400)
283 return Error("unsupported section type!");
Jim Grosbach6e563312011-03-21 22:15:52 +0000284
Jim Grosbachb0271052011-04-08 17:31:24 +0000285 // 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 Kramercc513e1c2011-04-09 10:10:35 +0000294 return Error("invalid section index for symbol: '" + Twine(i) + "'");
Jim Grosbachb0271052011-04-08 17:31:24 +0000295
296 // Just skip symbols not defined in this section.
Jim Grosbache2e777b2011-04-08 21:11:20 +0000297 if ((unsigned)STE->SectionIndex - 1 != SectNum)
Jim Grosbachb0271052011-04-08 17:31:24 +0000298 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 Grosbach6e563312011-03-21 22:15:52 +0000339 }
340
Jim Grosbach6e563312011-03-21 22:15:52 +0000341 return false;
342}
343
344
345bool RuntimeDyldImpl::
346loadSegment64(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 Grosbachc41ab782011-04-06 01:11:05 +0000354 for (unsigned SectNum = 0; SectNum != Segment64LC->NumSections; ++SectNum) {
Jim Grosbach6e563312011-03-21 22:15:52 +0000355 InMemoryStruct<macho::Section64> Sect;
Jim Grosbachc41ab782011-04-06 01:11:05 +0000356 Obj->ReadSection64(*SegmentLCI, SectNum, Sect);
Jim Grosbach6e563312011-03-21 22:15:52 +0000357 if (!Sect)
Jim Grosbachc41ab782011-04-06 01:11:05 +0000358 return Error("unable to load section: '" + Twine(SectNum) + "'");
Jim Grosbach6e563312011-03-21 22:15:52 +0000359
360 // FIXME: Improve check.
361 if (Sect->Flags != 0x80000400)
362 return Error("unsupported section type!");
363
Jim Grosbachc41ab782011-04-06 01:11:05 +0000364 // 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 Kramercc513e1c2011-04-09 10:10:35 +0000373 return Error("invalid section index for symbol: '" + Twine(i) + "'");
Jim Grosbachc41ab782011-04-06 01:11:05 +0000374
375 // Just skip symbols not defined in this section.
Jim Grosbache2e777b2011-04-08 21:11:20 +0000376 if ((unsigned)STE->SectionIndex - 1 != SectNum)
Jim Grosbachc41ab782011-04-06 01:11:05 +0000377 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 Grosbach6e563312011-03-21 22:15:52 +0000418 }
419
Jim Grosbach6e563312011-03-21 22:15:52 +0000420 return false;
421}
422
Jim Grosbach6e563312011-03-21 22:15:52 +0000423bool 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 Grosbacha8287e32011-03-23 22:06:06 +0000434 // 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 Grosbach6e563312011-03-21 22:15:52 +0000442 // Validate that the load commands match what we expect.
443 const MachOObject::LoadCommandInfo *SegmentLCI = 0, *SymtabLCI = 0,
444 *DysymtabLCI = 0;
Jim Grosbacha8287e32011-03-23 22:06:06 +0000445 for (unsigned i = 0; i != Header.NumLoadCommands; ++i) {
Jim Grosbach6e563312011-03-21 22:15:52 +0000446 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 Grosbach8b54dca2011-03-23 19:52:00 +0000490// 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 Grosbach6e563312011-03-21 22:15:52 +0000496 }
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 Grosbachfcbe5b72011-04-04 23:04:39 +0000513RuntimeDyld::RuntimeDyld(RTDyldMemoryManager *MM) {
514 Dyld = new RuntimeDyldImpl(MM);
Jim Grosbach6e563312011-03-21 22:15:52 +0000515}
516
517RuntimeDyld::~RuntimeDyld() {
518 delete Dyld;
519}
520
521bool RuntimeDyld::loadObject(MemoryBuffer *InputBuffer) {
522 return Dyld->loadObject(InputBuffer);
523}
524
Jim Grosbachb0271052011-04-08 17:31:24 +0000525void *RuntimeDyld::getSymbolAddress(StringRef Name) {
Jim Grosbach6e563312011-03-21 22:15:52 +0000526 return Dyld->getSymbolAddress(Name);
527}
528
Jim Grosbach91dde152011-03-22 18:22:27 +0000529StringRef RuntimeDyld::getErrorString() {
Jim Grosbachb3eecaf2011-03-22 18:19:42 +0000530 return Dyld->getErrorString();
531}
532
Jim Grosbach6e563312011-03-21 22:15:52 +0000533} // end namespace llvm