LLVM API Documentation

 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
Object/MachO.h
Go to the documentation of this file.
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/ADT/Triple.h"
21 #include "llvm/Object/ObjectFile.h"
22 #include "llvm/Support/MachO.h"
23 
24 namespace llvm {
25 namespace object {
26 
27 /// DiceRef - This is a value type class that represents a single
28 /// data in code entry in the table in a Mach-O object file.
29 class DiceRef {
30  DataRefImpl DicePimpl;
31  const ObjectFile *OwningObject;
32 
33 public:
34  DiceRef() : OwningObject(NULL) { }
35 
36  DiceRef(DataRefImpl DiceP, const ObjectFile *Owner);
37 
38  bool operator==(const DiceRef &Other) const;
39  bool operator<(const DiceRef &Other) const;
40 
41  error_code getNext(DiceRef &Result) const;
42 
43  error_code getOffset(uint32_t &Result) const;
44  error_code getLength(uint16_t &Result) const;
45  error_code getKind(uint16_t &Result) const;
46 
48  const ObjectFile *getObjectFile() const;
49 };
51 
52 class MachOObjectFile : public ObjectFile {
53 public:
54  struct LoadCommandInfo {
55  const char *Ptr; // Where in memory the load command is.
56  MachO::load_command C; // The command itself.
57  };
58 
59  MachOObjectFile(MemoryBuffer *Object, bool IsLittleEndian, bool Is64Bits,
60  error_code &ec);
61 
62  virtual error_code getSymbolNext(DataRefImpl Symb, SymbolRef &Res) const;
63  virtual error_code getSymbolName(DataRefImpl Symb, StringRef &Res) const;
64  virtual error_code getSymbolAddress(DataRefImpl Symb, uint64_t &Res) const;
65  virtual error_code getSymbolFileOffset(DataRefImpl Symb, uint64_t &Res) const;
66  virtual error_code getSymbolAlignment(DataRefImpl Symb, uint32_t &Res) const;
67  virtual error_code getSymbolSize(DataRefImpl Symb, uint64_t &Res) const;
69  SymbolRef::Type &Res) const;
70  virtual error_code getSymbolFlags(DataRefImpl Symb, uint32_t &Res) const;
72  section_iterator &Res) const;
73  virtual error_code getSymbolValue(DataRefImpl Symb, uint64_t &Val) const;
74 
75  virtual error_code getSectionNext(DataRefImpl Sec, SectionRef &Res) const;
76  virtual error_code getSectionName(DataRefImpl Sec, StringRef &Res) const;
77  virtual error_code getSectionAddress(DataRefImpl Sec, uint64_t &Res) const;
78  virtual error_code getSectionSize(DataRefImpl Sec, uint64_t &Res) const;
79  virtual error_code getSectionContents(DataRefImpl Sec, StringRef &Res) const;
80  virtual error_code getSectionAlignment(DataRefImpl Sec, uint64_t &Res) const;
81  virtual error_code isSectionText(DataRefImpl Sec, bool &Res) const;
82  virtual error_code isSectionData(DataRefImpl Sec, bool &Res) const;
83  virtual error_code isSectionBSS(DataRefImpl Sec, bool &Res) const;
85  bool &Res) const;
86  virtual error_code isSectionVirtual(DataRefImpl Sec, bool &Res) const;
87  virtual error_code isSectionZeroInit(DataRefImpl Sec, bool &Res) const;
88  virtual error_code isSectionReadOnlyData(DataRefImpl Sec, bool &Res) const;
90  bool &Result) const;
93 
95  RelocationRef &Res) const;
96  virtual error_code getRelocationAddress(DataRefImpl Rel, uint64_t &Res) const;
97  virtual error_code getRelocationOffset(DataRefImpl Rel, uint64_t &Res) const;
99  virtual error_code getRelocationType(DataRefImpl Rel, uint64_t &Res) const;
101  SmallVectorImpl<char> &Result) const;
103  SmallVectorImpl<char> &Result) const;
104  virtual error_code getRelocationHidden(DataRefImpl Rel, bool &Result) const;
105 
106  virtual error_code getLibraryNext(DataRefImpl LibData, LibraryRef &Res) const;
107  virtual error_code getLibraryPath(DataRefImpl LibData, StringRef &Res) const;
108 
109  // TODO: Would be useful to have an iterator based version
110  // of the load command interface too.
111 
112  virtual symbol_iterator begin_symbols() const;
113  virtual symbol_iterator end_symbols() const;
114 
115  virtual symbol_iterator begin_dynamic_symbols() const;
116  virtual symbol_iterator end_dynamic_symbols() const;
117 
118  virtual section_iterator begin_sections() const;
119  virtual section_iterator end_sections() const;
120 
122  virtual library_iterator end_libraries_needed() const;
123 
124  virtual uint8_t getBytesInAddress() const;
125 
126  virtual StringRef getFileFormatName() const;
127  virtual unsigned getArch() const;
128 
129  virtual StringRef getLoadName() const;
130 
131  relocation_iterator section_rel_begin(unsigned Index) const;
132  relocation_iterator section_rel_end(unsigned Index) const;
133 
134  dice_iterator begin_dices() const;
135  dice_iterator end_dices() const;
136 
137  // In a MachO file, sections have a segment name. This is used in the .o
138  // files. They have a single segment, but this field specifies which segment
139  // a section should be put in in the final object.
141 
142  // Names are stored as 16 bytes. These returns the raw 16 bytes without
143  // interpreting them as a C string.
146 
147  // MachO specific Info about relocations.
150  const MachO::any_relocation_info &RE) const;
153  const MachO::any_relocation_info &RE) const;
155  const MachO::any_relocation_info &RE) const;
156  unsigned getAnyRelocationAddress(const MachO::any_relocation_info &RE) const;
157  unsigned getAnyRelocationPCRel(const MachO::any_relocation_info &RE) const;
158  unsigned getAnyRelocationLength(const MachO::any_relocation_info &RE) const;
159  unsigned getAnyRelocationType(const MachO::any_relocation_info &RE) const;
161 
162  // Walk load commands.
165 
166  // MachO specific structures.
169  MachO::section getSection(const LoadCommandInfo &L, unsigned Index) const;
170  MachO::section_64 getSection64(const LoadCommandInfo &L,unsigned Index) const;
173 
177  getSegmentLoadCommand(const LoadCommandInfo &L) const;
182 
187  uint32_t
189  unsigned Index) const;
191  unsigned Index) const;
195 
197  bool is64Bit() const;
198  void ReadULEB128s(uint64_t Index, SmallVectorImpl<uint64_t> &Out) const;
199 
200  static Triple::ArchType getArch(uint32_t CPUType);
201 
202  static bool classof(const Binary *v) {
203  return v->isMachO();
204  }
205 
206 private:
207  typedef SmallVector<const char*, 1> SectionList;
208  SectionList Sections;
209  const char *SymtabLoadCmd;
210  const char *DysymtabLoadCmd;
211  const char *DataInCodeLoadCmd;
212 };
213 
214 /// DiceRef
215 inline DiceRef::DiceRef(DataRefImpl DiceP, const ObjectFile *Owner)
216  : DicePimpl(DiceP) , OwningObject(Owner) {}
217 
218 inline bool DiceRef::operator==(const DiceRef &Other) const {
219  return DicePimpl == Other.DicePimpl;
220 }
221 
222 inline bool DiceRef::operator<(const DiceRef &Other) const {
223  return DicePimpl < Other.DicePimpl;
224 }
225 
226 inline error_code DiceRef::getNext(DiceRef &Result) const {
227  DataRefImpl Rel = DicePimpl;
229  reinterpret_cast<const MachO::data_in_code_entry *>(Rel.p);
230  Rel.p = reinterpret_cast<uintptr_t>(P + 1);
231  Result = DiceRef(Rel, OwningObject);
232  return object_error::success;
233 }
234 
235 // Since a Mach-O data in code reference, a DiceRef, can only be created when
236 // the OwningObject ObjectFile is a MachOObjectFile a static_cast<> is used for
237 // the methods that get the values of the fields of the reference.
238 
239 inline error_code DiceRef::getOffset(uint32_t &Result) const {
240  const MachOObjectFile *MachOOF =
241  static_cast<const MachOObjectFile *>(OwningObject);
242  MachO::data_in_code_entry Dice = MachOOF->getDice(DicePimpl);
243  Result = Dice.offset;
244  return object_error::success;
245 }
246 
247 inline error_code DiceRef::getLength(uint16_t &Result) const {
248  const MachOObjectFile *MachOOF =
249  static_cast<const MachOObjectFile *>(OwningObject);
250  MachO::data_in_code_entry Dice = MachOOF->getDice(DicePimpl);
251  Result = Dice.length;
252  return object_error::success;
253 }
254 
255 inline error_code DiceRef::getKind(uint16_t &Result) const {
256  const MachOObjectFile *MachOOF =
257  static_cast<const MachOObjectFile *>(OwningObject);
258  MachO::data_in_code_entry Dice = MachOOF->getDice(DicePimpl);
259  Result = Dice.kind;
260  return object_error::success;
261 }
262 
264  return DicePimpl;
265 }
266 
267 inline const ObjectFile *DiceRef::getObjectFile() const {
268  return OwningObject;
269 }
270 
271 }
272 }
273 
274 #endif
275 
error_code getOffset(uint32_t &Result) const
Definition: Object/MachO.h:239
virtual error_code getSymbolAddress(DataRefImpl Symb, uint64_t &Res) const
dice_iterator end_dices() const
ArrayRef< char > getSectionRawFinalSegmentName(DataRefImpl Sec) const
StringRef getStringTableData() const
dice_iterator begin_dices() const
virtual error_code isSectionRequiredForExecution(DataRefImpl Sec, bool &Res) const
virtual error_code getSymbolSize(DataRefImpl Symb, uint64_t &Res) const
virtual error_code getSymbolSection(DataRefImpl Symb, section_iterator &Res) const
MachO::nlist getSymbolTableEntry(DataRefImpl DRI) const
uint32_t getIndirectSymbolTableEntry(const MachO::dysymtab_command &DLC, unsigned Index) const
MachO::symtab_command getSymtabLoadCommand() const
virtual relocation_iterator section_rel_end(DataRefImpl Sec) const
MachO::linker_options_command getLinkerOptionsLoadCommand(const LoadCommandInfo &L) const
virtual symbol_iterator begin_dynamic_symbols() const
virtual StringRef getFileFormatName() const
MachO::any_relocation_info getRelocation(DataRefImpl Rel) const
virtual section_iterator end_sections() const
virtual library_iterator end_libraries_needed() const
LoadCommandInfo getNextLoadCommandInfo(const LoadCommandInfo &L) const
virtual error_code getSectionAlignment(DataRefImpl Sec, uint64_t &Res) const
DataRefImpl getRawDataRefImpl() const
Definition: Object/MachO.h:263
error_code getLength(uint16_t &Result) const
Definition: Object/MachO.h:247
MachO::linkedit_data_command getLinkeditDataLoadCommand(const LoadCommandInfo &L) const
uint16_t length
virtual section_iterator begin_sections() const
MachO::nlist_64 getSymbol64TableEntry(DataRefImpl DRI) const
virtual error_code getSymbolFileOffset(DataRefImpl Symb, uint64_t &Res) const
MachO::mach_header getHeader() const
virtual StringRef getLoadName() const
SectionRef getRelocationSection(const MachO::any_relocation_info &RE) const
virtual error_code getSymbolType(DataRefImpl Symb, SymbolRef::Type &Res) const
bool getScatteredRelocationScattered(const MachO::any_relocation_info &RE) const
virtual error_code getLibraryNext(DataRefImpl LibData, LibraryRef &Res) const
virtual library_iterator begin_libraries_needed() const
virtual error_code getRelocationTypeName(DataRefImpl Rel, SmallVectorImpl< char > &Result) const
MachO::section_64 getSection64(DataRefImpl DRI) const
virtual error_code getSymbolValue(DataRefImpl Symb, uint64_t &Val) const
virtual error_code getRelocationAddress(DataRefImpl Rel, uint64_t &Res) const
virtual error_code isSectionVirtual(DataRefImpl Sec, bool &Res) const
#define P(N)
MachO::data_in_code_entry getDataInCodeTableEntry(uint32_t DataOffset, unsigned Index) const
MachO::section getSection(DataRefImpl DRI) const
unsigned getAnyRelocationLength(const MachO::any_relocation_info &RE) const
error_code getNext(DiceRef &Result) const
Definition: Object/MachO.h:226
uint32_t offset
virtual error_code getSymbolFlags(DataRefImpl Symb, uint32_t &Res) const
MachO::mach_header_64 getHeader64() const
unsigned getAnyRelocationAddress(const MachO::any_relocation_info &RE) const
virtual error_code getRelocationType(DataRefImpl Rel, uint64_t &Res) const
virtual error_code getRelocationHidden(DataRefImpl Rel, bool &Result) const
virtual error_code getRelocationNext(DataRefImpl Rel, RelocationRef &Res) const
MachO::dysymtab_command getDysymtabLoadCommand() const
bool operator==(const DiceRef &Other) const
Definition: Object/MachO.h:218
virtual error_code isSectionText(DataRefImpl Sec, bool &Res) const
bool isRelocationScattered(const MachO::any_relocation_info &RE) const
virtual error_code getSectionContents(DataRefImpl Sec, StringRef &Res) const
uint16_t kind
MachOObjectFile(MemoryBuffer *Object, bool IsLittleEndian, bool Is64Bits, error_code &ec)
virtual error_code getSymbolName(DataRefImpl Symb, StringRef &Res) const
virtual unsigned getArch() const
MachO::linkedit_data_command getDataInCodeLoadCommand() const
virtual symbol_iterator begin_symbols() const
virtual error_code getSymbolAlignment(DataRefImpl Symb, uint32_t &Res) const
content_iterator< DiceRef > dice_iterator
Definition: Object/MachO.h:50
bool isMachO() const
Definition: Binary.h:99
virtual symbol_iterator end_symbols() const
virtual error_code getSectionSize(DataRefImpl Sec, uint64_t &Res) const
unsigned getAnyRelocationPCRel(const MachO::any_relocation_info &RE) const
virtual uint8_t getBytesInAddress() const
The number of bytes used to represent an address in this object file format.
uint32_t getScatteredRelocationValue(const MachO::any_relocation_info &RE) const
virtual symbol_iterator end_dynamic_symbols() const
LoadCommandInfo getFirstLoadCommandInfo() const
virtual error_code sectionContainsSymbol(DataRefImpl Sec, DataRefImpl Symb, bool &Result) const
virtual error_code getRelocationOffset(DataRefImpl Rel, uint64_t &Res) const
bool operator<(const DiceRef &Other) const
Definition: Object/MachO.h:222
virtual relocation_iterator section_rel_begin(DataRefImpl Sec) const
virtual error_code isSectionReadOnlyData(DataRefImpl Sec, bool &Res) const
virtual error_code isSectionData(DataRefImpl Sec, bool &Res) const
unsigned getPlainRelocationSymbolNum(const MachO::any_relocation_info &RE) const
virtual symbol_iterator getRelocationSymbol(DataRefImpl Rel) const
virtual error_code isSectionZeroInit(DataRefImpl Sec, bool &Res) const
virtual error_code getSectionAddress(DataRefImpl Sec, uint64_t &Res) const
virtual error_code getSymbolNext(DataRefImpl Symb, SymbolRef &Res) const
MachO::data_in_code_entry getDice(DataRefImpl Rel) const
unsigned getAnyRelocationType(const MachO::any_relocation_info &RE) const
error_code getKind(uint16_t &Result) const
Definition: Object/MachO.h:255
MachO::segment_command getSegmentLoadCommand(const LoadCommandInfo &L) const
MachO::segment_command_64 getSegment64LoadCommand(const LoadCommandInfo &L) const
ArrayRef< char > getSectionRawName(DataRefImpl Sec) const
const ObjectFile * getObjectFile() const
Definition: Object/MachO.h:267
virtual error_code getLibraryPath(DataRefImpl LibData, StringRef &Res) const
bool getPlainRelocationExternal(const MachO::any_relocation_info &RE) const
StringRef getSectionFinalSegmentName(DataRefImpl Sec) const
void ReadULEB128s(uint64_t Index, SmallVectorImpl< uint64_t > &Out) const
static bool classof(const Binary *v)
Definition: Object/MachO.h:202
virtual error_code getSectionNext(DataRefImpl Sec, SectionRef &Res) const
virtual error_code getSectionName(DataRefImpl Sec, StringRef &Res) const
virtual error_code getRelocationValueString(DataRefImpl Rel, SmallVectorImpl< char > &Result) const
virtual error_code isSectionBSS(DataRefImpl Sec, bool &Res) const