blob: 26a6e136d753ff0ccb08f87f2f43abc23cb40f93 [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"
16#include "llvm/Object/MachOFormat.h"
17#include "llvm/Object/MachOObject.h"
18#include "llvm/Object/ObjectFile.h"
19#include "llvm/Support/MemoryBuffer.h"
Eric Christopherf4b2f932011-04-22 06:34:01 +000020#include "llvm/Support/MachO.h"
Eric Christopher6256b032011-04-22 03:19:48 +000021
22#include <cctype>
23#include <cstring>
24#include <limits>
25
26using namespace llvm;
27using namespace object;
28
29namespace llvm {
30
31typedef MachOObject::LoadCommandInfo LoadCommandInfo;
32
33class MachOObjectFile : public ObjectFile {
34public:
Michael J. Spencer001c9202011-06-25 17:54:50 +000035 MachOObjectFile(MemoryBuffer *Object, MachOObject *MOO, error_code &ec)
36 : ObjectFile(Binary::isMachO, Object, ec),
Eric Christopher6256b032011-04-22 03:19:48 +000037 MachOObj(MOO),
38 RegisteredStringTable(std::numeric_limits<uint32_t>::max()) {}
39
40 virtual symbol_iterator begin_symbols() const;
41 virtual symbol_iterator end_symbols() const;
42 virtual section_iterator begin_sections() const;
43 virtual section_iterator end_sections() const;
44
45 virtual uint8_t getBytesInAddress() const;
46 virtual StringRef getFileFormatName() const;
47 virtual unsigned getArch() const;
48
49protected:
Michael J. Spencer25b15772011-06-25 17:55:23 +000050 virtual error_code getSymbolNext(DataRefImpl Symb, SymbolRef &Res) const;
51 virtual error_code getSymbolName(DataRefImpl Symb, StringRef &Res) const;
52 virtual error_code getSymbolAddress(DataRefImpl Symb, uint64_t &Res) const;
53 virtual error_code getSymbolSize(DataRefImpl Symb, uint64_t &Res) const;
54 virtual error_code getSymbolNMTypeChar(DataRefImpl Symb, char &Res) const;
55 virtual error_code isSymbolInternal(DataRefImpl Symb, bool &Res) const;
Eric Christopher6256b032011-04-22 03:19:48 +000056
Michael J. Spencer25b15772011-06-25 17:55:23 +000057 virtual error_code getSectionNext(DataRefImpl Sec, SectionRef &Res) const;
58 virtual error_code getSectionName(DataRefImpl Sec, StringRef &Res) const;
59 virtual error_code getSectionAddress(DataRefImpl Sec, uint64_t &Res) const;
60 virtual error_code getSectionSize(DataRefImpl Sec, uint64_t &Res) const;
61 virtual error_code getSectionContents(DataRefImpl Sec, StringRef &Res) const;
62 virtual error_code isSectionText(DataRefImpl Sec, bool &Res) const;
Benjamin Kramer07ea23a2011-07-15 18:39:21 +000063 virtual error_code sectionContainsSymbol(DataRefImpl DRI, DataRefImpl S,
64 bool &Result) const;
Eric Christopher6256b032011-04-22 03:19:48 +000065
66private:
67 MachOObject *MachOObj;
68 mutable uint32_t RegisteredStringTable;
69
70 void moveToNextSection(DataRefImpl &DRI) const;
71 void getSymbolTableEntry(DataRefImpl DRI,
72 InMemoryStruct<macho::SymbolTableEntry> &Res) const;
Benjamin Kramer32fb2af2011-07-15 17:32:45 +000073 void getSymbol64TableEntry(DataRefImpl DRI,
74 InMemoryStruct<macho::Symbol64TableEntry> &Res) const;
Eric Christopher6256b032011-04-22 03:19:48 +000075 void moveToNextSymbol(DataRefImpl &DRI) const;
76 void getSection(DataRefImpl DRI, InMemoryStruct<macho::Section> &Res) const;
Benjamin Kramer7d145782011-07-15 00:14:48 +000077 void getSection64(DataRefImpl DRI,
78 InMemoryStruct<macho::Section64> &Res) const;
Eric Christopher6256b032011-04-22 03:19:48 +000079};
80
81ObjectFile *ObjectFile::createMachOObjectFile(MemoryBuffer *Buffer) {
Michael J. Spencer001c9202011-06-25 17:54:50 +000082 error_code ec;
Eric Christopher6256b032011-04-22 03:19:48 +000083 std::string Err;
84 MachOObject *MachOObj = MachOObject::LoadFromBuffer(Buffer, &Err);
85 if (!MachOObj)
86 return NULL;
Michael J. Spencer001c9202011-06-25 17:54:50 +000087 return new MachOObjectFile(Buffer, MachOObj, ec);
Eric Christopher6256b032011-04-22 03:19:48 +000088}
89
90/*===-- Symbols -----------------------------------------------------------===*/
91
92void MachOObjectFile::moveToNextSymbol(DataRefImpl &DRI) const {
93 uint32_t LoadCommandCount = MachOObj->getHeader().NumLoadCommands;
94 while (DRI.d.a < LoadCommandCount) {
95 LoadCommandInfo LCI = MachOObj->getLoadCommandInfo(DRI.d.a);
96 if (LCI.Command.Type == macho::LCT_Symtab) {
97 InMemoryStruct<macho::SymtabLoadCommand> SymtabLoadCmd;
98 MachOObj->ReadSymtabLoadCommand(LCI, SymtabLoadCmd);
99 if (DRI.d.b < SymtabLoadCmd->NumSymbolTableEntries)
100 return;
101 }
102
103 DRI.d.a++;
104 DRI.d.b = 0;
105 }
106}
107
108void MachOObjectFile::getSymbolTableEntry(DataRefImpl DRI,
109 InMemoryStruct<macho::SymbolTableEntry> &Res) const {
110 InMemoryStruct<macho::SymtabLoadCommand> SymtabLoadCmd;
111 LoadCommandInfo LCI = MachOObj->getLoadCommandInfo(DRI.d.a);
112 MachOObj->ReadSymtabLoadCommand(LCI, SymtabLoadCmd);
113
114 if (RegisteredStringTable != DRI.d.a) {
115 MachOObj->RegisterStringTable(*SymtabLoadCmd);
116 RegisteredStringTable = DRI.d.a;
117 }
118
119 MachOObj->ReadSymbolTableEntry(SymtabLoadCmd->SymbolTableOffset, DRI.d.b,
120 Res);
121}
122
Benjamin Kramer32fb2af2011-07-15 17:32:45 +0000123void MachOObjectFile::getSymbol64TableEntry(DataRefImpl DRI,
124 InMemoryStruct<macho::Symbol64TableEntry> &Res) const {
125 InMemoryStruct<macho::SymtabLoadCommand> SymtabLoadCmd;
126 LoadCommandInfo LCI = MachOObj->getLoadCommandInfo(DRI.d.a);
127 MachOObj->ReadSymtabLoadCommand(LCI, SymtabLoadCmd);
128
129 if (RegisteredStringTable != DRI.d.a) {
130 MachOObj->RegisterStringTable(*SymtabLoadCmd);
131 RegisteredStringTable = DRI.d.a;
132 }
133
134 MachOObj->ReadSymbol64TableEntry(SymtabLoadCmd->SymbolTableOffset, DRI.d.b,
135 Res);
136}
137
Eric Christopher6256b032011-04-22 03:19:48 +0000138
Michael J. Spencer25b15772011-06-25 17:55:23 +0000139error_code MachOObjectFile::getSymbolNext(DataRefImpl DRI,
140 SymbolRef &Result) const {
Eric Christopher6256b032011-04-22 03:19:48 +0000141 DRI.d.b++;
142 moveToNextSymbol(DRI);
Michael J. Spencer25b15772011-06-25 17:55:23 +0000143 Result = SymbolRef(DRI, this);
144 return object_error::success;
Eric Christopher6256b032011-04-22 03:19:48 +0000145}
146
Michael J. Spencer25b15772011-06-25 17:55:23 +0000147error_code MachOObjectFile::getSymbolName(DataRefImpl DRI,
148 StringRef &Result) const {
Benjamin Kramer32fb2af2011-07-15 17:32:45 +0000149 if (MachOObj->is64Bit()) {
150 InMemoryStruct<macho::Symbol64TableEntry> Entry;
151 getSymbol64TableEntry(DRI, Entry);
152 Result = MachOObj->getStringAtIndex(Entry->StringIndex);
153 } else {
154 InMemoryStruct<macho::SymbolTableEntry> Entry;
155 getSymbolTableEntry(DRI, Entry);
156 Result = MachOObj->getStringAtIndex(Entry->StringIndex);
157 }
Michael J. Spencer25b15772011-06-25 17:55:23 +0000158 return object_error::success;
Eric Christopher6256b032011-04-22 03:19:48 +0000159}
160
Michael J. Spencer25b15772011-06-25 17:55:23 +0000161error_code MachOObjectFile::getSymbolAddress(DataRefImpl DRI,
162 uint64_t &Result) const {
Benjamin Kramer32fb2af2011-07-15 17:32:45 +0000163 if (MachOObj->is64Bit()) {
164 InMemoryStruct<macho::Symbol64TableEntry> Entry;
165 getSymbol64TableEntry(DRI, Entry);
166 Result = Entry->Value;
167 } else {
168 InMemoryStruct<macho::SymbolTableEntry> Entry;
169 getSymbolTableEntry(DRI, Entry);
170 Result = Entry->Value;
171 }
Michael J. Spencer25b15772011-06-25 17:55:23 +0000172 return object_error::success;
Eric Christopher6256b032011-04-22 03:19:48 +0000173}
174
Michael J. Spencer25b15772011-06-25 17:55:23 +0000175error_code MachOObjectFile::getSymbolSize(DataRefImpl DRI,
176 uint64_t &Result) const {
177 Result = UnknownAddressOrSize;
178 return object_error::success;
Eric Christopher6256b032011-04-22 03:19:48 +0000179}
180
Michael J. Spencer25b15772011-06-25 17:55:23 +0000181error_code MachOObjectFile::getSymbolNMTypeChar(DataRefImpl DRI,
182 char &Result) const {
Benjamin Kramer32fb2af2011-07-15 17:32:45 +0000183 uint8_t Type, Flags;
184 if (MachOObj->is64Bit()) {
185 InMemoryStruct<macho::Symbol64TableEntry> Entry;
186 getSymbol64TableEntry(DRI, Entry);
187 Type = Entry->Type;
188 Flags = Entry->Flags;
189 } else {
190 InMemoryStruct<macho::SymbolTableEntry> Entry;
191 getSymbolTableEntry(DRI, Entry);
192 Type = Entry->Type;
193 Flags = Entry->Flags;
194 }
Eric Christopher6256b032011-04-22 03:19:48 +0000195
196 char Char;
Benjamin Kramer32fb2af2011-07-15 17:32:45 +0000197 switch (Type & macho::STF_TypeMask) {
Eric Christopher6256b032011-04-22 03:19:48 +0000198 case macho::STT_Undefined:
199 Char = 'u';
200 break;
201 case macho::STT_Absolute:
202 case macho::STT_Section:
203 Char = 's';
204 break;
205 default:
206 Char = '?';
207 break;
208 }
209
Benjamin Kramer32fb2af2011-07-15 17:32:45 +0000210 if (Flags & (macho::STF_External | macho::STF_PrivateExtern))
Eric Christopher6256b032011-04-22 03:19:48 +0000211 Char = toupper(Char);
Michael J. Spencer25b15772011-06-25 17:55:23 +0000212 Result = Char;
213 return object_error::success;
Eric Christopher6256b032011-04-22 03:19:48 +0000214}
215
Michael J. Spencer25b15772011-06-25 17:55:23 +0000216error_code MachOObjectFile::isSymbolInternal(DataRefImpl DRI,
217 bool &Result) const {
Benjamin Kramer32fb2af2011-07-15 17:32:45 +0000218 if (MachOObj->is64Bit()) {
219 InMemoryStruct<macho::Symbol64TableEntry> Entry;
220 getSymbol64TableEntry(DRI, Entry);
221 Result = Entry->Flags & macho::STF_StabsEntryMask;
222 } else {
223 InMemoryStruct<macho::SymbolTableEntry> Entry;
224 getSymbolTableEntry(DRI, Entry);
225 Result = Entry->Flags & macho::STF_StabsEntryMask;
226 }
Michael J. Spencer25b15772011-06-25 17:55:23 +0000227 return object_error::success;
Eric Christopher6256b032011-04-22 03:19:48 +0000228}
229
230ObjectFile::symbol_iterator MachOObjectFile::begin_symbols() const {
231 // DRI.d.a = segment number; DRI.d.b = symbol index.
232 DataRefImpl DRI;
233 DRI.d.a = DRI.d.b = 0;
234 moveToNextSymbol(DRI);
235 return symbol_iterator(SymbolRef(DRI, this));
236}
237
238ObjectFile::symbol_iterator MachOObjectFile::end_symbols() const {
239 DataRefImpl DRI;
240 DRI.d.a = MachOObj->getHeader().NumLoadCommands;
241 DRI.d.b = 0;
242 return symbol_iterator(SymbolRef(DRI, this));
243}
244
245
246/*===-- Sections ----------------------------------------------------------===*/
247
248void MachOObjectFile::moveToNextSection(DataRefImpl &DRI) const {
249 uint32_t LoadCommandCount = MachOObj->getHeader().NumLoadCommands;
250 while (DRI.d.a < LoadCommandCount) {
251 LoadCommandInfo LCI = MachOObj->getLoadCommandInfo(DRI.d.a);
252 if (LCI.Command.Type == macho::LCT_Segment) {
253 InMemoryStruct<macho::SegmentLoadCommand> SegmentLoadCmd;
254 MachOObj->ReadSegmentLoadCommand(LCI, SegmentLoadCmd);
255 if (DRI.d.b < SegmentLoadCmd->NumSections)
256 return;
257 } else if (LCI.Command.Type == macho::LCT_Segment64) {
258 InMemoryStruct<macho::Segment64LoadCommand> Segment64LoadCmd;
259 MachOObj->ReadSegment64LoadCommand(LCI, Segment64LoadCmd);
260 if (DRI.d.b < Segment64LoadCmd->NumSections)
261 return;
262 }
263
264 DRI.d.a++;
265 DRI.d.b = 0;
266 }
267}
268
Michael J. Spencer25b15772011-06-25 17:55:23 +0000269error_code MachOObjectFile::getSectionNext(DataRefImpl DRI,
270 SectionRef &Result) const {
Eric Christopher6256b032011-04-22 03:19:48 +0000271 DRI.d.b++;
272 moveToNextSection(DRI);
Michael J. Spencer25b15772011-06-25 17:55:23 +0000273 Result = SectionRef(DRI, this);
274 return object_error::success;
Eric Christopher6256b032011-04-22 03:19:48 +0000275}
276
277void
278MachOObjectFile::getSection(DataRefImpl DRI,
279 InMemoryStruct<macho::Section> &Res) const {
280 InMemoryStruct<macho::SegmentLoadCommand> SLC;
281 LoadCommandInfo LCI = MachOObj->getLoadCommandInfo(DRI.d.a);
282 MachOObj->ReadSegmentLoadCommand(LCI, SLC);
283 MachOObj->ReadSection(LCI, DRI.d.b, Res);
284}
285
Benjamin Kramer7d145782011-07-15 00:14:48 +0000286void
287MachOObjectFile::getSection64(DataRefImpl DRI,
288 InMemoryStruct<macho::Section64> &Res) const {
289 InMemoryStruct<macho::Segment64LoadCommand> SLC;
290 LoadCommandInfo LCI = MachOObj->getLoadCommandInfo(DRI.d.a);
291 MachOObj->ReadSegment64LoadCommand(LCI, SLC);
292 MachOObj->ReadSection64(LCI, DRI.d.b, Res);
293}
294
295static bool is64BitLoadCommand(const MachOObject *MachOObj, DataRefImpl DRI) {
296 LoadCommandInfo LCI = MachOObj->getLoadCommandInfo(DRI.d.a);
297 if (LCI.Command.Type == macho::LCT_Segment64)
298 return true;
299 assert(LCI.Command.Type == macho::LCT_Segment && "Unexpected Type.");
300 return false;
301}
302
Michael J. Spencer25b15772011-06-25 17:55:23 +0000303error_code MachOObjectFile::getSectionName(DataRefImpl DRI,
304 StringRef &Result) const {
Benjamin Kramer7d145782011-07-15 00:14:48 +0000305 // FIXME: thread safety.
Michael J. Spencer25b15772011-06-25 17:55:23 +0000306 static char result[34];
Benjamin Kramer7d145782011-07-15 00:14:48 +0000307 if (is64BitLoadCommand(MachOObj, DRI)) {
308 InMemoryStruct<macho::Segment64LoadCommand> SLC;
309 LoadCommandInfo LCI = MachOObj->getLoadCommandInfo(DRI.d.a);
310 MachOObj->ReadSegment64LoadCommand(LCI, SLC);
311 InMemoryStruct<macho::Section64> Sect;
312 MachOObj->ReadSection64(LCI, DRI.d.b, Sect);
313
Benjamin Kramer291e7672011-07-15 00:29:02 +0000314 strcpy(result, Sect->SegmentName);
Benjamin Kramer7d145782011-07-15 00:14:48 +0000315 strcat(result, ",");
316 strcat(result, Sect->Name);
317 } else {
318 InMemoryStruct<macho::SegmentLoadCommand> SLC;
319 LoadCommandInfo LCI = MachOObj->getLoadCommandInfo(DRI.d.a);
320 MachOObj->ReadSegmentLoadCommand(LCI, SLC);
321 InMemoryStruct<macho::Section> Sect;
322 MachOObj->ReadSection(LCI, DRI.d.b, Sect);
323
Benjamin Kramer291e7672011-07-15 00:29:02 +0000324 strcpy(result, Sect->SegmentName);
Benjamin Kramer7d145782011-07-15 00:14:48 +0000325 strcat(result, ",");
326 strcat(result, Sect->Name);
327 }
Michael J. Spencer25b15772011-06-25 17:55:23 +0000328 Result = StringRef(result);
329 return object_error::success;
Eric Christopher6256b032011-04-22 03:19:48 +0000330}
331
Michael J. Spencer25b15772011-06-25 17:55:23 +0000332error_code MachOObjectFile::getSectionAddress(DataRefImpl DRI,
333 uint64_t &Result) const {
Benjamin Kramer7d145782011-07-15 00:14:48 +0000334 if (is64BitLoadCommand(MachOObj, DRI)) {
335 InMemoryStruct<macho::Section64> Sect;
336 getSection64(DRI, Sect);
337 Result = Sect->Address;
338 } else {
339 InMemoryStruct<macho::Section> Sect;
340 getSection(DRI, Sect);
341 Result = Sect->Address;
342 }
Michael J. Spencer25b15772011-06-25 17:55:23 +0000343 return object_error::success;
Eric Christopher6256b032011-04-22 03:19:48 +0000344}
345
Michael J. Spencer25b15772011-06-25 17:55:23 +0000346error_code MachOObjectFile::getSectionSize(DataRefImpl DRI,
347 uint64_t &Result) const {
Benjamin Kramer7d145782011-07-15 00:14:48 +0000348 if (is64BitLoadCommand(MachOObj, DRI)) {
349 InMemoryStruct<macho::Section64> Sect;
350 getSection64(DRI, Sect);
351 Result = Sect->Size;
352 } else {
353 InMemoryStruct<macho::Section> Sect;
354 getSection(DRI, Sect);
355 Result = Sect->Size;
356 }
Michael J. Spencer25b15772011-06-25 17:55:23 +0000357 return object_error::success;
Eric Christopher6256b032011-04-22 03:19:48 +0000358}
359
Michael J. Spencer25b15772011-06-25 17:55:23 +0000360error_code MachOObjectFile::getSectionContents(DataRefImpl DRI,
361 StringRef &Result) const {
Benjamin Kramer7d145782011-07-15 00:14:48 +0000362 if (is64BitLoadCommand(MachOObj, DRI)) {
363 InMemoryStruct<macho::Section64> Sect;
364 getSection64(DRI, Sect);
365 Result = MachOObj->getData(Sect->Offset, Sect->Size);
366 } else {
367 InMemoryStruct<macho::Section> Sect;
368 getSection(DRI, Sect);
369 Result = MachOObj->getData(Sect->Offset, Sect->Size);
370 }
Michael J. Spencer25b15772011-06-25 17:55:23 +0000371 return object_error::success;
Eric Christopher6256b032011-04-22 03:19:48 +0000372}
373
Michael J. Spencer25b15772011-06-25 17:55:23 +0000374error_code MachOObjectFile::isSectionText(DataRefImpl DRI,
375 bool &Result) const {
Benjamin Kramer7d145782011-07-15 00:14:48 +0000376 if (is64BitLoadCommand(MachOObj, DRI)) {
377 InMemoryStruct<macho::Section64> Sect;
378 getSection64(DRI, Sect);
379 Result = !strcmp(Sect->Name, "__text");
380 } else {
381 InMemoryStruct<macho::Section> Sect;
382 getSection(DRI, Sect);
383 Result = !strcmp(Sect->Name, "__text");
384 }
Michael J. Spencer25b15772011-06-25 17:55:23 +0000385 return object_error::success;
Eric Christopher6256b032011-04-22 03:19:48 +0000386}
387
Benjamin Kramer07ea23a2011-07-15 18:39:21 +0000388error_code MachOObjectFile::sectionContainsSymbol(DataRefImpl Sec,
389 DataRefImpl Symb,
390 bool &Result) const {
391 if (MachOObj->is64Bit()) {
392 InMemoryStruct<macho::Symbol64TableEntry> Entry;
393 getSymbol64TableEntry(Symb, Entry);
394 Result = Entry->SectionIndex == 1 + Sec.d.a + Sec.d.b;
395 } else {
396 InMemoryStruct<macho::SymbolTableEntry> Entry;
397 getSymbolTableEntry(Symb, Entry);
398 Result = Entry->SectionIndex == 1 + Sec.d.a + Sec.d.b;
399 }
400 return object_error::success;
401}
402
Eric Christopher6256b032011-04-22 03:19:48 +0000403ObjectFile::section_iterator MachOObjectFile::begin_sections() const {
404 DataRefImpl DRI;
405 DRI.d.a = DRI.d.b = 0;
406 moveToNextSection(DRI);
407 return section_iterator(SectionRef(DRI, this));
408}
409
410ObjectFile::section_iterator MachOObjectFile::end_sections() const {
411 DataRefImpl DRI;
412 DRI.d.a = MachOObj->getHeader().NumLoadCommands;
413 DRI.d.b = 0;
414 return section_iterator(SectionRef(DRI, this));
415}
416
417/*===-- Miscellaneous -----------------------------------------------------===*/
418
419uint8_t MachOObjectFile::getBytesInAddress() const {
420 return MachOObj->is64Bit() ? 8 : 4;
421}
422
423StringRef MachOObjectFile::getFileFormatName() const {
424 if (!MachOObj->is64Bit()) {
425 switch (MachOObj->getHeader().CPUType) {
Eric Christopherf4b2f932011-04-22 06:34:01 +0000426 case llvm::MachO::CPUTypeI386:
Eric Christopher9ab1d7f2011-04-22 04:08:58 +0000427 return "Mach-O 32-bit i386";
Eric Christopherf4b2f932011-04-22 06:34:01 +0000428 case llvm::MachO::CPUTypeARM:
Eric Christopher9ab1d7f2011-04-22 04:08:58 +0000429 return "Mach-O arm";
Eric Christopherf4b2f932011-04-22 06:34:01 +0000430 case llvm::MachO::CPUTypePowerPC:
Eric Christopher9ab1d7f2011-04-22 04:08:58 +0000431 return "Mach-O 32-bit ppc";
432 default:
Eric Christopherf4b2f932011-04-22 06:34:01 +0000433 assert((MachOObj->getHeader().CPUType & llvm::MachO::CPUArchABI64) == 0 &&
Eric Christopher9ab1d7f2011-04-22 04:08:58 +0000434 "64-bit object file when we're not 64-bit?");
435 return "Mach-O 32-bit unknown";
Eric Christopher6256b032011-04-22 03:19:48 +0000436 }
437 }
438
439 switch (MachOObj->getHeader().CPUType) {
Eric Christopherf4b2f932011-04-22 06:34:01 +0000440 case llvm::MachO::CPUTypeX86_64:
Eric Christopher9ab1d7f2011-04-22 04:08:58 +0000441 return "Mach-O 64-bit x86-64";
Eric Christopherf4b2f932011-04-22 06:34:01 +0000442 case llvm::MachO::CPUTypePowerPC64:
Eric Christopher9ab1d7f2011-04-22 04:08:58 +0000443 return "Mach-O 64-bit ppc64";
Eric Christopher6256b032011-04-22 03:19:48 +0000444 default:
Eric Christopherf4b2f932011-04-22 06:34:01 +0000445 assert((MachOObj->getHeader().CPUType & llvm::MachO::CPUArchABI64) == 1 &&
Eric Christopher9ab1d7f2011-04-22 04:08:58 +0000446 "32-bit object file when we're 64-bit?");
447 return "Mach-O 64-bit unknown";
Eric Christopher6256b032011-04-22 03:19:48 +0000448 }
449}
450
451unsigned MachOObjectFile::getArch() const {
452 switch (MachOObj->getHeader().CPUType) {
Eric Christopherf4b2f932011-04-22 06:34:01 +0000453 case llvm::MachO::CPUTypeI386:
Eric Christopher6256b032011-04-22 03:19:48 +0000454 return Triple::x86;
Eric Christopherf4b2f932011-04-22 06:34:01 +0000455 case llvm::MachO::CPUTypeX86_64:
Eric Christopher6256b032011-04-22 03:19:48 +0000456 return Triple::x86_64;
Eric Christopherf4b2f932011-04-22 06:34:01 +0000457 case llvm::MachO::CPUTypeARM:
Eric Christopher6256b032011-04-22 03:19:48 +0000458 return Triple::arm;
Eric Christopherf4b2f932011-04-22 06:34:01 +0000459 case llvm::MachO::CPUTypePowerPC:
Eric Christopher6256b032011-04-22 03:19:48 +0000460 return Triple::ppc;
Eric Christopherf4b2f932011-04-22 06:34:01 +0000461 case llvm::MachO::CPUTypePowerPC64:
Eric Christopher6256b032011-04-22 03:19:48 +0000462 return Triple::ppc64;
463 default:
464 return Triple::UnknownArch;
465 }
466}
467
468} // end namespace llvm
469