Teach llvm-objdump with the -macho parser how to use the data in code table
[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 implement the ObjectFile
11 // interface for MachO files.
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/MachOFormat.h"
21 #include "llvm/Object/ObjectFile.h"
22 #include "llvm/Support/MachO.h"
23 #include "llvm/Support/raw_ostream.h"
24
25 namespace llvm {
26 namespace object {
27
28 /// DiceRef - This is a value type class that represents a single
29 /// data in code entry in the table in a Mach-O object file.
30 class DiceRef {
31   DataRefImpl DicePimpl;
32   const ObjectFile *OwningObject;
33
34 public:
35   DiceRef() : OwningObject(NULL) { }
36
37   DiceRef(DataRefImpl DiceP, const ObjectFile *Owner);
38
39   bool operator==(const DiceRef &Other) const;
40   bool operator<(const DiceRef &Other) const;
41
42   error_code getNext(DiceRef &Result) const;
43
44   error_code getOffset(uint32_t &Result) const;
45   error_code getLength(uint16_t &Result) const;
46   error_code getKind(uint16_t &Result) const;
47
48   DataRefImpl getRawDataRefImpl() const;
49   const ObjectFile *getObjectFile() const;
50 };
51 typedef content_iterator<DiceRef> dice_iterator;
52
53 class MachOObjectFile : public ObjectFile {
54 public:
55   struct LoadCommandInfo {
56     const char *Ptr;      // Where in memory the load command is.
57     macho::LoadCommand C; // The command itself.
58   };
59
60   MachOObjectFile(MemoryBuffer *Object, bool IsLittleEndian, bool Is64Bits,
61                   error_code &ec);
62
63   virtual error_code getSymbolNext(DataRefImpl Symb, SymbolRef &Res) const;
64   virtual error_code getSymbolName(DataRefImpl Symb, StringRef &Res) const;
65   virtual error_code getSymbolAddress(DataRefImpl Symb, uint64_t &Res) const;
66   virtual error_code getSymbolFileOffset(DataRefImpl Symb, uint64_t &Res) const;
67   virtual error_code getSymbolAlignment(DataRefImpl Symb, uint32_t &Res) const;
68   virtual error_code getSymbolSize(DataRefImpl Symb, uint64_t &Res) const;
69   virtual error_code getSymbolType(DataRefImpl Symb,
70                                    SymbolRef::Type &Res) const;
71   virtual error_code getSymbolNMTypeChar(DataRefImpl Symb, char &Res) const;
72   virtual error_code getSymbolFlags(DataRefImpl Symb, uint32_t &Res) const;
73   virtual error_code getSymbolSection(DataRefImpl Symb,
74                                       section_iterator &Res) const;
75   virtual error_code getSymbolValue(DataRefImpl Symb, uint64_t &Val) const;
76
77   virtual error_code getSectionNext(DataRefImpl Sec, SectionRef &Res) const;
78   virtual error_code getSectionName(DataRefImpl Sec, StringRef &Res) const;
79   virtual error_code getSectionAddress(DataRefImpl Sec, uint64_t &Res) const;
80   virtual error_code getSectionSize(DataRefImpl Sec, uint64_t &Res) const;
81   virtual error_code getSectionContents(DataRefImpl Sec, StringRef &Res) const;
82   virtual error_code getSectionAlignment(DataRefImpl Sec, uint64_t &Res) const;
83   virtual error_code isSectionText(DataRefImpl Sec, bool &Res) const;
84   virtual error_code isSectionData(DataRefImpl Sec, bool &Res) const;
85   virtual error_code isSectionBSS(DataRefImpl Sec, bool &Res) const;
86   virtual error_code isSectionRequiredForExecution(DataRefImpl Sec,
87                                                    bool &Res) const;
88   virtual error_code isSectionVirtual(DataRefImpl Sec, bool &Res) const;
89   virtual error_code isSectionZeroInit(DataRefImpl Sec, bool &Res) const;
90   virtual error_code isSectionReadOnlyData(DataRefImpl Sec, bool &Res) const;
91   virtual error_code sectionContainsSymbol(DataRefImpl Sec, DataRefImpl Symb,
92                                            bool &Result) const;
93   virtual relocation_iterator getSectionRelBegin(DataRefImpl Sec) const;
94   virtual relocation_iterator getSectionRelEnd(DataRefImpl Sec) const;
95
96   virtual error_code getRelocationNext(DataRefImpl Rel,
97                                        RelocationRef &Res) const;
98   virtual error_code getRelocationAddress(DataRefImpl Rel, uint64_t &Res) const;
99   virtual error_code getRelocationOffset(DataRefImpl Rel, uint64_t &Res) const;
100   virtual symbol_iterator getRelocationSymbol(DataRefImpl Rel) const;
101   virtual error_code getRelocationType(DataRefImpl Rel, uint64_t &Res) const;
102   virtual error_code getRelocationTypeName(DataRefImpl Rel,
103                                            SmallVectorImpl<char> &Result) const;
104   virtual error_code getRelocationValueString(DataRefImpl Rel,
105                                            SmallVectorImpl<char> &Result) const;
106   virtual error_code getRelocationHidden(DataRefImpl Rel, bool &Result) const;
107
108   virtual error_code getLibraryNext(DataRefImpl LibData, LibraryRef &Res) const;
109   virtual error_code getLibraryPath(DataRefImpl LibData, StringRef &Res) const;
110
111   // TODO: Would be useful to have an iterator based version
112   // of the load command interface too.
113
114   virtual symbol_iterator begin_symbols() const;
115   virtual symbol_iterator end_symbols() const;
116
117   virtual symbol_iterator begin_dynamic_symbols() const;
118   virtual symbol_iterator end_dynamic_symbols() const;
119
120   virtual section_iterator begin_sections() const;
121   virtual section_iterator end_sections() const;
122
123   virtual library_iterator begin_libraries_needed() const;
124   virtual library_iterator end_libraries_needed() const;
125
126   virtual uint8_t getBytesInAddress() const;
127
128   virtual StringRef getFileFormatName() const;
129   virtual unsigned getArch() const;
130
131   virtual StringRef getLoadName() const;
132
133   relocation_iterator getSectionRelBegin(unsigned Index) const;
134   relocation_iterator getSectionRelEnd(unsigned Index) const;
135
136   dice_iterator begin_dices() const;
137   dice_iterator end_dices() const;
138
139   // In a MachO file, sections have a segment name. This is used in the .o
140   // files. They have a single segment, but this field specifies which segment
141   // a section should be put in in the final object.
142   StringRef getSectionFinalSegmentName(DataRefImpl Sec) const;
143
144   // Names are stored as 16 bytes. These returns the raw 16 bytes without
145   // interpreting them as a C string.
146   ArrayRef<char> getSectionRawName(DataRefImpl Sec) const;
147   ArrayRef<char> getSectionRawFinalSegmentName(DataRefImpl Sec) const;
148
149   // MachO specific Info about relocations.
150   bool isRelocationScattered(const macho::RelocationEntry &RE) const;
151   unsigned getPlainRelocationSymbolNum(const macho::RelocationEntry &RE) const;
152   bool getPlainRelocationExternal(const macho::RelocationEntry &RE) const;
153   bool getScatteredRelocationScattered(const macho::RelocationEntry &RE) const;
154   uint32_t getScatteredRelocationValue(const macho::RelocationEntry &RE) const;
155   unsigned getAnyRelocationAddress(const macho::RelocationEntry &RE) const;
156   unsigned getAnyRelocationPCRel(const macho::RelocationEntry &RE) const;
157   unsigned getAnyRelocationLength(const macho::RelocationEntry &RE) const;
158   unsigned getAnyRelocationType(const macho::RelocationEntry &RE) const;
159   SectionRef getRelocationSection(const macho::RelocationEntry &RE) const;
160
161   // Walk load commands.
162   LoadCommandInfo getFirstLoadCommandInfo() const;
163   LoadCommandInfo getNextLoadCommandInfo(const LoadCommandInfo &L) const;
164
165   // MachO specific structures.
166   macho::Section getSection(DataRefImpl DRI) const;
167   macho::Section64 getSection64(DataRefImpl DRI) const;
168   macho::Section getSection(const LoadCommandInfo &L, unsigned Index) const;
169   macho::Section64 getSection64(const LoadCommandInfo &L, unsigned Index) const;
170   macho::SymbolTableEntry getSymbolTableEntry(DataRefImpl DRI) const;
171   macho::Symbol64TableEntry getSymbol64TableEntry(DataRefImpl DRI) const;
172
173   macho::LinkeditDataLoadCommand
174   getLinkeditDataLoadCommand(const LoadCommandInfo &L) const;
175   macho::SegmentLoadCommand
176   getSegmentLoadCommand(const LoadCommandInfo &L) const;
177   macho::Segment64LoadCommand
178   getSegment64LoadCommand(const LoadCommandInfo &L) const;
179   macho::LinkerOptionsLoadCommand
180   getLinkerOptionsLoadCommand(const LoadCommandInfo &L) const;
181
182   macho::RelocationEntry getRelocation(DataRefImpl Rel) const;
183   macho::DataInCodeTableEntry getDice(DataRefImpl Rel) const;
184   macho::Header getHeader() const;
185   macho::Header64Ext getHeader64Ext() const;
186   macho::IndirectSymbolTableEntry
187   getIndirectSymbolTableEntry(const macho::DysymtabLoadCommand &DLC,
188                               unsigned Index) const;
189   macho::DataInCodeTableEntry getDataInCodeTableEntry(uint32_t DataOffset,
190                                                       unsigned Index) const;
191   macho::SymtabLoadCommand getSymtabLoadCommand() const;
192   macho::DysymtabLoadCommand getDysymtabLoadCommand() const;
193   macho::LinkeditDataLoadCommand getDataInCodeLoadCommand() const;
194
195   StringRef getStringTableData() const;
196   bool is64Bit() const;
197   void ReadULEB128s(uint64_t Index, SmallVectorImpl<uint64_t> &Out) const;
198
199   static bool classof(const Binary *v) {
200     return v->isMachO();
201   }
202
203 private:
204   typedef SmallVector<const char*, 1> SectionList;
205   SectionList Sections;
206   const char *SymtabLoadCmd;
207   const char *DysymtabLoadCmd;
208   const char *DataInCodeLoadCmd;
209 };
210
211 /// DiceRef
212 inline DiceRef::DiceRef(DataRefImpl DiceP, const ObjectFile *Owner)
213   : DicePimpl(DiceP) , OwningObject(Owner) {}
214
215 inline bool DiceRef::operator==(const DiceRef &Other) const {
216   return DicePimpl == Other.DicePimpl;
217 }
218
219 inline bool DiceRef::operator<(const DiceRef &Other) const {
220   return DicePimpl < Other.DicePimpl;
221 }
222
223 inline error_code DiceRef::getNext(DiceRef &Result) const {
224   DataRefImpl Rel = DicePimpl;
225   const macho::DataInCodeTableEntry *P =
226     reinterpret_cast<const macho::DataInCodeTableEntry *>(Rel.p);
227   Rel.p = reinterpret_cast<uintptr_t>(P + 1);
228   Result = DiceRef(Rel, OwningObject);
229   return object_error::success;
230 }
231
232 // Since a Mach-O data in code reference, a DiceRef, can only be created when
233 // the OwningObject ObjectFile is a MachOObjectFile a static_cast<> is used for
234 // the methods that get the values of the fields of the reference.
235
236 inline error_code DiceRef::getOffset(uint32_t &Result) const {
237   const MachOObjectFile *MachOOF =
238     static_cast<const MachOObjectFile *>(OwningObject);
239   macho::DataInCodeTableEntry Dice = MachOOF->getDice(DicePimpl);
240   Result = Dice.Offset;
241   return object_error::success;
242 }
243
244 inline error_code DiceRef::getLength(uint16_t &Result) const {
245   const MachOObjectFile *MachOOF =
246     static_cast<const MachOObjectFile *>(OwningObject);
247   macho::DataInCodeTableEntry Dice = MachOOF->getDice(DicePimpl);
248   Result = Dice.Length;
249   return object_error::success;
250 }
251
252 inline error_code DiceRef::getKind(uint16_t &Result) const {
253   const MachOObjectFile *MachOOF =
254     static_cast<const MachOObjectFile *>(OwningObject);
255   macho::DataInCodeTableEntry Dice = MachOOF->getDice(DicePimpl);
256   Result = Dice.Kind;
257   return object_error::success;
258 }
259
260 inline DataRefImpl DiceRef::getRawDataRefImpl() const {
261   return DicePimpl;
262 }
263
264 inline const ObjectFile *DiceRef::getObjectFile() const {
265   return OwningObject;
266 }
267
268 }
269 }
270
271 #endif
272