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 InMemoryStruct<macho::SegmentLoadCommand> SLC;
451 LoadCommandInfo LCI = MachOObj->getLoadCommandInfo(DRI.d.a);
452 MachOObj->ReadSegmentLoadCommand(LCI, SLC);
453 MachOObj->ReadSection(LCI, DRI.d.b, Res);
456 std::size_t MachOObjectFile::getSectionIndex(DataRefImpl Sec) const {
457 SectionList::const_iterator loc =
458 std::find(Sections.begin(), Sections.end(), Sec);
459 assert(loc != Sections.end() && "Sec is not a valid section!");
460 return std::distance(Sections.begin(), loc);
464 MachOObjectFile::getSection64(DataRefImpl DRI,
465 InMemoryStruct<macho::Section64> &Res) const {
466 InMemoryStruct<macho::Segment64LoadCommand> SLC;
467 LoadCommandInfo LCI = MachOObj->getLoadCommandInfo(DRI.d.a);
468 MachOObj->ReadSegment64LoadCommand(LCI, SLC);
469 MachOObj->ReadSection64(LCI, DRI.d.b, Res);
472 static bool is64BitLoadCommand(const MachOObject *MachOObj, DataRefImpl DRI) {
473 LoadCommandInfo LCI = MachOObj->getLoadCommandInfo(DRI.d.a);
474 if (LCI.Command.Type == macho::LCT_Segment64)
476 assert(LCI.Command.Type == macho::LCT_Segment && "Unexpected Type.");
480 error_code MachOObjectFile::getSectionName(DataRefImpl DRI,
481 StringRef &Result) const {
482 // FIXME: thread safety.
483 static char result[34];
484 if (is64BitLoadCommand(MachOObj.get(), DRI)) {
485 InMemoryStruct<macho::Segment64LoadCommand> SLC;
486 LoadCommandInfo LCI = MachOObj->getLoadCommandInfo(DRI.d.a);
487 MachOObj->ReadSegment64LoadCommand(LCI, SLC);
488 InMemoryStruct<macho::Section64> Sect;
489 MachOObj->ReadSection64(LCI, DRI.d.b, Sect);
491 strcpy(result, Sect->SegmentName);
493 strcat(result, Sect->Name);
495 InMemoryStruct<macho::SegmentLoadCommand> SLC;
496 LoadCommandInfo LCI = MachOObj->getLoadCommandInfo(DRI.d.a);
497 MachOObj->ReadSegmentLoadCommand(LCI, SLC);
498 InMemoryStruct<macho::Section> Sect;
499 MachOObj->ReadSection(LCI, DRI.d.b, Sect);
501 strcpy(result, Sect->SegmentName);
503 strcat(result, Sect->Name);
505 Result = StringRef(result);
506 return object_error::success;
509 error_code MachOObjectFile::getSectionAddress(DataRefImpl DRI,
510 uint64_t &Result) const {
511 if (is64BitLoadCommand(MachOObj.get(), DRI)) {
512 InMemoryStruct<macho::Section64> Sect;
513 getSection64(DRI, Sect);
514 Result = Sect->Address;
516 InMemoryStruct<macho::Section> Sect;
517 getSection(DRI, Sect);
518 Result = Sect->Address;
520 return object_error::success;
523 error_code MachOObjectFile::getSectionSize(DataRefImpl DRI,
524 uint64_t &Result) const {
525 if (is64BitLoadCommand(MachOObj.get(), DRI)) {
526 InMemoryStruct<macho::Section64> Sect;
527 getSection64(DRI, Sect);
530 InMemoryStruct<macho::Section> Sect;
531 getSection(DRI, Sect);
534 return object_error::success;
537 error_code MachOObjectFile::getSectionContents(DataRefImpl DRI,
538 StringRef &Result) const {
539 if (is64BitLoadCommand(MachOObj.get(), DRI)) {
540 InMemoryStruct<macho::Section64> Sect;
541 getSection64(DRI, Sect);
542 Result = MachOObj->getData(Sect->Offset, Sect->Size);
544 InMemoryStruct<macho::Section> Sect;
545 getSection(DRI, Sect);
546 Result = MachOObj->getData(Sect->Offset, Sect->Size);
548 return object_error::success;
551 error_code MachOObjectFile::getSectionAlignment(DataRefImpl DRI,
552 uint64_t &Result) const {
553 if (is64BitLoadCommand(MachOObj.get(), DRI)) {
554 InMemoryStruct<macho::Section64> Sect;
555 getSection64(DRI, Sect);
556 Result = uint64_t(1) << Sect->Align;
558 InMemoryStruct<macho::Section> Sect;
559 getSection(DRI, Sect);
560 Result = uint64_t(1) << Sect->Align;
562 return object_error::success;
565 error_code MachOObjectFile::isSectionText(DataRefImpl DRI,
566 bool &Result) const {
567 if (is64BitLoadCommand(MachOObj.get(), DRI)) {
568 InMemoryStruct<macho::Section64> Sect;
569 getSection64(DRI, Sect);
570 Result = !strcmp(Sect->Name, "__text");
572 InMemoryStruct<macho::Section> Sect;
573 getSection(DRI, Sect);
574 Result = !strcmp(Sect->Name, "__text");
576 return object_error::success;
579 error_code MachOObjectFile::isSectionData(DataRefImpl DRI,
580 bool &Result) const {
581 // FIXME: Unimplemented.
583 return object_error::success;
586 error_code MachOObjectFile::isSectionBSS(DataRefImpl DRI,
587 bool &Result) const {
588 // FIXME: Unimplemented.
590 return object_error::success;
593 error_code MachOObjectFile::isSectionRequiredForExecution(DataRefImpl Sec,
594 bool &Result) const {
595 // FIXME: Unimplemented.
597 return object_error::success;
600 error_code MachOObjectFile::isSectionVirtual(DataRefImpl Sec,
601 bool &Result) const {
602 // FIXME: Unimplemented.
604 return object_error::success;
607 error_code MachOObjectFile::isSectionZeroInit(DataRefImpl DRI,
608 bool &Result) const {
609 if (MachOObj->is64Bit()) {
610 InMemoryStruct<macho::Section64> Sect;
611 getSection64(DRI, Sect);
612 unsigned SectionType = Sect->Flags & MachO::SectionFlagMaskSectionType;
613 Result = (SectionType == MachO::SectionTypeZeroFill ||
614 SectionType == MachO::SectionTypeZeroFillLarge);
616 InMemoryStruct<macho::Section> Sect;
617 getSection(DRI, Sect);
618 unsigned SectionType = Sect->Flags & MachO::SectionFlagMaskSectionType;
619 Result = (SectionType == MachO::SectionTypeZeroFill ||
620 SectionType == MachO::SectionTypeZeroFillLarge);
623 return object_error::success;
626 error_code MachOObjectFile::isSectionReadOnlyData(DataRefImpl Sec,
627 bool &Result) const {
628 // Consider using the code from isSectionText to look for __const sections.
629 // Alternately, emit S_ATTR_PURE_INSTRUCTIONS and/or S_ATTR_SOME_INSTRUCTIONS
630 // to use section attributes to distinguish code from data.
632 // FIXME: Unimplemented.
634 return object_error::success;
637 error_code MachOObjectFile::sectionContainsSymbol(DataRefImpl Sec,
639 bool &Result) const {
641 getSymbolType(Symb, ST);
642 if (ST == SymbolRef::ST_Unknown) {
644 return object_error::success;
647 uint64_t SectBegin, SectEnd;
648 getSectionAddress(Sec, SectBegin);
649 getSectionSize(Sec, SectEnd);
650 SectEnd += SectBegin;
652 if (MachOObj->is64Bit()) {
653 InMemoryStruct<macho::Symbol64TableEntry> Entry;
654 getSymbol64TableEntry(Symb, Entry);
655 uint64_t SymAddr= Entry->Value;
656 Result = (SymAddr >= SectBegin) && (SymAddr < SectEnd);
658 InMemoryStruct<macho::SymbolTableEntry> Entry;
659 getSymbolTableEntry(Symb, Entry);
660 uint64_t SymAddr= Entry->Value;
661 Result = (SymAddr >= SectBegin) && (SymAddr < SectEnd);
664 return object_error::success;
667 relocation_iterator MachOObjectFile::getSectionRelBegin(DataRefImpl Sec) const {
669 ret.d.b = getSectionIndex(Sec);
670 return relocation_iterator(RelocationRef(ret, this));
672 relocation_iterator MachOObjectFile::getSectionRelEnd(DataRefImpl Sec) const {
674 if (is64BitLoadCommand(MachOObj.get(), Sec)) {
675 InMemoryStruct<macho::Section64> Sect;
676 getSection64(Sec, Sect);
677 last_reloc = Sect->NumRelocationTableEntries;
679 InMemoryStruct<macho::Section> Sect;
680 getSection(Sec, Sect);
681 last_reloc = Sect->NumRelocationTableEntries;
684 ret.d.a = last_reloc;
685 ret.d.b = getSectionIndex(Sec);
686 return relocation_iterator(RelocationRef(ret, this));
689 section_iterator MachOObjectFile::begin_sections() const {
691 moveToNextSection(DRI);
692 return section_iterator(SectionRef(DRI, this));
695 section_iterator MachOObjectFile::end_sections() const {
697 DRI.d.a = MachOObj->getHeader().NumLoadCommands;
698 return section_iterator(SectionRef(DRI, this));
701 /*===-- Relocations -------------------------------------------------------===*/
703 void MachOObjectFile::
704 getRelocation(DataRefImpl Rel,
705 InMemoryStruct<macho::RelocationEntry> &Res) const {
707 if (MachOObj->is64Bit()) {
708 InMemoryStruct<macho::Section64> Sect;
709 getSection64(Sections[Rel.d.b], Sect);
710 relOffset = Sect->RelocationTableOffset;
712 InMemoryStruct<macho::Section> Sect;
713 getSection(Sections[Rel.d.b], Sect);
714 relOffset = Sect->RelocationTableOffset;
716 MachOObj->ReadRelocationEntry(relOffset, Rel.d.a, Res);
718 error_code MachOObjectFile::getRelocationNext(DataRefImpl Rel,
719 RelocationRef &Res) const {
721 Res = RelocationRef(Rel, this);
722 return object_error::success;
724 error_code MachOObjectFile::getRelocationAddress(DataRefImpl Rel,
725 uint64_t &Res) const {
726 const uint8_t* sectAddress = 0;
727 if (MachOObj->is64Bit()) {
728 InMemoryStruct<macho::Section64> Sect;
729 getSection64(Sections[Rel.d.b], Sect);
730 sectAddress += Sect->Address;
732 InMemoryStruct<macho::Section> Sect;
733 getSection(Sections[Rel.d.b], Sect);
734 sectAddress += Sect->Address;
736 InMemoryStruct<macho::RelocationEntry> RE;
737 getRelocation(Rel, RE);
739 unsigned Arch = getArch();
740 bool isScattered = (Arch != Triple::x86_64) &&
741 (RE->Word0 & macho::RF_Scattered);
742 uint64_t RelAddr = 0;
744 RelAddr = RE->Word0 & 0xFFFFFF;
748 Res = reinterpret_cast<uintptr_t>(sectAddress + RelAddr);
749 return object_error::success;
751 error_code MachOObjectFile::getRelocationOffset(DataRefImpl Rel,
752 uint64_t &Res) const {
753 InMemoryStruct<macho::RelocationEntry> RE;
754 getRelocation(Rel, RE);
756 unsigned Arch = getArch();
757 bool isScattered = (Arch != Triple::x86_64) &&
758 (RE->Word0 & macho::RF_Scattered);
760 Res = RE->Word0 & 0xFFFFFF;
763 return object_error::success;
765 error_code MachOObjectFile::getRelocationSymbol(DataRefImpl Rel,
766 SymbolRef &Res) const {
767 InMemoryStruct<macho::RelocationEntry> RE;
768 getRelocation(Rel, RE);
769 uint32_t SymbolIdx = RE->Word1 & 0xffffff;
770 bool isExtern = (RE->Word1 >> 27) & 1;
773 moveToNextSymbol(Sym);
775 for (unsigned i = 0; i < SymbolIdx; i++) {
777 moveToNextSymbol(Sym);
778 assert(Sym.d.a < MachOObj->getHeader().NumLoadCommands &&
779 "Relocation symbol index out of range!");
782 Res = SymbolRef(Sym, this);
783 return object_error::success;
785 error_code MachOObjectFile::getRelocationType(DataRefImpl Rel,
786 uint64_t &Res) const {
787 InMemoryStruct<macho::RelocationEntry> RE;
788 getRelocation(Rel, RE);
792 return object_error::success;
794 error_code MachOObjectFile::getRelocationTypeName(DataRefImpl Rel,
795 SmallVectorImpl<char> &Result) const {
796 // TODO: Support scattered relocations.
798 InMemoryStruct<macho::RelocationEntry> RE;
799 getRelocation(Rel, RE);
801 unsigned Arch = getArch();
802 bool isScattered = (Arch != Triple::x86_64) &&
803 (RE->Word0 & macho::RF_Scattered);
807 r_type = (RE->Word0 >> 24) & 0xF;
809 r_type = (RE->Word1 >> 28) & 0xF;
813 static const char *const Table[] = {
814 "GENERIC_RELOC_VANILLA",
815 "GENERIC_RELOC_PAIR",
816 "GENERIC_RELOC_SECTDIFF",
817 "GENERIC_RELOC_PB_LA_PTR",
818 "GENERIC_RELOC_LOCAL_SECTDIFF",
819 "GENERIC_RELOC_TLV" };
827 case Triple::x86_64: {
828 static const char *const Table[] = {
829 "X86_64_RELOC_UNSIGNED",
830 "X86_64_RELOC_SIGNED",
831 "X86_64_RELOC_BRANCH",
832 "X86_64_RELOC_GOT_LOAD",
834 "X86_64_RELOC_SUBTRACTOR",
835 "X86_64_RELOC_SIGNED_1",
836 "X86_64_RELOC_SIGNED_2",
837 "X86_64_RELOC_SIGNED_4",
838 "X86_64_RELOC_TLV" };
847 static const char *const Table[] = {
850 "ARM_RELOC_SECTDIFF",
851 "ARM_RELOC_LOCAL_SECTDIFF",
852 "ARM_RELOC_PB_LA_PTR",
854 "ARM_THUMB_RELOC_BR22",
855 "ARM_THUMB_32BIT_BRANCH",
857 "ARM_RELOC_HALF_SECTDIFF" };
866 static const char *const Table[] = {
875 "PPC_RELOC_SECTDIFF",
876 "PPC_RELOC_PB_LA_PTR",
877 "PPC_RELOC_HI16_SECTDIFF",
878 "PPC_RELOC_LO16_SECTDIFF",
879 "PPC_RELOC_HA16_SECTDIFF",
881 "PPC_RELOC_LO14_SECTDIFF",
882 "PPC_RELOC_LOCAL_SECTDIFF" };
887 case Triple::UnknownArch:
891 Result.append(res.begin(), res.end());
892 return object_error::success;
894 error_code MachOObjectFile::getRelocationAdditionalInfo(DataRefImpl Rel,
895 int64_t &Res) const {
896 InMemoryStruct<macho::RelocationEntry> RE;
897 getRelocation(Rel, RE);
898 bool isExtern = (RE->Word1 >> 27) & 1;
901 const uint8_t* sectAddress = base();
902 if (MachOObj->is64Bit()) {
903 InMemoryStruct<macho::Section64> Sect;
904 getSection64(Sections[Rel.d.b], Sect);
905 sectAddress += Sect->Offset;
907 InMemoryStruct<macho::Section> Sect;
908 getSection(Sections[Rel.d.b], Sect);
909 sectAddress += Sect->Offset;
911 Res = reinterpret_cast<uintptr_t>(sectAddress);
913 return object_error::success;
916 // Helper to advance a section or symbol iterator multiple increments at a time.
918 error_code advance(T &it, size_t Val) {
927 void advanceTo(T &it, size_t Val) {
928 if (error_code ec = advance(it, Val))
929 report_fatal_error(ec.message());
932 void MachOObjectFile::printRelocationTargetName(
933 InMemoryStruct<macho::RelocationEntry>& RE,
934 raw_string_ostream &fmt) const {
935 unsigned Arch = getArch();
936 bool isScattered = (Arch != Triple::x86_64) &&
937 (RE->Word0 & macho::RF_Scattered);
939 // Target of a scattered relocation is an address. In the interest of
940 // generating pretty output, scan through the symbol table looking for a
941 // symbol that aligns with that address. If we find one, print it.
942 // Otherwise, we just print the hex address of the target.
944 uint32_t Val = RE->Word1;
947 for (symbol_iterator SI = begin_symbols(), SE = end_symbols(); SI != SE;
949 if (ec) report_fatal_error(ec.message());
954 if ((ec = SI->getAddress(Addr)))
955 report_fatal_error(ec.message());
956 if (Addr != Val) continue;
957 if ((ec = SI->getName(Name)))
958 report_fatal_error(ec.message());
963 // If we couldn't find a symbol that this relocation refers to, try
964 // to find a section beginning instead.
965 for (section_iterator SI = begin_sections(), SE = end_sections(); SI != SE;
967 if (ec) report_fatal_error(ec.message());
972 if ((ec = SI->getAddress(Addr)))
973 report_fatal_error(ec.message());
974 if (Addr != Val) continue;
975 if ((ec = SI->getName(Name)))
976 report_fatal_error(ec.message());
981 fmt << format("0x%x", Val);
986 bool isExtern = (RE->Word1 >> 27) & 1;
987 uint32_t Val = RE->Word1 & 0xFFFFFF;
990 symbol_iterator SI = begin_symbols();
994 section_iterator SI = begin_sections();
1002 error_code MachOObjectFile::getRelocationValueString(DataRefImpl Rel,
1003 SmallVectorImpl<char> &Result) const {
1004 InMemoryStruct<macho::RelocationEntry> RE;
1005 getRelocation(Rel, RE);
1007 unsigned Arch = getArch();
1008 bool isScattered = (Arch != Triple::x86_64) &&
1009 (RE->Word0 & macho::RF_Scattered);
1012 raw_string_ostream fmt(fmtbuf);
1016 Type = (RE->Word0 >> 24) & 0xF;
1018 Type = (RE->Word1 >> 28) & 0xF;
1022 isPCRel = ((RE->Word0 >> 30) & 1);
1024 isPCRel = ((RE->Word1 >> 24) & 1);
1026 // Determine any addends that should be displayed with the relocation.
1027 // These require decoding the relocation type, which is triple-specific.
1029 // X86_64 has entirely custom relocation types.
1030 if (Arch == Triple::x86_64) {
1031 bool isPCRel = ((RE->Word1 >> 24) & 1);
1034 case macho::RIT_X86_64_GOTLoad: // X86_64_RELOC_GOT_LOAD
1035 case macho::RIT_X86_64_GOT: { // X86_64_RELOC_GOT
1036 printRelocationTargetName(RE, fmt);
1038 if (isPCRel) fmt << "PCREL";
1041 case macho::RIT_X86_64_Subtractor: { // X86_64_RELOC_SUBTRACTOR
1042 InMemoryStruct<macho::RelocationEntry> RENext;
1043 DataRefImpl RelNext = Rel;
1045 getRelocation(RelNext, RENext);
1047 // X86_64_SUBTRACTOR must be followed by a relocation of type
1048 // X86_64_RELOC_UNSIGNED.
1049 // NOTE: Scattered relocations don't exist on x86_64.
1050 unsigned RType = (RENext->Word1 >> 28) & 0xF;
1052 report_fatal_error("Expected X86_64_RELOC_UNSIGNED after "
1053 "X86_64_RELOC_SUBTRACTOR.");
1055 // The X86_64_RELOC_UNSIGNED contains the minuend symbol,
1056 // X86_64_SUBTRACTOR contains to the subtrahend.
1057 printRelocationTargetName(RENext, fmt);
1059 printRelocationTargetName(RE, fmt);
1061 case macho::RIT_X86_64_TLV:
1062 printRelocationTargetName(RE, fmt);
1064 if (isPCRel) fmt << "P";
1066 case macho::RIT_X86_64_Signed1: // X86_64_RELOC_SIGNED1
1067 printRelocationTargetName(RE, fmt);
1070 case macho::RIT_X86_64_Signed2: // X86_64_RELOC_SIGNED2
1071 printRelocationTargetName(RE, fmt);
1074 case macho::RIT_X86_64_Signed4: // X86_64_RELOC_SIGNED4
1075 printRelocationTargetName(RE, fmt);
1079 printRelocationTargetName(RE, fmt);
1082 // X86 and ARM share some relocation types in common.
1083 } else if (Arch == Triple::x86 || Arch == Triple::arm) {
1084 // Generic relocation types...
1086 case macho::RIT_Pair: // GENERIC_RELOC_PAIR - prints no info
1087 return object_error::success;
1088 case macho::RIT_Difference: { // GENERIC_RELOC_SECTDIFF
1089 InMemoryStruct<macho::RelocationEntry> RENext;
1090 DataRefImpl RelNext = Rel;
1092 getRelocation(RelNext, RENext);
1094 // X86 sect diff's must be followed by a relocation of type
1095 // GENERIC_RELOC_PAIR.
1096 bool isNextScattered = (Arch != Triple::x86_64) &&
1097 (RENext->Word0 & macho::RF_Scattered);
1099 if (isNextScattered)
1100 RType = (RENext->Word0 >> 24) & 0xF;
1102 RType = (RENext->Word1 >> 28) & 0xF;
1104 report_fatal_error("Expected GENERIC_RELOC_PAIR after "
1105 "GENERIC_RELOC_SECTDIFF.");
1107 printRelocationTargetName(RE, fmt);
1109 printRelocationTargetName(RENext, fmt);
1114 if (Arch == Triple::x86) {
1115 // All X86 relocations that need special printing were already
1116 // handled in the generic code.
1118 case macho::RIT_Generic_LocalDifference:{// GENERIC_RELOC_LOCAL_SECTDIFF
1119 InMemoryStruct<macho::RelocationEntry> RENext;
1120 DataRefImpl RelNext = Rel;
1122 getRelocation(RelNext, RENext);
1124 // X86 sect diff's must be followed by a relocation of type
1125 // GENERIC_RELOC_PAIR.
1126 bool isNextScattered = (Arch != Triple::x86_64) &&
1127 (RENext->Word0 & macho::RF_Scattered);
1129 if (isNextScattered)
1130 RType = (RENext->Word0 >> 24) & 0xF;
1132 RType = (RENext->Word1 >> 28) & 0xF;
1134 report_fatal_error("Expected GENERIC_RELOC_PAIR after "
1135 "GENERIC_RELOC_LOCAL_SECTDIFF.");
1137 printRelocationTargetName(RE, fmt);
1139 printRelocationTargetName(RENext, fmt);
1142 case macho::RIT_Generic_TLV: {
1143 printRelocationTargetName(RE, fmt);
1145 if (isPCRel) fmt << "P";
1149 printRelocationTargetName(RE, fmt);
1151 } else { // ARM-specific relocations
1153 case macho::RIT_ARM_Half: // ARM_RELOC_HALF
1154 case macho::RIT_ARM_HalfDifference: { // ARM_RELOC_HALF_SECTDIFF
1155 // Half relocations steal a bit from the length field to encode
1156 // whether this is an upper16 or a lower16 relocation.
1159 isUpper = (RE->Word0 >> 28) & 1;
1161 isUpper = (RE->Word1 >> 25) & 1;
1164 fmt << ":upper16:(";
1166 fmt << ":lower16:(";
1167 printRelocationTargetName(RE, fmt);
1169 InMemoryStruct<macho::RelocationEntry> RENext;
1170 DataRefImpl RelNext = Rel;
1172 getRelocation(RelNext, RENext);
1174 // ARM half relocs must be followed by a relocation of type
1176 bool isNextScattered = (Arch != Triple::x86_64) &&
1177 (RENext->Word0 & macho::RF_Scattered);
1179 if (isNextScattered)
1180 RType = (RENext->Word0 >> 24) & 0xF;
1182 RType = (RENext->Word1 >> 28) & 0xF;
1185 report_fatal_error("Expected ARM_RELOC_PAIR after "
1186 "GENERIC_RELOC_HALF");
1188 // NOTE: The half of the target virtual address is stashed in the
1189 // address field of the secondary relocation, but we can't reverse
1190 // engineer the constant offset from it without decoding the movw/movt
1191 // instruction to find the other half in its immediate field.
1193 // ARM_RELOC_HALF_SECTDIFF encodes the second section in the
1194 // symbol/section pointer of the follow-on relocation.
1195 if (Type == macho::RIT_ARM_HalfDifference) {
1197 printRelocationTargetName(RENext, fmt);
1204 printRelocationTargetName(RE, fmt);
1209 printRelocationTargetName(RE, fmt);
1212 Result.append(fmtbuf.begin(), fmtbuf.end());
1213 return object_error::success;
1216 error_code MachOObjectFile::getRelocationHidden(DataRefImpl Rel,
1217 bool &Result) const {
1218 InMemoryStruct<macho::RelocationEntry> RE;
1219 getRelocation(Rel, RE);
1221 unsigned Arch = getArch();
1222 bool isScattered = (Arch != Triple::x86_64) &&
1223 (RE->Word0 & macho::RF_Scattered);
1226 Type = (RE->Word0 >> 24) & 0xF;
1228 Type = (RE->Word1 >> 28) & 0xF;
1232 // On arches that use the generic relocations, GENERIC_RELOC_PAIR
1233 // is always hidden.
1234 if (Arch == Triple::x86 || Arch == Triple::arm) {
1235 if (Type == macho::RIT_Pair) Result = true;
1236 } else if (Arch == Triple::x86_64) {
1237 // On x86_64, X86_64_RELOC_UNSIGNED is hidden only when it follows
1238 // an X864_64_RELOC_SUBTRACTOR.
1239 if (Type == macho::RIT_X86_64_Unsigned && Rel.d.a > 0) {
1240 DataRefImpl RelPrev = Rel;
1242 InMemoryStruct<macho::RelocationEntry> REPrev;
1243 getRelocation(RelPrev, REPrev);
1245 unsigned PrevType = (REPrev->Word1 >> 28) & 0xF;
1247 if (PrevType == macho::RIT_X86_64_Subtractor) Result = true;
1251 return object_error::success;
1254 error_code MachOObjectFile::getLibraryNext(DataRefImpl LibData,
1255 LibraryRef &Res) const {
1256 report_fatal_error("Needed libraries unimplemented in MachOObjectFile");
1259 error_code MachOObjectFile::getLibraryPath(DataRefImpl LibData,
1260 StringRef &Res) const {
1261 report_fatal_error("Needed libraries unimplemented in MachOObjectFile");
1265 /*===-- Miscellaneous -----------------------------------------------------===*/
1267 uint8_t MachOObjectFile::getBytesInAddress() const {
1268 return MachOObj->is64Bit() ? 8 : 4;
1271 StringRef MachOObjectFile::getFileFormatName() const {
1272 if (!MachOObj->is64Bit()) {
1273 switch (MachOObj->getHeader().CPUType) {
1274 case llvm::MachO::CPUTypeI386:
1275 return "Mach-O 32-bit i386";
1276 case llvm::MachO::CPUTypeARM:
1277 return "Mach-O arm";
1278 case llvm::MachO::CPUTypePowerPC:
1279 return "Mach-O 32-bit ppc";
1281 assert((MachOObj->getHeader().CPUType & llvm::MachO::CPUArchABI64) == 0 &&
1282 "64-bit object file when we're not 64-bit?");
1283 return "Mach-O 32-bit unknown";
1287 switch (MachOObj->getHeader().CPUType) {
1288 case llvm::MachO::CPUTypeX86_64:
1289 return "Mach-O 64-bit x86-64";
1290 case llvm::MachO::CPUTypePowerPC64:
1291 return "Mach-O 64-bit ppc64";
1293 assert((MachOObj->getHeader().CPUType & llvm::MachO::CPUArchABI64) == 1 &&
1294 "32-bit object file when we're 64-bit?");
1295 return "Mach-O 64-bit unknown";
1299 unsigned MachOObjectFile::getArch() const {
1300 switch (MachOObj->getHeader().CPUType) {
1301 case llvm::MachO::CPUTypeI386:
1303 case llvm::MachO::CPUTypeX86_64:
1304 return Triple::x86_64;
1305 case llvm::MachO::CPUTypeARM:
1307 case llvm::MachO::CPUTypePowerPC:
1309 case llvm::MachO::CPUTypePowerPC64:
1310 return Triple::ppc64;
1312 return Triple::UnknownArch;
1316 } // end namespace object
1317 } // end namespace llvm