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 binds the MachOObject
11 // class to the generic ObjectFile wrapper.
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/MachOObject.h"
21 #include "llvm/Object/ObjectFile.h"
22 #include "llvm/Support/Endian.h"
23 #include "llvm/Support/MachO.h"
24 #include "llvm/Support/raw_ostream.h"
29 namespace MachOFormat {
35 template<bool is64Bits>
39 struct Section<false> {
42 support::ulittle32_t Address;
43 support::ulittle32_t Size;
44 support::ulittle32_t Offset;
45 support::ulittle32_t Align;
46 support::ulittle32_t RelocationTableOffset;
47 support::ulittle32_t NumRelocationTableEntries;
48 support::ulittle32_t Flags;
49 support::ulittle32_t Reserved1;
50 support::ulittle32_t Reserved2;
54 struct Section<true> {
57 support::ulittle64_t Address;
58 support::ulittle64_t Size;
59 support::ulittle32_t Offset;
60 support::ulittle32_t Align;
61 support::ulittle32_t RelocationTableOffset;
62 support::ulittle32_t NumRelocationTableEntries;
63 support::ulittle32_t Flags;
64 support::ulittle32_t Reserved1;
65 support::ulittle32_t Reserved2;
66 support::ulittle32_t Reserved3;
69 struct RelocationEntry {
70 support::ulittle32_t Word0;
71 support::ulittle32_t Word1;
74 struct SymbolTableEntryBase {
75 support::ulittle32_t StringIndex;
78 support::ulittle16_t Flags;
81 template<bool is64Bits>
82 struct SymbolTableEntry;
85 struct SymbolTableEntry<false> {
86 support::ulittle32_t StringIndex;
89 support::ulittle16_t Flags;
90 support::ulittle32_t Value;
94 struct SymbolTableEntry<true> {
95 support::ulittle32_t StringIndex;
98 support::ulittle16_t Flags;
99 support::ulittle64_t Value;
103 support::ulittle32_t Type;
104 support::ulittle32_t Size;
107 struct SymtabLoadCommand {
108 support::ulittle32_t Type;
109 support::ulittle32_t Size;
110 support::ulittle32_t SymbolTableOffset;
111 support::ulittle32_t NumSymbolTableEntries;
112 support::ulittle32_t StringTableOffset;
113 support::ulittle32_t StringTableSize;
116 template<bool is64Bits>
117 struct SegmentLoadCommand;
120 struct SegmentLoadCommand<false> {
121 support::ulittle32_t Type;
122 support::ulittle32_t Size;
124 support::ulittle32_t VMAddress;
125 support::ulittle32_t VMSize;
126 support::ulittle32_t FileOffset;
127 support::ulittle32_t FileSize;
128 support::ulittle32_t MaxVMProtection;
129 support::ulittle32_t InitialVMProtection;
130 support::ulittle32_t NumSections;
131 support::ulittle32_t Flags;
135 struct SegmentLoadCommand<true> {
136 support::ulittle32_t Type;
137 support::ulittle32_t Size;
139 support::ulittle64_t VMAddress;
140 support::ulittle64_t VMSize;
141 support::ulittle64_t FileOffset;
142 support::ulittle64_t FileSize;
143 support::ulittle32_t MaxVMProtection;
144 support::ulittle32_t InitialVMProtection;
145 support::ulittle32_t NumSections;
146 support::ulittle32_t Flags;
149 struct LinkeditDataLoadCommand {
150 support::ulittle32_t Type;
151 support::ulittle32_t Size;
152 support::ulittle32_t DataOffset;
153 support::ulittle32_t DataSize;
157 support::ulittle32_t Magic;
158 support::ulittle32_t CPUType;
159 support::ulittle32_t CPUSubtype;
160 support::ulittle32_t FileType;
161 support::ulittle32_t NumLoadCommands;
162 support::ulittle32_t SizeOfLoadCommands;
163 support::ulittle32_t Flags;
167 class MachOObjectFile : public ObjectFile {
169 MachOObjectFile(MemoryBuffer *Object, bool Is64bits, error_code &ec);
171 virtual symbol_iterator begin_symbols() const;
172 virtual symbol_iterator end_symbols() const;
173 virtual symbol_iterator begin_dynamic_symbols() const;
174 virtual symbol_iterator end_dynamic_symbols() const;
175 virtual library_iterator begin_libraries_needed() const;
176 virtual library_iterator end_libraries_needed() const;
177 virtual section_iterator begin_sections() const;
178 virtual section_iterator end_sections() const;
180 virtual uint8_t getBytesInAddress() const;
181 virtual StringRef getFileFormatName() const;
182 virtual unsigned getArch() const;
183 virtual StringRef getLoadName() const;
185 // In a MachO file, sections have a segment name. This is used in the .o
186 // files. They have a single segment, but this field specifies which segment
187 // a section should be put in in the final object.
188 StringRef getSectionFinalSegmentName(DataRefImpl Sec) const;
190 // Names are stored as 16 bytes. These returns the raw 16 bytes without
191 // interpreting them as a C string.
192 ArrayRef<char> getSectionRawName(DataRefImpl Sec) const;
193 ArrayRef<char>getSectionRawFinalSegmentName(DataRefImpl Sec) const;
195 const MachOFormat::Section<true> *getSection64(DataRefImpl DRI) const;
196 const MachOFormat::Section<false> *getSection(DataRefImpl DRI) const;
197 const MachOFormat::SymbolTableEntry<true> *
198 getSymbol64TableEntry(DataRefImpl DRI) const;
199 const MachOFormat::SymbolTableEntry<false> *
200 getSymbolTableEntry(DataRefImpl DRI) const;
201 bool is64Bit() const;
202 const MachOFormat::LoadCommand *getLoadCommandInfo(unsigned Index) const;
203 void ReadULEB128s(uint64_t Index, SmallVectorImpl<uint64_t> &Out) const;
204 const MachOFormat::Header *getHeader() const;
205 unsigned getHeaderSize() const;
206 StringRef getData(size_t Offset, size_t Size) const;
208 static inline bool classof(const Binary *v) {
213 virtual error_code getSymbolNext(DataRefImpl Symb, SymbolRef &Res) const;
214 virtual error_code getSymbolName(DataRefImpl Symb, StringRef &Res) const;
215 virtual error_code getSymbolFileOffset(DataRefImpl Symb, uint64_t &Res) const;
216 virtual error_code getSymbolAddress(DataRefImpl Symb, uint64_t &Res) const;
217 virtual error_code getSymbolSize(DataRefImpl Symb, uint64_t &Res) const;
218 virtual error_code getSymbolNMTypeChar(DataRefImpl Symb, char &Res) const;
219 virtual error_code getSymbolFlags(DataRefImpl Symb, uint32_t &Res) const;
220 virtual error_code getSymbolType(DataRefImpl Symb, SymbolRef::Type &Res) const;
221 virtual error_code getSymbolSection(DataRefImpl Symb,
222 section_iterator &Res) const;
223 virtual error_code getSymbolValue(DataRefImpl Symb, uint64_t &Val) const;
225 virtual error_code getSectionNext(DataRefImpl Sec, SectionRef &Res) const;
226 virtual error_code getSectionName(DataRefImpl Sec, StringRef &Res) const;
227 virtual error_code getSectionAddress(DataRefImpl Sec, uint64_t &Res) const;
228 virtual error_code getSectionSize(DataRefImpl Sec, uint64_t &Res) const;
229 virtual error_code getSectionContents(DataRefImpl Sec, StringRef &Res) const;
230 virtual error_code getSectionAlignment(DataRefImpl Sec, uint64_t &Res) const;
231 virtual error_code isSectionText(DataRefImpl Sec, bool &Res) const;
232 virtual error_code isSectionData(DataRefImpl Sec, bool &Res) const;
233 virtual error_code isSectionBSS(DataRefImpl Sec, bool &Res) const;
234 virtual error_code isSectionRequiredForExecution(DataRefImpl Sec,
236 virtual error_code isSectionVirtual(DataRefImpl Sec, bool &Res) const;
237 virtual error_code isSectionZeroInit(DataRefImpl Sec, bool &Res) const;
238 virtual error_code isSectionReadOnlyData(DataRefImpl Sec, bool &Res) const;
239 virtual error_code sectionContainsSymbol(DataRefImpl DRI, DataRefImpl S,
241 virtual relocation_iterator getSectionRelBegin(DataRefImpl Sec) const;
242 virtual relocation_iterator getSectionRelEnd(DataRefImpl Sec) const;
244 virtual error_code getRelocationNext(DataRefImpl Rel,
245 RelocationRef &Res) const;
246 virtual error_code getRelocationAddress(DataRefImpl Rel,
247 uint64_t &Res) const;
248 virtual error_code getRelocationOffset(DataRefImpl Rel,
249 uint64_t &Res) const;
250 virtual error_code getRelocationSymbol(DataRefImpl Rel,
251 SymbolRef &Res) const;
252 virtual error_code getRelocationType(DataRefImpl Rel,
253 uint64_t &Res) const;
254 virtual error_code getRelocationTypeName(DataRefImpl Rel,
255 SmallVectorImpl<char> &Result) const;
256 virtual error_code getRelocationAdditionalInfo(DataRefImpl Rel,
258 virtual error_code getRelocationValueString(DataRefImpl Rel,
259 SmallVectorImpl<char> &Result) const;
260 virtual error_code getRelocationHidden(DataRefImpl Rel, bool &Result) const;
262 virtual error_code getLibraryNext(DataRefImpl LibData, LibraryRef &Res) const;
263 virtual error_code getLibraryPath(DataRefImpl LibData, StringRef &Res) const;
266 typedef SmallVector<DataRefImpl, 1> SectionList;
267 SectionList Sections;
269 const MachOFormat::SectionBase *getSectionBase(DataRefImpl DRI) const;
271 void moveToNextSection(DataRefImpl &DRI) const;
273 const MachOFormat::SymbolTableEntryBase *
274 getSymbolTableEntryBase(DataRefImpl DRI) const;
276 const MachOFormat::SymbolTableEntryBase *
277 getSymbolTableEntryBase(DataRefImpl DRI,
278 const MachOFormat::SymtabLoadCommand *SymtabLoadCmd) const;
280 void moveToNextSymbol(DataRefImpl &DRI) const;
281 const MachOFormat::RelocationEntry *getRelocation(DataRefImpl Rel) const;
282 std::size_t getSectionIndex(DataRefImpl Sec) const;
284 void printRelocationTargetName(const MachOFormat::RelocationEntry *RE,
285 raw_string_ostream &fmt) const;