LCOV - code coverage report
Current view: top level - include/llvm/Object - MachO.h (source / functions) Hit Total Coverage
Test: llvm-toolchain.info Lines: 48 80 60.0 %
Date: 2017-09-14 15:23:50 Functions: 10 13 76.9 %
Legend: Lines: hit not hit

          Line data    Source code
       1             : //===- MachO.h - MachO object file implementation ---------------*- C++ -*-===//
       2             : //
       3             : //                     The LLVM Compiler Infrastructure
       4             : //
       5             : // This file is distributed under the University of Illinois Open Source
       6             : // License. See LICENSE.TXT for details.
       7             : //
       8             : //===----------------------------------------------------------------------===//
       9             : //
      10             : // This file declares the MachOObjectFile class, which implement the ObjectFile
      11             : // interface for MachO files.
      12             : //
      13             : //===----------------------------------------------------------------------===//
      14             : 
      15             : #ifndef LLVM_OBJECT_MACHO_H
      16             : #define LLVM_OBJECT_MACHO_H
      17             : 
      18             : #include "llvm/ADT/ArrayRef.h"
      19             : #include "llvm/ADT/SmallString.h"
      20             : #include "llvm/ADT/SmallVector.h"
      21             : #include "llvm/ADT/StringExtras.h"
      22             : #include "llvm/ADT/StringRef.h"
      23             : #include "llvm/ADT/Triple.h"
      24             : #include "llvm/ADT/iterator_range.h"
      25             : #include "llvm/BinaryFormat/MachO.h"
      26             : #include "llvm/MC/SubtargetFeature.h"
      27             : #include "llvm/Object/Binary.h"
      28             : #include "llvm/Object/ObjectFile.h"
      29             : #include "llvm/Object/SymbolicFile.h"
      30             : #include "llvm/Support/Error.h"
      31             : #include "llvm/Support/Format.h"
      32             : #include "llvm/Support/MemoryBuffer.h"
      33             : #include "llvm/Support/raw_ostream.h"
      34             : #include <cstdint>
      35             : #include <memory>
      36             : #include <string>
      37             : #include <system_error>
      38             : 
      39             : namespace llvm {
      40             : namespace object {
      41             : 
      42             : /// DiceRef - This is a value type class that represents a single
      43             : /// data in code entry in the table in a Mach-O object file.
      44             : class DiceRef {
      45             :   DataRefImpl DicePimpl;
      46             :   const ObjectFile *OwningObject = nullptr;
      47             : 
      48             : public:
      49        2488 :   DiceRef() = default;
      50             :   DiceRef(DataRefImpl DiceP, const ObjectFile *Owner);
      51             : 
      52             :   bool operator==(const DiceRef &Other) const;
      53             :   bool operator<(const DiceRef &Other) const;
      54             : 
      55             :   void moveNext();
      56             : 
      57             :   std::error_code getOffset(uint32_t &Result) const;
      58             :   std::error_code getLength(uint16_t &Result) const;
      59             :   std::error_code getKind(uint16_t &Result) const;
      60             : 
      61             :   DataRefImpl getRawDataRefImpl() const;
      62             :   const ObjectFile *getObjectFile() const;
      63             : };
      64             : using dice_iterator = content_iterator<DiceRef>;
      65             : 
      66             : /// ExportEntry encapsulates the current-state-of-the-walk used when doing a
      67             : /// non-recursive walk of the trie data structure.  This allows you to iterate
      68             : /// across all exported symbols using:
      69             : ///      Error Err;
      70             : ///      for (const llvm::object::ExportEntry &AnExport : Obj->exports(&Err)) {
      71             : ///      }
      72             : ///      if (Err) { report error ...
      73        1518 : class ExportEntry {
      74             : public:
      75             :   ExportEntry(Error *Err, const MachOObjectFile *O, ArrayRef<uint8_t> Trie);
      76             : 
      77             :   StringRef name() const;
      78             :   uint64_t flags() const;
      79             :   uint64_t address() const;
      80             :   uint64_t other() const;
      81             :   StringRef otherName() const;
      82             :   uint32_t nodeOffset() const;
      83             : 
      84             :   bool operator==(const ExportEntry &) const;
      85             : 
      86             :   void moveNext();
      87             : 
      88             : private:
      89             :   friend class MachOObjectFile;
      90             : 
      91             :   void moveToFirst();
      92             :   void moveToEnd();
      93             :   uint64_t readULEB128(const uint8_t *&p, const char **error);
      94             :   void pushDownUntilBottom();
      95             :   void pushNode(uint64_t Offset);
      96             : 
      97             :   // Represents a node in the mach-o exports trie.
      98             :   struct NodeState {
      99             :     NodeState(const uint8_t *Ptr);
     100             : 
     101             :     const uint8_t *Start;
     102             :     const uint8_t *Current;
     103             :     uint64_t Flags = 0;
     104             :     uint64_t Address = 0;
     105             :     uint64_t Other = 0;
     106             :     const char *ImportName = nullptr;
     107             :     unsigned ChildCount = 0;
     108             :     unsigned NextChildIndex = 0;
     109             :     unsigned ParentStringLength = 0;
     110             :     bool IsExportNode = false;
     111             :   };
     112             :   using NodeList = SmallVector<NodeState, 16>;
     113             :   using node_iterator = NodeList::const_iterator;
     114             : 
     115             :   Error *E;
     116             :   const MachOObjectFile *O;
     117             :   ArrayRef<uint8_t> Trie;
     118             :   SmallString<256> CumulativeString;
     119             :   NodeList Stack;
     120             :   bool Done = false;
     121             : 
     122             :   iterator_range<node_iterator> nodes() const {
     123         452 :     return make_range(Stack.begin(), Stack.end());
     124             :   }
     125             : };
     126             : using export_iterator = content_iterator<ExportEntry>;
     127             : 
     128             : // Segment info so SegIndex/SegOffset pairs in a Mach-O Bind or Rebase entry
     129             : // can be checked and translated.  Only the SegIndex/SegOffset pairs from
     130             : // checked entries are to be used with the segmentName(), sectionName() and
     131             : // address() methods below.
     132          28 : class BindRebaseSegInfo {
     133             : public:
     134             :   BindRebaseSegInfo(const MachOObjectFile *Obj);
     135             : 
     136             :   // Used to check a Mach-O Bind or Rebase entry for errors when iterating.
     137             :   const char *checkSegAndOffset(int32_t SegIndex, uint64_t SegOffset,
     138             :                                 bool endInvalid);
     139             :   const char *checkCountAndSkip(uint32_t Count, uint32_t Skip,
     140             :                                 uint8_t PointerSize, int32_t SegIndex,
     141             :                                 uint64_t SegOffset);
     142             :   // Used with valid SegIndex/SegOffset values from checked entries.
     143             :   StringRef segmentName(int32_t SegIndex);
     144             :   StringRef sectionName(int32_t SegIndex, uint64_t SegOffset);
     145             :   uint64_t address(uint32_t SegIndex, uint64_t SegOffset);
     146             : 
     147             : private:
     148        1179 :   struct SectionInfo {
     149             :     uint64_t Address;
     150             :     uint64_t Size;
     151             :     StringRef SectionName;
     152             :     StringRef SegmentName;
     153             :     uint64_t OffsetInSegment;
     154             :     uint64_t SegmentStartAddress;
     155             :     int32_t SegmentIndex;
     156             :   };
     157             :   const SectionInfo &findSection(int32_t SegIndex, uint64_t SegOffset);
     158             : 
     159             :   SmallVector<SectionInfo, 32> Sections;
     160             :   int32_t MaxSegIndex;
     161             : };
     162             : 
     163             : /// MachORebaseEntry encapsulates the current state in the decompression of
     164             : /// rebasing opcodes. This allows you to iterate through the compressed table of
     165             : /// rebasing using:
     166             : ///    Error Err;
     167             : ///    for (const llvm::object::MachORebaseEntry &Entry : Obj->rebaseTable(&Err)) {
     168             : ///    }
     169             : ///    if (Err) { report error ...
     170             : class MachORebaseEntry {
     171             : public:
     172             :   MachORebaseEntry(Error *Err, const MachOObjectFile *O,
     173             :                    ArrayRef<uint8_t> opcodes, bool is64Bit);
     174             : 
     175             :   int32_t segmentIndex() const;
     176             :   uint64_t segmentOffset() const;
     177             :   StringRef typeName() const;
     178             :   StringRef segmentName() const;
     179             :   StringRef sectionName() const;
     180             :   uint64_t address() const;
     181             : 
     182             :   bool operator==(const MachORebaseEntry &) const;
     183             : 
     184             :   void moveNext();
     185             : 
     186             : private:
     187             :   friend class MachOObjectFile;
     188             : 
     189             :   void moveToFirst();
     190             :   void moveToEnd();
     191             :   uint64_t readULEB128(const char **error);
     192             : 
     193             :   Error *E;
     194             :   const MachOObjectFile *O;
     195             :   ArrayRef<uint8_t> Opcodes;
     196             :   const uint8_t *Ptr;
     197             :   uint64_t SegmentOffset = 0;
     198             :   int32_t SegmentIndex = -1;
     199             :   uint64_t RemainingLoopCount = 0;
     200             :   uint64_t AdvanceAmount = 0;
     201             :   uint8_t  RebaseType = 0;
     202             :   uint8_t  PointerSize;
     203             :   bool     Done = false;
     204             : };
     205             : using rebase_iterator = content_iterator<MachORebaseEntry>;
     206             : 
     207             : /// MachOBindEntry encapsulates the current state in the decompression of
     208             : /// binding opcodes. This allows you to iterate through the compressed table of
     209             : /// bindings using:
     210             : ///    Error Err;
     211             : ///    for (const llvm::object::MachOBindEntry &Entry : Obj->bindTable(&Err)) {
     212             : ///    }
     213             : ///    if (Err) { report error ...
     214             : class MachOBindEntry {
     215             : public:
     216             :   enum class Kind { Regular, Lazy, Weak };
     217             : 
     218             :   MachOBindEntry(Error *Err, const MachOObjectFile *O,
     219             :                  ArrayRef<uint8_t> Opcodes, bool is64Bit, MachOBindEntry::Kind);
     220             : 
     221             :   int32_t segmentIndex() const;
     222             :   uint64_t segmentOffset() const;
     223             :   StringRef typeName() const;
     224             :   StringRef symbolName() const;
     225             :   uint32_t flags() const;
     226             :   int64_t addend() const;
     227             :   int ordinal() const;
     228             : 
     229             :   StringRef segmentName() const;
     230             :   StringRef sectionName() const;
     231             :   uint64_t address() const;
     232             : 
     233             :   bool operator==(const MachOBindEntry &) const;
     234             : 
     235             :   void moveNext();
     236             : 
     237             : private:
     238             :   friend class MachOObjectFile;
     239             : 
     240             :   void moveToFirst();
     241             :   void moveToEnd();
     242             :   uint64_t readULEB128(const char **error);
     243             :   int64_t readSLEB128(const char **error);
     244             : 
     245             :   Error *E;
     246             :   const MachOObjectFile *O;
     247             :   ArrayRef<uint8_t> Opcodes;
     248             :   const uint8_t *Ptr;
     249             :   uint64_t SegmentOffset = 0;
     250             :   int32_t  SegmentIndex = -1;
     251             :   StringRef SymbolName;
     252             :   bool     LibraryOrdinalSet = false;
     253             :   int      Ordinal = 0;
     254             :   uint32_t Flags = 0;
     255             :   int64_t  Addend = 0;
     256             :   uint64_t RemainingLoopCount = 0;
     257             :   uint64_t AdvanceAmount = 0;
     258             :   uint8_t  BindType = 0;
     259             :   uint8_t  PointerSize;
     260             :   Kind     TableKind;
     261             :   bool     Done = false;
     262             : };
     263             : using bind_iterator = content_iterator<MachOBindEntry>;
     264             : 
     265       13068 : class MachOObjectFile : public ObjectFile {
     266             : public:
     267             :   struct LoadCommandInfo {
     268             :     const char *Ptr;      // Where in memory the load command is.
     269             :     MachO::load_command C; // The command itself.
     270             :   };
     271             :   using LoadCommandList = SmallVector<LoadCommandInfo, 4>;
     272             :   using load_command_iterator = LoadCommandList::const_iterator;
     273             : 
     274             :   static Expected<std::unique_ptr<MachOObjectFile>>
     275             :   create(MemoryBufferRef Object, bool IsLittleEndian, bool Is64Bits,
     276             :          uint32_t UniversalCputype = 0, uint32_t UniversalIndex = 0);
     277             : 
     278             :   void moveSymbolNext(DataRefImpl &Symb) const override;
     279             : 
     280             :   uint64_t getNValue(DataRefImpl Sym) const;
     281             :   Expected<StringRef> getSymbolName(DataRefImpl Symb) const override;
     282             : 
     283             :   // MachO specific.
     284             :   Error checkSymbolTable() const;
     285             : 
     286             :   std::error_code getIndirectName(DataRefImpl Symb, StringRef &Res) const;
     287             :   unsigned getSectionType(SectionRef Sec) const;
     288             : 
     289             :   Expected<uint64_t> getSymbolAddress(DataRefImpl Symb) const override;
     290             :   uint32_t getSymbolAlignment(DataRefImpl Symb) const override;
     291             :   uint64_t getCommonSymbolSizeImpl(DataRefImpl Symb) const override;
     292             :   Expected<SymbolRef::Type> getSymbolType(DataRefImpl Symb) const override;
     293             :   uint32_t getSymbolFlags(DataRefImpl Symb) const override;
     294             :   Expected<section_iterator> getSymbolSection(DataRefImpl Symb) const override;
     295             :   unsigned getSymbolSectionID(SymbolRef Symb) const;
     296             :   unsigned getSectionID(SectionRef Sec) const;
     297             : 
     298             :   void moveSectionNext(DataRefImpl &Sec) const override;
     299             :   std::error_code getSectionName(DataRefImpl Sec,
     300             :                                  StringRef &Res) const override;
     301             :   uint64_t getSectionAddress(DataRefImpl Sec) const override;
     302             :   uint64_t getSectionIndex(DataRefImpl Sec) const override;
     303             :   uint64_t getSectionSize(DataRefImpl Sec) const override;
     304             :   std::error_code getSectionContents(DataRefImpl Sec,
     305             :                                      StringRef &Res) const override;
     306             :   uint64_t getSectionAlignment(DataRefImpl Sec) const override;
     307             :   bool isSectionCompressed(DataRefImpl Sec) const override;
     308             :   bool isSectionText(DataRefImpl Sec) const override;
     309             :   bool isSectionData(DataRefImpl Sec) const override;
     310             :   bool isSectionBSS(DataRefImpl Sec) const override;
     311             :   bool isSectionVirtual(DataRefImpl Sec) const override;
     312             :   bool isSectionBitcode(DataRefImpl Sec) const override;
     313             :   relocation_iterator section_rel_begin(DataRefImpl Sec) const override;
     314             :   relocation_iterator section_rel_end(DataRefImpl Sec) const override;
     315             : 
     316             :   relocation_iterator extrel_begin() const;
     317             :   relocation_iterator extrel_end() const;
     318         747 :   iterator_range<relocation_iterator> external_relocations() const {
     319        1494 :     return make_range(extrel_begin(), extrel_end());
     320             :   }
     321             : 
     322             :   void moveRelocationNext(DataRefImpl &Rel) const override;
     323             :   uint64_t getRelocationOffset(DataRefImpl Rel) const override;
     324             :   symbol_iterator getRelocationSymbol(DataRefImpl Rel) const override;
     325             :   section_iterator getRelocationSection(DataRefImpl Rel) const;
     326             :   uint64_t getRelocationType(DataRefImpl Rel) const override;
     327             :   void getRelocationTypeName(DataRefImpl Rel,
     328             :                              SmallVectorImpl<char> &Result) const override;
     329             :   uint8_t getRelocationLength(DataRefImpl Rel) const;
     330             : 
     331             :   // MachO specific.
     332             :   std::error_code getLibraryShortNameByIndex(unsigned Index, StringRef &) const;
     333             :   uint32_t getLibraryCount() const;
     334             : 
     335             :   section_iterator getRelocationRelocatedSection(relocation_iterator Rel) const;
     336             : 
     337             :   // TODO: Would be useful to have an iterator based version
     338             :   // of the load command interface too.
     339             : 
     340             :   basic_symbol_iterator symbol_begin() const override;
     341             :   basic_symbol_iterator symbol_end() const override;
     342             : 
     343             :   // MachO specific.
     344             :   basic_symbol_iterator getSymbolByIndex(unsigned Index) const;
     345             :   uint64_t getSymbolIndex(DataRefImpl Symb) const;
     346             : 
     347             :   section_iterator section_begin() const override;
     348             :   section_iterator section_end() const override;
     349             : 
     350             :   uint8_t getBytesInAddress() const override;
     351             : 
     352             :   StringRef getFileFormatName() const override;
     353             :   unsigned getArch() const override;
     354          56 :   SubtargetFeatures getFeatures() const override { return SubtargetFeatures(); }
     355             :   Triple getArchTriple(const char **McpuDefault = nullptr) const;
     356             : 
     357             :   relocation_iterator section_rel_begin(unsigned Index) const;
     358             :   relocation_iterator section_rel_end(unsigned Index) const;
     359             : 
     360             :   dice_iterator begin_dices() const;
     361             :   dice_iterator end_dices() const;
     362             : 
     363             :   load_command_iterator begin_load_commands() const;
     364             :   load_command_iterator end_load_commands() const;
     365             :   iterator_range<load_command_iterator> load_commands() const;
     366             : 
     367             :   /// For use iterating over all exported symbols.
     368             :   iterator_range<export_iterator> exports(Error &Err) const;
     369             : 
     370             :   /// For use examining a trie not in a MachOObjectFile.
     371             :   static iterator_range<export_iterator> exports(Error &Err,
     372             :                                                  ArrayRef<uint8_t> Trie,
     373             :                                                  const MachOObjectFile *O =
     374             :                                                                       nullptr);
     375             : 
     376             :   /// For use iterating over all rebase table entries.
     377             :   iterator_range<rebase_iterator> rebaseTable(Error &Err);
     378             : 
     379             :   /// For use examining rebase opcodes in a MachOObjectFile.
     380             :   static iterator_range<rebase_iterator> rebaseTable(Error &Err,
     381             :                                                      MachOObjectFile *O,
     382             :                                                      ArrayRef<uint8_t> Opcodes,
     383             :                                                      bool is64);
     384             : 
     385             :   /// For use iterating over all bind table entries.
     386             :   iterator_range<bind_iterator> bindTable(Error &Err);
     387             : 
     388             :   /// For use iterating over all lazy bind table entries.
     389             :   iterator_range<bind_iterator> lazyBindTable(Error &Err);
     390             : 
     391             :   /// For use iterating over all weak bind table entries.
     392             :   iterator_range<bind_iterator> weakBindTable(Error &Err);
     393             : 
     394             :   /// For use examining bind opcodes in a MachOObjectFile.
     395             :   static iterator_range<bind_iterator> bindTable(Error &Err,
     396             :                                                  MachOObjectFile *O,
     397             :                                                  ArrayRef<uint8_t> Opcodes,
     398             :                                                  bool is64,
     399             :                                                  MachOBindEntry::Kind);
     400             : 
     401             :   /// For use with a SegIndex,SegOffset pair in MachOBindEntry::moveNext() to
     402             :   /// validate a MachOBindEntry.
     403             :   const char *BindEntryCheckSegAndOffset(int32_t SegIndex, uint64_t SegOffset,
     404             :                                          bool endInvalid) const {
     405         234 :     return BindRebaseSectionTable->checkSegAndOffset(SegIndex, SegOffset,
     406         117 :                                                      endInvalid);
     407             :   }
     408             :   /// For use in MachOBindEntry::moveNext() to validate a MachOBindEntry for
     409             :   /// the BIND_OPCODE_DO_BIND_ULEB_TIMES_SKIPPING_ULEB opcode.
     410             :   const char *BindEntryCheckCountAndSkip(uint32_t Count, uint32_t Skip,
     411             :                                          uint8_t PointerSize, int32_t SegIndex,
     412             :                                          uint64_t SegOffset) const {
     413           4 :     return BindRebaseSectionTable->checkCountAndSkip(Count, Skip, PointerSize,
     414           2 :                                                      SegIndex, SegOffset);
     415             :   }
     416             : 
     417             :   /// For use with a SegIndex,SegOffset pair in MachORebaseEntry::moveNext() to
     418             :   /// validate a MachORebaseEntry.
     419             :   const char *RebaseEntryCheckSegAndOffset(int32_t SegIndex, uint64_t SegOffset,
     420             :                                            bool endInvalid) const {
     421          48 :     return BindRebaseSectionTable->checkSegAndOffset(SegIndex, SegOffset,
     422          24 :                                                      endInvalid);
     423             :   }
     424             :   /// For use in MachORebaseEntry::moveNext() to validate a MachORebaseEntry for
     425             :   /// the REBASE_OPCODE_DO_*_TIMES* opcodes.
     426             :   const char *RebaseEntryCheckCountAndSkip(uint32_t Count, uint32_t Skip,
     427             :                                          uint8_t PointerSize, int32_t SegIndex,
     428             :                                          uint64_t SegOffset) const {
     429          20 :     return BindRebaseSectionTable->checkCountAndSkip(Count, Skip, PointerSize,
     430          10 :                                                      SegIndex, SegOffset);
     431             :   }
     432             : 
     433             :   /// For use with the SegIndex of a checked Mach-O Bind or Rebase entry to
     434             :   /// get the segment name.
     435             :   StringRef BindRebaseSegmentName(int32_t SegIndex) const {
     436          76 :     return BindRebaseSectionTable->segmentName(SegIndex);
     437             :   }
     438             : 
     439             :   /// For use with a SegIndex,SegOffset pair from a checked Mach-O Bind or
     440             :   /// Rebase entry to get the section name.
     441             :   StringRef BindRebaseSectionName(uint32_t SegIndex, uint64_t SegOffset) const {
     442          76 :     return BindRebaseSectionTable->sectionName(SegIndex, SegOffset);
     443             :   }
     444             : 
     445             :   /// For use with a SegIndex,SegOffset pair from a checked Mach-O Bind or
     446             :   /// Rebase entry to get the address.
     447             :   uint64_t BindRebaseAddress(uint32_t SegIndex, uint64_t SegOffset) const {
     448         128 :     return BindRebaseSectionTable->address(SegIndex, SegOffset);
     449             :   }
     450             : 
     451             :   // In a MachO file, sections have a segment name. This is used in the .o
     452             :   // files. They have a single segment, but this field specifies which segment
     453             :   // a section should be put in in the final object.
     454             :   StringRef getSectionFinalSegmentName(DataRefImpl Sec) const;
     455             : 
     456             :   // Names are stored as 16 bytes. These returns the raw 16 bytes without
     457             :   // interpreting them as a C string.
     458             :   ArrayRef<char> getSectionRawName(DataRefImpl Sec) const;
     459             :   ArrayRef<char> getSectionRawFinalSegmentName(DataRefImpl Sec) const;
     460             : 
     461             :   // MachO specific Info about relocations.
     462             :   bool isRelocationScattered(const MachO::any_relocation_info &RE) const;
     463             :   unsigned getPlainRelocationSymbolNum(
     464             :                                     const MachO::any_relocation_info &RE) const;
     465             :   bool getPlainRelocationExternal(const MachO::any_relocation_info &RE) const;
     466             :   bool getScatteredRelocationScattered(
     467             :                                     const MachO::any_relocation_info &RE) const;
     468             :   uint32_t getScatteredRelocationValue(
     469             :                                     const MachO::any_relocation_info &RE) const;
     470             :   uint32_t getScatteredRelocationType(
     471             :                                     const MachO::any_relocation_info &RE) const;
     472             :   unsigned getAnyRelocationAddress(const MachO::any_relocation_info &RE) const;
     473             :   unsigned getAnyRelocationPCRel(const MachO::any_relocation_info &RE) const;
     474             :   unsigned getAnyRelocationLength(const MachO::any_relocation_info &RE) const;
     475             :   unsigned getAnyRelocationType(const MachO::any_relocation_info &RE) const;
     476             :   SectionRef getAnyRelocationSection(const MachO::any_relocation_info &RE) const;
     477             : 
     478             :   // MachO specific structures.
     479             :   MachO::section getSection(DataRefImpl DRI) const;
     480             :   MachO::section_64 getSection64(DataRefImpl DRI) const;
     481             :   MachO::section getSection(const LoadCommandInfo &L, unsigned Index) const;
     482             :   MachO::section_64 getSection64(const LoadCommandInfo &L,unsigned Index) const;
     483             :   MachO::nlist getSymbolTableEntry(DataRefImpl DRI) const;
     484             :   MachO::nlist_64 getSymbol64TableEntry(DataRefImpl DRI) const;
     485             : 
     486             :   MachO::linkedit_data_command
     487             :   getLinkeditDataLoadCommand(const LoadCommandInfo &L) const;
     488             :   MachO::segment_command
     489             :   getSegmentLoadCommand(const LoadCommandInfo &L) const;
     490             :   MachO::segment_command_64
     491             :   getSegment64LoadCommand(const LoadCommandInfo &L) const;
     492             :   MachO::linker_option_command
     493             :   getLinkerOptionLoadCommand(const LoadCommandInfo &L) const;
     494             :   MachO::version_min_command
     495             :   getVersionMinLoadCommand(const LoadCommandInfo &L) const;
     496             :   MachO::note_command
     497             :   getNoteLoadCommand(const LoadCommandInfo &L) const;
     498             :   MachO::build_version_command
     499             :   getBuildVersionLoadCommand(const LoadCommandInfo &L) const;
     500             :   MachO::build_tool_version
     501             :   getBuildToolVersion(unsigned index) const;
     502             :   MachO::dylib_command
     503             :   getDylibIDLoadCommand(const LoadCommandInfo &L) const;
     504             :   MachO::dyld_info_command
     505             :   getDyldInfoLoadCommand(const LoadCommandInfo &L) const;
     506             :   MachO::dylinker_command
     507             :   getDylinkerCommand(const LoadCommandInfo &L) const;
     508             :   MachO::uuid_command
     509             :   getUuidCommand(const LoadCommandInfo &L) const;
     510             :   MachO::rpath_command
     511             :   getRpathCommand(const LoadCommandInfo &L) const;
     512             :   MachO::source_version_command
     513             :   getSourceVersionCommand(const LoadCommandInfo &L) const;
     514             :   MachO::entry_point_command
     515             :   getEntryPointCommand(const LoadCommandInfo &L) const;
     516             :   MachO::encryption_info_command
     517             :   getEncryptionInfoCommand(const LoadCommandInfo &L) const;
     518             :   MachO::encryption_info_command_64
     519             :   getEncryptionInfoCommand64(const LoadCommandInfo &L) const;
     520             :   MachO::sub_framework_command
     521             :   getSubFrameworkCommand(const LoadCommandInfo &L) const;
     522             :   MachO::sub_umbrella_command
     523             :   getSubUmbrellaCommand(const LoadCommandInfo &L) const;
     524             :   MachO::sub_library_command
     525             :   getSubLibraryCommand(const LoadCommandInfo &L) const;
     526             :   MachO::sub_client_command
     527             :   getSubClientCommand(const LoadCommandInfo &L) const;
     528             :   MachO::routines_command
     529             :   getRoutinesCommand(const LoadCommandInfo &L) const;
     530             :   MachO::routines_command_64
     531             :   getRoutinesCommand64(const LoadCommandInfo &L) const;
     532             :   MachO::thread_command
     533             :   getThreadCommand(const LoadCommandInfo &L) const;
     534             : 
     535             :   MachO::any_relocation_info getRelocation(DataRefImpl Rel) const;
     536             :   MachO::data_in_code_entry getDice(DataRefImpl Rel) const;
     537             :   const MachO::mach_header &getHeader() const;
     538             :   const MachO::mach_header_64 &getHeader64() const;
     539             :   uint32_t
     540             :   getIndirectSymbolTableEntry(const MachO::dysymtab_command &DLC,
     541             :                               unsigned Index) const;
     542             :   MachO::data_in_code_entry getDataInCodeTableEntry(uint32_t DataOffset,
     543             :                                                     unsigned Index) const;
     544             :   MachO::symtab_command getSymtabLoadCommand() const;
     545             :   MachO::dysymtab_command getDysymtabLoadCommand() const;
     546             :   MachO::linkedit_data_command getDataInCodeLoadCommand() const;
     547             :   MachO::linkedit_data_command getLinkOptHintsLoadCommand() const;
     548             :   ArrayRef<uint8_t> getDyldInfoRebaseOpcodes() const;
     549             :   ArrayRef<uint8_t> getDyldInfoBindOpcodes() const;
     550             :   ArrayRef<uint8_t> getDyldInfoWeakBindOpcodes() const;
     551             :   ArrayRef<uint8_t> getDyldInfoLazyBindOpcodes() const;
     552             :   ArrayRef<uint8_t> getDyldInfoExportsTrie() const;
     553             :   ArrayRef<uint8_t> getUuid() const;
     554             : 
     555             :   StringRef getStringTableData() const;
     556             :   bool is64Bit() const;
     557             :   void ReadULEB128s(uint64_t Index, SmallVectorImpl<uint64_t> &Out) const;
     558             : 
     559             :   static StringRef guessLibraryShortName(StringRef Name, bool &isFramework,
     560             :                                          StringRef &Suffix);
     561             : 
     562             :   static Triple::ArchType getArch(uint32_t CPUType);
     563             :   static Triple getArchTriple(uint32_t CPUType, uint32_t CPUSubType,
     564             :                               const char **McpuDefault = nullptr,
     565             :                               const char **ArchFlag = nullptr);
     566             :   static bool isValidArch(StringRef ArchFlag);
     567             :   static Triple getHostArch();
     568             : 
     569             :   bool isRelocatableObject() const override;
     570             : 
     571             :   StringRef mapDebugSectionName(StringRef Name) const override;
     572             : 
     573             :   bool hasPageZeroSegment() const { return HasPageZeroSegment; }
     574             : 
     575             :   static bool classof(const Binary *v) {
     576       18608 :     return v->isMachO();
     577             :   }
     578             : 
     579             :   static uint32_t
     580             :   getVersionMinMajor(MachO::version_min_command &C, bool SDK) {
     581          98 :     uint32_t VersionOrSDK = (SDK) ? C.sdk : C.version;
     582          98 :     return (VersionOrSDK >> 16) & 0xffff;
     583             :   }
     584             : 
     585             :   static uint32_t
     586             :   getVersionMinMinor(MachO::version_min_command &C, bool SDK) {
     587          98 :     uint32_t VersionOrSDK = (SDK) ? C.sdk : C.version;
     588          98 :     return (VersionOrSDK >> 8) & 0xff;
     589             :   }
     590             : 
     591             :   static uint32_t
     592             :   getVersionMinUpdate(MachO::version_min_command &C, bool SDK) {
     593         109 :     uint32_t VersionOrSDK = (SDK) ? C.sdk : C.version;
     594         105 :     return VersionOrSDK & 0xff;
     595             :   }
     596             : 
     597           0 :   static std::string getBuildPlatform(uint32_t platform) {
     598           0 :     switch (platform) {
     599           0 :     case MachO::PLATFORM_MACOS: return "macos";
     600           0 :     case MachO::PLATFORM_IOS: return "ios";
     601           0 :     case MachO::PLATFORM_TVOS: return "tvos";
     602           0 :     case MachO::PLATFORM_WATCHOS: return "watchos";
     603           0 :     case MachO::PLATFORM_BRIDGEOS: return "bridgeos";
     604           0 :     default:
     605           0 :       std::string ret;
     606           0 :       raw_string_ostream ss(ret);
     607           0 :       ss << format_hex(platform, 8, true);
     608           0 :       return ss.str();
     609             :     }
     610             :   }
     611             : 
     612           0 :   static std::string getBuildTool(uint32_t tools) {
     613           0 :     switch (tools) {
     614           0 :     case MachO::TOOL_CLANG: return "clang";
     615           0 :     case MachO::TOOL_SWIFT: return "swift";
     616           0 :     case MachO::TOOL_LD: return "ld";
     617           0 :     default:
     618           0 :       std::string ret;
     619           0 :       raw_string_ostream ss(ret);
     620           0 :       ss << format_hex(tools, 8, true);
     621           0 :       return ss.str();
     622             :     }
     623             :   }
     624             : 
     625           0 :   static std::string getVersionString(uint32_t version) {
     626           0 :     uint32_t major = (version >> 16) & 0xffff;
     627           0 :     uint32_t minor = (version >> 8) & 0xff;
     628           0 :     uint32_t update = version & 0xff;
     629             : 
     630           0 :     SmallString<32> Version;
     631           0 :     Version = utostr(major) + "." + utostr(minor);
     632           0 :     if (update != 0)
     633           0 :       Version += "." + utostr(update);
     634           0 :     return Version.str();
     635             :   }
     636             : 
     637             : private:
     638             :   MachOObjectFile(MemoryBufferRef Object, bool IsLittleEndian, bool Is64Bits,
     639             :                   Error &Err, uint32_t UniversalCputype = 0,
     640             :                   uint32_t UniversalIndex = 0);
     641             : 
     642             :   uint64_t getSymbolValueImpl(DataRefImpl Symb) const override;
     643             : 
     644             :   union {
     645             :     MachO::mach_header_64 Header64;
     646             :     MachO::mach_header Header;
     647             :   };
     648             :   using SectionList = SmallVector<const char*, 1>;
     649             :   SectionList Sections;
     650             :   using LibraryList = SmallVector<const char*, 1>;
     651             :   LibraryList Libraries;
     652             :   LoadCommandList LoadCommands;
     653             :   using LibraryShortName = SmallVector<StringRef, 1>;
     654             :   using BuildToolList = SmallVector<const char*, 1>;
     655             :   BuildToolList BuildTools;
     656             :   mutable LibraryShortName LibrariesShortNames;
     657             :   std::unique_ptr<BindRebaseSegInfo> BindRebaseSectionTable;
     658             :   const char *SymtabLoadCmd = nullptr;
     659             :   const char *DysymtabLoadCmd = nullptr;
     660             :   const char *DataInCodeLoadCmd = nullptr;
     661             :   const char *LinkOptHintsLoadCmd = nullptr;
     662             :   const char *DyldInfoLoadCmd = nullptr;
     663             :   const char *UuidLoadCmd = nullptr;
     664             :   bool HasPageZeroSegment = false;
     665             : };
     666             : 
     667             : /// DiceRef
     668         110 : inline DiceRef::DiceRef(DataRefImpl DiceP, const ObjectFile *Owner)
     669         110 :   : DicePimpl(DiceP) , OwningObject(Owner) {}
     670             : 
     671             : inline bool DiceRef::operator==(const DiceRef &Other) const {
     672         130 :   return DicePimpl == Other.DicePimpl;
     673             : }
     674             : 
     675             : inline bool DiceRef::operator<(const DiceRef &Other) const {
     676           0 :   return DicePimpl < Other.DicePimpl;
     677             : }
     678             : 
     679             : inline void DiceRef::moveNext() {
     680          10 :   const MachO::data_in_code_entry *P =
     681          10 :     reinterpret_cast<const MachO::data_in_code_entry *>(DicePimpl.p);
     682          10 :   DicePimpl.p = reinterpret_cast<uintptr_t>(P + 1);
     683             : }
     684             : 
     685             : // Since a Mach-O data in code reference, a DiceRef, can only be created when
     686             : // the OwningObject ObjectFile is a MachOObjectFile a static_cast<> is used for
     687             : // the methods that get the values of the fields of the reference.
     688             : 
     689          10 : inline std::error_code DiceRef::getOffset(uint32_t &Result) const {
     690          10 :   const MachOObjectFile *MachOOF =
     691             :     static_cast<const MachOObjectFile *>(OwningObject);
     692          10 :   MachO::data_in_code_entry Dice = MachOOF->getDice(DicePimpl);
     693          10 :   Result = Dice.offset;
     694          10 :   return std::error_code();
     695             : }
     696             : 
     697          42 : inline std::error_code DiceRef::getLength(uint16_t &Result) const {
     698          42 :   const MachOObjectFile *MachOOF =
     699             :     static_cast<const MachOObjectFile *>(OwningObject);
     700          42 :   MachO::data_in_code_entry Dice = MachOOF->getDice(DicePimpl);
     701          42 :   Result = Dice.length;
     702          42 :   return std::error_code();
     703             : }
     704             : 
     705          10 : inline std::error_code DiceRef::getKind(uint16_t &Result) const {
     706          10 :   const MachOObjectFile *MachOOF =
     707             :     static_cast<const MachOObjectFile *>(OwningObject);
     708          10 :   MachO::data_in_code_entry Dice = MachOOF->getDice(DicePimpl);
     709          10 :   Result = Dice.kind;
     710          10 :   return std::error_code();
     711             : }
     712             : 
     713             : inline DataRefImpl DiceRef::getRawDataRefImpl() const {
     714             :   return DicePimpl;
     715             : }
     716             : 
     717             : inline const ObjectFile *DiceRef::getObjectFile() const {
     718             :   return OwningObject;
     719             : }
     720             : 
     721             : } // end namespace object
     722             : } // end namespace llvm
     723             : 
     724             : #endif // LLVM_OBJECT_MACHO_H

Generated by: LCOV version 1.13