macho-dump: Add support for dumping symbol table entries.
[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/System/Host.h"
14 #include "llvm/System/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 void MachOObject::RegisterStringTable(macho::SymtabLoadCommand &SLC) {
129   HasStringTable = true;
130   StringTable = Buffer->getBuffer().substr(SLC.StringTableOffset,
131                                            SLC.StringTableSize);
132 }
133
134 const MachOObject::LoadCommandInfo &
135 MachOObject::getLoadCommandInfo(unsigned Index) const {
136   assert(Index < getHeader().NumLoadCommands && "Invalid index!");
137
138   // Load the command, if necessary.
139   if (Index >= NumLoadedCommands) {
140     uint64_t Offset;
141     if (Index == 0) {
142       Offset = getHeaderSize();
143     } else {
144       const LoadCommandInfo &Prev = getLoadCommandInfo(Index - 1);
145       Offset = Prev.Offset + Prev.Command.Size;
146     }
147
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);
154     }
155     Info.Offset = Offset;
156     NumLoadedCommands = Index + 1;
157   }
158
159   return LoadCommands[Index];
160 }
161
162 template<>
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);
174 }
175 void MachOObject::ReadSegmentLoadCommand(const LoadCommandInfo &LCI,
176                          InMemoryStruct<macho::SegmentLoadCommand> &Res) const {
177   ReadInMemoryStruct(*this, Buffer->getBuffer(), LCI.Offset, Res);
178 }
179
180 template<>
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);
192 }
193 void MachOObject::ReadSegment64LoadCommand(const LoadCommandInfo &LCI,
194                        InMemoryStruct<macho::Segment64LoadCommand> &Res) const {
195   ReadInMemoryStruct(*this, Buffer->getBuffer(), LCI.Offset, Res);
196 }
197
198 template<>
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);
206 }
207 void MachOObject::ReadSymtabLoadCommand(const LoadCommandInfo &LCI,
208                           InMemoryStruct<macho::SymtabLoadCommand> &Res) const {
209   ReadInMemoryStruct(*this, Buffer->getBuffer(), LCI.Offset, Res);
210 }
211
212 template<>
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);
234 }
235 void MachOObject::ReadDysymtabLoadCommand(const LoadCommandInfo &LCI,
236                         InMemoryStruct<macho::DysymtabLoadCommand> &Res) const {
237   ReadInMemoryStruct(*this, Buffer->getBuffer(), LCI.Offset, Res);
238 }
239
240 template<>
241 void SwapStruct(macho::IndirectSymbolTableEntry &Value) {
242   SwapValue(Value.Index);
243 }
244 void
245 MachOObject::ReadIndirectSymbolTableEntry(const macho::DysymtabLoadCommand &DLC,
246                                           unsigned Index,
247                    InMemoryStruct<macho::IndirectSymbolTableEntry> &Res) const {
248   uint64_t Offset = (DLC.IndirectSymbolTableOffset +
249                      Index * sizeof(macho::IndirectSymbolTableEntry));
250   ReadInMemoryStruct(*this, Buffer->getBuffer(), Offset, Res);
251 }
252
253
254 template<>
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);
265 }
266 void MachOObject::ReadSection(const LoadCommandInfo &LCI,
267                               unsigned Index,
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);
274 }
275
276 template<>
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);
288 }
289 void MachOObject::ReadSection64(const LoadCommandInfo &LCI,
290                                 unsigned Index,
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);
297 }
298
299 template<>
300 void SwapStruct(macho::RelocationEntry &Value) {
301   SwapValue(Value.Word0);
302   SwapValue(Value.Word1);
303 }
304 void MachOObject::ReadRelocationEntry(uint64_t RelocationTableOffset,
305                                       unsigned Index,
306                             InMemoryStruct<macho::RelocationEntry> &Res) const {
307   uint64_t Offset = (RelocationTableOffset +
308                      Index * sizeof(macho::RelocationEntry));
309   ReadInMemoryStruct(*this, Buffer->getBuffer(), Offset, Res);
310 }
311
312 template<>
313 void SwapStruct(macho::SymbolTableEntry &Value) {
314   SwapValue(Value.StringIndex);
315   SwapValue(Value.Flags);
316   SwapValue(Value.Value);
317 }
318 void MachOObject::ReadSymbolTableEntry(uint64_t SymbolTableOffset,
319                                        unsigned Index,
320                            InMemoryStruct<macho::SymbolTableEntry> &Res) const {
321   uint64_t Offset = (SymbolTableOffset +
322                      Index * sizeof(macho::SymbolTableEntry));
323   ReadInMemoryStruct(*this, Buffer->getBuffer(), Offset, Res);
324 }
325
326 template<>
327 void SwapStruct(macho::Symbol64TableEntry &Value) {
328   SwapValue(Value.StringIndex);
329   SwapValue(Value.Flags);
330   SwapValue(Value.Value);
331 }
332 void MachOObject::ReadSymbol64TableEntry(uint64_t SymbolTableOffset,
333                                        unsigned Index,
334                          InMemoryStruct<macho::Symbol64TableEntry> &Res) const {
335   uint64_t Offset = (SymbolTableOffset +
336                      Index * sizeof(macho::Symbol64TableEntry));
337   ReadInMemoryStruct(*this, Buffer->getBuffer(), Offset, Res);
338 }