Remove last use of InMemoryStruct from MachOObjectFile.cpp.
[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
118 typedef MachOObject::LoadCommandInfo LoadCommandInfo;
119
120 class MachOObjectFile : public ObjectFile {
121 public:
122   MachOObjectFile(MemoryBuffer *Object, MachOObject *MOO, error_code &ec);
123
124   virtual symbol_iterator begin_symbols() const;
125   virtual symbol_iterator end_symbols() const;
126   virtual symbol_iterator begin_dynamic_symbols() const;
127   virtual symbol_iterator end_dynamic_symbols() const;
128   virtual library_iterator begin_libraries_needed() const;
129   virtual library_iterator end_libraries_needed() const;
130   virtual section_iterator begin_sections() const;
131   virtual section_iterator end_sections() const;
132
133   virtual uint8_t getBytesInAddress() const;
134   virtual StringRef getFileFormatName() const;
135   virtual unsigned getArch() const;
136   virtual StringRef getLoadName() const;
137
138   // In a MachO file, sections have a segment name. This is used in the .o
139   // files. They have a single segment, but this field specifies which segment
140   // a section should be put in in the final object.
141   StringRef getSectionFinalSegmentName(DataRefImpl Sec) const;
142
143   // Names are stored as 16 bytes. These returns the raw 16 bytes without
144   // interpreting them as a C string.
145   ArrayRef<char> getSectionRawName(DataRefImpl Sec) const;
146   ArrayRef<char>getSectionRawFinalSegmentName(DataRefImpl Sec) const;
147
148   MachOObject *getObject() { return MachOObj.get(); }
149
150   static inline bool classof(const Binary *v) {
151     return v->isMachO();
152   }
153
154 protected:
155   virtual error_code getSymbolNext(DataRefImpl Symb, SymbolRef &Res) const;
156   virtual error_code getSymbolName(DataRefImpl Symb, StringRef &Res) const;
157   virtual error_code getSymbolFileOffset(DataRefImpl Symb, uint64_t &Res) const;
158   virtual error_code getSymbolAddress(DataRefImpl Symb, uint64_t &Res) const;
159   virtual error_code getSymbolSize(DataRefImpl Symb, uint64_t &Res) const;
160   virtual error_code getSymbolNMTypeChar(DataRefImpl Symb, char &Res) const;
161   virtual error_code getSymbolFlags(DataRefImpl Symb, uint32_t &Res) const;
162   virtual error_code getSymbolType(DataRefImpl Symb, SymbolRef::Type &Res) const;
163   virtual error_code getSymbolSection(DataRefImpl Symb,
164                                       section_iterator &Res) const;
165   virtual error_code getSymbolValue(DataRefImpl Symb, uint64_t &Val) const;
166
167   virtual error_code getSectionNext(DataRefImpl Sec, SectionRef &Res) const;
168   virtual error_code getSectionName(DataRefImpl Sec, StringRef &Res) const;
169   virtual error_code getSectionAddress(DataRefImpl Sec, uint64_t &Res) const;
170   virtual error_code getSectionSize(DataRefImpl Sec, uint64_t &Res) const;
171   virtual error_code getSectionContents(DataRefImpl Sec, StringRef &Res) const;
172   virtual error_code getSectionAlignment(DataRefImpl Sec, uint64_t &Res) const;
173   virtual error_code isSectionText(DataRefImpl Sec, bool &Res) const;
174   virtual error_code isSectionData(DataRefImpl Sec, bool &Res) const;
175   virtual error_code isSectionBSS(DataRefImpl Sec, bool &Res) const;
176   virtual error_code isSectionRequiredForExecution(DataRefImpl Sec,
177                                                    bool &Res) const;
178   virtual error_code isSectionVirtual(DataRefImpl Sec, bool &Res) const;
179   virtual error_code isSectionZeroInit(DataRefImpl Sec, bool &Res) const;
180   virtual error_code isSectionReadOnlyData(DataRefImpl Sec, bool &Res) const;
181   virtual error_code sectionContainsSymbol(DataRefImpl DRI, DataRefImpl S,
182                                            bool &Result) const;
183   virtual relocation_iterator getSectionRelBegin(DataRefImpl Sec) const;
184   virtual relocation_iterator getSectionRelEnd(DataRefImpl Sec) const;
185
186   virtual error_code getRelocationNext(DataRefImpl Rel,
187                                        RelocationRef &Res) const;
188   virtual error_code getRelocationAddress(DataRefImpl Rel,
189                                           uint64_t &Res) const;
190   virtual error_code getRelocationOffset(DataRefImpl Rel,
191                                          uint64_t &Res) const;
192   virtual error_code getRelocationSymbol(DataRefImpl Rel,
193                                          SymbolRef &Res) const;
194   virtual error_code getRelocationType(DataRefImpl Rel,
195                                        uint64_t &Res) const;
196   virtual error_code getRelocationTypeName(DataRefImpl Rel,
197                                            SmallVectorImpl<char> &Result) const;
198   virtual error_code getRelocationAdditionalInfo(DataRefImpl Rel,
199                                                  int64_t &Res) const;
200   virtual error_code getRelocationValueString(DataRefImpl Rel,
201                                            SmallVectorImpl<char> &Result) const;
202   virtual error_code getRelocationHidden(DataRefImpl Rel, bool &Result) const;
203
204   virtual error_code getLibraryNext(DataRefImpl LibData, LibraryRef &Res) const;
205   virtual error_code getLibraryPath(DataRefImpl LibData, StringRef &Res) const;
206
207 private:
208   OwningPtr<MachOObject> MachOObj;
209   typedef SmallVector<DataRefImpl, 1> SectionList;
210   SectionList Sections;
211
212
213   void moveToNextSection(DataRefImpl &DRI) const;
214
215   const MachOFormat::SymbolTableEntry *
216   getSymbolTableEntry(DataRefImpl DRI) const;
217
218   const MachOFormat::SymbolTableEntry *
219   getSymbolTableEntry(DataRefImpl DRI,
220                      const MachOFormat::SymtabLoadCommand *SymtabLoadCmd) const;
221
222   const MachOFormat::Symbol64TableEntry *
223   getSymbol64TableEntry(DataRefImpl DRI) const;
224
225   const MachOFormat::Symbol64TableEntry *
226   getSymbol64TableEntry(DataRefImpl DRI,
227                      const MachOFormat::SymtabLoadCommand *SymtabLoadCmd) const;
228
229   void moveToNextSymbol(DataRefImpl &DRI) const;
230   const MachOFormat::Section *getSection(DataRefImpl DRI) const;
231   const MachOFormat::Section64 *getSection64(DataRefImpl DRI) const;
232   const MachOFormat::RelocationEntry *getRelocation(DataRefImpl Rel) const;
233   const MachOFormat::SymtabLoadCommand *
234     getSymtabLoadCommand(LoadCommandInfo LCI) const;
235   const MachOFormat::SegmentLoadCommand *
236     getSegmentLoadCommand(LoadCommandInfo LCI) const;
237   const MachOFormat::Segment64LoadCommand *
238     getSegment64LoadCommand(LoadCommandInfo LCI) const;
239   std::size_t getSectionIndex(DataRefImpl Sec) const;
240
241   void printRelocationTargetName(const MachOFormat::RelocationEntry *RE,
242                                  raw_string_ostream &fmt) const;
243 };
244
245 }
246 }
247
248 #endif
249