LLVM  8.0.1
MachO.h
Go to the documentation of this file.
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"
27 #include "llvm/Object/Binary.h"
28 #include "llvm/Object/ObjectFile.h"
30 #include "llvm/Support/Error.h"
31 #include "llvm/Support/Format.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  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 
62  const ObjectFile *getObjectFile() const;
63 };
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 class ExportEntry {
74 public:
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  };
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 
123  return make_range(Stack.begin(), Stack.end());
124  }
125 };
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.
133 public:
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  struct SectionInfo {
149  uint64_t Address;
150  uint64_t Size;
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 
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 ...
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 };
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 ...
215 public:
216  enum class Kind { Regular, Lazy, Weak };
217 
218  MachOBindEntry(Error *Err, const MachOObjectFile *O,
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;
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 };
264 
265 class MachOObjectFile : public ObjectFile {
266 public:
268  const char *Ptr; // Where in memory the load command is.
269  MachO::load_command C; // The command itself.
270  };
273 
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;
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  Expected<SectionRef> getSection(unsigned SectionIndex) const;
309  bool isSectionCompressed(DataRefImpl Sec) const override;
310  bool isSectionText(DataRefImpl Sec) const override;
311  bool isSectionData(DataRefImpl Sec) const override;
312  bool isSectionBSS(DataRefImpl Sec) const override;
313  bool isSectionVirtual(DataRefImpl Sec) const override;
314  bool isSectionBitcode(DataRefImpl Sec) const override;
315 
316  /// When dsymutil generates the companion file, it strips all unnecessary
317  /// sections (e.g. everything in the _TEXT segment) by omitting their body
318  /// and setting the offset in their corresponding load command to zero.
319  ///
320  /// While the load command itself is valid, reading the section corresponds
321  /// to reading the number of bytes specified in the load command, starting
322  /// from offset 0 (i.e. the Mach-O header at the beginning of the file).
323  bool isSectionStripped(DataRefImpl Sec) const override;
324 
325  relocation_iterator section_rel_begin(DataRefImpl Sec) const override;
326  relocation_iterator section_rel_end(DataRefImpl Sec) const override;
327 
328  relocation_iterator extrel_begin() const;
329  relocation_iterator extrel_end() const;
331  return make_range(extrel_begin(), extrel_end());
332  }
333 
334  relocation_iterator locrel_begin() const;
335  relocation_iterator locrel_end() const;
336 
337  void moveRelocationNext(DataRefImpl &Rel) const override;
338  uint64_t getRelocationOffset(DataRefImpl Rel) const override;
339  symbol_iterator getRelocationSymbol(DataRefImpl Rel) const override;
340  section_iterator getRelocationSection(DataRefImpl Rel) const;
341  uint64_t getRelocationType(DataRefImpl Rel) const override;
342  void getRelocationTypeName(DataRefImpl Rel,
343  SmallVectorImpl<char> &Result) const override;
344  uint8_t getRelocationLength(DataRefImpl Rel) const;
345 
346  // MachO specific.
347  std::error_code getLibraryShortNameByIndex(unsigned Index, StringRef &) const;
348  uint32_t getLibraryCount() const;
349 
350  section_iterator getRelocationRelocatedSection(relocation_iterator Rel) const;
351 
352  // TODO: Would be useful to have an iterator based version
353  // of the load command interface too.
354 
355  basic_symbol_iterator symbol_begin() const override;
356  basic_symbol_iterator symbol_end() const override;
357 
358  // MachO specific.
359  symbol_iterator getSymbolByIndex(unsigned Index) const;
360  uint64_t getSymbolIndex(DataRefImpl Symb) const;
361 
362  section_iterator section_begin() const override;
363  section_iterator section_end() const override;
364 
365  uint8_t getBytesInAddress() const override;
366 
367  StringRef getFileFormatName() const override;
368  Triple::ArchType getArch() const override;
369  SubtargetFeatures getFeatures() const override { return SubtargetFeatures(); }
370  Triple getArchTriple(const char **McpuDefault = nullptr) const;
371 
372  relocation_iterator section_rel_begin(unsigned Index) const;
373  relocation_iterator section_rel_end(unsigned Index) const;
374 
375  dice_iterator begin_dices() const;
376  dice_iterator end_dices() const;
377 
378  load_command_iterator begin_load_commands() const;
379  load_command_iterator end_load_commands() const;
380  iterator_range<load_command_iterator> load_commands() const;
381 
382  /// For use iterating over all exported symbols.
383  iterator_range<export_iterator> exports(Error &Err) const;
384 
385  /// For use examining a trie not in a MachOObjectFile.
386  static iterator_range<export_iterator> exports(Error &Err,
387  ArrayRef<uint8_t> Trie,
388  const MachOObjectFile *O =
389  nullptr);
390 
391  /// For use iterating over all rebase table entries.
392  iterator_range<rebase_iterator> rebaseTable(Error &Err);
393 
394  /// For use examining rebase opcodes in a MachOObjectFile.
395  static iterator_range<rebase_iterator> rebaseTable(Error &Err,
396  MachOObjectFile *O,
397  ArrayRef<uint8_t> Opcodes,
398  bool is64);
399 
400  /// For use iterating over all bind table entries.
401  iterator_range<bind_iterator> bindTable(Error &Err);
402 
403  /// For use iterating over all lazy bind table entries.
404  iterator_range<bind_iterator> lazyBindTable(Error &Err);
405 
406  /// For use iterating over all weak bind table entries.
407  iterator_range<bind_iterator> weakBindTable(Error &Err);
408 
409  /// For use examining bind opcodes in a MachOObjectFile.
410  static iterator_range<bind_iterator> bindTable(Error &Err,
411  MachOObjectFile *O,
412  ArrayRef<uint8_t> Opcodes,
413  bool is64,
415 
416  /// For use with a SegIndex,SegOffset pair in MachOBindEntry::moveNext() to
417  /// validate a MachOBindEntry.
418  const char *BindEntryCheckSegAndOffset(int32_t SegIndex, uint64_t SegOffset,
419  bool endInvalid) const {
420  return BindRebaseSectionTable->checkSegAndOffset(SegIndex, SegOffset,
421  endInvalid);
422  }
423  /// For use in MachOBindEntry::moveNext() to validate a MachOBindEntry for
424  /// the BIND_OPCODE_DO_BIND_ULEB_TIMES_SKIPPING_ULEB opcode.
426  uint8_t PointerSize, int32_t SegIndex,
427  uint64_t SegOffset) const {
428  return BindRebaseSectionTable->checkCountAndSkip(Count, Skip, PointerSize,
429  SegIndex, SegOffset);
430  }
431 
432  /// For use with a SegIndex,SegOffset pair in MachORebaseEntry::moveNext() to
433  /// validate a MachORebaseEntry.
434  const char *RebaseEntryCheckSegAndOffset(int32_t SegIndex, uint64_t SegOffset,
435  bool endInvalid) const {
436  return BindRebaseSectionTable->checkSegAndOffset(SegIndex, SegOffset,
437  endInvalid);
438  }
439  /// For use in MachORebaseEntry::moveNext() to validate a MachORebaseEntry for
440  /// the REBASE_OPCODE_DO_*_TIMES* opcodes.
442  uint8_t PointerSize, int32_t SegIndex,
443  uint64_t SegOffset) const {
444  return BindRebaseSectionTable->checkCountAndSkip(Count, Skip, PointerSize,
445  SegIndex, SegOffset);
446  }
447 
448  /// For use with the SegIndex of a checked Mach-O Bind or Rebase entry to
449  /// get the segment name.
450  StringRef BindRebaseSegmentName(int32_t SegIndex) const {
451  return BindRebaseSectionTable->segmentName(SegIndex);
452  }
453 
454  /// For use with a SegIndex,SegOffset pair from a checked Mach-O Bind or
455  /// Rebase entry to get the section name.
456  StringRef BindRebaseSectionName(uint32_t SegIndex, uint64_t SegOffset) const {
457  return BindRebaseSectionTable->sectionName(SegIndex, SegOffset);
458  }
459 
460  /// For use with a SegIndex,SegOffset pair from a checked Mach-O Bind or
461  /// Rebase entry to get the address.
462  uint64_t BindRebaseAddress(uint32_t SegIndex, uint64_t SegOffset) const {
463  return BindRebaseSectionTable->address(SegIndex, SegOffset);
464  }
465 
466  // In a MachO file, sections have a segment name. This is used in the .o
467  // files. They have a single segment, but this field specifies which segment
468  // a section should be put in the final object.
469  StringRef getSectionFinalSegmentName(DataRefImpl Sec) const;
470 
471  // Names are stored as 16 bytes. These returns the raw 16 bytes without
472  // interpreting them as a C string.
473  ArrayRef<char> getSectionRawName(DataRefImpl Sec) const;
474  ArrayRef<char> getSectionRawFinalSegmentName(DataRefImpl Sec) const;
475 
476  // MachO specific Info about relocations.
477  bool isRelocationScattered(const MachO::any_relocation_info &RE) const;
478  unsigned getPlainRelocationSymbolNum(
479  const MachO::any_relocation_info &RE) const;
480  bool getPlainRelocationExternal(const MachO::any_relocation_info &RE) const;
481  bool getScatteredRelocationScattered(
482  const MachO::any_relocation_info &RE) const;
483  uint32_t getScatteredRelocationValue(
484  const MachO::any_relocation_info &RE) const;
485  uint32_t getScatteredRelocationType(
486  const MachO::any_relocation_info &RE) const;
487  unsigned getAnyRelocationAddress(const MachO::any_relocation_info &RE) const;
488  unsigned getAnyRelocationPCRel(const MachO::any_relocation_info &RE) const;
489  unsigned getAnyRelocationLength(const MachO::any_relocation_info &RE) const;
490  unsigned getAnyRelocationType(const MachO::any_relocation_info &RE) const;
491  SectionRef getAnyRelocationSection(const MachO::any_relocation_info &RE) const;
492 
493  // MachO specific structures.
495  MachO::section_64 getSection64(DataRefImpl DRI) const;
496  MachO::section getSection(const LoadCommandInfo &L, unsigned Index) const;
497  MachO::section_64 getSection64(const LoadCommandInfo &L,unsigned Index) const;
498  MachO::nlist getSymbolTableEntry(DataRefImpl DRI) const;
499  MachO::nlist_64 getSymbol64TableEntry(DataRefImpl DRI) const;
500 
502  getLinkeditDataLoadCommand(const LoadCommandInfo &L) const;
504  getSegmentLoadCommand(const LoadCommandInfo &L) const;
506  getSegment64LoadCommand(const LoadCommandInfo &L) const;
508  getLinkerOptionLoadCommand(const LoadCommandInfo &L) const;
510  getVersionMinLoadCommand(const LoadCommandInfo &L) const;
512  getNoteLoadCommand(const LoadCommandInfo &L) const;
514  getBuildVersionLoadCommand(const LoadCommandInfo &L) const;
516  getBuildToolVersion(unsigned index) const;
518  getDylibIDLoadCommand(const LoadCommandInfo &L) const;
520  getDyldInfoLoadCommand(const LoadCommandInfo &L) const;
522  getDylinkerCommand(const LoadCommandInfo &L) const;
524  getUuidCommand(const LoadCommandInfo &L) const;
526  getRpathCommand(const LoadCommandInfo &L) const;
528  getSourceVersionCommand(const LoadCommandInfo &L) const;
530  getEntryPointCommand(const LoadCommandInfo &L) const;
532  getEncryptionInfoCommand(const LoadCommandInfo &L) const;
534  getEncryptionInfoCommand64(const LoadCommandInfo &L) const;
536  getSubFrameworkCommand(const LoadCommandInfo &L) const;
538  getSubUmbrellaCommand(const LoadCommandInfo &L) const;
540  getSubLibraryCommand(const LoadCommandInfo &L) const;
542  getSubClientCommand(const LoadCommandInfo &L) const;
544  getRoutinesCommand(const LoadCommandInfo &L) const;
546  getRoutinesCommand64(const LoadCommandInfo &L) const;
548  getThreadCommand(const LoadCommandInfo &L) const;
549 
550  MachO::any_relocation_info getRelocation(DataRefImpl Rel) const;
551  MachO::data_in_code_entry getDice(DataRefImpl Rel) const;
552  const MachO::mach_header &getHeader() const;
553  const MachO::mach_header_64 &getHeader64() const;
554  uint32_t
555  getIndirectSymbolTableEntry(const MachO::dysymtab_command &DLC,
556  unsigned Index) const;
557  MachO::data_in_code_entry getDataInCodeTableEntry(uint32_t DataOffset,
558  unsigned Index) const;
559  MachO::symtab_command getSymtabLoadCommand() const;
560  MachO::dysymtab_command getDysymtabLoadCommand() const;
561  MachO::linkedit_data_command getDataInCodeLoadCommand() const;
562  MachO::linkedit_data_command getLinkOptHintsLoadCommand() const;
563  ArrayRef<uint8_t> getDyldInfoRebaseOpcodes() const;
564  ArrayRef<uint8_t> getDyldInfoBindOpcodes() const;
565  ArrayRef<uint8_t> getDyldInfoWeakBindOpcodes() const;
566  ArrayRef<uint8_t> getDyldInfoLazyBindOpcodes() const;
567  ArrayRef<uint8_t> getDyldInfoExportsTrie() const;
568  ArrayRef<uint8_t> getUuid() const;
569 
570  StringRef getStringTableData() const;
571  bool is64Bit() const;
572  void ReadULEB128s(uint64_t Index, SmallVectorImpl<uint64_t> &Out) const;
573 
574  static StringRef guessLibraryShortName(StringRef Name, bool &isFramework,
575  StringRef &Suffix);
576 
577  static Triple::ArchType getArch(uint32_t CPUType);
578  static Triple getArchTriple(uint32_t CPUType, uint32_t CPUSubType,
579  const char **McpuDefault = nullptr,
580  const char **ArchFlag = nullptr);
581  static bool isValidArch(StringRef ArchFlag);
582  static Triple getHostArch();
583 
584  bool isRelocatableObject() const override;
585 
586  StringRef mapDebugSectionName(StringRef Name) const override;
587 
588  bool hasPageZeroSegment() const { return HasPageZeroSegment; }
589 
590  static bool classof(const Binary *v) {
591  return v->isMachO();
592  }
593 
594  static uint32_t
596  uint32_t VersionOrSDK = (SDK) ? C.sdk : C.version;
597  return (VersionOrSDK >> 16) & 0xffff;
598  }
599 
600  static uint32_t
602  uint32_t VersionOrSDK = (SDK) ? C.sdk : C.version;
603  return (VersionOrSDK >> 8) & 0xff;
604  }
605 
606  static uint32_t
608  uint32_t VersionOrSDK = (SDK) ? C.sdk : C.version;
609  return VersionOrSDK & 0xff;
610  }
611 
612  static std::string getBuildPlatform(uint32_t platform) {
613  switch (platform) {
614  case MachO::PLATFORM_MACOS: return "macos";
615  case MachO::PLATFORM_IOS: return "ios";
616  case MachO::PLATFORM_TVOS: return "tvos";
617  case MachO::PLATFORM_WATCHOS: return "watchos";
618  case MachO::PLATFORM_BRIDGEOS: return "bridgeos";
619  case MachO::PLATFORM_IOSSIMULATOR: return "iossimulator";
620  case MachO::PLATFORM_TVOSSIMULATOR: return "tvossimulator";
621  case MachO::PLATFORM_WATCHOSSIMULATOR: return "watchossimulator";
622  default:
623  std::string ret;
624  raw_string_ostream ss(ret);
625  ss << format_hex(platform, 8, true);
626  return ss.str();
627  }
628  }
629 
630  static std::string getBuildTool(uint32_t tools) {
631  switch (tools) {
632  case MachO::TOOL_CLANG: return "clang";
633  case MachO::TOOL_SWIFT: return "swift";
634  case MachO::TOOL_LD: return "ld";
635  default:
636  std::string ret;
637  raw_string_ostream ss(ret);
638  ss << format_hex(tools, 8, true);
639  return ss.str();
640  }
641  }
642 
643  static std::string getVersionString(uint32_t version) {
644  uint32_t major = (version >> 16) & 0xffff;
645  uint32_t minor = (version >> 8) & 0xff;
646  uint32_t update = version & 0xff;
647 
649  Version = utostr(major) + "." + utostr(minor);
650  if (update != 0)
651  Version += "." + utostr(update);
652  return Version.str();
653  }
654 
655 private:
656  MachOObjectFile(MemoryBufferRef Object, bool IsLittleEndian, bool Is64Bits,
657  Error &Err, uint32_t UniversalCputype = 0,
658  uint32_t UniversalIndex = 0);
659 
660  uint64_t getSymbolValueImpl(DataRefImpl Symb) const override;
661 
662  union {
665  };
667  SectionList Sections;
669  LibraryList Libraries;
670  LoadCommandList LoadCommands;
673  BuildToolList BuildTools;
674  mutable LibraryShortName LibrariesShortNames;
675  std::unique_ptr<BindRebaseSegInfo> BindRebaseSectionTable;
676  const char *SymtabLoadCmd = nullptr;
677  const char *DysymtabLoadCmd = nullptr;
678  const char *DataInCodeLoadCmd = nullptr;
679  const char *LinkOptHintsLoadCmd = nullptr;
680  const char *DyldInfoLoadCmd = nullptr;
681  const char *UuidLoadCmd = nullptr;
682  bool HasPageZeroSegment = false;
683 };
684 
685 /// DiceRef
686 inline DiceRef::DiceRef(DataRefImpl DiceP, const ObjectFile *Owner)
687  : DicePimpl(DiceP) , OwningObject(Owner) {}
688 
689 inline bool DiceRef::operator==(const DiceRef &Other) const {
690  return DicePimpl == Other.DicePimpl;
691 }
692 
693 inline bool DiceRef::operator<(const DiceRef &Other) const {
694  return DicePimpl < Other.DicePimpl;
695 }
696 
697 inline void DiceRef::moveNext() {
699  reinterpret_cast<const MachO::data_in_code_entry *>(DicePimpl.p);
700  DicePimpl.p = reinterpret_cast<uintptr_t>(P + 1);
701 }
702 
703 // Since a Mach-O data in code reference, a DiceRef, can only be created when
704 // the OwningObject ObjectFile is a MachOObjectFile a static_cast<> is used for
705 // the methods that get the values of the fields of the reference.
706 
707 inline std::error_code DiceRef::getOffset(uint32_t &Result) const {
708  const MachOObjectFile *MachOOF =
709  static_cast<const MachOObjectFile *>(OwningObject);
710  MachO::data_in_code_entry Dice = MachOOF->getDice(DicePimpl);
711  Result = Dice.offset;
712  return std::error_code();
713 }
714 
715 inline std::error_code DiceRef::getLength(uint16_t &Result) const {
716  const MachOObjectFile *MachOOF =
717  static_cast<const MachOObjectFile *>(OwningObject);
718  MachO::data_in_code_entry Dice = MachOOF->getDice(DicePimpl);
719  Result = Dice.length;
720  return std::error_code();
721 }
722 
723 inline std::error_code DiceRef::getKind(uint16_t &Result) const {
724  const MachOObjectFile *MachOOF =
725  static_cast<const MachOObjectFile *>(OwningObject);
726  MachO::data_in_code_entry Dice = MachOOF->getDice(DicePimpl);
727  Result = Dice.kind;
728  return std::error_code();
729 }
730 
732  return DicePimpl;
733 }
734 
735 inline const ObjectFile *DiceRef::getObjectFile() const {
736  return OwningObject;
737 }
738 
739 } // end namespace object
740 } // end namespace llvm
741 
742 #endif // LLVM_OBJECT_MACHO_H
DataRefImpl getRawDataRefImpl() const
Definition: MachO.h:731
uint64_t CallInst * C
const char * BindEntryCheckSegAndOffset(int32_t SegIndex, uint64_t SegOffset, bool endInvalid) const
For use with a SegIndex,SegOffset pair in MachOBindEntry::moveNext() to validate a MachOBindEntry...
Definition: MachO.h:418
static uint32_t getVersionMinUpdate(MachO::version_min_command &C, bool SDK)
Definition: MachO.h:607
StringRef BindRebaseSegmentName(int32_t SegIndex) const
For use with the SegIndex of a checked Mach-O Bind or Rebase entry to get the segment name...
Definition: MachO.h:450
typename SuperClass::const_iterator const_iterator
Definition: SmallVector.h:328
const ObjectFile * getObjectFile() const
Definition: MachO.h:735
This class represents lattice values for constants.
Definition: AllocatorList.h:24
FormattedNumber format_hex(uint64_t N, unsigned Width, bool Upper=false)
format_hex - Output N as a fixed width hexadecimal.
Definition: Format.h:186
ExportEntry encapsulates the current-state-of-the-walk used when doing a non-recursive walk of the tr...
Definition: MachO.h:73
const char * BindEntryCheckCountAndSkip(uint32_t Count, uint32_t Skip, uint8_t PointerSize, int32_t SegIndex, uint64_t SegOffset) const
For use in MachOBindEntry::moveNext() to validate a MachOBindEntry for the BIND_OPCODE_DO_BIND_ULEB_T...
Definition: MachO.h:425
This provides a very simple, boring adaptor for a begin and end iterator into a range type...
Definition: MachO.h:791
static std::string getVersionString(uint32_t version)
Definition: MachO.h:643
bool hasPageZeroSegment() const
Definition: MachO.h:588
const char * RebaseEntryCheckSegAndOffset(int32_t SegIndex, uint64_t SegOffset, bool endInvalid) const
For use with a SegIndex,SegOffset pair in MachORebaseEntry::moveNext() to validate a MachORebaseEntry...
Definition: MachO.h:434
This class is the base class for all object file types.
Definition: ObjectFile.h:202
#define error(X)
MachORebaseEntry encapsulates the current state in the decompression of rebasing opcodes.
Definition: MachO.h:170
static unsigned getSymbolSectionID(const ObjectFile &O, SymbolRef Sym)
Definition: SymbolSize.cpp:34
static std::string getBuildTool(uint32_t tools)
Definition: MachO.h:630
amdgpu Simplify well known AMD library false Value Value const Twine & Name
constexpr char SymbolName[]
Key for Kernel::Metadata::mSymbolName.
static StringRef getSymbolName(SymbolKind SymKind)
uint16_t length
Definition: MachO.h:793
Tagged union holding either a T or a Error.
Definition: CachePruning.h:23
MachOBindEntry encapsulates the current state in the decompression of binding opcodes.
Definition: MachO.h:214
ELFYAML::ELF_STO Other
Definition: ELFYAML.cpp:784
Expected< const typename ELFT::Shdr * > getSection(typename ELFT::ShdrRange Sections, uint32_t Index)
Definition: ELF.h:275
static std::string getBuildPlatform(uint32_t platform)
Definition: MachO.h:612
static uint64_t readULEB128(WasmObjectFile::ReadContext &Ctx)
StringRef str() const
Explicit conversion to StringRef.
Definition: SmallString.h:267
SubtargetFeatures getFeatures() const override
Definition: MachO.h:369
#define P(N)
static Constant * SegmentOffset(IRBuilder<> &IRB, unsigned Offset, unsigned AddressSpace)
uint64_t BindRebaseAddress(uint32_t SegIndex, uint64_t SegOffset) const
For use with a SegIndex,SegOffset pair from a checked Mach-O Bind or Rebase entry to get the address...
Definition: MachO.h:462
static GCRegistry::Add< CoreCLRGC > E("coreclr", "CoreCLR-compatible GC")
LLVM_ATTRIBUTE_ALWAYS_INLINE iterator begin()
Definition: SmallVector.h:129
static bool is64Bit(const char *name)
uint32_t offset
Definition: MachO.h:792
std::error_code getLength(uint16_t &Result) const
Definition: MachO.h:715
DiceRef - This is a value type class that represents a single data in code entry in the table in a Ma...
Definition: MachO.h:44
static unsigned getSectionID(const ObjectFile &O, SectionRef Sec)
Definition: SymbolSize.cpp:28
const char * RebaseEntryCheckCountAndSkip(uint32_t Count, uint32_t Skip, uint8_t PointerSize, int32_t SegIndex, uint64_t SegOffset) const
For use in MachORebaseEntry::moveNext() to validate a MachORebaseEntry for the REBASE_OPCODE_DO_*_TIM...
Definition: MachO.h:441
std::error_code getOffset(uint32_t &Result) const
Definition: MachO.h:707
std::string & str()
Flushes the stream contents to the target string and returns the string&#39;s reference.
Definition: raw_ostream.h:499
uint16_t kind
Definition: MachO.h:794
Triple - Helper class for working with autoconf configuration names.
Definition: Triple.h:44
iterator_range< T > make_range(T x, T y)
Convenience function for iterating over sub-ranges.
static uint32_t getVersionMinMinor(MachO::version_min_command &C, bool SDK)
Definition: MachO.h:601
MachO::data_in_code_entry getDice(DataRefImpl Rel) const
std::error_code getKind(uint16_t &Result) const
Definition: MachO.h:723
std::string utostr(uint64_t X, bool isNeg=false)
Definition: StringExtras.h:224
A range adaptor for a pair of iterators.
bool operator<(const DiceRef &Other) const
Definition: MachO.h:693
Manages the enabling and disabling of subtarget specific features.
This is a value type class that represents a single symbol in the list of symbols in the object file...
Definition: ObjectFile.h:141
static std::unique_ptr< PDBSymbol > getSymbolType(const PDBSymbol &Symbol)
Definition: UDTLayout.cpp:34
MachO::mach_header_64 Header64
Definition: MachO.h:663
bool operator==(const DiceRef &Other) const
Definition: MachO.h:689
LLVM_ATTRIBUTE_ALWAYS_INLINE iterator end()
Definition: SmallVector.h:133
iterator_range< typename GraphTraits< GraphType >::nodes_iterator > nodes(const GraphType &G)
Definition: GraphTraits.h:109
StringRef BindRebaseSectionName(uint32_t SegIndex, uint64_t SegOffset) const
For use with a SegIndex,SegOffset pair from a checked Mach-O Bind or Rebase entry to get the section ...
Definition: MachO.h:456
uint32_t Size
Definition: Profile.cpp:47
iterator_range< relocation_iterator > external_relocations() const
Definition: MachO.h:330
MachO::mach_header Header
Definition: MachO.h:664
A raw_ostream that writes to an std::string.
Definition: raw_ostream.h:483
static const char * name
LoadCommandList::const_iterator load_command_iterator
Definition: MachO.h:272
Lightweight error class with error context and mandatory checking.
Definition: Error.h:158
const char SectionName[]
Definition: AMDGPUPTNote.h:24
static uint32_t getVersionMinMajor(MachO::version_min_command &C, bool SDK)
Definition: MachO.h:595
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:49
static bool classof(const Binary *v)
Definition: MachO.h:590
bool isMachO() const
Definition: Binary.h:113
const uint64_t Version
Definition: InstrProf.h:895
This is a value type class that represents a single section in the list of sections in the object fil...
Definition: ObjectFile.h:81