Make getObject const. Remove a const_cast.
[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 binds the MachOObject
11 // class to the generic ObjectFile wrapper.
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/Object/MachOObject.h"
21 #include "llvm/Object/ObjectFile.h"
22 #include "llvm/Support/Endian.h"
23 #include "llvm/Support/MachO.h"
24 #include "llvm/Support/raw_ostream.h"
25
26 namespace llvm {
27 namespace object {
28
29 namespace MachOFormat {
30   struct Section {
31     char Name[16];
32     char SegmentName[16];
33     support::ulittle32_t Address;
34     support::ulittle32_t Size;
35     support::ulittle32_t Offset;
36     support::ulittle32_t Align;
37     support::ulittle32_t RelocationTableOffset;
38     support::ulittle32_t NumRelocationTableEntries;
39     support::ulittle32_t Flags;
40     support::ulittle32_t Reserved1;
41     support::ulittle32_t Reserved2;
42   };
43
44   struct Section64 {
45     char Name[16];
46     char SegmentName[16];
47     support::ulittle64_t Address;
48     support::ulittle64_t Size;
49     support::ulittle32_t Offset;
50     support::ulittle32_t Align;
51     support::ulittle32_t RelocationTableOffset;
52     support::ulittle32_t NumRelocationTableEntries;
53     support::ulittle32_t Flags;
54     support::ulittle32_t Reserved1;
55     support::ulittle32_t Reserved2;
56     support::ulittle32_t Reserved3;
57   };
58
59   struct RelocationEntry {
60     support::ulittle32_t Word0;
61     support::ulittle32_t Word1;
62   };
63
64   struct SymbolTableEntry {
65     support::ulittle32_t StringIndex;
66     uint8_t Type;
67     uint8_t SectionIndex;
68     support::ulittle16_t Flags;
69     support::ulittle32_t Value;
70   };
71
72   struct Symbol64TableEntry {
73     support::ulittle32_t StringIndex;
74     uint8_t Type;
75     uint8_t SectionIndex;
76     support::ulittle16_t Flags;
77     support::ulittle64_t Value;
78   };
79
80   struct SymtabLoadCommand {
81     support::ulittle32_t Type;
82     support::ulittle32_t Size;
83     support::ulittle32_t SymbolTableOffset;
84     support::ulittle32_t NumSymbolTableEntries;
85     support::ulittle32_t StringTableOffset;
86     support::ulittle32_t StringTableSize;
87   };
88
89   struct SegmentLoadCommand {
90     support::ulittle32_t Type;
91     support::ulittle32_t Size;
92     char Name[16];
93     support::ulittle32_t VMAddress;
94     support::ulittle32_t VMSize;
95     support::ulittle32_t FileOffset;
96     support::ulittle32_t FileSize;
97     support::ulittle32_t MaxVMProtection;
98     support::ulittle32_t InitialVMProtection;
99     support::ulittle32_t NumSections;
100     support::ulittle32_t Flags;
101   };
102
103   struct Segment64LoadCommand {
104     support::ulittle32_t Type;
105     support::ulittle32_t Size;
106     char Name[16];
107     support::ulittle64_t VMAddress;
108     support::ulittle64_t VMSize;
109     support::ulittle64_t FileOffset;
110     support::ulittle64_t FileSize;
111     support::ulittle32_t MaxVMProtection;
112     support::ulittle32_t InitialVMProtection;
113     support::ulittle32_t NumSections;
114     support::ulittle32_t Flags;
115   };
116
117   struct LinkeditDataLoadCommand {
118     support::ulittle32_t Type;
119     support::ulittle32_t Size;
120     support::ulittle32_t DataOffset;
121     support::ulittle32_t DataSize;
122   };
123 }
124
125 typedef MachOObject::LoadCommandInfo LoadCommandInfo;
126
127 class MachOObjectFile : public ObjectFile {
128 public:
129   MachOObjectFile(MemoryBuffer *Object, MachOObject *MOO, error_code &ec);
130
131   virtual symbol_iterator begin_symbols() const;
132   virtual symbol_iterator end_symbols() const;
133   virtual symbol_iterator begin_dynamic_symbols() const;
134   virtual symbol_iterator end_dynamic_symbols() const;
135   virtual library_iterator begin_libraries_needed() const;
136   virtual library_iterator end_libraries_needed() const;
137   virtual section_iterator begin_sections() const;
138   virtual section_iterator end_sections() const;
139
140   virtual uint8_t getBytesInAddress() const;
141   virtual StringRef getFileFormatName() const;
142   virtual unsigned getArch() const;
143   virtual StringRef getLoadName() const;
144
145   // In a MachO file, sections have a segment name. This is used in the .o
146   // files. They have a single segment, but this field specifies which segment
147   // a section should be put in in the final object.
148   StringRef getSectionFinalSegmentName(DataRefImpl Sec) const;
149
150   // Names are stored as 16 bytes. These returns the raw 16 bytes without
151   // interpreting them as a C string.
152   ArrayRef<char> getSectionRawName(DataRefImpl Sec) const;
153   ArrayRef<char>getSectionRawFinalSegmentName(DataRefImpl Sec) const;
154
155   const MachOFormat::LinkeditDataLoadCommand *
156     getLinkeditDataLoadCommand(LoadCommandInfo LCI) const;
157
158   const MachOObject *getObject() const { return MachOObj.get(); }
159
160   static inline bool classof(const Binary *v) {
161     return v->isMachO();
162   }
163
164 protected:
165   virtual error_code getSymbolNext(DataRefImpl Symb, SymbolRef &Res) const;
166   virtual error_code getSymbolName(DataRefImpl Symb, StringRef &Res) const;
167   virtual error_code getSymbolFileOffset(DataRefImpl Symb, uint64_t &Res) const;
168   virtual error_code getSymbolAddress(DataRefImpl Symb, uint64_t &Res) const;
169   virtual error_code getSymbolSize(DataRefImpl Symb, uint64_t &Res) const;
170   virtual error_code getSymbolNMTypeChar(DataRefImpl Symb, char &Res) const;
171   virtual error_code getSymbolFlags(DataRefImpl Symb, uint32_t &Res) const;
172   virtual error_code getSymbolType(DataRefImpl Symb, SymbolRef::Type &Res) const;
173   virtual error_code getSymbolSection(DataRefImpl Symb,
174                                       section_iterator &Res) const;
175   virtual error_code getSymbolValue(DataRefImpl Symb, uint64_t &Val) const;
176
177   virtual error_code getSectionNext(DataRefImpl Sec, SectionRef &Res) const;
178   virtual error_code getSectionName(DataRefImpl Sec, StringRef &Res) const;
179   virtual error_code getSectionAddress(DataRefImpl Sec, uint64_t &Res) const;
180   virtual error_code getSectionSize(DataRefImpl Sec, uint64_t &Res) const;
181   virtual error_code getSectionContents(DataRefImpl Sec, StringRef &Res) const;
182   virtual error_code getSectionAlignment(DataRefImpl Sec, uint64_t &Res) const;
183   virtual error_code isSectionText(DataRefImpl Sec, bool &Res) const;
184   virtual error_code isSectionData(DataRefImpl Sec, bool &Res) const;
185   virtual error_code isSectionBSS(DataRefImpl Sec, bool &Res) const;
186   virtual error_code isSectionRequiredForExecution(DataRefImpl Sec,
187                                                    bool &Res) const;
188   virtual error_code isSectionVirtual(DataRefImpl Sec, bool &Res) const;
189   virtual error_code isSectionZeroInit(DataRefImpl Sec, bool &Res) const;
190   virtual error_code isSectionReadOnlyData(DataRefImpl Sec, bool &Res) const;
191   virtual error_code sectionContainsSymbol(DataRefImpl DRI, DataRefImpl S,
192                                            bool &Result) const;
193   virtual relocation_iterator getSectionRelBegin(DataRefImpl Sec) const;
194   virtual relocation_iterator getSectionRelEnd(DataRefImpl Sec) const;
195
196   virtual error_code getRelocationNext(DataRefImpl Rel,
197                                        RelocationRef &Res) const;
198   virtual error_code getRelocationAddress(DataRefImpl Rel,
199                                           uint64_t &Res) const;
200   virtual error_code getRelocationOffset(DataRefImpl Rel,
201                                          uint64_t &Res) const;
202   virtual error_code getRelocationSymbol(DataRefImpl Rel,
203                                          SymbolRef &Res) const;
204   virtual error_code getRelocationType(DataRefImpl Rel,
205                                        uint64_t &Res) const;
206   virtual error_code getRelocationTypeName(DataRefImpl Rel,
207                                            SmallVectorImpl<char> &Result) const;
208   virtual error_code getRelocationAdditionalInfo(DataRefImpl Rel,
209                                                  int64_t &Res) const;
210   virtual error_code getRelocationValueString(DataRefImpl Rel,
211                                            SmallVectorImpl<char> &Result) const;
212   virtual error_code getRelocationHidden(DataRefImpl Rel, bool &Result) const;
213
214   virtual error_code getLibraryNext(DataRefImpl LibData, LibraryRef &Res) const;
215   virtual error_code getLibraryPath(DataRefImpl LibData, StringRef &Res) const;
216
217 private:
218   OwningPtr<MachOObject> MachOObj;
219   typedef SmallVector<DataRefImpl, 1> SectionList;
220   SectionList Sections;
221
222
223   void moveToNextSection(DataRefImpl &DRI) const;
224
225   const MachOFormat::SymbolTableEntry *
226   getSymbolTableEntry(DataRefImpl DRI) const;
227
228   const MachOFormat::SymbolTableEntry *
229   getSymbolTableEntry(DataRefImpl DRI,
230                      const MachOFormat::SymtabLoadCommand *SymtabLoadCmd) const;
231
232   const MachOFormat::Symbol64TableEntry *
233   getSymbol64TableEntry(DataRefImpl DRI) const;
234
235   const MachOFormat::Symbol64TableEntry *
236   getSymbol64TableEntry(DataRefImpl DRI,
237                      const MachOFormat::SymtabLoadCommand *SymtabLoadCmd) const;
238
239   void moveToNextSymbol(DataRefImpl &DRI) const;
240   const MachOFormat::Section *getSection(DataRefImpl DRI) const;
241   const MachOFormat::Section64 *getSection64(DataRefImpl DRI) const;
242   const MachOFormat::RelocationEntry *getRelocation(DataRefImpl Rel) const;
243   const MachOFormat::SymtabLoadCommand *
244     getSymtabLoadCommand(LoadCommandInfo LCI) const;
245   const MachOFormat::SegmentLoadCommand *
246     getSegmentLoadCommand(LoadCommandInfo LCI) const;
247   const MachOFormat::Segment64LoadCommand *
248     getSegment64LoadCommand(LoadCommandInfo LCI) const;
249   std::size_t getSectionIndex(DataRefImpl Sec) const;
250
251   void printRelocationTargetName(const MachOFormat::RelocationEntry *RE,
252                                  raw_string_ostream &fmt) const;
253 };
254
255 }
256 }
257
258 #endif
259