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.
193 typedef SmallVector<LoadCommandInfo, 4> LoadCommandList;
194 typedef LoadCommandList::const_iterator load_command_iterator;
196 MachOObjectFile(MemoryBufferRef Object, bool IsLittleEndian, bool Is64Bits,
197 std::error_code &EC);
199 void moveSymbolNext(DataRefImpl &Symb) const override;
201 uint64_t getNValue(DataRefImpl Sym) const;
202 std::error_code getSymbolName(DataRefImpl Symb,
203 StringRef &Res) const override;
206 std::error_code getIndirectName(DataRefImpl Symb, StringRef &Res) const;
207 unsigned getSectionType(SectionRef Sec) const;
209 std::error_code getSymbolAddress(DataRefImpl Symb,
210 uint64_t &Res) const override;
211 uint64_t getSymbolValue(DataRefImpl Symb) const override;
212 uint32_t getSymbolAlignment(DataRefImpl Symb) const override;
213 uint64_t getCommonSymbolSizeImpl(DataRefImpl Symb) const override;
214 SymbolRef::Type getSymbolType(DataRefImpl Symb) const override;
215 uint32_t getSymbolFlags(DataRefImpl Symb) const override;
216 std::error_code getSymbolSection(DataRefImpl Symb,
217 section_iterator &Res) const override;
218 unsigned getSymbolSectionID(SymbolRef Symb) const;
219 unsigned getSectionID(SectionRef Sec) const;
221 void moveSectionNext(DataRefImpl &Sec) const override;
222 std::error_code getSectionName(DataRefImpl Sec,
223 StringRef &Res) const override;
224 uint64_t getSectionAddress(DataRefImpl Sec) const override;
225 uint64_t getSectionSize(DataRefImpl Sec) const override;
226 std::error_code getSectionContents(DataRefImpl Sec,
227 StringRef &Res) const override;
228 uint64_t getSectionAlignment(DataRefImpl Sec) const override;
229 bool isSectionText(DataRefImpl Sec) const override;
230 bool isSectionData(DataRefImpl Sec) const override;
231 bool isSectionBSS(DataRefImpl Sec) const override;
232 bool isSectionVirtual(DataRefImpl Sec) const override;
233 bool sectionContainsSymbol(DataRefImpl Sec, DataRefImpl Symb) const override;
234 relocation_iterator section_rel_begin(DataRefImpl Sec) const override;
235 relocation_iterator section_rel_end(DataRefImpl Sec) const override;
237 void moveRelocationNext(DataRefImpl &Rel) const override;
238 std::error_code getRelocationAddress(DataRefImpl Rel,
239 uint64_t &Res) const override;
240 std::error_code getRelocationOffset(DataRefImpl Rel,
241 uint64_t &Res) const override;
242 symbol_iterator getRelocationSymbol(DataRefImpl Rel) const override;
243 section_iterator getRelocationSection(DataRefImpl Rel) const;
244 std::error_code getRelocationType(DataRefImpl Rel,
245 uint64_t &Res) const override;
247 getRelocationTypeName(DataRefImpl Rel,
248 SmallVectorImpl<char> &Result) const override;
249 std::error_code getRelocationHidden(DataRefImpl Rel,
250 bool &Result) const override;
251 uint8_t getRelocationLength(DataRefImpl Rel) const;
254 std::error_code getLibraryShortNameByIndex(unsigned Index, StringRef &) const;
256 // TODO: Would be useful to have an iterator based version
257 // of the load command interface too.
259 basic_symbol_iterator symbol_begin_impl() const override;
260 basic_symbol_iterator symbol_end_impl() const override;
263 basic_symbol_iterator getSymbolByIndex(unsigned Index) const;
265 section_iterator section_begin() const override;
266 section_iterator section_end() const override;
268 uint8_t getBytesInAddress() const override;
270 StringRef getFileFormatName() const override;
271 unsigned getArch() const override;
272 Triple getArch(const char **McpuDefault, Triple *ThumbTriple) const;
274 relocation_iterator section_rel_begin(unsigned Index) const;
275 relocation_iterator section_rel_end(unsigned Index) const;
277 dice_iterator begin_dices() const;
278 dice_iterator end_dices() const;
280 load_command_iterator begin_load_commands() const;
281 load_command_iterator end_load_commands() const;
282 iterator_range<load_command_iterator> load_commands() const;
284 /// For use iterating over all exported symbols.
285 iterator_range<export_iterator> exports() const;
287 /// For use examining a trie not in a MachOObjectFile.
288 static iterator_range<export_iterator> exports(ArrayRef<uint8_t> Trie);
290 /// For use iterating over all rebase table entries.
291 iterator_range<rebase_iterator> rebaseTable() const;
293 /// For use examining rebase opcodes not in a MachOObjectFile.
294 static iterator_range<rebase_iterator> rebaseTable(ArrayRef<uint8_t> Opcodes,
297 /// For use iterating over all bind table entries.
298 iterator_range<bind_iterator> bindTable() const;
300 /// For use iterating over all lazy bind table entries.
301 iterator_range<bind_iterator> lazyBindTable() const;
303 /// For use iterating over all lazy bind table entries.
304 iterator_range<bind_iterator> weakBindTable() const;
306 /// For use examining bind opcodes not in a MachOObjectFile.
307 static iterator_range<bind_iterator> bindTable(ArrayRef<uint8_t> Opcodes,
309 MachOBindEntry::Kind);
312 // In a MachO file, sections have a segment name. This is used in the .o
313 // files. They have a single segment, but this field specifies which segment
314 // a section should be put in in the final object.
315 StringRef getSectionFinalSegmentName(DataRefImpl Sec) const;
317 // Names are stored as 16 bytes. These returns the raw 16 bytes without
318 // interpreting them as a C string.
319 ArrayRef<char> getSectionRawName(DataRefImpl Sec) const;
320 ArrayRef<char> getSectionRawFinalSegmentName(DataRefImpl Sec) const;
322 // MachO specific Info about relocations.
323 bool isRelocationScattered(const MachO::any_relocation_info &RE) const;
324 unsigned getPlainRelocationSymbolNum(
325 const MachO::any_relocation_info &RE) const;
326 bool getPlainRelocationExternal(const MachO::any_relocation_info &RE) const;
327 bool getScatteredRelocationScattered(
328 const MachO::any_relocation_info &RE) const;
329 uint32_t getScatteredRelocationValue(
330 const MachO::any_relocation_info &RE) const;
331 uint32_t getScatteredRelocationType(
332 const MachO::any_relocation_info &RE) const;
333 unsigned getAnyRelocationAddress(const MachO::any_relocation_info &RE) const;
334 unsigned getAnyRelocationPCRel(const MachO::any_relocation_info &RE) const;
335 unsigned getAnyRelocationLength(const MachO::any_relocation_info &RE) const;
336 unsigned getAnyRelocationType(const MachO::any_relocation_info &RE) const;
337 SectionRef getAnyRelocationSection(const MachO::any_relocation_info &RE) const;
339 // MachO specific structures.
340 MachO::section getSection(DataRefImpl DRI) const;
341 MachO::section_64 getSection64(DataRefImpl DRI) const;
342 MachO::section getSection(const LoadCommandInfo &L, unsigned Index) const;
343 MachO::section_64 getSection64(const LoadCommandInfo &L,unsigned Index) const;
344 MachO::nlist getSymbolTableEntry(DataRefImpl DRI) const;
345 MachO::nlist_64 getSymbol64TableEntry(DataRefImpl DRI) const;
347 MachO::linkedit_data_command
348 getLinkeditDataLoadCommand(const LoadCommandInfo &L) const;
349 MachO::segment_command
350 getSegmentLoadCommand(const LoadCommandInfo &L) const;
351 MachO::segment_command_64
352 getSegment64LoadCommand(const LoadCommandInfo &L) const;
353 MachO::linker_option_command
354 getLinkerOptionLoadCommand(const LoadCommandInfo &L) const;
355 MachO::version_min_command
356 getVersionMinLoadCommand(const LoadCommandInfo &L) const;
358 getDylibIDLoadCommand(const LoadCommandInfo &L) const;
359 MachO::dyld_info_command
360 getDyldInfoLoadCommand(const LoadCommandInfo &L) const;
361 MachO::dylinker_command
362 getDylinkerCommand(const LoadCommandInfo &L) const;
364 getUuidCommand(const LoadCommandInfo &L) const;
366 getRpathCommand(const LoadCommandInfo &L) const;
367 MachO::source_version_command
368 getSourceVersionCommand(const LoadCommandInfo &L) const;
369 MachO::entry_point_command
370 getEntryPointCommand(const LoadCommandInfo &L) const;
371 MachO::encryption_info_command
372 getEncryptionInfoCommand(const LoadCommandInfo &L) const;
373 MachO::encryption_info_command_64
374 getEncryptionInfoCommand64(const LoadCommandInfo &L) const;
375 MachO::sub_framework_command
376 getSubFrameworkCommand(const LoadCommandInfo &L) const;
377 MachO::sub_umbrella_command
378 getSubUmbrellaCommand(const LoadCommandInfo &L) const;
379 MachO::sub_library_command
380 getSubLibraryCommand(const LoadCommandInfo &L) const;
381 MachO::sub_client_command
382 getSubClientCommand(const LoadCommandInfo &L) const;
383 MachO::routines_command
384 getRoutinesCommand(const LoadCommandInfo &L) const;
385 MachO::routines_command_64
386 getRoutinesCommand64(const LoadCommandInfo &L) const;
387 MachO::thread_command
388 getThreadCommand(const LoadCommandInfo &L) const;
390 MachO::any_relocation_info getRelocation(DataRefImpl Rel) const;
391 MachO::data_in_code_entry getDice(DataRefImpl Rel) const;
392 const MachO::mach_header &getHeader() const;
393 const MachO::mach_header_64 &getHeader64() const;
395 getIndirectSymbolTableEntry(const MachO::dysymtab_command &DLC,
396 unsigned Index) const;
397 MachO::data_in_code_entry getDataInCodeTableEntry(uint32_t DataOffset,
398 unsigned Index) const;
399 MachO::symtab_command getSymtabLoadCommand() const;
400 MachO::dysymtab_command getDysymtabLoadCommand() const;
401 MachO::linkedit_data_command getDataInCodeLoadCommand() const;
402 MachO::linkedit_data_command getLinkOptHintsLoadCommand() const;
403 ArrayRef<uint8_t> getDyldInfoRebaseOpcodes() const;
404 ArrayRef<uint8_t> getDyldInfoBindOpcodes() const;
405 ArrayRef<uint8_t> getDyldInfoWeakBindOpcodes() const;
406 ArrayRef<uint8_t> getDyldInfoLazyBindOpcodes() const;
407 ArrayRef<uint8_t> getDyldInfoExportsTrie() const;
408 ArrayRef<uint8_t> getUuid() const;
410 StringRef getStringTableData() const;
411 bool is64Bit() const;
412 void ReadULEB128s(uint64_t Index, SmallVectorImpl<uint64_t> &Out) const;
414 static StringRef guessLibraryShortName(StringRef Name, bool &isFramework,
417 static Triple::ArchType getArch(uint32_t CPUType);
418 static Triple getArch(uint32_t CPUType, uint32_t CPUSubType,
419 const char **McpuDefault = nullptr);
420 static Triple getThumbArch(uint32_t CPUType, uint32_t CPUSubType,
421 const char **McpuDefault = nullptr);
422 static Triple getArch(uint32_t CPUType, uint32_t CPUSubType,
423 const char **McpuDefault, Triple *ThumbTriple);
424 static bool isValidArch(StringRef ArchFlag);
425 static Triple getHostArch();
427 bool isRelocatableObject() const override;
429 bool hasPageZeroSegment() const { return HasPageZeroSegment; }
431 static bool classof(const Binary *v) {
437 MachO::mach_header_64 Header64;
438 MachO::mach_header Header;
440 typedef SmallVector<const char*, 1> SectionList;
441 SectionList Sections;
442 typedef SmallVector<const char*, 1> LibraryList;
443 LibraryList Libraries;
444 LoadCommandList LoadCommands;
445 typedef SmallVector<StringRef, 1> LibraryShortName;
446 mutable LibraryShortName LibrariesShortNames;
447 const char *SymtabLoadCmd;
448 const char *DysymtabLoadCmd;
449 const char *DataInCodeLoadCmd;
450 const char *LinkOptHintsLoadCmd;
451 const char *DyldInfoLoadCmd;
452 const char *UuidLoadCmd;
453 bool HasPageZeroSegment;
457 inline DiceRef::DiceRef(DataRefImpl DiceP, const ObjectFile *Owner)
458 : DicePimpl(DiceP) , OwningObject(Owner) {}
460 inline bool DiceRef::operator==(const DiceRef &Other) const {
461 return DicePimpl == Other.DicePimpl;
464 inline bool DiceRef::operator<(const DiceRef &Other) const {
465 return DicePimpl < Other.DicePimpl;
468 inline void DiceRef::moveNext() {
469 const MachO::data_in_code_entry *P =
470 reinterpret_cast<const MachO::data_in_code_entry *>(DicePimpl.p);
471 DicePimpl.p = reinterpret_cast<uintptr_t>(P + 1);
474 // Since a Mach-O data in code reference, a DiceRef, can only be created when
475 // the OwningObject ObjectFile is a MachOObjectFile a static_cast<> is used for
476 // the methods that get the values of the fields of the reference.
478 inline std::error_code DiceRef::getOffset(uint32_t &Result) const {
479 const MachOObjectFile *MachOOF =
480 static_cast<const MachOObjectFile *>(OwningObject);
481 MachO::data_in_code_entry Dice = MachOOF->getDice(DicePimpl);
482 Result = Dice.offset;
483 return std::error_code();
486 inline std::error_code DiceRef::getLength(uint16_t &Result) const {
487 const MachOObjectFile *MachOOF =
488 static_cast<const MachOObjectFile *>(OwningObject);
489 MachO::data_in_code_entry Dice = MachOOF->getDice(DicePimpl);
490 Result = Dice.length;
491 return std::error_code();
494 inline std::error_code DiceRef::getKind(uint16_t &Result) const {
495 const MachOObjectFile *MachOOF =
496 static_cast<const MachOObjectFile *>(OwningObject);
497 MachO::data_in_code_entry Dice = MachOOF->getDice(DicePimpl);
499 return std::error_code();
502 inline DataRefImpl DiceRef::getRawDataRefImpl() const {
506 inline const ObjectFile *DiceRef::getObjectFile() const {