1 //===- MachOObject.cpp - Mach-O Object File Wrapper -----------------------===//
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 #include "llvm/Object/MachOObject.h"
11 #include "llvm/ADT/StringRef.h"
12 #include "llvm/Support/MemoryBuffer.h"
13 #include "llvm/System/Host.h"
14 #include "llvm/System/SwapByteOrder.h"
17 using namespace llvm::object;
19 /* Translation Utilities */
22 static void SwapValue(T &Value) {
23 Value = sys::SwapByteOrder(Value);
27 static void SwapStruct(T &Value);
30 static void ReadInMemoryStruct(const MachOObject &MOO,
31 StringRef Buffer, uint64_t Base,
32 InMemoryStruct<T> &Res) {
33 typedef T struct_type;
34 uint64_t Size = sizeof(struct_type);
36 // Check that the buffer contains the expected data.
37 if (Base + Size > Buffer.size()) {
42 // Check whether we can return a direct pointer.
43 struct_type *Ptr = (struct_type *) (Buffer.data() + Base);
44 if (!MOO.isSwappedEndian()) {
49 // Otherwise, copy the struct and translate the values.
56 MachOObject::MachOObject(MemoryBuffer *Buffer_, bool IsLittleEndian_,
58 : Buffer(Buffer_), IsLittleEndian(IsLittleEndian_), Is64Bit(Is64Bit_),
59 IsSwappedEndian(IsLittleEndian != sys::isLittleEndianHost()),
60 HasStringTable(false), LoadCommands(0), NumLoadedCommands(0) {
61 // Load the common header.
62 memcpy(&Header, Buffer->getBuffer().data(), sizeof(Header));
63 if (IsSwappedEndian) {
64 SwapValue(Header.Magic);
65 SwapValue(Header.CPUType);
66 SwapValue(Header.CPUSubtype);
67 SwapValue(Header.FileType);
68 SwapValue(Header.NumLoadCommands);
69 SwapValue(Header.SizeOfLoadCommands);
70 SwapValue(Header.Flags);
74 memcpy(&Header64Ext, Buffer->getBuffer().data() + sizeof(Header),
76 if (IsSwappedEndian) {
77 SwapValue(Header64Ext.Reserved);
81 // Create the load command array if sane.
82 if (getHeader().NumLoadCommands < (1 << 20))
83 LoadCommands = new LoadCommandInfo[getHeader().NumLoadCommands];
86 MachOObject::~MachOObject() {
90 MachOObject *MachOObject::LoadFromBuffer(MemoryBuffer *Buffer,
91 std::string *ErrorStr) {
92 // First, check the magic value and initialize the basic object info.
93 bool IsLittleEndian = false, Is64Bit = false;
94 StringRef Magic = Buffer->getBuffer().slice(0, 4);
95 if (Magic == "\xFE\xED\xFA\xCE") {
96 } else if (Magic == "\xCE\xFA\xED\xFE") {
97 IsLittleEndian = true;
98 } else if (Magic == "\xFE\xED\xFA\xCF") {
100 } else if (Magic == "\xCF\xFA\xED\xFE") {
101 IsLittleEndian = true;
104 if (ErrorStr) *ErrorStr = "not a Mach object file (invalid magic)";
108 // Ensure that the at least the full header is present.
109 unsigned HeaderSize = Is64Bit ? macho::Header64Size : macho::Header32Size;
110 if (Buffer->getBufferSize() < HeaderSize) {
111 if (ErrorStr) *ErrorStr = "not a Mach object file (invalid header)";
115 OwningPtr<MachOObject> Object(new MachOObject(Buffer, IsLittleEndian,
118 // Check for bogus number of load commands.
119 if (Object->getHeader().NumLoadCommands >= (1 << 20)) {
120 if (ErrorStr) *ErrorStr = "not a Mach object file (unreasonable header)";
124 if (ErrorStr) *ErrorStr = "";
125 return Object.take();
128 void MachOObject::RegisterStringTable(macho::SymtabLoadCommand &SLC) {
129 HasStringTable = true;
130 StringTable = Buffer->getBuffer().substr(SLC.StringTableOffset,
131 SLC.StringTableSize);
134 const MachOObject::LoadCommandInfo &
135 MachOObject::getLoadCommandInfo(unsigned Index) const {
136 assert(Index < getHeader().NumLoadCommands && "Invalid index!");
138 // Load the command, if necessary.
139 if (Index >= NumLoadedCommands) {
142 Offset = getHeaderSize();
144 const LoadCommandInfo &Prev = getLoadCommandInfo(Index - 1);
145 Offset = Prev.Offset + Prev.Command.Size;
148 LoadCommandInfo &Info = LoadCommands[Index];
149 memcpy(&Info.Command, Buffer->getBuffer().data() + Offset,
150 sizeof(macho::LoadCommand));
151 if (IsSwappedEndian) {
152 SwapValue(Info.Command.Type);
153 SwapValue(Info.Command.Size);
155 Info.Offset = Offset;
156 NumLoadedCommands = Index + 1;
159 return LoadCommands[Index];
163 void SwapStruct(macho::SegmentLoadCommand &Value) {
164 SwapValue(Value.Type);
165 SwapValue(Value.Size);
166 SwapValue(Value.VMAddress);
167 SwapValue(Value.VMSize);
168 SwapValue(Value.FileOffset);
169 SwapValue(Value.FileSize);
170 SwapValue(Value.MaxVMProtection);
171 SwapValue(Value.InitialVMProtection);
172 SwapValue(Value.NumSections);
173 SwapValue(Value.Flags);
175 void MachOObject::ReadSegmentLoadCommand(const LoadCommandInfo &LCI,
176 InMemoryStruct<macho::SegmentLoadCommand> &Res) const {
177 ReadInMemoryStruct(*this, Buffer->getBuffer(), LCI.Offset, Res);
181 void SwapStruct(macho::Segment64LoadCommand &Value) {
182 SwapValue(Value.Type);
183 SwapValue(Value.Size);
184 SwapValue(Value.VMAddress);
185 SwapValue(Value.VMSize);
186 SwapValue(Value.FileOffset);
187 SwapValue(Value.FileSize);
188 SwapValue(Value.MaxVMProtection);
189 SwapValue(Value.InitialVMProtection);
190 SwapValue(Value.NumSections);
191 SwapValue(Value.Flags);
193 void MachOObject::ReadSegment64LoadCommand(const LoadCommandInfo &LCI,
194 InMemoryStruct<macho::Segment64LoadCommand> &Res) const {
195 ReadInMemoryStruct(*this, Buffer->getBuffer(), LCI.Offset, Res);
199 void SwapStruct(macho::SymtabLoadCommand &Value) {
200 SwapValue(Value.Type);
201 SwapValue(Value.Size);
202 SwapValue(Value.SymbolTableOffset);
203 SwapValue(Value.NumSymbolTableEntries);
204 SwapValue(Value.StringTableOffset);
205 SwapValue(Value.StringTableSize);
207 void MachOObject::ReadSymtabLoadCommand(const LoadCommandInfo &LCI,
208 InMemoryStruct<macho::SymtabLoadCommand> &Res) const {
209 ReadInMemoryStruct(*this, Buffer->getBuffer(), LCI.Offset, Res);
213 void SwapStruct(macho::DysymtabLoadCommand &Value) {
214 SwapValue(Value.Type);
215 SwapValue(Value.Size);
216 SwapValue(Value.LocalSymbolIndex);
217 SwapValue(Value.NumLocalSymbols);
218 SwapValue(Value.ExternalSymbolsIndex);
219 SwapValue(Value.NumExternalSymbols);
220 SwapValue(Value.UndefinedSymbolsIndex);
221 SwapValue(Value.NumUndefinedSymbols);
222 SwapValue(Value.TOCOffset);
223 SwapValue(Value.NumTOCEntries);
224 SwapValue(Value.ModuleTableOffset);
225 SwapValue(Value.NumModuleTableEntries);
226 SwapValue(Value.ReferenceSymbolTableOffset);
227 SwapValue(Value.NumReferencedSymbolTableEntries);
228 SwapValue(Value.IndirectSymbolTableOffset);
229 SwapValue(Value.NumIndirectSymbolTableEntries);
230 SwapValue(Value.ExternalRelocationTableOffset);
231 SwapValue(Value.NumExternalRelocationTableEntries);
232 SwapValue(Value.LocalRelocationTableOffset);
233 SwapValue(Value.NumLocalRelocationTableEntries);
235 void MachOObject::ReadDysymtabLoadCommand(const LoadCommandInfo &LCI,
236 InMemoryStruct<macho::DysymtabLoadCommand> &Res) const {
237 ReadInMemoryStruct(*this, Buffer->getBuffer(), LCI.Offset, Res);
241 void SwapStruct(macho::IndirectSymbolTableEntry &Value) {
242 SwapValue(Value.Index);
245 MachOObject::ReadIndirectSymbolTableEntry(const macho::DysymtabLoadCommand &DLC,
247 InMemoryStruct<macho::IndirectSymbolTableEntry> &Res) const {
248 uint64_t Offset = (DLC.IndirectSymbolTableOffset +
249 Index * sizeof(macho::IndirectSymbolTableEntry));
250 ReadInMemoryStruct(*this, Buffer->getBuffer(), Offset, Res);
255 void SwapStruct(macho::Section &Value) {
256 SwapValue(Value.Address);
257 SwapValue(Value.Size);
258 SwapValue(Value.Offset);
259 SwapValue(Value.Align);
260 SwapValue(Value.RelocationTableOffset);
261 SwapValue(Value.NumRelocationTableEntries);
262 SwapValue(Value.Flags);
263 SwapValue(Value.Reserved1);
264 SwapValue(Value.Reserved2);
266 void MachOObject::ReadSection(const LoadCommandInfo &LCI,
268 InMemoryStruct<macho::Section> &Res) const {
269 assert(LCI.Command.Type == macho::LCT_Segment &&
270 "Unexpected load command info!");
271 uint64_t Offset = (LCI.Offset + sizeof(macho::SegmentLoadCommand) +
272 Index * sizeof(macho::Section));
273 ReadInMemoryStruct(*this, Buffer->getBuffer(), Offset, Res);
277 void SwapStruct(macho::Section64 &Value) {
278 SwapValue(Value.Address);
279 SwapValue(Value.Size);
280 SwapValue(Value.Offset);
281 SwapValue(Value.Align);
282 SwapValue(Value.RelocationTableOffset);
283 SwapValue(Value.NumRelocationTableEntries);
284 SwapValue(Value.Flags);
285 SwapValue(Value.Reserved1);
286 SwapValue(Value.Reserved2);
287 SwapValue(Value.Reserved3);
289 void MachOObject::ReadSection64(const LoadCommandInfo &LCI,
291 InMemoryStruct<macho::Section64> &Res) const {
292 assert(LCI.Command.Type == macho::LCT_Segment64 &&
293 "Unexpected load command info!");
294 uint64_t Offset = (LCI.Offset + sizeof(macho::Segment64LoadCommand) +
295 Index * sizeof(macho::Section64));
296 ReadInMemoryStruct(*this, Buffer->getBuffer(), Offset, Res);
300 void SwapStruct(macho::RelocationEntry &Value) {
301 SwapValue(Value.Word0);
302 SwapValue(Value.Word1);
304 void MachOObject::ReadRelocationEntry(uint64_t RelocationTableOffset,
306 InMemoryStruct<macho::RelocationEntry> &Res) const {
307 uint64_t Offset = (RelocationTableOffset +
308 Index * sizeof(macho::RelocationEntry));
309 ReadInMemoryStruct(*this, Buffer->getBuffer(), Offset, Res);