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