Move everything depending on Object/MachOFormat.h over to Support/MachO.h.
[oota-llvm.git] / include / llvm / Object / MachO.h
1 //===- MachO.h - MachO object file implementation ---------------*- C++ -*-===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 //
10 // This file declares the MachOObjectFile class, which implement the ObjectFile
11 // interface for MachO files.
12 //
13 //===----------------------------------------------------------------------===//
14
15 #ifndef LLVM_OBJECT_MACHO_H
16 #define LLVM_OBJECT_MACHO_H
17
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"
23
24 namespace llvm {
25 namespace object {
26
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.
29 class DiceRef {
30   DataRefImpl DicePimpl;
31   const ObjectFile *OwningObject;
32
33 public:
34   DiceRef() : OwningObject(NULL) { }
35
36   DiceRef(DataRefImpl DiceP, const ObjectFile *Owner);
37
38   bool operator==(const DiceRef &Other) const;
39   bool operator<(const DiceRef &Other) const;
40
41   error_code getNext(DiceRef &Result) const;
42
43   error_code getOffset(uint32_t &Result) const;
44   error_code getLength(uint16_t &Result) const;
45   error_code getKind(uint16_t &Result) const;
46
47   DataRefImpl getRawDataRefImpl() const;
48   const ObjectFile *getObjectFile() const;
49 };
50 typedef content_iterator<DiceRef> dice_iterator;
51
52 class MachOObjectFile : public ObjectFile {
53 public:
54   struct LoadCommandInfo {
55     const char *Ptr;      // Where in memory the load command is.
56     MachO::load_command C; // The command itself.
57   };
58
59   MachOObjectFile(MemoryBuffer *Object, bool IsLittleEndian, bool Is64Bits,
60                   error_code &ec);
61
62   virtual error_code getSymbolNext(DataRefImpl Symb, SymbolRef &Res) const;
63   virtual error_code getSymbolName(DataRefImpl Symb, StringRef &Res) const;
64   virtual error_code getSymbolAddress(DataRefImpl Symb, uint64_t &Res) const;
65   virtual error_code getSymbolFileOffset(DataRefImpl Symb, uint64_t &Res) const;
66   virtual error_code getSymbolAlignment(DataRefImpl Symb, uint32_t &Res) const;
67   virtual error_code getSymbolSize(DataRefImpl Symb, uint64_t &Res) const;
68   virtual error_code getSymbolType(DataRefImpl Symb,
69                                    SymbolRef::Type &Res) const;
70   virtual error_code getSymbolNMTypeChar(DataRefImpl Symb, char &Res) const;
71   virtual error_code getSymbolFlags(DataRefImpl Symb, uint32_t &Res) const;
72   virtual error_code getSymbolSection(DataRefImpl Symb,
73                                       section_iterator &Res) const;
74   virtual error_code getSymbolValue(DataRefImpl Symb, uint64_t &Val) const;
75
76   virtual error_code getSectionNext(DataRefImpl Sec, SectionRef &Res) const;
77   virtual error_code getSectionName(DataRefImpl Sec, StringRef &Res) const;
78   virtual error_code getSectionAddress(DataRefImpl Sec, uint64_t &Res) const;
79   virtual error_code getSectionSize(DataRefImpl Sec, uint64_t &Res) const;
80   virtual error_code getSectionContents(DataRefImpl Sec, StringRef &Res) const;
81   virtual error_code getSectionAlignment(DataRefImpl Sec, uint64_t &Res) const;
82   virtual error_code isSectionText(DataRefImpl Sec, bool &Res) const;
83   virtual error_code isSectionData(DataRefImpl Sec, bool &Res) const;
84   virtual error_code isSectionBSS(DataRefImpl Sec, bool &Res) const;
85   virtual error_code isSectionRequiredForExecution(DataRefImpl Sec,
86                                                    bool &Res) const;
87   virtual error_code isSectionVirtual(DataRefImpl Sec, bool &Res) const;
88   virtual error_code isSectionZeroInit(DataRefImpl Sec, bool &Res) const;
89   virtual error_code isSectionReadOnlyData(DataRefImpl Sec, bool &Res) const;
90   virtual error_code sectionContainsSymbol(DataRefImpl Sec, DataRefImpl Symb,
91                                            bool &Result) const;
92   virtual relocation_iterator getSectionRelBegin(DataRefImpl Sec) const;
93   virtual relocation_iterator getSectionRelEnd(DataRefImpl Sec) const;
94
95   virtual error_code getRelocationNext(DataRefImpl Rel,
96                                        RelocationRef &Res) const;
97   virtual error_code getRelocationAddress(DataRefImpl Rel, uint64_t &Res) const;
98   virtual error_code getRelocationOffset(DataRefImpl Rel, uint64_t &Res) const;
99   virtual symbol_iterator getRelocationSymbol(DataRefImpl Rel) const;
100   virtual error_code getRelocationType(DataRefImpl Rel, uint64_t &Res) const;
101   virtual error_code getRelocationTypeName(DataRefImpl Rel,
102                                            SmallVectorImpl<char> &Result) const;
103   virtual error_code getRelocationValueString(DataRefImpl Rel,
104                                            SmallVectorImpl<char> &Result) const;
105   virtual error_code getRelocationHidden(DataRefImpl Rel, bool &Result) const;
106
107   virtual error_code getLibraryNext(DataRefImpl LibData, LibraryRef &Res) const;
108   virtual error_code getLibraryPath(DataRefImpl LibData, StringRef &Res) const;
109
110   // TODO: Would be useful to have an iterator based version
111   // of the load command interface too.
112
113   virtual symbol_iterator begin_symbols() const;
114   virtual symbol_iterator end_symbols() const;
115
116   virtual symbol_iterator begin_dynamic_symbols() const;
117   virtual symbol_iterator end_dynamic_symbols() const;
118
119   virtual section_iterator begin_sections() const;
120   virtual section_iterator end_sections() const;
121
122   virtual library_iterator begin_libraries_needed() const;
123   virtual library_iterator end_libraries_needed() const;
124
125   virtual uint8_t getBytesInAddress() const;
126
127   virtual StringRef getFileFormatName() const;
128   virtual unsigned getArch() const;
129
130   virtual StringRef getLoadName() const;
131
132   relocation_iterator getSectionRelBegin(unsigned Index) const;
133   relocation_iterator getSectionRelEnd(unsigned Index) const;
134
135   dice_iterator begin_dices() const;
136   dice_iterator end_dices() const;
137
138   // In a MachO file, sections have a segment name. This is used in the .o
139   // files. They have a single segment, but this field specifies which segment
140   // a section should be put in in the final object.
141   StringRef getSectionFinalSegmentName(DataRefImpl Sec) const;
142
143   // Names are stored as 16 bytes. These returns the raw 16 bytes without
144   // interpreting them as a C string.
145   ArrayRef<char> getSectionRawName(DataRefImpl Sec) const;
146   ArrayRef<char> getSectionRawFinalSegmentName(DataRefImpl Sec) const;
147
148   // MachO specific Info about relocations.
149   bool isRelocationScattered(const MachO::any_relocation_info &RE) const;
150   unsigned getPlainRelocationSymbolNum(
151                                     const MachO::any_relocation_info &RE) const;
152   bool getPlainRelocationExternal(const MachO::any_relocation_info &RE) const;
153   bool getScatteredRelocationScattered(
154                                     const MachO::any_relocation_info &RE) const;
155   uint32_t getScatteredRelocationValue(
156                                     const MachO::any_relocation_info &RE) const;
157   unsigned getAnyRelocationAddress(const MachO::any_relocation_info &RE) const;
158   unsigned getAnyRelocationPCRel(const MachO::any_relocation_info &RE) const;
159   unsigned getAnyRelocationLength(const MachO::any_relocation_info &RE) const;
160   unsigned getAnyRelocationType(const MachO::any_relocation_info &RE) const;
161   SectionRef getRelocationSection(const MachO::any_relocation_info &RE) const;
162
163   // Walk load commands.
164   LoadCommandInfo getFirstLoadCommandInfo() const;
165   LoadCommandInfo getNextLoadCommandInfo(const LoadCommandInfo &L) const;
166
167   // MachO specific structures.
168   MachO::section getSection(DataRefImpl DRI) const;
169   MachO::section_64 getSection64(DataRefImpl DRI) const;
170   MachO::section getSection(const LoadCommandInfo &L, unsigned Index) const;
171   MachO::section_64 getSection64(const LoadCommandInfo &L,unsigned Index) const;
172   MachO::nlist getSymbolTableEntry(DataRefImpl DRI) const;
173   MachO::nlist_64 getSymbol64TableEntry(DataRefImpl DRI) const;
174
175   MachO::linkedit_data_command
176   getLinkeditDataLoadCommand(const LoadCommandInfo &L) const;
177   MachO::segment_command
178   getSegmentLoadCommand(const LoadCommandInfo &L) const;
179   MachO::segment_command_64
180   getSegment64LoadCommand(const LoadCommandInfo &L) const;
181   MachO::linker_options_command
182   getLinkerOptionsLoadCommand(const LoadCommandInfo &L) const;
183
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;
188   uint32_t
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;
196
197   StringRef getStringTableData() const;
198   bool is64Bit() const;
199   void ReadULEB128s(uint64_t Index, SmallVectorImpl<uint64_t> &Out) const;
200
201   static Triple::ArchType getArch(uint32_t CPUType);
202
203   static bool classof(const Binary *v) {
204     return v->isMachO();
205   }
206
207 private:
208   typedef SmallVector<const char*, 1> SectionList;
209   SectionList Sections;
210   const char *SymtabLoadCmd;
211   const char *DysymtabLoadCmd;
212   const char *DataInCodeLoadCmd;
213 };
214
215 /// DiceRef
216 inline DiceRef::DiceRef(DataRefImpl DiceP, const ObjectFile *Owner)
217   : DicePimpl(DiceP) , OwningObject(Owner) {}
218
219 inline bool DiceRef::operator==(const DiceRef &Other) const {
220   return DicePimpl == Other.DicePimpl;
221 }
222
223 inline bool DiceRef::operator<(const DiceRef &Other) const {
224   return DicePimpl < Other.DicePimpl;
225 }
226
227 inline error_code DiceRef::getNext(DiceRef &Result) const {
228   DataRefImpl Rel = DicePimpl;
229   const MachO::data_in_code_entry *P =
230     reinterpret_cast<const MachO::data_in_code_entry *>(Rel.p);
231   Rel.p = reinterpret_cast<uintptr_t>(P + 1);
232   Result = DiceRef(Rel, OwningObject);
233   return object_error::success;
234 }
235
236 // Since a Mach-O data in code reference, a DiceRef, can only be created when
237 // the OwningObject ObjectFile is a MachOObjectFile a static_cast<> is used for
238 // the methods that get the values of the fields of the reference.
239
240 inline error_code DiceRef::getOffset(uint32_t &Result) const {
241   const MachOObjectFile *MachOOF =
242     static_cast<const MachOObjectFile *>(OwningObject);
243   MachO::data_in_code_entry Dice = MachOOF->getDice(DicePimpl);
244   Result = Dice.offset;
245   return object_error::success;
246 }
247
248 inline error_code DiceRef::getLength(uint16_t &Result) const {
249   const MachOObjectFile *MachOOF =
250     static_cast<const MachOObjectFile *>(OwningObject);
251   MachO::data_in_code_entry Dice = MachOOF->getDice(DicePimpl);
252   Result = Dice.length;
253   return object_error::success;
254 }
255
256 inline error_code DiceRef::getKind(uint16_t &Result) const {
257   const MachOObjectFile *MachOOF =
258     static_cast<const MachOObjectFile *>(OwningObject);
259   MachO::data_in_code_entry Dice = MachOOF->getDice(DicePimpl);
260   Result = Dice.kind;
261   return object_error::success;
262 }
263
264 inline DataRefImpl DiceRef::getRawDataRefImpl() const {
265   return DicePimpl;
266 }
267
268 inline const ObjectFile *DiceRef::getObjectFile() const {
269   return OwningObject;
270 }
271
272 }
273 }
274
275 #endif
276