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* checkSegAndOffsets(int32_t SegIndex, uint64_t SegOffset,
137  uint8_t PointerSize, uint32_t Count=1,
138  uint32_t Skip=0);
139  // Used with valid SegIndex/SegOffset values from checked entries.
140  StringRef segmentName(int32_t SegIndex);
141  StringRef sectionName(int32_t SegIndex, uint64_t SegOffset);
142  uint64_t address(uint32_t SegIndex, uint64_t SegOffset);
143 
144 private:
145  struct SectionInfo {
146  uint64_t Address;
147  uint64_t Size;
149  StringRef SegmentName;
150  uint64_t OffsetInSegment;
151  uint64_t SegmentStartAddress;
152  int32_t SegmentIndex;
153  };
154  const SectionInfo &findSection(int32_t SegIndex, uint64_t SegOffset);
155 
157  int32_t MaxSegIndex;
158 };
159 
160 /// MachORebaseEntry encapsulates the current state in the decompression of
161 /// rebasing opcodes. This allows you to iterate through the compressed table of
162 /// rebasing using:
163 /// Error Err;
164 /// for (const llvm::object::MachORebaseEntry &Entry : Obj->rebaseTable(&Err)) {
165 /// }
166 /// if (Err) { report error ...
168 public:
169  MachORebaseEntry(Error *Err, const MachOObjectFile *O,
170  ArrayRef<uint8_t> opcodes, bool is64Bit);
171 
172  int32_t segmentIndex() const;
173  uint64_t segmentOffset() const;
174  StringRef typeName() const;
175  StringRef segmentName() const;
176  StringRef sectionName() const;
177  uint64_t address() const;
178 
179  bool operator==(const MachORebaseEntry &) const;
180 
181  void moveNext();
182 
183 private:
184  friend class MachOObjectFile;
185 
186  void moveToFirst();
187  void moveToEnd();
188  uint64_t readULEB128(const char **error);
189 
190  Error *E;
191  const MachOObjectFile *O;
192  ArrayRef<uint8_t> Opcodes;
193  const uint8_t *Ptr;
194  uint64_t SegmentOffset = 0;
195  int32_t SegmentIndex = -1;
196  uint64_t RemainingLoopCount = 0;
197  uint64_t AdvanceAmount = 0;
198  uint8_t RebaseType = 0;
199  uint8_t PointerSize;
200  bool Done = false;
201 };
203 
204 /// MachOBindEntry encapsulates the current state in the decompression of
205 /// binding opcodes. This allows you to iterate through the compressed table of
206 /// bindings using:
207 /// Error Err;
208 /// for (const llvm::object::MachOBindEntry &Entry : Obj->bindTable(&Err)) {
209 /// }
210 /// if (Err) { report error ...
212 public:
213  enum class Kind { Regular, Lazy, Weak };
214 
215  MachOBindEntry(Error *Err, const MachOObjectFile *O,
217 
218  int32_t segmentIndex() const;
219  uint64_t segmentOffset() const;
220  StringRef typeName() const;
221  StringRef symbolName() const;
222  uint32_t flags() const;
223  int64_t addend() const;
224  int ordinal() const;
225 
226  StringRef segmentName() const;
227  StringRef sectionName() const;
228  uint64_t address() const;
229 
230  bool operator==(const MachOBindEntry &) const;
231 
232  void moveNext();
233 
234 private:
235  friend class MachOObjectFile;
236 
237  void moveToFirst();
238  void moveToEnd();
239  uint64_t readULEB128(const char **error);
240  int64_t readSLEB128(const char **error);
241 
242  Error *E;
243  const MachOObjectFile *O;
244  ArrayRef<uint8_t> Opcodes;
245  const uint8_t *Ptr;
246  uint64_t SegmentOffset = 0;
247  int32_t SegmentIndex = -1;
249  bool LibraryOrdinalSet = false;
250  int Ordinal = 0;
251  uint32_t Flags = 0;
252  int64_t Addend = 0;
253  uint64_t RemainingLoopCount = 0;
254  uint64_t AdvanceAmount = 0;
255  uint8_t BindType = 0;
256  uint8_t PointerSize;
257  Kind TableKind;
258  bool Done = false;
259 };
261 
262 class MachOObjectFile : public ObjectFile {
263 public:
265  const char *Ptr; // Where in memory the load command is.
266  MachO::load_command C; // The command itself.
267  };
270 
272  create(MemoryBufferRef Object, bool IsLittleEndian, bool Is64Bits,
273  uint32_t UniversalCputype = 0, uint32_t UniversalIndex = 0);
274 
275  void moveSymbolNext(DataRefImpl &Symb) const override;
276 
277  uint64_t getNValue(DataRefImpl Sym) const;
278  Expected<StringRef> getSymbolName(DataRefImpl Symb) const override;
279 
280  // MachO specific.
281  Error checkSymbolTable() const;
282 
283  std::error_code getIndirectName(DataRefImpl Symb, StringRef &Res) const;
284  unsigned getSectionType(SectionRef Sec) const;
285 
286  Expected<uint64_t> getSymbolAddress(DataRefImpl Symb) const override;
287  uint32_t getSymbolAlignment(DataRefImpl Symb) const override;
288  uint64_t getCommonSymbolSizeImpl(DataRefImpl Symb) const override;
290  uint32_t getSymbolFlags(DataRefImpl Symb) const override;
291  Expected<section_iterator> getSymbolSection(DataRefImpl Symb) const override;
292  unsigned getSymbolSectionID(SymbolRef Symb) const;
293  unsigned getSectionID(SectionRef Sec) const;
294 
295  void moveSectionNext(DataRefImpl &Sec) const override;
296  std::error_code getSectionName(DataRefImpl Sec,
297  StringRef &Res) const override;
298  uint64_t getSectionAddress(DataRefImpl Sec) const override;
299  uint64_t getSectionIndex(DataRefImpl Sec) const override;
300  uint64_t getSectionSize(DataRefImpl Sec) const override;
301  std::error_code getSectionContents(DataRefImpl Sec,
302  StringRef &Res) const override;
303  uint64_t getSectionAlignment(DataRefImpl Sec) const override;
304  Expected<SectionRef> getSection(unsigned SectionIndex) const;
306  bool isSectionCompressed(DataRefImpl Sec) const override;
307  bool isSectionText(DataRefImpl Sec) const override;
308  bool isSectionData(DataRefImpl Sec) const override;
309  bool isSectionBSS(DataRefImpl Sec) const override;
310  bool isSectionVirtual(DataRefImpl Sec) const override;
311  bool isSectionBitcode(DataRefImpl Sec) const override;
312 
313  /// When dsymutil generates the companion file, it strips all unnecessary
314  /// sections (e.g. everything in the _TEXT segment) by omitting their body
315  /// and setting the offset in their corresponding load command to zero.
316  ///
317  /// While the load command itself is valid, reading the section corresponds
318  /// to reading the number of bytes specified in the load command, starting
319  /// from offset 0 (i.e. the Mach-O header at the beginning of the file).
320  bool isSectionStripped(DataRefImpl Sec) const override;
321 
322  relocation_iterator section_rel_begin(DataRefImpl Sec) const override;
323  relocation_iterator section_rel_end(DataRefImpl Sec) const override;
324 
325  relocation_iterator extrel_begin() const;
326  relocation_iterator extrel_end() const;
328  return make_range(extrel_begin(), extrel_end());
329  }
330 
331  relocation_iterator locrel_begin() const;
332  relocation_iterator locrel_end() const;
333 
334  void moveRelocationNext(DataRefImpl &Rel) const override;
335  uint64_t getRelocationOffset(DataRefImpl Rel) const override;
336  symbol_iterator getRelocationSymbol(DataRefImpl Rel) const override;
337  section_iterator getRelocationSection(DataRefImpl Rel) const;
338  uint64_t getRelocationType(DataRefImpl Rel) const override;
339  void getRelocationTypeName(DataRefImpl Rel,
340  SmallVectorImpl<char> &Result) const override;
341  uint8_t getRelocationLength(DataRefImpl Rel) const;
342 
343  // MachO specific.
344  std::error_code getLibraryShortNameByIndex(unsigned Index, StringRef &) const;
345  uint32_t getLibraryCount() const;
346 
347  section_iterator getRelocationRelocatedSection(relocation_iterator Rel) const;
348 
349  // TODO: Would be useful to have an iterator based version
350  // of the load command interface too.
351 
352  basic_symbol_iterator symbol_begin() const override;
353  basic_symbol_iterator symbol_end() const override;
354 
355  // MachO specific.
356  symbol_iterator getSymbolByIndex(unsigned Index) const;
357  uint64_t getSymbolIndex(DataRefImpl Symb) const;
358 
359  section_iterator section_begin() const override;
360  section_iterator section_end() const override;
361 
362  uint8_t getBytesInAddress() const override;
363 
364  StringRef getFileFormatName() const override;
365  Triple::ArchType getArch() const override;
366  SubtargetFeatures getFeatures() const override { return SubtargetFeatures(); }
367  Triple getArchTriple(const char **McpuDefault = nullptr) const;
368 
369  relocation_iterator section_rel_begin(unsigned Index) const;
370  relocation_iterator section_rel_end(unsigned Index) const;
371 
372  dice_iterator begin_dices() const;
373  dice_iterator end_dices() const;
374 
375  load_command_iterator begin_load_commands() const;
376  load_command_iterator end_load_commands() const;
377  iterator_range<load_command_iterator> load_commands() const;
378 
379  /// For use iterating over all exported symbols.
380  iterator_range<export_iterator> exports(Error &Err) const;
381 
382  /// For use examining a trie not in a MachOObjectFile.
383  static iterator_range<export_iterator> exports(Error &Err,
384  ArrayRef<uint8_t> Trie,
385  const MachOObjectFile *O =
386  nullptr);
387 
388  /// For use iterating over all rebase table entries.
389  iterator_range<rebase_iterator> rebaseTable(Error &Err);
390 
391  /// For use examining rebase opcodes in a MachOObjectFile.
392  static iterator_range<rebase_iterator> rebaseTable(Error &Err,
393  MachOObjectFile *O,
394  ArrayRef<uint8_t> Opcodes,
395  bool is64);
396 
397  /// For use iterating over all bind table entries.
398  iterator_range<bind_iterator> bindTable(Error &Err);
399 
400  /// For use iterating over all lazy bind table entries.
401  iterator_range<bind_iterator> lazyBindTable(Error &Err);
402 
403  /// For use iterating over all weak bind table entries.
404  iterator_range<bind_iterator> weakBindTable(Error &Err);
405 
406  /// For use examining bind opcodes in a MachOObjectFile.
407  static iterator_range<bind_iterator> bindTable(Error &Err,
408  MachOObjectFile *O,
409  ArrayRef<uint8_t> Opcodes,
410  bool is64,
412 
413  // Given a SegIndex, SegOffset, and PointerSize, verify a valid section exists
414  // that fully contains a pointer at that location. Multiple fixups in a bind
415  // (such as with the BIND_OPCODE_DO_BIND_ULEB_TIMES_SKIPPING_ULEB opcode) can
416  // be tested via the Count and Skip parameters.
417  //
418  // This is used by MachOBindEntry::moveNext() to validate a MachOBindEntry.
419  const char *BindEntryCheckSegAndOffsets(int32_t SegIndex, uint64_t SegOffset,
420  uint8_t PointerSize, uint32_t Count=1,
421  uint32_t Skip=0) const {
422  return BindRebaseSectionTable->checkSegAndOffsets(SegIndex, SegOffset,
423  PointerSize, Count, Skip);
424  }
425 
426  // Given a SegIndex, SegOffset, and PointerSize, verify a valid section exists
427  // that fully contains a pointer at that location. Multiple fixups in a rebase
428  // (such as with the REBASE_OPCODE_DO_*_TIMES* opcodes) can be tested via the
429  // Count and Skip parameters.
430  //
431  // This is used by MachORebaseEntry::moveNext() to validate a MachORebaseEntry
432  const char *RebaseEntryCheckSegAndOffsets(int32_t SegIndex,
433  uint64_t SegOffset,
434  uint8_t PointerSize,
435  uint32_t Count=1,
436  uint32_t Skip=0) const {
437  return BindRebaseSectionTable->checkSegAndOffsets(SegIndex, SegOffset,
438  PointerSize, Count, Skip);
439  }
440 
441  /// For use with the SegIndex of a checked Mach-O Bind or Rebase entry to
442  /// get the segment name.
443  StringRef BindRebaseSegmentName(int32_t SegIndex) const {
444  return BindRebaseSectionTable->segmentName(SegIndex);
445  }
446 
447  /// For use with a SegIndex,SegOffset pair from a checked Mach-O Bind or
448  /// Rebase entry to get the section name.
449  StringRef BindRebaseSectionName(uint32_t SegIndex, uint64_t SegOffset) const {
450  return BindRebaseSectionTable->sectionName(SegIndex, SegOffset);
451  }
452 
453  /// For use with a SegIndex,SegOffset pair from a checked Mach-O Bind or
454  /// Rebase entry to get the address.
455  uint64_t BindRebaseAddress(uint32_t SegIndex, uint64_t SegOffset) const {
456  return BindRebaseSectionTable->address(SegIndex, SegOffset);
457  }
458 
459  // In a MachO file, sections have a segment name. This is used in the .o
460  // files. They have a single segment, but this field specifies which segment
461  // a section should be put in the final object.
462  StringRef getSectionFinalSegmentName(DataRefImpl Sec) const;
463 
464  // Names are stored as 16 bytes. These returns the raw 16 bytes without
465  // interpreting them as a C string.
466  ArrayRef<char> getSectionRawName(DataRefImpl Sec) const;
467  ArrayRef<char> getSectionRawFinalSegmentName(DataRefImpl Sec) const;
468 
469  // MachO specific Info about relocations.
470  bool isRelocationScattered(const MachO::any_relocation_info &RE) const;
471  unsigned getPlainRelocationSymbolNum(
472  const MachO::any_relocation_info &RE) const;
473  bool getPlainRelocationExternal(const MachO::any_relocation_info &RE) const;
474  bool getScatteredRelocationScattered(
475  const MachO::any_relocation_info &RE) const;
476  uint32_t getScatteredRelocationValue(
477  const MachO::any_relocation_info &RE) const;
478  uint32_t getScatteredRelocationType(
479  const MachO::any_relocation_info &RE) const;
480  unsigned getAnyRelocationAddress(const MachO::any_relocation_info &RE) const;
481  unsigned getAnyRelocationPCRel(const MachO::any_relocation_info &RE) const;
482  unsigned getAnyRelocationLength(const MachO::any_relocation_info &RE) const;
483  unsigned getAnyRelocationType(const MachO::any_relocation_info &RE) const;
484  SectionRef getAnyRelocationSection(const MachO::any_relocation_info &RE) const;
485 
486  // MachO specific structures.
488  MachO::section_64 getSection64(DataRefImpl DRI) const;
489  MachO::section getSection(const LoadCommandInfo &L, unsigned Index) const;
490  MachO::section_64 getSection64(const LoadCommandInfo &L,unsigned Index) const;
491  MachO::nlist getSymbolTableEntry(DataRefImpl DRI) const;
492  MachO::nlist_64 getSymbol64TableEntry(DataRefImpl DRI) const;
493 
495  getLinkeditDataLoadCommand(const LoadCommandInfo &L) const;
497  getSegmentLoadCommand(const LoadCommandInfo &L) const;
499  getSegment64LoadCommand(const LoadCommandInfo &L) const;
501  getLinkerOptionLoadCommand(const LoadCommandInfo &L) const;
503  getVersionMinLoadCommand(const LoadCommandInfo &L) const;
505  getNoteLoadCommand(const LoadCommandInfo &L) const;
507  getBuildVersionLoadCommand(const LoadCommandInfo &L) const;
509  getBuildToolVersion(unsigned index) const;
511  getDylibIDLoadCommand(const LoadCommandInfo &L) const;
513  getDyldInfoLoadCommand(const LoadCommandInfo &L) const;
515  getDylinkerCommand(const LoadCommandInfo &L) const;
517  getUuidCommand(const LoadCommandInfo &L) const;
519  getRpathCommand(const LoadCommandInfo &L) const;
521  getSourceVersionCommand(const LoadCommandInfo &L) const;
523  getEntryPointCommand(const LoadCommandInfo &L) const;
525  getEncryptionInfoCommand(const LoadCommandInfo &L) const;
527  getEncryptionInfoCommand64(const LoadCommandInfo &L) const;
529  getSubFrameworkCommand(const LoadCommandInfo &L) const;
531  getSubUmbrellaCommand(const LoadCommandInfo &L) const;
533  getSubLibraryCommand(const LoadCommandInfo &L) const;
535  getSubClientCommand(const LoadCommandInfo &L) const;
537  getRoutinesCommand(const LoadCommandInfo &L) const;
539  getRoutinesCommand64(const LoadCommandInfo &L) const;
541  getThreadCommand(const LoadCommandInfo &L) const;
542 
543  MachO::any_relocation_info getRelocation(DataRefImpl Rel) const;
544  MachO::data_in_code_entry getDice(DataRefImpl Rel) const;
545  const MachO::mach_header &getHeader() const;
546  const MachO::mach_header_64 &getHeader64() const;
547  uint32_t
548  getIndirectSymbolTableEntry(const MachO::dysymtab_command &DLC,
549  unsigned Index) const;
550  MachO::data_in_code_entry getDataInCodeTableEntry(uint32_t DataOffset,
551  unsigned Index) const;
552  MachO::symtab_command getSymtabLoadCommand() const;
553  MachO::dysymtab_command getDysymtabLoadCommand() const;
554  MachO::linkedit_data_command getDataInCodeLoadCommand() const;
555  MachO::linkedit_data_command getLinkOptHintsLoadCommand() const;
556  ArrayRef<uint8_t> getDyldInfoRebaseOpcodes() const;
557  ArrayRef<uint8_t> getDyldInfoBindOpcodes() const;
558  ArrayRef<uint8_t> getDyldInfoWeakBindOpcodes() const;
559  ArrayRef<uint8_t> getDyldInfoLazyBindOpcodes() const;
560  ArrayRef<uint8_t> getDyldInfoExportsTrie() const;
561  ArrayRef<uint8_t> getUuid() const;
562 
563  StringRef getStringTableData() const;
564  bool is64Bit() const;
565  void ReadULEB128s(uint64_t Index, SmallVectorImpl<uint64_t> &Out) const;
566 
567  static StringRef guessLibraryShortName(StringRef Name, bool &isFramework,
568  StringRef &Suffix);
569 
570  static Triple::ArchType getArch(uint32_t CPUType);
571  static Triple getArchTriple(uint32_t CPUType, uint32_t CPUSubType,
572  const char **McpuDefault = nullptr,
573  const char **ArchFlag = nullptr);
574  static bool isValidArch(StringRef ArchFlag);
575  static Triple getHostArch();
576 
577  bool isRelocatableObject() const override;
578 
579  StringRef mapDebugSectionName(StringRef Name) const override;
580 
581  bool hasPageZeroSegment() const { return HasPageZeroSegment; }
582 
583  static bool classof(const Binary *v) {
584  return v->isMachO();
585  }
586 
587  static uint32_t
589  uint32_t VersionOrSDK = (SDK) ? C.sdk : C.version;
590  return (VersionOrSDK >> 16) & 0xffff;
591  }
592 
593  static uint32_t
595  uint32_t VersionOrSDK = (SDK) ? C.sdk : C.version;
596  return (VersionOrSDK >> 8) & 0xff;
597  }
598 
599  static uint32_t
601  uint32_t VersionOrSDK = (SDK) ? C.sdk : C.version;
602  return VersionOrSDK & 0xff;
603  }
604 
605  static std::string getBuildPlatform(uint32_t platform) {
606  switch (platform) {
607  case MachO::PLATFORM_MACOS: return "macos";
608  case MachO::PLATFORM_IOS: return "ios";
609  case MachO::PLATFORM_TVOS: return "tvos";
610  case MachO::PLATFORM_WATCHOS: return "watchos";
611  case MachO::PLATFORM_BRIDGEOS: return "bridgeos";
612  case MachO::PLATFORM_IOSSIMULATOR: return "iossimulator";
613  case MachO::PLATFORM_TVOSSIMULATOR: return "tvossimulator";
614  case MachO::PLATFORM_WATCHOSSIMULATOR: return "watchossimulator";
615  default:
616  std::string ret;
617  raw_string_ostream ss(ret);
618  ss << format_hex(platform, 8, true);
619  return ss.str();
620  }
621  }
622 
623  static std::string getBuildTool(uint32_t tools) {
624  switch (tools) {
625  case MachO::TOOL_CLANG: return "clang";
626  case MachO::TOOL_SWIFT: return "swift";
627  case MachO::TOOL_LD: return "ld";
628  default:
629  std::string ret;
630  raw_string_ostream ss(ret);
631  ss << format_hex(tools, 8, true);
632  return ss.str();
633  }
634  }
635 
636  static std::string getVersionString(uint32_t version) {
637  uint32_t major = (version >> 16) & 0xffff;
638  uint32_t minor = (version >> 8) & 0xff;
639  uint32_t update = version & 0xff;
640 
642  Version = utostr(major) + "." + utostr(minor);
643  if (update != 0)
644  Version += "." + utostr(update);
645  return Version.str();
646  }
647 
648 private:
649  MachOObjectFile(MemoryBufferRef Object, bool IsLittleEndian, bool Is64Bits,
650  Error &Err, uint32_t UniversalCputype = 0,
651  uint32_t UniversalIndex = 0);
652 
653  uint64_t getSymbolValueImpl(DataRefImpl Symb) const override;
654 
655  union {
658  };
660  SectionList Sections;
662  LibraryList Libraries;
663  LoadCommandList LoadCommands;
666  BuildToolList BuildTools;
667  mutable LibraryShortName LibrariesShortNames;
668  std::unique_ptr<BindRebaseSegInfo> BindRebaseSectionTable;
669  const char *SymtabLoadCmd = nullptr;
670  const char *DysymtabLoadCmd = nullptr;
671  const char *DataInCodeLoadCmd = nullptr;
672  const char *LinkOptHintsLoadCmd = nullptr;
673  const char *DyldInfoLoadCmd = nullptr;
674  const char *UuidLoadCmd = nullptr;
675  bool HasPageZeroSegment = false;
676 };
677 
678 /// DiceRef
679 inline DiceRef::DiceRef(DataRefImpl DiceP, const ObjectFile *Owner)
680  : DicePimpl(DiceP) , OwningObject(Owner) {}
681 
682 inline bool DiceRef::operator==(const DiceRef &Other) const {
683  return DicePimpl == Other.DicePimpl;
684 }
685 
686 inline bool DiceRef::operator<(const DiceRef &Other) const {
687  return DicePimpl < Other.DicePimpl;
688 }
689 
690 inline void DiceRef::moveNext() {
692  reinterpret_cast<const MachO::data_in_code_entry *>(DicePimpl.p);
693  DicePimpl.p = reinterpret_cast<uintptr_t>(P + 1);
694 }
695 
696 // Since a Mach-O data in code reference, a DiceRef, can only be created when
697 // the OwningObject ObjectFile is a MachOObjectFile a static_cast<> is used for
698 // the methods that get the values of the fields of the reference.
699 
700 inline std::error_code DiceRef::getOffset(uint32_t &Result) const {
701  const MachOObjectFile *MachOOF =
702  static_cast<const MachOObjectFile *>(OwningObject);
703  MachO::data_in_code_entry Dice = MachOOF->getDice(DicePimpl);
704  Result = Dice.offset;
705  return std::error_code();
706 }
707 
708 inline std::error_code DiceRef::getLength(uint16_t &Result) const {
709  const MachOObjectFile *MachOOF =
710  static_cast<const MachOObjectFile *>(OwningObject);
711  MachO::data_in_code_entry Dice = MachOOF->getDice(DicePimpl);
712  Result = Dice.length;
713  return std::error_code();
714 }
715 
716 inline std::error_code DiceRef::getKind(uint16_t &Result) const {
717  const MachOObjectFile *MachOOF =
718  static_cast<const MachOObjectFile *>(OwningObject);
719  MachO::data_in_code_entry Dice = MachOOF->getDice(DicePimpl);
720  Result = Dice.kind;
721  return std::error_code();
722 }
723 
725  return DicePimpl;
726 }
727 
728 inline const ObjectFile *DiceRef::getObjectFile() const {
729  return OwningObject;
730 }
731 
732 } // end namespace object
733 } // end namespace llvm
734 
735 #endif // LLVM_OBJECT_MACHO_H
DataRefImpl getRawDataRefImpl() const
Definition: MachO.h:724
uint64_t CallInst * C
static uint32_t getVersionMinUpdate(MachO::version_min_command &C, bool SDK)
Definition: MachO.h:600
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:443
typename SuperClass::const_iterator const_iterator
Definition: SmallVector.h:320
const ObjectFile * getObjectFile() const
Definition: MachO.h:728
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
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:636
bool hasPageZeroSegment() const
Definition: MachO.h:581
This class is the base class for all object file types.
Definition: ObjectFile.h:225
#define error(X)
MachORebaseEntry encapsulates the current state in the decompression of rebasing opcodes.
Definition: MachO.h:167
static unsigned getSymbolSectionID(const ObjectFile &O, SymbolRef Sym)
Definition: SymbolSize.cpp:33
static std::string getBuildTool(uint32_t tools)
Definition: MachO.h:623
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:211
ELFYAML::ELF_STO Other
Definition: ELFYAML.cpp:849
Expected< const typename ELFT::Shdr * > getSection(typename ELFT::ShdrRange Sections, uint32_t Index)
Definition: ELF.h:272
static std::string getBuildPlatform(uint32_t platform)
Definition: MachO.h:605
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:366
const char * BindEntryCheckSegAndOffsets(int32_t SegIndex, uint64_t SegOffset, uint8_t PointerSize, uint32_t Count=1, uint32_t Skip=0) const
Definition: MachO.h:419
#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:455
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:708
const char * RebaseEntryCheckSegAndOffsets(int32_t SegIndex, uint64_t SegOffset, uint8_t PointerSize, uint32_t Count=1, uint32_t Skip=0) const
Definition: MachO.h:432
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
std::error_code getOffset(uint32_t &Result) const
Definition: MachO.h:700
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:594
MachO::data_in_code_entry getDice(DataRefImpl Rel) const
std::error_code getKind(uint16_t &Result) const
Definition: MachO.h:716
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:686
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:164
static std::unique_ptr< PDBSymbol > getSymbolType(const PDBSymbol &Symbol)
Definition: UDTLayout.cpp:33
MachO::mach_header_64 Header64
Definition: MachO.h:656
bool operator==(const DiceRef &Other) const
Definition: MachO.h:682
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:449
uint32_t Size
Definition: Profile.cpp:46
iterator_range< relocation_iterator > external_relocations() const
Definition: MachO.h:327
MachO::mach_header Header
Definition: MachO.h:657
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:269
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:588
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:48
static bool classof(const Binary *v)
Definition: MachO.h:583
bool isMachO() const
Definition: Binary.h:116
const uint64_t Version
Definition: InstrProf.h:904
This is a value type class that represents a single section in the list of sections in the object fil...
Definition: ObjectFile.h:80