blob: a3b4919f68b17e7c284a437b5de2b5676fc64c5a [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!");
Jim Grosbach652ca2f2012-01-16 23:50:49 +000053 case macho::RIT_X86_64_Signed1:
54 case macho::RIT_X86_64_Signed2:
55 case macho::RIT_X86_64_Signed4:
56 case macho::RIT_X86_64_Signed:
Danil Malyshevcf852dc2011-07-13 07:57:58 +000057 case macho::RIT_X86_64_Unsigned:
58 case macho::RIT_X86_64_Branch: {
Jim Grosbach652ca2f2012-01-16 23:50:49 +000059 Value += Addend;
Danil Malyshevcf852dc2011-07-13 07:57:58 +000060 // Mask in the target value a byte at a time (we don't have an alignment
61 // guarantee for the target address, so this is safest).
62 uint8_t *p = (uint8_t*)Address;
63 for (unsigned i = 0; i < Size; ++i) {
64 *p++ = (uint8_t)Value;
65 Value >>= 8;
66 }
67 return false;
68 }
Danil Malyshevcf852dc2011-07-13 07:57:58 +000069 case macho::RIT_X86_64_GOTLoad:
70 case macho::RIT_X86_64_GOT:
71 case macho::RIT_X86_64_Subtractor:
Danil Malyshevcf852dc2011-07-13 07:57:58 +000072 case macho::RIT_X86_64_TLV:
73 return Error("Relocation type not implemented yet!");
74 }
75 return false;
76}
77
Jim Grosbach61425c02012-01-16 22:26:39 +000078bool RuntimeDyldMachO::
79resolveARMRelocation(uintptr_t Address, uintptr_t Value, bool isPCRel,
80 unsigned Type, unsigned Size, int64_t Addend) {
Danil Malyshevcf852dc2011-07-13 07:57:58 +000081 // If the relocation is PC-relative, the value to be encoded is the
82 // pointer difference.
83 if (isPCRel) {
84 Value -= Address;
85 // ARM PCRel relocations have an effective-PC offset of two instructions
86 // (four bytes in Thumb mode, 8 bytes in ARM mode).
87 // FIXME: For now, assume ARM mode.
88 Value -= 8;
89 }
90
91 switch(Type) {
92 default:
93 llvm_unreachable("Invalid relocation type!");
94 case macho::RIT_Vanilla: {
95 llvm_unreachable("Invalid relocation type!");
96 // Mask in the target value a byte at a time (we don't have an alignment
97 // guarantee for the target address, so this is safest).
98 uint8_t *p = (uint8_t*)Address;
99 for (unsigned i = 0; i < Size; ++i) {
100 *p++ = (uint8_t)Value;
101 Value >>= 8;
102 }
103 break;
104 }
105 case macho::RIT_ARM_Branch24Bit: {
106 // Mask the value into the target address. We know instructions are
107 // 32-bit aligned, so we can do it all at once.
108 uint32_t *p = (uint32_t*)Address;
109 // The low two bits of the value are not encoded.
110 Value >>= 2;
111 // Mask the value to 24 bits.
112 Value &= 0xffffff;
113 // FIXME: If the destination is a Thumb function (and the instruction
114 // is a non-predicated BL instruction), we need to change it to a BLX
115 // instruction instead.
116
117 // Insert the value into the instruction.
118 *p = (*p & ~0xffffff) | Value;
119 break;
120 }
121 case macho::RIT_ARM_ThumbBranch22Bit:
122 case macho::RIT_ARM_ThumbBranch32Bit:
123 case macho::RIT_ARM_Half:
124 case macho::RIT_ARM_HalfDifference:
125 case macho::RIT_Pair:
126 case macho::RIT_Difference:
127 case macho::RIT_ARM_LocalDifference:
128 case macho::RIT_ARM_PreboundLazyPointer:
129 return Error("Relocation type not implemented yet!");
130 }
131 return false;
132}
133
134bool RuntimeDyldMachO::
135loadSegment32(const MachOObject *Obj,
136 const MachOObject::LoadCommandInfo *SegmentLCI,
137 const InMemoryStruct<macho::SymtabLoadCommand> &SymtabLC) {
Jim Grosbach61425c02012-01-16 22:26:39 +0000138 // FIXME: This should really be combined w/ loadSegment64. Templatized
139 // function on the 32/64 datatypes maybe?
Danil Malyshevcf852dc2011-07-13 07:57:58 +0000140 InMemoryStruct<macho::SegmentLoadCommand> SegmentLC;
141 Obj->ReadSegmentLoadCommand(*SegmentLCI, SegmentLC);
142 if (!SegmentLC)
143 return Error("unable to load segment load command");
144
Jim Grosbach61425c02012-01-16 22:26:39 +0000145
146 SmallVector<unsigned, 16> SectionMap;
Danil Malyshevcf852dc2011-07-13 07:57:58 +0000147 for (unsigned SectNum = 0; SectNum != SegmentLC->NumSections; ++SectNum) {
148 InMemoryStruct<macho::Section> Sect;
149 Obj->ReadSection(*SegmentLCI, SectNum, Sect);
150 if (!Sect)
151 return Error("unable to load section: '" + Twine(SectNum) + "'");
152
Jim Grosbach61425c02012-01-16 22:26:39 +0000153 // Allocate memory via the MM for the section.
154 uint8_t *Buffer;
155 uint32_t SectionID = Sections.size();
Danil Malyshevcf852dc2011-07-13 07:57:58 +0000156 if (Sect->Flags != 0x80000400)
Jim Grosbach61425c02012-01-16 22:26:39 +0000157 Buffer = MemMgr->allocateCodeSection(Sect->Size, Sect->Align, SectionID);
158 else
159 Buffer = MemMgr->allocateDataSection(Sect->Size, Sect->Align, SectionID);
Danil Malyshevcf852dc2011-07-13 07:57:58 +0000160
Jim Grosbach61425c02012-01-16 22:26:39 +0000161 DEBUG(dbgs() << "Loading "
162 << ((Sect->Flags == 0x80000400) ? "text" : "data")
163 << " (ID #" << SectionID << ")"
164 << " '" << Sect->SegmentName << ","
165 << Sect->Name << "' of size " << Sect->Size
166 << " to address " << Buffer << ".\n");
Danil Malyshevcf852dc2011-07-13 07:57:58 +0000167
Jim Grosbach61425c02012-01-16 22:26:39 +0000168 // Copy the payload from the object file into the allocated buffer.
Danil Malyshevcf852dc2011-07-13 07:57:58 +0000169 uint8_t *Base = (uint8_t*)Obj->getData(SegmentLC->FileOffset,
170 SegmentLC->FileSize).data();
Jim Grosbach61425c02012-01-16 22:26:39 +0000171 memcpy(Buffer, Base + Sect->Address, Sect->Size);
Danil Malyshevcf852dc2011-07-13 07:57:58 +0000172
Jim Grosbach61425c02012-01-16 22:26:39 +0000173 // Remember what got allocated for this SectionID.
174 Sections.push_back(sys::MemoryBlock(Buffer, Sect->Size));
175
176 // By default, the load address of a section is its memory buffer.
177 SectionLoadAddress.push_back((uint64_t)Buffer);
178
179 // Keep a map of object file section numbers to corresponding SectionIDs
180 // while processing the file.
181 SectionMap.push_back(SectionID);
182 }
183
184 // Process the symbol table.
185 SmallVector<StringRef, 64> SymbolNames;
186 processSymbols32(Obj, SectionMap, SymbolNames, SymtabLC);
187
188 // Process the relocations for each section we're loading.
189 Relocations.grow(Relocations.size() + SegmentLC->NumSections);
190 for (unsigned SectNum = 0; SectNum != SegmentLC->NumSections; ++SectNum) {
191 InMemoryStruct<macho::Section> Sect;
192 Obj->ReadSection(*SegmentLCI, SectNum, Sect);
193 if (!Sect)
194 return Error("unable to load section: '" + Twine(SectNum) + "'");
Danil Malyshevcf852dc2011-07-13 07:57:58 +0000195 for (unsigned j = 0; j != Sect->NumRelocationTableEntries; ++j) {
196 InMemoryStruct<macho::RelocationEntry> RE;
197 Obj->ReadRelocationEntry(Sect->RelocationTableOffset, j, RE);
198 if (RE->Word0 & macho::RF_Scattered)
199 return Error("NOT YET IMPLEMENTED: scattered relocations.");
200 // Word0 of the relocation is the offset into the section where the
201 // relocation should be applied. We need to translate that into an
202 // offset into a function since that's our atom.
203 uint32_t Offset = RE->Word0;
Danil Malyshevcf852dc2011-07-13 07:57:58 +0000204 bool isExtern = (RE->Word1 >> 27) & 1;
Jim Grosbach61425c02012-01-16 22:26:39 +0000205
206 // FIXME: Get the relocation addend from the target address.
207 // FIXME: VERY imporant for internal relocations.
208
Danil Malyshevcf852dc2011-07-13 07:57:58 +0000209 // Figure out the source symbol of the relocation. If isExtern is true,
210 // this relocation references the symbol table, otherwise it references
211 // a section in the same object, numbered from 1 through NumSections
212 // (SectionBases is [0, NumSections-1]).
Danil Malyshevcf852dc2011-07-13 07:57:58 +0000213 uint32_t SourceNum = RE->Word1 & 0xffffff; // 24-bit value
Jim Grosbach61425c02012-01-16 22:26:39 +0000214 if (!isExtern) {
215 assert(SourceNum > 0 && "Invalid relocation section number!");
216 unsigned SectionID = SectionMap[SourceNum - 1];
217 unsigned TargetID = SectionMap[SectNum];
218 DEBUG(dbgs() << "Internal relocation at Section #"
219 << TargetID << " + " << Offset
220 << " from Section #"
221 << SectionID << " (Word1: "
222 << format("0x%x", RE->Word1) << ")\n");
Danil Malyshevcf852dc2011-07-13 07:57:58 +0000223
Jim Grosbach61425c02012-01-16 22:26:39 +0000224 // Store the relocation information. It will get resolved when
225 // the section addresses are assigned.
226 Relocations[SectionID].push_back(RelocationEntry(TargetID,
227 Offset,
228 RE->Word1,
229 0 /*Addend*/));
230 } else {
231 StringRef SourceName = SymbolNames[SourceNum];
Danil Malyshevcf852dc2011-07-13 07:57:58 +0000232
Jim Grosbach61425c02012-01-16 22:26:39 +0000233 // Now store the relocation information. Associate it with the source
234 // symbol. Just add it to the unresolved list and let the general
235 // path post-load resolve it if we know where the symbol is.
236 UnresolvedRelocations[SourceName].push_back(RelocationEntry(SectNum,
237 Offset,
238 RE->Word1,
239 0 /*Addend*/));
240 DEBUG(dbgs() << "Relocation at Section #" << SectNum << " + " << Offset
241 << " from '" << SourceName << "(Word1: "
242 << format("0x%x", RE->Word1) << ")\n");
243 }
Danil Malyshevcf852dc2011-07-13 07:57:58 +0000244 }
245 }
Jim Grosbach61425c02012-01-16 22:26:39 +0000246
247 // Resolve the addresses of any symbols that were defined in this segment.
248 for (int i = 0, e = SymbolNames.size(); i != e; ++i)
249 resolveSymbol(SymbolNames[i]);
250
Danil Malyshevcf852dc2011-07-13 07:57:58 +0000251 return false;
252}
253
254
255bool RuntimeDyldMachO::
256loadSegment64(const MachOObject *Obj,
257 const MachOObject::LoadCommandInfo *SegmentLCI,
258 const InMemoryStruct<macho::SymtabLoadCommand> &SymtabLC) {
259 InMemoryStruct<macho::Segment64LoadCommand> Segment64LC;
260 Obj->ReadSegment64LoadCommand(*SegmentLCI, Segment64LC);
261 if (!Segment64LC)
262 return Error("unable to load segment load command");
263
Jim Grosbach61425c02012-01-16 22:26:39 +0000264
265 SmallVector<unsigned, 16> SectionMap;
Danil Malyshevcf852dc2011-07-13 07:57:58 +0000266 for (unsigned SectNum = 0; SectNum != Segment64LC->NumSections; ++SectNum) {
267 InMemoryStruct<macho::Section64> Sect;
268 Obj->ReadSection64(*SegmentLCI, SectNum, Sect);
269 if (!Sect)
270 return Error("unable to load section: '" + Twine(SectNum) + "'");
271
Jim Grosbach61425c02012-01-16 22:26:39 +0000272 // Allocate memory via the MM for the section.
273 uint8_t *Buffer;
274 uint32_t SectionID = Sections.size();
Danil Malyshevcf852dc2011-07-13 07:57:58 +0000275 if (Sect->Flags != 0x80000400)
Jim Grosbach61425c02012-01-16 22:26:39 +0000276 Buffer = MemMgr->allocateCodeSection(Sect->Size, Sect->Align, SectionID);
277 else
278 Buffer = MemMgr->allocateDataSection(Sect->Size, Sect->Align, SectionID);
Danil Malyshevcf852dc2011-07-13 07:57:58 +0000279
Jim Grosbach61425c02012-01-16 22:26:39 +0000280 DEBUG(dbgs() << "Loading "
281 << ((Sect->Flags == 0x80000400) ? "text" : "data")
282 << " (ID #" << SectionID << ")"
283 << " '" << Sect->SegmentName << ","
284 << Sect->Name << "' of size " << Sect->Size
285 << " to address " << Buffer << ".\n");
Danil Malyshevcf852dc2011-07-13 07:57:58 +0000286
Jim Grosbach61425c02012-01-16 22:26:39 +0000287 // Copy the payload from the object file into the allocated buffer.
Danil Malyshevcf852dc2011-07-13 07:57:58 +0000288 uint8_t *Base = (uint8_t*)Obj->getData(Segment64LC->FileOffset,
289 Segment64LC->FileSize).data();
Jim Grosbach61425c02012-01-16 22:26:39 +0000290 memcpy(Buffer, Base + Sect->Address, Sect->Size);
Danil Malyshevcf852dc2011-07-13 07:57:58 +0000291
Jim Grosbach61425c02012-01-16 22:26:39 +0000292 // Remember what got allocated for this SectionID.
293 Sections.push_back(sys::MemoryBlock(Buffer, Sect->Size));
294
295 // By default, the load address of a section is its memory buffer.
296 SectionLoadAddress.push_back((uint64_t)Buffer);
297
298 // Keep a map of object file section numbers to corresponding SectionIDs
299 // while processing the file.
300 SectionMap.push_back(SectionID);
301 }
302
303 // Process the symbol table.
304 SmallVector<StringRef, 64> SymbolNames;
305 processSymbols64(Obj, SectionMap, SymbolNames, SymtabLC);
306
307 // Process the relocations for each section we're loading.
308 Relocations.grow(Relocations.size() + Segment64LC->NumSections);
309 for (unsigned SectNum = 0; SectNum != Segment64LC->NumSections; ++SectNum) {
310 InMemoryStruct<macho::Section64> Sect;
311 Obj->ReadSection64(*SegmentLCI, SectNum, Sect);
312 if (!Sect)
313 return Error("unable to load section: '" + Twine(SectNum) + "'");
Danil Malyshevcf852dc2011-07-13 07:57:58 +0000314 for (unsigned j = 0; j != Sect->NumRelocationTableEntries; ++j) {
315 InMemoryStruct<macho::RelocationEntry> RE;
316 Obj->ReadRelocationEntry(Sect->RelocationTableOffset, j, RE);
317 if (RE->Word0 & macho::RF_Scattered)
318 return Error("NOT YET IMPLEMENTED: scattered relocations.");
319 // Word0 of the relocation is the offset into the section where the
320 // relocation should be applied. We need to translate that into an
321 // offset into a function since that's our atom.
322 uint32_t Offset = RE->Word0;
Danil Malyshevcf852dc2011-07-13 07:57:58 +0000323 bool isExtern = (RE->Word1 >> 27) & 1;
Jim Grosbach61425c02012-01-16 22:26:39 +0000324
325 // FIXME: Get the relocation addend from the target address.
326 // FIXME: VERY imporant for internal relocations.
327
Danil Malyshevcf852dc2011-07-13 07:57:58 +0000328 // Figure out the source symbol of the relocation. If isExtern is true,
329 // this relocation references the symbol table, otherwise it references
330 // a section in the same object, numbered from 1 through NumSections
331 // (SectionBases is [0, NumSections-1]).
Danil Malyshevcf852dc2011-07-13 07:57:58 +0000332 uint32_t SourceNum = RE->Word1 & 0xffffff; // 24-bit value
Jim Grosbach61425c02012-01-16 22:26:39 +0000333 if (!isExtern) {
334 assert(SourceNum > 0 && "Invalid relocation section number!");
335 unsigned SectionID = SectionMap[SourceNum - 1];
336 unsigned TargetID = SectionMap[SectNum];
337 DEBUG(dbgs() << "Internal relocation at Section #"
338 << TargetID << " + " << Offset
339 << " from Section #"
340 << SectionID << " (Word1: "
341 << format("0x%x", RE->Word1) << ")\n");
Danil Malyshevcf852dc2011-07-13 07:57:58 +0000342
Jim Grosbach61425c02012-01-16 22:26:39 +0000343 // Store the relocation information. It will get resolved when
344 // the section addresses are assigned.
345 Relocations[SectionID].push_back(RelocationEntry(TargetID,
346 Offset,
347 RE->Word1,
348 0 /*Addend*/));
349 } else {
350 StringRef SourceName = SymbolNames[SourceNum];
Danil Malyshevcf852dc2011-07-13 07:57:58 +0000351
Jim Grosbach61425c02012-01-16 22:26:39 +0000352 // Now store the relocation information. Associate it with the source
353 // symbol. Just add it to the unresolved list and let the general
354 // path post-load resolve it if we know where the symbol is.
355 UnresolvedRelocations[SourceName].push_back(RelocationEntry(SectNum,
356 Offset,
357 RE->Word1,
358 0 /*Addend*/));
359 DEBUG(dbgs() << "Relocation at Section #" << SectNum << " + " << Offset
360 << " from '" << SourceName << "(Word1: "
361 << format("0x%x", RE->Word1) << ")\n");
362 }
Danil Malyshevcf852dc2011-07-13 07:57:58 +0000363 }
364 }
Jim Grosbach61425c02012-01-16 22:26:39 +0000365
366 // Resolve the addresses of any symbols that were defined in this segment.
367 for (int i = 0, e = SymbolNames.size(); i != e; ++i)
368 resolveSymbol(SymbolNames[i]);
369
Danil Malyshevcf852dc2011-07-13 07:57:58 +0000370 return false;
371}
372
Jim Grosbach61425c02012-01-16 22:26:39 +0000373bool RuntimeDyldMachO::
374processSymbols32(const MachOObject *Obj,
375 SmallVectorImpl<unsigned> &SectionMap,
376 SmallVectorImpl<StringRef> &SymbolNames,
377 const InMemoryStruct<macho::SymtabLoadCommand> &SymtabLC) {
378 // FIXME: Combine w/ processSymbols64. Factor 64/32 datatype and such.
379 for (unsigned i = 0; i != SymtabLC->NumSymbolTableEntries; ++i) {
380 InMemoryStruct<macho::SymbolTableEntry> STE;
381 Obj->ReadSymbolTableEntry(SymtabLC->SymbolTableOffset, i, STE);
382 if (!STE)
383 return Error("unable to read symbol: '" + Twine(i) + "'");
384 // Get the symbol name.
385 StringRef Name = Obj->getStringAtIndex(STE->StringIndex);
386 SymbolNames.push_back(Name);
387
388 // FIXME: Check the symbol type and flags.
389 if (STE->Type != 0xF) // external, defined in this segment.
390 continue;
391 // Flags in the upper nibble we don't care about.
392 if ((STE->Flags & 0xf) != 0x0)
393 continue;
394
395 // Remember the symbol.
396 uint32_t SectionID = SectionMap[STE->SectionIndex - 1];
397 SymbolTable[Name] = SymbolLoc(SectionID, STE->Value);
398
399 DEBUG(dbgs() << "Symbol: '" << Name << "' @ "
400 << (getSectionAddress(SectionID) + STE->Value)
401 << "\n");
402 }
403 return false;
404}
405
406bool RuntimeDyldMachO::
407processSymbols64(const MachOObject *Obj,
408 SmallVectorImpl<unsigned> &SectionMap,
409 SmallVectorImpl<StringRef> &SymbolNames,
410 const InMemoryStruct<macho::SymtabLoadCommand> &SymtabLC) {
411 for (unsigned i = 0; i != SymtabLC->NumSymbolTableEntries; ++i) {
412 InMemoryStruct<macho::Symbol64TableEntry> STE;
413 Obj->ReadSymbol64TableEntry(SymtabLC->SymbolTableOffset, i, STE);
414 if (!STE)
415 return Error("unable to read symbol: '" + Twine(i) + "'");
416 // Get the symbol name.
417 StringRef Name = Obj->getStringAtIndex(STE->StringIndex);
418 SymbolNames.push_back(Name);
419
420 // FIXME: Check the symbol type and flags.
421 if (STE->Type != 0xF) // external, defined in this segment.
422 continue;
423 // Flags in the upper nibble we don't care about.
424 if ((STE->Flags & 0xf) != 0x0)
425 continue;
426
427 // Remember the symbol.
428 uint32_t SectionID = SectionMap[STE->SectionIndex - 1];
429 SymbolTable[Name] = SymbolLoc(SectionID, STE->Value);
430
431 DEBUG(dbgs() << "Symbol: '" << Name << "' @ "
432 << (getSectionAddress(SectionID) + STE->Value)
433 << "\n");
434 }
435 return false;
436}
437
438// resolveSymbol - Resolve any relocations to the specified symbol if
439// we know where it lives.
440void RuntimeDyldMachO::resolveSymbol(StringRef Name) {
441 StringMap<SymbolLoc>::const_iterator Loc = SymbolTable.find(Name);
442 if (Loc == SymbolTable.end())
443 return;
444
445 RelocationList &Relocs = UnresolvedRelocations[Name];
446 DEBUG(dbgs() << "Resolving symbol '" << Name << "'\n");
447 for (int i = 0, e = Relocs.size(); i != e; ++i) {
448 // Change the relocation to be section relative rather than symbol
449 // relative and move it to the resolved relocation list.
450 RelocationEntry Entry = Relocs[i];
451 Entry.Addend += Loc->second.second;
452 Relocations[Loc->second.first].push_back(Entry);
453 }
454 // FIXME: Keep a worklist of the relocations we've added so that we can
455 // resolve more selectively later.
456 Relocs.clear();
457}
458
Danil Malyshevcf852dc2011-07-13 07:57:58 +0000459bool RuntimeDyldMachO::loadObject(MemoryBuffer *InputBuffer) {
460 // If the linker is in an error state, don't do anything.
461 if (hasError())
462 return true;
463 // Load the Mach-O wrapper object.
464 std::string ErrorStr;
465 OwningPtr<MachOObject> Obj(
466 MachOObject::LoadFromBuffer(InputBuffer, &ErrorStr));
467 if (!Obj)
468 return Error("unable to load object: '" + ErrorStr + "'");
469
470 // Get the CPU type information from the header.
471 const macho::Header &Header = Obj->getHeader();
472
473 // FIXME: Error checking that the loaded object is compatible with
474 // the system we're running on.
475 CPUType = Header.CPUType;
476 CPUSubtype = Header.CPUSubtype;
477
478 // Validate that the load commands match what we expect.
479 const MachOObject::LoadCommandInfo *SegmentLCI = 0, *SymtabLCI = 0,
480 *DysymtabLCI = 0;
481 for (unsigned i = 0; i != Header.NumLoadCommands; ++i) {
482 const MachOObject::LoadCommandInfo &LCI = Obj->getLoadCommandInfo(i);
483 switch (LCI.Command.Type) {
484 case macho::LCT_Segment:
485 case macho::LCT_Segment64:
486 if (SegmentLCI)
487 return Error("unexpected input object (multiple segments)");
488 SegmentLCI = &LCI;
489 break;
490 case macho::LCT_Symtab:
491 if (SymtabLCI)
492 return Error("unexpected input object (multiple symbol tables)");
493 SymtabLCI = &LCI;
494 break;
495 case macho::LCT_Dysymtab:
496 if (DysymtabLCI)
497 return Error("unexpected input object (multiple symbol tables)");
498 DysymtabLCI = &LCI;
499 break;
500 default:
501 return Error("unexpected input object (unexpected load command");
502 }
503 }
504
505 if (!SymtabLCI)
506 return Error("no symbol table found in object");
507 if (!SegmentLCI)
Eli Bendersky1eb189b2012-01-06 07:49:17 +0000508 return Error("no segments found in object");
Danil Malyshevcf852dc2011-07-13 07:57:58 +0000509
510 // Read and register the symbol table data.
511 InMemoryStruct<macho::SymtabLoadCommand> SymtabLC;
512 Obj->ReadSymtabLoadCommand(*SymtabLCI, SymtabLC);
513 if (!SymtabLC)
514 return Error("unable to load symbol table load command");
515 Obj->RegisterStringTable(*SymtabLC);
516
517 // Read the dynamic link-edit information, if present (not present in static
518 // objects).
519 if (DysymtabLCI) {
520 InMemoryStruct<macho::DysymtabLoadCommand> DysymtabLC;
521 Obj->ReadDysymtabLoadCommand(*DysymtabLCI, DysymtabLC);
522 if (!DysymtabLC)
523 return Error("unable to load dynamic link-exit load command");
524
525 // FIXME: We don't support anything interesting yet.
526// if (DysymtabLC->LocalSymbolsIndex != 0)
527// return Error("NOT YET IMPLEMENTED: local symbol entries");
528// if (DysymtabLC->ExternalSymbolsIndex != 0)
529// return Error("NOT YET IMPLEMENTED: non-external symbol entries");
530// if (DysymtabLC->UndefinedSymbolsIndex != SymtabLC->NumSymbolTableEntries)
531// return Error("NOT YET IMPLEMENTED: undefined symbol entries");
532 }
533
534 // Load the segment load command.
535 if (SegmentLCI->Command.Type == macho::LCT_Segment) {
536 if (loadSegment32(Obj.get(), SegmentLCI, SymtabLC))
537 return true;
538 } else {
539 if (loadSegment64(Obj.get(), SegmentLCI, SymtabLC))
540 return true;
541 }
542
Jim Grosbach61425c02012-01-16 22:26:39 +0000543 // Assign the addresses of the sections from the object so that any
544 // relocations to them get set properly.
545 // FIXME: This is done directly from the client at the moment. We should
546 // default the values to the local storage, at least when the target arch
547 // is the same as the host arch.
548
Danil Malyshevcf852dc2011-07-13 07:57:58 +0000549 return false;
550}
551
552// Assign an address to a symbol name and resolve all the relocations
553// associated with it.
Jim Grosbach61425c02012-01-16 22:26:39 +0000554void RuntimeDyldMachO::reassignSectionAddress(unsigned SectionID,
555 uint64_t Addr) {
556 // The address to use for relocation resolution is not
557 // the address of the local section buffer. We must be doing
558 // a remote execution environment of some sort. Re-apply any
559 // relocations referencing this section with the given address.
560 //
561 // Addr is a uint64_t because we can't assume the pointer width
562 // of the target is the same as that of the host. Just use a generic
563 // "big enough" type.
Danil Malyshevcf852dc2011-07-13 07:57:58 +0000564
Jim Grosbach61425c02012-01-16 22:26:39 +0000565 SectionLoadAddress[SectionID] = Addr;
566
567 RelocationList &Relocs = Relocations[SectionID];
Danil Malyshevcf852dc2011-07-13 07:57:58 +0000568 for (unsigned i = 0, e = Relocs.size(); i != e; ++i) {
569 RelocationEntry &RE = Relocs[i];
Jim Grosbach61425c02012-01-16 22:26:39 +0000570 uint8_t *Target = (uint8_t*)Sections[RE.SectionID].base() + RE.Offset;
Danil Malyshevcf852dc2011-07-13 07:57:58 +0000571 bool isPCRel = (RE.Data >> 24) & 1;
572 unsigned Type = (RE.Data >> 28) & 0xf;
573 unsigned Size = 1 << ((RE.Data >> 25) & 3);
574
Jim Grosbach61425c02012-01-16 22:26:39 +0000575 DEBUG(dbgs() << "Resolving relocation at Section #" << RE.SectionID
576 << " + " << RE.Offset << " (" << format("%p", Target) << ")"
577 << " from Section #" << SectionID << " (" << format("%p", Addr) << ")"
Danil Malyshevcf852dc2011-07-13 07:57:58 +0000578 << "(" << (isPCRel ? "pcrel" : "absolute")
Jim Grosbach61425c02012-01-16 22:26:39 +0000579 << ", type: " << Type << ", Size: " << Size << ", Addend: "
580 << RE.Addend << ").\n");
Danil Malyshevcf852dc2011-07-13 07:57:58 +0000581
Jim Grosbach61425c02012-01-16 22:26:39 +0000582 resolveRelocation(Target, Addr, isPCRel, Type, Size, RE.Addend);
Danil Malyshevcf852dc2011-07-13 07:57:58 +0000583 }
584}
585
586bool RuntimeDyldMachO::isKnownFormat(const MemoryBuffer *InputBuffer) {
587 StringRef Magic = InputBuffer->getBuffer().slice(0, 4);
588 if (Magic == "\xFE\xED\xFA\xCE") return true;
589 if (Magic == "\xCE\xFA\xED\xFE") return true;
590 if (Magic == "\xFE\xED\xFA\xCF") return true;
591 if (Magic == "\xCF\xFA\xED\xFE") return true;
592 return false;
593}
594
595} // end namespace llvm