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 const MachOFormat::Section64 *Section =
142 getSection64(Sections[Entry->SectionIndex-1]);
143 Result += Section->Offset - Section->Address;
146 InMemoryStruct<macho::SymbolTableEntry> Entry;
147 getSymbolTableEntry(DRI, Entry);
148 Result = Entry->Value;
149 if (Entry->SectionIndex) {
150 const MachOFormat::Section *Section =
151 getSection(Sections[Entry->SectionIndex-1]);
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(static_cast<unsigned char>(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;
447 static bool is64BitLoadCommand(const MachOObject *MachOObj, DataRefImpl DRI) {
448 LoadCommandInfo LCI = MachOObj->getLoadCommandInfo(DRI.d.a);
449 if (LCI.Command.Type == macho::LCT_Segment64)
451 assert(LCI.Command.Type == macho::LCT_Segment && "Unexpected Type.");
455 const MachOFormat::Section *MachOObjectFile::getSection(DataRefImpl DRI) const {
456 assert(!is64BitLoadCommand(MachOObj.get(), DRI));
457 LoadCommandInfo LCI = MachOObj->getLoadCommandInfo(DRI.d.a);
458 unsigned SectionOffset = LCI.Offset + sizeof(macho::SegmentLoadCommand) +
459 DRI.d.b * sizeof(MachOFormat::Section);
460 StringRef Data = MachOObj->getData(SectionOffset, sizeof(MachOFormat::Section));
461 return reinterpret_cast<const MachOFormat::Section*>(Data.data());
464 std::size_t MachOObjectFile::getSectionIndex(DataRefImpl Sec) const {
465 SectionList::const_iterator loc =
466 std::find(Sections.begin(), Sections.end(), Sec);
467 assert(loc != Sections.end() && "Sec is not a valid section!");
468 return std::distance(Sections.begin(), loc);
471 const MachOFormat::Section64 *
472 MachOObjectFile::getSection64(DataRefImpl DRI) const {
473 assert(is64BitLoadCommand(MachOObj.get(), DRI));
474 LoadCommandInfo LCI = MachOObj->getLoadCommandInfo(DRI.d.a);
475 unsigned SectionOffset = LCI.Offset + sizeof(macho::Segment64LoadCommand) +
476 DRI.d.b * sizeof(MachOFormat::Section64);
477 StringRef Data = MachOObj->getData(SectionOffset, sizeof(MachOFormat::Section64));
478 return reinterpret_cast<const MachOFormat::Section64*>(Data.data());
481 static StringRef parseSegmentOrSectionName(const char *P) {
485 // Not null terminated, so this is a 16 char string.
486 return StringRef(P, 16);
489 ArrayRef<char> MachOObjectFile::getSectionRawName(DataRefImpl DRI) const {
490 if (is64BitLoadCommand(MachOObj.get(), DRI)) {
491 const MachOFormat::Section64 *sec = getSection64(DRI);
492 return ArrayRef<char>(sec->Name);
494 const MachOFormat::Section *sec = getSection(DRI);
495 return ArrayRef<char>(sec->Name);
499 error_code MachOObjectFile::getSectionName(DataRefImpl DRI,
500 StringRef &Result) const {
501 ArrayRef<char> Raw = getSectionRawName(DRI);
502 Result = parseSegmentOrSectionName(Raw.data());
503 return object_error::success;
507 MachOObjectFile::getSectionRawFinalSegmentName(DataRefImpl Sec) const {
508 if (is64BitLoadCommand(MachOObj.get(), Sec)) {
509 const MachOFormat::Section64 *sec = getSection64(Sec);
510 return ArrayRef<char>(sec->SegmentName, 16);
512 const MachOFormat::Section *sec = getSection(Sec);
513 return ArrayRef<char>(sec->SegmentName);
517 StringRef MachOObjectFile::getSectionFinalSegmentName(DataRefImpl DRI) const {
518 ArrayRef<char> Raw = getSectionRawFinalSegmentName(DRI);
519 return parseSegmentOrSectionName(Raw.data());
522 error_code MachOObjectFile::getSectionAddress(DataRefImpl DRI,
523 uint64_t &Result) const {
524 if (is64BitLoadCommand(MachOObj.get(), DRI)) {
525 const MachOFormat::Section64 *Sect = getSection64(DRI);
526 Result = Sect->Address;
528 const MachOFormat::Section *Sect = getSection(DRI);
529 Result = Sect->Address;
531 return object_error::success;
534 error_code MachOObjectFile::getSectionSize(DataRefImpl DRI,
535 uint64_t &Result) const {
536 if (is64BitLoadCommand(MachOObj.get(), DRI)) {
537 const MachOFormat::Section64 *Sect = getSection64(DRI);
540 const MachOFormat::Section *Sect = getSection(DRI);
543 return object_error::success;
546 error_code MachOObjectFile::getSectionContents(DataRefImpl DRI,
547 StringRef &Result) const {
548 if (is64BitLoadCommand(MachOObj.get(), DRI)) {
549 const MachOFormat::Section64 *Sect = getSection64(DRI);
550 Result = MachOObj->getData(Sect->Offset, Sect->Size);
552 const MachOFormat::Section *Sect = getSection(DRI);
553 Result = MachOObj->getData(Sect->Offset, Sect->Size);
555 return object_error::success;
558 error_code MachOObjectFile::getSectionAlignment(DataRefImpl DRI,
559 uint64_t &Result) const {
560 if (is64BitLoadCommand(MachOObj.get(), DRI)) {
561 const MachOFormat::Section64 *Sect = getSection64(DRI);
562 Result = uint64_t(1) << Sect->Align;
564 const MachOFormat::Section *Sect = getSection(DRI);
565 Result = uint64_t(1) << Sect->Align;
567 return object_error::success;
570 error_code MachOObjectFile::isSectionText(DataRefImpl DRI,
571 bool &Result) const {
572 if (is64BitLoadCommand(MachOObj.get(), DRI)) {
573 const MachOFormat::Section64 *Sect = getSection64(DRI);
574 Result = Sect->Flags & macho::SF_PureInstructions;
576 const MachOFormat::Section *Sect = getSection(DRI);
577 Result = Sect->Flags & macho::SF_PureInstructions;
579 return object_error::success;
582 error_code MachOObjectFile::isSectionData(DataRefImpl DRI,
583 bool &Result) const {
584 // FIXME: Unimplemented.
586 return object_error::success;
589 error_code MachOObjectFile::isSectionBSS(DataRefImpl DRI,
590 bool &Result) const {
591 // FIXME: Unimplemented.
593 return object_error::success;
596 error_code MachOObjectFile::isSectionRequiredForExecution(DataRefImpl Sec,
597 bool &Result) const {
598 // FIXME: Unimplemented.
600 return object_error::success;
603 error_code MachOObjectFile::isSectionVirtual(DataRefImpl Sec,
604 bool &Result) const {
605 // FIXME: Unimplemented.
607 return object_error::success;
610 error_code MachOObjectFile::isSectionZeroInit(DataRefImpl DRI,
611 bool &Result) const {
612 if (MachOObj->is64Bit()) {
613 const MachOFormat::Section64 *Sect = getSection64(DRI);
614 unsigned SectionType = Sect->Flags & MachO::SectionFlagMaskSectionType;
615 Result = (SectionType == MachO::SectionTypeZeroFill ||
616 SectionType == MachO::SectionTypeZeroFillLarge);
618 const MachOFormat::Section *Sect = getSection(DRI);
619 unsigned SectionType = Sect->Flags & MachO::SectionFlagMaskSectionType;
620 Result = (SectionType == MachO::SectionTypeZeroFill ||
621 SectionType == MachO::SectionTypeZeroFillLarge);
624 return object_error::success;
627 error_code MachOObjectFile::isSectionReadOnlyData(DataRefImpl Sec,
628 bool &Result) const {
629 // Consider using the code from isSectionText to look for __const sections.
630 // Alternately, emit S_ATTR_PURE_INSTRUCTIONS and/or S_ATTR_SOME_INSTRUCTIONS
631 // to use section attributes to distinguish code from data.
633 // FIXME: Unimplemented.
635 return object_error::success;
638 error_code MachOObjectFile::sectionContainsSymbol(DataRefImpl Sec,
640 bool &Result) const {
642 getSymbolType(Symb, ST);
643 if (ST == SymbolRef::ST_Unknown) {
645 return object_error::success;
648 uint64_t SectBegin, SectEnd;
649 getSectionAddress(Sec, SectBegin);
650 getSectionSize(Sec, SectEnd);
651 SectEnd += SectBegin;
653 if (MachOObj->is64Bit()) {
654 InMemoryStruct<macho::Symbol64TableEntry> Entry;
655 getSymbol64TableEntry(Symb, Entry);
656 uint64_t SymAddr= Entry->Value;
657 Result = (SymAddr >= SectBegin) && (SymAddr < SectEnd);
659 InMemoryStruct<macho::SymbolTableEntry> Entry;
660 getSymbolTableEntry(Symb, Entry);
661 uint64_t SymAddr= Entry->Value;
662 Result = (SymAddr >= SectBegin) && (SymAddr < SectEnd);
665 return object_error::success;
668 relocation_iterator MachOObjectFile::getSectionRelBegin(DataRefImpl Sec) const {
670 ret.d.b = getSectionIndex(Sec);
671 return relocation_iterator(RelocationRef(ret, this));
673 relocation_iterator MachOObjectFile::getSectionRelEnd(DataRefImpl Sec) const {
675 if (is64BitLoadCommand(MachOObj.get(), Sec)) {
676 const MachOFormat::Section64 *Sect = getSection64(Sec);
677 last_reloc = Sect->NumRelocationTableEntries;
679 const MachOFormat::Section *Sect = getSection(Sec);
680 last_reloc = Sect->NumRelocationTableEntries;
683 ret.d.a = last_reloc;
684 ret.d.b = getSectionIndex(Sec);
685 return relocation_iterator(RelocationRef(ret, this));
688 section_iterator MachOObjectFile::begin_sections() const {
690 moveToNextSection(DRI);
691 return section_iterator(SectionRef(DRI, this));
694 section_iterator MachOObjectFile::end_sections() const {
696 DRI.d.a = MachOObj->getHeader().NumLoadCommands;
697 return section_iterator(SectionRef(DRI, this));
700 /*===-- Relocations -------------------------------------------------------===*/
702 const MachOFormat::RelocationEntry *
703 MachOObjectFile::getRelocation(DataRefImpl Rel) const {
705 if (MachOObj->is64Bit()) {
706 const MachOFormat::Section64 *Sect = getSection64(Sections[Rel.d.b]);
707 relOffset = Sect->RelocationTableOffset;
709 const MachOFormat::Section *Sect = getSection(Sections[Rel.d.b]);
710 relOffset = Sect->RelocationTableOffset;
712 uint64_t Offset = relOffset + Rel.d.a * sizeof(MachOFormat::RelocationEntry);
714 MachOObj->getData(Offset, sizeof(MachOFormat::RelocationEntry));
715 return reinterpret_cast<const MachOFormat::RelocationEntry*>(Data.data());
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 const MachOFormat::Section64 *Sect = getSection64(Sections[Rel.d.b]);
729 sectAddress += Sect->Address;
731 const MachOFormat::Section *Sect = getSection(Sections[Rel.d.b]);
732 sectAddress += Sect->Address;
734 const MachOFormat::RelocationEntry *RE = getRelocation(Rel);
736 unsigned Arch = getArch();
737 bool isScattered = (Arch != Triple::x86_64) &&
738 (RE->Word0 & macho::RF_Scattered);
739 uint64_t RelAddr = 0;
741 RelAddr = RE->Word0 & 0xFFFFFF;
745 Res = reinterpret_cast<uintptr_t>(sectAddress + RelAddr);
746 return object_error::success;
748 error_code MachOObjectFile::getRelocationOffset(DataRefImpl Rel,
749 uint64_t &Res) const {
750 const MachOFormat::RelocationEntry *RE = getRelocation(Rel);
752 unsigned Arch = getArch();
753 bool isScattered = (Arch != Triple::x86_64) &&
754 (RE->Word0 & macho::RF_Scattered);
756 Res = RE->Word0 & 0xFFFFFF;
759 return object_error::success;
761 error_code MachOObjectFile::getRelocationSymbol(DataRefImpl Rel,
762 SymbolRef &Res) const {
763 const MachOFormat::RelocationEntry *RE = getRelocation(Rel);
764 uint32_t SymbolIdx = RE->Word1 & 0xffffff;
765 bool isExtern = (RE->Word1 >> 27) & 1;
768 moveToNextSymbol(Sym);
770 for (unsigned i = 0; i < SymbolIdx; i++) {
772 moveToNextSymbol(Sym);
773 assert(Sym.d.a < MachOObj->getHeader().NumLoadCommands &&
774 "Relocation symbol index out of range!");
777 Res = SymbolRef(Sym, this);
778 return object_error::success;
780 error_code MachOObjectFile::getRelocationType(DataRefImpl Rel,
781 uint64_t &Res) const {
782 const MachOFormat::RelocationEntry *RE = getRelocation(Rel);
786 return object_error::success;
788 error_code MachOObjectFile::getRelocationTypeName(DataRefImpl Rel,
789 SmallVectorImpl<char> &Result) const {
790 // TODO: Support scattered relocations.
792 const MachOFormat::RelocationEntry *RE = getRelocation(Rel);
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 const MachOFormat::RelocationEntry *RE = getRelocation(Rel);
890 bool isExtern = (RE->Word1 >> 27) & 1;
893 const uint8_t* sectAddress = base();
894 if (MachOObj->is64Bit()) {
895 const MachOFormat::Section64 *Sect = getSection64(Sections[Rel.d.b]);
896 sectAddress += Sect->Offset;
898 const MachOFormat::Section *Sect = getSection(Sections[Rel.d.b]);
899 sectAddress += Sect->Offset;
901 Res = reinterpret_cast<uintptr_t>(sectAddress);
903 return object_error::success;
906 // Helper to advance a section or symbol iterator multiple increments at a time.
908 error_code advance(T &it, size_t Val) {
917 void advanceTo(T &it, size_t Val) {
918 if (error_code ec = advance(it, Val))
919 report_fatal_error(ec.message());
922 void MachOObjectFile::printRelocationTargetName(
923 const MachOFormat::RelocationEntry *RE,
924 raw_string_ostream &fmt) const {
925 unsigned Arch = getArch();
926 bool isScattered = (Arch != Triple::x86_64) &&
927 (RE->Word0 & macho::RF_Scattered);
929 // Target of a scattered relocation is an address. In the interest of
930 // generating pretty output, scan through the symbol table looking for a
931 // symbol that aligns with that address. If we find one, print it.
932 // Otherwise, we just print the hex address of the target.
934 uint32_t Val = RE->Word1;
937 for (symbol_iterator SI = begin_symbols(), SE = end_symbols(); SI != SE;
939 if (ec) report_fatal_error(ec.message());
944 if ((ec = SI->getAddress(Addr)))
945 report_fatal_error(ec.message());
946 if (Addr != Val) continue;
947 if ((ec = SI->getName(Name)))
948 report_fatal_error(ec.message());
953 // If we couldn't find a symbol that this relocation refers to, try
954 // to find a section beginning instead.
955 for (section_iterator SI = begin_sections(), SE = end_sections(); SI != SE;
957 if (ec) report_fatal_error(ec.message());
962 if ((ec = SI->getAddress(Addr)))
963 report_fatal_error(ec.message());
964 if (Addr != Val) continue;
965 if ((ec = SI->getName(Name)))
966 report_fatal_error(ec.message());
971 fmt << format("0x%x", Val);
976 bool isExtern = (RE->Word1 >> 27) & 1;
977 uint32_t Val = RE->Word1 & 0xFFFFFF;
980 symbol_iterator SI = begin_symbols();
984 section_iterator SI = begin_sections();
992 error_code MachOObjectFile::getRelocationValueString(DataRefImpl Rel,
993 SmallVectorImpl<char> &Result) const {
994 const MachOFormat::RelocationEntry *RE = getRelocation(Rel);
996 unsigned Arch = getArch();
997 bool isScattered = (Arch != Triple::x86_64) &&
998 (RE->Word0 & macho::RF_Scattered);
1001 raw_string_ostream fmt(fmtbuf);
1005 Type = (RE->Word0 >> 24) & 0xF;
1007 Type = (RE->Word1 >> 28) & 0xF;
1011 isPCRel = ((RE->Word0 >> 30) & 1);
1013 isPCRel = ((RE->Word1 >> 24) & 1);
1015 // Determine any addends that should be displayed with the relocation.
1016 // These require decoding the relocation type, which is triple-specific.
1018 // X86_64 has entirely custom relocation types.
1019 if (Arch == Triple::x86_64) {
1020 bool isPCRel = ((RE->Word1 >> 24) & 1);
1023 case macho::RIT_X86_64_GOTLoad: // X86_64_RELOC_GOT_LOAD
1024 case macho::RIT_X86_64_GOT: { // X86_64_RELOC_GOT
1025 printRelocationTargetName(RE, fmt);
1027 if (isPCRel) fmt << "PCREL";
1030 case macho::RIT_X86_64_Subtractor: { // X86_64_RELOC_SUBTRACTOR
1031 DataRefImpl RelNext = Rel;
1033 const MachOFormat::RelocationEntry *RENext = getRelocation(RelNext);
1035 // X86_64_SUBTRACTOR must be followed by a relocation of type
1036 // X86_64_RELOC_UNSIGNED.
1037 // NOTE: Scattered relocations don't exist on x86_64.
1038 unsigned RType = (RENext->Word1 >> 28) & 0xF;
1040 report_fatal_error("Expected X86_64_RELOC_UNSIGNED after "
1041 "X86_64_RELOC_SUBTRACTOR.");
1043 // The X86_64_RELOC_UNSIGNED contains the minuend symbol,
1044 // X86_64_SUBTRACTOR contains to the subtrahend.
1045 printRelocationTargetName(RENext, fmt);
1047 printRelocationTargetName(RE, fmt);
1050 case macho::RIT_X86_64_TLV:
1051 printRelocationTargetName(RE, fmt);
1053 if (isPCRel) fmt << "P";
1055 case macho::RIT_X86_64_Signed1: // X86_64_RELOC_SIGNED1
1056 printRelocationTargetName(RE, fmt);
1059 case macho::RIT_X86_64_Signed2: // X86_64_RELOC_SIGNED2
1060 printRelocationTargetName(RE, fmt);
1063 case macho::RIT_X86_64_Signed4: // X86_64_RELOC_SIGNED4
1064 printRelocationTargetName(RE, fmt);
1068 printRelocationTargetName(RE, fmt);
1071 // X86 and ARM share some relocation types in common.
1072 } else if (Arch == Triple::x86 || Arch == Triple::arm) {
1073 // Generic relocation types...
1075 case macho::RIT_Pair: // GENERIC_RELOC_PAIR - prints no info
1076 return object_error::success;
1077 case macho::RIT_Difference: { // GENERIC_RELOC_SECTDIFF
1078 DataRefImpl RelNext = Rel;
1080 const MachOFormat::RelocationEntry *RENext = getRelocation(RelNext);
1082 // X86 sect diff's must be followed by a relocation of type
1083 // GENERIC_RELOC_PAIR.
1084 bool isNextScattered = (Arch != Triple::x86_64) &&
1085 (RENext->Word0 & macho::RF_Scattered);
1087 if (isNextScattered)
1088 RType = (RENext->Word0 >> 24) & 0xF;
1090 RType = (RENext->Word1 >> 28) & 0xF;
1092 report_fatal_error("Expected GENERIC_RELOC_PAIR after "
1093 "GENERIC_RELOC_SECTDIFF.");
1095 printRelocationTargetName(RE, fmt);
1097 printRelocationTargetName(RENext, fmt);
1102 if (Arch == Triple::x86) {
1103 // All X86 relocations that need special printing were already
1104 // handled in the generic code.
1106 case macho::RIT_Generic_LocalDifference:{// GENERIC_RELOC_LOCAL_SECTDIFF
1107 DataRefImpl RelNext = Rel;
1109 const MachOFormat::RelocationEntry *RENext = getRelocation(RelNext);
1111 // X86 sect diff's must be followed by a relocation of type
1112 // GENERIC_RELOC_PAIR.
1113 bool isNextScattered = (Arch != Triple::x86_64) &&
1114 (RENext->Word0 & macho::RF_Scattered);
1116 if (isNextScattered)
1117 RType = (RENext->Word0 >> 24) & 0xF;
1119 RType = (RENext->Word1 >> 28) & 0xF;
1121 report_fatal_error("Expected GENERIC_RELOC_PAIR after "
1122 "GENERIC_RELOC_LOCAL_SECTDIFF.");
1124 printRelocationTargetName(RE, fmt);
1126 printRelocationTargetName(RENext, fmt);
1129 case macho::RIT_Generic_TLV: {
1130 printRelocationTargetName(RE, fmt);
1132 if (isPCRel) fmt << "P";
1136 printRelocationTargetName(RE, fmt);
1138 } else { // ARM-specific relocations
1140 case macho::RIT_ARM_Half: // ARM_RELOC_HALF
1141 case macho::RIT_ARM_HalfDifference: { // ARM_RELOC_HALF_SECTDIFF
1142 // Half relocations steal a bit from the length field to encode
1143 // whether this is an upper16 or a lower16 relocation.
1146 isUpper = (RE->Word0 >> 28) & 1;
1148 isUpper = (RE->Word1 >> 25) & 1;
1151 fmt << ":upper16:(";
1153 fmt << ":lower16:(";
1154 printRelocationTargetName(RE, fmt);
1156 DataRefImpl RelNext = Rel;
1158 const MachOFormat::RelocationEntry *RENext = getRelocation(RelNext);
1160 // ARM half relocs must be followed by a relocation of type
1162 bool isNextScattered = (Arch != Triple::x86_64) &&
1163 (RENext->Word0 & macho::RF_Scattered);
1165 if (isNextScattered)
1166 RType = (RENext->Word0 >> 24) & 0xF;
1168 RType = (RENext->Word1 >> 28) & 0xF;
1171 report_fatal_error("Expected ARM_RELOC_PAIR after "
1172 "GENERIC_RELOC_HALF");
1174 // NOTE: The half of the target virtual address is stashed in the
1175 // address field of the secondary relocation, but we can't reverse
1176 // engineer the constant offset from it without decoding the movw/movt
1177 // instruction to find the other half in its immediate field.
1179 // ARM_RELOC_HALF_SECTDIFF encodes the second section in the
1180 // symbol/section pointer of the follow-on relocation.
1181 if (Type == macho::RIT_ARM_HalfDifference) {
1183 printRelocationTargetName(RENext, fmt);
1190 printRelocationTargetName(RE, fmt);
1195 printRelocationTargetName(RE, fmt);
1198 Result.append(fmtbuf.begin(), fmtbuf.end());
1199 return object_error::success;
1202 error_code MachOObjectFile::getRelocationHidden(DataRefImpl Rel,
1203 bool &Result) const {
1204 const MachOFormat::RelocationEntry *RE = getRelocation(Rel);
1206 unsigned Arch = getArch();
1207 bool isScattered = (Arch != Triple::x86_64) &&
1208 (RE->Word0 & macho::RF_Scattered);
1211 Type = (RE->Word0 >> 24) & 0xF;
1213 Type = (RE->Word1 >> 28) & 0xF;
1217 // On arches that use the generic relocations, GENERIC_RELOC_PAIR
1218 // is always hidden.
1219 if (Arch == Triple::x86 || Arch == Triple::arm) {
1220 if (Type == macho::RIT_Pair) Result = true;
1221 } else if (Arch == Triple::x86_64) {
1222 // On x86_64, X86_64_RELOC_UNSIGNED is hidden only when it follows
1223 // an X864_64_RELOC_SUBTRACTOR.
1224 if (Type == macho::RIT_X86_64_Unsigned && Rel.d.a > 0) {
1225 DataRefImpl RelPrev = Rel;
1227 const MachOFormat::RelocationEntry *REPrev = getRelocation(RelPrev);
1229 unsigned PrevType = (REPrev->Word1 >> 28) & 0xF;
1231 if (PrevType == macho::RIT_X86_64_Subtractor) Result = true;
1235 return object_error::success;
1238 error_code MachOObjectFile::getLibraryNext(DataRefImpl LibData,
1239 LibraryRef &Res) const {
1240 report_fatal_error("Needed libraries unimplemented in MachOObjectFile");
1243 error_code MachOObjectFile::getLibraryPath(DataRefImpl LibData,
1244 StringRef &Res) const {
1245 report_fatal_error("Needed libraries unimplemented in MachOObjectFile");
1249 /*===-- Miscellaneous -----------------------------------------------------===*/
1251 uint8_t MachOObjectFile::getBytesInAddress() const {
1252 return MachOObj->is64Bit() ? 8 : 4;
1255 StringRef MachOObjectFile::getFileFormatName() const {
1256 if (!MachOObj->is64Bit()) {
1257 switch (MachOObj->getHeader().CPUType) {
1258 case llvm::MachO::CPUTypeI386:
1259 return "Mach-O 32-bit i386";
1260 case llvm::MachO::CPUTypeARM:
1261 return "Mach-O arm";
1262 case llvm::MachO::CPUTypePowerPC:
1263 return "Mach-O 32-bit ppc";
1265 assert((MachOObj->getHeader().CPUType & llvm::MachO::CPUArchABI64) == 0 &&
1266 "64-bit object file when we're not 64-bit?");
1267 return "Mach-O 32-bit unknown";
1271 // Make sure the cpu type has the correct mask.
1272 assert((MachOObj->getHeader().CPUType & llvm::MachO::CPUArchABI64)
1273 == llvm::MachO::CPUArchABI64 &&
1274 "32-bit object file when we're 64-bit?");
1276 switch (MachOObj->getHeader().CPUType) {
1277 case llvm::MachO::CPUTypeX86_64:
1278 return "Mach-O 64-bit x86-64";
1279 case llvm::MachO::CPUTypePowerPC64:
1280 return "Mach-O 64-bit ppc64";
1282 return "Mach-O 64-bit unknown";
1286 unsigned MachOObjectFile::getArch() const {
1287 switch (MachOObj->getHeader().CPUType) {
1288 case llvm::MachO::CPUTypeI386:
1290 case llvm::MachO::CPUTypeX86_64:
1291 return Triple::x86_64;
1292 case llvm::MachO::CPUTypeARM:
1294 case llvm::MachO::CPUTypePowerPC:
1296 case llvm::MachO::CPUTypePowerPC64:
1297 return Triple::ppc64;
1299 return Triple::UnknownArch;
1303 } // end namespace object
1304 } // end namespace llvm