blob: 669ff4c6f8e02d7dafd4aae001b27c730dd49255 [file] [log] [blame]
Jim Grosbache0934be2012-01-16 23:50:58 +00001//===-- RuntimeDyldMachO.cpp - Run-time dynamic linker for MC-JIT -*- C++ -*-=//
Danil Malyshevcf852dc2011-07-13 07:57:58 +00002//
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"
Eli Bendersky76463fd2012-01-22 07:05:02 +000018#include "RuntimeDyldMachO.h"
Danil Malyshevcf852dc2011-07-13 07:57:58 +000019using 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 }
Danil Malyshevcf852dc2011-07-13 07:57:58 +000075}
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));
Jim Grosbach020f4e82012-01-16 23:50:55 +0000174 SectionLocalMemToID[Buffer] = SectionID;
Jim Grosbach61425c02012-01-16 22:26:39 +0000175
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();
Jim Grosbach93391342012-01-21 00:21:53 +0000275 unsigned Align = 1 << Sect->Align; // .o file has log2 alignment.
Jim Grosbachb442d7c2012-01-20 22:44:03 +0000276 if (Sect->Flags == 0x80000400)
Jim Grosbach93391342012-01-21 00:21:53 +0000277 Buffer = MemMgr->allocateCodeSection(Sect->Size, Align, SectionID);
Jim Grosbach61425c02012-01-16 22:26:39 +0000278 else
Jim Grosbach93391342012-01-21 00:21:53 +0000279 Buffer = MemMgr->allocateDataSection(Sect->Size, Align, SectionID);
Danil Malyshevcf852dc2011-07-13 07:57:58 +0000280
Jim Grosbach61425c02012-01-16 22:26:39 +0000281 DEBUG(dbgs() << "Loading "
282 << ((Sect->Flags == 0x80000400) ? "text" : "data")
283 << " (ID #" << SectionID << ")"
284 << " '" << Sect->SegmentName << ","
285 << Sect->Name << "' of size " << Sect->Size
Jim Grosbach93391342012-01-21 00:21:53 +0000286 << " (align " << Align << ")"
Jim Grosbach61425c02012-01-16 22:26:39 +0000287 << " to address " << Buffer << ".\n");
Danil Malyshevcf852dc2011-07-13 07:57:58 +0000288
Jim Grosbach61425c02012-01-16 22:26:39 +0000289 // Copy the payload from the object file into the allocated buffer.
Danil Malyshevcf852dc2011-07-13 07:57:58 +0000290 uint8_t *Base = (uint8_t*)Obj->getData(Segment64LC->FileOffset,
291 Segment64LC->FileSize).data();
Jim Grosbach61425c02012-01-16 22:26:39 +0000292 memcpy(Buffer, Base + Sect->Address, Sect->Size);
Danil Malyshevcf852dc2011-07-13 07:57:58 +0000293
Jim Grosbach61425c02012-01-16 22:26:39 +0000294 // Remember what got allocated for this SectionID.
295 Sections.push_back(sys::MemoryBlock(Buffer, Sect->Size));
Jim Grosbach020f4e82012-01-16 23:50:55 +0000296 SectionLocalMemToID[Buffer] = SectionID;
Jim Grosbach61425c02012-01-16 22:26:39 +0000297
298 // By default, the load address of a section is its memory buffer.
299 SectionLoadAddress.push_back((uint64_t)Buffer);
300
301 // Keep a map of object file section numbers to corresponding SectionIDs
302 // while processing the file.
303 SectionMap.push_back(SectionID);
304 }
305
306 // Process the symbol table.
307 SmallVector<StringRef, 64> SymbolNames;
308 processSymbols64(Obj, SectionMap, SymbolNames, SymtabLC);
309
310 // Process the relocations for each section we're loading.
311 Relocations.grow(Relocations.size() + Segment64LC->NumSections);
312 for (unsigned SectNum = 0; SectNum != Segment64LC->NumSections; ++SectNum) {
313 InMemoryStruct<macho::Section64> Sect;
314 Obj->ReadSection64(*SegmentLCI, SectNum, Sect);
315 if (!Sect)
316 return Error("unable to load section: '" + Twine(SectNum) + "'");
Danil Malyshevcf852dc2011-07-13 07:57:58 +0000317 for (unsigned j = 0; j != Sect->NumRelocationTableEntries; ++j) {
318 InMemoryStruct<macho::RelocationEntry> RE;
319 Obj->ReadRelocationEntry(Sect->RelocationTableOffset, j, RE);
320 if (RE->Word0 & macho::RF_Scattered)
321 return Error("NOT YET IMPLEMENTED: scattered relocations.");
322 // Word0 of the relocation is the offset into the section where the
323 // relocation should be applied. We need to translate that into an
324 // offset into a function since that's our atom.
325 uint32_t Offset = RE->Word0;
Danil Malyshevcf852dc2011-07-13 07:57:58 +0000326 bool isExtern = (RE->Word1 >> 27) & 1;
Jim Grosbach61425c02012-01-16 22:26:39 +0000327
328 // FIXME: Get the relocation addend from the target address.
329 // FIXME: VERY imporant for internal relocations.
330
Danil Malyshevcf852dc2011-07-13 07:57:58 +0000331 // Figure out the source symbol of the relocation. If isExtern is true,
332 // this relocation references the symbol table, otherwise it references
333 // a section in the same object, numbered from 1 through NumSections
334 // (SectionBases is [0, NumSections-1]).
Danil Malyshevcf852dc2011-07-13 07:57:58 +0000335 uint32_t SourceNum = RE->Word1 & 0xffffff; // 24-bit value
Jim Grosbach61425c02012-01-16 22:26:39 +0000336 if (!isExtern) {
337 assert(SourceNum > 0 && "Invalid relocation section number!");
338 unsigned SectionID = SectionMap[SourceNum - 1];
339 unsigned TargetID = SectionMap[SectNum];
340 DEBUG(dbgs() << "Internal relocation at Section #"
341 << TargetID << " + " << Offset
342 << " from Section #"
343 << SectionID << " (Word1: "
344 << format("0x%x", RE->Word1) << ")\n");
Danil Malyshevcf852dc2011-07-13 07:57:58 +0000345
Jim Grosbach61425c02012-01-16 22:26:39 +0000346 // Store the relocation information. It will get resolved when
347 // the section addresses are assigned.
348 Relocations[SectionID].push_back(RelocationEntry(TargetID,
349 Offset,
350 RE->Word1,
351 0 /*Addend*/));
352 } else {
353 StringRef SourceName = SymbolNames[SourceNum];
Danil Malyshevcf852dc2011-07-13 07:57:58 +0000354
Jim Grosbach61425c02012-01-16 22:26:39 +0000355 // Now store the relocation information. Associate it with the source
356 // symbol. Just add it to the unresolved list and let the general
357 // path post-load resolve it if we know where the symbol is.
358 UnresolvedRelocations[SourceName].push_back(RelocationEntry(SectNum,
359 Offset,
360 RE->Word1,
361 0 /*Addend*/));
362 DEBUG(dbgs() << "Relocation at Section #" << SectNum << " + " << Offset
363 << " from '" << SourceName << "(Word1: "
364 << format("0x%x", RE->Word1) << ")\n");
365 }
Danil Malyshevcf852dc2011-07-13 07:57:58 +0000366 }
367 }
Jim Grosbach61425c02012-01-16 22:26:39 +0000368
369 // Resolve the addresses of any symbols that were defined in this segment.
370 for (int i = 0, e = SymbolNames.size(); i != e; ++i)
371 resolveSymbol(SymbolNames[i]);
372
Danil Malyshevcf852dc2011-07-13 07:57:58 +0000373 return false;
374}
375
Jim Grosbach61425c02012-01-16 22:26:39 +0000376bool RuntimeDyldMachO::
377processSymbols32(const MachOObject *Obj,
378 SmallVectorImpl<unsigned> &SectionMap,
379 SmallVectorImpl<StringRef> &SymbolNames,
380 const InMemoryStruct<macho::SymtabLoadCommand> &SymtabLC) {
381 // FIXME: Combine w/ processSymbols64. Factor 64/32 datatype and such.
382 for (unsigned i = 0; i != SymtabLC->NumSymbolTableEntries; ++i) {
383 InMemoryStruct<macho::SymbolTableEntry> STE;
384 Obj->ReadSymbolTableEntry(SymtabLC->SymbolTableOffset, i, STE);
385 if (!STE)
386 return Error("unable to read symbol: '" + Twine(i) + "'");
387 // Get the symbol name.
388 StringRef Name = Obj->getStringAtIndex(STE->StringIndex);
389 SymbolNames.push_back(Name);
390
391 // FIXME: Check the symbol type and flags.
392 if (STE->Type != 0xF) // external, defined in this segment.
393 continue;
394 // Flags in the upper nibble we don't care about.
395 if ((STE->Flags & 0xf) != 0x0)
396 continue;
397
398 // Remember the symbol.
399 uint32_t SectionID = SectionMap[STE->SectionIndex - 1];
400 SymbolTable[Name] = SymbolLoc(SectionID, STE->Value);
401
402 DEBUG(dbgs() << "Symbol: '" << Name << "' @ "
403 << (getSectionAddress(SectionID) + STE->Value)
404 << "\n");
405 }
406 return false;
407}
408
409bool RuntimeDyldMachO::
410processSymbols64(const MachOObject *Obj,
411 SmallVectorImpl<unsigned> &SectionMap,
412 SmallVectorImpl<StringRef> &SymbolNames,
413 const InMemoryStruct<macho::SymtabLoadCommand> &SymtabLC) {
414 for (unsigned i = 0; i != SymtabLC->NumSymbolTableEntries; ++i) {
415 InMemoryStruct<macho::Symbol64TableEntry> STE;
416 Obj->ReadSymbol64TableEntry(SymtabLC->SymbolTableOffset, i, STE);
417 if (!STE)
418 return Error("unable to read symbol: '" + Twine(i) + "'");
419 // Get the symbol name.
420 StringRef Name = Obj->getStringAtIndex(STE->StringIndex);
421 SymbolNames.push_back(Name);
422
423 // FIXME: Check the symbol type and flags.
424 if (STE->Type != 0xF) // external, defined in this segment.
425 continue;
426 // Flags in the upper nibble we don't care about.
427 if ((STE->Flags & 0xf) != 0x0)
428 continue;
429
430 // Remember the symbol.
431 uint32_t SectionID = SectionMap[STE->SectionIndex - 1];
432 SymbolTable[Name] = SymbolLoc(SectionID, STE->Value);
433
434 DEBUG(dbgs() << "Symbol: '" << Name << "' @ "
435 << (getSectionAddress(SectionID) + STE->Value)
436 << "\n");
437 }
438 return false;
439}
440
441// resolveSymbol - Resolve any relocations to the specified symbol if
442// we know where it lives.
443void RuntimeDyldMachO::resolveSymbol(StringRef Name) {
444 StringMap<SymbolLoc>::const_iterator Loc = SymbolTable.find(Name);
445 if (Loc == SymbolTable.end())
446 return;
447
448 RelocationList &Relocs = UnresolvedRelocations[Name];
449 DEBUG(dbgs() << "Resolving symbol '" << Name << "'\n");
450 for (int i = 0, e = Relocs.size(); i != e; ++i) {
451 // Change the relocation to be section relative rather than symbol
452 // relative and move it to the resolved relocation list.
453 RelocationEntry Entry = Relocs[i];
454 Entry.Addend += Loc->second.second;
455 Relocations[Loc->second.first].push_back(Entry);
456 }
457 // FIXME: Keep a worklist of the relocations we've added so that we can
458 // resolve more selectively later.
459 Relocs.clear();
460}
461
Danil Malyshevcf852dc2011-07-13 07:57:58 +0000462bool RuntimeDyldMachO::loadObject(MemoryBuffer *InputBuffer) {
463 // If the linker is in an error state, don't do anything.
464 if (hasError())
465 return true;
466 // Load the Mach-O wrapper object.
467 std::string ErrorStr;
468 OwningPtr<MachOObject> Obj(
469 MachOObject::LoadFromBuffer(InputBuffer, &ErrorStr));
470 if (!Obj)
471 return Error("unable to load object: '" + ErrorStr + "'");
472
473 // Get the CPU type information from the header.
474 const macho::Header &Header = Obj->getHeader();
475
476 // FIXME: Error checking that the loaded object is compatible with
477 // the system we're running on.
478 CPUType = Header.CPUType;
479 CPUSubtype = Header.CPUSubtype;
480
481 // Validate that the load commands match what we expect.
482 const MachOObject::LoadCommandInfo *SegmentLCI = 0, *SymtabLCI = 0,
483 *DysymtabLCI = 0;
484 for (unsigned i = 0; i != Header.NumLoadCommands; ++i) {
485 const MachOObject::LoadCommandInfo &LCI = Obj->getLoadCommandInfo(i);
486 switch (LCI.Command.Type) {
487 case macho::LCT_Segment:
488 case macho::LCT_Segment64:
489 if (SegmentLCI)
490 return Error("unexpected input object (multiple segments)");
491 SegmentLCI = &LCI;
492 break;
493 case macho::LCT_Symtab:
494 if (SymtabLCI)
495 return Error("unexpected input object (multiple symbol tables)");
496 SymtabLCI = &LCI;
497 break;
498 case macho::LCT_Dysymtab:
499 if (DysymtabLCI)
500 return Error("unexpected input object (multiple symbol tables)");
501 DysymtabLCI = &LCI;
502 break;
503 default:
504 return Error("unexpected input object (unexpected load command");
505 }
506 }
507
508 if (!SymtabLCI)
509 return Error("no symbol table found in object");
510 if (!SegmentLCI)
Eli Bendersky1eb189b2012-01-06 07:49:17 +0000511 return Error("no segments found in object");
Danil Malyshevcf852dc2011-07-13 07:57:58 +0000512
513 // Read and register the symbol table data.
514 InMemoryStruct<macho::SymtabLoadCommand> SymtabLC;
515 Obj->ReadSymtabLoadCommand(*SymtabLCI, SymtabLC);
516 if (!SymtabLC)
517 return Error("unable to load symbol table load command");
518 Obj->RegisterStringTable(*SymtabLC);
519
520 // Read the dynamic link-edit information, if present (not present in static
521 // objects).
522 if (DysymtabLCI) {
523 InMemoryStruct<macho::DysymtabLoadCommand> DysymtabLC;
524 Obj->ReadDysymtabLoadCommand(*DysymtabLCI, DysymtabLC);
525 if (!DysymtabLC)
526 return Error("unable to load dynamic link-exit load command");
527
528 // FIXME: We don't support anything interesting yet.
529// if (DysymtabLC->LocalSymbolsIndex != 0)
530// return Error("NOT YET IMPLEMENTED: local symbol entries");
531// if (DysymtabLC->ExternalSymbolsIndex != 0)
532// return Error("NOT YET IMPLEMENTED: non-external symbol entries");
533// if (DysymtabLC->UndefinedSymbolsIndex != SymtabLC->NumSymbolTableEntries)
534// return Error("NOT YET IMPLEMENTED: undefined symbol entries");
535 }
536
537 // Load the segment load command.
538 if (SegmentLCI->Command.Type == macho::LCT_Segment) {
539 if (loadSegment32(Obj.get(), SegmentLCI, SymtabLC))
540 return true;
541 } else {
542 if (loadSegment64(Obj.get(), SegmentLCI, SymtabLC))
543 return true;
544 }
545
Jim Grosbach61425c02012-01-16 22:26:39 +0000546 // Assign the addresses of the sections from the object so that any
547 // relocations to them get set properly.
548 // FIXME: This is done directly from the client at the moment. We should
549 // default the values to the local storage, at least when the target arch
550 // is the same as the host arch.
551
Danil Malyshevcf852dc2011-07-13 07:57:58 +0000552 return false;
553}
554
555// Assign an address to a symbol name and resolve all the relocations
556// associated with it.
Jim Grosbach61425c02012-01-16 22:26:39 +0000557void RuntimeDyldMachO::reassignSectionAddress(unsigned SectionID,
558 uint64_t Addr) {
559 // The address to use for relocation resolution is not
560 // the address of the local section buffer. We must be doing
561 // a remote execution environment of some sort. Re-apply any
562 // relocations referencing this section with the given address.
563 //
564 // Addr is a uint64_t because we can't assume the pointer width
565 // of the target is the same as that of the host. Just use a generic
566 // "big enough" type.
Danil Malyshevcf852dc2011-07-13 07:57:58 +0000567
Jim Grosbach61425c02012-01-16 22:26:39 +0000568 SectionLoadAddress[SectionID] = Addr;
569
570 RelocationList &Relocs = Relocations[SectionID];
Danil Malyshevcf852dc2011-07-13 07:57:58 +0000571 for (unsigned i = 0, e = Relocs.size(); i != e; ++i) {
572 RelocationEntry &RE = Relocs[i];
Jim Grosbach61425c02012-01-16 22:26:39 +0000573 uint8_t *Target = (uint8_t*)Sections[RE.SectionID].base() + RE.Offset;
Danil Malyshevcf852dc2011-07-13 07:57:58 +0000574 bool isPCRel = (RE.Data >> 24) & 1;
575 unsigned Type = (RE.Data >> 28) & 0xf;
576 unsigned Size = 1 << ((RE.Data >> 25) & 3);
577
Jim Grosbach61425c02012-01-16 22:26:39 +0000578 DEBUG(dbgs() << "Resolving relocation at Section #" << RE.SectionID
579 << " + " << RE.Offset << " (" << format("%p", Target) << ")"
580 << " from Section #" << SectionID << " (" << format("%p", Addr) << ")"
Danil Malyshevcf852dc2011-07-13 07:57:58 +0000581 << "(" << (isPCRel ? "pcrel" : "absolute")
Jim Grosbach61425c02012-01-16 22:26:39 +0000582 << ", type: " << Type << ", Size: " << Size << ", Addend: "
583 << RE.Addend << ").\n");
Danil Malyshevcf852dc2011-07-13 07:57:58 +0000584
Jim Grosbach61425c02012-01-16 22:26:39 +0000585 resolveRelocation(Target, Addr, isPCRel, Type, Size, RE.Addend);
Danil Malyshevcf852dc2011-07-13 07:57:58 +0000586 }
587}
588
589bool RuntimeDyldMachO::isKnownFormat(const MemoryBuffer *InputBuffer) {
590 StringRef Magic = InputBuffer->getBuffer().slice(0, 4);
591 if (Magic == "\xFE\xED\xFA\xCE") return true;
592 if (Magic == "\xCE\xFA\xED\xFE") return true;
593 if (Magic == "\xFE\xED\xFA\xCF") return true;
594 if (Magic == "\xCF\xFA\xED\xFE") return true;
595 return false;
596}
597
598} // end namespace llvm