blob: 9ab3599e2d2ffee56c4ee94c909bd93709558a09 [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))
Guy Benyei87d0b9e2013-02-12 21:21:59 +0000276 Char = toupper(static_cast<unsigned char>(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
Rafael Espindolaf16c2bb2013-04-05 15:15:22 +0000484ArrayRef<char> MachOObjectFile::getSectionRawName(DataRefImpl DRI) const {
Jim Grosbach596e4742012-11-29 19:14:11 +0000485 if (is64BitLoadCommand(MachOObj.get(), DRI)) {
Benjamin Kramer7d145782011-07-15 00:14:48 +0000486 LoadCommandInfo LCI = MachOObj->getLoadCommandInfo(DRI.d.a);
Rafael Espindolacef81b32012-12-21 03:47:03 +0000487 unsigned SectionOffset = LCI.Offset + sizeof(macho::Segment64LoadCommand) +
488 DRI.d.b * sizeof(macho::Section64);
489 StringRef Data = MachOObj->getData(SectionOffset, sizeof(macho::Section64));
490 const macho::Section64 *sec =
491 reinterpret_cast<const macho::Section64*>(Data.data());
Rafael Espindolaf16c2bb2013-04-05 15:15:22 +0000492 return ArrayRef<char>(sec->Name, 16);
Benjamin Kramer7d145782011-07-15 00:14:48 +0000493 } else {
Benjamin Kramer7d145782011-07-15 00:14:48 +0000494 LoadCommandInfo LCI = MachOObj->getLoadCommandInfo(DRI.d.a);
Rafael Espindolacef81b32012-12-21 03:47:03 +0000495 unsigned SectionOffset = LCI.Offset + sizeof(macho::SegmentLoadCommand) +
496 DRI.d.b * sizeof(macho::Section);
497 StringRef Data = MachOObj->getData(SectionOffset, sizeof(macho::Section));
498 const macho::Section *sec =
499 reinterpret_cast<const macho::Section*>(Data.data());
Rafael Espindolaf16c2bb2013-04-05 15:15:22 +0000500 return ArrayRef<char>(sec->Name, 16);
Rafael Espindolaf9a6bd82012-12-19 14:15:04 +0000501 }
Rafael Espindolaf16c2bb2013-04-05 15:15:22 +0000502}
503
504error_code MachOObjectFile::getSectionName(DataRefImpl DRI,
505 StringRef &Result) const {
506 ArrayRef<char> Raw = getSectionRawName(DRI);
507 Result = parseSegmentOrSectionName(Raw.data());
Rafael Espindolacef81b32012-12-21 03:47:03 +0000508 return object_error::success;
509}
510
Rafael Espindolaf16c2bb2013-04-05 15:15:22 +0000511ArrayRef<char>
512MachOObjectFile::getSectionRawFinalSegmentName(DataRefImpl Sec) const {
Rafael Espindolacef81b32012-12-21 03:47:03 +0000513 if (is64BitLoadCommand(MachOObj.get(), Sec)) {
514 LoadCommandInfo LCI = MachOObj->getLoadCommandInfo(Sec.d.a);
515 unsigned SectionOffset = LCI.Offset + sizeof(macho::Segment64LoadCommand) +
516 Sec.d.b * sizeof(macho::Section64);
517 StringRef Data = MachOObj->getData(SectionOffset, sizeof(macho::Section64));
518 const macho::Section64 *sec =
519 reinterpret_cast<const macho::Section64*>(Data.data());
Rafael Espindolaf16c2bb2013-04-05 15:15:22 +0000520 return ArrayRef<char>(sec->SegmentName, 16);
Rafael Espindolacef81b32012-12-21 03:47:03 +0000521 } else {
522 LoadCommandInfo LCI = MachOObj->getLoadCommandInfo(Sec.d.a);
523 unsigned SectionOffset = LCI.Offset + sizeof(macho::SegmentLoadCommand) +
524 Sec.d.b * sizeof(macho::Section);
525 StringRef Data = MachOObj->getData(SectionOffset, sizeof(macho::Section));
526 const macho::Section *sec =
527 reinterpret_cast<const macho::Section*>(Data.data());
Rafael Espindolaf16c2bb2013-04-05 15:15:22 +0000528 return ArrayRef<char>(sec->SegmentName);
Rafael Espindolacef81b32012-12-21 03:47:03 +0000529 }
Rafael Espindolaf16c2bb2013-04-05 15:15:22 +0000530}
531
532StringRef MachOObjectFile::getSectionFinalSegmentName(DataRefImpl DRI) const {
533 ArrayRef<char> Raw = getSectionRawFinalSegmentName(DRI);
534 return parseSegmentOrSectionName(Raw.data());
Eric Christopher6256b032011-04-22 03:19:48 +0000535}
536
Michael J. Spencer25b15772011-06-25 17:55:23 +0000537error_code MachOObjectFile::getSectionAddress(DataRefImpl DRI,
538 uint64_t &Result) const {
Jim Grosbach596e4742012-11-29 19:14:11 +0000539 if (is64BitLoadCommand(MachOObj.get(), DRI)) {
Benjamin Kramer7d145782011-07-15 00:14:48 +0000540 InMemoryStruct<macho::Section64> Sect;
541 getSection64(DRI, Sect);
542 Result = Sect->Address;
543 } else {
544 InMemoryStruct<macho::Section> Sect;
545 getSection(DRI, Sect);
546 Result = Sect->Address;
547 }
Michael J. Spencer25b15772011-06-25 17:55:23 +0000548 return object_error::success;
Eric Christopher6256b032011-04-22 03:19:48 +0000549}
550
Michael J. Spencer25b15772011-06-25 17:55:23 +0000551error_code MachOObjectFile::getSectionSize(DataRefImpl DRI,
552 uint64_t &Result) const {
Jim Grosbach596e4742012-11-29 19:14:11 +0000553 if (is64BitLoadCommand(MachOObj.get(), DRI)) {
Benjamin Kramer7d145782011-07-15 00:14:48 +0000554 InMemoryStruct<macho::Section64> Sect;
555 getSection64(DRI, Sect);
556 Result = Sect->Size;
557 } else {
558 InMemoryStruct<macho::Section> Sect;
559 getSection(DRI, Sect);
560 Result = Sect->Size;
561 }
Michael J. Spencer25b15772011-06-25 17:55:23 +0000562 return object_error::success;
Eric Christopher6256b032011-04-22 03:19:48 +0000563}
564
Michael J. Spencer25b15772011-06-25 17:55:23 +0000565error_code MachOObjectFile::getSectionContents(DataRefImpl DRI,
566 StringRef &Result) const {
Jim Grosbach596e4742012-11-29 19:14:11 +0000567 if (is64BitLoadCommand(MachOObj.get(), DRI)) {
Benjamin Kramer7d145782011-07-15 00:14:48 +0000568 InMemoryStruct<macho::Section64> Sect;
569 getSection64(DRI, Sect);
570 Result = MachOObj->getData(Sect->Offset, Sect->Size);
571 } else {
572 InMemoryStruct<macho::Section> Sect;
573 getSection(DRI, Sect);
574 Result = MachOObj->getData(Sect->Offset, Sect->Size);
575 }
Michael J. Spencer25b15772011-06-25 17:55:23 +0000576 return object_error::success;
Eric Christopher6256b032011-04-22 03:19:48 +0000577}
578
Michael J. Spencere2f2f072011-10-10 21:55:43 +0000579error_code MachOObjectFile::getSectionAlignment(DataRefImpl DRI,
580 uint64_t &Result) const {
Jim Grosbach596e4742012-11-29 19:14:11 +0000581 if (is64BitLoadCommand(MachOObj.get(), DRI)) {
Michael J. Spencere2f2f072011-10-10 21:55:43 +0000582 InMemoryStruct<macho::Section64> Sect;
583 getSection64(DRI, Sect);
Michael J. Spencer15565ad2011-10-10 23:36:56 +0000584 Result = uint64_t(1) << Sect->Align;
Michael J. Spencere2f2f072011-10-10 21:55:43 +0000585 } else {
586 InMemoryStruct<macho::Section> Sect;
587 getSection(DRI, Sect);
Michael J. Spencer15565ad2011-10-10 23:36:56 +0000588 Result = uint64_t(1) << Sect->Align;
Michael J. Spencere2f2f072011-10-10 21:55:43 +0000589 }
590 return object_error::success;
591}
592
Michael J. Spencer25b15772011-06-25 17:55:23 +0000593error_code MachOObjectFile::isSectionText(DataRefImpl DRI,
594 bool &Result) const {
Jim Grosbach596e4742012-11-29 19:14:11 +0000595 if (is64BitLoadCommand(MachOObj.get(), DRI)) {
Benjamin Kramer7d145782011-07-15 00:14:48 +0000596 InMemoryStruct<macho::Section64> Sect;
597 getSection64(DRI, Sect);
Tim Northover1c2b2f92012-12-17 17:59:32 +0000598 Result = Sect->Flags & macho::SF_PureInstructions;
Benjamin Kramer7d145782011-07-15 00:14:48 +0000599 } else {
600 InMemoryStruct<macho::Section> Sect;
601 getSection(DRI, Sect);
Tim Northover1c2b2f92012-12-17 17:59:32 +0000602 Result = Sect->Flags & macho::SF_PureInstructions;
Benjamin Kramer7d145782011-07-15 00:14:48 +0000603 }
Michael J. Spencer25b15772011-06-25 17:55:23 +0000604 return object_error::success;
Eric Christopher6256b032011-04-22 03:19:48 +0000605}
606
Michael J. Spencer13afc5e2011-09-28 20:57:30 +0000607error_code MachOObjectFile::isSectionData(DataRefImpl DRI,
608 bool &Result) const {
609 // FIXME: Unimplemented.
610 Result = false;
611 return object_error::success;
612}
613
614error_code MachOObjectFile::isSectionBSS(DataRefImpl DRI,
615 bool &Result) const {
616 // FIXME: Unimplemented.
617 Result = false;
618 return object_error::success;
619}
620
Preston Gurdc68dda82012-04-12 20:13:57 +0000621error_code MachOObjectFile::isSectionRequiredForExecution(DataRefImpl Sec,
622 bool &Result) const {
Andrew Kaylor30b20eb2012-10-10 01:45:52 +0000623 // FIXME: Unimplemented.
Preston Gurdc68dda82012-04-12 20:13:57 +0000624 Result = true;
625 return object_error::success;
626}
627
628error_code MachOObjectFile::isSectionVirtual(DataRefImpl Sec,
Andrew Kaylor30b20eb2012-10-10 01:45:52 +0000629 bool &Result) const {
630 // FIXME: Unimplemented.
Preston Gurdc68dda82012-04-12 20:13:57 +0000631 Result = false;
632 return object_error::success;
633}
634
635error_code MachOObjectFile::isSectionZeroInit(DataRefImpl DRI,
636 bool &Result) const {
637 if (MachOObj->is64Bit()) {
638 InMemoryStruct<macho::Section64> Sect;
639 getSection64(DRI, Sect);
Eli Friedman41827f92012-05-02 02:31:28 +0000640 unsigned SectionType = Sect->Flags & MachO::SectionFlagMaskSectionType;
641 Result = (SectionType == MachO::SectionTypeZeroFill ||
642 SectionType == MachO::SectionTypeZeroFillLarge);
Preston Gurdc68dda82012-04-12 20:13:57 +0000643 } else {
644 InMemoryStruct<macho::Section> Sect;
645 getSection(DRI, Sect);
Eli Friedman41827f92012-05-02 02:31:28 +0000646 unsigned SectionType = Sect->Flags & MachO::SectionFlagMaskSectionType;
647 Result = (SectionType == MachO::SectionTypeZeroFill ||
648 SectionType == MachO::SectionTypeZeroFillLarge);
Preston Gurdc68dda82012-04-12 20:13:57 +0000649 }
650
651 return object_error::success;
652}
653
Andrew Kaylor3a129c82012-10-10 01:41:33 +0000654error_code MachOObjectFile::isSectionReadOnlyData(DataRefImpl Sec,
655 bool &Result) const {
656 // Consider using the code from isSectionText to look for __const sections.
657 // Alternately, emit S_ATTR_PURE_INSTRUCTIONS and/or S_ATTR_SOME_INSTRUCTIONS
658 // to use section attributes to distinguish code from data.
659
660 // FIXME: Unimplemented.
661 Result = false;
662 return object_error::success;
663}
664
Benjamin Kramer07ea23a2011-07-15 18:39:21 +0000665error_code MachOObjectFile::sectionContainsSymbol(DataRefImpl Sec,
666 DataRefImpl Symb,
667 bool &Result) const {
Michael J. Spencer1130a792011-10-17 20:19:29 +0000668 SymbolRef::Type ST;
Owen Andersoncd749882011-10-12 22:21:32 +0000669 getSymbolType(Symb, ST);
David Meyer2c677272012-02-29 02:11:55 +0000670 if (ST == SymbolRef::ST_Unknown) {
Owen Andersoncd749882011-10-12 22:21:32 +0000671 Result = false;
672 return object_error::success;
673 }
674
675 uint64_t SectBegin, SectEnd;
676 getSectionAddress(Sec, SectBegin);
677 getSectionSize(Sec, SectEnd);
678 SectEnd += SectBegin;
679
Benjamin Kramer07ea23a2011-07-15 18:39:21 +0000680 if (MachOObj->is64Bit()) {
681 InMemoryStruct<macho::Symbol64TableEntry> Entry;
682 getSymbol64TableEntry(Symb, Entry);
Owen Andersoncd749882011-10-12 22:21:32 +0000683 uint64_t SymAddr= Entry->Value;
684 Result = (SymAddr >= SectBegin) && (SymAddr < SectEnd);
Benjamin Kramer07ea23a2011-07-15 18:39:21 +0000685 } else {
686 InMemoryStruct<macho::SymbolTableEntry> Entry;
687 getSymbolTableEntry(Symb, Entry);
Owen Andersoncd749882011-10-12 22:21:32 +0000688 uint64_t SymAddr= Entry->Value;
689 Result = (SymAddr >= SectBegin) && (SymAddr < SectEnd);
Benjamin Kramer07ea23a2011-07-15 18:39:21 +0000690 }
Owen Andersoncd749882011-10-12 22:21:32 +0000691
Benjamin Kramer07ea23a2011-07-15 18:39:21 +0000692 return object_error::success;
693}
694
Michael J. Spencer4344b1e2011-10-07 19:25:32 +0000695relocation_iterator MachOObjectFile::getSectionRelBegin(DataRefImpl Sec) const {
696 DataRefImpl ret;
Michael J. Spencer4344b1e2011-10-07 19:25:32 +0000697 ret.d.b = getSectionIndex(Sec);
698 return relocation_iterator(RelocationRef(ret, this));
699}
700relocation_iterator MachOObjectFile::getSectionRelEnd(DataRefImpl Sec) const {
701 uint32_t last_reloc;
Jim Grosbach596e4742012-11-29 19:14:11 +0000702 if (is64BitLoadCommand(MachOObj.get(), Sec)) {
Michael J. Spencer4344b1e2011-10-07 19:25:32 +0000703 InMemoryStruct<macho::Section64> Sect;
704 getSection64(Sec, Sect);
705 last_reloc = Sect->NumRelocationTableEntries;
706 } else {
707 InMemoryStruct<macho::Section> Sect;
708 getSection(Sec, Sect);
709 last_reloc = Sect->NumRelocationTableEntries;
710 }
711 DataRefImpl ret;
712 ret.d.a = last_reloc;
713 ret.d.b = getSectionIndex(Sec);
714 return relocation_iterator(RelocationRef(ret, this));
715}
716
717section_iterator MachOObjectFile::begin_sections() const {
Eric Christopher6256b032011-04-22 03:19:48 +0000718 DataRefImpl DRI;
Eric Christopher6256b032011-04-22 03:19:48 +0000719 moveToNextSection(DRI);
720 return section_iterator(SectionRef(DRI, this));
721}
722
Michael J. Spencer4344b1e2011-10-07 19:25:32 +0000723section_iterator MachOObjectFile::end_sections() const {
Eric Christopher6256b032011-04-22 03:19:48 +0000724 DataRefImpl DRI;
725 DRI.d.a = MachOObj->getHeader().NumLoadCommands;
Eric Christopher6256b032011-04-22 03:19:48 +0000726 return section_iterator(SectionRef(DRI, this));
727}
728
Benjamin Kramer0fcab072011-09-08 20:52:17 +0000729/*===-- Relocations -------------------------------------------------------===*/
730
731void MachOObjectFile::
732getRelocation(DataRefImpl Rel,
733 InMemoryStruct<macho::RelocationEntry> &Res) const {
734 uint32_t relOffset;
735 if (MachOObj->is64Bit()) {
736 InMemoryStruct<macho::Section64> Sect;
737 getSection64(Sections[Rel.d.b], Sect);
738 relOffset = Sect->RelocationTableOffset;
739 } else {
740 InMemoryStruct<macho::Section> Sect;
741 getSection(Sections[Rel.d.b], Sect);
742 relOffset = Sect->RelocationTableOffset;
743 }
744 MachOObj->ReadRelocationEntry(relOffset, Rel.d.a, Res);
745}
746error_code MachOObjectFile::getRelocationNext(DataRefImpl Rel,
747 RelocationRef &Res) const {
748 ++Rel.d.a;
Benjamin Kramer0fcab072011-09-08 20:52:17 +0000749 Res = RelocationRef(Rel, this);
750 return object_error::success;
751}
752error_code MachOObjectFile::getRelocationAddress(DataRefImpl Rel,
753 uint64_t &Res) const {
Owen Anderson0135fe12011-10-24 21:44:00 +0000754 const uint8_t* sectAddress = 0;
Benjamin Kramer0fcab072011-09-08 20:52:17 +0000755 if (MachOObj->is64Bit()) {
756 InMemoryStruct<macho::Section64> Sect;
757 getSection64(Sections[Rel.d.b], Sect);
Owen Anderson0135fe12011-10-24 21:44:00 +0000758 sectAddress += Sect->Address;
Benjamin Kramer0fcab072011-09-08 20:52:17 +0000759 } else {
760 InMemoryStruct<macho::Section> Sect;
761 getSection(Sections[Rel.d.b], Sect);
Owen Anderson0135fe12011-10-24 21:44:00 +0000762 sectAddress += Sect->Address;
Benjamin Kramer0fcab072011-09-08 20:52:17 +0000763 }
764 InMemoryStruct<macho::RelocationEntry> RE;
765 getRelocation(Rel, RE);
Owen Anderson1832f4d2011-10-26 20:42:54 +0000766
767 unsigned Arch = getArch();
768 bool isScattered = (Arch != Triple::x86_64) &&
769 (RE->Word0 & macho::RF_Scattered);
770 uint64_t RelAddr = 0;
Danil Malyshevb0436a72011-11-29 17:40:10 +0000771 if (isScattered)
Owen Anderson1832f4d2011-10-26 20:42:54 +0000772 RelAddr = RE->Word0 & 0xFFFFFF;
773 else
774 RelAddr = RE->Word0;
775
776 Res = reinterpret_cast<uintptr_t>(sectAddress + RelAddr);
Benjamin Kramer0fcab072011-09-08 20:52:17 +0000777 return object_error::success;
778}
Danil Malyshevb0436a72011-11-29 17:40:10 +0000779error_code MachOObjectFile::getRelocationOffset(DataRefImpl Rel,
780 uint64_t &Res) const {
781 InMemoryStruct<macho::RelocationEntry> RE;
782 getRelocation(Rel, RE);
783
784 unsigned Arch = getArch();
785 bool isScattered = (Arch != Triple::x86_64) &&
786 (RE->Word0 & macho::RF_Scattered);
787 if (isScattered)
788 Res = RE->Word0 & 0xFFFFFF;
789 else
790 Res = RE->Word0;
791 return object_error::success;
792}
Benjamin Kramer0fcab072011-09-08 20:52:17 +0000793error_code MachOObjectFile::getRelocationSymbol(DataRefImpl Rel,
794 SymbolRef &Res) const {
795 InMemoryStruct<macho::RelocationEntry> RE;
796 getRelocation(Rel, RE);
797 uint32_t SymbolIdx = RE->Word1 & 0xffffff;
798 bool isExtern = (RE->Word1 >> 27) & 1;
799
800 DataRefImpl Sym;
Benjamin Kramer0fcab072011-09-08 20:52:17 +0000801 moveToNextSymbol(Sym);
Benjamin Kramer0fcab072011-09-08 20:52:17 +0000802 if (isExtern) {
803 for (unsigned i = 0; i < SymbolIdx; i++) {
804 Sym.d.b++;
805 moveToNextSymbol(Sym);
Nick Lewycky58856ea2011-09-09 00:16:50 +0000806 assert(Sym.d.a < MachOObj->getHeader().NumLoadCommands &&
Benjamin Kramer0fcab072011-09-08 20:52:17 +0000807 "Relocation symbol index out of range!");
808 }
809 }
810 Res = SymbolRef(Sym, this);
811 return object_error::success;
812}
813error_code MachOObjectFile::getRelocationType(DataRefImpl Rel,
Owen Anderson9472b8d2011-10-26 17:08:49 +0000814 uint64_t &Res) const {
Benjamin Kramer0fcab072011-09-08 20:52:17 +0000815 InMemoryStruct<macho::RelocationEntry> RE;
816 getRelocation(Rel, RE);
Owen Andersonf8261e72011-10-26 17:10:22 +0000817 Res = RE->Word0;
818 Res <<= 32;
819 Res |= RE->Word1;
Benjamin Kramer0fcab072011-09-08 20:52:17 +0000820 return object_error::success;
821}
Michael J. Spencer4344b1e2011-10-07 19:25:32 +0000822error_code MachOObjectFile::getRelocationTypeName(DataRefImpl Rel,
823 SmallVectorImpl<char> &Result) const {
Owen Anderson0135fe12011-10-24 21:44:00 +0000824 // TODO: Support scattered relocations.
825 StringRef res;
826 InMemoryStruct<macho::RelocationEntry> RE;
827 getRelocation(Rel, RE);
Owen Anderson0135fe12011-10-24 21:44:00 +0000828
829 unsigned Arch = getArch();
Owen Anderson1832f4d2011-10-26 20:42:54 +0000830 bool isScattered = (Arch != Triple::x86_64) &&
831 (RE->Word0 & macho::RF_Scattered);
832
833 unsigned r_type;
834 if (isScattered)
835 r_type = (RE->Word0 >> 24) & 0xF;
836 else
837 r_type = (RE->Word1 >> 28) & 0xF;
838
Owen Anderson0135fe12011-10-24 21:44:00 +0000839 switch (Arch) {
840 case Triple::x86: {
Craig Toppere3298102012-05-24 06:35:32 +0000841 static const char *const Table[] = {
Owen Anderson0135fe12011-10-24 21:44:00 +0000842 "GENERIC_RELOC_VANILLA",
843 "GENERIC_RELOC_PAIR",
844 "GENERIC_RELOC_SECTDIFF",
Owen Andersoneb6bd332011-10-27 20:46:09 +0000845 "GENERIC_RELOC_PB_LA_PTR",
Owen Anderson0135fe12011-10-24 21:44:00 +0000846 "GENERIC_RELOC_LOCAL_SECTDIFF",
Owen Andersoneb6bd332011-10-27 20:46:09 +0000847 "GENERIC_RELOC_TLV" };
Owen Anderson0135fe12011-10-24 21:44:00 +0000848
Owen Andersoneb6bd332011-10-27 20:46:09 +0000849 if (r_type > 6)
Owen Anderson0135fe12011-10-24 21:44:00 +0000850 res = "Unknown";
851 else
852 res = Table[r_type];
853 break;
854 }
855 case Triple::x86_64: {
Craig Toppere3298102012-05-24 06:35:32 +0000856 static const char *const Table[] = {
Owen Andersond8fa76d2011-10-24 23:20:07 +0000857 "X86_64_RELOC_UNSIGNED",
858 "X86_64_RELOC_SIGNED",
Owen Anderson0135fe12011-10-24 21:44:00 +0000859 "X86_64_RELOC_BRANCH",
860 "X86_64_RELOC_GOT_LOAD",
861 "X86_64_RELOC_GOT",
Owen Andersond8fa76d2011-10-24 23:20:07 +0000862 "X86_64_RELOC_SUBTRACTOR",
863 "X86_64_RELOC_SIGNED_1",
864 "X86_64_RELOC_SIGNED_2",
865 "X86_64_RELOC_SIGNED_4",
866 "X86_64_RELOC_TLV" };
Owen Anderson0135fe12011-10-24 21:44:00 +0000867
Owen Andersond8fa76d2011-10-24 23:20:07 +0000868 if (r_type > 9)
Owen Anderson0135fe12011-10-24 21:44:00 +0000869 res = "Unknown";
870 else
871 res = Table[r_type];
872 break;
873 }
874 case Triple::arm: {
Craig Toppere3298102012-05-24 06:35:32 +0000875 static const char *const Table[] = {
Owen Anderson0135fe12011-10-24 21:44:00 +0000876 "ARM_RELOC_VANILLA",
877 "ARM_RELOC_PAIR",
878 "ARM_RELOC_SECTDIFF",
879 "ARM_RELOC_LOCAL_SECTDIFF",
880 "ARM_RELOC_PB_LA_PTR",
881 "ARM_RELOC_BR24",
882 "ARM_THUMB_RELOC_BR22",
883 "ARM_THUMB_32BIT_BRANCH",
884 "ARM_RELOC_HALF",
885 "ARM_RELOC_HALF_SECTDIFF" };
886
887 if (r_type > 9)
888 res = "Unknown";
889 else
890 res = Table[r_type];
891 break;
892 }
893 case Triple::ppc: {
Craig Toppere3298102012-05-24 06:35:32 +0000894 static const char *const Table[] = {
Owen Anderson0135fe12011-10-24 21:44:00 +0000895 "PPC_RELOC_VANILLA",
896 "PPC_RELOC_PAIR",
897 "PPC_RELOC_BR14",
898 "PPC_RELOC_BR24",
899 "PPC_RELOC_HI16",
900 "PPC_RELOC_LO16",
901 "PPC_RELOC_HA16",
902 "PPC_RELOC_LO14",
903 "PPC_RELOC_SECTDIFF",
904 "PPC_RELOC_PB_LA_PTR",
905 "PPC_RELOC_HI16_SECTDIFF",
906 "PPC_RELOC_LO16_SECTDIFF",
907 "PPC_RELOC_HA16_SECTDIFF",
908 "PPC_RELOC_JBSR",
909 "PPC_RELOC_LO14_SECTDIFF",
910 "PPC_RELOC_LOCAL_SECTDIFF" };
911
912 res = Table[r_type];
913 break;
914 }
915 case Triple::UnknownArch:
916 res = "Unknown";
917 break;
918 }
Owen Anderson5f4e02c2011-10-24 20:19:18 +0000919 Result.append(res.begin(), res.end());
Michael J. Spencer4344b1e2011-10-07 19:25:32 +0000920 return object_error::success;
921}
Benjamin Kramer0fcab072011-09-08 20:52:17 +0000922error_code MachOObjectFile::getRelocationAdditionalInfo(DataRefImpl Rel,
923 int64_t &Res) const {
924 InMemoryStruct<macho::RelocationEntry> RE;
925 getRelocation(Rel, RE);
926 bool isExtern = (RE->Word1 >> 27) & 1;
927 Res = 0;
928 if (!isExtern) {
929 const uint8_t* sectAddress = base();
930 if (MachOObj->is64Bit()) {
931 InMemoryStruct<macho::Section64> Sect;
932 getSection64(Sections[Rel.d.b], Sect);
933 sectAddress += Sect->Offset;
934 } else {
935 InMemoryStruct<macho::Section> Sect;
936 getSection(Sections[Rel.d.b], Sect);
937 sectAddress += Sect->Offset;
938 }
939 Res = reinterpret_cast<uintptr_t>(sectAddress);
940 }
941 return object_error::success;
942}
Owen Andersond8fa76d2011-10-24 23:20:07 +0000943
944// Helper to advance a section or symbol iterator multiple increments at a time.
945template<class T>
946error_code advance(T &it, size_t Val) {
947 error_code ec;
948 while (Val--) {
949 it.increment(ec);
950 }
951 return ec;
952}
953
954template<class T>
955void advanceTo(T &it, size_t Val) {
956 if (error_code ec = advance(it, Val))
957 report_fatal_error(ec.message());
958}
959
Owen Anderson1832f4d2011-10-26 20:42:54 +0000960void MachOObjectFile::printRelocationTargetName(
961 InMemoryStruct<macho::RelocationEntry>& RE,
962 raw_string_ostream &fmt) const {
963 unsigned Arch = getArch();
964 bool isScattered = (Arch != Triple::x86_64) &&
965 (RE->Word0 & macho::RF_Scattered);
966
967 // Target of a scattered relocation is an address. In the interest of
968 // generating pretty output, scan through the symbol table looking for a
969 // symbol that aligns with that address. If we find one, print it.
970 // Otherwise, we just print the hex address of the target.
971 if (isScattered) {
972 uint32_t Val = RE->Word1;
973
974 error_code ec;
975 for (symbol_iterator SI = begin_symbols(), SE = end_symbols(); SI != SE;
976 SI.increment(ec)) {
977 if (ec) report_fatal_error(ec.message());
978
979 uint64_t Addr;
980 StringRef Name;
981
982 if ((ec = SI->getAddress(Addr)))
983 report_fatal_error(ec.message());
984 if (Addr != Val) continue;
985 if ((ec = SI->getName(Name)))
986 report_fatal_error(ec.message());
987 fmt << Name;
988 return;
989 }
990
Owen Andersonb28bdbf2011-10-27 21:53:50 +0000991 // If we couldn't find a symbol that this relocation refers to, try
992 // to find a section beginning instead.
993 for (section_iterator SI = begin_sections(), SE = end_sections(); SI != SE;
994 SI.increment(ec)) {
995 if (ec) report_fatal_error(ec.message());
996
997 uint64_t Addr;
998 StringRef Name;
999
1000 if ((ec = SI->getAddress(Addr)))
1001 report_fatal_error(ec.message());
1002 if (Addr != Val) continue;
1003 if ((ec = SI->getName(Name)))
1004 report_fatal_error(ec.message());
1005 fmt << Name;
1006 return;
1007 }
1008
Owen Anderson1832f4d2011-10-26 20:42:54 +00001009 fmt << format("0x%x", Val);
1010 return;
1011 }
1012
1013 StringRef S;
1014 bool isExtern = (RE->Word1 >> 27) & 1;
1015 uint32_t Val = RE->Word1 & 0xFFFFFF;
Owen Andersond8fa76d2011-10-24 23:20:07 +00001016
1017 if (isExtern) {
1018 symbol_iterator SI = begin_symbols();
1019 advanceTo(SI, Val);
Owen Anderson1832f4d2011-10-26 20:42:54 +00001020 SI->getName(S);
Owen Andersond8fa76d2011-10-24 23:20:07 +00001021 } else {
1022 section_iterator SI = begin_sections();
1023 advanceTo(SI, Val);
Owen Anderson1832f4d2011-10-26 20:42:54 +00001024 SI->getName(S);
Owen Andersond8fa76d2011-10-24 23:20:07 +00001025 }
1026
Owen Anderson1832f4d2011-10-26 20:42:54 +00001027 fmt << S;
Owen Andersond8fa76d2011-10-24 23:20:07 +00001028}
1029
Michael J. Spencer4344b1e2011-10-07 19:25:32 +00001030error_code MachOObjectFile::getRelocationValueString(DataRefImpl Rel,
1031 SmallVectorImpl<char> &Result) const {
Owen Anderson0135fe12011-10-24 21:44:00 +00001032 InMemoryStruct<macho::RelocationEntry> RE;
1033 getRelocation(Rel, RE);
1034
Owen Anderson1832f4d2011-10-26 20:42:54 +00001035 unsigned Arch = getArch();
1036 bool isScattered = (Arch != Triple::x86_64) &&
1037 (RE->Word0 & macho::RF_Scattered);
Owen Andersond8fa76d2011-10-24 23:20:07 +00001038
Owen Anderson013d7562011-10-25 18:48:41 +00001039 std::string fmtbuf;
1040 raw_string_ostream fmt(fmtbuf);
1041
Owen Anderson1832f4d2011-10-26 20:42:54 +00001042 unsigned Type;
1043 if (isScattered)
1044 Type = (RE->Word0 >> 24) & 0xF;
1045 else
1046 Type = (RE->Word1 >> 28) & 0xF;
1047
Owen Andersoneb6bd332011-10-27 20:46:09 +00001048 bool isPCRel;
1049 if (isScattered)
1050 isPCRel = ((RE->Word0 >> 30) & 1);
1051 else
1052 isPCRel = ((RE->Word1 >> 24) & 1);
1053
Owen Andersond8fa76d2011-10-24 23:20:07 +00001054 // Determine any addends that should be displayed with the relocation.
1055 // These require decoding the relocation type, which is triple-specific.
Owen Andersond8fa76d2011-10-24 23:20:07 +00001056
1057 // X86_64 has entirely custom relocation types.
1058 if (Arch == Triple::x86_64) {
Owen Anderson929e27c2011-10-26 17:05:20 +00001059 bool isPCRel = ((RE->Word1 >> 24) & 1);
Owen Anderson013d7562011-10-25 18:48:41 +00001060
Owen Andersond8fa76d2011-10-24 23:20:07 +00001061 switch (Type) {
Owen Anderson1832f4d2011-10-26 20:42:54 +00001062 case macho::RIT_X86_64_GOTLoad: // X86_64_RELOC_GOT_LOAD
1063 case macho::RIT_X86_64_GOT: { // X86_64_RELOC_GOT
1064 printRelocationTargetName(RE, fmt);
1065 fmt << "@GOT";
Owen Anderson929e27c2011-10-26 17:05:20 +00001066 if (isPCRel) fmt << "PCREL";
1067 break;
1068 }
Owen Anderson1832f4d2011-10-26 20:42:54 +00001069 case macho::RIT_X86_64_Subtractor: { // X86_64_RELOC_SUBTRACTOR
Owen Anderson013d7562011-10-25 18:48:41 +00001070 InMemoryStruct<macho::RelocationEntry> RENext;
1071 DataRefImpl RelNext = Rel;
1072 RelNext.d.a++;
1073 getRelocation(RelNext, RENext);
Owen Andersond8fa76d2011-10-24 23:20:07 +00001074
1075 // X86_64_SUBTRACTOR must be followed by a relocation of type
1076 // X86_64_RELOC_UNSIGNED.
Owen Anderson1832f4d2011-10-26 20:42:54 +00001077 // NOTE: Scattered relocations don't exist on x86_64.
Owen Anderson013d7562011-10-25 18:48:41 +00001078 unsigned RType = (RENext->Word1 >> 28) & 0xF;
Owen Andersond8fa76d2011-10-24 23:20:07 +00001079 if (RType != 0)
1080 report_fatal_error("Expected X86_64_RELOC_UNSIGNED after "
1081 "X86_64_RELOC_SUBTRACTOR.");
1082
Owen Andersonef22f782011-10-26 17:28:49 +00001083 // The X86_64_RELOC_UNSIGNED contains the minuend symbol,
1084 // X86_64_SUBTRACTOR contains to the subtrahend.
Owen Anderson1832f4d2011-10-26 20:42:54 +00001085 printRelocationTargetName(RENext, fmt);
1086 fmt << "-";
1087 printRelocationTargetName(RE, fmt);
Jim Grosbach133f6b82013-01-31 19:46:57 +00001088 break;
Owen Andersond8fa76d2011-10-24 23:20:07 +00001089 }
Owen Andersoneb6bd332011-10-27 20:46:09 +00001090 case macho::RIT_X86_64_TLV:
1091 printRelocationTargetName(RE, fmt);
1092 fmt << "@TLV";
1093 if (isPCRel) fmt << "P";
1094 break;
Owen Anderson1832f4d2011-10-26 20:42:54 +00001095 case macho::RIT_X86_64_Signed1: // X86_64_RELOC_SIGNED1
1096 printRelocationTargetName(RE, fmt);
1097 fmt << "-1";
Owen Andersond8fa76d2011-10-24 23:20:07 +00001098 break;
Owen Anderson1832f4d2011-10-26 20:42:54 +00001099 case macho::RIT_X86_64_Signed2: // X86_64_RELOC_SIGNED2
1100 printRelocationTargetName(RE, fmt);
1101 fmt << "-2";
Owen Andersond8fa76d2011-10-24 23:20:07 +00001102 break;
Owen Anderson1832f4d2011-10-26 20:42:54 +00001103 case macho::RIT_X86_64_Signed4: // X86_64_RELOC_SIGNED4
1104 printRelocationTargetName(RE, fmt);
1105 fmt << "-4";
Owen Anderson013d7562011-10-25 18:48:41 +00001106 break;
1107 default:
Owen Anderson1832f4d2011-10-26 20:42:54 +00001108 printRelocationTargetName(RE, fmt);
Owen Andersond8fa76d2011-10-24 23:20:07 +00001109 break;
1110 }
Owen Andersond8fa76d2011-10-24 23:20:07 +00001111 // X86 and ARM share some relocation types in common.
Owen Anderson013d7562011-10-25 18:48:41 +00001112 } else if (Arch == Triple::x86 || Arch == Triple::arm) {
1113 // Generic relocation types...
Owen Andersond8fa76d2011-10-24 23:20:07 +00001114 switch (Type) {
Owen Anderson1832f4d2011-10-26 20:42:54 +00001115 case macho::RIT_Pair: // GENERIC_RELOC_PAIR - prints no info
Owen Andersond8fa76d2011-10-24 23:20:07 +00001116 return object_error::success;
Owen Andersoneb6bd332011-10-27 20:46:09 +00001117 case macho::RIT_Difference: { // GENERIC_RELOC_SECTDIFF
Owen Anderson013d7562011-10-25 18:48:41 +00001118 InMemoryStruct<macho::RelocationEntry> RENext;
1119 DataRefImpl RelNext = Rel;
1120 RelNext.d.a++;
1121 getRelocation(RelNext, RENext);
Owen Andersond8fa76d2011-10-24 23:20:07 +00001122
1123 // X86 sect diff's must be followed by a relocation of type
1124 // GENERIC_RELOC_PAIR.
Owen Anderson1832f4d2011-10-26 20:42:54 +00001125 bool isNextScattered = (Arch != Triple::x86_64) &&
1126 (RENext->Word0 & macho::RF_Scattered);
1127 unsigned RType;
1128 if (isNextScattered)
1129 RType = (RENext->Word0 >> 24) & 0xF;
1130 else
1131 RType = (RENext->Word1 >> 28) & 0xF;
Owen Andersond8fa76d2011-10-24 23:20:07 +00001132 if (RType != 1)
1133 report_fatal_error("Expected GENERIC_RELOC_PAIR after "
Owen Andersoneb6bd332011-10-27 20:46:09 +00001134 "GENERIC_RELOC_SECTDIFF.");
Owen Andersond8fa76d2011-10-24 23:20:07 +00001135
Owen Anderson1832f4d2011-10-26 20:42:54 +00001136 printRelocationTargetName(RE, fmt);
1137 fmt << "-";
1138 printRelocationTargetName(RENext, fmt);
Owen Anderson013d7562011-10-25 18:48:41 +00001139 break;
Owen Andersond8fa76d2011-10-24 23:20:07 +00001140 }
1141 }
Owen Anderson013d7562011-10-25 18:48:41 +00001142
Owen Anderson1832f4d2011-10-26 20:42:54 +00001143 if (Arch == Triple::x86) {
Owen Anderson013d7562011-10-25 18:48:41 +00001144 // All X86 relocations that need special printing were already
1145 // handled in the generic code.
Owen Andersoneb6bd332011-10-27 20:46:09 +00001146 switch (Type) {
1147 case macho::RIT_Generic_LocalDifference:{// GENERIC_RELOC_LOCAL_SECTDIFF
1148 InMemoryStruct<macho::RelocationEntry> RENext;
1149 DataRefImpl RelNext = Rel;
1150 RelNext.d.a++;
1151 getRelocation(RelNext, RENext);
1152
1153 // X86 sect diff's must be followed by a relocation of type
1154 // GENERIC_RELOC_PAIR.
1155 bool isNextScattered = (Arch != Triple::x86_64) &&
1156 (RENext->Word0 & macho::RF_Scattered);
1157 unsigned RType;
1158 if (isNextScattered)
1159 RType = (RENext->Word0 >> 24) & 0xF;
1160 else
1161 RType = (RENext->Word1 >> 28) & 0xF;
1162 if (RType != 1)
1163 report_fatal_error("Expected GENERIC_RELOC_PAIR after "
1164 "GENERIC_RELOC_LOCAL_SECTDIFF.");
1165
1166 printRelocationTargetName(RE, fmt);
1167 fmt << "-";
1168 printRelocationTargetName(RENext, fmt);
1169 break;
1170 }
1171 case macho::RIT_Generic_TLV: {
1172 printRelocationTargetName(RE, fmt);
1173 fmt << "@TLV";
1174 if (isPCRel) fmt << "P";
1175 break;
1176 }
1177 default:
1178 printRelocationTargetName(RE, fmt);
1179 }
Owen Anderson013d7562011-10-25 18:48:41 +00001180 } else { // ARM-specific relocations
1181 switch (Type) {
Owen Anderson1832f4d2011-10-26 20:42:54 +00001182 case macho::RIT_ARM_Half: // ARM_RELOC_HALF
1183 case macho::RIT_ARM_HalfDifference: { // ARM_RELOC_HALF_SECTDIFF
Owen Anderson013d7562011-10-25 18:48:41 +00001184 // Half relocations steal a bit from the length field to encode
1185 // whether this is an upper16 or a lower16 relocation.
Owen Anderson1832f4d2011-10-26 20:42:54 +00001186 bool isUpper;
1187 if (isScattered)
1188 isUpper = (RE->Word0 >> 28) & 1;
Owen Anderson013d7562011-10-25 18:48:41 +00001189 else
Owen Anderson1832f4d2011-10-26 20:42:54 +00001190 isUpper = (RE->Word1 >> 25) & 1;
1191
1192 if (isUpper)
1193 fmt << ":upper16:(";
1194 else
1195 fmt << ":lower16:(";
1196 printRelocationTargetName(RE, fmt);
Owen Anderson013d7562011-10-25 18:48:41 +00001197
1198 InMemoryStruct<macho::RelocationEntry> RENext;
1199 DataRefImpl RelNext = Rel;
1200 RelNext.d.a++;
1201 getRelocation(RelNext, RENext);
1202
1203 // ARM half relocs must be followed by a relocation of type
1204 // ARM_RELOC_PAIR.
Owen Anderson1832f4d2011-10-26 20:42:54 +00001205 bool isNextScattered = (Arch != Triple::x86_64) &&
1206 (RENext->Word0 & macho::RF_Scattered);
1207 unsigned RType;
1208 if (isNextScattered)
1209 RType = (RENext->Word0 >> 24) & 0xF;
1210 else
1211 RType = (RENext->Word1 >> 28) & 0xF;
1212
Owen Anderson013d7562011-10-25 18:48:41 +00001213 if (RType != 1)
1214 report_fatal_error("Expected ARM_RELOC_PAIR after "
1215 "GENERIC_RELOC_HALF");
1216
Owen Anderson1832f4d2011-10-26 20:42:54 +00001217 // NOTE: The half of the target virtual address is stashed in the
1218 // address field of the secondary relocation, but we can't reverse
1219 // engineer the constant offset from it without decoding the movw/movt
1220 // instruction to find the other half in its immediate field.
Owen Anderson013d7562011-10-25 18:48:41 +00001221
1222 // ARM_RELOC_HALF_SECTDIFF encodes the second section in the
1223 // symbol/section pointer of the follow-on relocation.
Owen Anderson1832f4d2011-10-26 20:42:54 +00001224 if (Type == macho::RIT_ARM_HalfDifference) {
1225 fmt << "-";
1226 printRelocationTargetName(RENext, fmt);
Owen Anderson013d7562011-10-25 18:48:41 +00001227 }
1228
Owen Anderson013d7562011-10-25 18:48:41 +00001229 fmt << ")";
1230 break;
1231 }
1232 default: {
Owen Anderson1832f4d2011-10-26 20:42:54 +00001233 printRelocationTargetName(RE, fmt);
Owen Anderson013d7562011-10-25 18:48:41 +00001234 }
1235 }
1236 }
Owen Anderson1832f4d2011-10-26 20:42:54 +00001237 } else
1238 printRelocationTargetName(RE, fmt);
Owen Andersond8fa76d2011-10-24 23:20:07 +00001239
Owen Anderson0135fe12011-10-24 21:44:00 +00001240 fmt.flush();
1241 Result.append(fmtbuf.begin(), fmtbuf.end());
Michael J. Spencer4344b1e2011-10-07 19:25:32 +00001242 return object_error::success;
Benjamin Kramer0fcab072011-09-08 20:52:17 +00001243}
1244
Owen Anderson0685e942011-10-25 20:35:53 +00001245error_code MachOObjectFile::getRelocationHidden(DataRefImpl Rel,
1246 bool &Result) const {
1247 InMemoryStruct<macho::RelocationEntry> RE;
1248 getRelocation(Rel, RE);
1249
Owen Anderson0685e942011-10-25 20:35:53 +00001250 unsigned Arch = getArch();
Owen Anderson1832f4d2011-10-26 20:42:54 +00001251 bool isScattered = (Arch != Triple::x86_64) &&
1252 (RE->Word0 & macho::RF_Scattered);
1253 unsigned Type;
1254 if (isScattered)
1255 Type = (RE->Word0 >> 24) & 0xF;
1256 else
1257 Type = (RE->Word1 >> 28) & 0xF;
Owen Anderson0685e942011-10-25 20:35:53 +00001258
1259 Result = false;
1260
1261 // On arches that use the generic relocations, GENERIC_RELOC_PAIR
1262 // is always hidden.
1263 if (Arch == Triple::x86 || Arch == Triple::arm) {
Owen Anderson1832f4d2011-10-26 20:42:54 +00001264 if (Type == macho::RIT_Pair) Result = true;
Owen Anderson0685e942011-10-25 20:35:53 +00001265 } else if (Arch == Triple::x86_64) {
1266 // On x86_64, X86_64_RELOC_UNSIGNED is hidden only when it follows
1267 // an X864_64_RELOC_SUBTRACTOR.
Owen Anderson1832f4d2011-10-26 20:42:54 +00001268 if (Type == macho::RIT_X86_64_Unsigned && Rel.d.a > 0) {
Owen Anderson0685e942011-10-25 20:35:53 +00001269 DataRefImpl RelPrev = Rel;
1270 RelPrev.d.a--;
1271 InMemoryStruct<macho::RelocationEntry> REPrev;
1272 getRelocation(RelPrev, REPrev);
1273
1274 unsigned PrevType = (REPrev->Word1 >> 28) & 0xF;
1275
Owen Anderson1832f4d2011-10-26 20:42:54 +00001276 if (PrevType == macho::RIT_X86_64_Subtractor) Result = true;
Owen Anderson0685e942011-10-25 20:35:53 +00001277 }
1278 }
1279
1280 return object_error::success;
1281}
1282
David Meyer5c2b4ea2012-03-01 01:36:50 +00001283error_code MachOObjectFile::getLibraryNext(DataRefImpl LibData,
1284 LibraryRef &Res) const {
1285 report_fatal_error("Needed libraries unimplemented in MachOObjectFile");
1286}
1287
1288error_code MachOObjectFile::getLibraryPath(DataRefImpl LibData,
1289 StringRef &Res) const {
1290 report_fatal_error("Needed libraries unimplemented in MachOObjectFile");
1291}
1292
1293
Eric Christopher6256b032011-04-22 03:19:48 +00001294/*===-- Miscellaneous -----------------------------------------------------===*/
1295
1296uint8_t MachOObjectFile::getBytesInAddress() const {
1297 return MachOObj->is64Bit() ? 8 : 4;
1298}
1299
1300StringRef MachOObjectFile::getFileFormatName() const {
1301 if (!MachOObj->is64Bit()) {
1302 switch (MachOObj->getHeader().CPUType) {
Eric Christopherf4b2f932011-04-22 06:34:01 +00001303 case llvm::MachO::CPUTypeI386:
Eric Christopher9ab1d7f2011-04-22 04:08:58 +00001304 return "Mach-O 32-bit i386";
Eric Christopherf4b2f932011-04-22 06:34:01 +00001305 case llvm::MachO::CPUTypeARM:
Eric Christopher9ab1d7f2011-04-22 04:08:58 +00001306 return "Mach-O arm";
Eric Christopherf4b2f932011-04-22 06:34:01 +00001307 case llvm::MachO::CPUTypePowerPC:
Eric Christopher9ab1d7f2011-04-22 04:08:58 +00001308 return "Mach-O 32-bit ppc";
1309 default:
Eric Christopherf4b2f932011-04-22 06:34:01 +00001310 assert((MachOObj->getHeader().CPUType & llvm::MachO::CPUArchABI64) == 0 &&
Eric Christopher9ab1d7f2011-04-22 04:08:58 +00001311 "64-bit object file when we're not 64-bit?");
1312 return "Mach-O 32-bit unknown";
Eric Christopher6256b032011-04-22 03:19:48 +00001313 }
1314 }
1315
Eric Christopherb3e6b042013-02-28 20:26:17 +00001316 // Make sure the cpu type has the correct mask.
1317 assert((MachOObj->getHeader().CPUType & llvm::MachO::CPUArchABI64)
1318 == llvm::MachO::CPUArchABI64 &&
1319 "32-bit object file when we're 64-bit?");
1320
Eric Christopher6256b032011-04-22 03:19:48 +00001321 switch (MachOObj->getHeader().CPUType) {
Eric Christopherf4b2f932011-04-22 06:34:01 +00001322 case llvm::MachO::CPUTypeX86_64:
Eric Christopher9ab1d7f2011-04-22 04:08:58 +00001323 return "Mach-O 64-bit x86-64";
Eric Christopherf4b2f932011-04-22 06:34:01 +00001324 case llvm::MachO::CPUTypePowerPC64:
Eric Christopher9ab1d7f2011-04-22 04:08:58 +00001325 return "Mach-O 64-bit ppc64";
Eric Christopher6256b032011-04-22 03:19:48 +00001326 default:
Eric Christopher9ab1d7f2011-04-22 04:08:58 +00001327 return "Mach-O 64-bit unknown";
Eric Christopher6256b032011-04-22 03:19:48 +00001328 }
1329}
1330
1331unsigned MachOObjectFile::getArch() const {
1332 switch (MachOObj->getHeader().CPUType) {
Eric Christopherf4b2f932011-04-22 06:34:01 +00001333 case llvm::MachO::CPUTypeI386:
Eric Christopher6256b032011-04-22 03:19:48 +00001334 return Triple::x86;
Eric Christopherf4b2f932011-04-22 06:34:01 +00001335 case llvm::MachO::CPUTypeX86_64:
Eric Christopher6256b032011-04-22 03:19:48 +00001336 return Triple::x86_64;
Eric Christopherf4b2f932011-04-22 06:34:01 +00001337 case llvm::MachO::CPUTypeARM:
Eric Christopher6256b032011-04-22 03:19:48 +00001338 return Triple::arm;
Eric Christopherf4b2f932011-04-22 06:34:01 +00001339 case llvm::MachO::CPUTypePowerPC:
Eric Christopher6256b032011-04-22 03:19:48 +00001340 return Triple::ppc;
Eric Christopherf4b2f932011-04-22 06:34:01 +00001341 case llvm::MachO::CPUTypePowerPC64:
Eric Christopher6256b032011-04-22 03:19:48 +00001342 return Triple::ppc64;
1343 default:
1344 return Triple::UnknownArch;
1345 }
1346}
1347
Owen Andersonf7c93a32011-10-11 17:32:27 +00001348} // end namespace object
Eric Christopher6256b032011-04-22 03:19:48 +00001349} // end namespace llvm