LLVM 22.0.0git
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"
21#include "llvm/ADT/StringRef.h"
25#include "llvm/Object/Binary.h"
29#include "llvm/Support/Error.h"
30#include "llvm/Support/Format.h"
35#include <cstdint>
36#include <memory>
37#include <string>
38#include <system_error>
39
40namespace llvm {
41namespace object {
42
43/// DiceRef - This is a value type class that represents a single
44/// data in code entry in the table in a Mach-O object file.
45class DiceRef {
46 DataRefImpl DicePimpl;
47 const ObjectFile *OwningObject = nullptr;
48
49public:
50 DiceRef() = default;
51 DiceRef(DataRefImpl DiceP, const ObjectFile *Owner);
52
53 bool operator==(const DiceRef &Other) const;
54 bool operator<(const DiceRef &Other) const;
55
56 void moveNext();
57
58 std::error_code getOffset(uint32_t &Result) const;
59 std::error_code getLength(uint16_t &Result) const;
60 std::error_code getKind(uint16_t &Result) const;
61
63 const ObjectFile *getObjectFile() const;
64};
66
67/// ExportEntry encapsulates the current-state-of-the-walk used when doing a
68/// non-recursive walk of the trie data structure. This allows you to iterate
69/// across all exported symbols using:
70/// Error Err = Error::success();
71/// for (const llvm::object::ExportEntry &AnExport : Obj->exports(&Err)) {
72/// }
73/// if (Err) { report error ...
75public:
78
79 LLVM_ABI StringRef name() const;
80 LLVM_ABI uint64_t flags() const;
82 LLVM_ABI uint64_t other() const;
85
86 LLVM_ABI bool operator==(const ExportEntry &) const;
87
88 LLVM_ABI void moveNext();
89
90private:
91 friend class MachOObjectFile;
92
93 void moveToFirst();
94 void moveToEnd();
95 uint64_t readULEB128(const uint8_t *&p, const char **error);
96 void pushDownUntilBottom();
97 void pushNode(uint64_t Offset);
98
99 // Represents a node in the mach-o exports trie.
100 struct NodeState {
101 LLVM_ABI NodeState(const uint8_t *Ptr);
102
103 const uint8_t *Start;
104 const uint8_t *Current;
105 uint64_t Flags = 0;
106 uint64_t Address = 0;
107 uint64_t Other = 0;
108 const char *ImportName = nullptr;
109 unsigned ChildCount = 0;
110 unsigned NextChildIndex = 0;
111 unsigned ParentStringLength = 0;
112 bool IsExportNode = false;
113 };
115 using node_iterator = NodeList::const_iterator;
116
117 Error *E;
118 const MachOObjectFile *O;
120 SmallString<256> CumulativeString;
121 NodeList Stack;
122 bool Done = false;
123
124 iterator_range<node_iterator> nodes() const { return Stack; }
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.
133public:
135
136 // Used to check a Mach-O Bind or Rebase entry for errors when iterating.
137 LLVM_ABI const char *checkSegAndOffsets(int32_t SegIndex, uint64_t SegOffset,
138 uint8_t PointerSize,
139 uint64_t Count = 1,
140 uint64_t Skip = 0);
141 // Used with valid SegIndex/SegOffset values from checked entries.
142 LLVM_ABI StringRef segmentName(int32_t SegIndex);
143 LLVM_ABI StringRef sectionName(int32_t SegIndex, uint64_t SegOffset);
144 LLVM_ABI uint64_t address(uint32_t SegIndex, uint64_t SegOffset);
145
146private:
147 struct SectionInfo {
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 = Error::success();
166/// for (const llvm::object::MachORebaseEntry &Entry : Obj->rebaseTable(&Err)) {
167/// }
168/// if (Err) { report error ...
170public:
172 ArrayRef<uint8_t> opcodes, bool is64Bit);
173
174 LLVM_ABI int32_t segmentIndex() const;
179 LLVM_ABI uint64_t address() const;
180
181 LLVM_ABI bool operator==(const MachORebaseEntry &) const;
182
183 LLVM_ABI void moveNext();
184
185private:
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 = Error::success();
210/// for (const llvm::object::MachOBindEntry &Entry : Obj->bindTable(&Err)) {
211/// }
212/// if (Err) { report error ...
214public:
215 enum class Kind { Regular, Lazy, Weak };
216
218 ArrayRef<uint8_t> Opcodes, bool is64Bit,
220
221 LLVM_ABI int32_t segmentIndex() const;
225 LLVM_ABI uint32_t flags() const;
226 LLVM_ABI int64_t addend() const;
227 LLVM_ABI int ordinal() const;
228
231 LLVM_ABI uint64_t address() const;
232
233 LLVM_ABI bool operator==(const MachOBindEntry &) const;
234
235 LLVM_ABI void moveNext();
236
237private:
238 friend class MachOObjectFile;
239
240 void moveToFirst();
241 void moveToEnd();
242 uint64_t readULEB128(const char **error);
243 int64_t readSLEB128(const char **error);
244
245 Error *E;
246 const MachOObjectFile *O;
247 ArrayRef<uint8_t> Opcodes;
248 const uint8_t *Ptr;
249 uint64_t SegmentOffset = 0;
250 int32_t SegmentIndex = -1;
251 StringRef SymbolName;
252 bool LibraryOrdinalSet = false;
253 int Ordinal = 0;
254 uint32_t Flags = 0;
255 int64_t Addend = 0;
256 uint64_t RemainingLoopCount = 0;
257 uint64_t AdvanceAmount = 0;
258 uint8_t BindType = 0;
259 uint8_t PointerSize;
260 Kind TableKind;
261 bool Done = false;
262};
264
265/// ChainedFixupTarget holds all the information about an external symbol
266/// necessary to bind this binary to that symbol. These values are referenced
267/// indirectly by chained fixup binds. This structure captures values from all
268/// import and symbol formats.
269///
270/// Be aware there are two notions of weak here:
271/// WeakImport == true
272/// The associated bind may be set to 0 if this symbol is missing from its
273/// parent library. This is called a "weak import."
274/// LibOrdinal == BIND_SPECIAL_DYLIB_WEAK_LOOKUP
275/// This symbol may be coalesced with other libraries vending the same
276/// symbol. E.g., C++'s "operator new". This is called a "weak bind."
278public:
279 ChainedFixupTarget(int LibOrdinal, uint32_t NameOffset, StringRef Symbol,
280 uint64_t Addend, bool WeakImport)
281 : LibOrdinal(LibOrdinal), NameOffset(NameOffset), SymbolName(Symbol),
282 Addend(Addend), WeakImport(WeakImport) {}
283
284 int libOrdinal() { return LibOrdinal; }
285 uint32_t nameOffset() { return NameOffset; }
286 StringRef symbolName() { return SymbolName; }
287 uint64_t addend() { return Addend; }
288 bool weakImport() { return WeakImport; }
289 bool weakBind() {
290 return LibOrdinal == MachO::BIND_SPECIAL_DYLIB_WEAK_LOOKUP;
291 }
292
293private:
294 int LibOrdinal;
295 uint32_t NameOffset;
296 StringRef SymbolName;
297 uint64_t Addend;
298 bool WeakImport;
299};
300
307
309 uint32_t Offset; // dyld_chained_starts_in_image::seg_info_offset[SegIdx]
311 std::vector<uint16_t> PageStarts; // page_start[] entries, host endianness
312};
313
314/// MachOAbstractFixupEntry is an abstract class representing a fixup in a
315/// MH_DYLDLINK file. Fixups generally represent rebases and binds. Binds also
316/// subdivide into additional subtypes (weak, lazy, reexport).
317///
318/// The two concrete subclasses of MachOAbstractFixupEntry are:
319///
320/// MachORebaseBindEntry - for dyld opcode-based tables, including threaded-
321/// rebase, where rebases are mixed in with other
322/// bind opcodes.
323/// MachOChainedFixupEntry - for pointer chains embedded in data pages.
325public:
327
328 LLVM_ABI int32_t segmentIndex() const;
335 LLVM_ABI uint32_t flags() const;
336 LLVM_ABI int64_t addend() const;
337 LLVM_ABI int ordinal() const;
338
339 /// \return the location of this fixup as a VM Address. For the VM
340 /// Address this fixup is pointing to, use pointerValue().
341 LLVM_ABI uint64_t address() const;
342
343 /// \return the VM Address pointed to by this fixup. Use
344 /// pointerValue() to compare against other VM Addresses, such as
345 /// section addresses or segment vmaddrs.
347
348 /// \return the raw "on-disk" representation of the fixup. For
349 /// Threaded rebases and Chained pointers these values are generally
350 /// encoded into various different pointer formats. This value is
351 /// exposed in API for tools that want to display and annotate the
352 /// raw bits.
353 uint64_t rawValue() const { return RawValue; }
354
355 LLVM_ABI void moveNext();
356
357protected:
361 int32_t SegmentIndex = -1;
363 int32_t Ordinal = 0;
365 int64_t Addend = 0;
368 bool Done = false;
369
370 LLVM_ABI void moveToFirst();
371 LLVM_ABI void moveToEnd();
372
373 /// \return the vm address of the start of __TEXT segment.
374 uint64_t textAddress() const { return TextAddress; }
375
376private:
377 uint64_t TextAddress;
378};
379
381public:
382 enum class FixupKind { Bind, Rebase };
383
385 bool Parse);
386
387 LLVM_ABI bool operator==(const MachOChainedFixupEntry &) const;
388
389 bool isBind() const { return Kind == FixupKind::Bind; }
390 bool isRebase() const { return Kind == FixupKind::Rebase; }
391
392 LLVM_ABI void moveNext();
393 LLVM_ABI void moveToFirst();
394 LLVM_ABI void moveToEnd();
395
396private:
397 void findNextPageWithFixups();
398
399 std::vector<ChainedFixupTarget> FixupTargets;
400 std::vector<ChainedFixupsSegment> Segments;
401 ArrayRef<uint8_t> SegmentData;
403 uint32_t InfoSegIndex = 0; // Index into Segments
404 uint32_t PageIndex = 0; // Index into Segments[InfoSegIdx].PageStarts
405 uint32_t PageOffset = 0; // Page offset of the current fixup
406};
408
409class LLVM_ABI MachOObjectFile : public ObjectFile {
410public:
412 const char *Ptr; // Where in memory the load command is.
413 MachO::load_command C; // The command itself.
414 };
417
419 create(MemoryBufferRef Object, bool IsLittleEndian, bool Is64Bits,
420 uint32_t UniversalCputype = 0, uint32_t UniversalIndex = 0,
421 size_t MachOFilesetEntryOffset = 0);
422
423 static bool isMachOPairedReloc(uint64_t RelocType, uint64_t Arch);
424
425 void moveSymbolNext(DataRefImpl &Symb) const override;
426
427 uint64_t getNValue(DataRefImpl Sym) const;
428 Expected<StringRef> getSymbolName(DataRefImpl Symb) const override;
429
430 // MachO specific.
431 Error checkSymbolTable() const;
432
433 std::error_code getIndirectName(DataRefImpl Symb, StringRef &Res) const;
434 unsigned getSectionType(SectionRef Sec) const;
435
437 uint32_t getSymbolAlignment(DataRefImpl Symb) const override;
438 uint64_t getCommonSymbolSizeImpl(DataRefImpl Symb) const override;
440 Expected<uint32_t> getSymbolFlags(DataRefImpl Symb) const override;
442 unsigned getSymbolSectionID(SymbolRef Symb) const;
443 unsigned getSectionID(SectionRef Sec) const;
444
445 void moveSectionNext(DataRefImpl &Sec) const override;
447 uint64_t getSectionAddress(DataRefImpl Sec) const override;
448 uint64_t getSectionIndex(DataRefImpl Sec) const override;
449 uint64_t getSectionSize(DataRefImpl Sec) const override;
452 getSectionContents(DataRefImpl Sec) const override;
453 uint64_t getSectionAlignment(DataRefImpl Sec) const override;
454 Expected<SectionRef> getSection(unsigned SectionIndex) const;
456 bool isSectionCompressed(DataRefImpl Sec) const override;
457 bool isSectionText(DataRefImpl Sec) const override;
458 bool isSectionData(DataRefImpl Sec) const override;
459 bool isSectionBSS(DataRefImpl Sec) const override;
460 bool isSectionVirtual(DataRefImpl Sec) const override;
461 bool isSectionBitcode(DataRefImpl Sec) const override;
462 bool isDebugSection(DataRefImpl Sec) const override;
463
464 /// Return the raw contents of an entire segment.
466 ArrayRef<uint8_t> getSegmentContents(size_t SegmentIndex) const;
467
468 /// When dsymutil generates the companion file, it strips all unnecessary
469 /// sections (e.g. everything in the _TEXT segment) by omitting their body
470 /// and setting the offset in their corresponding load command to zero.
471 ///
472 /// While the load command itself is valid, reading the section corresponds
473 /// to reading the number of bytes specified in the load command, starting
474 /// from offset 0 (i.e. the Mach-O header at the beginning of the file).
475 bool isSectionStripped(DataRefImpl Sec) const override;
476
479
485
486 relocation_iterator locrel_begin() const;
487 relocation_iterator locrel_end() const;
488
489 void moveRelocationNext(DataRefImpl &Rel) const override;
490 uint64_t getRelocationOffset(DataRefImpl Rel) const override;
491 symbol_iterator getRelocationSymbol(DataRefImpl Rel) const override;
492 section_iterator getRelocationSection(DataRefImpl Rel) const;
493 uint64_t getRelocationType(DataRefImpl Rel) const override;
494 void getRelocationTypeName(DataRefImpl Rel,
495 SmallVectorImpl<char> &Result) const override;
496 uint8_t getRelocationLength(DataRefImpl Rel) const;
497
498 // MachO specific.
499 std::error_code getLibraryShortNameByIndex(unsigned Index, StringRef &) const;
500 uint32_t getLibraryCount() const;
501
502 section_iterator getRelocationRelocatedSection(relocation_iterator Rel) const;
503
504 // TODO: Would be useful to have an iterator based version
505 // of the load command interface too.
506
507 basic_symbol_iterator symbol_begin() const override;
508 basic_symbol_iterator symbol_end() const override;
509
510 bool is64Bit() const override;
511
512 // MachO specific.
513 symbol_iterator getSymbolByIndex(unsigned Index) const;
514 uint64_t getSymbolIndex(DataRefImpl Symb) const;
515
516 section_iterator section_begin() const override;
517 section_iterator section_end() const override;
518
519 uint8_t getBytesInAddress() const override;
520
521 StringRef getFileFormatName() const override;
522 Triple::ArchType getArch() const override;
524 return SubtargetFeatures();
525 }
526 Triple getArchTriple(const char **McpuDefault = nullptr) const;
527
528 relocation_iterator section_rel_begin(unsigned Index) const;
529 relocation_iterator section_rel_end(unsigned Index) const;
530
531 dice_iterator begin_dices() const;
532 dice_iterator end_dices() const;
533
534 load_command_iterator begin_load_commands() const;
535 load_command_iterator end_load_commands() const;
536 iterator_range<load_command_iterator> load_commands() const;
537
538 /// For use iterating over all exported symbols.
539 iterator_range<export_iterator> exports(Error &Err) const;
540
541 /// For use examining a trie not in a MachOObjectFile.
542 static iterator_range<export_iterator> exports(Error &Err,
544 const MachOObjectFile *O =
545 nullptr);
546
547 /// For use iterating over all rebase table entries.
548 iterator_range<rebase_iterator> rebaseTable(Error &Err);
549
550 /// For use examining rebase opcodes in a MachOObjectFile.
551 static iterator_range<rebase_iterator> rebaseTable(Error &Err,
553 ArrayRef<uint8_t> Opcodes,
554 bool is64);
555
556 /// For use iterating over all bind table entries.
557 iterator_range<bind_iterator> bindTable(Error &Err);
558
559 /// For iterating over all chained fixups.
560 iterator_range<fixup_iterator> fixupTable(Error &Err);
561
562 /// For use iterating over all lazy bind table entries.
563 iterator_range<bind_iterator> lazyBindTable(Error &Err);
564
565 /// For use iterating over all weak bind table entries.
566 iterator_range<bind_iterator> weakBindTable(Error &Err);
567
568 /// For use examining bind opcodes in a MachOObjectFile.
569 static iterator_range<bind_iterator> bindTable(Error &Err,
571 ArrayRef<uint8_t> Opcodes,
572 bool is64,
574
575 // Given a SegIndex, SegOffset, and PointerSize, verify a valid section exists
576 // that fully contains a pointer at that location. Multiple fixups in a bind
577 // (such as with the BIND_OPCODE_DO_BIND_ULEB_TIMES_SKIPPING_ULEB opcode) can
578 // be tested via the Count and Skip parameters.
579 //
580 // This is used by MachOBindEntry::moveNext() to validate a MachOBindEntry.
581 const char *BindEntryCheckSegAndOffsets(int32_t SegIndex, uint64_t SegOffset,
582 uint8_t PointerSize,
583 uint64_t Count = 1,
584 uint64_t Skip = 0) const {
585 return BindRebaseSectionTable->checkSegAndOffsets(SegIndex, SegOffset,
586 PointerSize, Count, Skip);
587 }
588
589 // Given a SegIndex, SegOffset, and PointerSize, verify a valid section exists
590 // that fully contains a pointer at that location. Multiple fixups in a rebase
591 // (such as with the REBASE_OPCODE_DO_*_TIMES* opcodes) can be tested via the
592 // Count and Skip parameters.
593 //
594 // This is used by MachORebaseEntry::moveNext() to validate a MachORebaseEntry
595 const char *RebaseEntryCheckSegAndOffsets(int32_t SegIndex,
596 uint64_t SegOffset,
597 uint8_t PointerSize,
598 uint64_t Count = 1,
599 uint64_t Skip = 0) const {
600 return BindRebaseSectionTable->checkSegAndOffsets(SegIndex, SegOffset,
601 PointerSize, Count, Skip);
602 }
603
604 /// For use with the SegIndex of a checked Mach-O Bind or Rebase entry to
605 /// get the segment name.
606 StringRef BindRebaseSegmentName(int32_t SegIndex) const {
607 return BindRebaseSectionTable->segmentName(SegIndex);
608 }
609
610 /// For use with a SegIndex,SegOffset pair from a checked Mach-O Bind or
611 /// Rebase entry to get the section name.
613 return BindRebaseSectionTable->sectionName(SegIndex, SegOffset);
614 }
615
616 /// For use with a SegIndex,SegOffset pair from a checked Mach-O Bind or
617 /// Rebase entry to get the address.
618 uint64_t BindRebaseAddress(uint32_t SegIndex, uint64_t SegOffset) const {
619 return BindRebaseSectionTable->address(SegIndex, SegOffset);
620 }
621
622 // In a MachO file, sections have a segment name. This is used in the .o
623 // files. They have a single segment, but this field specifies which segment
624 // a section should be put in the final object.
625 StringRef getSectionFinalSegmentName(DataRefImpl Sec) const;
626
627 // Names are stored as 16 bytes. These returns the raw 16 bytes without
628 // interpreting them as a C string.
629 ArrayRef<char> getSectionRawName(DataRefImpl Sec) const;
630 ArrayRef<char> getSectionRawFinalSegmentName(DataRefImpl Sec) const;
631
632 // MachO specific Info about relocations.
633 bool isRelocationScattered(const MachO::any_relocation_info &RE) const;
634 unsigned getPlainRelocationSymbolNum(
635 const MachO::any_relocation_info &RE) const;
636 bool getPlainRelocationExternal(const MachO::any_relocation_info &RE) const;
637 bool getScatteredRelocationScattered(
638 const MachO::any_relocation_info &RE) const;
639 uint32_t getScatteredRelocationValue(
640 const MachO::any_relocation_info &RE) const;
641 uint32_t getScatteredRelocationType(
642 const MachO::any_relocation_info &RE) const;
643 unsigned getAnyRelocationAddress(const MachO::any_relocation_info &RE) const;
644 unsigned getAnyRelocationPCRel(const MachO::any_relocation_info &RE) const;
645 unsigned getAnyRelocationLength(const MachO::any_relocation_info &RE) const;
646 unsigned getAnyRelocationType(const MachO::any_relocation_info &RE) const;
647 SectionRef getAnyRelocationSection(const MachO::any_relocation_info &RE) const;
648
649 // MachO specific structures.
651 MachO::section_64 getSection64(DataRefImpl DRI) const;
652 MachO::section getSection(const LoadCommandInfo &L, unsigned Index) const;
653 MachO::section_64 getSection64(const LoadCommandInfo &L,unsigned Index) const;
654 MachO::nlist getSymbolTableEntry(DataRefImpl DRI) const;
655 MachO::nlist_64 getSymbol64TableEntry(DataRefImpl DRI) const;
656
658 getLinkeditDataLoadCommand(const LoadCommandInfo &L) const;
660 getSegmentLoadCommand(const LoadCommandInfo &L) const;
662 getSegment64LoadCommand(const LoadCommandInfo &L) const;
664 getLinkerOptionLoadCommand(const LoadCommandInfo &L) const;
666 getVersionMinLoadCommand(const LoadCommandInfo &L) const;
668 getNoteLoadCommand(const LoadCommandInfo &L) const;
670 getBuildVersionLoadCommand(const LoadCommandInfo &L) const;
672 getBuildToolVersion(unsigned index) const;
674 getDylibIDLoadCommand(const LoadCommandInfo &L) const;
676 getDyldInfoLoadCommand(const LoadCommandInfo &L) const;
678 getDylinkerCommand(const LoadCommandInfo &L) const;
680 getUuidCommand(const LoadCommandInfo &L) const;
682 getRpathCommand(const LoadCommandInfo &L) const;
684 getSourceVersionCommand(const LoadCommandInfo &L) const;
686 getEntryPointCommand(const LoadCommandInfo &L) const;
688 getEncryptionInfoCommand(const LoadCommandInfo &L) const;
690 getEncryptionInfoCommand64(const LoadCommandInfo &L) const;
692 getSubFrameworkCommand(const LoadCommandInfo &L) const;
694 getSubUmbrellaCommand(const LoadCommandInfo &L) const;
696 getSubLibraryCommand(const LoadCommandInfo &L) const;
698 getSubClientCommand(const LoadCommandInfo &L) const;
700 getRoutinesCommand(const LoadCommandInfo &L) const;
702 getRoutinesCommand64(const LoadCommandInfo &L) const;
704 getThreadCommand(const LoadCommandInfo &L) const;
706 getFilesetEntryLoadCommand(const LoadCommandInfo &L) const;
707
708 MachO::any_relocation_info getRelocation(DataRefImpl Rel) const;
709 MachO::data_in_code_entry getDice(DataRefImpl Rel) const;
710 const MachO::mach_header &getHeader() const;
711 const MachO::mach_header_64 &getHeader64() const;
713 getIndirectSymbolTableEntry(const MachO::dysymtab_command &DLC,
714 unsigned Index) const;
715 MachO::data_in_code_entry getDataInCodeTableEntry(uint32_t DataOffset,
716 unsigned Index) const;
717 MachO::symtab_command getSymtabLoadCommand() const;
718 MachO::dysymtab_command getDysymtabLoadCommand() const;
719 MachO::linkedit_data_command getDataInCodeLoadCommand() const;
720 MachO::linkedit_data_command getLinkOptHintsLoadCommand() const;
721 ArrayRef<uint8_t> getDyldInfoRebaseOpcodes() const;
722 ArrayRef<uint8_t> getDyldInfoBindOpcodes() const;
723 ArrayRef<uint8_t> getDyldInfoWeakBindOpcodes() const;
724 ArrayRef<uint8_t> getDyldInfoLazyBindOpcodes() const;
725 ArrayRef<uint8_t> getDyldInfoExportsTrie() const;
726
727 /// If the optional is std::nullopt, no header was found, but the object was
728 /// well-formed.
730 getChainedFixupsHeader() const;
731 Expected<std::vector<ChainedFixupTarget>> getDyldChainedFixupTargets() const;
732
733 // Note: This is a limited, temporary API, which will be removed when Apple
734 // upstreams their implementation. Please do not rely on this.
736 getChainedFixupsLoadCommand() const;
737 // Returns the number of sections listed in dyld_chained_starts_in_image, and
738 // a ChainedFixupsSegment for each segment that has fixups.
740 getChainedFixupsSegments() const;
741 ArrayRef<uint8_t> getDyldExportsTrie() const;
742
743 SmallVector<uint64_t> getFunctionStarts() const;
744 ArrayRef<uint8_t> getUuid() const;
745
746 StringRef getStringTableData() const;
747
748 void ReadULEB128s(uint64_t Index, SmallVectorImpl<uint64_t> &Out) const;
749
750 static StringRef guessLibraryShortName(StringRef Name, bool &isFramework,
751 StringRef &Suffix);
752
753 static Triple::ArchType getArch(uint32_t CPUType, uint32_t CPUSubType);
754 static Triple getArchTriple(uint32_t CPUType, uint32_t CPUSubType,
755 const char **McpuDefault = nullptr,
756 const char **ArchFlag = nullptr);
757 static bool isValidArch(StringRef ArchFlag);
758 static ArrayRef<StringRef> getValidArchs();
759 static Triple getHostArch();
760
761 bool isRelocatableObject() const override;
762
763 StringRef mapDebugSectionName(StringRef Name) const override;
764
766 mapReflectionSectionNameToEnumValue(StringRef SectionName) const override;
767
768 bool hasPageZeroSegment() const { return HasPageZeroSegment; }
769
770 size_t getMachOFilesetEntryOffset() const { return MachOFilesetEntryOffset; }
771
772 static bool classof(const Binary *v) {
773 return v->isMachO();
774 }
775
776 static uint32_t
778 uint32_t VersionOrSDK = (SDK) ? C.sdk : C.version;
779 return (VersionOrSDK >> 16) & 0xffff;
780 }
781
782 static uint32_t
784 uint32_t VersionOrSDK = (SDK) ? C.sdk : C.version;
785 return (VersionOrSDK >> 8) & 0xff;
786 }
787
788 static uint32_t
790 uint32_t VersionOrSDK = (SDK) ? C.sdk : C.version;
791 return VersionOrSDK & 0xff;
792 }
793
794 static std::string getBuildPlatform(uint32_t platform) {
795 switch (platform) {
796#define PLATFORM(platform, id, name, build_name, target, tapi_target, \
797 marketing) \
798 case MachO::PLATFORM_##platform: \
799 return #name;
800#include "llvm/BinaryFormat/MachO.def"
801 default:
802 std::string ret;
803 raw_string_ostream ss(ret);
804 ss << format_hex(platform, 8, true);
805 return ret;
806 }
807 }
808
809 static std::string getBuildTool(uint32_t tools) {
810 switch (tools) {
811 case MachO::TOOL_CLANG: return "clang";
812 case MachO::TOOL_SWIFT: return "swift";
813 case MachO::TOOL_LD: return "ld";
814 case MachO::TOOL_LLD:
815 return "lld";
816 default:
817 std::string ret;
818 raw_string_ostream ss(ret);
819 ss << format_hex(tools, 8, true);
820 return ret;
821 }
822 }
823
824 static std::string getVersionString(uint32_t version) {
825 uint32_t major = (version >> 16) & 0xffff;
826 uint32_t minor = (version >> 8) & 0xff;
827 uint32_t update = version & 0xff;
828
830 Version = utostr(major) + "." + utostr(minor);
831 if (update != 0)
832 Version += "." + utostr(update);
833 return std::string(std::string(Version));
834 }
835
836 /// If the input path is a .dSYM bundle (as created by the dsymutil tool),
837 /// return the paths to the object files found in the bundle, otherwise return
838 /// an empty vector. If the path appears to be a .dSYM bundle but no objects
839 /// were found or there was a filesystem error, then return an error.
841 findDsymObjectMembers(StringRef Path);
842
843private:
844 MachOObjectFile(MemoryBufferRef Object, bool IsLittleEndian, bool Is64Bits,
845 Error &Err, uint32_t UniversalCputype = 0,
846 uint32_t UniversalIndex = 0,
847 size_t MachOFilesetEntryOffset = 0);
848
849 uint64_t getSymbolValueImpl(DataRefImpl Symb) const override;
850
851 union {
854 };
855 using SectionList = SmallVector<const char*, 1>;
856 SectionList Sections;
857 using LibraryList = SmallVector<const char*, 1>;
858 LibraryList Libraries;
859 LoadCommandList LoadCommands;
860 using LibraryShortName = SmallVector<StringRef, 1>;
861 using BuildToolList = SmallVector<const char*, 1>;
862 BuildToolList BuildTools;
863 mutable LibraryShortName LibrariesShortNames;
864 std::unique_ptr<BindRebaseSegInfo> BindRebaseSectionTable;
865 const char *SymtabLoadCmd = nullptr;
866 const char *DysymtabLoadCmd = nullptr;
867 const char *DataInCodeLoadCmd = nullptr;
868 const char *LinkOptHintsLoadCmd = nullptr;
869 const char *DyldInfoLoadCmd = nullptr;
870 const char *FuncStartsLoadCmd = nullptr;
871 const char *DyldChainedFixupsLoadCmd = nullptr;
872 const char *DyldExportsTrieLoadCmd = nullptr;
873 const char *UuidLoadCmd = nullptr;
874 bool HasPageZeroSegment = false;
875 size_t MachOFilesetEntryOffset = 0;
876};
877
878/// DiceRef
879inline DiceRef::DiceRef(DataRefImpl DiceP, const ObjectFile *Owner)
880 : DicePimpl(DiceP) , OwningObject(Owner) {}
881
882inline bool DiceRef::operator==(const DiceRef &Other) const {
883 return DicePimpl == Other.DicePimpl;
884}
885
886inline bool DiceRef::operator<(const DiceRef &Other) const {
887 return DicePimpl < Other.DicePimpl;
888}
889
890inline void DiceRef::moveNext() {
892 reinterpret_cast<const MachO::data_in_code_entry *>(DicePimpl.p);
893 DicePimpl.p = reinterpret_cast<uintptr_t>(P + 1);
894}
895
896// Since a Mach-O data in code reference, a DiceRef, can only be created when
897// the OwningObject ObjectFile is a MachOObjectFile a static_cast<> is used for
898// the methods that get the values of the fields of the reference.
899
900inline std::error_code DiceRef::getOffset(uint32_t &Result) const {
901 const MachOObjectFile *MachOOF =
902 static_cast<const MachOObjectFile *>(OwningObject);
903 MachO::data_in_code_entry Dice = MachOOF->getDice(DicePimpl);
904 Result = Dice.offset;
905 return std::error_code();
906}
907
908inline std::error_code DiceRef::getLength(uint16_t &Result) const {
909 const MachOObjectFile *MachOOF =
910 static_cast<const MachOObjectFile *>(OwningObject);
911 MachO::data_in_code_entry Dice = MachOOF->getDice(DicePimpl);
912 Result = Dice.length;
913 return std::error_code();
914}
915
916inline std::error_code DiceRef::getKind(uint16_t &Result) const {
917 const MachOObjectFile *MachOOF =
918 static_cast<const MachOObjectFile *>(OwningObject);
919 MachO::data_in_code_entry Dice = MachOOF->getDice(DicePimpl);
920 Result = Dice.kind;
921 return std::error_code();
922}
923
925 return DicePimpl;
926}
927
928inline const ObjectFile *DiceRef::getObjectFile() const {
929 return OwningObject;
930}
931
932} // end namespace object
933} // end namespace llvm
934
935#endif // LLVM_OBJECT_MACHO_H
Unify divergent function exit nodes
static GCRegistry::Add< CoreCLRGC > E("coreclr", "CoreCLR-compatible GC")
static StringRef getSymbolName(SymbolKind SymKind)
#define LLVM_ABI
Definition Compiler.h:213
static bool isDebugSection(const SectionBase &Sec)
#define P(N)
This file defines the SmallString class.
This file defines the SmallVector class.
This file contains some functions that are useful when dealing with strings.
#define error(X)
static unsigned getSymbolSectionID(const ObjectFile &O, SymbolRef Sym)
static unsigned getSectionID(const ObjectFile &O, SectionRef Sec)
static std::unique_ptr< PDBSymbol > getSymbolType(const PDBSymbol &Symbol)
Definition UDTLayout.cpp:35
static uint64_t readULEB128(WasmObjectFile::ReadContext &Ctx)
static bool is64Bit(const char *name)
ArrayRef - Represent a constant reference to an array (0 or more elements consecutively in memory),...
Definition ArrayRef.h:41
Lightweight error class with error context and mandatory checking.
Definition Error.h:159
Tagged union holding either a T or a Error.
Definition Error.h:485
SmallString - A SmallString is just a SmallVector with methods and accessors that make it work better...
Definition SmallString.h:26
This class consists of common code factored out of the SmallVector class to reduce code duplication b...
This is a 'vector' (really, a variable-sized array), optimized for the case when the array is small.
StringRef - Represent a constant reference to a string, i.e.
Definition StringRef.h:55
Manages the enabling and disabling of subtarget specific features.
Triple - Helper class for working with autoconf configuration names.
Definition Triple.h:47
A range adaptor for a pair of iterators.
LLVM_ABI StringRef segmentName(int32_t SegIndex)
LLVM_ABI StringRef sectionName(int32_t SegIndex, uint64_t SegOffset)
LLVM_ABI BindRebaseSegInfo(const MachOObjectFile *Obj)
LLVM_ABI const char * checkSegAndOffsets(int32_t SegIndex, uint64_t SegOffset, uint8_t PointerSize, uint64_t Count=1, uint64_t Skip=0)
LLVM_ABI uint64_t address(uint32_t SegIndex, uint64_t SegOffset)
bool operator==(const DiceRef &Other) const
Definition MachO.h:882
std::error_code getOffset(uint32_t &Result) const
Definition MachO.h:900
std::error_code getLength(uint16_t &Result) const
Definition MachO.h:908
bool operator<(const DiceRef &Other) const
Definition MachO.h:886
DataRefImpl getRawDataRefImpl() const
Definition MachO.h:924
std::error_code getKind(uint16_t &Result) const
Definition MachO.h:916
const ObjectFile * getObjectFile() const
Definition MachO.h:928
LLVM_ABI StringRef name() const
LLVM_ABI ExportEntry(Error *Err, const MachOObjectFile *O, ArrayRef< uint8_t > Trie)
LLVM_ABI bool operator==(const ExportEntry &) const
LLVM_ABI StringRef otherName() const
LLVM_ABI uint64_t address() const
LLVM_ABI uint64_t flags() const
LLVM_ABI uint32_t nodeOffset() const
friend class MachOObjectFile
Definition MachO.h:91
LLVM_ABI uint64_t other() const
LLVM_ABI MachOAbstractFixupEntry(Error *Err, const MachOObjectFile *O)
const MachOObjectFile * O
Definition MachO.h:359
MachOBindEntry encapsulates the current state in the decompression of binding opcodes.
Definition MachO.h:213
LLVM_ABI uint32_t flags() const
LLVM_ABI bool operator==(const MachOBindEntry &) const
LLVM_ABI StringRef symbolName() const
LLVM_ABI StringRef sectionName() const
LLVM_ABI MachOBindEntry(Error *Err, const MachOObjectFile *O, ArrayRef< uint8_t > Opcodes, bool is64Bit, MachOBindEntry::Kind)
LLVM_ABI StringRef segmentName() const
LLVM_ABI uint64_t segmentOffset() const
LLVM_ABI int64_t addend() const
LLVM_ABI uint64_t address() const
friend class MachOObjectFile
Definition MachO.h:238
LLVM_ABI int32_t segmentIndex() const
LLVM_ABI StringRef typeName() const
LLVM_ABI bool operator==(const MachOChainedFixupEntry &) const
LLVM_ABI MachOChainedFixupEntry(Error *Err, const MachOObjectFile *O, bool Parse)
void moveSectionNext(DataRefImpl &Sec) const override
static std::string getVersionString(uint32_t version)
Definition MachO.h:824
MachO::mach_header_64 Header64
Definition MachO.h:852
bool isSectionData(DataRefImpl Sec) const override
uint64_t getSectionAlignment(DataRefImpl Sec) const override
Expected< SectionRef > getSection(unsigned SectionIndex) const
std::error_code getIndirectName(DataRefImpl Symb, StringRef &Res) const
Expected< section_iterator > getSymbolSection(DataRefImpl Symb) const override
const char * RebaseEntryCheckSegAndOffsets(int32_t SegIndex, uint64_t SegOffset, uint8_t PointerSize, uint64_t Count=1, uint64_t Skip=0) const
Definition MachO.h:595
void moveSymbolNext(DataRefImpl &Symb) const override
MachO::mach_header Header
Definition MachO.h:853
uint64_t getCommonSymbolSizeImpl(DataRefImpl Symb) const override
relocation_iterator section_rel_begin(DataRefImpl Sec) const override
static std::string getBuildTool(uint32_t tools)
Definition MachO.h:809
ArrayRef< uint8_t > getSectionContents(uint32_t Offset, uint64_t Size) const
const char * BindEntryCheckSegAndOffsets(int32_t SegIndex, uint64_t SegOffset, uint8_t PointerSize, uint64_t Count=1, uint64_t Skip=0) const
Definition MachO.h:581
static std::string getBuildPlatform(uint32_t platform)
Definition MachO.h:794
relocation_iterator section_rel_end(DataRefImpl Sec) const override
static bool classof(const Binary *v)
Definition MachO.h:772
unsigned getSectionType(SectionRef Sec) const
static Expected< std::unique_ptr< MachOObjectFile > > create(MemoryBufferRef Object, bool IsLittleEndian, bool Is64Bits, uint32_t UniversalCputype=0, uint32_t UniversalIndex=0, size_t MachOFilesetEntryOffset=0)
Expected< uint32_t > getSymbolFlags(DataRefImpl Symb) const override
bool isSectionBSS(DataRefImpl Sec) const override
bool isSectionVirtual(DataRefImpl Sec) const override
LoadCommandList::const_iterator load_command_iterator
Definition MachO.h:416
iterator_range< relocation_iterator > external_relocations() const
Definition MachO.h:482
static bool isMachOPairedReloc(uint64_t RelocType, uint64_t Arch)
static uint32_t getVersionMinUpdate(MachO::version_min_command &C, bool SDK)
Definition MachO.h:789
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:618
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:606
relocation_iterator extrel_begin() const
SmallVector< LoadCommandInfo, 4 > LoadCommandList
Definition MachO.h:415
MachO::data_in_code_entry getDice(DataRefImpl Rel) const
bool isSectionStripped(DataRefImpl Sec) const override
When dsymutil generates the companion file, it strips all unnecessary sections (e....
uint64_t getSectionIndex(DataRefImpl Sec) const override
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:612
static uint32_t getVersionMinMajor(MachO::version_min_command &C, bool SDK)
Definition MachO.h:777
uint64_t getSectionAddress(DataRefImpl Sec) const override
bool hasPageZeroSegment() const
Definition MachO.h:768
Expected< StringRef > getSectionName(DataRefImpl Sec) const override
bool isSectionText(DataRefImpl Sec) const override
bool isSectionCompressed(DataRefImpl Sec) const override
bool isSectionBitcode(DataRefImpl Sec) const override
Expected< SubtargetFeatures > getFeatures() const override
Definition MachO.h:523
uint32_t getSymbolAlignment(DataRefImpl Symb) const override
uint64_t getNValue(DataRefImpl Sym) const
ArrayRef< uint8_t > getSegmentContents(StringRef SegmentName) const
Return the raw contents of an entire segment.
static uint32_t getVersionMinMinor(MachO::version_min_command &C, bool SDK)
Definition MachO.h:783
Expected< uint64_t > getSymbolAddress(DataRefImpl Symb) const override
size_t getMachOFilesetEntryOffset() const
Definition MachO.h:770
uint64_t getSectionSize(DataRefImpl Sec) const override
relocation_iterator extrel_end() const
LLVM_ABI int32_t segmentIndex() const
LLVM_ABI StringRef segmentName() const
LLVM_ABI MachORebaseEntry(Error *Err, const MachOObjectFile *O, ArrayRef< uint8_t > opcodes, bool is64Bit)
LLVM_ABI bool operator==(const MachORebaseEntry &) const
LLVM_ABI uint64_t address() const
LLVM_ABI StringRef sectionName() const
friend class MachOObjectFile
Definition MachO.h:186
LLVM_ABI uint64_t segmentOffset() const
LLVM_ABI StringRef typeName() const
This class is the base class for all object file types.
Definition ObjectFile.h:231
ObjectFile(unsigned int Type, MemoryBufferRef Source)
This is a value type class that represents a single section in the list of sections in the object fil...
Definition ObjectFile.h:83
A raw_ostream that writes to an std::string.
This provides a very simple, boring adaptor for a begin and end iterator into a range type.
@ C
The default llvm calling convention, compatible with C.
Definition CallingConv.h:34
@ BIND_SPECIAL_DYLIB_WEAK_LOOKUP
Definition MachO.h:264
@ TOOL_SWIFT
Definition MachO.h:508
@ TOOL_CLANG
Definition MachO.h:508
Swift5ReflectionSectionKind
Definition Swift.h:14
Expected< const typename ELFT::Shdr * > getSection(typename ELFT::ShdrRange Sections, uint32_t Index)
Definition ELF.h:582
content_iterator< SectionRef > section_iterator
Definition ObjectFile.h:49
content_iterator< MachOBindEntry > bind_iterator
Definition MachO.h:263
content_iterator< MachOChainedFixupEntry > fixup_iterator
Definition MachO.h:407
content_iterator< MachORebaseEntry > rebase_iterator
Definition MachO.h:204
content_iterator< BasicSymbolRef > basic_symbol_iterator
content_iterator< ExportEntry > export_iterator
Definition MachO.h:126
content_iterator< RelocationRef > relocation_iterator
Definition ObjectFile.h:79
content_iterator< DiceRef > dice_iterator
Definition MachO.h:65
This is an optimization pass for GlobalISel generic memory operations.
@ Offset
Definition DWP.cpp:477
@ Done
Definition Threading.h:60
iterator_range< T > make_range(T x, T y)
Convenience function for iterating over sub-ranges.
std::string utostr(uint64_t X, bool isNeg=false)
FunctionAddr VTableAddr uintptr_t uintptr_t Version
Definition InstrProf.h:302
static Lanai::Fixups FixupKind(const MCExpr *Expr)
FunctionAddr VTableAddr Count
Definition InstrProf.h:139
FormattedNumber format_hex(uint64_t N, unsigned Width, bool Upper=false)
format_hex - Output N as a fixed width hexadecimal.
Definition Format.h:191
@ Other
Any other memory.
Definition ModRef.h:68
Definition MachO.h:808
uint16_t length
Definition MachO.h:810
uint16_t kind
Definition MachO.h:811
uint32_t offset
Definition MachO.h:809
Definition MachO.h:899
ChainedFixupTarget(int LibOrdinal, uint32_t NameOffset, StringRef Symbol, uint64_t Addend, bool WeakImport)
Definition MachO.h:279
MachO::dyld_chained_starts_in_segment Header
Definition MachO.h:310
std::vector< uint16_t > PageStarts
Definition MachO.h:311
ChainedFixupsSegment(uint8_t SegIdx, uint32_t Offset, const MachO::dyld_chained_starts_in_segment &Header, std::vector< uint16_t > &&PageStarts)
Definition MachO.h:302