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