1 //===- MachO.h - MachO object file implementation ---------------*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This file declares the MachOObjectFile class, which implement the ObjectFile
11 // interface for MachO files.
13 //===----------------------------------------------------------------------===//
15 #ifndef LLVM_OBJECT_MACHO_H
16 #define LLVM_OBJECT_MACHO_H
18 #include "llvm/ADT/ArrayRef.h"
19 #include "llvm/ADT/SmallString.h"
20 #include "llvm/ADT/SmallVector.h"
21 #include "llvm/ADT/StringExtras.h"
22 #include "llvm/ADT/StringRef.h"
23 #include "llvm/ADT/Triple.h"
24 #include "llvm/ADT/iterator_range.h"
25 #include "llvm/BinaryFormat/MachO.h"
26 #include "llvm/MC/SubtargetFeature.h"
27 #include "llvm/Object/Binary.h"
28 #include "llvm/Object/ObjectFile.h"
29 #include "llvm/Object/SymbolicFile.h"
30 #include "llvm/Support/Error.h"
31 #include "llvm/Support/Format.h"
32 #include "llvm/Support/MemoryBuffer.h"
33 #include "llvm/Support/raw_ostream.h"
37 #include <system_error>
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.
45 DataRefImpl DicePimpl;
46 const ObjectFile *OwningObject = nullptr;
50 DiceRef(DataRefImpl DiceP, const ObjectFile *Owner);
52 bool operator==(const DiceRef &Other) const;
53 bool operator<(const DiceRef &Other) const;
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;
61 DataRefImpl getRawDataRefImpl() const;
62 const ObjectFile *getObjectFile() const;
64 using dice_iterator = content_iterator<DiceRef>;
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:
70 /// for (const llvm::object::ExportEntry &AnExport : Obj->exports(&Err)) {
72 /// if (Err) { report error ...
75 ExportEntry(Error *Err, const MachOObjectFile *O, ArrayRef<uint8_t> Trie);
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;
84 bool operator==(const ExportEntry &) const;
89 friend class MachOObjectFile;
93 uint64_t readULEB128(const uint8_t *&p, const char **error);
94 void pushDownUntilBottom();
95 void pushNode(uint64_t Offset);
97 // Represents a node in the mach-o exports trie.
99 NodeState(const uint8_t *Ptr);
101 const uint8_t *Start;
102 const uint8_t *Current;
104 uint64_t Address = 0;
106 const char *ImportName = nullptr;
107 unsigned ChildCount = 0;
108 unsigned NextChildIndex = 0;
109 unsigned ParentStringLength = 0;
110 bool IsExportNode = false;
112 using NodeList = SmallVector<NodeState, 16>;
113 using node_iterator = NodeList::const_iterator;
116 const MachOObjectFile *O;
117 ArrayRef<uint8_t> Trie;
118 SmallString<256> CumulativeString;
122 iterator_range<node_iterator> nodes() const {
123 return make_range(Stack.begin(), Stack.end());
126 using export_iterator = content_iterator<ExportEntry>;
128 // Segment info so SegIndex/SegOffset pairs in a Mach-O Bind or Rebase entry
129 // can be checked and translated. Only the SegIndex/SegOffset pairs from
130 // checked entries are to be used with the segmentName(), sectionName() and
131 // address() methods below.
132 class BindRebaseSegInfo {
134 BindRebaseSegInfo(const MachOObjectFile *Obj);
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,
139 const char *checkCountAndSkip(uint32_t Count, uint32_t Skip,
140 uint8_t PointerSize, int32_t SegIndex,
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);
151 StringRef SectionName;
152 StringRef SegmentName;
153 uint64_t OffsetInSegment;
154 uint64_t SegmentStartAddress;
155 int32_t SegmentIndex;
157 const SectionInfo &findSection(int32_t SegIndex, uint64_t SegOffset);
159 SmallVector<SectionInfo, 32> Sections;
163 /// MachORebaseEntry encapsulates the current state in the decompression of
164 /// rebasing opcodes. This allows you to iterate through the compressed table of
167 /// for (const llvm::object::MachORebaseEntry &Entry : Obj->rebaseTable(&Err)) {
169 /// if (Err) { report error ...
170 class MachORebaseEntry {
172 MachORebaseEntry(Error *Err, const MachOObjectFile *O,
173 ArrayRef<uint8_t> opcodes, bool is64Bit);
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;
182 bool operator==(const MachORebaseEntry &) const;
187 friend class MachOObjectFile;
191 uint64_t readULEB128(const char **error);
194 const MachOObjectFile *O;
195 ArrayRef<uint8_t> Opcodes;
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;
205 using rebase_iterator = content_iterator<MachORebaseEntry>;
207 /// MachOBindEntry encapsulates the current state in the decompression of
208 /// binding opcodes. This allows you to iterate through the compressed table of
211 /// for (const llvm::object::MachOBindEntry &Entry : Obj->bindTable(&Err)) {
213 /// if (Err) { report error ...
214 class MachOBindEntry {
216 enum class Kind { Regular, Lazy, Weak };
218 MachOBindEntry(Error *Err, const MachOObjectFile *O,
219 ArrayRef<uint8_t> Opcodes, bool is64Bit, MachOBindEntry::Kind);
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;
229 StringRef segmentName() const;
230 StringRef sectionName() const;
231 uint64_t address() const;
233 bool operator==(const MachOBindEntry &) const;
238 friend class MachOObjectFile;
242 uint64_t readULEB128(const char **error);
243 int64_t readSLEB128(const char **error);
246 const MachOObjectFile *O;
247 ArrayRef<uint8_t> Opcodes;
249 uint64_t SegmentOffset = 0;
250 int32_t SegmentIndex = -1;
251 StringRef SymbolName;
252 bool LibraryOrdinalSet = false;
256 uint64_t RemainingLoopCount = 0;
257 uint64_t AdvanceAmount = 0;
258 uint8_t BindType = 0;
263 using bind_iterator = content_iterator<MachOBindEntry>;
265 class MachOObjectFile : public ObjectFile {
267 struct LoadCommandInfo {
268 const char *Ptr; // Where in memory the load command is.
269 MachO::load_command C; // The command itself.
271 using LoadCommandList = SmallVector<LoadCommandInfo, 4>;
272 using load_command_iterator = LoadCommandList::const_iterator;
274 static Expected<std::unique_ptr<MachOObjectFile>>
275 create(MemoryBufferRef Object, bool IsLittleEndian, bool Is64Bits,
276 uint32_t UniversalCputype = 0, uint32_t UniversalIndex = 0);
278 void moveSymbolNext(DataRefImpl &Symb) const override;
280 uint64_t getNValue(DataRefImpl Sym) const;
281 Expected<StringRef> getSymbolName(DataRefImpl Symb) const override;
284 Error checkSymbolTable() const;
286 std::error_code getIndirectName(DataRefImpl Symb, StringRef &Res) const;
287 unsigned getSectionType(SectionRef Sec) const;
289 Expected<uint64_t> getSymbolAddress(DataRefImpl Symb) const override;
290 uint32_t getSymbolAlignment(DataRefImpl Symb) const override;
291 uint64_t getCommonSymbolSizeImpl(DataRefImpl Symb) const override;
292 Expected<SymbolRef::Type> getSymbolType(DataRefImpl Symb) const override;
293 uint32_t getSymbolFlags(DataRefImpl Symb) const override;
294 Expected<section_iterator> getSymbolSection(DataRefImpl Symb) const override;
295 unsigned getSymbolSectionID(SymbolRef Symb) const;
296 unsigned getSectionID(SectionRef Sec) const;
298 void moveSectionNext(DataRefImpl &Sec) const override;
299 std::error_code getSectionName(DataRefImpl Sec,
300 StringRef &Res) const override;
301 uint64_t getSectionAddress(DataRefImpl Sec) const override;
302 uint64_t getSectionIndex(DataRefImpl Sec) const override;
303 uint64_t getSectionSize(DataRefImpl Sec) const override;
304 std::error_code getSectionContents(DataRefImpl Sec,
305 StringRef &Res) const override;
306 uint64_t getSectionAlignment(DataRefImpl Sec) const override;
307 bool isSectionCompressed(DataRefImpl Sec) const override;
308 bool isSectionText(DataRefImpl Sec) const override;
309 bool isSectionData(DataRefImpl Sec) const override;
310 bool isSectionBSS(DataRefImpl Sec) const override;
311 bool isSectionVirtual(DataRefImpl Sec) const override;
312 bool isSectionBitcode(DataRefImpl Sec) const override;
314 /// When dsymutil generates the companion file, it strips all unnecessary
315 /// sections (e.g. everything in the _TEXT segment) by omitting their body
316 /// and setting the offset in their corresponding load command to zero.
318 /// While the load command itself is valid, reading the section corresponds
319 /// to reading the number of bytes specified in the load command, starting
320 /// from offset 0 (i.e. the Mach-O header at the beginning of the file).
321 bool isSectionStripped(DataRefImpl Sec) const override;
323 relocation_iterator section_rel_begin(DataRefImpl Sec) const override;
324 relocation_iterator section_rel_end(DataRefImpl Sec) const override;
326 relocation_iterator extrel_begin() const;
327 relocation_iterator extrel_end() const;
328 iterator_range<relocation_iterator> external_relocations() const {
329 return make_range(extrel_begin(), extrel_end());
332 relocation_iterator locrel_begin() const;
333 relocation_iterator locrel_end() const;
335 void moveRelocationNext(DataRefImpl &Rel) const override;
336 uint64_t getRelocationOffset(DataRefImpl Rel) const override;
337 symbol_iterator getRelocationSymbol(DataRefImpl Rel) const override;
338 section_iterator getRelocationSection(DataRefImpl Rel) const;
339 uint64_t getRelocationType(DataRefImpl Rel) const override;
340 void getRelocationTypeName(DataRefImpl Rel,
341 SmallVectorImpl<char> &Result) const override;
342 uint8_t getRelocationLength(DataRefImpl Rel) const;
345 std::error_code getLibraryShortNameByIndex(unsigned Index, StringRef &) const;
346 uint32_t getLibraryCount() const;
348 section_iterator getRelocationRelocatedSection(relocation_iterator Rel) const;
350 // TODO: Would be useful to have an iterator based version
351 // of the load command interface too.
353 basic_symbol_iterator symbol_begin() const override;
354 basic_symbol_iterator symbol_end() const override;
357 basic_symbol_iterator getSymbolByIndex(unsigned Index) const;
358 uint64_t getSymbolIndex(DataRefImpl Symb) const;
360 section_iterator section_begin() const override;
361 section_iterator section_end() const override;
363 uint8_t getBytesInAddress() const override;
365 StringRef getFileFormatName() const override;
366 Triple::ArchType getArch() const override;
367 SubtargetFeatures getFeatures() const override { return SubtargetFeatures(); }
368 Triple getArchTriple(const char **McpuDefault = nullptr) const;
370 relocation_iterator section_rel_begin(unsigned Index) const;
371 relocation_iterator section_rel_end(unsigned Index) const;
373 dice_iterator begin_dices() const;
374 dice_iterator end_dices() const;
376 load_command_iterator begin_load_commands() const;
377 load_command_iterator end_load_commands() const;
378 iterator_range<load_command_iterator> load_commands() const;
380 /// For use iterating over all exported symbols.
381 iterator_range<export_iterator> exports(Error &Err) const;
383 /// For use examining a trie not in a MachOObjectFile.
384 static iterator_range<export_iterator> exports(Error &Err,
385 ArrayRef<uint8_t> Trie,
386 const MachOObjectFile *O =
389 /// For use iterating over all rebase table entries.
390 iterator_range<rebase_iterator> rebaseTable(Error &Err);
392 /// For use examining rebase opcodes in a MachOObjectFile.
393 static iterator_range<rebase_iterator> rebaseTable(Error &Err,
395 ArrayRef<uint8_t> Opcodes,
398 /// For use iterating over all bind table entries.
399 iterator_range<bind_iterator> bindTable(Error &Err);
401 /// For use iterating over all lazy bind table entries.
402 iterator_range<bind_iterator> lazyBindTable(Error &Err);
404 /// For use iterating over all weak bind table entries.
405 iterator_range<bind_iterator> weakBindTable(Error &Err);
407 /// For use examining bind opcodes in a MachOObjectFile.
408 static iterator_range<bind_iterator> bindTable(Error &Err,
410 ArrayRef<uint8_t> Opcodes,
412 MachOBindEntry::Kind);
414 /// For use with a SegIndex,SegOffset pair in MachOBindEntry::moveNext() to
415 /// validate a MachOBindEntry.
416 const char *BindEntryCheckSegAndOffset(int32_t SegIndex, uint64_t SegOffset,
417 bool endInvalid) const {
418 return BindRebaseSectionTable->checkSegAndOffset(SegIndex, SegOffset,
421 /// For use in MachOBindEntry::moveNext() to validate a MachOBindEntry for
422 /// the BIND_OPCODE_DO_BIND_ULEB_TIMES_SKIPPING_ULEB opcode.
423 const char *BindEntryCheckCountAndSkip(uint32_t Count, uint32_t Skip,
424 uint8_t PointerSize, int32_t SegIndex,
425 uint64_t SegOffset) const {
426 return BindRebaseSectionTable->checkCountAndSkip(Count, Skip, PointerSize,
427 SegIndex, SegOffset);
430 /// For use with a SegIndex,SegOffset pair in MachORebaseEntry::moveNext() to
431 /// validate a MachORebaseEntry.
432 const char *RebaseEntryCheckSegAndOffset(int32_t SegIndex, uint64_t SegOffset,
433 bool endInvalid) const {
434 return BindRebaseSectionTable->checkSegAndOffset(SegIndex, SegOffset,
437 /// For use in MachORebaseEntry::moveNext() to validate a MachORebaseEntry for
438 /// the REBASE_OPCODE_DO_*_TIMES* opcodes.
439 const char *RebaseEntryCheckCountAndSkip(uint32_t Count, uint32_t Skip,
440 uint8_t PointerSize, int32_t SegIndex,
441 uint64_t SegOffset) const {
442 return BindRebaseSectionTable->checkCountAndSkip(Count, Skip, PointerSize,
443 SegIndex, SegOffset);
446 /// For use with the SegIndex of a checked Mach-O Bind or Rebase entry to
447 /// get the segment name.
448 StringRef BindRebaseSegmentName(int32_t SegIndex) const {
449 return BindRebaseSectionTable->segmentName(SegIndex);
452 /// For use with a SegIndex,SegOffset pair from a checked Mach-O Bind or
453 /// Rebase entry to get the section name.
454 StringRef BindRebaseSectionName(uint32_t SegIndex, uint64_t SegOffset) const {
455 return BindRebaseSectionTable->sectionName(SegIndex, SegOffset);
458 /// For use with a SegIndex,SegOffset pair from a checked Mach-O Bind or
459 /// Rebase entry to get the address.
460 uint64_t BindRebaseAddress(uint32_t SegIndex, uint64_t SegOffset) const {
461 return BindRebaseSectionTable->address(SegIndex, SegOffset);
464 // In a MachO file, sections have a segment name. This is used in the .o
465 // files. They have a single segment, but this field specifies which segment
466 // a section should be put in in the final object.
467 StringRef getSectionFinalSegmentName(DataRefImpl Sec) const;
469 // Names are stored as 16 bytes. These returns the raw 16 bytes without
470 // interpreting them as a C string.
471 ArrayRef<char> getSectionRawName(DataRefImpl Sec) const;
472 ArrayRef<char> getSectionRawFinalSegmentName(DataRefImpl Sec) const;
474 // MachO specific Info about relocations.
475 bool isRelocationScattered(const MachO::any_relocation_info &RE) const;
476 unsigned getPlainRelocationSymbolNum(
477 const MachO::any_relocation_info &RE) const;
478 bool getPlainRelocationExternal(const MachO::any_relocation_info &RE) const;
479 bool getScatteredRelocationScattered(
480 const MachO::any_relocation_info &RE) const;
481 uint32_t getScatteredRelocationValue(
482 const MachO::any_relocation_info &RE) const;
483 uint32_t getScatteredRelocationType(
484 const MachO::any_relocation_info &RE) const;
485 unsigned getAnyRelocationAddress(const MachO::any_relocation_info &RE) const;
486 unsigned getAnyRelocationPCRel(const MachO::any_relocation_info &RE) const;
487 unsigned getAnyRelocationLength(const MachO::any_relocation_info &RE) const;
488 unsigned getAnyRelocationType(const MachO::any_relocation_info &RE) const;
489 SectionRef getAnyRelocationSection(const MachO::any_relocation_info &RE) const;
491 // MachO specific structures.
492 MachO::section getSection(DataRefImpl DRI) const;
493 MachO::section_64 getSection64(DataRefImpl DRI) const;
494 MachO::section getSection(const LoadCommandInfo &L, unsigned Index) const;
495 MachO::section_64 getSection64(const LoadCommandInfo &L,unsigned Index) const;
496 MachO::nlist getSymbolTableEntry(DataRefImpl DRI) const;
497 MachO::nlist_64 getSymbol64TableEntry(DataRefImpl DRI) const;
499 MachO::linkedit_data_command
500 getLinkeditDataLoadCommand(const LoadCommandInfo &L) const;
501 MachO::segment_command
502 getSegmentLoadCommand(const LoadCommandInfo &L) const;
503 MachO::segment_command_64
504 getSegment64LoadCommand(const LoadCommandInfo &L) const;
505 MachO::linker_option_command
506 getLinkerOptionLoadCommand(const LoadCommandInfo &L) const;
507 MachO::version_min_command
508 getVersionMinLoadCommand(const LoadCommandInfo &L) const;
510 getNoteLoadCommand(const LoadCommandInfo &L) const;
511 MachO::build_version_command
512 getBuildVersionLoadCommand(const LoadCommandInfo &L) const;
513 MachO::build_tool_version
514 getBuildToolVersion(unsigned index) const;
516 getDylibIDLoadCommand(const LoadCommandInfo &L) const;
517 MachO::dyld_info_command
518 getDyldInfoLoadCommand(const LoadCommandInfo &L) const;
519 MachO::dylinker_command
520 getDylinkerCommand(const LoadCommandInfo &L) const;
522 getUuidCommand(const LoadCommandInfo &L) const;
524 getRpathCommand(const LoadCommandInfo &L) const;
525 MachO::source_version_command
526 getSourceVersionCommand(const LoadCommandInfo &L) const;
527 MachO::entry_point_command
528 getEntryPointCommand(const LoadCommandInfo &L) const;
529 MachO::encryption_info_command
530 getEncryptionInfoCommand(const LoadCommandInfo &L) const;
531 MachO::encryption_info_command_64
532 getEncryptionInfoCommand64(const LoadCommandInfo &L) const;
533 MachO::sub_framework_command
534 getSubFrameworkCommand(const LoadCommandInfo &L) const;
535 MachO::sub_umbrella_command
536 getSubUmbrellaCommand(const LoadCommandInfo &L) const;
537 MachO::sub_library_command
538 getSubLibraryCommand(const LoadCommandInfo &L) const;
539 MachO::sub_client_command
540 getSubClientCommand(const LoadCommandInfo &L) const;
541 MachO::routines_command
542 getRoutinesCommand(const LoadCommandInfo &L) const;
543 MachO::routines_command_64
544 getRoutinesCommand64(const LoadCommandInfo &L) const;
545 MachO::thread_command
546 getThreadCommand(const LoadCommandInfo &L) const;
548 MachO::any_relocation_info getRelocation(DataRefImpl Rel) const;
549 MachO::data_in_code_entry getDice(DataRefImpl Rel) const;
550 const MachO::mach_header &getHeader() const;
551 const MachO::mach_header_64 &getHeader64() const;
553 getIndirectSymbolTableEntry(const MachO::dysymtab_command &DLC,
554 unsigned Index) const;
555 MachO::data_in_code_entry getDataInCodeTableEntry(uint32_t DataOffset,
556 unsigned Index) const;
557 MachO::symtab_command getSymtabLoadCommand() const;
558 MachO::dysymtab_command getDysymtabLoadCommand() const;
559 MachO::linkedit_data_command getDataInCodeLoadCommand() const;
560 MachO::linkedit_data_command getLinkOptHintsLoadCommand() const;
561 ArrayRef<uint8_t> getDyldInfoRebaseOpcodes() const;
562 ArrayRef<uint8_t> getDyldInfoBindOpcodes() const;
563 ArrayRef<uint8_t> getDyldInfoWeakBindOpcodes() const;
564 ArrayRef<uint8_t> getDyldInfoLazyBindOpcodes() const;
565 ArrayRef<uint8_t> getDyldInfoExportsTrie() const;
566 ArrayRef<uint8_t> getUuid() const;
568 StringRef getStringTableData() const;
569 bool is64Bit() const;
570 void ReadULEB128s(uint64_t Index, SmallVectorImpl<uint64_t> &Out) const;
572 static StringRef guessLibraryShortName(StringRef Name, bool &isFramework,
575 static Triple::ArchType getArch(uint32_t CPUType);
576 static Triple getArchTriple(uint32_t CPUType, uint32_t CPUSubType,
577 const char **McpuDefault = nullptr,
578 const char **ArchFlag = nullptr);
579 static bool isValidArch(StringRef ArchFlag);
580 static Triple getHostArch();
582 bool isRelocatableObject() const override;
584 StringRef mapDebugSectionName(StringRef Name) const override;
586 bool hasPageZeroSegment() const { return HasPageZeroSegment; }
588 static bool classof(const Binary *v) {
593 getVersionMinMajor(MachO::version_min_command &C, bool SDK) {
594 uint32_t VersionOrSDK = (SDK) ? C.sdk : C.version;
595 return (VersionOrSDK >> 16) & 0xffff;
599 getVersionMinMinor(MachO::version_min_command &C, bool SDK) {
600 uint32_t VersionOrSDK = (SDK) ? C.sdk : C.version;
601 return (VersionOrSDK >> 8) & 0xff;
605 getVersionMinUpdate(MachO::version_min_command &C, bool SDK) {
606 uint32_t VersionOrSDK = (SDK) ? C.sdk : C.version;
607 return VersionOrSDK & 0xff;
610 static std::string getBuildPlatform(uint32_t platform) {
612 case MachO::PLATFORM_MACOS: return "macos";
613 case MachO::PLATFORM_IOS: return "ios";
614 case MachO::PLATFORM_TVOS: return "tvos";
615 case MachO::PLATFORM_WATCHOS: return "watchos";
616 case MachO::PLATFORM_BRIDGEOS: return "bridgeos";
619 raw_string_ostream ss(ret);
620 ss << format_hex(platform, 8, true);
625 static std::string getBuildTool(uint32_t tools) {
627 case MachO::TOOL_CLANG: return "clang";
628 case MachO::TOOL_SWIFT: return "swift";
629 case MachO::TOOL_LD: return "ld";
632 raw_string_ostream ss(ret);
633 ss << format_hex(tools, 8, true);
638 static std::string getVersionString(uint32_t version) {
639 uint32_t major = (version >> 16) & 0xffff;
640 uint32_t minor = (version >> 8) & 0xff;
641 uint32_t update = version & 0xff;
643 SmallString<32> Version;
644 Version = utostr(major) + "." + utostr(minor);
646 Version += "." + utostr(update);
647 return Version.str();
651 MachOObjectFile(MemoryBufferRef Object, bool IsLittleEndian, bool Is64Bits,
652 Error &Err, uint32_t UniversalCputype = 0,
653 uint32_t UniversalIndex = 0);
655 uint64_t getSymbolValueImpl(DataRefImpl Symb) const override;
658 MachO::mach_header_64 Header64;
659 MachO::mach_header Header;
661 using SectionList = SmallVector<const char*, 1>;
662 SectionList Sections;
663 using LibraryList = SmallVector<const char*, 1>;
664 LibraryList Libraries;
665 LoadCommandList LoadCommands;
666 using LibraryShortName = SmallVector<StringRef, 1>;
667 using BuildToolList = SmallVector<const char*, 1>;
668 BuildToolList BuildTools;
669 mutable LibraryShortName LibrariesShortNames;
670 std::unique_ptr<BindRebaseSegInfo> BindRebaseSectionTable;
671 const char *SymtabLoadCmd = nullptr;
672 const char *DysymtabLoadCmd = nullptr;
673 const char *DataInCodeLoadCmd = nullptr;
674 const char *LinkOptHintsLoadCmd = nullptr;
675 const char *DyldInfoLoadCmd = nullptr;
676 const char *UuidLoadCmd = nullptr;
677 bool HasPageZeroSegment = false;
681 inline DiceRef::DiceRef(DataRefImpl DiceP, const ObjectFile *Owner)
682 : DicePimpl(DiceP) , OwningObject(Owner) {}
684 inline bool DiceRef::operator==(const DiceRef &Other) const {
685 return DicePimpl == Other.DicePimpl;
688 inline bool DiceRef::operator<(const DiceRef &Other) const {
689 return DicePimpl < Other.DicePimpl;
692 inline void DiceRef::moveNext() {
693 const MachO::data_in_code_entry *P =
694 reinterpret_cast<const MachO::data_in_code_entry *>(DicePimpl.p);
695 DicePimpl.p = reinterpret_cast<uintptr_t>(P + 1);
698 // Since a Mach-O data in code reference, a DiceRef, can only be created when
699 // the OwningObject ObjectFile is a MachOObjectFile a static_cast<> is used for
700 // the methods that get the values of the fields of the reference.
702 inline std::error_code DiceRef::getOffset(uint32_t &Result) const {
703 const MachOObjectFile *MachOOF =
704 static_cast<const MachOObjectFile *>(OwningObject);
705 MachO::data_in_code_entry Dice = MachOOF->getDice(DicePimpl);
706 Result = Dice.offset;
707 return std::error_code();
710 inline std::error_code DiceRef::getLength(uint16_t &Result) const {
711 const MachOObjectFile *MachOOF =
712 static_cast<const MachOObjectFile *>(OwningObject);
713 MachO::data_in_code_entry Dice = MachOOF->getDice(DicePimpl);
714 Result = Dice.length;
715 return std::error_code();
718 inline std::error_code DiceRef::getKind(uint16_t &Result) const {
719 const MachOObjectFile *MachOOF =
720 static_cast<const MachOObjectFile *>(OwningObject);
721 MachO::data_in_code_entry Dice = MachOOF->getDice(DicePimpl);
723 return std::error_code();
726 inline DataRefImpl DiceRef::getRawDataRefImpl() const {
730 inline const ObjectFile *DiceRef::getObjectFile() const {
734 } // end namespace object
735 } // end namespace llvm
737 #endif // LLVM_OBJECT_MACHO_H