DenseMap<uintptr_t,...> doesn't allow all values as keys.
[oota-llvm.git] / lib / Object / MachOObject.cpp
1 //===- 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"
11 #include "llvm/ADT/StringRef.h"
12 #include "llvm/Support/MemoryBuffer.h"
13 #include "llvm/Support/Host.h"
14 #include "llvm/Support/SwapByteOrder.h"
15
16 using namespace llvm;
17 using namespace llvm::object;
18
19 /* Translation Utilities */
20
21 template<typename T>
22 static void SwapValue(T &Value) {
23   Value = sys::SwapByteOrder(Value);
24 }
25
26 template<typename T>
27 static void SwapStruct(T &Value);
28
29 template<typename T>
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);
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
56 MachOObject::MachOObject(MemoryBuffer *Buffer_, bool IsLittleEndian_,
57                          bool Is64Bit_)
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);
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
86 MachOObject::~MachOObject() {
87   delete [] LoadCommands;
88 }
89
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") {
99     Is64Bit = true;
100   } else if (Magic == "\xCF\xFA\xED\xFE") {
101     IsLittleEndian = true;
102     Is64Bit = true;
103   } else {
104     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)";
112     return 0;
113   }
114
115   OwningPtr<MachOObject> Object(new MachOObject(Buffer, IsLittleEndian,
116                                                 Is64Bit));
117
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)";
121     return 0;
122   }
123
124   if (ErrorStr) *ErrorStr = "";
125   return Object.take();
126 }
127
128 StringRef MachOObject::getData(size_t Offset, size_t Size) const {
129   return Buffer->getBuffer().substr(Offset,Size);
130 }
131
132 void MachOObject::RegisterStringTable(macho::SymtabLoadCommand &SLC) {
133   HasStringTable = true;
134   StringTable = Buffer->getBuffer().substr(SLC.StringTableOffset,
135                                            SLC.StringTableSize);
136 }
137
138 const MachOObject::LoadCommandInfo &
139 MachOObject::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 }
165
166 template<>
167 void SwapStruct(macho::SegmentLoadCommand &Value) {
168   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 }
179 void MachOObject::ReadSegmentLoadCommand(const LoadCommandInfo &LCI,
180                          InMemoryStruct<macho::SegmentLoadCommand> &Res) const {
181   ReadInMemoryStruct(*this, Buffer->getBuffer(), LCI.Offset, Res);
182 }
183
184 template<>
185 void SwapStruct(macho::Segment64LoadCommand &Value) {
186   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 }
197 void MachOObject::ReadSegment64LoadCommand(const LoadCommandInfo &LCI,
198                        InMemoryStruct<macho::Segment64LoadCommand> &Res) const {
199   ReadInMemoryStruct(*this, Buffer->getBuffer(), LCI.Offset, Res);
200 }
201
202 template<>
203 void SwapStruct(macho::SymtabLoadCommand &Value) {
204   SwapValue(Value.Type);
205   SwapValue(Value.Size);
206   SwapValue(Value.SymbolTableOffset);
207   SwapValue(Value.NumSymbolTableEntries);
208   SwapValue(Value.StringTableOffset);
209   SwapValue(Value.StringTableSize);
210 }
211 void MachOObject::ReadSymtabLoadCommand(const LoadCommandInfo &LCI,
212                           InMemoryStruct<macho::SymtabLoadCommand> &Res) const {
213   ReadInMemoryStruct(*this, Buffer->getBuffer(), LCI.Offset, Res);
214 }
215
216 template<>
217 void SwapStruct(macho::DysymtabLoadCommand &Value) {
218   SwapValue(Value.Type);
219   SwapValue(Value.Size);
220   SwapValue(Value.LocalSymbolsIndex);
221   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 }
239 void MachOObject::ReadDysymtabLoadCommand(const LoadCommandInfo &LCI,
240                         InMemoryStruct<macho::DysymtabLoadCommand> &Res) const {
241   ReadInMemoryStruct(*this, Buffer->getBuffer(), LCI.Offset, Res);
242 }
243
244 template<>
245 void SwapStruct(macho::IndirectSymbolTableEntry &Value) {
246   SwapValue(Value.Index);
247 }
248 void
249 MachOObject::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 }
256
257
258 template<>
259 void 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 }
270 void 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
280 template<>
281 void 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 }
293 void 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 }
302
303 template<>
304 void SwapStruct(macho::RelocationEntry &Value) {
305   SwapValue(Value.Word0);
306   SwapValue(Value.Word1);
307 }
308 void 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 }
315
316 template<>
317 void SwapStruct(macho::SymbolTableEntry &Value) {
318   SwapValue(Value.StringIndex);
319   SwapValue(Value.Flags);
320   SwapValue(Value.Value);
321 }
322 void 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
330 template<>
331 void SwapStruct(macho::Symbol64TableEntry &Value) {
332   SwapValue(Value.StringIndex);
333   SwapValue(Value.Flags);
334   SwapValue(Value.Value);
335 }
336 void 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 }