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/Object/MachO.h"
16 #include "llvm/ADT/Triple.h"
17 #include "llvm/Object/MachOFormat.h"
18 #include "llvm/Support/Format.h"
19 #include "llvm/Support/MemoryBuffer.h"
25 using namespace object;
30 MachOObjectFile::MachOObjectFile(MemoryBuffer *Object, MachOObject *MOO,
32 : ObjectFile(Binary::ID_MachO, Object, ec),
34 RegisteredStringTable(std::numeric_limits<uint32_t>::max()) {
36 moveToNextSection(DRI);
37 uint32_t LoadCommandCount = MachOObj->getHeader().NumLoadCommands;
38 while (DRI.d.a < LoadCommandCount) {
39 Sections.push_back(DRI);
41 moveToNextSection(DRI);
46 ObjectFile *ObjectFile::createMachOObjectFile(MemoryBuffer *Buffer) {
49 MachOObject *MachOObj = MachOObject::LoadFromBuffer(Buffer, &Err);
52 // MachOObject takes ownership of the Buffer we passed to it, and
53 // MachOObjectFile does, too, so we need to make sure they don't get the
54 // same object. A MemoryBuffer is cheap (it's just a reference to memory,
55 // not a copy of the memory itself), so just make a new copy here for
56 // the MachOObjectFile.
57 MemoryBuffer *NewBuffer =
58 MemoryBuffer::getMemBuffer(Buffer->getBuffer(),
59 Buffer->getBufferIdentifier(), false);
60 return new MachOObjectFile(NewBuffer, MachOObj, ec);
63 /*===-- Symbols -----------------------------------------------------------===*/
65 void MachOObjectFile::moveToNextSymbol(DataRefImpl &DRI) const {
66 uint32_t LoadCommandCount = MachOObj->getHeader().NumLoadCommands;
67 while (DRI.d.a < LoadCommandCount) {
68 LoadCommandInfo LCI = MachOObj->getLoadCommandInfo(DRI.d.a);
69 if (LCI.Command.Type == macho::LCT_Symtab) {
70 InMemoryStruct<macho::SymtabLoadCommand> SymtabLoadCmd;
71 MachOObj->ReadSymtabLoadCommand(LCI, SymtabLoadCmd);
72 if (DRI.d.b < SymtabLoadCmd->NumSymbolTableEntries)
81 void MachOObjectFile::getSymbolTableEntry(DataRefImpl DRI,
82 InMemoryStruct<macho::SymbolTableEntry> &Res) const {
83 InMemoryStruct<macho::SymtabLoadCommand> SymtabLoadCmd;
84 LoadCommandInfo LCI = MachOObj->getLoadCommandInfo(DRI.d.a);
85 MachOObj->ReadSymtabLoadCommand(LCI, SymtabLoadCmd);
87 if (RegisteredStringTable != DRI.d.a) {
88 MachOObj->RegisterStringTable(*SymtabLoadCmd);
89 RegisteredStringTable = DRI.d.a;
92 MachOObj->ReadSymbolTableEntry(SymtabLoadCmd->SymbolTableOffset, DRI.d.b,
96 void MachOObjectFile::getSymbol64TableEntry(DataRefImpl DRI,
97 InMemoryStruct<macho::Symbol64TableEntry> &Res) const {
98 InMemoryStruct<macho::SymtabLoadCommand> SymtabLoadCmd;
99 LoadCommandInfo LCI = MachOObj->getLoadCommandInfo(DRI.d.a);
100 MachOObj->ReadSymtabLoadCommand(LCI, SymtabLoadCmd);
102 if (RegisteredStringTable != DRI.d.a) {
103 MachOObj->RegisterStringTable(*SymtabLoadCmd);
104 RegisteredStringTable = DRI.d.a;
107 MachOObj->ReadSymbol64TableEntry(SymtabLoadCmd->SymbolTableOffset, DRI.d.b,
112 error_code MachOObjectFile::getSymbolNext(DataRefImpl DRI,
113 SymbolRef &Result) const {
115 moveToNextSymbol(DRI);
116 Result = SymbolRef(DRI, this);
117 return object_error::success;
120 error_code MachOObjectFile::getSymbolName(DataRefImpl DRI,
121 StringRef &Result) const {
122 if (MachOObj->is64Bit()) {
123 InMemoryStruct<macho::Symbol64TableEntry> Entry;
124 getSymbol64TableEntry(DRI, Entry);
125 Result = MachOObj->getStringAtIndex(Entry->StringIndex);
127 InMemoryStruct<macho::SymbolTableEntry> Entry;
128 getSymbolTableEntry(DRI, Entry);
129 Result = MachOObj->getStringAtIndex(Entry->StringIndex);
131 return object_error::success;
134 error_code MachOObjectFile::getSymbolFileOffset(DataRefImpl DRI,
135 uint64_t &Result) const {
136 if (MachOObj->is64Bit()) {
137 InMemoryStruct<macho::Symbol64TableEntry> Entry;
138 getSymbol64TableEntry(DRI, Entry);
139 Result = Entry->Value;
140 if (Entry->SectionIndex) {
141 InMemoryStruct<macho::Section64> Section;
142 getSection64(Sections[Entry->SectionIndex-1], Section);
143 Result += Section->Offset - Section->Address;
146 InMemoryStruct<macho::SymbolTableEntry> Entry;
147 getSymbolTableEntry(DRI, Entry);
148 Result = Entry->Value;
149 if (Entry->SectionIndex) {
150 InMemoryStruct<macho::Section> Section;
151 getSection(Sections[Entry->SectionIndex-1], Section);
152 Result += Section->Offset - Section->Address;
156 return object_error::success;
159 error_code MachOObjectFile::getSymbolAddress(DataRefImpl DRI,
160 uint64_t &Result) const {
161 if (MachOObj->is64Bit()) {
162 InMemoryStruct<macho::Symbol64TableEntry> Entry;
163 getSymbol64TableEntry(DRI, Entry);
164 Result = Entry->Value;
166 InMemoryStruct<macho::SymbolTableEntry> Entry;
167 getSymbolTableEntry(DRI, Entry);
168 Result = Entry->Value;
170 return object_error::success;
173 error_code MachOObjectFile::getSymbolSize(DataRefImpl DRI,
174 uint64_t &Result) const {
175 uint32_t LoadCommandCount = MachOObj->getHeader().NumLoadCommands;
176 uint64_t BeginOffset;
177 uint64_t EndOffset = 0;
178 uint8_t SectionIndex;
179 if (MachOObj->is64Bit()) {
180 InMemoryStruct<macho::Symbol64TableEntry> Entry;
181 getSymbol64TableEntry(DRI, Entry);
182 BeginOffset = Entry->Value;
183 SectionIndex = Entry->SectionIndex;
185 uint32_t flags = SymbolRef::SF_None;
186 getSymbolFlags(DRI, flags);
187 if (flags & SymbolRef::SF_Common)
188 Result = Entry->Value;
190 Result = UnknownAddressOrSize;
191 return object_error::success;
193 // Unfortunately symbols are unsorted so we need to touch all
194 // symbols from load command
196 uint32_t Command = DRI.d.a;
197 while (Command == DRI.d.a) {
198 moveToNextSymbol(DRI);
199 if (DRI.d.a < LoadCommandCount) {
200 getSymbol64TableEntry(DRI, Entry);
201 if (Entry->SectionIndex == SectionIndex && Entry->Value > BeginOffset)
202 if (!EndOffset || Entry->Value < EndOffset)
203 EndOffset = Entry->Value;
208 InMemoryStruct<macho::SymbolTableEntry> Entry;
209 getSymbolTableEntry(DRI, Entry);
210 BeginOffset = Entry->Value;
211 SectionIndex = Entry->SectionIndex;
213 uint32_t flags = SymbolRef::SF_None;
214 getSymbolFlags(DRI, flags);
215 if (flags & SymbolRef::SF_Common)
216 Result = Entry->Value;
218 Result = UnknownAddressOrSize;
219 return object_error::success;
221 // Unfortunately symbols are unsorted so we need to touch all
222 // symbols from load command
224 uint32_t Command = DRI.d.a;
225 while (Command == DRI.d.a) {
226 moveToNextSymbol(DRI);
227 if (DRI.d.a < LoadCommandCount) {
228 getSymbolTableEntry(DRI, Entry);
229 if (Entry->SectionIndex == SectionIndex && Entry->Value > BeginOffset)
230 if (!EndOffset || Entry->Value < EndOffset)
231 EndOffset = Entry->Value;
238 getSectionSize(Sections[SectionIndex-1], Size);
239 getSectionAddress(Sections[SectionIndex-1], EndOffset);
242 Result = EndOffset - BeginOffset;
243 return object_error::success;
246 error_code MachOObjectFile::getSymbolNMTypeChar(DataRefImpl DRI,
247 char &Result) const {
249 if (MachOObj->is64Bit()) {
250 InMemoryStruct<macho::Symbol64TableEntry> Entry;
251 getSymbol64TableEntry(DRI, Entry);
253 Flags = Entry->Flags;
255 InMemoryStruct<macho::SymbolTableEntry> Entry;
256 getSymbolTableEntry(DRI, Entry);
258 Flags = Entry->Flags;
262 switch (Type & macho::STF_TypeMask) {
263 case macho::STT_Undefined:
266 case macho::STT_Absolute:
267 case macho::STT_Section:
275 if (Flags & (macho::STF_External | macho::STF_PrivateExtern))
276 Char = toupper(Char);
278 return object_error::success;
281 error_code MachOObjectFile::getSymbolFlags(DataRefImpl DRI,
282 uint32_t &Result) const {
285 if (MachOObj->is64Bit()) {
286 InMemoryStruct<macho::Symbol64TableEntry> Entry;
287 getSymbol64TableEntry(DRI, Entry);
288 MachOFlags = Entry->Flags;
289 MachOType = Entry->Type;
291 InMemoryStruct<macho::SymbolTableEntry> Entry;
292 getSymbolTableEntry(DRI, Entry);
293 MachOFlags = Entry->Flags;
294 MachOType = Entry->Type;
297 // TODO: Correctly set SF_ThreadLocal
298 Result = SymbolRef::SF_None;
300 if ((MachOType & MachO::NlistMaskType) == MachO::NListTypeUndefined)
301 Result |= SymbolRef::SF_Undefined;
303 if (MachOFlags & macho::STF_StabsEntryMask)
304 Result |= SymbolRef::SF_FormatSpecific;
306 if (MachOType & MachO::NlistMaskExternal) {
307 Result |= SymbolRef::SF_Global;
308 if ((MachOType & MachO::NlistMaskType) == MachO::NListTypeUndefined)
309 Result |= SymbolRef::SF_Common;
312 if (MachOFlags & (MachO::NListDescWeakRef | MachO::NListDescWeakDef))
313 Result |= SymbolRef::SF_Weak;
315 if ((MachOType & MachO::NlistMaskType) == MachO::NListTypeAbsolute)
316 Result |= SymbolRef::SF_Absolute;
318 return object_error::success;
321 error_code MachOObjectFile::getSymbolSection(DataRefImpl Symb,
322 section_iterator &Res) const {
324 if (MachOObj->is64Bit()) {
325 InMemoryStruct<macho::Symbol64TableEntry> Entry;
326 getSymbol64TableEntry(Symb, Entry);
327 index = Entry->SectionIndex;
329 InMemoryStruct<macho::SymbolTableEntry> Entry;
330 getSymbolTableEntry(Symb, Entry);
331 index = Entry->SectionIndex;
335 Res = end_sections();
337 Res = section_iterator(SectionRef(Sections[index-1], this));
339 return object_error::success;
342 error_code MachOObjectFile::getSymbolType(DataRefImpl Symb,
343 SymbolRef::Type &Res) const {
345 if (MachOObj->is64Bit()) {
346 InMemoryStruct<macho::Symbol64TableEntry> Entry;
347 getSymbol64TableEntry(Symb, Entry);
348 n_type = Entry->Type;
350 InMemoryStruct<macho::SymbolTableEntry> Entry;
351 getSymbolTableEntry(Symb, Entry);
352 n_type = Entry->Type;
354 Res = SymbolRef::ST_Other;
356 // If this is a STAB debugging symbol, we can do nothing more.
357 if (n_type & MachO::NlistMaskStab) {
358 Res = SymbolRef::ST_Debug;
359 return object_error::success;
362 switch (n_type & MachO::NlistMaskType) {
363 case MachO::NListTypeUndefined :
364 Res = SymbolRef::ST_Unknown;
366 case MachO::NListTypeSection :
367 Res = SymbolRef::ST_Function;
370 return object_error::success;
373 error_code MachOObjectFile::getSymbolValue(DataRefImpl Symb,
374 uint64_t &Val) const {
375 report_fatal_error("getSymbolValue unimplemented in MachOObjectFile");
378 symbol_iterator MachOObjectFile::begin_symbols() const {
379 // DRI.d.a = segment number; DRI.d.b = symbol index.
381 moveToNextSymbol(DRI);
382 return symbol_iterator(SymbolRef(DRI, this));
385 symbol_iterator MachOObjectFile::end_symbols() const {
387 DRI.d.a = MachOObj->getHeader().NumLoadCommands;
388 return symbol_iterator(SymbolRef(DRI, this));
391 symbol_iterator MachOObjectFile::begin_dynamic_symbols() const {
393 report_fatal_error("Dynamic symbols unimplemented in MachOObjectFile");
396 symbol_iterator MachOObjectFile::end_dynamic_symbols() const {
398 report_fatal_error("Dynamic symbols unimplemented in MachOObjectFile");
401 library_iterator MachOObjectFile::begin_libraries_needed() const {
403 report_fatal_error("Needed libraries unimplemented in MachOObjectFile");
406 library_iterator MachOObjectFile::end_libraries_needed() const {
408 report_fatal_error("Needed libraries unimplemented in MachOObjectFile");
411 StringRef MachOObjectFile::getLoadName() const {
413 report_fatal_error("get_load_name() unimplemented in MachOObjectFile");
416 /*===-- Sections ----------------------------------------------------------===*/
418 void MachOObjectFile::moveToNextSection(DataRefImpl &DRI) const {
419 uint32_t LoadCommandCount = MachOObj->getHeader().NumLoadCommands;
420 while (DRI.d.a < LoadCommandCount) {
421 LoadCommandInfo LCI = MachOObj->getLoadCommandInfo(DRI.d.a);
422 if (LCI.Command.Type == macho::LCT_Segment) {
423 InMemoryStruct<macho::SegmentLoadCommand> SegmentLoadCmd;
424 MachOObj->ReadSegmentLoadCommand(LCI, SegmentLoadCmd);
425 if (DRI.d.b < SegmentLoadCmd->NumSections)
427 } else if (LCI.Command.Type == macho::LCT_Segment64) {
428 InMemoryStruct<macho::Segment64LoadCommand> Segment64LoadCmd;
429 MachOObj->ReadSegment64LoadCommand(LCI, Segment64LoadCmd);
430 if (DRI.d.b < Segment64LoadCmd->NumSections)
439 error_code MachOObjectFile::getSectionNext(DataRefImpl DRI,
440 SectionRef &Result) const {
442 moveToNextSection(DRI);
443 Result = SectionRef(DRI, this);
444 return object_error::success;
448 MachOObjectFile::getSection(DataRefImpl DRI,
449 InMemoryStruct<macho::Section> &Res) const {
450 LoadCommandInfo LCI = MachOObj->getLoadCommandInfo(DRI.d.a);
451 MachOObj->ReadSection(LCI, DRI.d.b, Res);
454 std::size_t MachOObjectFile::getSectionIndex(DataRefImpl Sec) const {
455 SectionList::const_iterator loc =
456 std::find(Sections.begin(), Sections.end(), Sec);
457 assert(loc != Sections.end() && "Sec is not a valid section!");
458 return std::distance(Sections.begin(), loc);
462 MachOObjectFile::getSection64(DataRefImpl DRI,
463 InMemoryStruct<macho::Section64> &Res) const {
464 LoadCommandInfo LCI = MachOObj->getLoadCommandInfo(DRI.d.a);
465 MachOObj->ReadSection64(LCI, DRI.d.b, Res);
468 static bool is64BitLoadCommand(const MachOObject *MachOObj, DataRefImpl DRI) {
469 LoadCommandInfo LCI = MachOObj->getLoadCommandInfo(DRI.d.a);
470 if (LCI.Command.Type == macho::LCT_Segment64)
472 assert(LCI.Command.Type == macho::LCT_Segment && "Unexpected Type.");
476 error_code MachOObjectFile::getSectionName(DataRefImpl DRI,
477 StringRef &Result) const {
478 // FIXME: thread safety.
479 static char result[34];
480 if (is64BitLoadCommand(MachOObj.get(), DRI)) {
481 LoadCommandInfo LCI = MachOObj->getLoadCommandInfo(DRI.d.a);
482 InMemoryStruct<macho::Section64> Sect;
483 MachOObj->ReadSection64(LCI, DRI.d.b, Sect);
485 strcpy(result, Sect->SegmentName);
487 strcat(result, Sect->Name);
489 LoadCommandInfo LCI = MachOObj->getLoadCommandInfo(DRI.d.a);
490 InMemoryStruct<macho::Section> Sect;
491 MachOObj->ReadSection(LCI, DRI.d.b, Sect);
493 strcpy(result, Sect->SegmentName);
495 strcat(result, Sect->Name);
497 Result = StringRef(result);
498 return object_error::success;
501 error_code MachOObjectFile::getSectionAddress(DataRefImpl DRI,
502 uint64_t &Result) const {
503 if (is64BitLoadCommand(MachOObj.get(), DRI)) {
504 InMemoryStruct<macho::Section64> Sect;
505 getSection64(DRI, Sect);
506 Result = Sect->Address;
508 InMemoryStruct<macho::Section> Sect;
509 getSection(DRI, Sect);
510 Result = Sect->Address;
512 return object_error::success;
515 error_code MachOObjectFile::getSectionSize(DataRefImpl DRI,
516 uint64_t &Result) const {
517 if (is64BitLoadCommand(MachOObj.get(), DRI)) {
518 InMemoryStruct<macho::Section64> Sect;
519 getSection64(DRI, Sect);
522 InMemoryStruct<macho::Section> Sect;
523 getSection(DRI, Sect);
526 return object_error::success;
529 error_code MachOObjectFile::getSectionContents(DataRefImpl DRI,
530 StringRef &Result) const {
531 if (is64BitLoadCommand(MachOObj.get(), DRI)) {
532 InMemoryStruct<macho::Section64> Sect;
533 getSection64(DRI, Sect);
534 Result = MachOObj->getData(Sect->Offset, Sect->Size);
536 InMemoryStruct<macho::Section> Sect;
537 getSection(DRI, Sect);
538 Result = MachOObj->getData(Sect->Offset, Sect->Size);
540 return object_error::success;
543 error_code MachOObjectFile::getSectionAlignment(DataRefImpl DRI,
544 uint64_t &Result) const {
545 if (is64BitLoadCommand(MachOObj.get(), DRI)) {
546 InMemoryStruct<macho::Section64> Sect;
547 getSection64(DRI, Sect);
548 Result = uint64_t(1) << Sect->Align;
550 InMemoryStruct<macho::Section> Sect;
551 getSection(DRI, Sect);
552 Result = uint64_t(1) << Sect->Align;
554 return object_error::success;
557 error_code MachOObjectFile::isSectionText(DataRefImpl DRI,
558 bool &Result) const {
559 if (is64BitLoadCommand(MachOObj.get(), DRI)) {
560 InMemoryStruct<macho::Section64> Sect;
561 getSection64(DRI, Sect);
562 Result = !strcmp(Sect->Name, "__text");
564 InMemoryStruct<macho::Section> Sect;
565 getSection(DRI, Sect);
566 Result = !strcmp(Sect->Name, "__text");
568 return object_error::success;
571 error_code MachOObjectFile::isSectionData(DataRefImpl DRI,
572 bool &Result) const {
573 // FIXME: Unimplemented.
575 return object_error::success;
578 error_code MachOObjectFile::isSectionBSS(DataRefImpl DRI,
579 bool &Result) const {
580 // FIXME: Unimplemented.
582 return object_error::success;
585 error_code MachOObjectFile::isSectionRequiredForExecution(DataRefImpl Sec,
586 bool &Result) const {
587 // FIXME: Unimplemented.
589 return object_error::success;
592 error_code MachOObjectFile::isSectionVirtual(DataRefImpl Sec,
593 bool &Result) const {
594 // FIXME: Unimplemented.
596 return object_error::success;
599 error_code MachOObjectFile::isSectionZeroInit(DataRefImpl DRI,
600 bool &Result) const {
601 if (MachOObj->is64Bit()) {
602 InMemoryStruct<macho::Section64> Sect;
603 getSection64(DRI, Sect);
604 unsigned SectionType = Sect->Flags & MachO::SectionFlagMaskSectionType;
605 Result = (SectionType == MachO::SectionTypeZeroFill ||
606 SectionType == MachO::SectionTypeZeroFillLarge);
608 InMemoryStruct<macho::Section> Sect;
609 getSection(DRI, Sect);
610 unsigned SectionType = Sect->Flags & MachO::SectionFlagMaskSectionType;
611 Result = (SectionType == MachO::SectionTypeZeroFill ||
612 SectionType == MachO::SectionTypeZeroFillLarge);
615 return object_error::success;
618 error_code MachOObjectFile::isSectionReadOnlyData(DataRefImpl Sec,
619 bool &Result) const {
620 // Consider using the code from isSectionText to look for __const sections.
621 // Alternately, emit S_ATTR_PURE_INSTRUCTIONS and/or S_ATTR_SOME_INSTRUCTIONS
622 // to use section attributes to distinguish code from data.
624 // FIXME: Unimplemented.
626 return object_error::success;
629 error_code MachOObjectFile::sectionContainsSymbol(DataRefImpl Sec,
631 bool &Result) const {
633 getSymbolType(Symb, ST);
634 if (ST == SymbolRef::ST_Unknown) {
636 return object_error::success;
639 uint64_t SectBegin, SectEnd;
640 getSectionAddress(Sec, SectBegin);
641 getSectionSize(Sec, SectEnd);
642 SectEnd += SectBegin;
644 if (MachOObj->is64Bit()) {
645 InMemoryStruct<macho::Symbol64TableEntry> Entry;
646 getSymbol64TableEntry(Symb, Entry);
647 uint64_t SymAddr= Entry->Value;
648 Result = (SymAddr >= SectBegin) && (SymAddr < SectEnd);
650 InMemoryStruct<macho::SymbolTableEntry> Entry;
651 getSymbolTableEntry(Symb, Entry);
652 uint64_t SymAddr= Entry->Value;
653 Result = (SymAddr >= SectBegin) && (SymAddr < SectEnd);
656 return object_error::success;
659 relocation_iterator MachOObjectFile::getSectionRelBegin(DataRefImpl Sec) const {
661 ret.d.b = getSectionIndex(Sec);
662 return relocation_iterator(RelocationRef(ret, this));
664 relocation_iterator MachOObjectFile::getSectionRelEnd(DataRefImpl Sec) const {
666 if (is64BitLoadCommand(MachOObj.get(), Sec)) {
667 InMemoryStruct<macho::Section64> Sect;
668 getSection64(Sec, Sect);
669 last_reloc = Sect->NumRelocationTableEntries;
671 InMemoryStruct<macho::Section> Sect;
672 getSection(Sec, Sect);
673 last_reloc = Sect->NumRelocationTableEntries;
676 ret.d.a = last_reloc;
677 ret.d.b = getSectionIndex(Sec);
678 return relocation_iterator(RelocationRef(ret, this));
681 section_iterator MachOObjectFile::begin_sections() const {
683 moveToNextSection(DRI);
684 return section_iterator(SectionRef(DRI, this));
687 section_iterator MachOObjectFile::end_sections() const {
689 DRI.d.a = MachOObj->getHeader().NumLoadCommands;
690 return section_iterator(SectionRef(DRI, this));
693 /*===-- Relocations -------------------------------------------------------===*/
695 void MachOObjectFile::
696 getRelocation(DataRefImpl Rel,
697 InMemoryStruct<macho::RelocationEntry> &Res) const {
699 if (MachOObj->is64Bit()) {
700 InMemoryStruct<macho::Section64> Sect;
701 getSection64(Sections[Rel.d.b], Sect);
702 relOffset = Sect->RelocationTableOffset;
704 InMemoryStruct<macho::Section> Sect;
705 getSection(Sections[Rel.d.b], Sect);
706 relOffset = Sect->RelocationTableOffset;
708 MachOObj->ReadRelocationEntry(relOffset, Rel.d.a, Res);
710 error_code MachOObjectFile::getRelocationNext(DataRefImpl Rel,
711 RelocationRef &Res) const {
713 Res = RelocationRef(Rel, this);
714 return object_error::success;
716 error_code MachOObjectFile::getRelocationAddress(DataRefImpl Rel,
717 uint64_t &Res) const {
718 const uint8_t* sectAddress = 0;
719 if (MachOObj->is64Bit()) {
720 InMemoryStruct<macho::Section64> Sect;
721 getSection64(Sections[Rel.d.b], Sect);
722 sectAddress += Sect->Address;
724 InMemoryStruct<macho::Section> Sect;
725 getSection(Sections[Rel.d.b], Sect);
726 sectAddress += Sect->Address;
728 InMemoryStruct<macho::RelocationEntry> RE;
729 getRelocation(Rel, RE);
731 unsigned Arch = getArch();
732 bool isScattered = (Arch != Triple::x86_64) &&
733 (RE->Word0 & macho::RF_Scattered);
734 uint64_t RelAddr = 0;
736 RelAddr = RE->Word0 & 0xFFFFFF;
740 Res = reinterpret_cast<uintptr_t>(sectAddress + RelAddr);
741 return object_error::success;
743 error_code MachOObjectFile::getRelocationOffset(DataRefImpl Rel,
744 uint64_t &Res) const {
745 InMemoryStruct<macho::RelocationEntry> RE;
746 getRelocation(Rel, RE);
748 unsigned Arch = getArch();
749 bool isScattered = (Arch != Triple::x86_64) &&
750 (RE->Word0 & macho::RF_Scattered);
752 Res = RE->Word0 & 0xFFFFFF;
755 return object_error::success;
757 error_code MachOObjectFile::getRelocationSymbol(DataRefImpl Rel,
758 SymbolRef &Res) const {
759 InMemoryStruct<macho::RelocationEntry> RE;
760 getRelocation(Rel, RE);
761 uint32_t SymbolIdx = RE->Word1 & 0xffffff;
762 bool isExtern = (RE->Word1 >> 27) & 1;
765 moveToNextSymbol(Sym);
767 for (unsigned i = 0; i < SymbolIdx; i++) {
769 moveToNextSymbol(Sym);
770 assert(Sym.d.a < MachOObj->getHeader().NumLoadCommands &&
771 "Relocation symbol index out of range!");
774 Res = SymbolRef(Sym, this);
775 return object_error::success;
777 error_code MachOObjectFile::getRelocationType(DataRefImpl Rel,
778 uint64_t &Res) const {
779 InMemoryStruct<macho::RelocationEntry> RE;
780 getRelocation(Rel, RE);
784 return object_error::success;
786 error_code MachOObjectFile::getRelocationTypeName(DataRefImpl Rel,
787 SmallVectorImpl<char> &Result) const {
788 // TODO: Support scattered relocations.
790 InMemoryStruct<macho::RelocationEntry> RE;
791 getRelocation(Rel, RE);
793 unsigned Arch = getArch();
794 bool isScattered = (Arch != Triple::x86_64) &&
795 (RE->Word0 & macho::RF_Scattered);
799 r_type = (RE->Word0 >> 24) & 0xF;
801 r_type = (RE->Word1 >> 28) & 0xF;
805 static const char *const Table[] = {
806 "GENERIC_RELOC_VANILLA",
807 "GENERIC_RELOC_PAIR",
808 "GENERIC_RELOC_SECTDIFF",
809 "GENERIC_RELOC_PB_LA_PTR",
810 "GENERIC_RELOC_LOCAL_SECTDIFF",
811 "GENERIC_RELOC_TLV" };
819 case Triple::x86_64: {
820 static const char *const Table[] = {
821 "X86_64_RELOC_UNSIGNED",
822 "X86_64_RELOC_SIGNED",
823 "X86_64_RELOC_BRANCH",
824 "X86_64_RELOC_GOT_LOAD",
826 "X86_64_RELOC_SUBTRACTOR",
827 "X86_64_RELOC_SIGNED_1",
828 "X86_64_RELOC_SIGNED_2",
829 "X86_64_RELOC_SIGNED_4",
830 "X86_64_RELOC_TLV" };
839 static const char *const Table[] = {
842 "ARM_RELOC_SECTDIFF",
843 "ARM_RELOC_LOCAL_SECTDIFF",
844 "ARM_RELOC_PB_LA_PTR",
846 "ARM_THUMB_RELOC_BR22",
847 "ARM_THUMB_32BIT_BRANCH",
849 "ARM_RELOC_HALF_SECTDIFF" };
858 static const char *const Table[] = {
867 "PPC_RELOC_SECTDIFF",
868 "PPC_RELOC_PB_LA_PTR",
869 "PPC_RELOC_HI16_SECTDIFF",
870 "PPC_RELOC_LO16_SECTDIFF",
871 "PPC_RELOC_HA16_SECTDIFF",
873 "PPC_RELOC_LO14_SECTDIFF",
874 "PPC_RELOC_LOCAL_SECTDIFF" };
879 case Triple::UnknownArch:
883 Result.append(res.begin(), res.end());
884 return object_error::success;
886 error_code MachOObjectFile::getRelocationAdditionalInfo(DataRefImpl Rel,
887 int64_t &Res) const {
888 InMemoryStruct<macho::RelocationEntry> RE;
889 getRelocation(Rel, RE);
890 bool isExtern = (RE->Word1 >> 27) & 1;
893 const uint8_t* sectAddress = base();
894 if (MachOObj->is64Bit()) {
895 InMemoryStruct<macho::Section64> Sect;
896 getSection64(Sections[Rel.d.b], Sect);
897 sectAddress += Sect->Offset;
899 InMemoryStruct<macho::Section> Sect;
900 getSection(Sections[Rel.d.b], Sect);
901 sectAddress += Sect->Offset;
903 Res = reinterpret_cast<uintptr_t>(sectAddress);
905 return object_error::success;
908 // Helper to advance a section or symbol iterator multiple increments at a time.
910 error_code advance(T &it, size_t Val) {
919 void advanceTo(T &it, size_t Val) {
920 if (error_code ec = advance(it, Val))
921 report_fatal_error(ec.message());
924 void MachOObjectFile::printRelocationTargetName(
925 InMemoryStruct<macho::RelocationEntry>& RE,
926 raw_string_ostream &fmt) const {
927 unsigned Arch = getArch();
928 bool isScattered = (Arch != Triple::x86_64) &&
929 (RE->Word0 & macho::RF_Scattered);
931 // Target of a scattered relocation is an address. In the interest of
932 // generating pretty output, scan through the symbol table looking for a
933 // symbol that aligns with that address. If we find one, print it.
934 // Otherwise, we just print the hex address of the target.
936 uint32_t Val = RE->Word1;
939 for (symbol_iterator SI = begin_symbols(), SE = end_symbols(); SI != SE;
941 if (ec) report_fatal_error(ec.message());
946 if ((ec = SI->getAddress(Addr)))
947 report_fatal_error(ec.message());
948 if (Addr != Val) continue;
949 if ((ec = SI->getName(Name)))
950 report_fatal_error(ec.message());
955 // If we couldn't find a symbol that this relocation refers to, try
956 // to find a section beginning instead.
957 for (section_iterator SI = begin_sections(), SE = end_sections(); SI != SE;
959 if (ec) report_fatal_error(ec.message());
964 if ((ec = SI->getAddress(Addr)))
965 report_fatal_error(ec.message());
966 if (Addr != Val) continue;
967 if ((ec = SI->getName(Name)))
968 report_fatal_error(ec.message());
973 fmt << format("0x%x", Val);
978 bool isExtern = (RE->Word1 >> 27) & 1;
979 uint32_t Val = RE->Word1 & 0xFFFFFF;
982 symbol_iterator SI = begin_symbols();
986 section_iterator SI = begin_sections();
994 error_code MachOObjectFile::getRelocationValueString(DataRefImpl Rel,
995 SmallVectorImpl<char> &Result) const {
996 InMemoryStruct<macho::RelocationEntry> RE;
997 getRelocation(Rel, RE);
999 unsigned Arch = getArch();
1000 bool isScattered = (Arch != Triple::x86_64) &&
1001 (RE->Word0 & macho::RF_Scattered);
1004 raw_string_ostream fmt(fmtbuf);
1008 Type = (RE->Word0 >> 24) & 0xF;
1010 Type = (RE->Word1 >> 28) & 0xF;
1014 isPCRel = ((RE->Word0 >> 30) & 1);
1016 isPCRel = ((RE->Word1 >> 24) & 1);
1018 // Determine any addends that should be displayed with the relocation.
1019 // These require decoding the relocation type, which is triple-specific.
1021 // X86_64 has entirely custom relocation types.
1022 if (Arch == Triple::x86_64) {
1023 bool isPCRel = ((RE->Word1 >> 24) & 1);
1026 case macho::RIT_X86_64_GOTLoad: // X86_64_RELOC_GOT_LOAD
1027 case macho::RIT_X86_64_GOT: { // X86_64_RELOC_GOT
1028 printRelocationTargetName(RE, fmt);
1030 if (isPCRel) fmt << "PCREL";
1033 case macho::RIT_X86_64_Subtractor: { // X86_64_RELOC_SUBTRACTOR
1034 InMemoryStruct<macho::RelocationEntry> RENext;
1035 DataRefImpl RelNext = Rel;
1037 getRelocation(RelNext, RENext);
1039 // X86_64_SUBTRACTOR must be followed by a relocation of type
1040 // X86_64_RELOC_UNSIGNED.
1041 // NOTE: Scattered relocations don't exist on x86_64.
1042 unsigned RType = (RENext->Word1 >> 28) & 0xF;
1044 report_fatal_error("Expected X86_64_RELOC_UNSIGNED after "
1045 "X86_64_RELOC_SUBTRACTOR.");
1047 // The X86_64_RELOC_UNSIGNED contains the minuend symbol,
1048 // X86_64_SUBTRACTOR contains to the subtrahend.
1049 printRelocationTargetName(RENext, fmt);
1051 printRelocationTargetName(RE, fmt);
1053 case macho::RIT_X86_64_TLV:
1054 printRelocationTargetName(RE, fmt);
1056 if (isPCRel) fmt << "P";
1058 case macho::RIT_X86_64_Signed1: // X86_64_RELOC_SIGNED1
1059 printRelocationTargetName(RE, fmt);
1062 case macho::RIT_X86_64_Signed2: // X86_64_RELOC_SIGNED2
1063 printRelocationTargetName(RE, fmt);
1066 case macho::RIT_X86_64_Signed4: // X86_64_RELOC_SIGNED4
1067 printRelocationTargetName(RE, fmt);
1071 printRelocationTargetName(RE, fmt);
1074 // X86 and ARM share some relocation types in common.
1075 } else if (Arch == Triple::x86 || Arch == Triple::arm) {
1076 // Generic relocation types...
1078 case macho::RIT_Pair: // GENERIC_RELOC_PAIR - prints no info
1079 return object_error::success;
1080 case macho::RIT_Difference: { // GENERIC_RELOC_SECTDIFF
1081 InMemoryStruct<macho::RelocationEntry> RENext;
1082 DataRefImpl RelNext = Rel;
1084 getRelocation(RelNext, RENext);
1086 // X86 sect diff's must be followed by a relocation of type
1087 // GENERIC_RELOC_PAIR.
1088 bool isNextScattered = (Arch != Triple::x86_64) &&
1089 (RENext->Word0 & macho::RF_Scattered);
1091 if (isNextScattered)
1092 RType = (RENext->Word0 >> 24) & 0xF;
1094 RType = (RENext->Word1 >> 28) & 0xF;
1096 report_fatal_error("Expected GENERIC_RELOC_PAIR after "
1097 "GENERIC_RELOC_SECTDIFF.");
1099 printRelocationTargetName(RE, fmt);
1101 printRelocationTargetName(RENext, fmt);
1106 if (Arch == Triple::x86) {
1107 // All X86 relocations that need special printing were already
1108 // handled in the generic code.
1110 case macho::RIT_Generic_LocalDifference:{// GENERIC_RELOC_LOCAL_SECTDIFF
1111 InMemoryStruct<macho::RelocationEntry> RENext;
1112 DataRefImpl RelNext = Rel;
1114 getRelocation(RelNext, RENext);
1116 // X86 sect diff's must be followed by a relocation of type
1117 // GENERIC_RELOC_PAIR.
1118 bool isNextScattered = (Arch != Triple::x86_64) &&
1119 (RENext->Word0 & macho::RF_Scattered);
1121 if (isNextScattered)
1122 RType = (RENext->Word0 >> 24) & 0xF;
1124 RType = (RENext->Word1 >> 28) & 0xF;
1126 report_fatal_error("Expected GENERIC_RELOC_PAIR after "
1127 "GENERIC_RELOC_LOCAL_SECTDIFF.");
1129 printRelocationTargetName(RE, fmt);
1131 printRelocationTargetName(RENext, fmt);
1134 case macho::RIT_Generic_TLV: {
1135 printRelocationTargetName(RE, fmt);
1137 if (isPCRel) fmt << "P";
1141 printRelocationTargetName(RE, fmt);
1143 } else { // ARM-specific relocations
1145 case macho::RIT_ARM_Half: // ARM_RELOC_HALF
1146 case macho::RIT_ARM_HalfDifference: { // ARM_RELOC_HALF_SECTDIFF
1147 // Half relocations steal a bit from the length field to encode
1148 // whether this is an upper16 or a lower16 relocation.
1151 isUpper = (RE->Word0 >> 28) & 1;
1153 isUpper = (RE->Word1 >> 25) & 1;
1156 fmt << ":upper16:(";
1158 fmt << ":lower16:(";
1159 printRelocationTargetName(RE, fmt);
1161 InMemoryStruct<macho::RelocationEntry> RENext;
1162 DataRefImpl RelNext = Rel;
1164 getRelocation(RelNext, RENext);
1166 // ARM half relocs must be followed by a relocation of type
1168 bool isNextScattered = (Arch != Triple::x86_64) &&
1169 (RENext->Word0 & macho::RF_Scattered);
1171 if (isNextScattered)
1172 RType = (RENext->Word0 >> 24) & 0xF;
1174 RType = (RENext->Word1 >> 28) & 0xF;
1177 report_fatal_error("Expected ARM_RELOC_PAIR after "
1178 "GENERIC_RELOC_HALF");
1180 // NOTE: The half of the target virtual address is stashed in the
1181 // address field of the secondary relocation, but we can't reverse
1182 // engineer the constant offset from it without decoding the movw/movt
1183 // instruction to find the other half in its immediate field.
1185 // ARM_RELOC_HALF_SECTDIFF encodes the second section in the
1186 // symbol/section pointer of the follow-on relocation.
1187 if (Type == macho::RIT_ARM_HalfDifference) {
1189 printRelocationTargetName(RENext, fmt);
1196 printRelocationTargetName(RE, fmt);
1201 printRelocationTargetName(RE, fmt);
1204 Result.append(fmtbuf.begin(), fmtbuf.end());
1205 return object_error::success;
1208 error_code MachOObjectFile::getRelocationHidden(DataRefImpl Rel,
1209 bool &Result) const {
1210 InMemoryStruct<macho::RelocationEntry> RE;
1211 getRelocation(Rel, RE);
1213 unsigned Arch = getArch();
1214 bool isScattered = (Arch != Triple::x86_64) &&
1215 (RE->Word0 & macho::RF_Scattered);
1218 Type = (RE->Word0 >> 24) & 0xF;
1220 Type = (RE->Word1 >> 28) & 0xF;
1224 // On arches that use the generic relocations, GENERIC_RELOC_PAIR
1225 // is always hidden.
1226 if (Arch == Triple::x86 || Arch == Triple::arm) {
1227 if (Type == macho::RIT_Pair) Result = true;
1228 } else if (Arch == Triple::x86_64) {
1229 // On x86_64, X86_64_RELOC_UNSIGNED is hidden only when it follows
1230 // an X864_64_RELOC_SUBTRACTOR.
1231 if (Type == macho::RIT_X86_64_Unsigned && Rel.d.a > 0) {
1232 DataRefImpl RelPrev = Rel;
1234 InMemoryStruct<macho::RelocationEntry> REPrev;
1235 getRelocation(RelPrev, REPrev);
1237 unsigned PrevType = (REPrev->Word1 >> 28) & 0xF;
1239 if (PrevType == macho::RIT_X86_64_Subtractor) Result = true;
1243 return object_error::success;
1246 error_code MachOObjectFile::getLibraryNext(DataRefImpl LibData,
1247 LibraryRef &Res) const {
1248 report_fatal_error("Needed libraries unimplemented in MachOObjectFile");
1251 error_code MachOObjectFile::getLibraryPath(DataRefImpl LibData,
1252 StringRef &Res) const {
1253 report_fatal_error("Needed libraries unimplemented in MachOObjectFile");
1257 /*===-- Miscellaneous -----------------------------------------------------===*/
1259 uint8_t MachOObjectFile::getBytesInAddress() const {
1260 return MachOObj->is64Bit() ? 8 : 4;
1263 StringRef MachOObjectFile::getFileFormatName() const {
1264 if (!MachOObj->is64Bit()) {
1265 switch (MachOObj->getHeader().CPUType) {
1266 case llvm::MachO::CPUTypeI386:
1267 return "Mach-O 32-bit i386";
1268 case llvm::MachO::CPUTypeARM:
1269 return "Mach-O arm";
1270 case llvm::MachO::CPUTypePowerPC:
1271 return "Mach-O 32-bit ppc";
1273 assert((MachOObj->getHeader().CPUType & llvm::MachO::CPUArchABI64) == 0 &&
1274 "64-bit object file when we're not 64-bit?");
1275 return "Mach-O 32-bit unknown";
1279 switch (MachOObj->getHeader().CPUType) {
1280 case llvm::MachO::CPUTypeX86_64:
1281 return "Mach-O 64-bit x86-64";
1282 case llvm::MachO::CPUTypePowerPC64:
1283 return "Mach-O 64-bit ppc64";
1285 assert((MachOObj->getHeader().CPUType & llvm::MachO::CPUArchABI64) == 1 &&
1286 "32-bit object file when we're 64-bit?");
1287 return "Mach-O 64-bit unknown";
1291 unsigned MachOObjectFile::getArch() const {
1292 switch (MachOObj->getHeader().CPUType) {
1293 case llvm::MachO::CPUTypeI386:
1295 case llvm::MachO::CPUTypeX86_64:
1296 return Triple::x86_64;
1297 case llvm::MachO::CPUTypeARM:
1299 case llvm::MachO::CPUTypePowerPC:
1301 case llvm::MachO::CPUTypePowerPC64:
1302 return Triple::ppc64;
1304 return Triple::UnknownArch;
1308 } // end namespace object
1309 } // end namespace llvm