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;
65 typedef MachOObject::LoadCommandInfo LoadCommandInfo;
67 class MachOObjectFile : public ObjectFile {
69 MachOObjectFile(MemoryBuffer *Object, MachOObject *MOO, error_code &ec);
71 virtual symbol_iterator begin_symbols() const;
72 virtual symbol_iterator end_symbols() const;
73 virtual symbol_iterator begin_dynamic_symbols() const;
74 virtual symbol_iterator end_dynamic_symbols() const;
75 virtual library_iterator begin_libraries_needed() const;
76 virtual library_iterator end_libraries_needed() const;
77 virtual section_iterator begin_sections() const;
78 virtual section_iterator end_sections() const;
80 virtual uint8_t getBytesInAddress() const;
81 virtual StringRef getFileFormatName() const;
82 virtual unsigned getArch() const;
83 virtual StringRef getLoadName() const;
85 // In a MachO file, sections have a segment name. This is used in the .o
86 // files. They have a single segment, but this field specifies which segment
87 // a section should be put in in the final object.
88 StringRef getSectionFinalSegmentName(DataRefImpl Sec) const;
90 // Names are stored as 16 bytes. These returns the raw 16 bytes without
91 // interpreting them as a C string.
92 ArrayRef<char> getSectionRawName(DataRefImpl Sec) const;
93 ArrayRef<char>getSectionRawFinalSegmentName(DataRefImpl Sec) const;
95 MachOObject *getObject() { return MachOObj.get(); }
97 static inline bool classof(const Binary *v) {
102 virtual error_code getSymbolNext(DataRefImpl Symb, SymbolRef &Res) const;
103 virtual error_code getSymbolName(DataRefImpl Symb, StringRef &Res) const;
104 virtual error_code getSymbolFileOffset(DataRefImpl Symb, uint64_t &Res) const;
105 virtual error_code getSymbolAddress(DataRefImpl Symb, uint64_t &Res) const;
106 virtual error_code getSymbolSize(DataRefImpl Symb, uint64_t &Res) const;
107 virtual error_code getSymbolNMTypeChar(DataRefImpl Symb, char &Res) const;
108 virtual error_code getSymbolFlags(DataRefImpl Symb, uint32_t &Res) const;
109 virtual error_code getSymbolType(DataRefImpl Symb, SymbolRef::Type &Res) const;
110 virtual error_code getSymbolSection(DataRefImpl Symb,
111 section_iterator &Res) const;
112 virtual error_code getSymbolValue(DataRefImpl Symb, uint64_t &Val) const;
114 virtual error_code getSectionNext(DataRefImpl Sec, SectionRef &Res) const;
115 virtual error_code getSectionName(DataRefImpl Sec, StringRef &Res) const;
116 virtual error_code getSectionAddress(DataRefImpl Sec, uint64_t &Res) const;
117 virtual error_code getSectionSize(DataRefImpl Sec, uint64_t &Res) const;
118 virtual error_code getSectionContents(DataRefImpl Sec, StringRef &Res) const;
119 virtual error_code getSectionAlignment(DataRefImpl Sec, uint64_t &Res) const;
120 virtual error_code isSectionText(DataRefImpl Sec, bool &Res) const;
121 virtual error_code isSectionData(DataRefImpl Sec, bool &Res) const;
122 virtual error_code isSectionBSS(DataRefImpl Sec, bool &Res) const;
123 virtual error_code isSectionRequiredForExecution(DataRefImpl Sec,
125 virtual error_code isSectionVirtual(DataRefImpl Sec, bool &Res) const;
126 virtual error_code isSectionZeroInit(DataRefImpl Sec, bool &Res) const;
127 virtual error_code isSectionReadOnlyData(DataRefImpl Sec, bool &Res) const;
128 virtual error_code sectionContainsSymbol(DataRefImpl DRI, DataRefImpl S,
130 virtual relocation_iterator getSectionRelBegin(DataRefImpl Sec) const;
131 virtual relocation_iterator getSectionRelEnd(DataRefImpl Sec) const;
133 virtual error_code getRelocationNext(DataRefImpl Rel,
134 RelocationRef &Res) const;
135 virtual error_code getRelocationAddress(DataRefImpl Rel,
136 uint64_t &Res) const;
137 virtual error_code getRelocationOffset(DataRefImpl Rel,
138 uint64_t &Res) const;
139 virtual error_code getRelocationSymbol(DataRefImpl Rel,
140 SymbolRef &Res) const;
141 virtual error_code getRelocationType(DataRefImpl Rel,
142 uint64_t &Res) const;
143 virtual error_code getRelocationTypeName(DataRefImpl Rel,
144 SmallVectorImpl<char> &Result) const;
145 virtual error_code getRelocationAdditionalInfo(DataRefImpl Rel,
147 virtual error_code getRelocationValueString(DataRefImpl Rel,
148 SmallVectorImpl<char> &Result) const;
149 virtual error_code getRelocationHidden(DataRefImpl Rel, bool &Result) const;
151 virtual error_code getLibraryNext(DataRefImpl LibData, LibraryRef &Res) const;
152 virtual error_code getLibraryPath(DataRefImpl LibData, StringRef &Res) const;
155 OwningPtr<MachOObject> MachOObj;
156 mutable uint32_t RegisteredStringTable;
157 typedef SmallVector<DataRefImpl, 1> SectionList;
158 SectionList Sections;
161 void moveToNextSection(DataRefImpl &DRI) const;
162 void getSymbolTableEntry(DataRefImpl DRI,
163 InMemoryStruct<macho::SymbolTableEntry> &Res) const;
164 void getSymbol64TableEntry(DataRefImpl DRI,
165 InMemoryStruct<macho::Symbol64TableEntry> &Res) const;
166 void moveToNextSymbol(DataRefImpl &DRI) const;
167 const MachOFormat::Section *getSection(DataRefImpl DRI) const;
168 const MachOFormat::Section64 *getSection64(DataRefImpl DRI) const;
169 const MachOFormat::RelocationEntry *getRelocation(DataRefImpl Rel) const;
170 std::size_t getSectionIndex(DataRefImpl Sec) const;
172 void printRelocationTargetName(const MachOFormat::RelocationEntry *RE,
173 raw_string_ostream &fmt) const;