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