Add a SymbolRef::getValue.
[oota-llvm.git] / lib / Object / MachOObjectFile.cpp
1 //===- MachOObjectFile.cpp - Mach-O object file binding ---------*- 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 // This file defines the MachOObjectFile class, which binds the MachOObject
11 // class to the generic ObjectFile wrapper.
12 //
13 //===----------------------------------------------------------------------===//
14
15 #include "llvm/Object/MachO.h"
16 #include "llvm/ADT/STLExtras.h"
17 #include "llvm/ADT/StringSwitch.h"
18 #include "llvm/ADT/Triple.h"
19 #include "llvm/Support/DataExtractor.h"
20 #include "llvm/Support/Debug.h"
21 #include "llvm/Support/Format.h"
22 #include "llvm/Support/Host.h"
23 #include "llvm/Support/LEB128.h"
24 #include "llvm/Support/MachO.h"
25 #include "llvm/Support/MemoryBuffer.h"
26 #include "llvm/Support/raw_ostream.h"
27 #include <cctype>
28 #include <cstring>
29 #include <limits>
30
31 using namespace llvm;
32 using namespace object;
33
34 namespace {
35   struct section_base {
36     char sectname[16];
37     char segname[16];
38   };
39 }
40
41 // FIXME: Replace all uses of this function with getStructOrErr.
42 template <typename T>
43 static T getStruct(const MachOObjectFile *O, const char *P) {
44   // Don't read before the beginning or past the end of the file
45   if (P < O->getData().begin() || P + sizeof(T) > O->getData().end())
46     report_fatal_error("Malformed MachO file.");
47
48   T Cmd;
49   memcpy(&Cmd, P, sizeof(T));
50   if (O->isLittleEndian() != sys::IsLittleEndianHost)
51     MachO::swapStruct(Cmd);
52   return Cmd;
53 }
54
55 template <typename T>
56 static ErrorOr<T> getStructOrErr(const MachOObjectFile *O, const char *P) {
57   // Don't read before the beginning or past the end of the file
58   if (P < O->getData().begin() || P + sizeof(T) > O->getData().end())
59     return object_error::parse_failed;
60
61   T Cmd;
62   memcpy(&Cmd, P, sizeof(T));
63   if (O->isLittleEndian() != sys::IsLittleEndianHost)
64     MachO::swapStruct(Cmd);
65   return Cmd;
66 }
67
68 static const char *
69 getSectionPtr(const MachOObjectFile *O, MachOObjectFile::LoadCommandInfo L,
70               unsigned Sec) {
71   uintptr_t CommandAddr = reinterpret_cast<uintptr_t>(L.Ptr);
72
73   bool Is64 = O->is64Bit();
74   unsigned SegmentLoadSize = Is64 ? sizeof(MachO::segment_command_64) :
75                                     sizeof(MachO::segment_command);
76   unsigned SectionSize = Is64 ? sizeof(MachO::section_64) :
77                                 sizeof(MachO::section);
78
79   uintptr_t SectionAddr = CommandAddr + SegmentLoadSize + Sec * SectionSize;
80   return reinterpret_cast<const char*>(SectionAddr);
81 }
82
83 static const char *getPtr(const MachOObjectFile *O, size_t Offset) {
84   return O->getData().substr(Offset, 1).data();
85 }
86
87 static MachO::nlist_base
88 getSymbolTableEntryBase(const MachOObjectFile *O, DataRefImpl DRI) {
89   const char *P = reinterpret_cast<const char *>(DRI.p);
90   return getStruct<MachO::nlist_base>(O, P);
91 }
92
93 static StringRef parseSegmentOrSectionName(const char *P) {
94   if (P[15] == 0)
95     // Null terminated.
96     return P;
97   // Not null terminated, so this is a 16 char string.
98   return StringRef(P, 16);
99 }
100
101 // Helper to advance a section or symbol iterator multiple increments at a time.
102 template<class T>
103 static void advance(T &it, size_t Val) {
104   while (Val--)
105     ++it;
106 }
107
108 static unsigned getCPUType(const MachOObjectFile *O) {
109   return O->getHeader().cputype;
110 }
111
112 static uint32_t
113 getPlainRelocationAddress(const MachO::any_relocation_info &RE) {
114   return RE.r_word0;
115 }
116
117 static unsigned
118 getScatteredRelocationAddress(const MachO::any_relocation_info &RE) {
119   return RE.r_word0 & 0xffffff;
120 }
121
122 static bool getPlainRelocationPCRel(const MachOObjectFile *O,
123                                     const MachO::any_relocation_info &RE) {
124   if (O->isLittleEndian())
125     return (RE.r_word1 >> 24) & 1;
126   return (RE.r_word1 >> 7) & 1;
127 }
128
129 static bool
130 getScatteredRelocationPCRel(const MachOObjectFile *O,
131                             const MachO::any_relocation_info &RE) {
132   return (RE.r_word0 >> 30) & 1;
133 }
134
135 static unsigned getPlainRelocationLength(const MachOObjectFile *O,
136                                          const MachO::any_relocation_info &RE) {
137   if (O->isLittleEndian())
138     return (RE.r_word1 >> 25) & 3;
139   return (RE.r_word1 >> 5) & 3;
140 }
141
142 static unsigned
143 getScatteredRelocationLength(const MachO::any_relocation_info &RE) {
144   return (RE.r_word0 >> 28) & 3;
145 }
146
147 static unsigned getPlainRelocationType(const MachOObjectFile *O,
148                                        const MachO::any_relocation_info &RE) {
149   if (O->isLittleEndian())
150     return RE.r_word1 >> 28;
151   return RE.r_word1 & 0xf;
152 }
153
154 static uint32_t getSectionFlags(const MachOObjectFile *O,
155                                 DataRefImpl Sec) {
156   if (O->is64Bit()) {
157     MachO::section_64 Sect = O->getSection64(Sec);
158     return Sect.flags;
159   }
160   MachO::section Sect = O->getSection(Sec);
161   return Sect.flags;
162 }
163
164 static ErrorOr<MachOObjectFile::LoadCommandInfo>
165 getLoadCommandInfo(const MachOObjectFile *Obj, const char *Ptr) {
166   auto CmdOrErr = getStructOrErr<MachO::load_command>(Obj, Ptr);
167   if (!CmdOrErr)
168     return CmdOrErr.getError();
169   if (CmdOrErr->cmdsize < 8)
170     return object_error::macho_small_load_command;
171   MachOObjectFile::LoadCommandInfo Load;
172   Load.Ptr = Ptr;
173   Load.C = CmdOrErr.get();
174   return Load;
175 }
176
177 static ErrorOr<MachOObjectFile::LoadCommandInfo>
178 getFirstLoadCommandInfo(const MachOObjectFile *Obj) {
179   unsigned HeaderSize = Obj->is64Bit() ? sizeof(MachO::mach_header_64)
180                                        : sizeof(MachO::mach_header);
181   return getLoadCommandInfo(Obj, getPtr(Obj, HeaderSize));
182 }
183
184 static ErrorOr<MachOObjectFile::LoadCommandInfo>
185 getNextLoadCommandInfo(const MachOObjectFile *Obj,
186                        const MachOObjectFile::LoadCommandInfo &L) {
187   return getLoadCommandInfo(Obj, L.Ptr + L.C.cmdsize);
188 }
189
190 template <typename T>
191 static void parseHeader(const MachOObjectFile *Obj, T &Header,
192                         std::error_code &EC) {
193   auto HeaderOrErr = getStructOrErr<T>(Obj, getPtr(Obj, 0));
194   if (HeaderOrErr)
195     Header = HeaderOrErr.get();
196   else
197     EC = HeaderOrErr.getError();
198 }
199
200 // Parses LC_SEGMENT or LC_SEGMENT_64 load command, adds addresses of all
201 // sections to \param Sections, and optionally sets
202 // \param IsPageZeroSegment to true.
203 template <typename SegmentCmd>
204 static std::error_code parseSegmentLoadCommand(
205     const MachOObjectFile *Obj, const MachOObjectFile::LoadCommandInfo &Load,
206     SmallVectorImpl<const char *> &Sections, bool &IsPageZeroSegment) {
207   const unsigned SegmentLoadSize = sizeof(SegmentCmd);
208   if (Load.C.cmdsize < SegmentLoadSize)
209     return object_error::macho_load_segment_too_small;
210   auto SegOrErr = getStructOrErr<SegmentCmd>(Obj, Load.Ptr);
211   if (!SegOrErr)
212     return SegOrErr.getError();
213   SegmentCmd S = SegOrErr.get();
214   const unsigned SectionSize =
215       Obj->is64Bit() ? sizeof(MachO::section_64) : sizeof(MachO::section);
216   if (S.nsects > std::numeric_limits<uint32_t>::max() / SectionSize ||
217       S.nsects * SectionSize > Load.C.cmdsize - SegmentLoadSize)
218     return object_error::macho_load_segment_too_many_sections;
219   for (unsigned J = 0; J < S.nsects; ++J) {
220     const char *Sec = getSectionPtr(Obj, Load, J);
221     Sections.push_back(Sec);
222   }
223   IsPageZeroSegment |= StringRef("__PAGEZERO").equals(S.segname);
224   return std::error_code();
225 }
226
227 MachOObjectFile::MachOObjectFile(MemoryBufferRef Object, bool IsLittleEndian,
228                                  bool Is64bits, std::error_code &EC)
229     : ObjectFile(getMachOType(IsLittleEndian, Is64bits), Object),
230       SymtabLoadCmd(nullptr), DysymtabLoadCmd(nullptr),
231       DataInCodeLoadCmd(nullptr), LinkOptHintsLoadCmd(nullptr),
232       DyldInfoLoadCmd(nullptr), UuidLoadCmd(nullptr),
233       HasPageZeroSegment(false) {
234   if (is64Bit())
235     parseHeader(this, Header64, EC);
236   else
237     parseHeader(this, Header, EC);
238   if (EC)
239     return;
240
241   uint32_t LoadCommandCount = getHeader().ncmds;
242   if (LoadCommandCount == 0)
243     return;
244
245   auto LoadOrErr = getFirstLoadCommandInfo(this);
246   if (!LoadOrErr) {
247     EC = LoadOrErr.getError();
248     return;
249   }
250   LoadCommandInfo Load = LoadOrErr.get();
251   for (unsigned I = 0; I < LoadCommandCount; ++I) {
252     LoadCommands.push_back(Load);
253     if (Load.C.cmd == MachO::LC_SYMTAB) {
254       // Multiple symbol tables
255       if (SymtabLoadCmd) {
256         EC = object_error::parse_failed;
257         return;
258       }
259       SymtabLoadCmd = Load.Ptr;
260     } else if (Load.C.cmd == MachO::LC_DYSYMTAB) {
261       // Multiple dynamic symbol tables
262       if (DysymtabLoadCmd) {
263         EC = object_error::parse_failed;
264         return;
265       }
266       DysymtabLoadCmd = Load.Ptr;
267     } else if (Load.C.cmd == MachO::LC_DATA_IN_CODE) {
268       // Multiple data in code tables
269       if (DataInCodeLoadCmd) {
270         EC = object_error::parse_failed;
271         return;
272       }
273       DataInCodeLoadCmd = Load.Ptr;
274     } else if (Load.C.cmd == MachO::LC_LINKER_OPTIMIZATION_HINT) {
275       // Multiple linker optimization hint tables
276       if (LinkOptHintsLoadCmd) {
277         EC = object_error::parse_failed;
278         return;
279       }
280       LinkOptHintsLoadCmd = Load.Ptr;
281     } else if (Load.C.cmd == MachO::LC_DYLD_INFO || 
282                Load.C.cmd == MachO::LC_DYLD_INFO_ONLY) {
283       // Multiple dyldinfo load commands
284       if (DyldInfoLoadCmd) {
285         EC = object_error::parse_failed;
286         return;
287       }
288       DyldInfoLoadCmd = Load.Ptr;
289     } else if (Load.C.cmd == MachO::LC_UUID) {
290       // Multiple UUID load commands
291       if (UuidLoadCmd) {
292         EC = object_error::parse_failed;
293         return;
294       }
295       UuidLoadCmd = Load.Ptr;
296     } else if (Load.C.cmd == MachO::LC_SEGMENT_64) {
297       if ((EC = parseSegmentLoadCommand<MachO::segment_command_64>(
298                this, Load, Sections, HasPageZeroSegment)))
299         return;
300     } else if (Load.C.cmd == MachO::LC_SEGMENT) {
301       if ((EC = parseSegmentLoadCommand<MachO::segment_command>(
302                this, Load, Sections, HasPageZeroSegment)))
303         return;
304     } else if (Load.C.cmd == MachO::LC_LOAD_DYLIB ||
305                Load.C.cmd == MachO::LC_LOAD_WEAK_DYLIB ||
306                Load.C.cmd == MachO::LC_LAZY_LOAD_DYLIB ||
307                Load.C.cmd == MachO::LC_REEXPORT_DYLIB ||
308                Load.C.cmd == MachO::LC_LOAD_UPWARD_DYLIB) {
309       Libraries.push_back(Load.Ptr);
310     }
311     if (I < LoadCommandCount - 1) {
312       auto LoadOrErr = getNextLoadCommandInfo(this, Load);
313       if (!LoadOrErr) {
314         EC = LoadOrErr.getError();
315         return;
316       }
317       Load = LoadOrErr.get();
318     }
319   }
320   assert(LoadCommands.size() == LoadCommandCount);
321 }
322
323 void MachOObjectFile::moveSymbolNext(DataRefImpl &Symb) const {
324   unsigned SymbolTableEntrySize = is64Bit() ?
325     sizeof(MachO::nlist_64) :
326     sizeof(MachO::nlist);
327   Symb.p += SymbolTableEntrySize;
328 }
329
330 std::error_code MachOObjectFile::getSymbolName(DataRefImpl Symb,
331                                                StringRef &Res) const {
332   StringRef StringTable = getStringTableData();
333   MachO::nlist_base Entry = getSymbolTableEntryBase(this, Symb);
334   const char *Start = &StringTable.data()[Entry.n_strx];
335   if (Start < getData().begin() || Start >= getData().end())
336     report_fatal_error(
337         "Symbol name entry points before beginning or past end of file.");
338   Res = StringRef(Start);
339   return std::error_code();
340 }
341
342 unsigned MachOObjectFile::getSectionType(SectionRef Sec) const {
343   DataRefImpl DRI = Sec.getRawDataRefImpl();
344   uint32_t Flags = getSectionFlags(this, DRI);
345   return Flags & MachO::SECTION_TYPE;
346 }
347
348 uint64_t MachOObjectFile::getNValue(DataRefImpl Sym) const {
349   if (is64Bit()) {
350     MachO::nlist_64 Entry = getSymbol64TableEntry(Sym);
351     return Entry.n_value;
352   }
353   MachO::nlist Entry = getSymbolTableEntry(Sym);
354   return Entry.n_value;
355 }
356
357 // getIndirectName() returns the name of the alias'ed symbol who's string table
358 // index is in the n_value field.
359 std::error_code MachOObjectFile::getIndirectName(DataRefImpl Symb,
360                                                  StringRef &Res) const {
361   StringRef StringTable = getStringTableData();
362   MachO::nlist_base Entry = getSymbolTableEntryBase(this, Symb);
363   if ((Entry.n_type & MachO::N_TYPE) != MachO::N_INDR)
364     return object_error::parse_failed;
365   uint64_t NValue = getNValue(Symb);
366   if (NValue >= StringTable.size())
367     return object_error::parse_failed;
368   const char *Start = &StringTable.data()[NValue];
369   Res = StringRef(Start);
370   return std::error_code();
371 }
372
373 uint64_t MachOObjectFile::getSymbolValue(DataRefImpl Sym) const {
374   uint64_t NValue = getNValue(Sym);
375   MachO::nlist_base Entry = getSymbolTableEntryBase(this, Sym);
376   if ((Entry.n_type & MachO::N_TYPE) == MachO::N_UNDF && NValue == 0)
377     return UnknownAddress;
378   return NValue;
379 }
380
381 std::error_code MachOObjectFile::getSymbolAddress(DataRefImpl Sym,
382                                                   uint64_t &Res) const {
383   Res = getSymbolValue(Sym);
384   return std::error_code();
385 }
386
387 uint32_t MachOObjectFile::getSymbolAlignment(DataRefImpl DRI) const {
388   uint32_t flags = getSymbolFlags(DRI);
389   if (flags & SymbolRef::SF_Common) {
390     MachO::nlist_base Entry = getSymbolTableEntryBase(this, DRI);
391     return 1 << MachO::GET_COMM_ALIGN(Entry.n_desc);
392   }
393   return 0;
394 }
395
396 uint64_t MachOObjectFile::getCommonSymbolSizeImpl(DataRefImpl DRI) const {
397   uint64_t Value;
398   getSymbolAddress(DRI, Value);
399   return Value;
400 }
401
402 std::error_code MachOObjectFile::getSymbolType(DataRefImpl Symb,
403                                                SymbolRef::Type &Res) const {
404   MachO::nlist_base Entry = getSymbolTableEntryBase(this, Symb);
405   uint8_t n_type = Entry.n_type;
406
407   Res = SymbolRef::ST_Other;
408
409   // If this is a STAB debugging symbol, we can do nothing more.
410   if (n_type & MachO::N_STAB) {
411     Res = SymbolRef::ST_Debug;
412     return std::error_code();
413   }
414
415   switch (n_type & MachO::N_TYPE) {
416     case MachO::N_UNDF :
417       Res = SymbolRef::ST_Unknown;
418       break;
419     case MachO::N_SECT :
420       Res = SymbolRef::ST_Function;
421       break;
422   }
423   return std::error_code();
424 }
425
426 uint32_t MachOObjectFile::getSymbolFlags(DataRefImpl DRI) const {
427   MachO::nlist_base Entry = getSymbolTableEntryBase(this, DRI);
428
429   uint8_t MachOType = Entry.n_type;
430   uint16_t MachOFlags = Entry.n_desc;
431
432   uint32_t Result = SymbolRef::SF_None;
433
434   if ((MachOType & MachO::N_TYPE) == MachO::N_UNDF)
435     Result |= SymbolRef::SF_Undefined;
436
437   if ((MachOType & MachO::N_TYPE) == MachO::N_INDR)
438     Result |= SymbolRef::SF_Indirect;
439
440   if (MachOType & MachO::N_STAB)
441     Result |= SymbolRef::SF_FormatSpecific;
442
443   if (MachOType & MachO::N_EXT) {
444     Result |= SymbolRef::SF_Global;
445     if ((MachOType & MachO::N_TYPE) == MachO::N_UNDF) {
446       uint64_t Value;
447       getSymbolAddress(DRI, Value);
448       if (Value && Value != UnknownAddress)
449         Result |= SymbolRef::SF_Common;
450     }
451
452     if (!(MachOType & MachO::N_PEXT))
453       Result |= SymbolRef::SF_Exported;
454   }
455
456   if (MachOFlags & (MachO::N_WEAK_REF | MachO::N_WEAK_DEF))
457     Result |= SymbolRef::SF_Weak;
458
459   if (MachOFlags & (MachO::N_ARM_THUMB_DEF))
460     Result |= SymbolRef::SF_Thumb;
461
462   if ((MachOType & MachO::N_TYPE) == MachO::N_ABS)
463     Result |= SymbolRef::SF_Absolute;
464
465   return Result;
466 }
467
468 std::error_code MachOObjectFile::getSymbolSection(DataRefImpl Symb,
469                                                   section_iterator &Res) const {
470   MachO::nlist_base Entry = getSymbolTableEntryBase(this, Symb);
471   uint8_t index = Entry.n_sect;
472
473   if (index == 0) {
474     Res = section_end();
475   } else {
476     DataRefImpl DRI;
477     DRI.d.a = index - 1;
478     if (DRI.d.a >= Sections.size())
479       report_fatal_error("getSymbolSection: Invalid section index.");
480     Res = section_iterator(SectionRef(DRI, this));
481   }
482
483   return std::error_code();
484 }
485
486 void MachOObjectFile::moveSectionNext(DataRefImpl &Sec) const {
487   Sec.d.a++;
488 }
489
490 std::error_code MachOObjectFile::getSectionName(DataRefImpl Sec,
491                                                 StringRef &Result) const {
492   ArrayRef<char> Raw = getSectionRawName(Sec);
493   Result = parseSegmentOrSectionName(Raw.data());
494   return std::error_code();
495 }
496
497 uint64_t MachOObjectFile::getSectionAddress(DataRefImpl Sec) const {
498   if (is64Bit())
499     return getSection64(Sec).addr;
500   return getSection(Sec).addr;
501 }
502
503 uint64_t MachOObjectFile::getSectionSize(DataRefImpl Sec) const {
504   if (is64Bit())
505     return getSection64(Sec).size;
506   return getSection(Sec).size;
507 }
508
509 std::error_code MachOObjectFile::getSectionContents(DataRefImpl Sec,
510                                                     StringRef &Res) const {
511   uint32_t Offset;
512   uint64_t Size;
513
514   if (is64Bit()) {
515     MachO::section_64 Sect = getSection64(Sec);
516     Offset = Sect.offset;
517     Size = Sect.size;
518   } else {
519     MachO::section Sect = getSection(Sec);
520     Offset = Sect.offset;
521     Size = Sect.size;
522   }
523
524   Res = this->getData().substr(Offset, Size);
525   return std::error_code();
526 }
527
528 uint64_t MachOObjectFile::getSectionAlignment(DataRefImpl Sec) const {
529   uint32_t Align;
530   if (is64Bit()) {
531     MachO::section_64 Sect = getSection64(Sec);
532     Align = Sect.align;
533   } else {
534     MachO::section Sect = getSection(Sec);
535     Align = Sect.align;
536   }
537
538   return uint64_t(1) << Align;
539 }
540
541 bool MachOObjectFile::isSectionText(DataRefImpl Sec) const {
542   uint32_t Flags = getSectionFlags(this, Sec);
543   return Flags & MachO::S_ATTR_PURE_INSTRUCTIONS;
544 }
545
546 bool MachOObjectFile::isSectionData(DataRefImpl Sec) const {
547   uint32_t Flags = getSectionFlags(this, Sec);
548   unsigned SectionType = Flags & MachO::SECTION_TYPE;
549   return !(Flags & MachO::S_ATTR_PURE_INSTRUCTIONS) &&
550          !(SectionType == MachO::S_ZEROFILL ||
551            SectionType == MachO::S_GB_ZEROFILL);
552 }
553
554 bool MachOObjectFile::isSectionBSS(DataRefImpl Sec) const {
555   uint32_t Flags = getSectionFlags(this, Sec);
556   unsigned SectionType = Flags & MachO::SECTION_TYPE;
557   return !(Flags & MachO::S_ATTR_PURE_INSTRUCTIONS) &&
558          (SectionType == MachO::S_ZEROFILL ||
559           SectionType == MachO::S_GB_ZEROFILL);
560 }
561
562 bool MachOObjectFile::isSectionVirtual(DataRefImpl Sec) const {
563   // FIXME: Unimplemented.
564   return false;
565 }
566
567 bool MachOObjectFile::sectionContainsSymbol(DataRefImpl Sec,
568                                             DataRefImpl Symb) const {
569   SymbolRef::Type ST;
570   this->getSymbolType(Symb, ST);
571   if (ST == SymbolRef::ST_Unknown)
572     return false;
573
574   uint64_t SectBegin = getSectionAddress(Sec);
575   uint64_t SectEnd = getSectionSize(Sec);
576   SectEnd += SectBegin;
577
578   uint64_t SymAddr;
579   getSymbolAddress(Symb, SymAddr);
580   return (SymAddr >= SectBegin) && (SymAddr < SectEnd);
581 }
582
583 relocation_iterator MachOObjectFile::section_rel_begin(DataRefImpl Sec) const {
584   DataRefImpl Ret;
585   Ret.d.a = Sec.d.a;
586   Ret.d.b = 0;
587   return relocation_iterator(RelocationRef(Ret, this));
588 }
589
590 relocation_iterator
591 MachOObjectFile::section_rel_end(DataRefImpl Sec) const {
592   uint32_t Num;
593   if (is64Bit()) {
594     MachO::section_64 Sect = getSection64(Sec);
595     Num = Sect.nreloc;
596   } else {
597     MachO::section Sect = getSection(Sec);
598     Num = Sect.nreloc;
599   }
600
601   DataRefImpl Ret;
602   Ret.d.a = Sec.d.a;
603   Ret.d.b = Num;
604   return relocation_iterator(RelocationRef(Ret, this));
605 }
606
607 void MachOObjectFile::moveRelocationNext(DataRefImpl &Rel) const {
608   ++Rel.d.b;
609 }
610
611 std::error_code MachOObjectFile::getRelocationAddress(DataRefImpl Rel,
612                                                       uint64_t &Res) const {
613   uint64_t Offset;
614   getRelocationOffset(Rel, Offset);
615
616   DataRefImpl Sec;
617   Sec.d.a = Rel.d.a;
618   uint64_t SecAddress = getSectionAddress(Sec);
619   Res = SecAddress + Offset;
620   return std::error_code();
621 }
622
623 std::error_code MachOObjectFile::getRelocationOffset(DataRefImpl Rel,
624                                                      uint64_t &Res) const {
625   assert(getHeader().filetype == MachO::MH_OBJECT &&
626          "Only implemented for MH_OBJECT");
627   MachO::any_relocation_info RE = getRelocation(Rel);
628   Res = getAnyRelocationAddress(RE);
629   return std::error_code();
630 }
631
632 symbol_iterator
633 MachOObjectFile::getRelocationSymbol(DataRefImpl Rel) const {
634   MachO::any_relocation_info RE = getRelocation(Rel);
635   if (isRelocationScattered(RE))
636     return symbol_end();
637
638   uint32_t SymbolIdx = getPlainRelocationSymbolNum(RE);
639   bool isExtern = getPlainRelocationExternal(RE);
640   if (!isExtern)
641     return symbol_end();
642
643   MachO::symtab_command S = getSymtabLoadCommand();
644   unsigned SymbolTableEntrySize = is64Bit() ?
645     sizeof(MachO::nlist_64) :
646     sizeof(MachO::nlist);
647   uint64_t Offset = S.symoff + SymbolIdx * SymbolTableEntrySize;
648   DataRefImpl Sym;
649   Sym.p = reinterpret_cast<uintptr_t>(getPtr(this, Offset));
650   return symbol_iterator(SymbolRef(Sym, this));
651 }
652
653 section_iterator
654 MachOObjectFile::getRelocationSection(DataRefImpl Rel) const {
655   return section_iterator(getAnyRelocationSection(getRelocation(Rel)));
656 }
657
658 std::error_code MachOObjectFile::getRelocationType(DataRefImpl Rel,
659                                                    uint64_t &Res) const {
660   MachO::any_relocation_info RE = getRelocation(Rel);
661   Res = getAnyRelocationType(RE);
662   return std::error_code();
663 }
664
665 std::error_code
666 MachOObjectFile::getRelocationTypeName(DataRefImpl Rel,
667                                        SmallVectorImpl<char> &Result) const {
668   StringRef res;
669   uint64_t RType;
670   getRelocationType(Rel, RType);
671
672   unsigned Arch = this->getArch();
673
674   switch (Arch) {
675     case Triple::x86: {
676       static const char *const Table[] =  {
677         "GENERIC_RELOC_VANILLA",
678         "GENERIC_RELOC_PAIR",
679         "GENERIC_RELOC_SECTDIFF",
680         "GENERIC_RELOC_PB_LA_PTR",
681         "GENERIC_RELOC_LOCAL_SECTDIFF",
682         "GENERIC_RELOC_TLV" };
683
684       if (RType > 5)
685         res = "Unknown";
686       else
687         res = Table[RType];
688       break;
689     }
690     case Triple::x86_64: {
691       static const char *const Table[] =  {
692         "X86_64_RELOC_UNSIGNED",
693         "X86_64_RELOC_SIGNED",
694         "X86_64_RELOC_BRANCH",
695         "X86_64_RELOC_GOT_LOAD",
696         "X86_64_RELOC_GOT",
697         "X86_64_RELOC_SUBTRACTOR",
698         "X86_64_RELOC_SIGNED_1",
699         "X86_64_RELOC_SIGNED_2",
700         "X86_64_RELOC_SIGNED_4",
701         "X86_64_RELOC_TLV" };
702
703       if (RType > 9)
704         res = "Unknown";
705       else
706         res = Table[RType];
707       break;
708     }
709     case Triple::arm: {
710       static const char *const Table[] =  {
711         "ARM_RELOC_VANILLA",
712         "ARM_RELOC_PAIR",
713         "ARM_RELOC_SECTDIFF",
714         "ARM_RELOC_LOCAL_SECTDIFF",
715         "ARM_RELOC_PB_LA_PTR",
716         "ARM_RELOC_BR24",
717         "ARM_THUMB_RELOC_BR22",
718         "ARM_THUMB_32BIT_BRANCH",
719         "ARM_RELOC_HALF",
720         "ARM_RELOC_HALF_SECTDIFF" };
721
722       if (RType > 9)
723         res = "Unknown";
724       else
725         res = Table[RType];
726       break;
727     }
728     case Triple::aarch64: {
729       static const char *const Table[] = {
730         "ARM64_RELOC_UNSIGNED",           "ARM64_RELOC_SUBTRACTOR",
731         "ARM64_RELOC_BRANCH26",           "ARM64_RELOC_PAGE21",
732         "ARM64_RELOC_PAGEOFF12",          "ARM64_RELOC_GOT_LOAD_PAGE21",
733         "ARM64_RELOC_GOT_LOAD_PAGEOFF12", "ARM64_RELOC_POINTER_TO_GOT",
734         "ARM64_RELOC_TLVP_LOAD_PAGE21",   "ARM64_RELOC_TLVP_LOAD_PAGEOFF12",
735         "ARM64_RELOC_ADDEND"
736       };
737
738       if (RType >= array_lengthof(Table))
739         res = "Unknown";
740       else
741         res = Table[RType];
742       break;
743     }
744     case Triple::ppc: {
745       static const char *const Table[] =  {
746         "PPC_RELOC_VANILLA",
747         "PPC_RELOC_PAIR",
748         "PPC_RELOC_BR14",
749         "PPC_RELOC_BR24",
750         "PPC_RELOC_HI16",
751         "PPC_RELOC_LO16",
752         "PPC_RELOC_HA16",
753         "PPC_RELOC_LO14",
754         "PPC_RELOC_SECTDIFF",
755         "PPC_RELOC_PB_LA_PTR",
756         "PPC_RELOC_HI16_SECTDIFF",
757         "PPC_RELOC_LO16_SECTDIFF",
758         "PPC_RELOC_HA16_SECTDIFF",
759         "PPC_RELOC_JBSR",
760         "PPC_RELOC_LO14_SECTDIFF",
761         "PPC_RELOC_LOCAL_SECTDIFF" };
762
763       if (RType > 15)
764         res = "Unknown";
765       else
766         res = Table[RType];
767       break;
768     }
769     case Triple::UnknownArch:
770       res = "Unknown";
771       break;
772   }
773   Result.append(res.begin(), res.end());
774   return std::error_code();
775 }
776
777 std::error_code MachOObjectFile::getRelocationHidden(DataRefImpl Rel,
778                                                      bool &Result) const {
779   unsigned Arch = getArch();
780   uint64_t Type;
781   getRelocationType(Rel, Type);
782
783   Result = false;
784
785   // On arches that use the generic relocations, GENERIC_RELOC_PAIR
786   // is always hidden.
787   if (Arch == Triple::x86 || Arch == Triple::arm || Arch == Triple::ppc) {
788     if (Type == MachO::GENERIC_RELOC_PAIR) Result = true;
789   } else if (Arch == Triple::x86_64) {
790     // On x86_64, X86_64_RELOC_UNSIGNED is hidden only when it follows
791     // an X86_64_RELOC_SUBTRACTOR.
792     if (Type == MachO::X86_64_RELOC_UNSIGNED && Rel.d.a > 0) {
793       DataRefImpl RelPrev = Rel;
794       RelPrev.d.a--;
795       uint64_t PrevType;
796       getRelocationType(RelPrev, PrevType);
797       if (PrevType == MachO::X86_64_RELOC_SUBTRACTOR)
798         Result = true;
799     }
800   }
801
802   return std::error_code();
803 }
804
805 uint8_t MachOObjectFile::getRelocationLength(DataRefImpl Rel) const {
806   MachO::any_relocation_info RE = getRelocation(Rel);
807   return getAnyRelocationLength(RE);
808 }
809
810 //
811 // guessLibraryShortName() is passed a name of a dynamic library and returns a
812 // guess on what the short name is.  Then name is returned as a substring of the
813 // StringRef Name passed in.  The name of the dynamic library is recognized as
814 // a framework if it has one of the two following forms:
815 //      Foo.framework/Versions/A/Foo
816 //      Foo.framework/Foo
817 // Where A and Foo can be any string.  And may contain a trailing suffix
818 // starting with an underbar.  If the Name is recognized as a framework then
819 // isFramework is set to true else it is set to false.  If the Name has a
820 // suffix then Suffix is set to the substring in Name that contains the suffix
821 // else it is set to a NULL StringRef.
822 //
823 // The Name of the dynamic library is recognized as a library name if it has
824 // one of the two following forms:
825 //      libFoo.A.dylib
826 //      libFoo.dylib
827 // The library may have a suffix trailing the name Foo of the form:
828 //      libFoo_profile.A.dylib
829 //      libFoo_profile.dylib
830 //
831 // The Name of the dynamic library is also recognized as a library name if it
832 // has the following form:
833 //      Foo.qtx
834 //
835 // If the Name of the dynamic library is none of the forms above then a NULL
836 // StringRef is returned.
837 //
838 StringRef MachOObjectFile::guessLibraryShortName(StringRef Name,
839                                                  bool &isFramework,
840                                                  StringRef &Suffix) {
841   StringRef Foo, F, DotFramework, V, Dylib, Lib, Dot, Qtx;
842   size_t a, b, c, d, Idx;
843
844   isFramework = false;
845   Suffix = StringRef();
846
847   // Pull off the last component and make Foo point to it
848   a = Name.rfind('/');
849   if (a == Name.npos || a == 0)
850     goto guess_library;
851   Foo = Name.slice(a+1, Name.npos);
852
853   // Look for a suffix starting with a '_'
854   Idx = Foo.rfind('_');
855   if (Idx != Foo.npos && Foo.size() >= 2) {
856     Suffix = Foo.slice(Idx, Foo.npos);
857     Foo = Foo.slice(0, Idx);
858   }
859
860   // First look for the form Foo.framework/Foo
861   b = Name.rfind('/', a);
862   if (b == Name.npos)
863     Idx = 0;
864   else
865     Idx = b+1;
866   F = Name.slice(Idx, Idx + Foo.size());
867   DotFramework = Name.slice(Idx + Foo.size(),
868                             Idx + Foo.size() + sizeof(".framework/")-1);
869   if (F == Foo && DotFramework == ".framework/") {
870     isFramework = true;
871     return Foo;
872   }
873
874   // Next look for the form Foo.framework/Versions/A/Foo
875   if (b == Name.npos)
876     goto guess_library;
877   c =  Name.rfind('/', b);
878   if (c == Name.npos || c == 0)
879     goto guess_library;
880   V = Name.slice(c+1, Name.npos);
881   if (!V.startswith("Versions/"))
882     goto guess_library;
883   d =  Name.rfind('/', c);
884   if (d == Name.npos)
885     Idx = 0;
886   else
887     Idx = d+1;
888   F = Name.slice(Idx, Idx + Foo.size());
889   DotFramework = Name.slice(Idx + Foo.size(),
890                             Idx + Foo.size() + sizeof(".framework/")-1);
891   if (F == Foo && DotFramework == ".framework/") {
892     isFramework = true;
893     return Foo;
894   }
895
896 guess_library:
897   // pull off the suffix after the "." and make a point to it
898   a = Name.rfind('.');
899   if (a == Name.npos || a == 0)
900     return StringRef();
901   Dylib = Name.slice(a, Name.npos);
902   if (Dylib != ".dylib")
903     goto guess_qtx;
904
905   // First pull off the version letter for the form Foo.A.dylib if any.
906   if (a >= 3) {
907     Dot = Name.slice(a-2, a-1);
908     if (Dot == ".")
909       a = a - 2;
910   }
911
912   b = Name.rfind('/', a);
913   if (b == Name.npos)
914     b = 0;
915   else
916     b = b+1;
917   // ignore any suffix after an underbar like Foo_profile.A.dylib
918   Idx = Name.find('_', b);
919   if (Idx != Name.npos && Idx != b) {
920     Lib = Name.slice(b, Idx);
921     Suffix = Name.slice(Idx, a);
922   }
923   else
924     Lib = Name.slice(b, a);
925   // There are incorrect library names of the form:
926   // libATS.A_profile.dylib so check for these.
927   if (Lib.size() >= 3) {
928     Dot = Lib.slice(Lib.size()-2, Lib.size()-1);
929     if (Dot == ".")
930       Lib = Lib.slice(0, Lib.size()-2);
931   }
932   return Lib;
933
934 guess_qtx:
935   Qtx = Name.slice(a, Name.npos);
936   if (Qtx != ".qtx")
937     return StringRef();
938   b = Name.rfind('/', a);
939   if (b == Name.npos)
940     Lib = Name.slice(0, a);
941   else
942     Lib = Name.slice(b+1, a);
943   // There are library names of the form: QT.A.qtx so check for these.
944   if (Lib.size() >= 3) {
945     Dot = Lib.slice(Lib.size()-2, Lib.size()-1);
946     if (Dot == ".")
947       Lib = Lib.slice(0, Lib.size()-2);
948   }
949   return Lib;
950 }
951
952 // getLibraryShortNameByIndex() is used to get the short name of the library
953 // for an undefined symbol in a linked Mach-O binary that was linked with the
954 // normal two-level namespace default (that is MH_TWOLEVEL in the header).
955 // It is passed the index (0 - based) of the library as translated from
956 // GET_LIBRARY_ORDINAL (1 - based).
957 std::error_code MachOObjectFile::getLibraryShortNameByIndex(unsigned Index,
958                                                          StringRef &Res) const {
959   if (Index >= Libraries.size())
960     return object_error::parse_failed;
961
962   // If the cache of LibrariesShortNames is not built up do that first for
963   // all the Libraries.
964   if (LibrariesShortNames.size() == 0) {
965     for (unsigned i = 0; i < Libraries.size(); i++) {
966       MachO::dylib_command D =
967         getStruct<MachO::dylib_command>(this, Libraries[i]);
968       if (D.dylib.name >= D.cmdsize)
969         return object_error::parse_failed;
970       const char *P = (const char *)(Libraries[i]) + D.dylib.name;
971       StringRef Name = StringRef(P);
972       if (D.dylib.name+Name.size() >= D.cmdsize)
973         return object_error::parse_failed;
974       StringRef Suffix;
975       bool isFramework;
976       StringRef shortName = guessLibraryShortName(Name, isFramework, Suffix);
977       if (shortName.empty())
978         LibrariesShortNames.push_back(Name);
979       else
980         LibrariesShortNames.push_back(shortName);
981     }
982   }
983
984   Res = LibrariesShortNames[Index];
985   return std::error_code();
986 }
987
988 basic_symbol_iterator MachOObjectFile::symbol_begin_impl() const {
989   return getSymbolByIndex(0);
990 }
991
992 basic_symbol_iterator MachOObjectFile::symbol_end_impl() const {
993   DataRefImpl DRI;
994   if (!SymtabLoadCmd)
995     return basic_symbol_iterator(SymbolRef(DRI, this));
996
997   MachO::symtab_command Symtab = getSymtabLoadCommand();
998   unsigned SymbolTableEntrySize = is64Bit() ?
999     sizeof(MachO::nlist_64) :
1000     sizeof(MachO::nlist);
1001   unsigned Offset = Symtab.symoff +
1002     Symtab.nsyms * SymbolTableEntrySize;
1003   DRI.p = reinterpret_cast<uintptr_t>(getPtr(this, Offset));
1004   return basic_symbol_iterator(SymbolRef(DRI, this));
1005 }
1006
1007 basic_symbol_iterator MachOObjectFile::getSymbolByIndex(unsigned Index) const {
1008   DataRefImpl DRI;
1009   if (!SymtabLoadCmd)
1010     return basic_symbol_iterator(SymbolRef(DRI, this));
1011
1012   MachO::symtab_command Symtab = getSymtabLoadCommand();
1013   if (Index >= Symtab.nsyms)
1014     report_fatal_error("Requested symbol index is out of range.");
1015   unsigned SymbolTableEntrySize =
1016     is64Bit() ? sizeof(MachO::nlist_64) : sizeof(MachO::nlist);
1017   DRI.p = reinterpret_cast<uintptr_t>(getPtr(this, Symtab.symoff));
1018   DRI.p += Index * SymbolTableEntrySize;
1019   return basic_symbol_iterator(SymbolRef(DRI, this));
1020 }
1021
1022 section_iterator MachOObjectFile::section_begin() const {
1023   DataRefImpl DRI;
1024   return section_iterator(SectionRef(DRI, this));
1025 }
1026
1027 section_iterator MachOObjectFile::section_end() const {
1028   DataRefImpl DRI;
1029   DRI.d.a = Sections.size();
1030   return section_iterator(SectionRef(DRI, this));
1031 }
1032
1033 uint8_t MachOObjectFile::getBytesInAddress() const {
1034   return is64Bit() ? 8 : 4;
1035 }
1036
1037 StringRef MachOObjectFile::getFileFormatName() const {
1038   unsigned CPUType = getCPUType(this);
1039   if (!is64Bit()) {
1040     switch (CPUType) {
1041     case llvm::MachO::CPU_TYPE_I386:
1042       return "Mach-O 32-bit i386";
1043     case llvm::MachO::CPU_TYPE_ARM:
1044       return "Mach-O arm";
1045     case llvm::MachO::CPU_TYPE_POWERPC:
1046       return "Mach-O 32-bit ppc";
1047     default:
1048       return "Mach-O 32-bit unknown";
1049     }
1050   }
1051
1052   switch (CPUType) {
1053   case llvm::MachO::CPU_TYPE_X86_64:
1054     return "Mach-O 64-bit x86-64";
1055   case llvm::MachO::CPU_TYPE_ARM64:
1056     return "Mach-O arm64";
1057   case llvm::MachO::CPU_TYPE_POWERPC64:
1058     return "Mach-O 64-bit ppc64";
1059   default:
1060     return "Mach-O 64-bit unknown";
1061   }
1062 }
1063
1064 Triple::ArchType MachOObjectFile::getArch(uint32_t CPUType) {
1065   switch (CPUType) {
1066   case llvm::MachO::CPU_TYPE_I386:
1067     return Triple::x86;
1068   case llvm::MachO::CPU_TYPE_X86_64:
1069     return Triple::x86_64;
1070   case llvm::MachO::CPU_TYPE_ARM:
1071     return Triple::arm;
1072   case llvm::MachO::CPU_TYPE_ARM64:
1073     return Triple::aarch64;
1074   case llvm::MachO::CPU_TYPE_POWERPC:
1075     return Triple::ppc;
1076   case llvm::MachO::CPU_TYPE_POWERPC64:
1077     return Triple::ppc64;
1078   default:
1079     return Triple::UnknownArch;
1080   }
1081 }
1082
1083 Triple MachOObjectFile::getArch(uint32_t CPUType, uint32_t CPUSubType,
1084                                 const char **McpuDefault) {
1085   if (McpuDefault)
1086     *McpuDefault = nullptr;
1087
1088   switch (CPUType) {
1089   case MachO::CPU_TYPE_I386:
1090     switch (CPUSubType & ~MachO::CPU_SUBTYPE_MASK) {
1091     case MachO::CPU_SUBTYPE_I386_ALL:
1092       return Triple("i386-apple-darwin");
1093     default:
1094       return Triple();
1095     }
1096   case MachO::CPU_TYPE_X86_64:
1097     switch (CPUSubType & ~MachO::CPU_SUBTYPE_MASK) {
1098     case MachO::CPU_SUBTYPE_X86_64_ALL:
1099       return Triple("x86_64-apple-darwin");
1100     case MachO::CPU_SUBTYPE_X86_64_H:
1101       return Triple("x86_64h-apple-darwin");
1102     default:
1103       return Triple();
1104     }
1105   case MachO::CPU_TYPE_ARM:
1106     switch (CPUSubType & ~MachO::CPU_SUBTYPE_MASK) {
1107     case MachO::CPU_SUBTYPE_ARM_V4T:
1108       return Triple("armv4t-apple-darwin");
1109     case MachO::CPU_SUBTYPE_ARM_V5TEJ:
1110       return Triple("armv5e-apple-darwin");
1111     case MachO::CPU_SUBTYPE_ARM_XSCALE:
1112       return Triple("xscale-apple-darwin");
1113     case MachO::CPU_SUBTYPE_ARM_V6:
1114       return Triple("armv6-apple-darwin");
1115     case MachO::CPU_SUBTYPE_ARM_V6M:
1116       if (McpuDefault)
1117         *McpuDefault = "cortex-m0";
1118       return Triple("armv6m-apple-darwin");
1119     case MachO::CPU_SUBTYPE_ARM_V7:
1120       return Triple("armv7-apple-darwin");
1121     case MachO::CPU_SUBTYPE_ARM_V7EM:
1122       if (McpuDefault)
1123         *McpuDefault = "cortex-m4";
1124       return Triple("armv7em-apple-darwin");
1125     case MachO::CPU_SUBTYPE_ARM_V7K:
1126       return Triple("armv7k-apple-darwin");
1127     case MachO::CPU_SUBTYPE_ARM_V7M:
1128       if (McpuDefault)
1129         *McpuDefault = "cortex-m3";
1130       return Triple("armv7m-apple-darwin");
1131     case MachO::CPU_SUBTYPE_ARM_V7S:
1132       return Triple("armv7s-apple-darwin");
1133     default:
1134       return Triple();
1135     }
1136   case MachO::CPU_TYPE_ARM64:
1137     switch (CPUSubType & ~MachO::CPU_SUBTYPE_MASK) {
1138     case MachO::CPU_SUBTYPE_ARM64_ALL:
1139       return Triple("arm64-apple-darwin");
1140     default:
1141       return Triple();
1142     }
1143   case MachO::CPU_TYPE_POWERPC:
1144     switch (CPUSubType & ~MachO::CPU_SUBTYPE_MASK) {
1145     case MachO::CPU_SUBTYPE_POWERPC_ALL:
1146       return Triple("ppc-apple-darwin");
1147     default:
1148       return Triple();
1149     }
1150   case MachO::CPU_TYPE_POWERPC64:
1151     switch (CPUSubType & ~MachO::CPU_SUBTYPE_MASK) {
1152     case MachO::CPU_SUBTYPE_POWERPC_ALL:
1153       return Triple("ppc64-apple-darwin");
1154     default:
1155       return Triple();
1156     }
1157   default:
1158     return Triple();
1159   }
1160 }
1161
1162 Triple MachOObjectFile::getThumbArch(uint32_t CPUType, uint32_t CPUSubType,
1163                                      const char **McpuDefault) {
1164   if (McpuDefault)
1165     *McpuDefault = nullptr;
1166
1167   switch (CPUType) {
1168   case MachO::CPU_TYPE_ARM:
1169     switch (CPUSubType & ~MachO::CPU_SUBTYPE_MASK) {
1170     case MachO::CPU_SUBTYPE_ARM_V4T:
1171       return Triple("thumbv4t-apple-darwin");
1172     case MachO::CPU_SUBTYPE_ARM_V5TEJ:
1173       return Triple("thumbv5e-apple-darwin");
1174     case MachO::CPU_SUBTYPE_ARM_XSCALE:
1175       return Triple("xscale-apple-darwin");
1176     case MachO::CPU_SUBTYPE_ARM_V6:
1177       return Triple("thumbv6-apple-darwin");
1178     case MachO::CPU_SUBTYPE_ARM_V6M:
1179       if (McpuDefault)
1180         *McpuDefault = "cortex-m0";
1181       return Triple("thumbv6m-apple-darwin");
1182     case MachO::CPU_SUBTYPE_ARM_V7:
1183       return Triple("thumbv7-apple-darwin");
1184     case MachO::CPU_SUBTYPE_ARM_V7EM:
1185       if (McpuDefault)
1186         *McpuDefault = "cortex-m4";
1187       return Triple("thumbv7em-apple-darwin");
1188     case MachO::CPU_SUBTYPE_ARM_V7K:
1189       return Triple("thumbv7k-apple-darwin");
1190     case MachO::CPU_SUBTYPE_ARM_V7M:
1191       if (McpuDefault)
1192         *McpuDefault = "cortex-m3";
1193       return Triple("thumbv7m-apple-darwin");
1194     case MachO::CPU_SUBTYPE_ARM_V7S:
1195       return Triple("thumbv7s-apple-darwin");
1196     default:
1197       return Triple();
1198     }
1199   default:
1200     return Triple();
1201   }
1202 }
1203
1204 Triple MachOObjectFile::getArch(uint32_t CPUType, uint32_t CPUSubType,
1205                                 const char **McpuDefault,
1206                                 Triple *ThumbTriple) {
1207   Triple T = MachOObjectFile::getArch(CPUType, CPUSubType, McpuDefault);
1208   *ThumbTriple = MachOObjectFile::getThumbArch(CPUType, CPUSubType,
1209                                                McpuDefault);
1210   return T;
1211 }
1212
1213 Triple MachOObjectFile::getHostArch() {
1214   return Triple(sys::getDefaultTargetTriple());
1215 }
1216
1217 bool MachOObjectFile::isValidArch(StringRef ArchFlag) {
1218   return StringSwitch<bool>(ArchFlag)
1219       .Case("i386", true)
1220       .Case("x86_64", true)
1221       .Case("x86_64h", true)
1222       .Case("armv4t", true)
1223       .Case("arm", true)
1224       .Case("armv5e", true)
1225       .Case("armv6", true)
1226       .Case("armv6m", true)
1227       .Case("armv7", true)
1228       .Case("armv7em", true)
1229       .Case("armv7k", true)
1230       .Case("armv7m", true)
1231       .Case("armv7s", true)
1232       .Case("arm64", true)
1233       .Case("ppc", true)
1234       .Case("ppc64", true)
1235       .Default(false);
1236 }
1237
1238 unsigned MachOObjectFile::getArch() const {
1239   return getArch(getCPUType(this));
1240 }
1241
1242 Triple MachOObjectFile::getArch(const char **McpuDefault,
1243                                 Triple *ThumbTriple) const {
1244   *ThumbTriple = getThumbArch(Header.cputype, Header.cpusubtype, McpuDefault);
1245   return getArch(Header.cputype, Header.cpusubtype, McpuDefault);
1246 }
1247
1248 relocation_iterator MachOObjectFile::section_rel_begin(unsigned Index) const {
1249   DataRefImpl DRI;
1250   DRI.d.a = Index;
1251   return section_rel_begin(DRI);
1252 }
1253
1254 relocation_iterator MachOObjectFile::section_rel_end(unsigned Index) const {
1255   DataRefImpl DRI;
1256   DRI.d.a = Index;
1257   return section_rel_end(DRI);
1258 }
1259
1260 dice_iterator MachOObjectFile::begin_dices() const {
1261   DataRefImpl DRI;
1262   if (!DataInCodeLoadCmd)
1263     return dice_iterator(DiceRef(DRI, this));
1264
1265   MachO::linkedit_data_command DicLC = getDataInCodeLoadCommand();
1266   DRI.p = reinterpret_cast<uintptr_t>(getPtr(this, DicLC.dataoff));
1267   return dice_iterator(DiceRef(DRI, this));
1268 }
1269
1270 dice_iterator MachOObjectFile::end_dices() const {
1271   DataRefImpl DRI;
1272   if (!DataInCodeLoadCmd)
1273     return dice_iterator(DiceRef(DRI, this));
1274
1275   MachO::linkedit_data_command DicLC = getDataInCodeLoadCommand();
1276   unsigned Offset = DicLC.dataoff + DicLC.datasize;
1277   DRI.p = reinterpret_cast<uintptr_t>(getPtr(this, Offset));
1278   return dice_iterator(DiceRef(DRI, this));
1279 }
1280
1281 ExportEntry::ExportEntry(ArrayRef<uint8_t> T) 
1282   : Trie(T), Malformed(false), Done(false) { }
1283
1284 void ExportEntry::moveToFirst() {
1285   pushNode(0);
1286   pushDownUntilBottom();
1287 }
1288
1289 void ExportEntry::moveToEnd() {
1290   Stack.clear();
1291   Done = true;
1292 }
1293
1294 bool ExportEntry::operator==(const ExportEntry &Other) const {
1295   // Common case, one at end, other iterating from begin. 
1296   if (Done || Other.Done)
1297     return (Done == Other.Done);
1298   // Not equal if different stack sizes.
1299   if (Stack.size() != Other.Stack.size())
1300     return false;
1301   // Not equal if different cumulative strings.
1302   if (!CumulativeString.equals(Other.CumulativeString))
1303     return false;
1304   // Equal if all nodes in both stacks match.
1305   for (unsigned i=0; i < Stack.size(); ++i) {
1306     if (Stack[i].Start != Other.Stack[i].Start)
1307       return false;
1308   }
1309   return true;  
1310 }
1311
1312 uint64_t ExportEntry::readULEB128(const uint8_t *&Ptr) {
1313   unsigned Count;
1314   uint64_t Result = decodeULEB128(Ptr, &Count);
1315   Ptr += Count;
1316   if (Ptr > Trie.end()) {
1317     Ptr = Trie.end();
1318     Malformed = true;
1319   }
1320   return Result;
1321 }
1322
1323 StringRef ExportEntry::name() const {
1324   return CumulativeString;
1325 }
1326
1327 uint64_t ExportEntry::flags() const {
1328   return Stack.back().Flags;
1329 }
1330
1331 uint64_t ExportEntry::address() const {
1332   return Stack.back().Address;
1333 }
1334
1335 uint64_t ExportEntry::other() const {
1336   return Stack.back().Other;
1337 }
1338
1339 StringRef ExportEntry::otherName() const {
1340   const char* ImportName = Stack.back().ImportName;
1341   if (ImportName)
1342     return StringRef(ImportName);
1343   return StringRef();
1344 }
1345
1346 uint32_t ExportEntry::nodeOffset() const {
1347   return Stack.back().Start - Trie.begin();
1348 }
1349
1350 ExportEntry::NodeState::NodeState(const uint8_t *Ptr) 
1351   : Start(Ptr), Current(Ptr), Flags(0), Address(0), Other(0), 
1352     ImportName(nullptr), ChildCount(0), NextChildIndex(0),  
1353     ParentStringLength(0), IsExportNode(false) {
1354 }
1355
1356 void ExportEntry::pushNode(uint64_t offset) {
1357   const uint8_t *Ptr = Trie.begin() + offset;
1358   NodeState State(Ptr);
1359   uint64_t ExportInfoSize = readULEB128(State.Current);
1360   State.IsExportNode = (ExportInfoSize != 0);
1361   const uint8_t* Children = State.Current + ExportInfoSize;
1362   if (State.IsExportNode) {
1363     State.Flags = readULEB128(State.Current);
1364     if (State.Flags & MachO::EXPORT_SYMBOL_FLAGS_REEXPORT) {
1365       State.Address = 0;
1366       State.Other = readULEB128(State.Current); // dylib ordinal
1367       State.ImportName = reinterpret_cast<const char*>(State.Current);
1368     } else {
1369       State.Address = readULEB128(State.Current);
1370       if (State.Flags & MachO::EXPORT_SYMBOL_FLAGS_STUB_AND_RESOLVER)
1371         State.Other = readULEB128(State.Current); 
1372     }
1373   }
1374   State.ChildCount = *Children;
1375   State.Current = Children + 1;
1376   State.NextChildIndex = 0;
1377   State.ParentStringLength = CumulativeString.size();
1378   Stack.push_back(State);
1379 }
1380
1381 void ExportEntry::pushDownUntilBottom() {
1382   while (Stack.back().NextChildIndex < Stack.back().ChildCount) {
1383     NodeState &Top = Stack.back();
1384     CumulativeString.resize(Top.ParentStringLength);
1385     for (;*Top.Current != 0; Top.Current++) {
1386       char C = *Top.Current;
1387       CumulativeString.push_back(C);
1388     }
1389     Top.Current += 1;
1390     uint64_t childNodeIndex = readULEB128(Top.Current);
1391     Top.NextChildIndex += 1;
1392     pushNode(childNodeIndex);
1393   }
1394   if (!Stack.back().IsExportNode) {
1395     Malformed = true;
1396     moveToEnd();
1397   }
1398 }
1399
1400 // We have a trie data structure and need a way to walk it that is compatible
1401 // with the C++ iterator model. The solution is a non-recursive depth first
1402 // traversal where the iterator contains a stack of parent nodes along with a
1403 // string that is the accumulation of all edge strings along the parent chain
1404 // to this point.
1405 //
1406 // There is one "export" node for each exported symbol.  But because some
1407 // symbols may be a prefix of another symbol (e.g. _dup and _dup2), an export
1408 // node may have child nodes too.  
1409 //
1410 // The algorithm for moveNext() is to keep moving down the leftmost unvisited
1411 // child until hitting a node with no children (which is an export node or
1412 // else the trie is malformed). On the way down, each node is pushed on the
1413 // stack ivar.  If there is no more ways down, it pops up one and tries to go
1414 // down a sibling path until a childless node is reached.
1415 void ExportEntry::moveNext() {
1416   if (Stack.empty() || !Stack.back().IsExportNode) {
1417     Malformed = true;
1418     moveToEnd();
1419     return;
1420   }
1421
1422   Stack.pop_back();
1423   while (!Stack.empty()) {
1424     NodeState &Top = Stack.back();
1425     if (Top.NextChildIndex < Top.ChildCount) {
1426       pushDownUntilBottom();
1427       // Now at the next export node.
1428       return;
1429     } else {
1430       if (Top.IsExportNode) {
1431         // This node has no children but is itself an export node.
1432         CumulativeString.resize(Top.ParentStringLength);
1433         return;
1434       }
1435       Stack.pop_back();
1436     }
1437   }
1438   Done = true;
1439 }
1440
1441 iterator_range<export_iterator> 
1442 MachOObjectFile::exports(ArrayRef<uint8_t> Trie) {
1443   ExportEntry Start(Trie);
1444   if (Trie.size() == 0)
1445     Start.moveToEnd();
1446   else
1447     Start.moveToFirst();
1448
1449   ExportEntry Finish(Trie);
1450   Finish.moveToEnd();
1451
1452   return iterator_range<export_iterator>(export_iterator(Start), 
1453                                          export_iterator(Finish));
1454 }
1455
1456 iterator_range<export_iterator> MachOObjectFile::exports() const {
1457   return exports(getDyldInfoExportsTrie());
1458 }
1459
1460
1461 MachORebaseEntry::MachORebaseEntry(ArrayRef<uint8_t> Bytes, bool is64Bit)
1462     : Opcodes(Bytes), Ptr(Bytes.begin()), SegmentOffset(0), SegmentIndex(0),
1463       RemainingLoopCount(0), AdvanceAmount(0), RebaseType(0),
1464       PointerSize(is64Bit ? 8 : 4), Malformed(false), Done(false) {}
1465
1466 void MachORebaseEntry::moveToFirst() {
1467   Ptr = Opcodes.begin();
1468   moveNext();
1469 }
1470
1471 void MachORebaseEntry::moveToEnd() {
1472   Ptr = Opcodes.end();
1473   RemainingLoopCount = 0;
1474   Done = true;
1475 }
1476
1477 void MachORebaseEntry::moveNext() {
1478   // If in the middle of some loop, move to next rebasing in loop.
1479   SegmentOffset += AdvanceAmount;
1480   if (RemainingLoopCount) {
1481     --RemainingLoopCount;
1482     return;
1483   }
1484   if (Ptr == Opcodes.end()) {
1485     Done = true;
1486     return;
1487   }
1488   bool More = true;
1489   while (More && !Malformed) {
1490     // Parse next opcode and set up next loop.
1491     uint8_t Byte = *Ptr++;
1492     uint8_t ImmValue = Byte & MachO::REBASE_IMMEDIATE_MASK;
1493     uint8_t Opcode = Byte & MachO::REBASE_OPCODE_MASK;
1494     switch (Opcode) {
1495     case MachO::REBASE_OPCODE_DONE:
1496       More = false;
1497       Done = true;
1498       moveToEnd();
1499       DEBUG_WITH_TYPE("mach-o-rebase", llvm::dbgs() << "REBASE_OPCODE_DONE\n");
1500       break;
1501     case MachO::REBASE_OPCODE_SET_TYPE_IMM:
1502       RebaseType = ImmValue;
1503       DEBUG_WITH_TYPE(
1504           "mach-o-rebase",
1505           llvm::dbgs() << "REBASE_OPCODE_SET_TYPE_IMM: "
1506                        << "RebaseType=" << (int) RebaseType << "\n");
1507       break;
1508     case MachO::REBASE_OPCODE_SET_SEGMENT_AND_OFFSET_ULEB:
1509       SegmentIndex = ImmValue;
1510       SegmentOffset = readULEB128();
1511       DEBUG_WITH_TYPE(
1512           "mach-o-rebase",
1513           llvm::dbgs() << "REBASE_OPCODE_SET_SEGMENT_AND_OFFSET_ULEB: "
1514                        << "SegmentIndex=" << SegmentIndex << ", "
1515                        << format("SegmentOffset=0x%06X", SegmentOffset)
1516                        << "\n");
1517       break;
1518     case MachO::REBASE_OPCODE_ADD_ADDR_ULEB:
1519       SegmentOffset += readULEB128();
1520       DEBUG_WITH_TYPE("mach-o-rebase",
1521                       llvm::dbgs() << "REBASE_OPCODE_ADD_ADDR_ULEB: "
1522                                    << format("SegmentOffset=0x%06X",
1523                                              SegmentOffset) << "\n");
1524       break;
1525     case MachO::REBASE_OPCODE_ADD_ADDR_IMM_SCALED:
1526       SegmentOffset += ImmValue * PointerSize;
1527       DEBUG_WITH_TYPE("mach-o-rebase",
1528                       llvm::dbgs() << "REBASE_OPCODE_ADD_ADDR_IMM_SCALED: "
1529                                    << format("SegmentOffset=0x%06X",
1530                                              SegmentOffset) << "\n");
1531       break;
1532     case MachO::REBASE_OPCODE_DO_REBASE_IMM_TIMES:
1533       AdvanceAmount = PointerSize;
1534       RemainingLoopCount = ImmValue - 1;
1535       DEBUG_WITH_TYPE(
1536           "mach-o-rebase",
1537           llvm::dbgs() << "REBASE_OPCODE_DO_REBASE_IMM_TIMES: "
1538                        << format("SegmentOffset=0x%06X", SegmentOffset)
1539                        << ", AdvanceAmount=" << AdvanceAmount
1540                        << ", RemainingLoopCount=" << RemainingLoopCount
1541                        << "\n");
1542       return;
1543     case MachO::REBASE_OPCODE_DO_REBASE_ULEB_TIMES:
1544       AdvanceAmount = PointerSize;
1545       RemainingLoopCount = readULEB128() - 1;
1546       DEBUG_WITH_TYPE(
1547           "mach-o-rebase",
1548           llvm::dbgs() << "REBASE_OPCODE_DO_REBASE_ULEB_TIMES: "
1549                        << format("SegmentOffset=0x%06X", SegmentOffset)
1550                        << ", AdvanceAmount=" << AdvanceAmount
1551                        << ", RemainingLoopCount=" << RemainingLoopCount
1552                        << "\n");
1553       return;
1554     case MachO::REBASE_OPCODE_DO_REBASE_ADD_ADDR_ULEB:
1555       AdvanceAmount = readULEB128() + PointerSize;
1556       RemainingLoopCount = 0;
1557       DEBUG_WITH_TYPE(
1558           "mach-o-rebase",
1559           llvm::dbgs() << "REBASE_OPCODE_DO_REBASE_ADD_ADDR_ULEB: "
1560                        << format("SegmentOffset=0x%06X", SegmentOffset)
1561                        << ", AdvanceAmount=" << AdvanceAmount
1562                        << ", RemainingLoopCount=" << RemainingLoopCount
1563                        << "\n");
1564       return;
1565     case MachO::REBASE_OPCODE_DO_REBASE_ULEB_TIMES_SKIPPING_ULEB:
1566       RemainingLoopCount = readULEB128() - 1;
1567       AdvanceAmount = readULEB128() + PointerSize;
1568       DEBUG_WITH_TYPE(
1569           "mach-o-rebase",
1570           llvm::dbgs() << "REBASE_OPCODE_DO_REBASE_ULEB_TIMES_SKIPPING_ULEB: "
1571                        << format("SegmentOffset=0x%06X", SegmentOffset)
1572                        << ", AdvanceAmount=" << AdvanceAmount
1573                        << ", RemainingLoopCount=" << RemainingLoopCount
1574                        << "\n");
1575       return;
1576     default:
1577       Malformed = true;
1578     }
1579   }
1580 }
1581
1582 uint64_t MachORebaseEntry::readULEB128() {
1583   unsigned Count;
1584   uint64_t Result = decodeULEB128(Ptr, &Count);
1585   Ptr += Count;
1586   if (Ptr > Opcodes.end()) {
1587     Ptr = Opcodes.end();
1588     Malformed = true;
1589   }
1590   return Result;
1591 }
1592
1593 uint32_t MachORebaseEntry::segmentIndex() const { return SegmentIndex; }
1594
1595 uint64_t MachORebaseEntry::segmentOffset() const { return SegmentOffset; }
1596
1597 StringRef MachORebaseEntry::typeName() const {
1598   switch (RebaseType) {
1599   case MachO::REBASE_TYPE_POINTER:
1600     return "pointer";
1601   case MachO::REBASE_TYPE_TEXT_ABSOLUTE32:
1602     return "text abs32";
1603   case MachO::REBASE_TYPE_TEXT_PCREL32:
1604     return "text rel32";
1605   }
1606   return "unknown";
1607 }
1608
1609 bool MachORebaseEntry::operator==(const MachORebaseEntry &Other) const {
1610   assert(Opcodes == Other.Opcodes && "compare iterators of different files");
1611   return (Ptr == Other.Ptr) &&
1612          (RemainingLoopCount == Other.RemainingLoopCount) &&
1613          (Done == Other.Done);
1614 }
1615
1616 iterator_range<rebase_iterator>
1617 MachOObjectFile::rebaseTable(ArrayRef<uint8_t> Opcodes, bool is64) {
1618   MachORebaseEntry Start(Opcodes, is64);
1619   Start.moveToFirst();
1620
1621   MachORebaseEntry Finish(Opcodes, is64);
1622   Finish.moveToEnd();
1623
1624   return iterator_range<rebase_iterator>(rebase_iterator(Start),
1625                                          rebase_iterator(Finish));
1626 }
1627
1628 iterator_range<rebase_iterator> MachOObjectFile::rebaseTable() const {
1629   return rebaseTable(getDyldInfoRebaseOpcodes(), is64Bit());
1630 }
1631
1632
1633 MachOBindEntry::MachOBindEntry(ArrayRef<uint8_t> Bytes, bool is64Bit,
1634                                Kind BK)
1635     : Opcodes(Bytes), Ptr(Bytes.begin()), SegmentOffset(0), SegmentIndex(0),
1636       Ordinal(0), Flags(0), Addend(0), RemainingLoopCount(0), AdvanceAmount(0),
1637       BindType(0), PointerSize(is64Bit ? 8 : 4),
1638       TableKind(BK), Malformed(false), Done(false) {}
1639
1640 void MachOBindEntry::moveToFirst() {
1641   Ptr = Opcodes.begin();
1642   moveNext();
1643 }
1644
1645 void MachOBindEntry::moveToEnd() {
1646   Ptr = Opcodes.end();
1647   RemainingLoopCount = 0;
1648   Done = true;
1649 }
1650
1651 void MachOBindEntry::moveNext() {
1652   // If in the middle of some loop, move to next binding in loop.
1653   SegmentOffset += AdvanceAmount;
1654   if (RemainingLoopCount) {
1655     --RemainingLoopCount;
1656     return;
1657   }
1658   if (Ptr == Opcodes.end()) {
1659     Done = true;
1660     return;
1661   }
1662   bool More = true;
1663   while (More && !Malformed) {
1664     // Parse next opcode and set up next loop.
1665     uint8_t Byte = *Ptr++;
1666     uint8_t ImmValue = Byte & MachO::BIND_IMMEDIATE_MASK;
1667     uint8_t Opcode = Byte & MachO::BIND_OPCODE_MASK;
1668     int8_t SignExtended;
1669     const uint8_t *SymStart;
1670     switch (Opcode) {
1671     case MachO::BIND_OPCODE_DONE:
1672       if (TableKind == Kind::Lazy) {
1673         // Lazying bindings have a DONE opcode between entries.  Need to ignore
1674         // it to advance to next entry.  But need not if this is last entry.
1675         bool NotLastEntry = false;
1676         for (const uint8_t *P = Ptr; P < Opcodes.end(); ++P) {
1677           if (*P) {
1678             NotLastEntry = true;
1679           }
1680         }
1681         if (NotLastEntry)
1682           break;
1683       }
1684       More = false;
1685       Done = true;
1686       moveToEnd();
1687       DEBUG_WITH_TYPE("mach-o-bind", llvm::dbgs() << "BIND_OPCODE_DONE\n");
1688       break;
1689     case MachO::BIND_OPCODE_SET_DYLIB_ORDINAL_IMM:
1690       Ordinal = ImmValue;
1691       DEBUG_WITH_TYPE(
1692           "mach-o-bind",
1693           llvm::dbgs() << "BIND_OPCODE_SET_DYLIB_ORDINAL_IMM: "
1694                        << "Ordinal=" << Ordinal << "\n");
1695       break;
1696     case MachO::BIND_OPCODE_SET_DYLIB_ORDINAL_ULEB:
1697       Ordinal = readULEB128();
1698       DEBUG_WITH_TYPE(
1699           "mach-o-bind",
1700           llvm::dbgs() << "BIND_OPCODE_SET_DYLIB_ORDINAL_ULEB: "
1701                        << "Ordinal=" << Ordinal << "\n");
1702       break;
1703     case MachO::BIND_OPCODE_SET_DYLIB_SPECIAL_IMM:
1704       if (ImmValue) {
1705         SignExtended = MachO::BIND_OPCODE_MASK | ImmValue;
1706         Ordinal = SignExtended;
1707       } else
1708         Ordinal = 0;
1709       DEBUG_WITH_TYPE(
1710           "mach-o-bind",
1711           llvm::dbgs() << "BIND_OPCODE_SET_DYLIB_SPECIAL_IMM: "
1712                        << "Ordinal=" << Ordinal << "\n");
1713       break;
1714     case MachO::BIND_OPCODE_SET_SYMBOL_TRAILING_FLAGS_IMM:
1715       Flags = ImmValue;
1716       SymStart = Ptr;
1717       while (*Ptr) {
1718         ++Ptr;
1719       }
1720       SymbolName = StringRef(reinterpret_cast<const char*>(SymStart),
1721                              Ptr-SymStart);
1722       ++Ptr;
1723       DEBUG_WITH_TYPE(
1724           "mach-o-bind",
1725           llvm::dbgs() << "BIND_OPCODE_SET_SYMBOL_TRAILING_FLAGS_IMM: "
1726                        << "SymbolName=" << SymbolName << "\n");
1727       if (TableKind == Kind::Weak) {
1728         if (ImmValue & MachO::BIND_SYMBOL_FLAGS_NON_WEAK_DEFINITION)
1729           return;
1730       }
1731       break;
1732     case MachO::BIND_OPCODE_SET_TYPE_IMM:
1733       BindType = ImmValue;
1734       DEBUG_WITH_TYPE(
1735           "mach-o-bind",
1736           llvm::dbgs() << "BIND_OPCODE_SET_TYPE_IMM: "
1737                        << "BindType=" << (int)BindType << "\n");
1738       break;
1739     case MachO::BIND_OPCODE_SET_ADDEND_SLEB:
1740       Addend = readSLEB128();
1741       if (TableKind == Kind::Lazy)
1742         Malformed = true;
1743       DEBUG_WITH_TYPE(
1744           "mach-o-bind",
1745           llvm::dbgs() << "BIND_OPCODE_SET_ADDEND_SLEB: "
1746                        << "Addend=" << Addend << "\n");
1747       break;
1748     case MachO::BIND_OPCODE_SET_SEGMENT_AND_OFFSET_ULEB:
1749       SegmentIndex = ImmValue;
1750       SegmentOffset = readULEB128();
1751       DEBUG_WITH_TYPE(
1752           "mach-o-bind",
1753           llvm::dbgs() << "BIND_OPCODE_SET_SEGMENT_AND_OFFSET_ULEB: "
1754                        << "SegmentIndex=" << SegmentIndex << ", "
1755                        << format("SegmentOffset=0x%06X", SegmentOffset)
1756                        << "\n");
1757       break;
1758     case MachO::BIND_OPCODE_ADD_ADDR_ULEB:
1759       SegmentOffset += readULEB128();
1760       DEBUG_WITH_TYPE("mach-o-bind",
1761                       llvm::dbgs() << "BIND_OPCODE_ADD_ADDR_ULEB: "
1762                                    << format("SegmentOffset=0x%06X",
1763                                              SegmentOffset) << "\n");
1764       break;
1765     case MachO::BIND_OPCODE_DO_BIND:
1766       AdvanceAmount = PointerSize;
1767       RemainingLoopCount = 0;
1768       DEBUG_WITH_TYPE("mach-o-bind",
1769                       llvm::dbgs() << "BIND_OPCODE_DO_BIND: "
1770                                    << format("SegmentOffset=0x%06X",
1771                                              SegmentOffset) << "\n");
1772       return;
1773      case MachO::BIND_OPCODE_DO_BIND_ADD_ADDR_ULEB:
1774       AdvanceAmount = readULEB128() + PointerSize;
1775       RemainingLoopCount = 0;
1776       if (TableKind == Kind::Lazy)
1777         Malformed = true;
1778       DEBUG_WITH_TYPE(
1779           "mach-o-bind",
1780           llvm::dbgs() << "BIND_OPCODE_DO_BIND_ADD_ADDR_ULEB: "
1781                        << format("SegmentOffset=0x%06X", SegmentOffset)
1782                        << ", AdvanceAmount=" << AdvanceAmount
1783                        << ", RemainingLoopCount=" << RemainingLoopCount
1784                        << "\n");
1785       return;
1786     case MachO::BIND_OPCODE_DO_BIND_ADD_ADDR_IMM_SCALED:
1787       AdvanceAmount = ImmValue * PointerSize + PointerSize;
1788       RemainingLoopCount = 0;
1789       if (TableKind == Kind::Lazy)
1790         Malformed = true;
1791       DEBUG_WITH_TYPE("mach-o-bind",
1792                       llvm::dbgs()
1793                       << "BIND_OPCODE_DO_BIND_ADD_ADDR_IMM_SCALED: "
1794                       << format("SegmentOffset=0x%06X",
1795                                              SegmentOffset) << "\n");
1796       return;
1797     case MachO::BIND_OPCODE_DO_BIND_ULEB_TIMES_SKIPPING_ULEB:
1798       RemainingLoopCount = readULEB128() - 1;
1799       AdvanceAmount = readULEB128() + PointerSize;
1800       if (TableKind == Kind::Lazy)
1801         Malformed = true;
1802       DEBUG_WITH_TYPE(
1803           "mach-o-bind",
1804           llvm::dbgs() << "BIND_OPCODE_DO_BIND_ULEB_TIMES_SKIPPING_ULEB: "
1805                        << format("SegmentOffset=0x%06X", SegmentOffset)
1806                        << ", AdvanceAmount=" << AdvanceAmount
1807                        << ", RemainingLoopCount=" << RemainingLoopCount
1808                        << "\n");
1809       return;
1810     default:
1811       Malformed = true;
1812     }
1813   }
1814 }
1815
1816 uint64_t MachOBindEntry::readULEB128() {
1817   unsigned Count;
1818   uint64_t Result = decodeULEB128(Ptr, &Count);
1819   Ptr += Count;
1820   if (Ptr > Opcodes.end()) {
1821     Ptr = Opcodes.end();
1822     Malformed = true;
1823   }
1824   return Result;
1825 }
1826
1827 int64_t MachOBindEntry::readSLEB128() {
1828   unsigned Count;
1829   int64_t Result = decodeSLEB128(Ptr, &Count);
1830   Ptr += Count;
1831   if (Ptr > Opcodes.end()) {
1832     Ptr = Opcodes.end();
1833     Malformed = true;
1834   }
1835   return Result;
1836 }
1837
1838
1839 uint32_t MachOBindEntry::segmentIndex() const { return SegmentIndex; }
1840
1841 uint64_t MachOBindEntry::segmentOffset() const { return SegmentOffset; }
1842
1843 StringRef MachOBindEntry::typeName() const {
1844   switch (BindType) {
1845   case MachO::BIND_TYPE_POINTER:
1846     return "pointer";
1847   case MachO::BIND_TYPE_TEXT_ABSOLUTE32:
1848     return "text abs32";
1849   case MachO::BIND_TYPE_TEXT_PCREL32:
1850     return "text rel32";
1851   }
1852   return "unknown";
1853 }
1854
1855 StringRef MachOBindEntry::symbolName() const { return SymbolName; }
1856
1857 int64_t MachOBindEntry::addend() const { return Addend; }
1858
1859 uint32_t MachOBindEntry::flags() const { return Flags; }
1860
1861 int MachOBindEntry::ordinal() const { return Ordinal; }
1862
1863 bool MachOBindEntry::operator==(const MachOBindEntry &Other) const {
1864   assert(Opcodes == Other.Opcodes && "compare iterators of different files");
1865   return (Ptr == Other.Ptr) &&
1866          (RemainingLoopCount == Other.RemainingLoopCount) &&
1867          (Done == Other.Done);
1868 }
1869
1870 iterator_range<bind_iterator>
1871 MachOObjectFile::bindTable(ArrayRef<uint8_t> Opcodes, bool is64,
1872                            MachOBindEntry::Kind BKind) {
1873   MachOBindEntry Start(Opcodes, is64, BKind);
1874   Start.moveToFirst();
1875
1876   MachOBindEntry Finish(Opcodes, is64, BKind);
1877   Finish.moveToEnd();
1878
1879   return iterator_range<bind_iterator>(bind_iterator(Start),
1880                                        bind_iterator(Finish));
1881 }
1882
1883 iterator_range<bind_iterator> MachOObjectFile::bindTable() const {
1884   return bindTable(getDyldInfoBindOpcodes(), is64Bit(),
1885                    MachOBindEntry::Kind::Regular);
1886 }
1887
1888 iterator_range<bind_iterator> MachOObjectFile::lazyBindTable() const {
1889   return bindTable(getDyldInfoLazyBindOpcodes(), is64Bit(),
1890                    MachOBindEntry::Kind::Lazy);
1891 }
1892
1893 iterator_range<bind_iterator> MachOObjectFile::weakBindTable() const {
1894   return bindTable(getDyldInfoWeakBindOpcodes(), is64Bit(),
1895                    MachOBindEntry::Kind::Weak);
1896 }
1897
1898 MachOObjectFile::load_command_iterator
1899 MachOObjectFile::begin_load_commands() const {
1900   return LoadCommands.begin();
1901 }
1902
1903 MachOObjectFile::load_command_iterator
1904 MachOObjectFile::end_load_commands() const {
1905   return LoadCommands.end();
1906 }
1907
1908 iterator_range<MachOObjectFile::load_command_iterator>
1909 MachOObjectFile::load_commands() const {
1910   return iterator_range<load_command_iterator>(begin_load_commands(),
1911                                                end_load_commands());
1912 }
1913
1914 StringRef
1915 MachOObjectFile::getSectionFinalSegmentName(DataRefImpl Sec) const {
1916   ArrayRef<char> Raw = getSectionRawFinalSegmentName(Sec);
1917   return parseSegmentOrSectionName(Raw.data());
1918 }
1919
1920 ArrayRef<char>
1921 MachOObjectFile::getSectionRawName(DataRefImpl Sec) const {
1922   assert(Sec.d.a < Sections.size() && "Should have detected this earlier");
1923   const section_base *Base =
1924     reinterpret_cast<const section_base *>(Sections[Sec.d.a]);
1925   return makeArrayRef(Base->sectname);
1926 }
1927
1928 ArrayRef<char>
1929 MachOObjectFile::getSectionRawFinalSegmentName(DataRefImpl Sec) const {
1930   assert(Sec.d.a < Sections.size() && "Should have detected this earlier");
1931   const section_base *Base =
1932     reinterpret_cast<const section_base *>(Sections[Sec.d.a]);
1933   return makeArrayRef(Base->segname);
1934 }
1935
1936 bool
1937 MachOObjectFile::isRelocationScattered(const MachO::any_relocation_info &RE)
1938   const {
1939   if (getCPUType(this) == MachO::CPU_TYPE_X86_64)
1940     return false;
1941   return getPlainRelocationAddress(RE) & MachO::R_SCATTERED;
1942 }
1943
1944 unsigned MachOObjectFile::getPlainRelocationSymbolNum(
1945     const MachO::any_relocation_info &RE) const {
1946   if (isLittleEndian())
1947     return RE.r_word1 & 0xffffff;
1948   return RE.r_word1 >> 8;
1949 }
1950
1951 bool MachOObjectFile::getPlainRelocationExternal(
1952     const MachO::any_relocation_info &RE) const {
1953   if (isLittleEndian())
1954     return (RE.r_word1 >> 27) & 1;
1955   return (RE.r_word1 >> 4) & 1;
1956 }
1957
1958 bool MachOObjectFile::getScatteredRelocationScattered(
1959     const MachO::any_relocation_info &RE) const {
1960   return RE.r_word0 >> 31;
1961 }
1962
1963 uint32_t MachOObjectFile::getScatteredRelocationValue(
1964     const MachO::any_relocation_info &RE) const {
1965   return RE.r_word1;
1966 }
1967
1968 uint32_t MachOObjectFile::getScatteredRelocationType(
1969     const MachO::any_relocation_info &RE) const {
1970   return (RE.r_word0 >> 24) & 0xf;
1971 }
1972
1973 unsigned MachOObjectFile::getAnyRelocationAddress(
1974     const MachO::any_relocation_info &RE) const {
1975   if (isRelocationScattered(RE))
1976     return getScatteredRelocationAddress(RE);
1977   return getPlainRelocationAddress(RE);
1978 }
1979
1980 unsigned MachOObjectFile::getAnyRelocationPCRel(
1981     const MachO::any_relocation_info &RE) const {
1982   if (isRelocationScattered(RE))
1983     return getScatteredRelocationPCRel(this, RE);
1984   return getPlainRelocationPCRel(this, RE);
1985 }
1986
1987 unsigned MachOObjectFile::getAnyRelocationLength(
1988     const MachO::any_relocation_info &RE) const {
1989   if (isRelocationScattered(RE))
1990     return getScatteredRelocationLength(RE);
1991   return getPlainRelocationLength(this, RE);
1992 }
1993
1994 unsigned
1995 MachOObjectFile::getAnyRelocationType(
1996                                    const MachO::any_relocation_info &RE) const {
1997   if (isRelocationScattered(RE))
1998     return getScatteredRelocationType(RE);
1999   return getPlainRelocationType(this, RE);
2000 }
2001
2002 SectionRef
2003 MachOObjectFile::getAnyRelocationSection(
2004                                    const MachO::any_relocation_info &RE) const {
2005   if (isRelocationScattered(RE) || getPlainRelocationExternal(RE))
2006     return *section_end();
2007   unsigned SecNum = getPlainRelocationSymbolNum(RE);
2008   if (SecNum == MachO::R_ABS || SecNum > Sections.size())
2009     return *section_end();
2010   DataRefImpl DRI;
2011   DRI.d.a = SecNum - 1;
2012   return SectionRef(DRI, this);
2013 }
2014
2015 MachO::section MachOObjectFile::getSection(DataRefImpl DRI) const {
2016   assert(DRI.d.a < Sections.size() && "Should have detected this earlier");
2017   return getStruct<MachO::section>(this, Sections[DRI.d.a]);
2018 }
2019
2020 MachO::section_64 MachOObjectFile::getSection64(DataRefImpl DRI) const {
2021   assert(DRI.d.a < Sections.size() && "Should have detected this earlier");
2022   return getStruct<MachO::section_64>(this, Sections[DRI.d.a]);
2023 }
2024
2025 MachO::section MachOObjectFile::getSection(const LoadCommandInfo &L,
2026                                            unsigned Index) const {
2027   const char *Sec = getSectionPtr(this, L, Index);
2028   return getStruct<MachO::section>(this, Sec);
2029 }
2030
2031 MachO::section_64 MachOObjectFile::getSection64(const LoadCommandInfo &L,
2032                                                 unsigned Index) const {
2033   const char *Sec = getSectionPtr(this, L, Index);
2034   return getStruct<MachO::section_64>(this, Sec);
2035 }
2036
2037 MachO::nlist
2038 MachOObjectFile::getSymbolTableEntry(DataRefImpl DRI) const {
2039   const char *P = reinterpret_cast<const char *>(DRI.p);
2040   return getStruct<MachO::nlist>(this, P);
2041 }
2042
2043 MachO::nlist_64
2044 MachOObjectFile::getSymbol64TableEntry(DataRefImpl DRI) const {
2045   const char *P = reinterpret_cast<const char *>(DRI.p);
2046   return getStruct<MachO::nlist_64>(this, P);
2047 }
2048
2049 MachO::linkedit_data_command
2050 MachOObjectFile::getLinkeditDataLoadCommand(const LoadCommandInfo &L) const {
2051   return getStruct<MachO::linkedit_data_command>(this, L.Ptr);
2052 }
2053
2054 MachO::segment_command
2055 MachOObjectFile::getSegmentLoadCommand(const LoadCommandInfo &L) const {
2056   return getStruct<MachO::segment_command>(this, L.Ptr);
2057 }
2058
2059 MachO::segment_command_64
2060 MachOObjectFile::getSegment64LoadCommand(const LoadCommandInfo &L) const {
2061   return getStruct<MachO::segment_command_64>(this, L.Ptr);
2062 }
2063
2064 MachO::linker_option_command
2065 MachOObjectFile::getLinkerOptionLoadCommand(const LoadCommandInfo &L) const {
2066   return getStruct<MachO::linker_option_command>(this, L.Ptr);
2067 }
2068
2069 MachO::version_min_command
2070 MachOObjectFile::getVersionMinLoadCommand(const LoadCommandInfo &L) const {
2071   return getStruct<MachO::version_min_command>(this, L.Ptr);
2072 }
2073
2074 MachO::dylib_command
2075 MachOObjectFile::getDylibIDLoadCommand(const LoadCommandInfo &L) const {
2076   return getStruct<MachO::dylib_command>(this, L.Ptr);
2077 }
2078
2079 MachO::dyld_info_command
2080 MachOObjectFile::getDyldInfoLoadCommand(const LoadCommandInfo &L) const {
2081   return getStruct<MachO::dyld_info_command>(this, L.Ptr);
2082 }
2083
2084 MachO::dylinker_command
2085 MachOObjectFile::getDylinkerCommand(const LoadCommandInfo &L) const {
2086   return getStruct<MachO::dylinker_command>(this, L.Ptr);
2087 }
2088
2089 MachO::uuid_command
2090 MachOObjectFile::getUuidCommand(const LoadCommandInfo &L) const {
2091   return getStruct<MachO::uuid_command>(this, L.Ptr);
2092 }
2093
2094 MachO::rpath_command
2095 MachOObjectFile::getRpathCommand(const LoadCommandInfo &L) const {
2096   return getStruct<MachO::rpath_command>(this, L.Ptr);
2097 }
2098
2099 MachO::source_version_command
2100 MachOObjectFile::getSourceVersionCommand(const LoadCommandInfo &L) const {
2101   return getStruct<MachO::source_version_command>(this, L.Ptr);
2102 }
2103
2104 MachO::entry_point_command
2105 MachOObjectFile::getEntryPointCommand(const LoadCommandInfo &L) const {
2106   return getStruct<MachO::entry_point_command>(this, L.Ptr);
2107 }
2108
2109 MachO::encryption_info_command
2110 MachOObjectFile::getEncryptionInfoCommand(const LoadCommandInfo &L) const {
2111   return getStruct<MachO::encryption_info_command>(this, L.Ptr);
2112 }
2113
2114 MachO::encryption_info_command_64
2115 MachOObjectFile::getEncryptionInfoCommand64(const LoadCommandInfo &L) const {
2116   return getStruct<MachO::encryption_info_command_64>(this, L.Ptr);
2117 }
2118
2119 MachO::sub_framework_command
2120 MachOObjectFile::getSubFrameworkCommand(const LoadCommandInfo &L) const {
2121   return getStruct<MachO::sub_framework_command>(this, L.Ptr);
2122 }
2123
2124 MachO::sub_umbrella_command
2125 MachOObjectFile::getSubUmbrellaCommand(const LoadCommandInfo &L) const {
2126   return getStruct<MachO::sub_umbrella_command>(this, L.Ptr);
2127 }
2128
2129 MachO::sub_library_command
2130 MachOObjectFile::getSubLibraryCommand(const LoadCommandInfo &L) const {
2131   return getStruct<MachO::sub_library_command>(this, L.Ptr);
2132 }
2133
2134 MachO::sub_client_command
2135 MachOObjectFile::getSubClientCommand(const LoadCommandInfo &L) const {
2136   return getStruct<MachO::sub_client_command>(this, L.Ptr);
2137 }
2138
2139 MachO::routines_command
2140 MachOObjectFile::getRoutinesCommand(const LoadCommandInfo &L) const {
2141   return getStruct<MachO::routines_command>(this, L.Ptr);
2142 }
2143
2144 MachO::routines_command_64
2145 MachOObjectFile::getRoutinesCommand64(const LoadCommandInfo &L) const {
2146   return getStruct<MachO::routines_command_64>(this, L.Ptr);
2147 }
2148
2149 MachO::thread_command
2150 MachOObjectFile::getThreadCommand(const LoadCommandInfo &L) const {
2151   return getStruct<MachO::thread_command>(this, L.Ptr);
2152 }
2153
2154 MachO::any_relocation_info
2155 MachOObjectFile::getRelocation(DataRefImpl Rel) const {
2156   DataRefImpl Sec;
2157   Sec.d.a = Rel.d.a;
2158   uint32_t Offset;
2159   if (is64Bit()) {
2160     MachO::section_64 Sect = getSection64(Sec);
2161     Offset = Sect.reloff;
2162   } else {
2163     MachO::section Sect = getSection(Sec);
2164     Offset = Sect.reloff;
2165   }
2166
2167   auto P = reinterpret_cast<const MachO::any_relocation_info *>(
2168       getPtr(this, Offset)) + Rel.d.b;
2169   return getStruct<MachO::any_relocation_info>(
2170       this, reinterpret_cast<const char *>(P));
2171 }
2172
2173 MachO::data_in_code_entry
2174 MachOObjectFile::getDice(DataRefImpl Rel) const {
2175   const char *P = reinterpret_cast<const char *>(Rel.p);
2176   return getStruct<MachO::data_in_code_entry>(this, P);
2177 }
2178
2179 const MachO::mach_header &MachOObjectFile::getHeader() const {
2180   return Header;
2181 }
2182
2183 const MachO::mach_header_64 &MachOObjectFile::getHeader64() const {
2184   assert(is64Bit());
2185   return Header64;
2186 }
2187
2188 uint32_t MachOObjectFile::getIndirectSymbolTableEntry(
2189                                              const MachO::dysymtab_command &DLC,
2190                                              unsigned Index) const {
2191   uint64_t Offset = DLC.indirectsymoff + Index * sizeof(uint32_t);
2192   return getStruct<uint32_t>(this, getPtr(this, Offset));
2193 }
2194
2195 MachO::data_in_code_entry
2196 MachOObjectFile::getDataInCodeTableEntry(uint32_t DataOffset,
2197                                          unsigned Index) const {
2198   uint64_t Offset = DataOffset + Index * sizeof(MachO::data_in_code_entry);
2199   return getStruct<MachO::data_in_code_entry>(this, getPtr(this, Offset));
2200 }
2201
2202 MachO::symtab_command MachOObjectFile::getSymtabLoadCommand() const {
2203   if (SymtabLoadCmd)
2204     return getStruct<MachO::symtab_command>(this, SymtabLoadCmd);
2205
2206   // If there is no SymtabLoadCmd return a load command with zero'ed fields.
2207   MachO::symtab_command Cmd;
2208   Cmd.cmd = MachO::LC_SYMTAB;
2209   Cmd.cmdsize = sizeof(MachO::symtab_command);
2210   Cmd.symoff = 0;
2211   Cmd.nsyms = 0;
2212   Cmd.stroff = 0;
2213   Cmd.strsize = 0;
2214   return Cmd;
2215 }
2216
2217 MachO::dysymtab_command MachOObjectFile::getDysymtabLoadCommand() const {
2218   if (DysymtabLoadCmd)
2219     return getStruct<MachO::dysymtab_command>(this, DysymtabLoadCmd);
2220
2221   // If there is no DysymtabLoadCmd return a load command with zero'ed fields.
2222   MachO::dysymtab_command Cmd;
2223   Cmd.cmd = MachO::LC_DYSYMTAB;
2224   Cmd.cmdsize = sizeof(MachO::dysymtab_command);
2225   Cmd.ilocalsym = 0;
2226   Cmd.nlocalsym = 0;
2227   Cmd.iextdefsym = 0;
2228   Cmd.nextdefsym = 0;
2229   Cmd.iundefsym = 0;
2230   Cmd.nundefsym = 0;
2231   Cmd.tocoff = 0;
2232   Cmd.ntoc = 0;
2233   Cmd.modtaboff = 0;
2234   Cmd.nmodtab = 0;
2235   Cmd.extrefsymoff = 0;
2236   Cmd.nextrefsyms = 0;
2237   Cmd.indirectsymoff = 0;
2238   Cmd.nindirectsyms = 0;
2239   Cmd.extreloff = 0;
2240   Cmd.nextrel = 0;
2241   Cmd.locreloff = 0;
2242   Cmd.nlocrel = 0;
2243   return Cmd;
2244 }
2245
2246 MachO::linkedit_data_command
2247 MachOObjectFile::getDataInCodeLoadCommand() const {
2248   if (DataInCodeLoadCmd)
2249     return getStruct<MachO::linkedit_data_command>(this, DataInCodeLoadCmd);
2250
2251   // If there is no DataInCodeLoadCmd return a load command with zero'ed fields.
2252   MachO::linkedit_data_command Cmd;
2253   Cmd.cmd = MachO::LC_DATA_IN_CODE;
2254   Cmd.cmdsize = sizeof(MachO::linkedit_data_command);
2255   Cmd.dataoff = 0;
2256   Cmd.datasize = 0;
2257   return Cmd;
2258 }
2259
2260 MachO::linkedit_data_command
2261 MachOObjectFile::getLinkOptHintsLoadCommand() const {
2262   if (LinkOptHintsLoadCmd)
2263     return getStruct<MachO::linkedit_data_command>(this, LinkOptHintsLoadCmd);
2264
2265   // If there is no LinkOptHintsLoadCmd return a load command with zero'ed
2266   // fields.
2267   MachO::linkedit_data_command Cmd;
2268   Cmd.cmd = MachO::LC_LINKER_OPTIMIZATION_HINT;
2269   Cmd.cmdsize = sizeof(MachO::linkedit_data_command);
2270   Cmd.dataoff = 0;
2271   Cmd.datasize = 0;
2272   return Cmd;
2273 }
2274
2275 ArrayRef<uint8_t> MachOObjectFile::getDyldInfoRebaseOpcodes() const {
2276   if (!DyldInfoLoadCmd) 
2277     return ArrayRef<uint8_t>();
2278
2279   MachO::dyld_info_command DyldInfo 
2280                    = getStruct<MachO::dyld_info_command>(this, DyldInfoLoadCmd);
2281   const uint8_t *Ptr = reinterpret_cast<const uint8_t*>(
2282                                              getPtr(this, DyldInfo.rebase_off));
2283   return ArrayRef<uint8_t>(Ptr, DyldInfo.rebase_size);
2284 }
2285
2286 ArrayRef<uint8_t> MachOObjectFile::getDyldInfoBindOpcodes() const {
2287   if (!DyldInfoLoadCmd) 
2288     return ArrayRef<uint8_t>();
2289
2290   MachO::dyld_info_command DyldInfo 
2291                    = getStruct<MachO::dyld_info_command>(this, DyldInfoLoadCmd);
2292   const uint8_t *Ptr = reinterpret_cast<const uint8_t*>(
2293                                                getPtr(this, DyldInfo.bind_off));
2294   return ArrayRef<uint8_t>(Ptr, DyldInfo.bind_size);
2295 }
2296
2297 ArrayRef<uint8_t> MachOObjectFile::getDyldInfoWeakBindOpcodes() const {
2298   if (!DyldInfoLoadCmd) 
2299     return ArrayRef<uint8_t>();
2300
2301   MachO::dyld_info_command DyldInfo 
2302                    = getStruct<MachO::dyld_info_command>(this, DyldInfoLoadCmd);
2303   const uint8_t *Ptr = reinterpret_cast<const uint8_t*>(
2304                                           getPtr(this, DyldInfo.weak_bind_off));
2305   return ArrayRef<uint8_t>(Ptr, DyldInfo.weak_bind_size);
2306 }
2307
2308 ArrayRef<uint8_t> MachOObjectFile::getDyldInfoLazyBindOpcodes() const {
2309   if (!DyldInfoLoadCmd) 
2310     return ArrayRef<uint8_t>();
2311
2312   MachO::dyld_info_command DyldInfo 
2313                    = getStruct<MachO::dyld_info_command>(this, DyldInfoLoadCmd);
2314   const uint8_t *Ptr = reinterpret_cast<const uint8_t*>(
2315                                           getPtr(this, DyldInfo.lazy_bind_off));
2316   return ArrayRef<uint8_t>(Ptr, DyldInfo.lazy_bind_size);
2317 }
2318
2319 ArrayRef<uint8_t> MachOObjectFile::getDyldInfoExportsTrie() const {
2320   if (!DyldInfoLoadCmd) 
2321     return ArrayRef<uint8_t>();
2322
2323   MachO::dyld_info_command DyldInfo 
2324                    = getStruct<MachO::dyld_info_command>(this, DyldInfoLoadCmd);
2325   const uint8_t *Ptr = reinterpret_cast<const uint8_t*>(
2326                                              getPtr(this, DyldInfo.export_off));
2327   return ArrayRef<uint8_t>(Ptr, DyldInfo.export_size);
2328 }
2329
2330 ArrayRef<uint8_t> MachOObjectFile::getUuid() const {
2331   if (!UuidLoadCmd)
2332     return ArrayRef<uint8_t>();
2333   // Returning a pointer is fine as uuid doesn't need endian swapping.
2334   const char *Ptr = UuidLoadCmd + offsetof(MachO::uuid_command, uuid);
2335   return ArrayRef<uint8_t>(reinterpret_cast<const uint8_t *>(Ptr), 16);
2336 }
2337
2338 StringRef MachOObjectFile::getStringTableData() const {
2339   MachO::symtab_command S = getSymtabLoadCommand();
2340   return getData().substr(S.stroff, S.strsize);
2341 }
2342
2343 bool MachOObjectFile::is64Bit() const {
2344   return getType() == getMachOType(false, true) ||
2345     getType() == getMachOType(true, true);
2346 }
2347
2348 void MachOObjectFile::ReadULEB128s(uint64_t Index,
2349                                    SmallVectorImpl<uint64_t> &Out) const {
2350   DataExtractor extractor(ObjectFile::getData(), true, 0);
2351
2352   uint32_t offset = Index;
2353   uint64_t data = 0;
2354   while (uint64_t delta = extractor.getULEB128(&offset)) {
2355     data += delta;
2356     Out.push_back(data);
2357   }
2358 }
2359
2360 bool MachOObjectFile::isRelocatableObject() const {
2361   return getHeader().filetype == MachO::MH_OBJECT;
2362 }
2363
2364 ErrorOr<std::unique_ptr<MachOObjectFile>>
2365 ObjectFile::createMachOObjectFile(MemoryBufferRef Buffer) {
2366   StringRef Magic = Buffer.getBuffer().slice(0, 4);
2367   std::error_code EC;
2368   std::unique_ptr<MachOObjectFile> Ret;
2369   if (Magic == "\xFE\xED\xFA\xCE")
2370     Ret.reset(new MachOObjectFile(Buffer, false, false, EC));
2371   else if (Magic == "\xCE\xFA\xED\xFE")
2372     Ret.reset(new MachOObjectFile(Buffer, true, false, EC));
2373   else if (Magic == "\xFE\xED\xFA\xCF")
2374     Ret.reset(new MachOObjectFile(Buffer, false, true, EC));
2375   else if (Magic == "\xCF\xFA\xED\xFE")
2376     Ret.reset(new MachOObjectFile(Buffer, true, true, EC));
2377   else
2378     return object_error::parse_failed;
2379
2380   if (EC)
2381     return EC;
2382   return std::move(Ret);
2383 }
2384