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