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