blob: 0ad88934005c56206d774af0b2c7623434512891 [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
Owen Andersonf7c93a32011-10-11 17:32:27 +000015#include "llvm/Object/MachO.h"
Chandler Carruthd04a8d42012-12-03 16:50:05 +000016#include "llvm/ADT/Triple.h"
Eric Christopher6256b032011-04-22 03:19:48 +000017#include "llvm/Object/MachOFormat.h"
Owen Anderson1832f4d2011-10-26 20:42:54 +000018#include "llvm/Support/Format.h"
Eric Christopher6256b032011-04-22 03:19:48 +000019#include "llvm/Support/MemoryBuffer.h"
Eric Christopher6256b032011-04-22 03:19:48 +000020#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)
David Meyer6f9489a2012-03-09 20:41:57 +000032 : ObjectFile(Binary::ID_MachO, Object, ec),
Benjamin Kramer0fcab072011-09-08 20:52:17 +000033 MachOObj(MOO),
34 RegisteredStringTable(std::numeric_limits<uint32_t>::max()) {
35 DataRefImpl DRI;
Benjamin Kramer0fcab072011-09-08 20:52:17 +000036 moveToNextSection(DRI);
37 uint32_t LoadCommandCount = MachOObj->getHeader().NumLoadCommands;
38 while (DRI.d.a < LoadCommandCount) {
39 Sections.push_back(DRI);
Benjamin Kramer0fcab072011-09-08 20:52:17 +000040 DRI.d.b++;
41 moveToNextSection(DRI);
42 }
43}
44
45
Eric Christopher6256b032011-04-22 03:19:48 +000046ObjectFile *ObjectFile::createMachOObjectFile(MemoryBuffer *Buffer) {
Michael J. Spencer001c9202011-06-25 17:54:50 +000047 error_code ec;
Eric Christopher6256b032011-04-22 03:19:48 +000048 std::string Err;
49 MachOObject *MachOObj = MachOObject::LoadFromBuffer(Buffer, &Err);
50 if (!MachOObj)
51 return NULL;
Jim Grosbach596e4742012-11-29 19:14:11 +000052 // MachOObject takes ownership of the Buffer we passed to it, and
53 // MachOObjectFile does, too, so we need to make sure they don't get the
54 // same object. A MemoryBuffer is cheap (it's just a reference to memory,
55 // not a copy of the memory itself), so just make a new copy here for
56 // the MachOObjectFile.
57 MemoryBuffer *NewBuffer =
Benjamin Kramerf56c3e22012-11-29 20:08:03 +000058 MemoryBuffer::getMemBuffer(Buffer->getBuffer(),
59 Buffer->getBufferIdentifier(), false);
Jim Grosbach596e4742012-11-29 19:14:11 +000060 return new MachOObjectFile(NewBuffer, MachOObj, ec);
Eric Christopher6256b032011-04-22 03:19:48 +000061}
62
63/*===-- Symbols -----------------------------------------------------------===*/
64
65void MachOObjectFile::moveToNextSymbol(DataRefImpl &DRI) const {
66 uint32_t LoadCommandCount = MachOObj->getHeader().NumLoadCommands;
67 while (DRI.d.a < LoadCommandCount) {
68 LoadCommandInfo LCI = MachOObj->getLoadCommandInfo(DRI.d.a);
69 if (LCI.Command.Type == macho::LCT_Symtab) {
70 InMemoryStruct<macho::SymtabLoadCommand> SymtabLoadCmd;
71 MachOObj->ReadSymtabLoadCommand(LCI, SymtabLoadCmd);
72 if (DRI.d.b < SymtabLoadCmd->NumSymbolTableEntries)
73 return;
74 }
75
76 DRI.d.a++;
77 DRI.d.b = 0;
78 }
79}
80
81void MachOObjectFile::getSymbolTableEntry(DataRefImpl DRI,
82 InMemoryStruct<macho::SymbolTableEntry> &Res) const {
83 InMemoryStruct<macho::SymtabLoadCommand> SymtabLoadCmd;
84 LoadCommandInfo LCI = MachOObj->getLoadCommandInfo(DRI.d.a);
85 MachOObj->ReadSymtabLoadCommand(LCI, SymtabLoadCmd);
86
87 if (RegisteredStringTable != DRI.d.a) {
88 MachOObj->RegisterStringTable(*SymtabLoadCmd);
89 RegisteredStringTable = DRI.d.a;
90 }
91
92 MachOObj->ReadSymbolTableEntry(SymtabLoadCmd->SymbolTableOffset, DRI.d.b,
93 Res);
94}
95
Benjamin Kramer32fb2af2011-07-15 17:32:45 +000096void MachOObjectFile::getSymbol64TableEntry(DataRefImpl DRI,
97 InMemoryStruct<macho::Symbol64TableEntry> &Res) const {
98 InMemoryStruct<macho::SymtabLoadCommand> SymtabLoadCmd;
99 LoadCommandInfo LCI = MachOObj->getLoadCommandInfo(DRI.d.a);
100 MachOObj->ReadSymtabLoadCommand(LCI, SymtabLoadCmd);
101
102 if (RegisteredStringTable != DRI.d.a) {
103 MachOObj->RegisterStringTable(*SymtabLoadCmd);
104 RegisteredStringTable = DRI.d.a;
105 }
106
107 MachOObj->ReadSymbol64TableEntry(SymtabLoadCmd->SymbolTableOffset, DRI.d.b,
108 Res);
109}
110
Eric Christopher6256b032011-04-22 03:19:48 +0000111
Michael J. Spencer25b15772011-06-25 17:55:23 +0000112error_code MachOObjectFile::getSymbolNext(DataRefImpl DRI,
113 SymbolRef &Result) const {
Eric Christopher6256b032011-04-22 03:19:48 +0000114 DRI.d.b++;
115 moveToNextSymbol(DRI);
Michael J. Spencer25b15772011-06-25 17:55:23 +0000116 Result = SymbolRef(DRI, this);
117 return object_error::success;
Eric Christopher6256b032011-04-22 03:19:48 +0000118}
119
Michael J. Spencer25b15772011-06-25 17:55:23 +0000120error_code MachOObjectFile::getSymbolName(DataRefImpl DRI,
121 StringRef &Result) const {
Benjamin Kramer32fb2af2011-07-15 17:32:45 +0000122 if (MachOObj->is64Bit()) {
123 InMemoryStruct<macho::Symbol64TableEntry> Entry;
124 getSymbol64TableEntry(DRI, Entry);
125 Result = MachOObj->getStringAtIndex(Entry->StringIndex);
126 } else {
127 InMemoryStruct<macho::SymbolTableEntry> Entry;
128 getSymbolTableEntry(DRI, Entry);
129 Result = MachOObj->getStringAtIndex(Entry->StringIndex);
130 }
Michael J. Spencer25b15772011-06-25 17:55:23 +0000131 return object_error::success;
Eric Christopher6256b032011-04-22 03:19:48 +0000132}
133
Danil Malyshevb0436a72011-11-29 17:40:10 +0000134error_code MachOObjectFile::getSymbolFileOffset(DataRefImpl DRI,
135 uint64_t &Result) const {
Benjamin Kramer32fb2af2011-07-15 17:32:45 +0000136 if (MachOObj->is64Bit()) {
137 InMemoryStruct<macho::Symbol64TableEntry> Entry;
138 getSymbol64TableEntry(DRI, Entry);
139 Result = Entry->Value;
Danil Malyshevb0436a72011-11-29 17:40:10 +0000140 if (Entry->SectionIndex) {
141 InMemoryStruct<macho::Section64> Section;
142 getSection64(Sections[Entry->SectionIndex-1], Section);
143 Result += Section->Offset - Section->Address;
144 }
Benjamin Kramer32fb2af2011-07-15 17:32:45 +0000145 } else {
146 InMemoryStruct<macho::SymbolTableEntry> Entry;
147 getSymbolTableEntry(DRI, Entry);
148 Result = Entry->Value;
Danil Malyshevb0436a72011-11-29 17:40:10 +0000149 if (Entry->SectionIndex) {
150 InMemoryStruct<macho::Section> Section;
151 getSection(Sections[Entry->SectionIndex-1], Section);
152 Result += Section->Offset - Section->Address;
153 }
Benjamin Kramer32fb2af2011-07-15 17:32:45 +0000154 }
Owen Anderson95f8db42011-10-12 22:37:10 +0000155
Michael J. Spencer25b15772011-06-25 17:55:23 +0000156 return object_error::success;
Eric Christopher6256b032011-04-22 03:19:48 +0000157}
158
Benjamin Kramerac241fe2011-09-14 01:22:52 +0000159error_code MachOObjectFile::getSymbolAddress(DataRefImpl DRI,
160 uint64_t &Result) const {
Benjamin Kramerac241fe2011-09-14 01:22:52 +0000161 if (MachOObj->is64Bit()) {
162 InMemoryStruct<macho::Symbol64TableEntry> Entry;
163 getSymbol64TableEntry(DRI, Entry);
Owen Anderson95f8db42011-10-12 22:37:10 +0000164 Result = Entry->Value;
Benjamin Kramerac241fe2011-09-14 01:22:52 +0000165 } else {
166 InMemoryStruct<macho::SymbolTableEntry> Entry;
167 getSymbolTableEntry(DRI, Entry);
Owen Anderson95f8db42011-10-12 22:37:10 +0000168 Result = Entry->Value;
Benjamin Kramerac241fe2011-09-14 01:22:52 +0000169 }
Benjamin Kramerac241fe2011-09-14 01:22:52 +0000170 return object_error::success;
171}
172
Michael J. Spencer25b15772011-06-25 17:55:23 +0000173error_code MachOObjectFile::getSymbolSize(DataRefImpl DRI,
174 uint64_t &Result) const {
Danil Malyshevb0436a72011-11-29 17:40:10 +0000175 uint32_t LoadCommandCount = MachOObj->getHeader().NumLoadCommands;
176 uint64_t BeginOffset;
177 uint64_t EndOffset = 0;
178 uint8_t SectionIndex;
179 if (MachOObj->is64Bit()) {
180 InMemoryStruct<macho::Symbol64TableEntry> Entry;
181 getSymbol64TableEntry(DRI, Entry);
182 BeginOffset = Entry->Value;
183 SectionIndex = Entry->SectionIndex;
184 if (!SectionIndex) {
Preston Gurdc68dda82012-04-12 20:13:57 +0000185 uint32_t flags = SymbolRef::SF_None;
186 getSymbolFlags(DRI, flags);
187 if (flags & SymbolRef::SF_Common)
188 Result = Entry->Value;
189 else
190 Result = UnknownAddressOrSize;
Danil Malyshevb0436a72011-11-29 17:40:10 +0000191 return object_error::success;
192 }
193 // Unfortunately symbols are unsorted so we need to touch all
194 // symbols from load command
195 DRI.d.b = 0;
196 uint32_t Command = DRI.d.a;
197 while (Command == DRI.d.a) {
198 moveToNextSymbol(DRI);
199 if (DRI.d.a < LoadCommandCount) {
200 getSymbol64TableEntry(DRI, Entry);
201 if (Entry->SectionIndex == SectionIndex && Entry->Value > BeginOffset)
202 if (!EndOffset || Entry->Value < EndOffset)
203 EndOffset = Entry->Value;
204 }
205 DRI.d.b++;
206 }
207 } else {
208 InMemoryStruct<macho::SymbolTableEntry> Entry;
209 getSymbolTableEntry(DRI, Entry);
210 BeginOffset = Entry->Value;
211 SectionIndex = Entry->SectionIndex;
212 if (!SectionIndex) {
Preston Gurdc68dda82012-04-12 20:13:57 +0000213 uint32_t flags = SymbolRef::SF_None;
214 getSymbolFlags(DRI, flags);
215 if (flags & SymbolRef::SF_Common)
216 Result = Entry->Value;
217 else
218 Result = UnknownAddressOrSize;
Danil Malyshevb0436a72011-11-29 17:40:10 +0000219 return object_error::success;
220 }
221 // Unfortunately symbols are unsorted so we need to touch all
222 // symbols from load command
223 DRI.d.b = 0;
224 uint32_t Command = DRI.d.a;
225 while (Command == DRI.d.a) {
226 moveToNextSymbol(DRI);
227 if (DRI.d.a < LoadCommandCount) {
228 getSymbolTableEntry(DRI, Entry);
229 if (Entry->SectionIndex == SectionIndex && Entry->Value > BeginOffset)
230 if (!EndOffset || Entry->Value < EndOffset)
231 EndOffset = Entry->Value;
232 }
233 DRI.d.b++;
234 }
235 }
236 if (!EndOffset) {
237 uint64_t Size;
238 getSectionSize(Sections[SectionIndex-1], Size);
239 getSectionAddress(Sections[SectionIndex-1], EndOffset);
240 EndOffset += Size;
241 }
242 Result = EndOffset - BeginOffset;
Michael J. Spencer25b15772011-06-25 17:55:23 +0000243 return object_error::success;
Eric Christopher6256b032011-04-22 03:19:48 +0000244}
245
Michael J. Spencer25b15772011-06-25 17:55:23 +0000246error_code MachOObjectFile::getSymbolNMTypeChar(DataRefImpl DRI,
247 char &Result) const {
Benjamin Kramer32fb2af2011-07-15 17:32:45 +0000248 uint8_t Type, Flags;
249 if (MachOObj->is64Bit()) {
250 InMemoryStruct<macho::Symbol64TableEntry> Entry;
251 getSymbol64TableEntry(DRI, Entry);
252 Type = Entry->Type;
253 Flags = Entry->Flags;
254 } else {
255 InMemoryStruct<macho::SymbolTableEntry> Entry;
256 getSymbolTableEntry(DRI, Entry);
257 Type = Entry->Type;
258 Flags = Entry->Flags;
259 }
Eric Christopher6256b032011-04-22 03:19:48 +0000260
261 char Char;
Benjamin Kramer32fb2af2011-07-15 17:32:45 +0000262 switch (Type & macho::STF_TypeMask) {
Eric Christopher6256b032011-04-22 03:19:48 +0000263 case macho::STT_Undefined:
264 Char = 'u';
265 break;
266 case macho::STT_Absolute:
267 case macho::STT_Section:
268 Char = 's';
269 break;
270 default:
271 Char = '?';
272 break;
273 }
274
Benjamin Kramer32fb2af2011-07-15 17:32:45 +0000275 if (Flags & (macho::STF_External | macho::STF_PrivateExtern))
Eric Christopher6256b032011-04-22 03:19:48 +0000276 Char = toupper(Char);
Michael J. Spencer25b15772011-06-25 17:55:23 +0000277 Result = Char;
278 return object_error::success;
Eric Christopher6256b032011-04-22 03:19:48 +0000279}
280
David Meyerc46255a2012-02-28 23:47:53 +0000281error_code MachOObjectFile::getSymbolFlags(DataRefImpl DRI,
282 uint32_t &Result) const {
283 uint16_t MachOFlags;
284 uint8_t MachOType;
Benjamin Kramer32fb2af2011-07-15 17:32:45 +0000285 if (MachOObj->is64Bit()) {
286 InMemoryStruct<macho::Symbol64TableEntry> Entry;
287 getSymbol64TableEntry(DRI, Entry);
David Meyerc46255a2012-02-28 23:47:53 +0000288 MachOFlags = Entry->Flags;
289 MachOType = Entry->Type;
Benjamin Kramer32fb2af2011-07-15 17:32:45 +0000290 } else {
291 InMemoryStruct<macho::SymbolTableEntry> Entry;
292 getSymbolTableEntry(DRI, Entry);
David Meyerc46255a2012-02-28 23:47:53 +0000293 MachOFlags = Entry->Flags;
294 MachOType = Entry->Type;
Michael J. Spencer9b2b8122011-10-17 23:54:46 +0000295 }
296
Preston Gurdc68dda82012-04-12 20:13:57 +0000297 // TODO: Correctly set SF_ThreadLocal
David Meyerc46255a2012-02-28 23:47:53 +0000298 Result = SymbolRef::SF_None;
David Meyer2c677272012-02-29 02:11:55 +0000299
300 if ((MachOType & MachO::NlistMaskType) == MachO::NListTypeUndefined)
301 Result |= SymbolRef::SF_Undefined;
302
David Meyerc46255a2012-02-28 23:47:53 +0000303 if (MachOFlags & macho::STF_StabsEntryMask)
304 Result |= SymbolRef::SF_FormatSpecific;
305
Preston Gurdc68dda82012-04-12 20:13:57 +0000306 if (MachOType & MachO::NlistMaskExternal) {
David Meyerc46255a2012-02-28 23:47:53 +0000307 Result |= SymbolRef::SF_Global;
Preston Gurdc68dda82012-04-12 20:13:57 +0000308 if ((MachOType & MachO::NlistMaskType) == MachO::NListTypeUndefined)
309 Result |= SymbolRef::SF_Common;
310 }
David Meyerc46255a2012-02-28 23:47:53 +0000311
312 if (MachOFlags & (MachO::NListDescWeakRef | MachO::NListDescWeakDef))
313 Result |= SymbolRef::SF_Weak;
314
315 if ((MachOType & MachO::NlistMaskType) == MachO::NListTypeAbsolute)
316 Result |= SymbolRef::SF_Absolute;
317
Michael J. Spencer9b2b8122011-10-17 23:54:46 +0000318 return object_error::success;
319}
320
321error_code MachOObjectFile::getSymbolSection(DataRefImpl Symb,
322 section_iterator &Res) const {
323 uint8_t index;
324 if (MachOObj->is64Bit()) {
325 InMemoryStruct<macho::Symbol64TableEntry> Entry;
326 getSymbol64TableEntry(Symb, Entry);
327 index = Entry->SectionIndex;
328 } else {
329 InMemoryStruct<macho::SymbolTableEntry> Entry;
330 getSymbolTableEntry(Symb, Entry);
331 index = Entry->SectionIndex;
332 }
333
334 if (index == 0)
335 Res = end_sections();
336 else
Danil Malyshevb0436a72011-11-29 17:40:10 +0000337 Res = section_iterator(SectionRef(Sections[index-1], this));
Michael J. Spencer9b2b8122011-10-17 23:54:46 +0000338
339 return object_error::success;
340}
341
Benjamin Kramerac241fe2011-09-14 01:22:52 +0000342error_code MachOObjectFile::getSymbolType(DataRefImpl Symb,
Michael J. Spencer1130a792011-10-17 20:19:29 +0000343 SymbolRef::Type &Res) const {
Benjamin Kramerac241fe2011-09-14 01:22:52 +0000344 uint8_t n_type;
345 if (MachOObj->is64Bit()) {
346 InMemoryStruct<macho::Symbol64TableEntry> Entry;
347 getSymbol64TableEntry(Symb, Entry);
348 n_type = Entry->Type;
349 } else {
350 InMemoryStruct<macho::SymbolTableEntry> Entry;
351 getSymbolTableEntry(Symb, Entry);
352 n_type = Entry->Type;
353 }
354 Res = SymbolRef::ST_Other;
Owen Anderson10a8c622011-10-12 22:23:12 +0000355
356 // If this is a STAB debugging symbol, we can do nothing more.
Owen Andersona48aab92011-10-21 19:26:54 +0000357 if (n_type & MachO::NlistMaskStab) {
358 Res = SymbolRef::ST_Debug;
Owen Anderson10a8c622011-10-12 22:23:12 +0000359 return object_error::success;
Owen Andersona48aab92011-10-21 19:26:54 +0000360 }
Owen Anderson10a8c622011-10-12 22:23:12 +0000361
Benjamin Kramerac241fe2011-09-14 01:22:52 +0000362 switch (n_type & MachO::NlistMaskType) {
363 case MachO::NListTypeUndefined :
David Meyer2c677272012-02-29 02:11:55 +0000364 Res = SymbolRef::ST_Unknown;
Benjamin Kramerac241fe2011-09-14 01:22:52 +0000365 break;
366 case MachO::NListTypeSection :
367 Res = SymbolRef::ST_Function;
368 break;
369 }
370 return object_error::success;
371}
372
Tim Northovera41dce32012-10-29 10:47:00 +0000373error_code MachOObjectFile::getSymbolValue(DataRefImpl Symb,
374 uint64_t &Val) const {
375 report_fatal_error("getSymbolValue unimplemented in MachOObjectFile");
376}
Benjamin Kramerac241fe2011-09-14 01:22:52 +0000377
Michael J. Spencer4344b1e2011-10-07 19:25:32 +0000378symbol_iterator MachOObjectFile::begin_symbols() const {
Eric Christopher6256b032011-04-22 03:19:48 +0000379 // DRI.d.a = segment number; DRI.d.b = symbol index.
380 DataRefImpl DRI;
Eric Christopher6256b032011-04-22 03:19:48 +0000381 moveToNextSymbol(DRI);
382 return symbol_iterator(SymbolRef(DRI, this));
383}
384
Michael J. Spencer4344b1e2011-10-07 19:25:32 +0000385symbol_iterator MachOObjectFile::end_symbols() const {
Eric Christopher6256b032011-04-22 03:19:48 +0000386 DataRefImpl DRI;
387 DRI.d.a = MachOObj->getHeader().NumLoadCommands;
Eric Christopher6256b032011-04-22 03:19:48 +0000388 return symbol_iterator(SymbolRef(DRI, this));
389}
390
Michael J. Spencerdfa18962012-02-28 00:40:37 +0000391symbol_iterator MachOObjectFile::begin_dynamic_symbols() const {
392 // TODO: implement
393 report_fatal_error("Dynamic symbols unimplemented in MachOObjectFile");
394}
395
396symbol_iterator MachOObjectFile::end_dynamic_symbols() const {
397 // TODO: implement
398 report_fatal_error("Dynamic symbols unimplemented in MachOObjectFile");
399}
Eric Christopher6256b032011-04-22 03:19:48 +0000400
David Meyer5c2b4ea2012-03-01 01:36:50 +0000401library_iterator MachOObjectFile::begin_libraries_needed() const {
402 // TODO: implement
403 report_fatal_error("Needed libraries unimplemented in MachOObjectFile");
404}
405
406library_iterator MachOObjectFile::end_libraries_needed() const {
407 // TODO: implement
408 report_fatal_error("Needed libraries unimplemented in MachOObjectFile");
409}
410
David Meyer97f77872012-03-01 22:19:54 +0000411StringRef MachOObjectFile::getLoadName() const {
412 // TODO: Implement
413 report_fatal_error("get_load_name() unimplemented in MachOObjectFile");
414}
415
Eric Christopher6256b032011-04-22 03:19:48 +0000416/*===-- Sections ----------------------------------------------------------===*/
417
418void MachOObjectFile::moveToNextSection(DataRefImpl &DRI) const {
419 uint32_t LoadCommandCount = MachOObj->getHeader().NumLoadCommands;
420 while (DRI.d.a < LoadCommandCount) {
421 LoadCommandInfo LCI = MachOObj->getLoadCommandInfo(DRI.d.a);
422 if (LCI.Command.Type == macho::LCT_Segment) {
423 InMemoryStruct<macho::SegmentLoadCommand> SegmentLoadCmd;
424 MachOObj->ReadSegmentLoadCommand(LCI, SegmentLoadCmd);
425 if (DRI.d.b < SegmentLoadCmd->NumSections)
426 return;
427 } else if (LCI.Command.Type == macho::LCT_Segment64) {
428 InMemoryStruct<macho::Segment64LoadCommand> Segment64LoadCmd;
429 MachOObj->ReadSegment64LoadCommand(LCI, Segment64LoadCmd);
430 if (DRI.d.b < Segment64LoadCmd->NumSections)
431 return;
432 }
433
434 DRI.d.a++;
435 DRI.d.b = 0;
436 }
437}
438
Michael J. Spencer25b15772011-06-25 17:55:23 +0000439error_code MachOObjectFile::getSectionNext(DataRefImpl DRI,
440 SectionRef &Result) const {
Eric Christopher6256b032011-04-22 03:19:48 +0000441 DRI.d.b++;
442 moveToNextSection(DRI);
Michael J. Spencer25b15772011-06-25 17:55:23 +0000443 Result = SectionRef(DRI, this);
444 return object_error::success;
Eric Christopher6256b032011-04-22 03:19:48 +0000445}
446
447void
448MachOObjectFile::getSection(DataRefImpl DRI,
449 InMemoryStruct<macho::Section> &Res) const {
Eric Christopher6256b032011-04-22 03:19:48 +0000450 LoadCommandInfo LCI = MachOObj->getLoadCommandInfo(DRI.d.a);
Eric Christopher6256b032011-04-22 03:19:48 +0000451 MachOObj->ReadSection(LCI, DRI.d.b, Res);
452}
453
Michael J. Spencer4344b1e2011-10-07 19:25:32 +0000454std::size_t MachOObjectFile::getSectionIndex(DataRefImpl Sec) const {
455 SectionList::const_iterator loc =
456 std::find(Sections.begin(), Sections.end(), Sec);
457 assert(loc != Sections.end() && "Sec is not a valid section!");
458 return std::distance(Sections.begin(), loc);
459}
460
Benjamin Kramer7d145782011-07-15 00:14:48 +0000461void
462MachOObjectFile::getSection64(DataRefImpl DRI,
463 InMemoryStruct<macho::Section64> &Res) const {
Benjamin Kramer7d145782011-07-15 00:14:48 +0000464 LoadCommandInfo LCI = MachOObj->getLoadCommandInfo(DRI.d.a);
Benjamin Kramer7d145782011-07-15 00:14:48 +0000465 MachOObj->ReadSection64(LCI, DRI.d.b, Res);
466}
467
468static bool is64BitLoadCommand(const MachOObject *MachOObj, DataRefImpl DRI) {
469 LoadCommandInfo LCI = MachOObj->getLoadCommandInfo(DRI.d.a);
470 if (LCI.Command.Type == macho::LCT_Segment64)
471 return true;
472 assert(LCI.Command.Type == macho::LCT_Segment && "Unexpected Type.");
473 return false;
474}
475
Rafael Espindolacef81b32012-12-21 03:47:03 +0000476static StringRef parseSegmentOrSectionName(const char *P) {
477 if (P[15] == 0)
478 // Null terminated.
479 return P;
480 // Not null terminated, so this is a 16 char string.
481 return StringRef(P, 16);
482}
483
Michael J. Spencer25b15772011-06-25 17:55:23 +0000484error_code MachOObjectFile::getSectionName(DataRefImpl DRI,
485 StringRef &Result) const {
Jim Grosbach596e4742012-11-29 19:14:11 +0000486 if (is64BitLoadCommand(MachOObj.get(), DRI)) {
Benjamin Kramer7d145782011-07-15 00:14:48 +0000487 LoadCommandInfo LCI = MachOObj->getLoadCommandInfo(DRI.d.a);
Rafael Espindolacef81b32012-12-21 03:47:03 +0000488 unsigned SectionOffset = LCI.Offset + sizeof(macho::Segment64LoadCommand) +
489 DRI.d.b * sizeof(macho::Section64);
490 StringRef Data = MachOObj->getData(SectionOffset, sizeof(macho::Section64));
491 const macho::Section64 *sec =
492 reinterpret_cast<const macho::Section64*>(Data.data());
493 Result = parseSegmentOrSectionName(sec->Name);
Benjamin Kramer7d145782011-07-15 00:14:48 +0000494 } else {
Benjamin Kramer7d145782011-07-15 00:14:48 +0000495 LoadCommandInfo LCI = MachOObj->getLoadCommandInfo(DRI.d.a);
Rafael Espindolacef81b32012-12-21 03:47:03 +0000496 unsigned SectionOffset = LCI.Offset + sizeof(macho::SegmentLoadCommand) +
497 DRI.d.b * sizeof(macho::Section);
498 StringRef Data = MachOObj->getData(SectionOffset, sizeof(macho::Section));
499 const macho::Section *sec =
500 reinterpret_cast<const macho::Section*>(Data.data());
501 Result = parseSegmentOrSectionName(sec->Name);
Rafael Espindolaf9a6bd82012-12-19 14:15:04 +0000502 }
Rafael Espindolacef81b32012-12-21 03:47:03 +0000503 return object_error::success;
504}
505
506error_code MachOObjectFile::getSectionFinalSegmentName(DataRefImpl Sec,
507 StringRef &Res) const {
508 if (is64BitLoadCommand(MachOObj.get(), Sec)) {
509 LoadCommandInfo LCI = MachOObj->getLoadCommandInfo(Sec.d.a);
510 unsigned SectionOffset = LCI.Offset + sizeof(macho::Segment64LoadCommand) +
511 Sec.d.b * sizeof(macho::Section64);
512 StringRef Data = MachOObj->getData(SectionOffset, sizeof(macho::Section64));
513 const macho::Section64 *sec =
514 reinterpret_cast<const macho::Section64*>(Data.data());
515 Res = parseSegmentOrSectionName(sec->SegmentName);
516 } else {
517 LoadCommandInfo LCI = MachOObj->getLoadCommandInfo(Sec.d.a);
518 unsigned SectionOffset = LCI.Offset + sizeof(macho::SegmentLoadCommand) +
519 Sec.d.b * sizeof(macho::Section);
520 StringRef Data = MachOObj->getData(SectionOffset, sizeof(macho::Section));
521 const macho::Section *sec =
522 reinterpret_cast<const macho::Section*>(Data.data());
523 Res = parseSegmentOrSectionName(sec->SegmentName);
524 }
Michael J. Spencer25b15772011-06-25 17:55:23 +0000525 return object_error::success;
Eric Christopher6256b032011-04-22 03:19:48 +0000526}
527
Michael J. Spencer25b15772011-06-25 17:55:23 +0000528error_code MachOObjectFile::getSectionAddress(DataRefImpl DRI,
529 uint64_t &Result) const {
Jim Grosbach596e4742012-11-29 19:14:11 +0000530 if (is64BitLoadCommand(MachOObj.get(), DRI)) {
Benjamin Kramer7d145782011-07-15 00:14:48 +0000531 InMemoryStruct<macho::Section64> Sect;
532 getSection64(DRI, Sect);
533 Result = Sect->Address;
534 } else {
535 InMemoryStruct<macho::Section> Sect;
536 getSection(DRI, Sect);
537 Result = Sect->Address;
538 }
Michael J. Spencer25b15772011-06-25 17:55:23 +0000539 return object_error::success;
Eric Christopher6256b032011-04-22 03:19:48 +0000540}
541
Michael J. Spencer25b15772011-06-25 17:55:23 +0000542error_code MachOObjectFile::getSectionSize(DataRefImpl DRI,
543 uint64_t &Result) const {
Jim Grosbach596e4742012-11-29 19:14:11 +0000544 if (is64BitLoadCommand(MachOObj.get(), DRI)) {
Benjamin Kramer7d145782011-07-15 00:14:48 +0000545 InMemoryStruct<macho::Section64> Sect;
546 getSection64(DRI, Sect);
547 Result = Sect->Size;
548 } else {
549 InMemoryStruct<macho::Section> Sect;
550 getSection(DRI, Sect);
551 Result = Sect->Size;
552 }
Michael J. Spencer25b15772011-06-25 17:55:23 +0000553 return object_error::success;
Eric Christopher6256b032011-04-22 03:19:48 +0000554}
555
Michael J. Spencer25b15772011-06-25 17:55:23 +0000556error_code MachOObjectFile::getSectionContents(DataRefImpl DRI,
557 StringRef &Result) const {
Jim Grosbach596e4742012-11-29 19:14:11 +0000558 if (is64BitLoadCommand(MachOObj.get(), DRI)) {
Benjamin Kramer7d145782011-07-15 00:14:48 +0000559 InMemoryStruct<macho::Section64> Sect;
560 getSection64(DRI, Sect);
561 Result = MachOObj->getData(Sect->Offset, Sect->Size);
562 } else {
563 InMemoryStruct<macho::Section> Sect;
564 getSection(DRI, Sect);
565 Result = MachOObj->getData(Sect->Offset, Sect->Size);
566 }
Michael J. Spencer25b15772011-06-25 17:55:23 +0000567 return object_error::success;
Eric Christopher6256b032011-04-22 03:19:48 +0000568}
569
Michael J. Spencere2f2f072011-10-10 21:55:43 +0000570error_code MachOObjectFile::getSectionAlignment(DataRefImpl DRI,
571 uint64_t &Result) const {
Jim Grosbach596e4742012-11-29 19:14:11 +0000572 if (is64BitLoadCommand(MachOObj.get(), DRI)) {
Michael J. Spencere2f2f072011-10-10 21:55:43 +0000573 InMemoryStruct<macho::Section64> Sect;
574 getSection64(DRI, Sect);
Michael J. Spencer15565ad2011-10-10 23:36:56 +0000575 Result = uint64_t(1) << Sect->Align;
Michael J. Spencere2f2f072011-10-10 21:55:43 +0000576 } else {
577 InMemoryStruct<macho::Section> Sect;
578 getSection(DRI, Sect);
Michael J. Spencer15565ad2011-10-10 23:36:56 +0000579 Result = uint64_t(1) << Sect->Align;
Michael J. Spencere2f2f072011-10-10 21:55:43 +0000580 }
581 return object_error::success;
582}
583
Michael J. Spencer25b15772011-06-25 17:55:23 +0000584error_code MachOObjectFile::isSectionText(DataRefImpl DRI,
585 bool &Result) const {
Jim Grosbach596e4742012-11-29 19:14:11 +0000586 if (is64BitLoadCommand(MachOObj.get(), DRI)) {
Benjamin Kramer7d145782011-07-15 00:14:48 +0000587 InMemoryStruct<macho::Section64> Sect;
588 getSection64(DRI, Sect);
Tim Northover1c2b2f92012-12-17 17:59:32 +0000589 Result = Sect->Flags & macho::SF_PureInstructions;
Benjamin Kramer7d145782011-07-15 00:14:48 +0000590 } else {
591 InMemoryStruct<macho::Section> Sect;
592 getSection(DRI, Sect);
Tim Northover1c2b2f92012-12-17 17:59:32 +0000593 Result = Sect->Flags & macho::SF_PureInstructions;
Benjamin Kramer7d145782011-07-15 00:14:48 +0000594 }
Michael J. Spencer25b15772011-06-25 17:55:23 +0000595 return object_error::success;
Eric Christopher6256b032011-04-22 03:19:48 +0000596}
597
Michael J. Spencer13afc5e2011-09-28 20:57:30 +0000598error_code MachOObjectFile::isSectionData(DataRefImpl DRI,
599 bool &Result) const {
600 // FIXME: Unimplemented.
601 Result = false;
602 return object_error::success;
603}
604
605error_code MachOObjectFile::isSectionBSS(DataRefImpl DRI,
606 bool &Result) const {
607 // FIXME: Unimplemented.
608 Result = false;
609 return object_error::success;
610}
611
Preston Gurdc68dda82012-04-12 20:13:57 +0000612error_code MachOObjectFile::isSectionRequiredForExecution(DataRefImpl Sec,
613 bool &Result) const {
Andrew Kaylor30b20eb2012-10-10 01:45:52 +0000614 // FIXME: Unimplemented.
Preston Gurdc68dda82012-04-12 20:13:57 +0000615 Result = true;
616 return object_error::success;
617}
618
619error_code MachOObjectFile::isSectionVirtual(DataRefImpl Sec,
Andrew Kaylor30b20eb2012-10-10 01:45:52 +0000620 bool &Result) const {
621 // FIXME: Unimplemented.
Preston Gurdc68dda82012-04-12 20:13:57 +0000622 Result = false;
623 return object_error::success;
624}
625
626error_code MachOObjectFile::isSectionZeroInit(DataRefImpl DRI,
627 bool &Result) const {
628 if (MachOObj->is64Bit()) {
629 InMemoryStruct<macho::Section64> Sect;
630 getSection64(DRI, Sect);
Eli Friedman41827f92012-05-02 02:31:28 +0000631 unsigned SectionType = Sect->Flags & MachO::SectionFlagMaskSectionType;
632 Result = (SectionType == MachO::SectionTypeZeroFill ||
633 SectionType == MachO::SectionTypeZeroFillLarge);
Preston Gurdc68dda82012-04-12 20:13:57 +0000634 } else {
635 InMemoryStruct<macho::Section> Sect;
636 getSection(DRI, Sect);
Eli Friedman41827f92012-05-02 02:31:28 +0000637 unsigned SectionType = Sect->Flags & MachO::SectionFlagMaskSectionType;
638 Result = (SectionType == MachO::SectionTypeZeroFill ||
639 SectionType == MachO::SectionTypeZeroFillLarge);
Preston Gurdc68dda82012-04-12 20:13:57 +0000640 }
641
642 return object_error::success;
643}
644
Andrew Kaylor3a129c82012-10-10 01:41:33 +0000645error_code MachOObjectFile::isSectionReadOnlyData(DataRefImpl Sec,
646 bool &Result) const {
647 // Consider using the code from isSectionText to look for __const sections.
648 // Alternately, emit S_ATTR_PURE_INSTRUCTIONS and/or S_ATTR_SOME_INSTRUCTIONS
649 // to use section attributes to distinguish code from data.
650
651 // FIXME: Unimplemented.
652 Result = false;
653 return object_error::success;
654}
655
Benjamin Kramer07ea23a2011-07-15 18:39:21 +0000656error_code MachOObjectFile::sectionContainsSymbol(DataRefImpl Sec,
657 DataRefImpl Symb,
658 bool &Result) const {
Michael J. Spencer1130a792011-10-17 20:19:29 +0000659 SymbolRef::Type ST;
Owen Andersoncd749882011-10-12 22:21:32 +0000660 getSymbolType(Symb, ST);
David Meyer2c677272012-02-29 02:11:55 +0000661 if (ST == SymbolRef::ST_Unknown) {
Owen Andersoncd749882011-10-12 22:21:32 +0000662 Result = false;
663 return object_error::success;
664 }
665
666 uint64_t SectBegin, SectEnd;
667 getSectionAddress(Sec, SectBegin);
668 getSectionSize(Sec, SectEnd);
669 SectEnd += SectBegin;
670
Benjamin Kramer07ea23a2011-07-15 18:39:21 +0000671 if (MachOObj->is64Bit()) {
672 InMemoryStruct<macho::Symbol64TableEntry> Entry;
673 getSymbol64TableEntry(Symb, Entry);
Owen Andersoncd749882011-10-12 22:21:32 +0000674 uint64_t SymAddr= Entry->Value;
675 Result = (SymAddr >= SectBegin) && (SymAddr < SectEnd);
Benjamin Kramer07ea23a2011-07-15 18:39:21 +0000676 } else {
677 InMemoryStruct<macho::SymbolTableEntry> Entry;
678 getSymbolTableEntry(Symb, Entry);
Owen Andersoncd749882011-10-12 22:21:32 +0000679 uint64_t SymAddr= Entry->Value;
680 Result = (SymAddr >= SectBegin) && (SymAddr < SectEnd);
Benjamin Kramer07ea23a2011-07-15 18:39:21 +0000681 }
Owen Andersoncd749882011-10-12 22:21:32 +0000682
Benjamin Kramer07ea23a2011-07-15 18:39:21 +0000683 return object_error::success;
684}
685
Michael J. Spencer4344b1e2011-10-07 19:25:32 +0000686relocation_iterator MachOObjectFile::getSectionRelBegin(DataRefImpl Sec) const {
687 DataRefImpl ret;
Michael J. Spencer4344b1e2011-10-07 19:25:32 +0000688 ret.d.b = getSectionIndex(Sec);
689 return relocation_iterator(RelocationRef(ret, this));
690}
691relocation_iterator MachOObjectFile::getSectionRelEnd(DataRefImpl Sec) const {
692 uint32_t last_reloc;
Jim Grosbach596e4742012-11-29 19:14:11 +0000693 if (is64BitLoadCommand(MachOObj.get(), Sec)) {
Michael J. Spencer4344b1e2011-10-07 19:25:32 +0000694 InMemoryStruct<macho::Section64> Sect;
695 getSection64(Sec, Sect);
696 last_reloc = Sect->NumRelocationTableEntries;
697 } else {
698 InMemoryStruct<macho::Section> Sect;
699 getSection(Sec, Sect);
700 last_reloc = Sect->NumRelocationTableEntries;
701 }
702 DataRefImpl ret;
703 ret.d.a = last_reloc;
704 ret.d.b = getSectionIndex(Sec);
705 return relocation_iterator(RelocationRef(ret, this));
706}
707
708section_iterator MachOObjectFile::begin_sections() const {
Eric Christopher6256b032011-04-22 03:19:48 +0000709 DataRefImpl DRI;
Eric Christopher6256b032011-04-22 03:19:48 +0000710 moveToNextSection(DRI);
711 return section_iterator(SectionRef(DRI, this));
712}
713
Michael J. Spencer4344b1e2011-10-07 19:25:32 +0000714section_iterator MachOObjectFile::end_sections() const {
Eric Christopher6256b032011-04-22 03:19:48 +0000715 DataRefImpl DRI;
716 DRI.d.a = MachOObj->getHeader().NumLoadCommands;
Eric Christopher6256b032011-04-22 03:19:48 +0000717 return section_iterator(SectionRef(DRI, this));
718}
719
Benjamin Kramer0fcab072011-09-08 20:52:17 +0000720/*===-- Relocations -------------------------------------------------------===*/
721
722void MachOObjectFile::
723getRelocation(DataRefImpl Rel,
724 InMemoryStruct<macho::RelocationEntry> &Res) const {
725 uint32_t relOffset;
726 if (MachOObj->is64Bit()) {
727 InMemoryStruct<macho::Section64> Sect;
728 getSection64(Sections[Rel.d.b], Sect);
729 relOffset = Sect->RelocationTableOffset;
730 } else {
731 InMemoryStruct<macho::Section> Sect;
732 getSection(Sections[Rel.d.b], Sect);
733 relOffset = Sect->RelocationTableOffset;
734 }
735 MachOObj->ReadRelocationEntry(relOffset, Rel.d.a, Res);
736}
737error_code MachOObjectFile::getRelocationNext(DataRefImpl Rel,
738 RelocationRef &Res) const {
739 ++Rel.d.a;
Benjamin Kramer0fcab072011-09-08 20:52:17 +0000740 Res = RelocationRef(Rel, this);
741 return object_error::success;
742}
743error_code MachOObjectFile::getRelocationAddress(DataRefImpl Rel,
744 uint64_t &Res) const {
Owen Anderson0135fe12011-10-24 21:44:00 +0000745 const uint8_t* sectAddress = 0;
Benjamin Kramer0fcab072011-09-08 20:52:17 +0000746 if (MachOObj->is64Bit()) {
747 InMemoryStruct<macho::Section64> Sect;
748 getSection64(Sections[Rel.d.b], Sect);
Owen Anderson0135fe12011-10-24 21:44:00 +0000749 sectAddress += Sect->Address;
Benjamin Kramer0fcab072011-09-08 20:52:17 +0000750 } else {
751 InMemoryStruct<macho::Section> Sect;
752 getSection(Sections[Rel.d.b], Sect);
Owen Anderson0135fe12011-10-24 21:44:00 +0000753 sectAddress += Sect->Address;
Benjamin Kramer0fcab072011-09-08 20:52:17 +0000754 }
755 InMemoryStruct<macho::RelocationEntry> RE;
756 getRelocation(Rel, RE);
Owen Anderson1832f4d2011-10-26 20:42:54 +0000757
758 unsigned Arch = getArch();
759 bool isScattered = (Arch != Triple::x86_64) &&
760 (RE->Word0 & macho::RF_Scattered);
761 uint64_t RelAddr = 0;
Danil Malyshevb0436a72011-11-29 17:40:10 +0000762 if (isScattered)
Owen Anderson1832f4d2011-10-26 20:42:54 +0000763 RelAddr = RE->Word0 & 0xFFFFFF;
764 else
765 RelAddr = RE->Word0;
766
767 Res = reinterpret_cast<uintptr_t>(sectAddress + RelAddr);
Benjamin Kramer0fcab072011-09-08 20:52:17 +0000768 return object_error::success;
769}
Danil Malyshevb0436a72011-11-29 17:40:10 +0000770error_code MachOObjectFile::getRelocationOffset(DataRefImpl Rel,
771 uint64_t &Res) const {
772 InMemoryStruct<macho::RelocationEntry> RE;
773 getRelocation(Rel, RE);
774
775 unsigned Arch = getArch();
776 bool isScattered = (Arch != Triple::x86_64) &&
777 (RE->Word0 & macho::RF_Scattered);
778 if (isScattered)
779 Res = RE->Word0 & 0xFFFFFF;
780 else
781 Res = RE->Word0;
782 return object_error::success;
783}
Benjamin Kramer0fcab072011-09-08 20:52:17 +0000784error_code MachOObjectFile::getRelocationSymbol(DataRefImpl Rel,
785 SymbolRef &Res) const {
786 InMemoryStruct<macho::RelocationEntry> RE;
787 getRelocation(Rel, RE);
788 uint32_t SymbolIdx = RE->Word1 & 0xffffff;
789 bool isExtern = (RE->Word1 >> 27) & 1;
790
791 DataRefImpl Sym;
Benjamin Kramer0fcab072011-09-08 20:52:17 +0000792 moveToNextSymbol(Sym);
Benjamin Kramer0fcab072011-09-08 20:52:17 +0000793 if (isExtern) {
794 for (unsigned i = 0; i < SymbolIdx; i++) {
795 Sym.d.b++;
796 moveToNextSymbol(Sym);
Nick Lewycky58856ea2011-09-09 00:16:50 +0000797 assert(Sym.d.a < MachOObj->getHeader().NumLoadCommands &&
Benjamin Kramer0fcab072011-09-08 20:52:17 +0000798 "Relocation symbol index out of range!");
799 }
800 }
801 Res = SymbolRef(Sym, this);
802 return object_error::success;
803}
804error_code MachOObjectFile::getRelocationType(DataRefImpl Rel,
Owen Anderson9472b8d2011-10-26 17:08:49 +0000805 uint64_t &Res) const {
Benjamin Kramer0fcab072011-09-08 20:52:17 +0000806 InMemoryStruct<macho::RelocationEntry> RE;
807 getRelocation(Rel, RE);
Owen Andersonf8261e72011-10-26 17:10:22 +0000808 Res = RE->Word0;
809 Res <<= 32;
810 Res |= RE->Word1;
Benjamin Kramer0fcab072011-09-08 20:52:17 +0000811 return object_error::success;
812}
Michael J. Spencer4344b1e2011-10-07 19:25:32 +0000813error_code MachOObjectFile::getRelocationTypeName(DataRefImpl Rel,
814 SmallVectorImpl<char> &Result) const {
Owen Anderson0135fe12011-10-24 21:44:00 +0000815 // TODO: Support scattered relocations.
816 StringRef res;
817 InMemoryStruct<macho::RelocationEntry> RE;
818 getRelocation(Rel, RE);
Owen Anderson0135fe12011-10-24 21:44:00 +0000819
820 unsigned Arch = getArch();
Owen Anderson1832f4d2011-10-26 20:42:54 +0000821 bool isScattered = (Arch != Triple::x86_64) &&
822 (RE->Word0 & macho::RF_Scattered);
823
824 unsigned r_type;
825 if (isScattered)
826 r_type = (RE->Word0 >> 24) & 0xF;
827 else
828 r_type = (RE->Word1 >> 28) & 0xF;
829
Owen Anderson0135fe12011-10-24 21:44:00 +0000830 switch (Arch) {
831 case Triple::x86: {
Craig Toppere3298102012-05-24 06:35:32 +0000832 static const char *const Table[] = {
Owen Anderson0135fe12011-10-24 21:44:00 +0000833 "GENERIC_RELOC_VANILLA",
834 "GENERIC_RELOC_PAIR",
835 "GENERIC_RELOC_SECTDIFF",
Owen Andersoneb6bd332011-10-27 20:46:09 +0000836 "GENERIC_RELOC_PB_LA_PTR",
Owen Anderson0135fe12011-10-24 21:44:00 +0000837 "GENERIC_RELOC_LOCAL_SECTDIFF",
Owen Andersoneb6bd332011-10-27 20:46:09 +0000838 "GENERIC_RELOC_TLV" };
Owen Anderson0135fe12011-10-24 21:44:00 +0000839
Owen Andersoneb6bd332011-10-27 20:46:09 +0000840 if (r_type > 6)
Owen Anderson0135fe12011-10-24 21:44:00 +0000841 res = "Unknown";
842 else
843 res = Table[r_type];
844 break;
845 }
846 case Triple::x86_64: {
Craig Toppere3298102012-05-24 06:35:32 +0000847 static const char *const Table[] = {
Owen Andersond8fa76d2011-10-24 23:20:07 +0000848 "X86_64_RELOC_UNSIGNED",
849 "X86_64_RELOC_SIGNED",
Owen Anderson0135fe12011-10-24 21:44:00 +0000850 "X86_64_RELOC_BRANCH",
851 "X86_64_RELOC_GOT_LOAD",
852 "X86_64_RELOC_GOT",
Owen Andersond8fa76d2011-10-24 23:20:07 +0000853 "X86_64_RELOC_SUBTRACTOR",
854 "X86_64_RELOC_SIGNED_1",
855 "X86_64_RELOC_SIGNED_2",
856 "X86_64_RELOC_SIGNED_4",
857 "X86_64_RELOC_TLV" };
Owen Anderson0135fe12011-10-24 21:44:00 +0000858
Owen Andersond8fa76d2011-10-24 23:20:07 +0000859 if (r_type > 9)
Owen Anderson0135fe12011-10-24 21:44:00 +0000860 res = "Unknown";
861 else
862 res = Table[r_type];
863 break;
864 }
865 case Triple::arm: {
Craig Toppere3298102012-05-24 06:35:32 +0000866 static const char *const Table[] = {
Owen Anderson0135fe12011-10-24 21:44:00 +0000867 "ARM_RELOC_VANILLA",
868 "ARM_RELOC_PAIR",
869 "ARM_RELOC_SECTDIFF",
870 "ARM_RELOC_LOCAL_SECTDIFF",
871 "ARM_RELOC_PB_LA_PTR",
872 "ARM_RELOC_BR24",
873 "ARM_THUMB_RELOC_BR22",
874 "ARM_THUMB_32BIT_BRANCH",
875 "ARM_RELOC_HALF",
876 "ARM_RELOC_HALF_SECTDIFF" };
877
878 if (r_type > 9)
879 res = "Unknown";
880 else
881 res = Table[r_type];
882 break;
883 }
884 case Triple::ppc: {
Craig Toppere3298102012-05-24 06:35:32 +0000885 static const char *const Table[] = {
Owen Anderson0135fe12011-10-24 21:44:00 +0000886 "PPC_RELOC_VANILLA",
887 "PPC_RELOC_PAIR",
888 "PPC_RELOC_BR14",
889 "PPC_RELOC_BR24",
890 "PPC_RELOC_HI16",
891 "PPC_RELOC_LO16",
892 "PPC_RELOC_HA16",
893 "PPC_RELOC_LO14",
894 "PPC_RELOC_SECTDIFF",
895 "PPC_RELOC_PB_LA_PTR",
896 "PPC_RELOC_HI16_SECTDIFF",
897 "PPC_RELOC_LO16_SECTDIFF",
898 "PPC_RELOC_HA16_SECTDIFF",
899 "PPC_RELOC_JBSR",
900 "PPC_RELOC_LO14_SECTDIFF",
901 "PPC_RELOC_LOCAL_SECTDIFF" };
902
903 res = Table[r_type];
904 break;
905 }
906 case Triple::UnknownArch:
907 res = "Unknown";
908 break;
909 }
Owen Anderson5f4e02c2011-10-24 20:19:18 +0000910 Result.append(res.begin(), res.end());
Michael J. Spencer4344b1e2011-10-07 19:25:32 +0000911 return object_error::success;
912}
Benjamin Kramer0fcab072011-09-08 20:52:17 +0000913error_code MachOObjectFile::getRelocationAdditionalInfo(DataRefImpl Rel,
914 int64_t &Res) const {
915 InMemoryStruct<macho::RelocationEntry> RE;
916 getRelocation(Rel, RE);
917 bool isExtern = (RE->Word1 >> 27) & 1;
918 Res = 0;
919 if (!isExtern) {
920 const uint8_t* sectAddress = base();
921 if (MachOObj->is64Bit()) {
922 InMemoryStruct<macho::Section64> Sect;
923 getSection64(Sections[Rel.d.b], Sect);
924 sectAddress += Sect->Offset;
925 } else {
926 InMemoryStruct<macho::Section> Sect;
927 getSection(Sections[Rel.d.b], Sect);
928 sectAddress += Sect->Offset;
929 }
930 Res = reinterpret_cast<uintptr_t>(sectAddress);
931 }
932 return object_error::success;
933}
Owen Andersond8fa76d2011-10-24 23:20:07 +0000934
935// Helper to advance a section or symbol iterator multiple increments at a time.
936template<class T>
937error_code advance(T &it, size_t Val) {
938 error_code ec;
939 while (Val--) {
940 it.increment(ec);
941 }
942 return ec;
943}
944
945template<class T>
946void advanceTo(T &it, size_t Val) {
947 if (error_code ec = advance(it, Val))
948 report_fatal_error(ec.message());
949}
950
Owen Anderson1832f4d2011-10-26 20:42:54 +0000951void MachOObjectFile::printRelocationTargetName(
952 InMemoryStruct<macho::RelocationEntry>& RE,
953 raw_string_ostream &fmt) const {
954 unsigned Arch = getArch();
955 bool isScattered = (Arch != Triple::x86_64) &&
956 (RE->Word0 & macho::RF_Scattered);
957
958 // Target of a scattered relocation is an address. In the interest of
959 // generating pretty output, scan through the symbol table looking for a
960 // symbol that aligns with that address. If we find one, print it.
961 // Otherwise, we just print the hex address of the target.
962 if (isScattered) {
963 uint32_t Val = RE->Word1;
964
965 error_code ec;
966 for (symbol_iterator SI = begin_symbols(), SE = end_symbols(); SI != SE;
967 SI.increment(ec)) {
968 if (ec) report_fatal_error(ec.message());
969
970 uint64_t Addr;
971 StringRef Name;
972
973 if ((ec = SI->getAddress(Addr)))
974 report_fatal_error(ec.message());
975 if (Addr != Val) continue;
976 if ((ec = SI->getName(Name)))
977 report_fatal_error(ec.message());
978 fmt << Name;
979 return;
980 }
981
Owen Andersonb28bdbf2011-10-27 21:53:50 +0000982 // If we couldn't find a symbol that this relocation refers to, try
983 // to find a section beginning instead.
984 for (section_iterator SI = begin_sections(), SE = end_sections(); SI != SE;
985 SI.increment(ec)) {
986 if (ec) report_fatal_error(ec.message());
987
988 uint64_t Addr;
989 StringRef Name;
990
991 if ((ec = SI->getAddress(Addr)))
992 report_fatal_error(ec.message());
993 if (Addr != Val) continue;
994 if ((ec = SI->getName(Name)))
995 report_fatal_error(ec.message());
996 fmt << Name;
997 return;
998 }
999
Owen Anderson1832f4d2011-10-26 20:42:54 +00001000 fmt << format("0x%x", Val);
1001 return;
1002 }
1003
1004 StringRef S;
1005 bool isExtern = (RE->Word1 >> 27) & 1;
1006 uint32_t Val = RE->Word1 & 0xFFFFFF;
Owen Andersond8fa76d2011-10-24 23:20:07 +00001007
1008 if (isExtern) {
1009 symbol_iterator SI = begin_symbols();
1010 advanceTo(SI, Val);
Owen Anderson1832f4d2011-10-26 20:42:54 +00001011 SI->getName(S);
Owen Andersond8fa76d2011-10-24 23:20:07 +00001012 } else {
1013 section_iterator SI = begin_sections();
1014 advanceTo(SI, Val);
Owen Anderson1832f4d2011-10-26 20:42:54 +00001015 SI->getName(S);
Owen Andersond8fa76d2011-10-24 23:20:07 +00001016 }
1017
Owen Anderson1832f4d2011-10-26 20:42:54 +00001018 fmt << S;
Owen Andersond8fa76d2011-10-24 23:20:07 +00001019}
1020
Michael J. Spencer4344b1e2011-10-07 19:25:32 +00001021error_code MachOObjectFile::getRelocationValueString(DataRefImpl Rel,
1022 SmallVectorImpl<char> &Result) const {
Owen Anderson0135fe12011-10-24 21:44:00 +00001023 InMemoryStruct<macho::RelocationEntry> RE;
1024 getRelocation(Rel, RE);
1025
Owen Anderson1832f4d2011-10-26 20:42:54 +00001026 unsigned Arch = getArch();
1027 bool isScattered = (Arch != Triple::x86_64) &&
1028 (RE->Word0 & macho::RF_Scattered);
Owen Andersond8fa76d2011-10-24 23:20:07 +00001029
Owen Anderson013d7562011-10-25 18:48:41 +00001030 std::string fmtbuf;
1031 raw_string_ostream fmt(fmtbuf);
1032
Owen Anderson1832f4d2011-10-26 20:42:54 +00001033 unsigned Type;
1034 if (isScattered)
1035 Type = (RE->Word0 >> 24) & 0xF;
1036 else
1037 Type = (RE->Word1 >> 28) & 0xF;
1038
Owen Andersoneb6bd332011-10-27 20:46:09 +00001039 bool isPCRel;
1040 if (isScattered)
1041 isPCRel = ((RE->Word0 >> 30) & 1);
1042 else
1043 isPCRel = ((RE->Word1 >> 24) & 1);
1044
Owen Andersond8fa76d2011-10-24 23:20:07 +00001045 // Determine any addends that should be displayed with the relocation.
1046 // These require decoding the relocation type, which is triple-specific.
Owen Andersond8fa76d2011-10-24 23:20:07 +00001047
1048 // X86_64 has entirely custom relocation types.
1049 if (Arch == Triple::x86_64) {
Owen Anderson929e27c2011-10-26 17:05:20 +00001050 bool isPCRel = ((RE->Word1 >> 24) & 1);
Owen Anderson013d7562011-10-25 18:48:41 +00001051
Owen Andersond8fa76d2011-10-24 23:20:07 +00001052 switch (Type) {
Owen Anderson1832f4d2011-10-26 20:42:54 +00001053 case macho::RIT_X86_64_GOTLoad: // X86_64_RELOC_GOT_LOAD
1054 case macho::RIT_X86_64_GOT: { // X86_64_RELOC_GOT
1055 printRelocationTargetName(RE, fmt);
1056 fmt << "@GOT";
Owen Anderson929e27c2011-10-26 17:05:20 +00001057 if (isPCRel) fmt << "PCREL";
1058 break;
1059 }
Owen Anderson1832f4d2011-10-26 20:42:54 +00001060 case macho::RIT_X86_64_Subtractor: { // X86_64_RELOC_SUBTRACTOR
Owen Anderson013d7562011-10-25 18:48:41 +00001061 InMemoryStruct<macho::RelocationEntry> RENext;
1062 DataRefImpl RelNext = Rel;
1063 RelNext.d.a++;
1064 getRelocation(RelNext, RENext);
Owen Andersond8fa76d2011-10-24 23:20:07 +00001065
1066 // X86_64_SUBTRACTOR must be followed by a relocation of type
1067 // X86_64_RELOC_UNSIGNED.
Owen Anderson1832f4d2011-10-26 20:42:54 +00001068 // NOTE: Scattered relocations don't exist on x86_64.
Owen Anderson013d7562011-10-25 18:48:41 +00001069 unsigned RType = (RENext->Word1 >> 28) & 0xF;
Owen Andersond8fa76d2011-10-24 23:20:07 +00001070 if (RType != 0)
1071 report_fatal_error("Expected X86_64_RELOC_UNSIGNED after "
1072 "X86_64_RELOC_SUBTRACTOR.");
1073
Owen Andersonef22f782011-10-26 17:28:49 +00001074 // The X86_64_RELOC_UNSIGNED contains the minuend symbol,
1075 // X86_64_SUBTRACTOR contains to the subtrahend.
Owen Anderson1832f4d2011-10-26 20:42:54 +00001076 printRelocationTargetName(RENext, fmt);
1077 fmt << "-";
1078 printRelocationTargetName(RE, fmt);
Owen Andersond8fa76d2011-10-24 23:20:07 +00001079 }
Owen Andersoneb6bd332011-10-27 20:46:09 +00001080 case macho::RIT_X86_64_TLV:
1081 printRelocationTargetName(RE, fmt);
1082 fmt << "@TLV";
1083 if (isPCRel) fmt << "P";
1084 break;
Owen Anderson1832f4d2011-10-26 20:42:54 +00001085 case macho::RIT_X86_64_Signed1: // X86_64_RELOC_SIGNED1
1086 printRelocationTargetName(RE, fmt);
1087 fmt << "-1";
Owen Andersond8fa76d2011-10-24 23:20:07 +00001088 break;
Owen Anderson1832f4d2011-10-26 20:42:54 +00001089 case macho::RIT_X86_64_Signed2: // X86_64_RELOC_SIGNED2
1090 printRelocationTargetName(RE, fmt);
1091 fmt << "-2";
Owen Andersond8fa76d2011-10-24 23:20:07 +00001092 break;
Owen Anderson1832f4d2011-10-26 20:42:54 +00001093 case macho::RIT_X86_64_Signed4: // X86_64_RELOC_SIGNED4
1094 printRelocationTargetName(RE, fmt);
1095 fmt << "-4";
Owen Anderson013d7562011-10-25 18:48:41 +00001096 break;
1097 default:
Owen Anderson1832f4d2011-10-26 20:42:54 +00001098 printRelocationTargetName(RE, fmt);
Owen Andersond8fa76d2011-10-24 23:20:07 +00001099 break;
1100 }
Owen Andersond8fa76d2011-10-24 23:20:07 +00001101 // X86 and ARM share some relocation types in common.
Owen Anderson013d7562011-10-25 18:48:41 +00001102 } else if (Arch == Triple::x86 || Arch == Triple::arm) {
1103 // Generic relocation types...
Owen Andersond8fa76d2011-10-24 23:20:07 +00001104 switch (Type) {
Owen Anderson1832f4d2011-10-26 20:42:54 +00001105 case macho::RIT_Pair: // GENERIC_RELOC_PAIR - prints no info
Owen Andersond8fa76d2011-10-24 23:20:07 +00001106 return object_error::success;
Owen Andersoneb6bd332011-10-27 20:46:09 +00001107 case macho::RIT_Difference: { // GENERIC_RELOC_SECTDIFF
Owen Anderson013d7562011-10-25 18:48:41 +00001108 InMemoryStruct<macho::RelocationEntry> RENext;
1109 DataRefImpl RelNext = Rel;
1110 RelNext.d.a++;
1111 getRelocation(RelNext, RENext);
Owen Andersond8fa76d2011-10-24 23:20:07 +00001112
1113 // X86 sect diff's must be followed by a relocation of type
1114 // GENERIC_RELOC_PAIR.
Owen Anderson1832f4d2011-10-26 20:42:54 +00001115 bool isNextScattered = (Arch != Triple::x86_64) &&
1116 (RENext->Word0 & macho::RF_Scattered);
1117 unsigned RType;
1118 if (isNextScattered)
1119 RType = (RENext->Word0 >> 24) & 0xF;
1120 else
1121 RType = (RENext->Word1 >> 28) & 0xF;
Owen Andersond8fa76d2011-10-24 23:20:07 +00001122 if (RType != 1)
1123 report_fatal_error("Expected GENERIC_RELOC_PAIR after "
Owen Andersoneb6bd332011-10-27 20:46:09 +00001124 "GENERIC_RELOC_SECTDIFF.");
Owen Andersond8fa76d2011-10-24 23:20:07 +00001125
Owen Anderson1832f4d2011-10-26 20:42:54 +00001126 printRelocationTargetName(RE, fmt);
1127 fmt << "-";
1128 printRelocationTargetName(RENext, fmt);
Owen Anderson013d7562011-10-25 18:48:41 +00001129 break;
Owen Andersond8fa76d2011-10-24 23:20:07 +00001130 }
1131 }
Owen Anderson013d7562011-10-25 18:48:41 +00001132
Owen Anderson1832f4d2011-10-26 20:42:54 +00001133 if (Arch == Triple::x86) {
Owen Anderson013d7562011-10-25 18:48:41 +00001134 // All X86 relocations that need special printing were already
1135 // handled in the generic code.
Owen Andersoneb6bd332011-10-27 20:46:09 +00001136 switch (Type) {
1137 case macho::RIT_Generic_LocalDifference:{// GENERIC_RELOC_LOCAL_SECTDIFF
1138 InMemoryStruct<macho::RelocationEntry> RENext;
1139 DataRefImpl RelNext = Rel;
1140 RelNext.d.a++;
1141 getRelocation(RelNext, RENext);
1142
1143 // X86 sect diff's must be followed by a relocation of type
1144 // GENERIC_RELOC_PAIR.
1145 bool isNextScattered = (Arch != Triple::x86_64) &&
1146 (RENext->Word0 & macho::RF_Scattered);
1147 unsigned RType;
1148 if (isNextScattered)
1149 RType = (RENext->Word0 >> 24) & 0xF;
1150 else
1151 RType = (RENext->Word1 >> 28) & 0xF;
1152 if (RType != 1)
1153 report_fatal_error("Expected GENERIC_RELOC_PAIR after "
1154 "GENERIC_RELOC_LOCAL_SECTDIFF.");
1155
1156 printRelocationTargetName(RE, fmt);
1157 fmt << "-";
1158 printRelocationTargetName(RENext, fmt);
1159 break;
1160 }
1161 case macho::RIT_Generic_TLV: {
1162 printRelocationTargetName(RE, fmt);
1163 fmt << "@TLV";
1164 if (isPCRel) fmt << "P";
1165 break;
1166 }
1167 default:
1168 printRelocationTargetName(RE, fmt);
1169 }
Owen Anderson013d7562011-10-25 18:48:41 +00001170 } else { // ARM-specific relocations
1171 switch (Type) {
Owen Anderson1832f4d2011-10-26 20:42:54 +00001172 case macho::RIT_ARM_Half: // ARM_RELOC_HALF
1173 case macho::RIT_ARM_HalfDifference: { // ARM_RELOC_HALF_SECTDIFF
Owen Anderson013d7562011-10-25 18:48:41 +00001174 // Half relocations steal a bit from the length field to encode
1175 // whether this is an upper16 or a lower16 relocation.
Owen Anderson1832f4d2011-10-26 20:42:54 +00001176 bool isUpper;
1177 if (isScattered)
1178 isUpper = (RE->Word0 >> 28) & 1;
Owen Anderson013d7562011-10-25 18:48:41 +00001179 else
Owen Anderson1832f4d2011-10-26 20:42:54 +00001180 isUpper = (RE->Word1 >> 25) & 1;
1181
1182 if (isUpper)
1183 fmt << ":upper16:(";
1184 else
1185 fmt << ":lower16:(";
1186 printRelocationTargetName(RE, fmt);
Owen Anderson013d7562011-10-25 18:48:41 +00001187
1188 InMemoryStruct<macho::RelocationEntry> RENext;
1189 DataRefImpl RelNext = Rel;
1190 RelNext.d.a++;
1191 getRelocation(RelNext, RENext);
1192
1193 // ARM half relocs must be followed by a relocation of type
1194 // ARM_RELOC_PAIR.
Owen Anderson1832f4d2011-10-26 20:42:54 +00001195 bool isNextScattered = (Arch != Triple::x86_64) &&
1196 (RENext->Word0 & macho::RF_Scattered);
1197 unsigned RType;
1198 if (isNextScattered)
1199 RType = (RENext->Word0 >> 24) & 0xF;
1200 else
1201 RType = (RENext->Word1 >> 28) & 0xF;
1202
Owen Anderson013d7562011-10-25 18:48:41 +00001203 if (RType != 1)
1204 report_fatal_error("Expected ARM_RELOC_PAIR after "
1205 "GENERIC_RELOC_HALF");
1206
Owen Anderson1832f4d2011-10-26 20:42:54 +00001207 // NOTE: The half of the target virtual address is stashed in the
1208 // address field of the secondary relocation, but we can't reverse
1209 // engineer the constant offset from it without decoding the movw/movt
1210 // instruction to find the other half in its immediate field.
Owen Anderson013d7562011-10-25 18:48:41 +00001211
1212 // ARM_RELOC_HALF_SECTDIFF encodes the second section in the
1213 // symbol/section pointer of the follow-on relocation.
Owen Anderson1832f4d2011-10-26 20:42:54 +00001214 if (Type == macho::RIT_ARM_HalfDifference) {
1215 fmt << "-";
1216 printRelocationTargetName(RENext, fmt);
Owen Anderson013d7562011-10-25 18:48:41 +00001217 }
1218
Owen Anderson013d7562011-10-25 18:48:41 +00001219 fmt << ")";
1220 break;
1221 }
1222 default: {
Owen Anderson1832f4d2011-10-26 20:42:54 +00001223 printRelocationTargetName(RE, fmt);
Owen Anderson013d7562011-10-25 18:48:41 +00001224 }
1225 }
1226 }
Owen Anderson1832f4d2011-10-26 20:42:54 +00001227 } else
1228 printRelocationTargetName(RE, fmt);
Owen Andersond8fa76d2011-10-24 23:20:07 +00001229
Owen Anderson0135fe12011-10-24 21:44:00 +00001230 fmt.flush();
1231 Result.append(fmtbuf.begin(), fmtbuf.end());
Michael J. Spencer4344b1e2011-10-07 19:25:32 +00001232 return object_error::success;
Benjamin Kramer0fcab072011-09-08 20:52:17 +00001233}
1234
Owen Anderson0685e942011-10-25 20:35:53 +00001235error_code MachOObjectFile::getRelocationHidden(DataRefImpl Rel,
1236 bool &Result) const {
1237 InMemoryStruct<macho::RelocationEntry> RE;
1238 getRelocation(Rel, RE);
1239
Owen Anderson0685e942011-10-25 20:35:53 +00001240 unsigned Arch = getArch();
Owen Anderson1832f4d2011-10-26 20:42:54 +00001241 bool isScattered = (Arch != Triple::x86_64) &&
1242 (RE->Word0 & macho::RF_Scattered);
1243 unsigned Type;
1244 if (isScattered)
1245 Type = (RE->Word0 >> 24) & 0xF;
1246 else
1247 Type = (RE->Word1 >> 28) & 0xF;
Owen Anderson0685e942011-10-25 20:35:53 +00001248
1249 Result = false;
1250
1251 // On arches that use the generic relocations, GENERIC_RELOC_PAIR
1252 // is always hidden.
1253 if (Arch == Triple::x86 || Arch == Triple::arm) {
Owen Anderson1832f4d2011-10-26 20:42:54 +00001254 if (Type == macho::RIT_Pair) Result = true;
Owen Anderson0685e942011-10-25 20:35:53 +00001255 } else if (Arch == Triple::x86_64) {
1256 // On x86_64, X86_64_RELOC_UNSIGNED is hidden only when it follows
1257 // an X864_64_RELOC_SUBTRACTOR.
Owen Anderson1832f4d2011-10-26 20:42:54 +00001258 if (Type == macho::RIT_X86_64_Unsigned && Rel.d.a > 0) {
Owen Anderson0685e942011-10-25 20:35:53 +00001259 DataRefImpl RelPrev = Rel;
1260 RelPrev.d.a--;
1261 InMemoryStruct<macho::RelocationEntry> REPrev;
1262 getRelocation(RelPrev, REPrev);
1263
1264 unsigned PrevType = (REPrev->Word1 >> 28) & 0xF;
1265
Owen Anderson1832f4d2011-10-26 20:42:54 +00001266 if (PrevType == macho::RIT_X86_64_Subtractor) Result = true;
Owen Anderson0685e942011-10-25 20:35:53 +00001267 }
1268 }
1269
1270 return object_error::success;
1271}
1272
David Meyer5c2b4ea2012-03-01 01:36:50 +00001273error_code MachOObjectFile::getLibraryNext(DataRefImpl LibData,
1274 LibraryRef &Res) const {
1275 report_fatal_error("Needed libraries unimplemented in MachOObjectFile");
1276}
1277
1278error_code MachOObjectFile::getLibraryPath(DataRefImpl LibData,
1279 StringRef &Res) const {
1280 report_fatal_error("Needed libraries unimplemented in MachOObjectFile");
1281}
1282
1283
Eric Christopher6256b032011-04-22 03:19:48 +00001284/*===-- Miscellaneous -----------------------------------------------------===*/
1285
1286uint8_t MachOObjectFile::getBytesInAddress() const {
1287 return MachOObj->is64Bit() ? 8 : 4;
1288}
1289
1290StringRef MachOObjectFile::getFileFormatName() const {
1291 if (!MachOObj->is64Bit()) {
1292 switch (MachOObj->getHeader().CPUType) {
Eric Christopherf4b2f932011-04-22 06:34:01 +00001293 case llvm::MachO::CPUTypeI386:
Eric Christopher9ab1d7f2011-04-22 04:08:58 +00001294 return "Mach-O 32-bit i386";
Eric Christopherf4b2f932011-04-22 06:34:01 +00001295 case llvm::MachO::CPUTypeARM:
Eric Christopher9ab1d7f2011-04-22 04:08:58 +00001296 return "Mach-O arm";
Eric Christopherf4b2f932011-04-22 06:34:01 +00001297 case llvm::MachO::CPUTypePowerPC:
Eric Christopher9ab1d7f2011-04-22 04:08:58 +00001298 return "Mach-O 32-bit ppc";
1299 default:
Eric Christopherf4b2f932011-04-22 06:34:01 +00001300 assert((MachOObj->getHeader().CPUType & llvm::MachO::CPUArchABI64) == 0 &&
Eric Christopher9ab1d7f2011-04-22 04:08:58 +00001301 "64-bit object file when we're not 64-bit?");
1302 return "Mach-O 32-bit unknown";
Eric Christopher6256b032011-04-22 03:19:48 +00001303 }
1304 }
1305
1306 switch (MachOObj->getHeader().CPUType) {
Eric Christopherf4b2f932011-04-22 06:34:01 +00001307 case llvm::MachO::CPUTypeX86_64:
Eric Christopher9ab1d7f2011-04-22 04:08:58 +00001308 return "Mach-O 64-bit x86-64";
Eric Christopherf4b2f932011-04-22 06:34:01 +00001309 case llvm::MachO::CPUTypePowerPC64:
Eric Christopher9ab1d7f2011-04-22 04:08:58 +00001310 return "Mach-O 64-bit ppc64";
Eric Christopher6256b032011-04-22 03:19:48 +00001311 default:
Eric Christopherf4b2f932011-04-22 06:34:01 +00001312 assert((MachOObj->getHeader().CPUType & llvm::MachO::CPUArchABI64) == 1 &&
Eric Christopher9ab1d7f2011-04-22 04:08:58 +00001313 "32-bit object file when we're 64-bit?");
1314 return "Mach-O 64-bit unknown";
Eric Christopher6256b032011-04-22 03:19:48 +00001315 }
1316}
1317
1318unsigned MachOObjectFile::getArch() const {
1319 switch (MachOObj->getHeader().CPUType) {
Eric Christopherf4b2f932011-04-22 06:34:01 +00001320 case llvm::MachO::CPUTypeI386:
Eric Christopher6256b032011-04-22 03:19:48 +00001321 return Triple::x86;
Eric Christopherf4b2f932011-04-22 06:34:01 +00001322 case llvm::MachO::CPUTypeX86_64:
Eric Christopher6256b032011-04-22 03:19:48 +00001323 return Triple::x86_64;
Eric Christopherf4b2f932011-04-22 06:34:01 +00001324 case llvm::MachO::CPUTypeARM:
Eric Christopher6256b032011-04-22 03:19:48 +00001325 return Triple::arm;
Eric Christopherf4b2f932011-04-22 06:34:01 +00001326 case llvm::MachO::CPUTypePowerPC:
Eric Christopher6256b032011-04-22 03:19:48 +00001327 return Triple::ppc;
Eric Christopherf4b2f932011-04-22 06:34:01 +00001328 case llvm::MachO::CPUTypePowerPC64:
Eric Christopher6256b032011-04-22 03:19:48 +00001329 return Triple::ppc64;
1330 default:
1331 return Triple::UnknownArch;
1332 }
1333}
1334
Owen Andersonf7c93a32011-10-11 17:32:27 +00001335} // end namespace object
Eric Christopher6256b032011-04-22 03:19:48 +00001336} // end namespace llvm