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