blob: 38198b17938d1af0f9f80abba13597b64d0b3d51 [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"
Rafael Espindola8764c892013-04-07 20:01:29 +000018#include "llvm/Support/DataExtractor.h"
Owen Anderson1832f4d2011-10-26 20:42:54 +000019#include "llvm/Support/Format.h"
Rafael Espindolafd7aa382013-04-18 18:08:55 +000020#include "llvm/Support/Host.h"
Eric Christopher6256b032011-04-22 03:19:48 +000021#include "llvm/Support/MemoryBuffer.h"
Eric Christopher6256b032011-04-22 03:19:48 +000022#include <cctype>
23#include <cstring>
24#include <limits>
25
26using namespace llvm;
27using namespace object;
28
29namespace llvm {
Owen Andersonf7c93a32011-10-11 17:32:27 +000030namespace object {
Eric Christopher6256b032011-04-22 03:19:48 +000031
Rafael Espindolafd7aa382013-04-18 18:08:55 +000032struct SymbolTableEntryBase {
33 uint32_t StringIndex;
34 uint8_t Type;
35 uint8_t SectionIndex;
36 uint16_t Flags;
37};
38
39struct SectionBase {
40 char Name[16];
41 char SegmentName[16];
42};
43
44template<typename T>
45static void SwapValue(T &Value) {
46 Value = sys::SwapByteOrder(Value);
Benjamin Kramer0fcab072011-09-08 20:52:17 +000047}
48
Rafael Espindolafd7aa382013-04-18 18:08:55 +000049template<typename T>
50static void SwapStruct(T &Value);
51
52template<>
53void SwapStruct(macho::RelocationEntry &H) {
54 SwapValue(H.Word0);
55 SwapValue(H.Word1);
Rafael Espindola3eff3182013-04-07 16:07:35 +000056}
57
Rafael Espindolafd7aa382013-04-18 18:08:55 +000058template<>
59void SwapStruct(macho::LoadCommand &L) {
60 SwapValue(L.Type);
61 SwapValue(L.Size);
62}
Rafael Espindola8764c892013-04-07 20:01:29 +000063
Rafael Espindolafd7aa382013-04-18 18:08:55 +000064template<>
65void SwapStruct(SymbolTableEntryBase &S) {
66 SwapValue(S.StringIndex);
67 SwapValue(S.Flags);
68}
69
70template<>
71void SwapStruct(macho::Section &S) {
72 SwapValue(S.Address);
73 SwapValue(S.Size);
74 SwapValue(S.Offset);
75 SwapValue(S.Align);
76 SwapValue(S.RelocationTableOffset);
77 SwapValue(S.NumRelocationTableEntries);
78 SwapValue(S.Flags);
79 SwapValue(S.Reserved1);
80 SwapValue(S.Reserved2);
81}
82
83template<>
84void SwapStruct(macho::Section64 &S) {
85 SwapValue(S.Address);
86 SwapValue(S.Size);
87 SwapValue(S.Offset);
88 SwapValue(S.Align);
89 SwapValue(S.RelocationTableOffset);
90 SwapValue(S.NumRelocationTableEntries);
91 SwapValue(S.Flags);
92 SwapValue(S.Reserved1);
93 SwapValue(S.Reserved2);
94 SwapValue(S.Reserved3);
95}
96
97template<>
98void SwapStruct(macho::SymbolTableEntry &S) {
99 SwapValue(S.StringIndex);
100 SwapValue(S.Flags);
101 SwapValue(S.Value);
102}
103
104template<>
105void SwapStruct(macho::Symbol64TableEntry &S) {
106 SwapValue(S.StringIndex);
107 SwapValue(S.Flags);
108 SwapValue(S.Value);
109}
110
111template<>
112void SwapStruct(macho::Header &H) {
113 SwapValue(H.Magic);
114 SwapValue(H.CPUType);
115 SwapValue(H.CPUSubtype);
116 SwapValue(H.FileType);
117 SwapValue(H.NumLoadCommands);
118 SwapValue(H.SizeOfLoadCommands);
119 SwapValue(H.Flags);
120}
121
122template<>
123void SwapStruct(macho::SymtabLoadCommand &C) {
124 SwapValue(C.Type);
125 SwapValue(C.Size);
126 SwapValue(C.SymbolTableOffset);
127 SwapValue(C.NumSymbolTableEntries);
128 SwapValue(C.StringTableOffset);
129 SwapValue(C.StringTableSize);
130}
131
132template<>
133void SwapStruct(macho::LinkeditDataLoadCommand &C) {
134 SwapValue(C.Type);
135 SwapValue(C.Size);
136 SwapValue(C.DataOffset);
137 SwapValue(C.DataSize);
138}
139
140template<>
141void SwapStruct(macho::SegmentLoadCommand &C) {
142 SwapValue(C.Type);
143 SwapValue(C.Size);
144 SwapValue(C.VMAddress);
145 SwapValue(C.VMSize);
146 SwapValue(C.FileOffset);
147 SwapValue(C.FileSize);
148 SwapValue(C.MaxVMProtection);
149 SwapValue(C.InitialVMProtection);
150 SwapValue(C.NumSections);
151 SwapValue(C.Flags);
152}
153
154template<>
155void SwapStruct(macho::Segment64LoadCommand &C) {
156 SwapValue(C.Type);
157 SwapValue(C.Size);
158 SwapValue(C.VMAddress);
159 SwapValue(C.VMSize);
160 SwapValue(C.FileOffset);
161 SwapValue(C.FileSize);
162 SwapValue(C.MaxVMProtection);
163 SwapValue(C.InitialVMProtection);
164 SwapValue(C.NumSections);
165 SwapValue(C.Flags);
166}
167
Rafael Espindola143d2232013-04-19 13:45:05 +0000168template<typename T>
169T getStruct(const MachOObjectFile *O, const char *P) {
170 T Cmd;
171 memcpy(&Cmd, P, sizeof(T));
172 if (O->isLittleEndian() != sys::IsLittleEndianHost)
173 SwapStruct(Cmd);
174 return Cmd;
Rafael Espindolafd7aa382013-04-18 18:08:55 +0000175}
176
177static macho::SegmentLoadCommand
178getSegmentLoadCommand(const MachOObjectFile *O,
179 const MachOObjectFile::LoadCommandInfo &L) {
Rafael Espindola143d2232013-04-19 13:45:05 +0000180 return getStruct<macho::SegmentLoadCommand>(O, L.Ptr);
Rafael Espindolafd7aa382013-04-18 18:08:55 +0000181}
182
183static macho::Segment64LoadCommand
184getSegment64LoadCommand(const MachOObjectFile *O,
185 const MachOObjectFile::LoadCommandInfo &L) {
Rafael Espindola143d2232013-04-19 13:45:05 +0000186 return getStruct<macho::Segment64LoadCommand>(O, L.Ptr);
Rafael Espindolafd7aa382013-04-18 18:08:55 +0000187}
188
189static uint32_t
190getSegmentLoadCommandNumSections(const MachOObjectFile *O,
191 const MachOObjectFile::LoadCommandInfo &L) {
192 if (O->is64Bit()) {
193 macho::Segment64LoadCommand S = getSegment64LoadCommand(O, L);
194 return S.NumSections;
Rafael Espindola8764c892013-04-07 20:01:29 +0000195 }
Rafael Espindolafd7aa382013-04-18 18:08:55 +0000196 macho::SegmentLoadCommand S = getSegmentLoadCommand(O, L);
197 return S.NumSections;
Rafael Espindola3eff3182013-04-07 16:07:35 +0000198}
199
Rafael Espindolafd7aa382013-04-18 18:08:55 +0000200static const SectionBase *
201getSectionBase(const MachOObjectFile *O, MachOObjectFile::LoadCommandInfo L,
202 unsigned Sec) {
203 uintptr_t CommandAddr = reinterpret_cast<uintptr_t>(L.Ptr);
204
205 bool Is64 = O->is64Bit();
206 unsigned SegmentLoadSize = Is64 ? sizeof(macho::Segment64LoadCommand) :
207 sizeof(macho::SegmentLoadCommand);
208 unsigned SectionSize = Is64 ? sizeof(macho::Section64) :
209 sizeof(macho::Section);
210
211 uintptr_t SectionAddr = CommandAddr + SegmentLoadSize + Sec * SectionSize;
212 return reinterpret_cast<const SectionBase*>(SectionAddr);
Rafael Espindola0f08eb12013-04-07 19:05:30 +0000213}
214
Rafael Espindolafd7aa382013-04-18 18:08:55 +0000215static const char *getPtr(const MachOObjectFile *O, size_t Offset) {
216 return O->getData().substr(Offset, 1).data();
Rafael Espindola0f08eb12013-04-07 19:05:30 +0000217}
218
Rafael Espindolafd7aa382013-04-18 18:08:55 +0000219static SymbolTableEntryBase
220getSymbolTableEntryBase(const MachOObjectFile *O, DataRefImpl DRI) {
Rafael Espindola802fe932013-04-24 19:47:55 +0000221 const char *P = reinterpret_cast<const char *>(DRI.p);
Rafael Espindola143d2232013-04-19 13:45:05 +0000222 return getStruct<SymbolTableEntryBase>(O, P);
Eric Christopher6256b032011-04-22 03:19:48 +0000223}
224
Rafael Espindolafd7aa382013-04-18 18:08:55 +0000225static StringRef parseSegmentOrSectionName(const char *P) {
Rafael Espindolacef81b32012-12-21 03:47:03 +0000226 if (P[15] == 0)
227 // Null terminated.
228 return P;
229 // Not null terminated, so this is a 16 char string.
230 return StringRef(P, 16);
231}
232
Rafael Espindolafd7aa382013-04-18 18:08:55 +0000233// Helper to advance a section or symbol iterator multiple increments at a time.
234template<class T>
235static error_code advance(T &it, size_t Val) {
236 error_code ec;
237 while (Val--) {
238 it.increment(ec);
239 }
240 return ec;
241}
242
243template<class T>
244static void advanceTo(T &it, size_t Val) {
245 if (error_code ec = advance(it, Val))
246 report_fatal_error(ec.message());
247}
248
249static unsigned getCPUType(const MachOObjectFile *O) {
250 return O->getHeader().CPUType;
251}
252
253static void printRelocationTargetName(const MachOObjectFile *O,
254 const macho::RelocationEntry &RE,
255 raw_string_ostream &fmt) {
256 bool IsScattered = O->isRelocationScattered(RE);
257
258 // Target of a scattered relocation is an address. In the interest of
259 // generating pretty output, scan through the symbol table looking for a
260 // symbol that aligns with that address. If we find one, print it.
261 // Otherwise, we just print the hex address of the target.
262 if (IsScattered) {
263 uint32_t Val = O->getPlainRelocationSymbolNum(RE);
264
265 error_code ec;
266 for (symbol_iterator SI = O->begin_symbols(), SE = O->end_symbols();
267 SI != SE; SI.increment(ec)) {
268 if (ec) report_fatal_error(ec.message());
269
270 uint64_t Addr;
271 StringRef Name;
272
273 if ((ec = SI->getAddress(Addr)))
274 report_fatal_error(ec.message());
275 if (Addr != Val) continue;
276 if ((ec = SI->getName(Name)))
277 report_fatal_error(ec.message());
278 fmt << Name;
279 return;
280 }
281
282 // If we couldn't find a symbol that this relocation refers to, try
283 // to find a section beginning instead.
284 for (section_iterator SI = O->begin_sections(), SE = O->end_sections();
285 SI != SE; SI.increment(ec)) {
286 if (ec) report_fatal_error(ec.message());
287
288 uint64_t Addr;
289 StringRef Name;
290
291 if ((ec = SI->getAddress(Addr)))
292 report_fatal_error(ec.message());
293 if (Addr != Val) continue;
294 if ((ec = SI->getName(Name)))
295 report_fatal_error(ec.message());
296 fmt << Name;
297 return;
298 }
299
300 fmt << format("0x%x", Val);
301 return;
302 }
303
304 StringRef S;
305 bool isExtern = O->getPlainRelocationExternal(RE);
306 uint64_t Val = O->getAnyRelocationAddress(RE);
307
308 if (isExtern) {
309 symbol_iterator SI = O->begin_symbols();
310 advanceTo(SI, Val);
311 SI->getName(S);
312 } else {
313 section_iterator SI = O->begin_sections();
314 advanceTo(SI, Val);
315 SI->getName(S);
316 }
317
318 fmt << S;
319}
320
321static uint32_t getPlainRelocationAddress(const macho::RelocationEntry &RE) {
322 return RE.Word0;
323}
324
325static unsigned
326getScatteredRelocationAddress(const macho::RelocationEntry &RE) {
327 return RE.Word0 & 0xffffff;
328}
329
330static bool getPlainRelocationPCRel(const MachOObjectFile *O,
331 const macho::RelocationEntry &RE) {
332 if (O->isLittleEndian())
333 return (RE.Word1 >> 24) & 1;
334 return (RE.Word1 >> 7) & 1;
335}
336
337static bool
338getScatteredRelocationPCRel(const MachOObjectFile *O,
339 const macho::RelocationEntry &RE) {
340 return (RE.Word0 >> 30) & 1;
341}
342
343static unsigned getPlainRelocationLength(const MachOObjectFile *O,
344 const macho::RelocationEntry &RE) {
345 if (O->isLittleEndian())
346 return (RE.Word1 >> 25) & 3;
347 return (RE.Word1 >> 5) & 3;
348}
349
350static unsigned
351getScatteredRelocationLength(const macho::RelocationEntry &RE) {
352 return (RE.Word0 >> 28) & 3;
353}
354
355static unsigned getPlainRelocationType(const MachOObjectFile *O,
356 const macho::RelocationEntry &RE) {
357 if (O->isLittleEndian())
358 return RE.Word1 >> 28;
359 return RE.Word1 & 0xf;
360}
361
362static unsigned getScatteredRelocationType(const macho::RelocationEntry &RE) {
363 return (RE.Word0 >> 24) & 0xf;
364}
365
366static uint32_t getSectionFlags(const MachOObjectFile *O,
367 DataRefImpl Sec) {
368 if (O->is64Bit()) {
369 macho::Section64 Sect = O->getSection64(Sec);
370 return Sect.Flags;
371 }
372 macho::Section Sect = O->getSection(Sec);
373 return Sect.Flags;
374}
375
376MachOObjectFile::MachOObjectFile(MemoryBuffer *Object,
377 bool IsLittleEndian, bool Is64bits,
378 error_code &ec)
379 : ObjectFile(getMachOType(IsLittleEndian, Is64bits), Object),
380 SymtabLoadCmd(NULL) {
381 uint32_t LoadCommandCount = this->getHeader().NumLoadCommands;
382 macho::LoadCommandType SegmentLoadType = is64Bit() ?
383 macho::LCT_Segment64 : macho::LCT_Segment;
384
385 MachOObjectFile::LoadCommandInfo Load = getFirstLoadCommandInfo();
Rafael Espindoladb5f9272013-04-19 11:36:47 +0000386 for (unsigned I = 0; ; ++I) {
Rafael Espindolafd7aa382013-04-18 18:08:55 +0000387 if (Load.C.Type == macho::LCT_Symtab) {
388 assert(!SymtabLoadCmd && "Multiple symbol tables");
389 SymtabLoadCmd = Load.Ptr;
390 }
391
392 if (Load.C.Type == SegmentLoadType) {
393 uint32_t NumSections = getSegmentLoadCommandNumSections(this, Load);
394 for (unsigned J = 0; J < NumSections; ++J) {
395 const SectionBase *Sec = getSectionBase(this, Load, J);
396 Sections.push_back(reinterpret_cast<const char*>(Sec));
397 }
398 }
Rafael Espindoladb5f9272013-04-19 11:36:47 +0000399
400 if (I == LoadCommandCount - 1)
401 break;
402 else
403 Load = getNextLoadCommandInfo(Load);
Rafael Espindolafd7aa382013-04-18 18:08:55 +0000404 }
405}
406
407error_code MachOObjectFile::getSymbolNext(DataRefImpl Symb,
408 SymbolRef &Res) const {
Rafael Espindola802fe932013-04-24 19:47:55 +0000409 unsigned SymbolTableEntrySize = is64Bit() ?
410 sizeof(macho::Symbol64TableEntry) :
411 sizeof(macho::SymbolTableEntry);
412 Symb.p += SymbolTableEntrySize;
Rafael Espindolafd7aa382013-04-18 18:08:55 +0000413 Res = SymbolRef(Symb, this);
414 return object_error::success;
415}
416
417error_code MachOObjectFile::getSymbolName(DataRefImpl Symb,
418 StringRef &Res) const {
419 macho::SymtabLoadCommand S = getSymtabLoadCommand();
420 const char *StringTable = getPtr(this, S.StringTableOffset);
421 SymbolTableEntryBase Entry = getSymbolTableEntryBase(this, Symb);
422 const char *Start = &StringTable[Entry.StringIndex];
423 Res = StringRef(Start);
424 return object_error::success;
425}
426
427error_code MachOObjectFile::getSymbolAddress(DataRefImpl Symb,
428 uint64_t &Res) const {
429 if (is64Bit()) {
430 macho::Symbol64TableEntry Entry = getSymbol64TableEntry(Symb);
431 Res = Entry.Value;
432 } else {
433 macho::SymbolTableEntry Entry = getSymbolTableEntry(Symb);
434 Res = Entry.Value;
435 }
436 return object_error::success;
437}
438
439error_code
440MachOObjectFile::getSymbolFileOffset(DataRefImpl Symb,
441 uint64_t &Res) const {
442 SymbolTableEntryBase Entry = getSymbolTableEntryBase(this, Symb);
443 getSymbolAddress(Symb, Res);
444 if (Entry.SectionIndex) {
445 uint64_t Delta;
446 DataRefImpl SecRel;
447 SecRel.d.a = Entry.SectionIndex-1;
448 if (is64Bit()) {
449 macho::Section64 Sec = getSection64(SecRel);
450 Delta = Sec.Offset - Sec.Address;
451 } else {
452 macho::Section Sec = getSection(SecRel);
453 Delta = Sec.Offset - Sec.Address;
454 }
455
456 Res += Delta;
457 }
458
459 return object_error::success;
460}
461
462error_code MachOObjectFile::getSymbolSize(DataRefImpl DRI,
463 uint64_t &Result) const {
464 uint64_t BeginOffset;
465 uint64_t EndOffset = 0;
466 uint8_t SectionIndex;
467
468 SymbolTableEntryBase Entry = getSymbolTableEntryBase(this, DRI);
469 uint64_t Value;
470 getSymbolAddress(DRI, Value);
471
472 BeginOffset = Value;
473
474 SectionIndex = Entry.SectionIndex;
475 if (!SectionIndex) {
476 uint32_t flags = SymbolRef::SF_None;
477 this->getSymbolFlags(DRI, flags);
478 if (flags & SymbolRef::SF_Common)
479 Result = Value;
480 else
481 Result = UnknownAddressOrSize;
482 return object_error::success;
483 }
484 // Unfortunately symbols are unsorted so we need to touch all
485 // symbols from load command
Rafael Espindola802fe932013-04-24 19:47:55 +0000486 error_code ec;
487 for (symbol_iterator I = begin_symbols(), E = end_symbols(); I != E;
488 I.increment(ec)) {
489 DataRefImpl DRI = I->getRawDataRefImpl();
Rafael Espindolafd7aa382013-04-18 18:08:55 +0000490 Entry = getSymbolTableEntryBase(this, DRI);
491 getSymbolAddress(DRI, Value);
492 if (Entry.SectionIndex == SectionIndex && Value > BeginOffset)
493 if (!EndOffset || Value < EndOffset)
494 EndOffset = Value;
Rafael Espindolafd7aa382013-04-18 18:08:55 +0000495 }
496 if (!EndOffset) {
497 uint64_t Size;
498 DataRefImpl Sec;
499 Sec.d.a = SectionIndex-1;
500 getSectionSize(Sec, Size);
501 getSectionAddress(Sec, EndOffset);
502 EndOffset += Size;
503 }
504 Result = EndOffset - BeginOffset;
505 return object_error::success;
506}
507
508error_code MachOObjectFile::getSymbolType(DataRefImpl Symb,
509 SymbolRef::Type &Res) const {
510 SymbolTableEntryBase Entry = getSymbolTableEntryBase(this, Symb);
511 uint8_t n_type = Entry.Type;
512
513 Res = SymbolRef::ST_Other;
514
515 // If this is a STAB debugging symbol, we can do nothing more.
516 if (n_type & MachO::NlistMaskStab) {
517 Res = SymbolRef::ST_Debug;
518 return object_error::success;
519 }
520
521 switch (n_type & MachO::NlistMaskType) {
522 case MachO::NListTypeUndefined :
523 Res = SymbolRef::ST_Unknown;
524 break;
525 case MachO::NListTypeSection :
526 Res = SymbolRef::ST_Function;
527 break;
528 }
529 return object_error::success;
530}
531
532error_code MachOObjectFile::getSymbolNMTypeChar(DataRefImpl Symb,
533 char &Res) const {
534 SymbolTableEntryBase Entry = getSymbolTableEntryBase(this, Symb);
535 uint8_t Type = Entry.Type;
536 uint16_t Flags = Entry.Flags;
537
538 char Char;
539 switch (Type & macho::STF_TypeMask) {
540 case macho::STT_Undefined:
541 Char = 'u';
542 break;
543 case macho::STT_Absolute:
544 case macho::STT_Section:
545 Char = 's';
546 break;
547 default:
548 Char = '?';
549 break;
550 }
551
552 if (Flags & (macho::STF_External | macho::STF_PrivateExtern))
553 Char = toupper(static_cast<unsigned char>(Char));
554 Res = Char;
555 return object_error::success;
556}
557
558error_code MachOObjectFile::getSymbolFlags(DataRefImpl DRI,
559 uint32_t &Result) const {
560 SymbolTableEntryBase Entry = getSymbolTableEntryBase(this, DRI);
561
562 uint8_t MachOType = Entry.Type;
563 uint16_t MachOFlags = Entry.Flags;
564
565 // TODO: Correctly set SF_ThreadLocal
566 Result = SymbolRef::SF_None;
567
568 if ((MachOType & MachO::NlistMaskType) == MachO::NListTypeUndefined)
569 Result |= SymbolRef::SF_Undefined;
570
571 if (MachOFlags & macho::STF_StabsEntryMask)
572 Result |= SymbolRef::SF_FormatSpecific;
573
574 if (MachOType & MachO::NlistMaskExternal) {
575 Result |= SymbolRef::SF_Global;
576 if ((MachOType & MachO::NlistMaskType) == MachO::NListTypeUndefined)
577 Result |= SymbolRef::SF_Common;
578 }
579
580 if (MachOFlags & (MachO::NListDescWeakRef | MachO::NListDescWeakDef))
581 Result |= SymbolRef::SF_Weak;
582
583 if ((MachOType & MachO::NlistMaskType) == MachO::NListTypeAbsolute)
584 Result |= SymbolRef::SF_Absolute;
585
586 return object_error::success;
587}
588
589error_code
590MachOObjectFile::getSymbolSection(DataRefImpl Symb,
591 section_iterator &Res) const {
592 SymbolTableEntryBase Entry = getSymbolTableEntryBase(this, Symb);
593 uint8_t index = Entry.SectionIndex;
594
595 if (index == 0) {
596 Res = end_sections();
597 } else {
598 DataRefImpl DRI;
599 DRI.d.a = index - 1;
600 Res = section_iterator(SectionRef(DRI, this));
601 }
602
603 return object_error::success;
604}
605
606error_code MachOObjectFile::getSymbolValue(DataRefImpl Symb,
Rafael Espindolaf69a81f2013-04-24 15:14:22 +0000607 uint64_t &Val) const {
Rafael Espindolafd7aa382013-04-18 18:08:55 +0000608 report_fatal_error("getSymbolValue unimplemented in MachOObjectFile");
609}
610
611error_code MachOObjectFile::getSectionNext(DataRefImpl Sec,
612 SectionRef &Res) const {
613 Sec.d.a++;
614 Res = SectionRef(Sec, this);
615 return object_error::success;
616}
617
618error_code
Rafael Espindolaf69a81f2013-04-24 15:14:22 +0000619MachOObjectFile::getSectionName(DataRefImpl Sec, StringRef &Result) const {
Rafael Espindolafd7aa382013-04-18 18:08:55 +0000620 ArrayRef<char> Raw = getSectionRawName(Sec);
621 Result = parseSegmentOrSectionName(Raw.data());
622 return object_error::success;
623}
624
625error_code
Rafael Espindolaf69a81f2013-04-24 15:14:22 +0000626MachOObjectFile::getSectionAddress(DataRefImpl Sec, uint64_t &Res) const {
Rafael Espindolafd7aa382013-04-18 18:08:55 +0000627 if (is64Bit()) {
628 macho::Section64 Sect = getSection64(Sec);
629 Res = Sect.Address;
630 } else {
631 macho::Section Sect = getSection(Sec);
632 Res = Sect.Address;
633 }
634 return object_error::success;
635}
636
637error_code
Rafael Espindolaf69a81f2013-04-24 15:14:22 +0000638MachOObjectFile::getSectionSize(DataRefImpl Sec, uint64_t &Res) const {
Rafael Espindolafd7aa382013-04-18 18:08:55 +0000639 if (is64Bit()) {
640 macho::Section64 Sect = getSection64(Sec);
641 Res = Sect.Size;
642 } else {
643 macho::Section Sect = getSection(Sec);
644 Res = Sect.Size;
645 }
646
647 return object_error::success;
648}
649
650error_code
Rafael Espindolaf69a81f2013-04-24 15:14:22 +0000651MachOObjectFile::getSectionContents(DataRefImpl Sec, StringRef &Res) const {
Rafael Espindolafd7aa382013-04-18 18:08:55 +0000652 uint32_t Offset;
653 uint64_t Size;
654
655 if (is64Bit()) {
656 macho::Section64 Sect = getSection64(Sec);
657 Offset = Sect.Offset;
658 Size = Sect.Size;
659 } else {
660 macho::Section Sect =getSection(Sec);
661 Offset = Sect.Offset;
662 Size = Sect.Size;
663 }
664
665 Res = this->getData().substr(Offset, Size);
666 return object_error::success;
667}
668
669error_code
Rafael Espindolaf69a81f2013-04-24 15:14:22 +0000670MachOObjectFile::getSectionAlignment(DataRefImpl Sec, uint64_t &Res) const {
Rafael Espindolafd7aa382013-04-18 18:08:55 +0000671 uint32_t Align;
672 if (is64Bit()) {
673 macho::Section64 Sect = getSection64(Sec);
674 Align = Sect.Align;
675 } else {
676 macho::Section Sect = getSection(Sec);
677 Align = Sect.Align;
678 }
679
680 Res = uint64_t(1) << Align;
681 return object_error::success;
682}
683
684error_code
685MachOObjectFile::isSectionText(DataRefImpl Sec, bool &Res) const {
686 uint32_t Flags = getSectionFlags(this, Sec);
687 Res = Flags & macho::SF_PureInstructions;
688 return object_error::success;
689}
690
Rafael Espindolaf69a81f2013-04-24 15:14:22 +0000691error_code MachOObjectFile::isSectionData(DataRefImpl DRI, bool &Result) const {
Michael J. Spencer13afc5e2011-09-28 20:57:30 +0000692 // FIXME: Unimplemented.
693 Result = false;
694 return object_error::success;
695}
696
Rafael Espindolaf69a81f2013-04-24 15:14:22 +0000697error_code MachOObjectFile::isSectionBSS(DataRefImpl DRI, bool &Result) const {
Andrew Kaylor30b20eb2012-10-10 01:45:52 +0000698 // FIXME: Unimplemented.
Preston Gurdc68dda82012-04-12 20:13:57 +0000699 Result = false;
700 return object_error::success;
701}
702
Rafael Espindolaf6cfc152013-04-09 14:49:08 +0000703error_code
Rafael Espindolafd7aa382013-04-18 18:08:55 +0000704MachOObjectFile::isSectionRequiredForExecution(DataRefImpl Sec,
Rafael Espindolaf69a81f2013-04-24 15:14:22 +0000705 bool &Result) const {
Rafael Espindolaf6cfc152013-04-09 14:49:08 +0000706 // FIXME: Unimplemented.
707 Result = true;
Preston Gurdc68dda82012-04-12 20:13:57 +0000708 return object_error::success;
709}
710
Rafael Espindolafd7aa382013-04-18 18:08:55 +0000711error_code MachOObjectFile::isSectionVirtual(DataRefImpl Sec,
Rafael Espindolaf69a81f2013-04-24 15:14:22 +0000712 bool &Result) const {
Rafael Espindolaf6cfc152013-04-09 14:49:08 +0000713 // FIXME: Unimplemented.
714 Result = false;
715 return object_error::success;
716}
717
Rafael Espindolafd7aa382013-04-18 18:08:55 +0000718error_code
719MachOObjectFile::isSectionZeroInit(DataRefImpl Sec, bool &Res) const {
720 uint32_t Flags = getSectionFlags(this, Sec);
721 unsigned SectionType = Flags & MachO::SectionFlagMaskSectionType;
722 Res = SectionType == MachO::SectionTypeZeroFill ||
723 SectionType == MachO::SectionTypeZeroFillLarge;
724 return object_error::success;
725}
726
727error_code MachOObjectFile::isSectionReadOnlyData(DataRefImpl Sec,
Rafael Espindolaf69a81f2013-04-24 15:14:22 +0000728 bool &Result) const {
Andrew Kaylor3a129c82012-10-10 01:41:33 +0000729 // Consider using the code from isSectionText to look for __const sections.
730 // Alternately, emit S_ATTR_PURE_INSTRUCTIONS and/or S_ATTR_SOME_INSTRUCTIONS
731 // to use section attributes to distinguish code from data.
732
733 // FIXME: Unimplemented.
734 Result = false;
735 return object_error::success;
736}
737
Rafael Espindolafd7aa382013-04-18 18:08:55 +0000738error_code
Rafael Espindolaf69a81f2013-04-24 15:14:22 +0000739MachOObjectFile::sectionContainsSymbol(DataRefImpl Sec, DataRefImpl Symb,
Rafael Espindolafd7aa382013-04-18 18:08:55 +0000740 bool &Result) const {
741 SymbolRef::Type ST;
742 this->getSymbolType(Symb, ST);
743 if (ST == SymbolRef::ST_Unknown) {
744 Result = false;
745 return object_error::success;
746 }
747
748 uint64_t SectBegin, SectEnd;
749 getSectionAddress(Sec, SectBegin);
750 getSectionSize(Sec, SectEnd);
751 SectEnd += SectBegin;
752
753 uint64_t SymAddr;
754 getSymbolAddress(Symb, SymAddr);
755 Result = (SymAddr >= SectBegin) && (SymAddr < SectEnd);
756
757 return object_error::success;
758}
759
760relocation_iterator MachOObjectFile::getSectionRelBegin(DataRefImpl Sec) const {
Rafael Espindolae5330f72013-04-25 12:45:46 +0000761 uint32_t Offset;
762 if (is64Bit()) {
763 macho::Section64 Sect = getSection64(Sec);
764 Offset = Sect.RelocationTableOffset;
765 } else {
766 macho::Section Sect = getSection(Sec);
767 Offset = Sect.RelocationTableOffset;
768 }
769
770 DataRefImpl Ret;
771 Ret.p = reinterpret_cast<uintptr_t>(getPtr(this, Offset));
772 return relocation_iterator(RelocationRef(Ret, this));
Michael J. Spencer4344b1e2011-10-07 19:25:32 +0000773}
Rafael Espindola335f1d42013-04-08 20:45:01 +0000774
Rafael Espindolafd7aa382013-04-18 18:08:55 +0000775relocation_iterator
776MachOObjectFile::getSectionRelEnd(DataRefImpl Sec) const {
Rafael Espindolae5330f72013-04-25 12:45:46 +0000777 uint32_t Offset;
778 uint32_t Num;
Rafael Espindolafd7aa382013-04-18 18:08:55 +0000779 if (is64Bit()) {
780 macho::Section64 Sect = getSection64(Sec);
Rafael Espindolae5330f72013-04-25 12:45:46 +0000781 Offset = Sect.RelocationTableOffset;
782 Num = Sect.NumRelocationTableEntries;
Rafael Espindolafd7aa382013-04-18 18:08:55 +0000783 } else {
784 macho::Section Sect = getSection(Sec);
Rafael Espindolae5330f72013-04-25 12:45:46 +0000785 Offset = Sect.RelocationTableOffset;
786 Num = Sect.NumRelocationTableEntries;
Rafael Espindolafd7aa382013-04-18 18:08:55 +0000787 }
Eric Christopher6256b032011-04-22 03:19:48 +0000788
Rafael Espindolae5330f72013-04-25 12:45:46 +0000789 const macho::RelocationEntry *P =
790 reinterpret_cast<const macho::RelocationEntry*>(getPtr(this, Offset));
791
Rafael Espindolafd7aa382013-04-18 18:08:55 +0000792 DataRefImpl Ret;
Rafael Espindolae5330f72013-04-25 12:45:46 +0000793 Ret.p = reinterpret_cast<uintptr_t>(P + Num);
Rafael Espindolafd7aa382013-04-18 18:08:55 +0000794 return relocation_iterator(RelocationRef(Ret, this));
795}
Benjamin Kramer0fcab072011-09-08 20:52:17 +0000796
Rafael Espindolae5330f72013-04-25 12:45:46 +0000797error_code MachOObjectFile::getRelocationNext(DataRefImpl Rel,
798 RelocationRef &Res) const {
799 const macho::RelocationEntry *P =
800 reinterpret_cast<const macho::RelocationEntry *>(Rel.p);
801 Rel.p = reinterpret_cast<uintptr_t>(P + 1);
Benjamin Kramer0fcab072011-09-08 20:52:17 +0000802 Res = RelocationRef(Rel, this);
803 return object_error::success;
804}
Owen Andersond8fa76d2011-10-24 23:20:07 +0000805
Rafael Espindolafd7aa382013-04-18 18:08:55 +0000806error_code
Rafael Espindolaf69a81f2013-04-24 15:14:22 +0000807MachOObjectFile::getRelocationAddress(DataRefImpl Rel, uint64_t &Res) const {
Rafael Espindola956ca722013-04-25 12:28:45 +0000808 report_fatal_error("getRelocationAddress not implemented in MachOObjectFile");
Benjamin Kramer0fcab072011-09-08 20:52:17 +0000809}
810
Rafael Espindolafd7aa382013-04-18 18:08:55 +0000811error_code MachOObjectFile::getRelocationOffset(DataRefImpl Rel,
Rafael Espindolaf69a81f2013-04-24 15:14:22 +0000812 uint64_t &Res) const {
Rafael Espindolafd7aa382013-04-18 18:08:55 +0000813 macho::RelocationEntry RE = getRelocation(Rel);
814 Res = getAnyRelocationAddress(RE);
815 return object_error::success;
David Meyer5c2b4ea2012-03-01 01:36:50 +0000816}
817
Rafael Espindolafd7aa382013-04-18 18:08:55 +0000818error_code
Rafael Espindolaf69a81f2013-04-24 15:14:22 +0000819MachOObjectFile::getRelocationSymbol(DataRefImpl Rel, SymbolRef &Res) const {
Rafael Espindolafd7aa382013-04-18 18:08:55 +0000820 macho::RelocationEntry RE = getRelocation(Rel);
821 uint32_t SymbolIdx = getPlainRelocationSymbolNum(RE);
822 bool isExtern = getPlainRelocationExternal(RE);
Rafael Espindola802fe932013-04-24 19:47:55 +0000823 if (!isExtern) {
824 Res = *end_symbols();
825 return object_error::success;
Rafael Espindolafd7aa382013-04-18 18:08:55 +0000826 }
Rafael Espindola802fe932013-04-24 19:47:55 +0000827
828 macho::SymtabLoadCommand S = getSymtabLoadCommand();
829 unsigned SymbolTableEntrySize = is64Bit() ?
830 sizeof(macho::Symbol64TableEntry) :
831 sizeof(macho::SymbolTableEntry);
832 uint64_t Offset = S.SymbolTableOffset + SymbolIdx * SymbolTableEntrySize;
833 DataRefImpl Sym;
834 Sym.p = reinterpret_cast<uintptr_t>(getPtr(this, Offset));
Rafael Espindolafd7aa382013-04-18 18:08:55 +0000835 Res = SymbolRef(Sym, this);
836 return object_error::success;
837}
838
839error_code MachOObjectFile::getRelocationType(DataRefImpl Rel,
Rafael Espindolaf69a81f2013-04-24 15:14:22 +0000840 uint64_t &Res) const {
Rafael Espindolafd7aa382013-04-18 18:08:55 +0000841 macho::RelocationEntry RE = getRelocation(Rel);
842 Res = getAnyRelocationType(RE);
843 return object_error::success;
844}
845
846error_code
847MachOObjectFile::getRelocationTypeName(DataRefImpl Rel,
848 SmallVectorImpl<char> &Result) const {
849 StringRef res;
850 uint64_t RType;
851 getRelocationType(Rel, RType);
852
853 unsigned Arch = this->getArch();
854
855 switch (Arch) {
856 case Triple::x86: {
857 static const char *const Table[] = {
858 "GENERIC_RELOC_VANILLA",
859 "GENERIC_RELOC_PAIR",
860 "GENERIC_RELOC_SECTDIFF",
861 "GENERIC_RELOC_PB_LA_PTR",
862 "GENERIC_RELOC_LOCAL_SECTDIFF",
863 "GENERIC_RELOC_TLV" };
864
865 if (RType > 6)
866 res = "Unknown";
867 else
868 res = Table[RType];
869 break;
870 }
871 case Triple::x86_64: {
872 static const char *const Table[] = {
873 "X86_64_RELOC_UNSIGNED",
874 "X86_64_RELOC_SIGNED",
875 "X86_64_RELOC_BRANCH",
876 "X86_64_RELOC_GOT_LOAD",
877 "X86_64_RELOC_GOT",
878 "X86_64_RELOC_SUBTRACTOR",
879 "X86_64_RELOC_SIGNED_1",
880 "X86_64_RELOC_SIGNED_2",
881 "X86_64_RELOC_SIGNED_4",
882 "X86_64_RELOC_TLV" };
883
884 if (RType > 9)
885 res = "Unknown";
886 else
887 res = Table[RType];
888 break;
889 }
890 case Triple::arm: {
891 static const char *const Table[] = {
892 "ARM_RELOC_VANILLA",
893 "ARM_RELOC_PAIR",
894 "ARM_RELOC_SECTDIFF",
895 "ARM_RELOC_LOCAL_SECTDIFF",
896 "ARM_RELOC_PB_LA_PTR",
897 "ARM_RELOC_BR24",
898 "ARM_THUMB_RELOC_BR22",
899 "ARM_THUMB_32BIT_BRANCH",
900 "ARM_RELOC_HALF",
901 "ARM_RELOC_HALF_SECTDIFF" };
902
903 if (RType > 9)
904 res = "Unknown";
905 else
906 res = Table[RType];
907 break;
908 }
909 case Triple::ppc: {
910 static const char *const Table[] = {
911 "PPC_RELOC_VANILLA",
912 "PPC_RELOC_PAIR",
913 "PPC_RELOC_BR14",
914 "PPC_RELOC_BR24",
915 "PPC_RELOC_HI16",
916 "PPC_RELOC_LO16",
917 "PPC_RELOC_HA16",
918 "PPC_RELOC_LO14",
919 "PPC_RELOC_SECTDIFF",
920 "PPC_RELOC_PB_LA_PTR",
921 "PPC_RELOC_HI16_SECTDIFF",
922 "PPC_RELOC_LO16_SECTDIFF",
923 "PPC_RELOC_HA16_SECTDIFF",
924 "PPC_RELOC_JBSR",
925 "PPC_RELOC_LO14_SECTDIFF",
926 "PPC_RELOC_LOCAL_SECTDIFF" };
927
928 res = Table[RType];
929 break;
930 }
931 case Triple::UnknownArch:
932 res = "Unknown";
933 break;
934 }
935 Result.append(res.begin(), res.end());
936 return object_error::success;
937}
938
939error_code MachOObjectFile::getRelocationAdditionalInfo(DataRefImpl Rel,
940 int64_t &Res) const {
Rafael Espindola8bf80062013-04-11 02:21:31 +0000941 Res = 0;
942 return object_error::success;
943}
944
Rafael Espindolafd7aa382013-04-18 18:08:55 +0000945error_code
946MachOObjectFile::getRelocationValueString(DataRefImpl Rel,
Rafael Espindolaf69a81f2013-04-24 15:14:22 +0000947 SmallVectorImpl<char> &Result) const {
Rafael Espindolafd7aa382013-04-18 18:08:55 +0000948 macho::RelocationEntry RE = getRelocation(Rel);
David Meyer5c2b4ea2012-03-01 01:36:50 +0000949
Rafael Espindolafd7aa382013-04-18 18:08:55 +0000950 unsigned Arch = this->getArch();
Eric Christopher6256b032011-04-22 03:19:48 +0000951
Rafael Espindolafd7aa382013-04-18 18:08:55 +0000952 std::string fmtbuf;
953 raw_string_ostream fmt(fmtbuf);
954 unsigned Type = this->getAnyRelocationType(RE);
955 bool IsPCRel = this->getAnyRelocationPCRel(RE);
956
957 // Determine any addends that should be displayed with the relocation.
958 // These require decoding the relocation type, which is triple-specific.
959
960 // X86_64 has entirely custom relocation types.
961 if (Arch == Triple::x86_64) {
962 bool isPCRel = getAnyRelocationPCRel(RE);
963
964 switch (Type) {
965 case macho::RIT_X86_64_GOTLoad: // X86_64_RELOC_GOT_LOAD
966 case macho::RIT_X86_64_GOT: { // X86_64_RELOC_GOT
967 printRelocationTargetName(this, RE, fmt);
968 fmt << "@GOT";
969 if (isPCRel) fmt << "PCREL";
970 break;
971 }
972 case macho::RIT_X86_64_Subtractor: { // X86_64_RELOC_SUBTRACTOR
973 DataRefImpl RelNext = Rel;
974 RelNext.d.a++;
975 macho::RelocationEntry RENext = getRelocation(RelNext);
976
977 // X86_64_SUBTRACTOR must be followed by a relocation of type
978 // X86_64_RELOC_UNSIGNED.
979 // NOTE: Scattered relocations don't exist on x86_64.
980 unsigned RType = getAnyRelocationType(RENext);
981 if (RType != 0)
982 report_fatal_error("Expected X86_64_RELOC_UNSIGNED after "
983 "X86_64_RELOC_SUBTRACTOR.");
984
985 // The X86_64_RELOC_UNSIGNED contains the minuend symbol,
986 // X86_64_SUBTRACTOR contains to the subtrahend.
987 printRelocationTargetName(this, RENext, fmt);
988 fmt << "-";
989 printRelocationTargetName(this, RE, fmt);
990 break;
991 }
992 case macho::RIT_X86_64_TLV:
993 printRelocationTargetName(this, RE, fmt);
994 fmt << "@TLV";
995 if (isPCRel) fmt << "P";
996 break;
997 case macho::RIT_X86_64_Signed1: // X86_64_RELOC_SIGNED1
998 printRelocationTargetName(this, RE, fmt);
999 fmt << "-1";
1000 break;
1001 case macho::RIT_X86_64_Signed2: // X86_64_RELOC_SIGNED2
1002 printRelocationTargetName(this, RE, fmt);
1003 fmt << "-2";
1004 break;
1005 case macho::RIT_X86_64_Signed4: // X86_64_RELOC_SIGNED4
1006 printRelocationTargetName(this, RE, fmt);
1007 fmt << "-4";
1008 break;
1009 default:
1010 printRelocationTargetName(this, RE, fmt);
1011 break;
1012 }
1013 // X86 and ARM share some relocation types in common.
1014 } else if (Arch == Triple::x86 || Arch == Triple::arm) {
1015 // Generic relocation types...
1016 switch (Type) {
1017 case macho::RIT_Pair: // GENERIC_RELOC_PAIR - prints no info
1018 return object_error::success;
1019 case macho::RIT_Difference: { // GENERIC_RELOC_SECTDIFF
1020 DataRefImpl RelNext = Rel;
1021 RelNext.d.a++;
1022 macho::RelocationEntry RENext = getRelocation(RelNext);
1023
1024 // X86 sect diff's must be followed by a relocation of type
1025 // GENERIC_RELOC_PAIR.
1026 unsigned RType = getAnyRelocationType(RENext);
1027
1028 if (RType != 1)
1029 report_fatal_error("Expected GENERIC_RELOC_PAIR after "
1030 "GENERIC_RELOC_SECTDIFF.");
1031
1032 printRelocationTargetName(this, RE, fmt);
1033 fmt << "-";
1034 printRelocationTargetName(this, RENext, fmt);
1035 break;
1036 }
1037 }
1038
1039 if (Arch == Triple::x86) {
1040 // All X86 relocations that need special printing were already
1041 // handled in the generic code.
1042 switch (Type) {
1043 case macho::RIT_Generic_LocalDifference:{// GENERIC_RELOC_LOCAL_SECTDIFF
1044 DataRefImpl RelNext = Rel;
1045 RelNext.d.a++;
1046 macho::RelocationEntry RENext = getRelocation(RelNext);
1047
1048 // X86 sect diff's must be followed by a relocation of type
1049 // GENERIC_RELOC_PAIR.
1050 unsigned RType = getAnyRelocationType(RENext);
1051 if (RType != 1)
1052 report_fatal_error("Expected GENERIC_RELOC_PAIR after "
1053 "GENERIC_RELOC_LOCAL_SECTDIFF.");
1054
1055 printRelocationTargetName(this, RE, fmt);
1056 fmt << "-";
1057 printRelocationTargetName(this, RENext, fmt);
1058 break;
1059 }
1060 case macho::RIT_Generic_TLV: {
1061 printRelocationTargetName(this, RE, fmt);
1062 fmt << "@TLV";
1063 if (IsPCRel) fmt << "P";
1064 break;
1065 }
1066 default:
1067 printRelocationTargetName(this, RE, fmt);
1068 }
1069 } else { // ARM-specific relocations
1070 switch (Type) {
1071 case macho::RIT_ARM_Half: // ARM_RELOC_HALF
1072 case macho::RIT_ARM_HalfDifference: { // ARM_RELOC_HALF_SECTDIFF
1073 // Half relocations steal a bit from the length field to encode
1074 // whether this is an upper16 or a lower16 relocation.
1075 bool isUpper = getAnyRelocationLength(RE) >> 1;
1076
1077 if (isUpper)
1078 fmt << ":upper16:(";
1079 else
1080 fmt << ":lower16:(";
1081 printRelocationTargetName(this, RE, fmt);
1082
1083 DataRefImpl RelNext = Rel;
1084 RelNext.d.a++;
1085 macho::RelocationEntry RENext = getRelocation(RelNext);
1086
1087 // ARM half relocs must be followed by a relocation of type
1088 // ARM_RELOC_PAIR.
1089 unsigned RType = getAnyRelocationType(RENext);
1090 if (RType != 1)
1091 report_fatal_error("Expected ARM_RELOC_PAIR after "
1092 "GENERIC_RELOC_HALF");
1093
1094 // NOTE: The half of the target virtual address is stashed in the
1095 // address field of the secondary relocation, but we can't reverse
1096 // engineer the constant offset from it without decoding the movw/movt
1097 // instruction to find the other half in its immediate field.
1098
1099 // ARM_RELOC_HALF_SECTDIFF encodes the second section in the
1100 // symbol/section pointer of the follow-on relocation.
1101 if (Type == macho::RIT_ARM_HalfDifference) {
1102 fmt << "-";
1103 printRelocationTargetName(this, RENext, fmt);
1104 }
1105
1106 fmt << ")";
1107 break;
1108 }
1109 default: {
1110 printRelocationTargetName(this, RE, fmt);
1111 }
1112 }
1113 }
1114 } else
1115 printRelocationTargetName(this, RE, fmt);
1116
1117 fmt.flush();
1118 Result.append(fmtbuf.begin(), fmtbuf.end());
1119 return object_error::success;
1120}
1121
1122error_code
Rafael Espindolaf69a81f2013-04-24 15:14:22 +00001123MachOObjectFile::getRelocationHidden(DataRefImpl Rel, bool &Result) const {
Rafael Espindolafd7aa382013-04-18 18:08:55 +00001124 unsigned Arch = getArch();
1125 uint64_t Type;
1126 getRelocationType(Rel, Type);
1127
1128 Result = false;
1129
1130 // On arches that use the generic relocations, GENERIC_RELOC_PAIR
1131 // is always hidden.
1132 if (Arch == Triple::x86 || Arch == Triple::arm) {
1133 if (Type == macho::RIT_Pair) Result = true;
1134 } else if (Arch == Triple::x86_64) {
1135 // On x86_64, X86_64_RELOC_UNSIGNED is hidden only when it follows
1136 // an X864_64_RELOC_SUBTRACTOR.
1137 if (Type == macho::RIT_X86_64_Unsigned && Rel.d.a > 0) {
1138 DataRefImpl RelPrev = Rel;
1139 RelPrev.d.a--;
1140 uint64_t PrevType;
1141 getRelocationType(RelPrev, PrevType);
1142 if (PrevType == macho::RIT_X86_64_Subtractor)
1143 Result = true;
1144 }
1145 }
1146
1147 return object_error::success;
1148}
1149
1150error_code MachOObjectFile::getLibraryNext(DataRefImpl LibData,
Rafael Espindolaf69a81f2013-04-24 15:14:22 +00001151 LibraryRef &Res) const {
Rafael Espindolafd7aa382013-04-18 18:08:55 +00001152 report_fatal_error("Needed libraries unimplemented in MachOObjectFile");
1153}
1154
1155error_code MachOObjectFile::getLibraryPath(DataRefImpl LibData,
Rafael Espindolaf69a81f2013-04-24 15:14:22 +00001156 StringRef &Res) const {
Rafael Espindolafd7aa382013-04-18 18:08:55 +00001157 report_fatal_error("Needed libraries unimplemented in MachOObjectFile");
1158}
1159
1160symbol_iterator MachOObjectFile::begin_symbols() const {
Rafael Espindolafd7aa382013-04-18 18:08:55 +00001161 DataRefImpl DRI;
Rafael Espindola802fe932013-04-24 19:47:55 +00001162 if (!SymtabLoadCmd)
1163 return symbol_iterator(SymbolRef(DRI, this));
1164
1165 macho::SymtabLoadCommand Symtab = getSymtabLoadCommand();
1166 DRI.p = reinterpret_cast<uintptr_t>(getPtr(this, Symtab.SymbolTableOffset));
Rafael Espindolafd7aa382013-04-18 18:08:55 +00001167 return symbol_iterator(SymbolRef(DRI, this));
1168}
1169
1170symbol_iterator MachOObjectFile::end_symbols() const {
1171 DataRefImpl DRI;
Rafael Espindola802fe932013-04-24 19:47:55 +00001172 if (!SymtabLoadCmd)
1173 return symbol_iterator(SymbolRef(DRI, this));
1174
1175 macho::SymtabLoadCommand Symtab = getSymtabLoadCommand();
1176 unsigned SymbolTableEntrySize = is64Bit() ?
1177 sizeof(macho::Symbol64TableEntry) :
1178 sizeof(macho::SymbolTableEntry);
1179 unsigned Offset = Symtab.SymbolTableOffset +
1180 Symtab.NumSymbolTableEntries * SymbolTableEntrySize;
1181 DRI.p = reinterpret_cast<uintptr_t>(getPtr(this, Offset));
Rafael Espindolafd7aa382013-04-18 18:08:55 +00001182 return symbol_iterator(SymbolRef(DRI, this));
1183}
1184
1185symbol_iterator MachOObjectFile::begin_dynamic_symbols() const {
1186 // TODO: implement
1187 report_fatal_error("Dynamic symbols unimplemented in MachOObjectFile");
1188}
1189
1190symbol_iterator MachOObjectFile::end_dynamic_symbols() const {
1191 // TODO: implement
1192 report_fatal_error("Dynamic symbols unimplemented in MachOObjectFile");
1193}
1194
1195section_iterator MachOObjectFile::begin_sections() const {
1196 DataRefImpl DRI;
1197 return section_iterator(SectionRef(DRI, this));
1198}
1199
1200section_iterator MachOObjectFile::end_sections() const {
1201 DataRefImpl DRI;
1202 DRI.d.a = Sections.size();
1203 return section_iterator(SectionRef(DRI, this));
1204}
1205
1206library_iterator MachOObjectFile::begin_libraries_needed() const {
1207 // TODO: implement
1208 report_fatal_error("Needed libraries unimplemented in MachOObjectFile");
1209}
1210
1211library_iterator MachOObjectFile::end_libraries_needed() const {
1212 // TODO: implement
1213 report_fatal_error("Needed libraries unimplemented in MachOObjectFile");
1214}
1215
1216uint8_t MachOObjectFile::getBytesInAddress() const {
Rafael Espindola0f08eb12013-04-07 19:05:30 +00001217 return is64Bit() ? 8 : 4;
Eric Christopher6256b032011-04-22 03:19:48 +00001218}
1219
Rafael Espindolafd7aa382013-04-18 18:08:55 +00001220StringRef MachOObjectFile::getFileFormatName() const {
1221 unsigned CPUType = getCPUType(this);
1222 if (!is64Bit()) {
1223 switch (CPUType) {
1224 case llvm::MachO::CPUTypeI386:
1225 return "Mach-O 32-bit i386";
1226 case llvm::MachO::CPUTypeARM:
1227 return "Mach-O arm";
1228 case llvm::MachO::CPUTypePowerPC:
1229 return "Mach-O 32-bit ppc";
1230 default:
1231 assert((CPUType & llvm::MachO::CPUArchABI64) == 0 &&
1232 "64-bit object file when we're not 64-bit?");
1233 return "Mach-O 32-bit unknown";
1234 }
1235 }
1236
1237 // Make sure the cpu type has the correct mask.
1238 assert((CPUType & llvm::MachO::CPUArchABI64)
1239 == llvm::MachO::CPUArchABI64 &&
1240 "32-bit object file when we're 64-bit?");
1241
1242 switch (CPUType) {
1243 case llvm::MachO::CPUTypeX86_64:
1244 return "Mach-O 64-bit x86-64";
1245 case llvm::MachO::CPUTypePowerPC64:
1246 return "Mach-O 64-bit ppc64";
1247 default:
1248 return "Mach-O 64-bit unknown";
1249 }
1250}
1251
1252unsigned MachOObjectFile::getArch() const {
1253 switch (getCPUType(this)) {
1254 case llvm::MachO::CPUTypeI386:
1255 return Triple::x86;
1256 case llvm::MachO::CPUTypeX86_64:
1257 return Triple::x86_64;
1258 case llvm::MachO::CPUTypeARM:
1259 return Triple::arm;
1260 case llvm::MachO::CPUTypePowerPC:
1261 return Triple::ppc;
1262 case llvm::MachO::CPUTypePowerPC64:
1263 return Triple::ppc64;
1264 default:
1265 return Triple::UnknownArch;
1266 }
1267}
1268
1269StringRef MachOObjectFile::getLoadName() const {
1270 // TODO: Implement
1271 report_fatal_error("get_load_name() unimplemented in MachOObjectFile");
1272}
1273
1274StringRef
1275MachOObjectFile::getSectionFinalSegmentName(DataRefImpl Sec) const {
1276 ArrayRef<char> Raw = getSectionRawFinalSegmentName(Sec);
1277 return parseSegmentOrSectionName(Raw.data());
1278}
1279
1280ArrayRef<char>
1281MachOObjectFile::getSectionRawName(DataRefImpl Sec) const {
1282 const SectionBase *Base =
1283 reinterpret_cast<const SectionBase*>(Sections[Sec.d.a]);
1284 return ArrayRef<char>(Base->Name);
1285}
1286
1287ArrayRef<char>
1288MachOObjectFile::getSectionRawFinalSegmentName(DataRefImpl Sec) const {
1289 const SectionBase *Base =
1290 reinterpret_cast<const SectionBase*>(Sections[Sec.d.a]);
1291 return ArrayRef<char>(Base->SegmentName);
1292}
1293
1294bool
1295MachOObjectFile::isRelocationScattered(const macho::RelocationEntry &RE)
1296 const {
1297 if (getCPUType(this) == llvm::MachO::CPUTypeX86_64)
1298 return false;
1299 return getPlainRelocationAddress(RE) & macho::RF_Scattered;
1300}
1301
1302unsigned MachOObjectFile::getPlainRelocationSymbolNum(const macho::RelocationEntry &RE) const {
1303 if (isLittleEndian())
1304 return RE.Word1 & 0xffffff;
1305 return RE.Word1 >> 8;
1306}
1307
1308bool MachOObjectFile::getPlainRelocationExternal(const macho::RelocationEntry &RE) const {
1309 if (isLittleEndian())
1310 return (RE.Word1 >> 27) & 1;
1311 return (RE.Word1 >> 4) & 1;
1312}
1313
1314bool
1315MachOObjectFile::getScatteredRelocationScattered(const macho::RelocationEntry &RE) const {
1316 return RE.Word0 >> 31;
1317}
1318
1319uint32_t
1320MachOObjectFile::getScatteredRelocationValue(const macho::RelocationEntry &RE) const {
1321 return RE.Word1;
1322}
1323
1324unsigned
1325MachOObjectFile::getAnyRelocationAddress(const macho::RelocationEntry &RE) const {
1326 if (isRelocationScattered(RE))
1327 return getScatteredRelocationAddress(RE);
1328 return getPlainRelocationAddress(RE);
1329}
1330
1331unsigned
1332MachOObjectFile::getAnyRelocationPCRel(const macho::RelocationEntry &RE) const {
1333 if (isRelocationScattered(RE))
1334 return getScatteredRelocationPCRel(this, RE);
1335 return getPlainRelocationPCRel(this, RE);
1336}
1337
1338unsigned
1339MachOObjectFile::getAnyRelocationLength(const macho::RelocationEntry &RE) const {
1340 if (isRelocationScattered(RE))
1341 return getScatteredRelocationLength(RE);
1342 return getPlainRelocationLength(this, RE);
1343}
1344
1345unsigned
1346MachOObjectFile::getAnyRelocationType(const macho::RelocationEntry &RE) const {
1347 if (isRelocationScattered(RE))
1348 return getScatteredRelocationType(RE);
1349 return getPlainRelocationType(this, RE);
1350}
1351
1352MachOObjectFile::LoadCommandInfo
1353MachOObjectFile::getFirstLoadCommandInfo() const {
1354 MachOObjectFile::LoadCommandInfo Load;
1355
1356 unsigned HeaderSize = is64Bit() ? macho::Header64Size : macho::Header32Size;
1357 Load.Ptr = getPtr(this, HeaderSize);
Rafael Espindola143d2232013-04-19 13:45:05 +00001358 Load.C = getStruct<macho::LoadCommand>(this, Load.Ptr);
Rafael Espindolafd7aa382013-04-18 18:08:55 +00001359 return Load;
1360}
1361
1362MachOObjectFile::LoadCommandInfo
1363MachOObjectFile::getNextLoadCommandInfo(const LoadCommandInfo &L) const {
1364 MachOObjectFile::LoadCommandInfo Next;
1365 Next.Ptr = L.Ptr + L.C.Size;
Rafael Espindola143d2232013-04-19 13:45:05 +00001366 Next.C = getStruct<macho::LoadCommand>(this, Next.Ptr);
Rafael Espindolafd7aa382013-04-18 18:08:55 +00001367 return Next;
1368}
1369
1370macho::Section MachOObjectFile::getSection(DataRefImpl DRI) const {
Rafael Espindola143d2232013-04-19 13:45:05 +00001371 return getStruct<macho::Section>(this, Sections[DRI.d.a]);
Rafael Espindolafd7aa382013-04-18 18:08:55 +00001372}
1373
1374macho::Section64 MachOObjectFile::getSection64(DataRefImpl DRI) const {
Rafael Espindola143d2232013-04-19 13:45:05 +00001375 return getStruct<macho::Section64>(this, Sections[DRI.d.a]);
Rafael Espindolafd7aa382013-04-18 18:08:55 +00001376}
1377
1378macho::SymbolTableEntry
1379MachOObjectFile::getSymbolTableEntry(DataRefImpl DRI) const {
Rafael Espindola802fe932013-04-24 19:47:55 +00001380 const char *P = reinterpret_cast<const char *>(DRI.p);
Rafael Espindola143d2232013-04-19 13:45:05 +00001381 return getStruct<macho::SymbolTableEntry>(this, P);
Rafael Espindolafd7aa382013-04-18 18:08:55 +00001382}
1383
1384macho::Symbol64TableEntry
1385MachOObjectFile::getSymbol64TableEntry(DataRefImpl DRI) const {
Rafael Espindola802fe932013-04-24 19:47:55 +00001386 const char *P = reinterpret_cast<const char *>(DRI.p);
Rafael Espindola143d2232013-04-19 13:45:05 +00001387 return getStruct<macho::Symbol64TableEntry>(this, P);
Rafael Espindolafd7aa382013-04-18 18:08:55 +00001388}
1389
1390macho::LinkeditDataLoadCommand
1391MachOObjectFile::getLinkeditDataLoadCommand(const MachOObjectFile::LoadCommandInfo &L) const {
Rafael Espindola143d2232013-04-19 13:45:05 +00001392 return getStruct<macho::LinkeditDataLoadCommand>(this, L.Ptr);
Rafael Espindolafd7aa382013-04-18 18:08:55 +00001393}
1394
1395macho::RelocationEntry
1396MachOObjectFile::getRelocation(DataRefImpl Rel) const {
Rafael Espindolae5330f72013-04-25 12:45:46 +00001397 const char *P = reinterpret_cast<const char *>(Rel.p);
1398 return getStruct<macho::RelocationEntry>(this, P);
Rafael Espindolafd7aa382013-04-18 18:08:55 +00001399}
1400
1401macho::Header MachOObjectFile::getHeader() const {
Rafael Espindola143d2232013-04-19 13:45:05 +00001402 return getStruct<macho::Header>(this, getPtr(this, 0));
Rafael Espindolafd7aa382013-04-18 18:08:55 +00001403}
1404
1405macho::SymtabLoadCommand
1406MachOObjectFile::getSymtabLoadCommand() const {
Rafael Espindola143d2232013-04-19 13:45:05 +00001407 return getStruct<macho::SymtabLoadCommand>(this, SymtabLoadCmd);
Rafael Espindolafd7aa382013-04-18 18:08:55 +00001408}
1409
1410bool MachOObjectFile::is64Bit() const {
1411 return getType() == getMachOType(false, true) ||
1412 getType() == getMachOType(true, true);
1413}
1414
1415void MachOObjectFile::ReadULEB128s(uint64_t Index,
1416 SmallVectorImpl<uint64_t> &Out) const {
1417 DataExtractor extractor(ObjectFile::getData(), true, 0);
1418
1419 uint32_t offset = Index;
1420 uint64_t data = 0;
1421 while (uint64_t delta = extractor.getULEB128(&offset)) {
1422 data += delta;
1423 Out.push_back(data);
1424 }
1425}
1426
1427ObjectFile *ObjectFile::createMachOObjectFile(MemoryBuffer *Buffer) {
1428 StringRef Magic = Buffer->getBuffer().slice(0, 4);
1429 error_code ec;
1430 ObjectFile *Ret;
1431 if (Magic == "\xFE\xED\xFA\xCE")
1432 Ret = new MachOObjectFile(Buffer, false, false, ec);
1433 else if (Magic == "\xCE\xFA\xED\xFE")
1434 Ret = new MachOObjectFile(Buffer, true, false, ec);
1435 else if (Magic == "\xFE\xED\xFA\xCF")
1436 Ret = new MachOObjectFile(Buffer, false, true, ec);
1437 else if (Magic == "\xCF\xFA\xED\xFE")
1438 Ret = new MachOObjectFile(Buffer, true, true, ec);
1439 else
1440 return NULL;
1441
1442 if (ec)
1443 return NULL;
1444 return Ret;
1445}
1446
Owen Andersonf7c93a32011-10-11 17:32:27 +00001447} // end namespace object
Eric Christopher6256b032011-04-22 03:19:48 +00001448} // end namespace llvm