xref: /llvm-project/llvm/lib/Object/MachOObjectFile.cpp (revision 1126d02c0ca06557dffdd1cf870693f2647bdc53)
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/Triple.h"
18 #include "llvm/Support/DataExtractor.h"
19 #include "llvm/Support/Format.h"
20 #include "llvm/Support/Host.h"
21 #include "llvm/Support/MemoryBuffer.h"
22 #include "llvm/Support/raw_ostream.h"
23 #include <cctype>
24 #include <cstring>
25 #include <limits>
26 
27 using namespace llvm;
28 using namespace object;
29 
30 namespace llvm {
31 namespace object {
32 
33 struct nlist_base {
34   uint32_t n_strx;
35   uint8_t n_type;
36   uint8_t n_sect;
37   uint16_t n_desc;
38 };
39 
40 struct section_base {
41   char sectname[16];
42   char segname[16];
43 };
44 
45 template<typename T>
46 static void SwapValue(T &Value) {
47   Value = sys::SwapByteOrder(Value);
48 }
49 
50 template<typename T>
51 static void SwapStruct(T &Value);
52 
53 template<>
54 void SwapStruct(MachO::any_relocation_info &H) {
55   SwapValue(H.r_word0);
56   SwapValue(H.r_word1);
57 }
58 
59 template<>
60 void SwapStruct(MachO::load_command &L) {
61   SwapValue(L.cmd);
62   SwapValue(L.cmdsize);
63 }
64 
65 template<>
66 void SwapStruct(nlist_base &S) {
67   SwapValue(S.n_strx);
68   SwapValue(S.n_desc);
69 }
70 
71 template<>
72 void SwapStruct(MachO::section &S) {
73   SwapValue(S.addr);
74   SwapValue(S.size);
75   SwapValue(S.offset);
76   SwapValue(S.align);
77   SwapValue(S.reloff);
78   SwapValue(S.nreloc);
79   SwapValue(S.flags);
80   SwapValue(S.reserved1);
81   SwapValue(S.reserved2);
82 }
83 
84 template<>
85 void SwapStruct(MachO::section_64 &S) {
86   SwapValue(S.addr);
87   SwapValue(S.size);
88   SwapValue(S.offset);
89   SwapValue(S.align);
90   SwapValue(S.reloff);
91   SwapValue(S.nreloc);
92   SwapValue(S.flags);
93   SwapValue(S.reserved1);
94   SwapValue(S.reserved2);
95   SwapValue(S.reserved3);
96 }
97 
98 template<>
99 void SwapStruct(MachO::nlist &S) {
100   SwapValue(S.n_strx);
101   SwapValue(S.n_desc);
102   SwapValue(S.n_value);
103 }
104 
105 template<>
106 void SwapStruct(MachO::nlist_64 &S) {
107   SwapValue(S.n_strx);
108   SwapValue(S.n_desc);
109   SwapValue(S.n_value);
110 }
111 
112 template<>
113 void SwapStruct(MachO::mach_header &H) {
114   SwapValue(H.magic);
115   SwapValue(H.cputype);
116   SwapValue(H.cpusubtype);
117   SwapValue(H.filetype);
118   SwapValue(H.ncmds);
119   SwapValue(H.sizeofcmds);
120   SwapValue(H.flags);
121 }
122 
123 template<>
124 void SwapStruct(MachO::mach_header_64 &H) {
125   SwapValue(H.magic);
126   SwapValue(H.cputype);
127   SwapValue(H.cpusubtype);
128   SwapValue(H.filetype);
129   SwapValue(H.ncmds);
130   SwapValue(H.sizeofcmds);
131   SwapValue(H.flags);
132   SwapValue(H.reserved);
133 }
134 
135 template<>
136 void SwapStruct(MachO::symtab_command &C) {
137   SwapValue(C.cmd);
138   SwapValue(C.cmdsize);
139   SwapValue(C.symoff);
140   SwapValue(C.nsyms);
141   SwapValue(C.stroff);
142   SwapValue(C.strsize);
143 }
144 
145 template<>
146 void SwapStruct(MachO::dysymtab_command &C) {
147   SwapValue(C.cmd);
148   SwapValue(C.cmdsize);
149   SwapValue(C.ilocalsym);
150   SwapValue(C.nlocalsym);
151   SwapValue(C.iextdefsym);
152   SwapValue(C.nextdefsym);
153   SwapValue(C.iundefsym);
154   SwapValue(C.nundefsym);
155   SwapValue(C.tocoff);
156   SwapValue(C.ntoc);
157   SwapValue(C.modtaboff);
158   SwapValue(C.nmodtab);
159   SwapValue(C.extrefsymoff);
160   SwapValue(C.nextrefsyms);
161   SwapValue(C.indirectsymoff);
162   SwapValue(C.nindirectsyms);
163   SwapValue(C.extreloff);
164   SwapValue(C.nextrel);
165   SwapValue(C.locreloff);
166   SwapValue(C.nlocrel);
167 }
168 
169 template<>
170 void SwapStruct(MachO::linkedit_data_command &C) {
171   SwapValue(C.cmd);
172   SwapValue(C.cmdsize);
173   SwapValue(C.dataoff);
174   SwapValue(C.datasize);
175 }
176 
177 template<>
178 void SwapStruct(MachO::segment_command &C) {
179   SwapValue(C.cmd);
180   SwapValue(C.cmdsize);
181   SwapValue(C.vmaddr);
182   SwapValue(C.vmsize);
183   SwapValue(C.fileoff);
184   SwapValue(C.filesize);
185   SwapValue(C.maxprot);
186   SwapValue(C.initprot);
187   SwapValue(C.nsects);
188   SwapValue(C.flags);
189 }
190 
191 template<>
192 void SwapStruct(MachO::segment_command_64 &C) {
193   SwapValue(C.cmd);
194   SwapValue(C.cmdsize);
195   SwapValue(C.vmaddr);
196   SwapValue(C.vmsize);
197   SwapValue(C.fileoff);
198   SwapValue(C.filesize);
199   SwapValue(C.maxprot);
200   SwapValue(C.initprot);
201   SwapValue(C.nsects);
202   SwapValue(C.flags);
203 }
204 
205 template<>
206 void SwapStruct(uint32_t &C) {
207   SwapValue(C);
208 }
209 
210 template<>
211 void SwapStruct(MachO::linker_options_command &C) {
212   SwapValue(C.cmd);
213   SwapValue(C.cmdsize);
214   SwapValue(C.count);
215 }
216 
217 template<>
218 void SwapStruct(MachO::version_min_command&C) {
219   SwapValue(C.cmd);
220   SwapValue(C.cmdsize);
221   SwapValue(C.version);
222   SwapValue(C.reserved);
223 }
224 
225 template<>
226 void SwapStruct(MachO::data_in_code_entry &C) {
227   SwapValue(C.offset);
228   SwapValue(C.length);
229   SwapValue(C.kind);
230 }
231 
232 template<typename T>
233 T getStruct(const MachOObjectFile *O, const char *P) {
234   T Cmd;
235   memcpy(&Cmd, P, sizeof(T));
236   if (O->isLittleEndian() != sys::IsLittleEndianHost)
237     SwapStruct(Cmd);
238   return Cmd;
239 }
240 
241 static uint32_t
242 getSegmentLoadCommandNumSections(const MachOObjectFile *O,
243                                  const MachOObjectFile::LoadCommandInfo &L) {
244   if (O->is64Bit()) {
245     MachO::segment_command_64 S = O->getSegment64LoadCommand(L);
246     return S.nsects;
247   }
248   MachO::segment_command S = O->getSegmentLoadCommand(L);
249   return S.nsects;
250 }
251 
252 static const char *
253 getSectionPtr(const MachOObjectFile *O, MachOObjectFile::LoadCommandInfo L,
254               unsigned Sec) {
255   uintptr_t CommandAddr = reinterpret_cast<uintptr_t>(L.Ptr);
256 
257   bool Is64 = O->is64Bit();
258   unsigned SegmentLoadSize = Is64 ? sizeof(MachO::segment_command_64) :
259                                     sizeof(MachO::segment_command);
260   unsigned SectionSize = Is64 ? sizeof(MachO::section_64) :
261                                 sizeof(MachO::section);
262 
263   uintptr_t SectionAddr = CommandAddr + SegmentLoadSize + Sec * SectionSize;
264   return reinterpret_cast<const char*>(SectionAddr);
265 }
266 
267 static const char *getPtr(const MachOObjectFile *O, size_t Offset) {
268   return O->getData().substr(Offset, 1).data();
269 }
270 
271 static nlist_base
272 getSymbolTableEntryBase(const MachOObjectFile *O, DataRefImpl DRI) {
273   const char *P = reinterpret_cast<const char *>(DRI.p);
274   return getStruct<nlist_base>(O, P);
275 }
276 
277 static StringRef parseSegmentOrSectionName(const char *P) {
278   if (P[15] == 0)
279     // Null terminated.
280     return P;
281   // Not null terminated, so this is a 16 char string.
282   return StringRef(P, 16);
283 }
284 
285 // Helper to advance a section or symbol iterator multiple increments at a time.
286 template<class T>
287 static void advance(T &it, size_t Val) {
288   while (Val--)
289     ++it;
290 }
291 
292 static unsigned getCPUType(const MachOObjectFile *O) {
293   return O->getHeader().cputype;
294 }
295 
296 static void printRelocationTargetName(const MachOObjectFile *O,
297                                       const MachO::any_relocation_info &RE,
298                                       raw_string_ostream &fmt) {
299   bool IsScattered = O->isRelocationScattered(RE);
300 
301   // Target of a scattered relocation is an address.  In the interest of
302   // generating pretty output, scan through the symbol table looking for a
303   // symbol that aligns with that address.  If we find one, print it.
304   // Otherwise, we just print the hex address of the target.
305   if (IsScattered) {
306     uint32_t Val = O->getPlainRelocationSymbolNum(RE);
307 
308     for (const SymbolRef &Symbol : O->symbols()) {
309       error_code ec;
310       uint64_t Addr;
311       StringRef Name;
312 
313       if ((ec = Symbol.getAddress(Addr)))
314         report_fatal_error(ec.message());
315       if (Addr != Val)
316         continue;
317       if ((ec = Symbol.getName(Name)))
318         report_fatal_error(ec.message());
319       fmt << Name;
320       return;
321     }
322 
323     // If we couldn't find a symbol that this relocation refers to, try
324     // to find a section beginning instead.
325     for (const SectionRef &Section : O->sections()) {
326       error_code ec;
327       uint64_t Addr;
328       StringRef Name;
329 
330       if ((ec = Section.getAddress(Addr)))
331         report_fatal_error(ec.message());
332       if (Addr != Val)
333         continue;
334       if ((ec = Section.getName(Name)))
335         report_fatal_error(ec.message());
336       fmt << Name;
337       return;
338     }
339 
340     fmt << format("0x%x", Val);
341     return;
342   }
343 
344   StringRef S;
345   bool isExtern = O->getPlainRelocationExternal(RE);
346   uint64_t Val = O->getPlainRelocationSymbolNum(RE);
347 
348   if (isExtern) {
349     symbol_iterator SI = O->symbol_begin();
350     advance(SI, Val);
351     SI->getName(S);
352   } else {
353     section_iterator SI = O->section_begin();
354     // Adjust for the fact that sections are 1-indexed.
355     advance(SI, Val - 1);
356     SI->getName(S);
357   }
358 
359   fmt << S;
360 }
361 
362 static uint32_t
363 getPlainRelocationAddress(const MachO::any_relocation_info &RE) {
364   return RE.r_word0;
365 }
366 
367 static unsigned
368 getScatteredRelocationAddress(const MachO::any_relocation_info &RE) {
369   return RE.r_word0 & 0xffffff;
370 }
371 
372 static bool getPlainRelocationPCRel(const MachOObjectFile *O,
373                                     const MachO::any_relocation_info &RE) {
374   if (O->isLittleEndian())
375     return (RE.r_word1 >> 24) & 1;
376   return (RE.r_word1 >> 7) & 1;
377 }
378 
379 static bool
380 getScatteredRelocationPCRel(const MachOObjectFile *O,
381                             const MachO::any_relocation_info &RE) {
382   return (RE.r_word0 >> 30) & 1;
383 }
384 
385 static unsigned getPlainRelocationLength(const MachOObjectFile *O,
386                                          const MachO::any_relocation_info &RE) {
387   if (O->isLittleEndian())
388     return (RE.r_word1 >> 25) & 3;
389   return (RE.r_word1 >> 5) & 3;
390 }
391 
392 static unsigned
393 getScatteredRelocationLength(const MachO::any_relocation_info &RE) {
394   return (RE.r_word0 >> 28) & 3;
395 }
396 
397 static unsigned getPlainRelocationType(const MachOObjectFile *O,
398                                        const MachO::any_relocation_info &RE) {
399   if (O->isLittleEndian())
400     return RE.r_word1 >> 28;
401   return RE.r_word1 & 0xf;
402 }
403 
404 static unsigned
405 getScatteredRelocationType(const MachO::any_relocation_info &RE) {
406   return (RE.r_word0 >> 24) & 0xf;
407 }
408 
409 static uint32_t getSectionFlags(const MachOObjectFile *O,
410                                 DataRefImpl Sec) {
411   if (O->is64Bit()) {
412     MachO::section_64 Sect = O->getSection64(Sec);
413     return Sect.flags;
414   }
415   MachO::section Sect = O->getSection(Sec);
416   return Sect.flags;
417 }
418 
419 MachOObjectFile::MachOObjectFile(MemoryBuffer *Object, bool IsLittleEndian,
420                                  bool Is64bits, error_code &EC,
421                                  bool BufferOwned)
422     : ObjectFile(getMachOType(IsLittleEndian, Is64bits), Object, BufferOwned),
423       SymtabLoadCmd(nullptr), DysymtabLoadCmd(nullptr),
424       DataInCodeLoadCmd(nullptr) {
425   uint32_t LoadCommandCount = this->getHeader().ncmds;
426   MachO::LoadCommandType SegmentLoadType = is64Bit() ?
427     MachO::LC_SEGMENT_64 : MachO::LC_SEGMENT;
428 
429   MachOObjectFile::LoadCommandInfo Load = getFirstLoadCommandInfo();
430   for (unsigned I = 0; ; ++I) {
431     if (Load.C.cmd == MachO::LC_SYMTAB) {
432       assert(!SymtabLoadCmd && "Multiple symbol tables");
433       SymtabLoadCmd = Load.Ptr;
434     } else if (Load.C.cmd == MachO::LC_DYSYMTAB) {
435       assert(!DysymtabLoadCmd && "Multiple dynamic symbol tables");
436       DysymtabLoadCmd = Load.Ptr;
437     } else if (Load.C.cmd == MachO::LC_DATA_IN_CODE) {
438       assert(!DataInCodeLoadCmd && "Multiple data in code tables");
439       DataInCodeLoadCmd = Load.Ptr;
440     } else if (Load.C.cmd == SegmentLoadType) {
441       uint32_t NumSections = getSegmentLoadCommandNumSections(this, Load);
442       for (unsigned J = 0; J < NumSections; ++J) {
443         const char *Sec = getSectionPtr(this, Load, J);
444         Sections.push_back(Sec);
445       }
446     }
447 
448     if (I == LoadCommandCount - 1)
449       break;
450     else
451       Load = getNextLoadCommandInfo(Load);
452   }
453 }
454 
455 void MachOObjectFile::moveSymbolNext(DataRefImpl &Symb) const {
456   unsigned SymbolTableEntrySize = is64Bit() ?
457     sizeof(MachO::nlist_64) :
458     sizeof(MachO::nlist);
459   Symb.p += SymbolTableEntrySize;
460 }
461 
462 error_code MachOObjectFile::getSymbolName(DataRefImpl Symb,
463                                           StringRef &Res) const {
464   StringRef StringTable = getStringTableData();
465   nlist_base Entry = getSymbolTableEntryBase(this, Symb);
466   const char *Start = &StringTable.data()[Entry.n_strx];
467   Res = StringRef(Start);
468   return object_error::success;
469 }
470 
471 error_code MachOObjectFile::getSymbolAddress(DataRefImpl Symb,
472                                              uint64_t &Res) const {
473   if (is64Bit()) {
474     MachO::nlist_64 Entry = getSymbol64TableEntry(Symb);
475     if ((Entry.n_type & MachO::N_TYPE) == MachO::N_UNDF &&
476         Entry.n_value == 0)
477       Res = UnknownAddressOrSize;
478     else
479       Res = Entry.n_value;
480   } else {
481     MachO::nlist Entry = getSymbolTableEntry(Symb);
482     if ((Entry.n_type & MachO::N_TYPE) == MachO::N_UNDF &&
483         Entry.n_value == 0)
484       Res = UnknownAddressOrSize;
485     else
486       Res = Entry.n_value;
487   }
488   return object_error::success;
489 }
490 
491 error_code MachOObjectFile::getSymbolAlignment(DataRefImpl DRI,
492                                                uint32_t &Result) const {
493   uint32_t flags = getSymbolFlags(DRI);
494   if (flags & SymbolRef::SF_Common) {
495     nlist_base Entry = getSymbolTableEntryBase(this, DRI);
496     Result = 1 << MachO::GET_COMM_ALIGN(Entry.n_desc);
497   } else {
498     Result = 0;
499   }
500   return object_error::success;
501 }
502 
503 error_code MachOObjectFile::getSymbolSize(DataRefImpl DRI,
504                                           uint64_t &Result) const {
505   uint64_t BeginOffset;
506   uint64_t EndOffset = 0;
507   uint8_t SectionIndex;
508 
509   nlist_base Entry = getSymbolTableEntryBase(this, DRI);
510   uint64_t Value;
511   getSymbolAddress(DRI, Value);
512 
513   BeginOffset = Value;
514 
515   SectionIndex = Entry.n_sect;
516   if (!SectionIndex) {
517     uint32_t flags = getSymbolFlags(DRI);
518     if (flags & SymbolRef::SF_Common)
519       Result = Value;
520     else
521       Result = UnknownAddressOrSize;
522     return object_error::success;
523   }
524   // Unfortunately symbols are unsorted so we need to touch all
525   // symbols from load command
526   for (const SymbolRef &Symbol : symbols()) {
527     DataRefImpl DRI = Symbol.getRawDataRefImpl();
528     Entry = getSymbolTableEntryBase(this, DRI);
529     getSymbolAddress(DRI, Value);
530     if (Entry.n_sect == SectionIndex && Value > BeginOffset)
531       if (!EndOffset || Value < EndOffset)
532         EndOffset = Value;
533   }
534   if (!EndOffset) {
535     uint64_t Size;
536     DataRefImpl Sec;
537     Sec.d.a = SectionIndex-1;
538     getSectionSize(Sec, Size);
539     getSectionAddress(Sec, EndOffset);
540     EndOffset += Size;
541   }
542   Result = EndOffset - BeginOffset;
543   return object_error::success;
544 }
545 
546 error_code MachOObjectFile::getSymbolType(DataRefImpl Symb,
547                                           SymbolRef::Type &Res) const {
548   nlist_base Entry = getSymbolTableEntryBase(this, Symb);
549   uint8_t n_type = Entry.n_type;
550 
551   Res = SymbolRef::ST_Other;
552 
553   // If this is a STAB debugging symbol, we can do nothing more.
554   if (n_type & MachO::N_STAB) {
555     Res = SymbolRef::ST_Debug;
556     return object_error::success;
557   }
558 
559   switch (n_type & MachO::N_TYPE) {
560     case MachO::N_UNDF :
561       Res = SymbolRef::ST_Unknown;
562       break;
563     case MachO::N_SECT :
564       Res = SymbolRef::ST_Function;
565       break;
566   }
567   return object_error::success;
568 }
569 
570 uint32_t MachOObjectFile::getSymbolFlags(DataRefImpl DRI) const {
571   nlist_base Entry = getSymbolTableEntryBase(this, DRI);
572 
573   uint8_t MachOType = Entry.n_type;
574   uint16_t MachOFlags = Entry.n_desc;
575 
576   uint32_t Result = SymbolRef::SF_None;
577 
578   if ((MachOType & MachO::N_TYPE) == MachO::N_UNDF)
579     Result |= SymbolRef::SF_Undefined;
580 
581   if (MachOType & MachO::N_STAB)
582     Result |= SymbolRef::SF_FormatSpecific;
583 
584   if (MachOType & MachO::N_EXT) {
585     Result |= SymbolRef::SF_Global;
586     if ((MachOType & MachO::N_TYPE) == MachO::N_UNDF) {
587       uint64_t Value;
588       getSymbolAddress(DRI, Value);
589       if (Value)
590         Result |= SymbolRef::SF_Common;
591     }
592   }
593 
594   if (MachOFlags & (MachO::N_WEAK_REF | MachO::N_WEAK_DEF))
595     Result |= SymbolRef::SF_Weak;
596 
597   if ((MachOType & MachO::N_TYPE) == MachO::N_ABS)
598     Result |= SymbolRef::SF_Absolute;
599 
600   return Result;
601 }
602 
603 error_code
604 MachOObjectFile::getSymbolSection(DataRefImpl Symb,
605                                   section_iterator &Res) const {
606   nlist_base Entry = getSymbolTableEntryBase(this, Symb);
607   uint8_t index = Entry.n_sect;
608 
609   if (index == 0) {
610     Res = section_end();
611   } else {
612     DataRefImpl DRI;
613     DRI.d.a = index - 1;
614     Res = section_iterator(SectionRef(DRI, this));
615   }
616 
617   return object_error::success;
618 }
619 
620 void MachOObjectFile::moveSectionNext(DataRefImpl &Sec) const {
621   Sec.d.a++;
622 }
623 
624 error_code
625 MachOObjectFile::getSectionName(DataRefImpl Sec, StringRef &Result) const {
626   ArrayRef<char> Raw = getSectionRawName(Sec);
627   Result = parseSegmentOrSectionName(Raw.data());
628   return object_error::success;
629 }
630 
631 error_code
632 MachOObjectFile::getSectionAddress(DataRefImpl Sec, uint64_t &Res) const {
633   if (is64Bit()) {
634     MachO::section_64 Sect = getSection64(Sec);
635     Res = Sect.addr;
636   } else {
637     MachO::section Sect = getSection(Sec);
638     Res = Sect.addr;
639   }
640   return object_error::success;
641 }
642 
643 error_code
644 MachOObjectFile::getSectionSize(DataRefImpl Sec, uint64_t &Res) const {
645   if (is64Bit()) {
646     MachO::section_64 Sect = getSection64(Sec);
647     Res = Sect.size;
648   } else {
649     MachO::section Sect = getSection(Sec);
650     Res = Sect.size;
651   }
652 
653   return object_error::success;
654 }
655 
656 error_code
657 MachOObjectFile::getSectionContents(DataRefImpl Sec, StringRef &Res) const {
658   uint32_t Offset;
659   uint64_t Size;
660 
661   if (is64Bit()) {
662     MachO::section_64 Sect = getSection64(Sec);
663     Offset = Sect.offset;
664     Size = Sect.size;
665   } else {
666     MachO::section Sect = getSection(Sec);
667     Offset = Sect.offset;
668     Size = Sect.size;
669   }
670 
671   Res = this->getData().substr(Offset, Size);
672   return object_error::success;
673 }
674 
675 error_code
676 MachOObjectFile::getSectionAlignment(DataRefImpl Sec, uint64_t &Res) const {
677   uint32_t Align;
678   if (is64Bit()) {
679     MachO::section_64 Sect = getSection64(Sec);
680     Align = Sect.align;
681   } else {
682     MachO::section Sect = getSection(Sec);
683     Align = Sect.align;
684   }
685 
686   Res = uint64_t(1) << Align;
687   return object_error::success;
688 }
689 
690 error_code
691 MachOObjectFile::isSectionText(DataRefImpl Sec, bool &Res) const {
692   uint32_t Flags = getSectionFlags(this, Sec);
693   Res = Flags & MachO::S_ATTR_PURE_INSTRUCTIONS;
694   return object_error::success;
695 }
696 
697 error_code MachOObjectFile::isSectionData(DataRefImpl Sec, bool &Result) const {
698   uint32_t Flags = getSectionFlags(this, Sec);
699   unsigned SectionType = Flags & MachO::SECTION_TYPE;
700   Result = !(Flags & MachO::S_ATTR_PURE_INSTRUCTIONS) &&
701            !(SectionType == MachO::S_ZEROFILL ||
702              SectionType == MachO::S_GB_ZEROFILL);
703   return object_error::success;
704 }
705 
706 error_code MachOObjectFile::isSectionBSS(DataRefImpl Sec, bool &Result) const {
707   uint32_t Flags = getSectionFlags(this, Sec);
708   unsigned SectionType = Flags & MachO::SECTION_TYPE;
709   Result = !(Flags & MachO::S_ATTR_PURE_INSTRUCTIONS) &&
710            (SectionType == MachO::S_ZEROFILL ||
711             SectionType == MachO::S_GB_ZEROFILL);
712   return object_error::success;
713 }
714 
715 error_code
716 MachOObjectFile::isSectionRequiredForExecution(DataRefImpl Sec,
717                                                bool &Result) const {
718   // FIXME: Unimplemented.
719   Result = true;
720   return object_error::success;
721 }
722 
723 error_code MachOObjectFile::isSectionVirtual(DataRefImpl Sec,
724                                              bool &Result) const {
725   // FIXME: Unimplemented.
726   Result = false;
727   return object_error::success;
728 }
729 
730 error_code
731 MachOObjectFile::isSectionZeroInit(DataRefImpl Sec, bool &Res) const {
732   uint32_t Flags = getSectionFlags(this, Sec);
733   unsigned SectionType = Flags & MachO::SECTION_TYPE;
734   Res = SectionType == MachO::S_ZEROFILL ||
735     SectionType == MachO::S_GB_ZEROFILL;
736   return object_error::success;
737 }
738 
739 error_code MachOObjectFile::isSectionReadOnlyData(DataRefImpl Sec,
740                                                   bool &Result) const {
741   // Consider using the code from isSectionText to look for __const sections.
742   // Alternately, emit S_ATTR_PURE_INSTRUCTIONS and/or S_ATTR_SOME_INSTRUCTIONS
743   // to use section attributes to distinguish code from data.
744 
745   // FIXME: Unimplemented.
746   Result = false;
747   return object_error::success;
748 }
749 
750 error_code
751 MachOObjectFile::sectionContainsSymbol(DataRefImpl Sec, DataRefImpl Symb,
752                                        bool &Result) const {
753   SymbolRef::Type ST;
754   this->getSymbolType(Symb, ST);
755   if (ST == SymbolRef::ST_Unknown) {
756     Result = false;
757     return object_error::success;
758   }
759 
760   uint64_t SectBegin, SectEnd;
761   getSectionAddress(Sec, SectBegin);
762   getSectionSize(Sec, SectEnd);
763   SectEnd += SectBegin;
764 
765   uint64_t SymAddr;
766   getSymbolAddress(Symb, SymAddr);
767   Result = (SymAddr >= SectBegin) && (SymAddr < SectEnd);
768 
769   return object_error::success;
770 }
771 
772 relocation_iterator MachOObjectFile::section_rel_begin(DataRefImpl Sec) const {
773   DataRefImpl Ret;
774   Ret.d.a = Sec.d.a;
775   Ret.d.b = 0;
776   return relocation_iterator(RelocationRef(Ret, this));
777 }
778 
779 relocation_iterator
780 MachOObjectFile::section_rel_end(DataRefImpl Sec) const {
781   uint32_t Num;
782   if (is64Bit()) {
783     MachO::section_64 Sect = getSection64(Sec);
784     Num = Sect.nreloc;
785   } else {
786     MachO::section Sect = getSection(Sec);
787     Num = Sect.nreloc;
788   }
789 
790   DataRefImpl Ret;
791   Ret.d.a = Sec.d.a;
792   Ret.d.b = Num;
793   return relocation_iterator(RelocationRef(Ret, this));
794 }
795 
796 void MachOObjectFile::moveRelocationNext(DataRefImpl &Rel) const {
797   ++Rel.d.b;
798 }
799 
800 error_code
801 MachOObjectFile::getRelocationAddress(DataRefImpl Rel, uint64_t &Res) const {
802   uint64_t Offset;
803   getRelocationOffset(Rel, Offset);
804 
805   DataRefImpl Sec;
806   Sec.d.a = Rel.d.a;
807   uint64_t SecAddress;
808   getSectionAddress(Sec, SecAddress);
809   Res = SecAddress + Offset;
810   return object_error::success;
811 }
812 
813 error_code MachOObjectFile::getRelocationOffset(DataRefImpl Rel,
814                                                 uint64_t &Res) const {
815   assert(getHeader().filetype == MachO::MH_OBJECT &&
816          "Only implemented for MH_OBJECT");
817   MachO::any_relocation_info RE = getRelocation(Rel);
818   Res = getAnyRelocationAddress(RE);
819   return object_error::success;
820 }
821 
822 symbol_iterator
823 MachOObjectFile::getRelocationSymbol(DataRefImpl Rel) const {
824   MachO::any_relocation_info RE = getRelocation(Rel);
825   uint32_t SymbolIdx = getPlainRelocationSymbolNum(RE);
826   bool isExtern = getPlainRelocationExternal(RE);
827   if (!isExtern)
828     return symbol_end();
829 
830   MachO::symtab_command S = getSymtabLoadCommand();
831   unsigned SymbolTableEntrySize = is64Bit() ?
832     sizeof(MachO::nlist_64) :
833     sizeof(MachO::nlist);
834   uint64_t Offset = S.symoff + SymbolIdx * SymbolTableEntrySize;
835   DataRefImpl Sym;
836   Sym.p = reinterpret_cast<uintptr_t>(getPtr(this, Offset));
837   return symbol_iterator(SymbolRef(Sym, this));
838 }
839 
840 error_code MachOObjectFile::getRelocationType(DataRefImpl Rel,
841                                               uint64_t &Res) const {
842   MachO::any_relocation_info RE = getRelocation(Rel);
843   Res = getAnyRelocationType(RE);
844   return object_error::success;
845 }
846 
847 error_code
848 MachOObjectFile::getRelocationTypeName(DataRefImpl Rel,
849                                        SmallVectorImpl<char> &Result) const {
850   StringRef res;
851   uint64_t RType;
852   getRelocationType(Rel, RType);
853 
854   unsigned Arch = this->getArch();
855 
856   switch (Arch) {
857     case Triple::x86: {
858       static const char *const Table[] =  {
859         "GENERIC_RELOC_VANILLA",
860         "GENERIC_RELOC_PAIR",
861         "GENERIC_RELOC_SECTDIFF",
862         "GENERIC_RELOC_PB_LA_PTR",
863         "GENERIC_RELOC_LOCAL_SECTDIFF",
864         "GENERIC_RELOC_TLV" };
865 
866       if (RType > 5)
867         res = "Unknown";
868       else
869         res = Table[RType];
870       break;
871     }
872     case Triple::x86_64: {
873       static const char *const Table[] =  {
874         "X86_64_RELOC_UNSIGNED",
875         "X86_64_RELOC_SIGNED",
876         "X86_64_RELOC_BRANCH",
877         "X86_64_RELOC_GOT_LOAD",
878         "X86_64_RELOC_GOT",
879         "X86_64_RELOC_SUBTRACTOR",
880         "X86_64_RELOC_SIGNED_1",
881         "X86_64_RELOC_SIGNED_2",
882         "X86_64_RELOC_SIGNED_4",
883         "X86_64_RELOC_TLV" };
884 
885       if (RType > 9)
886         res = "Unknown";
887       else
888         res = Table[RType];
889       break;
890     }
891     case Triple::arm: {
892       static const char *const Table[] =  {
893         "ARM_RELOC_VANILLA",
894         "ARM_RELOC_PAIR",
895         "ARM_RELOC_SECTDIFF",
896         "ARM_RELOC_LOCAL_SECTDIFF",
897         "ARM_RELOC_PB_LA_PTR",
898         "ARM_RELOC_BR24",
899         "ARM_THUMB_RELOC_BR22",
900         "ARM_THUMB_32BIT_BRANCH",
901         "ARM_RELOC_HALF",
902         "ARM_RELOC_HALF_SECTDIFF" };
903 
904       if (RType > 9)
905         res = "Unknown";
906       else
907         res = Table[RType];
908       break;
909     }
910     case Triple::arm64:
911     case Triple::aarch64: {
912       static const char *const Table[] = {
913         "ARM64_RELOC_UNSIGNED",           "ARM64_RELOC_SUBTRACTOR",
914         "ARM64_RELOC_BRANCH26",           "ARM64_RELOC_PAGE21",
915         "ARM64_RELOC_PAGEOFF12",          "ARM64_RELOC_GOT_LOAD_PAGE21",
916         "ARM64_RELOC_GOT_LOAD_PAGEOFF12", "ARM64_RELOC_POINTER_TO_GOT",
917         "ARM64_RELOC_TLVP_LOAD_PAGE21",   "ARM64_RELOC_TLVP_LOAD_PAGEOFF12",
918         "ARM64_RELOC_ADDEND"
919       };
920 
921       if (RType >= array_lengthof(Table))
922         res = "Unknown";
923       else
924         res = Table[RType];
925       break;
926     }
927     case Triple::ppc: {
928       static const char *const Table[] =  {
929         "PPC_RELOC_VANILLA",
930         "PPC_RELOC_PAIR",
931         "PPC_RELOC_BR14",
932         "PPC_RELOC_BR24",
933         "PPC_RELOC_HI16",
934         "PPC_RELOC_LO16",
935         "PPC_RELOC_HA16",
936         "PPC_RELOC_LO14",
937         "PPC_RELOC_SECTDIFF",
938         "PPC_RELOC_PB_LA_PTR",
939         "PPC_RELOC_HI16_SECTDIFF",
940         "PPC_RELOC_LO16_SECTDIFF",
941         "PPC_RELOC_HA16_SECTDIFF",
942         "PPC_RELOC_JBSR",
943         "PPC_RELOC_LO14_SECTDIFF",
944         "PPC_RELOC_LOCAL_SECTDIFF" };
945 
946       if (RType > 15)
947         res = "Unknown";
948       else
949         res = Table[RType];
950       break;
951     }
952     case Triple::UnknownArch:
953       res = "Unknown";
954       break;
955   }
956   Result.append(res.begin(), res.end());
957   return object_error::success;
958 }
959 
960 error_code
961 MachOObjectFile::getRelocationValueString(DataRefImpl Rel,
962                                           SmallVectorImpl<char> &Result) const {
963   MachO::any_relocation_info RE = getRelocation(Rel);
964 
965   unsigned Arch = this->getArch();
966 
967   std::string fmtbuf;
968   raw_string_ostream fmt(fmtbuf);
969   unsigned Type = this->getAnyRelocationType(RE);
970   bool IsPCRel = this->getAnyRelocationPCRel(RE);
971 
972   // Determine any addends that should be displayed with the relocation.
973   // These require decoding the relocation type, which is triple-specific.
974 
975   // X86_64 has entirely custom relocation types.
976   if (Arch == Triple::x86_64) {
977     bool isPCRel = getAnyRelocationPCRel(RE);
978 
979     switch (Type) {
980       case MachO::X86_64_RELOC_GOT_LOAD:
981       case MachO::X86_64_RELOC_GOT: {
982         printRelocationTargetName(this, RE, fmt);
983         fmt << "@GOT";
984         if (isPCRel) fmt << "PCREL";
985         break;
986       }
987       case MachO::X86_64_RELOC_SUBTRACTOR: {
988         DataRefImpl RelNext = Rel;
989         moveRelocationNext(RelNext);
990         MachO::any_relocation_info RENext = getRelocation(RelNext);
991 
992         // X86_64_RELOC_SUBTRACTOR must be followed by a relocation of type
993         // X86_64_RELOC_UNSIGNED.
994         // NOTE: Scattered relocations don't exist on x86_64.
995         unsigned RType = getAnyRelocationType(RENext);
996         if (RType != MachO::X86_64_RELOC_UNSIGNED)
997           report_fatal_error("Expected X86_64_RELOC_UNSIGNED after "
998                              "X86_64_RELOC_SUBTRACTOR.");
999 
1000         // The X86_64_RELOC_UNSIGNED contains the minuend symbol;
1001         // X86_64_RELOC_SUBTRACTOR contains the subtrahend.
1002         printRelocationTargetName(this, RENext, fmt);
1003         fmt << "-";
1004         printRelocationTargetName(this, RE, fmt);
1005         break;
1006       }
1007       case MachO::X86_64_RELOC_TLV:
1008         printRelocationTargetName(this, RE, fmt);
1009         fmt << "@TLV";
1010         if (isPCRel) fmt << "P";
1011         break;
1012       case MachO::X86_64_RELOC_SIGNED_1:
1013         printRelocationTargetName(this, RE, fmt);
1014         fmt << "-1";
1015         break;
1016       case MachO::X86_64_RELOC_SIGNED_2:
1017         printRelocationTargetName(this, RE, fmt);
1018         fmt << "-2";
1019         break;
1020       case MachO::X86_64_RELOC_SIGNED_4:
1021         printRelocationTargetName(this, RE, fmt);
1022         fmt << "-4";
1023         break;
1024       default:
1025         printRelocationTargetName(this, RE, fmt);
1026         break;
1027     }
1028   // X86 and ARM share some relocation types in common.
1029   } else if (Arch == Triple::x86 || Arch == Triple::arm ||
1030              Arch == Triple::ppc) {
1031     // Generic relocation types...
1032     switch (Type) {
1033       case MachO::GENERIC_RELOC_PAIR: // prints no info
1034         return object_error::success;
1035       case MachO::GENERIC_RELOC_SECTDIFF: {
1036         DataRefImpl RelNext = Rel;
1037         moveRelocationNext(RelNext);
1038         MachO::any_relocation_info RENext = getRelocation(RelNext);
1039 
1040         // X86 sect diff's must be followed by a relocation of type
1041         // GENERIC_RELOC_PAIR.
1042         unsigned RType = getAnyRelocationType(RENext);
1043 
1044         if (RType != MachO::GENERIC_RELOC_PAIR)
1045           report_fatal_error("Expected GENERIC_RELOC_PAIR after "
1046                              "GENERIC_RELOC_SECTDIFF.");
1047 
1048         printRelocationTargetName(this, RE, fmt);
1049         fmt << "-";
1050         printRelocationTargetName(this, RENext, fmt);
1051         break;
1052       }
1053     }
1054 
1055     if (Arch == Triple::x86 || Arch == Triple::ppc) {
1056       switch (Type) {
1057         case MachO::GENERIC_RELOC_LOCAL_SECTDIFF: {
1058           DataRefImpl RelNext = Rel;
1059           moveRelocationNext(RelNext);
1060           MachO::any_relocation_info RENext = getRelocation(RelNext);
1061 
1062           // X86 sect diff's must be followed by a relocation of type
1063           // GENERIC_RELOC_PAIR.
1064           unsigned RType = getAnyRelocationType(RENext);
1065           if (RType != MachO::GENERIC_RELOC_PAIR)
1066             report_fatal_error("Expected GENERIC_RELOC_PAIR after "
1067                                "GENERIC_RELOC_LOCAL_SECTDIFF.");
1068 
1069           printRelocationTargetName(this, RE, fmt);
1070           fmt << "-";
1071           printRelocationTargetName(this, RENext, fmt);
1072           break;
1073         }
1074         case MachO::GENERIC_RELOC_TLV: {
1075           printRelocationTargetName(this, RE, fmt);
1076           fmt << "@TLV";
1077           if (IsPCRel) fmt << "P";
1078           break;
1079         }
1080         default:
1081           printRelocationTargetName(this, RE, fmt);
1082       }
1083     } else { // ARM-specific relocations
1084       switch (Type) {
1085         case MachO::ARM_RELOC_HALF:
1086         case MachO::ARM_RELOC_HALF_SECTDIFF: {
1087           // Half relocations steal a bit from the length field to encode
1088           // whether this is an upper16 or a lower16 relocation.
1089           bool isUpper = getAnyRelocationLength(RE) >> 1;
1090 
1091           if (isUpper)
1092             fmt << ":upper16:(";
1093           else
1094             fmt << ":lower16:(";
1095           printRelocationTargetName(this, RE, fmt);
1096 
1097           DataRefImpl RelNext = Rel;
1098           moveRelocationNext(RelNext);
1099           MachO::any_relocation_info RENext = getRelocation(RelNext);
1100 
1101           // ARM half relocs must be followed by a relocation of type
1102           // ARM_RELOC_PAIR.
1103           unsigned RType = getAnyRelocationType(RENext);
1104           if (RType != MachO::ARM_RELOC_PAIR)
1105             report_fatal_error("Expected ARM_RELOC_PAIR after "
1106                                "ARM_RELOC_HALF");
1107 
1108           // NOTE: The half of the target virtual address is stashed in the
1109           // address field of the secondary relocation, but we can't reverse
1110           // engineer the constant offset from it without decoding the movw/movt
1111           // instruction to find the other half in its immediate field.
1112 
1113           // ARM_RELOC_HALF_SECTDIFF encodes the second section in the
1114           // symbol/section pointer of the follow-on relocation.
1115           if (Type == MachO::ARM_RELOC_HALF_SECTDIFF) {
1116             fmt << "-";
1117             printRelocationTargetName(this, RENext, fmt);
1118           }
1119 
1120           fmt << ")";
1121           break;
1122         }
1123         default: {
1124           printRelocationTargetName(this, RE, fmt);
1125         }
1126       }
1127     }
1128   } else
1129     printRelocationTargetName(this, RE, fmt);
1130 
1131   fmt.flush();
1132   Result.append(fmtbuf.begin(), fmtbuf.end());
1133   return object_error::success;
1134 }
1135 
1136 error_code
1137 MachOObjectFile::getRelocationHidden(DataRefImpl Rel, bool &Result) const {
1138   unsigned Arch = getArch();
1139   uint64_t Type;
1140   getRelocationType(Rel, Type);
1141 
1142   Result = false;
1143 
1144   // On arches that use the generic relocations, GENERIC_RELOC_PAIR
1145   // is always hidden.
1146   if (Arch == Triple::x86 || Arch == Triple::arm || Arch == Triple::ppc) {
1147     if (Type == MachO::GENERIC_RELOC_PAIR) Result = true;
1148   } else if (Arch == Triple::x86_64) {
1149     // On x86_64, X86_64_RELOC_UNSIGNED is hidden only when it follows
1150     // an X86_64_RELOC_SUBTRACTOR.
1151     if (Type == MachO::X86_64_RELOC_UNSIGNED && Rel.d.a > 0) {
1152       DataRefImpl RelPrev = Rel;
1153       RelPrev.d.a--;
1154       uint64_t PrevType;
1155       getRelocationType(RelPrev, PrevType);
1156       if (PrevType == MachO::X86_64_RELOC_SUBTRACTOR)
1157         Result = true;
1158     }
1159   }
1160 
1161   return object_error::success;
1162 }
1163 
1164 error_code MachOObjectFile::getLibraryNext(DataRefImpl LibData,
1165                                            LibraryRef &Res) const {
1166   report_fatal_error("Needed libraries unimplemented in MachOObjectFile");
1167 }
1168 
1169 error_code MachOObjectFile::getLibraryPath(DataRefImpl LibData,
1170                                            StringRef &Res) const {
1171   report_fatal_error("Needed libraries unimplemented in MachOObjectFile");
1172 }
1173 
1174 basic_symbol_iterator MachOObjectFile::symbol_begin_impl() const {
1175   return getSymbolByIndex(0);
1176 }
1177 
1178 basic_symbol_iterator MachOObjectFile::symbol_end_impl() const {
1179   DataRefImpl DRI;
1180   if (!SymtabLoadCmd)
1181     return basic_symbol_iterator(SymbolRef(DRI, this));
1182 
1183   MachO::symtab_command Symtab = getSymtabLoadCommand();
1184   unsigned SymbolTableEntrySize = is64Bit() ?
1185     sizeof(MachO::nlist_64) :
1186     sizeof(MachO::nlist);
1187   unsigned Offset = Symtab.symoff +
1188     Symtab.nsyms * SymbolTableEntrySize;
1189   DRI.p = reinterpret_cast<uintptr_t>(getPtr(this, Offset));
1190   return basic_symbol_iterator(SymbolRef(DRI, this));
1191 }
1192 
1193 basic_symbol_iterator MachOObjectFile::getSymbolByIndex(unsigned Index) const {
1194   DataRefImpl DRI;
1195   if (!SymtabLoadCmd)
1196     return basic_symbol_iterator(SymbolRef(DRI, this));
1197 
1198   MachO::symtab_command Symtab = getSymtabLoadCommand();
1199   assert(Index < Symtab.nsyms && "Requested symbol index is out of range.");
1200   unsigned SymbolTableEntrySize =
1201     is64Bit() ? sizeof(MachO::nlist_64) : sizeof(MachO::nlist);
1202   DRI.p = reinterpret_cast<uintptr_t>(getPtr(this, Symtab.symoff));
1203   DRI.p += Index * SymbolTableEntrySize;
1204   return basic_symbol_iterator(SymbolRef(DRI, this));
1205 }
1206 
1207 section_iterator MachOObjectFile::section_begin() const {
1208   DataRefImpl DRI;
1209   return section_iterator(SectionRef(DRI, this));
1210 }
1211 
1212 section_iterator MachOObjectFile::section_end() const {
1213   DataRefImpl DRI;
1214   DRI.d.a = Sections.size();
1215   return section_iterator(SectionRef(DRI, this));
1216 }
1217 
1218 library_iterator MachOObjectFile::needed_library_begin() const {
1219   // TODO: implement
1220   report_fatal_error("Needed libraries unimplemented in MachOObjectFile");
1221 }
1222 
1223 library_iterator MachOObjectFile::needed_library_end() const {
1224   // TODO: implement
1225   report_fatal_error("Needed libraries unimplemented in MachOObjectFile");
1226 }
1227 
1228 uint8_t MachOObjectFile::getBytesInAddress() const {
1229   return is64Bit() ? 8 : 4;
1230 }
1231 
1232 StringRef MachOObjectFile::getFileFormatName() const {
1233   unsigned CPUType = getCPUType(this);
1234   if (!is64Bit()) {
1235     switch (CPUType) {
1236     case llvm::MachO::CPU_TYPE_I386:
1237       return "Mach-O 32-bit i386";
1238     case llvm::MachO::CPU_TYPE_ARM:
1239       return "Mach-O arm";
1240     case llvm::MachO::CPU_TYPE_POWERPC:
1241       return "Mach-O 32-bit ppc";
1242     default:
1243       assert((CPUType & llvm::MachO::CPU_ARCH_ABI64) == 0 &&
1244              "64-bit object file when we're not 64-bit?");
1245       return "Mach-O 32-bit unknown";
1246     }
1247   }
1248 
1249   // Make sure the cpu type has the correct mask.
1250   assert((CPUType & llvm::MachO::CPU_ARCH_ABI64)
1251          == llvm::MachO::CPU_ARCH_ABI64 &&
1252          "32-bit object file when we're 64-bit?");
1253 
1254   switch (CPUType) {
1255   case llvm::MachO::CPU_TYPE_X86_64:
1256     return "Mach-O 64-bit x86-64";
1257   case llvm::MachO::CPU_TYPE_ARM64:
1258     return "Mach-O arm64";
1259   case llvm::MachO::CPU_TYPE_POWERPC64:
1260     return "Mach-O 64-bit ppc64";
1261   default:
1262     return "Mach-O 64-bit unknown";
1263   }
1264 }
1265 
1266 Triple::ArchType MachOObjectFile::getArch(uint32_t CPUType) {
1267   switch (CPUType) {
1268   case llvm::MachO::CPU_TYPE_I386:
1269     return Triple::x86;
1270   case llvm::MachO::CPU_TYPE_X86_64:
1271     return Triple::x86_64;
1272   case llvm::MachO::CPU_TYPE_ARM:
1273     return Triple::arm;
1274   case llvm::MachO::CPU_TYPE_ARM64:
1275     return Triple::arm64;
1276   case llvm::MachO::CPU_TYPE_POWERPC:
1277     return Triple::ppc;
1278   case llvm::MachO::CPU_TYPE_POWERPC64:
1279     return Triple::ppc64;
1280   default:
1281     return Triple::UnknownArch;
1282   }
1283 }
1284 
1285 unsigned MachOObjectFile::getArch() const {
1286   return getArch(getCPUType(this));
1287 }
1288 
1289 StringRef MachOObjectFile::getLoadName() const {
1290   // TODO: Implement
1291   report_fatal_error("get_load_name() unimplemented in MachOObjectFile");
1292 }
1293 
1294 relocation_iterator MachOObjectFile::section_rel_begin(unsigned Index) const {
1295   DataRefImpl DRI;
1296   DRI.d.a = Index;
1297   return section_rel_begin(DRI);
1298 }
1299 
1300 relocation_iterator MachOObjectFile::section_rel_end(unsigned Index) const {
1301   DataRefImpl DRI;
1302   DRI.d.a = Index;
1303   return section_rel_end(DRI);
1304 }
1305 
1306 dice_iterator MachOObjectFile::begin_dices() const {
1307   DataRefImpl DRI;
1308   if (!DataInCodeLoadCmd)
1309     return dice_iterator(DiceRef(DRI, this));
1310 
1311   MachO::linkedit_data_command DicLC = getDataInCodeLoadCommand();
1312   DRI.p = reinterpret_cast<uintptr_t>(getPtr(this, DicLC.dataoff));
1313   return dice_iterator(DiceRef(DRI, this));
1314 }
1315 
1316 dice_iterator MachOObjectFile::end_dices() const {
1317   DataRefImpl DRI;
1318   if (!DataInCodeLoadCmd)
1319     return dice_iterator(DiceRef(DRI, this));
1320 
1321   MachO::linkedit_data_command DicLC = getDataInCodeLoadCommand();
1322   unsigned Offset = DicLC.dataoff + DicLC.datasize;
1323   DRI.p = reinterpret_cast<uintptr_t>(getPtr(this, Offset));
1324   return dice_iterator(DiceRef(DRI, this));
1325 }
1326 
1327 StringRef
1328 MachOObjectFile::getSectionFinalSegmentName(DataRefImpl Sec) const {
1329   ArrayRef<char> Raw = getSectionRawFinalSegmentName(Sec);
1330   return parseSegmentOrSectionName(Raw.data());
1331 }
1332 
1333 ArrayRef<char>
1334 MachOObjectFile::getSectionRawName(DataRefImpl Sec) const {
1335   const section_base *Base =
1336     reinterpret_cast<const section_base *>(Sections[Sec.d.a]);
1337   return ArrayRef<char>(Base->sectname);
1338 }
1339 
1340 ArrayRef<char>
1341 MachOObjectFile::getSectionRawFinalSegmentName(DataRefImpl Sec) const {
1342   const section_base *Base =
1343     reinterpret_cast<const section_base *>(Sections[Sec.d.a]);
1344   return ArrayRef<char>(Base->segname);
1345 }
1346 
1347 bool
1348 MachOObjectFile::isRelocationScattered(const MachO::any_relocation_info &RE)
1349   const {
1350   if (getCPUType(this) == MachO::CPU_TYPE_X86_64)
1351     return false;
1352   return getPlainRelocationAddress(RE) & MachO::R_SCATTERED;
1353 }
1354 
1355 unsigned MachOObjectFile::getPlainRelocationSymbolNum(
1356     const MachO::any_relocation_info &RE) const {
1357   if (isLittleEndian())
1358     return RE.r_word1 & 0xffffff;
1359   return RE.r_word1 >> 8;
1360 }
1361 
1362 bool MachOObjectFile::getPlainRelocationExternal(
1363     const MachO::any_relocation_info &RE) const {
1364   if (isLittleEndian())
1365     return (RE.r_word1 >> 27) & 1;
1366   return (RE.r_word1 >> 4) & 1;
1367 }
1368 
1369 bool MachOObjectFile::getScatteredRelocationScattered(
1370     const MachO::any_relocation_info &RE) const {
1371   return RE.r_word0 >> 31;
1372 }
1373 
1374 uint32_t MachOObjectFile::getScatteredRelocationValue(
1375     const MachO::any_relocation_info &RE) const {
1376   return RE.r_word1;
1377 }
1378 
1379 unsigned MachOObjectFile::getAnyRelocationAddress(
1380     const MachO::any_relocation_info &RE) const {
1381   if (isRelocationScattered(RE))
1382     return getScatteredRelocationAddress(RE);
1383   return getPlainRelocationAddress(RE);
1384 }
1385 
1386 unsigned MachOObjectFile::getAnyRelocationPCRel(
1387     const MachO::any_relocation_info &RE) const {
1388   if (isRelocationScattered(RE))
1389     return getScatteredRelocationPCRel(this, RE);
1390   return getPlainRelocationPCRel(this, RE);
1391 }
1392 
1393 unsigned MachOObjectFile::getAnyRelocationLength(
1394     const MachO::any_relocation_info &RE) const {
1395   if (isRelocationScattered(RE))
1396     return getScatteredRelocationLength(RE);
1397   return getPlainRelocationLength(this, RE);
1398 }
1399 
1400 unsigned
1401 MachOObjectFile::getAnyRelocationType(
1402                                    const MachO::any_relocation_info &RE) const {
1403   if (isRelocationScattered(RE))
1404     return getScatteredRelocationType(RE);
1405   return getPlainRelocationType(this, RE);
1406 }
1407 
1408 SectionRef
1409 MachOObjectFile::getRelocationSection(
1410                                    const MachO::any_relocation_info &RE) const {
1411   if (isRelocationScattered(RE) || getPlainRelocationExternal(RE))
1412     return *section_end();
1413   unsigned SecNum = getPlainRelocationSymbolNum(RE) - 1;
1414   DataRefImpl DRI;
1415   DRI.d.a = SecNum;
1416   return SectionRef(DRI, this);
1417 }
1418 
1419 MachOObjectFile::LoadCommandInfo
1420 MachOObjectFile::getFirstLoadCommandInfo() const {
1421   MachOObjectFile::LoadCommandInfo Load;
1422 
1423   unsigned HeaderSize = is64Bit() ? sizeof(MachO::mach_header_64) :
1424                                     sizeof(MachO::mach_header);
1425   Load.Ptr = getPtr(this, HeaderSize);
1426   Load.C = getStruct<MachO::load_command>(this, Load.Ptr);
1427   return Load;
1428 }
1429 
1430 MachOObjectFile::LoadCommandInfo
1431 MachOObjectFile::getNextLoadCommandInfo(const LoadCommandInfo &L) const {
1432   MachOObjectFile::LoadCommandInfo Next;
1433   Next.Ptr = L.Ptr + L.C.cmdsize;
1434   Next.C = getStruct<MachO::load_command>(this, Next.Ptr);
1435   return Next;
1436 }
1437 
1438 MachO::section MachOObjectFile::getSection(DataRefImpl DRI) const {
1439   return getStruct<MachO::section>(this, Sections[DRI.d.a]);
1440 }
1441 
1442 MachO::section_64 MachOObjectFile::getSection64(DataRefImpl DRI) const {
1443   return getStruct<MachO::section_64>(this, Sections[DRI.d.a]);
1444 }
1445 
1446 MachO::section MachOObjectFile::getSection(const LoadCommandInfo &L,
1447                                            unsigned Index) const {
1448   const char *Sec = getSectionPtr(this, L, Index);
1449   return getStruct<MachO::section>(this, Sec);
1450 }
1451 
1452 MachO::section_64 MachOObjectFile::getSection64(const LoadCommandInfo &L,
1453                                                 unsigned Index) const {
1454   const char *Sec = getSectionPtr(this, L, Index);
1455   return getStruct<MachO::section_64>(this, Sec);
1456 }
1457 
1458 MachO::nlist
1459 MachOObjectFile::getSymbolTableEntry(DataRefImpl DRI) const {
1460   const char *P = reinterpret_cast<const char *>(DRI.p);
1461   return getStruct<MachO::nlist>(this, P);
1462 }
1463 
1464 MachO::nlist_64
1465 MachOObjectFile::getSymbol64TableEntry(DataRefImpl DRI) const {
1466   const char *P = reinterpret_cast<const char *>(DRI.p);
1467   return getStruct<MachO::nlist_64>(this, P);
1468 }
1469 
1470 MachO::linkedit_data_command
1471 MachOObjectFile::getLinkeditDataLoadCommand(const LoadCommandInfo &L) const {
1472   return getStruct<MachO::linkedit_data_command>(this, L.Ptr);
1473 }
1474 
1475 MachO::segment_command
1476 MachOObjectFile::getSegmentLoadCommand(const LoadCommandInfo &L) const {
1477   return getStruct<MachO::segment_command>(this, L.Ptr);
1478 }
1479 
1480 MachO::segment_command_64
1481 MachOObjectFile::getSegment64LoadCommand(const LoadCommandInfo &L) const {
1482   return getStruct<MachO::segment_command_64>(this, L.Ptr);
1483 }
1484 
1485 MachO::linker_options_command
1486 MachOObjectFile::getLinkerOptionsLoadCommand(const LoadCommandInfo &L) const {
1487   return getStruct<MachO::linker_options_command>(this, L.Ptr);
1488 }
1489 
1490 MachO::version_min_command
1491 MachOObjectFile::getVersionMinLoadCommand(const LoadCommandInfo &L) const {
1492   return getStruct<MachO::version_min_command>(this, L.Ptr);
1493 }
1494 
1495 MachO::any_relocation_info
1496 MachOObjectFile::getRelocation(DataRefImpl Rel) const {
1497   DataRefImpl Sec;
1498   Sec.d.a = Rel.d.a;
1499   uint32_t Offset;
1500   if (is64Bit()) {
1501     MachO::section_64 Sect = getSection64(Sec);
1502     Offset = Sect.reloff;
1503   } else {
1504     MachO::section Sect = getSection(Sec);
1505     Offset = Sect.reloff;
1506   }
1507 
1508   auto P = reinterpret_cast<const MachO::any_relocation_info *>(
1509       getPtr(this, Offset)) + Rel.d.b;
1510   return getStruct<MachO::any_relocation_info>(
1511       this, reinterpret_cast<const char *>(P));
1512 }
1513 
1514 MachO::data_in_code_entry
1515 MachOObjectFile::getDice(DataRefImpl Rel) const {
1516   const char *P = reinterpret_cast<const char *>(Rel.p);
1517   return getStruct<MachO::data_in_code_entry>(this, P);
1518 }
1519 
1520 MachO::mach_header MachOObjectFile::getHeader() const {
1521   return getStruct<MachO::mach_header>(this, getPtr(this, 0));
1522 }
1523 
1524 MachO::mach_header_64 MachOObjectFile::getHeader64() const {
1525   return getStruct<MachO::mach_header_64>(this, getPtr(this, 0));
1526 }
1527 
1528 uint32_t MachOObjectFile::getIndirectSymbolTableEntry(
1529                                              const MachO::dysymtab_command &DLC,
1530                                              unsigned Index) const {
1531   uint64_t Offset = DLC.indirectsymoff + Index * sizeof(uint32_t);
1532   return getStruct<uint32_t>(this, getPtr(this, Offset));
1533 }
1534 
1535 MachO::data_in_code_entry
1536 MachOObjectFile::getDataInCodeTableEntry(uint32_t DataOffset,
1537                                          unsigned Index) const {
1538   uint64_t Offset = DataOffset + Index * sizeof(MachO::data_in_code_entry);
1539   return getStruct<MachO::data_in_code_entry>(this, getPtr(this, Offset));
1540 }
1541 
1542 MachO::symtab_command MachOObjectFile::getSymtabLoadCommand() const {
1543   return getStruct<MachO::symtab_command>(this, SymtabLoadCmd);
1544 }
1545 
1546 MachO::dysymtab_command MachOObjectFile::getDysymtabLoadCommand() const {
1547   return getStruct<MachO::dysymtab_command>(this, DysymtabLoadCmd);
1548 }
1549 
1550 MachO::linkedit_data_command
1551 MachOObjectFile::getDataInCodeLoadCommand() const {
1552   if (DataInCodeLoadCmd)
1553     return getStruct<MachO::linkedit_data_command>(this, DataInCodeLoadCmd);
1554 
1555   // If there is no DataInCodeLoadCmd return a load command with zero'ed fields.
1556   MachO::linkedit_data_command Cmd;
1557   Cmd.cmd = MachO::LC_DATA_IN_CODE;
1558   Cmd.cmdsize = sizeof(MachO::linkedit_data_command);
1559   Cmd.dataoff = 0;
1560   Cmd.datasize = 0;
1561   return Cmd;
1562 }
1563 
1564 StringRef MachOObjectFile::getStringTableData() const {
1565   MachO::symtab_command S = getSymtabLoadCommand();
1566   return getData().substr(S.stroff, S.strsize);
1567 }
1568 
1569 bool MachOObjectFile::is64Bit() const {
1570   return getType() == getMachOType(false, true) ||
1571     getType() == getMachOType(true, true);
1572 }
1573 
1574 void MachOObjectFile::ReadULEB128s(uint64_t Index,
1575                                    SmallVectorImpl<uint64_t> &Out) const {
1576   DataExtractor extractor(ObjectFile::getData(), true, 0);
1577 
1578   uint32_t offset = Index;
1579   uint64_t data = 0;
1580   while (uint64_t delta = extractor.getULEB128(&offset)) {
1581     data += delta;
1582     Out.push_back(data);
1583   }
1584 }
1585 
1586 ErrorOr<ObjectFile *> ObjectFile::createMachOObjectFile(MemoryBuffer *Buffer,
1587                                                         bool BufferOwned) {
1588   StringRef Magic = Buffer->getBuffer().slice(0, 4);
1589   error_code EC;
1590   std::unique_ptr<MachOObjectFile> Ret;
1591   if (Magic == "\xFE\xED\xFA\xCE")
1592     Ret.reset(new MachOObjectFile(Buffer, false, false, EC, BufferOwned));
1593   else if (Magic == "\xCE\xFA\xED\xFE")
1594     Ret.reset(new MachOObjectFile(Buffer, true, false, EC, BufferOwned));
1595   else if (Magic == "\xFE\xED\xFA\xCF")
1596     Ret.reset(new MachOObjectFile(Buffer, false, true, EC, BufferOwned));
1597   else if (Magic == "\xCF\xFA\xED\xFE")
1598     Ret.reset(new MachOObjectFile(Buffer, true, true, EC, BufferOwned));
1599   else {
1600     delete Buffer;
1601     return object_error::parse_failed;
1602   }
1603 
1604   if (EC)
1605     return EC;
1606   return Ret.release();
1607 }
1608 
1609 } // end namespace object
1610 } // end namespace llvm
1611