LCOV - code coverage report
Current view: top level - lib/Object - MachOObjectFile.cpp (source / functions) Hit Total Coverage
Test: llvm-toolchain.info Lines: 2235 2585 86.5 %
Date: 2018-02-23 15:42:53 Functions: 249 262 95.0 %
Legend: Lines: hit not hit

          Line data    Source code
       1             : //===- MachOObjectFile.cpp - Mach-O object file binding -------------------===//
       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/ADT/ArrayRef.h"
      16             : #include "llvm/ADT/None.h"
      17             : #include "llvm/ADT/STLExtras.h"
      18             : #include "llvm/ADT/SmallVector.h"
      19             : #include "llvm/ADT/StringRef.h"
      20             : #include "llvm/ADT/StringSwitch.h"
      21             : #include "llvm/ADT/Triple.h"
      22             : #include "llvm/ADT/Twine.h"
      23             : #include "llvm/BinaryFormat/MachO.h"
      24             : #include "llvm/Object/Error.h"
      25             : #include "llvm/Object/MachO.h"
      26             : #include "llvm/Object/ObjectFile.h"
      27             : #include "llvm/Object/SymbolicFile.h"
      28             : #include "llvm/Support/DataExtractor.h"
      29             : #include "llvm/Support/Debug.h"
      30             : #include "llvm/Support/Error.h"
      31             : #include "llvm/Support/ErrorHandling.h"
      32             : #include "llvm/Support/Format.h"
      33             : #include "llvm/Support/Host.h"
      34             : #include "llvm/Support/LEB128.h"
      35             : #include "llvm/Support/MemoryBuffer.h"
      36             : #include "llvm/Support/SwapByteOrder.h"
      37             : #include "llvm/Support/raw_ostream.h"
      38             : #include <algorithm>
      39             : #include <cassert>
      40             : #include <cstddef>
      41             : #include <cstdint>
      42             : #include <cstring>
      43             : #include <limits>
      44             : #include <list>
      45             : #include <memory>
      46             : #include <string>
      47             : #include <system_error>
      48             : 
      49             : using namespace llvm;
      50             : using namespace object;
      51             : 
      52             : namespace {
      53             : 
      54             :   struct section_base {
      55             :     char sectname[16];
      56             :     char segname[16];
      57             :   };
      58             : 
      59             : } // end anonymous namespace
      60             : 
      61         216 : static Error malformedError(const Twine &Msg) {
      62         216 :   return make_error<GenericBinaryError>("truncated or malformed object (" +
      63         432 :                                             Msg + ")",
      64         216 :                                         object_error::parse_failed);
      65             : }
      66             : 
      67             : // FIXME: Replace all uses of this function with getStructOrErr.
      68             : template <typename T>
      69      118162 : static T getStruct(const MachOObjectFile &O, const char *P) {
      70             :   // Don't read before the beginning or past the end of the file
      71      354486 :   if (P < O.getData().begin() || P + sizeof(T) > O.getData().end())
      72           0 :     report_fatal_error("Malformed MachO file.");
      73             : 
      74             :   T Cmd;
      75      118162 :   memcpy(&Cmd, P, sizeof(T));
      76      118162 :   if (O.isLittleEndian() != sys::IsLittleEndianHost)
      77         412 :     MachO::swapStruct(Cmd);
      78      118162 :   return Cmd;
      79             : }
      80             : 
      81             : template <typename T>
      82       14981 : static Expected<T> getStructOrErr(const MachOObjectFile &O, const char *P) {
      83             :   // Don't read before the beginning or past the end of the file
      84       44943 :   if (P < O.getData().begin() || P + sizeof(T) > O.getData().end())
      85           0 :     return malformedError("Structure read out-of-range");
      86             : 
      87             :   T Cmd;
      88       14981 :   memcpy(&Cmd, P, sizeof(T));
      89       14981 :   if (O.isLittleEndian() != sys::IsLittleEndianHost)
      90          20 :     MachO::swapStruct(Cmd);
      91             :   return Cmd;
      92             : }
      93             : 
      94             : static const char *
      95             : getSectionPtr(const MachOObjectFile &O, MachOObjectFile::LoadCommandInfo L,
      96             :               unsigned Sec) {
      97       15178 :   uintptr_t CommandAddr = reinterpret_cast<uintptr_t>(L.Ptr);
      98             : 
      99       15178 :   bool Is64 = O.is64Bit();
     100       15178 :   unsigned SegmentLoadSize = Is64 ? sizeof(MachO::segment_command_64) :
     101             :                                     sizeof(MachO::segment_command);
     102       15178 :   unsigned SectionSize = Is64 ? sizeof(MachO::section_64) :
     103             :                                 sizeof(MachO::section);
     104             : 
     105       15178 :   uintptr_t SectionAddr = CommandAddr + SegmentLoadSize + Sec * SectionSize;
     106       15178 :   return reinterpret_cast<const char*>(SectionAddr);
     107             : }
     108             : 
     109       16018 : static const char *getPtr(const MachOObjectFile &O, size_t Offset) {
     110       32036 :   return O.getData().substr(Offset, 1).data();
     111             : }
     112             : 
     113             : static MachO::nlist_base
     114             : getSymbolTableEntryBase(const MachOObjectFile &O, DataRefImpl DRI) {
     115       17161 :   const char *P = reinterpret_cast<const char *>(DRI.p);
     116       17161 :   return getStruct<MachO::nlist_base>(O, P);
     117             : }
     118             : 
     119             : static StringRef parseSegmentOrSectionName(const char *P) {
     120       33281 :   if (P[15] == 0)
     121             :     // Null terminated.
     122             :     return P;
     123             :   // Not null terminated, so this is a 16 char string.
     124             :   return StringRef(P, 16);
     125             : }
     126             : 
     127             : static unsigned getCPUType(const MachOObjectFile &O) {
     128       16204 :   return O.getHeader().cputype;
     129             : }
     130             : 
     131             : static uint32_t
     132             : getPlainRelocationAddress(const MachO::any_relocation_info &RE) {
     133             :   return RE.r_word0;
     134             : }
     135             : 
     136             : static unsigned
     137             : getScatteredRelocationAddress(const MachO::any_relocation_info &RE) {
     138         164 :   return RE.r_word0 & 0xffffff;
     139             : }
     140             : 
     141             : static bool getPlainRelocationPCRel(const MachOObjectFile &O,
     142             :                                     const MachO::any_relocation_info &RE) {
     143         448 :   if (O.isLittleEndian())
     144         438 :     return (RE.r_word1 >> 24) & 1;
     145          10 :   return (RE.r_word1 >> 7) & 1;
     146             : }
     147             : 
     148             : static bool
     149             : getScatteredRelocationPCRel(const MachO::any_relocation_info &RE) {
     150         100 :   return (RE.r_word0 >> 30) & 1;
     151             : }
     152             : 
     153             : static unsigned getPlainRelocationLength(const MachOObjectFile &O,
     154             :                                          const MachO::any_relocation_info &RE) {
     155        1246 :   if (O.isLittleEndian())
     156        1236 :     return (RE.r_word1 >> 25) & 3;
     157          10 :   return (RE.r_word1 >> 5) & 3;
     158             : }
     159             : 
     160             : static unsigned
     161             : getScatteredRelocationLength(const MachO::any_relocation_info &RE) {
     162         107 :   return (RE.r_word0 >> 28) & 3;
     163             : }
     164             : 
     165             : static unsigned getPlainRelocationType(const MachOObjectFile &O,
     166             :                                        const MachO::any_relocation_info &RE) {
     167        1630 :   if (O.isLittleEndian())
     168        1610 :     return RE.r_word1 >> 28;
     169          20 :   return RE.r_word1 & 0xf;
     170             : }
     171             : 
     172       10548 : static uint32_t getSectionFlags(const MachOObjectFile &O,
     173             :                                 DataRefImpl Sec) {
     174       10548 :   if (O.is64Bit()) {
     175        8952 :     MachO::section_64 Sect = O.getSection64(Sec);
     176        8952 :     return Sect.flags;
     177             :   }
     178        1596 :   MachO::section Sect = O.getSection(Sec);
     179        1596 :   return Sect.flags;
     180             : }
     181             : 
     182             : static Expected<MachOObjectFile::LoadCommandInfo>
     183       10298 : getLoadCommandInfo(const MachOObjectFile &Obj, const char *Ptr,
     184             :                    uint32_t LoadCommandIndex) {
     185       20596 :   if (auto CmdOrErr = getStructOrErr<MachO::load_command>(Obj, Ptr)) {
     186       20596 :     if (CmdOrErr->cmdsize + Ptr > Obj.getData().end())
     187           0 :       return malformedError("load command " + Twine(LoadCommandIndex) +
     188           0 :                             " extends past end of file");
     189       10298 :     if (CmdOrErr->cmdsize < 8)
     190           2 :       return malformedError("load command " + Twine(LoadCommandIndex) +
     191           1 :                             " with size less than 8 bytes");
     192       10297 :     return MachOObjectFile::LoadCommandInfo({Ptr, *CmdOrErr});
     193             :   } else
     194             :     return CmdOrErr.takeError();
     195             : }
     196             : 
     197             : static Expected<MachOObjectFile::LoadCommandInfo>
     198        1759 : getFirstLoadCommandInfo(const MachOObjectFile &Obj) {
     199        1759 :   unsigned HeaderSize = Obj.is64Bit() ? sizeof(MachO::mach_header_64)
     200        1759 :                                       : sizeof(MachO::mach_header);
     201        1759 :   if (sizeof(MachO::load_command) > Obj.getHeader().sizeofcmds)
     202           2 :     return malformedError("load command 0 extends past the end all load "
     203             :                           "commands in the file");
     204        1758 :   return getLoadCommandInfo(Obj, getPtr(Obj, HeaderSize), 0);
     205             : }
     206             : 
     207             : static Expected<MachOObjectFile::LoadCommandInfo>
     208        8541 : getNextLoadCommandInfo(const MachOObjectFile &Obj, uint32_t LoadCommandIndex,
     209             :                        const MachOObjectFile::LoadCommandInfo &L) {
     210        8541 :   unsigned HeaderSize = Obj.is64Bit() ? sizeof(MachO::mach_header_64)
     211        8541 :                                       : sizeof(MachO::mach_header);
     212       17082 :   if (L.Ptr + L.C.cmdsize + sizeof(MachO::load_command) >
     213       17082 :       Obj.getData().data() + HeaderSize + Obj.getHeader().sizeofcmds)
     214           3 :     return malformedError("load command " + Twine(LoadCommandIndex + 1) +
     215           1 :                           " extends past the end all load commands in the file");
     216        8540 :   return getLoadCommandInfo(Obj, L.Ptr + L.C.cmdsize, LoadCommandIndex + 1);
     217             : }
     218             : 
     219             : template <typename T>
     220        1772 : static void parseHeader(const MachOObjectFile &Obj, T &Header,
     221             :                         Error &Err) {
     222        3544 :   if (sizeof(T) > Obj.getData().size()) {
     223           0 :     Err = malformedError("the mach header extends past the end of the "
     224             :                          "file");
     225           0 :     return;
     226             :   }
     227        3544 :   if (auto HeaderOrErr = getStructOrErr<T>(Obj, getPtr(Obj, 0)))
     228        1772 :     Header = *HeaderOrErr;
     229             :   else
     230             :     Err = HeaderOrErr.takeError();
     231             : }
     232             : 
     233             : // This is used to check for overlapping of Mach-O elements.
     234             : struct MachOElement {
     235             :   uint64_t Offset;
     236             :   uint64_t Size;
     237             :   const char *Name;
     238             : };
     239             : 
     240       39974 : static Error checkOverlappingElement(std::list<MachOElement> &Elements,
     241             :                                      uint64_t Offset, uint64_t Size,
     242             :                                      const char *Name) {
     243       39974 :   if (Size == 0)
     244             :     return Error::success();
     245             : 
     246      159364 :   for (auto it=Elements.begin() ; it != Elements.end(); ++it) {
     247      130487 :     auto E = *it;
     248      260954 :     if ((Offset >= E.Offset && Offset < E.Offset + E.Size) ||
     249      260934 :         (Offset + Size > E.Offset && Offset + Size < E.Offset + E.Size) ||
     250           0 :         (Offset <= E.Offset && Offset + Size >= E.Offset + E.Size))
     251          20 :       return malformedError(Twine(Name) + " at offset " + Twine(Offset) +
     252          40 :                             " with a size of " + Twine(Size) + ", overlaps " +
     253          60 :                             E.Name + " at offset " + Twine(E.Offset) + " with "
     254          40 :                             "a size of " + Twine(E.Size));
     255             :     auto nt = it;
     256             :     nt++;
     257      130467 :     if (nt != Elements.end()) {
     258      119512 :       auto N = *nt;
     259      119512 :       if (Offset + Size <= N.Offset) {
     260       13894 :         Elements.insert(nt, {Offset, Size, Name});
     261             :         return Error::success();
     262             :       }
     263             :     }
     264             :   }
     265       21910 :   Elements.push_back({Offset, Size, Name});
     266             :   return Error::success();
     267             : }
     268             : 
     269             : // Parses LC_SEGMENT or LC_SEGMENT_64 load command, adds addresses of all
     270             : // sections to \param Sections, and optionally sets
     271             : // \param IsPageZeroSegment to true.
     272             : template <typename Segment, typename Section>
     273        2912 : static Error parseSegmentLoadCommand(
     274             :     const MachOObjectFile &Obj, const MachOObjectFile::LoadCommandInfo &Load,
     275             :     SmallVectorImpl<const char *> &Sections, bool &IsPageZeroSegment,
     276             :     uint32_t LoadCommandIndex, const char *CmdName, uint64_t SizeOfHeaders,
     277             :     std::list<MachOElement> &Elements) {
     278             :   const unsigned SegmentLoadSize = sizeof(Segment);
     279        2912 :   if (Load.C.cmdsize < SegmentLoadSize)
     280           1 :     return malformedError("load command " + Twine(LoadCommandIndex) +
     281           2 :                           " " + CmdName + " cmdsize too small");
     282        5822 :   if (auto SegOrErr = getStructOrErr<Segment>(Obj, Load.Ptr)) {
     283        2911 :     Segment S = SegOrErr.get();
     284             :     const unsigned SectionSize = sizeof(Section);
     285        5822 :     uint64_t FileSize = Obj.getData().size();
     286        5822 :     if (S.nsects > std::numeric_limits<uint32_t>::max() / SectionSize ||
     287        2911 :         S.nsects * SectionSize > Load.C.cmdsize - SegmentLoadSize)
     288           2 :       return malformedError("load command " + Twine(LoadCommandIndex) +
     289           4 :                             " inconsistent cmdsize in " + CmdName +
     290           2 :                             " for the number of sections");
     291       29983 :     for (unsigned J = 0; J < S.nsects; ++J) {
     292       27104 :       const char *Sec = getSectionPtr(Obj, Load, J);
     293       13552 :       Sections.push_back(Sec);
     294       13552 :       Section s = getStruct<Section>(Obj, Sec);
     295       27102 :       if (Obj.getHeader().filetype != MachO::MH_DYLIB_STUB &&
     296       25852 :           Obj.getHeader().filetype != MachO::MH_DSYM &&
     297       11973 :           s.flags != MachO::S_ZEROFILL &&
     298       25515 :           s.flags != MachO::S_THREAD_LOCAL_ZEROFILL &&
     299       11963 :           s.offset > FileSize)
     300           1 :         return malformedError("offset field of section " + Twine(J) + " in " +
     301           2 :                               CmdName + " command " + Twine(LoadCommandIndex) +
     302           1 :                               " extends past the end of the file");
     303       27100 :       if (Obj.getHeader().filetype != MachO::MH_DYLIB_STUB &&
     304       25850 :           Obj.getHeader().filetype != MachO::MH_DSYM &&
     305       11972 :           s.flags != MachO::S_ZEROFILL &&
     306       13168 :           s.flags != MachO::S_THREAD_LOCAL_ZEROFILL && S.fileoff == 0 &&
     307       14790 :           s.offset < SizeOfHeaders && s.size != 0)
     308           1 :         return malformedError("offset field of section " + Twine(J) + " in " +
     309           2 :                               CmdName + " command " + Twine(LoadCommandIndex) +
     310           1 :                               " not past the headers of the file");
     311       13550 :       uint64_t BigSize = s.offset;
     312       13550 :       BigSize += s.size;
     313       27098 :       if (Obj.getHeader().filetype != MachO::MH_DYLIB_STUB &&
     314       25848 :           Obj.getHeader().filetype != MachO::MH_DSYM &&
     315       11971 :           s.flags != MachO::S_ZEROFILL &&
     316       25511 :           s.flags != MachO::S_THREAD_LOCAL_ZEROFILL &&
     317             :           BigSize > FileSize)
     318           2 :         return malformedError("offset field plus size field of section " +
     319           4 :                               Twine(J) + " in " + CmdName + " command " +
     320           4 :                               Twine(LoadCommandIndex) +
     321           2 :                               " extends past the end of the file");
     322       27094 :       if (Obj.getHeader().filetype != MachO::MH_DYLIB_STUB &&
     323       25844 :           Obj.getHeader().filetype != MachO::MH_DSYM &&
     324       11969 :           s.flags != MachO::S_ZEROFILL &&
     325       25507 :           s.flags != MachO::S_THREAD_LOCAL_ZEROFILL &&
     326       11959 :           s.size > S.filesize)
     327           1 :         return malformedError("size field of section " +
     328           2 :                               Twine(J) + " in " + CmdName + " command " +
     329           2 :                               Twine(LoadCommandIndex) +
     330           1 :                               " greater than the segment");
     331       27092 :       if (Obj.getHeader().filetype != MachO::MH_DYLIB_STUB &&
     332       49959 :           Obj.getHeader().filetype != MachO::MH_DSYM && s.size != 0 &&
     333       10570 :           s.addr < S.vmaddr)
     334           1 :         return malformedError("addr field of section " + Twine(J) + " in " +
     335           2 :                               CmdName + " command " + Twine(LoadCommandIndex) +
     336           1 :                               " less than the segment's vmaddr");
     337        1978 :       BigSize = s.addr;
     338       13546 :       BigSize += s.size;
     339       13546 :       uint64_t BigEnd = S.vmaddr;
     340       13546 :       BigEnd += S.vmsize;
     341       26964 :       if (S.vmsize != 0 && s.size != 0 && BigSize > BigEnd)
     342           3 :         return malformedError("addr field plus size of section " + Twine(J) +
     343           6 :                               " in " + CmdName + " command " +
     344           6 :                               Twine(LoadCommandIndex) +
     345             :                               " greater than than "
     346           3 :                               "the segment's vmaddr plus vmsize");
     347       27084 :       if (Obj.getHeader().filetype != MachO::MH_DYLIB_STUB &&
     348       25834 :           Obj.getHeader().filetype != MachO::MH_DSYM &&
     349       25507 :           s.flags != MachO::S_ZEROFILL &&
     350             :           s.flags != MachO::S_THREAD_LOCAL_ZEROFILL)
     351       23908 :         if (Error Err = checkOverlappingElement(Elements, s.offset, s.size,
     352             :                                                 "section contents"))
     353             :           return Err;
     354       13541 :       if (s.reloff > FileSize)
     355           1 :         return malformedError("reloff field of section " + Twine(J) + " in " +
     356           2 :                               CmdName + " command " + Twine(LoadCommandIndex) +
     357           1 :                               " extends past the end of the file");
     358       13540 :       BigSize = s.nreloc;
     359       13540 :       BigSize *= sizeof(struct MachO::relocation_info);
     360       13540 :       BigSize += s.reloff;
     361       13540 :       if (BigSize > FileSize)
     362             :         return malformedError("reloff field plus nreloc field times sizeof("
     363           1 :                               "struct relocation_info) of section " +
     364           2 :                               Twine(J) + " in " + CmdName + " command " +
     365           2 :                               Twine(LoadCommandIndex) +
     366           1 :                               " extends past the end of the file");
     367       27078 :       if (Error Err = checkOverlappingElement(Elements, s.reloff, s.nreloc *
     368             :                                               sizeof(struct
     369             :                                               MachO::relocation_info),
     370             :                                               "section relocation entries"))
     371             :         return Err;
     372             :     }
     373        2894 :     if (S.fileoff > FileSize)
     374           3 :       return malformedError("load command " + Twine(LoadCommandIndex) +
     375           6 :                             " fileoff field in " + CmdName +
     376           3 :                             " extends past the end of the file");
     377             :     uint64_t BigSize = S.fileoff;
     378        2891 :     BigSize += S.filesize;
     379        2891 :     if (BigSize > FileSize)
     380           1 :       return malformedError("load command " + Twine(LoadCommandIndex) +
     381           2 :                             " fileoff field plus filesize field in " +
     382           2 :                             CmdName + " extends past the end of the file");
     383        2890 :     if (S.vmsize != 0 && S.filesize > S.vmsize)
     384           1 :       return malformedError("load command " + Twine(LoadCommandIndex) +
     385           2 :                             " filesize field in " + CmdName +
     386           1 :                             " greater than vmsize field");
     387        2889 :     IsPageZeroSegment |= StringRef("__PAGEZERO").equals(S.segname);
     388             :   } else
     389             :     return SegOrErr.takeError();
     390             : 
     391             :   return Error::success();
     392             : }
     393             : 
     394        1522 : static Error checkSymtabCommand(const MachOObjectFile &Obj,
     395             :                                 const MachOObjectFile::LoadCommandInfo &Load,
     396             :                                 uint32_t LoadCommandIndex,
     397             :                                 const char **SymtabLoadCmd,
     398             :                                 std::list<MachOElement> &Elements) {
     399        1522 :   if (Load.C.cmdsize < sizeof(MachO::symtab_command))
     400           1 :     return malformedError("load command " + Twine(LoadCommandIndex) +
     401           2 :                           " LC_SYMTAB cmdsize too small");
     402        1521 :   if (*SymtabLoadCmd != nullptr)
     403           1 :     return malformedError("more than one LC_SYMTAB command");
     404             :   MachO::symtab_command Symtab =
     405        1520 :     getStruct<MachO::symtab_command>(Obj, Load.Ptr);
     406        1520 :   if (Symtab.cmdsize != sizeof(MachO::symtab_command))
     407           1 :     return malformedError("LC_SYMTAB command " + Twine(LoadCommandIndex) +
     408           2 :                           " has incorrect cmdsize");
     409        3038 :   uint64_t FileSize = Obj.getData().size();
     410        1519 :   if (Symtab.symoff > FileSize)
     411           1 :     return malformedError("symoff field of LC_SYMTAB command " +
     412           2 :                           Twine(LoadCommandIndex) + " extends past the end "
     413           2 :                           "of the file");
     414        1518 :   uint64_t SymtabSize = Symtab.nsyms;
     415             :   const char *struct_nlist_name;
     416        1518 :   if (Obj.is64Bit()) {
     417        1163 :     SymtabSize *= sizeof(MachO::nlist_64);
     418             :     struct_nlist_name = "struct nlist_64";
     419             :   } else {
     420         355 :     SymtabSize *= sizeof(MachO::nlist);
     421             :     struct_nlist_name = "struct nlist";
     422             :   }
     423             :   uint64_t BigSize = SymtabSize;
     424        1518 :   BigSize += Symtab.symoff;
     425        1518 :   if (BigSize > FileSize)
     426           1 :     return malformedError("symoff field plus nsyms field times sizeof(" +
     427           2 :                           Twine(struct_nlist_name) + ") of LC_SYMTAB command " +
     428           2 :                           Twine(LoadCommandIndex) + " extends past the end "
     429           2 :                           "of the file");
     430        1517 :   if (Error Err = checkOverlappingElement(Elements, Symtab.symoff, SymtabSize,
     431        1517 :                                           "symbol table"))
     432             :     return Err;
     433        1515 :   if (Symtab.stroff > FileSize)
     434           1 :     return malformedError("stroff field of LC_SYMTAB command " +
     435           2 :                           Twine(LoadCommandIndex) + " extends past the end "
     436           2 :                           "of the file");
     437             :   BigSize = Symtab.stroff;
     438        1514 :   BigSize += Symtab.strsize;
     439        1514 :   if (BigSize > FileSize)
     440             :     return malformedError("stroff field plus strsize field of LC_SYMTAB "
     441           1 :                           "command " + Twine(LoadCommandIndex) + " extends "
     442           2 :                           "past the end of the file");
     443        1513 :   if (Error Err = checkOverlappingElement(Elements, Symtab.stroff,
     444        1513 :                                           Symtab.strsize, "string table"))
     445             :     return Err;
     446        1512 :   *SymtabLoadCmd = Load.Ptr;
     447             :   return Error::success();
     448             : }
     449             : 
     450        1424 : static Error checkDysymtabCommand(const MachOObjectFile &Obj,
     451             :                                   const MachOObjectFile::LoadCommandInfo &Load,
     452             :                                   uint32_t LoadCommandIndex,
     453             :                                   const char **DysymtabLoadCmd,
     454             :                                   std::list<MachOElement> &Elements) {
     455        1424 :   if (Load.C.cmdsize < sizeof(MachO::dysymtab_command))
     456           1 :     return malformedError("load command " + Twine(LoadCommandIndex) +
     457           2 :                           " LC_DYSYMTAB cmdsize too small");
     458        1423 :   if (*DysymtabLoadCmd != nullptr)
     459           1 :     return malformedError("more than one LC_DYSYMTAB command");
     460             :   MachO::dysymtab_command Dysymtab =
     461        1422 :     getStruct<MachO::dysymtab_command>(Obj, Load.Ptr);
     462        1422 :   if (Dysymtab.cmdsize != sizeof(MachO::dysymtab_command))
     463           1 :     return malformedError("LC_DYSYMTAB command " + Twine(LoadCommandIndex) +
     464           2 :                           " has incorrect cmdsize");
     465        2842 :   uint64_t FileSize = Obj.getData().size();
     466        1421 :   if (Dysymtab.tocoff > FileSize)
     467           1 :     return malformedError("tocoff field of LC_DYSYMTAB command " +
     468           2 :                           Twine(LoadCommandIndex) + " extends past the end of "
     469           2 :                           "the file");
     470        1420 :   uint64_t BigSize = Dysymtab.ntoc;
     471        1420 :   BigSize *= sizeof(MachO::dylib_table_of_contents);
     472        1420 :   BigSize += Dysymtab.tocoff;
     473        1420 :   if (BigSize > FileSize)
     474             :     return malformedError("tocoff field plus ntoc field times sizeof(struct "
     475           1 :                           "dylib_table_of_contents) of LC_DYSYMTAB command " +
     476           2 :                           Twine(LoadCommandIndex) + " extends past the end of "
     477           2 :                           "the file");
     478        1419 :   if (Error Err = checkOverlappingElement(Elements, Dysymtab.tocoff,
     479             :                                           Dysymtab.ntoc * sizeof(struct
     480             :                                           MachO::dylib_table_of_contents),
     481        1419 :                                           "table of contents"))
     482             :     return Err;
     483        1418 :   if (Dysymtab.modtaboff > FileSize)
     484           1 :     return malformedError("modtaboff field of LC_DYSYMTAB command " +
     485           2 :                           Twine(LoadCommandIndex) + " extends past the end of "
     486           2 :                           "the file");
     487        1417 :   BigSize = Dysymtab.nmodtab;
     488             :   const char *struct_dylib_module_name;
     489             :   uint64_t sizeof_modtab;
     490        1417 :   if (Obj.is64Bit()) {
     491             :     sizeof_modtab = sizeof(MachO::dylib_module_64);
     492             :     struct_dylib_module_name = "struct dylib_module_64";
     493             :   } else {
     494             :     sizeof_modtab = sizeof(MachO::dylib_module);
     495             :     struct_dylib_module_name = "struct dylib_module";
     496             :   }
     497        1417 :   BigSize *= sizeof_modtab;
     498        1417 :   BigSize += Dysymtab.modtaboff;
     499        1417 :   if (BigSize > FileSize)
     500           1 :     return malformedError("modtaboff field plus nmodtab field times sizeof(" +
     501           2 :                           Twine(struct_dylib_module_name) + ") of LC_DYSYMTAB "
     502           2 :                           "command " + Twine(LoadCommandIndex) + " extends "
     503           2 :                           "past the end of the file");
     504        1416 :   if (Error Err = checkOverlappingElement(Elements, Dysymtab.modtaboff,
     505             :                                           Dysymtab.nmodtab * sizeof_modtab,
     506        1416 :                                           "module table"))
     507             :     return Err;
     508        1415 :   if (Dysymtab.extrefsymoff > FileSize)
     509           1 :     return malformedError("extrefsymoff field of LC_DYSYMTAB command " +
     510           2 :                           Twine(LoadCommandIndex) + " extends past the end of "
     511           2 :                           "the file");
     512        1414 :   BigSize = Dysymtab.nextrefsyms;
     513        1414 :   BigSize *= sizeof(MachO::dylib_reference);
     514        1414 :   BigSize += Dysymtab.extrefsymoff;
     515        1414 :   if (BigSize > FileSize)
     516             :     return malformedError("extrefsymoff field plus nextrefsyms field times "
     517             :                           "sizeof(struct dylib_reference) of LC_DYSYMTAB "
     518           1 :                           "command " + Twine(LoadCommandIndex) + " extends "
     519           2 :                           "past the end of the file");
     520        1413 :   if (Error Err = checkOverlappingElement(Elements, Dysymtab.extrefsymoff,
     521             :                                           Dysymtab.nextrefsyms *
     522             :                                               sizeof(MachO::dylib_reference),
     523        1413 :                                           "reference table"))
     524             :     return Err;
     525        1412 :   if (Dysymtab.indirectsymoff > FileSize)
     526           1 :     return malformedError("indirectsymoff field of LC_DYSYMTAB command " +
     527           2 :                           Twine(LoadCommandIndex) + " extends past the end of "
     528           2 :                           "the file");
     529        1411 :   BigSize = Dysymtab.nindirectsyms;
     530        1411 :   BigSize *= sizeof(uint32_t);
     531        1411 :   BigSize += Dysymtab.indirectsymoff;
     532        1411 :   if (BigSize > FileSize)
     533             :     return malformedError("indirectsymoff field plus nindirectsyms field times "
     534           1 :                           "sizeof(uint32_t) of LC_DYSYMTAB command " +
     535           2 :                           Twine(LoadCommandIndex) + " extends past the end of "
     536           2 :                           "the file");
     537        1410 :   if (Error Err = checkOverlappingElement(Elements, Dysymtab.indirectsymoff,
     538             :                                           Dysymtab.nindirectsyms *
     539             :                                           sizeof(uint32_t),
     540        1410 :                                           "indirect table"))
     541             :     return Err;
     542        1409 :   if (Dysymtab.extreloff > FileSize)
     543           1 :     return malformedError("extreloff field of LC_DYSYMTAB command " +
     544           2 :                           Twine(LoadCommandIndex) + " extends past the end of "
     545           2 :                           "the file");
     546        1408 :   BigSize = Dysymtab.nextrel;
     547        1408 :   BigSize *= sizeof(MachO::relocation_info);
     548        1408 :   BigSize += Dysymtab.extreloff;
     549        1408 :   if (BigSize > FileSize)
     550             :     return malformedError("extreloff field plus nextrel field times sizeof"
     551           1 :                           "(struct relocation_info) of LC_DYSYMTAB command " +
     552           2 :                           Twine(LoadCommandIndex) + " extends past the end of "
     553           2 :                           "the file");
     554        1407 :   if (Error Err = checkOverlappingElement(Elements, Dysymtab.extreloff,
     555             :                                           Dysymtab.nextrel *
     556             :                                               sizeof(MachO::relocation_info),
     557        1407 :                                           "external relocation table"))
     558             :     return Err;
     559        1406 :   if (Dysymtab.locreloff > FileSize)
     560           1 :     return malformedError("locreloff field of LC_DYSYMTAB command " +
     561           2 :                           Twine(LoadCommandIndex) + " extends past the end of "
     562           2 :                           "the file");
     563        1405 :   BigSize = Dysymtab.nlocrel;
     564        1405 :   BigSize *= sizeof(MachO::relocation_info);
     565        1405 :   BigSize += Dysymtab.locreloff;
     566        1405 :   if (BigSize > FileSize)
     567             :     return malformedError("locreloff field plus nlocrel field times sizeof"
     568           1 :                           "(struct relocation_info) of LC_DYSYMTAB command " +
     569           2 :                           Twine(LoadCommandIndex) + " extends past the end of "
     570           2 :                           "the file");
     571        1404 :   if (Error Err = checkOverlappingElement(Elements, Dysymtab.locreloff,
     572             :                                           Dysymtab.nlocrel *
     573             :                                               sizeof(MachO::relocation_info),
     574        1404 :                                           "local relocation table"))
     575             :     return Err;
     576        1403 :   *DysymtabLoadCmd = Load.Ptr;
     577             :   return Error::success();
     578             : }
     579             : 
     580         991 : static Error checkLinkeditDataCommand(const MachOObjectFile &Obj,
     581             :                                  const MachOObjectFile::LoadCommandInfo &Load,
     582             :                                  uint32_t LoadCommandIndex,
     583             :                                  const char **LoadCmd, const char *CmdName,
     584             :                                  std::list<MachOElement> &Elements,
     585             :                                  const char *ElementName) {
     586         991 :   if (Load.C.cmdsize < sizeof(MachO::linkedit_data_command))
     587           1 :     return malformedError("load command " + Twine(LoadCommandIndex) + " " +
     588           2 :                           CmdName + " cmdsize too small");
     589         990 :   if (*LoadCmd != nullptr)
     590           1 :     return malformedError("more than one " + Twine(CmdName) + " command");
     591             :   MachO::linkedit_data_command LinkData =
     592         989 :     getStruct<MachO::linkedit_data_command>(Obj, Load.Ptr);
     593         989 :   if (LinkData.cmdsize != sizeof(MachO::linkedit_data_command))
     594           3 :     return malformedError(Twine(CmdName) + " command " +
     595           6 :                           Twine(LoadCommandIndex) + " has incorrect cmdsize");
     596        1972 :   uint64_t FileSize = Obj.getData().size();
     597         986 :   if (LinkData.dataoff > FileSize)
     598           2 :     return malformedError("dataoff field of " + Twine(CmdName) + " command " +
     599           4 :                           Twine(LoadCommandIndex) + " extends past the end of "
     600           4 :                           "the file");
     601             :   uint64_t BigSize = LinkData.dataoff;
     602         984 :   BigSize += LinkData.datasize;
     603         984 :   if (BigSize > FileSize)
     604           2 :     return malformedError("dataoff field plus datasize field of " +
     605           4 :                           Twine(CmdName) + " command " +
     606           4 :                           Twine(LoadCommandIndex) + " extends past the end of "
     607           4 :                           "the file");
     608         982 :   if (Error Err = checkOverlappingElement(Elements, LinkData.dataoff,
     609         982 :                                           LinkData.datasize, ElementName))
     610             :     return Err;
     611         981 :   *LoadCmd = Load.Ptr;
     612             :   return Error::success();
     613             : }
     614             : 
     615         410 : static Error checkDyldInfoCommand(const MachOObjectFile &Obj,
     616             :                                   const MachOObjectFile::LoadCommandInfo &Load,
     617             :                                   uint32_t LoadCommandIndex,
     618             :                                   const char **LoadCmd, const char *CmdName,
     619             :                                   std::list<MachOElement> &Elements) {
     620         410 :   if (Load.C.cmdsize < sizeof(MachO::dyld_info_command))
     621           1 :     return malformedError("load command " + Twine(LoadCommandIndex) + " " +
     622           2 :                           CmdName + " cmdsize too small");
     623         409 :   if (*LoadCmd != nullptr)
     624             :     return malformedError("more than one LC_DYLD_INFO and or LC_DYLD_INFO_ONLY "
     625           1 :                           "command");
     626             :   MachO::dyld_info_command DyldInfo =
     627         408 :     getStruct<MachO::dyld_info_command>(Obj, Load.Ptr);
     628         408 :   if (DyldInfo.cmdsize != sizeof(MachO::dyld_info_command))
     629           1 :     return malformedError(Twine(CmdName) + " command " +
     630           2 :                           Twine(LoadCommandIndex) + " has incorrect cmdsize");
     631         814 :   uint64_t FileSize = Obj.getData().size();
     632         407 :   if (DyldInfo.rebase_off > FileSize)
     633           1 :     return malformedError("rebase_off field of " + Twine(CmdName) +
     634           2 :                           " command " + Twine(LoadCommandIndex) + " extends "
     635           2 :                           "past the end of the file");
     636             :   uint64_t BigSize = DyldInfo.rebase_off;
     637         406 :   BigSize += DyldInfo.rebase_size;
     638         406 :   if (BigSize > FileSize)
     639           1 :     return malformedError("rebase_off field plus rebase_size field of " +
     640           2 :                           Twine(CmdName) + " command " +
     641           2 :                           Twine(LoadCommandIndex) + " extends past the end of "
     642           2 :                           "the file");
     643         405 :   if (Error Err = checkOverlappingElement(Elements, DyldInfo.rebase_off,
     644             :                                           DyldInfo.rebase_size,
     645         405 :                                           "dyld rebase info"))
     646             :     return Err;
     647         404 :   if (DyldInfo.bind_off > FileSize)
     648           1 :     return malformedError("bind_off field of " + Twine(CmdName) +
     649           2 :                           " command " + Twine(LoadCommandIndex) + " extends "
     650           2 :                           "past the end of the file");
     651             :   BigSize = DyldInfo.bind_off;
     652         403 :   BigSize += DyldInfo.bind_size;
     653         403 :   if (BigSize > FileSize)
     654           1 :     return malformedError("bind_off field plus bind_size field of " +
     655           2 :                           Twine(CmdName) + " command " +
     656           2 :                           Twine(LoadCommandIndex) + " extends past the end of "
     657           2 :                           "the file");
     658         402 :   if (Error Err = checkOverlappingElement(Elements, DyldInfo.bind_off,
     659             :                                           DyldInfo.bind_size,
     660         402 :                                           "dyld bind info"))
     661             :     return Err;
     662         401 :   if (DyldInfo.weak_bind_off > FileSize)
     663           1 :     return malformedError("weak_bind_off field of " + Twine(CmdName) +
     664           2 :                           " command " + Twine(LoadCommandIndex) + " extends "
     665           2 :                           "past the end of the file");
     666             :   BigSize = DyldInfo.weak_bind_off;
     667         400 :   BigSize += DyldInfo.weak_bind_size;
     668         400 :   if (BigSize > FileSize)
     669           1 :     return malformedError("weak_bind_off field plus weak_bind_size field of " +
     670           2 :                           Twine(CmdName) + " command " +
     671           2 :                           Twine(LoadCommandIndex) + " extends past the end of "
     672           2 :                           "the file");
     673         399 :   if (Error Err = checkOverlappingElement(Elements, DyldInfo.weak_bind_off,
     674             :                                           DyldInfo.weak_bind_size,
     675         399 :                                           "dyld weak bind info"))
     676             :     return Err;
     677         398 :   if (DyldInfo.lazy_bind_off > FileSize)
     678           1 :     return malformedError("lazy_bind_off field of " + Twine(CmdName) +
     679           2 :                           " command " + Twine(LoadCommandIndex) + " extends "
     680           2 :                           "past the end of the file");
     681             :   BigSize = DyldInfo.lazy_bind_off;
     682         397 :   BigSize += DyldInfo.lazy_bind_size;
     683         397 :   if (BigSize > FileSize)
     684           1 :     return malformedError("lazy_bind_off field plus lazy_bind_size field of " +
     685           2 :                           Twine(CmdName) + " command " +
     686           2 :                           Twine(LoadCommandIndex) + " extends past the end of "
     687           2 :                           "the file");
     688         396 :   if (Error Err = checkOverlappingElement(Elements, DyldInfo.lazy_bind_off,
     689             :                                           DyldInfo.lazy_bind_size,
     690         396 :                                           "dyld lazy bind info"))
     691             :     return Err;
     692         395 :   if (DyldInfo.export_off > FileSize)
     693           1 :     return malformedError("export_off field of " + Twine(CmdName) +
     694           2 :                           " command " + Twine(LoadCommandIndex) + " extends "
     695           2 :                           "past the end of the file");
     696             :   BigSize = DyldInfo.export_off;
     697         394 :   BigSize += DyldInfo.export_size;
     698         394 :   if (BigSize > FileSize)
     699           1 :     return malformedError("export_off field plus export_size field of " +
     700           2 :                           Twine(CmdName) + " command " +
     701           2 :                           Twine(LoadCommandIndex) + " extends past the end of "
     702           2 :                           "the file");
     703         393 :   if (Error Err = checkOverlappingElement(Elements, DyldInfo.export_off,
     704             :                                           DyldInfo.export_size,
     705         393 :                                           "dyld export info"))
     706             :     return Err;
     707         392 :   *LoadCmd = Load.Ptr;
     708             :   return Error::success();
     709             : }
     710             : 
     711         632 : static Error checkDylibCommand(const MachOObjectFile &Obj,
     712             :                                const MachOObjectFile::LoadCommandInfo &Load,
     713             :                                uint32_t LoadCommandIndex, const char *CmdName) {
     714         632 :   if (Load.C.cmdsize < sizeof(MachO::dylib_command))
     715           1 :     return malformedError("load command " + Twine(LoadCommandIndex) + " " +
     716           2 :                           CmdName + " cmdsize too small");
     717         631 :   MachO::dylib_command D = getStruct<MachO::dylib_command>(Obj, Load.Ptr);
     718         631 :   if (D.dylib.name < sizeof(MachO::dylib_command))
     719           1 :     return malformedError("load command " + Twine(LoadCommandIndex) + " " +
     720           2 :                           CmdName + " name.offset field too small, not past "
     721           2 :                           "the end of the dylib_command struct");
     722         630 :   if (D.dylib.name >= D.cmdsize)
     723           1 :     return malformedError("load command " + Twine(LoadCommandIndex) + " " +
     724           2 :                           CmdName + " name.offset field extends past the end "
     725           2 :                           "of the load command");
     726             :   // Make sure there is a null between the starting offset of the name and
     727             :   // the end of the load command.
     728             :   uint32_t i;
     729         629 :   const char *P = (const char *)Load.Ptr;
     730       37603 :   for (i = D.dylib.name; i < D.cmdsize; i++)
     731       19115 :     if (P[i] == '\0')
     732             :       break;
     733         629 :   if (i >= D.cmdsize)
     734           1 :     return malformedError("load command " + Twine(LoadCommandIndex) + " " +
     735           2 :                           CmdName + " library name extends past the end of the "
     736           2 :                           "load command");
     737             :   return Error::success();
     738             : }
     739             : 
     740         119 : static Error checkDylibIdCommand(const MachOObjectFile &Obj,
     741             :                                  const MachOObjectFile::LoadCommandInfo &Load,
     742             :                                  uint32_t LoadCommandIndex,
     743             :                                  const char **LoadCmd) {
     744         119 :   if (Error Err = checkDylibCommand(Obj, Load, LoadCommandIndex,
     745         119 :                                      "LC_ID_DYLIB"))
     746             :     return Err;
     747         119 :   if (*LoadCmd != nullptr)
     748           1 :     return malformedError("more than one LC_ID_DYLIB command");
     749         120 :   if (Obj.getHeader().filetype != MachO::MH_DYLIB &&
     750           2 :       Obj.getHeader().filetype != MachO::MH_DYLIB_STUB)
     751             :     return malformedError("LC_ID_DYLIB load command in non-dynamic library "
     752           1 :                           "file type");
     753         117 :   *LoadCmd = Load.Ptr;
     754             :   return Error::success();
     755             : }
     756             : 
     757         279 : static Error checkDyldCommand(const MachOObjectFile &Obj,
     758             :                               const MachOObjectFile::LoadCommandInfo &Load,
     759             :                               uint32_t LoadCommandIndex, const char *CmdName) {
     760         279 :   if (Load.C.cmdsize < sizeof(MachO::dylinker_command))
     761           1 :     return malformedError("load command " + Twine(LoadCommandIndex) + " " +
     762           2 :                           CmdName + " cmdsize too small");
     763         278 :   MachO::dylinker_command D = getStruct<MachO::dylinker_command>(Obj, Load.Ptr);
     764         278 :   if (D.name < sizeof(MachO::dylinker_command))
     765           0 :     return malformedError("load command " + Twine(LoadCommandIndex) + " " +
     766           0 :                           CmdName + " name.offset field too small, not past "
     767           0 :                           "the end of the dylinker_command struct");
     768         278 :   if (D.name >= D.cmdsize)
     769           1 :     return malformedError("load command " + Twine(LoadCommandIndex) + " " +
     770           2 :                           CmdName + " name.offset field extends past the end "
     771           2 :                           "of the load command");
     772             :   // Make sure there is a null between the starting offset of the name and
     773             :   // the end of the load command.
     774             :   uint32_t i;
     775         277 :   const char *P = (const char *)Load.Ptr;
     776        7409 :   for (i = D.name; i < D.cmdsize; i++)
     777        3842 :     if (P[i] == '\0')
     778             :       break;
     779         277 :   if (i >= D.cmdsize)
     780           1 :     return malformedError("load command " + Twine(LoadCommandIndex) + " " +
     781           2 :                           CmdName + " dyld name extends past the end of the "
     782           2 :                           "load command");
     783             :   return Error::success();
     784             : }
     785             : 
     786         903 : static Error checkVersCommand(const MachOObjectFile &Obj,
     787             :                               const MachOObjectFile::LoadCommandInfo &Load,
     788             :                               uint32_t LoadCommandIndex,
     789             :                               const char **LoadCmd, const char *CmdName) {
     790         903 :   if (Load.C.cmdsize != sizeof(MachO::version_min_command))
     791           1 :     return malformedError("load command " + Twine(LoadCommandIndex) + " " +
     792           2 :                           CmdName + " has incorrect cmdsize");
     793         902 :   if (*LoadCmd != nullptr)
     794             :     return malformedError("more than one LC_VERSION_MIN_MACOSX, "
     795             :                           "LC_VERSION_MIN_IPHONEOS, LC_VERSION_MIN_TVOS or "
     796           1 :                           "LC_VERSION_MIN_WATCHOS command");
     797         901 :   *LoadCmd = Load.Ptr;
     798             :   return Error::success();
     799             : }
     800             : 
     801           2 : static Error checkNoteCommand(const MachOObjectFile &Obj,
     802             :                               const MachOObjectFile::LoadCommandInfo &Load,
     803             :                               uint32_t LoadCommandIndex,
     804             :                               std::list<MachOElement> &Elements) {
     805           2 :   if (Load.C.cmdsize != sizeof(MachO::note_command))
     806           0 :     return malformedError("load command " + Twine(LoadCommandIndex) +
     807           0 :                           " LC_NOTE has incorrect cmdsize");
     808           2 :   MachO::note_command Nt = getStruct<MachO::note_command>(Obj, Load.Ptr);
     809           4 :   uint64_t FileSize = Obj.getData().size();
     810           2 :   if (Nt.offset > FileSize)
     811           0 :     return malformedError("offset field of LC_NOTE command " +
     812           0 :                           Twine(LoadCommandIndex) + " extends "
     813           0 :                           "past the end of the file");
     814             :   uint64_t BigSize = Nt.offset;
     815           2 :   BigSize += Nt.size;
     816           2 :   if (BigSize > FileSize)
     817           1 :     return malformedError("size field plus offset field of LC_NOTE command " +
     818           2 :                           Twine(LoadCommandIndex) + " extends past the end of "
     819           2 :                           "the file");
     820           1 :   if (Error Err = checkOverlappingElement(Elements, Nt.offset, Nt.size,
     821           1 :                                           "LC_NOTE data"))
     822             :     return Err;
     823             :   return Error::success();
     824             : }
     825             : 
     826             : static Error
     827           2 : parseBuildVersionCommand(const MachOObjectFile &Obj,
     828             :                          const MachOObjectFile::LoadCommandInfo &Load,
     829             :                          SmallVectorImpl<const char*> &BuildTools,
     830             :                          uint32_t LoadCommandIndex) {
     831             :   MachO::build_version_command BVC =
     832           2 :       getStruct<MachO::build_version_command>(Obj, Load.Ptr);
     833           4 :   if (Load.C.cmdsize !=
     834             :       sizeof(MachO::build_version_command) +
     835           2 :           BVC.ntools * sizeof(MachO::build_tool_version))
     836           0 :     return malformedError("load command " + Twine(LoadCommandIndex) +
     837           0 :                           " LC_BUILD_VERSION_COMMAND has incorrect cmdsize");
     838             : 
     839           2 :   auto Start = Load.Ptr + sizeof(MachO::build_version_command);
     840           2 :   BuildTools.resize(BVC.ntools);
     841           4 :   for (unsigned i = 0; i < BVC.ntools; ++i)
     842           2 :     BuildTools[i] = Start + i * sizeof(MachO::build_tool_version);
     843             : 
     844             :   return Error::success();
     845             : }
     846             : 
     847          11 : static Error checkRpathCommand(const MachOObjectFile &Obj,
     848             :                                const MachOObjectFile::LoadCommandInfo &Load,
     849             :                                uint32_t LoadCommandIndex) {
     850          11 :   if (Load.C.cmdsize < sizeof(MachO::rpath_command))
     851           1 :     return malformedError("load command " + Twine(LoadCommandIndex) +
     852           2 :                           " LC_RPATH cmdsize too small");
     853          10 :   MachO::rpath_command R = getStruct<MachO::rpath_command>(Obj, Load.Ptr);
     854          10 :   if (R.path < sizeof(MachO::rpath_command))
     855           0 :     return malformedError("load command " + Twine(LoadCommandIndex) +
     856             :                           " LC_RPATH path.offset field too small, not past "
     857           0 :                           "the end of the rpath_command struct");
     858          10 :   if (R.path >= R.cmdsize)
     859           1 :     return malformedError("load command " + Twine(LoadCommandIndex) +
     860             :                           " LC_RPATH path.offset field extends past the end "
     861           2 :                           "of the load command");
     862             :   // Make sure there is a null between the starting offset of the path and
     863             :   // the end of the load command.
     864             :   uint32_t i;
     865           9 :   const char *P = (const char *)Load.Ptr;
     866         603 :   for (i = R.path; i < R.cmdsize; i++)
     867         305 :     if (P[i] == '\0')
     868             :       break;
     869           9 :   if (i >= R.cmdsize)
     870           1 :     return malformedError("load command " + Twine(LoadCommandIndex) +
     871             :                           " LC_RPATH library name extends past the end of the "
     872           2 :                           "load command");
     873             :   return Error::success();
     874             : }
     875             : 
     876          21 : static Error checkEncryptCommand(const MachOObjectFile &Obj,
     877             :                                  const MachOObjectFile::LoadCommandInfo &Load,
     878             :                                  uint32_t LoadCommandIndex,
     879             :                                  uint64_t cryptoff, uint64_t cryptsize,
     880             :                                  const char **LoadCmd, const char *CmdName) {
     881          21 :   if (*LoadCmd != nullptr)
     882             :     return malformedError("more than one LC_ENCRYPTION_INFO and or "
     883           1 :                           "LC_ENCRYPTION_INFO_64 command");
     884          40 :   uint64_t FileSize = Obj.getData().size();
     885          20 :   if (cryptoff > FileSize)
     886           1 :     return malformedError("cryptoff field of " + Twine(CmdName) +
     887           2 :                           " command " + Twine(LoadCommandIndex) + " extends "
     888           2 :                           "past the end of the file");
     889             :   uint64_t BigSize = cryptoff;
     890          19 :   BigSize += cryptsize;
     891          19 :   if (BigSize > FileSize)
     892           1 :     return malformedError("cryptoff field plus cryptsize field of " +
     893           2 :                           Twine(CmdName) + " command " +
     894           2 :                           Twine(LoadCommandIndex) + " extends past the end of "
     895           2 :                           "the file");
     896          18 :   *LoadCmd = Load.Ptr;
     897             :   return Error::success();
     898             : }
     899             : 
     900         108 : static Error checkLinkerOptCommand(const MachOObjectFile &Obj,
     901             :                                    const MachOObjectFile::LoadCommandInfo &Load,
     902             :                                    uint32_t LoadCommandIndex) {
     903         108 :   if (Load.C.cmdsize < sizeof(MachO::linker_option_command))
     904           1 :     return malformedError("load command " + Twine(LoadCommandIndex) +
     905           2 :                           " LC_LINKER_OPTION cmdsize too small");
     906             :   MachO::linker_option_command L =
     907         107 :     getStruct<MachO::linker_option_command>(Obj, Load.Ptr);
     908             :   // Make sure the count of strings is correct.
     909         107 :   const char *string = (const char *)Load.Ptr +
     910             :                        sizeof(struct MachO::linker_option_command);
     911         107 :   uint32_t left = L.cmdsize - sizeof(struct MachO::linker_option_command);
     912             :   uint32_t i = 0;
     913         399 :   while (left > 0) {
     914         586 :     while (*string == '\0' && left > 0) {
     915         294 :       string++;
     916         294 :       left--;
     917             :     }
     918         292 :     if (left > 0) {
     919         210 :       i++;
     920         420 :       uint32_t NullPos = StringRef(string, left).find('\0');
     921         210 :       uint32_t len = std::min(NullPos, left) + 1;
     922         210 :       string += len;
     923         210 :       left -= len;
     924             :     }
     925             :   }
     926         107 :   if (L.count != i)
     927           1 :     return malformedError("load command " + Twine(LoadCommandIndex) +
     928           2 :                           " LC_LINKER_OPTION string count " + Twine(L.count) +
     929           2 :                           " does not match number of strings");
     930             :   return Error::success();
     931             : }
     932             : 
     933           9 : static Error checkSubCommand(const MachOObjectFile &Obj,
     934             :                              const MachOObjectFile::LoadCommandInfo &Load,
     935             :                              uint32_t LoadCommandIndex, const char *CmdName,
     936             :                              size_t SizeOfCmd, const char *CmdStructName,
     937             :                              uint32_t PathOffset, const char *PathFieldName) {
     938           9 :   if (PathOffset < SizeOfCmd)
     939           1 :     return malformedError("load command " + Twine(LoadCommandIndex) + " " +
     940           2 :                           CmdName + " " + PathFieldName + ".offset field too "
     941           2 :                           "small, not past the end of the " + CmdStructName);
     942           8 :   if (PathOffset >= Load.C.cmdsize)
     943           1 :     return malformedError("load command " + Twine(LoadCommandIndex) + " " +
     944           2 :                           CmdName + " " + PathFieldName + ".offset field "
     945           2 :                           "extends past the end of the load command");
     946             :   // Make sure there is a null between the starting offset of the path and
     947             :   // the end of the load command.
     948             :   uint32_t i;
     949           7 :   const char *P = (const char *)Load.Ptr;
     950          57 :   for (i = PathOffset; i < Load.C.cmdsize; i++)
     951          31 :     if (P[i] == '\0')
     952             :       break;
     953           7 :   if (i >= Load.C.cmdsize)
     954           1 :     return malformedError("load command " + Twine(LoadCommandIndex) + " " +
     955           2 :                           CmdName + " " + PathFieldName + " name extends past "
     956           2 :                           "the end of the load command");
     957             :   return Error::success();
     958             : }
     959             : 
     960          21 : static Error checkThreadCommand(const MachOObjectFile &Obj,
     961             :                                 const MachOObjectFile::LoadCommandInfo &Load,
     962             :                                 uint32_t LoadCommandIndex,
     963             :                                 const char *CmdName) {
     964          21 :   if (Load.C.cmdsize < sizeof(MachO::thread_command))
     965           0 :     return malformedError("load command " + Twine(LoadCommandIndex) +
     966           0 :                           CmdName + " cmdsize too small");
     967             :   MachO::thread_command T =
     968          21 :     getStruct<MachO::thread_command>(Obj, Load.Ptr);
     969          21 :   const char *state = Load.Ptr + sizeof(MachO::thread_command);
     970          21 :   const char *end = Load.Ptr + T.cmdsize;
     971             :   uint32_t nflavor = 0;
     972             :   uint32_t cputype = getCPUType(Obj);
     973          37 :   while (state < end) {
     974          21 :     if(state + sizeof(uint32_t) > end)
     975           0 :       return malformedError("load command " + Twine(LoadCommandIndex) +
     976           0 :                             "flavor in " + CmdName + " extends past end of "
     977           0 :                             "command");
     978             :     uint32_t flavor;
     979          21 :     memcpy(&flavor, state, sizeof(uint32_t));
     980          21 :     if (Obj.isLittleEndian() != sys::IsLittleEndianHost)
     981             :       sys::swapByteOrder(flavor);
     982             :     state += sizeof(uint32_t);
     983             : 
     984          21 :     if(state + sizeof(uint32_t) > end)
     985           1 :       return malformedError("load command " + Twine(LoadCommandIndex) +
     986           2 :                             " count in " + CmdName + " extends past end of "
     987           2 :                             "command");
     988             :     uint32_t count;
     989          20 :     memcpy(&count, state, sizeof(uint32_t));
     990             :     if (Obj.isLittleEndian() != sys::IsLittleEndianHost)
     991             :       sys::swapByteOrder(count);
     992             :     state += sizeof(uint32_t);
     993             : 
     994          20 :     if (cputype == MachO::CPU_TYPE_I386) {
     995           1 :       if (flavor == MachO::x86_THREAD_STATE32) {
     996           1 :         if (count != MachO::x86_THREAD_STATE32_COUNT)
     997           0 :           return malformedError("load command " + Twine(LoadCommandIndex) +
     998             :                                 " count not x86_THREAD_STATE32_COUNT for "
     999           0 :                                 "flavor number " + Twine(nflavor) + " which is "
    1000           0 :                                 "a x86_THREAD_STATE32 flavor in " + CmdName +
    1001           0 :                                 " command");
    1002           1 :         if (state + sizeof(MachO::x86_thread_state32_t) > end)
    1003           0 :           return malformedError("load command " + Twine(LoadCommandIndex) +
    1004             :                                 " x86_THREAD_STATE32 extends past end of "
    1005           0 :                                 "command in " + CmdName + " command");
    1006             :         state += sizeof(MachO::x86_thread_state32_t);
    1007             :       } else {
    1008           0 :         return malformedError("load command " + Twine(LoadCommandIndex) +
    1009           0 :                               " unknown flavor (" + Twine(flavor) + ") for "
    1010           0 :                               "flavor number " + Twine(nflavor) + " in " +
    1011           0 :                               CmdName + " command");
    1012             :       }
    1013          19 :     } else if (cputype == MachO::CPU_TYPE_X86_64) {
    1014          10 :       if (flavor == MachO::x86_THREAD_STATE64) {
    1015           9 :         if (count != MachO::x86_THREAD_STATE64_COUNT)
    1016           1 :           return malformedError("load command " + Twine(LoadCommandIndex) +
    1017             :                                 " count not x86_THREAD_STATE64_COUNT for "
    1018           2 :                                 "flavor number " + Twine(nflavor) + " which is "
    1019           2 :                                 "a x86_THREAD_STATE64 flavor in " + CmdName +
    1020           2 :                                 " command");
    1021           8 :         if (state + sizeof(MachO::x86_thread_state64_t) > end)
    1022           1 :           return malformedError("load command " + Twine(LoadCommandIndex) +
    1023             :                                 " x86_THREAD_STATE64 extends past end of "
    1024           2 :                                 "command in " + CmdName + " command");
    1025             :         state += sizeof(MachO::x86_thread_state64_t);
    1026             :       } else {
    1027           1 :         return malformedError("load command " + Twine(LoadCommandIndex) +
    1028           2 :                               " unknown flavor (" + Twine(flavor) + ") for "
    1029           2 :                               "flavor number " + Twine(nflavor) + " in " +
    1030           2 :                               CmdName + " command");
    1031             :       }
    1032           9 :     } else if (cputype == MachO::CPU_TYPE_ARM) {
    1033           5 :       if (flavor == MachO::ARM_THREAD_STATE) {
    1034           5 :         if (count != MachO::ARM_THREAD_STATE_COUNT)
    1035           0 :           return malformedError("load command " + Twine(LoadCommandIndex) +
    1036             :                                 " count not ARM_THREAD_STATE_COUNT for "
    1037           0 :                                 "flavor number " + Twine(nflavor) + " which is "
    1038           0 :                                 "a ARM_THREAD_STATE flavor in " + CmdName +
    1039           0 :                                 " command");
    1040           5 :         if (state + sizeof(MachO::arm_thread_state32_t) > end)
    1041           0 :           return malformedError("load command " + Twine(LoadCommandIndex) +
    1042             :                                 " ARM_THREAD_STATE extends past end of "
    1043           0 :                                 "command in " + CmdName + " command");
    1044             :         state += sizeof(MachO::arm_thread_state32_t);
    1045             :       } else {
    1046           0 :         return malformedError("load command " + Twine(LoadCommandIndex) +
    1047           0 :                               " unknown flavor (" + Twine(flavor) + ") for "
    1048           0 :                               "flavor number " + Twine(nflavor) + " in " +
    1049           0 :                               CmdName + " command");
    1050             :       }
    1051           4 :     } else if (cputype == MachO::CPU_TYPE_ARM64) {
    1052           1 :       if (flavor == MachO::ARM_THREAD_STATE64) {
    1053           1 :         if (count != MachO::ARM_THREAD_STATE64_COUNT)
    1054           0 :           return malformedError("load command " + Twine(LoadCommandIndex) +
    1055             :                                 " count not ARM_THREAD_STATE64_COUNT for "
    1056           0 :                                 "flavor number " + Twine(nflavor) + " which is "
    1057           0 :                                 "a ARM_THREAD_STATE64 flavor in " + CmdName +
    1058           0 :                                 " command");
    1059           1 :         if (state + sizeof(MachO::arm_thread_state64_t) > end)
    1060           0 :           return malformedError("load command " + Twine(LoadCommandIndex) +
    1061             :                                 " ARM_THREAD_STATE64 extends past end of "
    1062           0 :                                 "command in " + CmdName + " command");
    1063             :         state += sizeof(MachO::arm_thread_state64_t);
    1064             :       } else {
    1065           0 :         return malformedError("load command " + Twine(LoadCommandIndex) +
    1066           0 :                               " unknown flavor (" + Twine(flavor) + ") for "
    1067           0 :                               "flavor number " + Twine(nflavor) + " in " +
    1068           0 :                               CmdName + " command");
    1069             :       }
    1070           3 :     } else if (cputype == MachO::CPU_TYPE_POWERPC) {
    1071           2 :       if (flavor == MachO::PPC_THREAD_STATE) {
    1072           2 :         if (count != MachO::PPC_THREAD_STATE_COUNT)
    1073           0 :           return malformedError("load command " + Twine(LoadCommandIndex) +
    1074             :                                 " count not PPC_THREAD_STATE_COUNT for "
    1075           0 :                                 "flavor number " + Twine(nflavor) + " which is "
    1076           0 :                                 "a PPC_THREAD_STATE flavor in " + CmdName +
    1077           0 :                                 " command");
    1078           2 :         if (state + sizeof(MachO::ppc_thread_state32_t) > end)
    1079           0 :           return malformedError("load command " + Twine(LoadCommandIndex) +
    1080             :                                 " PPC_THREAD_STATE extends past end of "
    1081           0 :                                 "command in " + CmdName + " command");
    1082             :         state += sizeof(MachO::ppc_thread_state32_t);
    1083             :       } else {
    1084           0 :         return malformedError("load command " + Twine(LoadCommandIndex) +
    1085           0 :                               " unknown flavor (" + Twine(flavor) + ") for "
    1086           0 :                               "flavor number " + Twine(nflavor) + " in " +
    1087           0 :                               CmdName + " command");
    1088             :       }
    1089             :     } else {
    1090           1 :       return malformedError("unknown cputype (" + Twine(cputype) + ") load "
    1091           2 :                             "command " + Twine(LoadCommandIndex) + " for " +
    1092           2 :                             CmdName + " command can't be checked");
    1093             :     }
    1094          16 :     nflavor++;
    1095             :   }
    1096             :   return Error::success();
    1097             : }
    1098             : 
    1099           8 : static Error checkTwoLevelHintsCommand(const MachOObjectFile &Obj,
    1100             :                                        const MachOObjectFile::LoadCommandInfo
    1101             :                                          &Load,
    1102             :                                        uint32_t LoadCommandIndex,
    1103             :                                        const char **LoadCmd,
    1104             :                                        std::list<MachOElement> &Elements) {
    1105           8 :   if (Load.C.cmdsize != sizeof(MachO::twolevel_hints_command))
    1106           1 :     return malformedError("load command " + Twine(LoadCommandIndex) +
    1107           2 :                           " LC_TWOLEVEL_HINTS has incorrect cmdsize");
    1108           7 :   if (*LoadCmd != nullptr)
    1109           1 :     return malformedError("more than one LC_TWOLEVEL_HINTS command");
    1110             :   MachO::twolevel_hints_command Hints =
    1111           6 :     getStruct<MachO::twolevel_hints_command>(Obj, Load.Ptr);
    1112          12 :   uint64_t FileSize = Obj.getData().size();
    1113           6 :   if (Hints.offset > FileSize)
    1114           1 :     return malformedError("offset field of LC_TWOLEVEL_HINTS command " +
    1115           2 :                           Twine(LoadCommandIndex) + " extends past the end of "
    1116           2 :                           "the file");
    1117           5 :   uint64_t BigSize = Hints.nhints;
    1118           5 :   BigSize *= sizeof(MachO::twolevel_hint);
    1119           5 :   BigSize += Hints.offset;
    1120           5 :   if (BigSize > FileSize)
    1121             :     return malformedError("offset field plus nhints times sizeof(struct "
    1122           1 :                           "twolevel_hint) field of LC_TWOLEVEL_HINTS command " +
    1123           2 :                           Twine(LoadCommandIndex) + " extends past the end of "
    1124           2 :                           "the file");
    1125           4 :   if (Error Err = checkOverlappingElement(Elements, Hints.offset, Hints.nhints *
    1126             :                                           sizeof(MachO::twolevel_hint),
    1127           4 :                                           "two level hints"))
    1128             :     return Err;
    1129           3 :   *LoadCmd = Load.Ptr;
    1130             :   return Error::success();
    1131             : }
    1132             : 
    1133             : // Returns true if the libObject code does not support the load command and its
    1134             : // contents.  The cmd value it is treated as an unknown load command but with
    1135             : // an error message that says the cmd value is obsolete.
    1136          10 : static bool isLoadCommandObsolete(uint32_t cmd) {
    1137          20 :   if (cmd == MachO::LC_SYMSEG ||
    1138          10 :       cmd == MachO::LC_LOADFVMLIB ||
    1139           8 :       cmd == MachO::LC_IDFVMLIB ||
    1140             :       cmd == MachO::LC_IDENT ||
    1141           6 :       cmd == MachO::LC_FVMFILE ||
    1142             :       cmd == MachO::LC_PREPAGE ||
    1143           8 :       cmd == MachO::LC_PREBOUND_DYLIB ||
    1144           7 :       cmd == MachO::LC_TWOLEVEL_HINTS ||
    1145             :       cmd == MachO::LC_PREBIND_CKSUM)
    1146             :     return true;
    1147           2 :   return false;
    1148             : }
    1149             : 
    1150             : Expected<std::unique_ptr<MachOObjectFile>>
    1151        1772 : MachOObjectFile::create(MemoryBufferRef Object, bool IsLittleEndian,
    1152             :                         bool Is64Bits, uint32_t UniversalCputype,
    1153             :                         uint32_t UniversalIndex) {
    1154             :   Error Err = Error::success();
    1155             :   std::unique_ptr<MachOObjectFile> Obj(
    1156             :       new MachOObjectFile(std::move(Object), IsLittleEndian,
    1157             :                           Is64Bits, Err, UniversalCputype,
    1158        1772 :                           UniversalIndex));
    1159        1772 :   if (Err)
    1160             :     return std::move(Err);
    1161             :   return std::move(Obj);
    1162             : }
    1163             : 
    1164        1772 : MachOObjectFile::MachOObjectFile(MemoryBufferRef Object, bool IsLittleEndian,
    1165             :                                  bool Is64bits, Error &Err,
    1166             :                                  uint32_t UniversalCputype,
    1167        1772 :                                  uint32_t UniversalIndex)
    1168        5316 :     : ObjectFile(getMachOType(IsLittleEndian, Is64bits), Object) {
    1169             :   ErrorAsOutParameter ErrAsOutParam(&Err);
    1170             :   uint64_t SizeOfHeaders;
    1171             :   uint32_t cputype;
    1172        1772 :   if (is64Bit()) {
    1173        1263 :     parseHeader(*this, Header64, Err);
    1174             :     SizeOfHeaders = sizeof(MachO::mach_header_64);
    1175        1263 :     cputype = Header64.cputype;
    1176             :   } else {
    1177         509 :     parseHeader(*this, Header, Err);
    1178             :     SizeOfHeaders = sizeof(MachO::mach_header);
    1179         509 :     cputype = Header.cputype;
    1180             :   }
    1181        1772 :   if (Err)
    1182             :     return;
    1183        1772 :   SizeOfHeaders += getHeader().sizeofcmds;
    1184        5316 :   if (getData().data() + SizeOfHeaders > getData().end()) {
    1185          12 :     Err = malformedError("load commands extend past the end of the file");
    1186           6 :     return;
    1187             :   }
    1188        1766 :   if (UniversalCputype != 0 && cputype != UniversalCputype) {
    1189           3 :     Err = malformedError("universal header architecture: " +
    1190           2 :                          Twine(UniversalIndex) + "'s cputype does not match "
    1191           1 :                          "object file's mach header");
    1192           1 :     return;
    1193             :   }
    1194             :   std::list<MachOElement> Elements;
    1195        3530 :   Elements.push_back({0, SizeOfHeaders, "Mach-O headers"});
    1196             : 
    1197        1765 :   uint32_t LoadCommandCount = getHeader().ncmds;
    1198             :   LoadCommandInfo Load;
    1199        1765 :   if (LoadCommandCount != 0) {
    1200        3518 :     if (auto LoadOrErr = getFirstLoadCommandInfo(*this))
    1201        1758 :       Load = *LoadOrErr;
    1202             :     else {
    1203             :       Err = LoadOrErr.takeError();
    1204             :       return;
    1205             :     }
    1206             :   }
    1207             : 
    1208        1764 :   const char *DyldIdLoadCmd = nullptr;
    1209        1764 :   const char *FuncStartsLoadCmd = nullptr;
    1210        1764 :   const char *SplitInfoLoadCmd = nullptr;
    1211        1764 :   const char *CodeSignDrsLoadCmd = nullptr;
    1212        1764 :   const char *CodeSignLoadCmd = nullptr;
    1213        1764 :   const char *VersLoadCmd = nullptr;
    1214             :   const char *SourceLoadCmd = nullptr;
    1215             :   const char *EntryPointLoadCmd = nullptr;
    1216        1764 :   const char *EncryptLoadCmd = nullptr;
    1217             :   const char *RoutinesLoadCmd = nullptr;
    1218             :   const char *UnixThreadLoadCmd = nullptr;
    1219        1764 :   const char *TwoLevelHintsLoadCmd = nullptr;
    1220       22080 :   for (unsigned I = 0; I < LoadCommandCount; ++I) {
    1221       10297 :     if (is64Bit()) {
    1222        8181 :       if (Load.C.cmdsize % 8 != 0) {
    1223             :         // We have a hack here to allow 64-bit Mach-O core files to have
    1224             :         // LC_THREAD commands that are only a multiple of 4 and not 8 to be
    1225             :         // allowed since the macOS kernel produces them.
    1226           1 :         if (getHeader().filetype != MachO::MH_CORE ||
    1227           1 :             Load.C.cmd != MachO::LC_THREAD || Load.C.cmdsize % 4) {
    1228           2 :           Err = malformedError("load command " + Twine(I) + " cmdsize not a "
    1229           1 :                                "multiple of 8");
    1230           1 :           return;
    1231             :         }
    1232             :       }
    1233             :     } else {
    1234        2116 :       if (Load.C.cmdsize % 4 != 0) {
    1235           2 :         Err = malformedError("load command " + Twine(I) + " cmdsize not a "
    1236           1 :                              "multiple of 4");
    1237           1 :         return;
    1238             :       }
    1239             :     }
    1240       10295 :     LoadCommands.push_back(Load);
    1241       10295 :     if (Load.C.cmd == MachO::LC_SYMTAB) {
    1242        3044 :       if ((Err = checkSymtabCommand(*this, Load, I, &SymtabLoadCmd, Elements)))
    1243             :         return;
    1244        8773 :     } else if (Load.C.cmd == MachO::LC_DYSYMTAB) {
    1245        2848 :       if ((Err = checkDysymtabCommand(*this, Load, I, &DysymtabLoadCmd,
    1246             :                                       Elements)))
    1247             :         return;
    1248        7349 :     } else if (Load.C.cmd == MachO::LC_DATA_IN_CODE) {
    1249         838 :       if ((Err = checkLinkeditDataCommand(*this, Load, I, &DataInCodeLoadCmd,
    1250             :                                           "LC_DATA_IN_CODE", Elements,
    1251             :                                           "data in code info")))
    1252             :         return;
    1253        6930 :     } else if (Load.C.cmd == MachO::LC_LINKER_OPTIMIZATION_HINT) {
    1254           8 :       if ((Err = checkLinkeditDataCommand(*this, Load, I, &LinkOptHintsLoadCmd,
    1255             :                                           "LC_LINKER_OPTIMIZATION_HINT",
    1256             :                                           Elements, "linker optimization "
    1257             :                                           "hints")))
    1258             :         return;
    1259        6926 :     } else if (Load.C.cmd == MachO::LC_FUNCTION_STARTS) {
    1260         766 :       if ((Err = checkLinkeditDataCommand(*this, Load, I, &FuncStartsLoadCmd,
    1261             :                                           "LC_FUNCTION_STARTS", Elements,
    1262             :                                           "function starts data")))
    1263             :         return;
    1264        6543 :     } else if (Load.C.cmd == MachO::LC_SEGMENT_SPLIT_INFO) {
    1265          14 :       if ((Err = checkLinkeditDataCommand(*this, Load, I, &SplitInfoLoadCmd,
    1266             :                                           "LC_SEGMENT_SPLIT_INFO", Elements,
    1267             :                                           "split info data")))
    1268             :         return;
    1269        6536 :     } else if (Load.C.cmd == MachO::LC_DYLIB_CODE_SIGN_DRS) {
    1270         344 :       if ((Err = checkLinkeditDataCommand(*this, Load, I, &CodeSignDrsLoadCmd,
    1271             :                                           "LC_DYLIB_CODE_SIGN_DRS", Elements,
    1272             :                                           "code signing RDs data")))
    1273             :         return;
    1274        6364 :     } else if (Load.C.cmd == MachO::LC_CODE_SIGNATURE) {
    1275          12 :       if ((Err = checkLinkeditDataCommand(*this, Load, I, &CodeSignLoadCmd,
    1276             :                                           "LC_CODE_SIGNATURE", Elements,
    1277             :                                           "code signature data")))
    1278             :         return;
    1279        6358 :     } else if (Load.C.cmd == MachO::LC_DYLD_INFO) {
    1280          26 :       if ((Err = checkDyldInfoCommand(*this, Load, I, &DyldInfoLoadCmd,
    1281             :                                       "LC_DYLD_INFO", Elements)))
    1282             :         return;
    1283        6345 :     } else if (Load.C.cmd == MachO::LC_DYLD_INFO_ONLY) {
    1284         794 :       if ((Err = checkDyldInfoCommand(*this, Load, I, &DyldInfoLoadCmd,
    1285             :                                       "LC_DYLD_INFO_ONLY", Elements)))
    1286             :         return;
    1287        5948 :     } else if (Load.C.cmd == MachO::LC_UUID) {
    1288         380 :       if (Load.C.cmdsize != sizeof(MachO::uuid_command)) {
    1289           2 :         Err = malformedError("LC_UUID command " + Twine(I) + " has incorrect "
    1290           1 :                              "cmdsize");
    1291           1 :         return;
    1292             :       }
    1293         379 :       if (UuidLoadCmd) {
    1294           2 :         Err = malformedError("more than one LC_UUID command");
    1295           1 :         return;
    1296             :       }
    1297         378 :       UuidLoadCmd = Load.Ptr;
    1298        5568 :     } else if (Load.C.cmd == MachO::LC_SEGMENT_64) {
    1299        2347 :       if ((Err = parseSegmentLoadCommand<MachO::segment_command_64,
    1300        4694 :                                          MachO::section_64>(
    1301             :                    *this, Load, Sections, HasPageZeroSegment, I,
    1302             :                    "LC_SEGMENT_64", SizeOfHeaders, Elements)))
    1303             :         return;
    1304        3221 :     } else if (Load.C.cmd == MachO::LC_SEGMENT) {
    1305         565 :       if ((Err = parseSegmentLoadCommand<MachO::segment_command,
    1306        1130 :                                          MachO::section>(
    1307             :                    *this, Load, Sections, HasPageZeroSegment, I,
    1308             :                    "LC_SEGMENT", SizeOfHeaders, Elements)))
    1309             :         return;
    1310        2656 :     } else if (Load.C.cmd == MachO::LC_ID_DYLIB) {
    1311         238 :       if ((Err = checkDylibIdCommand(*this, Load, I, &DyldIdLoadCmd)))
    1312             :         return;
    1313        2537 :     } else if (Load.C.cmd == MachO::LC_LOAD_DYLIB) {
    1314        1000 :       if ((Err = checkDylibCommand(*this, Load, I, "LC_LOAD_DYLIB")))
    1315             :         return;
    1316         499 :       Libraries.push_back(Load.Ptr);
    1317        2037 :     } else if (Load.C.cmd == MachO::LC_LOAD_WEAK_DYLIB) {
    1318           8 :       if ((Err = checkDylibCommand(*this, Load, I, "LC_LOAD_WEAK_DYLIB")))
    1319             :         return;
    1320           3 :       Libraries.push_back(Load.Ptr);
    1321        2033 :     } else if (Load.C.cmd == MachO::LC_LAZY_LOAD_DYLIB) {
    1322           8 :       if ((Err = checkDylibCommand(*this, Load, I, "LC_LAZY_LOAD_DYLIB")))
    1323             :         return;
    1324           3 :       Libraries.push_back(Load.Ptr);
    1325        2029 :     } else if (Load.C.cmd == MachO::LC_REEXPORT_DYLIB) {
    1326           6 :       if ((Err = checkDylibCommand(*this, Load, I, "LC_REEXPORT_DYLIB")))
    1327             :         return;
    1328           3 :       Libraries.push_back(Load.Ptr);
    1329        2026 :     } else if (Load.C.cmd == MachO::LC_LOAD_UPWARD_DYLIB) {
    1330           4 :       if ((Err = checkDylibCommand(*this, Load, I, "LC_LOAD_UPWARD_DYLIB")))
    1331             :         return;
    1332           1 :       Libraries.push_back(Load.Ptr);
    1333        2024 :     } else if (Load.C.cmd == MachO::LC_ID_DYLINKER) {
    1334           6 :       if ((Err = checkDyldCommand(*this, Load, I, "LC_ID_DYLINKER")))
    1335             :         return;
    1336        2021 :     } else if (Load.C.cmd == MachO::LC_LOAD_DYLINKER) {
    1337         550 :       if ((Err = checkDyldCommand(*this, Load, I, "LC_LOAD_DYLINKER")))
    1338             :         return;
    1339        1746 :     } else if (Load.C.cmd == MachO::LC_DYLD_ENVIRONMENT) {
    1340           2 :       if ((Err = checkDyldCommand(*this, Load, I, "LC_DYLD_ENVIRONMENT")))
    1341             :         return;
    1342        1745 :     } else if (Load.C.cmd == MachO::LC_VERSION_MIN_MACOSX) {
    1343        1542 :       if ((Err = checkVersCommand(*this, Load, I, &VersLoadCmd,
    1344             :                                   "LC_VERSION_MIN_MACOSX")))
    1345             :         return;
    1346         974 :     } else if (Load.C.cmd == MachO::LC_VERSION_MIN_IPHONEOS) {
    1347         252 :       if ((Err = checkVersCommand(*this, Load, I, &VersLoadCmd,
    1348             :                                   "LC_VERSION_MIN_IPHONEOS")))
    1349             :         return;
    1350         848 :     } else if (Load.C.cmd == MachO::LC_VERSION_MIN_TVOS) {
    1351           4 :       if ((Err = checkVersCommand(*this, Load, I, &VersLoadCmd,
    1352             :                                   "LC_VERSION_MIN_TVOS")))
    1353             :         return;
    1354         846 :     } else if (Load.C.cmd == MachO::LC_VERSION_MIN_WATCHOS) {
    1355           8 :       if ((Err = checkVersCommand(*this, Load, I, &VersLoadCmd,
    1356             :                                   "LC_VERSION_MIN_WATCHOS")))
    1357             :         return;
    1358         842 :     } else if (Load.C.cmd == MachO::LC_NOTE) {
    1359           4 :       if ((Err = checkNoteCommand(*this, Load, I, Elements)))
    1360             :         return;
    1361         840 :     } else if (Load.C.cmd == MachO::LC_BUILD_VERSION) {
    1362           4 :       if ((Err = parseBuildVersionCommand(*this, Load, BuildTools, I)))
    1363             :         return;
    1364         838 :     } else if (Load.C.cmd == MachO::LC_RPATH) {
    1365          22 :       if ((Err = checkRpathCommand(*this, Load, I)))
    1366             :         return;
    1367         827 :     } else if (Load.C.cmd == MachO::LC_SOURCE_VERSION) {
    1368         372 :       if (Load.C.cmdsize != sizeof(MachO::source_version_command)) {
    1369           2 :         Err = malformedError("LC_SOURCE_VERSION command " + Twine(I) +
    1370           1 :                              " has incorrect cmdsize");
    1371           1 :         return;
    1372             :       }
    1373         371 :       if (SourceLoadCmd) {
    1374           2 :         Err = malformedError("more than one LC_SOURCE_VERSION command");
    1375           1 :         return;
    1376             :       }
    1377         370 :       SourceLoadCmd = Load.Ptr;
    1378         455 :     } else if (Load.C.cmd == MachO::LC_MAIN) {
    1379         269 :       if (Load.C.cmdsize != sizeof(MachO::entry_point_command)) {
    1380           2 :         Err = malformedError("LC_MAIN command " + Twine(I) +
    1381           1 :                              " has incorrect cmdsize");
    1382           1 :         return;
    1383             :       }
    1384         268 :       if (EntryPointLoadCmd) {
    1385           2 :         Err = malformedError("more than one LC_MAIN command");
    1386           1 :         return;
    1387             :       }
    1388         267 :       EntryPointLoadCmd = Load.Ptr;
    1389         186 :     } else if (Load.C.cmd == MachO::LC_ENCRYPTION_INFO) {
    1390           5 :       if (Load.C.cmdsize != sizeof(MachO::encryption_info_command)) {
    1391           2 :         Err = malformedError("LC_ENCRYPTION_INFO command " + Twine(I) +
    1392           1 :                              " has incorrect cmdsize");
    1393           3 :         return;
    1394             :       }
    1395             :       MachO::encryption_info_command E =
    1396           4 :         getStruct<MachO::encryption_info_command>(*this, Load.Ptr);
    1397           8 :       if ((Err = checkEncryptCommand(*this, Load, I, E.cryptoff, E.cryptsize,
    1398             :                                      &EncryptLoadCmd, "LC_ENCRYPTION_INFO")))
    1399             :         return;
    1400         181 :     } else if (Load.C.cmd == MachO::LC_ENCRYPTION_INFO_64) {
    1401          18 :       if (Load.C.cmdsize != sizeof(MachO::encryption_info_command_64)) {
    1402           2 :         Err = malformedError("LC_ENCRYPTION_INFO_64 command " + Twine(I) +
    1403           1 :                              " has incorrect cmdsize");
    1404           4 :         return;
    1405             :       }
    1406             :       MachO::encryption_info_command_64 E =
    1407          17 :         getStruct<MachO::encryption_info_command_64>(*this, Load.Ptr);
    1408          34 :       if ((Err = checkEncryptCommand(*this, Load, I, E.cryptoff, E.cryptsize,
    1409             :                                      &EncryptLoadCmd, "LC_ENCRYPTION_INFO_64")))
    1410             :         return;
    1411         163 :     } else if (Load.C.cmd == MachO::LC_LINKER_OPTION) {
    1412         216 :       if ((Err = checkLinkerOptCommand(*this, Load, I)))
    1413             :         return;
    1414          55 :     } else if (Load.C.cmd == MachO::LC_SUB_FRAMEWORK) {
    1415           4 :       if (Load.C.cmdsize < sizeof(MachO::sub_framework_command)) {
    1416           2 :         Err =  malformedError("load command " + Twine(I) +
    1417           1 :                               " LC_SUB_FRAMEWORK cmdsize too small");
    1418           2 :         return;
    1419             :       }
    1420             :       MachO::sub_framework_command S =
    1421           3 :         getStruct<MachO::sub_framework_command>(*this, Load.Ptr);
    1422           6 :       if ((Err = checkSubCommand(*this, Load, I, "LC_SUB_FRAMEWORK",
    1423             :                                  sizeof(MachO::sub_framework_command),
    1424             :                                  "sub_framework_command", S.umbrella,
    1425             :                                  "umbrella")))
    1426             :         return;
    1427          51 :     } else if (Load.C.cmd == MachO::LC_SUB_UMBRELLA) {
    1428           2 :       if (Load.C.cmdsize < sizeof(MachO::sub_umbrella_command)) {
    1429           0 :         Err =  malformedError("load command " + Twine(I) +
    1430           0 :                               " LC_SUB_UMBRELLA cmdsize too small");
    1431           1 :         return;
    1432             :       }
    1433             :       MachO::sub_umbrella_command S =
    1434           2 :         getStruct<MachO::sub_umbrella_command>(*this, Load.Ptr);
    1435           4 :       if ((Err = checkSubCommand(*this, Load, I, "LC_SUB_UMBRELLA",
    1436             :                                  sizeof(MachO::sub_umbrella_command),
    1437             :                                  "sub_umbrella_command", S.sub_umbrella,
    1438             :                                  "sub_umbrella")))
    1439             :         return;
    1440          49 :     } else if (Load.C.cmd == MachO::LC_SUB_LIBRARY) {
    1441           2 :       if (Load.C.cmdsize < sizeof(MachO::sub_library_command)) {
    1442           0 :         Err =  malformedError("load command " + Twine(I) +
    1443           0 :                               " LC_SUB_LIBRARY cmdsize too small");
    1444           1 :         return;
    1445             :       }
    1446             :       MachO::sub_library_command S =
    1447           2 :         getStruct<MachO::sub_library_command>(*this, Load.Ptr);
    1448           4 :       if ((Err = checkSubCommand(*this, Load, I, "LC_SUB_LIBRARY",
    1449             :                                  sizeof(MachO::sub_library_command),
    1450             :                                  "sub_library_command", S.sub_library,
    1451             :                                  "sub_library")))
    1452             :         return;
    1453          47 :     } else if (Load.C.cmd == MachO::LC_SUB_CLIENT) {
    1454           2 :       if (Load.C.cmdsize < sizeof(MachO::sub_client_command)) {
    1455           0 :         Err =  malformedError("load command " + Twine(I) +
    1456           0 :                               " LC_SUB_CLIENT cmdsize too small");
    1457           1 :         return;
    1458             :       }
    1459             :       MachO::sub_client_command S =
    1460           2 :         getStruct<MachO::sub_client_command>(*this, Load.Ptr);
    1461           4 :       if ((Err = checkSubCommand(*this, Load, I, "LC_SUB_CLIENT",
    1462             :                                  sizeof(MachO::sub_client_command),
    1463             :                                  "sub_client_command", S.client, "client")))
    1464             :         return;
    1465          45 :     } else if (Load.C.cmd == MachO::LC_ROUTINES) {
    1466           2 :       if (Load.C.cmdsize != sizeof(MachO::routines_command)) {
    1467           2 :         Err = malformedError("LC_ROUTINES command " + Twine(I) +
    1468           1 :                              " has incorrect cmdsize");
    1469           1 :         return;
    1470             :       }
    1471           1 :       if (RoutinesLoadCmd) {
    1472           0 :         Err = malformedError("more than one LC_ROUTINES and or LC_ROUTINES_64 "
    1473             :                              "command");
    1474           0 :         return;
    1475             :       }
    1476           1 :       RoutinesLoadCmd = Load.Ptr;
    1477          43 :     } else if (Load.C.cmd == MachO::LC_ROUTINES_64) {
    1478           4 :       if (Load.C.cmdsize != sizeof(MachO::routines_command_64)) {
    1479           0 :         Err = malformedError("LC_ROUTINES_64 command " + Twine(I) +
    1480           0 :                              " has incorrect cmdsize");
    1481           0 :         return;
    1482             :       }
    1483           4 :       if (RoutinesLoadCmd) {
    1484           2 :         Err = malformedError("more than one LC_ROUTINES_64 and or LC_ROUTINES "
    1485             :                              "command");
    1486           1 :         return;
    1487             :       }
    1488           3 :       RoutinesLoadCmd = Load.Ptr;
    1489          39 :     } else if (Load.C.cmd == MachO::LC_UNIXTHREAD) {
    1490          32 :       if ((Err = checkThreadCommand(*this, Load, I, "LC_UNIXTHREAD")))
    1491             :         return;
    1492          15 :       if (UnixThreadLoadCmd) {
    1493           2 :         Err = malformedError("more than one LC_UNIXTHREAD command");
    1494           1 :         return;
    1495             :       }
    1496          14 :       UnixThreadLoadCmd = Load.Ptr;
    1497          23 :     } else if (Load.C.cmd == MachO::LC_THREAD) {
    1498          10 :       if ((Err = checkThreadCommand(*this, Load, I, "LC_THREAD")))
    1499             :         return;
    1500             :     // Note: LC_TWOLEVEL_HINTS is really obsolete and is not supported.
    1501          18 :     } else if (Load.C.cmd == MachO::LC_TWOLEVEL_HINTS) {
    1502          16 :        if ((Err = checkTwoLevelHintsCommand(*this, Load, I,
    1503             :                                             &TwoLevelHintsLoadCmd, Elements)))
    1504             :          return;
    1505          10 :     } else if (isLoadCommandObsolete(Load.C.cmd)) {
    1506          24 :       Err = malformedError("load command " + Twine(I) + " for cmd value of: " +
    1507          16 :                            Twine(Load.C.cmd) + " is obsolete and not "
    1508           8 :                            "supported");
    1509           8 :       return;
    1510             :     }
    1511             :     // TODO: generate a error for unknown load commands by default.  But still
    1512             :     // need work out an approach to allow or not allow unknown values like this
    1513             :     // as an option for some uses like lldb.
    1514       10160 :     if (I < LoadCommandCount - 1) {
    1515       17082 :       if (auto LoadOrErr = getNextLoadCommandInfo(*this, I, Load))
    1516        8539 :         Load = *LoadOrErr;
    1517             :       else {
    1518             :         Err = LoadOrErr.takeError();
    1519             :         return;
    1520             :       }
    1521             :     }
    1522             :   }
    1523        1625 :   if (!SymtabLoadCmd) {
    1524         115 :     if (DysymtabLoadCmd) {
    1525           0 :       Err = malformedError("contains LC_DYSYMTAB load command without a "
    1526             :                            "LC_SYMTAB load command");
    1527           0 :       return;
    1528             :     }
    1529        1510 :   } else if (DysymtabLoadCmd) {
    1530             :     MachO::symtab_command Symtab =
    1531        1402 :       getStruct<MachO::symtab_command>(*this, SymtabLoadCmd);
    1532             :     MachO::dysymtab_command Dysymtab =
    1533        1402 :       getStruct<MachO::dysymtab_command>(*this, DysymtabLoadCmd);
    1534        1402 :     if (Dysymtab.nlocalsym != 0 && Dysymtab.ilocalsym > Symtab.nsyms) {
    1535           0 :       Err = malformedError("ilocalsym in LC_DYSYMTAB load command "
    1536             :                            "extends past the end of the symbol table");
    1537           1 :       return;
    1538             :     }
    1539        1402 :     uint64_t BigSize = Dysymtab.ilocalsym;
    1540        1402 :     BigSize += Dysymtab.nlocalsym;
    1541        1402 :     if (Dysymtab.nlocalsym != 0 && BigSize > Symtab.nsyms) {
    1542           2 :       Err = malformedError("ilocalsym plus nlocalsym in LC_DYSYMTAB load "
    1543             :                            "command extends past the end of the symbol table");
    1544           1 :       return;
    1545             :     }
    1546        1401 :     if (Dysymtab.nextdefsym != 0 && Dysymtab.ilocalsym > Symtab.nsyms) {
    1547           0 :       Err = malformedError("nextdefsym in LC_DYSYMTAB load command "
    1548             :                            "extends past the end of the symbol table");
    1549           0 :       return;
    1550             :     }
    1551        1401 :     BigSize = Dysymtab.iextdefsym;
    1552        1401 :     BigSize += Dysymtab.nextdefsym;
    1553        1401 :     if (Dysymtab.nextdefsym != 0 && BigSize > Symtab.nsyms) {
    1554           0 :       Err = malformedError("iextdefsym plus nextdefsym in LC_DYSYMTAB "
    1555             :                            "load command extends past the end of the symbol "
    1556             :                            "table");
    1557           0 :       return;
    1558             :     }
    1559        1401 :     if (Dysymtab.nundefsym != 0 && Dysymtab.iundefsym > Symtab.nsyms) {
    1560           0 :       Err = malformedError("nundefsym in LC_DYSYMTAB load command "
    1561             :                            "extends past the end of the symbol table");
    1562           0 :       return;
    1563             :     }
    1564        1401 :     BigSize = Dysymtab.iundefsym;
    1565        1401 :     BigSize += Dysymtab.nundefsym;
    1566        1401 :     if (Dysymtab.nundefsym != 0 && BigSize > Symtab.nsyms) {
    1567           0 :       Err = malformedError("iundefsym plus nundefsym in LC_DYSYMTAB load "
    1568             :                            " command extends past the end of the symbol table");
    1569           0 :       return;
    1570             :     }
    1571             :   }
    1572        3132 :   if ((getHeader().filetype == MachO::MH_DYLIB ||
    1573        1741 :        getHeader().filetype == MachO::MH_DYLIB_STUB) &&
    1574         117 :        DyldIdLoadCmd == nullptr) {
    1575           2 :     Err = malformedError("no LC_ID_DYLIB load command in dynamic library "
    1576             :                          "filetype");
    1577           1 :     return;
    1578             :   }
    1579             :   assert(LoadCommands.size() == LoadCommandCount);
    1580             : 
    1581             :   Err = Error::success();
    1582             : }
    1583             : 
    1584          89 : Error MachOObjectFile::checkSymbolTable() const {
    1585             :   uint32_t Flags = 0;
    1586          89 :   if (is64Bit()) {
    1587          51 :     MachO::mach_header_64 H_64 = MachOObjectFile::getHeader64();
    1588             :     Flags = H_64.flags;
    1589             :   } else {
    1590          38 :     MachO::mach_header H = MachOObjectFile::getHeader();
    1591             :     Flags = H.flags;
    1592             :   }
    1593             :   uint8_t NType = 0;
    1594             :   uint8_t NSect = 0;
    1595             :   uint16_t NDesc = 0;
    1596             :   uint32_t NStrx = 0;
    1597             :   uint64_t NValue = 0;
    1598             :   uint32_t SymbolIndex = 0;
    1599          89 :   MachO::symtab_command S = getSymtabLoadCommand();
    1600         691 :   for (const SymbolRef &Symbol : symbols()) {
    1601         342 :     DataRefImpl SymDRI = Symbol.getRawDataRefImpl();
    1602         342 :     if (is64Bit()) {
    1603         255 :       MachO::nlist_64 STE_64 = getSymbol64TableEntry(SymDRI);
    1604         255 :       NType = STE_64.n_type;
    1605         255 :       NSect = STE_64.n_sect;
    1606         255 :       NDesc = STE_64.n_desc;
    1607         255 :       NStrx = STE_64.n_strx;
    1608         255 :       NValue = STE_64.n_value;
    1609             :     } else {
    1610          87 :       MachO::nlist STE = getSymbolTableEntry(SymDRI);
    1611             :       NType = STE.n_type;
    1612             :       NType = STE.n_type;
    1613          87 :       NSect = STE.n_sect;
    1614          87 :       NDesc = STE.n_desc;
    1615          87 :       NStrx = STE.n_strx;
    1616          87 :       NValue = STE.n_value;
    1617             :     }
    1618         342 :     if ((NType & MachO::N_STAB) == 0 &&
    1619             :         (NType & MachO::N_TYPE) == MachO::N_SECT) {
    1620         440 :       if (NSect == 0 || NSect > Sections.size())
    1621           4 :         return malformedError("bad section index: " + Twine((int)NSect) +
    1622           4 :                               " for symbol at index " + Twine(SymbolIndex));
    1623             :     }
    1624         340 :     if ((NType & MachO::N_STAB) == 0 &&
    1625             :         (NType & MachO::N_TYPE) == MachO::N_INDR) {
    1626           2 :       if (NValue >= S.strsize)
    1627           4 :         return malformedError("bad n_value: " + Twine((int)NValue) + " past "
    1628             :                               "the end of string table, for N_INDR symbol at "
    1629           4 :                               "index " + Twine(SymbolIndex));
    1630             :     }
    1631         338 :     if ((Flags & MachO::MH_TWOLEVEL) == MachO::MH_TWOLEVEL &&
    1632          87 :         (((NType & MachO::N_TYPE) == MachO::N_UNDF && NValue == 0) ||
    1633             :          (NType & MachO::N_TYPE) == MachO::N_PBUD)) {
    1634          89 :       uint32_t LibraryOrdinal = MachO::GET_LIBRARY_ORDINAL(NDesc);
    1635         178 :       if (LibraryOrdinal != 0 &&
    1636         176 :           LibraryOrdinal != MachO::EXECUTABLE_ORDINAL &&
    1637         174 :           LibraryOrdinal != MachO::DYNAMIC_LOOKUP_ORDINAL &&
    1638          85 :           LibraryOrdinal - 1 >= Libraries.size() ) {
    1639           1 :         return malformedError("bad library ordinal: " + Twine(LibraryOrdinal) +
    1640           2 :                             " for symbol at index " + Twine(SymbolIndex));
    1641             :       }
    1642             :     }
    1643         337 :     if (NStrx >= S.strsize)
    1644           4 :       return malformedError("bad string table index: " + Twine((int)NStrx) +
    1645             :                             " past the end of string table, for symbol at "
    1646           4 :                             "index " + Twine(SymbolIndex));
    1647         335 :     SymbolIndex++;
    1648             :   }
    1649             :   return Error::success();
    1650             : }
    1651             : 
    1652        9662 : void MachOObjectFile::moveSymbolNext(DataRefImpl &Symb) const {
    1653        9662 :   unsigned SymbolTableEntrySize = is64Bit() ?
    1654             :     sizeof(MachO::nlist_64) :
    1655        9662 :     sizeof(MachO::nlist);
    1656        9662 :   Symb.p += SymbolTableEntrySize;
    1657        9662 : }
    1658             : 
    1659        3321 : Expected<StringRef> MachOObjectFile::getSymbolName(DataRefImpl Symb) const {
    1660        3321 :   StringRef StringTable = getStringTableData();
    1661             :   MachO::nlist_base Entry = getSymbolTableEntryBase(*this, Symb);
    1662        3321 :   if (Entry.n_strx == 0)
    1663             :     // A n_strx value of 0 indicates that no name is associated with a
    1664             :     // particular symbol table entry.
    1665             :     return StringRef();
    1666        3319 :   const char *Start = &StringTable.data()[Entry.n_strx];
    1667        9950 :   if (Start < getData().begin() || Start >= getData().end()) {
    1668          21 :     return malformedError("bad string index: " + Twine(Entry.n_strx) +
    1669          21 :                           " for symbol at index " + Twine(getSymbolIndex(Symb)));
    1670             :   }
    1671             :   return StringRef(Start);
    1672             : }
    1673             : 
    1674          41 : unsigned MachOObjectFile::getSectionType(SectionRef Sec) const {
    1675          41 :   DataRefImpl DRI = Sec.getRawDataRefImpl();
    1676          41 :   uint32_t Flags = getSectionFlags(*this, DRI);
    1677          41 :   return Flags & MachO::SECTION_TYPE;
    1678             : }
    1679             : 
    1680        3701 : uint64_t MachOObjectFile::getNValue(DataRefImpl Sym) const {
    1681        3701 :   if (is64Bit()) {
    1682        3009 :     MachO::nlist_64 Entry = getSymbol64TableEntry(Sym);
    1683        3009 :     return Entry.n_value;
    1684             :   }
    1685         692 :   MachO::nlist Entry = getSymbolTableEntry(Sym);
    1686         692 :   return Entry.n_value;
    1687             : }
    1688             : 
    1689             : // getIndirectName() returns the name of the alias'ed symbol who's string table
    1690             : // index is in the n_value field.
    1691           1 : std::error_code MachOObjectFile::getIndirectName(DataRefImpl Symb,
    1692             :                                                  StringRef &Res) const {
    1693           1 :   StringRef StringTable = getStringTableData();
    1694             :   MachO::nlist_base Entry = getSymbolTableEntryBase(*this, Symb);
    1695           1 :   if ((Entry.n_type & MachO::N_TYPE) != MachO::N_INDR)
    1696           0 :     return object_error::parse_failed;
    1697           1 :   uint64_t NValue = getNValue(Symb);
    1698           1 :   if (NValue >= StringTable.size())
    1699           0 :     return object_error::parse_failed;
    1700           1 :   const char *Start = &StringTable.data()[NValue];
    1701           1 :   Res = StringRef(Start);
    1702           1 :   return std::error_code();
    1703             : }
    1704             : 
    1705        2401 : uint64_t MachOObjectFile::getSymbolValueImpl(DataRefImpl Sym) const {
    1706        2401 :   return getNValue(Sym);
    1707             : }
    1708             : 
    1709         845 : Expected<uint64_t> MachOObjectFile::getSymbolAddress(DataRefImpl Sym) const {
    1710        1690 :   return getSymbolValue(Sym);
    1711             : }
    1712             : 
    1713          16 : uint32_t MachOObjectFile::getSymbolAlignment(DataRefImpl DRI) const {
    1714          16 :   uint32_t flags = getSymbolFlags(DRI);
    1715          16 :   if (flags & SymbolRef::SF_Common) {
    1716             :     MachO::nlist_base Entry = getSymbolTableEntryBase(*this, DRI);
    1717          32 :     return 1 << MachO::GET_COMM_ALIGN(Entry.n_desc);
    1718             :   }
    1719             :   return 0;
    1720             : }
    1721             : 
    1722          78 : uint64_t MachOObjectFile::getCommonSymbolSizeImpl(DataRefImpl DRI) const {
    1723          78 :   return getNValue(DRI);
    1724             : }
    1725             : 
    1726             : Expected<SymbolRef::Type>
    1727        4227 : MachOObjectFile::getSymbolType(DataRefImpl Symb) const {
    1728             :   MachO::nlist_base Entry = getSymbolTableEntryBase(*this, Symb);
    1729             :   uint8_t n_type = Entry.n_type;
    1730             : 
    1731             :   // If this is a STAB debugging symbol, we can do nothing more.
    1732        4227 :   if (n_type & MachO::N_STAB)
    1733             :     return SymbolRef::ST_Debug;
    1734             : 
    1735        2572 :   switch (n_type & MachO::N_TYPE) {
    1736         875 :     case MachO::N_UNDF :
    1737             :       return SymbolRef::ST_Unknown;
    1738        1693 :     case MachO::N_SECT :
    1739        1693 :       Expected<section_iterator> SecOrError = getSymbolSection(Symb);
    1740        1693 :       if (!SecOrError)
    1741             :         return SecOrError.takeError();
    1742        1692 :       section_iterator Sec = *SecOrError;
    1743        3057 :       if (Sec->isData() || Sec->isBSS())
    1744             :         return SymbolRef::ST_Data;
    1745             :       return SymbolRef::ST_Function;
    1746             :   }
    1747             :   return SymbolRef::ST_Other;
    1748             : }
    1749             : 
    1750        6173 : uint32_t MachOObjectFile::getSymbolFlags(DataRefImpl DRI) const {
    1751             :   MachO::nlist_base Entry = getSymbolTableEntryBase(*this, DRI);
    1752             : 
    1753             :   uint8_t MachOType = Entry.n_type;
    1754             :   uint16_t MachOFlags = Entry.n_desc;
    1755             : 
    1756             :   uint32_t Result = SymbolRef::SF_None;
    1757             : 
    1758        6173 :   if ((MachOType & MachO::N_TYPE) == MachO::N_INDR)
    1759             :     Result |= SymbolRef::SF_Indirect;
    1760             : 
    1761        6173 :   if (MachOType & MachO::N_STAB)
    1762         464 :     Result |= SymbolRef::SF_FormatSpecific;
    1763             : 
    1764        6173 :   if (MachOType & MachO::N_EXT) {
    1765        3879 :     Result |= SymbolRef::SF_Global;
    1766        3879 :     if ((MachOType & MachO::N_TYPE) == MachO::N_UNDF) {
    1767        1221 :       if (getNValue(DRI))
    1768         219 :         Result |= SymbolRef::SF_Common;
    1769             :       else
    1770        1002 :         Result |= SymbolRef::SF_Undefined;
    1771             :     }
    1772             : 
    1773        3879 :     if (!(MachOType & MachO::N_PEXT))
    1774        3819 :       Result |= SymbolRef::SF_Exported;
    1775             :   }
    1776             : 
    1777        6173 :   if (MachOFlags & (MachO::N_WEAK_REF | MachO::N_WEAK_DEF))
    1778          62 :     Result |= SymbolRef::SF_Weak;
    1779             : 
    1780        6173 :   if (MachOFlags & (MachO::N_ARM_THUMB_DEF))
    1781         221 :     Result |= SymbolRef::SF_Thumb;
    1782             : 
    1783        6173 :   if ((MachOType & MachO::N_TYPE) == MachO::N_ABS)
    1784          13 :     Result |= SymbolRef::SF_Absolute;
    1785             : 
    1786        6173 :   return Result;
    1787             : }
    1788             : 
    1789             : Expected<section_iterator>
    1790        3315 : MachOObjectFile::getSymbolSection(DataRefImpl Symb) const {
    1791             :   MachO::nlist_base Entry = getSymbolTableEntryBase(*this, Symb);
    1792             :   uint8_t index = Entry.n_sect;
    1793             : 
    1794        3315 :   if (index == 0)
    1795         458 :     return section_end();
    1796             :   DataRefImpl DRI;
    1797        3086 :   DRI.d.a = index - 1;
    1798        6172 :   if (DRI.d.a >= Sections.size()){
    1799          15 :     return malformedError("bad section index: " + Twine((int)index) +
    1800          15 :                           " for symbol at index " + Twine(getSymbolIndex(Symb)));
    1801             :   }
    1802        6162 :   return section_iterator(SectionRef(DRI, this));
    1803             : }
    1804             : 
    1805         108 : unsigned MachOObjectFile::getSymbolSectionID(SymbolRef Sym) const {
    1806             :   MachO::nlist_base Entry =
    1807             :       getSymbolTableEntryBase(*this, Sym.getRawDataRefImpl());
    1808         108 :   return Entry.n_sect - 1;
    1809             : }
    1810             : 
    1811       12577 : void MachOObjectFile::moveSectionNext(DataRefImpl &Sec) const {
    1812       12577 :   Sec.d.a++;
    1813       12577 : }
    1814             : 
    1815       27218 : std::error_code MachOObjectFile::getSectionName(DataRefImpl Sec,
    1816             :                                                 StringRef &Result) const {
    1817       27218 :   ArrayRef<char> Raw = getSectionRawName(Sec);
    1818       54436 :   Result = parseSegmentOrSectionName(Raw.data());
    1819       27218 :   return std::error_code();
    1820             : }
    1821             : 
    1822        7203 : uint64_t MachOObjectFile::getSectionAddress(DataRefImpl Sec) const {
    1823        7203 :   if (is64Bit())
    1824        2289 :     return getSection64(Sec).addr;
    1825        4914 :   return getSection(Sec).addr;
    1826             : }
    1827             : 
    1828          13 : uint64_t MachOObjectFile::getSectionIndex(DataRefImpl Sec) const {
    1829          13 :   return Sec.d.a;
    1830             : }
    1831             : 
    1832        7229 : uint64_t MachOObjectFile::getSectionSize(DataRefImpl Sec) const {
    1833             :   // In the case if a malformed Mach-O file where the section offset is past
    1834             :   // the end of the file or some part of the section size is past the end of
    1835             :   // the file return a size of zero or a size that covers the rest of the file
    1836             :   // but does not extend past the end of the file.
    1837             :   uint32_t SectOffset, SectType;
    1838             :   uint64_t SectSize;
    1839             : 
    1840        7229 :   if (is64Bit()) {
    1841        2446 :     MachO::section_64 Sect = getSection64(Sec);
    1842        2446 :     SectOffset = Sect.offset;
    1843        2446 :     SectSize = Sect.size;
    1844        2446 :     SectType = Sect.flags & MachO::SECTION_TYPE;
    1845             :   } else {
    1846        4783 :     MachO::section Sect = getSection(Sec);
    1847        4783 :     SectOffset = Sect.offset;
    1848        4783 :     SectSize = Sect.size;
    1849        4783 :     SectType = Sect.flags & MachO::SECTION_TYPE;
    1850             :   }
    1851        7229 :   if (SectType == MachO::S_ZEROFILL || SectType == MachO::S_GB_ZEROFILL)
    1852             :     return SectSize;
    1853       14450 :   uint64_t FileSize = getData().size();
    1854        7225 :   if (SectOffset > FileSize)
    1855             :     return 0;
    1856        7225 :   if (FileSize - SectOffset < SectSize)
    1857           0 :     return FileSize - SectOffset;
    1858             :   return SectSize;
    1859             : }
    1860             : 
    1861        7340 : std::error_code MachOObjectFile::getSectionContents(DataRefImpl Sec,
    1862             :                                                     StringRef &Res) const {
    1863             :   uint32_t Offset;
    1864             :   uint64_t Size;
    1865             : 
    1866        7340 :   if (is64Bit()) {
    1867        6054 :     MachO::section_64 Sect = getSection64(Sec);
    1868        6054 :     Offset = Sect.offset;
    1869        6054 :     Size = Sect.size;
    1870             :   } else {
    1871        1286 :     MachO::section Sect = getSection(Sec);
    1872        1286 :     Offset = Sect.offset;
    1873        1286 :     Size = Sect.size;
    1874             :   }
    1875             : 
    1876       14680 :   Res = this->getData().substr(Offset, Size);
    1877        7340 :   return std::error_code();
    1878             : }
    1879             : 
    1880          75 : uint64_t MachOObjectFile::getSectionAlignment(DataRefImpl Sec) const {
    1881             :   uint32_t Align;
    1882          75 :   if (is64Bit()) {
    1883          58 :     MachO::section_64 Sect = getSection64(Sec);
    1884          58 :     Align = Sect.align;
    1885             :   } else {
    1886          17 :     MachO::section Sect = getSection(Sec);
    1887          17 :     Align = Sect.align;
    1888             :   }
    1889             : 
    1890          75 :   return uint64_t(1) << Align;
    1891             : }
    1892             : 
    1893        6109 : bool MachOObjectFile::isSectionCompressed(DataRefImpl Sec) const {
    1894        6109 :   return false;
    1895             : }
    1896             : 
    1897         535 : bool MachOObjectFile::isSectionText(DataRefImpl Sec) const {
    1898         535 :   uint32_t Flags = getSectionFlags(*this, Sec);
    1899         535 :   return Flags & MachO::S_ATTR_PURE_INSTRUCTIONS;
    1900             : }
    1901             : 
    1902        1743 : bool MachOObjectFile::isSectionData(DataRefImpl Sec) const {
    1903        1743 :   uint32_t Flags = getSectionFlags(*this, Sec);
    1904        1743 :   unsigned SectionType = Flags & MachO::SECTION_TYPE;
    1905        2344 :   return !(Flags & MachO::S_ATTR_PURE_INSTRUCTIONS) &&
    1906        1202 :          !(SectionType == MachO::S_ZEROFILL ||
    1907        2344 :            SectionType == MachO::S_GB_ZEROFILL);
    1908             : }
    1909             : 
    1910        8229 : bool MachOObjectFile::isSectionBSS(DataRefImpl Sec) const {
    1911        8229 :   uint32_t Flags = getSectionFlags(*this, Sec);
    1912        8229 :   unsigned SectionType = Flags & MachO::SECTION_TYPE;
    1913       14725 :   return !(Flags & MachO::S_ATTR_PURE_INSTRUCTIONS) &&
    1914       12992 :          (SectionType == MachO::S_ZEROFILL ||
    1915       14725 :           SectionType == MachO::S_GB_ZEROFILL);
    1916             : }
    1917             : 
    1918         159 : unsigned MachOObjectFile::getSectionID(SectionRef Sec) const {
    1919         159 :   return Sec.getRawDataRefImpl().d.a;
    1920             : }
    1921             : 
    1922        6504 : bool MachOObjectFile::isSectionVirtual(DataRefImpl Sec) const {
    1923             :   // FIXME: Unimplemented.
    1924        6504 :   return false;
    1925             : }
    1926             : 
    1927         193 : bool MachOObjectFile::isSectionBitcode(DataRefImpl Sec) const {
    1928         193 :   StringRef SegmentName = getSectionFinalSegmentName(Sec);
    1929         193 :   StringRef SectName;
    1930         193 :   if (!getSectionName(Sec, SectName))
    1931             :     return (SegmentName == "__LLVM" && SectName == "__bitcode");
    1932             :   return false;
    1933             : }
    1934             : 
    1935        6414 : bool MachOObjectFile::isSectionStripped(DataRefImpl Sec) const {
    1936        6414 :   if (is64Bit())
    1937        5822 :     return getSection64(Sec).offset == 0;
    1938         592 :   return getSection(Sec).offset == 0;
    1939             : }
    1940             : 
    1941        1046 : relocation_iterator MachOObjectFile::section_rel_begin(DataRefImpl Sec) const {
    1942             :   DataRefImpl Ret;
    1943        1046 :   Ret.d.a = Sec.d.a;
    1944             :   Ret.d.b = 0;
    1945        2092 :   return relocation_iterator(RelocationRef(Ret, this));
    1946             : }
    1947             : 
    1948             : relocation_iterator
    1949        1046 : MachOObjectFile::section_rel_end(DataRefImpl Sec) const {
    1950             :   uint32_t Num;
    1951        1046 :   if (is64Bit()) {
    1952         704 :     MachO::section_64 Sect = getSection64(Sec);
    1953         704 :     Num = Sect.nreloc;
    1954             :   } else {
    1955         342 :     MachO::section Sect = getSection(Sec);
    1956         342 :     Num = Sect.nreloc;
    1957             :   }
    1958             : 
    1959             :   DataRefImpl Ret;
    1960        1046 :   Ret.d.a = Sec.d.a;
    1961        1046 :   Ret.d.b = Num;
    1962        2092 :   return relocation_iterator(RelocationRef(Ret, this));
    1963             : }
    1964             : 
    1965         747 : relocation_iterator MachOObjectFile::extrel_begin() const {
    1966             :   DataRefImpl Ret;
    1967             :   // for DYSYMTAB symbols, Ret.d.a == 0 for external relocations
    1968             :   Ret.d.a = 0; // Would normally be a section index.
    1969             :   Ret.d.b = 0; // Index into the external relocations
    1970        1494 :   return relocation_iterator(RelocationRef(Ret, this));
    1971             : }
    1972             : 
    1973         747 : relocation_iterator MachOObjectFile::extrel_end() const {
    1974         747 :   MachO::dysymtab_command DysymtabLoadCmd = getDysymtabLoadCommand();
    1975             :   DataRefImpl Ret;
    1976             :   // for DYSYMTAB symbols, Ret.d.a == 0 for external relocations
    1977             :   Ret.d.a = 0; // Would normally be a section index.
    1978         747 :   Ret.d.b = DysymtabLoadCmd.nextrel; // Index into the external relocations
    1979        1494 :   return relocation_iterator(RelocationRef(Ret, this));
    1980             : }
    1981             : 
    1982           1 : relocation_iterator MachOObjectFile::locrel_begin() const {
    1983             :   DataRefImpl Ret;
    1984             :   // for DYSYMTAB symbols, Ret.d.a == 1 for local relocations
    1985           1 :   Ret.d.a = 1; // Would normally be a section index.
    1986             :   Ret.d.b = 0; // Index into the local relocations
    1987           2 :   return relocation_iterator(RelocationRef(Ret, this));
    1988             : }
    1989             : 
    1990           1 : relocation_iterator MachOObjectFile::locrel_end() const {
    1991           1 :   MachO::dysymtab_command DysymtabLoadCmd = getDysymtabLoadCommand();
    1992             :   DataRefImpl Ret;
    1993             :   // for DYSYMTAB symbols, Ret.d.a == 1 for local relocations
    1994           1 :   Ret.d.a = 1; // Would normally be a section index.
    1995           1 :   Ret.d.b = DysymtabLoadCmd.nlocrel; // Index into the local relocations
    1996           2 :   return relocation_iterator(RelocationRef(Ret, this));
    1997             : }
    1998             : 
    1999        3755 : void MachOObjectFile::moveRelocationNext(DataRefImpl &Rel) const {
    2000        3755 :   ++Rel.d.b;
    2001        3755 : }
    2002             : 
    2003        3786 : uint64_t MachOObjectFile::getRelocationOffset(DataRefImpl Rel) const {
    2004             :   assert((getHeader().filetype == MachO::MH_OBJECT ||
    2005             :           getHeader().filetype == MachO::MH_KEXT_BUNDLE) &&
    2006             :          "Only implemented for MH_OBJECT && MH_KEXT_BUNDLE");
    2007        3786 :   MachO::any_relocation_info RE = getRelocation(Rel);
    2008        3786 :   return getAnyRelocationAddress(RE);
    2009             : }
    2010             : 
    2011             : symbol_iterator
    2012        1212 : MachOObjectFile::getRelocationSymbol(DataRefImpl Rel) const {
    2013        1212 :   MachO::any_relocation_info RE = getRelocation(Rel);
    2014        1212 :   if (isRelocationScattered(RE))
    2015           1 :     return symbol_end();
    2016             : 
    2017        1211 :   uint32_t SymbolIdx = getPlainRelocationSymbolNum(RE);
    2018        1211 :   bool isExtern = getPlainRelocationExternal(RE);
    2019        1211 :   if (!isExtern)
    2020         848 :     return symbol_end();
    2021             : 
    2022         363 :   MachO::symtab_command S = getSymtabLoadCommand();
    2023         363 :   unsigned SymbolTableEntrySize = is64Bit() ?
    2024             :     sizeof(MachO::nlist_64) :
    2025         363 :     sizeof(MachO::nlist);
    2026         363 :   uint64_t Offset = S.symoff + SymbolIdx * SymbolTableEntrySize;
    2027             :   DataRefImpl Sym;
    2028         363 :   Sym.p = reinterpret_cast<uintptr_t>(getPtr(*this, Offset));
    2029         726 :   return symbol_iterator(SymbolRef(Sym, this));
    2030             : }
    2031             : 
    2032             : section_iterator
    2033         107 : MachOObjectFile::getRelocationSection(DataRefImpl Rel) const {
    2034         214 :   return section_iterator(getAnyRelocationSection(getRelocation(Rel)));
    2035             : }
    2036             : 
    2037         515 : uint64_t MachOObjectFile::getRelocationType(DataRefImpl Rel) const {
    2038         515 :   MachO::any_relocation_info RE = getRelocation(Rel);
    2039         515 :   return getAnyRelocationType(RE);
    2040             : }
    2041             : 
    2042         454 : void MachOObjectFile::getRelocationTypeName(
    2043             :     DataRefImpl Rel, SmallVectorImpl<char> &Result) const {
    2044             :   StringRef res;
    2045         454 :   uint64_t RType = getRelocationType(Rel);
    2046             : 
    2047         454 :   unsigned Arch = this->getArch();
    2048             : 
    2049         454 :   switch (Arch) {
    2050          80 :     case Triple::x86: {
    2051             :       static const char *const Table[] =  {
    2052             :         "GENERIC_RELOC_VANILLA",
    2053             :         "GENERIC_RELOC_PAIR",
    2054             :         "GENERIC_RELOC_SECTDIFF",
    2055             :         "GENERIC_RELOC_PB_LA_PTR",
    2056             :         "GENERIC_RELOC_LOCAL_SECTDIFF",
    2057             :         "GENERIC_RELOC_TLV" };
    2058             : 
    2059          80 :       if (RType > 5)
    2060             :         res = "Unknown";
    2061             :       else
    2062          80 :         res = Table[RType];
    2063             :       break;
    2064             :     }
    2065         174 :     case Triple::x86_64: {
    2066             :       static const char *const Table[] =  {
    2067             :         "X86_64_RELOC_UNSIGNED",
    2068             :         "X86_64_RELOC_SIGNED",
    2069             :         "X86_64_RELOC_BRANCH",
    2070             :         "X86_64_RELOC_GOT_LOAD",
    2071             :         "X86_64_RELOC_GOT",
    2072             :         "X86_64_RELOC_SUBTRACTOR",
    2073             :         "X86_64_RELOC_SIGNED_1",
    2074             :         "X86_64_RELOC_SIGNED_2",
    2075             :         "X86_64_RELOC_SIGNED_4",
    2076             :         "X86_64_RELOC_TLV" };
    2077             : 
    2078         174 :       if (RType > 9)
    2079             :         res = "Unknown";
    2080             :       else
    2081         174 :         res = Table[RType];
    2082             :       break;
    2083             :     }
    2084          94 :     case Triple::arm: {
    2085             :       static const char *const Table[] =  {
    2086             :         "ARM_RELOC_VANILLA",
    2087             :         "ARM_RELOC_PAIR",
    2088             :         "ARM_RELOC_SECTDIFF",
    2089             :         "ARM_RELOC_LOCAL_SECTDIFF",
    2090             :         "ARM_RELOC_PB_LA_PTR",
    2091             :         "ARM_RELOC_BR24",
    2092             :         "ARM_THUMB_RELOC_BR22",
    2093             :         "ARM_THUMB_32BIT_BRANCH",
    2094             :         "ARM_RELOC_HALF",
    2095             :         "ARM_RELOC_HALF_SECTDIFF" };
    2096             : 
    2097          94 :       if (RType > 9)
    2098             :         res = "Unknown";
    2099             :       else
    2100          94 :         res = Table[RType];
    2101             :       break;
    2102             :     }
    2103          56 :     case Triple::aarch64: {
    2104             :       static const char *const Table[] = {
    2105             :         "ARM64_RELOC_UNSIGNED",           "ARM64_RELOC_SUBTRACTOR",
    2106             :         "ARM64_RELOC_BRANCH26",           "ARM64_RELOC_PAGE21",
    2107             :         "ARM64_RELOC_PAGEOFF12",          "ARM64_RELOC_GOT_LOAD_PAGE21",
    2108             :         "ARM64_RELOC_GOT_LOAD_PAGEOFF12", "ARM64_RELOC_POINTER_TO_GOT",
    2109             :         "ARM64_RELOC_TLVP_LOAD_PAGE21",   "ARM64_RELOC_TLVP_LOAD_PAGEOFF12",
    2110             :         "ARM64_RELOC_ADDEND"
    2111             :       };
    2112             : 
    2113          56 :       if (RType >= array_lengthof(Table))
    2114             :         res = "Unknown";
    2115             :       else
    2116          56 :         res = Table[RType];
    2117             :       break;
    2118             :     }
    2119          30 :     case Triple::ppc: {
    2120             :       static const char *const Table[] =  {
    2121             :         "PPC_RELOC_VANILLA",
    2122             :         "PPC_RELOC_PAIR",
    2123             :         "PPC_RELOC_BR14",
    2124             :         "PPC_RELOC_BR24",
    2125             :         "PPC_RELOC_HI16",
    2126             :         "PPC_RELOC_LO16",
    2127             :         "PPC_RELOC_HA16",
    2128             :         "PPC_RELOC_LO14",
    2129             :         "PPC_RELOC_SECTDIFF",
    2130             :         "PPC_RELOC_PB_LA_PTR",
    2131             :         "PPC_RELOC_HI16_SECTDIFF",
    2132             :         "PPC_RELOC_LO16_SECTDIFF",
    2133             :         "PPC_RELOC_HA16_SECTDIFF",
    2134             :         "PPC_RELOC_JBSR",
    2135             :         "PPC_RELOC_LO14_SECTDIFF",
    2136             :         "PPC_RELOC_LOCAL_SECTDIFF" };
    2137             : 
    2138          30 :       if (RType > 15)
    2139             :         res = "Unknown";
    2140             :       else
    2141          30 :         res = Table[RType];
    2142             :       break;
    2143             :     }
    2144             :     case Triple::UnknownArch:
    2145             :       res = "Unknown";
    2146           0 :       break;
    2147             :   }
    2148         454 :   Result.append(res.begin(), res.end());
    2149         454 : }
    2150             : 
    2151           0 : uint8_t MachOObjectFile::getRelocationLength(DataRefImpl Rel) const {
    2152           0 :   MachO::any_relocation_info RE = getRelocation(Rel);
    2153           0 :   return getAnyRelocationLength(RE);
    2154             : }
    2155             : 
    2156             : //
    2157             : // guessLibraryShortName() is passed a name of a dynamic library and returns a
    2158             : // guess on what the short name is.  Then name is returned as a substring of the
    2159             : // StringRef Name passed in.  The name of the dynamic library is recognized as
    2160             : // a framework if it has one of the two following forms:
    2161             : //      Foo.framework/Versions/A/Foo
    2162             : //      Foo.framework/Foo
    2163             : // Where A and Foo can be any string.  And may contain a trailing suffix
    2164             : // starting with an underbar.  If the Name is recognized as a framework then
    2165             : // isFramework is set to true else it is set to false.  If the Name has a
    2166             : // suffix then Suffix is set to the substring in Name that contains the suffix
    2167             : // else it is set to a NULL StringRef.
    2168             : //
    2169             : // The Name of the dynamic library is recognized as a library name if it has
    2170             : // one of the two following forms:
    2171             : //      libFoo.A.dylib
    2172             : //      libFoo.dylib
    2173             : // The library may have a suffix trailing the name Foo of the form:
    2174             : //      libFoo_profile.A.dylib
    2175             : //      libFoo_profile.dylib
    2176             : //
    2177             : // The Name of the dynamic library is also recognized as a library name if it
    2178             : // has the following form:
    2179             : //      Foo.qtx
    2180             : //
    2181             : // If the Name of the dynamic library is none of the forms above then a NULL
    2182             : // StringRef is returned.
    2183             : //
    2184          88 : StringRef MachOObjectFile::guessLibraryShortName(StringRef Name,
    2185             :                                                  bool &isFramework,
    2186             :                                                  StringRef &Suffix) {
    2187          88 :   StringRef Foo, F, DotFramework, V, Dylib, Lib, Dot, Qtx;
    2188             :   size_t a, b, c, d, Idx;
    2189             : 
    2190          88 :   isFramework = false;
    2191          88 :   Suffix = StringRef();
    2192             : 
    2193             :   // Pull off the last component and make Foo point to it
    2194             :   a = Name.rfind('/');
    2195          88 :   if (a == Name.npos || a == 0)
    2196             :     goto guess_library;
    2197         146 :   Foo = Name.slice(a+1, Name.npos);
    2198             : 
    2199             :   // Look for a suffix starting with a '_'
    2200             :   Idx = Foo.rfind('_');
    2201          73 :   if (Idx != Foo.npos && Foo.size() >= 2) {
    2202           4 :     Suffix = Foo.slice(Idx, Foo.npos);
    2203           4 :     Foo = Foo.slice(0, Idx);
    2204             :   }
    2205             : 
    2206             :   // First look for the form Foo.framework/Foo
    2207             :   b = Name.rfind('/', a);
    2208          73 :   if (b == Name.npos)
    2209             :     Idx = 0;
    2210             :   else
    2211          71 :     Idx = b+1;
    2212          73 :   F = Name.slice(Idx, Idx + Foo.size());
    2213             :   DotFramework = Name.slice(Idx + Foo.size(),
    2214          73 :                             Idx + Foo.size() + sizeof(".framework/")-1);
    2215             :   if (F == Foo && DotFramework == ".framework/") {
    2216           4 :     isFramework = true;
    2217           4 :     return Foo;
    2218             :   }
    2219             : 
    2220             :   // Next look for the form Foo.framework/Versions/A/Foo
    2221          69 :   if (b == Name.npos)
    2222             :     goto guess_library;
    2223             :   c =  Name.rfind('/', b);
    2224          69 :   if (c == Name.npos || c == 0)
    2225             :     goto guess_library;
    2226           3 :   V = Name.slice(c+1, Name.npos);
    2227             :   if (!V.startswith("Versions/"))
    2228             :     goto guess_library;
    2229             :   d =  Name.rfind('/', c);
    2230           2 :   if (d == Name.npos)
    2231             :     Idx = 0;
    2232             :   else
    2233           1 :     Idx = d+1;
    2234           2 :   F = Name.slice(Idx, Idx + Foo.size());
    2235             :   DotFramework = Name.slice(Idx + Foo.size(),
    2236           2 :                             Idx + Foo.size() + sizeof(".framework/")-1);
    2237             :   if (F == Foo && DotFramework == ".framework/") {
    2238           2 :     isFramework = true;
    2239           2 :     return Foo;
    2240             :   }
    2241             : 
    2242          81 : guess_library:
    2243             :   // pull off the suffix after the "." and make a point to it
    2244             :   a = Name.rfind('.');
    2245          82 :   if (a == Name.npos || a == 0)
    2246           0 :     return StringRef();
    2247             :   Dylib = Name.slice(a, Name.npos);
    2248             :   if (Dylib != ".dylib")
    2249             :     goto guess_qtx;
    2250             : 
    2251             :   // First pull off the version letter for the form Foo.A.dylib if any.
    2252          80 :   if (a >= 3) {
    2253          80 :     Dot = Name.slice(a-2, a-1);
    2254             :     if (Dot == ".")
    2255             :       a = a - 2;
    2256             :   }
    2257             : 
    2258             :   b = Name.rfind('/', a);
    2259          80 :   if (b == Name.npos)
    2260             :     b = 0;
    2261             :   else
    2262          66 :     b = b+1;
    2263             :   // ignore any suffix after an underbar like Foo_profile.A.dylib
    2264             :   Idx = Name.find('_', b);
    2265          80 :   if (Idx != Name.npos && Idx != b) {
    2266           4 :     Lib = Name.slice(b, Idx);
    2267           4 :     Suffix = Name.slice(Idx, a);
    2268             :   }
    2269             :   else
    2270          76 :     Lib = Name.slice(b, a);
    2271             :   // There are incorrect library names of the form:
    2272             :   // libATS.A_profile.dylib so check for these.
    2273          80 :   if (Lib.size() >= 3) {
    2274          80 :     Dot = Lib.slice(Lib.size()-2, Lib.size()-1);
    2275             :     if (Dot == ".")
    2276           2 :       Lib = Lib.slice(0, Lib.size()-2);
    2277             :   }
    2278          80 :   return Lib;
    2279             : 
    2280           2 : guess_qtx:
    2281             :   Qtx = Name.slice(a, Name.npos);
    2282             :   if (Qtx != ".qtx")
    2283           0 :     return StringRef();
    2284             :   b = Name.rfind('/', a);
    2285           2 :   if (b == Name.npos)
    2286           1 :     Lib = Name.slice(0, a);
    2287             :   else
    2288           2 :     Lib = Name.slice(b+1, a);
    2289             :   // There are library names of the form: QT.A.qtx so check for these.
    2290           2 :   if (Lib.size() >= 3) {
    2291           2 :     Dot = Lib.slice(Lib.size()-2, Lib.size()-1);
    2292             :     if (Dot == ".")
    2293           1 :       Lib = Lib.slice(0, Lib.size()-2);
    2294             :   }
    2295           2 :   return Lib;
    2296             : }
    2297             : 
    2298             : // getLibraryShortNameByIndex() is used to get the short name of the library
    2299             : // for an undefined symbol in a linked Mach-O binary that was linked with the
    2300             : // normal two-level namespace default (that is MH_TWOLEVEL in the header).
    2301             : // It is passed the index (0 - based) of the library as translated from
    2302             : // GET_LIBRARY_ORDINAL (1 - based).
    2303          95 : std::error_code MachOObjectFile::getLibraryShortNameByIndex(unsigned Index,
    2304             :                                                          StringRef &Res) const {
    2305         190 :   if (Index >= Libraries.size())
    2306           0 :     return object_error::parse_failed;
    2307             : 
    2308             :   // If the cache of LibrariesShortNames is not built up do that first for
    2309             :   // all the Libraries.
    2310          95 :   if (LibrariesShortNames.size() == 0) {
    2311         358 :     for (unsigned i = 0; i < Libraries.size(); i++) {
    2312             :       MachO::dylib_command D =
    2313          88 :         getStruct<MachO::dylib_command>(*this, Libraries[i]);
    2314          88 :       if (D.dylib.name >= D.cmdsize)
    2315           0 :         return object_error::parse_failed;
    2316          88 :       const char *P = (const char *)(Libraries[i]) + D.dylib.name;
    2317             :       StringRef Name = StringRef(P);
    2318          88 :       if (D.dylib.name+Name.size() >= D.cmdsize)
    2319           0 :         return object_error::parse_failed;
    2320          88 :       StringRef Suffix;
    2321             :       bool isFramework;
    2322          88 :       StringRef shortName = guessLibraryShortName(Name, isFramework, Suffix);
    2323          88 :       if (shortName.empty())
    2324           0 :         LibrariesShortNames.push_back(Name);
    2325             :       else
    2326          88 :         LibrariesShortNames.push_back(shortName);
    2327             :     }
    2328             :   }
    2329             : 
    2330          95 :   Res = LibrariesShortNames[Index];
    2331          95 :   return std::error_code();
    2332             : }
    2333             : 
    2334          62 : uint32_t MachOObjectFile::getLibraryCount() const {
    2335          62 :   return Libraries.size();
    2336             : }
    2337             : 
    2338             : section_iterator
    2339           7 : MachOObjectFile::getRelocationRelocatedSection(relocation_iterator Rel) const {
    2340             :   DataRefImpl Sec;
    2341           7 :   Sec.d.a = Rel->getRawDataRefImpl().d.a;
    2342          14 :   return section_iterator(SectionRef(Sec, this));
    2343             : }
    2344             : 
    2345         986 : basic_symbol_iterator MachOObjectFile::symbol_begin() const {
    2346             :   DataRefImpl DRI;
    2347         986 :   MachO::symtab_command Symtab = getSymtabLoadCommand();
    2348         986 :   if (!SymtabLoadCmd || Symtab.nsyms == 0)
    2349         118 :     return basic_symbol_iterator(SymbolRef(DRI, this));
    2350             : 
    2351         927 :   return getSymbolByIndex(0);
    2352             : }
    2353             : 
    2354        3605 : basic_symbol_iterator MachOObjectFile::symbol_end() const {
    2355             :   DataRefImpl DRI;
    2356        3605 :   MachO::symtab_command Symtab = getSymtabLoadCommand();
    2357        3605 :   if (!SymtabLoadCmd || Symtab.nsyms == 0)
    2358         118 :     return basic_symbol_iterator(SymbolRef(DRI, this));
    2359             : 
    2360        3546 :   unsigned SymbolTableEntrySize = is64Bit() ?
    2361             :     sizeof(MachO::nlist_64) :
    2362        3546 :     sizeof(MachO::nlist);
    2363        7092 :   unsigned Offset = Symtab.symoff +
    2364        3546 :     Symtab.nsyms * SymbolTableEntrySize;
    2365        3546 :   DRI.p = reinterpret_cast<uintptr_t>(getPtr(*this, Offset));
    2366        7092 :   return basic_symbol_iterator(SymbolRef(DRI, this));
    2367             : }
    2368             : 
    2369        1012 : basic_symbol_iterator MachOObjectFile::getSymbolByIndex(unsigned Index) const {
    2370        1012 :   MachO::symtab_command Symtab = getSymtabLoadCommand();
    2371        1012 :   if (!SymtabLoadCmd || Index >= Symtab.nsyms)
    2372           0 :     report_fatal_error("Requested symbol index is out of range.");
    2373             :   unsigned SymbolTableEntrySize =
    2374        1012 :     is64Bit() ? sizeof(MachO::nlist_64) : sizeof(MachO::nlist);
    2375             :   DataRefImpl DRI;
    2376        1012 :   DRI.p = reinterpret_cast<uintptr_t>(getPtr(*this, Symtab.symoff));
    2377        1012 :   DRI.p += Index * SymbolTableEntrySize;
    2378        2024 :   return basic_symbol_iterator(SymbolRef(DRI, this));
    2379             : }
    2380             : 
    2381          12 : uint64_t MachOObjectFile::getSymbolIndex(DataRefImpl Symb) const {
    2382          12 :   MachO::symtab_command Symtab = getSymtabLoadCommand();
    2383          12 :   if (!SymtabLoadCmd)
    2384           0 :     report_fatal_error("getSymbolIndex() called with no symbol table symbol");
    2385             :   unsigned SymbolTableEntrySize =
    2386          12 :     is64Bit() ? sizeof(MachO::nlist_64) : sizeof(MachO::nlist);
    2387             :   DataRefImpl DRIstart;
    2388          12 :   DRIstart.p = reinterpret_cast<uintptr_t>(getPtr(*this, Symtab.symoff));
    2389          12 :   uint64_t Index = (Symb.p - DRIstart.p) / SymbolTableEntrySize;
    2390          12 :   return Index;
    2391             : }
    2392             : 
    2393        1549 : section_iterator MachOObjectFile::section_begin() const {
    2394             :   DataRefImpl DRI;
    2395        3098 :   return section_iterator(SectionRef(DRI, this));
    2396             : }
    2397             : 
    2398        9735 : section_iterator MachOObjectFile::section_end() const {
    2399             :   DataRefImpl DRI;
    2400        9735 :   DRI.d.a = Sections.size();
    2401       19470 :   return section_iterator(SectionRef(DRI, this));
    2402             : }
    2403             : 
    2404         793 : uint8_t MachOObjectFile::getBytesInAddress() const {
    2405         793 :   return is64Bit() ? 8 : 4;
    2406             : }
    2407             : 
    2408         610 : StringRef MachOObjectFile::getFileFormatName() const {
    2409             :   unsigned CPUType = getCPUType(*this);
    2410         610 :   if (!is64Bit()) {
    2411         186 :     switch (CPUType) {
    2412             :     case MachO::CPU_TYPE_I386:
    2413          93 :       return "Mach-O 32-bit i386";
    2414             :     case MachO::CPU_TYPE_ARM:
    2415          87 :       return "Mach-O arm";
    2416             :     case MachO::CPU_TYPE_POWERPC:
    2417           6 :       return "Mach-O 32-bit ppc";
    2418             :     default:
    2419           0 :       return "Mach-O 32-bit unknown";
    2420             :     }
    2421             :   }
    2422             : 
    2423         424 :   switch (CPUType) {
    2424             :   case MachO::CPU_TYPE_X86_64:
    2425         383 :     return "Mach-O 64-bit x86-64";
    2426             :   case MachO::CPU_TYPE_ARM64:
    2427          37 :     return "Mach-O arm64";
    2428             :   case MachO::CPU_TYPE_POWERPC64:
    2429           4 :     return "Mach-O 64-bit ppc64";
    2430             :   default:
    2431           0 :     return "Mach-O 64-bit unknown";
    2432             :   }
    2433             : }
    2434             : 
    2435        5242 : Triple::ArchType MachOObjectFile::getArch(uint32_t CPUType) {
    2436        5242 :   switch (CPUType) {
    2437             :   case MachO::CPU_TYPE_I386:
    2438             :     return Triple::x86;
    2439        4243 :   case MachO::CPU_TYPE_X86_64:
    2440        4243 :     return Triple::x86_64;
    2441         370 :   case MachO::CPU_TYPE_ARM:
    2442         370 :     return Triple::arm;
    2443         276 :   case MachO::CPU_TYPE_ARM64:
    2444         276 :     return Triple::aarch64;
    2445          36 :   case MachO::CPU_TYPE_POWERPC:
    2446          36 :     return Triple::ppc;
    2447          24 :   case MachO::CPU_TYPE_POWERPC64:
    2448          24 :     return Triple::ppc64;
    2449           0 :   default:
    2450           0 :     return Triple::UnknownArch;
    2451             :   }
    2452             : }
    2453             : 
    2454         831 : Triple MachOObjectFile::getArchTriple(uint32_t CPUType, uint32_t CPUSubType,
    2455             :                                       const char **McpuDefault,
    2456             :                                       const char **ArchFlag) {
    2457         831 :   if (McpuDefault)
    2458         171 :     *McpuDefault = nullptr;
    2459         831 :   if (ArchFlag)
    2460         122 :     *ArchFlag = nullptr;
    2461             : 
    2462         831 :   switch (CPUType) {
    2463          79 :   case MachO::CPU_TYPE_I386:
    2464          79 :     switch (CPUSubType & ~MachO::CPU_SUBTYPE_MASK) {
    2465          79 :     case MachO::CPU_SUBTYPE_I386_ALL:
    2466          79 :       if (ArchFlag)
    2467          39 :         *ArchFlag = "i386";
    2468          79 :       return Triple("i386-apple-darwin");
    2469             :     default:
    2470             :       return Triple();
    2471             :     }
    2472         619 :   case MachO::CPU_TYPE_X86_64:
    2473         619 :     switch (CPUSubType & ~MachO::CPU_SUBTYPE_MASK) {
    2474         593 :     case MachO::CPU_SUBTYPE_X86_64_ALL:
    2475         593 :       if (ArchFlag)
    2476          58 :         *ArchFlag = "x86_64";
    2477         593 :       return Triple("x86_64-apple-darwin");
    2478          26 :     case MachO::CPU_SUBTYPE_X86_64_H:
    2479          26 :       if (ArchFlag)
    2480          10 :         *ArchFlag = "x86_64h";
    2481          26 :       return Triple("x86_64h-apple-darwin");
    2482             :     default:
    2483             :       return Triple();
    2484             :     }
    2485         100 :   case MachO::CPU_TYPE_ARM:
    2486         100 :     switch (CPUSubType & ~MachO::CPU_SUBTYPE_MASK) {
    2487           0 :     case MachO::CPU_SUBTYPE_ARM_V4T:
    2488           0 :       if (ArchFlag)
    2489           0 :         *ArchFlag = "armv4t";
    2490           0 :       return Triple("armv4t-apple-darwin");
    2491           0 :     case MachO::CPU_SUBTYPE_ARM_V5TEJ:
    2492           0 :       if (ArchFlag)
    2493           0 :         *ArchFlag = "armv5e";
    2494           0 :       return Triple("armv5e-apple-darwin");
    2495           0 :     case MachO::CPU_SUBTYPE_ARM_XSCALE:
    2496           0 :       if (ArchFlag)
    2497           0 :         *ArchFlag = "xscale";
    2498           0 :       return Triple("xscale-apple-darwin");
    2499           0 :     case MachO::CPU_SUBTYPE_ARM_V6:
    2500           0 :       if (ArchFlag)
    2501           0 :         *ArchFlag = "armv6";
    2502           0 :       return Triple("armv6-apple-darwin");
    2503           0 :     case MachO::CPU_SUBTYPE_ARM_V6M:
    2504           0 :       if (McpuDefault)
    2505           0 :         *McpuDefault = "cortex-m0";
    2506           0 :       if (ArchFlag)
    2507           0 :         *ArchFlag = "armv6m";
    2508           0 :       return Triple("armv6m-apple-darwin");
    2509          49 :     case MachO::CPU_SUBTYPE_ARM_V7:
    2510          49 :       if (ArchFlag)
    2511           5 :         *ArchFlag = "armv7";
    2512          49 :       return Triple("armv7-apple-darwin");
    2513           3 :     case MachO::CPU_SUBTYPE_ARM_V7EM:
    2514           3 :       if (McpuDefault)
    2515           3 :         *McpuDefault = "cortex-m4";
    2516           3 :       if (ArchFlag)
    2517           2 :         *ArchFlag = "armv7em";
    2518           3 :       return Triple("thumbv7em-apple-darwin");
    2519           0 :     case MachO::CPU_SUBTYPE_ARM_V7K:
    2520           0 :       if (McpuDefault)
    2521           0 :         *McpuDefault = "cortex-a7";
    2522           0 :       if (ArchFlag)
    2523           0 :         *ArchFlag = "armv7k";
    2524           0 :       return Triple("armv7k-apple-darwin");
    2525          17 :     case MachO::CPU_SUBTYPE_ARM_V7M:
    2526          17 :       if (McpuDefault)
    2527           7 :         *McpuDefault = "cortex-m3";
    2528          17 :       if (ArchFlag)
    2529           5 :         *ArchFlag = "armv7m";
    2530          17 :       return Triple("thumbv7m-apple-darwin");
    2531          30 :     case MachO::CPU_SUBTYPE_ARM_V7S:
    2532          30 :       if (McpuDefault)
    2533           3 :         *McpuDefault = "cortex-a7";
    2534          30 :       if (ArchFlag)
    2535           1 :         *ArchFlag = "armv7s";
    2536          30 :       return Triple("armv7s-apple-darwin");
    2537             :     default:
    2538             :       return Triple();
    2539             :     }
    2540          33 :   case MachO::CPU_TYPE_ARM64:
    2541          33 :     switch (CPUSubType & ~MachO::CPU_SUBTYPE_MASK) {
    2542          33 :     case MachO::CPU_SUBTYPE_ARM64_ALL:
    2543          33 :       if (McpuDefault)
    2544           9 :         *McpuDefault = "cyclone";
    2545          33 :       if (ArchFlag)
    2546           1 :         *ArchFlag = "arm64";
    2547          33 :       return Triple("arm64-apple-darwin");
    2548             :     default:
    2549             :       return Triple();
    2550             :     }
    2551           0 :   case MachO::CPU_TYPE_POWERPC:
    2552           0 :     switch (CPUSubType & ~MachO::CPU_SUBTYPE_MASK) {
    2553           0 :     case MachO::CPU_SUBTYPE_POWERPC_ALL:
    2554           0 :       if (ArchFlag)
    2555           0 :         *ArchFlag = "ppc";
    2556           0 :       return Triple("ppc-apple-darwin");
    2557             :     default:
    2558             :       return Triple();
    2559             :     }
    2560           0 :   case MachO::CPU_TYPE_POWERPC64:
    2561           0 :     switch (CPUSubType & ~MachO::CPU_SUBTYPE_MASK) {
    2562           0 :     case MachO::CPU_SUBTYPE_POWERPC_ALL:
    2563           0 :       if (ArchFlag)
    2564           0 :         *ArchFlag = "ppc64";
    2565           0 :       return Triple("ppc64-apple-darwin");
    2566             :     default:
    2567             :       return Triple();
    2568             :     }
    2569             :   default:
    2570             :     return Triple();
    2571             :   }
    2572             : }
    2573             : 
    2574           5 : Triple MachOObjectFile::getHostArch() {
    2575          15 :   return Triple(sys::getDefaultTargetTriple());
    2576             : }
    2577             : 
    2578          23 : bool MachOObjectFile::isValidArch(StringRef ArchFlag) {
    2579             :   return StringSwitch<bool>(ArchFlag)
    2580          46 :       .Case("i386", true)
    2581          46 :       .Case("x86_64", true)
    2582          46 :       .Case("x86_64h", true)
    2583          46 :       .Case("armv4t", true)
    2584          46 :       .Case("arm", true)
    2585          46 :       .Case("armv5e", true)
    2586          46 :       .Case("armv6", true)
    2587          46 :       .Case("armv6m", true)
    2588          46 :       .Case("armv7", true)
    2589          46 :       .Case("armv7em", true)
    2590          46 :       .Case("armv7k", true)
    2591          46 :       .Case("armv7m", true)
    2592          46 :       .Case("armv7s", true)
    2593          46 :       .Case("arm64", true)
    2594          46 :       .Case("ppc", true)
    2595          46 :       .Case("ppc64", true)
    2596          23 :       .Default(false);
    2597             : }
    2598             : 
    2599        5242 : Triple::ArchType MachOObjectFile::getArch() const {
    2600        5242 :   return getArch(getCPUType(*this));
    2601             : }
    2602             : 
    2603         709 : Triple MachOObjectFile::getArchTriple(const char **McpuDefault) const {
    2604         709 :   return getArchTriple(Header.cputype, Header.cpusubtype, McpuDefault);
    2605             : }
    2606             : 
    2607           0 : relocation_iterator MachOObjectFile::section_rel_begin(unsigned Index) const {
    2608             :   DataRefImpl DRI;
    2609           0 :   DRI.d.a = Index;
    2610           0 :   return section_rel_begin(DRI);
    2611             : }
    2612             : 
    2613           0 : relocation_iterator MachOObjectFile::section_rel_end(unsigned Index) const {
    2614             :   DataRefImpl DRI;
    2615           0 :   DRI.d.a = Index;
    2616           0 :   return section_rel_end(DRI);
    2617             : }
    2618             : 
    2619          57 : dice_iterator MachOObjectFile::begin_dices() const {
    2620             :   DataRefImpl DRI;
    2621          57 :   if (!DataInCodeLoadCmd)
    2622          70 :     return dice_iterator(DiceRef(DRI, this));
    2623             : 
    2624          22 :   MachO::linkedit_data_command DicLC = getDataInCodeLoadCommand();
    2625          22 :   DRI.p = reinterpret_cast<uintptr_t>(getPtr(*this, DicLC.dataoff));
    2626          44 :   return dice_iterator(DiceRef(DRI, this));
    2627             : }
    2628             : 
    2629          57 : dice_iterator MachOObjectFile::end_dices() const {
    2630             :   DataRefImpl DRI;
    2631          57 :   if (!DataInCodeLoadCmd)
    2632          70 :     return dice_iterator(DiceRef(DRI, this));
    2633             : 
    2634          22 :   MachO::linkedit_data_command DicLC = getDataInCodeLoadCommand();
    2635          22 :   unsigned Offset = DicLC.dataoff + DicLC.datasize;
    2636          22 :   DRI.p = reinterpret_cast<uintptr_t>(getPtr(*this, Offset));
    2637          44 :   return dice_iterator(DiceRef(DRI, this));
    2638             : }
    2639             : 
    2640          46 : ExportEntry::ExportEntry(Error *E, const MachOObjectFile *O,
    2641          92 :                          ArrayRef<uint8_t> T) : E(E), O(O), Trie(T) {}
    2642             : 
    2643          22 : void ExportEntry::moveToFirst() {
    2644          22 :   ErrorAsOutParameter ErrAsOutParam(E);
    2645          22 :   pushNode(0);
    2646          44 :   if (*E)
    2647             :     return;
    2648          22 :   pushDownUntilBottom();
    2649             : }
    2650             : 
    2651          36 : void ExportEntry::moveToEnd() {
    2652             :   Stack.clear();
    2653          36 :   Done = true;
    2654          36 : }
    2655             : 
    2656          92 : bool ExportEntry::operator==(const ExportEntry &Other) const {
    2657             :   // Common case, one at end, other iterating from begin.
    2658          92 :   if (Done || Other.Done)
    2659          92 :     return (Done == Other.Done);
    2660             :   // Not equal if different stack sizes.
    2661           0 :   if (Stack.size() != Other.Stack.size())
    2662             :     return false;
    2663             :   // Not equal if different cumulative strings.
    2664           0 :   if (!CumulativeString.equals(Other.CumulativeString))
    2665             :     return false;
    2666             :   // Equal if all nodes in both stacks match.
    2667           0 :   for (unsigned i=0; i < Stack.size(); ++i) {
    2668           0 :     if (Stack[i].Start != Other.Stack[i].Start)
    2669             :       return false;
    2670             :   }
    2671             :   return true;
    2672             : }
    2673             : 
    2674         399 : uint64_t ExportEntry::readULEB128(const uint8_t *&Ptr, const char **error) {
    2675             :   unsigned Count;
    2676         399 :   uint64_t Result = decodeULEB128(Ptr, &Count, Trie.end(), error);
    2677         399 :   Ptr += Count;
    2678         399 :   if (Ptr > Trie.end())
    2679           0 :     Ptr = Trie.end();
    2680         399 :   return Result;
    2681             : }
    2682             : 
    2683          82 : StringRef ExportEntry::name() const {
    2684          82 :   return CumulativeString;
    2685             : }
    2686             : 
    2687          72 : uint64_t ExportEntry::flags() const {
    2688          72 :   return Stack.back().Flags;
    2689             : }
    2690             : 
    2691          63 : uint64_t ExportEntry::address() const {
    2692          63 :   return Stack.back().Address;
    2693             : }
    2694             : 
    2695          24 : uint64_t ExportEntry::other() const {
    2696          24 :   return Stack.back().Other;
    2697             : }
    2698             : 
    2699          28 : StringRef ExportEntry::otherName() const {
    2700          28 :   const char* ImportName = Stack.back().ImportName;
    2701          28 :   if (ImportName)
    2702          24 :     return StringRef(ImportName);
    2703           4 :   return StringRef();
    2704             : }
    2705             : 
    2706           0 : uint32_t ExportEntry::nodeOffset() const {
    2707           0 :   return Stack.back().Start - Trie.begin();
    2708             : }
    2709             : 
    2710         134 : ExportEntry::NodeState::NodeState(const uint8_t *Ptr)
    2711         134 :     : Start(Ptr), Current(Ptr) {}
    2712             : 
    2713         134 : void ExportEntry::pushNode(uint64_t offset) {
    2714         134 :   ErrorAsOutParameter ErrAsOutParam(E);
    2715         134 :   const uint8_t *Ptr = Trie.begin() + offset;
    2716         134 :   NodeState State(Ptr);
    2717             :   const char *error;
    2718         134 :   uint64_t ExportInfoSize = readULEB128(State.Current, &error);
    2719         134 :   if (error) {
    2720           6 :     *E = malformedError("export info size " + Twine(error) +
    2721           4 :                         " in export trie data at node: 0x" +
    2722           2 :                         Twine::utohexstr(offset));
    2723           2 :     moveToEnd();
    2724           2 :     return;
    2725             :   }
    2726         132 :   State.IsExportNode = (ExportInfoSize != 0);
    2727         132 :   const uint8_t* Children = State.Current + ExportInfoSize;
    2728         132 :   if (Children > Trie.end()) {
    2729           2 :     *E = malformedError(
    2730           1 :         "export info size: 0x" + Twine::utohexstr(ExportInfoSize) +
    2731           2 :         " in export trie data at node: 0x" + Twine::utohexstr(offset) +
    2732           1 :         " too big and extends past end of trie data");
    2733           1 :     moveToEnd();
    2734           1 :     return;
    2735             :   }
    2736         131 :   if (State.IsExportNode) {
    2737             :     const uint8_t *ExportStart = State.Current;
    2738          74 :     State.Flags = readULEB128(State.Current, &error);
    2739          74 :     if (error) {
    2740           0 :       *E = malformedError("flags " + Twine(error) +
    2741           0 :                           " in export trie data at node: 0x" +
    2742           0 :                           Twine::utohexstr(offset));
    2743           0 :       moveToEnd();
    2744           0 :       return;
    2745             :     }
    2746          74 :     uint64_t Kind = State.Flags & MachO::EXPORT_SYMBOL_FLAGS_KIND_MASK;
    2747         114 :     if (State.Flags != 0 &&
    2748          80 :         (Kind != MachO::EXPORT_SYMBOL_FLAGS_KIND_REGULAR &&
    2749          42 :          Kind != MachO::EXPORT_SYMBOL_FLAGS_KIND_ABSOLUTE &&
    2750             :          Kind != MachO::EXPORT_SYMBOL_FLAGS_KIND_THREAD_LOCAL)) {
    2751           2 :       *E = malformedError(
    2752           2 :           "unsupported exported symbol kind: " + Twine((int)Kind) +
    2753           2 :           " in flags: 0x" + Twine::utohexstr(State.Flags) +
    2754           2 :           " in export trie data at node: 0x" + Twine::utohexstr(offset));
    2755           1 :       moveToEnd();
    2756           1 :       return;
    2757             :     }
    2758          73 :     if (State.Flags & MachO::EXPORT_SYMBOL_FLAGS_REEXPORT) {
    2759          19 :       State.Address = 0;
    2760          19 :       State.Other = readULEB128(State.Current, &error); // dylib ordinal
    2761          19 :       if (error) {
    2762           0 :         *E = malformedError("dylib ordinal of re-export " + Twine(error) +
    2763           0 :                             " in export trie data at node: 0x" +
    2764           0 :                             Twine::utohexstr(offset));
    2765           0 :         moveToEnd();
    2766           0 :         return;
    2767             :       }
    2768          19 :       if (O != nullptr) {
    2769          19 :         if (State.Other > O->getLibraryCount()) {
    2770           2 :           *E = malformedError(
    2771           2 :               "bad library ordinal: " + Twine((int)State.Other) + " (max " +
    2772           3 :               Twine((int)O->getLibraryCount()) +
    2773           2 :               ") in export trie data at node: 0x" + Twine::utohexstr(offset));
    2774           1 :           moveToEnd();
    2775           1 :           return;
    2776             :         }
    2777             :       }
    2778          18 :       State.ImportName = reinterpret_cast<const char*>(State.Current);
    2779          18 :       if (*State.ImportName == '\0') {
    2780           8 :         State.Current++;
    2781             :       } else {
    2782          10 :         const uint8_t *End = State.Current + 1;
    2783          10 :         if (End >= Trie.end()) {
    2784           2 :           *E = malformedError("import name of re-export in export trie data at "
    2785           1 :                               "node: 0x" +
    2786           2 :                               Twine::utohexstr(offset) +
    2787           1 :                               " starts past end of trie data");
    2788           1 :           moveToEnd();
    2789           1 :           return;
    2790             :         }
    2791          75 :         while(*End != '\0' && End < Trie.end())
    2792          33 :           End++;
    2793           9 :         if (*End != '\0') {
    2794           2 :           *E = malformedError("import name of re-export in export trie data at "
    2795           1 :                               "node: 0x" +
    2796           2 :                               Twine::utohexstr(offset) +
    2797           1 :                               " extends past end of trie data");
    2798           1 :           moveToEnd();
    2799           1 :           return;
    2800             :         }
    2801           8 :         State.Current = End + 1;
    2802             :       }
    2803             :     } else {
    2804          54 :       State.Address = readULEB128(State.Current, &error);
    2805          54 :       if (error) {
    2806           0 :         *E = malformedError("address " + Twine(error) +
    2807           0 :                             " in export trie data at node: 0x" +
    2808           0 :                             Twine::utohexstr(offset));
    2809           0 :         moveToEnd();
    2810           0 :         return;
    2811             :       }
    2812          54 :       if (State.Flags & MachO::EXPORT_SYMBOL_FLAGS_STUB_AND_RESOLVER) {
    2813           5 :         State.Other = readULEB128(State.Current, &error);
    2814           5 :         if (error) {
    2815           0 :           *E = malformedError("resolver of stub and resolver " + Twine(error) +
    2816           0 :                               " in export trie data at node: 0x" +
    2817           0 :                               Twine::utohexstr(offset));
    2818           0 :           moveToEnd();
    2819           0 :           return;
    2820             :         }
    2821             :       }
    2822             :     }
    2823          70 :     if(ExportStart + ExportInfoSize != State.Current) {
    2824           2 :       *E = malformedError(
    2825           1 :           "inconsistant export info size: 0x" +
    2826           2 :           Twine::utohexstr(ExportInfoSize) + " where actual size was: 0x" +
    2827           3 :           Twine::utohexstr(State.Current - ExportStart) +
    2828           2 :           " in export trie data at node: 0x" + Twine::utohexstr(offset));
    2829           1 :       moveToEnd();
    2830           1 :       return;
    2831             :     }
    2832             :   }
    2833         126 :   State.ChildCount = *Children;
    2834         182 :   if (State.ChildCount != 0 && Children + 1 >= Trie.end()) {
    2835           2 :     *E = malformedError("byte for count of childern in export trie data at "
    2836           1 :                         "node: 0x" +
    2837           2 :                         Twine::utohexstr(offset) +
    2838           1 :                         " extends past end of trie data");
    2839           1 :     moveToEnd();
    2840           1 :     return;
    2841             :   }
    2842         125 :   State.Current = Children + 1;
    2843         125 :   State.NextChildIndex = 0;
    2844         125 :   State.ParentStringLength = CumulativeString.size();
    2845         125 :   Stack.push_back(State);
    2846             : }
    2847             : 
    2848          81 : void ExportEntry::pushDownUntilBottom() {
    2849          81 :   ErrorAsOutParameter ErrAsOutParam(E);
    2850             :   const char *error;
    2851         287 :   while (Stack.back().NextChildIndex < Stack.back().ChildCount) {
    2852             :     NodeState &Top = Stack.back();
    2853         114 :     CumulativeString.resize(Top.ParentStringLength);
    2854        1840 :     for (;*Top.Current != 0 && Top.Current < Trie.end(); Top.Current++) {
    2855         575 :       char C = *Top.Current;
    2856         575 :       CumulativeString.push_back(C);
    2857             :     }
    2858         114 :     if (Top.Current >= Trie.end()) {
    2859           3 :       *E = malformedError("edge sub-string in export trie data at node: 0x" +
    2860           3 :                           Twine::utohexstr(Top.Start - Trie.begin()) +
    2861           3 :                           " for child #" + Twine((int)Top.NextChildIndex) +
    2862           1 :                           " extends past end of trie data");
    2863           1 :       moveToEnd();
    2864           1 :       return;
    2865             :     }
    2866         113 :     Top.Current += 1;
    2867         113 :     uint64_t childNodeIndex = readULEB128(Top.Current, &error);
    2868         113 :     if (error) {
    2869           0 :       *E = malformedError("child node offset " + Twine(error) +
    2870           0 :                           " in export trie data at node: 0x" +
    2871           0 :                           Twine::utohexstr(Top.Start - Trie.begin()));
    2872           0 :       moveToEnd();
    2873           0 :       return;
    2874             :     }
    2875         595 :     for (const NodeState &node : nodes()) {
    2876         242 :       if (node.Start == Trie.begin() + childNodeIndex){
    2877           3 :         *E = malformedError("loop in childern in export trie data at node: 0x" +
    2878           3 :                             Twine::utohexstr(Top.Start - Trie.begin()) +
    2879           2 :                             " back to node: 0x" +
    2880           1 :                             Twine::utohexstr(childNodeIndex));
    2881           1 :         moveToEnd();
    2882           1 :         return;
    2883             :       }
    2884             :     }
    2885         112 :     Top.NextChildIndex += 1;
    2886         112 :     pushNode(childNodeIndex);
    2887         224 :     if (*E)
    2888             :       return;
    2889             :   }
    2890          70 :   if (!Stack.back().IsExportNode) {
    2891           2 :     *E = malformedError("node is not an export node in export trie data at "
    2892           1 :                         "node: 0x" +
    2893           2 :                         Twine::utohexstr(Stack.back().Start - Trie.begin()));
    2894           1 :     moveToEnd();
    2895           1 :     return;
    2896             :   }
    2897             : }
    2898             : 
    2899             : // We have a trie data structure and need a way to walk it that is compatible
    2900             : // with the C++ iterator model. The solution is a non-recursive depth first
    2901             : // traversal where the iterator contains a stack of parent nodes along with a
    2902             : // string that is the accumulation of all edge strings along the parent chain
    2903             : // to this point.
    2904             : //
    2905             : // There is one "export" node for each exported symbol.  But because some
    2906             : // symbols may be a prefix of another symbol (e.g. _dup and _dup2), an export
    2907             : // node may have child nodes too.
    2908             : //
    2909             : // The algorithm for moveNext() is to keep moving down the leftmost unvisited
    2910             : // child until hitting a node with no children (which is an export node or
    2911             : // else the trie is malformed). On the way down, each node is pushed on the
    2912             : // stack ivar.  If there is no more ways down, it pops up one and tries to go
    2913             : // down a sibling path until a childless node is reached.
    2914          69 : void ExportEntry::moveNext() {
    2915             :   assert(!Stack.empty() && "ExportEntry::moveNext() with empty node stack");
    2916          69 :   if (!Stack.back().IsExportNode) {
    2917           0 :     *E = malformedError("node is not an export node in export trie data at "
    2918           0 :                         "node: 0x" +
    2919           0 :                         Twine::utohexstr(Stack.back().Start - Trie.begin()));
    2920           0 :     moveToEnd();
    2921           0 :     return;
    2922             :   }
    2923             : 
    2924             :   Stack.pop_back();
    2925          94 :   while (!Stack.empty()) {
    2926             :     NodeState &Top = Stack.back();
    2927          84 :     if (Top.NextChildIndex < Top.ChildCount) {
    2928          59 :       pushDownUntilBottom();
    2929             :       // Now at the next export node.
    2930          59 :       return;
    2931             :     } else {
    2932          25 :       if (Top.IsExportNode) {
    2933             :         // This node has no children but is itself an export node.
    2934           0 :         CumulativeString.resize(Top.ParentStringLength);
    2935           0 :         return;
    2936             :       }
    2937             :       Stack.pop_back();
    2938             :     }
    2939             :   }
    2940          10 :   Done = true;
    2941             : }
    2942             : 
    2943             : iterator_range<export_iterator>
    2944          23 : MachOObjectFile::exports(Error &E, ArrayRef<uint8_t> Trie,
    2945             :                          const MachOObjectFile *O) {
    2946          46 :   ExportEntry Start(&E, O, Trie);
    2947          23 :   if (Trie.empty())
    2948           1 :     Start.moveToEnd();
    2949             :   else
    2950          22 :     Start.moveToFirst();
    2951             : 
    2952          46 :   ExportEntry Finish(&E, O, Trie);
    2953          23 :   Finish.moveToEnd();
    2954             : 
    2955         138 :   return make_range(export_iterator(Start), export_iterator(Finish));
    2956             : }
    2957             : 
    2958          19 : iterator_range<export_iterator> MachOObjectFile::exports(Error &Err) const {
    2959          19 :   return exports(Err, getDyldInfoExportsTrie(), this);
    2960             : }
    2961             : 
    2962          24 : MachORebaseEntry::MachORebaseEntry(Error *E, const MachOObjectFile *O,
    2963          24 :                                    ArrayRef<uint8_t> Bytes, bool is64Bit)
    2964          24 :     : E(E), O(O), Opcodes(Bytes), Ptr(Bytes.begin()),
    2965          48 :       PointerSize(is64Bit ? 8 : 4) {}
    2966             : 
    2967          12 : void MachORebaseEntry::moveToFirst() {
    2968          12 :   Ptr = Opcodes.begin();
    2969          12 :   moveNext();
    2970          12 : }
    2971             : 
    2972          24 : void MachORebaseEntry::moveToEnd() {
    2973          24 :   Ptr = Opcodes.end();
    2974          24 :   RemainingLoopCount = 0;
    2975          24 :   Done = true;
    2976          24 : }
    2977             : 
    2978          23 : void MachORebaseEntry::moveNext() {
    2979          23 :   ErrorAsOutParameter ErrAsOutParam(E);
    2980             :   // If in the middle of some loop, move to next rebasing in loop.
    2981          23 :   SegmentOffset += AdvanceAmount;
    2982          23 :   if (RemainingLoopCount) {
    2983           5 :     --RemainingLoopCount;
    2984           5 :     return;
    2985             :   }
    2986             :   // REBASE_OPCODE_DONE is only used for padding if we are not aligned to
    2987             :   // pointer size. Therefore it is possible to reach the end without ever having
    2988             :   // seen REBASE_OPCODE_DONE.
    2989          36 :   if (Ptr == Opcodes.end()) {
    2990           0 :     Done = true;
    2991           0 :     return;
    2992             :   }
    2993             :   bool More = true;
    2994          20 :   while (More) {
    2995             :     // Parse next opcode and set up next loop.
    2996          38 :     const uint8_t *OpcodeStart = Ptr;
    2997          38 :     uint8_t Byte = *Ptr++;
    2998          38 :     uint8_t ImmValue = Byte & MachO::REBASE_IMMEDIATE_MASK;
    2999          38 :     uint8_t Opcode = Byte & MachO::REBASE_OPCODE_MASK;
    3000             :     uint32_t Count, Skip;
    3001          38 :     const char *error = nullptr;
    3002          38 :     switch (Opcode) {
    3003           1 :     case MachO::REBASE_OPCODE_DONE:
    3004             :       More = false;
    3005           1 :       Done = true;
    3006           1 :       moveToEnd();
    3007             :       DEBUG_WITH_TYPE("mach-o-rebase", dbgs() << "REBASE_OPCODE_DONE\n");
    3008             :       break;
    3009          12 :     case MachO::REBASE_OPCODE_SET_TYPE_IMM:
    3010          12 :       RebaseType = ImmValue;
    3011          12 :       if (RebaseType > MachO::REBASE_TYPE_TEXT_PCREL32) {
    3012           3 :         *E = malformedError("for REBASE_OPCODE_SET_TYPE_IMM bad bind type: " +
    3013           3 :                             Twine((int)RebaseType) + " for opcode at: 0x" +
    3014           2 :                             Twine::utohexstr(OpcodeStart - Opcodes.begin()));
    3015           1 :         moveToEnd();
    3016           1 :         return;
    3017             :       }
    3018             :       DEBUG_WITH_TYPE(
    3019             :           "mach-o-rebase",
    3020             :           dbgs() << "REBASE_OPCODE_SET_TYPE_IMM: "
    3021             :                  << "RebaseType=" << (int) RebaseType << "\n");
    3022             :       break;
    3023          11 :     case MachO::REBASE_OPCODE_SET_SEGMENT_AND_OFFSET_ULEB:
    3024          11 :       SegmentIndex = ImmValue;
    3025          11 :       SegmentOffset = readULEB128(&error);
    3026          11 :       if (error) {
    3027           3 :         *E = malformedError("for REBASE_OPCODE_SET_SEGMENT_AND_OFFSET_ULEB " +
    3028           2 :                             Twine(error) + " for opcode at: 0x" +
    3029           2 :                             Twine::utohexstr(OpcodeStart - Opcodes.begin()));
    3030           1 :         moveToEnd();
    3031           1 :         return;
    3032             :       }
    3033          20 :       error = O->RebaseEntryCheckSegAndOffset(SegmentIndex, SegmentOffset,
    3034             :                                               true);
    3035          10 :       if (error) {
    3036           6 :         *E = malformedError("for REBASE_OPCODE_SET_SEGMENT_AND_OFFSET_ULEB " +
    3037           4 :                             Twine(error) + " for opcode at: 0x" +
    3038           4 :                             Twine::utohexstr(OpcodeStart - Opcodes.begin()));
    3039           2 :         moveToEnd();
    3040           2 :         return;
    3041             :       }
    3042             :       DEBUG_WITH_TYPE(
    3043             :           "mach-o-rebase",
    3044             :           dbgs() << "REBASE_OPCODE_SET_SEGMENT_AND_OFFSET_ULEB: "
    3045             :                  << "SegmentIndex=" << SegmentIndex << ", "
    3046             :                  << format("SegmentOffset=0x%06X", SegmentOffset)
    3047             :                  << "\n");
    3048             :       break;
    3049           2 :     case MachO::REBASE_OPCODE_ADD_ADDR_ULEB:
    3050           2 :       SegmentOffset += readULEB128(&error);
    3051           2 :       if (error) {
    3052           0 :         *E = malformedError("for REBASE_OPCODE_ADD_ADDR_ULEB " + Twine(error) +
    3053           0 :                             " for opcode at: 0x" +
    3054           0 :                             Twine::utohexstr(OpcodeStart - Opcodes.begin()));
    3055           0 :         moveToEnd();
    3056           0 :         return;
    3057             :       }
    3058           4 :       error = O->RebaseEntryCheckSegAndOffset(SegmentIndex, SegmentOffset,
    3059             :                                               true);
    3060           2 :       if (error) {
    3061           3 :         *E = malformedError("for REBASE_OPCODE_ADD_ADDR_ULEB " + Twine(error) +
    3062           2 :                             " for opcode at: 0x" +
    3063           2 :                             Twine::utohexstr(OpcodeStart - Opcodes.begin()));
    3064           1 :         moveToEnd();
    3065           1 :         return;
    3066             :       }
    3067             :       DEBUG_WITH_TYPE("mach-o-rebase",
    3068             :                       dbgs() << "REBASE_OPCODE_ADD_ADDR_ULEB: "
    3069             :                              << format("SegmentOffset=0x%06X",
    3070             :                                        SegmentOffset) << "\n");
    3071             :       break;
    3072           1 :     case MachO::REBASE_OPCODE_ADD_ADDR_IMM_SCALED:
    3073           2 :       error = O->RebaseEntryCheckSegAndOffset(SegmentIndex, SegmentOffset,
    3074             :                                               true);
    3075           1 :       if (error) {
    3076           0 :         *E = malformedError("for REBASE_OPCODE_ADD_ADDR_IMM_SCALED " +
    3077           0 :                             Twine(error) + " for opcode at: 0x" +
    3078           0 :                             Twine::utohexstr(OpcodeStart - Opcodes.begin()));
    3079           0 :         moveToEnd();
    3080           0 :         return;
    3081             :       }
    3082           1 :       SegmentOffset += ImmValue * PointerSize;
    3083           2 :       error = O->RebaseEntryCheckSegAndOffset(SegmentIndex, SegmentOffset,
    3084             :                                               false);
    3085           1 :       if (error) {
    3086           1 :         *E =
    3087           2 :             malformedError("for REBASE_OPCODE_ADD_ADDR_IMM_SCALED "
    3088           1 :                            " (after adding immediate times the pointer size) " +
    3089           2 :                            Twine(error) + " for opcode at: 0x" +
    3090           2 :                            Twine::utohexstr(OpcodeStart - Opcodes.begin()));
    3091           1 :         moveToEnd();
    3092           1 :         return;
    3093             :       }
    3094             :       DEBUG_WITH_TYPE("mach-o-rebase",
    3095             :                       dbgs() << "REBASE_OPCODE_ADD_ADDR_IMM_SCALED: "
    3096             :                              << format("SegmentOffset=0x%06X",
    3097             :                                        SegmentOffset) << "\n");
    3098             :       break;
    3099           4 :     case MachO::REBASE_OPCODE_DO_REBASE_IMM_TIMES:
    3100           8 :       error = O->RebaseEntryCheckSegAndOffset(SegmentIndex, SegmentOffset,
    3101             :                                               true);
    3102           4 :       if (error) {
    3103           0 :         *E = malformedError("for REBASE_OPCODE_DO_REBASE_IMM_TIMES " +
    3104           0 :                             Twine(error) + " for opcode at: 0x" +
    3105           0 :                             Twine::utohexstr(OpcodeStart - Opcodes.begin()));
    3106           0 :         moveToEnd();
    3107           0 :         return;
    3108             :       }
    3109           4 :       AdvanceAmount = PointerSize;
    3110             :       Skip = 0;
    3111           4 :       Count = ImmValue;
    3112           4 :       if (ImmValue != 0)
    3113           4 :         RemainingLoopCount = ImmValue - 1;
    3114             :       else
    3115           0 :         RemainingLoopCount = 0;
    3116           8 :       error = O->RebaseEntryCheckCountAndSkip(Count, Skip, PointerSize,
    3117             :                                               SegmentIndex, SegmentOffset);
    3118           4 :       if (error) {
    3119           3 :         *E = malformedError("for REBASE_OPCODE_DO_REBASE_IMM_TIMES " +
    3120           2 :                             Twine(error) + " for opcode at: 0x" +
    3121           2 :                             Twine::utohexstr(OpcodeStart - Opcodes.begin()));
    3122           1 :         moveToEnd();
    3123           1 :         return;
    3124             :       }
    3125             :       DEBUG_WITH_TYPE(
    3126             :           "mach-o-rebase",
    3127             :           dbgs() << "REBASE_OPCODE_DO_REBASE_IMM_TIMES: "
    3128             :                  << format("SegmentOffset=0x%06X", SegmentOffset)
    3129             :                  << ", AdvanceAmount=" << AdvanceAmount
    3130             :                  << ", RemainingLoopCount=" << RemainingLoopCount
    3131             :                  << "\n");
    3132             :       return;
    3133           1 :     case MachO::REBASE_OPCODE_DO_REBASE_ULEB_TIMES:
    3134           2 :       error = O->RebaseEntryCheckSegAndOffset(SegmentIndex, SegmentOffset,
    3135             :                                               true);
    3136           1 :       if (error) {
    3137           0 :         *E = malformedError("for REBASE_OPCODE_DO_REBASE_ULEB_TIMES " +
    3138           0 :                             Twine(error) + " for opcode at: 0x" +
    3139           0 :                             Twine::utohexstr(OpcodeStart - Opcodes.begin()));
    3140           0 :         moveToEnd();
    3141           0 :         return;
    3142             :       }
    3143           1 :       AdvanceAmount = PointerSize;
    3144             :       Skip = 0;
    3145           1 :       Count = readULEB128(&error);
    3146           1 :       if (error) {
    3147           0 :         *E = malformedError("for REBASE_OPCODE_DO_REBASE_ULEB_TIMES " +
    3148           0 :                             Twine(error) + " for opcode at: 0x" +
    3149           0 :                             Twine::utohexstr(OpcodeStart - Opcodes.begin()));
    3150           0 :         moveToEnd();
    3151           0 :         return;
    3152             :       }
    3153           1 :       if (Count != 0)
    3154           1 :         RemainingLoopCount = Count - 1;
    3155             :       else
    3156           0 :         RemainingLoopCount = 0;
    3157           2 :       error = O->RebaseEntryCheckCountAndSkip(Count, Skip, PointerSize,
    3158             :                                               SegmentIndex, SegmentOffset);
    3159           1 :       if (error) {
    3160           3 :         *E = malformedError("for REBASE_OPCODE_DO_REBASE_ULEB_TIMES " +
    3161           2 :                             Twine(error) + " for opcode at: 0x" +
    3162           2 :                             Twine::utohexstr(OpcodeStart - Opcodes.begin()));
    3163           1 :         moveToEnd();
    3164           1 :         return;
    3165             :       }
    3166             :       DEBUG_WITH_TYPE(
    3167             :           "mach-o-rebase",
    3168             :           dbgs() << "REBASE_OPCODE_DO_REBASE_ULEB_TIMES: "
    3169             :                  << format("SegmentOffset=0x%06X", SegmentOffset)
    3170             :                  << ", AdvanceAmount=" << AdvanceAmount
    3171             :                  << ", RemainingLoopCount=" << RemainingLoopCount
    3172             :                  << "\n");
    3173             :       return;
    3174           3 :     case MachO::REBASE_OPCODE_DO_REBASE_ADD_ADDR_ULEB:
    3175           6 :       error = O->RebaseEntryCheckSegAndOffset(SegmentIndex, SegmentOffset,
    3176             :                                               true);
    3177           3 :       if (error) {
    3178           0 :         *E = malformedError("for REBASE_OPCODE_DO_REBASE_ADD_ADDR_ULEB " +
    3179           0 :                             Twine(error) + " for opcode at: 0x" +
    3180           0 :                             Twine::utohexstr(OpcodeStart - Opcodes.begin()));
    3181           0 :         moveToEnd();
    3182           0 :         return;
    3183             :       }
    3184           3 :       Skip = readULEB128(&error);
    3185           3 :       if (error) {
    3186           0 :         *E = malformedError("for REBASE_OPCODE_DO_REBASE_ADD_ADDR_ULEB " +
    3187           0 :                             Twine(error) + " for opcode at: 0x" +
    3188           0 :                             Twine::utohexstr(OpcodeStart - Opcodes.begin()));
    3189           0 :         moveToEnd();
    3190           0 :         return;
    3191             :       }
    3192           3 :       AdvanceAmount = Skip + PointerSize;
    3193             :       Count = 1;
    3194           3 :       RemainingLoopCount = 0;
    3195           6 :       error = O->RebaseEntryCheckCountAndSkip(Count, Skip, PointerSize,
    3196             :                                               SegmentIndex, SegmentOffset);
    3197           3 :       if (error) {
    3198           3 :         *E = malformedError("for REBASE_OPCODE_DO_REBASE_ADD_ADDR_ULEB " +
    3199           2 :                             Twine(error) + " for opcode at: 0x" +
    3200           2 :                             Twine::utohexstr(OpcodeStart - Opcodes.begin()));
    3201           1 :         moveToEnd();
    3202           1 :         return;
    3203             :       }
    3204             :       DEBUG_WITH_TYPE(
    3205             :           "mach-o-rebase",
    3206             :           dbgs() << "REBASE_OPCODE_DO_REBASE_ADD_ADDR_ULEB: "
    3207             :                  << format("SegmentOffset=0x%06X", SegmentOffset)
    3208             :                  << ", AdvanceAmount=" << AdvanceAmount
    3209             :                  << ", RemainingLoopCount=" << RemainingLoopCount
    3210             :                  << "\n");
    3211             :       return;
    3212           2 :     case MachO::REBASE_OPCODE_DO_REBASE_ULEB_TIMES_SKIPPING_ULEB:
    3213           4 :       error = O->RebaseEntryCheckSegAndOffset(SegmentIndex, SegmentOffset,
    3214             :                                               true);
    3215           2 :       if (error) {
    3216           0 :         *E = malformedError("for REBASE_OPCODE_DO_REBASE_ULEB_TIMES_SKIPPING_"
    3217           0 :                             "ULEB " +
    3218           0 :                             Twine(error) + " for opcode at: 0x" +
    3219           0 :                             Twine::utohexstr(OpcodeStart - Opcodes.begin()));
    3220           0 :         moveToEnd();
    3221           0 :         return;
    3222             :       }
    3223           2 :       Count = readULEB128(&error);
    3224           2 :       if (error) {
    3225           0 :         *E = malformedError("for REBASE_OPCODE_DO_REBASE_ULEB_TIMES_SKIPPING_"
    3226           0 :                             "ULEB " +
    3227           0 :                             Twine(error) + " for opcode at: 0x" +
    3228           0 :                             Twine::utohexstr(OpcodeStart - Opcodes.begin()));
    3229           0 :         moveToEnd();
    3230           0 :         return;
    3231             :       }
    3232           2 :       if (Count != 0)
    3233           2 :         RemainingLoopCount = Count - 1;
    3234             :       else
    3235           0 :         RemainingLoopCount = 0;
    3236           2 :       Skip = readULEB128(&error);
    3237           2 :       if (error) {
    3238           0 :         *E = malformedError("for REBASE_OPCODE_DO_REBASE_ULEB_TIMES_SKIPPING_"
    3239           0 :                             "ULEB " +
    3240           0 :                             Twine(error) + " for opcode at: 0x" +
    3241           0 :                             Twine::utohexstr(OpcodeStart - Opcodes.begin()));
    3242           0 :         moveToEnd();
    3243           0 :         return;
    3244             :       }
    3245           2 :       AdvanceAmount = Skip + PointerSize;
    3246             : 
    3247           4 :       error = O->RebaseEntryCheckCountAndSkip(Count, Skip, PointerSize,
    3248             :                                               SegmentIndex, SegmentOffset);
    3249           2 :       if (error) {
    3250           2 :         *E = malformedError("for REBASE_OPCODE_DO_REBASE_ULEB_TIMES_SKIPPING_"
    3251           1 :                             "ULEB " +
    3252           2 :                             Twine(error) + " for opcode at: 0x" +
    3253           2 :                             Twine::utohexstr(OpcodeStart - Opcodes.begin()));
    3254           1 :         moveToEnd();
    3255           1 :         return;
    3256             :       }
    3257             :       DEBUG_WITH_TYPE(
    3258             :           "mach-o-rebase",
    3259             :           dbgs() << "REBASE_OPCODE_DO_REBASE_ULEB_TIMES_SKIPPING_ULEB: "
    3260             :                  << format("SegmentOffset=0x%06X", SegmentOffset)
    3261             :                  << ", AdvanceAmount=" << AdvanceAmount
    3262             :                  << ", RemainingLoopCount=" << RemainingLoopCount
    3263             :                  << "\n");
    3264             :       return;
    3265           1 :     default:
    3266           3 :       *E = malformedError("bad rebase info (bad opcode value 0x" +
    3267           3 :                           Twine::utohexstr(Opcode) + " for opcode at: 0x" +
    3268           2 :                           Twine::utohexstr(OpcodeStart - Opcodes.begin()));
    3269           1 :       moveToEnd();
    3270           1 :       return;
    3271             :     }
    3272             :   }
    3273             : }
    3274             : 
    3275          21 : uint64_t MachORebaseEntry::readULEB128(const char **error) {
    3276             :   unsigned Count;
    3277          21 :   uint64_t Result = decodeULEB128(Ptr, &Count, Opcodes.end(), error);
    3278          21 :   Ptr += Count;
    3279          21 :   if (Ptr > Opcodes.end())
    3280           0 :     Ptr = Opcodes.end();
    3281          21 :   return Result;
    3282             : }
    3283             : 
    3284           0 : int32_t MachORebaseEntry::segmentIndex() const { return SegmentIndex; }
    3285             : 
    3286           0 : uint64_t MachORebaseEntry::segmentOffset() const { return SegmentOffset; }
    3287             : 
    3288          11 : StringRef MachORebaseEntry::typeName() const {
    3289          11 :   switch (RebaseType) {
    3290             :   case MachO::REBASE_TYPE_POINTER:
    3291          11 :     return "pointer";
    3292             :   case MachO::REBASE_TYPE_TEXT_ABSOLUTE32:
    3293           0 :     return "text abs32";
    3294             :   case MachO::REBASE_TYPE_TEXT_PCREL32:
    3295           0 :     return "text rel32";
    3296             :   }
    3297           0 :   return "unknown";
    3298             : }
    3299             : 
    3300             : // For use with the SegIndex of a checked Mach-O Rebase entry
    3301             : // to get the segment name.
    3302          11 : StringRef MachORebaseEntry::segmentName() const {
    3303          22 :   return O->BindRebaseSegmentName(SegmentIndex);
    3304             : }
    3305             : 
    3306             : // For use with a SegIndex,SegOffset pair from a checked Mach-O Rebase entry
    3307             : // to get the section name.
    3308          11 : StringRef MachORebaseEntry::sectionName() const {
    3309          22 :   return O->BindRebaseSectionName(SegmentIndex, SegmentOffset);
    3310             : }
    3311             : 
    3312             : // For use with a SegIndex,SegOffset pair from a checked Mach-O Rebase entry
    3313             : // to get the address.
    3314          11 : uint64_t MachORebaseEntry::address() const {
    3315          22 :   return O->BindRebaseAddress(SegmentIndex, SegmentOffset);
    3316             : }
    3317             : 
    3318          23 : bool MachORebaseEntry::operator==(const MachORebaseEntry &Other) const {
    3319             : #ifdef EXPENSIVE_CHECKS
    3320             :   assert(Opcodes == Other.Opcodes && "compare iterators of different files");
    3321             : #else
    3322             :   assert(Opcodes.data() == Other.Opcodes.data() && "compare iterators of different files");
    3323             : #endif
    3324          35 :   return (Ptr == Other.Ptr) &&
    3325          35 :          (RemainingLoopCount == Other.RemainingLoopCount) &&
    3326          35 :          (Done == Other.Done);
    3327             : }
    3328             : 
    3329             : iterator_range<rebase_iterator>
    3330          12 : MachOObjectFile::rebaseTable(Error &Err, MachOObjectFile *O,
    3331             :                              ArrayRef<uint8_t> Opcodes, bool is64) {
    3332          12 :   if (O->BindRebaseSectionTable == nullptr)
    3333             :     O->BindRebaseSectionTable = llvm::make_unique<BindRebaseSegInfo>(O);
    3334          12 :   MachORebaseEntry Start(&Err, O, Opcodes, is64);
    3335          12 :   Start.moveToFirst();
    3336             : 
    3337          12 :   MachORebaseEntry Finish(&Err, O, Opcodes, is64);
    3338          12 :   Finish.moveToEnd();
    3339             : 
    3340          12 :   return make_range(rebase_iterator(Start), rebase_iterator(Finish));
    3341             : }
    3342             : 
    3343          12 : iterator_range<rebase_iterator> MachOObjectFile::rebaseTable(Error &Err) {
    3344          12 :   return rebaseTable(Err, this, getDyldInfoRebaseOpcodes(), is64Bit());
    3345             : }
    3346             : 
    3347          84 : MachOBindEntry::MachOBindEntry(Error *E, const MachOObjectFile *O,
    3348          84 :                                ArrayRef<uint8_t> Bytes, bool is64Bit, Kind BK)
    3349          84 :     : E(E), O(O), Opcodes(Bytes), Ptr(Bytes.begin()),
    3350         252 :       PointerSize(is64Bit ? 8 : 4), TableKind(BK) {}
    3351             : 
    3352          42 : void MachOBindEntry::moveToFirst() {
    3353          42 :   Ptr = Opcodes.begin();
    3354          42 :   moveNext();
    3355          42 : }
    3356             : 
    3357          77 : void MachOBindEntry::moveToEnd() {
    3358          77 :   Ptr = Opcodes.end();
    3359          77 :   RemainingLoopCount = 0;
    3360          77 :   Done = true;
    3361          77 : }
    3362             : 
    3363          97 : void MachOBindEntry::moveNext() {
    3364          97 :   ErrorAsOutParameter ErrAsOutParam(E);
    3365             :   // If in the middle of some loop, move to next binding in loop.
    3366          97 :   SegmentOffset += AdvanceAmount;
    3367          97 :   if (RemainingLoopCount) {
    3368           2 :     --RemainingLoopCount;
    3369           2 :     return;
    3370             :   }
    3371             :   // BIND_OPCODE_DONE is only used for padding if we are not aligned to
    3372             :   // pointer size. Therefore it is possible to reach the end without ever having
    3373             :   // seen BIND_OPCODE_DONE.
    3374         190 :   if (Ptr == Opcodes.end()) {
    3375           7 :     Done = true;
    3376           7 :     return;
    3377             :   }
    3378             :   bool More = true;
    3379         166 :   while (More) {
    3380             :     // Parse next opcode and set up next loop.
    3381         254 :     const uint8_t *OpcodeStart = Ptr;
    3382         254 :     uint8_t Byte = *Ptr++;
    3383         254 :     uint8_t ImmValue = Byte & MachO::BIND_IMMEDIATE_MASK;
    3384         254 :     uint8_t Opcode = Byte & MachO::BIND_OPCODE_MASK;
    3385             :     int8_t SignExtended;
    3386             :     const uint8_t *SymStart;
    3387             :     uint32_t Count, Skip;
    3388         254 :     const char *error = nullptr;
    3389         254 :     switch (Opcode) {
    3390          14 :     case MachO::BIND_OPCODE_DONE:
    3391          14 :       if (TableKind == Kind::Lazy) {
    3392             :         // Lazying bindings have a DONE opcode between entries.  Need to ignore
    3393             :         // it to advance to next entry.  But need not if this is last entry.
    3394             :         bool NotLastEntry = false;
    3395         216 :         for (const uint8_t *P = Ptr; P < Opcodes.end(); ++P) {
    3396         105 :           if (*P) {
    3397             :             NotLastEntry = true;
    3398             :           }
    3399             :         }
    3400           6 :         if (NotLastEntry)
    3401             :           break;
    3402             :       }
    3403             :       More = false;
    3404          10 :       moveToEnd();
    3405             :       DEBUG_WITH_TYPE("mach-o-bind", dbgs() << "BIND_OPCODE_DONE\n");
    3406             :       break;
    3407          26 :     case MachO::BIND_OPCODE_SET_DYLIB_ORDINAL_IMM:
    3408          26 :       if (TableKind == Kind::Weak) {
    3409           2 :         *E = malformedError("BIND_OPCODE_SET_DYLIB_ORDINAL_IMM not allowed in "
    3410           1 :                             "weak bind table for opcode at: 0x" +
    3411           2 :                             Twine::utohexstr(OpcodeStart - Opcodes.begin()));
    3412           1 :         moveToEnd();
    3413           1 :         return;
    3414             :       }
    3415          25 :       Ordinal = ImmValue;
    3416          25 :       LibraryOrdinalSet = true;
    3417          25 :       if (ImmValue > O->getLibraryCount()) {
    3418           2 :         *E = malformedError("for BIND_OPCODE_SET_DYLIB_ORDINAL_ULEB bad "
    3419           1 :                             "library ordinal: " +
    3420           2 :                             Twine((int)ImmValue) + " (max " +
    3421           3 :                             Twine((int)O->getLibraryCount()) +
    3422           2 :                             ") for opcode at: 0x" +
    3423           2 :                             Twine::utohexstr(OpcodeStart - Opcodes.begin()));
    3424           1 :         moveToEnd();
    3425           1 :         return;
    3426             :       }
    3427             :       DEBUG_WITH_TYPE(
    3428             :           "mach-o-bind",
    3429             :           dbgs() << "BIND_OPCODE_SET_DYLIB_ORDINAL_IMM: "
    3430             :                  << "Ordinal=" << Ordinal << "\n");
    3431             :       break;
    3432          17 :     case MachO::BIND_OPCODE_SET_DYLIB_ORDINAL_ULEB:
    3433          17 :       if (TableKind == Kind::Weak) {
    3434           2 :         *E = malformedError("BIND_OPCODE_SET_DYLIB_ORDINAL_ULEB not allowed in "
    3435           1 :                             "weak bind table for opcode at: 0x" +
    3436           2 :                             Twine::utohexstr(OpcodeStart - Opcodes.begin()));
    3437           1 :         moveToEnd();
    3438           1 :         return;
    3439             :       }
    3440          16 :       Ordinal = readULEB128(&error);
    3441          16 :       LibraryOrdinalSet = true;
    3442          16 :       if (error) {
    3443           6 :         *E = malformedError("for BIND_OPCODE_SET_DYLIB_ORDINAL_ULEB " +
    3444           4 :                             Twine(error) + " for opcode at: 0x" +
    3445           4 :                             Twine::utohexstr(OpcodeStart - Opcodes.begin()));
    3446           2 :         moveToEnd();
    3447           2 :         return;
    3448             :       }
    3449          14 :       if (Ordinal > (int)O->getLibraryCount()) {
    3450           4 :         *E = malformedError("for BIND_OPCODE_SET_DYLIB_ORDINAL_ULEB bad "
    3451           2 :                             "library ordinal: " +
    3452           6 :                             Twine((int)Ordinal) + " (max " +
    3453           6 :                             Twine((int)O->getLibraryCount()) +
    3454           4 :                             ") for opcode at: 0x" +
    3455           4 :                             Twine::utohexstr(OpcodeStart - Opcodes.begin()));
    3456           2 :         moveToEnd();
    3457           2 :         return;
    3458             :       }
    3459             :       DEBUG_WITH_TYPE(
    3460             :           "mach-o-bind",
    3461             :           dbgs() << "BIND_OPCODE_SET_DYLIB_ORDINAL_ULEB: "
    3462             :                  << "Ordinal=" << Ordinal << "\n");
    3463             :       break;
    3464           5 :     case MachO::BIND_OPCODE_SET_DYLIB_SPECIAL_IMM:
    3465           5 :       if (TableKind == Kind::Weak) {
    3466           2 :         *E = malformedError("BIND_OPCODE_SET_DYLIB_SPECIAL_IMM not allowed in "
    3467           1 :                             "weak bind table for opcode at: 0x" +
    3468           2 :                             Twine::utohexstr(OpcodeStart - Opcodes.begin()));
    3469           1 :         moveToEnd();
    3470           1 :         return;
    3471             :       }
    3472           4 :       if (ImmValue) {
    3473           3 :         SignExtended = MachO::BIND_OPCODE_MASK | ImmValue;
    3474           3 :         Ordinal = SignExtended;
    3475           3 :         if (Ordinal < MachO::BIND_SPECIAL_DYLIB_FLAT_LOOKUP) {
    3476           2 :           *E = malformedError("for BIND_OPCODE_SET_DYLIB_SPECIAL_IMM unknown "
    3477           1 :                               "special ordinal: " +
    3478           2 :                               Twine((int)Ordinal) + " for opcode at: 0x" +
    3479           2 :                               Twine::utohexstr(OpcodeStart - Opcodes.begin()));
    3480           1 :           moveToEnd();
    3481           1 :           return;
    3482             :         }
    3483             :       } else
    3484           1 :         Ordinal = 0;
    3485           3 :       LibraryOrdinalSet = true;
    3486             :       DEBUG_WITH_TYPE(
    3487             :           "mach-o-bind",
    3488             :           dbgs() << "BIND_OPCODE_SET_DYLIB_SPECIAL_IMM: "
    3489             :                  << "Ordinal=" << Ordinal << "\n");
    3490           3 :       break;
    3491          57 :     case MachO::BIND_OPCODE_SET_SYMBOL_TRAILING_FLAGS_IMM:
    3492          57 :       Flags = ImmValue;
    3493             :       SymStart = Ptr;
    3494        2398 :       while (*Ptr && (Ptr < Opcodes.end())) {
    3495         780 :         ++Ptr;
    3496             :       }
    3497          57 :       if (Ptr == Opcodes.end()) {
    3498           2 :         *E = malformedError(
    3499             :             "for BIND_OPCODE_SET_SYMBOL_TRAILING_FLAGS_IMM "
    3500           1 :             "symbol name extends past opcodes for opcode at: 0x" +
    3501           2 :             Twine::utohexstr(OpcodeStart - Opcodes.begin()));
    3502           1 :         moveToEnd();
    3503           1 :         return;
    3504             :       }
    3505          56 :       SymbolName = StringRef(reinterpret_cast<const char*>(SymStart),
    3506          56 :                              Ptr-SymStart);
    3507          56 :       ++Ptr;
    3508             :       DEBUG_WITH_TYPE(
    3509             :           "mach-o-bind",
    3510             :           dbgs() << "BIND_OPCODE_SET_SYMBOL_TRAILING_FLAGS_IMM: "
    3511             :                  << "SymbolName=" << SymbolName << "\n");
    3512          56 :       if (TableKind == Kind::Weak) {
    3513           4 :         if (ImmValue & MachO::BIND_SYMBOL_FLAGS_NON_WEAK_DEFINITION)
    3514             :           return;
    3515             :       }
    3516             :       break;
    3517          19 :     case MachO::BIND_OPCODE_SET_TYPE_IMM:
    3518          19 :       BindType = ImmValue;
    3519          19 :       if (ImmValue > MachO::BIND_TYPE_TEXT_PCREL32) {
    3520           3 :         *E = malformedError("for BIND_OPCODE_SET_TYPE_IMM bad bind type: " +
    3521           3 :                             Twine((int)ImmValue) + " for opcode at: 0x" +
    3522           2 :                             Twine::utohexstr(OpcodeStart - Opcodes.begin()));
    3523           1 :         moveToEnd();
    3524           1 :         return;
    3525             :       }
    3526             :       DEBUG_WITH_TYPE(
    3527             :           "mach-o-bind",
    3528             :           dbgs() << "BIND_OPCODE_SET_TYPE_IMM: "
    3529             :                  << "BindType=" << (int)BindType << "\n");
    3530             :       break;
    3531           1 :     case MachO::BIND_OPCODE_SET_ADDEND_SLEB:
    3532           1 :       Addend = readSLEB128(&error);
    3533           1 :       if (error) {
    3534           3 :         *E = malformedError("for BIND_OPCODE_SET_ADDEND_SLEB " + Twine(error) +
    3535           2 :                             " for opcode at: 0x" +
    3536           2 :                             Twine::utohexstr(OpcodeStart - Opcodes.begin()));
    3537           1 :         moveToEnd();
    3538           1 :         return;
    3539             :       }
    3540             :       DEBUG_WITH_TYPE(
    3541             :           "mach-o-bind",
    3542             :           dbgs() << "BIND_OPCODE_SET_ADDEND_SLEB: "
    3543             :                  << "Addend=" << Addend << "\n");
    3544             :       break;
    3545          36 :     case MachO::BIND_OPCODE_SET_SEGMENT_AND_OFFSET_ULEB:
    3546          36 :       SegmentIndex = ImmValue;
    3547          36 :       SegmentOffset = readULEB128(&error);
    3548          36 :       if (error) {
    3549           0 :         *E = malformedError("for BIND_OPCODE_SET_SEGMENT_AND_OFFSET_ULEB " +
    3550           0 :                             Twine(error) + " for opcode at: 0x" +
    3551           0 :                             Twine::utohexstr(OpcodeStart - Opcodes.begin()));
    3552           0 :         moveToEnd();
    3553           0 :         return;
    3554             :       }
    3555          72 :       error = O->BindEntryCheckSegAndOffset(SegmentIndex, SegmentOffset, true);
    3556          36 :       if (error) {
    3557           6 :         *E = malformedError("for BIND_OPCODE_SET_SEGMENT_AND_OFFSET_ULEB " +
    3558           4 :                             Twine(error) + " for opcode at: 0x" +
    3559           4 :                             Twine::utohexstr(OpcodeStart - Opcodes.begin()));
    3560           2 :         moveToEnd();
    3561           2 :         return;
    3562             :       }
    3563             :       DEBUG_WITH_TYPE(
    3564             :           "mach-o-bind",
    3565             :           dbgs() << "BIND_OPCODE_SET_SEGMENT_AND_OFFSET_ULEB: "
    3566             :                  << "SegmentIndex=" << SegmentIndex << ", "
    3567             :                  << format("SegmentOffset=0x%06X", SegmentOffset)
    3568             :                  << "\n");
    3569             :       break;
    3570          18 :     case MachO::BIND_OPCODE_ADD_ADDR_ULEB:
    3571          18 :       SegmentOffset += readULEB128(&error);
    3572          18 :       if (error) {
    3573           0 :         *E = malformedError("for BIND_OPCODE_ADD_ADDR_ULEB " + Twine(error) +
    3574           0 :                             " for opcode at: 0x" +
    3575           0 :                             Twine::utohexstr(OpcodeStart - Opcodes.begin()));
    3576           0 :         moveToEnd();
    3577           0 :         return;
    3578             :       }
    3579          36 :       error = O->BindEntryCheckSegAndOffset(SegmentIndex, SegmentOffset, true);
    3580          18 :       if (error) {
    3581           3 :         *E = malformedError("for BIND_OPCODE_ADD_ADDR_ULEB " + Twine(error) +
    3582           2 :                             " for opcode at: 0x" +
    3583           2 :                             Twine::utohexstr(OpcodeStart - Opcodes.begin()));
    3584           1 :         moveToEnd();
    3585           1 :         return;
    3586             :       }
    3587             :       DEBUG_WITH_TYPE("mach-o-bind",
    3588             :                       dbgs() << "BIND_OPCODE_ADD_ADDR_ULEB: "
    3589             :                              << format("SegmentOffset=0x%06X",
    3590             :                                        SegmentOffset) << "\n");
    3591             :       break;
    3592          47 :     case MachO::BIND_OPCODE_DO_BIND:
    3593          47 :       AdvanceAmount = PointerSize;
    3594          47 :       RemainingLoopCount = 0;
    3595          94 :       error = O->BindEntryCheckSegAndOffset(SegmentIndex, SegmentOffset, true);
    3596          47 :       if (error) {
    3597           3 :         *E = malformedError("for BIND_OPCODE_DO_BIND " + Twine(error) +
    3598           2 :                             " for opcode at: 0x" +
    3599           2 :                             Twine::utohexstr(OpcodeStart - Opcodes.begin()));
    3600           1 :         moveToEnd();
    3601           1 :         return;
    3602             :       }
    3603             :       if (SymbolName == StringRef()) {
    3604           0 :         *E = malformedError(
    3605             :             "for BIND_OPCODE_DO_BIND missing preceding "
    3606           0 :             "BIND_OPCODE_SET_SYMBOL_TRAILING_FLAGS_IMM for opcode at: 0x" +
    3607           0 :             Twine::utohexstr(OpcodeStart - Opcodes.begin()));
    3608           0 :         moveToEnd();
    3609           0 :         return;
    3610             :       }
    3611          46 :       if (!LibraryOrdinalSet && TableKind != Kind::Weak) {
    3612           0 :         *E =
    3613           0 :             malformedError("for BIND_OPCODE_DO_BIND missing preceding "
    3614           0 :                            "BIND_OPCODE_SET_DYLIB_ORDINAL_* for opcode at: 0x" +
    3615           0 :                            Twine::utohexstr(OpcodeStart - Opcodes.begin()));
    3616           0 :         moveToEnd();
    3617           0 :         return;
    3618             :       }
    3619             :       DEBUG_WITH_TYPE("mach-o-bind",
    3620             :                       dbgs() << "BIND_OPCODE_DO_BIND: "
    3621             :                              << format("SegmentOffset=0x%06X",
    3622             :                                        SegmentOffset) << "\n");
    3623             :       return;
    3624           5 :      case MachO::BIND_OPCODE_DO_BIND_ADD_ADDR_ULEB:
    3625           5 :       if (TableKind == Kind::Lazy) {
    3626           2 :         *E = malformedError("BIND_OPCODE_DO_BIND_ADD_ADDR_ULEB not allowed in "
    3627           1 :                             "lazy bind table for opcode at: 0x" +
    3628           2 :                             Twine::utohexstr(OpcodeStart - Opcodes.begin()));
    3629           1 :         moveToEnd();
    3630           1 :         return;
    3631             :       }
    3632           8 :       error = O->BindEntryCheckSegAndOffset(SegmentIndex, SegmentOffset, true);
    3633           4 :       if (error) {
    3634           0 :         *E = malformedError("for BIND_OPCODE_DO_BIND_ADD_ADDR_ULEB " +
    3635           0 :                             Twine(error) + " for opcode at: 0x" +
    3636           0 :                             Twine::utohexstr(OpcodeStart - Opcodes.begin()));
    3637           0 :         moveToEnd();
    3638           0 :         return;
    3639             :       }
    3640             :       if (SymbolName == StringRef()) {
    3641           2 :         *E = malformedError(
    3642             :             "for BIND_OPCODE_DO_BIND_ADD_ADDR_ULEB missing "
    3643             :             "preceding BIND_OPCODE_SET_SYMBOL_TRAILING_FLAGS_IMM for opcode "
    3644           1 :             "at: 0x" +
    3645           2 :             Twine::utohexstr(OpcodeStart - Opcodes.begin()));
    3646           1 :         moveToEnd();
    3647           1 :         return;
    3648             :       }
    3649           3 :       if (!LibraryOrdinalSet && TableKind != Kind::Weak) {
    3650           2 :         *E = malformedError(
    3651             :             "for BIND_OPCODE_DO_BIND_ADD_ADDR_ULEB missing "
    3652           1 :             "preceding BIND_OPCODE_SET_DYLIB_ORDINAL_* for opcode at: 0x" +
    3653           2 :             Twine::utohexstr(OpcodeStart - Opcodes.begin()));
    3654           1 :         moveToEnd();
    3655           1 :         return;
    3656             :       }
    3657           2 :       AdvanceAmount = readULEB128(&error) + PointerSize;
    3658           2 :       if (error) {
    3659           0 :         *E = malformedError("for BIND_OPCODE_DO_BIND_ADD_ADDR_ULEB " +
    3660           0 :                             Twine(error) + " for opcode at: 0x" +
    3661           0 :                             Twine::utohexstr(OpcodeStart - Opcodes.begin()));
    3662           0 :         moveToEnd();
    3663           0 :         return;
    3664             :       }
    3665             :       // Note, this is not really an error until the next bind but make no sense
    3666             :       // for a BIND_OPCODE_DO_BIND_ADD_ADDR_ULEB to not be followed by another
    3667             :       // bind operation.
    3668           4 :       error = O->BindEntryCheckSegAndOffset(SegmentIndex, SegmentOffset +
    3669             :                                             AdvanceAmount, false);
    3670           2 :       if (error) {
    3671           2 :         *E = malformedError("for BIND_OPCODE_ADD_ADDR_ULEB (after adding "
    3672           1 :                             "ULEB) " +
    3673           2 :                             Twine(error) + " for opcode at: 0x" +
    3674           2 :                             Twine::utohexstr(OpcodeStart - Opcodes.begin()));
    3675           1 :         moveToEnd();
    3676           1 :         return;
    3677             :       }
    3678           1 :       RemainingLoopCount = 0;
    3679             :       DEBUG_WITH_TYPE(
    3680             :           "mach-o-bind",
    3681             :           dbgs() << "BIND_OPCODE_DO_BIND_ADD_ADDR_ULEB: "
    3682             :                  << format("SegmentOffset=0x%06X", SegmentOffset)
    3683             :                  << ", AdvanceAmount=" << AdvanceAmount
    3684             :                  << ", RemainingLoopCount=" << RemainingLoopCount
    3685             :                  << "\n");
    3686           1 :       return;
    3687           5 :     case MachO::BIND_OPCODE_DO_BIND_ADD_ADDR_IMM_SCALED:
    3688           5 :       if (TableKind == Kind::Lazy) {
    3689           2 :         *E = malformedError("BIND_OPCODE_DO_BIND_ADD_ADDR_IMM_SCALED not "
    3690           1 :                             "allowed in lazy bind table for opcode at: 0x" +
    3691           2 :                             Twine::utohexstr(OpcodeStart - Opcodes.begin()));
    3692           1 :         moveToEnd();
    3693           1 :         return;
    3694             :       }
    3695           8 :       error = O->BindEntryCheckSegAndOffset(SegmentIndex, SegmentOffset, true);
    3696           4 :       if (error) {
    3697           0 :         *E = malformedError("for BIND_OPCODE_DO_BIND_ADD_ADDR_IMM_SCALED " +
    3698           0 :                             Twine(error) + " for opcode at: 0x" +
    3699           0 :                             Twine::utohexstr(OpcodeStart - Opcodes.begin()));
    3700           0 :         moveToEnd();
    3701           0 :         return;
    3702             :       }
    3703             :       if (SymbolName == StringRef()) {
    3704           0 :         *E = malformedError(
    3705             :             "for BIND_OPCODE_DO_BIND_ADD_ADDR_IMM_SCALED "
    3706             :             "missing preceding BIND_OPCODE_SET_SYMBOL_TRAILING_FLAGS_IMM for "
    3707           0 :             "opcode at: 0x" +
    3708           0 :             Twine::utohexstr(OpcodeStart - Opcodes.begin()));
    3709           0 :         moveToEnd();
    3710           0 :         return;
    3711             :       }
    3712           4 :       if (!LibraryOrdinalSet && TableKind != Kind::Weak) {
    3713           0 :         *E = malformedError(
    3714             :             "for BIND_OPCODE_DO_BIND_ADD_ADDR_IMM_SCALED "
    3715             :             "missing preceding BIND_OPCODE_SET_DYLIB_ORDINAL_* for opcode "
    3716           0 :             "at: 0x" +
    3717           0 :             Twine::utohexstr(OpcodeStart - Opcodes.begin()));
    3718           0 :         moveToEnd();
    3719           0 :         return;
    3720             :       }
    3721           4 :       AdvanceAmount = ImmValue * PointerSize + PointerSize;
    3722           4 :       RemainingLoopCount = 0;
    3723           8 :       error = O->BindEntryCheckSegAndOffset(SegmentIndex, SegmentOffset +
    3724             :                                             AdvanceAmount, false);
    3725           4 :       if (error) {
    3726           1 :         *E =
    3727           2 :             malformedError("for BIND_OPCODE_DO_BIND_ADD_ADDR_IMM_SCALED "
    3728           1 :                            " (after adding immediate times the pointer size) " +
    3729           2 :                            Twine(error) + " for opcode at: 0x" +
    3730           2 :                            Twine::utohexstr(OpcodeStart - Opcodes.begin()));
    3731           1 :         moveToEnd();
    3732           1 :         return;
    3733             :       }
    3734             :       DEBUG_WITH_TYPE("mach-o-bind",
    3735             :                       dbgs()
    3736             :                       << "BIND_OPCODE_DO_BIND_ADD_ADDR_IMM_SCALED: "
    3737             :                       << format("SegmentOffset=0x%06X", SegmentOffset) << "\n");
    3738             :       return;
    3739           3 :     case MachO::BIND_OPCODE_DO_BIND_ULEB_TIMES_SKIPPING_ULEB:
    3740           3 :       if (TableKind == Kind::Lazy) {
    3741           2 :         *E = malformedError("BIND_OPCODE_DO_BIND_ULEB_TIMES_SKIPPING_ULEB not "
    3742           1 :                             "allowed in lazy bind table for opcode at: 0x" +
    3743           2 :                             Twine::utohexstr(OpcodeStart - Opcodes.begin()));
    3744           1 :         moveToEnd();
    3745           1 :         return;
    3746             :       }
    3747           2 :       Count = readULEB128(&error);
    3748           2 :       if (Count != 0)
    3749           2 :         RemainingLoopCount = Count - 1;
    3750             :       else
    3751           0 :         RemainingLoopCount = 0;
    3752           2 :       if (error) {
    3753           0 :         *E = malformedError("for BIND_OPCODE_DO_BIND_ULEB_TIMES_SKIPPING_ULEB "
    3754           0 :                             " (count value) " +
    3755           0 :                             Twine(error) + " for opcode at: 0x" +
    3756           0 :                             Twine::utohexstr(OpcodeStart - Opcodes.begin()));
    3757           0 :         moveToEnd();
    3758           0 :         return;
    3759             :       }
    3760           2 :       Skip = readULEB128(&error);
    3761           2 :       AdvanceAmount = Skip + PointerSize;
    3762           2 :       if (error) {
    3763           0 :         *E = malformedError("for BIND_OPCODE_DO_BIND_ULEB_TIMES_SKIPPING_ULEB "
    3764           0 :                             " (skip value) " +
    3765           0 :                             Twine(error) + " for opcode at: 0x" +
    3766           0 :                             Twine::utohexstr(OpcodeStart - Opcodes.begin()));
    3767           0 :         moveToEnd();
    3768           0 :         return;
    3769             :       }
    3770           4 :       error = O->BindEntryCheckSegAndOffset(SegmentIndex, SegmentOffset, true);
    3771           2 :       if (error) {
    3772           0 :         *E =
    3773           0 :             malformedError("for BIND_OPCODE_DO_BIND_ULEB_TIMES_SKIPPING_ULEB " +
    3774           0 :                            Twine(error) + " for opcode at: 0x" +
    3775           0 :                            Twine::utohexstr(OpcodeStart - Opcodes.begin()));
    3776           0 :         moveToEnd();
    3777           0 :         return;
    3778             :       }
    3779             :       if (SymbolName == StringRef()) {
    3780           0 :         *E = malformedError(
    3781             :             "for BIND_OPCODE_DO_BIND_ULEB_TIMES_SKIPPING_ULEB "
    3782             :             "missing preceding BIND_OPCODE_SET_SYMBOL_TRAILING_FLAGS_IMM for "
    3783           0 :             "opcode at: 0x" +
    3784           0 :             Twine::utohexstr(OpcodeStart - Opcodes.begin()));
    3785           0 :         moveToEnd();
    3786           0 :         return;
    3787             :       }
    3788           2 :       if (!LibraryOrdinalSet && TableKind != Kind::Weak) {
    3789           0 :         *E = malformedError(
    3790             :             "for BIND_OPCODE_DO_BIND_ULEB_TIMES_SKIPPING_ULEB "
    3791             :             "missing preceding BIND_OPCODE_SET_DYLIB_ORDINAL_* for opcode "
    3792           0 :             "at: 0x" +
    3793           0 :             Twine::utohexstr(OpcodeStart - Opcodes.begin()));
    3794           0 :         moveToEnd();
    3795           0 :         return;
    3796             :       }
    3797           4 :       error = O->BindEntryCheckCountAndSkip(Count, Skip, PointerSize,
    3798             :                                             SegmentIndex, SegmentOffset);
    3799           2 :       if (error) {
    3800           1 :         *E =
    3801           3 :             malformedError("for BIND_OPCODE_DO_BIND_ULEB_TIMES_SKIPPING_ULEB " +
    3802           2 :                            Twine(error) + " for opcode at: 0x" +
    3803           2 :                            Twine::utohexstr(OpcodeStart - Opcodes.begin()));
    3804           1 :         moveToEnd();
    3805           1 :         return;
    3806             :       }
    3807             :       DEBUG_WITH_TYPE(
    3808             :           "mach-o-bind",
    3809             :           dbgs() << "BIND_OPCODE_DO_BIND_ULEB_TIMES_SKIPPING_ULEB: "
    3810             :                  << format("SegmentOffset=0x%06X", SegmentOffset)
    3811             :                  << ", AdvanceAmount=" << AdvanceAmount
    3812             :                  << ", RemainingLoopCount=" << RemainingLoopCount
    3813             :                  << "\n");
    3814             :       return;
    3815           1 :     default:
    3816           3 :       *E = malformedError("bad bind info (bad opcode value 0x" +
    3817           3 :                           Twine::utohexstr(Opcode) + " for opcode at: 0x" +
    3818           2 :                           Twine::utohexstr(OpcodeStart - Opcodes.begin()));
    3819           1 :       moveToEnd();
    3820           1 :       return;
    3821             :     }
    3822             :   }
    3823             : }
    3824             : 
    3825          76 : uint64_t MachOBindEntry::readULEB128(const char **error) {
    3826             :   unsigned Count;
    3827          76 :   uint64_t Result = decodeULEB128(Ptr, &Count, Opcodes.end(), error);
    3828          76 :   Ptr += Count;
    3829          76 :   if (Ptr > Opcodes.end())
    3830           0 :     Ptr = Opcodes.end();
    3831          76 :   return Result;
    3832             : }
    3833             : 
    3834           1 : int64_t MachOBindEntry::readSLEB128(const char **error) {
    3835             :   unsigned Count;
    3836           1 :   int64_t Result = decodeSLEB128(Ptr, &Count, Opcodes.end(), error);
    3837           1 :   Ptr += Count;
    3838           1 :   if (Ptr > Opcodes.end())
    3839           0 :     Ptr = Opcodes.end();
    3840           1 :   return Result;
    3841             : }
    3842             : 
    3843           0 : int32_t MachOBindEntry::segmentIndex() const { return SegmentIndex; }
    3844             : 
    3845           0 : uint64_t MachOBindEntry::segmentOffset() const { return SegmentOffset; }
    3846             : 
    3847          21 : StringRef MachOBindEntry::typeName() const {
    3848          21 :   switch (BindType) {
    3849             :   case MachO::BIND_TYPE_POINTER:
    3850          21 :     return "pointer";
    3851             :   case MachO::BIND_TYPE_TEXT_ABSOLUTE32:
    3852           0 :     return "text abs32";
    3853             :   case MachO::BIND_TYPE_TEXT_PCREL32:
    3854           0 :     return "text rel32";
    3855             :   }
    3856           0 :   return "unknown";
    3857             : }
    3858             : 
    3859          55 : StringRef MachOBindEntry::symbolName() const { return SymbolName; }
    3860             : 
    3861          21 : int64_t MachOBindEntry::addend() const { return Addend; }
    3862             : 
    3863          23 : uint32_t MachOBindEntry::flags() const { return Flags; }
    3864             : 
    3865          23 : int MachOBindEntry::ordinal() const { return Ordinal; }
    3866             : 
    3867             : // For use with the SegIndex of a checked Mach-O Bind entry
    3868             : // to get the segment name.
    3869          27 : StringRef MachOBindEntry::segmentName() const {
    3870          54 :   return O->BindRebaseSegmentName(SegmentIndex);
    3871             : }
    3872             : 
    3873             : // For use with a SegIndex,SegOffset pair from a checked Mach-O Bind entry
    3874             : // to get the section name.
    3875          27 : StringRef MachOBindEntry::sectionName() const {
    3876          54 :   return O->BindRebaseSectionName(SegmentIndex, SegmentOffset);
    3877             : }
    3878             : 
    3879             : // For use with a SegIndex,SegOffset pair from a checked Mach-O Bind entry
    3880             : // to get the address.
    3881          53 : uint64_t MachOBindEntry::address() const {
    3882         106 :   return O->BindRebaseAddress(SegmentIndex, SegmentOffset);
    3883             : }
    3884             : 
    3885          97 : bool MachOBindEntry::operator==(const MachOBindEntry &Other) const {
    3886             : #ifdef EXPENSIVE_CHECKS
    3887             :   assert(Opcodes == Other.Opcodes && "compare iterators of different files");
    3888             : #else
    3889             :   assert(Opcodes.data() == Other.Opcodes.data() && "compare iterators of different files");
    3890             : #endif
    3891         139 :   return (Ptr == Other.Ptr) &&
    3892         139 :          (RemainingLoopCount == Other.RemainingLoopCount) &&
    3893         139 :          (Done == Other.Done);
    3894             : }
    3895             : 
    3896             : // Build table of sections so SegIndex/SegOffset pairs can be translated.
    3897          50 : BindRebaseSegInfo::BindRebaseSegInfo(const object::MachOObjectFile *Obj) {
    3898          50 :   uint32_t CurSegIndex = Obj->hasPageZeroSegment() ? 1 : 0;
    3899             :   StringRef CurSegName;
    3900             :   uint64_t CurSegAddress;
    3901         593 :   for (const SectionRef &Section : Obj->sections()) {
    3902             :     SectionInfo Info;
    3903             :     Section.getName(Info.SectionName);
    3904         393 :     Info.Address = Section.getAddress();
    3905         393 :     Info.Size = Section.getSize();
    3906         393 :     Info.SegmentName =
    3907             :         Obj->getSectionFinalSegmentName(Section.getRawDataRefImpl());
    3908             :     if (!Info.SegmentName.equals(CurSegName)) {
    3909         106 :       ++CurSegIndex;
    3910         106 :       CurSegName = Info.SegmentName;
    3911         106 :       CurSegAddress = Info.Address;
    3912             :     }
    3913         393 :     Info.SegmentIndex = CurSegIndex - 1;
    3914         393 :     Info.OffsetInSegment = Info.Address - CurSegAddress;
    3915         393 :     Info.SegmentStartAddress = CurSegAddress;
    3916         393 :     Sections.push_back(Info);
    3917             :   }
    3918          50 :   MaxSegIndex = CurSegIndex;
    3919          50 : }
    3920             : 
    3921             : // For use with a SegIndex,SegOffset pair in MachOBindEntry::moveNext() to
    3922             : // validate a MachOBindEntry or MachORebaseEntry.
    3923         148 : const char * BindRebaseSegInfo::checkSegAndOffset(int32_t SegIndex,
    3924             :                                                   uint64_t SegOffset,
    3925             :                                                   bool endInvalid) {
    3926         148 :   if (SegIndex == -1)
    3927             :     return "missing preceding *_OPCODE_SET_SEGMENT_AND_OFFSET_ULEB";
    3928         147 :   if (SegIndex >= MaxSegIndex)
    3929             :     return "bad segIndex (too large)";
    3930        2185 :   for (const SectionInfo &SI : Sections) {
    3931        1153 :     if (SI.SegmentIndex != SegIndex)
    3932         776 :       continue;
    3933         377 :     if (SI.OffsetInSegment > SegOffset)
    3934           0 :       continue;
    3935         377 :     if (SegOffset > (SI.OffsetInSegment + SI.Size))
    3936         208 :       continue;
    3937         169 :     if (endInvalid && SegOffset >= (SI.OffsetInSegment + SI.Size))
    3938          36 :       continue;
    3939             :     return nullptr;
    3940             :   }
    3941             :   return "bad segOffset, too large";
    3942             : }
    3943             : 
    3944             : // For use in MachOBindEntry::moveNext() to validate a MachOBindEntry for
    3945             : // the BIND_OPCODE_DO_BIND_ULEB_TIMES_SKIPPING_ULEB opcode and for use in
    3946             : // MachORebaseEntry::moveNext() to validate a MachORebaseEntry for
    3947             : // REBASE_OPCODE_DO_*_TIMES* opcodes.  The SegIndex and SegOffset must have
    3948             : // been already checked.
    3949          12 : const char * BindRebaseSegInfo::checkCountAndSkip(uint32_t Count, uint32_t Skip,
    3950             :                                                   uint8_t PointerSize,
    3951             :                                                   int32_t SegIndex,
    3952             :                                                   uint64_t SegOffset) {
    3953          12 :   const SectionInfo &SI = findSection(SegIndex, SegOffset);
    3954          12 :   uint64_t addr = SI.SegmentStartAddress + SegOffset;
    3955          12 :   if (addr >= SI.Address + SI.Size)
    3956             :     return "bad segOffset, too large";
    3957             :   uint64_t i = 0;
    3958          12 :   if (Count > 1)
    3959           7 :     i = (Skip + PointerSize) * (Count - 1);
    3960           5 :   else if (Count == 1)
    3961           5 :     i = Skip + PointerSize;
    3962          12 :   if (addr + i >= SI.Address + SI.Size) {
    3963             :     // For rebase opcodes they can step from one section to another.
    3964           7 :     uint64_t TrailingSegOffset = (addr + i) - SI.SegmentStartAddress;
    3965           7 :     const char *error = checkSegAndOffset(SegIndex, TrailingSegOffset, false);
    3966           7 :     if (error)
    3967             :       return "bad count and skip, too large";
    3968             :   }
    3969             :   return nullptr;
    3970             : }
    3971             : 
    3972             : // For use with the SegIndex of a checked Mach-O Bind or Rebase entry
    3973             : // to get the segment name.
    3974          38 : StringRef BindRebaseSegInfo::segmentName(int32_t SegIndex) {
    3975         256 :   for (const SectionInfo &SI : Sections) {
    3976         147 :     if (SI.SegmentIndex == SegIndex)
    3977          38 :       return SI.SegmentName;
    3978             :   }
    3979           0 :   llvm_unreachable("invalid SegIndex");
    3980             : }
    3981             : 
    3982             : // For use with a SegIndex,SegOffset pair from a checked Mach-O Bind or Rebase
    3983             : // to get the SectionInfo.
    3984         114 : const BindRebaseSegInfo::SectionInfo &BindRebaseSegInfo::findSection(
    3985             :                                      int32_t SegIndex, uint64_t SegOffset) {
    3986        1410 :   for (const SectionInfo &SI : Sections) {
    3987         762 :     if (SI.SegmentIndex != SegIndex)
    3988         484 :       continue;
    3989         278 :     if (SI.OffsetInSegment > SegOffset)
    3990           0 :       continue;
    3991         278 :     if (SegOffset >= (SI.OffsetInSegment + SI.Size))
    3992         164 :       continue;
    3993         114 :     return SI;
    3994             :   }
    3995           0 :   llvm_unreachable("SegIndex and SegOffset not in any section");
    3996             : }
    3997             : 
    3998             : // For use with a SegIndex,SegOffset pair from a checked Mach-O Bind or Rebase
    3999             : // entry to get the section name.
    4000          38 : StringRef BindRebaseSegInfo::sectionName(int32_t SegIndex,
    4001             :                                          uint64_t SegOffset) {
    4002          38 :   return findSection(SegIndex, SegOffset).SectionName;
    4003             : }
    4004             : 
    4005             : // For use with a SegIndex,SegOffset pair from a checked Mach-O Bind or Rebase
    4006             : // entry to get the address.
    4007          64 : uint64_t BindRebaseSegInfo::address(uint32_t SegIndex, uint64_t OffsetInSeg) {
    4008          64 :   const SectionInfo &SI = findSection(SegIndex, OffsetInSeg);
    4009          64 :   return SI.SegmentStartAddress + OffsetInSeg;
    4010             : }
    4011             : 
    4012             : iterator_range<bind_iterator>
    4013          42 : MachOObjectFile::bindTable(Error &Err, MachOObjectFile *O,
    4014             :                            ArrayRef<uint8_t> Opcodes, bool is64,
    4015             :                            MachOBindEntry::Kind BKind) {
    4016          42 :   if (O->BindRebaseSectionTable == nullptr)
    4017             :     O->BindRebaseSectionTable = llvm::make_unique<BindRebaseSegInfo>(O);
    4018          42 :   MachOBindEntry Start(&Err, O, Opcodes, is64, BKind);
    4019          42 :   Start.moveToFirst();
    4020             : 
    4021          42 :   MachOBindEntry Finish(&Err, O, Opcodes, is64, BKind);
    4022          42 :   Finish.moveToEnd();
    4023             : 
    4024          42 :   return make_range(bind_iterator(Start), bind_iterator(Finish));
    4025             : }
    4026             : 
    4027          28 : iterator_range<bind_iterator> MachOObjectFile::bindTable(Error &Err) {
    4028          28 :   return bindTable(Err, this, getDyldInfoBindOpcodes(), is64Bit(),
    4029          28 :                    MachOBindEntry::Kind::Regular);
    4030             : }
    4031             : 
    4032           8 : iterator_range<bind_iterator> MachOObjectFile::lazyBindTable(Error &Err) {
    4033           8 :   return bindTable(Err, this, getDyldInfoLazyBindOpcodes(), is64Bit(),
    4034           8 :                    MachOBindEntry::Kind::Lazy);
    4035             : }
    4036             : 
    4037           6 : iterator_range<bind_iterator> MachOObjectFile::weakBindTable(Error &Err) {
    4038           6 :   return bindTable(Err, this, getDyldInfoWeakBindOpcodes(), is64Bit(),
    4039           6 :                    MachOBindEntry::Kind::Weak);
    4040             : }
    4041             : 
    4042             : MachOObjectFile::load_command_iterator
    4043         870 : MachOObjectFile::begin_load_commands() const {
    4044         870 :   return LoadCommands.begin();
    4045             : }
    4046             : 
    4047             : MachOObjectFile::load_command_iterator
    4048         870 : MachOObjectFile::end_load_commands() const {
    4049         870 :   return LoadCommands.end();
    4050             : }
    4051             : 
    4052             : iterator_range<MachOObjectFile::load_command_iterator>
    4053         870 : MachOObjectFile::load_commands() const {
    4054         870 :   return make_range(begin_load_commands(), end_load_commands());
    4055             : }
    4056             : 
    4057             : StringRef
    4058        6063 : MachOObjectFile::getSectionFinalSegmentName(DataRefImpl Sec) const {
    4059        6063 :   ArrayRef<char> Raw = getSectionRawFinalSegmentName(Sec);
    4060       12126 :   return parseSegmentOrSectionName(Raw.data());
    4061             : }
    4062             : 
    4063             : ArrayRef<char>
    4064       27666 : MachOObjectFile::getSectionRawName(DataRefImpl Sec) const {
    4065             :   assert(Sec.d.a < Sections.size() && "Should have detected this earlier");
    4066             :   const section_base *Base =
    4067       55332 :     reinterpret_cast<const section_base *>(Sections[Sec.d.a]);
    4068       27666 :   return makeArrayRef(Base->sectname);
    4069             : }
    4070             : 
    4071             : ArrayRef<char>
    4072        6511 : MachOObjectFile::getSectionRawFinalSegmentName(DataRefImpl Sec) const {
    4073             :   assert(Sec.d.a < Sections.size() && "Should have detected this earlier");
    4074             :   const section_base *Base =
    4075       13022 :     reinterpret_cast<const section_base *>(Sections[Sec.d.a]);
    4076        6511 :   return makeArrayRef(Base->segname);
    4077             : }
    4078             : 
    4079             : bool
    4080       10331 : MachOObjectFile::isRelocationScattered(const MachO::any_relocation_info &RE)
    4081             :   const {
    4082       10331 :   if (getCPUType(*this) == MachO::CPU_TYPE_X86_64)
    4083             :     return false;
    4084        2972 :   return getPlainRelocationAddress(RE) & MachO::R_SCATTERED;
    4085             : }
    4086             : 
    4087        1588 : unsigned MachOObjectFile::getPlainRelocationSymbolNum(
    4088             :     const MachO::any_relocation_info &RE) const {
    4089        1588 :   if (isLittleEndian())
    4090        1568 :     return RE.r_word1 & 0xffffff;
    4091          20 :   return RE.r_word1 >> 8;
    4092             : }
    4093             : 
    4094        2151 : bool MachOObjectFile::getPlainRelocationExternal(
    4095             :     const MachO::any_relocation_info &RE) const {
    4096        2151 :   if (isLittleEndian())
    4097        2131 :     return (RE.r_word1 >> 27) & 1;
    4098          20 :   return (RE.r_word1 >> 4) & 1;
    4099             : }
    4100             : 
    4101           0 : bool MachOObjectFile::getScatteredRelocationScattered(
    4102             :     const MachO::any_relocation_info &RE) const {
    4103           0 :   return RE.r_word0 >> 31;
    4104             : }
    4105             : 
    4106         120 : uint32_t MachOObjectFile::getScatteredRelocationValue(
    4107             :     const MachO::any_relocation_info &RE) const {
    4108         120 :   return RE.r_word1;
    4109             : }
    4110             : 
    4111         173 : uint32_t MachOObjectFile::getScatteredRelocationType(
    4112             :     const MachO::any_relocation_info &RE) const {
    4113         173 :   return (RE.r_word0 >> 24) & 0xf;
    4114             : }
    4115             : 
    4116        3817 : unsigned MachOObjectFile::getAnyRelocationAddress(
    4117             :     const MachO::any_relocation_info &RE) const {
    4118        3817 :   if (isRelocationScattered(RE))
    4119         328 :     return getScatteredRelocationAddress(RE);
    4120        3653 :   return getPlainRelocationAddress(RE);
    4121             : }
    4122             : 
    4123         548 : unsigned MachOObjectFile::getAnyRelocationPCRel(
    4124             :     const MachO::any_relocation_info &RE) const {
    4125         548 :   if (isRelocationScattered(RE))
    4126         200 :     return getScatteredRelocationPCRel(RE);
    4127         896 :   return getPlainRelocationPCRel(*this, RE);
    4128             : }
    4129             : 
    4130        1353 : unsigned MachOObjectFile::getAnyRelocationLength(
    4131             :     const MachO::any_relocation_info &RE) const {
    4132        1353 :   if (isRelocationScattered(RE))
    4133         214 :     return getScatteredRelocationLength(RE);
    4134        1246 :   return getPlainRelocationLength(*this, RE);
    4135             : }
    4136             : 
    4137             : unsigned
    4138        1797 : MachOObjectFile::getAnyRelocationType(
    4139             :                                    const MachO::any_relocation_info &RE) const {
    4140        1797 :   if (isRelocationScattered(RE))
    4141         167 :     return getScatteredRelocationType(RE);
    4142        1630 :   return getPlainRelocationType(*this, RE);
    4143             : }
    4144             : 
    4145             : SectionRef
    4146         169 : MachOObjectFile::getAnyRelocationSection(
    4147             :                                    const MachO::any_relocation_info &RE) const {
    4148         169 :   if (isRelocationScattered(RE) || getPlainRelocationExternal(RE))
    4149           0 :     return *section_end();
    4150         169 :   unsigned SecNum = getPlainRelocationSymbolNum(RE);
    4151         337 :   if (SecNum == MachO::R_ABS || SecNum > Sections.size())
    4152          27 :     return *section_end();
    4153             :   DataRefImpl DRI;
    4154         142 :   DRI.d.a = SecNum - 1;
    4155         284 :   return SectionRef(DRI, this);
    4156             : }
    4157             : 
    4158       15168 : MachO::section MachOObjectFile::getSection(DataRefImpl DRI) const {
    4159             :   assert(DRI.d.a < Sections.size() && "Should have detected this earlier");
    4160       30336 :   return getStruct<MachO::section>(*this, Sections[DRI.d.a]);
    4161             : }
    4162             : 
    4163       32457 : MachO::section_64 MachOObjectFile::getSection64(DataRefImpl DRI) const {
    4164             :   assert(DRI.d.a < Sections.size() && "Should have detected this earlier");
    4165       64914 :   return getStruct<MachO::section_64>(*this, Sections[DRI.d.a]);
    4166             : }
    4167             : 
    4168          85 : MachO::section MachOObjectFile::getSection(const LoadCommandInfo &L,
    4169             :                                            unsigned Index) const {
    4170          85 :   const char *Sec = getSectionPtr(*this, L, Index);
    4171          85 :   return getStruct<MachO::section>(*this, Sec);
    4172             : }
    4173             : 
    4174        1541 : MachO::section_64 MachOObjectFile::getSection64(const LoadCommandInfo &L,
    4175             :                                                 unsigned Index) const {
    4176        1541 :   const char *Sec = getSectionPtr(*this, L, Index);
    4177        1541 :   return getStruct<MachO::section_64>(*this, Sec);
    4178             : }
    4179             : 
    4180             : MachO::nlist
    4181        1768 : MachOObjectFile::getSymbolTableEntry(DataRefImpl DRI) const {
    4182        1768 :   const char *P = reinterpret_cast<const char *>(DRI.p);
    4183        1768 :   return getStruct<MachO::nlist>(*this, P);
    4184             : }
    4185             : 
    4186             : MachO::nlist_64
    4187        7701 : MachOObjectFile::getSymbol64TableEntry(DataRefImpl DRI) const {
    4188        7701 :   const char *P = reinterpret_cast<const char *>(DRI.p);
    4189        7701 :   return getStruct<MachO::nlist_64>(*this, P);
    4190             : }
    4191             : 
    4192             : MachO::linkedit_data_command
    4193         129 : MachOObjectFile::getLinkeditDataLoadCommand(const LoadCommandInfo &L) const {
    4194         129 :   return getStruct<MachO::linkedit_data_command>(*this, L.Ptr);
    4195             : }
    4196             : 
    4197             : MachO::segment_command
    4198         170 : MachOObjectFile::getSegmentLoadCommand(const LoadCommandInfo &L) const {
    4199         170 :   return getStruct<MachO::segment_command>(*this, L.Ptr);
    4200             : }
    4201             : 
    4202             : MachO::segment_command_64
    4203        1339 : MachOObjectFile::getSegment64LoadCommand(const LoadCommandInfo &L) const {
    4204        1339 :   return getStruct<MachO::segment_command_64>(*this, L.Ptr);
    4205             : }
    4206             : 
    4207             : MachO::linker_option_command
    4208           7 : MachOObjectFile::getLinkerOptionLoadCommand(const LoadCommandInfo &L) const {
    4209           7 :   return getStruct<MachO::linker_option_command>(*this, L.Ptr);
    4210             : }
    4211             : 
    4212             : MachO::version_min_command
    4213          55 : MachOObjectFile::getVersionMinLoadCommand(const LoadCommandInfo &L) const {
    4214          55 :   return getStruct<MachO::version_min_command>(*this, L.Ptr);
    4215             : }
    4216             : 
    4217             : MachO::note_command
    4218           1 : MachOObjectFile::getNoteLoadCommand(const LoadCommandInfo &L) const {
    4219           1 :   return getStruct<MachO::note_command>(*this, L.Ptr);
    4220             : }
    4221             : 
    4222             : MachO::build_version_command
    4223           1 : MachOObjectFile::getBuildVersionLoadCommand(const LoadCommandInfo &L) const {
    4224           1 :   return getStruct<MachO::build_version_command>(*this, L.Ptr);
    4225             : }
    4226             : 
    4227             : MachO::build_tool_version
    4228           0 : MachOObjectFile::getBuildToolVersion(unsigned index) const {
    4229           0 :   return getStruct<MachO::build_tool_version>(*this, BuildTools[index]);
    4230             : }
    4231             : 
    4232             : MachO::dylib_command
    4233          93 : MachOObjectFile::getDylibIDLoadCommand(const LoadCommandInfo &L) const {
    4234          93 :   return getStruct<MachO::dylib_command>(*this, L.Ptr);
    4235             : }
    4236             : 
    4237             : MachO::dyld_info_command
    4238          51 : MachOObjectFile::getDyldInfoLoadCommand(const LoadCommandInfo &L) const {
    4239          51 :   return getStruct<MachO::dyld_info_command>(*this, L.Ptr);
    4240             : }
    4241             : 
    4242             : MachO::dylinker_command
    4243          30 : MachOObjectFile::getDylinkerCommand(const LoadCommandInfo &L) const {
    4244          30 :   return getStruct<MachO::dylinker_command>(*this, L.Ptr);
    4245             : }
    4246             : 
    4247             : MachO::uuid_command
    4248          60 : MachOObjectFile::getUuidCommand(const LoadCommandInfo &L) const {
    4249          60 :   return getStruct<MachO::uuid_command>(*this, L.Ptr);
    4250             : }
    4251             : 
    4252             : MachO::rpath_command
    4253           2 : MachOObjectFile::getRpathCommand(const LoadCommandInfo &L) const {
    4254           2 :   return getStruct<MachO::rpath_command>(*this, L.Ptr);
    4255             : }
    4256             : 
    4257             : MachO::source_version_command
    4258          51 : MachOObjectFile::getSourceVersionCommand(const LoadCommandInfo &L) const {
    4259          51 :   return getStruct<MachO::source_version_command>(*this, L.Ptr);
    4260             : }
    4261             : 
    4262             : MachO::entry_point_command
    4263          28 : MachOObjectFile::getEntryPointCommand(const LoadCommandInfo &L) const {
    4264          28 :   return getStruct<MachO::entry_point_command>(*this, L.Ptr);
    4265             : }
    4266             : 
    4267             : MachO::encryption_info_command
    4268           1 : MachOObjectFile::getEncryptionInfoCommand(const LoadCommandInfo &L) const {
    4269           1 :   return getStruct<MachO::encryption_info_command>(*this, L.Ptr);
    4270             : }
    4271             : 
    4272             : MachO::encryption_info_command_64
    4273           1 : MachOObjectFile::getEncryptionInfoCommand64(const LoadCommandInfo &L) const {
    4274           1 :   return getStruct<MachO::encryption_info_command_64>(*this, L.Ptr);
    4275             : }
    4276             : 
    4277             : MachO::sub_framework_command
    4278           3 : MachOObjectFile::getSubFrameworkCommand(const LoadCommandInfo &L) const {
    4279           3 :   return getStruct<MachO::sub_framework_command>(*this, L.Ptr);
    4280             : }
    4281             : 
    4282             : MachO::sub_umbrella_command
    4283           1 : MachOObjectFile::getSubUmbrellaCommand(const LoadCommandInfo &L) const {
    4284           1 :   return getStruct<MachO::sub_umbrella_command>(*this, L.Ptr);
    4285             : }
    4286             : 
    4287             : MachO::sub_library_command
    4288           1 : MachOObjectFile::getSubLibraryCommand(const LoadCommandInfo &L) const {
    4289           1 :   return getStruct<MachO::sub_library_command>(*this, L.Ptr);
    4290             : }
    4291             : 
    4292             : MachO::sub_client_command
    4293           1 : MachOObjectFile::getSubClientCommand(const LoadCommandInfo &L) const {
    4294           1 :   return getStruct<MachO::sub_client_command>(*this, L.Ptr);
    4295             : }
    4296             : 
    4297             : MachO::routines_command
    4298           0 : MachOObjectFile::getRoutinesCommand(const LoadCommandInfo &L) const {
    4299           0 :   return getStruct<MachO::routines_command>(*this, L.Ptr);
    4300             : }
    4301             : 
    4302             : MachO::routines_command_64
    4303           1 : MachOObjectFile::getRoutinesCommand64(const LoadCommandInfo &L) const {
    4304           1 :   return getStruct<MachO::routines_command_64>(*this, L.Ptr);
    4305             : }
    4306             : 
    4307             : MachO::thread_command
    4308           4 : MachOObjectFile::getThreadCommand(const LoadCommandInfo &L) const {
    4309           4 :   return getStruct<MachO::thread_command>(*this, L.Ptr);
    4310             : }
    4311             : 
    4312             : MachO::any_relocation_info
    4313        7277 : MachOObjectFile::getRelocation(DataRefImpl Rel) const {
    4314             :   uint32_t Offset;
    4315        7277 :   if (getHeader().filetype == MachO::MH_OBJECT) {
    4316             :     DataRefImpl Sec;
    4317        7273 :     Sec.d.a = Rel.d.a;
    4318        7273 :     if (is64Bit()) {
    4319        5870 :       MachO::section_64 Sect = getSection64(Sec);
    4320        5870 :       Offset = Sect.reloff;
    4321             :     } else {
    4322        1403 :       MachO::section Sect = getSection(Sec);
    4323        1403 :       Offset = Sect.reloff;
    4324             :     }
    4325             :   } else {
    4326           4 :     MachO::dysymtab_command DysymtabLoadCmd = getDysymtabLoadCommand();
    4327           4 :     if (Rel.d.a == 0)
    4328           3 :       Offset = DysymtabLoadCmd.extreloff; // Offset to the external relocations
    4329             :     else
    4330           1 :       Offset = DysymtabLoadCmd.locreloff; // Offset to the local relocations
    4331             :   }
    4332             : 
    4333             :   auto P = reinterpret_cast<const MachO::any_relocation_info *>(
    4334        7277 :       getPtr(*this, Offset)) + Rel.d.b;
    4335             :   return getStruct<MachO::any_relocation_info>(
    4336        7277 :       *this, reinterpret_cast<const char *>(P));
    4337             : }
    4338             : 
    4339             : MachO::data_in_code_entry
    4340          62 : MachOObjectFile::getDice(DataRefImpl Rel) const {
    4341          62 :   const char *P = reinterpret_cast<const char *>(Rel.p);
    4342          62 :   return getStruct<MachO::data_in_code_entry>(*this, P);
    4343             : }
    4344             : 
    4345      204686 : const MachO::mach_header &MachOObjectFile::getHeader() const {
    4346      204686 :   return Header;
    4347             : }
    4348             : 
    4349         829 : const MachO::mach_header_64 &MachOObjectFile::getHeader64() const {
    4350             :   assert(is64Bit());
    4351         829 :   return Header64;
    4352             : }
    4353             : 
    4354          86 : uint32_t MachOObjectFile::getIndirectSymbolTableEntry(
    4355             :                                              const MachO::dysymtab_command &DLC,
    4356             :                                              unsigned Index) const {
    4357          86 :   uint64_t Offset = DLC.indirectsymoff + Index * sizeof(uint32_t);
    4358          86 :   return getStruct<uint32_t>(*this, getPtr(*this, Offset));
    4359             : }
    4360             : 
    4361             : MachO::data_in_code_entry
    4362          16 : MachOObjectFile::getDataInCodeTableEntry(uint32_t DataOffset,
    4363             :                                          unsigned Index) const {
    4364          16 :   uint64_t Offset = DataOffset + Index * sizeof(MachO::data_in_code_entry);
    4365          16 :   return getStruct<MachO::data_in_code_entry>(*this, getPtr(*this, Offset));
    4366             : }
    4367             : 
    4368        9876 : MachO::symtab_command MachOObjectFile::getSymtabLoadCommand() const {
    4369        9876 :   if (SymtabLoadCmd)
    4370        9733 :     return getStruct<MachO::symtab_command>(*this, SymtabLoadCmd);
    4371             : 
    4372             :   // If there is no SymtabLoadCmd return a load command with zero'ed fields.
    4373             :   MachO::symtab_command Cmd;
    4374             :   Cmd.cmd = MachO::LC_SYMTAB;
    4375             :   Cmd.cmdsize = sizeof(MachO::symtab_command);
    4376             :   Cmd.symoff = 0;
    4377             :   Cmd.nsyms = 0;
    4378             :   Cmd.stroff = 0;
    4379             :   Cmd.strsize = 0;
    4380         143 :   return Cmd;
    4381             : }
    4382             : 
    4383        1034 : MachO::dysymtab_command MachOObjectFile::getDysymtabLoadCommand() const {
    4384        1034 :   if (DysymtabLoadCmd)
    4385        1028 :     return getStruct<MachO::dysymtab_command>(*this, DysymtabLoadCmd);
    4386             : 
    4387             :   // If there is no DysymtabLoadCmd return a load command with zero'ed fields.
    4388             :   MachO::dysymtab_command Cmd;
    4389             :   Cmd.cmd = MachO::LC_DYSYMTAB;
    4390             :   Cmd.cmdsize = sizeof(MachO::dysymtab_command);
    4391             :   Cmd.ilocalsym = 0;
    4392             :   Cmd.nlocalsym = 0;
    4393             :   Cmd.iextdefsym = 0;
    4394             :   Cmd.nextdefsym = 0;
    4395             :   Cmd.iundefsym = 0;
    4396             :   Cmd.nundefsym = 0;
    4397             :   Cmd.tocoff = 0;
    4398             :   Cmd.ntoc = 0;
    4399             :   Cmd.modtaboff = 0;
    4400             :   Cmd.nmodtab = 0;
    4401             :   Cmd.extrefsymoff = 0;
    4402             :   Cmd.nextrefsyms = 0;
    4403             :   Cmd.indirectsymoff = 0;
    4404             :   Cmd.nindirectsyms = 0;
    4405             :   Cmd.extreloff = 0;
    4406             :   Cmd.nextrel = 0;
    4407             :   Cmd.locreloff = 0;
    4408             :   Cmd.nlocrel = 0;
    4409           6 :   return Cmd;
    4410             : }
    4411             : 
    4412             : MachO::linkedit_data_command
    4413          46 : MachOObjectFile::getDataInCodeLoadCommand() const {
    4414          46 :   if (DataInCodeLoadCmd)
    4415          46 :     return getStruct<MachO::linkedit_data_command>(*this, DataInCodeLoadCmd);
    4416             : 
    4417             :   // If there is no DataInCodeLoadCmd return a load command with zero'ed fields.
    4418             :   MachO::linkedit_data_command Cmd;
    4419             :   Cmd.cmd = MachO::LC_DATA_IN_CODE;
    4420             :   Cmd.cmdsize = sizeof(MachO::linkedit_data_command);
    4421             :   Cmd.dataoff = 0;
    4422             :   Cmd.datasize = 0;
    4423           0 :   return Cmd;
    4424             : }
    4425             : 
    4426             : MachO::linkedit_data_command
    4427           1 : MachOObjectFile::getLinkOptHintsLoadCommand() const {
    4428           1 :   if (LinkOptHintsLoadCmd)
    4429           1 :     return getStruct<MachO::linkedit_data_command>(*this, LinkOptHintsLoadCmd);
    4430             : 
    4431             :   // If there is no LinkOptHintsLoadCmd return a load command with zero'ed
    4432             :   // fields.
    4433             :   MachO::linkedit_data_command Cmd;
    4434             :   Cmd.cmd = MachO::LC_LINKER_OPTIMIZATION_HINT;
    4435             :   Cmd.cmdsize = sizeof(MachO::linkedit_data_command);
    4436             :   Cmd.dataoff = 0;
    4437             :   Cmd.datasize = 0;
    4438           0 :   return Cmd;
    4439             : }
    4440             : 
    4441          44 : ArrayRef<uint8_t> MachOObjectFile::getDyldInfoRebaseOpcodes() const {
    4442          44 :   if (!DyldInfoLoadCmd)
    4443          20 :     return None;
    4444             : 
    4445             :   MachO::dyld_info_command DyldInfo =
    4446          24 :       getStruct<MachO::dyld_info_command>(*this, DyldInfoLoadCmd);
    4447             :   const uint8_t *Ptr =
    4448          24 :       reinterpret_cast<const uint8_t *>(getPtr(*this, DyldInfo.rebase_off));
    4449          24 :   return makeArrayRef(Ptr, DyldInfo.rebase_size);
    4450             : }
    4451             : 
    4452          60 : ArrayRef<uint8_t> MachOObjectFile::getDyldInfoBindOpcodes() const {
    4453          60 :   if (!DyldInfoLoadCmd)
    4454          21 :     return None;
    4455             : 
    4456             :   MachO::dyld_info_command DyldInfo =
    4457          39 :       getStruct<MachO::dyld_info_command>(*this, DyldInfoLoadCmd);
    4458             :   const uint8_t *Ptr =
    4459          39 :       reinterpret_cast<const uint8_t *>(getPtr(*this, DyldInfo.bind_off));
    4460          39 :   return makeArrayRef(Ptr, DyldInfo.bind_size);
    4461             : }
    4462             : 
    4463          38 : ArrayRef<uint8_t> MachOObjectFile::getDyldInfoWeakBindOpcodes() const {
    4464          38 :   if (!DyldInfoLoadCmd)
    4465          20 :     return None;
    4466             : 
    4467             :   MachO::dyld_info_command DyldInfo =
    4468          18 :       getStruct<MachO::dyld_info_command>(*this, DyldInfoLoadCmd);
    4469             :   const uint8_t *Ptr =
    4470          18 :       reinterpret_cast<const uint8_t *>(getPtr(*this, DyldInfo.weak_bind_off));
    4471          18 :   return makeArrayRef(Ptr, DyldInfo.weak_bind_size);
    4472             : }
    4473             : 
    4474          40 : ArrayRef<uint8_t> MachOObjectFile::getDyldInfoLazyBindOpcodes() const {
    4475          40 :   if (!DyldInfoLoadCmd)
    4476          20 :     return None;
    4477             : 
    4478             :   MachO::dyld_info_command DyldInfo =
    4479          20 :       getStruct<MachO::dyld_info_command>(*this, DyldInfoLoadCmd);
    4480             :   const uint8_t *Ptr =
    4481          20 :       reinterpret_cast<const uint8_t *>(getPtr(*this, DyldInfo.lazy_bind_off));
    4482          20 :   return makeArrayRef(Ptr, DyldInfo.lazy_bind_size);
    4483             : }
    4484             : 
    4485          51 : ArrayRef<uint8_t> MachOObjectFile::getDyldInfoExportsTrie() const {
    4486          51 :   if (!DyldInfoLoadCmd)
    4487          20 :     return None;
    4488             : 
    4489             :   MachO::dyld_info_command DyldInfo =
    4490          31 :       getStruct<MachO::dyld_info_command>(*this, DyldInfoLoadCmd);
    4491             :   const uint8_t *Ptr =
    4492          31 :       reinterpret_cast<const uint8_t *>(getPtr(*this, DyldInfo.export_off));
    4493          31 :   return makeArrayRef(Ptr, DyldInfo.export_size);
    4494             : }
    4495             : 
    4496           6 : ArrayRef<uint8_t> MachOObjectFile::getUuid() const {
    4497           6 :   if (!UuidLoadCmd)
    4498           0 :     return None;
    4499             :   // Returning a pointer is fine as uuid doesn't need endian swapping.
    4500           6 :   const char *Ptr = UuidLoadCmd + offsetof(MachO::uuid_command, uuid);
    4501             :   return makeArrayRef(reinterpret_cast<const uint8_t *>(Ptr), 16);
    4502             : }
    4503             : 
    4504        3489 : StringRef MachOObjectFile::getStringTableData() const {
    4505        3489 :   MachO::symtab_command S = getSymtabLoadCommand();
    4506        6978 :   return getData().substr(S.stroff, S.strsize);
    4507             : }
    4508             : 
    4509      114445 : bool MachOObjectFile::is64Bit() const {
    4510      114445 :   return getType() == getMachOType(false, true) ||
    4511      114445 :     getType() == getMachOType(true, true);
    4512             : }
    4513             : 
    4514          18 : void MachOObjectFile::ReadULEB128s(uint64_t Index,
    4515             :                                    SmallVectorImpl<uint64_t> &Out) const {
    4516          18 :   DataExtractor extractor(ObjectFile::getData(), true, 0);
    4517             : 
    4518          18 :   uint32_t offset = Index;
    4519          18 :   uint64_t data = 0;
    4520          58 :   while (uint64_t delta = extractor.getULEB128(&offset)) {
    4521          40 :     data += delta;
    4522          40 :     Out.push_back(data);
    4523          40 :   }
    4524          18 : }
    4525             : 
    4526          32 : bool MachOObjectFile::isRelocatableObject() const {
    4527          32 :   return getHeader().filetype == MachO::MH_OBJECT;
    4528             : }
    4529             : 
    4530             : Expected<std::unique_ptr<MachOObjectFile>>
    4531        1824 : ObjectFile::createMachOObjectFile(MemoryBufferRef Buffer,
    4532             :                                   uint32_t UniversalCputype,
    4533             :                                   uint32_t UniversalIndex) {
    4534        3648 :   StringRef Magic = Buffer.getBuffer().slice(0, 4);
    4535             :   if (Magic == "\xFE\xED\xFA\xCE")
    4536             :     return MachOObjectFile::create(Buffer, false, false,
    4537          10 :                                    UniversalCputype, UniversalIndex);
    4538             :   if (Magic == "\xCE\xFA\xED\xFE")
    4539             :     return MachOObjectFile::create(Buffer, true, false,
    4540         499 :                                    UniversalCputype, UniversalIndex);
    4541             :   if (Magic == "\xFE\xED\xFA\xCF")
    4542             :     return MachOObjectFile::create(Buffer, false, true,
    4543           4 :                                    UniversalCputype, UniversalIndex);
    4544             :   if (Magic == "\xCF\xFA\xED\xFE")
    4545             :     return MachOObjectFile::create(Buffer, true, true,
    4546        1259 :                                    UniversalCputype, UniversalIndex);
    4547             :   return make_error<GenericBinaryError>("Unrecognized MachO magic number",
    4548             :                                         object_error::invalid_file_type);
    4549             : }
    4550             : 
    4551        6109 : StringRef MachOObjectFile::mapDebugSectionName(StringRef Name) const {
    4552             :   return StringSwitch<StringRef>(Name)
    4553        6109 :       .Case("debug_str_offs", "debug_str_offsets")
    4554        6109 :       .Default(Name);
    4555             : }

Generated by: LCOV version 1.13