blob: de54f082eb4bf72054aeb5168e20a7422580d7b4 [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.
54 sys::MemoryBlock Data;
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 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 Grosbachfcbe5b72011-04-04 23:04:39 +000088 uint64_t 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.
91 return (uint64_t)Functions.lookup(Name).base();
Jim Grosbach6e563312011-03-21 22:15:52 +000092 }
93
94 sys::MemoryBlock getMemoryBlock() { return Data; }
95
96 // Is the linker in an error state?
97 bool hasError() { return HasError; }
98
99 // Mark the error condition as handled and continue.
100 void clearError() { HasError = false; }
101
102 // Get the error message.
103 StringRef getErrorString() { return ErrorStr; }
104};
105
Jim Grosbachc41ab782011-04-06 01:11:05 +0000106void RuntimeDyldImpl::extractFunction(StringRef Name, uint8_t *StartAddress,
107 uint8_t *EndAddress) {
108 // Allocate memory for the function via the memory manager.
109 uintptr_t Size = EndAddress - StartAddress + 1;
110 uint8_t *Mem = MemMgr->startFunctionBody(Name.data(), Size);
111 assert(Size >= (uint64_t)(EndAddress - StartAddress + 1) &&
112 "Memory manager failed to allocate enough memory!");
113 // Copy the function payload into the memory block.
114 memcpy(Mem, StartAddress, EndAddress - StartAddress + 1);
115 MemMgr->endFunctionBody(Name.data(), Mem, Mem + Size);
116 // Remember where we put it.
117 Functions[Name] = sys::MemoryBlock(Mem, Size);
118 DEBUG(dbgs() << " allocated to " << Mem << "\n");
119}
120
Jim Grosbach8b54dca2011-03-23 19:52:00 +0000121bool RuntimeDyldImpl::
122resolveRelocation(uint32_t BaseSection, macho::RelocationEntry RE,
123 SmallVectorImpl<void *> &SectionBases,
124 SmallVectorImpl<StringRef> &SymbolNames) {
125 // struct relocation_info {
126 // int32_t r_address;
127 // uint32_t r_symbolnum:24,
128 // r_pcrel:1,
129 // r_length:2,
130 // r_extern:1,
131 // r_type:4;
132 // };
133 uint32_t SymbolNum = RE.Word1 & 0xffffff; // 24-bit value
134 bool isPCRel = (RE.Word1 >> 24) & 1;
135 unsigned Log2Size = (RE.Word1 >> 25) & 3;
136 bool isExtern = (RE.Word1 >> 27) & 1;
137 unsigned Type = (RE.Word1 >> 28) & 0xf;
138 if (RE.Word0 & macho::RF_Scattered)
139 return Error("NOT YET IMPLEMENTED: scattered relocations.");
Jim Grosbach6e563312011-03-21 22:15:52 +0000140
Jim Grosbach8b54dca2011-03-23 19:52:00 +0000141 // The address requiring a relocation.
142 intptr_t Address = (intptr_t)SectionBases[BaseSection] + RE.Word0;
143
144 // Figure out the target address of the relocation. If isExtern is true,
145 // this relocation references the symbol table, otherwise it references
146 // a section in the same object, numbered from 1 through NumSections
147 // (SectionBases is [0, NumSections-1]).
148 intptr_t Value;
149 if (isExtern) {
150 StringRef Name = SymbolNames[SymbolNum];
151 if (SymbolTable.lookup(Name)) {
152 // The symbol is in our symbol table, so we can resolve it directly.
153 Value = (intptr_t)SymbolTable[Name];
154 } else {
155 return Error("NOT YET IMPLEMENTED: relocations to pre-compiled code.");
156 }
157 DEBUG(dbgs() << "Resolve relocation(" << Type << ") from '" << Name
158 << "' to " << format("0x%x", Address) << ".\n");
159 } else {
160 // For non-external relocations, the SymbolNum is actual a section number
161 // as described above.
162 Value = (intptr_t)SectionBases[SymbolNum - 1];
163 }
164
Jim Grosbacha8287e32011-03-23 22:06:06 +0000165 unsigned Size = 1 << Log2Size;
166 switch (CPUType) {
167 default: assert(0 && "Unsupported CPU type!");
168 case mach::CTM_x86_64:
169 return resolveX86_64Relocation(Address, Value, isPCRel, Type, Size);
170 case mach::CTM_ARM:
171 return resolveARMRelocation(Address, Value, isPCRel, Type, Size);
172 }
173 llvm_unreachable("");
174}
175
176bool RuntimeDyldImpl::resolveX86_64Relocation(intptr_t Address, intptr_t Value,
177 bool isPCRel, unsigned Type,
178 unsigned Size) {
Jim Grosbach8b54dca2011-03-23 19:52:00 +0000179 // If the relocation is PC-relative, the value to be encoded is the
180 // pointer difference.
181 if (isPCRel)
182 // FIXME: It seems this value needs to be adjusted by 4 for an effective PC
183 // address. Is that expected? Only for branches, perhaps?
184 Value -= Address + 4;
185
186 switch(Type) {
187 default:
188 llvm_unreachable("Invalid relocation type!");
189 case macho::RIT_X86_64_Unsigned:
190 case macho::RIT_X86_64_Branch: {
191 // Mask in the target value a byte at a time (we don't have an alignment
192 // guarantee for the target address, so this is safest).
Jim Grosbach8b54dca2011-03-23 19:52:00 +0000193 uint8_t *p = (uint8_t*)Address;
Jim Grosbacha8287e32011-03-23 22:06:06 +0000194 for (unsigned i = 0; i < Size; ++i) {
Jim Grosbach8b54dca2011-03-23 19:52:00 +0000195 *p++ = (uint8_t)Value;
196 Value >>= 8;
197 }
198 return false;
199 }
200 case macho::RIT_X86_64_Signed:
201 case macho::RIT_X86_64_GOTLoad:
202 case macho::RIT_X86_64_GOT:
203 case macho::RIT_X86_64_Subtractor:
204 case macho::RIT_X86_64_Signed1:
205 case macho::RIT_X86_64_Signed2:
206 case macho::RIT_X86_64_Signed4:
207 case macho::RIT_X86_64_TLV:
208 return Error("Relocation type not implemented yet!");
209 }
210 return false;
211}
Jim Grosbach6e563312011-03-21 22:15:52 +0000212
Jim Grosbacha8287e32011-03-23 22:06:06 +0000213bool RuntimeDyldImpl::resolveARMRelocation(intptr_t Address, intptr_t Value,
214 bool isPCRel, unsigned Type,
215 unsigned Size) {
216 // If the relocation is PC-relative, the value to be encoded is the
217 // pointer difference.
218 if (isPCRel) {
219 Value -= Address;
220 // ARM PCRel relocations have an effective-PC offset of two instructions
221 // (four bytes in Thumb mode, 8 bytes in ARM mode).
222 // FIXME: For now, assume ARM mode.
223 Value -= 8;
224 }
225
226 switch(Type) {
227 default:
228 case macho::RIT_Vanilla: {
229 llvm_unreachable("Invalid relocation type!");
230 // Mask in the target value a byte at a time (we don't have an alignment
231 // guarantee for the target address, so this is safest).
232 uint8_t *p = (uint8_t*)Address;
233 for (unsigned i = 0; i < Size; ++i) {
234 *p++ = (uint8_t)Value;
235 Value >>= 8;
236 }
Jim Grosbach5ffe37f2011-03-23 23:35:17 +0000237 break;
Jim Grosbacha8287e32011-03-23 22:06:06 +0000238 }
239 case macho::RIT_Pair:
240 case macho::RIT_Difference:
241 case macho::RIT_ARM_LocalDifference:
242 case macho::RIT_ARM_PreboundLazyPointer:
Jim Grosbach5ffe37f2011-03-23 23:35:17 +0000243 case macho::RIT_ARM_Branch24Bit: {
244 // Mask the value into the target address. We know instructions are
245 // 32-bit aligned, so we can do it all at once.
246 uint32_t *p = (uint32_t*)Address;
247 // The low two bits of the value are not encoded.
248 Value >>= 2;
249 // Mask the value to 24 bits.
250 Value &= 0xffffff;
251 // FIXME: If the destination is a Thumb function (and the instruction
252 // is a non-predicated BL instruction), we need to change it to a BLX
253 // instruction instead.
254
255 // Insert the value into the instruction.
256 *p = (*p & ~0xffffff) | Value;
257 break;
258 }
Jim Grosbacha8287e32011-03-23 22:06:06 +0000259 case macho::RIT_ARM_ThumbBranch22Bit:
260 case macho::RIT_ARM_ThumbBranch32Bit:
261 case macho::RIT_ARM_Half:
262 case macho::RIT_ARM_HalfDifference:
263 return Error("Relocation type not implemented yet!");
264 }
265 return false;
266}
267
Jim Grosbach6e563312011-03-21 22:15:52 +0000268bool RuntimeDyldImpl::
269loadSegment32(const MachOObject *Obj,
270 const MachOObject::LoadCommandInfo *SegmentLCI,
271 const InMemoryStruct<macho::SymtabLoadCommand> &SymtabLC) {
272 InMemoryStruct<macho::SegmentLoadCommand> Segment32LC;
273 Obj->ReadSegmentLoadCommand(*SegmentLCI, Segment32LC);
274 if (!Segment32LC)
275 return Error("unable to load segment load command");
276
277 // Map the segment into memory.
278 std::string ErrorStr;
279 Data = sys::Memory::AllocateRWX(Segment32LC->VMSize, 0, &ErrorStr);
280 if (!Data.base())
281 return Error("unable to allocate memory block: '" + ErrorStr + "'");
282 memcpy(Data.base(), Obj->getData(Segment32LC->FileOffset,
283 Segment32LC->FileSize).data(),
284 Segment32LC->FileSize);
285 memset((char*)Data.base() + Segment32LC->FileSize, 0,
286 Segment32LC->VMSize - Segment32LC->FileSize);
287
Jim Grosbach5ffe37f2011-03-23 23:35:17 +0000288 // Bind the section indices to addresses and record the relocations we
289 // need to resolve.
290 typedef std::pair<uint32_t, macho::RelocationEntry> RelocationMap;
291 SmallVector<RelocationMap, 64> Relocations;
292
Jim Grosbach8b54dca2011-03-23 19:52:00 +0000293 SmallVector<void *, 16> SectionBases;
Jim Grosbach6e563312011-03-21 22:15:52 +0000294 for (unsigned i = 0; i != Segment32LC->NumSections; ++i) {
295 InMemoryStruct<macho::Section> Sect;
296 Obj->ReadSection(*SegmentLCI, i, Sect);
Jim Grosbachc41ab782011-04-06 01:11:05 +0000297 if (!Sect)
Jim Grosbach6e563312011-03-21 22:15:52 +0000298 return Error("unable to load section: '" + Twine(i) + "'");
299
Jim Grosbach5ffe37f2011-03-23 23:35:17 +0000300 // Remember any relocations the section has so we can resolve them later.
301 for (unsigned j = 0; j != Sect->NumRelocationTableEntries; ++j) {
302 InMemoryStruct<macho::RelocationEntry> RE;
303 Obj->ReadRelocationEntry(Sect->RelocationTableOffset, j, RE);
304 Relocations.push_back(RelocationMap(j, *RE));
305 }
Jim Grosbach6e563312011-03-21 22:15:52 +0000306
307 // FIXME: Improve check.
Jim Grosbach5ffe37f2011-03-23 23:35:17 +0000308// if (Sect->Flags != 0x80000400)
309// return Error("unsupported section type!");
Jim Grosbach6e563312011-03-21 22:15:52 +0000310
Jim Grosbach8b54dca2011-03-23 19:52:00 +0000311 SectionBases.push_back((char*) Data.base() + Sect->Address);
Jim Grosbach6e563312011-03-21 22:15:52 +0000312 }
313
Jim Grosbach5ffe37f2011-03-23 23:35:17 +0000314 // Bind all the symbols to address. Keep a record of the names for use
315 // by relocation resolution.
316 SmallVector<StringRef, 64> SymbolNames;
Jim Grosbach6e563312011-03-21 22:15:52 +0000317 for (unsigned i = 0; i != SymtabLC->NumSymbolTableEntries; ++i) {
318 InMemoryStruct<macho::SymbolTableEntry> STE;
319 Obj->ReadSymbolTableEntry(SymtabLC->SymbolTableOffset, i, STE);
320 if (!STE)
321 return Error("unable to read symbol: '" + Twine(i) + "'");
Jim Grosbach5ffe37f2011-03-23 23:35:17 +0000322 // Get the symbol name.
323 StringRef Name = Obj->getStringAtIndex(STE->StringIndex);
324 SymbolNames.push_back(Name);
325
326 // Just skip undefined symbols. They'll be loaded from whatever
327 // module they come from (or system dylib) when we resolve relocations
328 // involving them.
Jim Grosbach6e563312011-03-21 22:15:52 +0000329 if (STE->SectionIndex == 0)
Jim Grosbach5ffe37f2011-03-23 23:35:17 +0000330 continue;
Jim Grosbach6e563312011-03-21 22:15:52 +0000331
332 unsigned Index = STE->SectionIndex - 1;
333 if (Index >= Segment32LC->NumSections)
334 return Error("invalid section index for symbol: '" + Twine() + "'");
335
Jim Grosbach6e563312011-03-21 22:15:52 +0000336 // Get the section base address.
337 void *SectionBase = SectionBases[Index];
338
339 // Get the symbol address.
Jim Grosbachfcbe5b72011-04-04 23:04:39 +0000340 uint64_t Address = (uint64_t)SectionBase + STE->Value;
Jim Grosbach6e563312011-03-21 22:15:52 +0000341
342 // FIXME: Check the symbol type and flags.
343 if (STE->Type != 0xF)
344 return Error("unexpected symbol type!");
345 if (STE->Flags != 0x0)
346 return Error("unexpected symbol type!");
347
Jim Grosbach8b54dca2011-03-23 19:52:00 +0000348 DEBUG(dbgs() << "Symbol: '" << Name << "' @ " << Address << "\n");
349
Jim Grosbach6e563312011-03-21 22:15:52 +0000350 SymbolTable[Name] = Address;
351 }
352
Jim Grosbach5ffe37f2011-03-23 23:35:17 +0000353 // Now resolve any relocations.
354 for (unsigned i = 0, e = Relocations.size(); i != e; ++i) {
355 if (resolveRelocation(Relocations[i].first, Relocations[i].second,
356 SectionBases, SymbolNames))
357 return true;
358 }
359
Jim Grosbachf9229102011-03-22 01:06:42 +0000360 // We've loaded the section; now mark the functions in it as executable.
Jim Grosbachfcbe5b72011-04-04 23:04:39 +0000361 // FIXME: We really should use the MemoryManager for this.
Jim Grosbachf9229102011-03-22 01:06:42 +0000362 sys::Memory::setRangeExecutable(Data.base(), Data.size());
363
Jim Grosbach6e563312011-03-21 22:15:52 +0000364 return false;
365}
366
367
368bool RuntimeDyldImpl::
369loadSegment64(const MachOObject *Obj,
370 const MachOObject::LoadCommandInfo *SegmentLCI,
371 const InMemoryStruct<macho::SymtabLoadCommand> &SymtabLC) {
372 InMemoryStruct<macho::Segment64LoadCommand> Segment64LC;
373 Obj->ReadSegment64LoadCommand(*SegmentLCI, Segment64LC);
374 if (!Segment64LC)
375 return Error("unable to load segment load command");
376
Jim Grosbachc41ab782011-04-06 01:11:05 +0000377 for (unsigned SectNum = 0; SectNum != Segment64LC->NumSections; ++SectNum) {
Jim Grosbach6e563312011-03-21 22:15:52 +0000378 InMemoryStruct<macho::Section64> Sect;
Jim Grosbachc41ab782011-04-06 01:11:05 +0000379 Obj->ReadSection64(*SegmentLCI, SectNum, Sect);
Jim Grosbach6e563312011-03-21 22:15:52 +0000380 if (!Sect)
Jim Grosbachc41ab782011-04-06 01:11:05 +0000381 return Error("unable to load section: '" + Twine(SectNum) + "'");
Jim Grosbach6e563312011-03-21 22:15:52 +0000382
383 // FIXME: Improve check.
384 if (Sect->Flags != 0x80000400)
385 return Error("unsupported section type!");
386
Jim Grosbachc41ab782011-04-06 01:11:05 +0000387 // Address and names of symbols in the section.
388 typedef std::pair<uint64_t, StringRef> SymbolEntry;
389 SmallVector<SymbolEntry, 64> Symbols;
390 for (unsigned i = 0; i != SymtabLC->NumSymbolTableEntries; ++i) {
391 InMemoryStruct<macho::Symbol64TableEntry> STE;
392 Obj->ReadSymbol64TableEntry(SymtabLC->SymbolTableOffset, i, STE);
393 if (!STE)
394 return Error("unable to read symbol: '" + Twine(i) + "'");
395 if (STE->SectionIndex > Segment64LC->NumSections)
396 return Error("invalid section index for symbol: '" + Twine() + "'");
397
398 // Just skip symbols not defined in this section.
399 if (STE->SectionIndex - 1 != SectNum)
400 continue;
401
402 // Get the symbol name.
403 StringRef Name = Obj->getStringAtIndex(STE->StringIndex);
404
405 // FIXME: Check the symbol type and flags.
406 if (STE->Type != 0xF) // external, defined in this section.
407 return Error("unexpected symbol type!");
408 if (STE->Flags != 0x0)
409 return Error("unexpected symbol type!");
410
411 uint64_t BaseAddress = Sect->Address;
412 uint64_t Address = BaseAddress + STE->Value;
413
414 // Remember the symbol.
415 Symbols.push_back(SymbolEntry(Address, Name));
416
417 DEBUG(dbgs() << "Function sym: '" << Name << "' @ " << Address << "\n");
418 }
419 // Sort the symbols by address, just in case they didn't come in that
420 // way.
421 array_pod_sort(Symbols.begin(), Symbols.end());
422
423 // Extract the function data.
424 uint8_t *Base = (uint8_t*)Obj->getData(Segment64LC->FileOffset,
425 Segment64LC->FileSize).data();
426 for (unsigned i = 0, e = Symbols.size() - 1; i != e; ++i) {
427 uint64_t StartOffset = Symbols[i].first;
428 uint64_t EndOffset = Symbols[i + 1].first - 1;
429 DEBUG(dbgs() << "Extracting function: " << Symbols[i].second
430 << " from [" << StartOffset << ", " << EndOffset << "]\n");
431 extractFunction(Symbols[i].second, Base + StartOffset, Base + EndOffset);
432 }
433 // The last symbol we do after since the end address is calculated
434 // differently because there is no next symbol to reference.
435 uint64_t StartOffset = Symbols[Symbols.size() - 1].first;
436 uint64_t EndOffset = Sect->Size - 1;
437 DEBUG(dbgs() << "Extracting function: " << Symbols[Symbols.size()-1].second
438 << " from [" << StartOffset << ", " << EndOffset << "]\n");
439 extractFunction(Symbols[Symbols.size()-1].second,
440 Base + StartOffset, Base + EndOffset);
Jim Grosbach6e563312011-03-21 22:15:52 +0000441 }
442
Jim Grosbach6e563312011-03-21 22:15:52 +0000443 return false;
444}
445
Jim Grosbach6e563312011-03-21 22:15:52 +0000446bool RuntimeDyldImpl::loadObject(MemoryBuffer *InputBuffer) {
447 // If the linker is in an error state, don't do anything.
448 if (hasError())
449 return true;
450 // Load the Mach-O wrapper object.
451 std::string ErrorStr;
452 OwningPtr<MachOObject> Obj(
453 MachOObject::LoadFromBuffer(InputBuffer, &ErrorStr));
454 if (!Obj)
455 return Error("unable to load object: '" + ErrorStr + "'");
456
Jim Grosbacha8287e32011-03-23 22:06:06 +0000457 // Get the CPU type information from the header.
458 const macho::Header &Header = Obj->getHeader();
459
460 // FIXME: Error checking that the loaded object is compatible with
461 // the system we're running on.
462 CPUType = Header.CPUType;
463 CPUSubtype = Header.CPUSubtype;
464
Jim Grosbach6e563312011-03-21 22:15:52 +0000465 // Validate that the load commands match what we expect.
466 const MachOObject::LoadCommandInfo *SegmentLCI = 0, *SymtabLCI = 0,
467 *DysymtabLCI = 0;
Jim Grosbacha8287e32011-03-23 22:06:06 +0000468 for (unsigned i = 0; i != Header.NumLoadCommands; ++i) {
Jim Grosbach6e563312011-03-21 22:15:52 +0000469 const MachOObject::LoadCommandInfo &LCI = Obj->getLoadCommandInfo(i);
470 switch (LCI.Command.Type) {
471 case macho::LCT_Segment:
472 case macho::LCT_Segment64:
473 if (SegmentLCI)
474 return Error("unexpected input object (multiple segments)");
475 SegmentLCI = &LCI;
476 break;
477 case macho::LCT_Symtab:
478 if (SymtabLCI)
479 return Error("unexpected input object (multiple symbol tables)");
480 SymtabLCI = &LCI;
481 break;
482 case macho::LCT_Dysymtab:
483 if (DysymtabLCI)
484 return Error("unexpected input object (multiple symbol tables)");
485 DysymtabLCI = &LCI;
486 break;
487 default:
488 return Error("unexpected input object (unexpected load command");
489 }
490 }
491
492 if (!SymtabLCI)
493 return Error("no symbol table found in object");
494 if (!SegmentLCI)
495 return Error("no symbol table found in object");
496
497 // Read and register the symbol table data.
498 InMemoryStruct<macho::SymtabLoadCommand> SymtabLC;
499 Obj->ReadSymtabLoadCommand(*SymtabLCI, SymtabLC);
500 if (!SymtabLC)
501 return Error("unable to load symbol table load command");
502 Obj->RegisterStringTable(*SymtabLC);
503
504 // Read the dynamic link-edit information, if present (not present in static
505 // objects).
506 if (DysymtabLCI) {
507 InMemoryStruct<macho::DysymtabLoadCommand> DysymtabLC;
508 Obj->ReadDysymtabLoadCommand(*DysymtabLCI, DysymtabLC);
509 if (!DysymtabLC)
510 return Error("unable to load dynamic link-exit load command");
511
512 // FIXME: We don't support anything interesting yet.
Jim Grosbach8b54dca2011-03-23 19:52:00 +0000513// if (DysymtabLC->LocalSymbolsIndex != 0)
514// return Error("NOT YET IMPLEMENTED: local symbol entries");
515// if (DysymtabLC->ExternalSymbolsIndex != 0)
516// return Error("NOT YET IMPLEMENTED: non-external symbol entries");
517// if (DysymtabLC->UndefinedSymbolsIndex != SymtabLC->NumSymbolTableEntries)
518// return Error("NOT YET IMPLEMENTED: undefined symbol entries");
Jim Grosbach6e563312011-03-21 22:15:52 +0000519 }
520
521 // Load the segment load command.
522 if (SegmentLCI->Command.Type == macho::LCT_Segment) {
523 if (loadSegment32(Obj.get(), SegmentLCI, SymtabLC))
524 return true;
525 } else {
526 if (loadSegment64(Obj.get(), SegmentLCI, SymtabLC))
527 return true;
528 }
529
530 return false;
531}
532
533
534//===----------------------------------------------------------------------===//
535// RuntimeDyld class implementation
Jim Grosbachfcbe5b72011-04-04 23:04:39 +0000536RuntimeDyld::RuntimeDyld(RTDyldMemoryManager *MM) {
537 Dyld = new RuntimeDyldImpl(MM);
Jim Grosbach6e563312011-03-21 22:15:52 +0000538}
539
540RuntimeDyld::~RuntimeDyld() {
541 delete Dyld;
542}
543
544bool RuntimeDyld::loadObject(MemoryBuffer *InputBuffer) {
545 return Dyld->loadObject(InputBuffer);
546}
547
Jim Grosbachfcbe5b72011-04-04 23:04:39 +0000548uint64_t RuntimeDyld::getSymbolAddress(StringRef Name) {
Jim Grosbach6e563312011-03-21 22:15:52 +0000549 return Dyld->getSymbolAddress(Name);
550}
551
552sys::MemoryBlock RuntimeDyld::getMemoryBlock() {
553 return Dyld->getMemoryBlock();
554}
555
Jim Grosbach91dde152011-03-22 18:22:27 +0000556StringRef RuntimeDyld::getErrorString() {
Jim Grosbachb3eecaf2011-03-22 18:19:42 +0000557 return Dyld->getErrorString();
558}
559
Jim Grosbach6e563312011-03-21 22:15:52 +0000560} // end namespace llvm