blob: 0d4dbd9ec45abc5436a975d9cc276270b82c8593 [file] [log] [blame]
Eric Christopher6256b032011-04-22 03:19:48 +00001//===- MachOObjectFile.cpp - Mach-O object file binding ---------*- C++ -*-===//
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// This file defines the MachOObjectFile class, which binds the MachOObject
11// class to the generic ObjectFile wrapper.
12//
13//===----------------------------------------------------------------------===//
14
15#include "llvm/ADT/Triple.h"
Owen Andersonf7c93a32011-10-11 17:32:27 +000016#include "llvm/Object/MachO.h"
Eric Christopher6256b032011-04-22 03:19:48 +000017#include "llvm/Object/MachOFormat.h"
Eric Christopher6256b032011-04-22 03:19:48 +000018#include "llvm/Support/MemoryBuffer.h"
19
20#include <cctype>
21#include <cstring>
22#include <limits>
23
24using namespace llvm;
25using namespace object;
26
27namespace llvm {
Owen Andersonf7c93a32011-10-11 17:32:27 +000028namespace object {
Eric Christopher6256b032011-04-22 03:19:48 +000029
Benjamin Kramer0fcab072011-09-08 20:52:17 +000030MachOObjectFile::MachOObjectFile(MemoryBuffer *Object, MachOObject *MOO,
31 error_code &ec)
32 : ObjectFile(Binary::isMachO, Object, ec),
33 MachOObj(MOO),
34 RegisteredStringTable(std::numeric_limits<uint32_t>::max()) {
35 DataRefImpl DRI;
36 DRI.d.a = DRI.d.b = 0;
37 moveToNextSection(DRI);
38 uint32_t LoadCommandCount = MachOObj->getHeader().NumLoadCommands;
39 while (DRI.d.a < LoadCommandCount) {
40 Sections.push_back(DRI);
Benjamin Kramer0fcab072011-09-08 20:52:17 +000041 DRI.d.b++;
42 moveToNextSection(DRI);
43 }
44}
45
46
Eric Christopher6256b032011-04-22 03:19:48 +000047ObjectFile *ObjectFile::createMachOObjectFile(MemoryBuffer *Buffer) {
Michael J. Spencer001c9202011-06-25 17:54:50 +000048 error_code ec;
Eric Christopher6256b032011-04-22 03:19:48 +000049 std::string Err;
50 MachOObject *MachOObj = MachOObject::LoadFromBuffer(Buffer, &Err);
51 if (!MachOObj)
52 return NULL;
Michael J. Spencer001c9202011-06-25 17:54:50 +000053 return new MachOObjectFile(Buffer, MachOObj, ec);
Eric Christopher6256b032011-04-22 03:19:48 +000054}
55
56/*===-- Symbols -----------------------------------------------------------===*/
57
58void MachOObjectFile::moveToNextSymbol(DataRefImpl &DRI) const {
59 uint32_t LoadCommandCount = MachOObj->getHeader().NumLoadCommands;
60 while (DRI.d.a < LoadCommandCount) {
61 LoadCommandInfo LCI = MachOObj->getLoadCommandInfo(DRI.d.a);
62 if (LCI.Command.Type == macho::LCT_Symtab) {
63 InMemoryStruct<macho::SymtabLoadCommand> SymtabLoadCmd;
64 MachOObj->ReadSymtabLoadCommand(LCI, SymtabLoadCmd);
65 if (DRI.d.b < SymtabLoadCmd->NumSymbolTableEntries)
66 return;
67 }
68
69 DRI.d.a++;
70 DRI.d.b = 0;
71 }
72}
73
74void MachOObjectFile::getSymbolTableEntry(DataRefImpl DRI,
75 InMemoryStruct<macho::SymbolTableEntry> &Res) const {
76 InMemoryStruct<macho::SymtabLoadCommand> SymtabLoadCmd;
77 LoadCommandInfo LCI = MachOObj->getLoadCommandInfo(DRI.d.a);
78 MachOObj->ReadSymtabLoadCommand(LCI, SymtabLoadCmd);
79
80 if (RegisteredStringTable != DRI.d.a) {
81 MachOObj->RegisterStringTable(*SymtabLoadCmd);
82 RegisteredStringTable = DRI.d.a;
83 }
84
85 MachOObj->ReadSymbolTableEntry(SymtabLoadCmd->SymbolTableOffset, DRI.d.b,
86 Res);
87}
88
Benjamin Kramer32fb2af2011-07-15 17:32:45 +000089void MachOObjectFile::getSymbol64TableEntry(DataRefImpl DRI,
90 InMemoryStruct<macho::Symbol64TableEntry> &Res) const {
91 InMemoryStruct<macho::SymtabLoadCommand> SymtabLoadCmd;
92 LoadCommandInfo LCI = MachOObj->getLoadCommandInfo(DRI.d.a);
93 MachOObj->ReadSymtabLoadCommand(LCI, SymtabLoadCmd);
94
95 if (RegisteredStringTable != DRI.d.a) {
96 MachOObj->RegisterStringTable(*SymtabLoadCmd);
97 RegisteredStringTable = DRI.d.a;
98 }
99
100 MachOObj->ReadSymbol64TableEntry(SymtabLoadCmd->SymbolTableOffset, DRI.d.b,
101 Res);
102}
103
Eric Christopher6256b032011-04-22 03:19:48 +0000104
Michael J. Spencer25b15772011-06-25 17:55:23 +0000105error_code MachOObjectFile::getSymbolNext(DataRefImpl DRI,
106 SymbolRef &Result) const {
Eric Christopher6256b032011-04-22 03:19:48 +0000107 DRI.d.b++;
108 moveToNextSymbol(DRI);
Michael J. Spencer25b15772011-06-25 17:55:23 +0000109 Result = SymbolRef(DRI, this);
110 return object_error::success;
Eric Christopher6256b032011-04-22 03:19:48 +0000111}
112
Michael J. Spencer25b15772011-06-25 17:55:23 +0000113error_code MachOObjectFile::getSymbolName(DataRefImpl DRI,
114 StringRef &Result) const {
Benjamin Kramer32fb2af2011-07-15 17:32:45 +0000115 if (MachOObj->is64Bit()) {
116 InMemoryStruct<macho::Symbol64TableEntry> Entry;
117 getSymbol64TableEntry(DRI, Entry);
118 Result = MachOObj->getStringAtIndex(Entry->StringIndex);
119 } else {
120 InMemoryStruct<macho::SymbolTableEntry> Entry;
121 getSymbolTableEntry(DRI, Entry);
122 Result = MachOObj->getStringAtIndex(Entry->StringIndex);
123 }
Michael J. Spencer25b15772011-06-25 17:55:23 +0000124 return object_error::success;
Eric Christopher6256b032011-04-22 03:19:48 +0000125}
126
Benjamin Kramerac241fe2011-09-14 01:22:52 +0000127error_code MachOObjectFile::getSymbolOffset(DataRefImpl DRI,
Michael J. Spencer25b15772011-06-25 17:55:23 +0000128 uint64_t &Result) const {
Benjamin Kramer32fb2af2011-07-15 17:32:45 +0000129 if (MachOObj->is64Bit()) {
130 InMemoryStruct<macho::Symbol64TableEntry> Entry;
131 getSymbol64TableEntry(DRI, Entry);
132 Result = Entry->Value;
133 } else {
134 InMemoryStruct<macho::SymbolTableEntry> Entry;
135 getSymbolTableEntry(DRI, Entry);
136 Result = Entry->Value;
137 }
Michael J. Spencer25b15772011-06-25 17:55:23 +0000138 return object_error::success;
Eric Christopher6256b032011-04-22 03:19:48 +0000139}
140
Benjamin Kramerac241fe2011-09-14 01:22:52 +0000141error_code MachOObjectFile::getSymbolAddress(DataRefImpl DRI,
142 uint64_t &Result) const {
143 uint64_t SymbolOffset;
144 uint8_t SectionIndex;
145 if (MachOObj->is64Bit()) {
146 InMemoryStruct<macho::Symbol64TableEntry> Entry;
147 getSymbol64TableEntry(DRI, Entry);
148 SymbolOffset = Entry->Value;
149 SectionIndex = Entry->SectionIndex;
150 } else {
151 InMemoryStruct<macho::SymbolTableEntry> Entry;
152 getSymbolTableEntry(DRI, Entry);
153 SymbolOffset = Entry->Value;
154 SectionIndex = Entry->SectionIndex;
155 }
Owen Anderson41242942011-10-12 21:43:24 +0000156 getSectionAddress(Sections[SectionIndex-1], Result);
Benjamin Kramerac241fe2011-09-14 01:22:52 +0000157 Result += SymbolOffset;
158
159 return object_error::success;
160}
161
Michael J. Spencer25b15772011-06-25 17:55:23 +0000162error_code MachOObjectFile::getSymbolSize(DataRefImpl DRI,
163 uint64_t &Result) const {
164 Result = UnknownAddressOrSize;
165 return object_error::success;
Eric Christopher6256b032011-04-22 03:19:48 +0000166}
167
Michael J. Spencer25b15772011-06-25 17:55:23 +0000168error_code MachOObjectFile::getSymbolNMTypeChar(DataRefImpl DRI,
169 char &Result) const {
Benjamin Kramer32fb2af2011-07-15 17:32:45 +0000170 uint8_t Type, Flags;
171 if (MachOObj->is64Bit()) {
172 InMemoryStruct<macho::Symbol64TableEntry> Entry;
173 getSymbol64TableEntry(DRI, Entry);
174 Type = Entry->Type;
175 Flags = Entry->Flags;
176 } else {
177 InMemoryStruct<macho::SymbolTableEntry> Entry;
178 getSymbolTableEntry(DRI, Entry);
179 Type = Entry->Type;
180 Flags = Entry->Flags;
181 }
Eric Christopher6256b032011-04-22 03:19:48 +0000182
183 char Char;
Benjamin Kramer32fb2af2011-07-15 17:32:45 +0000184 switch (Type & macho::STF_TypeMask) {
Eric Christopher6256b032011-04-22 03:19:48 +0000185 case macho::STT_Undefined:
186 Char = 'u';
187 break;
188 case macho::STT_Absolute:
189 case macho::STT_Section:
190 Char = 's';
191 break;
192 default:
193 Char = '?';
194 break;
195 }
196
Benjamin Kramer32fb2af2011-07-15 17:32:45 +0000197 if (Flags & (macho::STF_External | macho::STF_PrivateExtern))
Eric Christopher6256b032011-04-22 03:19:48 +0000198 Char = toupper(Char);
Michael J. Spencer25b15772011-06-25 17:55:23 +0000199 Result = Char;
200 return object_error::success;
Eric Christopher6256b032011-04-22 03:19:48 +0000201}
202
Michael J. Spencer25b15772011-06-25 17:55:23 +0000203error_code MachOObjectFile::isSymbolInternal(DataRefImpl DRI,
204 bool &Result) const {
Benjamin Kramer32fb2af2011-07-15 17:32:45 +0000205 if (MachOObj->is64Bit()) {
206 InMemoryStruct<macho::Symbol64TableEntry> Entry;
207 getSymbol64TableEntry(DRI, Entry);
208 Result = Entry->Flags & macho::STF_StabsEntryMask;
209 } else {
210 InMemoryStruct<macho::SymbolTableEntry> Entry;
211 getSymbolTableEntry(DRI, Entry);
212 Result = Entry->Flags & macho::STF_StabsEntryMask;
213 }
Michael J. Spencer25b15772011-06-25 17:55:23 +0000214 return object_error::success;
Eric Christopher6256b032011-04-22 03:19:48 +0000215}
216
Benjamin Kramerac241fe2011-09-14 01:22:52 +0000217error_code MachOObjectFile::isSymbolGlobal(DataRefImpl Symb, bool &Res) const {
218
219 if (MachOObj->is64Bit()) {
220 InMemoryStruct<macho::Symbol64TableEntry> Entry;
221 getSymbol64TableEntry(Symb, Entry);
222 Res = Entry->Type & MachO::NlistMaskExternal;
223 } else {
224 InMemoryStruct<macho::SymbolTableEntry> Entry;
225 getSymbolTableEntry(Symb, Entry);
226 Res = Entry->Type & MachO::NlistMaskExternal;
227 }
228 return object_error::success;
229}
230
231error_code MachOObjectFile::getSymbolType(DataRefImpl Symb,
232 SymbolRef::SymbolType &Res) const {
233 uint8_t n_type;
234 if (MachOObj->is64Bit()) {
235 InMemoryStruct<macho::Symbol64TableEntry> Entry;
236 getSymbol64TableEntry(Symb, Entry);
237 n_type = Entry->Type;
238 } else {
239 InMemoryStruct<macho::SymbolTableEntry> Entry;
240 getSymbolTableEntry(Symb, Entry);
241 n_type = Entry->Type;
242 }
243 Res = SymbolRef::ST_Other;
244 switch (n_type & MachO::NlistMaskType) {
245 case MachO::NListTypeUndefined :
246 Res = SymbolRef::ST_External;
247 break;
248 case MachO::NListTypeSection :
249 Res = SymbolRef::ST_Function;
250 break;
251 }
252 return object_error::success;
253}
254
255
Michael J. Spencer4344b1e2011-10-07 19:25:32 +0000256symbol_iterator MachOObjectFile::begin_symbols() const {
Eric Christopher6256b032011-04-22 03:19:48 +0000257 // DRI.d.a = segment number; DRI.d.b = symbol index.
258 DataRefImpl DRI;
259 DRI.d.a = DRI.d.b = 0;
260 moveToNextSymbol(DRI);
261 return symbol_iterator(SymbolRef(DRI, this));
262}
263
Michael J. Spencer4344b1e2011-10-07 19:25:32 +0000264symbol_iterator MachOObjectFile::end_symbols() const {
Eric Christopher6256b032011-04-22 03:19:48 +0000265 DataRefImpl DRI;
266 DRI.d.a = MachOObj->getHeader().NumLoadCommands;
267 DRI.d.b = 0;
268 return symbol_iterator(SymbolRef(DRI, this));
269}
270
271
272/*===-- Sections ----------------------------------------------------------===*/
273
274void MachOObjectFile::moveToNextSection(DataRefImpl &DRI) const {
275 uint32_t LoadCommandCount = MachOObj->getHeader().NumLoadCommands;
276 while (DRI.d.a < LoadCommandCount) {
277 LoadCommandInfo LCI = MachOObj->getLoadCommandInfo(DRI.d.a);
278 if (LCI.Command.Type == macho::LCT_Segment) {
279 InMemoryStruct<macho::SegmentLoadCommand> SegmentLoadCmd;
280 MachOObj->ReadSegmentLoadCommand(LCI, SegmentLoadCmd);
281 if (DRI.d.b < SegmentLoadCmd->NumSections)
282 return;
283 } else if (LCI.Command.Type == macho::LCT_Segment64) {
284 InMemoryStruct<macho::Segment64LoadCommand> Segment64LoadCmd;
285 MachOObj->ReadSegment64LoadCommand(LCI, Segment64LoadCmd);
286 if (DRI.d.b < Segment64LoadCmd->NumSections)
287 return;
288 }
289
290 DRI.d.a++;
291 DRI.d.b = 0;
292 }
293}
294
Michael J. Spencer25b15772011-06-25 17:55:23 +0000295error_code MachOObjectFile::getSectionNext(DataRefImpl DRI,
296 SectionRef &Result) const {
Eric Christopher6256b032011-04-22 03:19:48 +0000297 DRI.d.b++;
298 moveToNextSection(DRI);
Michael J. Spencer25b15772011-06-25 17:55:23 +0000299 Result = SectionRef(DRI, this);
300 return object_error::success;
Eric Christopher6256b032011-04-22 03:19:48 +0000301}
302
303void
304MachOObjectFile::getSection(DataRefImpl DRI,
305 InMemoryStruct<macho::Section> &Res) const {
306 InMemoryStruct<macho::SegmentLoadCommand> SLC;
307 LoadCommandInfo LCI = MachOObj->getLoadCommandInfo(DRI.d.a);
308 MachOObj->ReadSegmentLoadCommand(LCI, SLC);
309 MachOObj->ReadSection(LCI, DRI.d.b, Res);
310}
311
Michael J. Spencer4344b1e2011-10-07 19:25:32 +0000312std::size_t MachOObjectFile::getSectionIndex(DataRefImpl Sec) const {
313 SectionList::const_iterator loc =
314 std::find(Sections.begin(), Sections.end(), Sec);
315 assert(loc != Sections.end() && "Sec is not a valid section!");
316 return std::distance(Sections.begin(), loc);
317}
318
Benjamin Kramer7d145782011-07-15 00:14:48 +0000319void
320MachOObjectFile::getSection64(DataRefImpl DRI,
321 InMemoryStruct<macho::Section64> &Res) const {
322 InMemoryStruct<macho::Segment64LoadCommand> SLC;
323 LoadCommandInfo LCI = MachOObj->getLoadCommandInfo(DRI.d.a);
324 MachOObj->ReadSegment64LoadCommand(LCI, SLC);
325 MachOObj->ReadSection64(LCI, DRI.d.b, Res);
326}
327
328static bool is64BitLoadCommand(const MachOObject *MachOObj, DataRefImpl DRI) {
329 LoadCommandInfo LCI = MachOObj->getLoadCommandInfo(DRI.d.a);
330 if (LCI.Command.Type == macho::LCT_Segment64)
331 return true;
332 assert(LCI.Command.Type == macho::LCT_Segment && "Unexpected Type.");
333 return false;
334}
335
Michael J. Spencer25b15772011-06-25 17:55:23 +0000336error_code MachOObjectFile::getSectionName(DataRefImpl DRI,
337 StringRef &Result) const {
Benjamin Kramer7d145782011-07-15 00:14:48 +0000338 // FIXME: thread safety.
Michael J. Spencer25b15772011-06-25 17:55:23 +0000339 static char result[34];
Benjamin Kramer7d145782011-07-15 00:14:48 +0000340 if (is64BitLoadCommand(MachOObj, DRI)) {
341 InMemoryStruct<macho::Segment64LoadCommand> SLC;
342 LoadCommandInfo LCI = MachOObj->getLoadCommandInfo(DRI.d.a);
343 MachOObj->ReadSegment64LoadCommand(LCI, SLC);
344 InMemoryStruct<macho::Section64> Sect;
345 MachOObj->ReadSection64(LCI, DRI.d.b, Sect);
346
Benjamin Kramer291e7672011-07-15 00:29:02 +0000347 strcpy(result, Sect->SegmentName);
Benjamin Kramer7d145782011-07-15 00:14:48 +0000348 strcat(result, ",");
349 strcat(result, Sect->Name);
350 } else {
351 InMemoryStruct<macho::SegmentLoadCommand> SLC;
352 LoadCommandInfo LCI = MachOObj->getLoadCommandInfo(DRI.d.a);
353 MachOObj->ReadSegmentLoadCommand(LCI, SLC);
354 InMemoryStruct<macho::Section> Sect;
355 MachOObj->ReadSection(LCI, DRI.d.b, Sect);
356
Benjamin Kramer291e7672011-07-15 00:29:02 +0000357 strcpy(result, Sect->SegmentName);
Benjamin Kramer7d145782011-07-15 00:14:48 +0000358 strcat(result, ",");
359 strcat(result, Sect->Name);
360 }
Michael J. Spencer25b15772011-06-25 17:55:23 +0000361 Result = StringRef(result);
362 return object_error::success;
Eric Christopher6256b032011-04-22 03:19:48 +0000363}
364
Michael J. Spencer25b15772011-06-25 17:55:23 +0000365error_code MachOObjectFile::getSectionAddress(DataRefImpl DRI,
366 uint64_t &Result) const {
Benjamin Kramer7d145782011-07-15 00:14:48 +0000367 if (is64BitLoadCommand(MachOObj, DRI)) {
368 InMemoryStruct<macho::Section64> Sect;
369 getSection64(DRI, Sect);
370 Result = Sect->Address;
371 } else {
372 InMemoryStruct<macho::Section> Sect;
373 getSection(DRI, Sect);
374 Result = Sect->Address;
375 }
Michael J. Spencer25b15772011-06-25 17:55:23 +0000376 return object_error::success;
Eric Christopher6256b032011-04-22 03:19:48 +0000377}
378
Michael J. Spencer25b15772011-06-25 17:55:23 +0000379error_code MachOObjectFile::getSectionSize(DataRefImpl DRI,
380 uint64_t &Result) const {
Benjamin Kramer7d145782011-07-15 00:14:48 +0000381 if (is64BitLoadCommand(MachOObj, DRI)) {
382 InMemoryStruct<macho::Section64> Sect;
383 getSection64(DRI, Sect);
384 Result = Sect->Size;
385 } else {
386 InMemoryStruct<macho::Section> Sect;
387 getSection(DRI, Sect);
388 Result = Sect->Size;
389 }
Michael J. Spencer25b15772011-06-25 17:55:23 +0000390 return object_error::success;
Eric Christopher6256b032011-04-22 03:19:48 +0000391}
392
Michael J. Spencer25b15772011-06-25 17:55:23 +0000393error_code MachOObjectFile::getSectionContents(DataRefImpl DRI,
394 StringRef &Result) const {
Benjamin Kramer7d145782011-07-15 00:14:48 +0000395 if (is64BitLoadCommand(MachOObj, DRI)) {
396 InMemoryStruct<macho::Section64> Sect;
397 getSection64(DRI, Sect);
398 Result = MachOObj->getData(Sect->Offset, Sect->Size);
399 } else {
400 InMemoryStruct<macho::Section> Sect;
401 getSection(DRI, Sect);
402 Result = MachOObj->getData(Sect->Offset, Sect->Size);
403 }
Michael J. Spencer25b15772011-06-25 17:55:23 +0000404 return object_error::success;
Eric Christopher6256b032011-04-22 03:19:48 +0000405}
406
Michael J. Spencere2f2f072011-10-10 21:55:43 +0000407error_code MachOObjectFile::getSectionAlignment(DataRefImpl DRI,
408 uint64_t &Result) const {
409 if (is64BitLoadCommand(MachOObj, DRI)) {
410 InMemoryStruct<macho::Section64> Sect;
411 getSection64(DRI, Sect);
Michael J. Spencer15565ad2011-10-10 23:36:56 +0000412 Result = uint64_t(1) << Sect->Align;
Michael J. Spencere2f2f072011-10-10 21:55:43 +0000413 } else {
414 InMemoryStruct<macho::Section> Sect;
415 getSection(DRI, Sect);
Michael J. Spencer15565ad2011-10-10 23:36:56 +0000416 Result = uint64_t(1) << Sect->Align;
Michael J. Spencere2f2f072011-10-10 21:55:43 +0000417 }
418 return object_error::success;
419}
420
Michael J. Spencer25b15772011-06-25 17:55:23 +0000421error_code MachOObjectFile::isSectionText(DataRefImpl DRI,
422 bool &Result) const {
Benjamin Kramer7d145782011-07-15 00:14:48 +0000423 if (is64BitLoadCommand(MachOObj, DRI)) {
424 InMemoryStruct<macho::Section64> Sect;
425 getSection64(DRI, Sect);
426 Result = !strcmp(Sect->Name, "__text");
427 } else {
428 InMemoryStruct<macho::Section> Sect;
429 getSection(DRI, Sect);
430 Result = !strcmp(Sect->Name, "__text");
431 }
Michael J. Spencer25b15772011-06-25 17:55:23 +0000432 return object_error::success;
Eric Christopher6256b032011-04-22 03:19:48 +0000433}
434
Michael J. Spencer13afc5e2011-09-28 20:57:30 +0000435error_code MachOObjectFile::isSectionData(DataRefImpl DRI,
436 bool &Result) const {
437 // FIXME: Unimplemented.
438 Result = false;
439 return object_error::success;
440}
441
442error_code MachOObjectFile::isSectionBSS(DataRefImpl DRI,
443 bool &Result) const {
444 // FIXME: Unimplemented.
445 Result = false;
446 return object_error::success;
447}
448
Benjamin Kramer07ea23a2011-07-15 18:39:21 +0000449error_code MachOObjectFile::sectionContainsSymbol(DataRefImpl Sec,
450 DataRefImpl Symb,
451 bool &Result) const {
Owen Andersoncd749882011-10-12 22:21:32 +0000452 SymbolRef::SymbolType ST;
453 getSymbolType(Symb, ST);
454 if (ST == SymbolRef::ST_External) {
455 Result = false;
456 return object_error::success;
457 }
458
459 uint64_t SectBegin, SectEnd;
460 getSectionAddress(Sec, SectBegin);
461 getSectionSize(Sec, SectEnd);
462 SectEnd += SectBegin;
463
Benjamin Kramer07ea23a2011-07-15 18:39:21 +0000464 if (MachOObj->is64Bit()) {
465 InMemoryStruct<macho::Symbol64TableEntry> Entry;
466 getSymbol64TableEntry(Symb, Entry);
Owen Andersoncd749882011-10-12 22:21:32 +0000467 uint64_t SymAddr= Entry->Value;
468 Result = (SymAddr >= SectBegin) && (SymAddr < SectEnd);
Benjamin Kramer07ea23a2011-07-15 18:39:21 +0000469 } else {
470 InMemoryStruct<macho::SymbolTableEntry> Entry;
471 getSymbolTableEntry(Symb, Entry);
Owen Andersoncd749882011-10-12 22:21:32 +0000472 uint64_t SymAddr= Entry->Value;
473 Result = (SymAddr >= SectBegin) && (SymAddr < SectEnd);
Benjamin Kramer07ea23a2011-07-15 18:39:21 +0000474 }
Owen Andersoncd749882011-10-12 22:21:32 +0000475
Benjamin Kramer07ea23a2011-07-15 18:39:21 +0000476 return object_error::success;
477}
478
Michael J. Spencer4344b1e2011-10-07 19:25:32 +0000479relocation_iterator MachOObjectFile::getSectionRelBegin(DataRefImpl Sec) const {
480 DataRefImpl ret;
481 ret.d.a = 0;
482 ret.d.b = getSectionIndex(Sec);
483 return relocation_iterator(RelocationRef(ret, this));
484}
485relocation_iterator MachOObjectFile::getSectionRelEnd(DataRefImpl Sec) const {
486 uint32_t last_reloc;
487 if (is64BitLoadCommand(MachOObj, Sec)) {
488 InMemoryStruct<macho::Section64> Sect;
489 getSection64(Sec, Sect);
490 last_reloc = Sect->NumRelocationTableEntries;
491 } else {
492 InMemoryStruct<macho::Section> Sect;
493 getSection(Sec, Sect);
494 last_reloc = Sect->NumRelocationTableEntries;
495 }
496 DataRefImpl ret;
497 ret.d.a = last_reloc;
498 ret.d.b = getSectionIndex(Sec);
499 return relocation_iterator(RelocationRef(ret, this));
500}
501
502section_iterator MachOObjectFile::begin_sections() const {
Eric Christopher6256b032011-04-22 03:19:48 +0000503 DataRefImpl DRI;
504 DRI.d.a = DRI.d.b = 0;
505 moveToNextSection(DRI);
506 return section_iterator(SectionRef(DRI, this));
507}
508
Michael J. Spencer4344b1e2011-10-07 19:25:32 +0000509section_iterator MachOObjectFile::end_sections() const {
Eric Christopher6256b032011-04-22 03:19:48 +0000510 DataRefImpl DRI;
511 DRI.d.a = MachOObj->getHeader().NumLoadCommands;
512 DRI.d.b = 0;
513 return section_iterator(SectionRef(DRI, this));
514}
515
Benjamin Kramer0fcab072011-09-08 20:52:17 +0000516/*===-- Relocations -------------------------------------------------------===*/
517
518void MachOObjectFile::
519getRelocation(DataRefImpl Rel,
520 InMemoryStruct<macho::RelocationEntry> &Res) const {
521 uint32_t relOffset;
522 if (MachOObj->is64Bit()) {
523 InMemoryStruct<macho::Section64> Sect;
524 getSection64(Sections[Rel.d.b], Sect);
525 relOffset = Sect->RelocationTableOffset;
526 } else {
527 InMemoryStruct<macho::Section> Sect;
528 getSection(Sections[Rel.d.b], Sect);
529 relOffset = Sect->RelocationTableOffset;
530 }
531 MachOObj->ReadRelocationEntry(relOffset, Rel.d.a, Res);
532}
533error_code MachOObjectFile::getRelocationNext(DataRefImpl Rel,
534 RelocationRef &Res) const {
535 ++Rel.d.a;
Benjamin Kramer0fcab072011-09-08 20:52:17 +0000536 Res = RelocationRef(Rel, this);
537 return object_error::success;
538}
539error_code MachOObjectFile::getRelocationAddress(DataRefImpl Rel,
540 uint64_t &Res) const {
541 const uint8_t* sectAddress = base();
542 if (MachOObj->is64Bit()) {
543 InMemoryStruct<macho::Section64> Sect;
544 getSection64(Sections[Rel.d.b], Sect);
545 sectAddress += Sect->Offset;
546 } else {
547 InMemoryStruct<macho::Section> Sect;
548 getSection(Sections[Rel.d.b], Sect);
549 sectAddress += Sect->Offset;
550 }
551 InMemoryStruct<macho::RelocationEntry> RE;
552 getRelocation(Rel, RE);
553 Res = reinterpret_cast<uintptr_t>(sectAddress + RE->Word0);
554 return object_error::success;
555}
556error_code MachOObjectFile::getRelocationSymbol(DataRefImpl Rel,
557 SymbolRef &Res) const {
558 InMemoryStruct<macho::RelocationEntry> RE;
559 getRelocation(Rel, RE);
560 uint32_t SymbolIdx = RE->Word1 & 0xffffff;
561 bool isExtern = (RE->Word1 >> 27) & 1;
562
563 DataRefImpl Sym;
564 Sym.d.a = Sym.d.b = 0;
565 moveToNextSymbol(Sym);
Benjamin Kramer0fcab072011-09-08 20:52:17 +0000566 if (isExtern) {
567 for (unsigned i = 0; i < SymbolIdx; i++) {
568 Sym.d.b++;
569 moveToNextSymbol(Sym);
Nick Lewycky58856ea2011-09-09 00:16:50 +0000570 assert(Sym.d.a < MachOObj->getHeader().NumLoadCommands &&
Benjamin Kramer0fcab072011-09-08 20:52:17 +0000571 "Relocation symbol index out of range!");
572 }
573 }
574 Res = SymbolRef(Sym, this);
575 return object_error::success;
576}
577error_code MachOObjectFile::getRelocationType(DataRefImpl Rel,
578 uint32_t &Res) const {
579 InMemoryStruct<macho::RelocationEntry> RE;
580 getRelocation(Rel, RE);
581 Res = RE->Word1;
582 return object_error::success;
583}
Michael J. Spencer4344b1e2011-10-07 19:25:32 +0000584error_code MachOObjectFile::getRelocationTypeName(DataRefImpl Rel,
585 SmallVectorImpl<char> &Result) const {
586 return object_error::success;
587}
Benjamin Kramer0fcab072011-09-08 20:52:17 +0000588error_code MachOObjectFile::getRelocationAdditionalInfo(DataRefImpl Rel,
589 int64_t &Res) const {
590 InMemoryStruct<macho::RelocationEntry> RE;
591 getRelocation(Rel, RE);
592 bool isExtern = (RE->Word1 >> 27) & 1;
593 Res = 0;
594 if (!isExtern) {
595 const uint8_t* sectAddress = base();
596 if (MachOObj->is64Bit()) {
597 InMemoryStruct<macho::Section64> Sect;
598 getSection64(Sections[Rel.d.b], Sect);
599 sectAddress += Sect->Offset;
600 } else {
601 InMemoryStruct<macho::Section> Sect;
602 getSection(Sections[Rel.d.b], Sect);
603 sectAddress += Sect->Offset;
604 }
605 Res = reinterpret_cast<uintptr_t>(sectAddress);
606 }
607 return object_error::success;
608}
Michael J. Spencer4344b1e2011-10-07 19:25:32 +0000609error_code MachOObjectFile::getRelocationValueString(DataRefImpl Rel,
610 SmallVectorImpl<char> &Result) const {
611 return object_error::success;
Benjamin Kramer0fcab072011-09-08 20:52:17 +0000612}
613
Eric Christopher6256b032011-04-22 03:19:48 +0000614/*===-- Miscellaneous -----------------------------------------------------===*/
615
616uint8_t MachOObjectFile::getBytesInAddress() const {
617 return MachOObj->is64Bit() ? 8 : 4;
618}
619
620StringRef MachOObjectFile::getFileFormatName() const {
621 if (!MachOObj->is64Bit()) {
622 switch (MachOObj->getHeader().CPUType) {
Eric Christopherf4b2f932011-04-22 06:34:01 +0000623 case llvm::MachO::CPUTypeI386:
Eric Christopher9ab1d7f2011-04-22 04:08:58 +0000624 return "Mach-O 32-bit i386";
Eric Christopherf4b2f932011-04-22 06:34:01 +0000625 case llvm::MachO::CPUTypeARM:
Eric Christopher9ab1d7f2011-04-22 04:08:58 +0000626 return "Mach-O arm";
Eric Christopherf4b2f932011-04-22 06:34:01 +0000627 case llvm::MachO::CPUTypePowerPC:
Eric Christopher9ab1d7f2011-04-22 04:08:58 +0000628 return "Mach-O 32-bit ppc";
629 default:
Eric Christopherf4b2f932011-04-22 06:34:01 +0000630 assert((MachOObj->getHeader().CPUType & llvm::MachO::CPUArchABI64) == 0 &&
Eric Christopher9ab1d7f2011-04-22 04:08:58 +0000631 "64-bit object file when we're not 64-bit?");
632 return "Mach-O 32-bit unknown";
Eric Christopher6256b032011-04-22 03:19:48 +0000633 }
634 }
635
636 switch (MachOObj->getHeader().CPUType) {
Eric Christopherf4b2f932011-04-22 06:34:01 +0000637 case llvm::MachO::CPUTypeX86_64:
Eric Christopher9ab1d7f2011-04-22 04:08:58 +0000638 return "Mach-O 64-bit x86-64";
Eric Christopherf4b2f932011-04-22 06:34:01 +0000639 case llvm::MachO::CPUTypePowerPC64:
Eric Christopher9ab1d7f2011-04-22 04:08:58 +0000640 return "Mach-O 64-bit ppc64";
Eric Christopher6256b032011-04-22 03:19:48 +0000641 default:
Eric Christopherf4b2f932011-04-22 06:34:01 +0000642 assert((MachOObj->getHeader().CPUType & llvm::MachO::CPUArchABI64) == 1 &&
Eric Christopher9ab1d7f2011-04-22 04:08:58 +0000643 "32-bit object file when we're 64-bit?");
644 return "Mach-O 64-bit unknown";
Eric Christopher6256b032011-04-22 03:19:48 +0000645 }
646}
647
648unsigned MachOObjectFile::getArch() const {
649 switch (MachOObj->getHeader().CPUType) {
Eric Christopherf4b2f932011-04-22 06:34:01 +0000650 case llvm::MachO::CPUTypeI386:
Eric Christopher6256b032011-04-22 03:19:48 +0000651 return Triple::x86;
Eric Christopherf4b2f932011-04-22 06:34:01 +0000652 case llvm::MachO::CPUTypeX86_64:
Eric Christopher6256b032011-04-22 03:19:48 +0000653 return Triple::x86_64;
Eric Christopherf4b2f932011-04-22 06:34:01 +0000654 case llvm::MachO::CPUTypeARM:
Eric Christopher6256b032011-04-22 03:19:48 +0000655 return Triple::arm;
Eric Christopherf4b2f932011-04-22 06:34:01 +0000656 case llvm::MachO::CPUTypePowerPC:
Eric Christopher6256b032011-04-22 03:19:48 +0000657 return Triple::ppc;
Eric Christopherf4b2f932011-04-22 06:34:01 +0000658 case llvm::MachO::CPUTypePowerPC64:
Eric Christopher6256b032011-04-22 03:19:48 +0000659 return Triple::ppc64;
660 default:
661 return Triple::UnknownArch;
662 }
663}
664
Owen Andersonf7c93a32011-10-11 17:32:27 +0000665} // end namespace object
Eric Christopher6256b032011-04-22 03:19:48 +0000666} // end namespace llvm