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