add a bunch of LLVM_OVERRIDE before I start refactoring these.
[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, bool BufferOwned = true);
61
62   error_code getSymbolNext(DataRefImpl Symb, SymbolRef &Res) const
63       LLVM_OVERRIDE;
64   error_code getSymbolName(DataRefImpl Symb, StringRef &Res) const
65       LLVM_OVERRIDE;
66   error_code getSymbolAddress(DataRefImpl Symb, uint64_t &Res) const
67       LLVM_OVERRIDE;
68   error_code getSymbolFileOffset(DataRefImpl Symb, uint64_t &Res) const
69       LLVM_OVERRIDE;
70   error_code getSymbolAlignment(DataRefImpl Symb, uint32_t &Res) const
71       LLVM_OVERRIDE;
72   error_code getSymbolSize(DataRefImpl Symb, uint64_t &Res) const LLVM_OVERRIDE;
73   error_code getSymbolType(DataRefImpl Symb, SymbolRef::Type &Res) const
74       LLVM_OVERRIDE;
75   error_code getSymbolFlags(DataRefImpl Symb, uint32_t &Res) const
76       LLVM_OVERRIDE;
77   error_code getSymbolSection(DataRefImpl Symb, section_iterator &Res) const
78       LLVM_OVERRIDE;
79   error_code getSymbolValue(DataRefImpl Symb, uint64_t &Val) const
80       LLVM_OVERRIDE;
81
82   error_code getSectionNext(DataRefImpl Sec, SectionRef &Res) const
83       LLVM_OVERRIDE;
84   error_code getSectionName(DataRefImpl Sec, StringRef &Res) const
85       LLVM_OVERRIDE;
86   error_code getSectionAddress(DataRefImpl Sec, uint64_t &Res) const
87       LLVM_OVERRIDE;
88   error_code getSectionSize(DataRefImpl Sec, uint64_t &Res) const LLVM_OVERRIDE;
89   error_code getSectionContents(DataRefImpl Sec, StringRef &Res) const
90       LLVM_OVERRIDE;
91   error_code getSectionAlignment(DataRefImpl Sec, uint64_t &Res) const
92       LLVM_OVERRIDE;
93   error_code isSectionText(DataRefImpl Sec, bool &Res) const LLVM_OVERRIDE;
94   error_code isSectionData(DataRefImpl Sec, bool &Res) const LLVM_OVERRIDE;
95   error_code isSectionBSS(DataRefImpl Sec, bool &Res) const LLVM_OVERRIDE;
96   error_code isSectionRequiredForExecution(DataRefImpl Sec, bool &Res) const
97       LLVM_OVERRIDE;
98   error_code isSectionVirtual(DataRefImpl Sec, bool &Res) const LLVM_OVERRIDE;
99   error_code isSectionZeroInit(DataRefImpl Sec, bool &Res) const LLVM_OVERRIDE;
100   error_code isSectionReadOnlyData(DataRefImpl Sec, bool &Res) const
101       LLVM_OVERRIDE;
102   error_code sectionContainsSymbol(DataRefImpl Sec, DataRefImpl Symb,
103                                    bool &Result) const LLVM_OVERRIDE;
104   relocation_iterator section_rel_begin(DataRefImpl Sec) const LLVM_OVERRIDE;
105   relocation_iterator section_rel_end(DataRefImpl Sec) const LLVM_OVERRIDE;
106
107   error_code getRelocationNext(DataRefImpl Rel, RelocationRef &Res) const
108       LLVM_OVERRIDE;
109   error_code getRelocationAddress(DataRefImpl Rel, uint64_t &Res) const
110       LLVM_OVERRIDE;
111   error_code getRelocationOffset(DataRefImpl Rel, uint64_t &Res) const
112       LLVM_OVERRIDE;
113   symbol_iterator getRelocationSymbol(DataRefImpl Rel) const LLVM_OVERRIDE;
114   error_code getRelocationType(DataRefImpl Rel, uint64_t &Res) const
115       LLVM_OVERRIDE;
116   error_code getRelocationTypeName(DataRefImpl Rel,
117                                    SmallVectorImpl<char> &Result) const
118       LLVM_OVERRIDE;
119   error_code getRelocationValueString(DataRefImpl Rel,
120                                       SmallVectorImpl<char> &Result) const
121       LLVM_OVERRIDE;
122   error_code getRelocationHidden(DataRefImpl Rel, bool &Result) const
123       LLVM_OVERRIDE;
124
125   error_code getLibraryNext(DataRefImpl LibData, LibraryRef &Res) const
126       LLVM_OVERRIDE;
127   error_code getLibraryPath(DataRefImpl LibData, StringRef &Res) const
128       LLVM_OVERRIDE;
129
130   // TODO: Would be useful to have an iterator based version
131   // of the load command interface too.
132
133   symbol_iterator begin_symbols() const LLVM_OVERRIDE;
134   symbol_iterator end_symbols() const LLVM_OVERRIDE;
135
136   symbol_iterator begin_dynamic_symbols() const LLVM_OVERRIDE;
137   symbol_iterator end_dynamic_symbols() const LLVM_OVERRIDE;
138
139   section_iterator begin_sections() const LLVM_OVERRIDE;
140   section_iterator end_sections() const LLVM_OVERRIDE;
141
142   library_iterator begin_libraries_needed() const LLVM_OVERRIDE;
143   library_iterator end_libraries_needed() const LLVM_OVERRIDE;
144
145   uint8_t getBytesInAddress() const LLVM_OVERRIDE;
146
147   StringRef getFileFormatName() const LLVM_OVERRIDE;
148   unsigned getArch() const LLVM_OVERRIDE;
149
150   StringRef getLoadName() const LLVM_OVERRIDE;
151
152   relocation_iterator section_rel_begin(unsigned Index) const;
153   relocation_iterator section_rel_end(unsigned Index) const;
154
155   dice_iterator begin_dices() const;
156   dice_iterator end_dices() const;
157
158   // In a MachO file, sections have a segment name. This is used in the .o
159   // files. They have a single segment, but this field specifies which segment
160   // a section should be put in in the final object.
161   StringRef getSectionFinalSegmentName(DataRefImpl Sec) const;
162
163   // Names are stored as 16 bytes. These returns the raw 16 bytes without
164   // interpreting them as a C string.
165   ArrayRef<char> getSectionRawName(DataRefImpl Sec) const;
166   ArrayRef<char> getSectionRawFinalSegmentName(DataRefImpl Sec) const;
167
168   // MachO specific Info about relocations.
169   bool isRelocationScattered(const MachO::any_relocation_info &RE) const;
170   unsigned getPlainRelocationSymbolNum(
171                                     const MachO::any_relocation_info &RE) const;
172   bool getPlainRelocationExternal(const MachO::any_relocation_info &RE) const;
173   bool getScatteredRelocationScattered(
174                                     const MachO::any_relocation_info &RE) const;
175   uint32_t getScatteredRelocationValue(
176                                     const MachO::any_relocation_info &RE) const;
177   unsigned getAnyRelocationAddress(const MachO::any_relocation_info &RE) const;
178   unsigned getAnyRelocationPCRel(const MachO::any_relocation_info &RE) const;
179   unsigned getAnyRelocationLength(const MachO::any_relocation_info &RE) const;
180   unsigned getAnyRelocationType(const MachO::any_relocation_info &RE) const;
181   SectionRef getRelocationSection(const MachO::any_relocation_info &RE) const;
182
183   // Walk load commands.
184   LoadCommandInfo getFirstLoadCommandInfo() const;
185   LoadCommandInfo getNextLoadCommandInfo(const LoadCommandInfo &L) const;
186
187   // MachO specific structures.
188   MachO::section getSection(DataRefImpl DRI) const;
189   MachO::section_64 getSection64(DataRefImpl DRI) const;
190   MachO::section getSection(const LoadCommandInfo &L, unsigned Index) const;
191   MachO::section_64 getSection64(const LoadCommandInfo &L,unsigned Index) const;
192   MachO::nlist getSymbolTableEntry(DataRefImpl DRI) const;
193   MachO::nlist_64 getSymbol64TableEntry(DataRefImpl DRI) const;
194
195   MachO::linkedit_data_command
196   getLinkeditDataLoadCommand(const LoadCommandInfo &L) const;
197   MachO::segment_command
198   getSegmentLoadCommand(const LoadCommandInfo &L) const;
199   MachO::segment_command_64
200   getSegment64LoadCommand(const LoadCommandInfo &L) const;
201   MachO::linker_options_command
202   getLinkerOptionsLoadCommand(const LoadCommandInfo &L) const;
203
204   MachO::any_relocation_info getRelocation(DataRefImpl Rel) const;
205   MachO::data_in_code_entry getDice(DataRefImpl Rel) const;
206   MachO::mach_header getHeader() const;
207   MachO::mach_header_64 getHeader64() const;
208   uint32_t
209   getIndirectSymbolTableEntry(const MachO::dysymtab_command &DLC,
210                               unsigned Index) const;
211   MachO::data_in_code_entry getDataInCodeTableEntry(uint32_t DataOffset,
212                                                     unsigned Index) const;
213   MachO::symtab_command getSymtabLoadCommand() const;
214   MachO::dysymtab_command getDysymtabLoadCommand() const;
215   MachO::linkedit_data_command getDataInCodeLoadCommand() const;
216
217   StringRef getStringTableData() const;
218   bool is64Bit() const;
219   void ReadULEB128s(uint64_t Index, SmallVectorImpl<uint64_t> &Out) const;
220
221   static Triple::ArchType getArch(uint32_t CPUType);
222
223   static bool classof(const Binary *v) {
224     return v->isMachO();
225   }
226
227 private:
228   typedef SmallVector<const char*, 1> SectionList;
229   SectionList Sections;
230   const char *SymtabLoadCmd;
231   const char *DysymtabLoadCmd;
232   const char *DataInCodeLoadCmd;
233 };
234
235 /// DiceRef
236 inline DiceRef::DiceRef(DataRefImpl DiceP, const ObjectFile *Owner)
237   : DicePimpl(DiceP) , OwningObject(Owner) {}
238
239 inline bool DiceRef::operator==(const DiceRef &Other) const {
240   return DicePimpl == Other.DicePimpl;
241 }
242
243 inline bool DiceRef::operator<(const DiceRef &Other) const {
244   return DicePimpl < Other.DicePimpl;
245 }
246
247 inline error_code DiceRef::getNext(DiceRef &Result) const {
248   DataRefImpl Rel = DicePimpl;
249   const MachO::data_in_code_entry *P =
250     reinterpret_cast<const MachO::data_in_code_entry *>(Rel.p);
251   Rel.p = reinterpret_cast<uintptr_t>(P + 1);
252   Result = DiceRef(Rel, OwningObject);
253   return object_error::success;
254 }
255
256 // Since a Mach-O data in code reference, a DiceRef, can only be created when
257 // the OwningObject ObjectFile is a MachOObjectFile a static_cast<> is used for
258 // the methods that get the values of the fields of the reference.
259
260 inline error_code DiceRef::getOffset(uint32_t &Result) const {
261   const MachOObjectFile *MachOOF =
262     static_cast<const MachOObjectFile *>(OwningObject);
263   MachO::data_in_code_entry Dice = MachOOF->getDice(DicePimpl);
264   Result = Dice.offset;
265   return object_error::success;
266 }
267
268 inline error_code DiceRef::getLength(uint16_t &Result) const {
269   const MachOObjectFile *MachOOF =
270     static_cast<const MachOObjectFile *>(OwningObject);
271   MachO::data_in_code_entry Dice = MachOOF->getDice(DicePimpl);
272   Result = Dice.length;
273   return object_error::success;
274 }
275
276 inline error_code DiceRef::getKind(uint16_t &Result) const {
277   const MachOObjectFile *MachOOF =
278     static_cast<const MachOObjectFile *>(OwningObject);
279   MachO::data_in_code_entry Dice = MachOOF->getDice(DicePimpl);
280   Result = Dice.kind;
281   return object_error::success;
282 }
283
284 inline DataRefImpl DiceRef::getRawDataRefImpl() const {
285   return DicePimpl;
286 }
287
288 inline const ObjectFile *DiceRef::getObjectFile() const {
289   return OwningObject;
290 }
291
292 }
293 }
294
295 #endif
296