blob: 5e64d6323288c3f4ddcd46c0655bc128a3806a35 [file] [log] [blame]
Daniel Dunbarfbd25b72010-11-27 05:38:50 +00001//===- MachOObject.cpp - Mach-O Object File Wrapper -----------------------===//
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#include "llvm/Object/MachOObject.h"
Daniel Dunbar95369162010-11-27 06:39:22 +000011#include "llvm/ADT/StringRef.h"
Daniel Dunbarfbd25b72010-11-27 05:38:50 +000012#include "llvm/Support/MemoryBuffer.h"
Michael J. Spencer1f6efa32010-11-29 18:16:10 +000013#include "llvm/Support/Host.h"
14#include "llvm/Support/SwapByteOrder.h"
Daniel Dunbarfbd25b72010-11-27 05:38:50 +000015
16using namespace llvm;
Daniel Dunbara956d8b2010-11-27 07:19:41 +000017using namespace llvm::object;
18
Daniel Dunbar4ba1f5e2010-11-27 08:22:29 +000019/* Translation Utilities */
20
Daniel Dunbara956d8b2010-11-27 07:19:41 +000021template<typename T>
22static void SwapValue(T &Value) {
23 Value = sys::SwapByteOrder(Value);
24}
Daniel Dunbarfbd25b72010-11-27 05:38:50 +000025
Daniel Dunbar4ba1f5e2010-11-27 08:22:29 +000026template<typename T>
27static void SwapStruct(T &Value);
28
29template<typename T>
30static 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);
35
36 // Check that the buffer contains the expected data.
37 if (Base + Size > Buffer.size()) {
38 Res = 0;
39 return;
40 }
41
42 // Check whether we can return a direct pointer.
43 struct_type *Ptr = (struct_type *) (Buffer.data() + Base);
44 if (!MOO.isSwappedEndian()) {
45 Res = Ptr;
46 return;
47 }
48
49 // Otherwise, copy the struct and translate the values.
50 Res = *Ptr;
51 SwapStruct(*Res);
52}
53
54/* *** */
55
Daniel Dunbar95369162010-11-27 06:39:22 +000056MachOObject::MachOObject(MemoryBuffer *Buffer_, bool IsLittleEndian_,
57 bool Is64Bit_)
Daniel Dunbara956d8b2010-11-27 07:19:41 +000058 : Buffer(Buffer_), IsLittleEndian(IsLittleEndian_), Is64Bit(Is64Bit_),
59 IsSwappedEndian(IsLittleEndian != sys::isLittleEndianHost()),
Daniel Dunbarf2e2a5f2010-11-27 13:46:11 +000060 HasStringTable(false), LoadCommands(0), NumLoadedCommands(0) {
Daniel Dunbara956d8b2010-11-27 07:19:41 +000061 // 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);
71 }
72
73 if (is64Bit()) {
74 memcpy(&Header64Ext, Buffer->getBuffer().data() + sizeof(Header),
75 sizeof(Header64Ext));
76 if (IsSwappedEndian) {
77 SwapValue(Header64Ext.Reserved);
78 }
79 }
80
81 // Create the load command array if sane.
82 if (getHeader().NumLoadCommands < (1 << 20))
83 LoadCommands = new LoadCommandInfo[getHeader().NumLoadCommands];
84}
85
86MachOObject::~MachOObject() {
Benjamin Kramer23dcd002010-12-17 09:56:50 +000087 delete [] LoadCommands;
Daniel Dunbarfbd25b72010-11-27 05:38:50 +000088}
89
90MachOObject *MachOObject::LoadFromBuffer(MemoryBuffer *Buffer,
91 std::string *ErrorStr) {
Daniel Dunbar95369162010-11-27 06:39:22 +000092 // 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") {
99 Is64Bit = true;
100 } else if (Magic == "\xCF\xFA\xED\xFE") {
101 IsLittleEndian = true;
102 Is64Bit = true;
103 } else {
Daniel Dunbara956d8b2010-11-27 07:19:41 +0000104 if (ErrorStr) *ErrorStr = "not a Mach object file (invalid magic)";
105 return 0;
106 }
107
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)";
Daniel Dunbar95369162010-11-27 06:39:22 +0000112 return 0;
113 }
114
115 OwningPtr<MachOObject> Object(new MachOObject(Buffer, IsLittleEndian,
116 Is64Bit));
117
Daniel Dunbara956d8b2010-11-27 07:19:41 +0000118 // 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)";
121 return 0;
122 }
123
Daniel Dunbarfbd25b72010-11-27 05:38:50 +0000124 if (ErrorStr) *ErrorStr = "";
Daniel Dunbar95369162010-11-27 06:39:22 +0000125 return Object.take();
Daniel Dunbarfbd25b72010-11-27 05:38:50 +0000126}
Daniel Dunbara956d8b2010-11-27 07:19:41 +0000127
Daniel Dunbar71130f82010-11-27 13:58:16 +0000128StringRef MachOObject::getData(size_t Offset, size_t Size) const {
129 return Buffer->getBuffer().substr(Offset,Size);
130}
131
Daniel Dunbarf2e2a5f2010-11-27 13:46:11 +0000132void MachOObject::RegisterStringTable(macho::SymtabLoadCommand &SLC) {
133 HasStringTable = true;
134 StringTable = Buffer->getBuffer().substr(SLC.StringTableOffset,
135 SLC.StringTableSize);
136}
137
Daniel Dunbara956d8b2010-11-27 07:19:41 +0000138const MachOObject::LoadCommandInfo &
139MachOObject::getLoadCommandInfo(unsigned Index) const {
140 assert(Index < getHeader().NumLoadCommands && "Invalid index!");
141
142 // Load the command, if necessary.
143 if (Index >= NumLoadedCommands) {
144 uint64_t Offset;
145 if (Index == 0) {
146 Offset = getHeaderSize();
147 } else {
148 const LoadCommandInfo &Prev = getLoadCommandInfo(Index - 1);
149 Offset = Prev.Offset + Prev.Command.Size;
150 }
151
152 LoadCommandInfo &Info = LoadCommands[Index];
153 memcpy(&Info.Command, Buffer->getBuffer().data() + Offset,
154 sizeof(macho::LoadCommand));
155 if (IsSwappedEndian) {
156 SwapValue(Info.Command.Type);
157 SwapValue(Info.Command.Size);
158 }
159 Info.Offset = Offset;
160 NumLoadedCommands = Index + 1;
161 }
162
163 return LoadCommands[Index];
164}
Daniel Dunbar4ba1f5e2010-11-27 08:22:29 +0000165
166template<>
Benjamin Kramer3946e3b2010-11-27 09:08:25 +0000167void SwapStruct(macho::SegmentLoadCommand &Value) {
Daniel Dunbar4ba1f5e2010-11-27 08:22:29 +0000168 SwapValue(Value.Type);
169 SwapValue(Value.Size);
170 SwapValue(Value.VMAddress);
171 SwapValue(Value.VMSize);
172 SwapValue(Value.FileOffset);
173 SwapValue(Value.FileSize);
174 SwapValue(Value.MaxVMProtection);
175 SwapValue(Value.InitialVMProtection);
176 SwapValue(Value.NumSections);
177 SwapValue(Value.Flags);
178}
179void MachOObject::ReadSegmentLoadCommand(const LoadCommandInfo &LCI,
180 InMemoryStruct<macho::SegmentLoadCommand> &Res) const {
181 ReadInMemoryStruct(*this, Buffer->getBuffer(), LCI.Offset, Res);
182}
183
184template<>
Benjamin Kramer3946e3b2010-11-27 09:08:25 +0000185void SwapStruct(macho::Segment64LoadCommand &Value) {
Daniel Dunbar4ba1f5e2010-11-27 08:22:29 +0000186 SwapValue(Value.Type);
187 SwapValue(Value.Size);
188 SwapValue(Value.VMAddress);
189 SwapValue(Value.VMSize);
190 SwapValue(Value.FileOffset);
191 SwapValue(Value.FileSize);
192 SwapValue(Value.MaxVMProtection);
193 SwapValue(Value.InitialVMProtection);
194 SwapValue(Value.NumSections);
195 SwapValue(Value.Flags);
196}
197void MachOObject::ReadSegment64LoadCommand(const LoadCommandInfo &LCI,
198 InMemoryStruct<macho::Segment64LoadCommand> &Res) const {
199 ReadInMemoryStruct(*this, Buffer->getBuffer(), LCI.Offset, Res);
200}
Daniel Dunbarf879f142010-11-27 08:33:44 +0000201
202template<>
Benjamin Kramer3946e3b2010-11-27 09:08:25 +0000203void SwapStruct(macho::SymtabLoadCommand &Value) {
Daniel Dunbarf879f142010-11-27 08:33:44 +0000204 SwapValue(Value.Type);
205 SwapValue(Value.Size);
206 SwapValue(Value.SymbolTableOffset);
207 SwapValue(Value.NumSymbolTableEntries);
208 SwapValue(Value.StringTableOffset);
209 SwapValue(Value.StringTableSize);
210}
211void MachOObject::ReadSymtabLoadCommand(const LoadCommandInfo &LCI,
212 InMemoryStruct<macho::SymtabLoadCommand> &Res) const {
213 ReadInMemoryStruct(*this, Buffer->getBuffer(), LCI.Offset, Res);
214}
215
216template<>
Benjamin Kramer3946e3b2010-11-27 09:08:25 +0000217void SwapStruct(macho::DysymtabLoadCommand &Value) {
Daniel Dunbarf879f142010-11-27 08:33:44 +0000218 SwapValue(Value.Type);
219 SwapValue(Value.Size);
Daniel Dunbara87d7ec2010-12-10 06:19:39 +0000220 SwapValue(Value.LocalSymbolsIndex);
Daniel Dunbarf879f142010-11-27 08:33:44 +0000221 SwapValue(Value.NumLocalSymbols);
222 SwapValue(Value.ExternalSymbolsIndex);
223 SwapValue(Value.NumExternalSymbols);
224 SwapValue(Value.UndefinedSymbolsIndex);
225 SwapValue(Value.NumUndefinedSymbols);
226 SwapValue(Value.TOCOffset);
227 SwapValue(Value.NumTOCEntries);
228 SwapValue(Value.ModuleTableOffset);
229 SwapValue(Value.NumModuleTableEntries);
230 SwapValue(Value.ReferenceSymbolTableOffset);
231 SwapValue(Value.NumReferencedSymbolTableEntries);
232 SwapValue(Value.IndirectSymbolTableOffset);
233 SwapValue(Value.NumIndirectSymbolTableEntries);
234 SwapValue(Value.ExternalRelocationTableOffset);
235 SwapValue(Value.NumExternalRelocationTableEntries);
236 SwapValue(Value.LocalRelocationTableOffset);
237 SwapValue(Value.NumLocalRelocationTableEntries);
238}
239void MachOObject::ReadDysymtabLoadCommand(const LoadCommandInfo &LCI,
240 InMemoryStruct<macho::DysymtabLoadCommand> &Res) const {
241 ReadInMemoryStruct(*this, Buffer->getBuffer(), LCI.Offset, Res);
242}
Daniel Dunbar4c55e0d2010-11-27 13:26:12 +0000243
244template<>
245void SwapStruct(macho::IndirectSymbolTableEntry &Value) {
246 SwapValue(Value.Index);
247}
248void
249MachOObject::ReadIndirectSymbolTableEntry(const macho::DysymtabLoadCommand &DLC,
250 unsigned Index,
251 InMemoryStruct<macho::IndirectSymbolTableEntry> &Res) const {
252 uint64_t Offset = (DLC.IndirectSymbolTableOffset +
253 Index * sizeof(macho::IndirectSymbolTableEntry));
254 ReadInMemoryStruct(*this, Buffer->getBuffer(), Offset, Res);
255}
Daniel Dunbar2acadbd2010-11-27 13:33:15 +0000256
257
258template<>
259void SwapStruct(macho::Section &Value) {
260 SwapValue(Value.Address);
261 SwapValue(Value.Size);
262 SwapValue(Value.Offset);
263 SwapValue(Value.Align);
264 SwapValue(Value.RelocationTableOffset);
265 SwapValue(Value.NumRelocationTableEntries);
266 SwapValue(Value.Flags);
267 SwapValue(Value.Reserved1);
268 SwapValue(Value.Reserved2);
269}
270void MachOObject::ReadSection(const LoadCommandInfo &LCI,
271 unsigned Index,
272 InMemoryStruct<macho::Section> &Res) const {
273 assert(LCI.Command.Type == macho::LCT_Segment &&
274 "Unexpected load command info!");
275 uint64_t Offset = (LCI.Offset + sizeof(macho::SegmentLoadCommand) +
276 Index * sizeof(macho::Section));
277 ReadInMemoryStruct(*this, Buffer->getBuffer(), Offset, Res);
278}
279
280template<>
281void SwapStruct(macho::Section64 &Value) {
282 SwapValue(Value.Address);
283 SwapValue(Value.Size);
284 SwapValue(Value.Offset);
285 SwapValue(Value.Align);
286 SwapValue(Value.RelocationTableOffset);
287 SwapValue(Value.NumRelocationTableEntries);
288 SwapValue(Value.Flags);
289 SwapValue(Value.Reserved1);
290 SwapValue(Value.Reserved2);
291 SwapValue(Value.Reserved3);
292}
293void MachOObject::ReadSection64(const LoadCommandInfo &LCI,
294 unsigned Index,
295 InMemoryStruct<macho::Section64> &Res) const {
296 assert(LCI.Command.Type == macho::LCT_Segment64 &&
297 "Unexpected load command info!");
298 uint64_t Offset = (LCI.Offset + sizeof(macho::Segment64LoadCommand) +
299 Index * sizeof(macho::Section64));
300 ReadInMemoryStruct(*this, Buffer->getBuffer(), Offset, Res);
301}
Daniel Dunbar90e3e3a2010-11-27 13:39:48 +0000302
303template<>
304void SwapStruct(macho::RelocationEntry &Value) {
305 SwapValue(Value.Word0);
306 SwapValue(Value.Word1);
307}
308void MachOObject::ReadRelocationEntry(uint64_t RelocationTableOffset,
309 unsigned Index,
310 InMemoryStruct<macho::RelocationEntry> &Res) const {
311 uint64_t Offset = (RelocationTableOffset +
312 Index * sizeof(macho::RelocationEntry));
313 ReadInMemoryStruct(*this, Buffer->getBuffer(), Offset, Res);
314}
Daniel Dunbar2208b582010-11-27 13:52:53 +0000315
316template<>
317void SwapStruct(macho::SymbolTableEntry &Value) {
318 SwapValue(Value.StringIndex);
319 SwapValue(Value.Flags);
320 SwapValue(Value.Value);
321}
322void MachOObject::ReadSymbolTableEntry(uint64_t SymbolTableOffset,
323 unsigned Index,
324 InMemoryStruct<macho::SymbolTableEntry> &Res) const {
325 uint64_t Offset = (SymbolTableOffset +
326 Index * sizeof(macho::SymbolTableEntry));
327 ReadInMemoryStruct(*this, Buffer->getBuffer(), Offset, Res);
328}
329
330template<>
331void SwapStruct(macho::Symbol64TableEntry &Value) {
332 SwapValue(Value.StringIndex);
333 SwapValue(Value.Flags);
334 SwapValue(Value.Value);
335}
336void MachOObject::ReadSymbol64TableEntry(uint64_t SymbolTableOffset,
337 unsigned Index,
338 InMemoryStruct<macho::Symbol64TableEntry> &Res) const {
339 uint64_t Offset = (SymbolTableOffset +
340 Index * sizeof(macho::Symbol64TableEntry));
341 ReadInMemoryStruct(*this, Buffer->getBuffer(), Offset, Res);
342}