blob: 3e29b2f9a412ca889669e1dcfe9d87371589fbbe [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 Espindolaf9a6bd82012-12-19 14:15:04 +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);
Benjamin Kramer7d145782011-07-15 00:14:48 +0000488 InMemoryStruct<macho::Section64> Sect;
489 MachOObj->ReadSection64(LCI, DRI.d.b, Sect);
Rafael Espindolaf9a6bd82012-12-19 14:15:04 +0000490 Result = parseSegmentOrSectionName(Sect->Name);
Benjamin Kramer7d145782011-07-15 00:14:48 +0000491 } else {
Benjamin Kramer7d145782011-07-15 00:14:48 +0000492 LoadCommandInfo LCI = MachOObj->getLoadCommandInfo(DRI.d.a);
Benjamin Kramer7d145782011-07-15 00:14:48 +0000493 InMemoryStruct<macho::Section> Sect;
494 MachOObj->ReadSection(LCI, DRI.d.b, Sect);
Rafael Espindolaf9a6bd82012-12-19 14:15:04 +0000495 Result = parseSegmentOrSectionName(Sect->Name);
Rafael Espindolae3ec87a2012-12-13 04:07:18 +0000496 }
Rafael Espindolaf9a6bd82012-12-19 14:15:04 +0000497 return object_error::success;
498}
499
500error_code MachOObjectFile::getSectionFinalSegmentName(DataRefImpl Sec,
501 StringRef &Res) const {
502 if (is64BitLoadCommand(MachOObj.get(), Sec)) {
503 LoadCommandInfo LCI = MachOObj->getLoadCommandInfo(Sec.d.a);
504 InMemoryStruct<macho::Section64> Sect;
505 MachOObj->ReadSection64(LCI, Sec.d.b, Sect);
506 Res = parseSegmentOrSectionName(Sect->SegmentName);
507 } else {
508 LoadCommandInfo LCI = MachOObj->getLoadCommandInfo(Sec.d.a);
509 InMemoryStruct<macho::Section> Sect;
510 MachOObj->ReadSection(LCI, Sec.d.b, Sect);
511 Res = parseSegmentOrSectionName(Sect->SegmentName);
512 }
Michael J. Spencer25b15772011-06-25 17:55:23 +0000513 return object_error::success;
Eric Christopher6256b032011-04-22 03:19:48 +0000514}
515
Michael J. Spencer25b15772011-06-25 17:55:23 +0000516error_code MachOObjectFile::getSectionAddress(DataRefImpl DRI,
517 uint64_t &Result) const {
Jim Grosbach596e4742012-11-29 19:14:11 +0000518 if (is64BitLoadCommand(MachOObj.get(), DRI)) {
Benjamin Kramer7d145782011-07-15 00:14:48 +0000519 InMemoryStruct<macho::Section64> Sect;
520 getSection64(DRI, Sect);
521 Result = Sect->Address;
522 } else {
523 InMemoryStruct<macho::Section> Sect;
524 getSection(DRI, Sect);
525 Result = Sect->Address;
526 }
Michael J. Spencer25b15772011-06-25 17:55:23 +0000527 return object_error::success;
Eric Christopher6256b032011-04-22 03:19:48 +0000528}
529
Michael J. Spencer25b15772011-06-25 17:55:23 +0000530error_code MachOObjectFile::getSectionSize(DataRefImpl DRI,
531 uint64_t &Result) const {
Jim Grosbach596e4742012-11-29 19:14:11 +0000532 if (is64BitLoadCommand(MachOObj.get(), DRI)) {
Benjamin Kramer7d145782011-07-15 00:14:48 +0000533 InMemoryStruct<macho::Section64> Sect;
534 getSection64(DRI, Sect);
535 Result = Sect->Size;
536 } else {
537 InMemoryStruct<macho::Section> Sect;
538 getSection(DRI, Sect);
539 Result = Sect->Size;
540 }
Michael J. Spencer25b15772011-06-25 17:55:23 +0000541 return object_error::success;
Eric Christopher6256b032011-04-22 03:19:48 +0000542}
543
Michael J. Spencer25b15772011-06-25 17:55:23 +0000544error_code MachOObjectFile::getSectionContents(DataRefImpl DRI,
545 StringRef &Result) const {
Jim Grosbach596e4742012-11-29 19:14:11 +0000546 if (is64BitLoadCommand(MachOObj.get(), DRI)) {
Benjamin Kramer7d145782011-07-15 00:14:48 +0000547 InMemoryStruct<macho::Section64> Sect;
548 getSection64(DRI, Sect);
549 Result = MachOObj->getData(Sect->Offset, Sect->Size);
550 } else {
551 InMemoryStruct<macho::Section> Sect;
552 getSection(DRI, Sect);
553 Result = MachOObj->getData(Sect->Offset, Sect->Size);
554 }
Michael J. Spencer25b15772011-06-25 17:55:23 +0000555 return object_error::success;
Eric Christopher6256b032011-04-22 03:19:48 +0000556}
557
Michael J. Spencere2f2f072011-10-10 21:55:43 +0000558error_code MachOObjectFile::getSectionAlignment(DataRefImpl DRI,
559 uint64_t &Result) const {
Jim Grosbach596e4742012-11-29 19:14:11 +0000560 if (is64BitLoadCommand(MachOObj.get(), DRI)) {
Michael J. Spencere2f2f072011-10-10 21:55:43 +0000561 InMemoryStruct<macho::Section64> Sect;
562 getSection64(DRI, Sect);
Michael J. Spencer15565ad2011-10-10 23:36:56 +0000563 Result = uint64_t(1) << Sect->Align;
Michael J. Spencere2f2f072011-10-10 21:55:43 +0000564 } else {
565 InMemoryStruct<macho::Section> Sect;
566 getSection(DRI, Sect);
Michael J. Spencer15565ad2011-10-10 23:36:56 +0000567 Result = uint64_t(1) << Sect->Align;
Michael J. Spencere2f2f072011-10-10 21:55:43 +0000568 }
569 return object_error::success;
570}
571
Michael J. Spencer25b15772011-06-25 17:55:23 +0000572error_code MachOObjectFile::isSectionText(DataRefImpl DRI,
573 bool &Result) const {
Jim Grosbach596e4742012-11-29 19:14:11 +0000574 if (is64BitLoadCommand(MachOObj.get(), DRI)) {
Benjamin Kramer7d145782011-07-15 00:14:48 +0000575 InMemoryStruct<macho::Section64> Sect;
576 getSection64(DRI, Sect);
Tim Northover1c2b2f92012-12-17 17:59:32 +0000577 Result = Sect->Flags & macho::SF_PureInstructions;
Benjamin Kramer7d145782011-07-15 00:14:48 +0000578 } else {
579 InMemoryStruct<macho::Section> Sect;
580 getSection(DRI, Sect);
Tim Northover1c2b2f92012-12-17 17:59:32 +0000581 Result = Sect->Flags & macho::SF_PureInstructions;
Benjamin Kramer7d145782011-07-15 00:14:48 +0000582 }
Michael J. Spencer25b15772011-06-25 17:55:23 +0000583 return object_error::success;
Eric Christopher6256b032011-04-22 03:19:48 +0000584}
585
Michael J. Spencer13afc5e2011-09-28 20:57:30 +0000586error_code MachOObjectFile::isSectionData(DataRefImpl DRI,
587 bool &Result) const {
588 // FIXME: Unimplemented.
589 Result = false;
590 return object_error::success;
591}
592
593error_code MachOObjectFile::isSectionBSS(DataRefImpl DRI,
594 bool &Result) const {
595 // FIXME: Unimplemented.
596 Result = false;
597 return object_error::success;
598}
599
Preston Gurdc68dda82012-04-12 20:13:57 +0000600error_code MachOObjectFile::isSectionRequiredForExecution(DataRefImpl Sec,
601 bool &Result) const {
Andrew Kaylor30b20eb2012-10-10 01:45:52 +0000602 // FIXME: Unimplemented.
Preston Gurdc68dda82012-04-12 20:13:57 +0000603 Result = true;
604 return object_error::success;
605}
606
607error_code MachOObjectFile::isSectionVirtual(DataRefImpl Sec,
Andrew Kaylor30b20eb2012-10-10 01:45:52 +0000608 bool &Result) const {
609 // FIXME: Unimplemented.
Preston Gurdc68dda82012-04-12 20:13:57 +0000610 Result = false;
611 return object_error::success;
612}
613
614error_code MachOObjectFile::isSectionZeroInit(DataRefImpl DRI,
615 bool &Result) const {
616 if (MachOObj->is64Bit()) {
617 InMemoryStruct<macho::Section64> Sect;
618 getSection64(DRI, Sect);
Eli Friedman41827f92012-05-02 02:31:28 +0000619 unsigned SectionType = Sect->Flags & MachO::SectionFlagMaskSectionType;
620 Result = (SectionType == MachO::SectionTypeZeroFill ||
621 SectionType == MachO::SectionTypeZeroFillLarge);
Preston Gurdc68dda82012-04-12 20:13:57 +0000622 } else {
623 InMemoryStruct<macho::Section> Sect;
624 getSection(DRI, Sect);
Eli Friedman41827f92012-05-02 02:31:28 +0000625 unsigned SectionType = Sect->Flags & MachO::SectionFlagMaskSectionType;
626 Result = (SectionType == MachO::SectionTypeZeroFill ||
627 SectionType == MachO::SectionTypeZeroFillLarge);
Preston Gurdc68dda82012-04-12 20:13:57 +0000628 }
629
630 return object_error::success;
631}
632
Andrew Kaylor3a129c82012-10-10 01:41:33 +0000633error_code MachOObjectFile::isSectionReadOnlyData(DataRefImpl Sec,
634 bool &Result) const {
635 // Consider using the code from isSectionText to look for __const sections.
636 // Alternately, emit S_ATTR_PURE_INSTRUCTIONS and/or S_ATTR_SOME_INSTRUCTIONS
637 // to use section attributes to distinguish code from data.
638
639 // FIXME: Unimplemented.
640 Result = false;
641 return object_error::success;
642}
643
Benjamin Kramer07ea23a2011-07-15 18:39:21 +0000644error_code MachOObjectFile::sectionContainsSymbol(DataRefImpl Sec,
645 DataRefImpl Symb,
646 bool &Result) const {
Michael J. Spencer1130a792011-10-17 20:19:29 +0000647 SymbolRef::Type ST;
Owen Andersoncd749882011-10-12 22:21:32 +0000648 getSymbolType(Symb, ST);
David Meyer2c677272012-02-29 02:11:55 +0000649 if (ST == SymbolRef::ST_Unknown) {
Owen Andersoncd749882011-10-12 22:21:32 +0000650 Result = false;
651 return object_error::success;
652 }
653
654 uint64_t SectBegin, SectEnd;
655 getSectionAddress(Sec, SectBegin);
656 getSectionSize(Sec, SectEnd);
657 SectEnd += SectBegin;
658
Benjamin Kramer07ea23a2011-07-15 18:39:21 +0000659 if (MachOObj->is64Bit()) {
660 InMemoryStruct<macho::Symbol64TableEntry> Entry;
661 getSymbol64TableEntry(Symb, Entry);
Owen Andersoncd749882011-10-12 22:21:32 +0000662 uint64_t SymAddr= Entry->Value;
663 Result = (SymAddr >= SectBegin) && (SymAddr < SectEnd);
Benjamin Kramer07ea23a2011-07-15 18:39:21 +0000664 } else {
665 InMemoryStruct<macho::SymbolTableEntry> Entry;
666 getSymbolTableEntry(Symb, Entry);
Owen Andersoncd749882011-10-12 22:21:32 +0000667 uint64_t SymAddr= Entry->Value;
668 Result = (SymAddr >= SectBegin) && (SymAddr < SectEnd);
Benjamin Kramer07ea23a2011-07-15 18:39:21 +0000669 }
Owen Andersoncd749882011-10-12 22:21:32 +0000670
Benjamin Kramer07ea23a2011-07-15 18:39:21 +0000671 return object_error::success;
672}
673
Michael J. Spencer4344b1e2011-10-07 19:25:32 +0000674relocation_iterator MachOObjectFile::getSectionRelBegin(DataRefImpl Sec) const {
675 DataRefImpl ret;
Michael J. Spencer4344b1e2011-10-07 19:25:32 +0000676 ret.d.b = getSectionIndex(Sec);
677 return relocation_iterator(RelocationRef(ret, this));
678}
679relocation_iterator MachOObjectFile::getSectionRelEnd(DataRefImpl Sec) const {
680 uint32_t last_reloc;
Jim Grosbach596e4742012-11-29 19:14:11 +0000681 if (is64BitLoadCommand(MachOObj.get(), Sec)) {
Michael J. Spencer4344b1e2011-10-07 19:25:32 +0000682 InMemoryStruct<macho::Section64> Sect;
683 getSection64(Sec, Sect);
684 last_reloc = Sect->NumRelocationTableEntries;
685 } else {
686 InMemoryStruct<macho::Section> Sect;
687 getSection(Sec, Sect);
688 last_reloc = Sect->NumRelocationTableEntries;
689 }
690 DataRefImpl ret;
691 ret.d.a = last_reloc;
692 ret.d.b = getSectionIndex(Sec);
693 return relocation_iterator(RelocationRef(ret, this));
694}
695
696section_iterator MachOObjectFile::begin_sections() const {
Eric Christopher6256b032011-04-22 03:19:48 +0000697 DataRefImpl DRI;
Eric Christopher6256b032011-04-22 03:19:48 +0000698 moveToNextSection(DRI);
699 return section_iterator(SectionRef(DRI, this));
700}
701
Michael J. Spencer4344b1e2011-10-07 19:25:32 +0000702section_iterator MachOObjectFile::end_sections() const {
Eric Christopher6256b032011-04-22 03:19:48 +0000703 DataRefImpl DRI;
704 DRI.d.a = MachOObj->getHeader().NumLoadCommands;
Eric Christopher6256b032011-04-22 03:19:48 +0000705 return section_iterator(SectionRef(DRI, this));
706}
707
Benjamin Kramer0fcab072011-09-08 20:52:17 +0000708/*===-- Relocations -------------------------------------------------------===*/
709
710void MachOObjectFile::
711getRelocation(DataRefImpl Rel,
712 InMemoryStruct<macho::RelocationEntry> &Res) const {
713 uint32_t relOffset;
714 if (MachOObj->is64Bit()) {
715 InMemoryStruct<macho::Section64> Sect;
716 getSection64(Sections[Rel.d.b], Sect);
717 relOffset = Sect->RelocationTableOffset;
718 } else {
719 InMemoryStruct<macho::Section> Sect;
720 getSection(Sections[Rel.d.b], Sect);
721 relOffset = Sect->RelocationTableOffset;
722 }
723 MachOObj->ReadRelocationEntry(relOffset, Rel.d.a, Res);
724}
725error_code MachOObjectFile::getRelocationNext(DataRefImpl Rel,
726 RelocationRef &Res) const {
727 ++Rel.d.a;
Benjamin Kramer0fcab072011-09-08 20:52:17 +0000728 Res = RelocationRef(Rel, this);
729 return object_error::success;
730}
731error_code MachOObjectFile::getRelocationAddress(DataRefImpl Rel,
732 uint64_t &Res) const {
Owen Anderson0135fe12011-10-24 21:44:00 +0000733 const uint8_t* sectAddress = 0;
Benjamin Kramer0fcab072011-09-08 20:52:17 +0000734 if (MachOObj->is64Bit()) {
735 InMemoryStruct<macho::Section64> Sect;
736 getSection64(Sections[Rel.d.b], Sect);
Owen Anderson0135fe12011-10-24 21:44:00 +0000737 sectAddress += Sect->Address;
Benjamin Kramer0fcab072011-09-08 20:52:17 +0000738 } else {
739 InMemoryStruct<macho::Section> Sect;
740 getSection(Sections[Rel.d.b], Sect);
Owen Anderson0135fe12011-10-24 21:44:00 +0000741 sectAddress += Sect->Address;
Benjamin Kramer0fcab072011-09-08 20:52:17 +0000742 }
743 InMemoryStruct<macho::RelocationEntry> RE;
744 getRelocation(Rel, RE);
Owen Anderson1832f4d2011-10-26 20:42:54 +0000745
746 unsigned Arch = getArch();
747 bool isScattered = (Arch != Triple::x86_64) &&
748 (RE->Word0 & macho::RF_Scattered);
749 uint64_t RelAddr = 0;
Danil Malyshevb0436a72011-11-29 17:40:10 +0000750 if (isScattered)
Owen Anderson1832f4d2011-10-26 20:42:54 +0000751 RelAddr = RE->Word0 & 0xFFFFFF;
752 else
753 RelAddr = RE->Word0;
754
755 Res = reinterpret_cast<uintptr_t>(sectAddress + RelAddr);
Benjamin Kramer0fcab072011-09-08 20:52:17 +0000756 return object_error::success;
757}
Danil Malyshevb0436a72011-11-29 17:40:10 +0000758error_code MachOObjectFile::getRelocationOffset(DataRefImpl Rel,
759 uint64_t &Res) const {
760 InMemoryStruct<macho::RelocationEntry> RE;
761 getRelocation(Rel, RE);
762
763 unsigned Arch = getArch();
764 bool isScattered = (Arch != Triple::x86_64) &&
765 (RE->Word0 & macho::RF_Scattered);
766 if (isScattered)
767 Res = RE->Word0 & 0xFFFFFF;
768 else
769 Res = RE->Word0;
770 return object_error::success;
771}
Benjamin Kramer0fcab072011-09-08 20:52:17 +0000772error_code MachOObjectFile::getRelocationSymbol(DataRefImpl Rel,
773 SymbolRef &Res) const {
774 InMemoryStruct<macho::RelocationEntry> RE;
775 getRelocation(Rel, RE);
776 uint32_t SymbolIdx = RE->Word1 & 0xffffff;
777 bool isExtern = (RE->Word1 >> 27) & 1;
778
779 DataRefImpl Sym;
Benjamin Kramer0fcab072011-09-08 20:52:17 +0000780 moveToNextSymbol(Sym);
Benjamin Kramer0fcab072011-09-08 20:52:17 +0000781 if (isExtern) {
782 for (unsigned i = 0; i < SymbolIdx; i++) {
783 Sym.d.b++;
784 moveToNextSymbol(Sym);
Nick Lewycky58856ea2011-09-09 00:16:50 +0000785 assert(Sym.d.a < MachOObj->getHeader().NumLoadCommands &&
Benjamin Kramer0fcab072011-09-08 20:52:17 +0000786 "Relocation symbol index out of range!");
787 }
788 }
789 Res = SymbolRef(Sym, this);
790 return object_error::success;
791}
792error_code MachOObjectFile::getRelocationType(DataRefImpl Rel,
Owen Anderson9472b8d2011-10-26 17:08:49 +0000793 uint64_t &Res) const {
Benjamin Kramer0fcab072011-09-08 20:52:17 +0000794 InMemoryStruct<macho::RelocationEntry> RE;
795 getRelocation(Rel, RE);
Owen Andersonf8261e72011-10-26 17:10:22 +0000796 Res = RE->Word0;
797 Res <<= 32;
798 Res |= RE->Word1;
Benjamin Kramer0fcab072011-09-08 20:52:17 +0000799 return object_error::success;
800}
Michael J. Spencer4344b1e2011-10-07 19:25:32 +0000801error_code MachOObjectFile::getRelocationTypeName(DataRefImpl Rel,
802 SmallVectorImpl<char> &Result) const {
Owen Anderson0135fe12011-10-24 21:44:00 +0000803 // TODO: Support scattered relocations.
804 StringRef res;
805 InMemoryStruct<macho::RelocationEntry> RE;
806 getRelocation(Rel, RE);
Owen Anderson0135fe12011-10-24 21:44:00 +0000807
808 unsigned Arch = getArch();
Owen Anderson1832f4d2011-10-26 20:42:54 +0000809 bool isScattered = (Arch != Triple::x86_64) &&
810 (RE->Word0 & macho::RF_Scattered);
811
812 unsigned r_type;
813 if (isScattered)
814 r_type = (RE->Word0 >> 24) & 0xF;
815 else
816 r_type = (RE->Word1 >> 28) & 0xF;
817
Owen Anderson0135fe12011-10-24 21:44:00 +0000818 switch (Arch) {
819 case Triple::x86: {
Craig Toppere3298102012-05-24 06:35:32 +0000820 static const char *const Table[] = {
Owen Anderson0135fe12011-10-24 21:44:00 +0000821 "GENERIC_RELOC_VANILLA",
822 "GENERIC_RELOC_PAIR",
823 "GENERIC_RELOC_SECTDIFF",
Owen Andersoneb6bd332011-10-27 20:46:09 +0000824 "GENERIC_RELOC_PB_LA_PTR",
Owen Anderson0135fe12011-10-24 21:44:00 +0000825 "GENERIC_RELOC_LOCAL_SECTDIFF",
Owen Andersoneb6bd332011-10-27 20:46:09 +0000826 "GENERIC_RELOC_TLV" };
Owen Anderson0135fe12011-10-24 21:44:00 +0000827
Owen Andersoneb6bd332011-10-27 20:46:09 +0000828 if (r_type > 6)
Owen Anderson0135fe12011-10-24 21:44:00 +0000829 res = "Unknown";
830 else
831 res = Table[r_type];
832 break;
833 }
834 case Triple::x86_64: {
Craig Toppere3298102012-05-24 06:35:32 +0000835 static const char *const Table[] = {
Owen Andersond8fa76d2011-10-24 23:20:07 +0000836 "X86_64_RELOC_UNSIGNED",
837 "X86_64_RELOC_SIGNED",
Owen Anderson0135fe12011-10-24 21:44:00 +0000838 "X86_64_RELOC_BRANCH",
839 "X86_64_RELOC_GOT_LOAD",
840 "X86_64_RELOC_GOT",
Owen Andersond8fa76d2011-10-24 23:20:07 +0000841 "X86_64_RELOC_SUBTRACTOR",
842 "X86_64_RELOC_SIGNED_1",
843 "X86_64_RELOC_SIGNED_2",
844 "X86_64_RELOC_SIGNED_4",
845 "X86_64_RELOC_TLV" };
Owen Anderson0135fe12011-10-24 21:44:00 +0000846
Owen Andersond8fa76d2011-10-24 23:20:07 +0000847 if (r_type > 9)
Owen Anderson0135fe12011-10-24 21:44:00 +0000848 res = "Unknown";
849 else
850 res = Table[r_type];
851 break;
852 }
853 case Triple::arm: {
Craig Toppere3298102012-05-24 06:35:32 +0000854 static const char *const Table[] = {
Owen Anderson0135fe12011-10-24 21:44:00 +0000855 "ARM_RELOC_VANILLA",
856 "ARM_RELOC_PAIR",
857 "ARM_RELOC_SECTDIFF",
858 "ARM_RELOC_LOCAL_SECTDIFF",
859 "ARM_RELOC_PB_LA_PTR",
860 "ARM_RELOC_BR24",
861 "ARM_THUMB_RELOC_BR22",
862 "ARM_THUMB_32BIT_BRANCH",
863 "ARM_RELOC_HALF",
864 "ARM_RELOC_HALF_SECTDIFF" };
865
866 if (r_type > 9)
867 res = "Unknown";
868 else
869 res = Table[r_type];
870 break;
871 }
872 case Triple::ppc: {
Craig Toppere3298102012-05-24 06:35:32 +0000873 static const char *const Table[] = {
Owen Anderson0135fe12011-10-24 21:44:00 +0000874 "PPC_RELOC_VANILLA",
875 "PPC_RELOC_PAIR",
876 "PPC_RELOC_BR14",
877 "PPC_RELOC_BR24",
878 "PPC_RELOC_HI16",
879 "PPC_RELOC_LO16",
880 "PPC_RELOC_HA16",
881 "PPC_RELOC_LO14",
882 "PPC_RELOC_SECTDIFF",
883 "PPC_RELOC_PB_LA_PTR",
884 "PPC_RELOC_HI16_SECTDIFF",
885 "PPC_RELOC_LO16_SECTDIFF",
886 "PPC_RELOC_HA16_SECTDIFF",
887 "PPC_RELOC_JBSR",
888 "PPC_RELOC_LO14_SECTDIFF",
889 "PPC_RELOC_LOCAL_SECTDIFF" };
890
891 res = Table[r_type];
892 break;
893 }
894 case Triple::UnknownArch:
895 res = "Unknown";
896 break;
897 }
Owen Anderson5f4e02c2011-10-24 20:19:18 +0000898 Result.append(res.begin(), res.end());
Michael J. Spencer4344b1e2011-10-07 19:25:32 +0000899 return object_error::success;
900}
Benjamin Kramer0fcab072011-09-08 20:52:17 +0000901error_code MachOObjectFile::getRelocationAdditionalInfo(DataRefImpl Rel,
902 int64_t &Res) const {
903 InMemoryStruct<macho::RelocationEntry> RE;
904 getRelocation(Rel, RE);
905 bool isExtern = (RE->Word1 >> 27) & 1;
906 Res = 0;
907 if (!isExtern) {
908 const uint8_t* sectAddress = base();
909 if (MachOObj->is64Bit()) {
910 InMemoryStruct<macho::Section64> Sect;
911 getSection64(Sections[Rel.d.b], Sect);
912 sectAddress += Sect->Offset;
913 } else {
914 InMemoryStruct<macho::Section> Sect;
915 getSection(Sections[Rel.d.b], Sect);
916 sectAddress += Sect->Offset;
917 }
918 Res = reinterpret_cast<uintptr_t>(sectAddress);
919 }
920 return object_error::success;
921}
Owen Andersond8fa76d2011-10-24 23:20:07 +0000922
923// Helper to advance a section or symbol iterator multiple increments at a time.
924template<class T>
925error_code advance(T &it, size_t Val) {
926 error_code ec;
927 while (Val--) {
928 it.increment(ec);
929 }
930 return ec;
931}
932
933template<class T>
934void advanceTo(T &it, size_t Val) {
935 if (error_code ec = advance(it, Val))
936 report_fatal_error(ec.message());
937}
938
Owen Anderson1832f4d2011-10-26 20:42:54 +0000939void MachOObjectFile::printRelocationTargetName(
940 InMemoryStruct<macho::RelocationEntry>& RE,
941 raw_string_ostream &fmt) const {
942 unsigned Arch = getArch();
943 bool isScattered = (Arch != Triple::x86_64) &&
944 (RE->Word0 & macho::RF_Scattered);
945
946 // Target of a scattered relocation is an address. In the interest of
947 // generating pretty output, scan through the symbol table looking for a
948 // symbol that aligns with that address. If we find one, print it.
949 // Otherwise, we just print the hex address of the target.
950 if (isScattered) {
951 uint32_t Val = RE->Word1;
952
953 error_code ec;
954 for (symbol_iterator SI = begin_symbols(), SE = end_symbols(); SI != SE;
955 SI.increment(ec)) {
956 if (ec) report_fatal_error(ec.message());
957
958 uint64_t Addr;
959 StringRef Name;
960
961 if ((ec = SI->getAddress(Addr)))
962 report_fatal_error(ec.message());
963 if (Addr != Val) continue;
964 if ((ec = SI->getName(Name)))
965 report_fatal_error(ec.message());
966 fmt << Name;
967 return;
968 }
969
Owen Andersonb28bdbf2011-10-27 21:53:50 +0000970 // If we couldn't find a symbol that this relocation refers to, try
971 // to find a section beginning instead.
972 for (section_iterator SI = begin_sections(), SE = end_sections(); SI != SE;
973 SI.increment(ec)) {
974 if (ec) report_fatal_error(ec.message());
975
976 uint64_t Addr;
977 StringRef Name;
978
979 if ((ec = SI->getAddress(Addr)))
980 report_fatal_error(ec.message());
981 if (Addr != Val) continue;
982 if ((ec = SI->getName(Name)))
983 report_fatal_error(ec.message());
984 fmt << Name;
985 return;
986 }
987
Owen Anderson1832f4d2011-10-26 20:42:54 +0000988 fmt << format("0x%x", Val);
989 return;
990 }
991
992 StringRef S;
993 bool isExtern = (RE->Word1 >> 27) & 1;
994 uint32_t Val = RE->Word1 & 0xFFFFFF;
Owen Andersond8fa76d2011-10-24 23:20:07 +0000995
996 if (isExtern) {
997 symbol_iterator SI = begin_symbols();
998 advanceTo(SI, Val);
Owen Anderson1832f4d2011-10-26 20:42:54 +0000999 SI->getName(S);
Owen Andersond8fa76d2011-10-24 23:20:07 +00001000 } else {
1001 section_iterator SI = begin_sections();
1002 advanceTo(SI, Val);
Owen Anderson1832f4d2011-10-26 20:42:54 +00001003 SI->getName(S);
Owen Andersond8fa76d2011-10-24 23:20:07 +00001004 }
1005
Owen Anderson1832f4d2011-10-26 20:42:54 +00001006 fmt << S;
Owen Andersond8fa76d2011-10-24 23:20:07 +00001007}
1008
Michael J. Spencer4344b1e2011-10-07 19:25:32 +00001009error_code MachOObjectFile::getRelocationValueString(DataRefImpl Rel,
1010 SmallVectorImpl<char> &Result) const {
Owen Anderson0135fe12011-10-24 21:44:00 +00001011 InMemoryStruct<macho::RelocationEntry> RE;
1012 getRelocation(Rel, RE);
1013
Owen Anderson1832f4d2011-10-26 20:42:54 +00001014 unsigned Arch = getArch();
1015 bool isScattered = (Arch != Triple::x86_64) &&
1016 (RE->Word0 & macho::RF_Scattered);
Owen Andersond8fa76d2011-10-24 23:20:07 +00001017
Owen Anderson013d7562011-10-25 18:48:41 +00001018 std::string fmtbuf;
1019 raw_string_ostream fmt(fmtbuf);
1020
Owen Anderson1832f4d2011-10-26 20:42:54 +00001021 unsigned Type;
1022 if (isScattered)
1023 Type = (RE->Word0 >> 24) & 0xF;
1024 else
1025 Type = (RE->Word1 >> 28) & 0xF;
1026
Owen Andersoneb6bd332011-10-27 20:46:09 +00001027 bool isPCRel;
1028 if (isScattered)
1029 isPCRel = ((RE->Word0 >> 30) & 1);
1030 else
1031 isPCRel = ((RE->Word1 >> 24) & 1);
1032
Owen Andersond8fa76d2011-10-24 23:20:07 +00001033 // Determine any addends that should be displayed with the relocation.
1034 // These require decoding the relocation type, which is triple-specific.
Owen Andersond8fa76d2011-10-24 23:20:07 +00001035
1036 // X86_64 has entirely custom relocation types.
1037 if (Arch == Triple::x86_64) {
Owen Anderson929e27c2011-10-26 17:05:20 +00001038 bool isPCRel = ((RE->Word1 >> 24) & 1);
Owen Anderson013d7562011-10-25 18:48:41 +00001039
Owen Andersond8fa76d2011-10-24 23:20:07 +00001040 switch (Type) {
Owen Anderson1832f4d2011-10-26 20:42:54 +00001041 case macho::RIT_X86_64_GOTLoad: // X86_64_RELOC_GOT_LOAD
1042 case macho::RIT_X86_64_GOT: { // X86_64_RELOC_GOT
1043 printRelocationTargetName(RE, fmt);
1044 fmt << "@GOT";
Owen Anderson929e27c2011-10-26 17:05:20 +00001045 if (isPCRel) fmt << "PCREL";
1046 break;
1047 }
Owen Anderson1832f4d2011-10-26 20:42:54 +00001048 case macho::RIT_X86_64_Subtractor: { // X86_64_RELOC_SUBTRACTOR
Owen Anderson013d7562011-10-25 18:48:41 +00001049 InMemoryStruct<macho::RelocationEntry> RENext;
1050 DataRefImpl RelNext = Rel;
1051 RelNext.d.a++;
1052 getRelocation(RelNext, RENext);
Owen Andersond8fa76d2011-10-24 23:20:07 +00001053
1054 // X86_64_SUBTRACTOR must be followed by a relocation of type
1055 // X86_64_RELOC_UNSIGNED.
Owen Anderson1832f4d2011-10-26 20:42:54 +00001056 // NOTE: Scattered relocations don't exist on x86_64.
Owen Anderson013d7562011-10-25 18:48:41 +00001057 unsigned RType = (RENext->Word1 >> 28) & 0xF;
Owen Andersond8fa76d2011-10-24 23:20:07 +00001058 if (RType != 0)
1059 report_fatal_error("Expected X86_64_RELOC_UNSIGNED after "
1060 "X86_64_RELOC_SUBTRACTOR.");
1061
Owen Andersonef22f782011-10-26 17:28:49 +00001062 // The X86_64_RELOC_UNSIGNED contains the minuend symbol,
1063 // X86_64_SUBTRACTOR contains to the subtrahend.
Owen Anderson1832f4d2011-10-26 20:42:54 +00001064 printRelocationTargetName(RENext, fmt);
1065 fmt << "-";
1066 printRelocationTargetName(RE, fmt);
Owen Andersond8fa76d2011-10-24 23:20:07 +00001067 }
Owen Andersoneb6bd332011-10-27 20:46:09 +00001068 case macho::RIT_X86_64_TLV:
1069 printRelocationTargetName(RE, fmt);
1070 fmt << "@TLV";
1071 if (isPCRel) fmt << "P";
1072 break;
Owen Anderson1832f4d2011-10-26 20:42:54 +00001073 case macho::RIT_X86_64_Signed1: // X86_64_RELOC_SIGNED1
1074 printRelocationTargetName(RE, fmt);
1075 fmt << "-1";
Owen Andersond8fa76d2011-10-24 23:20:07 +00001076 break;
Owen Anderson1832f4d2011-10-26 20:42:54 +00001077 case macho::RIT_X86_64_Signed2: // X86_64_RELOC_SIGNED2
1078 printRelocationTargetName(RE, fmt);
1079 fmt << "-2";
Owen Andersond8fa76d2011-10-24 23:20:07 +00001080 break;
Owen Anderson1832f4d2011-10-26 20:42:54 +00001081 case macho::RIT_X86_64_Signed4: // X86_64_RELOC_SIGNED4
1082 printRelocationTargetName(RE, fmt);
1083 fmt << "-4";
Owen Anderson013d7562011-10-25 18:48:41 +00001084 break;
1085 default:
Owen Anderson1832f4d2011-10-26 20:42:54 +00001086 printRelocationTargetName(RE, fmt);
Owen Andersond8fa76d2011-10-24 23:20:07 +00001087 break;
1088 }
Owen Andersond8fa76d2011-10-24 23:20:07 +00001089 // X86 and ARM share some relocation types in common.
Owen Anderson013d7562011-10-25 18:48:41 +00001090 } else if (Arch == Triple::x86 || Arch == Triple::arm) {
1091 // Generic relocation types...
Owen Andersond8fa76d2011-10-24 23:20:07 +00001092 switch (Type) {
Owen Anderson1832f4d2011-10-26 20:42:54 +00001093 case macho::RIT_Pair: // GENERIC_RELOC_PAIR - prints no info
Owen Andersond8fa76d2011-10-24 23:20:07 +00001094 return object_error::success;
Owen Andersoneb6bd332011-10-27 20:46:09 +00001095 case macho::RIT_Difference: { // GENERIC_RELOC_SECTDIFF
Owen Anderson013d7562011-10-25 18:48:41 +00001096 InMemoryStruct<macho::RelocationEntry> RENext;
1097 DataRefImpl RelNext = Rel;
1098 RelNext.d.a++;
1099 getRelocation(RelNext, RENext);
Owen Andersond8fa76d2011-10-24 23:20:07 +00001100
1101 // X86 sect diff's must be followed by a relocation of type
1102 // GENERIC_RELOC_PAIR.
Owen Anderson1832f4d2011-10-26 20:42:54 +00001103 bool isNextScattered = (Arch != Triple::x86_64) &&
1104 (RENext->Word0 & macho::RF_Scattered);
1105 unsigned RType;
1106 if (isNextScattered)
1107 RType = (RENext->Word0 >> 24) & 0xF;
1108 else
1109 RType = (RENext->Word1 >> 28) & 0xF;
Owen Andersond8fa76d2011-10-24 23:20:07 +00001110 if (RType != 1)
1111 report_fatal_error("Expected GENERIC_RELOC_PAIR after "
Owen Andersoneb6bd332011-10-27 20:46:09 +00001112 "GENERIC_RELOC_SECTDIFF.");
Owen Andersond8fa76d2011-10-24 23:20:07 +00001113
Owen Anderson1832f4d2011-10-26 20:42:54 +00001114 printRelocationTargetName(RE, fmt);
1115 fmt << "-";
1116 printRelocationTargetName(RENext, fmt);
Owen Anderson013d7562011-10-25 18:48:41 +00001117 break;
Owen Andersond8fa76d2011-10-24 23:20:07 +00001118 }
1119 }
Owen Anderson013d7562011-10-25 18:48:41 +00001120
Owen Anderson1832f4d2011-10-26 20:42:54 +00001121 if (Arch == Triple::x86) {
Owen Anderson013d7562011-10-25 18:48:41 +00001122 // All X86 relocations that need special printing were already
1123 // handled in the generic code.
Owen Andersoneb6bd332011-10-27 20:46:09 +00001124 switch (Type) {
1125 case macho::RIT_Generic_LocalDifference:{// GENERIC_RELOC_LOCAL_SECTDIFF
1126 InMemoryStruct<macho::RelocationEntry> RENext;
1127 DataRefImpl RelNext = Rel;
1128 RelNext.d.a++;
1129 getRelocation(RelNext, RENext);
1130
1131 // X86 sect diff's must be followed by a relocation of type
1132 // GENERIC_RELOC_PAIR.
1133 bool isNextScattered = (Arch != Triple::x86_64) &&
1134 (RENext->Word0 & macho::RF_Scattered);
1135 unsigned RType;
1136 if (isNextScattered)
1137 RType = (RENext->Word0 >> 24) & 0xF;
1138 else
1139 RType = (RENext->Word1 >> 28) & 0xF;
1140 if (RType != 1)
1141 report_fatal_error("Expected GENERIC_RELOC_PAIR after "
1142 "GENERIC_RELOC_LOCAL_SECTDIFF.");
1143
1144 printRelocationTargetName(RE, fmt);
1145 fmt << "-";
1146 printRelocationTargetName(RENext, fmt);
1147 break;
1148 }
1149 case macho::RIT_Generic_TLV: {
1150 printRelocationTargetName(RE, fmt);
1151 fmt << "@TLV";
1152 if (isPCRel) fmt << "P";
1153 break;
1154 }
1155 default:
1156 printRelocationTargetName(RE, fmt);
1157 }
Owen Anderson013d7562011-10-25 18:48:41 +00001158 } else { // ARM-specific relocations
1159 switch (Type) {
Owen Anderson1832f4d2011-10-26 20:42:54 +00001160 case macho::RIT_ARM_Half: // ARM_RELOC_HALF
1161 case macho::RIT_ARM_HalfDifference: { // ARM_RELOC_HALF_SECTDIFF
Owen Anderson013d7562011-10-25 18:48:41 +00001162 // Half relocations steal a bit from the length field to encode
1163 // whether this is an upper16 or a lower16 relocation.
Owen Anderson1832f4d2011-10-26 20:42:54 +00001164 bool isUpper;
1165 if (isScattered)
1166 isUpper = (RE->Word0 >> 28) & 1;
Owen Anderson013d7562011-10-25 18:48:41 +00001167 else
Owen Anderson1832f4d2011-10-26 20:42:54 +00001168 isUpper = (RE->Word1 >> 25) & 1;
1169
1170 if (isUpper)
1171 fmt << ":upper16:(";
1172 else
1173 fmt << ":lower16:(";
1174 printRelocationTargetName(RE, fmt);
Owen Anderson013d7562011-10-25 18:48:41 +00001175
1176 InMemoryStruct<macho::RelocationEntry> RENext;
1177 DataRefImpl RelNext = Rel;
1178 RelNext.d.a++;
1179 getRelocation(RelNext, RENext);
1180
1181 // ARM half relocs must be followed by a relocation of type
1182 // ARM_RELOC_PAIR.
Owen Anderson1832f4d2011-10-26 20:42:54 +00001183 bool isNextScattered = (Arch != Triple::x86_64) &&
1184 (RENext->Word0 & macho::RF_Scattered);
1185 unsigned RType;
1186 if (isNextScattered)
1187 RType = (RENext->Word0 >> 24) & 0xF;
1188 else
1189 RType = (RENext->Word1 >> 28) & 0xF;
1190
Owen Anderson013d7562011-10-25 18:48:41 +00001191 if (RType != 1)
1192 report_fatal_error("Expected ARM_RELOC_PAIR after "
1193 "GENERIC_RELOC_HALF");
1194
Owen Anderson1832f4d2011-10-26 20:42:54 +00001195 // NOTE: The half of the target virtual address is stashed in the
1196 // address field of the secondary relocation, but we can't reverse
1197 // engineer the constant offset from it without decoding the movw/movt
1198 // instruction to find the other half in its immediate field.
Owen Anderson013d7562011-10-25 18:48:41 +00001199
1200 // ARM_RELOC_HALF_SECTDIFF encodes the second section in the
1201 // symbol/section pointer of the follow-on relocation.
Owen Anderson1832f4d2011-10-26 20:42:54 +00001202 if (Type == macho::RIT_ARM_HalfDifference) {
1203 fmt << "-";
1204 printRelocationTargetName(RENext, fmt);
Owen Anderson013d7562011-10-25 18:48:41 +00001205 }
1206
Owen Anderson013d7562011-10-25 18:48:41 +00001207 fmt << ")";
1208 break;
1209 }
1210 default: {
Owen Anderson1832f4d2011-10-26 20:42:54 +00001211 printRelocationTargetName(RE, fmt);
Owen Anderson013d7562011-10-25 18:48:41 +00001212 }
1213 }
1214 }
Owen Anderson1832f4d2011-10-26 20:42:54 +00001215 } else
1216 printRelocationTargetName(RE, fmt);
Owen Andersond8fa76d2011-10-24 23:20:07 +00001217
Owen Anderson0135fe12011-10-24 21:44:00 +00001218 fmt.flush();
1219 Result.append(fmtbuf.begin(), fmtbuf.end());
Michael J. Spencer4344b1e2011-10-07 19:25:32 +00001220 return object_error::success;
Benjamin Kramer0fcab072011-09-08 20:52:17 +00001221}
1222
Owen Anderson0685e942011-10-25 20:35:53 +00001223error_code MachOObjectFile::getRelocationHidden(DataRefImpl Rel,
1224 bool &Result) const {
1225 InMemoryStruct<macho::RelocationEntry> RE;
1226 getRelocation(Rel, RE);
1227
Owen Anderson0685e942011-10-25 20:35:53 +00001228 unsigned Arch = getArch();
Owen Anderson1832f4d2011-10-26 20:42:54 +00001229 bool isScattered = (Arch != Triple::x86_64) &&
1230 (RE->Word0 & macho::RF_Scattered);
1231 unsigned Type;
1232 if (isScattered)
1233 Type = (RE->Word0 >> 24) & 0xF;
1234 else
1235 Type = (RE->Word1 >> 28) & 0xF;
Owen Anderson0685e942011-10-25 20:35:53 +00001236
1237 Result = false;
1238
1239 // On arches that use the generic relocations, GENERIC_RELOC_PAIR
1240 // is always hidden.
1241 if (Arch == Triple::x86 || Arch == Triple::arm) {
Owen Anderson1832f4d2011-10-26 20:42:54 +00001242 if (Type == macho::RIT_Pair) Result = true;
Owen Anderson0685e942011-10-25 20:35:53 +00001243 } else if (Arch == Triple::x86_64) {
1244 // On x86_64, X86_64_RELOC_UNSIGNED is hidden only when it follows
1245 // an X864_64_RELOC_SUBTRACTOR.
Owen Anderson1832f4d2011-10-26 20:42:54 +00001246 if (Type == macho::RIT_X86_64_Unsigned && Rel.d.a > 0) {
Owen Anderson0685e942011-10-25 20:35:53 +00001247 DataRefImpl RelPrev = Rel;
1248 RelPrev.d.a--;
1249 InMemoryStruct<macho::RelocationEntry> REPrev;
1250 getRelocation(RelPrev, REPrev);
1251
1252 unsigned PrevType = (REPrev->Word1 >> 28) & 0xF;
1253
Owen Anderson1832f4d2011-10-26 20:42:54 +00001254 if (PrevType == macho::RIT_X86_64_Subtractor) Result = true;
Owen Anderson0685e942011-10-25 20:35:53 +00001255 }
1256 }
1257
1258 return object_error::success;
1259}
1260
David Meyer5c2b4ea2012-03-01 01:36:50 +00001261error_code MachOObjectFile::getLibraryNext(DataRefImpl LibData,
1262 LibraryRef &Res) const {
1263 report_fatal_error("Needed libraries unimplemented in MachOObjectFile");
1264}
1265
1266error_code MachOObjectFile::getLibraryPath(DataRefImpl LibData,
1267 StringRef &Res) const {
1268 report_fatal_error("Needed libraries unimplemented in MachOObjectFile");
1269}
1270
1271
Eric Christopher6256b032011-04-22 03:19:48 +00001272/*===-- Miscellaneous -----------------------------------------------------===*/
1273
1274uint8_t MachOObjectFile::getBytesInAddress() const {
1275 return MachOObj->is64Bit() ? 8 : 4;
1276}
1277
1278StringRef MachOObjectFile::getFileFormatName() const {
1279 if (!MachOObj->is64Bit()) {
1280 switch (MachOObj->getHeader().CPUType) {
Eric Christopherf4b2f932011-04-22 06:34:01 +00001281 case llvm::MachO::CPUTypeI386:
Eric Christopher9ab1d7f2011-04-22 04:08:58 +00001282 return "Mach-O 32-bit i386";
Eric Christopherf4b2f932011-04-22 06:34:01 +00001283 case llvm::MachO::CPUTypeARM:
Eric Christopher9ab1d7f2011-04-22 04:08:58 +00001284 return "Mach-O arm";
Eric Christopherf4b2f932011-04-22 06:34:01 +00001285 case llvm::MachO::CPUTypePowerPC:
Eric Christopher9ab1d7f2011-04-22 04:08:58 +00001286 return "Mach-O 32-bit ppc";
1287 default:
Eric Christopherf4b2f932011-04-22 06:34:01 +00001288 assert((MachOObj->getHeader().CPUType & llvm::MachO::CPUArchABI64) == 0 &&
Eric Christopher9ab1d7f2011-04-22 04:08:58 +00001289 "64-bit object file when we're not 64-bit?");
1290 return "Mach-O 32-bit unknown";
Eric Christopher6256b032011-04-22 03:19:48 +00001291 }
1292 }
1293
1294 switch (MachOObj->getHeader().CPUType) {
Eric Christopherf4b2f932011-04-22 06:34:01 +00001295 case llvm::MachO::CPUTypeX86_64:
Eric Christopher9ab1d7f2011-04-22 04:08:58 +00001296 return "Mach-O 64-bit x86-64";
Eric Christopherf4b2f932011-04-22 06:34:01 +00001297 case llvm::MachO::CPUTypePowerPC64:
Eric Christopher9ab1d7f2011-04-22 04:08:58 +00001298 return "Mach-O 64-bit ppc64";
Eric Christopher6256b032011-04-22 03:19:48 +00001299 default:
Eric Christopherf4b2f932011-04-22 06:34:01 +00001300 assert((MachOObj->getHeader().CPUType & llvm::MachO::CPUArchABI64) == 1 &&
Eric Christopher9ab1d7f2011-04-22 04:08:58 +00001301 "32-bit object file when we're 64-bit?");
1302 return "Mach-O 64-bit unknown";
Eric Christopher6256b032011-04-22 03:19:48 +00001303 }
1304}
1305
1306unsigned MachOObjectFile::getArch() const {
1307 switch (MachOObj->getHeader().CPUType) {
Eric Christopherf4b2f932011-04-22 06:34:01 +00001308 case llvm::MachO::CPUTypeI386:
Eric Christopher6256b032011-04-22 03:19:48 +00001309 return Triple::x86;
Eric Christopherf4b2f932011-04-22 06:34:01 +00001310 case llvm::MachO::CPUTypeX86_64:
Eric Christopher6256b032011-04-22 03:19:48 +00001311 return Triple::x86_64;
Eric Christopherf4b2f932011-04-22 06:34:01 +00001312 case llvm::MachO::CPUTypeARM:
Eric Christopher6256b032011-04-22 03:19:48 +00001313 return Triple::arm;
Eric Christopherf4b2f932011-04-22 06:34:01 +00001314 case llvm::MachO::CPUTypePowerPC:
Eric Christopher6256b032011-04-22 03:19:48 +00001315 return Triple::ppc;
Eric Christopherf4b2f932011-04-22 06:34:01 +00001316 case llvm::MachO::CPUTypePowerPC64:
Eric Christopher6256b032011-04-22 03:19:48 +00001317 return Triple::ppc64;
1318 default:
1319 return Triple::UnknownArch;
1320 }
1321}
1322
Owen Andersonf7c93a32011-10-11 17:32:27 +00001323} // end namespace object
Eric Christopher6256b032011-04-22 03:19:48 +00001324} // end namespace llvm