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/MachO.h"
17 #include "llvm/Object/MachOFormat.h"
18 #include "llvm/Support/Format.h"
19 #include "llvm/Support/MemoryBuffer.h"
26 using namespace object;
31 MachOObjectFile::MachOObjectFile(MemoryBuffer *Object, MachOObject *MOO,
33 : ObjectFile(Binary::isMachO, Object, ec),
35 RegisteredStringTable(std::numeric_limits<uint32_t>::max()) {
37 DRI.d.a = DRI.d.b = 0;
38 moveToNextSection(DRI);
39 uint32_t LoadCommandCount = MachOObj->getHeader().NumLoadCommands;
40 while (DRI.d.a < LoadCommandCount) {
41 Sections.push_back(DRI);
43 moveToNextSection(DRI);
48 ObjectFile *ObjectFile::createMachOObjectFile(MemoryBuffer *Buffer) {
51 MachOObject *MachOObj = MachOObject::LoadFromBuffer(Buffer, &Err);
54 return new MachOObjectFile(Buffer, MachOObj, ec);
57 /*===-- Symbols -----------------------------------------------------------===*/
59 void MachOObjectFile::moveToNextSymbol(DataRefImpl &DRI) const {
60 uint32_t LoadCommandCount = MachOObj->getHeader().NumLoadCommands;
61 while (DRI.d.a < LoadCommandCount) {
62 LoadCommandInfo LCI = MachOObj->getLoadCommandInfo(DRI.d.a);
63 if (LCI.Command.Type == macho::LCT_Symtab) {
64 InMemoryStruct<macho::SymtabLoadCommand> SymtabLoadCmd;
65 MachOObj->ReadSymtabLoadCommand(LCI, SymtabLoadCmd);
66 if (DRI.d.b < SymtabLoadCmd->NumSymbolTableEntries)
75 void MachOObjectFile::getSymbolTableEntry(DataRefImpl DRI,
76 InMemoryStruct<macho::SymbolTableEntry> &Res) const {
77 InMemoryStruct<macho::SymtabLoadCommand> SymtabLoadCmd;
78 LoadCommandInfo LCI = MachOObj->getLoadCommandInfo(DRI.d.a);
79 MachOObj->ReadSymtabLoadCommand(LCI, SymtabLoadCmd);
81 if (RegisteredStringTable != DRI.d.a) {
82 MachOObj->RegisterStringTable(*SymtabLoadCmd);
83 RegisteredStringTable = DRI.d.a;
86 MachOObj->ReadSymbolTableEntry(SymtabLoadCmd->SymbolTableOffset, DRI.d.b,
90 void MachOObjectFile::getSymbol64TableEntry(DataRefImpl DRI,
91 InMemoryStruct<macho::Symbol64TableEntry> &Res) const {
92 InMemoryStruct<macho::SymtabLoadCommand> SymtabLoadCmd;
93 LoadCommandInfo LCI = MachOObj->getLoadCommandInfo(DRI.d.a);
94 MachOObj->ReadSymtabLoadCommand(LCI, SymtabLoadCmd);
96 if (RegisteredStringTable != DRI.d.a) {
97 MachOObj->RegisterStringTable(*SymtabLoadCmd);
98 RegisteredStringTable = DRI.d.a;
101 MachOObj->ReadSymbol64TableEntry(SymtabLoadCmd->SymbolTableOffset, DRI.d.b,
106 error_code MachOObjectFile::getSymbolNext(DataRefImpl DRI,
107 SymbolRef &Result) const {
109 moveToNextSymbol(DRI);
110 Result = SymbolRef(DRI, this);
111 return object_error::success;
114 error_code MachOObjectFile::getSymbolName(DataRefImpl DRI,
115 StringRef &Result) const {
116 if (MachOObj->is64Bit()) {
117 InMemoryStruct<macho::Symbol64TableEntry> Entry;
118 getSymbol64TableEntry(DRI, Entry);
119 Result = MachOObj->getStringAtIndex(Entry->StringIndex);
121 InMemoryStruct<macho::SymbolTableEntry> Entry;
122 getSymbolTableEntry(DRI, Entry);
123 Result = MachOObj->getStringAtIndex(Entry->StringIndex);
125 return object_error::success;
128 error_code MachOObjectFile::getSymbolOffset(DataRefImpl DRI,
129 uint64_t &Result) const {
130 uint64_t SectionOffset;
131 uint8_t SectionIndex;
132 if (MachOObj->is64Bit()) {
133 InMemoryStruct<macho::Symbol64TableEntry> Entry;
134 getSymbol64TableEntry(DRI, Entry);
135 Result = Entry->Value;
136 SectionIndex = Entry->SectionIndex;
138 InMemoryStruct<macho::SymbolTableEntry> Entry;
139 getSymbolTableEntry(DRI, Entry);
140 Result = Entry->Value;
141 SectionIndex = Entry->SectionIndex;
143 getSectionAddress(Sections[SectionIndex-1], SectionOffset);
144 Result -= SectionOffset;
146 return object_error::success;
149 error_code MachOObjectFile::getSymbolAddress(DataRefImpl DRI,
150 uint64_t &Result) const {
151 if (MachOObj->is64Bit()) {
152 InMemoryStruct<macho::Symbol64TableEntry> Entry;
153 getSymbol64TableEntry(DRI, Entry);
154 Result = Entry->Value;
156 InMemoryStruct<macho::SymbolTableEntry> Entry;
157 getSymbolTableEntry(DRI, Entry);
158 Result = Entry->Value;
160 return object_error::success;
163 error_code MachOObjectFile::getSymbolSize(DataRefImpl DRI,
164 uint64_t &Result) const {
165 Result = UnknownAddressOrSize;
166 return object_error::success;
169 error_code MachOObjectFile::getSymbolNMTypeChar(DataRefImpl DRI,
170 char &Result) const {
172 if (MachOObj->is64Bit()) {
173 InMemoryStruct<macho::Symbol64TableEntry> Entry;
174 getSymbol64TableEntry(DRI, Entry);
176 Flags = Entry->Flags;
178 InMemoryStruct<macho::SymbolTableEntry> Entry;
179 getSymbolTableEntry(DRI, Entry);
181 Flags = Entry->Flags;
185 switch (Type & macho::STF_TypeMask) {
186 case macho::STT_Undefined:
189 case macho::STT_Absolute:
190 case macho::STT_Section:
198 if (Flags & (macho::STF_External | macho::STF_PrivateExtern))
199 Char = toupper(Char);
201 return object_error::success;
204 error_code MachOObjectFile::isSymbolInternal(DataRefImpl DRI,
205 bool &Result) const {
206 if (MachOObj->is64Bit()) {
207 InMemoryStruct<macho::Symbol64TableEntry> Entry;
208 getSymbol64TableEntry(DRI, Entry);
209 Result = Entry->Flags & macho::STF_StabsEntryMask;
211 InMemoryStruct<macho::SymbolTableEntry> Entry;
212 getSymbolTableEntry(DRI, Entry);
213 Result = Entry->Flags & macho::STF_StabsEntryMask;
215 return object_error::success;
218 error_code MachOObjectFile::isSymbolGlobal(DataRefImpl Symb, bool &Res) const {
220 if (MachOObj->is64Bit()) {
221 InMemoryStruct<macho::Symbol64TableEntry> Entry;
222 getSymbol64TableEntry(Symb, Entry);
223 Res = Entry->Type & MachO::NlistMaskExternal;
225 InMemoryStruct<macho::SymbolTableEntry> Entry;
226 getSymbolTableEntry(Symb, Entry);
227 Res = Entry->Type & MachO::NlistMaskExternal;
229 return object_error::success;
232 error_code MachOObjectFile::isSymbolWeak(DataRefImpl Symb, bool &Res) const {
234 if (MachOObj->is64Bit()) {
235 InMemoryStruct<macho::Symbol64TableEntry> Entry;
236 getSymbol64TableEntry(Symb, Entry);
237 Res = Entry->Flags & (MachO::NListDescWeakRef | MachO::NListDescWeakDef);
239 InMemoryStruct<macho::SymbolTableEntry> Entry;
240 getSymbolTableEntry(Symb, Entry);
241 Res = Entry->Flags & (MachO::NListDescWeakRef | MachO::NListDescWeakDef);
243 return object_error::success;
246 error_code MachOObjectFile::isSymbolAbsolute(DataRefImpl Symb, bool &Res) const{
248 if (MachOObj->is64Bit()) {
249 InMemoryStruct<macho::Symbol64TableEntry> Entry;
250 getSymbol64TableEntry(Symb, Entry);
251 n_type = Entry->Type;
253 InMemoryStruct<macho::SymbolTableEntry> Entry;
254 getSymbolTableEntry(Symb, Entry);
255 n_type = Entry->Type;
258 Res = (n_type & MachO::NlistMaskType) == MachO::NListTypeAbsolute;
259 return object_error::success;
262 error_code MachOObjectFile::getSymbolSection(DataRefImpl Symb,
263 section_iterator &Res) const {
265 if (MachOObj->is64Bit()) {
266 InMemoryStruct<macho::Symbol64TableEntry> Entry;
267 getSymbol64TableEntry(Symb, Entry);
268 index = Entry->SectionIndex;
270 InMemoryStruct<macho::SymbolTableEntry> Entry;
271 getSymbolTableEntry(Symb, Entry);
272 index = Entry->SectionIndex;
276 Res = end_sections();
278 Res = section_iterator(SectionRef(Sections[index], this));
280 return object_error::success;
283 error_code MachOObjectFile::getSymbolType(DataRefImpl Symb,
284 SymbolRef::Type &Res) const {
286 if (MachOObj->is64Bit()) {
287 InMemoryStruct<macho::Symbol64TableEntry> Entry;
288 getSymbol64TableEntry(Symb, Entry);
289 n_type = Entry->Type;
291 InMemoryStruct<macho::SymbolTableEntry> Entry;
292 getSymbolTableEntry(Symb, Entry);
293 n_type = Entry->Type;
295 Res = SymbolRef::ST_Other;
297 // If this is a STAB debugging symbol, we can do nothing more.
298 if (n_type & MachO::NlistMaskStab) {
299 Res = SymbolRef::ST_Debug;
300 return object_error::success;
303 switch (n_type & MachO::NlistMaskType) {
304 case MachO::NListTypeUndefined :
305 Res = SymbolRef::ST_External;
307 case MachO::NListTypeSection :
308 Res = SymbolRef::ST_Function;
311 return object_error::success;
315 symbol_iterator MachOObjectFile::begin_symbols() const {
316 // DRI.d.a = segment number; DRI.d.b = symbol index.
318 DRI.d.a = DRI.d.b = 0;
319 moveToNextSymbol(DRI);
320 return symbol_iterator(SymbolRef(DRI, this));
323 symbol_iterator MachOObjectFile::end_symbols() const {
325 DRI.d.a = MachOObj->getHeader().NumLoadCommands;
327 return symbol_iterator(SymbolRef(DRI, this));
331 /*===-- Sections ----------------------------------------------------------===*/
333 void MachOObjectFile::moveToNextSection(DataRefImpl &DRI) const {
334 uint32_t LoadCommandCount = MachOObj->getHeader().NumLoadCommands;
335 while (DRI.d.a < LoadCommandCount) {
336 LoadCommandInfo LCI = MachOObj->getLoadCommandInfo(DRI.d.a);
337 if (LCI.Command.Type == macho::LCT_Segment) {
338 InMemoryStruct<macho::SegmentLoadCommand> SegmentLoadCmd;
339 MachOObj->ReadSegmentLoadCommand(LCI, SegmentLoadCmd);
340 if (DRI.d.b < SegmentLoadCmd->NumSections)
342 } else if (LCI.Command.Type == macho::LCT_Segment64) {
343 InMemoryStruct<macho::Segment64LoadCommand> Segment64LoadCmd;
344 MachOObj->ReadSegment64LoadCommand(LCI, Segment64LoadCmd);
345 if (DRI.d.b < Segment64LoadCmd->NumSections)
354 error_code MachOObjectFile::getSectionNext(DataRefImpl DRI,
355 SectionRef &Result) const {
357 moveToNextSection(DRI);
358 Result = SectionRef(DRI, this);
359 return object_error::success;
363 MachOObjectFile::getSection(DataRefImpl DRI,
364 InMemoryStruct<macho::Section> &Res) const {
365 InMemoryStruct<macho::SegmentLoadCommand> SLC;
366 LoadCommandInfo LCI = MachOObj->getLoadCommandInfo(DRI.d.a);
367 MachOObj->ReadSegmentLoadCommand(LCI, SLC);
368 MachOObj->ReadSection(LCI, DRI.d.b, Res);
371 std::size_t MachOObjectFile::getSectionIndex(DataRefImpl Sec) const {
372 SectionList::const_iterator loc =
373 std::find(Sections.begin(), Sections.end(), Sec);
374 assert(loc != Sections.end() && "Sec is not a valid section!");
375 return std::distance(Sections.begin(), loc);
379 MachOObjectFile::getSection64(DataRefImpl DRI,
380 InMemoryStruct<macho::Section64> &Res) const {
381 InMemoryStruct<macho::Segment64LoadCommand> SLC;
382 LoadCommandInfo LCI = MachOObj->getLoadCommandInfo(DRI.d.a);
383 MachOObj->ReadSegment64LoadCommand(LCI, SLC);
384 MachOObj->ReadSection64(LCI, DRI.d.b, Res);
387 static bool is64BitLoadCommand(const MachOObject *MachOObj, DataRefImpl DRI) {
388 LoadCommandInfo LCI = MachOObj->getLoadCommandInfo(DRI.d.a);
389 if (LCI.Command.Type == macho::LCT_Segment64)
391 assert(LCI.Command.Type == macho::LCT_Segment && "Unexpected Type.");
395 error_code MachOObjectFile::getSectionName(DataRefImpl DRI,
396 StringRef &Result) const {
397 // FIXME: thread safety.
398 static char result[34];
399 if (is64BitLoadCommand(MachOObj, DRI)) {
400 InMemoryStruct<macho::Segment64LoadCommand> SLC;
401 LoadCommandInfo LCI = MachOObj->getLoadCommandInfo(DRI.d.a);
402 MachOObj->ReadSegment64LoadCommand(LCI, SLC);
403 InMemoryStruct<macho::Section64> Sect;
404 MachOObj->ReadSection64(LCI, DRI.d.b, Sect);
406 strcpy(result, Sect->SegmentName);
408 strcat(result, Sect->Name);
410 InMemoryStruct<macho::SegmentLoadCommand> SLC;
411 LoadCommandInfo LCI = MachOObj->getLoadCommandInfo(DRI.d.a);
412 MachOObj->ReadSegmentLoadCommand(LCI, SLC);
413 InMemoryStruct<macho::Section> Sect;
414 MachOObj->ReadSection(LCI, DRI.d.b, Sect);
416 strcpy(result, Sect->SegmentName);
418 strcat(result, Sect->Name);
420 Result = StringRef(result);
421 return object_error::success;
424 error_code MachOObjectFile::getSectionAddress(DataRefImpl DRI,
425 uint64_t &Result) const {
426 if (is64BitLoadCommand(MachOObj, DRI)) {
427 InMemoryStruct<macho::Section64> Sect;
428 getSection64(DRI, Sect);
429 Result = Sect->Address;
431 InMemoryStruct<macho::Section> Sect;
432 getSection(DRI, Sect);
433 Result = Sect->Address;
435 return object_error::success;
438 error_code MachOObjectFile::getSectionSize(DataRefImpl DRI,
439 uint64_t &Result) const {
440 if (is64BitLoadCommand(MachOObj, DRI)) {
441 InMemoryStruct<macho::Section64> Sect;
442 getSection64(DRI, Sect);
445 InMemoryStruct<macho::Section> Sect;
446 getSection(DRI, Sect);
449 return object_error::success;
452 error_code MachOObjectFile::getSectionContents(DataRefImpl DRI,
453 StringRef &Result) const {
454 if (is64BitLoadCommand(MachOObj, DRI)) {
455 InMemoryStruct<macho::Section64> Sect;
456 getSection64(DRI, Sect);
457 Result = MachOObj->getData(Sect->Offset, Sect->Size);
459 InMemoryStruct<macho::Section> Sect;
460 getSection(DRI, Sect);
461 Result = MachOObj->getData(Sect->Offset, Sect->Size);
463 return object_error::success;
466 error_code MachOObjectFile::getSectionAlignment(DataRefImpl DRI,
467 uint64_t &Result) const {
468 if (is64BitLoadCommand(MachOObj, DRI)) {
469 InMemoryStruct<macho::Section64> Sect;
470 getSection64(DRI, Sect);
471 Result = uint64_t(1) << Sect->Align;
473 InMemoryStruct<macho::Section> Sect;
474 getSection(DRI, Sect);
475 Result = uint64_t(1) << Sect->Align;
477 return object_error::success;
480 error_code MachOObjectFile::isSectionText(DataRefImpl DRI,
481 bool &Result) const {
482 if (is64BitLoadCommand(MachOObj, DRI)) {
483 InMemoryStruct<macho::Section64> Sect;
484 getSection64(DRI, Sect);
485 Result = !strcmp(Sect->Name, "__text");
487 InMemoryStruct<macho::Section> Sect;
488 getSection(DRI, Sect);
489 Result = !strcmp(Sect->Name, "__text");
491 return object_error::success;
494 error_code MachOObjectFile::isSectionData(DataRefImpl DRI,
495 bool &Result) const {
496 // FIXME: Unimplemented.
498 return object_error::success;
501 error_code MachOObjectFile::isSectionBSS(DataRefImpl DRI,
502 bool &Result) const {
503 // FIXME: Unimplemented.
505 return object_error::success;
508 error_code MachOObjectFile::sectionContainsSymbol(DataRefImpl Sec,
510 bool &Result) const {
512 getSymbolType(Symb, ST);
513 if (ST == SymbolRef::ST_External) {
515 return object_error::success;
518 uint64_t SectBegin, SectEnd;
519 getSectionAddress(Sec, SectBegin);
520 getSectionSize(Sec, SectEnd);
521 SectEnd += SectBegin;
523 if (MachOObj->is64Bit()) {
524 InMemoryStruct<macho::Symbol64TableEntry> Entry;
525 getSymbol64TableEntry(Symb, Entry);
526 uint64_t SymAddr= Entry->Value;
527 Result = (SymAddr >= SectBegin) && (SymAddr < SectEnd);
529 InMemoryStruct<macho::SymbolTableEntry> Entry;
530 getSymbolTableEntry(Symb, Entry);
531 uint64_t SymAddr= Entry->Value;
532 Result = (SymAddr >= SectBegin) && (SymAddr < SectEnd);
535 return object_error::success;
538 relocation_iterator MachOObjectFile::getSectionRelBegin(DataRefImpl Sec) const {
541 ret.d.b = getSectionIndex(Sec);
542 return relocation_iterator(RelocationRef(ret, this));
544 relocation_iterator MachOObjectFile::getSectionRelEnd(DataRefImpl Sec) const {
546 if (is64BitLoadCommand(MachOObj, Sec)) {
547 InMemoryStruct<macho::Section64> Sect;
548 getSection64(Sec, Sect);
549 last_reloc = Sect->NumRelocationTableEntries;
551 InMemoryStruct<macho::Section> Sect;
552 getSection(Sec, Sect);
553 last_reloc = Sect->NumRelocationTableEntries;
556 ret.d.a = last_reloc;
557 ret.d.b = getSectionIndex(Sec);
558 return relocation_iterator(RelocationRef(ret, this));
561 section_iterator MachOObjectFile::begin_sections() const {
563 DRI.d.a = DRI.d.b = 0;
564 moveToNextSection(DRI);
565 return section_iterator(SectionRef(DRI, this));
568 section_iterator MachOObjectFile::end_sections() const {
570 DRI.d.a = MachOObj->getHeader().NumLoadCommands;
572 return section_iterator(SectionRef(DRI, this));
575 /*===-- Relocations -------------------------------------------------------===*/
577 void MachOObjectFile::
578 getRelocation(DataRefImpl Rel,
579 InMemoryStruct<macho::RelocationEntry> &Res) const {
581 if (MachOObj->is64Bit()) {
582 InMemoryStruct<macho::Section64> Sect;
583 getSection64(Sections[Rel.d.b], Sect);
584 relOffset = Sect->RelocationTableOffset;
586 InMemoryStruct<macho::Section> Sect;
587 getSection(Sections[Rel.d.b], Sect);
588 relOffset = Sect->RelocationTableOffset;
590 MachOObj->ReadRelocationEntry(relOffset, Rel.d.a, Res);
592 error_code MachOObjectFile::getRelocationNext(DataRefImpl Rel,
593 RelocationRef &Res) const {
595 Res = RelocationRef(Rel, this);
596 return object_error::success;
598 error_code MachOObjectFile::getRelocationAddress(DataRefImpl Rel,
599 uint64_t &Res) const {
600 const uint8_t* sectAddress = 0;
601 if (MachOObj->is64Bit()) {
602 InMemoryStruct<macho::Section64> Sect;
603 getSection64(Sections[Rel.d.b], Sect);
604 sectAddress += Sect->Address;
606 InMemoryStruct<macho::Section> Sect;
607 getSection(Sections[Rel.d.b], Sect);
608 sectAddress += Sect->Address;
610 InMemoryStruct<macho::RelocationEntry> RE;
611 getRelocation(Rel, RE);
613 unsigned Arch = getArch();
614 bool isScattered = (Arch != Triple::x86_64) &&
615 (RE->Word0 & macho::RF_Scattered);
616 uint64_t RelAddr = 0;
618 RelAddr = RE->Word0 & 0xFFFFFF;
622 Res = reinterpret_cast<uintptr_t>(sectAddress + RelAddr);
623 return object_error::success;
625 error_code MachOObjectFile::getRelocationSymbol(DataRefImpl Rel,
626 SymbolRef &Res) const {
627 InMemoryStruct<macho::RelocationEntry> RE;
628 getRelocation(Rel, RE);
629 uint32_t SymbolIdx = RE->Word1 & 0xffffff;
630 bool isExtern = (RE->Word1 >> 27) & 1;
633 Sym.d.a = Sym.d.b = 0;
634 moveToNextSymbol(Sym);
636 for (unsigned i = 0; i < SymbolIdx; i++) {
638 moveToNextSymbol(Sym);
639 assert(Sym.d.a < MachOObj->getHeader().NumLoadCommands &&
640 "Relocation symbol index out of range!");
643 Res = SymbolRef(Sym, this);
644 return object_error::success;
646 error_code MachOObjectFile::getRelocationType(DataRefImpl Rel,
647 uint64_t &Res) const {
648 InMemoryStruct<macho::RelocationEntry> RE;
649 getRelocation(Rel, RE);
653 return object_error::success;
655 error_code MachOObjectFile::getRelocationTypeName(DataRefImpl Rel,
656 SmallVectorImpl<char> &Result) const {
657 // TODO: Support scattered relocations.
659 InMemoryStruct<macho::RelocationEntry> RE;
660 getRelocation(Rel, RE);
662 unsigned Arch = getArch();
663 bool isScattered = (Arch != Triple::x86_64) &&
664 (RE->Word0 & macho::RF_Scattered);
668 r_type = (RE->Word0 >> 24) & 0xF;
670 r_type = (RE->Word1 >> 28) & 0xF;
674 const char* Table[] = {
675 "GENERIC_RELOC_VANILLA",
676 "GENERIC_RELOC_PAIR",
677 "GENERIC_RELOC_SECTDIFF",
678 "GENERIC_RELOC_LOCAL_SECTDIFF",
679 "GENERIC_RELOC_PB_LA_PTR" };
687 case Triple::x86_64: {
688 const char* Table[] = {
689 "X86_64_RELOC_UNSIGNED",
690 "X86_64_RELOC_SIGNED",
691 "X86_64_RELOC_BRANCH",
692 "X86_64_RELOC_GOT_LOAD",
694 "X86_64_RELOC_SUBTRACTOR",
695 "X86_64_RELOC_SIGNED_1",
696 "X86_64_RELOC_SIGNED_2",
697 "X86_64_RELOC_SIGNED_4",
698 "X86_64_RELOC_TLV" };
707 const char* Table[] = {
710 "ARM_RELOC_SECTDIFF",
711 "ARM_RELOC_LOCAL_SECTDIFF",
712 "ARM_RELOC_PB_LA_PTR",
714 "ARM_THUMB_RELOC_BR22",
715 "ARM_THUMB_32BIT_BRANCH",
717 "ARM_RELOC_HALF_SECTDIFF" };
726 const char* Table[] = {
735 "PPC_RELOC_SECTDIFF",
736 "PPC_RELOC_PB_LA_PTR",
737 "PPC_RELOC_HI16_SECTDIFF",
738 "PPC_RELOC_LO16_SECTDIFF",
739 "PPC_RELOC_HA16_SECTDIFF",
741 "PPC_RELOC_LO14_SECTDIFF",
742 "PPC_RELOC_LOCAL_SECTDIFF" };
747 case Triple::UnknownArch:
751 Result.append(res.begin(), res.end());
752 return object_error::success;
754 error_code MachOObjectFile::getRelocationAdditionalInfo(DataRefImpl Rel,
755 int64_t &Res) const {
756 InMemoryStruct<macho::RelocationEntry> RE;
757 getRelocation(Rel, RE);
758 bool isExtern = (RE->Word1 >> 27) & 1;
761 const uint8_t* sectAddress = base();
762 if (MachOObj->is64Bit()) {
763 InMemoryStruct<macho::Section64> Sect;
764 getSection64(Sections[Rel.d.b], Sect);
765 sectAddress += Sect->Offset;
767 InMemoryStruct<macho::Section> Sect;
768 getSection(Sections[Rel.d.b], Sect);
769 sectAddress += Sect->Offset;
771 Res = reinterpret_cast<uintptr_t>(sectAddress);
773 return object_error::success;
776 // Helper to advance a section or symbol iterator multiple increments at a time.
778 error_code advance(T &it, size_t Val) {
787 void advanceTo(T &it, size_t Val) {
788 if (error_code ec = advance(it, Val))
789 report_fatal_error(ec.message());
792 void MachOObjectFile::printRelocationTargetName(
793 InMemoryStruct<macho::RelocationEntry>& RE,
794 raw_string_ostream &fmt) const {
795 unsigned Arch = getArch();
796 bool isScattered = (Arch != Triple::x86_64) &&
797 (RE->Word0 & macho::RF_Scattered);
799 // Target of a scattered relocation is an address. In the interest of
800 // generating pretty output, scan through the symbol table looking for a
801 // symbol that aligns with that address. If we find one, print it.
802 // Otherwise, we just print the hex address of the target.
804 uint32_t Val = RE->Word1;
807 for (symbol_iterator SI = begin_symbols(), SE = end_symbols(); SI != SE;
809 if (ec) report_fatal_error(ec.message());
814 if ((ec = SI->getAddress(Addr)))
815 report_fatal_error(ec.message());
816 if (Addr != Val) continue;
817 if ((ec = SI->getName(Name)))
818 report_fatal_error(ec.message());
823 fmt << format("0x%x", Val);
828 bool isExtern = (RE->Word1 >> 27) & 1;
829 uint32_t Val = RE->Word1 & 0xFFFFFF;
832 symbol_iterator SI = begin_symbols();
836 section_iterator SI = begin_sections();
844 error_code MachOObjectFile::getRelocationValueString(DataRefImpl Rel,
845 SmallVectorImpl<char> &Result) const {
846 InMemoryStruct<macho::RelocationEntry> RE;
847 getRelocation(Rel, RE);
849 unsigned Arch = getArch();
850 bool isScattered = (Arch != Triple::x86_64) &&
851 (RE->Word0 & macho::RF_Scattered);
854 raw_string_ostream fmt(fmtbuf);
858 Type = (RE->Word0 >> 24) & 0xF;
860 Type = (RE->Word1 >> 28) & 0xF;
862 // Determine any addends that should be displayed with the relocation.
863 // These require decoding the relocation type, which is triple-specific.
865 // X86_64 has entirely custom relocation types.
866 if (Arch == Triple::x86_64) {
867 bool isPCRel = ((RE->Word1 >> 24) & 1);
870 case macho::RIT_X86_64_GOTLoad: // X86_64_RELOC_GOT_LOAD
871 case macho::RIT_X86_64_GOT: { // X86_64_RELOC_GOT
872 printRelocationTargetName(RE, fmt);
874 if (isPCRel) fmt << "PCREL";
877 case macho::RIT_X86_64_Subtractor: { // X86_64_RELOC_SUBTRACTOR
878 InMemoryStruct<macho::RelocationEntry> RENext;
879 DataRefImpl RelNext = Rel;
881 getRelocation(RelNext, RENext);
883 // X86_64_SUBTRACTOR must be followed by a relocation of type
884 // X86_64_RELOC_UNSIGNED.
885 // NOTE: Scattered relocations don't exist on x86_64.
886 unsigned RType = (RENext->Word1 >> 28) & 0xF;
888 report_fatal_error("Expected X86_64_RELOC_UNSIGNED after "
889 "X86_64_RELOC_SUBTRACTOR.");
891 // The X86_64_RELOC_UNSIGNED contains the minuend symbol,
892 // X86_64_SUBTRACTOR contains to the subtrahend.
893 printRelocationTargetName(RENext, fmt);
895 printRelocationTargetName(RE, fmt);
897 case macho::RIT_X86_64_Signed1: // X86_64_RELOC_SIGNED1
898 printRelocationTargetName(RE, fmt);
901 case macho::RIT_X86_64_Signed2: // X86_64_RELOC_SIGNED2
902 printRelocationTargetName(RE, fmt);
905 case macho::RIT_X86_64_Signed4: // X86_64_RELOC_SIGNED4
906 printRelocationTargetName(RE, fmt);
910 printRelocationTargetName(RE, fmt);
913 // X86 and ARM share some relocation types in common.
914 } else if (Arch == Triple::x86 || Arch == Triple::arm) {
915 // Generic relocation types...
917 case macho::RIT_Pair: // GENERIC_RELOC_PAIR - prints no info
918 return object_error::success;
919 case macho::RIT_Difference: // GENERIC_RELOC_SECTDIFF
920 case macho::RIT_Generic_LocalDifference: { // GENERIC_RELOC_LOCAL_SECTDIFF
921 InMemoryStruct<macho::RelocationEntry> RENext;
922 DataRefImpl RelNext = Rel;
924 getRelocation(RelNext, RENext);
926 // X86 sect diff's must be followed by a relocation of type
927 // GENERIC_RELOC_PAIR.
928 bool isNextScattered = (Arch != Triple::x86_64) &&
929 (RENext->Word0 & macho::RF_Scattered);
932 RType = (RENext->Word0 >> 24) & 0xF;
934 RType = (RENext->Word1 >> 28) & 0xF;
936 report_fatal_error("Expected GENERIC_RELOC_PAIR after "
937 "GENERIC_RELOC_SECTDIFF or "
938 "GENERIC_RELOC_LOCAL_SECTDIFF.");
940 printRelocationTargetName(RE, fmt);
942 printRelocationTargetName(RENext, fmt);
947 if (Arch == Triple::x86) {
948 // All X86 relocations that need special printing were already
949 // handled in the generic code.
950 printRelocationTargetName(RE, fmt);
951 } else { // ARM-specific relocations
953 case macho::RIT_ARM_Half: // ARM_RELOC_HALF
954 case macho::RIT_ARM_HalfDifference: { // ARM_RELOC_HALF_SECTDIFF
955 // Half relocations steal a bit from the length field to encode
956 // whether this is an upper16 or a lower16 relocation.
959 isUpper = (RE->Word0 >> 28) & 1;
961 isUpper = (RE->Word1 >> 25) & 1;
967 printRelocationTargetName(RE, fmt);
969 InMemoryStruct<macho::RelocationEntry> RENext;
970 DataRefImpl RelNext = Rel;
972 getRelocation(RelNext, RENext);
974 // ARM half relocs must be followed by a relocation of type
976 bool isNextScattered = (Arch != Triple::x86_64) &&
977 (RENext->Word0 & macho::RF_Scattered);
980 RType = (RENext->Word0 >> 24) & 0xF;
982 RType = (RENext->Word1 >> 28) & 0xF;
985 report_fatal_error("Expected ARM_RELOC_PAIR after "
986 "GENERIC_RELOC_HALF");
988 // NOTE: The half of the target virtual address is stashed in the
989 // address field of the secondary relocation, but we can't reverse
990 // engineer the constant offset from it without decoding the movw/movt
991 // instruction to find the other half in its immediate field.
993 // ARM_RELOC_HALF_SECTDIFF encodes the second section in the
994 // symbol/section pointer of the follow-on relocation.
995 if (Type == macho::RIT_ARM_HalfDifference) {
997 printRelocationTargetName(RENext, fmt);
1004 printRelocationTargetName(RE, fmt);
1009 printRelocationTargetName(RE, fmt);
1012 Result.append(fmtbuf.begin(), fmtbuf.end());
1013 return object_error::success;
1016 error_code MachOObjectFile::getRelocationHidden(DataRefImpl Rel,
1017 bool &Result) const {
1018 InMemoryStruct<macho::RelocationEntry> RE;
1019 getRelocation(Rel, RE);
1021 unsigned Arch = getArch();
1022 bool isScattered = (Arch != Triple::x86_64) &&
1023 (RE->Word0 & macho::RF_Scattered);
1026 Type = (RE->Word0 >> 24) & 0xF;
1028 Type = (RE->Word1 >> 28) & 0xF;
1032 // On arches that use the generic relocations, GENERIC_RELOC_PAIR
1033 // is always hidden.
1034 if (Arch == Triple::x86 || Arch == Triple::arm) {
1035 if (Type == macho::RIT_Pair) Result = true;
1036 } else if (Arch == Triple::x86_64) {
1037 // On x86_64, X86_64_RELOC_UNSIGNED is hidden only when it follows
1038 // an X864_64_RELOC_SUBTRACTOR.
1039 if (Type == macho::RIT_X86_64_Unsigned && Rel.d.a > 0) {
1040 DataRefImpl RelPrev = Rel;
1042 InMemoryStruct<macho::RelocationEntry> REPrev;
1043 getRelocation(RelPrev, REPrev);
1045 unsigned PrevType = (REPrev->Word1 >> 28) & 0xF;
1047 if (PrevType == macho::RIT_X86_64_Subtractor) Result = true;
1051 return object_error::success;
1054 /*===-- Miscellaneous -----------------------------------------------------===*/
1056 uint8_t MachOObjectFile::getBytesInAddress() const {
1057 return MachOObj->is64Bit() ? 8 : 4;
1060 StringRef MachOObjectFile::getFileFormatName() const {
1061 if (!MachOObj->is64Bit()) {
1062 switch (MachOObj->getHeader().CPUType) {
1063 case llvm::MachO::CPUTypeI386:
1064 return "Mach-O 32-bit i386";
1065 case llvm::MachO::CPUTypeARM:
1066 return "Mach-O arm";
1067 case llvm::MachO::CPUTypePowerPC:
1068 return "Mach-O 32-bit ppc";
1070 assert((MachOObj->getHeader().CPUType & llvm::MachO::CPUArchABI64) == 0 &&
1071 "64-bit object file when we're not 64-bit?");
1072 return "Mach-O 32-bit unknown";
1076 switch (MachOObj->getHeader().CPUType) {
1077 case llvm::MachO::CPUTypeX86_64:
1078 return "Mach-O 64-bit x86-64";
1079 case llvm::MachO::CPUTypePowerPC64:
1080 return "Mach-O 64-bit ppc64";
1082 assert((MachOObj->getHeader().CPUType & llvm::MachO::CPUArchABI64) == 1 &&
1083 "32-bit object file when we're 64-bit?");
1084 return "Mach-O 64-bit unknown";
1088 unsigned MachOObjectFile::getArch() const {
1089 switch (MachOObj->getHeader().CPUType) {
1090 case llvm::MachO::CPUTypeI386:
1092 case llvm::MachO::CPUTypeX86_64:
1093 return Triple::x86_64;
1094 case llvm::MachO::CPUTypeARM:
1096 case llvm::MachO::CPUTypePowerPC:
1098 case llvm::MachO::CPUTypePowerPC64:
1099 return Triple::ppc64;
1101 return Triple::UnknownArch;
1105 } // end namespace object
1106 } // end namespace llvm