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