1 //===- MachOObjectFile.cpp - Mach-O object file binding ---------*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This file defines the MachOObjectFile class, which binds the MachOObject
11 // class to the generic ObjectFile wrapper.
13 //===----------------------------------------------------------------------===//
15 #include "llvm/ADT/Triple.h"
16 #include "llvm/Object/MachOFormat.h"
17 #include "llvm/Object/MachOObject.h"
18 #include "llvm/Object/ObjectFile.h"
19 #include "llvm/Support/MemoryBuffer.h"
20 #include "llvm/Support/MachO.h"
21 #include "llvm/ADT/SmallVector.h"
28 using namespace object;
32 typedef MachOObject::LoadCommandInfo LoadCommandInfo;
34 class MachOObjectFile : public ObjectFile {
36 MachOObjectFile(MemoryBuffer *Object, MachOObject *MOO, error_code &ec);
38 virtual symbol_iterator begin_symbols() const;
39 virtual symbol_iterator end_symbols() const;
40 virtual section_iterator begin_sections() const;
41 virtual section_iterator end_sections() const;
43 virtual uint8_t getBytesInAddress() const;
44 virtual StringRef getFileFormatName() const;
45 virtual unsigned getArch() const;
48 virtual error_code getSymbolNext(DataRefImpl Symb, SymbolRef &Res) const;
49 virtual error_code getSymbolName(DataRefImpl Symb, StringRef &Res) const;
50 virtual error_code getSymbolOffset(DataRefImpl Symb, uint64_t &Res) const;
51 virtual error_code getSymbolAddress(DataRefImpl Symb, uint64_t &Res) const;
52 virtual error_code getSymbolSize(DataRefImpl Symb, uint64_t &Res) const;
53 virtual error_code getSymbolNMTypeChar(DataRefImpl Symb, char &Res) const;
54 virtual error_code isSymbolInternal(DataRefImpl Symb, bool &Res) const;
55 virtual error_code isSymbolGlobal(DataRefImpl Symb, bool &Res) const;
56 virtual error_code getSymbolType(DataRefImpl Symb, SymbolRef::SymbolType &Res) const;
58 virtual error_code getSectionNext(DataRefImpl Sec, SectionRef &Res) const;
59 virtual error_code getSectionName(DataRefImpl Sec, StringRef &Res) const;
60 virtual error_code getSectionAddress(DataRefImpl Sec, uint64_t &Res) const;
61 virtual error_code getSectionSize(DataRefImpl Sec, uint64_t &Res) const;
62 virtual error_code getSectionContents(DataRefImpl Sec, StringRef &Res) const;
63 virtual error_code getSectionAlignment(DataRefImpl Sec, uint64_t &Res) const;
64 virtual error_code isSectionText(DataRefImpl Sec, bool &Res) const;
65 virtual error_code isSectionData(DataRefImpl Sec, bool &Res) const;
66 virtual error_code isSectionBSS(DataRefImpl Sec, bool &Res) const;
67 virtual error_code sectionContainsSymbol(DataRefImpl DRI, DataRefImpl S,
69 virtual relocation_iterator getSectionRelBegin(DataRefImpl Sec) const;
70 virtual relocation_iterator getSectionRelEnd(DataRefImpl Sec) const;
72 virtual error_code getRelocationNext(DataRefImpl Rel,
73 RelocationRef &Res) const;
74 virtual error_code getRelocationAddress(DataRefImpl Rel,
76 virtual error_code getRelocationSymbol(DataRefImpl Rel,
77 SymbolRef &Res) const;
78 virtual error_code getRelocationType(DataRefImpl Rel,
80 virtual error_code getRelocationTypeName(DataRefImpl Rel,
81 SmallVectorImpl<char> &Result) const;
82 virtual error_code getRelocationAdditionalInfo(DataRefImpl Rel,
84 virtual error_code getRelocationValueString(DataRefImpl Rel,
85 SmallVectorImpl<char> &Result) const;
88 MachOObject *MachOObj;
89 mutable uint32_t RegisteredStringTable;
90 typedef SmallVector<DataRefImpl, 1> SectionList;
94 void moveToNextSection(DataRefImpl &DRI) const;
95 void getSymbolTableEntry(DataRefImpl DRI,
96 InMemoryStruct<macho::SymbolTableEntry> &Res) const;
97 void getSymbol64TableEntry(DataRefImpl DRI,
98 InMemoryStruct<macho::Symbol64TableEntry> &Res) const;
99 void moveToNextSymbol(DataRefImpl &DRI) const;
100 void getSection(DataRefImpl DRI, InMemoryStruct<macho::Section> &Res) const;
101 void getSection64(DataRefImpl DRI,
102 InMemoryStruct<macho::Section64> &Res) const;
103 void getRelocation(DataRefImpl Rel,
104 InMemoryStruct<macho::RelocationEntry> &Res) const;
105 std::size_t getSectionIndex(DataRefImpl Sec) const;
108 MachOObjectFile::MachOObjectFile(MemoryBuffer *Object, MachOObject *MOO,
110 : ObjectFile(Binary::isMachO, Object, ec),
112 RegisteredStringTable(std::numeric_limits<uint32_t>::max()) {
114 DRI.d.a = DRI.d.b = 0;
115 moveToNextSection(DRI);
116 uint32_t LoadCommandCount = MachOObj->getHeader().NumLoadCommands;
117 while (DRI.d.a < LoadCommandCount) {
118 Sections.push_back(DRI);
120 moveToNextSection(DRI);
125 ObjectFile *ObjectFile::createMachOObjectFile(MemoryBuffer *Buffer) {
128 MachOObject *MachOObj = MachOObject::LoadFromBuffer(Buffer, &Err);
131 return new MachOObjectFile(Buffer, MachOObj, ec);
134 /*===-- Symbols -----------------------------------------------------------===*/
136 void MachOObjectFile::moveToNextSymbol(DataRefImpl &DRI) const {
137 uint32_t LoadCommandCount = MachOObj->getHeader().NumLoadCommands;
138 while (DRI.d.a < LoadCommandCount) {
139 LoadCommandInfo LCI = MachOObj->getLoadCommandInfo(DRI.d.a);
140 if (LCI.Command.Type == macho::LCT_Symtab) {
141 InMemoryStruct<macho::SymtabLoadCommand> SymtabLoadCmd;
142 MachOObj->ReadSymtabLoadCommand(LCI, SymtabLoadCmd);
143 if (DRI.d.b < SymtabLoadCmd->NumSymbolTableEntries)
152 void MachOObjectFile::getSymbolTableEntry(DataRefImpl DRI,
153 InMemoryStruct<macho::SymbolTableEntry> &Res) const {
154 InMemoryStruct<macho::SymtabLoadCommand> SymtabLoadCmd;
155 LoadCommandInfo LCI = MachOObj->getLoadCommandInfo(DRI.d.a);
156 MachOObj->ReadSymtabLoadCommand(LCI, SymtabLoadCmd);
158 if (RegisteredStringTable != DRI.d.a) {
159 MachOObj->RegisterStringTable(*SymtabLoadCmd);
160 RegisteredStringTable = DRI.d.a;
163 MachOObj->ReadSymbolTableEntry(SymtabLoadCmd->SymbolTableOffset, DRI.d.b,
167 void MachOObjectFile::getSymbol64TableEntry(DataRefImpl DRI,
168 InMemoryStruct<macho::Symbol64TableEntry> &Res) const {
169 InMemoryStruct<macho::SymtabLoadCommand> SymtabLoadCmd;
170 LoadCommandInfo LCI = MachOObj->getLoadCommandInfo(DRI.d.a);
171 MachOObj->ReadSymtabLoadCommand(LCI, SymtabLoadCmd);
173 if (RegisteredStringTable != DRI.d.a) {
174 MachOObj->RegisterStringTable(*SymtabLoadCmd);
175 RegisteredStringTable = DRI.d.a;
178 MachOObj->ReadSymbol64TableEntry(SymtabLoadCmd->SymbolTableOffset, DRI.d.b,
183 error_code MachOObjectFile::getSymbolNext(DataRefImpl DRI,
184 SymbolRef &Result) const {
186 moveToNextSymbol(DRI);
187 Result = SymbolRef(DRI, this);
188 return object_error::success;
191 error_code MachOObjectFile::getSymbolName(DataRefImpl DRI,
192 StringRef &Result) const {
193 if (MachOObj->is64Bit()) {
194 InMemoryStruct<macho::Symbol64TableEntry> Entry;
195 getSymbol64TableEntry(DRI, Entry);
196 Result = MachOObj->getStringAtIndex(Entry->StringIndex);
198 InMemoryStruct<macho::SymbolTableEntry> Entry;
199 getSymbolTableEntry(DRI, Entry);
200 Result = MachOObj->getStringAtIndex(Entry->StringIndex);
202 return object_error::success;
205 error_code MachOObjectFile::getSymbolOffset(DataRefImpl DRI,
206 uint64_t &Result) const {
207 if (MachOObj->is64Bit()) {
208 InMemoryStruct<macho::Symbol64TableEntry> Entry;
209 getSymbol64TableEntry(DRI, Entry);
210 Result = Entry->Value;
212 InMemoryStruct<macho::SymbolTableEntry> Entry;
213 getSymbolTableEntry(DRI, Entry);
214 Result = Entry->Value;
216 return object_error::success;
219 error_code MachOObjectFile::getSymbolAddress(DataRefImpl DRI,
220 uint64_t &Result) const {
221 uint64_t SymbolOffset;
222 uint8_t SectionIndex;
223 if (MachOObj->is64Bit()) {
224 InMemoryStruct<macho::Symbol64TableEntry> Entry;
225 getSymbol64TableEntry(DRI, Entry);
226 SymbolOffset = Entry->Value;
227 SectionIndex = Entry->SectionIndex;
229 InMemoryStruct<macho::SymbolTableEntry> Entry;
230 getSymbolTableEntry(DRI, Entry);
231 SymbolOffset = Entry->Value;
232 SectionIndex = Entry->SectionIndex;
234 getSectionAddress(Sections[SectionIndex], Result);
235 Result += SymbolOffset;
237 return object_error::success;
240 error_code MachOObjectFile::getSymbolSize(DataRefImpl DRI,
241 uint64_t &Result) const {
242 Result = UnknownAddressOrSize;
243 return object_error::success;
246 error_code MachOObjectFile::getSymbolNMTypeChar(DataRefImpl DRI,
247 char &Result) const {
249 if (MachOObj->is64Bit()) {
250 InMemoryStruct<macho::Symbol64TableEntry> Entry;
251 getSymbol64TableEntry(DRI, Entry);
253 Flags = Entry->Flags;
255 InMemoryStruct<macho::SymbolTableEntry> Entry;
256 getSymbolTableEntry(DRI, Entry);
258 Flags = Entry->Flags;
262 switch (Type & macho::STF_TypeMask) {
263 case macho::STT_Undefined:
266 case macho::STT_Absolute:
267 case macho::STT_Section:
275 if (Flags & (macho::STF_External | macho::STF_PrivateExtern))
276 Char = toupper(Char);
278 return object_error::success;
281 error_code MachOObjectFile::isSymbolInternal(DataRefImpl DRI,
282 bool &Result) const {
283 if (MachOObj->is64Bit()) {
284 InMemoryStruct<macho::Symbol64TableEntry> Entry;
285 getSymbol64TableEntry(DRI, Entry);
286 Result = Entry->Flags & macho::STF_StabsEntryMask;
288 InMemoryStruct<macho::SymbolTableEntry> Entry;
289 getSymbolTableEntry(DRI, Entry);
290 Result = Entry->Flags & macho::STF_StabsEntryMask;
292 return object_error::success;
295 error_code MachOObjectFile::isSymbolGlobal(DataRefImpl Symb, bool &Res) const {
297 if (MachOObj->is64Bit()) {
298 InMemoryStruct<macho::Symbol64TableEntry> Entry;
299 getSymbol64TableEntry(Symb, Entry);
300 Res = Entry->Type & MachO::NlistMaskExternal;
302 InMemoryStruct<macho::SymbolTableEntry> Entry;
303 getSymbolTableEntry(Symb, Entry);
304 Res = Entry->Type & MachO::NlistMaskExternal;
306 return object_error::success;
309 error_code MachOObjectFile::getSymbolType(DataRefImpl Symb,
310 SymbolRef::SymbolType &Res) const {
312 if (MachOObj->is64Bit()) {
313 InMemoryStruct<macho::Symbol64TableEntry> Entry;
314 getSymbol64TableEntry(Symb, Entry);
315 n_type = Entry->Type;
317 InMemoryStruct<macho::SymbolTableEntry> Entry;
318 getSymbolTableEntry(Symb, Entry);
319 n_type = Entry->Type;
321 Res = SymbolRef::ST_Other;
322 switch (n_type & MachO::NlistMaskType) {
323 case MachO::NListTypeUndefined :
324 Res = SymbolRef::ST_External;
326 case MachO::NListTypeSection :
327 Res = SymbolRef::ST_Function;
330 return object_error::success;
334 symbol_iterator MachOObjectFile::begin_symbols() const {
335 // DRI.d.a = segment number; DRI.d.b = symbol index.
337 DRI.d.a = DRI.d.b = 0;
338 moveToNextSymbol(DRI);
339 return symbol_iterator(SymbolRef(DRI, this));
342 symbol_iterator MachOObjectFile::end_symbols() const {
344 DRI.d.a = MachOObj->getHeader().NumLoadCommands;
346 return symbol_iterator(SymbolRef(DRI, this));
350 /*===-- Sections ----------------------------------------------------------===*/
352 void MachOObjectFile::moveToNextSection(DataRefImpl &DRI) const {
353 uint32_t LoadCommandCount = MachOObj->getHeader().NumLoadCommands;
354 while (DRI.d.a < LoadCommandCount) {
355 LoadCommandInfo LCI = MachOObj->getLoadCommandInfo(DRI.d.a);
356 if (LCI.Command.Type == macho::LCT_Segment) {
357 InMemoryStruct<macho::SegmentLoadCommand> SegmentLoadCmd;
358 MachOObj->ReadSegmentLoadCommand(LCI, SegmentLoadCmd);
359 if (DRI.d.b < SegmentLoadCmd->NumSections)
361 } else if (LCI.Command.Type == macho::LCT_Segment64) {
362 InMemoryStruct<macho::Segment64LoadCommand> Segment64LoadCmd;
363 MachOObj->ReadSegment64LoadCommand(LCI, Segment64LoadCmd);
364 if (DRI.d.b < Segment64LoadCmd->NumSections)
373 error_code MachOObjectFile::getSectionNext(DataRefImpl DRI,
374 SectionRef &Result) const {
376 moveToNextSection(DRI);
377 Result = SectionRef(DRI, this);
378 return object_error::success;
382 MachOObjectFile::getSection(DataRefImpl DRI,
383 InMemoryStruct<macho::Section> &Res) const {
384 InMemoryStruct<macho::SegmentLoadCommand> SLC;
385 LoadCommandInfo LCI = MachOObj->getLoadCommandInfo(DRI.d.a);
386 MachOObj->ReadSegmentLoadCommand(LCI, SLC);
387 MachOObj->ReadSection(LCI, DRI.d.b, Res);
390 std::size_t MachOObjectFile::getSectionIndex(DataRefImpl Sec) const {
391 SectionList::const_iterator loc =
392 std::find(Sections.begin(), Sections.end(), Sec);
393 assert(loc != Sections.end() && "Sec is not a valid section!");
394 return std::distance(Sections.begin(), loc);
398 MachOObjectFile::getSection64(DataRefImpl DRI,
399 InMemoryStruct<macho::Section64> &Res) const {
400 InMemoryStruct<macho::Segment64LoadCommand> SLC;
401 LoadCommandInfo LCI = MachOObj->getLoadCommandInfo(DRI.d.a);
402 MachOObj->ReadSegment64LoadCommand(LCI, SLC);
403 MachOObj->ReadSection64(LCI, DRI.d.b, Res);
406 static bool is64BitLoadCommand(const MachOObject *MachOObj, DataRefImpl DRI) {
407 LoadCommandInfo LCI = MachOObj->getLoadCommandInfo(DRI.d.a);
408 if (LCI.Command.Type == macho::LCT_Segment64)
410 assert(LCI.Command.Type == macho::LCT_Segment && "Unexpected Type.");
414 error_code MachOObjectFile::getSectionName(DataRefImpl DRI,
415 StringRef &Result) const {
416 // FIXME: thread safety.
417 static char result[34];
418 if (is64BitLoadCommand(MachOObj, DRI)) {
419 InMemoryStruct<macho::Segment64LoadCommand> SLC;
420 LoadCommandInfo LCI = MachOObj->getLoadCommandInfo(DRI.d.a);
421 MachOObj->ReadSegment64LoadCommand(LCI, SLC);
422 InMemoryStruct<macho::Section64> Sect;
423 MachOObj->ReadSection64(LCI, DRI.d.b, Sect);
425 strcpy(result, Sect->SegmentName);
427 strcat(result, Sect->Name);
429 InMemoryStruct<macho::SegmentLoadCommand> SLC;
430 LoadCommandInfo LCI = MachOObj->getLoadCommandInfo(DRI.d.a);
431 MachOObj->ReadSegmentLoadCommand(LCI, SLC);
432 InMemoryStruct<macho::Section> Sect;
433 MachOObj->ReadSection(LCI, DRI.d.b, Sect);
435 strcpy(result, Sect->SegmentName);
437 strcat(result, Sect->Name);
439 Result = StringRef(result);
440 return object_error::success;
443 error_code MachOObjectFile::getSectionAddress(DataRefImpl DRI,
444 uint64_t &Result) const {
445 if (is64BitLoadCommand(MachOObj, DRI)) {
446 InMemoryStruct<macho::Section64> Sect;
447 getSection64(DRI, Sect);
448 Result = Sect->Address;
450 InMemoryStruct<macho::Section> Sect;
451 getSection(DRI, Sect);
452 Result = Sect->Address;
454 return object_error::success;
457 error_code MachOObjectFile::getSectionSize(DataRefImpl DRI,
458 uint64_t &Result) const {
459 if (is64BitLoadCommand(MachOObj, DRI)) {
460 InMemoryStruct<macho::Section64> Sect;
461 getSection64(DRI, Sect);
464 InMemoryStruct<macho::Section> Sect;
465 getSection(DRI, Sect);
468 return object_error::success;
471 error_code MachOObjectFile::getSectionContents(DataRefImpl DRI,
472 StringRef &Result) const {
473 if (is64BitLoadCommand(MachOObj, DRI)) {
474 InMemoryStruct<macho::Section64> Sect;
475 getSection64(DRI, Sect);
476 Result = MachOObj->getData(Sect->Offset, Sect->Size);
478 InMemoryStruct<macho::Section> Sect;
479 getSection(DRI, Sect);
480 Result = MachOObj->getData(Sect->Offset, Sect->Size);
482 return object_error::success;
485 error_code MachOObjectFile::getSectionAlignment(DataRefImpl DRI,
486 uint64_t &Result) const {
487 if (is64BitLoadCommand(MachOObj, DRI)) {
488 InMemoryStruct<macho::Section64> Sect;
489 getSection64(DRI, Sect);
490 Result = uint64_t(1) << Sect->Align;
492 InMemoryStruct<macho::Section> Sect;
493 getSection(DRI, Sect);
494 Result = uint64_t(1) << Sect->Align;
496 return object_error::success;
499 error_code MachOObjectFile::isSectionText(DataRefImpl DRI,
500 bool &Result) const {
501 if (is64BitLoadCommand(MachOObj, DRI)) {
502 InMemoryStruct<macho::Section64> Sect;
503 getSection64(DRI, Sect);
504 Result = !strcmp(Sect->Name, "__text");
506 InMemoryStruct<macho::Section> Sect;
507 getSection(DRI, Sect);
508 Result = !strcmp(Sect->Name, "__text");
510 return object_error::success;
513 error_code MachOObjectFile::isSectionData(DataRefImpl DRI,
514 bool &Result) const {
515 // FIXME: Unimplemented.
517 return object_error::success;
520 error_code MachOObjectFile::isSectionBSS(DataRefImpl DRI,
521 bool &Result) const {
522 // FIXME: Unimplemented.
524 return object_error::success;
527 error_code MachOObjectFile::sectionContainsSymbol(DataRefImpl Sec,
529 bool &Result) const {
530 if (MachOObj->is64Bit()) {
531 InMemoryStruct<macho::Symbol64TableEntry> Entry;
532 getSymbol64TableEntry(Symb, Entry);
533 Result = Entry->SectionIndex == 1 + Sec.d.a + Sec.d.b;
535 InMemoryStruct<macho::SymbolTableEntry> Entry;
536 getSymbolTableEntry(Symb, Entry);
537 Result = Entry->SectionIndex == 1 + Sec.d.a + Sec.d.b;
539 return object_error::success;
542 relocation_iterator MachOObjectFile::getSectionRelBegin(DataRefImpl Sec) const {
545 ret.d.b = getSectionIndex(Sec);
546 return relocation_iterator(RelocationRef(ret, this));
548 relocation_iterator MachOObjectFile::getSectionRelEnd(DataRefImpl Sec) const {
550 if (is64BitLoadCommand(MachOObj, Sec)) {
551 InMemoryStruct<macho::Section64> Sect;
552 getSection64(Sec, Sect);
553 last_reloc = Sect->NumRelocationTableEntries;
555 InMemoryStruct<macho::Section> Sect;
556 getSection(Sec, Sect);
557 last_reloc = Sect->NumRelocationTableEntries;
560 ret.d.a = last_reloc;
561 ret.d.b = getSectionIndex(Sec);
562 return relocation_iterator(RelocationRef(ret, this));
565 section_iterator MachOObjectFile::begin_sections() const {
567 DRI.d.a = DRI.d.b = 0;
568 moveToNextSection(DRI);
569 return section_iterator(SectionRef(DRI, this));
572 section_iterator MachOObjectFile::end_sections() const {
574 DRI.d.a = MachOObj->getHeader().NumLoadCommands;
576 return section_iterator(SectionRef(DRI, this));
579 /*===-- Relocations -------------------------------------------------------===*/
581 void MachOObjectFile::
582 getRelocation(DataRefImpl Rel,
583 InMemoryStruct<macho::RelocationEntry> &Res) const {
585 if (MachOObj->is64Bit()) {
586 InMemoryStruct<macho::Section64> Sect;
587 getSection64(Sections[Rel.d.b], Sect);
588 relOffset = Sect->RelocationTableOffset;
590 InMemoryStruct<macho::Section> Sect;
591 getSection(Sections[Rel.d.b], Sect);
592 relOffset = Sect->RelocationTableOffset;
594 MachOObj->ReadRelocationEntry(relOffset, Rel.d.a, Res);
596 error_code MachOObjectFile::getRelocationNext(DataRefImpl Rel,
597 RelocationRef &Res) const {
599 Res = RelocationRef(Rel, this);
600 return object_error::success;
602 error_code MachOObjectFile::getRelocationAddress(DataRefImpl Rel,
603 uint64_t &Res) const {
604 const uint8_t* sectAddress = base();
605 if (MachOObj->is64Bit()) {
606 InMemoryStruct<macho::Section64> Sect;
607 getSection64(Sections[Rel.d.b], Sect);
608 sectAddress += Sect->Offset;
610 InMemoryStruct<macho::Section> Sect;
611 getSection(Sections[Rel.d.b], Sect);
612 sectAddress += Sect->Offset;
614 InMemoryStruct<macho::RelocationEntry> RE;
615 getRelocation(Rel, RE);
616 Res = reinterpret_cast<uintptr_t>(sectAddress + RE->Word0);
617 return object_error::success;
619 error_code MachOObjectFile::getRelocationSymbol(DataRefImpl Rel,
620 SymbolRef &Res) const {
621 InMemoryStruct<macho::RelocationEntry> RE;
622 getRelocation(Rel, RE);
623 uint32_t SymbolIdx = RE->Word1 & 0xffffff;
624 bool isExtern = (RE->Word1 >> 27) & 1;
627 Sym.d.a = Sym.d.b = 0;
628 moveToNextSymbol(Sym);
630 for (unsigned i = 0; i < SymbolIdx; i++) {
632 moveToNextSymbol(Sym);
633 assert(Sym.d.a < MachOObj->getHeader().NumLoadCommands &&
634 "Relocation symbol index out of range!");
637 Res = SymbolRef(Sym, this);
638 return object_error::success;
640 error_code MachOObjectFile::getRelocationType(DataRefImpl Rel,
641 uint32_t &Res) const {
642 InMemoryStruct<macho::RelocationEntry> RE;
643 getRelocation(Rel, RE);
645 return object_error::success;
647 error_code MachOObjectFile::getRelocationTypeName(DataRefImpl Rel,
648 SmallVectorImpl<char> &Result) const {
649 return object_error::success;
651 error_code MachOObjectFile::getRelocationAdditionalInfo(DataRefImpl Rel,
652 int64_t &Res) const {
653 InMemoryStruct<macho::RelocationEntry> RE;
654 getRelocation(Rel, RE);
655 bool isExtern = (RE->Word1 >> 27) & 1;
658 const uint8_t* sectAddress = base();
659 if (MachOObj->is64Bit()) {
660 InMemoryStruct<macho::Section64> Sect;
661 getSection64(Sections[Rel.d.b], Sect);
662 sectAddress += Sect->Offset;
664 InMemoryStruct<macho::Section> Sect;
665 getSection(Sections[Rel.d.b], Sect);
666 sectAddress += Sect->Offset;
668 Res = reinterpret_cast<uintptr_t>(sectAddress);
670 return object_error::success;
672 error_code MachOObjectFile::getRelocationValueString(DataRefImpl Rel,
673 SmallVectorImpl<char> &Result) const {
674 return object_error::success;
677 /*===-- Miscellaneous -----------------------------------------------------===*/
679 uint8_t MachOObjectFile::getBytesInAddress() const {
680 return MachOObj->is64Bit() ? 8 : 4;
683 StringRef MachOObjectFile::getFileFormatName() const {
684 if (!MachOObj->is64Bit()) {
685 switch (MachOObj->getHeader().CPUType) {
686 case llvm::MachO::CPUTypeI386:
687 return "Mach-O 32-bit i386";
688 case llvm::MachO::CPUTypeARM:
690 case llvm::MachO::CPUTypePowerPC:
691 return "Mach-O 32-bit ppc";
693 assert((MachOObj->getHeader().CPUType & llvm::MachO::CPUArchABI64) == 0 &&
694 "64-bit object file when we're not 64-bit?");
695 return "Mach-O 32-bit unknown";
699 switch (MachOObj->getHeader().CPUType) {
700 case llvm::MachO::CPUTypeX86_64:
701 return "Mach-O 64-bit x86-64";
702 case llvm::MachO::CPUTypePowerPC64:
703 return "Mach-O 64-bit ppc64";
705 assert((MachOObj->getHeader().CPUType & llvm::MachO::CPUArchABI64) == 1 &&
706 "32-bit object file when we're 64-bit?");
707 return "Mach-O 64-bit unknown";
711 unsigned MachOObjectFile::getArch() const {
712 switch (MachOObj->getHeader().CPUType) {
713 case llvm::MachO::CPUTypeI386:
715 case llvm::MachO::CPUTypeX86_64:
716 return Triple::x86_64;
717 case llvm::MachO::CPUTypeARM:
719 case llvm::MachO::CPUTypePowerPC:
721 case llvm::MachO::CPUTypePowerPC64:
722 return Triple::ppc64;
724 return Triple::UnknownArch;
728 } // end namespace llvm