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