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/Object/MachOFormat.h"
21 #include "llvm/Object/ObjectFile.h"
22 #include "llvm/Support/MachO.h"
23 #include "llvm/Support/raw_ostream.h"
28 class MachOObjectFile : public ObjectFile {
30 struct LoadCommandInfo {
31 const char *Ptr; // Where in memory the load command is.
32 macho::LoadCommand C; // The command itself.
35 MachOObjectFile(MemoryBuffer *Object, bool IsLittleEndian, bool Is64Bits,
38 virtual error_code getSymbolNext(DataRefImpl Symb, SymbolRef &Res) const;
39 virtual error_code getSymbolName(DataRefImpl Symb, StringRef &Res) const;
40 virtual error_code getSymbolAddress(DataRefImpl Symb, uint64_t &Res) const;
41 virtual error_code getSymbolFileOffset(DataRefImpl Symb, uint64_t &Res) const;
42 virtual error_code getSymbolSize(DataRefImpl Symb, uint64_t &Res) const;
43 virtual error_code getSymbolType(DataRefImpl Symb,
44 SymbolRef::Type &Res) const;
45 virtual error_code getSymbolNMTypeChar(DataRefImpl Symb, char &Res) const;
46 virtual error_code getSymbolFlags(DataRefImpl Symb, uint32_t &Res) const;
47 virtual error_code getSymbolSection(DataRefImpl Symb,
48 section_iterator &Res) const;
49 virtual error_code getSymbolValue(DataRefImpl Symb, uint64_t &Val) const;
51 virtual error_code getSectionNext(DataRefImpl Sec, SectionRef &Res) const;
52 virtual error_code getSectionName(DataRefImpl Sec, StringRef &Res) const;
53 virtual error_code getSectionAddress(DataRefImpl Sec, uint64_t &Res) const;
54 virtual error_code getSectionSize(DataRefImpl Sec, uint64_t &Res) const;
55 virtual error_code getSectionContents(DataRefImpl Sec, StringRef &Res) const;
56 virtual error_code getSectionAlignment(DataRefImpl Sec, uint64_t &Res) const;
57 virtual error_code isSectionText(DataRefImpl Sec, bool &Res) const;
58 virtual error_code isSectionData(DataRefImpl Sec, bool &Res) const;
59 virtual error_code isSectionBSS(DataRefImpl Sec, bool &Res) const;
60 virtual error_code isSectionRequiredForExecution(DataRefImpl Sec,
62 virtual error_code isSectionVirtual(DataRefImpl Sec, bool &Res) const;
63 virtual error_code isSectionZeroInit(DataRefImpl Sec, bool &Res) const;
64 virtual error_code isSectionReadOnlyData(DataRefImpl Sec, bool &Res) const;
65 virtual error_code sectionContainsSymbol(DataRefImpl Sec, DataRefImpl Symb,
67 virtual relocation_iterator getSectionRelBegin(DataRefImpl Sec) const;
68 virtual relocation_iterator getSectionRelEnd(DataRefImpl Sec) const;
70 virtual error_code getRelocationNext(DataRefImpl Rel,
71 RelocationRef &Res) const;
72 virtual error_code getRelocationAddress(DataRefImpl Rel, uint64_t &Res) const;
73 virtual error_code getRelocationOffset(DataRefImpl Rel, uint64_t &Res) const;
74 virtual error_code getRelocationSymbol(DataRefImpl Rel, SymbolRef &Res) const;
75 virtual error_code getRelocationType(DataRefImpl Rel, uint64_t &Res) const;
76 virtual error_code getRelocationTypeName(DataRefImpl Rel,
77 SmallVectorImpl<char> &Result) const;
78 virtual error_code getRelocationAdditionalInfo(DataRefImpl Rel,
80 virtual error_code getRelocationValueString(DataRefImpl Rel,
81 SmallVectorImpl<char> &Result) const;
82 virtual error_code getRelocationHidden(DataRefImpl Rel, bool &Result) const;
84 virtual error_code getLibraryNext(DataRefImpl LibData, LibraryRef &Res) const;
85 virtual error_code getLibraryPath(DataRefImpl LibData, StringRef &Res) const;
87 virtual symbol_iterator begin_symbols() const;
88 virtual symbol_iterator end_symbols() const;
90 virtual symbol_iterator begin_dynamic_symbols() const;
91 virtual symbol_iterator end_dynamic_symbols() const;
93 virtual section_iterator begin_sections() const;
94 virtual section_iterator end_sections() const;
96 virtual library_iterator begin_libraries_needed() const;
97 virtual library_iterator end_libraries_needed() const;
99 virtual uint8_t getBytesInAddress() const;
101 virtual StringRef getFileFormatName() const;
102 virtual unsigned getArch() const;
104 virtual StringRef getLoadName() const;
106 // In a MachO file, sections have a segment name. This is used in the .o
107 // files. They have a single segment, but this field specifies which segment
108 // a section should be put in in the final object.
109 StringRef getSectionFinalSegmentName(DataRefImpl Sec) const;
111 // Names are stored as 16 bytes. These returns the raw 16 bytes without
112 // interpreting them as a C string.
113 ArrayRef<char> getSectionRawName(DataRefImpl Sec) const;
114 ArrayRef<char> getSectionRawFinalSegmentName(DataRefImpl Sec) const;
116 // MachO specific Info about relocations.
117 bool isRelocationScattered(const macho::RelocationEntry &RE) const;
118 unsigned getPlainRelocationSymbolNum(const macho::RelocationEntry &RE) const;
119 bool getPlainRelocationExternal(const macho::RelocationEntry &RE) const;
120 bool getScatteredRelocationScattered(const macho::RelocationEntry &RE) const;
121 uint32_t getScatteredRelocationValue(const macho::RelocationEntry &RE) const;
122 unsigned getAnyRelocationAddress(const macho::RelocationEntry &RE) const;
123 unsigned getAnyRelocationPCRel(const macho::RelocationEntry &RE) const;
124 unsigned getAnyRelocationLength(const macho::RelocationEntry &RE) const;
125 unsigned getAnyRelocationType(const macho::RelocationEntry &RE) const;
127 // Walk load commands.
128 LoadCommandInfo getFirstLoadCommandInfo() const;
129 LoadCommandInfo getNextLoadCommandInfo(const LoadCommandInfo &L) const;
131 // MachO specific structures.
132 macho::Section getSection(DataRefImpl DRI) const;
133 macho::Section64 getSection64(DataRefImpl DRI) const;
134 macho::SymbolTableEntry getSymbolTableEntry(DataRefImpl DRI) const;
135 macho::Symbol64TableEntry getSymbol64TableEntry(DataRefImpl DRI) const;
136 macho::LinkeditDataLoadCommand
137 getLinkeditDataLoadCommand(const LoadCommandInfo &L) const;
138 macho::RelocationEntry getRelocation(DataRefImpl Rel) const;
139 macho::Header getHeader() const;
140 macho::SymtabLoadCommand getSymtabLoadCommand() const;
142 bool is64Bit() const;
143 void ReadULEB128s(uint64_t Index, SmallVectorImpl<uint64_t> &Out) const;
145 static bool classof(const Binary *v) {
150 typedef SmallVector<const char*, 1> SectionList;
151 SectionList Sections;
152 const char *SymtabLoadCmd;