blob: 81b2d17f885c97946c753d357d33c346f7fe840a [file] [log] [blame]
Danil Malyshevcf852dc2011-07-13 07:57:58 +00001//===-- RuntimeDyldMachO.cpp - 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
14#define DEBUG_TYPE "dyld"
15#include "llvm/ADT/OwningPtr.h"
16#include "llvm/ADT/StringRef.h"
17#include "llvm/ADT/STLExtras.h"
18#include "RuntimeDyldImpl.h"
19using namespace llvm;
20using namespace llvm::object;
21
22namespace llvm {
23
24bool RuntimeDyldMachO::
Jim Grosbach61425c02012-01-16 22:26:39 +000025resolveRelocation(uint8_t *Address, uint64_t Value, bool isPCRel,
26 unsigned Type, unsigned Size, int64_t Addend) {
Danil Malyshevcf852dc2011-07-13 07:57:58 +000027 // This just dispatches to the proper target specific routine.
28 switch (CPUType) {
29 default: assert(0 && "Unsupported CPU type!");
30 case mach::CTM_x86_64:
31 return resolveX86_64Relocation((uintptr_t)Address, (uintptr_t)Value,
Jim Grosbach61425c02012-01-16 22:26:39 +000032 isPCRel, Type, Size, Addend);
Danil Malyshevcf852dc2011-07-13 07:57:58 +000033 case mach::CTM_ARM:
34 return resolveARMRelocation((uintptr_t)Address, (uintptr_t)Value,
Jim Grosbach61425c02012-01-16 22:26:39 +000035 isPCRel, Type, Size, Addend);
Danil Malyshevcf852dc2011-07-13 07:57:58 +000036 }
37 llvm_unreachable("");
38}
39
40bool RuntimeDyldMachO::
Jim Grosbach61425c02012-01-16 22:26:39 +000041resolveX86_64Relocation(uintptr_t Address, uintptr_t Value, bool isPCRel,
42 unsigned Type, unsigned Size, int64_t Addend) {
Danil Malyshevcf852dc2011-07-13 07:57:58 +000043 // If the relocation is PC-relative, the value to be encoded is the
44 // pointer difference.
45 if (isPCRel)
46 // FIXME: It seems this value needs to be adjusted by 4 for an effective PC
47 // address. Is that expected? Only for branches, perhaps?
48 Value -= Address + 4;
49
50 switch(Type) {
51 default:
52 llvm_unreachable("Invalid relocation type!");
53 case macho::RIT_X86_64_Unsigned:
54 case macho::RIT_X86_64_Branch: {
55 // Mask in the target value a byte at a time (we don't have an alignment
56 // guarantee for the target address, so this is safest).
57 uint8_t *p = (uint8_t*)Address;
58 for (unsigned i = 0; i < Size; ++i) {
59 *p++ = (uint8_t)Value;
60 Value >>= 8;
61 }
62 return false;
63 }
64 case macho::RIT_X86_64_Signed:
65 case macho::RIT_X86_64_GOTLoad:
66 case macho::RIT_X86_64_GOT:
67 case macho::RIT_X86_64_Subtractor:
68 case macho::RIT_X86_64_Signed1:
69 case macho::RIT_X86_64_Signed2:
70 case macho::RIT_X86_64_Signed4:
71 case macho::RIT_X86_64_TLV:
72 return Error("Relocation type not implemented yet!");
73 }
74 return false;
75}
76
Jim Grosbach61425c02012-01-16 22:26:39 +000077bool RuntimeDyldMachO::
78resolveARMRelocation(uintptr_t Address, uintptr_t Value, bool isPCRel,
79 unsigned Type, unsigned Size, int64_t Addend) {
Danil Malyshevcf852dc2011-07-13 07:57:58 +000080 // If the relocation is PC-relative, the value to be encoded is the
81 // pointer difference.
82 if (isPCRel) {
83 Value -= Address;
84 // ARM PCRel relocations have an effective-PC offset of two instructions
85 // (four bytes in Thumb mode, 8 bytes in ARM mode).
86 // FIXME: For now, assume ARM mode.
87 Value -= 8;
88 }
89
90 switch(Type) {
91 default:
92 llvm_unreachable("Invalid relocation type!");
93 case macho::RIT_Vanilla: {
94 llvm_unreachable("Invalid relocation type!");
95 // Mask in the target value a byte at a time (we don't have an alignment
96 // guarantee for the target address, so this is safest).
97 uint8_t *p = (uint8_t*)Address;
98 for (unsigned i = 0; i < Size; ++i) {
99 *p++ = (uint8_t)Value;
100 Value >>= 8;
101 }
102 break;
103 }
104 case macho::RIT_ARM_Branch24Bit: {
105 // Mask the value into the target address. We know instructions are
106 // 32-bit aligned, so we can do it all at once.
107 uint32_t *p = (uint32_t*)Address;
108 // The low two bits of the value are not encoded.
109 Value >>= 2;
110 // Mask the value to 24 bits.
111 Value &= 0xffffff;
112 // FIXME: If the destination is a Thumb function (and the instruction
113 // is a non-predicated BL instruction), we need to change it to a BLX
114 // instruction instead.
115
116 // Insert the value into the instruction.
117 *p = (*p & ~0xffffff) | Value;
118 break;
119 }
120 case macho::RIT_ARM_ThumbBranch22Bit:
121 case macho::RIT_ARM_ThumbBranch32Bit:
122 case macho::RIT_ARM_Half:
123 case macho::RIT_ARM_HalfDifference:
124 case macho::RIT_Pair:
125 case macho::RIT_Difference:
126 case macho::RIT_ARM_LocalDifference:
127 case macho::RIT_ARM_PreboundLazyPointer:
128 return Error("Relocation type not implemented yet!");
129 }
130 return false;
131}
132
133bool RuntimeDyldMachO::
134loadSegment32(const MachOObject *Obj,
135 const MachOObject::LoadCommandInfo *SegmentLCI,
136 const InMemoryStruct<macho::SymtabLoadCommand> &SymtabLC) {
Jim Grosbach61425c02012-01-16 22:26:39 +0000137 // FIXME: This should really be combined w/ loadSegment64. Templatized
138 // function on the 32/64 datatypes maybe?
Danil Malyshevcf852dc2011-07-13 07:57:58 +0000139 InMemoryStruct<macho::SegmentLoadCommand> SegmentLC;
140 Obj->ReadSegmentLoadCommand(*SegmentLCI, SegmentLC);
141 if (!SegmentLC)
142 return Error("unable to load segment load command");
143
Jim Grosbach61425c02012-01-16 22:26:39 +0000144
145 SmallVector<unsigned, 16> SectionMap;
Danil Malyshevcf852dc2011-07-13 07:57:58 +0000146 for (unsigned SectNum = 0; SectNum != SegmentLC->NumSections; ++SectNum) {
147 InMemoryStruct<macho::Section> Sect;
148 Obj->ReadSection(*SegmentLCI, SectNum, Sect);
149 if (!Sect)
150 return Error("unable to load section: '" + Twine(SectNum) + "'");
151
Jim Grosbach61425c02012-01-16 22:26:39 +0000152 // Allocate memory via the MM for the section.
153 uint8_t *Buffer;
154 uint32_t SectionID = Sections.size();
Danil Malyshevcf852dc2011-07-13 07:57:58 +0000155 if (Sect->Flags != 0x80000400)
Jim Grosbach61425c02012-01-16 22:26:39 +0000156 Buffer = MemMgr->allocateCodeSection(Sect->Size, Sect->Align, SectionID);
157 else
158 Buffer = MemMgr->allocateDataSection(Sect->Size, Sect->Align, SectionID);
Danil Malyshevcf852dc2011-07-13 07:57:58 +0000159
Jim Grosbach61425c02012-01-16 22:26:39 +0000160 DEBUG(dbgs() << "Loading "
161 << ((Sect->Flags == 0x80000400) ? "text" : "data")
162 << " (ID #" << SectionID << ")"
163 << " '" << Sect->SegmentName << ","
164 << Sect->Name << "' of size " << Sect->Size
165 << " to address " << Buffer << ".\n");
Danil Malyshevcf852dc2011-07-13 07:57:58 +0000166
Jim Grosbach61425c02012-01-16 22:26:39 +0000167 // Copy the payload from the object file into the allocated buffer.
Danil Malyshevcf852dc2011-07-13 07:57:58 +0000168 uint8_t *Base = (uint8_t*)Obj->getData(SegmentLC->FileOffset,
169 SegmentLC->FileSize).data();
Jim Grosbach61425c02012-01-16 22:26:39 +0000170 memcpy(Buffer, Base + Sect->Address, Sect->Size);
Danil Malyshevcf852dc2011-07-13 07:57:58 +0000171
Jim Grosbach61425c02012-01-16 22:26:39 +0000172 // Remember what got allocated for this SectionID.
173 Sections.push_back(sys::MemoryBlock(Buffer, Sect->Size));
174
175 // By default, the load address of a section is its memory buffer.
176 SectionLoadAddress.push_back((uint64_t)Buffer);
177
178 // Keep a map of object file section numbers to corresponding SectionIDs
179 // while processing the file.
180 SectionMap.push_back(SectionID);
181 }
182
183 // Process the symbol table.
184 SmallVector<StringRef, 64> SymbolNames;
185 processSymbols32(Obj, SectionMap, SymbolNames, SymtabLC);
186
187 // Process the relocations for each section we're loading.
188 Relocations.grow(Relocations.size() + SegmentLC->NumSections);
189 for (unsigned SectNum = 0; SectNum != SegmentLC->NumSections; ++SectNum) {
190 InMemoryStruct<macho::Section> Sect;
191 Obj->ReadSection(*SegmentLCI, SectNum, Sect);
192 if (!Sect)
193 return Error("unable to load section: '" + Twine(SectNum) + "'");
Danil Malyshevcf852dc2011-07-13 07:57:58 +0000194 for (unsigned j = 0; j != Sect->NumRelocationTableEntries; ++j) {
195 InMemoryStruct<macho::RelocationEntry> RE;
196 Obj->ReadRelocationEntry(Sect->RelocationTableOffset, j, RE);
197 if (RE->Word0 & macho::RF_Scattered)
198 return Error("NOT YET IMPLEMENTED: scattered relocations.");
199 // Word0 of the relocation is the offset into the section where the
200 // relocation should be applied. We need to translate that into an
201 // offset into a function since that's our atom.
202 uint32_t Offset = RE->Word0;
Danil Malyshevcf852dc2011-07-13 07:57:58 +0000203 bool isExtern = (RE->Word1 >> 27) & 1;
Jim Grosbach61425c02012-01-16 22:26:39 +0000204
205 // FIXME: Get the relocation addend from the target address.
206 // FIXME: VERY imporant for internal relocations.
207
Danil Malyshevcf852dc2011-07-13 07:57:58 +0000208 // Figure out the source symbol of the relocation. If isExtern is true,
209 // this relocation references the symbol table, otherwise it references
210 // a section in the same object, numbered from 1 through NumSections
211 // (SectionBases is [0, NumSections-1]).
Danil Malyshevcf852dc2011-07-13 07:57:58 +0000212 uint32_t SourceNum = RE->Word1 & 0xffffff; // 24-bit value
Jim Grosbach61425c02012-01-16 22:26:39 +0000213 if (!isExtern) {
214 assert(SourceNum > 0 && "Invalid relocation section number!");
215 unsigned SectionID = SectionMap[SourceNum - 1];
216 unsigned TargetID = SectionMap[SectNum];
217 DEBUG(dbgs() << "Internal relocation at Section #"
218 << TargetID << " + " << Offset
219 << " from Section #"
220 << SectionID << " (Word1: "
221 << format("0x%x", RE->Word1) << ")\n");
Danil Malyshevcf852dc2011-07-13 07:57:58 +0000222
Jim Grosbach61425c02012-01-16 22:26:39 +0000223 // Store the relocation information. It will get resolved when
224 // the section addresses are assigned.
225 Relocations[SectionID].push_back(RelocationEntry(TargetID,
226 Offset,
227 RE->Word1,
228 0 /*Addend*/));
229 } else {
230 StringRef SourceName = SymbolNames[SourceNum];
Danil Malyshevcf852dc2011-07-13 07:57:58 +0000231
Jim Grosbach61425c02012-01-16 22:26:39 +0000232 // Now store the relocation information. Associate it with the source
233 // symbol. Just add it to the unresolved list and let the general
234 // path post-load resolve it if we know where the symbol is.
235 UnresolvedRelocations[SourceName].push_back(RelocationEntry(SectNum,
236 Offset,
237 RE->Word1,
238 0 /*Addend*/));
239 DEBUG(dbgs() << "Relocation at Section #" << SectNum << " + " << Offset
240 << " from '" << SourceName << "(Word1: "
241 << format("0x%x", RE->Word1) << ")\n");
242 }
Danil Malyshevcf852dc2011-07-13 07:57:58 +0000243 }
244 }
Jim Grosbach61425c02012-01-16 22:26:39 +0000245
246 // Resolve the addresses of any symbols that were defined in this segment.
247 for (int i = 0, e = SymbolNames.size(); i != e; ++i)
248 resolveSymbol(SymbolNames[i]);
249
Danil Malyshevcf852dc2011-07-13 07:57:58 +0000250 return false;
251}
252
253
254bool RuntimeDyldMachO::
255loadSegment64(const MachOObject *Obj,
256 const MachOObject::LoadCommandInfo *SegmentLCI,
257 const InMemoryStruct<macho::SymtabLoadCommand> &SymtabLC) {
258 InMemoryStruct<macho::Segment64LoadCommand> Segment64LC;
259 Obj->ReadSegment64LoadCommand(*SegmentLCI, Segment64LC);
260 if (!Segment64LC)
261 return Error("unable to load segment load command");
262
Jim Grosbach61425c02012-01-16 22:26:39 +0000263
264 SmallVector<unsigned, 16> SectionMap;
Danil Malyshevcf852dc2011-07-13 07:57:58 +0000265 for (unsigned SectNum = 0; SectNum != Segment64LC->NumSections; ++SectNum) {
266 InMemoryStruct<macho::Section64> Sect;
267 Obj->ReadSection64(*SegmentLCI, SectNum, Sect);
268 if (!Sect)
269 return Error("unable to load section: '" + Twine(SectNum) + "'");
270
Jim Grosbach61425c02012-01-16 22:26:39 +0000271 // Allocate memory via the MM for the section.
272 uint8_t *Buffer;
273 uint32_t SectionID = Sections.size();
Danil Malyshevcf852dc2011-07-13 07:57:58 +0000274 if (Sect->Flags != 0x80000400)
Jim Grosbach61425c02012-01-16 22:26:39 +0000275 Buffer = MemMgr->allocateCodeSection(Sect->Size, Sect->Align, SectionID);
276 else
277 Buffer = MemMgr->allocateDataSection(Sect->Size, Sect->Align, SectionID);
Danil Malyshevcf852dc2011-07-13 07:57:58 +0000278
Jim Grosbach61425c02012-01-16 22:26:39 +0000279 DEBUG(dbgs() << "Loading "
280 << ((Sect->Flags == 0x80000400) ? "text" : "data")
281 << " (ID #" << SectionID << ")"
282 << " '" << Sect->SegmentName << ","
283 << Sect->Name << "' of size " << Sect->Size
284 << " to address " << Buffer << ".\n");
Danil Malyshevcf852dc2011-07-13 07:57:58 +0000285
Jim Grosbach61425c02012-01-16 22:26:39 +0000286 // Copy the payload from the object file into the allocated buffer.
Danil Malyshevcf852dc2011-07-13 07:57:58 +0000287 uint8_t *Base = (uint8_t*)Obj->getData(Segment64LC->FileOffset,
288 Segment64LC->FileSize).data();
Jim Grosbach61425c02012-01-16 22:26:39 +0000289 memcpy(Buffer, Base + Sect->Address, Sect->Size);
Danil Malyshevcf852dc2011-07-13 07:57:58 +0000290
Jim Grosbach61425c02012-01-16 22:26:39 +0000291 // Remember what got allocated for this SectionID.
292 Sections.push_back(sys::MemoryBlock(Buffer, Sect->Size));
293
294 // By default, the load address of a section is its memory buffer.
295 SectionLoadAddress.push_back((uint64_t)Buffer);
296
297 // Keep a map of object file section numbers to corresponding SectionIDs
298 // while processing the file.
299 SectionMap.push_back(SectionID);
300 }
301
302 // Process the symbol table.
303 SmallVector<StringRef, 64> SymbolNames;
304 processSymbols64(Obj, SectionMap, SymbolNames, SymtabLC);
305
306 // Process the relocations for each section we're loading.
307 Relocations.grow(Relocations.size() + Segment64LC->NumSections);
308 for (unsigned SectNum = 0; SectNum != Segment64LC->NumSections; ++SectNum) {
309 InMemoryStruct<macho::Section64> Sect;
310 Obj->ReadSection64(*SegmentLCI, SectNum, Sect);
311 if (!Sect)
312 return Error("unable to load section: '" + Twine(SectNum) + "'");
Danil Malyshevcf852dc2011-07-13 07:57:58 +0000313 for (unsigned j = 0; j != Sect->NumRelocationTableEntries; ++j) {
314 InMemoryStruct<macho::RelocationEntry> RE;
315 Obj->ReadRelocationEntry(Sect->RelocationTableOffset, j, RE);
316 if (RE->Word0 & macho::RF_Scattered)
317 return Error("NOT YET IMPLEMENTED: scattered relocations.");
318 // Word0 of the relocation is the offset into the section where the
319 // relocation should be applied. We need to translate that into an
320 // offset into a function since that's our atom.
321 uint32_t Offset = RE->Word0;
Danil Malyshevcf852dc2011-07-13 07:57:58 +0000322 bool isExtern = (RE->Word1 >> 27) & 1;
Jim Grosbach61425c02012-01-16 22:26:39 +0000323
324 // FIXME: Get the relocation addend from the target address.
325 // FIXME: VERY imporant for internal relocations.
326
Danil Malyshevcf852dc2011-07-13 07:57:58 +0000327 // Figure out the source symbol of the relocation. If isExtern is true,
328 // this relocation references the symbol table, otherwise it references
329 // a section in the same object, numbered from 1 through NumSections
330 // (SectionBases is [0, NumSections-1]).
Danil Malyshevcf852dc2011-07-13 07:57:58 +0000331 uint32_t SourceNum = RE->Word1 & 0xffffff; // 24-bit value
Jim Grosbach61425c02012-01-16 22:26:39 +0000332 if (!isExtern) {
333 assert(SourceNum > 0 && "Invalid relocation section number!");
334 unsigned SectionID = SectionMap[SourceNum - 1];
335 unsigned TargetID = SectionMap[SectNum];
336 DEBUG(dbgs() << "Internal relocation at Section #"
337 << TargetID << " + " << Offset
338 << " from Section #"
339 << SectionID << " (Word1: "
340 << format("0x%x", RE->Word1) << ")\n");
Danil Malyshevcf852dc2011-07-13 07:57:58 +0000341
Jim Grosbach61425c02012-01-16 22:26:39 +0000342 // Store the relocation information. It will get resolved when
343 // the section addresses are assigned.
344 Relocations[SectionID].push_back(RelocationEntry(TargetID,
345 Offset,
346 RE->Word1,
347 0 /*Addend*/));
348 } else {
349 StringRef SourceName = SymbolNames[SourceNum];
Danil Malyshevcf852dc2011-07-13 07:57:58 +0000350
Jim Grosbach61425c02012-01-16 22:26:39 +0000351 // Now store the relocation information. Associate it with the source
352 // symbol. Just add it to the unresolved list and let the general
353 // path post-load resolve it if we know where the symbol is.
354 UnresolvedRelocations[SourceName].push_back(RelocationEntry(SectNum,
355 Offset,
356 RE->Word1,
357 0 /*Addend*/));
358 DEBUG(dbgs() << "Relocation at Section #" << SectNum << " + " << Offset
359 << " from '" << SourceName << "(Word1: "
360 << format("0x%x", RE->Word1) << ")\n");
361 }
Danil Malyshevcf852dc2011-07-13 07:57:58 +0000362 }
363 }
Jim Grosbach61425c02012-01-16 22:26:39 +0000364
365 // Resolve the addresses of any symbols that were defined in this segment.
366 for (int i = 0, e = SymbolNames.size(); i != e; ++i)
367 resolveSymbol(SymbolNames[i]);
368
Danil Malyshevcf852dc2011-07-13 07:57:58 +0000369 return false;
370}
371
Jim Grosbach61425c02012-01-16 22:26:39 +0000372bool RuntimeDyldMachO::
373processSymbols32(const MachOObject *Obj,
374 SmallVectorImpl<unsigned> &SectionMap,
375 SmallVectorImpl<StringRef> &SymbolNames,
376 const InMemoryStruct<macho::SymtabLoadCommand> &SymtabLC) {
377 // FIXME: Combine w/ processSymbols64. Factor 64/32 datatype and such.
378 for (unsigned i = 0; i != SymtabLC->NumSymbolTableEntries; ++i) {
379 InMemoryStruct<macho::SymbolTableEntry> STE;
380 Obj->ReadSymbolTableEntry(SymtabLC->SymbolTableOffset, i, STE);
381 if (!STE)
382 return Error("unable to read symbol: '" + Twine(i) + "'");
383 // Get the symbol name.
384 StringRef Name = Obj->getStringAtIndex(STE->StringIndex);
385 SymbolNames.push_back(Name);
386
387 // FIXME: Check the symbol type and flags.
388 if (STE->Type != 0xF) // external, defined in this segment.
389 continue;
390 // Flags in the upper nibble we don't care about.
391 if ((STE->Flags & 0xf) != 0x0)
392 continue;
393
394 // Remember the symbol.
395 uint32_t SectionID = SectionMap[STE->SectionIndex - 1];
396 SymbolTable[Name] = SymbolLoc(SectionID, STE->Value);
397
398 DEBUG(dbgs() << "Symbol: '" << Name << "' @ "
399 << (getSectionAddress(SectionID) + STE->Value)
400 << "\n");
401 }
402 return false;
403}
404
405bool RuntimeDyldMachO::
406processSymbols64(const MachOObject *Obj,
407 SmallVectorImpl<unsigned> &SectionMap,
408 SmallVectorImpl<StringRef> &SymbolNames,
409 const InMemoryStruct<macho::SymtabLoadCommand> &SymtabLC) {
410 for (unsigned i = 0; i != SymtabLC->NumSymbolTableEntries; ++i) {
411 InMemoryStruct<macho::Symbol64TableEntry> STE;
412 Obj->ReadSymbol64TableEntry(SymtabLC->SymbolTableOffset, i, STE);
413 if (!STE)
414 return Error("unable to read symbol: '" + Twine(i) + "'");
415 // Get the symbol name.
416 StringRef Name = Obj->getStringAtIndex(STE->StringIndex);
417 SymbolNames.push_back(Name);
418
419 // FIXME: Check the symbol type and flags.
420 if (STE->Type != 0xF) // external, defined in this segment.
421 continue;
422 // Flags in the upper nibble we don't care about.
423 if ((STE->Flags & 0xf) != 0x0)
424 continue;
425
426 // Remember the symbol.
427 uint32_t SectionID = SectionMap[STE->SectionIndex - 1];
428 SymbolTable[Name] = SymbolLoc(SectionID, STE->Value);
429
430 DEBUG(dbgs() << "Symbol: '" << Name << "' @ "
431 << (getSectionAddress(SectionID) + STE->Value)
432 << "\n");
433 }
434 return false;
435}
436
437// resolveSymbol - Resolve any relocations to the specified symbol if
438// we know where it lives.
439void RuntimeDyldMachO::resolveSymbol(StringRef Name) {
440 StringMap<SymbolLoc>::const_iterator Loc = SymbolTable.find(Name);
441 if (Loc == SymbolTable.end())
442 return;
443
444 RelocationList &Relocs = UnresolvedRelocations[Name];
445 DEBUG(dbgs() << "Resolving symbol '" << Name << "'\n");
446 for (int i = 0, e = Relocs.size(); i != e; ++i) {
447 // Change the relocation to be section relative rather than symbol
448 // relative and move it to the resolved relocation list.
449 RelocationEntry Entry = Relocs[i];
450 Entry.Addend += Loc->second.second;
451 Relocations[Loc->second.first].push_back(Entry);
452 }
453 // FIXME: Keep a worklist of the relocations we've added so that we can
454 // resolve more selectively later.
455 Relocs.clear();
456}
457
Danil Malyshevcf852dc2011-07-13 07:57:58 +0000458bool RuntimeDyldMachO::loadObject(MemoryBuffer *InputBuffer) {
459 // If the linker is in an error state, don't do anything.
460 if (hasError())
461 return true;
462 // Load the Mach-O wrapper object.
463 std::string ErrorStr;
464 OwningPtr<MachOObject> Obj(
465 MachOObject::LoadFromBuffer(InputBuffer, &ErrorStr));
466 if (!Obj)
467 return Error("unable to load object: '" + ErrorStr + "'");
468
469 // Get the CPU type information from the header.
470 const macho::Header &Header = Obj->getHeader();
471
472 // FIXME: Error checking that the loaded object is compatible with
473 // the system we're running on.
474 CPUType = Header.CPUType;
475 CPUSubtype = Header.CPUSubtype;
476
477 // Validate that the load commands match what we expect.
478 const MachOObject::LoadCommandInfo *SegmentLCI = 0, *SymtabLCI = 0,
479 *DysymtabLCI = 0;
480 for (unsigned i = 0; i != Header.NumLoadCommands; ++i) {
481 const MachOObject::LoadCommandInfo &LCI = Obj->getLoadCommandInfo(i);
482 switch (LCI.Command.Type) {
483 case macho::LCT_Segment:
484 case macho::LCT_Segment64:
485 if (SegmentLCI)
486 return Error("unexpected input object (multiple segments)");
487 SegmentLCI = &LCI;
488 break;
489 case macho::LCT_Symtab:
490 if (SymtabLCI)
491 return Error("unexpected input object (multiple symbol tables)");
492 SymtabLCI = &LCI;
493 break;
494 case macho::LCT_Dysymtab:
495 if (DysymtabLCI)
496 return Error("unexpected input object (multiple symbol tables)");
497 DysymtabLCI = &LCI;
498 break;
499 default:
500 return Error("unexpected input object (unexpected load command");
501 }
502 }
503
504 if (!SymtabLCI)
505 return Error("no symbol table found in object");
506 if (!SegmentLCI)
Eli Bendersky1eb189b2012-01-06 07:49:17 +0000507 return Error("no segments found in object");
Danil Malyshevcf852dc2011-07-13 07:57:58 +0000508
509 // Read and register the symbol table data.
510 InMemoryStruct<macho::SymtabLoadCommand> SymtabLC;
511 Obj->ReadSymtabLoadCommand(*SymtabLCI, SymtabLC);
512 if (!SymtabLC)
513 return Error("unable to load symbol table load command");
514 Obj->RegisterStringTable(*SymtabLC);
515
516 // Read the dynamic link-edit information, if present (not present in static
517 // objects).
518 if (DysymtabLCI) {
519 InMemoryStruct<macho::DysymtabLoadCommand> DysymtabLC;
520 Obj->ReadDysymtabLoadCommand(*DysymtabLCI, DysymtabLC);
521 if (!DysymtabLC)
522 return Error("unable to load dynamic link-exit load command");
523
524 // FIXME: We don't support anything interesting yet.
525// if (DysymtabLC->LocalSymbolsIndex != 0)
526// return Error("NOT YET IMPLEMENTED: local symbol entries");
527// if (DysymtabLC->ExternalSymbolsIndex != 0)
528// return Error("NOT YET IMPLEMENTED: non-external symbol entries");
529// if (DysymtabLC->UndefinedSymbolsIndex != SymtabLC->NumSymbolTableEntries)
530// return Error("NOT YET IMPLEMENTED: undefined symbol entries");
531 }
532
533 // Load the segment load command.
534 if (SegmentLCI->Command.Type == macho::LCT_Segment) {
535 if (loadSegment32(Obj.get(), SegmentLCI, SymtabLC))
536 return true;
537 } else {
538 if (loadSegment64(Obj.get(), SegmentLCI, SymtabLC))
539 return true;
540 }
541
Jim Grosbach61425c02012-01-16 22:26:39 +0000542 // Assign the addresses of the sections from the object so that any
543 // relocations to them get set properly.
544 // FIXME: This is done directly from the client at the moment. We should
545 // default the values to the local storage, at least when the target arch
546 // is the same as the host arch.
547
Danil Malyshevcf852dc2011-07-13 07:57:58 +0000548 return false;
549}
550
551// Assign an address to a symbol name and resolve all the relocations
552// associated with it.
Jim Grosbach61425c02012-01-16 22:26:39 +0000553void RuntimeDyldMachO::reassignSectionAddress(unsigned SectionID,
554 uint64_t Addr) {
555 // The address to use for relocation resolution is not
556 // the address of the local section buffer. We must be doing
557 // a remote execution environment of some sort. Re-apply any
558 // relocations referencing this section with the given address.
559 //
560 // Addr is a uint64_t because we can't assume the pointer width
561 // of the target is the same as that of the host. Just use a generic
562 // "big enough" type.
Danil Malyshevcf852dc2011-07-13 07:57:58 +0000563
Jim Grosbach61425c02012-01-16 22:26:39 +0000564 SectionLoadAddress[SectionID] = Addr;
565
566 RelocationList &Relocs = Relocations[SectionID];
Danil Malyshevcf852dc2011-07-13 07:57:58 +0000567 for (unsigned i = 0, e = Relocs.size(); i != e; ++i) {
568 RelocationEntry &RE = Relocs[i];
Jim Grosbach61425c02012-01-16 22:26:39 +0000569 uint8_t *Target = (uint8_t*)Sections[RE.SectionID].base() + RE.Offset;
Danil Malyshevcf852dc2011-07-13 07:57:58 +0000570 bool isPCRel = (RE.Data >> 24) & 1;
571 unsigned Type = (RE.Data >> 28) & 0xf;
572 unsigned Size = 1 << ((RE.Data >> 25) & 3);
573
Jim Grosbach61425c02012-01-16 22:26:39 +0000574 DEBUG(dbgs() << "Resolving relocation at Section #" << RE.SectionID
575 << " + " << RE.Offset << " (" << format("%p", Target) << ")"
576 << " from Section #" << SectionID << " (" << format("%p", Addr) << ")"
Danil Malyshevcf852dc2011-07-13 07:57:58 +0000577 << "(" << (isPCRel ? "pcrel" : "absolute")
Jim Grosbach61425c02012-01-16 22:26:39 +0000578 << ", type: " << Type << ", Size: " << Size << ", Addend: "
579 << RE.Addend << ").\n");
Danil Malyshevcf852dc2011-07-13 07:57:58 +0000580
Jim Grosbach61425c02012-01-16 22:26:39 +0000581 resolveRelocation(Target, Addr, isPCRel, Type, Size, RE.Addend);
Danil Malyshevcf852dc2011-07-13 07:57:58 +0000582 }
583}
584
585bool RuntimeDyldMachO::isKnownFormat(const MemoryBuffer *InputBuffer) {
586 StringRef Magic = InputBuffer->getBuffer().slice(0, 4);
587 if (Magic == "\xFE\xED\xFA\xCE") return true;
588 if (Magic == "\xCE\xFA\xED\xFE") return true;
589 if (Magic == "\xFE\xED\xFA\xCF") return true;
590 if (Magic == "\xCF\xFA\xED\xFE") return true;
591 return false;
592}
593
594} // end namespace llvm