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