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