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 {
33 support::ulittle32_t Address;
34 support::ulittle32_t Size;
35 support::ulittle32_t Offset;
36 support::ulittle32_t Align;
37 support::ulittle32_t RelocationTableOffset;
38 support::ulittle32_t NumRelocationTableEntries;
39 support::ulittle32_t Flags;
40 support::ulittle32_t Reserved1;
41 support::ulittle32_t Reserved2;
47 support::ulittle64_t Address;
48 support::ulittle64_t Size;
49 support::ulittle32_t Offset;
50 support::ulittle32_t Align;
51 support::ulittle32_t RelocationTableOffset;
52 support::ulittle32_t NumRelocationTableEntries;
53 support::ulittle32_t Flags;
54 support::ulittle32_t Reserved1;
55 support::ulittle32_t Reserved2;
56 support::ulittle32_t Reserved3;
59 struct RelocationEntry {
60 support::ulittle32_t Word0;
61 support::ulittle32_t Word1;
64 struct SymbolTableEntry {
65 support::ulittle32_t StringIndex;
68 support::ulittle16_t Flags;
69 support::ulittle32_t Value;
72 struct Symbol64TableEntry {
73 support::ulittle32_t StringIndex;
76 support::ulittle16_t Flags;
77 support::ulittle64_t Value;
80 struct SymtabLoadCommand {
81 support::ulittle32_t Type;
82 support::ulittle32_t Size;
83 support::ulittle32_t SymbolTableOffset;
84 support::ulittle32_t NumSymbolTableEntries;
85 support::ulittle32_t StringTableOffset;
86 support::ulittle32_t StringTableSize;
89 struct SegmentLoadCommand {
90 support::ulittle32_t Type;
91 support::ulittle32_t Size;
93 support::ulittle32_t VMAddress;
94 support::ulittle32_t VMSize;
95 support::ulittle32_t FileOffset;
96 support::ulittle32_t FileSize;
97 support::ulittle32_t MaxVMProtection;
98 support::ulittle32_t InitialVMProtection;
99 support::ulittle32_t NumSections;
100 support::ulittle32_t Flags;
103 struct Segment64LoadCommand {
104 support::ulittle32_t Type;
105 support::ulittle32_t Size;
107 support::ulittle64_t VMAddress;
108 support::ulittle64_t VMSize;
109 support::ulittle64_t FileOffset;
110 support::ulittle64_t FileSize;
111 support::ulittle32_t MaxVMProtection;
112 support::ulittle32_t InitialVMProtection;
113 support::ulittle32_t NumSections;
114 support::ulittle32_t Flags;
117 struct LinkeditDataLoadCommand {
118 support::ulittle32_t Type;
119 support::ulittle32_t Size;
120 support::ulittle32_t DataOffset;
121 support::ulittle32_t DataSize;
125 typedef MachOObject::LoadCommandInfo LoadCommandInfo;
127 class MachOObjectFile : public ObjectFile {
129 MachOObjectFile(MemoryBuffer *Object, MachOObject *MOO, error_code &ec);
131 virtual symbol_iterator begin_symbols() const;
132 virtual symbol_iterator end_symbols() const;
133 virtual symbol_iterator begin_dynamic_symbols() const;
134 virtual symbol_iterator end_dynamic_symbols() const;
135 virtual library_iterator begin_libraries_needed() const;
136 virtual library_iterator end_libraries_needed() const;
137 virtual section_iterator begin_sections() const;
138 virtual section_iterator end_sections() const;
140 virtual uint8_t getBytesInAddress() const;
141 virtual StringRef getFileFormatName() const;
142 virtual unsigned getArch() const;
143 virtual StringRef getLoadName() const;
145 // In a MachO file, sections have a segment name. This is used in the .o
146 // files. They have a single segment, but this field specifies which segment
147 // a section should be put in in the final object.
148 StringRef getSectionFinalSegmentName(DataRefImpl Sec) const;
150 // Names are stored as 16 bytes. These returns the raw 16 bytes without
151 // interpreting them as a C string.
152 ArrayRef<char> getSectionRawName(DataRefImpl Sec) const;
153 ArrayRef<char>getSectionRawFinalSegmentName(DataRefImpl Sec) const;
155 const MachOFormat::LinkeditDataLoadCommand *
156 getLinkeditDataLoadCommand(LoadCommandInfo LCI) const;
157 const MachOFormat::Section64 *getSection64(DataRefImpl DRI) const;
158 const MachOFormat::Section *getSection(DataRefImpl DRI) const;
159 const MachOFormat::Symbol64TableEntry *
160 getSymbol64TableEntry(DataRefImpl DRI) const;
161 const MachOFormat::SymbolTableEntry *
162 getSymbolTableEntry(DataRefImpl DRI) const;
163 bool is64Bit() const;
165 const MachOObject *getObject() const { return MachOObj.get(); }
167 static inline bool classof(const Binary *v) {
172 virtual error_code getSymbolNext(DataRefImpl Symb, SymbolRef &Res) const;
173 virtual error_code getSymbolName(DataRefImpl Symb, StringRef &Res) const;
174 virtual error_code getSymbolFileOffset(DataRefImpl Symb, uint64_t &Res) const;
175 virtual error_code getSymbolAddress(DataRefImpl Symb, uint64_t &Res) const;
176 virtual error_code getSymbolSize(DataRefImpl Symb, uint64_t &Res) const;
177 virtual error_code getSymbolNMTypeChar(DataRefImpl Symb, char &Res) const;
178 virtual error_code getSymbolFlags(DataRefImpl Symb, uint32_t &Res) const;
179 virtual error_code getSymbolType(DataRefImpl Symb, SymbolRef::Type &Res) const;
180 virtual error_code getSymbolSection(DataRefImpl Symb,
181 section_iterator &Res) const;
182 virtual error_code getSymbolValue(DataRefImpl Symb, uint64_t &Val) const;
184 virtual error_code getSectionNext(DataRefImpl Sec, SectionRef &Res) const;
185 virtual error_code getSectionName(DataRefImpl Sec, StringRef &Res) const;
186 virtual error_code getSectionAddress(DataRefImpl Sec, uint64_t &Res) const;
187 virtual error_code getSectionSize(DataRefImpl Sec, uint64_t &Res) const;
188 virtual error_code getSectionContents(DataRefImpl Sec, StringRef &Res) const;
189 virtual error_code getSectionAlignment(DataRefImpl Sec, uint64_t &Res) const;
190 virtual error_code isSectionText(DataRefImpl Sec, bool &Res) const;
191 virtual error_code isSectionData(DataRefImpl Sec, bool &Res) const;
192 virtual error_code isSectionBSS(DataRefImpl Sec, bool &Res) const;
193 virtual error_code isSectionRequiredForExecution(DataRefImpl Sec,
195 virtual error_code isSectionVirtual(DataRefImpl Sec, bool &Res) const;
196 virtual error_code isSectionZeroInit(DataRefImpl Sec, bool &Res) const;
197 virtual error_code isSectionReadOnlyData(DataRefImpl Sec, bool &Res) const;
198 virtual error_code sectionContainsSymbol(DataRefImpl DRI, DataRefImpl S,
200 virtual relocation_iterator getSectionRelBegin(DataRefImpl Sec) const;
201 virtual relocation_iterator getSectionRelEnd(DataRefImpl Sec) const;
203 virtual error_code getRelocationNext(DataRefImpl Rel,
204 RelocationRef &Res) const;
205 virtual error_code getRelocationAddress(DataRefImpl Rel,
206 uint64_t &Res) const;
207 virtual error_code getRelocationOffset(DataRefImpl Rel,
208 uint64_t &Res) const;
209 virtual error_code getRelocationSymbol(DataRefImpl Rel,
210 SymbolRef &Res) const;
211 virtual error_code getRelocationType(DataRefImpl Rel,
212 uint64_t &Res) const;
213 virtual error_code getRelocationTypeName(DataRefImpl Rel,
214 SmallVectorImpl<char> &Result) const;
215 virtual error_code getRelocationAdditionalInfo(DataRefImpl Rel,
217 virtual error_code getRelocationValueString(DataRefImpl Rel,
218 SmallVectorImpl<char> &Result) const;
219 virtual error_code getRelocationHidden(DataRefImpl Rel, bool &Result) const;
221 virtual error_code getLibraryNext(DataRefImpl LibData, LibraryRef &Res) const;
222 virtual error_code getLibraryPath(DataRefImpl LibData, StringRef &Res) const;
225 OwningPtr<MachOObject> MachOObj;
226 typedef SmallVector<DataRefImpl, 1> SectionList;
227 SectionList Sections;
230 void moveToNextSection(DataRefImpl &DRI) const;
232 const MachOFormat::SymbolTableEntry *
233 getSymbolTableEntry(DataRefImpl DRI,
234 const MachOFormat::SymtabLoadCommand *SymtabLoadCmd) const;
236 const MachOFormat::Symbol64TableEntry *
237 getSymbol64TableEntry(DataRefImpl DRI,
238 const MachOFormat::SymtabLoadCommand *SymtabLoadCmd) const;
240 void moveToNextSymbol(DataRefImpl &DRI) const;
241 const MachOFormat::RelocationEntry *getRelocation(DataRefImpl Rel) const;
242 const MachOFormat::SymtabLoadCommand *
243 getSymtabLoadCommand(LoadCommandInfo LCI) const;
244 const MachOFormat::SegmentLoadCommand *
245 getSegmentLoadCommand(LoadCommandInfo LCI) const;
246 const MachOFormat::Segment64LoadCommand *
247 getSegment64LoadCommand(LoadCommandInfo LCI) const;
248 std::size_t getSectionIndex(DataRefImpl Sec) const;
250 void printRelocationTargetName(const MachOFormat::RelocationEntry *RE,
251 raw_string_ostream &fmt) const;