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