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::getSymbolFileOffset(DataRefImpl DRI,
129 uint64_t &Result) const {
130 if (MachOObj->is64Bit()) {
131 InMemoryStruct<macho::Symbol64TableEntry> Entry;
132 getSymbol64TableEntry(DRI, Entry);
133 Result = Entry->Value;
134 if (Entry->SectionIndex) {
135 InMemoryStruct<macho::Section64> Section;
136 getSection64(Sections[Entry->SectionIndex-1], Section);
137 Result += Section->Offset - Section->Address;
140 InMemoryStruct<macho::SymbolTableEntry> Entry;
141 getSymbolTableEntry(DRI, Entry);
142 Result = Entry->Value;
143 if (Entry->SectionIndex) {
144 InMemoryStruct<macho::Section> Section;
145 getSection(Sections[Entry->SectionIndex-1], Section);
146 Result += Section->Offset - Section->Address;
150 return object_error::success;
153 error_code MachOObjectFile::getSymbolAddress(DataRefImpl DRI,
154 uint64_t &Result) const {
155 if (MachOObj->is64Bit()) {
156 InMemoryStruct<macho::Symbol64TableEntry> Entry;
157 getSymbol64TableEntry(DRI, Entry);
158 Result = Entry->Value;
160 InMemoryStruct<macho::SymbolTableEntry> Entry;
161 getSymbolTableEntry(DRI, Entry);
162 Result = Entry->Value;
164 return object_error::success;
167 error_code MachOObjectFile::getSymbolSize(DataRefImpl DRI,
168 uint64_t &Result) const {
169 uint32_t LoadCommandCount = MachOObj->getHeader().NumLoadCommands;
170 uint64_t BeginOffset;
171 uint64_t EndOffset = 0;
172 uint8_t SectionIndex;
173 if (MachOObj->is64Bit()) {
174 InMemoryStruct<macho::Symbol64TableEntry> Entry;
175 getSymbol64TableEntry(DRI, Entry);
176 BeginOffset = Entry->Value;
177 SectionIndex = Entry->SectionIndex;
179 Result = UnknownAddressOrSize;
180 return object_error::success;
182 // Unfortunately symbols are unsorted so we need to touch all
183 // symbols from load command
185 uint32_t Command = DRI.d.a;
186 while (Command == DRI.d.a) {
187 moveToNextSymbol(DRI);
188 if (DRI.d.a < LoadCommandCount) {
189 getSymbol64TableEntry(DRI, Entry);
190 if (Entry->SectionIndex == SectionIndex && Entry->Value > BeginOffset)
191 if (!EndOffset || Entry->Value < EndOffset)
192 EndOffset = Entry->Value;
197 InMemoryStruct<macho::SymbolTableEntry> Entry;
198 getSymbolTableEntry(DRI, Entry);
199 BeginOffset = Entry->Value;
200 SectionIndex = Entry->SectionIndex;
202 Result = UnknownAddressOrSize;
203 return object_error::success;
205 // Unfortunately symbols are unsorted so we need to touch all
206 // symbols from load command
208 uint32_t Command = DRI.d.a;
209 while (Command == DRI.d.a) {
210 moveToNextSymbol(DRI);
211 if (DRI.d.a < LoadCommandCount) {
212 getSymbolTableEntry(DRI, Entry);
213 if (Entry->SectionIndex == SectionIndex && Entry->Value > BeginOffset)
214 if (!EndOffset || Entry->Value < EndOffset)
215 EndOffset = Entry->Value;
222 getSectionSize(Sections[SectionIndex-1], Size);
223 getSectionAddress(Sections[SectionIndex-1], EndOffset);
226 Result = EndOffset - BeginOffset;
227 return object_error::success;
230 error_code MachOObjectFile::getSymbolNMTypeChar(DataRefImpl DRI,
231 char &Result) const {
233 if (MachOObj->is64Bit()) {
234 InMemoryStruct<macho::Symbol64TableEntry> Entry;
235 getSymbol64TableEntry(DRI, Entry);
237 Flags = Entry->Flags;
239 InMemoryStruct<macho::SymbolTableEntry> Entry;
240 getSymbolTableEntry(DRI, Entry);
242 Flags = Entry->Flags;
246 switch (Type & macho::STF_TypeMask) {
247 case macho::STT_Undefined:
250 case macho::STT_Absolute:
251 case macho::STT_Section:
259 if (Flags & (macho::STF_External | macho::STF_PrivateExtern))
260 Char = toupper(Char);
262 return object_error::success;
265 error_code MachOObjectFile::getSymbolFlags(DataRefImpl DRI,
266 uint32_t &Result) const {
269 if (MachOObj->is64Bit()) {
270 InMemoryStruct<macho::Symbol64TableEntry> Entry;
271 getSymbol64TableEntry(DRI, Entry);
272 MachOFlags = Entry->Flags;
273 MachOType = Entry->Type;
275 InMemoryStruct<macho::SymbolTableEntry> Entry;
276 getSymbolTableEntry(DRI, Entry);
277 MachOFlags = Entry->Flags;
278 MachOType = Entry->Type;
281 // TODO: Correctly set SF_ThreadLocal and SF_Common.
282 Result = SymbolRef::SF_None;
284 if ((MachOType & MachO::NlistMaskType) == MachO::NListTypeUndefined)
285 Result |= SymbolRef::SF_Undefined;
287 if (MachOFlags & macho::STF_StabsEntryMask)
288 Result |= SymbolRef::SF_FormatSpecific;
290 if (MachOType & MachO::NlistMaskExternal)
291 Result |= SymbolRef::SF_Global;
293 if (MachOFlags & (MachO::NListDescWeakRef | MachO::NListDescWeakDef))
294 Result |= SymbolRef::SF_Weak;
296 if ((MachOType & MachO::NlistMaskType) == MachO::NListTypeAbsolute)
297 Result |= SymbolRef::SF_Absolute;
299 return object_error::success;
302 error_code MachOObjectFile::getSymbolSection(DataRefImpl Symb,
303 section_iterator &Res) const {
305 if (MachOObj->is64Bit()) {
306 InMemoryStruct<macho::Symbol64TableEntry> Entry;
307 getSymbol64TableEntry(Symb, Entry);
308 index = Entry->SectionIndex;
310 InMemoryStruct<macho::SymbolTableEntry> Entry;
311 getSymbolTableEntry(Symb, Entry);
312 index = Entry->SectionIndex;
316 Res = end_sections();
318 Res = section_iterator(SectionRef(Sections[index-1], this));
320 return object_error::success;
323 error_code MachOObjectFile::getSymbolType(DataRefImpl Symb,
324 SymbolRef::Type &Res) const {
326 if (MachOObj->is64Bit()) {
327 InMemoryStruct<macho::Symbol64TableEntry> Entry;
328 getSymbol64TableEntry(Symb, Entry);
329 n_type = Entry->Type;
331 InMemoryStruct<macho::SymbolTableEntry> Entry;
332 getSymbolTableEntry(Symb, Entry);
333 n_type = Entry->Type;
335 Res = SymbolRef::ST_Other;
337 // If this is a STAB debugging symbol, we can do nothing more.
338 if (n_type & MachO::NlistMaskStab) {
339 Res = SymbolRef::ST_Debug;
340 return object_error::success;
343 switch (n_type & MachO::NlistMaskType) {
344 case MachO::NListTypeUndefined :
345 Res = SymbolRef::ST_Unknown;
347 case MachO::NListTypeSection :
348 Res = SymbolRef::ST_Function;
351 return object_error::success;
355 symbol_iterator MachOObjectFile::begin_symbols() const {
356 // DRI.d.a = segment number; DRI.d.b = symbol index.
358 DRI.d.a = DRI.d.b = 0;
359 moveToNextSymbol(DRI);
360 return symbol_iterator(SymbolRef(DRI, this));
363 symbol_iterator MachOObjectFile::end_symbols() const {
365 DRI.d.a = MachOObj->getHeader().NumLoadCommands;
367 return symbol_iterator(SymbolRef(DRI, this));
370 symbol_iterator MachOObjectFile::begin_dynamic_symbols() const {
372 report_fatal_error("Dynamic symbols unimplemented in MachOObjectFile");
375 symbol_iterator MachOObjectFile::end_dynamic_symbols() const {
377 report_fatal_error("Dynamic symbols unimplemented in MachOObjectFile");
380 library_iterator MachOObjectFile::begin_libraries_needed() const {
382 report_fatal_error("Needed libraries unimplemented in MachOObjectFile");
385 library_iterator MachOObjectFile::end_libraries_needed() const {
387 report_fatal_error("Needed libraries unimplemented in MachOObjectFile");
390 /*===-- Sections ----------------------------------------------------------===*/
392 void MachOObjectFile::moveToNextSection(DataRefImpl &DRI) const {
393 uint32_t LoadCommandCount = MachOObj->getHeader().NumLoadCommands;
394 while (DRI.d.a < LoadCommandCount) {
395 LoadCommandInfo LCI = MachOObj->getLoadCommandInfo(DRI.d.a);
396 if (LCI.Command.Type == macho::LCT_Segment) {
397 InMemoryStruct<macho::SegmentLoadCommand> SegmentLoadCmd;
398 MachOObj->ReadSegmentLoadCommand(LCI, SegmentLoadCmd);
399 if (DRI.d.b < SegmentLoadCmd->NumSections)
401 } else if (LCI.Command.Type == macho::LCT_Segment64) {
402 InMemoryStruct<macho::Segment64LoadCommand> Segment64LoadCmd;
403 MachOObj->ReadSegment64LoadCommand(LCI, Segment64LoadCmd);
404 if (DRI.d.b < Segment64LoadCmd->NumSections)
413 error_code MachOObjectFile::getSectionNext(DataRefImpl DRI,
414 SectionRef &Result) const {
416 moveToNextSection(DRI);
417 Result = SectionRef(DRI, this);
418 return object_error::success;
422 MachOObjectFile::getSection(DataRefImpl DRI,
423 InMemoryStruct<macho::Section> &Res) const {
424 InMemoryStruct<macho::SegmentLoadCommand> SLC;
425 LoadCommandInfo LCI = MachOObj->getLoadCommandInfo(DRI.d.a);
426 MachOObj->ReadSegmentLoadCommand(LCI, SLC);
427 MachOObj->ReadSection(LCI, DRI.d.b, Res);
430 std::size_t MachOObjectFile::getSectionIndex(DataRefImpl Sec) const {
431 SectionList::const_iterator loc =
432 std::find(Sections.begin(), Sections.end(), Sec);
433 assert(loc != Sections.end() && "Sec is not a valid section!");
434 return std::distance(Sections.begin(), loc);
438 MachOObjectFile::getSection64(DataRefImpl DRI,
439 InMemoryStruct<macho::Section64> &Res) const {
440 InMemoryStruct<macho::Segment64LoadCommand> SLC;
441 LoadCommandInfo LCI = MachOObj->getLoadCommandInfo(DRI.d.a);
442 MachOObj->ReadSegment64LoadCommand(LCI, SLC);
443 MachOObj->ReadSection64(LCI, DRI.d.b, Res);
446 static bool is64BitLoadCommand(const MachOObject *MachOObj, DataRefImpl DRI) {
447 LoadCommandInfo LCI = MachOObj->getLoadCommandInfo(DRI.d.a);
448 if (LCI.Command.Type == macho::LCT_Segment64)
450 assert(LCI.Command.Type == macho::LCT_Segment && "Unexpected Type.");
454 error_code MachOObjectFile::getSectionName(DataRefImpl DRI,
455 StringRef &Result) const {
456 // FIXME: thread safety.
457 static char result[34];
458 if (is64BitLoadCommand(MachOObj, DRI)) {
459 InMemoryStruct<macho::Segment64LoadCommand> SLC;
460 LoadCommandInfo LCI = MachOObj->getLoadCommandInfo(DRI.d.a);
461 MachOObj->ReadSegment64LoadCommand(LCI, SLC);
462 InMemoryStruct<macho::Section64> Sect;
463 MachOObj->ReadSection64(LCI, DRI.d.b, Sect);
465 strcpy(result, Sect->SegmentName);
467 strcat(result, Sect->Name);
469 InMemoryStruct<macho::SegmentLoadCommand> SLC;
470 LoadCommandInfo LCI = MachOObj->getLoadCommandInfo(DRI.d.a);
471 MachOObj->ReadSegmentLoadCommand(LCI, SLC);
472 InMemoryStruct<macho::Section> Sect;
473 MachOObj->ReadSection(LCI, DRI.d.b, Sect);
475 strcpy(result, Sect->SegmentName);
477 strcat(result, Sect->Name);
479 Result = StringRef(result);
480 return object_error::success;
483 error_code MachOObjectFile::getSectionAddress(DataRefImpl DRI,
484 uint64_t &Result) const {
485 if (is64BitLoadCommand(MachOObj, DRI)) {
486 InMemoryStruct<macho::Section64> Sect;
487 getSection64(DRI, Sect);
488 Result = Sect->Address;
490 InMemoryStruct<macho::Section> Sect;
491 getSection(DRI, Sect);
492 Result = Sect->Address;
494 return object_error::success;
497 error_code MachOObjectFile::getSectionSize(DataRefImpl DRI,
498 uint64_t &Result) const {
499 if (is64BitLoadCommand(MachOObj, DRI)) {
500 InMemoryStruct<macho::Section64> Sect;
501 getSection64(DRI, Sect);
504 InMemoryStruct<macho::Section> Sect;
505 getSection(DRI, Sect);
508 return object_error::success;
511 error_code MachOObjectFile::getSectionContents(DataRefImpl DRI,
512 StringRef &Result) const {
513 if (is64BitLoadCommand(MachOObj, DRI)) {
514 InMemoryStruct<macho::Section64> Sect;
515 getSection64(DRI, Sect);
516 Result = MachOObj->getData(Sect->Offset, Sect->Size);
518 InMemoryStruct<macho::Section> Sect;
519 getSection(DRI, Sect);
520 Result = MachOObj->getData(Sect->Offset, Sect->Size);
522 return object_error::success;
525 error_code MachOObjectFile::getSectionAlignment(DataRefImpl DRI,
526 uint64_t &Result) const {
527 if (is64BitLoadCommand(MachOObj, DRI)) {
528 InMemoryStruct<macho::Section64> Sect;
529 getSection64(DRI, Sect);
530 Result = uint64_t(1) << Sect->Align;
532 InMemoryStruct<macho::Section> Sect;
533 getSection(DRI, Sect);
534 Result = uint64_t(1) << Sect->Align;
536 return object_error::success;
539 error_code MachOObjectFile::isSectionText(DataRefImpl DRI,
540 bool &Result) const {
541 if (is64BitLoadCommand(MachOObj, DRI)) {
542 InMemoryStruct<macho::Section64> Sect;
543 getSection64(DRI, Sect);
544 Result = !strcmp(Sect->Name, "__text");
546 InMemoryStruct<macho::Section> Sect;
547 getSection(DRI, Sect);
548 Result = !strcmp(Sect->Name, "__text");
550 return object_error::success;
553 error_code MachOObjectFile::isSectionData(DataRefImpl DRI,
554 bool &Result) const {
555 // FIXME: Unimplemented.
557 return object_error::success;
560 error_code MachOObjectFile::isSectionBSS(DataRefImpl DRI,
561 bool &Result) const {
562 // FIXME: Unimplemented.
564 return object_error::success;
567 error_code MachOObjectFile::sectionContainsSymbol(DataRefImpl Sec,
569 bool &Result) const {
571 getSymbolType(Symb, ST);
572 if (ST == SymbolRef::ST_Unknown) {
574 return object_error::success;
577 uint64_t SectBegin, SectEnd;
578 getSectionAddress(Sec, SectBegin);
579 getSectionSize(Sec, SectEnd);
580 SectEnd += SectBegin;
582 if (MachOObj->is64Bit()) {
583 InMemoryStruct<macho::Symbol64TableEntry> Entry;
584 getSymbol64TableEntry(Symb, Entry);
585 uint64_t SymAddr= Entry->Value;
586 Result = (SymAddr >= SectBegin) && (SymAddr < SectEnd);
588 InMemoryStruct<macho::SymbolTableEntry> Entry;
589 getSymbolTableEntry(Symb, Entry);
590 uint64_t SymAddr= Entry->Value;
591 Result = (SymAddr >= SectBegin) && (SymAddr < SectEnd);
594 return object_error::success;
597 relocation_iterator MachOObjectFile::getSectionRelBegin(DataRefImpl Sec) const {
600 ret.d.b = getSectionIndex(Sec);
601 return relocation_iterator(RelocationRef(ret, this));
603 relocation_iterator MachOObjectFile::getSectionRelEnd(DataRefImpl Sec) const {
605 if (is64BitLoadCommand(MachOObj, Sec)) {
606 InMemoryStruct<macho::Section64> Sect;
607 getSection64(Sec, Sect);
608 last_reloc = Sect->NumRelocationTableEntries;
610 InMemoryStruct<macho::Section> Sect;
611 getSection(Sec, Sect);
612 last_reloc = Sect->NumRelocationTableEntries;
615 ret.d.a = last_reloc;
616 ret.d.b = getSectionIndex(Sec);
617 return relocation_iterator(RelocationRef(ret, this));
620 section_iterator MachOObjectFile::begin_sections() const {
622 DRI.d.a = DRI.d.b = 0;
623 moveToNextSection(DRI);
624 return section_iterator(SectionRef(DRI, this));
627 section_iterator MachOObjectFile::end_sections() const {
629 DRI.d.a = MachOObj->getHeader().NumLoadCommands;
631 return section_iterator(SectionRef(DRI, this));
634 /*===-- Relocations -------------------------------------------------------===*/
636 void MachOObjectFile::
637 getRelocation(DataRefImpl Rel,
638 InMemoryStruct<macho::RelocationEntry> &Res) const {
640 if (MachOObj->is64Bit()) {
641 InMemoryStruct<macho::Section64> Sect;
642 getSection64(Sections[Rel.d.b], Sect);
643 relOffset = Sect->RelocationTableOffset;
645 InMemoryStruct<macho::Section> Sect;
646 getSection(Sections[Rel.d.b], Sect);
647 relOffset = Sect->RelocationTableOffset;
649 MachOObj->ReadRelocationEntry(relOffset, Rel.d.a, Res);
651 error_code MachOObjectFile::getRelocationNext(DataRefImpl Rel,
652 RelocationRef &Res) const {
654 Res = RelocationRef(Rel, this);
655 return object_error::success;
657 error_code MachOObjectFile::getRelocationAddress(DataRefImpl Rel,
658 uint64_t &Res) const {
659 const uint8_t* sectAddress = 0;
660 if (MachOObj->is64Bit()) {
661 InMemoryStruct<macho::Section64> Sect;
662 getSection64(Sections[Rel.d.b], Sect);
663 sectAddress += Sect->Address;
665 InMemoryStruct<macho::Section> Sect;
666 getSection(Sections[Rel.d.b], Sect);
667 sectAddress += Sect->Address;
669 InMemoryStruct<macho::RelocationEntry> RE;
670 getRelocation(Rel, RE);
672 unsigned Arch = getArch();
673 bool isScattered = (Arch != Triple::x86_64) &&
674 (RE->Word0 & macho::RF_Scattered);
675 uint64_t RelAddr = 0;
677 RelAddr = RE->Word0 & 0xFFFFFF;
681 Res = reinterpret_cast<uintptr_t>(sectAddress + RelAddr);
682 return object_error::success;
684 error_code MachOObjectFile::getRelocationOffset(DataRefImpl Rel,
685 uint64_t &Res) const {
686 InMemoryStruct<macho::RelocationEntry> RE;
687 getRelocation(Rel, RE);
689 unsigned Arch = getArch();
690 bool isScattered = (Arch != Triple::x86_64) &&
691 (RE->Word0 & macho::RF_Scattered);
693 Res = RE->Word0 & 0xFFFFFF;
696 return object_error::success;
698 error_code MachOObjectFile::getRelocationSymbol(DataRefImpl Rel,
699 SymbolRef &Res) const {
700 InMemoryStruct<macho::RelocationEntry> RE;
701 getRelocation(Rel, RE);
702 uint32_t SymbolIdx = RE->Word1 & 0xffffff;
703 bool isExtern = (RE->Word1 >> 27) & 1;
706 Sym.d.a = Sym.d.b = 0;
707 moveToNextSymbol(Sym);
709 for (unsigned i = 0; i < SymbolIdx; i++) {
711 moveToNextSymbol(Sym);
712 assert(Sym.d.a < MachOObj->getHeader().NumLoadCommands &&
713 "Relocation symbol index out of range!");
716 Res = SymbolRef(Sym, this);
717 return object_error::success;
719 error_code MachOObjectFile::getRelocationType(DataRefImpl Rel,
720 uint64_t &Res) const {
721 InMemoryStruct<macho::RelocationEntry> RE;
722 getRelocation(Rel, RE);
726 return object_error::success;
728 error_code MachOObjectFile::getRelocationTypeName(DataRefImpl Rel,
729 SmallVectorImpl<char> &Result) const {
730 // TODO: Support scattered relocations.
732 InMemoryStruct<macho::RelocationEntry> RE;
733 getRelocation(Rel, RE);
735 unsigned Arch = getArch();
736 bool isScattered = (Arch != Triple::x86_64) &&
737 (RE->Word0 & macho::RF_Scattered);
741 r_type = (RE->Word0 >> 24) & 0xF;
743 r_type = (RE->Word1 >> 28) & 0xF;
747 const char* Table[] = {
748 "GENERIC_RELOC_VANILLA",
749 "GENERIC_RELOC_PAIR",
750 "GENERIC_RELOC_SECTDIFF",
751 "GENERIC_RELOC_PB_LA_PTR",
752 "GENERIC_RELOC_LOCAL_SECTDIFF",
753 "GENERIC_RELOC_TLV" };
761 case Triple::x86_64: {
762 const char* Table[] = {
763 "X86_64_RELOC_UNSIGNED",
764 "X86_64_RELOC_SIGNED",
765 "X86_64_RELOC_BRANCH",
766 "X86_64_RELOC_GOT_LOAD",
768 "X86_64_RELOC_SUBTRACTOR",
769 "X86_64_RELOC_SIGNED_1",
770 "X86_64_RELOC_SIGNED_2",
771 "X86_64_RELOC_SIGNED_4",
772 "X86_64_RELOC_TLV" };
781 const char* Table[] = {
784 "ARM_RELOC_SECTDIFF",
785 "ARM_RELOC_LOCAL_SECTDIFF",
786 "ARM_RELOC_PB_LA_PTR",
788 "ARM_THUMB_RELOC_BR22",
789 "ARM_THUMB_32BIT_BRANCH",
791 "ARM_RELOC_HALF_SECTDIFF" };
800 const char* Table[] = {
809 "PPC_RELOC_SECTDIFF",
810 "PPC_RELOC_PB_LA_PTR",
811 "PPC_RELOC_HI16_SECTDIFF",
812 "PPC_RELOC_LO16_SECTDIFF",
813 "PPC_RELOC_HA16_SECTDIFF",
815 "PPC_RELOC_LO14_SECTDIFF",
816 "PPC_RELOC_LOCAL_SECTDIFF" };
821 case Triple::UnknownArch:
825 Result.append(res.begin(), res.end());
826 return object_error::success;
828 error_code MachOObjectFile::getRelocationAdditionalInfo(DataRefImpl Rel,
829 int64_t &Res) const {
830 InMemoryStruct<macho::RelocationEntry> RE;
831 getRelocation(Rel, RE);
832 bool isExtern = (RE->Word1 >> 27) & 1;
835 const uint8_t* sectAddress = base();
836 if (MachOObj->is64Bit()) {
837 InMemoryStruct<macho::Section64> Sect;
838 getSection64(Sections[Rel.d.b], Sect);
839 sectAddress += Sect->Offset;
841 InMemoryStruct<macho::Section> Sect;
842 getSection(Sections[Rel.d.b], Sect);
843 sectAddress += Sect->Offset;
845 Res = reinterpret_cast<uintptr_t>(sectAddress);
847 return object_error::success;
850 // Helper to advance a section or symbol iterator multiple increments at a time.
852 error_code advance(T &it, size_t Val) {
861 void advanceTo(T &it, size_t Val) {
862 if (error_code ec = advance(it, Val))
863 report_fatal_error(ec.message());
866 void MachOObjectFile::printRelocationTargetName(
867 InMemoryStruct<macho::RelocationEntry>& RE,
868 raw_string_ostream &fmt) const {
869 unsigned Arch = getArch();
870 bool isScattered = (Arch != Triple::x86_64) &&
871 (RE->Word0 & macho::RF_Scattered);
873 // Target of a scattered relocation is an address. In the interest of
874 // generating pretty output, scan through the symbol table looking for a
875 // symbol that aligns with that address. If we find one, print it.
876 // Otherwise, we just print the hex address of the target.
878 uint32_t Val = RE->Word1;
881 for (symbol_iterator SI = begin_symbols(), SE = end_symbols(); SI != SE;
883 if (ec) report_fatal_error(ec.message());
888 if ((ec = SI->getAddress(Addr)))
889 report_fatal_error(ec.message());
890 if (Addr != Val) continue;
891 if ((ec = SI->getName(Name)))
892 report_fatal_error(ec.message());
897 // If we couldn't find a symbol that this relocation refers to, try
898 // to find a section beginning instead.
899 for (section_iterator SI = begin_sections(), SE = end_sections(); SI != SE;
901 if (ec) report_fatal_error(ec.message());
906 if ((ec = SI->getAddress(Addr)))
907 report_fatal_error(ec.message());
908 if (Addr != Val) continue;
909 if ((ec = SI->getName(Name)))
910 report_fatal_error(ec.message());
915 fmt << format("0x%x", Val);
920 bool isExtern = (RE->Word1 >> 27) & 1;
921 uint32_t Val = RE->Word1 & 0xFFFFFF;
924 symbol_iterator SI = begin_symbols();
928 section_iterator SI = begin_sections();
936 error_code MachOObjectFile::getRelocationValueString(DataRefImpl Rel,
937 SmallVectorImpl<char> &Result) const {
938 InMemoryStruct<macho::RelocationEntry> RE;
939 getRelocation(Rel, RE);
941 unsigned Arch = getArch();
942 bool isScattered = (Arch != Triple::x86_64) &&
943 (RE->Word0 & macho::RF_Scattered);
946 raw_string_ostream fmt(fmtbuf);
950 Type = (RE->Word0 >> 24) & 0xF;
952 Type = (RE->Word1 >> 28) & 0xF;
956 isPCRel = ((RE->Word0 >> 30) & 1);
958 isPCRel = ((RE->Word1 >> 24) & 1);
960 // Determine any addends that should be displayed with the relocation.
961 // These require decoding the relocation type, which is triple-specific.
963 // X86_64 has entirely custom relocation types.
964 if (Arch == Triple::x86_64) {
965 bool isPCRel = ((RE->Word1 >> 24) & 1);
968 case macho::RIT_X86_64_GOTLoad: // X86_64_RELOC_GOT_LOAD
969 case macho::RIT_X86_64_GOT: { // X86_64_RELOC_GOT
970 printRelocationTargetName(RE, fmt);
972 if (isPCRel) fmt << "PCREL";
975 case macho::RIT_X86_64_Subtractor: { // X86_64_RELOC_SUBTRACTOR
976 InMemoryStruct<macho::RelocationEntry> RENext;
977 DataRefImpl RelNext = Rel;
979 getRelocation(RelNext, RENext);
981 // X86_64_SUBTRACTOR must be followed by a relocation of type
982 // X86_64_RELOC_UNSIGNED.
983 // NOTE: Scattered relocations don't exist on x86_64.
984 unsigned RType = (RENext->Word1 >> 28) & 0xF;
986 report_fatal_error("Expected X86_64_RELOC_UNSIGNED after "
987 "X86_64_RELOC_SUBTRACTOR.");
989 // The X86_64_RELOC_UNSIGNED contains the minuend symbol,
990 // X86_64_SUBTRACTOR contains to the subtrahend.
991 printRelocationTargetName(RENext, fmt);
993 printRelocationTargetName(RE, fmt);
995 case macho::RIT_X86_64_TLV:
996 printRelocationTargetName(RE, fmt);
998 if (isPCRel) fmt << "P";
1000 case macho::RIT_X86_64_Signed1: // X86_64_RELOC_SIGNED1
1001 printRelocationTargetName(RE, fmt);
1004 case macho::RIT_X86_64_Signed2: // X86_64_RELOC_SIGNED2
1005 printRelocationTargetName(RE, fmt);
1008 case macho::RIT_X86_64_Signed4: // X86_64_RELOC_SIGNED4
1009 printRelocationTargetName(RE, fmt);
1013 printRelocationTargetName(RE, fmt);
1016 // X86 and ARM share some relocation types in common.
1017 } else if (Arch == Triple::x86 || Arch == Triple::arm) {
1018 // Generic relocation types...
1020 case macho::RIT_Pair: // GENERIC_RELOC_PAIR - prints no info
1021 return object_error::success;
1022 case macho::RIT_Difference: { // GENERIC_RELOC_SECTDIFF
1023 InMemoryStruct<macho::RelocationEntry> RENext;
1024 DataRefImpl RelNext = Rel;
1026 getRelocation(RelNext, RENext);
1028 // X86 sect diff's must be followed by a relocation of type
1029 // GENERIC_RELOC_PAIR.
1030 bool isNextScattered = (Arch != Triple::x86_64) &&
1031 (RENext->Word0 & macho::RF_Scattered);
1033 if (isNextScattered)
1034 RType = (RENext->Word0 >> 24) & 0xF;
1036 RType = (RENext->Word1 >> 28) & 0xF;
1038 report_fatal_error("Expected GENERIC_RELOC_PAIR after "
1039 "GENERIC_RELOC_SECTDIFF.");
1041 printRelocationTargetName(RE, fmt);
1043 printRelocationTargetName(RENext, fmt);
1048 if (Arch == Triple::x86) {
1049 // All X86 relocations that need special printing were already
1050 // handled in the generic code.
1052 case macho::RIT_Generic_LocalDifference:{// GENERIC_RELOC_LOCAL_SECTDIFF
1053 InMemoryStruct<macho::RelocationEntry> RENext;
1054 DataRefImpl RelNext = Rel;
1056 getRelocation(RelNext, RENext);
1058 // X86 sect diff's must be followed by a relocation of type
1059 // GENERIC_RELOC_PAIR.
1060 bool isNextScattered = (Arch != Triple::x86_64) &&
1061 (RENext->Word0 & macho::RF_Scattered);
1063 if (isNextScattered)
1064 RType = (RENext->Word0 >> 24) & 0xF;
1066 RType = (RENext->Word1 >> 28) & 0xF;
1068 report_fatal_error("Expected GENERIC_RELOC_PAIR after "
1069 "GENERIC_RELOC_LOCAL_SECTDIFF.");
1071 printRelocationTargetName(RE, fmt);
1073 printRelocationTargetName(RENext, fmt);
1076 case macho::RIT_Generic_TLV: {
1077 printRelocationTargetName(RE, fmt);
1079 if (isPCRel) fmt << "P";
1083 printRelocationTargetName(RE, fmt);
1085 } else { // ARM-specific relocations
1087 case macho::RIT_ARM_Half: // ARM_RELOC_HALF
1088 case macho::RIT_ARM_HalfDifference: { // ARM_RELOC_HALF_SECTDIFF
1089 // Half relocations steal a bit from the length field to encode
1090 // whether this is an upper16 or a lower16 relocation.
1093 isUpper = (RE->Word0 >> 28) & 1;
1095 isUpper = (RE->Word1 >> 25) & 1;
1098 fmt << ":upper16:(";
1100 fmt << ":lower16:(";
1101 printRelocationTargetName(RE, fmt);
1103 InMemoryStruct<macho::RelocationEntry> RENext;
1104 DataRefImpl RelNext = Rel;
1106 getRelocation(RelNext, RENext);
1108 // ARM half relocs must be followed by a relocation of type
1110 bool isNextScattered = (Arch != Triple::x86_64) &&
1111 (RENext->Word0 & macho::RF_Scattered);
1113 if (isNextScattered)
1114 RType = (RENext->Word0 >> 24) & 0xF;
1116 RType = (RENext->Word1 >> 28) & 0xF;
1119 report_fatal_error("Expected ARM_RELOC_PAIR after "
1120 "GENERIC_RELOC_HALF");
1122 // NOTE: The half of the target virtual address is stashed in the
1123 // address field of the secondary relocation, but we can't reverse
1124 // engineer the constant offset from it without decoding the movw/movt
1125 // instruction to find the other half in its immediate field.
1127 // ARM_RELOC_HALF_SECTDIFF encodes the second section in the
1128 // symbol/section pointer of the follow-on relocation.
1129 if (Type == macho::RIT_ARM_HalfDifference) {
1131 printRelocationTargetName(RENext, fmt);
1138 printRelocationTargetName(RE, fmt);
1143 printRelocationTargetName(RE, fmt);
1146 Result.append(fmtbuf.begin(), fmtbuf.end());
1147 return object_error::success;
1150 error_code MachOObjectFile::getRelocationHidden(DataRefImpl Rel,
1151 bool &Result) const {
1152 InMemoryStruct<macho::RelocationEntry> RE;
1153 getRelocation(Rel, RE);
1155 unsigned Arch = getArch();
1156 bool isScattered = (Arch != Triple::x86_64) &&
1157 (RE->Word0 & macho::RF_Scattered);
1160 Type = (RE->Word0 >> 24) & 0xF;
1162 Type = (RE->Word1 >> 28) & 0xF;
1166 // On arches that use the generic relocations, GENERIC_RELOC_PAIR
1167 // is always hidden.
1168 if (Arch == Triple::x86 || Arch == Triple::arm) {
1169 if (Type == macho::RIT_Pair) Result = true;
1170 } else if (Arch == Triple::x86_64) {
1171 // On x86_64, X86_64_RELOC_UNSIGNED is hidden only when it follows
1172 // an X864_64_RELOC_SUBTRACTOR.
1173 if (Type == macho::RIT_X86_64_Unsigned && Rel.d.a > 0) {
1174 DataRefImpl RelPrev = Rel;
1176 InMemoryStruct<macho::RelocationEntry> REPrev;
1177 getRelocation(RelPrev, REPrev);
1179 unsigned PrevType = (REPrev->Word1 >> 28) & 0xF;
1181 if (PrevType == macho::RIT_X86_64_Subtractor) Result = true;
1185 return object_error::success;
1188 error_code MachOObjectFile::getLibraryNext(DataRefImpl LibData,
1189 LibraryRef &Res) const {
1190 report_fatal_error("Needed libraries unimplemented in MachOObjectFile");
1193 error_code MachOObjectFile::getLibraryPath(DataRefImpl LibData,
1194 StringRef &Res) const {
1195 report_fatal_error("Needed libraries unimplemented in MachOObjectFile");
1199 /*===-- Miscellaneous -----------------------------------------------------===*/
1201 uint8_t MachOObjectFile::getBytesInAddress() const {
1202 return MachOObj->is64Bit() ? 8 : 4;
1205 StringRef MachOObjectFile::getFileFormatName() const {
1206 if (!MachOObj->is64Bit()) {
1207 switch (MachOObj->getHeader().CPUType) {
1208 case llvm::MachO::CPUTypeI386:
1209 return "Mach-O 32-bit i386";
1210 case llvm::MachO::CPUTypeARM:
1211 return "Mach-O arm";
1212 case llvm::MachO::CPUTypePowerPC:
1213 return "Mach-O 32-bit ppc";
1215 assert((MachOObj->getHeader().CPUType & llvm::MachO::CPUArchABI64) == 0 &&
1216 "64-bit object file when we're not 64-bit?");
1217 return "Mach-O 32-bit unknown";
1221 switch (MachOObj->getHeader().CPUType) {
1222 case llvm::MachO::CPUTypeX86_64:
1223 return "Mach-O 64-bit x86-64";
1224 case llvm::MachO::CPUTypePowerPC64:
1225 return "Mach-O 64-bit ppc64";
1227 assert((MachOObj->getHeader().CPUType & llvm::MachO::CPUArchABI64) == 1 &&
1228 "32-bit object file when we're 64-bit?");
1229 return "Mach-O 64-bit unknown";
1233 unsigned MachOObjectFile::getArch() const {
1234 switch (MachOObj->getHeader().CPUType) {
1235 case llvm::MachO::CPUTypeI386:
1237 case llvm::MachO::CPUTypeX86_64:
1238 return Triple::x86_64;
1239 case llvm::MachO::CPUTypeARM:
1241 case llvm::MachO::CPUTypePowerPC:
1243 case llvm::MachO::CPUTypePowerPC64:
1244 return Triple::ppc64;
1246 return Triple::UnknownArch;
1250 } // end namespace object
1251 } // end namespace llvm