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/SmallVector.h"
20 #include "llvm/ADT/Triple.h"
21 #include "llvm/Object/ObjectFile.h"
22 #include "llvm/Support/MachO.h"
27 /// DiceRef - This is a value type class that represents a single
28 /// data in code entry in the table in a Mach-O object file.
30 DataRefImpl DicePimpl;
31 const ObjectFile *OwningObject;
34 DiceRef() : OwningObject(nullptr) { }
36 DiceRef(DataRefImpl DiceP, const ObjectFile *Owner);
38 bool operator==(const DiceRef &Other) const;
39 bool operator<(const DiceRef &Other) const;
43 std::error_code getOffset(uint32_t &Result) const;
44 std::error_code getLength(uint16_t &Result) const;
45 std::error_code getKind(uint16_t &Result) const;
47 DataRefImpl getRawDataRefImpl() const;
48 const ObjectFile *getObjectFile() const;
50 typedef content_iterator<DiceRef> dice_iterator;
52 /// ExportEntry encapsulates the current-state-of-the-walk used when doing a
53 /// non-recursive walk of the trie data structure. This allows you to iterate
54 /// across all exported symbols using:
55 /// for (const llvm::object::ExportEntry &AnExport : Obj->exports()) {
59 ExportEntry(ArrayRef<uint8_t> Trie);
61 StringRef name() const;
62 uint64_t flags() const;
63 uint64_t address() const;
64 uint64_t other() const;
65 StringRef otherName() const;
66 uint32_t nodeOffset() const;
68 bool operator==(const ExportEntry &) const;
73 friend class MachOObjectFile;
76 uint64_t readULEB128(const uint8_t *&p);
77 void pushDownUntilBottom();
78 void pushNode(uint64_t Offset);
80 // Represents a node in the mach-o exports trie.
82 NodeState(const uint8_t *Ptr);
84 const uint8_t *Current;
88 const char *ImportName;
90 unsigned NextChildIndex;
91 unsigned ParentStringLength;
95 ArrayRef<uint8_t> Trie;
96 SmallString<256> CumulativeString;
97 SmallVector<NodeState, 16> Stack;
101 typedef content_iterator<ExportEntry> export_iterator;
103 /// MachORebaseEntry encapsulates the current state in the decompression of
104 /// rebasing opcodes. This allows you to iterate through the compressed table of
106 /// for (const llvm::object::MachORebaseEntry &Entry : Obj->rebaseTable()) {
108 class MachORebaseEntry {
110 MachORebaseEntry(ArrayRef<uint8_t> opcodes, bool is64Bit);
112 uint32_t segmentIndex() const;
113 uint64_t segmentOffset() const;
114 StringRef typeName() const;
116 bool operator==(const MachORebaseEntry &) const;
121 friend class MachOObjectFile;
124 uint64_t readULEB128();
126 ArrayRef<uint8_t> Opcodes;
128 uint64_t SegmentOffset;
129 uint32_t SegmentIndex;
130 uint64_t RemainingLoopCount;
131 uint64_t AdvanceAmount;
137 typedef content_iterator<MachORebaseEntry> rebase_iterator;
139 /// MachOBindEntry encapsulates the current state in the decompression of
140 /// binding opcodes. This allows you to iterate through the compressed table of
142 /// for (const llvm::object::MachOBindEntry &Entry : Obj->bindTable()) {
144 class MachOBindEntry {
146 enum class Kind { Regular, Lazy, Weak };
148 MachOBindEntry(ArrayRef<uint8_t> Opcodes, bool is64Bit, MachOBindEntry::Kind);
150 uint32_t segmentIndex() const;
151 uint64_t segmentOffset() const;
152 StringRef typeName() const;
153 StringRef symbolName() const;
154 uint32_t flags() const;
155 int64_t addend() const;
158 bool operator==(const MachOBindEntry &) const;
163 friend class MachOObjectFile;
166 uint64_t readULEB128();
167 int64_t readSLEB128();
169 ArrayRef<uint8_t> Opcodes;
171 uint64_t SegmentOffset;
172 uint32_t SegmentIndex;
173 StringRef SymbolName;
177 uint64_t RemainingLoopCount;
178 uint64_t AdvanceAmount;
185 typedef content_iterator<MachOBindEntry> bind_iterator;
187 class MachOObjectFile : public ObjectFile {
189 struct LoadCommandInfo {
190 const char *Ptr; // Where in memory the load command is.
191 MachO::load_command C; // The command itself.
194 MachOObjectFile(MemoryBufferRef Object, bool IsLittleEndian, bool Is64Bits,
195 std::error_code &EC);
197 void moveSymbolNext(DataRefImpl &Symb) const override;
198 std::error_code getSymbolName(DataRefImpl Symb,
199 StringRef &Res) const override;
202 std::error_code getIndirectName(DataRefImpl Symb, StringRef &Res) const;
203 unsigned getSectionType(SectionRef Sec) const;
205 std::error_code getSymbolAddress(DataRefImpl Symb,
206 uint64_t &Res) const override;
207 std::error_code getSymbolAlignment(DataRefImpl Symb,
208 uint32_t &Res) const override;
209 std::error_code getSymbolSize(DataRefImpl Symb, uint64_t &Res) const override;
210 std::error_code getSymbolType(DataRefImpl Symb,
211 SymbolRef::Type &Res) const override;
212 uint32_t getSymbolFlags(DataRefImpl Symb) const override;
213 std::error_code getSymbolSection(DataRefImpl Symb,
214 section_iterator &Res) const override;
216 void moveSectionNext(DataRefImpl &Sec) const override;
217 std::error_code getSectionName(DataRefImpl Sec,
218 StringRef &Res) const override;
219 uint64_t getSectionAddress(DataRefImpl Sec) const override;
220 uint64_t getSectionSize(DataRefImpl Sec) const override;
221 std::error_code getSectionContents(DataRefImpl Sec,
222 StringRef &Res) const override;
223 uint64_t getSectionAlignment(DataRefImpl Sec) const override;
224 bool isSectionText(DataRefImpl Sec) const override;
225 bool isSectionData(DataRefImpl Sec) const override;
226 bool isSectionBSS(DataRefImpl Sec) const override;
227 bool isSectionVirtual(DataRefImpl Sec) const override;
228 bool sectionContainsSymbol(DataRefImpl Sec, DataRefImpl Symb) const override;
229 relocation_iterator section_rel_begin(DataRefImpl Sec) const override;
230 relocation_iterator section_rel_end(DataRefImpl Sec) const override;
232 void moveRelocationNext(DataRefImpl &Rel) const override;
233 std::error_code getRelocationAddress(DataRefImpl Rel,
234 uint64_t &Res) const override;
235 std::error_code getRelocationOffset(DataRefImpl Rel,
236 uint64_t &Res) const override;
237 symbol_iterator getRelocationSymbol(DataRefImpl Rel) const override;
238 std::error_code getRelocationType(DataRefImpl Rel,
239 uint64_t &Res) const override;
241 getRelocationTypeName(DataRefImpl Rel,
242 SmallVectorImpl<char> &Result) const override;
244 getRelocationValueString(DataRefImpl Rel,
245 SmallVectorImpl<char> &Result) const override;
246 std::error_code getRelocationHidden(DataRefImpl Rel,
247 bool &Result) const override;
250 std::error_code getLibraryShortNameByIndex(unsigned Index, StringRef &) const;
252 // TODO: Would be useful to have an iterator based version
253 // of the load command interface too.
255 basic_symbol_iterator symbol_begin_impl() const override;
256 basic_symbol_iterator symbol_end_impl() const override;
259 basic_symbol_iterator getSymbolByIndex(unsigned Index) const;
261 section_iterator section_begin() const override;
262 section_iterator section_end() const override;
264 uint8_t getBytesInAddress() const override;
266 StringRef getFileFormatName() const override;
267 unsigned getArch() const override;
268 Triple getArch(const char **McpuDefault, Triple *ThumbTriple) const;
270 relocation_iterator section_rel_begin(unsigned Index) const;
271 relocation_iterator section_rel_end(unsigned Index) const;
273 dice_iterator begin_dices() const;
274 dice_iterator end_dices() const;
276 /// For use iterating over all exported symbols.
277 iterator_range<export_iterator> exports() const;
279 /// For use examining a trie not in a MachOObjectFile.
280 static iterator_range<export_iterator> exports(ArrayRef<uint8_t> Trie);
282 /// For use iterating over all rebase table entries.
283 iterator_range<rebase_iterator> rebaseTable() const;
285 /// For use examining rebase opcodes not in a MachOObjectFile.
286 static iterator_range<rebase_iterator> rebaseTable(ArrayRef<uint8_t> Opcodes,
289 /// For use iterating over all bind table entries.
290 iterator_range<bind_iterator> bindTable() const;
292 /// For use iterating over all lazy bind table entries.
293 iterator_range<bind_iterator> lazyBindTable() const;
295 /// For use iterating over all lazy bind table entries.
296 iterator_range<bind_iterator> weakBindTable() const;
298 /// For use examining bind opcodes not in a MachOObjectFile.
299 static iterator_range<bind_iterator> bindTable(ArrayRef<uint8_t> Opcodes,
301 MachOBindEntry::Kind);
304 // In a MachO file, sections have a segment name. This is used in the .o
305 // files. They have a single segment, but this field specifies which segment
306 // a section should be put in in the final object.
307 StringRef getSectionFinalSegmentName(DataRefImpl Sec) const;
309 // Names are stored as 16 bytes. These returns the raw 16 bytes without
310 // interpreting them as a C string.
311 ArrayRef<char> getSectionRawName(DataRefImpl Sec) const;
312 ArrayRef<char> getSectionRawFinalSegmentName(DataRefImpl Sec) const;
314 // MachO specific Info about relocations.
315 bool isRelocationScattered(const MachO::any_relocation_info &RE) const;
316 unsigned getPlainRelocationSymbolNum(
317 const MachO::any_relocation_info &RE) const;
318 bool getPlainRelocationExternal(const MachO::any_relocation_info &RE) const;
319 bool getScatteredRelocationScattered(
320 const MachO::any_relocation_info &RE) const;
321 uint32_t getScatteredRelocationValue(
322 const MachO::any_relocation_info &RE) const;
323 uint32_t getScatteredRelocationType(
324 const MachO::any_relocation_info &RE) const;
325 unsigned getAnyRelocationAddress(const MachO::any_relocation_info &RE) const;
326 unsigned getAnyRelocationPCRel(const MachO::any_relocation_info &RE) const;
327 unsigned getAnyRelocationLength(const MachO::any_relocation_info &RE) const;
328 unsigned getAnyRelocationType(const MachO::any_relocation_info &RE) const;
329 SectionRef getRelocationSection(const MachO::any_relocation_info &RE) const;
331 // Walk load commands.
332 LoadCommandInfo getFirstLoadCommandInfo() const;
333 LoadCommandInfo getNextLoadCommandInfo(const LoadCommandInfo &L) const;
335 // MachO specific structures.
336 MachO::section getSection(DataRefImpl DRI) const;
337 MachO::section_64 getSection64(DataRefImpl DRI) const;
338 MachO::section getSection(const LoadCommandInfo &L, unsigned Index) const;
339 MachO::section_64 getSection64(const LoadCommandInfo &L,unsigned Index) const;
340 MachO::nlist getSymbolTableEntry(DataRefImpl DRI) const;
341 MachO::nlist_64 getSymbol64TableEntry(DataRefImpl DRI) const;
343 MachO::linkedit_data_command
344 getLinkeditDataLoadCommand(const LoadCommandInfo &L) const;
345 MachO::segment_command
346 getSegmentLoadCommand(const LoadCommandInfo &L) const;
347 MachO::segment_command_64
348 getSegment64LoadCommand(const LoadCommandInfo &L) const;
349 MachO::linker_options_command
350 getLinkerOptionsLoadCommand(const LoadCommandInfo &L) const;
351 MachO::version_min_command
352 getVersionMinLoadCommand(const LoadCommandInfo &L) const;
354 getDylibIDLoadCommand(const LoadCommandInfo &L) const;
355 MachO::dyld_info_command
356 getDyldInfoLoadCommand(const LoadCommandInfo &L) const;
357 MachO::dylinker_command
358 getDylinkerCommand(const LoadCommandInfo &L) const;
360 getUuidCommand(const LoadCommandInfo &L) const;
362 getRpathCommand(const LoadCommandInfo &L) const;
363 MachO::source_version_command
364 getSourceVersionCommand(const LoadCommandInfo &L) const;
365 MachO::entry_point_command
366 getEntryPointCommand(const LoadCommandInfo &L) const;
367 MachO::encryption_info_command
368 getEncryptionInfoCommand(const LoadCommandInfo &L) const;
370 MachO::any_relocation_info getRelocation(DataRefImpl Rel) const;
371 MachO::data_in_code_entry getDice(DataRefImpl Rel) const;
372 MachO::mach_header getHeader() const;
373 MachO::mach_header_64 getHeader64() const;
375 getIndirectSymbolTableEntry(const MachO::dysymtab_command &DLC,
376 unsigned Index) const;
377 MachO::data_in_code_entry getDataInCodeTableEntry(uint32_t DataOffset,
378 unsigned Index) const;
379 MachO::symtab_command getSymtabLoadCommand() const;
380 MachO::dysymtab_command getDysymtabLoadCommand() const;
381 MachO::linkedit_data_command getDataInCodeLoadCommand() const;
382 ArrayRef<uint8_t> getDyldInfoRebaseOpcodes() const;
383 ArrayRef<uint8_t> getDyldInfoBindOpcodes() const;
384 ArrayRef<uint8_t> getDyldInfoWeakBindOpcodes() const;
385 ArrayRef<uint8_t> getDyldInfoLazyBindOpcodes() const;
386 ArrayRef<uint8_t> getDyldInfoExportsTrie() const;
387 ArrayRef<uint8_t> getUuid() const;
389 StringRef getStringTableData() const;
390 bool is64Bit() const;
391 void ReadULEB128s(uint64_t Index, SmallVectorImpl<uint64_t> &Out) const;
393 static StringRef guessLibraryShortName(StringRef Name, bool &isFramework,
396 static Triple::ArchType getArch(uint32_t CPUType);
397 static Triple getArch(uint32_t CPUType, uint32_t CPUSubType,
398 const char **McpuDefault = nullptr);
399 static Triple getThumbArch(uint32_t CPUType, uint32_t CPUSubType,
400 const char **McpuDefault = nullptr);
401 static Triple getArch(uint32_t CPUType, uint32_t CPUSubType,
402 const char **McpuDefault, Triple *ThumbTriple);
403 static bool isValidArch(StringRef ArchFlag);
404 static Triple getHostArch();
406 bool isRelocatableObject() const override;
408 bool hasPageZeroSegment() const { return HasPageZeroSegment; }
410 static bool classof(const Binary *v) {
415 typedef SmallVector<const char*, 1> SectionList;
416 SectionList Sections;
417 typedef SmallVector<const char*, 1> LibraryList;
418 LibraryList Libraries;
419 typedef SmallVector<StringRef, 1> LibraryShortName;
420 mutable LibraryShortName LibrariesShortNames;
421 const char *SymtabLoadCmd;
422 const char *DysymtabLoadCmd;
423 const char *DataInCodeLoadCmd;
424 const char *DyldInfoLoadCmd;
425 const char *UuidLoadCmd;
426 bool HasPageZeroSegment;
430 inline DiceRef::DiceRef(DataRefImpl DiceP, const ObjectFile *Owner)
431 : DicePimpl(DiceP) , OwningObject(Owner) {}
433 inline bool DiceRef::operator==(const DiceRef &Other) const {
434 return DicePimpl == Other.DicePimpl;
437 inline bool DiceRef::operator<(const DiceRef &Other) const {
438 return DicePimpl < Other.DicePimpl;
441 inline void DiceRef::moveNext() {
442 const MachO::data_in_code_entry *P =
443 reinterpret_cast<const MachO::data_in_code_entry *>(DicePimpl.p);
444 DicePimpl.p = reinterpret_cast<uintptr_t>(P + 1);
447 // Since a Mach-O data in code reference, a DiceRef, can only be created when
448 // the OwningObject ObjectFile is a MachOObjectFile a static_cast<> is used for
449 // the methods that get the values of the fields of the reference.
451 inline std::error_code DiceRef::getOffset(uint32_t &Result) const {
452 const MachOObjectFile *MachOOF =
453 static_cast<const MachOObjectFile *>(OwningObject);
454 MachO::data_in_code_entry Dice = MachOOF->getDice(DicePimpl);
455 Result = Dice.offset;
456 return object_error::success;
459 inline std::error_code DiceRef::getLength(uint16_t &Result) const {
460 const MachOObjectFile *MachOOF =
461 static_cast<const MachOObjectFile *>(OwningObject);
462 MachO::data_in_code_entry Dice = MachOOF->getDice(DicePimpl);
463 Result = Dice.length;
464 return object_error::success;
467 inline std::error_code DiceRef::getKind(uint16_t &Result) const {
468 const MachOObjectFile *MachOOF =
469 static_cast<const MachOObjectFile *>(OwningObject);
470 MachO::data_in_code_entry Dice = MachOOF->getDice(DicePimpl);
472 return object_error::success;
475 inline DataRefImpl DiceRef::getRawDataRefImpl() const {
479 inline const ObjectFile *DiceRef::getObjectFile() const {