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/MachOFormat.h"
22 #include "llvm/Object/ObjectFile.h"
23 #include "llvm/Support/MachO.h"
24 #include "llvm/Support/raw_ostream.h"
29 /// DiceRef - This is a value type class that represents a single
30 /// data in code entry in the table in a Mach-O object file.
32 DataRefImpl DicePimpl;
33 const ObjectFile *OwningObject;
36 DiceRef() : OwningObject(NULL) { }
38 DiceRef(DataRefImpl DiceP, const ObjectFile *Owner);
40 bool operator==(const DiceRef &Other) const;
41 bool operator<(const DiceRef &Other) const;
43 error_code getNext(DiceRef &Result) const;
45 error_code getOffset(uint32_t &Result) const;
46 error_code getLength(uint16_t &Result) const;
47 error_code getKind(uint16_t &Result) const;
49 DataRefImpl getRawDataRefImpl() const;
50 const ObjectFile *getObjectFile() const;
52 typedef content_iterator<DiceRef> dice_iterator;
54 class MachOObjectFile : public ObjectFile {
56 struct LoadCommandInfo {
57 const char *Ptr; // Where in memory the load command is.
58 macho::LoadCommand C; // The command itself.
61 MachOObjectFile(MemoryBuffer *Object, bool IsLittleEndian, bool Is64Bits,
64 virtual error_code getSymbolNext(DataRefImpl Symb, SymbolRef &Res) const;
65 virtual error_code getSymbolName(DataRefImpl Symb, StringRef &Res) const;
66 virtual error_code getSymbolAddress(DataRefImpl Symb, uint64_t &Res) const;
67 virtual error_code getSymbolFileOffset(DataRefImpl Symb, uint64_t &Res) const;
68 virtual error_code getSymbolAlignment(DataRefImpl Symb, uint32_t &Res) const;
69 virtual error_code getSymbolSize(DataRefImpl Symb, uint64_t &Res) const;
70 virtual error_code getSymbolType(DataRefImpl Symb,
71 SymbolRef::Type &Res) const;
72 virtual error_code getSymbolNMTypeChar(DataRefImpl Symb, char &Res) const;
73 virtual error_code getSymbolFlags(DataRefImpl Symb, uint32_t &Res) const;
74 virtual error_code getSymbolSection(DataRefImpl Symb,
75 section_iterator &Res) const;
76 virtual error_code getSymbolValue(DataRefImpl Symb, uint64_t &Val) const;
78 virtual error_code getSectionNext(DataRefImpl Sec, SectionRef &Res) const;
79 virtual error_code getSectionName(DataRefImpl Sec, StringRef &Res) const;
80 virtual error_code getSectionAddress(DataRefImpl Sec, uint64_t &Res) const;
81 virtual error_code getSectionSize(DataRefImpl Sec, uint64_t &Res) const;
82 virtual error_code getSectionContents(DataRefImpl Sec, StringRef &Res) const;
83 virtual error_code getSectionAlignment(DataRefImpl Sec, uint64_t &Res) const;
84 virtual error_code isSectionText(DataRefImpl Sec, bool &Res) const;
85 virtual error_code isSectionData(DataRefImpl Sec, bool &Res) const;
86 virtual error_code isSectionBSS(DataRefImpl Sec, bool &Res) const;
87 virtual error_code isSectionRequiredForExecution(DataRefImpl Sec,
89 virtual error_code isSectionVirtual(DataRefImpl Sec, bool &Res) const;
90 virtual error_code isSectionZeroInit(DataRefImpl Sec, bool &Res) const;
91 virtual error_code isSectionReadOnlyData(DataRefImpl Sec, bool &Res) const;
92 virtual error_code sectionContainsSymbol(DataRefImpl Sec, DataRefImpl Symb,
94 virtual relocation_iterator getSectionRelBegin(DataRefImpl Sec) const;
95 virtual relocation_iterator getSectionRelEnd(DataRefImpl Sec) const;
97 virtual error_code getRelocationNext(DataRefImpl Rel,
98 RelocationRef &Res) const;
99 virtual error_code getRelocationAddress(DataRefImpl Rel, uint64_t &Res) const;
100 virtual error_code getRelocationOffset(DataRefImpl Rel, uint64_t &Res) const;
101 virtual symbol_iterator getRelocationSymbol(DataRefImpl Rel) const;
102 virtual error_code getRelocationType(DataRefImpl Rel, uint64_t &Res) const;
103 virtual error_code getRelocationTypeName(DataRefImpl Rel,
104 SmallVectorImpl<char> &Result) const;
105 virtual error_code getRelocationValueString(DataRefImpl Rel,
106 SmallVectorImpl<char> &Result) const;
107 virtual error_code getRelocationHidden(DataRefImpl Rel, bool &Result) const;
109 virtual error_code getLibraryNext(DataRefImpl LibData, LibraryRef &Res) const;
110 virtual error_code getLibraryPath(DataRefImpl LibData, StringRef &Res) const;
112 // TODO: Would be useful to have an iterator based version
113 // of the load command interface too.
115 virtual symbol_iterator begin_symbols() const;
116 virtual symbol_iterator end_symbols() const;
118 virtual symbol_iterator begin_dynamic_symbols() const;
119 virtual symbol_iterator end_dynamic_symbols() const;
121 virtual section_iterator begin_sections() const;
122 virtual section_iterator end_sections() const;
124 virtual library_iterator begin_libraries_needed() const;
125 virtual library_iterator end_libraries_needed() const;
127 virtual uint8_t getBytesInAddress() const;
129 virtual StringRef getFileFormatName() const;
130 virtual unsigned getArch() const;
132 virtual StringRef getLoadName() const;
134 relocation_iterator getSectionRelBegin(unsigned Index) const;
135 relocation_iterator getSectionRelEnd(unsigned Index) const;
137 dice_iterator begin_dices() const;
138 dice_iterator end_dices() const;
140 // In a MachO file, sections have a segment name. This is used in the .o
141 // files. They have a single segment, but this field specifies which segment
142 // a section should be put in in the final object.
143 StringRef getSectionFinalSegmentName(DataRefImpl Sec) const;
145 // Names are stored as 16 bytes. These returns the raw 16 bytes without
146 // interpreting them as a C string.
147 ArrayRef<char> getSectionRawName(DataRefImpl Sec) const;
148 ArrayRef<char> getSectionRawFinalSegmentName(DataRefImpl Sec) const;
150 // MachO specific Info about relocations.
151 bool isRelocationScattered(const macho::RelocationEntry &RE) const;
152 unsigned getPlainRelocationSymbolNum(const macho::RelocationEntry &RE) const;
153 bool getPlainRelocationExternal(const macho::RelocationEntry &RE) const;
154 bool getScatteredRelocationScattered(const macho::RelocationEntry &RE) const;
155 uint32_t getScatteredRelocationValue(const macho::RelocationEntry &RE) const;
156 unsigned getAnyRelocationAddress(const macho::RelocationEntry &RE) const;
157 unsigned getAnyRelocationPCRel(const macho::RelocationEntry &RE) const;
158 unsigned getAnyRelocationLength(const macho::RelocationEntry &RE) const;
159 unsigned getAnyRelocationType(const macho::RelocationEntry &RE) const;
160 SectionRef getRelocationSection(const macho::RelocationEntry &RE) const;
162 // Walk load commands.
163 LoadCommandInfo getFirstLoadCommandInfo() const;
164 LoadCommandInfo getNextLoadCommandInfo(const LoadCommandInfo &L) const;
166 // MachO specific structures.
167 macho::Section getSection(DataRefImpl DRI) const;
168 macho::Section64 getSection64(DataRefImpl DRI) const;
169 macho::Section getSection(const LoadCommandInfo &L, unsigned Index) const;
170 macho::Section64 getSection64(const LoadCommandInfo &L, unsigned Index) const;
171 macho::SymbolTableEntry getSymbolTableEntry(DataRefImpl DRI) const;
172 macho::Symbol64TableEntry getSymbol64TableEntry(DataRefImpl DRI) const;
174 macho::LinkeditDataLoadCommand
175 getLinkeditDataLoadCommand(const LoadCommandInfo &L) const;
176 macho::SegmentLoadCommand
177 getSegmentLoadCommand(const LoadCommandInfo &L) const;
178 macho::Segment64LoadCommand
179 getSegment64LoadCommand(const LoadCommandInfo &L) const;
180 macho::LinkerOptionsLoadCommand
181 getLinkerOptionsLoadCommand(const LoadCommandInfo &L) const;
183 macho::RelocationEntry getRelocation(DataRefImpl Rel) const;
184 macho::DataInCodeTableEntry getDice(DataRefImpl Rel) const;
185 macho::Header getHeader() const;
186 macho::Header64Ext getHeader64Ext() const;
187 macho::IndirectSymbolTableEntry
188 getIndirectSymbolTableEntry(const macho::DysymtabLoadCommand &DLC,
189 unsigned Index) const;
190 macho::DataInCodeTableEntry getDataInCodeTableEntry(uint32_t DataOffset,
191 unsigned Index) const;
192 macho::SymtabLoadCommand getSymtabLoadCommand() const;
193 macho::DysymtabLoadCommand getDysymtabLoadCommand() const;
194 macho::LinkeditDataLoadCommand getDataInCodeLoadCommand() const;
196 StringRef getStringTableData() const;
197 bool is64Bit() const;
198 void ReadULEB128s(uint64_t Index, SmallVectorImpl<uint64_t> &Out) const;
200 static Triple::ArchType getArch(uint32_t CPUType);
202 static bool classof(const Binary *v) {
207 typedef SmallVector<const char*, 1> SectionList;
208 SectionList Sections;
209 const char *SymtabLoadCmd;
210 const char *DysymtabLoadCmd;
211 const char *DataInCodeLoadCmd;
215 inline DiceRef::DiceRef(DataRefImpl DiceP, const ObjectFile *Owner)
216 : DicePimpl(DiceP) , OwningObject(Owner) {}
218 inline bool DiceRef::operator==(const DiceRef &Other) const {
219 return DicePimpl == Other.DicePimpl;
222 inline bool DiceRef::operator<(const DiceRef &Other) const {
223 return DicePimpl < Other.DicePimpl;
226 inline error_code DiceRef::getNext(DiceRef &Result) const {
227 DataRefImpl Rel = DicePimpl;
228 const macho::DataInCodeTableEntry *P =
229 reinterpret_cast<const macho::DataInCodeTableEntry *>(Rel.p);
230 Rel.p = reinterpret_cast<uintptr_t>(P + 1);
231 Result = DiceRef(Rel, OwningObject);
232 return object_error::success;
235 // Since a Mach-O data in code reference, a DiceRef, can only be created when
236 // the OwningObject ObjectFile is a MachOObjectFile a static_cast<> is used for
237 // the methods that get the values of the fields of the reference.
239 inline error_code DiceRef::getOffset(uint32_t &Result) const {
240 const MachOObjectFile *MachOOF =
241 static_cast<const MachOObjectFile *>(OwningObject);
242 macho::DataInCodeTableEntry Dice = MachOOF->getDice(DicePimpl);
243 Result = Dice.Offset;
244 return object_error::success;
247 inline error_code DiceRef::getLength(uint16_t &Result) const {
248 const MachOObjectFile *MachOOF =
249 static_cast<const MachOObjectFile *>(OwningObject);
250 macho::DataInCodeTableEntry Dice = MachOOF->getDice(DicePimpl);
251 Result = Dice.Length;
252 return object_error::success;
255 inline error_code DiceRef::getKind(uint16_t &Result) const {
256 const MachOObjectFile *MachOOF =
257 static_cast<const MachOObjectFile *>(OwningObject);
258 macho::DataInCodeTableEntry Dice = MachOOF->getDice(DicePimpl);
260 return object_error::success;
263 inline DataRefImpl DiceRef::getRawDataRefImpl() const {
267 inline const ObjectFile *DiceRef::getObjectFile() const {