blob: e96a68925a0e1c08e6a27a23bfc1cbddd2cf1218 [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::
Sean Callanan61dfa772012-03-07 23:05:25 +000025resolveRelocation(uint8_t *LocalAddress,
26 uint64_t FinalAddress,
27 uint64_t Value,
28 bool isPCRel,
29 unsigned Type,
30 unsigned Size,
31 int64_t Addend) {
Danil Malyshevcf852dc2011-07-13 07:57:58 +000032 // This just dispatches to the proper target specific routine.
33 switch (CPUType) {
Craig Topper85814382012-02-07 05:05:23 +000034 default: llvm_unreachable("Unsupported CPU type!");
Danil Malyshevcf852dc2011-07-13 07:57:58 +000035 case mach::CTM_x86_64:
Sean Callanan61dfa772012-03-07 23:05:25 +000036 return resolveX86_64Relocation(LocalAddress,
37 FinalAddress,
38 (uintptr_t)Value,
39 isPCRel,
40 Type,
41 Size,
42 Addend);
Danil Malyshevcf852dc2011-07-13 07:57:58 +000043 case mach::CTM_ARM:
Sean Callanan61dfa772012-03-07 23:05:25 +000044 return resolveARMRelocation(LocalAddress,
45 FinalAddress,
46 (uintptr_t)Value,
47 isPCRel,
48 Type,
49 Size,
50 Addend);
Danil Malyshevcf852dc2011-07-13 07:57:58 +000051 }
Danil Malyshevcf852dc2011-07-13 07:57:58 +000052}
53
54bool RuntimeDyldMachO::
Sean Callanan61dfa772012-03-07 23:05:25 +000055resolveX86_64Relocation(uint8_t *LocalAddress,
56 uint64_t FinalAddress,
57 uint64_t Value,
58 bool isPCRel,
59 unsigned Type,
60 unsigned Size,
61 int64_t Addend) {
Danil Malyshevcf852dc2011-07-13 07:57:58 +000062 // If the relocation is PC-relative, the value to be encoded is the
63 // pointer difference.
64 if (isPCRel)
65 // FIXME: It seems this value needs to be adjusted by 4 for an effective PC
66 // address. Is that expected? Only for branches, perhaps?
Sean Callanan61dfa772012-03-07 23:05:25 +000067 Value -= FinalAddress + 4;
Danil Malyshevcf852dc2011-07-13 07:57:58 +000068
69 switch(Type) {
70 default:
71 llvm_unreachable("Invalid relocation type!");
Jim Grosbach652ca2f2012-01-16 23:50:49 +000072 case macho::RIT_X86_64_Signed1:
73 case macho::RIT_X86_64_Signed2:
74 case macho::RIT_X86_64_Signed4:
75 case macho::RIT_X86_64_Signed:
Danil Malyshevcf852dc2011-07-13 07:57:58 +000076 case macho::RIT_X86_64_Unsigned:
77 case macho::RIT_X86_64_Branch: {
Jim Grosbach652ca2f2012-01-16 23:50:49 +000078 Value += Addend;
Danil Malyshevcf852dc2011-07-13 07:57:58 +000079 // Mask in the target value a byte at a time (we don't have an alignment
80 // guarantee for the target address, so this is safest).
Sean Callanan61dfa772012-03-07 23:05:25 +000081 uint8_t *p = (uint8_t*)LocalAddress;
Danil Malyshevcf852dc2011-07-13 07:57:58 +000082 for (unsigned i = 0; i < Size; ++i) {
83 *p++ = (uint8_t)Value;
84 Value >>= 8;
85 }
86 return false;
87 }
Danil Malyshevcf852dc2011-07-13 07:57:58 +000088 case macho::RIT_X86_64_GOTLoad:
89 case macho::RIT_X86_64_GOT:
90 case macho::RIT_X86_64_Subtractor:
Danil Malyshevcf852dc2011-07-13 07:57:58 +000091 case macho::RIT_X86_64_TLV:
92 return Error("Relocation type not implemented yet!");
93 }
Danil Malyshevcf852dc2011-07-13 07:57:58 +000094}
95
Jim Grosbach61425c02012-01-16 22:26:39 +000096bool RuntimeDyldMachO::
Sean Callanan61dfa772012-03-07 23:05:25 +000097resolveARMRelocation(uint8_t *LocalAddress,
98 uint64_t FinalAddress,
99 uint64_t Value,
100 bool isPCRel,
101 unsigned Type,
102 unsigned Size,
103 int64_t Addend) {
Danil Malyshevcf852dc2011-07-13 07:57:58 +0000104 // If the relocation is PC-relative, the value to be encoded is the
105 // pointer difference.
106 if (isPCRel) {
Sean Callanan61dfa772012-03-07 23:05:25 +0000107 Value -= FinalAddress;
Danil Malyshevcf852dc2011-07-13 07:57:58 +0000108 // ARM PCRel relocations have an effective-PC offset of two instructions
109 // (four bytes in Thumb mode, 8 bytes in ARM mode).
110 // FIXME: For now, assume ARM mode.
111 Value -= 8;
112 }
113
114 switch(Type) {
115 default:
116 llvm_unreachable("Invalid relocation type!");
117 case macho::RIT_Vanilla: {
Danil Malyshevcf852dc2011-07-13 07:57:58 +0000118 // Mask in the target value a byte at a time (we don't have an alignment
119 // guarantee for the target address, so this is safest).
Sean Callanan61dfa772012-03-07 23:05:25 +0000120 uint8_t *p = (uint8_t*)LocalAddress;
Danil Malyshevcf852dc2011-07-13 07:57:58 +0000121 for (unsigned i = 0; i < Size; ++i) {
122 *p++ = (uint8_t)Value;
123 Value >>= 8;
124 }
125 break;
126 }
127 case macho::RIT_ARM_Branch24Bit: {
128 // Mask the value into the target address. We know instructions are
129 // 32-bit aligned, so we can do it all at once.
Sean Callanan61dfa772012-03-07 23:05:25 +0000130 uint32_t *p = (uint32_t*)LocalAddress;
Danil Malyshevcf852dc2011-07-13 07:57:58 +0000131 // The low two bits of the value are not encoded.
132 Value >>= 2;
133 // Mask the value to 24 bits.
134 Value &= 0xffffff;
135 // FIXME: If the destination is a Thumb function (and the instruction
136 // is a non-predicated BL instruction), we need to change it to a BLX
137 // instruction instead.
138
139 // Insert the value into the instruction.
140 *p = (*p & ~0xffffff) | Value;
141 break;
142 }
143 case macho::RIT_ARM_ThumbBranch22Bit:
144 case macho::RIT_ARM_ThumbBranch32Bit:
145 case macho::RIT_ARM_Half:
146 case macho::RIT_ARM_HalfDifference:
147 case macho::RIT_Pair:
148 case macho::RIT_Difference:
149 case macho::RIT_ARM_LocalDifference:
150 case macho::RIT_ARM_PreboundLazyPointer:
151 return Error("Relocation type not implemented yet!");
152 }
153 return false;
154}
155
156bool RuntimeDyldMachO::
157loadSegment32(const MachOObject *Obj,
158 const MachOObject::LoadCommandInfo *SegmentLCI,
159 const InMemoryStruct<macho::SymtabLoadCommand> &SymtabLC) {
Jim Grosbach61425c02012-01-16 22:26:39 +0000160 // FIXME: This should really be combined w/ loadSegment64. Templatized
161 // function on the 32/64 datatypes maybe?
Danil Malyshevcf852dc2011-07-13 07:57:58 +0000162 InMemoryStruct<macho::SegmentLoadCommand> SegmentLC;
163 Obj->ReadSegmentLoadCommand(*SegmentLCI, SegmentLC);
164 if (!SegmentLC)
165 return Error("unable to load segment load command");
166
Jim Grosbach61425c02012-01-16 22:26:39 +0000167
168 SmallVector<unsigned, 16> SectionMap;
Danil Malyshevcf852dc2011-07-13 07:57:58 +0000169 for (unsigned SectNum = 0; SectNum != SegmentLC->NumSections; ++SectNum) {
170 InMemoryStruct<macho::Section> Sect;
171 Obj->ReadSection(*SegmentLCI, SectNum, Sect);
172 if (!Sect)
173 return Error("unable to load section: '" + Twine(SectNum) + "'");
174
Jim Grosbach61425c02012-01-16 22:26:39 +0000175 // Allocate memory via the MM for the section.
176 uint8_t *Buffer;
177 uint32_t SectionID = Sections.size();
Sean Callanan996d6fd2012-03-01 00:15:29 +0000178 if (Sect->Flags == 0x80000400)
Jim Grosbach61425c02012-01-16 22:26:39 +0000179 Buffer = MemMgr->allocateCodeSection(Sect->Size, Sect->Align, SectionID);
180 else
181 Buffer = MemMgr->allocateDataSection(Sect->Size, Sect->Align, SectionID);
Danil Malyshevcf852dc2011-07-13 07:57:58 +0000182
Jim Grosbach61425c02012-01-16 22:26:39 +0000183 DEBUG(dbgs() << "Loading "
184 << ((Sect->Flags == 0x80000400) ? "text" : "data")
185 << " (ID #" << SectionID << ")"
186 << " '" << Sect->SegmentName << ","
187 << Sect->Name << "' of size " << Sect->Size
188 << " to address " << Buffer << ".\n");
Danil Malyshevcf852dc2011-07-13 07:57:58 +0000189
Jim Grosbach61425c02012-01-16 22:26:39 +0000190 // Copy the payload from the object file into the allocated buffer.
Danil Malyshevcf852dc2011-07-13 07:57:58 +0000191 uint8_t *Base = (uint8_t*)Obj->getData(SegmentLC->FileOffset,
192 SegmentLC->FileSize).data();
Jim Grosbach61425c02012-01-16 22:26:39 +0000193 memcpy(Buffer, Base + Sect->Address, Sect->Size);
Danil Malyshevcf852dc2011-07-13 07:57:58 +0000194
Jim Grosbach61425c02012-01-16 22:26:39 +0000195 // Remember what got allocated for this SectionID.
196 Sections.push_back(sys::MemoryBlock(Buffer, Sect->Size));
Jim Grosbach020f4e82012-01-16 23:50:55 +0000197 SectionLocalMemToID[Buffer] = SectionID;
Jim Grosbach61425c02012-01-16 22:26:39 +0000198
199 // By default, the load address of a section is its memory buffer.
200 SectionLoadAddress.push_back((uint64_t)Buffer);
201
202 // Keep a map of object file section numbers to corresponding SectionIDs
203 // while processing the file.
204 SectionMap.push_back(SectionID);
205 }
206
207 // Process the symbol table.
208 SmallVector<StringRef, 64> SymbolNames;
209 processSymbols32(Obj, SectionMap, SymbolNames, SymtabLC);
210
211 // Process the relocations for each section we're loading.
212 Relocations.grow(Relocations.size() + SegmentLC->NumSections);
213 for (unsigned SectNum = 0; SectNum != SegmentLC->NumSections; ++SectNum) {
214 InMemoryStruct<macho::Section> Sect;
215 Obj->ReadSection(*SegmentLCI, SectNum, Sect);
216 if (!Sect)
217 return Error("unable to load section: '" + Twine(SectNum) + "'");
Danil Malyshevcf852dc2011-07-13 07:57:58 +0000218 for (unsigned j = 0; j != Sect->NumRelocationTableEntries; ++j) {
219 InMemoryStruct<macho::RelocationEntry> RE;
220 Obj->ReadRelocationEntry(Sect->RelocationTableOffset, j, RE);
221 if (RE->Word0 & macho::RF_Scattered)
222 return Error("NOT YET IMPLEMENTED: scattered relocations.");
223 // Word0 of the relocation is the offset into the section where the
224 // relocation should be applied. We need to translate that into an
225 // offset into a function since that's our atom.
226 uint32_t Offset = RE->Word0;
Danil Malyshevcf852dc2011-07-13 07:57:58 +0000227 bool isExtern = (RE->Word1 >> 27) & 1;
Jim Grosbach61425c02012-01-16 22:26:39 +0000228
229 // FIXME: Get the relocation addend from the target address.
230 // FIXME: VERY imporant for internal relocations.
231
Danil Malyshevcf852dc2011-07-13 07:57:58 +0000232 // Figure out the source symbol of the relocation. If isExtern is true,
233 // this relocation references the symbol table, otherwise it references
234 // a section in the same object, numbered from 1 through NumSections
235 // (SectionBases is [0, NumSections-1]).
Danil Malyshevcf852dc2011-07-13 07:57:58 +0000236 uint32_t SourceNum = RE->Word1 & 0xffffff; // 24-bit value
Jim Grosbach61425c02012-01-16 22:26:39 +0000237 if (!isExtern) {
238 assert(SourceNum > 0 && "Invalid relocation section number!");
239 unsigned SectionID = SectionMap[SourceNum - 1];
240 unsigned TargetID = SectionMap[SectNum];
241 DEBUG(dbgs() << "Internal relocation at Section #"
242 << TargetID << " + " << Offset
243 << " from Section #"
244 << SectionID << " (Word1: "
245 << format("0x%x", RE->Word1) << ")\n");
Danil Malyshevcf852dc2011-07-13 07:57:58 +0000246
Jim Grosbach61425c02012-01-16 22:26:39 +0000247 // Store the relocation information. It will get resolved when
248 // the section addresses are assigned.
249 Relocations[SectionID].push_back(RelocationEntry(TargetID,
250 Offset,
251 RE->Word1,
252 0 /*Addend*/));
253 } else {
254 StringRef SourceName = SymbolNames[SourceNum];
Danil Malyshevcf852dc2011-07-13 07:57:58 +0000255
Jim Grosbach61425c02012-01-16 22:26:39 +0000256 // Now store the relocation information. Associate it with the source
257 // symbol. Just add it to the unresolved list and let the general
258 // path post-load resolve it if we know where the symbol is.
259 UnresolvedRelocations[SourceName].push_back(RelocationEntry(SectNum,
260 Offset,
261 RE->Word1,
262 0 /*Addend*/));
263 DEBUG(dbgs() << "Relocation at Section #" << SectNum << " + " << Offset
264 << " from '" << SourceName << "(Word1: "
265 << format("0x%x", RE->Word1) << ")\n");
266 }
Danil Malyshevcf852dc2011-07-13 07:57:58 +0000267 }
268 }
Jim Grosbach61425c02012-01-16 22:26:39 +0000269
270 // Resolve the addresses of any symbols that were defined in this segment.
271 for (int i = 0, e = SymbolNames.size(); i != e; ++i)
272 resolveSymbol(SymbolNames[i]);
273
Danil Malyshevcf852dc2011-07-13 07:57:58 +0000274 return false;
275}
276
277
278bool RuntimeDyldMachO::
279loadSegment64(const MachOObject *Obj,
280 const MachOObject::LoadCommandInfo *SegmentLCI,
281 const InMemoryStruct<macho::SymtabLoadCommand> &SymtabLC) {
282 InMemoryStruct<macho::Segment64LoadCommand> Segment64LC;
283 Obj->ReadSegment64LoadCommand(*SegmentLCI, Segment64LC);
284 if (!Segment64LC)
285 return Error("unable to load segment load command");
286
Jim Grosbach61425c02012-01-16 22:26:39 +0000287
288 SmallVector<unsigned, 16> SectionMap;
Danil Malyshevcf852dc2011-07-13 07:57:58 +0000289 for (unsigned SectNum = 0; SectNum != Segment64LC->NumSections; ++SectNum) {
290 InMemoryStruct<macho::Section64> Sect;
291 Obj->ReadSection64(*SegmentLCI, SectNum, Sect);
292 if (!Sect)
293 return Error("unable to load section: '" + Twine(SectNum) + "'");
294
Jim Grosbach61425c02012-01-16 22:26:39 +0000295 // Allocate memory via the MM for the section.
296 uint8_t *Buffer;
297 uint32_t SectionID = Sections.size();
Jim Grosbach93391342012-01-21 00:21:53 +0000298 unsigned Align = 1 << Sect->Align; // .o file has log2 alignment.
Jim Grosbachb442d7c2012-01-20 22:44:03 +0000299 if (Sect->Flags == 0x80000400)
Jim Grosbach93391342012-01-21 00:21:53 +0000300 Buffer = MemMgr->allocateCodeSection(Sect->Size, Align, SectionID);
Jim Grosbach61425c02012-01-16 22:26:39 +0000301 else
Jim Grosbach93391342012-01-21 00:21:53 +0000302 Buffer = MemMgr->allocateDataSection(Sect->Size, Align, SectionID);
Danil Malyshevcf852dc2011-07-13 07:57:58 +0000303
Jim Grosbach61425c02012-01-16 22:26:39 +0000304 DEBUG(dbgs() << "Loading "
305 << ((Sect->Flags == 0x80000400) ? "text" : "data")
306 << " (ID #" << SectionID << ")"
307 << " '" << Sect->SegmentName << ","
308 << Sect->Name << "' of size " << Sect->Size
Jim Grosbach93391342012-01-21 00:21:53 +0000309 << " (align " << Align << ")"
Jim Grosbach61425c02012-01-16 22:26:39 +0000310 << " to address " << Buffer << ".\n");
Danil Malyshevcf852dc2011-07-13 07:57:58 +0000311
Jim Grosbach61425c02012-01-16 22:26:39 +0000312 // Copy the payload from the object file into the allocated buffer.
Danil Malyshevcf852dc2011-07-13 07:57:58 +0000313 uint8_t *Base = (uint8_t*)Obj->getData(Segment64LC->FileOffset,
314 Segment64LC->FileSize).data();
Jim Grosbach61425c02012-01-16 22:26:39 +0000315 memcpy(Buffer, Base + Sect->Address, Sect->Size);
Danil Malyshevcf852dc2011-07-13 07:57:58 +0000316
Jim Grosbach61425c02012-01-16 22:26:39 +0000317 // Remember what got allocated for this SectionID.
318 Sections.push_back(sys::MemoryBlock(Buffer, Sect->Size));
Jim Grosbach020f4e82012-01-16 23:50:55 +0000319 SectionLocalMemToID[Buffer] = SectionID;
Jim Grosbach61425c02012-01-16 22:26:39 +0000320
321 // By default, the load address of a section is its memory buffer.
322 SectionLoadAddress.push_back((uint64_t)Buffer);
323
324 // Keep a map of object file section numbers to corresponding SectionIDs
325 // while processing the file.
326 SectionMap.push_back(SectionID);
327 }
328
329 // Process the symbol table.
330 SmallVector<StringRef, 64> SymbolNames;
331 processSymbols64(Obj, SectionMap, SymbolNames, SymtabLC);
332
333 // Process the relocations for each section we're loading.
334 Relocations.grow(Relocations.size() + Segment64LC->NumSections);
335 for (unsigned SectNum = 0; SectNum != Segment64LC->NumSections; ++SectNum) {
336 InMemoryStruct<macho::Section64> Sect;
337 Obj->ReadSection64(*SegmentLCI, SectNum, Sect);
338 if (!Sect)
339 return Error("unable to load section: '" + Twine(SectNum) + "'");
Danil Malyshevcf852dc2011-07-13 07:57:58 +0000340 for (unsigned j = 0; j != Sect->NumRelocationTableEntries; ++j) {
341 InMemoryStruct<macho::RelocationEntry> RE;
342 Obj->ReadRelocationEntry(Sect->RelocationTableOffset, j, RE);
343 if (RE->Word0 & macho::RF_Scattered)
344 return Error("NOT YET IMPLEMENTED: scattered relocations.");
345 // Word0 of the relocation is the offset into the section where the
346 // relocation should be applied. We need to translate that into an
347 // offset into a function since that's our atom.
348 uint32_t Offset = RE->Word0;
Danil Malyshevcf852dc2011-07-13 07:57:58 +0000349 bool isExtern = (RE->Word1 >> 27) & 1;
Jim Grosbach61425c02012-01-16 22:26:39 +0000350
351 // FIXME: Get the relocation addend from the target address.
352 // FIXME: VERY imporant for internal relocations.
353
Danil Malyshevcf852dc2011-07-13 07:57:58 +0000354 // Figure out the source symbol of the relocation. If isExtern is true,
355 // this relocation references the symbol table, otherwise it references
356 // a section in the same object, numbered from 1 through NumSections
357 // (SectionBases is [0, NumSections-1]).
Danil Malyshevcf852dc2011-07-13 07:57:58 +0000358 uint32_t SourceNum = RE->Word1 & 0xffffff; // 24-bit value
Jim Grosbach61425c02012-01-16 22:26:39 +0000359 if (!isExtern) {
360 assert(SourceNum > 0 && "Invalid relocation section number!");
361 unsigned SectionID = SectionMap[SourceNum - 1];
362 unsigned TargetID = SectionMap[SectNum];
363 DEBUG(dbgs() << "Internal relocation at Section #"
364 << TargetID << " + " << Offset
365 << " from Section #"
366 << SectionID << " (Word1: "
367 << format("0x%x", RE->Word1) << ")\n");
Danil Malyshevcf852dc2011-07-13 07:57:58 +0000368
Jim Grosbach61425c02012-01-16 22:26:39 +0000369 // Store the relocation information. It will get resolved when
370 // the section addresses are assigned.
371 Relocations[SectionID].push_back(RelocationEntry(TargetID,
372 Offset,
373 RE->Word1,
374 0 /*Addend*/));
375 } else {
376 StringRef SourceName = SymbolNames[SourceNum];
Danil Malyshevcf852dc2011-07-13 07:57:58 +0000377
Jim Grosbach61425c02012-01-16 22:26:39 +0000378 // Now store the relocation information. Associate it with the source
379 // symbol. Just add it to the unresolved list and let the general
380 // path post-load resolve it if we know where the symbol is.
381 UnresolvedRelocations[SourceName].push_back(RelocationEntry(SectNum,
382 Offset,
383 RE->Word1,
384 0 /*Addend*/));
385 DEBUG(dbgs() << "Relocation at Section #" << SectNum << " + " << Offset
386 << " from '" << SourceName << "(Word1: "
387 << format("0x%x", RE->Word1) << ")\n");
388 }
Danil Malyshevcf852dc2011-07-13 07:57:58 +0000389 }
390 }
Jim Grosbach61425c02012-01-16 22:26:39 +0000391
392 // Resolve the addresses of any symbols that were defined in this segment.
393 for (int i = 0, e = SymbolNames.size(); i != e; ++i)
394 resolveSymbol(SymbolNames[i]);
395
Danil Malyshevcf852dc2011-07-13 07:57:58 +0000396 return false;
397}
398
Jim Grosbach61425c02012-01-16 22:26:39 +0000399bool RuntimeDyldMachO::
400processSymbols32(const MachOObject *Obj,
401 SmallVectorImpl<unsigned> &SectionMap,
402 SmallVectorImpl<StringRef> &SymbolNames,
403 const InMemoryStruct<macho::SymtabLoadCommand> &SymtabLC) {
404 // FIXME: Combine w/ processSymbols64. Factor 64/32 datatype and such.
405 for (unsigned i = 0; i != SymtabLC->NumSymbolTableEntries; ++i) {
406 InMemoryStruct<macho::SymbolTableEntry> STE;
407 Obj->ReadSymbolTableEntry(SymtabLC->SymbolTableOffset, i, STE);
408 if (!STE)
409 return Error("unable to read symbol: '" + Twine(i) + "'");
410 // Get the symbol name.
411 StringRef Name = Obj->getStringAtIndex(STE->StringIndex);
412 SymbolNames.push_back(Name);
413
414 // FIXME: Check the symbol type and flags.
415 if (STE->Type != 0xF) // external, defined in this segment.
416 continue;
417 // Flags in the upper nibble we don't care about.
418 if ((STE->Flags & 0xf) != 0x0)
419 continue;
420
421 // Remember the symbol.
422 uint32_t SectionID = SectionMap[STE->SectionIndex - 1];
423 SymbolTable[Name] = SymbolLoc(SectionID, STE->Value);
424
425 DEBUG(dbgs() << "Symbol: '" << Name << "' @ "
426 << (getSectionAddress(SectionID) + STE->Value)
427 << "\n");
428 }
429 return false;
430}
431
432bool RuntimeDyldMachO::
433processSymbols64(const MachOObject *Obj,
434 SmallVectorImpl<unsigned> &SectionMap,
435 SmallVectorImpl<StringRef> &SymbolNames,
436 const InMemoryStruct<macho::SymtabLoadCommand> &SymtabLC) {
437 for (unsigned i = 0; i != SymtabLC->NumSymbolTableEntries; ++i) {
438 InMemoryStruct<macho::Symbol64TableEntry> STE;
439 Obj->ReadSymbol64TableEntry(SymtabLC->SymbolTableOffset, i, STE);
440 if (!STE)
441 return Error("unable to read symbol: '" + Twine(i) + "'");
442 // Get the symbol name.
443 StringRef Name = Obj->getStringAtIndex(STE->StringIndex);
444 SymbolNames.push_back(Name);
445
446 // FIXME: Check the symbol type and flags.
447 if (STE->Type != 0xF) // external, defined in this segment.
448 continue;
449 // Flags in the upper nibble we don't care about.
450 if ((STE->Flags & 0xf) != 0x0)
451 continue;
452
453 // Remember the symbol.
454 uint32_t SectionID = SectionMap[STE->SectionIndex - 1];
455 SymbolTable[Name] = SymbolLoc(SectionID, STE->Value);
456
457 DEBUG(dbgs() << "Symbol: '" << Name << "' @ "
458 << (getSectionAddress(SectionID) + STE->Value)
459 << "\n");
460 }
461 return false;
462}
463
464// resolveSymbol - Resolve any relocations to the specified symbol if
465// we know where it lives.
466void RuntimeDyldMachO::resolveSymbol(StringRef Name) {
467 StringMap<SymbolLoc>::const_iterator Loc = SymbolTable.find(Name);
468 if (Loc == SymbolTable.end())
469 return;
470
471 RelocationList &Relocs = UnresolvedRelocations[Name];
472 DEBUG(dbgs() << "Resolving symbol '" << Name << "'\n");
473 for (int i = 0, e = Relocs.size(); i != e; ++i) {
474 // Change the relocation to be section relative rather than symbol
475 // relative and move it to the resolved relocation list.
476 RelocationEntry Entry = Relocs[i];
477 Entry.Addend += Loc->second.second;
478 Relocations[Loc->second.first].push_back(Entry);
479 }
480 // FIXME: Keep a worklist of the relocations we've added so that we can
481 // resolve more selectively later.
482 Relocs.clear();
483}
484
Danil Malyshevcf852dc2011-07-13 07:57:58 +0000485bool RuntimeDyldMachO::loadObject(MemoryBuffer *InputBuffer) {
486 // If the linker is in an error state, don't do anything.
487 if (hasError())
488 return true;
489 // Load the Mach-O wrapper object.
490 std::string ErrorStr;
491 OwningPtr<MachOObject> Obj(
492 MachOObject::LoadFromBuffer(InputBuffer, &ErrorStr));
493 if (!Obj)
494 return Error("unable to load object: '" + ErrorStr + "'");
495
496 // Get the CPU type information from the header.
497 const macho::Header &Header = Obj->getHeader();
498
499 // FIXME: Error checking that the loaded object is compatible with
500 // the system we're running on.
501 CPUType = Header.CPUType;
502 CPUSubtype = Header.CPUSubtype;
503
504 // Validate that the load commands match what we expect.
505 const MachOObject::LoadCommandInfo *SegmentLCI = 0, *SymtabLCI = 0,
506 *DysymtabLCI = 0;
507 for (unsigned i = 0; i != Header.NumLoadCommands; ++i) {
508 const MachOObject::LoadCommandInfo &LCI = Obj->getLoadCommandInfo(i);
509 switch (LCI.Command.Type) {
510 case macho::LCT_Segment:
511 case macho::LCT_Segment64:
512 if (SegmentLCI)
513 return Error("unexpected input object (multiple segments)");
514 SegmentLCI = &LCI;
515 break;
516 case macho::LCT_Symtab:
517 if (SymtabLCI)
518 return Error("unexpected input object (multiple symbol tables)");
519 SymtabLCI = &LCI;
520 break;
521 case macho::LCT_Dysymtab:
522 if (DysymtabLCI)
523 return Error("unexpected input object (multiple symbol tables)");
524 DysymtabLCI = &LCI;
525 break;
526 default:
527 return Error("unexpected input object (unexpected load command");
528 }
529 }
530
531 if (!SymtabLCI)
532 return Error("no symbol table found in object");
533 if (!SegmentLCI)
Eli Bendersky1eb189b2012-01-06 07:49:17 +0000534 return Error("no segments found in object");
Danil Malyshevcf852dc2011-07-13 07:57:58 +0000535
536 // Read and register the symbol table data.
537 InMemoryStruct<macho::SymtabLoadCommand> SymtabLC;
538 Obj->ReadSymtabLoadCommand(*SymtabLCI, SymtabLC);
539 if (!SymtabLC)
540 return Error("unable to load symbol table load command");
541 Obj->RegisterStringTable(*SymtabLC);
542
543 // Read the dynamic link-edit information, if present (not present in static
544 // objects).
545 if (DysymtabLCI) {
546 InMemoryStruct<macho::DysymtabLoadCommand> DysymtabLC;
547 Obj->ReadDysymtabLoadCommand(*DysymtabLCI, DysymtabLC);
548 if (!DysymtabLC)
549 return Error("unable to load dynamic link-exit load command");
550
551 // FIXME: We don't support anything interesting yet.
552// if (DysymtabLC->LocalSymbolsIndex != 0)
553// return Error("NOT YET IMPLEMENTED: local symbol entries");
554// if (DysymtabLC->ExternalSymbolsIndex != 0)
555// return Error("NOT YET IMPLEMENTED: non-external symbol entries");
556// if (DysymtabLC->UndefinedSymbolsIndex != SymtabLC->NumSymbolTableEntries)
557// return Error("NOT YET IMPLEMENTED: undefined symbol entries");
558 }
559
560 // Load the segment load command.
561 if (SegmentLCI->Command.Type == macho::LCT_Segment) {
562 if (loadSegment32(Obj.get(), SegmentLCI, SymtabLC))
563 return true;
564 } else {
565 if (loadSegment64(Obj.get(), SegmentLCI, SymtabLC))
566 return true;
567 }
568
Jim Grosbach61425c02012-01-16 22:26:39 +0000569 // Assign the addresses of the sections from the object so that any
570 // relocations to them get set properly.
571 // FIXME: This is done directly from the client at the moment. We should
572 // default the values to the local storage, at least when the target arch
573 // is the same as the host arch.
574
Danil Malyshevcf852dc2011-07-13 07:57:58 +0000575 return false;
576}
577
578// Assign an address to a symbol name and resolve all the relocations
579// associated with it.
Jim Grosbach61425c02012-01-16 22:26:39 +0000580void RuntimeDyldMachO::reassignSectionAddress(unsigned SectionID,
581 uint64_t Addr) {
582 // The address to use for relocation resolution is not
583 // the address of the local section buffer. We must be doing
584 // a remote execution environment of some sort. Re-apply any
585 // relocations referencing this section with the given address.
586 //
587 // Addr is a uint64_t because we can't assume the pointer width
588 // of the target is the same as that of the host. Just use a generic
589 // "big enough" type.
Danil Malyshevcf852dc2011-07-13 07:57:58 +0000590
Jim Grosbach61425c02012-01-16 22:26:39 +0000591 SectionLoadAddress[SectionID] = Addr;
592
593 RelocationList &Relocs = Relocations[SectionID];
Danil Malyshevcf852dc2011-07-13 07:57:58 +0000594 for (unsigned i = 0, e = Relocs.size(); i != e; ++i) {
595 RelocationEntry &RE = Relocs[i];
Jim Grosbach61425c02012-01-16 22:26:39 +0000596 uint8_t *Target = (uint8_t*)Sections[RE.SectionID].base() + RE.Offset;
Sean Callanan61dfa772012-03-07 23:05:25 +0000597 uint64_t FinalTarget = (uint64_t)SectionLoadAddress[RE.SectionID] + RE.Offset;
Danil Malyshevcf852dc2011-07-13 07:57:58 +0000598 bool isPCRel = (RE.Data >> 24) & 1;
599 unsigned Type = (RE.Data >> 28) & 0xf;
600 unsigned Size = 1 << ((RE.Data >> 25) & 3);
601
Jim Grosbach61425c02012-01-16 22:26:39 +0000602 DEBUG(dbgs() << "Resolving relocation at Section #" << RE.SectionID
603 << " + " << RE.Offset << " (" << format("%p", Target) << ")"
604 << " from Section #" << SectionID << " (" << format("%p", Addr) << ")"
Danil Malyshevcf852dc2011-07-13 07:57:58 +0000605 << "(" << (isPCRel ? "pcrel" : "absolute")
Jim Grosbach61425c02012-01-16 22:26:39 +0000606 << ", type: " << Type << ", Size: " << Size << ", Addend: "
607 << RE.Addend << ").\n");
Danil Malyshevcf852dc2011-07-13 07:57:58 +0000608
Sean Callanan61dfa772012-03-07 23:05:25 +0000609 resolveRelocation(Target,
610 FinalTarget,
611 Addr,
612 isPCRel,
613 Type,
614 Size,
615 RE.Addend);
Danil Malyshevcf852dc2011-07-13 07:57:58 +0000616 }
617}
618
619bool RuntimeDyldMachO::isKnownFormat(const MemoryBuffer *InputBuffer) {
620 StringRef Magic = InputBuffer->getBuffer().slice(0, 4);
621 if (Magic == "\xFE\xED\xFA\xCE") return true;
622 if (Magic == "\xCE\xFA\xED\xFE") return true;
623 if (Magic == "\xFE\xED\xFA\xCF") return true;
624 if (Magic == "\xCF\xFA\xED\xFE") return true;
625 return false;
626}
627
628} // end namespace llvm