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::ID_MachO, Object, ec),
35 RegisteredStringTable(std::numeric_limits<uint32_t>::max()) {
37 moveToNextSection(DRI);
38 uint32_t LoadCommandCount = MachOObj->getHeader().NumLoadCommands;
39 while (DRI.d.a < LoadCommandCount) {
40 Sections.push_back(DRI);
42 moveToNextSection(DRI);
47 ObjectFile *ObjectFile::createMachOObjectFile(MemoryBuffer *Buffer) {
50 MachOObject *MachOObj = MachOObject::LoadFromBuffer(Buffer, &Err);
53 return new MachOObjectFile(Buffer, MachOObj, ec);
56 /*===-- Symbols -----------------------------------------------------------===*/
58 void MachOObjectFile::moveToNextSymbol(DataRefImpl &DRI) const {
59 uint32_t LoadCommandCount = MachOObj->getHeader().NumLoadCommands;
60 while (DRI.d.a < LoadCommandCount) {
61 LoadCommandInfo LCI = MachOObj->getLoadCommandInfo(DRI.d.a);
62 if (LCI.Command.Type == macho::LCT_Symtab) {
63 InMemoryStruct<macho::SymtabLoadCommand> SymtabLoadCmd;
64 MachOObj->ReadSymtabLoadCommand(LCI, SymtabLoadCmd);
65 if (DRI.d.b < SymtabLoadCmd->NumSymbolTableEntries)
74 void MachOObjectFile::getSymbolTableEntry(DataRefImpl DRI,
75 InMemoryStruct<macho::SymbolTableEntry> &Res) const {
76 InMemoryStruct<macho::SymtabLoadCommand> SymtabLoadCmd;
77 LoadCommandInfo LCI = MachOObj->getLoadCommandInfo(DRI.d.a);
78 MachOObj->ReadSymtabLoadCommand(LCI, SymtabLoadCmd);
80 if (RegisteredStringTable != DRI.d.a) {
81 MachOObj->RegisterStringTable(*SymtabLoadCmd);
82 RegisteredStringTable = DRI.d.a;
85 MachOObj->ReadSymbolTableEntry(SymtabLoadCmd->SymbolTableOffset, DRI.d.b,
89 void MachOObjectFile::getSymbol64TableEntry(DataRefImpl DRI,
90 InMemoryStruct<macho::Symbol64TableEntry> &Res) const {
91 InMemoryStruct<macho::SymtabLoadCommand> SymtabLoadCmd;
92 LoadCommandInfo LCI = MachOObj->getLoadCommandInfo(DRI.d.a);
93 MachOObj->ReadSymtabLoadCommand(LCI, SymtabLoadCmd);
95 if (RegisteredStringTable != DRI.d.a) {
96 MachOObj->RegisterStringTable(*SymtabLoadCmd);
97 RegisteredStringTable = DRI.d.a;
100 MachOObj->ReadSymbol64TableEntry(SymtabLoadCmd->SymbolTableOffset, DRI.d.b,
105 error_code MachOObjectFile::getSymbolNext(DataRefImpl DRI,
106 SymbolRef &Result) const {
108 moveToNextSymbol(DRI);
109 Result = SymbolRef(DRI, this);
110 return object_error::success;
113 error_code MachOObjectFile::getSymbolName(DataRefImpl DRI,
114 StringRef &Result) const {
115 if (MachOObj->is64Bit()) {
116 InMemoryStruct<macho::Symbol64TableEntry> Entry;
117 getSymbol64TableEntry(DRI, Entry);
118 Result = MachOObj->getStringAtIndex(Entry->StringIndex);
120 InMemoryStruct<macho::SymbolTableEntry> Entry;
121 getSymbolTableEntry(DRI, Entry);
122 Result = MachOObj->getStringAtIndex(Entry->StringIndex);
124 return object_error::success;
127 error_code MachOObjectFile::getSymbolFileOffset(DataRefImpl DRI,
128 uint64_t &Result) const {
129 if (MachOObj->is64Bit()) {
130 InMemoryStruct<macho::Symbol64TableEntry> Entry;
131 getSymbol64TableEntry(DRI, Entry);
132 Result = Entry->Value;
133 if (Entry->SectionIndex) {
134 InMemoryStruct<macho::Section64> Section;
135 getSection64(Sections[Entry->SectionIndex-1], Section);
136 Result += Section->Offset - Section->Address;
139 InMemoryStruct<macho::SymbolTableEntry> Entry;
140 getSymbolTableEntry(DRI, Entry);
141 Result = Entry->Value;
142 if (Entry->SectionIndex) {
143 InMemoryStruct<macho::Section> Section;
144 getSection(Sections[Entry->SectionIndex-1], Section);
145 Result += Section->Offset - Section->Address;
149 return object_error::success;
152 error_code MachOObjectFile::getSymbolAddress(DataRefImpl DRI,
153 uint64_t &Result) const {
154 if (MachOObj->is64Bit()) {
155 InMemoryStruct<macho::Symbol64TableEntry> Entry;
156 getSymbol64TableEntry(DRI, Entry);
157 Result = Entry->Value;
159 InMemoryStruct<macho::SymbolTableEntry> Entry;
160 getSymbolTableEntry(DRI, Entry);
161 Result = Entry->Value;
163 return object_error::success;
166 error_code MachOObjectFile::getSymbolSize(DataRefImpl DRI,
167 uint64_t &Result) const {
168 uint32_t LoadCommandCount = MachOObj->getHeader().NumLoadCommands;
169 uint64_t BeginOffset;
170 uint64_t EndOffset = 0;
171 uint8_t SectionIndex;
172 if (MachOObj->is64Bit()) {
173 InMemoryStruct<macho::Symbol64TableEntry> Entry;
174 getSymbol64TableEntry(DRI, Entry);
175 BeginOffset = Entry->Value;
176 SectionIndex = Entry->SectionIndex;
178 uint32_t flags = SymbolRef::SF_None;
179 getSymbolFlags(DRI, flags);
180 if (flags & SymbolRef::SF_Common)
181 Result = Entry->Value;
183 Result = UnknownAddressOrSize;
184 return object_error::success;
186 // Unfortunately symbols are unsorted so we need to touch all
187 // symbols from load command
189 uint32_t Command = DRI.d.a;
190 while (Command == DRI.d.a) {
191 moveToNextSymbol(DRI);
192 if (DRI.d.a < LoadCommandCount) {
193 getSymbol64TableEntry(DRI, Entry);
194 if (Entry->SectionIndex == SectionIndex && Entry->Value > BeginOffset)
195 if (!EndOffset || Entry->Value < EndOffset)
196 EndOffset = Entry->Value;
201 InMemoryStruct<macho::SymbolTableEntry> Entry;
202 getSymbolTableEntry(DRI, Entry);
203 BeginOffset = Entry->Value;
204 SectionIndex = Entry->SectionIndex;
206 uint32_t flags = SymbolRef::SF_None;
207 getSymbolFlags(DRI, flags);
208 if (flags & SymbolRef::SF_Common)
209 Result = Entry->Value;
211 Result = UnknownAddressOrSize;
212 return object_error::success;
214 // Unfortunately symbols are unsorted so we need to touch all
215 // symbols from load command
217 uint32_t Command = DRI.d.a;
218 while (Command == DRI.d.a) {
219 moveToNextSymbol(DRI);
220 if (DRI.d.a < LoadCommandCount) {
221 getSymbolTableEntry(DRI, Entry);
222 if (Entry->SectionIndex == SectionIndex && Entry->Value > BeginOffset)
223 if (!EndOffset || Entry->Value < EndOffset)
224 EndOffset = Entry->Value;
231 getSectionSize(Sections[SectionIndex-1], Size);
232 getSectionAddress(Sections[SectionIndex-1], EndOffset);
235 Result = EndOffset - BeginOffset;
236 return object_error::success;
239 error_code MachOObjectFile::getSymbolNMTypeChar(DataRefImpl DRI,
240 char &Result) const {
242 if (MachOObj->is64Bit()) {
243 InMemoryStruct<macho::Symbol64TableEntry> Entry;
244 getSymbol64TableEntry(DRI, Entry);
246 Flags = Entry->Flags;
248 InMemoryStruct<macho::SymbolTableEntry> Entry;
249 getSymbolTableEntry(DRI, Entry);
251 Flags = Entry->Flags;
255 switch (Type & macho::STF_TypeMask) {
256 case macho::STT_Undefined:
259 case macho::STT_Absolute:
260 case macho::STT_Section:
268 if (Flags & (macho::STF_External | macho::STF_PrivateExtern))
269 Char = toupper(Char);
271 return object_error::success;
274 error_code MachOObjectFile::getSymbolFlags(DataRefImpl DRI,
275 uint32_t &Result) const {
278 if (MachOObj->is64Bit()) {
279 InMemoryStruct<macho::Symbol64TableEntry> Entry;
280 getSymbol64TableEntry(DRI, Entry);
281 MachOFlags = Entry->Flags;
282 MachOType = Entry->Type;
284 InMemoryStruct<macho::SymbolTableEntry> Entry;
285 getSymbolTableEntry(DRI, Entry);
286 MachOFlags = Entry->Flags;
287 MachOType = Entry->Type;
290 // TODO: Correctly set SF_ThreadLocal
291 Result = SymbolRef::SF_None;
293 if ((MachOType & MachO::NlistMaskType) == MachO::NListTypeUndefined)
294 Result |= SymbolRef::SF_Undefined;
296 if (MachOFlags & macho::STF_StabsEntryMask)
297 Result |= SymbolRef::SF_FormatSpecific;
299 if (MachOType & MachO::NlistMaskExternal) {
300 Result |= SymbolRef::SF_Global;
301 if ((MachOType & MachO::NlistMaskType) == MachO::NListTypeUndefined)
302 Result |= SymbolRef::SF_Common;
305 if (MachOFlags & (MachO::NListDescWeakRef | MachO::NListDescWeakDef))
306 Result |= SymbolRef::SF_Weak;
308 if ((MachOType & MachO::NlistMaskType) == MachO::NListTypeAbsolute)
309 Result |= SymbolRef::SF_Absolute;
311 return object_error::success;
314 error_code MachOObjectFile::getSymbolSection(DataRefImpl Symb,
315 section_iterator &Res) const {
317 if (MachOObj->is64Bit()) {
318 InMemoryStruct<macho::Symbol64TableEntry> Entry;
319 getSymbol64TableEntry(Symb, Entry);
320 index = Entry->SectionIndex;
322 InMemoryStruct<macho::SymbolTableEntry> Entry;
323 getSymbolTableEntry(Symb, Entry);
324 index = Entry->SectionIndex;
328 Res = end_sections();
330 Res = section_iterator(SectionRef(Sections[index-1], this));
332 return object_error::success;
335 error_code MachOObjectFile::getSymbolType(DataRefImpl Symb,
336 SymbolRef::Type &Res) const {
338 if (MachOObj->is64Bit()) {
339 InMemoryStruct<macho::Symbol64TableEntry> Entry;
340 getSymbol64TableEntry(Symb, Entry);
341 n_type = Entry->Type;
343 InMemoryStruct<macho::SymbolTableEntry> Entry;
344 getSymbolTableEntry(Symb, Entry);
345 n_type = Entry->Type;
347 Res = SymbolRef::ST_Other;
349 // If this is a STAB debugging symbol, we can do nothing more.
350 if (n_type & MachO::NlistMaskStab) {
351 Res = SymbolRef::ST_Debug;
352 return object_error::success;
355 switch (n_type & MachO::NlistMaskType) {
356 case MachO::NListTypeUndefined :
357 Res = SymbolRef::ST_Unknown;
359 case MachO::NListTypeSection :
360 Res = SymbolRef::ST_Function;
363 return object_error::success;
366 error_code MachOObjectFile::getSymbolValue(DataRefImpl Symb,
367 uint64_t &Val) const {
368 report_fatal_error("getSymbolValue unimplemented in MachOObjectFile");
371 symbol_iterator MachOObjectFile::begin_symbols() const {
372 // DRI.d.a = segment number; DRI.d.b = symbol index.
374 moveToNextSymbol(DRI);
375 return symbol_iterator(SymbolRef(DRI, this));
378 symbol_iterator MachOObjectFile::end_symbols() const {
380 DRI.d.a = MachOObj->getHeader().NumLoadCommands;
381 return symbol_iterator(SymbolRef(DRI, this));
384 symbol_iterator MachOObjectFile::begin_dynamic_symbols() const {
386 report_fatal_error("Dynamic symbols unimplemented in MachOObjectFile");
389 symbol_iterator MachOObjectFile::end_dynamic_symbols() const {
391 report_fatal_error("Dynamic symbols unimplemented in MachOObjectFile");
394 library_iterator MachOObjectFile::begin_libraries_needed() const {
396 report_fatal_error("Needed libraries unimplemented in MachOObjectFile");
399 library_iterator MachOObjectFile::end_libraries_needed() const {
401 report_fatal_error("Needed libraries unimplemented in MachOObjectFile");
404 StringRef MachOObjectFile::getLoadName() const {
406 report_fatal_error("get_load_name() unimplemented in MachOObjectFile");
409 /*===-- Sections ----------------------------------------------------------===*/
411 void MachOObjectFile::moveToNextSection(DataRefImpl &DRI) const {
412 uint32_t LoadCommandCount = MachOObj->getHeader().NumLoadCommands;
413 while (DRI.d.a < LoadCommandCount) {
414 LoadCommandInfo LCI = MachOObj->getLoadCommandInfo(DRI.d.a);
415 if (LCI.Command.Type == macho::LCT_Segment) {
416 InMemoryStruct<macho::SegmentLoadCommand> SegmentLoadCmd;
417 MachOObj->ReadSegmentLoadCommand(LCI, SegmentLoadCmd);
418 if (DRI.d.b < SegmentLoadCmd->NumSections)
420 } else if (LCI.Command.Type == macho::LCT_Segment64) {
421 InMemoryStruct<macho::Segment64LoadCommand> Segment64LoadCmd;
422 MachOObj->ReadSegment64LoadCommand(LCI, Segment64LoadCmd);
423 if (DRI.d.b < Segment64LoadCmd->NumSections)
432 error_code MachOObjectFile::getSectionNext(DataRefImpl DRI,
433 SectionRef &Result) const {
435 moveToNextSection(DRI);
436 Result = SectionRef(DRI, this);
437 return object_error::success;
441 MachOObjectFile::getSection(DataRefImpl DRI,
442 InMemoryStruct<macho::Section> &Res) const {
443 InMemoryStruct<macho::SegmentLoadCommand> SLC;
444 LoadCommandInfo LCI = MachOObj->getLoadCommandInfo(DRI.d.a);
445 MachOObj->ReadSegmentLoadCommand(LCI, SLC);
446 MachOObj->ReadSection(LCI, DRI.d.b, Res);
449 std::size_t MachOObjectFile::getSectionIndex(DataRefImpl Sec) const {
450 SectionList::const_iterator loc =
451 std::find(Sections.begin(), Sections.end(), Sec);
452 assert(loc != Sections.end() && "Sec is not a valid section!");
453 return std::distance(Sections.begin(), loc);
457 MachOObjectFile::getSection64(DataRefImpl DRI,
458 InMemoryStruct<macho::Section64> &Res) const {
459 InMemoryStruct<macho::Segment64LoadCommand> SLC;
460 LoadCommandInfo LCI = MachOObj->getLoadCommandInfo(DRI.d.a);
461 MachOObj->ReadSegment64LoadCommand(LCI, SLC);
462 MachOObj->ReadSection64(LCI, DRI.d.b, Res);
465 static bool is64BitLoadCommand(const MachOObject *MachOObj, DataRefImpl DRI) {
466 LoadCommandInfo LCI = MachOObj->getLoadCommandInfo(DRI.d.a);
467 if (LCI.Command.Type == macho::LCT_Segment64)
469 assert(LCI.Command.Type == macho::LCT_Segment && "Unexpected Type.");
473 error_code MachOObjectFile::getSectionName(DataRefImpl DRI,
474 StringRef &Result) const {
475 // FIXME: thread safety.
476 static char result[34];
477 if (is64BitLoadCommand(MachOObj, DRI)) {
478 InMemoryStruct<macho::Segment64LoadCommand> SLC;
479 LoadCommandInfo LCI = MachOObj->getLoadCommandInfo(DRI.d.a);
480 MachOObj->ReadSegment64LoadCommand(LCI, SLC);
481 InMemoryStruct<macho::Section64> Sect;
482 MachOObj->ReadSection64(LCI, DRI.d.b, Sect);
484 strcpy(result, Sect->SegmentName);
486 strcat(result, Sect->Name);
488 InMemoryStruct<macho::SegmentLoadCommand> SLC;
489 LoadCommandInfo LCI = MachOObj->getLoadCommandInfo(DRI.d.a);
490 MachOObj->ReadSegmentLoadCommand(LCI, SLC);
491 InMemoryStruct<macho::Section> Sect;
492 MachOObj->ReadSection(LCI, DRI.d.b, Sect);
494 strcpy(result, Sect->SegmentName);
496 strcat(result, Sect->Name);
498 Result = StringRef(result);
499 return object_error::success;
502 error_code MachOObjectFile::getSectionAddress(DataRefImpl DRI,
503 uint64_t &Result) const {
504 if (is64BitLoadCommand(MachOObj, DRI)) {
505 InMemoryStruct<macho::Section64> Sect;
506 getSection64(DRI, Sect);
507 Result = Sect->Address;
509 InMemoryStruct<macho::Section> Sect;
510 getSection(DRI, Sect);
511 Result = Sect->Address;
513 return object_error::success;
516 error_code MachOObjectFile::getSectionSize(DataRefImpl DRI,
517 uint64_t &Result) const {
518 if (is64BitLoadCommand(MachOObj, DRI)) {
519 InMemoryStruct<macho::Section64> Sect;
520 getSection64(DRI, Sect);
523 InMemoryStruct<macho::Section> Sect;
524 getSection(DRI, Sect);
527 return object_error::success;
530 error_code MachOObjectFile::getSectionContents(DataRefImpl DRI,
531 StringRef &Result) const {
532 if (is64BitLoadCommand(MachOObj, DRI)) {
533 InMemoryStruct<macho::Section64> Sect;
534 getSection64(DRI, Sect);
535 Result = MachOObj->getData(Sect->Offset, Sect->Size);
537 InMemoryStruct<macho::Section> Sect;
538 getSection(DRI, Sect);
539 Result = MachOObj->getData(Sect->Offset, Sect->Size);
541 return object_error::success;
544 error_code MachOObjectFile::getSectionAlignment(DataRefImpl DRI,
545 uint64_t &Result) const {
546 if (is64BitLoadCommand(MachOObj, DRI)) {
547 InMemoryStruct<macho::Section64> Sect;
548 getSection64(DRI, Sect);
549 Result = uint64_t(1) << Sect->Align;
551 InMemoryStruct<macho::Section> Sect;
552 getSection(DRI, Sect);
553 Result = uint64_t(1) << Sect->Align;
555 return object_error::success;
558 error_code MachOObjectFile::isSectionText(DataRefImpl DRI,
559 bool &Result) const {
560 if (is64BitLoadCommand(MachOObj, DRI)) {
561 InMemoryStruct<macho::Section64> Sect;
562 getSection64(DRI, Sect);
563 Result = !strcmp(Sect->Name, "__text");
565 InMemoryStruct<macho::Section> Sect;
566 getSection(DRI, Sect);
567 Result = !strcmp(Sect->Name, "__text");
569 return object_error::success;
572 error_code MachOObjectFile::isSectionData(DataRefImpl DRI,
573 bool &Result) const {
574 // FIXME: Unimplemented.
576 return object_error::success;
579 error_code MachOObjectFile::isSectionBSS(DataRefImpl DRI,
580 bool &Result) const {
581 // FIXME: Unimplemented.
583 return object_error::success;
586 error_code MachOObjectFile::isSectionRequiredForExecution(DataRefImpl Sec,
587 bool &Result) const {
588 // FIXME: Unimplemented.
590 return object_error::success;
593 error_code MachOObjectFile::isSectionVirtual(DataRefImpl Sec,
594 bool &Result) const {
595 // FIXME: Unimplemented.
597 return object_error::success;
600 error_code MachOObjectFile::isSectionZeroInit(DataRefImpl DRI,
601 bool &Result) const {
602 if (MachOObj->is64Bit()) {
603 InMemoryStruct<macho::Section64> Sect;
604 getSection64(DRI, Sect);
605 unsigned SectionType = Sect->Flags & MachO::SectionFlagMaskSectionType;
606 Result = (SectionType == MachO::SectionTypeZeroFill ||
607 SectionType == MachO::SectionTypeZeroFillLarge);
609 InMemoryStruct<macho::Section> Sect;
610 getSection(DRI, Sect);
611 unsigned SectionType = Sect->Flags & MachO::SectionFlagMaskSectionType;
612 Result = (SectionType == MachO::SectionTypeZeroFill ||
613 SectionType == MachO::SectionTypeZeroFillLarge);
616 return object_error::success;
619 error_code MachOObjectFile::isSectionReadOnlyData(DataRefImpl Sec,
620 bool &Result) const {
621 // Consider using the code from isSectionText to look for __const sections.
622 // Alternately, emit S_ATTR_PURE_INSTRUCTIONS and/or S_ATTR_SOME_INSTRUCTIONS
623 // to use section attributes to distinguish code from data.
625 // FIXME: Unimplemented.
627 return object_error::success;
630 error_code MachOObjectFile::sectionContainsSymbol(DataRefImpl Sec,
632 bool &Result) const {
634 getSymbolType(Symb, ST);
635 if (ST == SymbolRef::ST_Unknown) {
637 return object_error::success;
640 uint64_t SectBegin, SectEnd;
641 getSectionAddress(Sec, SectBegin);
642 getSectionSize(Sec, SectEnd);
643 SectEnd += SectBegin;
645 if (MachOObj->is64Bit()) {
646 InMemoryStruct<macho::Symbol64TableEntry> Entry;
647 getSymbol64TableEntry(Symb, Entry);
648 uint64_t SymAddr= Entry->Value;
649 Result = (SymAddr >= SectBegin) && (SymAddr < SectEnd);
651 InMemoryStruct<macho::SymbolTableEntry> Entry;
652 getSymbolTableEntry(Symb, Entry);
653 uint64_t SymAddr= Entry->Value;
654 Result = (SymAddr >= SectBegin) && (SymAddr < SectEnd);
657 return object_error::success;
660 relocation_iterator MachOObjectFile::getSectionRelBegin(DataRefImpl Sec) const {
662 ret.d.b = getSectionIndex(Sec);
663 return relocation_iterator(RelocationRef(ret, this));
665 relocation_iterator MachOObjectFile::getSectionRelEnd(DataRefImpl Sec) const {
667 if (is64BitLoadCommand(MachOObj, Sec)) {
668 InMemoryStruct<macho::Section64> Sect;
669 getSection64(Sec, Sect);
670 last_reloc = Sect->NumRelocationTableEntries;
672 InMemoryStruct<macho::Section> Sect;
673 getSection(Sec, Sect);
674 last_reloc = Sect->NumRelocationTableEntries;
677 ret.d.a = last_reloc;
678 ret.d.b = getSectionIndex(Sec);
679 return relocation_iterator(RelocationRef(ret, this));
682 section_iterator MachOObjectFile::begin_sections() const {
684 moveToNextSection(DRI);
685 return section_iterator(SectionRef(DRI, this));
688 section_iterator MachOObjectFile::end_sections() const {
690 DRI.d.a = MachOObj->getHeader().NumLoadCommands;
691 return section_iterator(SectionRef(DRI, this));
694 /*===-- Relocations -------------------------------------------------------===*/
696 void MachOObjectFile::
697 getRelocation(DataRefImpl Rel,
698 InMemoryStruct<macho::RelocationEntry> &Res) const {
700 if (MachOObj->is64Bit()) {
701 InMemoryStruct<macho::Section64> Sect;
702 getSection64(Sections[Rel.d.b], Sect);
703 relOffset = Sect->RelocationTableOffset;
705 InMemoryStruct<macho::Section> Sect;
706 getSection(Sections[Rel.d.b], Sect);
707 relOffset = Sect->RelocationTableOffset;
709 MachOObj->ReadRelocationEntry(relOffset, Rel.d.a, Res);
711 error_code MachOObjectFile::getRelocationNext(DataRefImpl Rel,
712 RelocationRef &Res) const {
714 Res = RelocationRef(Rel, this);
715 return object_error::success;
717 error_code MachOObjectFile::getRelocationAddress(DataRefImpl Rel,
718 uint64_t &Res) const {
719 const uint8_t* sectAddress = 0;
720 if (MachOObj->is64Bit()) {
721 InMemoryStruct<macho::Section64> Sect;
722 getSection64(Sections[Rel.d.b], Sect);
723 sectAddress += Sect->Address;
725 InMemoryStruct<macho::Section> Sect;
726 getSection(Sections[Rel.d.b], Sect);
727 sectAddress += Sect->Address;
729 InMemoryStruct<macho::RelocationEntry> RE;
730 getRelocation(Rel, RE);
732 unsigned Arch = getArch();
733 bool isScattered = (Arch != Triple::x86_64) &&
734 (RE->Word0 & macho::RF_Scattered);
735 uint64_t RelAddr = 0;
737 RelAddr = RE->Word0 & 0xFFFFFF;
741 Res = reinterpret_cast<uintptr_t>(sectAddress + RelAddr);
742 return object_error::success;
744 error_code MachOObjectFile::getRelocationOffset(DataRefImpl Rel,
745 uint64_t &Res) const {
746 InMemoryStruct<macho::RelocationEntry> RE;
747 getRelocation(Rel, RE);
749 unsigned Arch = getArch();
750 bool isScattered = (Arch != Triple::x86_64) &&
751 (RE->Word0 & macho::RF_Scattered);
753 Res = RE->Word0 & 0xFFFFFF;
756 return object_error::success;
758 error_code MachOObjectFile::getRelocationSymbol(DataRefImpl Rel,
759 SymbolRef &Res) const {
760 InMemoryStruct<macho::RelocationEntry> RE;
761 getRelocation(Rel, RE);
762 uint32_t SymbolIdx = RE->Word1 & 0xffffff;
763 bool isExtern = (RE->Word1 >> 27) & 1;
766 moveToNextSymbol(Sym);
768 for (unsigned i = 0; i < SymbolIdx; i++) {
770 moveToNextSymbol(Sym);
771 assert(Sym.d.a < MachOObj->getHeader().NumLoadCommands &&
772 "Relocation symbol index out of range!");
775 Res = SymbolRef(Sym, this);
776 return object_error::success;
778 error_code MachOObjectFile::getRelocationType(DataRefImpl Rel,
779 uint64_t &Res) const {
780 InMemoryStruct<macho::RelocationEntry> RE;
781 getRelocation(Rel, RE);
785 return object_error::success;
787 error_code MachOObjectFile::getRelocationTypeName(DataRefImpl Rel,
788 SmallVectorImpl<char> &Result) const {
789 // TODO: Support scattered relocations.
791 InMemoryStruct<macho::RelocationEntry> RE;
792 getRelocation(Rel, RE);
794 unsigned Arch = getArch();
795 bool isScattered = (Arch != Triple::x86_64) &&
796 (RE->Word0 & macho::RF_Scattered);
800 r_type = (RE->Word0 >> 24) & 0xF;
802 r_type = (RE->Word1 >> 28) & 0xF;
806 static const char *const Table[] = {
807 "GENERIC_RELOC_VANILLA",
808 "GENERIC_RELOC_PAIR",
809 "GENERIC_RELOC_SECTDIFF",
810 "GENERIC_RELOC_PB_LA_PTR",
811 "GENERIC_RELOC_LOCAL_SECTDIFF",
812 "GENERIC_RELOC_TLV" };
820 case Triple::x86_64: {
821 static const char *const Table[] = {
822 "X86_64_RELOC_UNSIGNED",
823 "X86_64_RELOC_SIGNED",
824 "X86_64_RELOC_BRANCH",
825 "X86_64_RELOC_GOT_LOAD",
827 "X86_64_RELOC_SUBTRACTOR",
828 "X86_64_RELOC_SIGNED_1",
829 "X86_64_RELOC_SIGNED_2",
830 "X86_64_RELOC_SIGNED_4",
831 "X86_64_RELOC_TLV" };
840 static const char *const Table[] = {
843 "ARM_RELOC_SECTDIFF",
844 "ARM_RELOC_LOCAL_SECTDIFF",
845 "ARM_RELOC_PB_LA_PTR",
847 "ARM_THUMB_RELOC_BR22",
848 "ARM_THUMB_32BIT_BRANCH",
850 "ARM_RELOC_HALF_SECTDIFF" };
859 static const char *const Table[] = {
868 "PPC_RELOC_SECTDIFF",
869 "PPC_RELOC_PB_LA_PTR",
870 "PPC_RELOC_HI16_SECTDIFF",
871 "PPC_RELOC_LO16_SECTDIFF",
872 "PPC_RELOC_HA16_SECTDIFF",
874 "PPC_RELOC_LO14_SECTDIFF",
875 "PPC_RELOC_LOCAL_SECTDIFF" };
880 case Triple::UnknownArch:
884 Result.append(res.begin(), res.end());
885 return object_error::success;
887 error_code MachOObjectFile::getRelocationAdditionalInfo(DataRefImpl Rel,
888 int64_t &Res) const {
889 InMemoryStruct<macho::RelocationEntry> RE;
890 getRelocation(Rel, RE);
891 bool isExtern = (RE->Word1 >> 27) & 1;
894 const uint8_t* sectAddress = base();
895 if (MachOObj->is64Bit()) {
896 InMemoryStruct<macho::Section64> Sect;
897 getSection64(Sections[Rel.d.b], Sect);
898 sectAddress += Sect->Offset;
900 InMemoryStruct<macho::Section> Sect;
901 getSection(Sections[Rel.d.b], Sect);
902 sectAddress += Sect->Offset;
904 Res = reinterpret_cast<uintptr_t>(sectAddress);
906 return object_error::success;
909 // Helper to advance a section or symbol iterator multiple increments at a time.
911 error_code advance(T &it, size_t Val) {
920 void advanceTo(T &it, size_t Val) {
921 if (error_code ec = advance(it, Val))
922 report_fatal_error(ec.message());
925 void MachOObjectFile::printRelocationTargetName(
926 InMemoryStruct<macho::RelocationEntry>& RE,
927 raw_string_ostream &fmt) const {
928 unsigned Arch = getArch();
929 bool isScattered = (Arch != Triple::x86_64) &&
930 (RE->Word0 & macho::RF_Scattered);
932 // Target of a scattered relocation is an address. In the interest of
933 // generating pretty output, scan through the symbol table looking for a
934 // symbol that aligns with that address. If we find one, print it.
935 // Otherwise, we just print the hex address of the target.
937 uint32_t Val = RE->Word1;
940 for (symbol_iterator SI = begin_symbols(), SE = end_symbols(); SI != SE;
942 if (ec) report_fatal_error(ec.message());
947 if ((ec = SI->getAddress(Addr)))
948 report_fatal_error(ec.message());
949 if (Addr != Val) continue;
950 if ((ec = SI->getName(Name)))
951 report_fatal_error(ec.message());
956 // If we couldn't find a symbol that this relocation refers to, try
957 // to find a section beginning instead.
958 for (section_iterator SI = begin_sections(), SE = end_sections(); SI != SE;
960 if (ec) report_fatal_error(ec.message());
965 if ((ec = SI->getAddress(Addr)))
966 report_fatal_error(ec.message());
967 if (Addr != Val) continue;
968 if ((ec = SI->getName(Name)))
969 report_fatal_error(ec.message());
974 fmt << format("0x%x", Val);
979 bool isExtern = (RE->Word1 >> 27) & 1;
980 uint32_t Val = RE->Word1 & 0xFFFFFF;
983 symbol_iterator SI = begin_symbols();
987 section_iterator SI = begin_sections();
995 error_code MachOObjectFile::getRelocationValueString(DataRefImpl Rel,
996 SmallVectorImpl<char> &Result) const {
997 InMemoryStruct<macho::RelocationEntry> RE;
998 getRelocation(Rel, RE);
1000 unsigned Arch = getArch();
1001 bool isScattered = (Arch != Triple::x86_64) &&
1002 (RE->Word0 & macho::RF_Scattered);
1005 raw_string_ostream fmt(fmtbuf);
1009 Type = (RE->Word0 >> 24) & 0xF;
1011 Type = (RE->Word1 >> 28) & 0xF;
1015 isPCRel = ((RE->Word0 >> 30) & 1);
1017 isPCRel = ((RE->Word1 >> 24) & 1);
1019 // Determine any addends that should be displayed with the relocation.
1020 // These require decoding the relocation type, which is triple-specific.
1022 // X86_64 has entirely custom relocation types.
1023 if (Arch == Triple::x86_64) {
1024 bool isPCRel = ((RE->Word1 >> 24) & 1);
1027 case macho::RIT_X86_64_GOTLoad: // X86_64_RELOC_GOT_LOAD
1028 case macho::RIT_X86_64_GOT: { // X86_64_RELOC_GOT
1029 printRelocationTargetName(RE, fmt);
1031 if (isPCRel) fmt << "PCREL";
1034 case macho::RIT_X86_64_Subtractor: { // X86_64_RELOC_SUBTRACTOR
1035 InMemoryStruct<macho::RelocationEntry> RENext;
1036 DataRefImpl RelNext = Rel;
1038 getRelocation(RelNext, RENext);
1040 // X86_64_SUBTRACTOR must be followed by a relocation of type
1041 // X86_64_RELOC_UNSIGNED.
1042 // NOTE: Scattered relocations don't exist on x86_64.
1043 unsigned RType = (RENext->Word1 >> 28) & 0xF;
1045 report_fatal_error("Expected X86_64_RELOC_UNSIGNED after "
1046 "X86_64_RELOC_SUBTRACTOR.");
1048 // The X86_64_RELOC_UNSIGNED contains the minuend symbol,
1049 // X86_64_SUBTRACTOR contains to the subtrahend.
1050 printRelocationTargetName(RENext, fmt);
1052 printRelocationTargetName(RE, fmt);
1054 case macho::RIT_X86_64_TLV:
1055 printRelocationTargetName(RE, fmt);
1057 if (isPCRel) fmt << "P";
1059 case macho::RIT_X86_64_Signed1: // X86_64_RELOC_SIGNED1
1060 printRelocationTargetName(RE, fmt);
1063 case macho::RIT_X86_64_Signed2: // X86_64_RELOC_SIGNED2
1064 printRelocationTargetName(RE, fmt);
1067 case macho::RIT_X86_64_Signed4: // X86_64_RELOC_SIGNED4
1068 printRelocationTargetName(RE, fmt);
1072 printRelocationTargetName(RE, fmt);
1075 // X86 and ARM share some relocation types in common.
1076 } else if (Arch == Triple::x86 || Arch == Triple::arm) {
1077 // Generic relocation types...
1079 case macho::RIT_Pair: // GENERIC_RELOC_PAIR - prints no info
1080 return object_error::success;
1081 case macho::RIT_Difference: { // GENERIC_RELOC_SECTDIFF
1082 InMemoryStruct<macho::RelocationEntry> RENext;
1083 DataRefImpl RelNext = Rel;
1085 getRelocation(RelNext, RENext);
1087 // X86 sect diff's must be followed by a relocation of type
1088 // GENERIC_RELOC_PAIR.
1089 bool isNextScattered = (Arch != Triple::x86_64) &&
1090 (RENext->Word0 & macho::RF_Scattered);
1092 if (isNextScattered)
1093 RType = (RENext->Word0 >> 24) & 0xF;
1095 RType = (RENext->Word1 >> 28) & 0xF;
1097 report_fatal_error("Expected GENERIC_RELOC_PAIR after "
1098 "GENERIC_RELOC_SECTDIFF.");
1100 printRelocationTargetName(RE, fmt);
1102 printRelocationTargetName(RENext, fmt);
1107 if (Arch == Triple::x86) {
1108 // All X86 relocations that need special printing were already
1109 // handled in the generic code.
1111 case macho::RIT_Generic_LocalDifference:{// GENERIC_RELOC_LOCAL_SECTDIFF
1112 InMemoryStruct<macho::RelocationEntry> RENext;
1113 DataRefImpl RelNext = Rel;
1115 getRelocation(RelNext, RENext);
1117 // X86 sect diff's must be followed by a relocation of type
1118 // GENERIC_RELOC_PAIR.
1119 bool isNextScattered = (Arch != Triple::x86_64) &&
1120 (RENext->Word0 & macho::RF_Scattered);
1122 if (isNextScattered)
1123 RType = (RENext->Word0 >> 24) & 0xF;
1125 RType = (RENext->Word1 >> 28) & 0xF;
1127 report_fatal_error("Expected GENERIC_RELOC_PAIR after "
1128 "GENERIC_RELOC_LOCAL_SECTDIFF.");
1130 printRelocationTargetName(RE, fmt);
1132 printRelocationTargetName(RENext, fmt);
1135 case macho::RIT_Generic_TLV: {
1136 printRelocationTargetName(RE, fmt);
1138 if (isPCRel) fmt << "P";
1142 printRelocationTargetName(RE, fmt);
1144 } else { // ARM-specific relocations
1146 case macho::RIT_ARM_Half: // ARM_RELOC_HALF
1147 case macho::RIT_ARM_HalfDifference: { // ARM_RELOC_HALF_SECTDIFF
1148 // Half relocations steal a bit from the length field to encode
1149 // whether this is an upper16 or a lower16 relocation.
1152 isUpper = (RE->Word0 >> 28) & 1;
1154 isUpper = (RE->Word1 >> 25) & 1;
1157 fmt << ":upper16:(";
1159 fmt << ":lower16:(";
1160 printRelocationTargetName(RE, fmt);
1162 InMemoryStruct<macho::RelocationEntry> RENext;
1163 DataRefImpl RelNext = Rel;
1165 getRelocation(RelNext, RENext);
1167 // ARM half relocs must be followed by a relocation of type
1169 bool isNextScattered = (Arch != Triple::x86_64) &&
1170 (RENext->Word0 & macho::RF_Scattered);
1172 if (isNextScattered)
1173 RType = (RENext->Word0 >> 24) & 0xF;
1175 RType = (RENext->Word1 >> 28) & 0xF;
1178 report_fatal_error("Expected ARM_RELOC_PAIR after "
1179 "GENERIC_RELOC_HALF");
1181 // NOTE: The half of the target virtual address is stashed in the
1182 // address field of the secondary relocation, but we can't reverse
1183 // engineer the constant offset from it without decoding the movw/movt
1184 // instruction to find the other half in its immediate field.
1186 // ARM_RELOC_HALF_SECTDIFF encodes the second section in the
1187 // symbol/section pointer of the follow-on relocation.
1188 if (Type == macho::RIT_ARM_HalfDifference) {
1190 printRelocationTargetName(RENext, fmt);
1197 printRelocationTargetName(RE, fmt);
1202 printRelocationTargetName(RE, fmt);
1205 Result.append(fmtbuf.begin(), fmtbuf.end());
1206 return object_error::success;
1209 error_code MachOObjectFile::getRelocationHidden(DataRefImpl Rel,
1210 bool &Result) const {
1211 InMemoryStruct<macho::RelocationEntry> RE;
1212 getRelocation(Rel, RE);
1214 unsigned Arch = getArch();
1215 bool isScattered = (Arch != Triple::x86_64) &&
1216 (RE->Word0 & macho::RF_Scattered);
1219 Type = (RE->Word0 >> 24) & 0xF;
1221 Type = (RE->Word1 >> 28) & 0xF;
1225 // On arches that use the generic relocations, GENERIC_RELOC_PAIR
1226 // is always hidden.
1227 if (Arch == Triple::x86 || Arch == Triple::arm) {
1228 if (Type == macho::RIT_Pair) Result = true;
1229 } else if (Arch == Triple::x86_64) {
1230 // On x86_64, X86_64_RELOC_UNSIGNED is hidden only when it follows
1231 // an X864_64_RELOC_SUBTRACTOR.
1232 if (Type == macho::RIT_X86_64_Unsigned && Rel.d.a > 0) {
1233 DataRefImpl RelPrev = Rel;
1235 InMemoryStruct<macho::RelocationEntry> REPrev;
1236 getRelocation(RelPrev, REPrev);
1238 unsigned PrevType = (REPrev->Word1 >> 28) & 0xF;
1240 if (PrevType == macho::RIT_X86_64_Subtractor) Result = true;
1244 return object_error::success;
1247 error_code MachOObjectFile::getLibraryNext(DataRefImpl LibData,
1248 LibraryRef &Res) const {
1249 report_fatal_error("Needed libraries unimplemented in MachOObjectFile");
1252 error_code MachOObjectFile::getLibraryPath(DataRefImpl LibData,
1253 StringRef &Res) const {
1254 report_fatal_error("Needed libraries unimplemented in MachOObjectFile");
1258 /*===-- Miscellaneous -----------------------------------------------------===*/
1260 uint8_t MachOObjectFile::getBytesInAddress() const {
1261 return MachOObj->is64Bit() ? 8 : 4;
1264 StringRef MachOObjectFile::getFileFormatName() const {
1265 if (!MachOObj->is64Bit()) {
1266 switch (MachOObj->getHeader().CPUType) {
1267 case llvm::MachO::CPUTypeI386:
1268 return "Mach-O 32-bit i386";
1269 case llvm::MachO::CPUTypeARM:
1270 return "Mach-O arm";
1271 case llvm::MachO::CPUTypePowerPC:
1272 return "Mach-O 32-bit ppc";
1274 assert((MachOObj->getHeader().CPUType & llvm::MachO::CPUArchABI64) == 0 &&
1275 "64-bit object file when we're not 64-bit?");
1276 return "Mach-O 32-bit unknown";
1280 switch (MachOObj->getHeader().CPUType) {
1281 case llvm::MachO::CPUTypeX86_64:
1282 return "Mach-O 64-bit x86-64";
1283 case llvm::MachO::CPUTypePowerPC64:
1284 return "Mach-O 64-bit ppc64";
1286 assert((MachOObj->getHeader().CPUType & llvm::MachO::CPUArchABI64) == 1 &&
1287 "32-bit object file when we're 64-bit?");
1288 return "Mach-O 64-bit unknown";
1292 unsigned MachOObjectFile::getArch() const {
1293 switch (MachOObj->getHeader().CPUType) {
1294 case llvm::MachO::CPUTypeI386:
1296 case llvm::MachO::CPUTypeX86_64:
1297 return Triple::x86_64;
1298 case llvm::MachO::CPUTypeARM:
1300 case llvm::MachO::CPUTypePowerPC:
1302 case llvm::MachO::CPUTypePowerPC64:
1303 return Triple::ppc64;
1305 return Triple::UnknownArch;
1309 } // end namespace object
1310 } // end namespace llvm