Use llvm/Object/MachO.h in macho-dumper. Drop the old macho parser.
[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 class MachOObjectFile : public ObjectFile {
29 public:
30   struct LoadCommandInfo {
31     const char *Ptr;      // Where in memory the load command is.
32     macho::LoadCommand C; // The command itself.
33   };
34
35   MachOObjectFile(MemoryBuffer *Object, bool IsLittleEndian, bool Is64Bits,
36                   error_code &ec);
37
38   virtual error_code getSymbolNext(DataRefImpl Symb, SymbolRef &Res) const;
39   virtual error_code getSymbolName(DataRefImpl Symb, StringRef &Res) const;
40   virtual error_code getSymbolAddress(DataRefImpl Symb, uint64_t &Res) const;
41   virtual error_code getSymbolFileOffset(DataRefImpl Symb, uint64_t &Res) const;
42   virtual error_code getSymbolSize(DataRefImpl Symb, uint64_t &Res) const;
43   virtual error_code getSymbolType(DataRefImpl Symb,
44                                    SymbolRef::Type &Res) const;
45   virtual error_code getSymbolNMTypeChar(DataRefImpl Symb, char &Res) const;
46   virtual error_code getSymbolFlags(DataRefImpl Symb, uint32_t &Res) const;
47   virtual error_code getSymbolSection(DataRefImpl Symb,
48                                       section_iterator &Res) const;
49   virtual error_code getSymbolValue(DataRefImpl Symb, uint64_t &Val) const;
50
51   virtual error_code getSectionNext(DataRefImpl Sec, SectionRef &Res) const;
52   virtual error_code getSectionName(DataRefImpl Sec, StringRef &Res) const;
53   virtual error_code getSectionAddress(DataRefImpl Sec, uint64_t &Res) const;
54   virtual error_code getSectionSize(DataRefImpl Sec, uint64_t &Res) const;
55   virtual error_code getSectionContents(DataRefImpl Sec, StringRef &Res) const;
56   virtual error_code getSectionAlignment(DataRefImpl Sec, uint64_t &Res) const;
57   virtual error_code isSectionText(DataRefImpl Sec, bool &Res) const;
58   virtual error_code isSectionData(DataRefImpl Sec, bool &Res) const;
59   virtual error_code isSectionBSS(DataRefImpl Sec, bool &Res) const;
60   virtual error_code isSectionRequiredForExecution(DataRefImpl Sec,
61                                                    bool &Res) const;
62   virtual error_code isSectionVirtual(DataRefImpl Sec, bool &Res) const;
63   virtual error_code isSectionZeroInit(DataRefImpl Sec, bool &Res) const;
64   virtual error_code isSectionReadOnlyData(DataRefImpl Sec, bool &Res) const;
65   virtual error_code sectionContainsSymbol(DataRefImpl Sec, DataRefImpl Symb,
66                                            bool &Result) const;
67   virtual relocation_iterator getSectionRelBegin(DataRefImpl Sec) const;
68   virtual relocation_iterator getSectionRelEnd(DataRefImpl Sec) const;
69
70   virtual error_code getRelocationNext(DataRefImpl Rel,
71                                        RelocationRef &Res) const;
72   virtual error_code getRelocationAddress(DataRefImpl Rel, uint64_t &Res) const;
73   virtual error_code getRelocationOffset(DataRefImpl Rel, uint64_t &Res) const;
74   virtual error_code getRelocationSymbol(DataRefImpl Rel, SymbolRef &Res) const;
75   virtual error_code getRelocationType(DataRefImpl Rel, uint64_t &Res) const;
76   virtual error_code getRelocationTypeName(DataRefImpl Rel,
77                                            SmallVectorImpl<char> &Result) const;
78   virtual error_code getRelocationAdditionalInfo(DataRefImpl Rel,
79                                                  int64_t &Res) const;
80   virtual error_code getRelocationValueString(DataRefImpl Rel,
81                                            SmallVectorImpl<char> &Result) const;
82   virtual error_code getRelocationHidden(DataRefImpl Rel, bool &Result) const;
83
84   virtual error_code getLibraryNext(DataRefImpl LibData, LibraryRef &Res) const;
85   virtual error_code getLibraryPath(DataRefImpl LibData, StringRef &Res) const;
86
87   // TODO: Would be useful to have an iterator based version
88   // of the load command interface too.
89
90   virtual symbol_iterator begin_symbols() const;
91   virtual symbol_iterator end_symbols() const;
92
93   virtual symbol_iterator begin_dynamic_symbols() const;
94   virtual symbol_iterator end_dynamic_symbols() const;
95
96   virtual section_iterator begin_sections() const;
97   virtual section_iterator end_sections() const;
98
99   virtual library_iterator begin_libraries_needed() const;
100   virtual library_iterator end_libraries_needed() const;
101
102   virtual uint8_t getBytesInAddress() const;
103
104   virtual StringRef getFileFormatName() const;
105   virtual unsigned getArch() const;
106
107   virtual StringRef getLoadName() const;
108
109   relocation_iterator getSectionRelBegin(unsigned Index) const;
110   relocation_iterator getSectionRelEnd(unsigned Index) const;
111
112   // In a MachO file, sections have a segment name. This is used in the .o
113   // files. They have a single segment, but this field specifies which segment
114   // a section should be put in in the final object.
115   StringRef getSectionFinalSegmentName(DataRefImpl Sec) const;
116
117   // Names are stored as 16 bytes. These returns the raw 16 bytes without
118   // interpreting them as a C string.
119   ArrayRef<char> getSectionRawName(DataRefImpl Sec) const;
120   ArrayRef<char> getSectionRawFinalSegmentName(DataRefImpl Sec) const;
121
122   // MachO specific Info about relocations.
123   bool isRelocationScattered(const macho::RelocationEntry &RE) const;
124   unsigned getPlainRelocationSymbolNum(const macho::RelocationEntry &RE) const;
125   bool getPlainRelocationExternal(const macho::RelocationEntry &RE) const;
126   bool getScatteredRelocationScattered(const macho::RelocationEntry &RE) const;
127   uint32_t getScatteredRelocationValue(const macho::RelocationEntry &RE) const;
128   unsigned getAnyRelocationAddress(const macho::RelocationEntry &RE) const;
129   unsigned getAnyRelocationPCRel(const macho::RelocationEntry &RE) const;
130   unsigned getAnyRelocationLength(const macho::RelocationEntry &RE) const;
131   unsigned getAnyRelocationType(const macho::RelocationEntry &RE) const;
132
133   // Walk load commands.
134   LoadCommandInfo getFirstLoadCommandInfo() const;
135   LoadCommandInfo getNextLoadCommandInfo(const LoadCommandInfo &L) const;
136
137   // MachO specific structures.
138   macho::Section getSection(DataRefImpl DRI) const;
139   macho::Section64 getSection64(DataRefImpl DRI) const;
140   macho::Section getSection(const LoadCommandInfo &L, unsigned Index) const;
141   macho::Section64 getSection64(const LoadCommandInfo &L, unsigned Index) const;
142   macho::SymbolTableEntry getSymbolTableEntry(DataRefImpl DRI) const;
143   macho::Symbol64TableEntry getSymbol64TableEntry(DataRefImpl DRI) const;
144
145   macho::LinkeditDataLoadCommand
146   getLinkeditDataLoadCommand(const LoadCommandInfo &L) const;
147   macho::SegmentLoadCommand
148   getSegmentLoadCommand(const LoadCommandInfo &L) const;
149   macho::Segment64LoadCommand
150   getSegment64LoadCommand(const LoadCommandInfo &L) const;
151   macho::LinkerOptionsLoadCommand
152   getLinkerOptionsLoadCommand(const LoadCommandInfo &L) const;
153
154   macho::RelocationEntry getRelocation(DataRefImpl Rel) const;
155   macho::Header getHeader() const;
156   macho::Header64Ext getHeader64Ext() const;
157   macho::IndirectSymbolTableEntry
158   getIndirectSymbolTableEntry(const macho::DysymtabLoadCommand &DLC,
159                               unsigned Index) const;
160   macho::DataInCodeTableEntry getDataInCodeTableEntry(uint32_t DataOffset,
161                                                       unsigned Index) const;
162   macho::SymtabLoadCommand getSymtabLoadCommand() const;
163   macho::DysymtabLoadCommand getDysymtabLoadCommand() const;
164
165   StringRef getStringTableData() const;
166   bool is64Bit() const;
167   void ReadULEB128s(uint64_t Index, SmallVectorImpl<uint64_t> &Out) const;
168
169   static bool classof(const Binary *v) {
170     return v->isMachO();
171   }
172
173 private:
174   typedef SmallVector<const char*, 1> SectionList;
175   SectionList Sections;
176   const char *SymtabLoadCmd;
177   const char *DysymtabLoadCmd;
178 };
179
180 }
181 }
182
183 #endif
184