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"
23#include "llvm/ADT/iterator_range.h"
24#include "llvm/BinaryFormat/MachO.h"
25#include "llvm/MC/SubtargetFeature.h"
26#include "llvm/Object/Binary.h"
27#include "llvm/Object/ObjectFile.h"
28#include "llvm/Object/SymbolicFile.h"
29#include "llvm/Support/Error.h"
30#include "llvm/Support/Format.h"
31#include "llvm/Support/MemoryBuffer.h"
32#include "llvm/Support/raw_ostream.h"
33#include <cstdint>
34#include <memory>
35#include <string>
36#include <system_error>
37
38namespace llvm {
39namespace 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.
43class DiceRef {
44 DataRefImpl DicePimpl;
45 const ObjectFile *OwningObject = nullptr;
46
47public:
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
60 DataRefImpl getRawDataRefImpl() const;
61 const ObjectFile *getObjectFile() const;
62};
63using dice_iterator = content_iterator<DiceRef>;
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 = Error::success();
69/// for (const llvm::object::ExportEntry &AnExport : Obj->exports(&Err)) {
70/// }
71/// if (Err) { report error ...
72class ExportEntry {
73public:
74 ExportEntry(Error *Err, const MachOObjectFile *O, ArrayRef<uint8_t> Trie);
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
87private:
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 };
111 using NodeList = SmallVector<NodeState, 16>;
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
121 iterator_range<node_iterator> nodes() const {
122 return make_range(Stack.begin(), Stack.end());
123 }
124};
125using export_iterator = content_iterator<ExportEntry>;
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.
131class BindRebaseSegInfo {
132public:
133 BindRebaseSegInfo(const MachOObjectFile *Obj);
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
144private:
145 struct SectionInfo {
146 uint64_t Address;
147 uint64_t Size;
148 StringRef SectionName;
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
156 SmallVector<SectionInfo, 32> Sections;
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 = Error::success();
164/// for (const llvm::object::MachORebaseEntry &Entry : Obj->rebaseTable(&Err)) {
165/// }
166/// if (Err) { report error ...
167class MachORebaseEntry {
168public:
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
183private:
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};
202using rebase_iterator = content_iterator<MachORebaseEntry>;
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 = Error::success();
208/// for (const llvm::object::MachOBindEntry &Entry : Obj->bindTable(&Err)) {
209/// }
210/// if (Err) { report error ...
211class MachOBindEntry {
212public:
213 enum class Kind { Regular, Lazy, Weak };
214
215 MachOBindEntry(Error *Err, const MachOObjectFile *O,
216 ArrayRef<uint8_t> Opcodes, bool is64Bit, MachOBindEntry::Kind);
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
234private:
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;
248 StringRef SymbolName;
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};
260using bind_iterator = content_iterator<MachOBindEntry>;
261
262class MachOObjectFile : public ObjectFile {
263public:
264 struct LoadCommandInfo {
265 const char *Ptr; // Where in memory the load command is.
266 MachO::load_command C; // The command itself.
267 };
268 using LoadCommandList = SmallVector<LoadCommandInfo, 4>;
269 using load_command_iterator = LoadCommandList::const_iterator;
270
271 static Expected<std::unique_ptr<MachOObjectFile>>
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;
289 Expected<SymbolRef::Type> getSymbolType(DataRefImpl Symb) const override;
290 Expected<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 Expected<StringRef> getSectionName(DataRefImpl Sec) const override;
297 uint64_t getSectionAddress(DataRefImpl Sec) const override;
298 uint64_t getSectionIndex(DataRefImpl Sec) const override;
299 uint64_t getSectionSize(DataRefImpl Sec) const override;
300 ArrayRef<uint8_t> getSectionContents(uint32_t Offset, uint64_t Size) const;
301 Expected<ArrayRef<uint8_t>>
302 getSectionContents(DataRefImpl Sec) const override;
303 uint64_t getSectionAlignment(DataRefImpl Sec) const override;
304 Expected<SectionRef> getSection(unsigned SectionIndex) const;
305 Expected<SectionRef> getSection(StringRef SectionName) 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 bool isDebugSection(StringRef SectionName) const override;
313
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.
317 ///
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;
322
323 relocation_iterator section_rel_begin(DataRefImpl Sec) const override;
324 relocation_iterator section_rel_end(DataRefImpl Sec) const override;
325
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());
330 }
331
332 relocation_iterator locrel_begin() const;
333 relocation_iterator locrel_end() const;
334
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;
343
344 // MachO specific.
345 std::error_code getLibraryShortNameByIndex(unsigned Index, StringRef &) const;
346 uint32_t getLibraryCount() const;
347
348 section_iterator getRelocationRelocatedSection(relocation_iterator Rel) const;
349
350 // TODO: Would be useful to have an iterator based version
351 // of the load command interface too.
352
353 basic_symbol_iterator symbol_begin() const override;
354 basic_symbol_iterator symbol_end() const override;
355
356 // MachO specific.
357 symbol_iterator getSymbolByIndex(unsigned Index) const;
358 uint64_t getSymbolIndex(DataRefImpl Symb) const;
359
360 section_iterator section_begin() const override;
361 section_iterator section_end() const override;
362
363 uint8_t getBytesInAddress() const override;
364
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;
369
370 relocation_iterator section_rel_begin(unsigned Index) const;
371 relocation_iterator section_rel_end(unsigned Index) const;
372
373 dice_iterator begin_dices() const;
374 dice_iterator end_dices() const;
375
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;
379
380 /// For use iterating over all exported symbols.
381 iterator_range<export_iterator> exports(Error &Err) const;
382
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 =
387 nullptr);
388
389 /// For use iterating over all rebase table entries.
390 iterator_range<rebase_iterator> rebaseTable(Error &Err);
391
392 /// For use examining rebase opcodes in a MachOObjectFile.
393 static iterator_range<rebase_iterator> rebaseTable(Error &Err,
394 MachOObjectFile *O,
395 ArrayRef<uint8_t> Opcodes,
396 bool is64);
397
398 /// For use iterating over all bind table entries.
399 iterator_range<bind_iterator> bindTable(Error &Err);
400
401 /// For use iterating over all lazy bind table entries.
402 iterator_range<bind_iterator> lazyBindTable(Error &Err);
403
404 /// For use iterating over all weak bind table entries.
405 iterator_range<bind_iterator> weakBindTable(Error &Err);
406
407 /// For use examining bind opcodes in a MachOObjectFile.
408 static iterator_range<bind_iterator> bindTable(Error &Err,
409 MachOObjectFile *O,
410 ArrayRef<uint8_t> Opcodes,
411 bool is64,
412 MachOBindEntry::Kind);
413
414 // Given a SegIndex, SegOffset, and PointerSize, verify a valid section exists
415 // that fully contains a pointer at that location. Multiple fixups in a bind
416 // (such as with the BIND_OPCODE_DO_BIND_ULEB_TIMES_SKIPPING_ULEB opcode) can
417 // be tested via the Count and Skip parameters.
418 //
419 // This is used by MachOBindEntry::moveNext() to validate a MachOBindEntry.
420 const char *BindEntryCheckSegAndOffsets(int32_t SegIndex, uint64_t SegOffset,
421 uint8_t PointerSize, uint32_t Count=1,
422 uint32_t Skip=0) const {
423 return BindRebaseSectionTable->checkSegAndOffsets(SegIndex, SegOffset,
424 PointerSize, Count, Skip);
425 }
426
427 // Given a SegIndex, SegOffset, and PointerSize, verify a valid section exists
428 // that fully contains a pointer at that location. Multiple fixups in a rebase
429 // (such as with the REBASE_OPCODE_DO_*_TIMES* opcodes) can be tested via the
430 // Count and Skip parameters.
431 //
432 // This is used by MachORebaseEntry::moveNext() to validate a MachORebaseEntry
433 const char *RebaseEntryCheckSegAndOffsets(int32_t SegIndex,
434 uint64_t SegOffset,
435 uint8_t PointerSize,
436 uint32_t Count=1,
437 uint32_t Skip=0) const {
438 return BindRebaseSectionTable->checkSegAndOffsets(SegIndex, SegOffset,
439 PointerSize, Count, Skip);
440 }
441
442 /// For use with the SegIndex of a checked Mach-O Bind or Rebase entry to
443 /// get the segment name.
444 StringRef BindRebaseSegmentName(int32_t SegIndex) const {
445 return BindRebaseSectionTable->segmentName(SegIndex);
446 }
447
448 /// For use with a SegIndex,SegOffset pair from a checked Mach-O Bind or
449 /// Rebase entry to get the section name.
450 StringRef BindRebaseSectionName(uint32_t SegIndex, uint64_t SegOffset) const {
451 return BindRebaseSectionTable->sectionName(SegIndex, SegOffset);
452 }
453
454 /// For use with a SegIndex,SegOffset pair from a checked Mach-O Bind or
455 /// Rebase entry to get the address.
456 uint64_t BindRebaseAddress(uint32_t SegIndex, uint64_t SegOffset) const {
457 return BindRebaseSectionTable->address(SegIndex, SegOffset);
458 }
459
460 // In a MachO file, sections have a segment name. This is used in the .o
461 // files. They have a single segment, but this field specifies which segment
462 // a section should be put in the final object.
463 StringRef getSectionFinalSegmentName(DataRefImpl Sec) const;
464
465 // Names are stored as 16 bytes. These returns the raw 16 bytes without
466 // interpreting them as a C string.
467 ArrayRef<char> getSectionRawName(DataRefImpl Sec) const;
468 ArrayRef<char> getSectionRawFinalSegmentName(DataRefImpl Sec) const;
469
470 // MachO specific Info about relocations.
471 bool isRelocationScattered(const MachO::any_relocation_info &RE) const;
472 unsigned getPlainRelocationSymbolNum(
473 const MachO::any_relocation_info &RE) const;
474 bool getPlainRelocationExternal(const MachO::any_relocation_info &RE) const;
475 bool getScatteredRelocationScattered(
476 const MachO::any_relocation_info &RE) const;
477 uint32_t getScatteredRelocationValue(
478 const MachO::any_relocation_info &RE) const;
479 uint32_t getScatteredRelocationType(
480 const MachO::any_relocation_info &RE) const;
481 unsigned getAnyRelocationAddress(const MachO::any_relocation_info &RE) const;
482 unsigned getAnyRelocationPCRel(const MachO::any_relocation_info &RE) const;
483 unsigned getAnyRelocationLength(const MachO::any_relocation_info &RE) const;
484 unsigned getAnyRelocationType(const MachO::any_relocation_info &RE) const;
485 SectionRef getAnyRelocationSection(const MachO::any_relocation_info &RE) const;
486
487 // MachO specific structures.
488 MachO::section getSection(DataRefImpl DRI) const;
489 MachO::section_64 getSection64(DataRefImpl DRI) const;
490 MachO::section getSection(const LoadCommandInfo &L, unsigned Index) const;
491 MachO::section_64 getSection64(const LoadCommandInfo &L,unsigned Index) const;
492 MachO::nlist getSymbolTableEntry(DataRefImpl DRI) const;
493 MachO::nlist_64 getSymbol64TableEntry(DataRefImpl DRI) const;
494
495 MachO::linkedit_data_command
496 getLinkeditDataLoadCommand(const LoadCommandInfo &L) const;
497 MachO::segment_command
498 getSegmentLoadCommand(const LoadCommandInfo &L) const;
499 MachO::segment_command_64
500 getSegment64LoadCommand(const LoadCommandInfo &L) const;
501 MachO::linker_option_command
502 getLinkerOptionLoadCommand(const LoadCommandInfo &L) const;
503 MachO::version_min_command
504 getVersionMinLoadCommand(const LoadCommandInfo &L) const;
505 MachO::note_command
506 getNoteLoadCommand(const LoadCommandInfo &L) const;
507 MachO::build_version_command
508 getBuildVersionLoadCommand(const LoadCommandInfo &L) const;
509 MachO::build_tool_version
510 getBuildToolVersion(unsigned index) const;
511 MachO::dylib_command
512 getDylibIDLoadCommand(const LoadCommandInfo &L) const;
513 MachO::dyld_info_command
514 getDyldInfoLoadCommand(const LoadCommandInfo &L) const;
515 MachO::dylinker_command
516 getDylinkerCommand(const LoadCommandInfo &L) const;
517 MachO::uuid_command
518 getUuidCommand(const LoadCommandInfo &L) const;
519 MachO::rpath_command
520 getRpathCommand(const LoadCommandInfo &L) const;
521 MachO::source_version_command
522 getSourceVersionCommand(const LoadCommandInfo &L) const;
523 MachO::entry_point_command
524 getEntryPointCommand(const LoadCommandInfo &L) const;
525 MachO::encryption_info_command
526 getEncryptionInfoCommand(const LoadCommandInfo &L) const;
527 MachO::encryption_info_command_64
528 getEncryptionInfoCommand64(const LoadCommandInfo &L) const;
529 MachO::sub_framework_command
530 getSubFrameworkCommand(const LoadCommandInfo &L) const;
531 MachO::sub_umbrella_command
532 getSubUmbrellaCommand(const LoadCommandInfo &L) const;
533 MachO::sub_library_command
534 getSubLibraryCommand(const LoadCommandInfo &L) const;
535 MachO::sub_client_command
536 getSubClientCommand(const LoadCommandInfo &L) const;
537 MachO::routines_command
538 getRoutinesCommand(const LoadCommandInfo &L) const;
539 MachO::routines_command_64
540 getRoutinesCommand64(const LoadCommandInfo &L) const;
541 MachO::thread_command
542 getThreadCommand(const LoadCommandInfo &L) const;
543
544 MachO::any_relocation_info getRelocation(DataRefImpl Rel) const;
545 MachO::data_in_code_entry getDice(DataRefImpl Rel) const;
546 const MachO::mach_header &getHeader() const;
547 const MachO::mach_header_64 &getHeader64() const;
548 uint32_t
549 getIndirectSymbolTableEntry(const MachO::dysymtab_command &DLC,
550 unsigned Index) const;
551 MachO::data_in_code_entry getDataInCodeTableEntry(uint32_t DataOffset,
552 unsigned Index) const;
553 MachO::symtab_command getSymtabLoadCommand() const;
554 MachO::dysymtab_command getDysymtabLoadCommand() const;
555 MachO::linkedit_data_command getDataInCodeLoadCommand() const;
556 MachO::linkedit_data_command getLinkOptHintsLoadCommand() const;
557 ArrayRef<uint8_t> getDyldInfoRebaseOpcodes() const;
558 ArrayRef<uint8_t> getDyldInfoBindOpcodes() const;
559 ArrayRef<uint8_t> getDyldInfoWeakBindOpcodes() const;
560 ArrayRef<uint8_t> getDyldInfoLazyBindOpcodes() const;
561 ArrayRef<uint8_t> getDyldInfoExportsTrie() const;
562 ArrayRef<uint8_t> getUuid() const;
563
564 StringRef getStringTableData() const;
565 bool is64Bit() const;
566 void ReadULEB128s(uint64_t Index, SmallVectorImpl<uint64_t> &Out) const;
567
568 static StringRef guessLibraryShortName(StringRef Name, bool &isFramework,
569 StringRef &Suffix);
570
571 static Triple::ArchType getArch(uint32_t CPUType, uint32_t CPUSubType);
572 static Triple getArchTriple(uint32_t CPUType, uint32_t CPUSubType,
573 const char **McpuDefault = nullptr,
574 const char **ArchFlag = nullptr);
575 static bool isValidArch(StringRef ArchFlag);
576 static ArrayRef<StringRef> getValidArchs();
577 static Triple getHostArch();
578
579 bool isRelocatableObject() const override;
580
581 StringRef mapDebugSectionName(StringRef Name) const override;
582
583 bool hasPageZeroSegment() const { return HasPageZeroSegment; }
584
585 static bool classof(const Binary *v) {
586 return v->isMachO();
587 }
588
589 static uint32_t
590 getVersionMinMajor(MachO::version_min_command &C, bool SDK) {
591 uint32_t VersionOrSDK = (SDK) ? C.sdk : C.version;
592 return (VersionOrSDK >> 16) & 0xffff;
593 }
594
595 static uint32_t
596 getVersionMinMinor(MachO::version_min_command &C, bool SDK) {
597 uint32_t VersionOrSDK = (SDK) ? C.sdk : C.version;
598 return (VersionOrSDK >> 8) & 0xff;
599 }
600
601 static uint32_t
602 getVersionMinUpdate(MachO::version_min_command &C, bool SDK) {
603 uint32_t VersionOrSDK = (SDK) ? C.sdk : C.version;
604 return VersionOrSDK & 0xff;
605 }
606
607 static std::string getBuildPlatform(uint32_t platform) {
608 switch (platform) {
609 case MachO::PLATFORM_MACOS: return "macos";
610 case MachO::PLATFORM_IOS: return "ios";
611 case MachO::PLATFORM_TVOS: return "tvos";
612 case MachO::PLATFORM_WATCHOS: return "watchos";
613 case MachO::PLATFORM_BRIDGEOS: return "bridgeos";
614 case MachO::PLATFORM_MACCATALYST: return "macCatalyst";
615 case MachO::PLATFORM_IOSSIMULATOR: return "iossimulator";
616 case MachO::PLATFORM_TVOSSIMULATOR: return "tvossimulator";
617 case MachO::PLATFORM_WATCHOSSIMULATOR: return "watchossimulator";
618 case MachO::PLATFORM_DRIVERKIT: return "driverkit";
619 default:
620 std::string ret;
621 raw_string_ostream ss(ret);
622 ss << format_hex(platform, 8, true);
623 return ss.str();
624 }
625 }
626
627 static std::string getBuildTool(uint32_t tools) {
628 switch (tools) {
629 case MachO::TOOL_CLANG: return "clang";
630 case MachO::TOOL_SWIFT: return "swift";
631 case MachO::TOOL_LD: return "ld";
632 default:
633 std::string ret;
634 raw_string_ostream ss(ret);
635 ss << format_hex(tools, 8, true);
636 return ss.str();
637 }
638 }
639
640 static std::string getVersionString(uint32_t version) {
641 uint32_t major = (version >> 16) & 0xffff;
642 uint32_t minor = (version >> 8) & 0xff;
643 uint32_t update = version & 0xff;
644
645 SmallString<32> Version;
646 Version = utostr(major) + "." + utostr(minor);
647 if (update != 0)
648 Version += "." + utostr(update);
649 return std::string(std::string(Version.str()));
650 }
651
652private:
653 MachOObjectFile(MemoryBufferRef Object, bool IsLittleEndian, bool Is64Bits,
654 Error &Err, uint32_t UniversalCputype = 0,
655 uint32_t UniversalIndex = 0);
656
657 uint64_t getSymbolValueImpl(DataRefImpl Symb) const override;
658
659 union {
660 MachO::mach_header_64 Header64;
661 MachO::mach_header Header;
662 };
663 using SectionList = SmallVector<const char*, 1>;
664 SectionList Sections;
665 using LibraryList = SmallVector<const char*, 1>;
666 LibraryList Libraries;
667 LoadCommandList LoadCommands;
668 using LibraryShortName = SmallVector<StringRef, 1>;
669 using BuildToolList = SmallVector<const char*, 1>;
670 BuildToolList BuildTools;
671 mutable LibraryShortName LibrariesShortNames;
672 std::unique_ptr<BindRebaseSegInfo> BindRebaseSectionTable;
673 const char *SymtabLoadCmd = nullptr;
674 const char *DysymtabLoadCmd = nullptr;
675 const char *DataInCodeLoadCmd = nullptr;
676 const char *LinkOptHintsLoadCmd = nullptr;
677 const char *DyldInfoLoadCmd = nullptr;
678 const char *UuidLoadCmd = nullptr;
679 bool HasPageZeroSegment = false;
680};
681
682/// DiceRef
683inline DiceRef::DiceRef(DataRefImpl DiceP, const ObjectFile *Owner)
684 : DicePimpl(DiceP) , OwningObject(Owner) {}
685
686inline bool DiceRef::operator==(const DiceRef &Other) const {
687 return DicePimpl == Other.DicePimpl;
688}
689
690inline bool DiceRef::operator<(const DiceRef &Other) const {
691 return DicePimpl < Other.DicePimpl;
692}
693
694inline void DiceRef::moveNext() {
695 const MachO::data_in_code_entry *P =
696 reinterpret_cast<const MachO::data_in_code_entry *>(DicePimpl.p);
697 DicePimpl.p = reinterpret_cast<uintptr_t>(P + 1);
698}
699
700// Since a Mach-O data in code reference, a DiceRef, can only be created when
701// the OwningObject ObjectFile is a MachOObjectFile a static_cast<> is used for
702// the methods that get the values of the fields of the reference.
703
704inline std::error_code DiceRef::getOffset(uint32_t &Result) const {
705 const MachOObjectFile *MachOOF =
706 static_cast<const MachOObjectFile *>(OwningObject);
707 MachO::data_in_code_entry Dice = MachOOF->getDice(DicePimpl);
708 Result = Dice.offset;
709 return std::error_code();
710}
711
712inline std::error_code DiceRef::getLength(uint16_t &Result) const {
713 const MachOObjectFile *MachOOF =
714 static_cast<const MachOObjectFile *>(OwningObject);
715 MachO::data_in_code_entry Dice = MachOOF->getDice(DicePimpl);
716 Result = Dice.length;
717 return std::error_code();
718}
719
720inline std::error_code DiceRef::getKind(uint16_t &Result) const {
721 const MachOObjectFile *MachOOF =
722 static_cast<const MachOObjectFile *>(OwningObject);
723 MachO::data_in_code_entry Dice = MachOOF->getDice(DicePimpl);
724 Result = Dice.kind;
725 return std::error_code();
726}
727
728inline DataRefImpl DiceRef::getRawDataRefImpl() const {
729 return DicePimpl;
730}
731
732inline const ObjectFile *DiceRef::getObjectFile() const {
733 return OwningObject;
734}
735
736} // end namespace object
737} // end namespace llvm
738
739#endif // LLVM_OBJECT_MACHO_H
740