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(NULL) { }
36 DiceRef(DataRefImpl DiceP, const ObjectFile *Owner);
38 bool operator==(const DiceRef &Other) const;
39 bool operator<(const DiceRef &Other) const;
43 error_code getOffset(uint32_t &Result) const;
44 error_code getLength(uint16_t &Result) const;
45 error_code getKind(uint16_t &Result) const;
47 DataRefImpl getRawDataRefImpl() const;
48 const ObjectFile *getObjectFile() const;
50 typedef content_iterator<DiceRef> dice_iterator;
52 class MachOObjectFile : public ObjectFile {
54 struct LoadCommandInfo {
55 const char *Ptr; // Where in memory the load command is.
56 MachO::load_command C; // The command itself.
59 MachOObjectFile(MemoryBuffer *Object, bool IsLittleEndian, bool Is64Bits,
60 error_code &EC, bool BufferOwned = true);
62 void moveSymbolNext(DataRefImpl &Symb) const override;
63 error_code getSymbolName(DataRefImpl Symb, StringRef &Res) const override;
64 error_code getSymbolAddress(DataRefImpl Symb, uint64_t &Res) const override;
65 error_code getSymbolFileOffset(DataRefImpl Symb,
66 uint64_t &Res) const override;
67 error_code getSymbolAlignment(DataRefImpl Symb, uint32_t &Res) const override;
68 error_code getSymbolSize(DataRefImpl Symb, uint64_t &Res) const override;
69 error_code getSymbolType(DataRefImpl Symb,
70 SymbolRef::Type &Res) const override;
71 uint32_t getSymbolFlags(DataRefImpl Symb) const override;
72 error_code getSymbolSection(DataRefImpl Symb,
73 section_iterator &Res) const override;
74 error_code getSymbolValue(DataRefImpl Symb, uint64_t &Val) const override;
76 void moveSectionNext(DataRefImpl &Sec) const override;
77 error_code getSectionName(DataRefImpl Sec, StringRef &Res) const override;
78 error_code getSectionAddress(DataRefImpl Sec, uint64_t &Res) const override;
79 error_code getSectionSize(DataRefImpl Sec, uint64_t &Res) const override;
80 error_code getSectionContents(DataRefImpl Sec, StringRef &Res) const override;
81 error_code getSectionAlignment(DataRefImpl Sec, uint64_t &Res) const override;
82 error_code isSectionText(DataRefImpl Sec, bool &Res) const override;
83 error_code isSectionData(DataRefImpl Sec, bool &Res) const override;
84 error_code isSectionBSS(DataRefImpl Sec, bool &Res) const override;
85 error_code isSectionRequiredForExecution(DataRefImpl Sec,
86 bool &Res) const override;
87 error_code isSectionVirtual(DataRefImpl Sec, bool &Res) const override;
88 error_code isSectionZeroInit(DataRefImpl Sec, bool &Res) const override;
89 error_code isSectionReadOnlyData(DataRefImpl Sec, bool &Res) const override;
90 error_code sectionContainsSymbol(DataRefImpl Sec, DataRefImpl Symb,
91 bool &Result) const override;
92 relocation_iterator section_rel_begin(DataRefImpl Sec) const override;
93 relocation_iterator section_rel_end(DataRefImpl Sec) const override;
95 void moveRelocationNext(DataRefImpl &Rel) const override;
96 error_code getRelocationAddress(DataRefImpl Rel,
97 uint64_t &Res) const override;
98 error_code getRelocationOffset(DataRefImpl Rel, uint64_t &Res) const override;
99 symbol_iterator getRelocationSymbol(DataRefImpl Rel) const override;
100 error_code getRelocationType(DataRefImpl Rel, uint64_t &Res) const override;
101 error_code getRelocationTypeName(DataRefImpl Rel,
102 SmallVectorImpl<char> &Result) const override;
103 error_code getRelocationValueString(DataRefImpl Rel,
104 SmallVectorImpl<char> &Result) const override;
105 error_code getRelocationHidden(DataRefImpl Rel, bool &Result) const override;
107 error_code getLibraryNext(DataRefImpl LibData,
108 LibraryRef &Res) const override;
109 error_code getLibraryPath(DataRefImpl LibData, StringRef &Res) const override;
111 // TODO: Would be useful to have an iterator based version
112 // of the load command interface too.
114 basic_symbol_iterator symbol_begin_impl() const override;
115 basic_symbol_iterator symbol_end_impl() const override;
117 section_iterator section_begin() const override;
118 section_iterator section_end() const override;
120 library_iterator needed_library_begin() const override;
121 library_iterator needed_library_end() const override;
123 uint8_t getBytesInAddress() const override;
125 StringRef getFileFormatName() const override;
126 unsigned getArch() const override;
128 StringRef getLoadName() const override;
130 relocation_iterator section_rel_begin(unsigned Index) const;
131 relocation_iterator section_rel_end(unsigned Index) const;
133 dice_iterator begin_dices() const;
134 dice_iterator end_dices() const;
136 // In a MachO file, sections have a segment name. This is used in the .o
137 // files. They have a single segment, but this field specifies which segment
138 // a section should be put in in the final object.
139 StringRef getSectionFinalSegmentName(DataRefImpl Sec) const;
141 // Names are stored as 16 bytes. These returns the raw 16 bytes without
142 // interpreting them as a C string.
143 ArrayRef<char> getSectionRawName(DataRefImpl Sec) const;
144 ArrayRef<char> getSectionRawFinalSegmentName(DataRefImpl Sec) const;
146 // MachO specific Info about relocations.
147 bool isRelocationScattered(const MachO::any_relocation_info &RE) const;
148 unsigned getPlainRelocationSymbolNum(
149 const MachO::any_relocation_info &RE) const;
150 bool getPlainRelocationExternal(const MachO::any_relocation_info &RE) const;
151 bool getScatteredRelocationScattered(
152 const MachO::any_relocation_info &RE) const;
153 uint32_t getScatteredRelocationValue(
154 const MachO::any_relocation_info &RE) const;
155 unsigned getAnyRelocationAddress(const MachO::any_relocation_info &RE) const;
156 unsigned getAnyRelocationPCRel(const MachO::any_relocation_info &RE) const;
157 unsigned getAnyRelocationLength(const MachO::any_relocation_info &RE) const;
158 unsigned getAnyRelocationType(const MachO::any_relocation_info &RE) const;
159 SectionRef getRelocationSection(const MachO::any_relocation_info &RE) const;
161 // Walk load commands.
162 LoadCommandInfo getFirstLoadCommandInfo() const;
163 LoadCommandInfo getNextLoadCommandInfo(const LoadCommandInfo &L) const;
165 // MachO specific structures.
166 MachO::section getSection(DataRefImpl DRI) const;
167 MachO::section_64 getSection64(DataRefImpl DRI) const;
168 MachO::section getSection(const LoadCommandInfo &L, unsigned Index) const;
169 MachO::section_64 getSection64(const LoadCommandInfo &L,unsigned Index) const;
170 MachO::nlist getSymbolTableEntry(DataRefImpl DRI) const;
171 MachO::nlist_64 getSymbol64TableEntry(DataRefImpl DRI) const;
173 MachO::linkedit_data_command
174 getLinkeditDataLoadCommand(const LoadCommandInfo &L) const;
175 MachO::segment_command
176 getSegmentLoadCommand(const LoadCommandInfo &L) const;
177 MachO::segment_command_64
178 getSegment64LoadCommand(const LoadCommandInfo &L) const;
179 MachO::linker_options_command
180 getLinkerOptionsLoadCommand(const LoadCommandInfo &L) const;
181 MachO::version_min_command
182 getVersionMinLoadCommand(const LoadCommandInfo &L) const;
184 MachO::any_relocation_info getRelocation(DataRefImpl Rel) const;
185 MachO::data_in_code_entry getDice(DataRefImpl Rel) const;
186 MachO::mach_header getHeader() const;
187 MachO::mach_header_64 getHeader64() const;
189 getIndirectSymbolTableEntry(const MachO::dysymtab_command &DLC,
190 unsigned Index) const;
191 MachO::data_in_code_entry getDataInCodeTableEntry(uint32_t DataOffset,
192 unsigned Index) const;
193 MachO::symtab_command getSymtabLoadCommand() const;
194 MachO::dysymtab_command getDysymtabLoadCommand() const;
195 MachO::linkedit_data_command getDataInCodeLoadCommand() const;
197 StringRef getStringTableData() const;
198 bool is64Bit() const;
199 void ReadULEB128s(uint64_t Index, SmallVectorImpl<uint64_t> &Out) const;
201 static Triple::ArchType getArch(uint32_t CPUType);
203 static bool classof(const Binary *v) {
208 typedef SmallVector<const char*, 1> SectionList;
209 SectionList Sections;
210 const char *SymtabLoadCmd;
211 const char *DysymtabLoadCmd;
212 const char *DataInCodeLoadCmd;
216 inline DiceRef::DiceRef(DataRefImpl DiceP, const ObjectFile *Owner)
217 : DicePimpl(DiceP) , OwningObject(Owner) {}
219 inline bool DiceRef::operator==(const DiceRef &Other) const {
220 return DicePimpl == Other.DicePimpl;
223 inline bool DiceRef::operator<(const DiceRef &Other) const {
224 return DicePimpl < Other.DicePimpl;
227 inline void DiceRef::moveNext() {
228 const MachO::data_in_code_entry *P =
229 reinterpret_cast<const MachO::data_in_code_entry *>(DicePimpl.p);
230 DicePimpl.p = reinterpret_cast<uintptr_t>(P + 1);
233 // Since a Mach-O data in code reference, a DiceRef, can only be created when
234 // the OwningObject ObjectFile is a MachOObjectFile a static_cast<> is used for
235 // the methods that get the values of the fields of the reference.
237 inline error_code DiceRef::getOffset(uint32_t &Result) const {
238 const MachOObjectFile *MachOOF =
239 static_cast<const MachOObjectFile *>(OwningObject);
240 MachO::data_in_code_entry Dice = MachOOF->getDice(DicePimpl);
241 Result = Dice.offset;
242 return object_error::success;
245 inline error_code DiceRef::getLength(uint16_t &Result) const {
246 const MachOObjectFile *MachOOF =
247 static_cast<const MachOObjectFile *>(OwningObject);
248 MachO::data_in_code_entry Dice = MachOOF->getDice(DicePimpl);
249 Result = Dice.length;
250 return object_error::success;
253 inline error_code DiceRef::getKind(uint16_t &Result) const {
254 const MachOObjectFile *MachOOF =
255 static_cast<const MachOObjectFile *>(OwningObject);
256 MachO::data_in_code_entry Dice = MachOOF->getDice(DicePimpl);
258 return object_error::success;
261 inline DataRefImpl DiceRef::getRawDataRefImpl() const {
265 inline const ObjectFile *DiceRef::getObjectFile() const {