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/Object/MachOFormat.h"
21 #include "llvm/Object/ObjectFile.h"
22 #include "llvm/Support/MachO.h"
23 #include "llvm/Support/raw_ostream.h"
28 class MachOObjectFile : public ObjectFile {
30 struct LoadCommandInfo {
31 const char *Ptr; // Where in memory the load command is.
32 macho::LoadCommand C; // The command itself.
35 MachOObjectFile(MemoryBuffer *Object, bool IsLittleEndian, bool Is64Bits,
38 virtual error_code getSymbolNext(DataRefImpl Symb, SymbolRef &Res) const;
39 virtual error_code getSymbolName(DataRefImpl Symb, StringRef &Res) const;
40 virtual error_code getSymbolAddress(DataRefImpl Symb, uint64_t &Res) const;
41 virtual error_code getSymbolFileOffset(DataRefImpl Symb, uint64_t &Res) const;
42 virtual error_code getSymbolAlignment(DataRefImpl Symb, uint32_t &Res) const;
43 virtual error_code getSymbolSize(DataRefImpl Symb, uint64_t &Res) const;
44 virtual error_code getSymbolType(DataRefImpl Symb,
45 SymbolRef::Type &Res) const;
46 virtual error_code getSymbolNMTypeChar(DataRefImpl Symb, char &Res) const;
47 virtual error_code getSymbolFlags(DataRefImpl Symb, uint32_t &Res) const;
48 virtual error_code getSymbolSection(DataRefImpl Symb,
49 section_iterator &Res) const;
50 virtual error_code getSymbolValue(DataRefImpl Symb, uint64_t &Val) const;
52 virtual error_code getSectionNext(DataRefImpl Sec, SectionRef &Res) const;
53 virtual error_code getSectionName(DataRefImpl Sec, StringRef &Res) const;
54 virtual error_code getSectionAddress(DataRefImpl Sec, uint64_t &Res) const;
55 virtual error_code getSectionSize(DataRefImpl Sec, uint64_t &Res) const;
56 virtual error_code getSectionContents(DataRefImpl Sec, StringRef &Res) const;
57 virtual error_code getSectionAlignment(DataRefImpl Sec, uint64_t &Res) const;
58 virtual error_code isSectionText(DataRefImpl Sec, bool &Res) const;
59 virtual error_code isSectionData(DataRefImpl Sec, bool &Res) const;
60 virtual error_code isSectionBSS(DataRefImpl Sec, bool &Res) const;
61 virtual error_code isSectionRequiredForExecution(DataRefImpl Sec,
63 virtual error_code isSectionVirtual(DataRefImpl Sec, bool &Res) const;
64 virtual error_code isSectionZeroInit(DataRefImpl Sec, bool &Res) const;
65 virtual error_code isSectionReadOnlyData(DataRefImpl Sec, bool &Res) const;
66 virtual error_code sectionContainsSymbol(DataRefImpl Sec, DataRefImpl Symb,
68 virtual relocation_iterator getSectionRelBegin(DataRefImpl Sec) const;
69 virtual relocation_iterator getSectionRelEnd(DataRefImpl Sec) const;
71 virtual error_code getRelocationNext(DataRefImpl Rel,
72 RelocationRef &Res) const;
73 virtual error_code getRelocationAddress(DataRefImpl Rel, uint64_t &Res) const;
74 virtual error_code getRelocationOffset(DataRefImpl Rel, uint64_t &Res) const;
75 virtual symbol_iterator getRelocationSymbol(DataRefImpl Rel) const;
76 virtual error_code getRelocationType(DataRefImpl Rel, uint64_t &Res) const;
77 virtual error_code getRelocationTypeName(DataRefImpl Rel,
78 SmallVectorImpl<char> &Result) const;
79 virtual error_code getRelocationValueString(DataRefImpl Rel,
80 SmallVectorImpl<char> &Result) const;
81 virtual error_code getRelocationHidden(DataRefImpl Rel, bool &Result) const;
83 virtual error_code getLibraryNext(DataRefImpl LibData, LibraryRef &Res) const;
84 virtual error_code getLibraryPath(DataRefImpl LibData, StringRef &Res) const;
86 // TODO: Would be useful to have an iterator based version
87 // of the load command interface too.
89 virtual symbol_iterator begin_symbols() const;
90 virtual symbol_iterator end_symbols() const;
92 virtual symbol_iterator begin_dynamic_symbols() const;
93 virtual symbol_iterator end_dynamic_symbols() const;
95 virtual section_iterator begin_sections() const;
96 virtual section_iterator end_sections() const;
98 virtual library_iterator begin_libraries_needed() const;
99 virtual library_iterator end_libraries_needed() const;
101 virtual uint8_t getBytesInAddress() const;
103 virtual StringRef getFileFormatName() const;
104 virtual unsigned getArch() const;
106 virtual StringRef getLoadName() const;
108 relocation_iterator getSectionRelBegin(unsigned Index) const;
109 relocation_iterator getSectionRelEnd(unsigned Index) const;
111 // In a MachO file, sections have a segment name. This is used in the .o
112 // files. They have a single segment, but this field specifies which segment
113 // a section should be put in in the final object.
114 StringRef getSectionFinalSegmentName(DataRefImpl Sec) const;
116 // Names are stored as 16 bytes. These returns the raw 16 bytes without
117 // interpreting them as a C string.
118 ArrayRef<char> getSectionRawName(DataRefImpl Sec) const;
119 ArrayRef<char> getSectionRawFinalSegmentName(DataRefImpl Sec) const;
121 // MachO specific Info about relocations.
122 bool isRelocationScattered(const macho::RelocationEntry &RE) const;
123 unsigned getPlainRelocationSymbolNum(const macho::RelocationEntry &RE) const;
124 bool getPlainRelocationExternal(const macho::RelocationEntry &RE) const;
125 bool getScatteredRelocationScattered(const macho::RelocationEntry &RE) const;
126 uint32_t getScatteredRelocationValue(const macho::RelocationEntry &RE) const;
127 unsigned getAnyRelocationAddress(const macho::RelocationEntry &RE) const;
128 unsigned getAnyRelocationPCRel(const macho::RelocationEntry &RE) const;
129 unsigned getAnyRelocationLength(const macho::RelocationEntry &RE) const;
130 unsigned getAnyRelocationType(const macho::RelocationEntry &RE) const;
131 SectionRef getRelocationSection(const macho::RelocationEntry &RE) const;
133 // Walk load commands.
134 LoadCommandInfo getFirstLoadCommandInfo() const;
135 LoadCommandInfo getNextLoadCommandInfo(const LoadCommandInfo &L) const;
137 // MachO specific structures.
138 macho::Section getSection(DataRefImpl DRI) const;
139 macho::Section64 getSection64(DataRefImpl DRI) const;
140 macho::Section getSection(const LoadCommandInfo &L, unsigned Index) const;
141 macho::Section64 getSection64(const LoadCommandInfo &L, unsigned Index) const;
142 macho::SymbolTableEntry getSymbolTableEntry(DataRefImpl DRI) const;
143 macho::Symbol64TableEntry getSymbol64TableEntry(DataRefImpl DRI) const;
145 macho::LinkeditDataLoadCommand
146 getLinkeditDataLoadCommand(const LoadCommandInfo &L) const;
147 macho::SegmentLoadCommand
148 getSegmentLoadCommand(const LoadCommandInfo &L) const;
149 macho::Segment64LoadCommand
150 getSegment64LoadCommand(const LoadCommandInfo &L) const;
151 macho::LinkerOptionsLoadCommand
152 getLinkerOptionsLoadCommand(const LoadCommandInfo &L) const;
154 macho::RelocationEntry getRelocation(DataRefImpl Rel) const;
155 macho::Header getHeader() const;
156 macho::Header64Ext getHeader64Ext() const;
157 macho::IndirectSymbolTableEntry
158 getIndirectSymbolTableEntry(const macho::DysymtabLoadCommand &DLC,
159 unsigned Index) const;
160 macho::DataInCodeTableEntry getDataInCodeTableEntry(uint32_t DataOffset,
161 unsigned Index) const;
162 macho::SymtabLoadCommand getSymtabLoadCommand() const;
163 macho::DysymtabLoadCommand getDysymtabLoadCommand() const;
165 StringRef getStringTableData() const;
166 bool is64Bit() const;
167 void ReadULEB128s(uint64_t Index, SmallVectorImpl<uint64_t> &Out) const;
169 static bool classof(const Binary *v) {
174 typedef SmallVector<const char*, 1> SectionList;
175 SectionList Sections;
176 const char *SymtabLoadCmd;
177 const char *DysymtabLoadCmd;