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