1 //===- MachO.h - MachO object file implementation ---------------*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This file declares the MachOObjectFile class, which binds the MachOObject
11 // class to the generic ObjectFile wrapper.
13 //===----------------------------------------------------------------------===//
15 #ifndef LLVM_OBJECT_MACHO_H
16 #define LLVM_OBJECT_MACHO_H
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"
29 namespace MachOFormat {
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;
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;
59 struct RelocationEntry {
60 support::ulittle32_t Word0;
61 support::ulittle32_t Word1;
64 struct SymbolTableEntry {
65 support::ulittle32_t StringIndex;
68 support::ulittle16_t Flags;
69 support::ulittle32_t Value;
74 typedef MachOObject::LoadCommandInfo LoadCommandInfo;
76 class MachOObjectFile : public ObjectFile {
78 MachOObjectFile(MemoryBuffer *Object, MachOObject *MOO, error_code &ec);
80 virtual symbol_iterator begin_symbols() const;
81 virtual symbol_iterator end_symbols() const;
82 virtual symbol_iterator begin_dynamic_symbols() const;
83 virtual symbol_iterator end_dynamic_symbols() const;
84 virtual library_iterator begin_libraries_needed() const;
85 virtual library_iterator end_libraries_needed() const;
86 virtual section_iterator begin_sections() const;
87 virtual section_iterator end_sections() const;
89 virtual uint8_t getBytesInAddress() const;
90 virtual StringRef getFileFormatName() const;
91 virtual unsigned getArch() const;
92 virtual StringRef getLoadName() const;
94 // In a MachO file, sections have a segment name. This is used in the .o
95 // files. They have a single segment, but this field specifies which segment
96 // a section should be put in in the final object.
97 StringRef getSectionFinalSegmentName(DataRefImpl Sec) const;
99 // Names are stored as 16 bytes. These returns the raw 16 bytes without
100 // interpreting them as a C string.
101 ArrayRef<char> getSectionRawName(DataRefImpl Sec) const;
102 ArrayRef<char>getSectionRawFinalSegmentName(DataRefImpl Sec) const;
104 MachOObject *getObject() { return MachOObj.get(); }
106 static inline bool classof(const Binary *v) {
111 virtual error_code getSymbolNext(DataRefImpl Symb, SymbolRef &Res) const;
112 virtual error_code getSymbolName(DataRefImpl Symb, StringRef &Res) const;
113 virtual error_code getSymbolFileOffset(DataRefImpl Symb, uint64_t &Res) const;
114 virtual error_code getSymbolAddress(DataRefImpl Symb, uint64_t &Res) const;
115 virtual error_code getSymbolSize(DataRefImpl Symb, uint64_t &Res) const;
116 virtual error_code getSymbolNMTypeChar(DataRefImpl Symb, char &Res) const;
117 virtual error_code getSymbolFlags(DataRefImpl Symb, uint32_t &Res) const;
118 virtual error_code getSymbolType(DataRefImpl Symb, SymbolRef::Type &Res) const;
119 virtual error_code getSymbolSection(DataRefImpl Symb,
120 section_iterator &Res) const;
121 virtual error_code getSymbolValue(DataRefImpl Symb, uint64_t &Val) const;
123 virtual error_code getSectionNext(DataRefImpl Sec, SectionRef &Res) const;
124 virtual error_code getSectionName(DataRefImpl Sec, StringRef &Res) const;
125 virtual error_code getSectionAddress(DataRefImpl Sec, uint64_t &Res) const;
126 virtual error_code getSectionSize(DataRefImpl Sec, uint64_t &Res) const;
127 virtual error_code getSectionContents(DataRefImpl Sec, StringRef &Res) const;
128 virtual error_code getSectionAlignment(DataRefImpl Sec, uint64_t &Res) const;
129 virtual error_code isSectionText(DataRefImpl Sec, bool &Res) const;
130 virtual error_code isSectionData(DataRefImpl Sec, bool &Res) const;
131 virtual error_code isSectionBSS(DataRefImpl Sec, bool &Res) const;
132 virtual error_code isSectionRequiredForExecution(DataRefImpl Sec,
134 virtual error_code isSectionVirtual(DataRefImpl Sec, bool &Res) const;
135 virtual error_code isSectionZeroInit(DataRefImpl Sec, bool &Res) const;
136 virtual error_code isSectionReadOnlyData(DataRefImpl Sec, bool &Res) const;
137 virtual error_code sectionContainsSymbol(DataRefImpl DRI, DataRefImpl S,
139 virtual relocation_iterator getSectionRelBegin(DataRefImpl Sec) const;
140 virtual relocation_iterator getSectionRelEnd(DataRefImpl Sec) const;
142 virtual error_code getRelocationNext(DataRefImpl Rel,
143 RelocationRef &Res) const;
144 virtual error_code getRelocationAddress(DataRefImpl Rel,
145 uint64_t &Res) const;
146 virtual error_code getRelocationOffset(DataRefImpl Rel,
147 uint64_t &Res) const;
148 virtual error_code getRelocationSymbol(DataRefImpl Rel,
149 SymbolRef &Res) const;
150 virtual error_code getRelocationType(DataRefImpl Rel,
151 uint64_t &Res) const;
152 virtual error_code getRelocationTypeName(DataRefImpl Rel,
153 SmallVectorImpl<char> &Result) const;
154 virtual error_code getRelocationAdditionalInfo(DataRefImpl Rel,
156 virtual error_code getRelocationValueString(DataRefImpl Rel,
157 SmallVectorImpl<char> &Result) const;
158 virtual error_code getRelocationHidden(DataRefImpl Rel, bool &Result) const;
160 virtual error_code getLibraryNext(DataRefImpl LibData, LibraryRef &Res) const;
161 virtual error_code getLibraryPath(DataRefImpl LibData, StringRef &Res) const;
164 OwningPtr<MachOObject> MachOObj;
165 mutable uint32_t RegisteredStringTable;
166 typedef SmallVector<DataRefImpl, 1> SectionList;
167 SectionList Sections;
170 void moveToNextSection(DataRefImpl &DRI) const;
171 const MachOFormat::SymbolTableEntry *
172 getSymbolTableEntry(DataRefImpl DRI) const;
173 void getSymbol64TableEntry(DataRefImpl DRI,
174 InMemoryStruct<macho::Symbol64TableEntry> &Res) const;
175 void moveToNextSymbol(DataRefImpl &DRI) const;
176 const MachOFormat::Section *getSection(DataRefImpl DRI) const;
177 const MachOFormat::Section64 *getSection64(DataRefImpl DRI) const;
178 const MachOFormat::RelocationEntry *getRelocation(DataRefImpl Rel) const;
179 std::size_t getSectionIndex(DataRefImpl Sec) const;
181 void printRelocationTargetName(const MachOFormat::RelocationEntry *RE,
182 raw_string_ostream &fmt) const;