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(nullptr) { }
36 DiceRef(DataRefImpl DiceP, const ObjectFile *Owner);
38 bool operator==(const DiceRef &Other) const;
39 bool operator<(const DiceRef &Other) const;
43 std::error_code getOffset(uint32_t &Result) const;
44 std::error_code getLength(uint16_t &Result) const;
45 std::error_code getKind(uint16_t &Result) const;
47 DataRefImpl getRawDataRefImpl() const;
48 const ObjectFile *getObjectFile() const;
50 typedef content_iterator<DiceRef> dice_iterator;
52 /// ExportEntry encapsulates the current-state-of-the-walk used when doing a
53 /// non-recursive walk of the trie data structure. This allows you to iterate
54 /// across all exported symbols using:
55 /// for (const llvm::object::ExportEntry &AnExport : Obj->exports()) {
59 ExportEntry(ArrayRef<uint8_t> Trie);
61 StringRef name() const;
62 uint64_t flags() const;
63 uint64_t address() const;
64 uint64_t other() const;
65 StringRef otherName() const;
66 uint32_t nodeOffset() const;
68 bool operator==(const ExportEntry &) const;
73 friend class MachOObjectFile;
76 uint64_t readULEB128(const uint8_t *&p);
77 void pushDownUntilBottom();
78 void pushNode(uint64_t Offset);
80 // Represents a node in the mach-o exports trie.
82 NodeState(const uint8_t *Ptr);
84 const uint8_t *Current;
88 const char *ImportName;
90 unsigned NextChildIndex;
91 unsigned ParentStringLength;
95 ArrayRef<uint8_t> Trie;
96 SmallString<256> CumulativeString;
97 SmallVector<NodeState, 16> Stack;
101 typedef content_iterator<ExportEntry> export_iterator;
103 class MachOObjectFile : public ObjectFile {
105 struct LoadCommandInfo {
106 const char *Ptr; // Where in memory the load command is.
107 MachO::load_command C; // The command itself.
110 MachOObjectFile(MemoryBufferRef Object, bool IsLittleEndian, bool Is64Bits,
111 std::error_code &EC);
113 void moveSymbolNext(DataRefImpl &Symb) const override;
114 std::error_code getSymbolName(DataRefImpl Symb,
115 StringRef &Res) const override;
118 std::error_code getIndirectName(DataRefImpl Symb, StringRef &Res) const;
120 std::error_code getSymbolAddress(DataRefImpl Symb,
121 uint64_t &Res) const override;
122 std::error_code getSymbolAlignment(DataRefImpl Symb,
123 uint32_t &Res) const override;
124 std::error_code getSymbolSize(DataRefImpl Symb, uint64_t &Res) const override;
125 std::error_code getSymbolType(DataRefImpl Symb,
126 SymbolRef::Type &Res) const override;
127 uint32_t getSymbolFlags(DataRefImpl Symb) const override;
128 std::error_code getSymbolSection(DataRefImpl Symb,
129 section_iterator &Res) const override;
131 void moveSectionNext(DataRefImpl &Sec) const override;
132 std::error_code getSectionName(DataRefImpl Sec,
133 StringRef &Res) const override;
134 std::error_code getSectionAddress(DataRefImpl Sec,
135 uint64_t &Res) const override;
136 std::error_code getSectionSize(DataRefImpl Sec, uint64_t &Res) const override;
137 std::error_code getSectionContents(DataRefImpl Sec,
138 StringRef &Res) const override;
139 std::error_code getSectionAlignment(DataRefImpl Sec,
140 uint64_t &Res) const override;
141 std::error_code isSectionText(DataRefImpl Sec, bool &Res) const override;
142 std::error_code isSectionData(DataRefImpl Sec, bool &Res) const override;
143 std::error_code isSectionBSS(DataRefImpl Sec, bool &Res) const override;
144 std::error_code isSectionRequiredForExecution(DataRefImpl Sec,
145 bool &Res) const override;
146 std::error_code isSectionVirtual(DataRefImpl Sec, bool &Res) const override;
147 std::error_code isSectionZeroInit(DataRefImpl Sec, bool &Res) const override;
148 std::error_code isSectionReadOnlyData(DataRefImpl Sec,
149 bool &Res) const override;
150 std::error_code sectionContainsSymbol(DataRefImpl Sec, DataRefImpl Symb,
151 bool &Result) const override;
152 relocation_iterator section_rel_begin(DataRefImpl Sec) const override;
153 relocation_iterator section_rel_end(DataRefImpl Sec) const override;
155 void moveRelocationNext(DataRefImpl &Rel) const override;
156 std::error_code getRelocationAddress(DataRefImpl Rel,
157 uint64_t &Res) const override;
158 std::error_code getRelocationOffset(DataRefImpl Rel,
159 uint64_t &Res) const override;
160 symbol_iterator getRelocationSymbol(DataRefImpl Rel) const override;
161 std::error_code getRelocationType(DataRefImpl Rel,
162 uint64_t &Res) const override;
164 getRelocationTypeName(DataRefImpl Rel,
165 SmallVectorImpl<char> &Result) const override;
167 getRelocationValueString(DataRefImpl Rel,
168 SmallVectorImpl<char> &Result) const override;
169 std::error_code getRelocationHidden(DataRefImpl Rel,
170 bool &Result) const override;
173 std::error_code getLibraryShortNameByIndex(unsigned Index, StringRef &) const;
175 // TODO: Would be useful to have an iterator based version
176 // of the load command interface too.
178 basic_symbol_iterator symbol_begin_impl() const override;
179 basic_symbol_iterator symbol_end_impl() const override;
182 basic_symbol_iterator getSymbolByIndex(unsigned Index) const;
184 section_iterator section_begin() const override;
185 section_iterator section_end() const override;
187 uint8_t getBytesInAddress() const override;
189 StringRef getFileFormatName() const override;
190 unsigned getArch() const override;
191 Triple getArch(const char **McpuDefault, Triple *ThumbTriple) const;
193 relocation_iterator section_rel_begin(unsigned Index) const;
194 relocation_iterator section_rel_end(unsigned Index) const;
196 dice_iterator begin_dices() const;
197 dice_iterator end_dices() const;
199 /// For use iterating over all exported symbols.
200 iterator_range<export_iterator> exports() const;
202 /// For use examining a trie not in a MachOObjectFile.
203 static iterator_range<export_iterator> exports(ArrayRef<uint8_t> Trie);
205 // In a MachO file, sections have a segment name. This is used in the .o
206 // files. They have a single segment, but this field specifies which segment
207 // a section should be put in in the final object.
208 StringRef getSectionFinalSegmentName(DataRefImpl Sec) const;
210 // Names are stored as 16 bytes. These returns the raw 16 bytes without
211 // interpreting them as a C string.
212 ArrayRef<char> getSectionRawName(DataRefImpl Sec) const;
213 ArrayRef<char> getSectionRawFinalSegmentName(DataRefImpl Sec) const;
215 // MachO specific Info about relocations.
216 bool isRelocationScattered(const MachO::any_relocation_info &RE) const;
217 unsigned getPlainRelocationSymbolNum(
218 const MachO::any_relocation_info &RE) const;
219 bool getPlainRelocationExternal(const MachO::any_relocation_info &RE) const;
220 bool getScatteredRelocationScattered(
221 const MachO::any_relocation_info &RE) const;
222 uint32_t getScatteredRelocationValue(
223 const MachO::any_relocation_info &RE) const;
224 unsigned getAnyRelocationAddress(const MachO::any_relocation_info &RE) const;
225 unsigned getAnyRelocationPCRel(const MachO::any_relocation_info &RE) const;
226 unsigned getAnyRelocationLength(const MachO::any_relocation_info &RE) const;
227 unsigned getAnyRelocationType(const MachO::any_relocation_info &RE) const;
228 SectionRef getRelocationSection(const MachO::any_relocation_info &RE) const;
230 // Walk load commands.
231 LoadCommandInfo getFirstLoadCommandInfo() const;
232 LoadCommandInfo getNextLoadCommandInfo(const LoadCommandInfo &L) const;
234 // MachO specific structures.
235 MachO::section getSection(DataRefImpl DRI) const;
236 MachO::section_64 getSection64(DataRefImpl DRI) const;
237 MachO::section getSection(const LoadCommandInfo &L, unsigned Index) const;
238 MachO::section_64 getSection64(const LoadCommandInfo &L,unsigned Index) const;
239 MachO::nlist getSymbolTableEntry(DataRefImpl DRI) const;
240 MachO::nlist_64 getSymbol64TableEntry(DataRefImpl DRI) const;
242 MachO::linkedit_data_command
243 getLinkeditDataLoadCommand(const LoadCommandInfo &L) const;
244 MachO::segment_command
245 getSegmentLoadCommand(const LoadCommandInfo &L) const;
246 MachO::segment_command_64
247 getSegment64LoadCommand(const LoadCommandInfo &L) const;
248 MachO::linker_options_command
249 getLinkerOptionsLoadCommand(const LoadCommandInfo &L) const;
250 MachO::version_min_command
251 getVersionMinLoadCommand(const LoadCommandInfo &L) const;
253 getDylibIDLoadCommand(const LoadCommandInfo &L) const;
255 MachO::any_relocation_info getRelocation(DataRefImpl Rel) const;
256 MachO::data_in_code_entry getDice(DataRefImpl Rel) const;
257 MachO::mach_header getHeader() const;
258 MachO::mach_header_64 getHeader64() const;
260 getIndirectSymbolTableEntry(const MachO::dysymtab_command &DLC,
261 unsigned Index) const;
262 MachO::data_in_code_entry getDataInCodeTableEntry(uint32_t DataOffset,
263 unsigned Index) const;
264 MachO::symtab_command getSymtabLoadCommand() const;
265 MachO::dysymtab_command getDysymtabLoadCommand() const;
266 MachO::linkedit_data_command getDataInCodeLoadCommand() const;
267 ArrayRef<uint8_t> getDyldInfoRebaseOpcodes() const;
268 ArrayRef<uint8_t> getDyldInfoBindOpcodes() const;
269 ArrayRef<uint8_t> getDyldInfoWeakBindOpcodes() const;
270 ArrayRef<uint8_t> getDyldInfoLazyBindOpcodes() const;
271 ArrayRef<uint8_t> getDyldInfoExportsTrie() const;
273 StringRef getStringTableData() const;
274 bool is64Bit() const;
275 void ReadULEB128s(uint64_t Index, SmallVectorImpl<uint64_t> &Out) const;
277 static StringRef guessLibraryShortName(StringRef Name, bool &isFramework,
280 static Triple::ArchType getArch(uint32_t CPUType);
281 static Triple getArch(uint32_t CPUType, uint32_t CPUSubType,
282 const char **McpuDefault = nullptr);
283 static Triple getThumbArch(uint32_t CPUType, uint32_t CPUSubType,
284 const char **McpuDefault = nullptr);
285 static Triple getArch(uint32_t CPUType, uint32_t CPUSubType,
286 const char **McpuDefault, Triple *ThumbTriple);
287 static bool isValidArch(StringRef ArchFlag);
288 static Triple getHostArch();
290 bool isRelocatableObject() const override;
292 static bool classof(const Binary *v) {
297 typedef SmallVector<const char*, 1> SectionList;
298 SectionList Sections;
299 typedef SmallVector<const char*, 1> LibraryList;
300 LibraryList Libraries;
301 typedef SmallVector<StringRef, 1> LibraryShortName;
302 mutable LibraryShortName LibrariesShortNames;
303 const char *SymtabLoadCmd;
304 const char *DysymtabLoadCmd;
305 const char *DataInCodeLoadCmd;
306 const char *DyldInfoLoadCmd;
310 inline DiceRef::DiceRef(DataRefImpl DiceP, const ObjectFile *Owner)
311 : DicePimpl(DiceP) , OwningObject(Owner) {}
313 inline bool DiceRef::operator==(const DiceRef &Other) const {
314 return DicePimpl == Other.DicePimpl;
317 inline bool DiceRef::operator<(const DiceRef &Other) const {
318 return DicePimpl < Other.DicePimpl;
321 inline void DiceRef::moveNext() {
322 const MachO::data_in_code_entry *P =
323 reinterpret_cast<const MachO::data_in_code_entry *>(DicePimpl.p);
324 DicePimpl.p = reinterpret_cast<uintptr_t>(P + 1);
327 // Since a Mach-O data in code reference, a DiceRef, can only be created when
328 // the OwningObject ObjectFile is a MachOObjectFile a static_cast<> is used for
329 // the methods that get the values of the fields of the reference.
331 inline std::error_code DiceRef::getOffset(uint32_t &Result) const {
332 const MachOObjectFile *MachOOF =
333 static_cast<const MachOObjectFile *>(OwningObject);
334 MachO::data_in_code_entry Dice = MachOOF->getDice(DicePimpl);
335 Result = Dice.offset;
336 return object_error::success;
339 inline std::error_code DiceRef::getLength(uint16_t &Result) const {
340 const MachOObjectFile *MachOOF =
341 static_cast<const MachOObjectFile *>(OwningObject);
342 MachO::data_in_code_entry Dice = MachOOF->getDice(DicePimpl);
343 Result = Dice.length;
344 return object_error::success;
347 inline std::error_code DiceRef::getKind(uint16_t &Result) const {
348 const MachOObjectFile *MachOOF =
349 static_cast<const MachOObjectFile *>(OwningObject);
350 MachO::data_in_code_entry Dice = MachOOF->getDice(DicePimpl);
352 return object_error::success;
355 inline DataRefImpl DiceRef::getRawDataRefImpl() const {
359 inline const ObjectFile *DiceRef::getObjectFile() const {