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),
35 moveToNextSection(DRI);
36 uint32_t LoadCommandCount = MachOObj->getHeader().NumLoadCommands;
37 while (DRI.d.a < LoadCommandCount) {
38 Sections.push_back(DRI);
40 moveToNextSection(DRI);
45 ObjectFile *ObjectFile::createMachOObjectFile(MemoryBuffer *Buffer) {
48 MachOObject *MachOObj = MachOObject::LoadFromBuffer(Buffer, &Err);
51 // MachOObject takes ownership of the Buffer we passed to it, and
52 // MachOObjectFile does, too, so we need to make sure they don't get the
53 // same object. A MemoryBuffer is cheap (it's just a reference to memory,
54 // not a copy of the memory itself), so just make a new copy here for
55 // the MachOObjectFile.
56 MemoryBuffer *NewBuffer =
57 MemoryBuffer::getMemBuffer(Buffer->getBuffer(),
58 Buffer->getBufferIdentifier(), false);
59 return new MachOObjectFile(NewBuffer, MachOObj, ec);
62 /*===-- Symbols -----------------------------------------------------------===*/
64 const MachOFormat::SymtabLoadCommand *
65 MachOObjectFile::getSymtabLoadCommand(LoadCommandInfo LCI) const {
66 StringRef Data = MachOObj->getData(LCI.Offset,
67 sizeof(MachOFormat::SymtabLoadCommand));
68 return reinterpret_cast<const MachOFormat::SymtabLoadCommand*>(Data.data());
71 void MachOObjectFile::moveToNextSymbol(DataRefImpl &DRI) const {
72 uint32_t LoadCommandCount = MachOObj->getHeader().NumLoadCommands;
73 while (DRI.d.a < LoadCommandCount) {
74 LoadCommandInfo LCI = MachOObj->getLoadCommandInfo(DRI.d.a);
75 if (LCI.Command.Type == macho::LCT_Symtab) {
76 const MachOFormat::SymtabLoadCommand *SymtabLoadCmd =
77 getSymtabLoadCommand(LCI);
78 if (DRI.d.b < SymtabLoadCmd->NumSymbolTableEntries)
87 const MachOFormat::SymbolTableEntry *
88 MachOObjectFile::getSymbolTableEntry(DataRefImpl DRI) const {
89 LoadCommandInfo LCI = MachOObj->getLoadCommandInfo(DRI.d.a);
90 const MachOFormat::SymtabLoadCommand *SymtabLoadCmd =
91 getSymtabLoadCommand(LCI);
93 return getSymbolTableEntry(DRI, SymtabLoadCmd);
96 const MachOFormat::SymbolTableEntry *
97 MachOObjectFile::getSymbolTableEntry(DataRefImpl DRI,
98 const MachOFormat::SymtabLoadCommand *SymtabLoadCmd) const {
99 uint64_t SymbolTableOffset = SymtabLoadCmd->SymbolTableOffset;
100 unsigned Index = DRI.d.b;
101 uint64_t Offset = (SymbolTableOffset +
102 Index * sizeof(macho::SymbolTableEntry));
103 StringRef Data = MachOObj->getData(Offset,
104 sizeof(MachOFormat::SymbolTableEntry));
105 return reinterpret_cast<const MachOFormat::SymbolTableEntry*>(Data.data());
108 const MachOFormat::Symbol64TableEntry*
109 MachOObjectFile::getSymbol64TableEntry(DataRefImpl DRI) const {
110 LoadCommandInfo LCI = MachOObj->getLoadCommandInfo(DRI.d.a);
111 const MachOFormat::SymtabLoadCommand *SymtabLoadCmd =
112 getSymtabLoadCommand(LCI);
114 return getSymbol64TableEntry(DRI, SymtabLoadCmd);
117 const MachOFormat::Symbol64TableEntry*
118 MachOObjectFile::getSymbol64TableEntry(DataRefImpl DRI,
119 const MachOFormat::SymtabLoadCommand *SymtabLoadCmd) const {
120 uint64_t SymbolTableOffset = SymtabLoadCmd->SymbolTableOffset;
121 unsigned Index = DRI.d.b;
122 uint64_t Offset = (SymbolTableOffset +
123 Index * sizeof(macho::Symbol64TableEntry));
124 StringRef Data = MachOObj->getData(Offset,
125 sizeof(MachOFormat::Symbol64TableEntry));
126 return reinterpret_cast<const MachOFormat::Symbol64TableEntry*>(Data.data());
129 error_code MachOObjectFile::getSymbolNext(DataRefImpl DRI,
130 SymbolRef &Result) const {
132 moveToNextSymbol(DRI);
133 Result = SymbolRef(DRI, this);
134 return object_error::success;
137 error_code MachOObjectFile::getSymbolName(DataRefImpl DRI,
138 StringRef &Result) const {
139 LoadCommandInfo LCI = MachOObj->getLoadCommandInfo(DRI.d.a);
140 const MachOFormat::SymtabLoadCommand *SymtabLoadCmd =
141 getSymtabLoadCommand(LCI);
143 StringRef StringTable =
144 MachOObj->getData(SymtabLoadCmd->StringTableOffset,
145 SymtabLoadCmd->StringTableSize);
147 uint32_t StringIndex;
148 if (MachOObj->is64Bit()) {
149 const MachOFormat::Symbol64TableEntry *Entry =
150 getSymbol64TableEntry(DRI, SymtabLoadCmd);
151 StringIndex = Entry->StringIndex;
153 const MachOFormat::SymbolTableEntry *Entry =
154 getSymbolTableEntry(DRI, SymtabLoadCmd);
155 StringIndex = Entry->StringIndex;
158 const char *Start = &StringTable.data()[StringIndex];
159 Result = StringRef(Start);
161 return object_error::success;
164 error_code MachOObjectFile::getSymbolFileOffset(DataRefImpl DRI,
165 uint64_t &Result) const {
166 if (MachOObj->is64Bit()) {
167 const MachOFormat::Symbol64TableEntry *Entry = getSymbol64TableEntry(DRI);
168 Result = Entry->Value;
169 if (Entry->SectionIndex) {
170 const MachOFormat::Section64 *Section =
171 getSection64(Sections[Entry->SectionIndex-1]);
172 Result += Section->Offset - Section->Address;
175 const MachOFormat::SymbolTableEntry *Entry = getSymbolTableEntry(DRI);
176 Result = Entry->Value;
177 if (Entry->SectionIndex) {
178 const MachOFormat::Section *Section =
179 getSection(Sections[Entry->SectionIndex-1]);
180 Result += Section->Offset - Section->Address;
184 return object_error::success;
187 error_code MachOObjectFile::getSymbolAddress(DataRefImpl DRI,
188 uint64_t &Result) const {
189 if (MachOObj->is64Bit()) {
190 const MachOFormat::Symbol64TableEntry *Entry = getSymbol64TableEntry(DRI);
191 Result = Entry->Value;
193 const MachOFormat::SymbolTableEntry *Entry = getSymbolTableEntry(DRI);
194 Result = Entry->Value;
196 return object_error::success;
199 error_code MachOObjectFile::getSymbolSize(DataRefImpl DRI,
200 uint64_t &Result) const {
201 uint32_t LoadCommandCount = MachOObj->getHeader().NumLoadCommands;
202 uint64_t BeginOffset;
203 uint64_t EndOffset = 0;
204 uint8_t SectionIndex;
205 if (MachOObj->is64Bit()) {
206 const MachOFormat::Symbol64TableEntry *Entry = getSymbol64TableEntry(DRI);
207 BeginOffset = Entry->Value;
208 SectionIndex = Entry->SectionIndex;
210 uint32_t flags = SymbolRef::SF_None;
211 getSymbolFlags(DRI, flags);
212 if (flags & SymbolRef::SF_Common)
213 Result = Entry->Value;
215 Result = UnknownAddressOrSize;
216 return object_error::success;
218 // Unfortunately symbols are unsorted so we need to touch all
219 // symbols from load command
221 uint32_t Command = DRI.d.a;
222 while (Command == DRI.d.a) {
223 moveToNextSymbol(DRI);
224 if (DRI.d.a < LoadCommandCount) {
225 Entry = getSymbol64TableEntry(DRI);
226 if (Entry->SectionIndex == SectionIndex && Entry->Value > BeginOffset)
227 if (!EndOffset || Entry->Value < EndOffset)
228 EndOffset = Entry->Value;
233 const MachOFormat::SymbolTableEntry *Entry = getSymbolTableEntry(DRI);
234 BeginOffset = Entry->Value;
235 SectionIndex = Entry->SectionIndex;
237 uint32_t flags = SymbolRef::SF_None;
238 getSymbolFlags(DRI, flags);
239 if (flags & SymbolRef::SF_Common)
240 Result = Entry->Value;
242 Result = UnknownAddressOrSize;
243 return object_error::success;
245 // Unfortunately symbols are unsorted so we need to touch all
246 // symbols from load command
248 uint32_t Command = DRI.d.a;
249 while (Command == DRI.d.a) {
250 moveToNextSymbol(DRI);
251 if (DRI.d.a < LoadCommandCount) {
252 Entry = getSymbolTableEntry(DRI);
253 if (Entry->SectionIndex == SectionIndex && Entry->Value > BeginOffset)
254 if (!EndOffset || Entry->Value < EndOffset)
255 EndOffset = Entry->Value;
262 getSectionSize(Sections[SectionIndex-1], Size);
263 getSectionAddress(Sections[SectionIndex-1], EndOffset);
266 Result = EndOffset - BeginOffset;
267 return object_error::success;
270 error_code MachOObjectFile::getSymbolNMTypeChar(DataRefImpl DRI,
271 char &Result) const {
273 if (MachOObj->is64Bit()) {
274 const MachOFormat::Symbol64TableEntry *Entry = getSymbol64TableEntry(DRI);
276 Flags = Entry->Flags;
278 const MachOFormat::SymbolTableEntry *Entry = getSymbolTableEntry(DRI);
280 Flags = Entry->Flags;
284 switch (Type & macho::STF_TypeMask) {
285 case macho::STT_Undefined:
288 case macho::STT_Absolute:
289 case macho::STT_Section:
297 if (Flags & (macho::STF_External | macho::STF_PrivateExtern))
298 Char = toupper(static_cast<unsigned char>(Char));
300 return object_error::success;
303 error_code MachOObjectFile::getSymbolFlags(DataRefImpl DRI,
304 uint32_t &Result) const {
307 if (MachOObj->is64Bit()) {
308 const MachOFormat::Symbol64TableEntry *Entry = getSymbol64TableEntry(DRI);
309 MachOFlags = Entry->Flags;
310 MachOType = Entry->Type;
312 const MachOFormat::SymbolTableEntry *Entry = getSymbolTableEntry(DRI);
313 MachOFlags = Entry->Flags;
314 MachOType = Entry->Type;
317 // TODO: Correctly set SF_ThreadLocal
318 Result = SymbolRef::SF_None;
320 if ((MachOType & MachO::NlistMaskType) == MachO::NListTypeUndefined)
321 Result |= SymbolRef::SF_Undefined;
323 if (MachOFlags & macho::STF_StabsEntryMask)
324 Result |= SymbolRef::SF_FormatSpecific;
326 if (MachOType & MachO::NlistMaskExternal) {
327 Result |= SymbolRef::SF_Global;
328 if ((MachOType & MachO::NlistMaskType) == MachO::NListTypeUndefined)
329 Result |= SymbolRef::SF_Common;
332 if (MachOFlags & (MachO::NListDescWeakRef | MachO::NListDescWeakDef))
333 Result |= SymbolRef::SF_Weak;
335 if ((MachOType & MachO::NlistMaskType) == MachO::NListTypeAbsolute)
336 Result |= SymbolRef::SF_Absolute;
338 return object_error::success;
341 error_code MachOObjectFile::getSymbolSection(DataRefImpl Symb,
342 section_iterator &Res) const {
344 if (MachOObj->is64Bit()) {
345 const MachOFormat::Symbol64TableEntry *Entry = getSymbol64TableEntry(Symb);
346 index = Entry->SectionIndex;
348 const MachOFormat::SymbolTableEntry *Entry = getSymbolTableEntry(Symb);
349 index = Entry->SectionIndex;
353 Res = end_sections();
355 Res = section_iterator(SectionRef(Sections[index-1], this));
357 return object_error::success;
360 error_code MachOObjectFile::getSymbolType(DataRefImpl Symb,
361 SymbolRef::Type &Res) const {
363 if (MachOObj->is64Bit()) {
364 const MachOFormat::Symbol64TableEntry *Entry = getSymbol64TableEntry(Symb);
365 n_type = Entry->Type;
367 const MachOFormat::SymbolTableEntry *Entry = getSymbolTableEntry(Symb);
368 n_type = Entry->Type;
370 Res = SymbolRef::ST_Other;
372 // If this is a STAB debugging symbol, we can do nothing more.
373 if (n_type & MachO::NlistMaskStab) {
374 Res = SymbolRef::ST_Debug;
375 return object_error::success;
378 switch (n_type & MachO::NlistMaskType) {
379 case MachO::NListTypeUndefined :
380 Res = SymbolRef::ST_Unknown;
382 case MachO::NListTypeSection :
383 Res = SymbolRef::ST_Function;
386 return object_error::success;
389 error_code MachOObjectFile::getSymbolValue(DataRefImpl Symb,
390 uint64_t &Val) const {
391 report_fatal_error("getSymbolValue unimplemented in MachOObjectFile");
394 symbol_iterator MachOObjectFile::begin_symbols() const {
395 // DRI.d.a = segment number; DRI.d.b = symbol index.
397 moveToNextSymbol(DRI);
398 return symbol_iterator(SymbolRef(DRI, this));
401 symbol_iterator MachOObjectFile::end_symbols() const {
403 DRI.d.a = MachOObj->getHeader().NumLoadCommands;
404 return symbol_iterator(SymbolRef(DRI, this));
407 symbol_iterator MachOObjectFile::begin_dynamic_symbols() const {
409 report_fatal_error("Dynamic symbols unimplemented in MachOObjectFile");
412 symbol_iterator MachOObjectFile::end_dynamic_symbols() const {
414 report_fatal_error("Dynamic symbols unimplemented in MachOObjectFile");
417 library_iterator MachOObjectFile::begin_libraries_needed() const {
419 report_fatal_error("Needed libraries unimplemented in MachOObjectFile");
422 library_iterator MachOObjectFile::end_libraries_needed() const {
424 report_fatal_error("Needed libraries unimplemented in MachOObjectFile");
427 StringRef MachOObjectFile::getLoadName() const {
429 report_fatal_error("get_load_name() unimplemented in MachOObjectFile");
432 /*===-- Sections ----------------------------------------------------------===*/
434 void MachOObjectFile::moveToNextSection(DataRefImpl &DRI) const {
435 uint32_t LoadCommandCount = MachOObj->getHeader().NumLoadCommands;
436 while (DRI.d.a < LoadCommandCount) {
437 LoadCommandInfo LCI = MachOObj->getLoadCommandInfo(DRI.d.a);
438 if (LCI.Command.Type == macho::LCT_Segment) {
439 InMemoryStruct<macho::SegmentLoadCommand> SegmentLoadCmd;
440 MachOObj->ReadSegmentLoadCommand(LCI, SegmentLoadCmd);
441 if (DRI.d.b < SegmentLoadCmd->NumSections)
443 } else if (LCI.Command.Type == macho::LCT_Segment64) {
444 InMemoryStruct<macho::Segment64LoadCommand> Segment64LoadCmd;
445 MachOObj->ReadSegment64LoadCommand(LCI, Segment64LoadCmd);
446 if (DRI.d.b < Segment64LoadCmd->NumSections)
455 error_code MachOObjectFile::getSectionNext(DataRefImpl DRI,
456 SectionRef &Result) const {
458 moveToNextSection(DRI);
459 Result = SectionRef(DRI, this);
460 return object_error::success;
463 static bool is64BitLoadCommand(const MachOObject *MachOObj, DataRefImpl DRI) {
464 LoadCommandInfo LCI = MachOObj->getLoadCommandInfo(DRI.d.a);
465 if (LCI.Command.Type == macho::LCT_Segment64)
467 assert(LCI.Command.Type == macho::LCT_Segment && "Unexpected Type.");
471 const MachOFormat::Section *MachOObjectFile::getSection(DataRefImpl DRI) const {
472 assert(!is64BitLoadCommand(MachOObj.get(), DRI));
473 LoadCommandInfo LCI = MachOObj->getLoadCommandInfo(DRI.d.a);
474 unsigned SectionOffset = LCI.Offset + sizeof(macho::SegmentLoadCommand) +
475 DRI.d.b * sizeof(MachOFormat::Section);
476 StringRef Data = MachOObj->getData(SectionOffset, sizeof(MachOFormat::Section));
477 return reinterpret_cast<const MachOFormat::Section*>(Data.data());
480 std::size_t MachOObjectFile::getSectionIndex(DataRefImpl Sec) const {
481 SectionList::const_iterator loc =
482 std::find(Sections.begin(), Sections.end(), Sec);
483 assert(loc != Sections.end() && "Sec is not a valid section!");
484 return std::distance(Sections.begin(), loc);
487 const MachOFormat::Section64 *
488 MachOObjectFile::getSection64(DataRefImpl DRI) const {
489 assert(is64BitLoadCommand(MachOObj.get(), DRI));
490 LoadCommandInfo LCI = MachOObj->getLoadCommandInfo(DRI.d.a);
491 unsigned SectionOffset = LCI.Offset + sizeof(macho::Segment64LoadCommand) +
492 DRI.d.b * sizeof(MachOFormat::Section64);
493 StringRef Data = MachOObj->getData(SectionOffset, sizeof(MachOFormat::Section64));
494 return reinterpret_cast<const MachOFormat::Section64*>(Data.data());
497 static StringRef parseSegmentOrSectionName(const char *P) {
501 // Not null terminated, so this is a 16 char string.
502 return StringRef(P, 16);
505 ArrayRef<char> MachOObjectFile::getSectionRawName(DataRefImpl DRI) const {
506 if (is64BitLoadCommand(MachOObj.get(), DRI)) {
507 const MachOFormat::Section64 *sec = getSection64(DRI);
508 return ArrayRef<char>(sec->Name);
510 const MachOFormat::Section *sec = getSection(DRI);
511 return ArrayRef<char>(sec->Name);
515 error_code MachOObjectFile::getSectionName(DataRefImpl DRI,
516 StringRef &Result) const {
517 ArrayRef<char> Raw = getSectionRawName(DRI);
518 Result = parseSegmentOrSectionName(Raw.data());
519 return object_error::success;
523 MachOObjectFile::getSectionRawFinalSegmentName(DataRefImpl Sec) const {
524 if (is64BitLoadCommand(MachOObj.get(), Sec)) {
525 const MachOFormat::Section64 *sec = getSection64(Sec);
526 return ArrayRef<char>(sec->SegmentName, 16);
528 const MachOFormat::Section *sec = getSection(Sec);
529 return ArrayRef<char>(sec->SegmentName);
533 StringRef MachOObjectFile::getSectionFinalSegmentName(DataRefImpl DRI) const {
534 ArrayRef<char> Raw = getSectionRawFinalSegmentName(DRI);
535 return parseSegmentOrSectionName(Raw.data());
538 error_code MachOObjectFile::getSectionAddress(DataRefImpl DRI,
539 uint64_t &Result) const {
540 if (is64BitLoadCommand(MachOObj.get(), DRI)) {
541 const MachOFormat::Section64 *Sect = getSection64(DRI);
542 Result = Sect->Address;
544 const MachOFormat::Section *Sect = getSection(DRI);
545 Result = Sect->Address;
547 return object_error::success;
550 error_code MachOObjectFile::getSectionSize(DataRefImpl DRI,
551 uint64_t &Result) const {
552 if (is64BitLoadCommand(MachOObj.get(), DRI)) {
553 const MachOFormat::Section64 *Sect = getSection64(DRI);
556 const MachOFormat::Section *Sect = getSection(DRI);
559 return object_error::success;
562 error_code MachOObjectFile::getSectionContents(DataRefImpl DRI,
563 StringRef &Result) const {
564 if (is64BitLoadCommand(MachOObj.get(), DRI)) {
565 const MachOFormat::Section64 *Sect = getSection64(DRI);
566 Result = MachOObj->getData(Sect->Offset, Sect->Size);
568 const MachOFormat::Section *Sect = getSection(DRI);
569 Result = MachOObj->getData(Sect->Offset, Sect->Size);
571 return object_error::success;
574 error_code MachOObjectFile::getSectionAlignment(DataRefImpl DRI,
575 uint64_t &Result) const {
576 if (is64BitLoadCommand(MachOObj.get(), DRI)) {
577 const MachOFormat::Section64 *Sect = getSection64(DRI);
578 Result = uint64_t(1) << Sect->Align;
580 const MachOFormat::Section *Sect = getSection(DRI);
581 Result = uint64_t(1) << Sect->Align;
583 return object_error::success;
586 error_code MachOObjectFile::isSectionText(DataRefImpl DRI,
587 bool &Result) const {
588 if (is64BitLoadCommand(MachOObj.get(), DRI)) {
589 const MachOFormat::Section64 *Sect = getSection64(DRI);
590 Result = Sect->Flags & macho::SF_PureInstructions;
592 const MachOFormat::Section *Sect = getSection(DRI);
593 Result = Sect->Flags & macho::SF_PureInstructions;
595 return object_error::success;
598 error_code MachOObjectFile::isSectionData(DataRefImpl DRI,
599 bool &Result) const {
600 // FIXME: Unimplemented.
602 return object_error::success;
605 error_code MachOObjectFile::isSectionBSS(DataRefImpl DRI,
606 bool &Result) const {
607 // FIXME: Unimplemented.
609 return object_error::success;
612 error_code MachOObjectFile::isSectionRequiredForExecution(DataRefImpl Sec,
613 bool &Result) const {
614 // FIXME: Unimplemented.
616 return object_error::success;
619 error_code MachOObjectFile::isSectionVirtual(DataRefImpl Sec,
620 bool &Result) const {
621 // FIXME: Unimplemented.
623 return object_error::success;
626 error_code MachOObjectFile::isSectionZeroInit(DataRefImpl DRI,
627 bool &Result) const {
628 if (MachOObj->is64Bit()) {
629 const MachOFormat::Section64 *Sect = getSection64(DRI);
630 unsigned SectionType = Sect->Flags & MachO::SectionFlagMaskSectionType;
631 Result = (SectionType == MachO::SectionTypeZeroFill ||
632 SectionType == MachO::SectionTypeZeroFillLarge);
634 const MachOFormat::Section *Sect = getSection(DRI);
635 unsigned SectionType = Sect->Flags & MachO::SectionFlagMaskSectionType;
636 Result = (SectionType == MachO::SectionTypeZeroFill ||
637 SectionType == MachO::SectionTypeZeroFillLarge);
640 return object_error::success;
643 error_code MachOObjectFile::isSectionReadOnlyData(DataRefImpl Sec,
644 bool &Result) const {
645 // Consider using the code from isSectionText to look for __const sections.
646 // Alternately, emit S_ATTR_PURE_INSTRUCTIONS and/or S_ATTR_SOME_INSTRUCTIONS
647 // to use section attributes to distinguish code from data.
649 // FIXME: Unimplemented.
651 return object_error::success;
654 error_code MachOObjectFile::sectionContainsSymbol(DataRefImpl Sec,
656 bool &Result) const {
658 getSymbolType(Symb, ST);
659 if (ST == SymbolRef::ST_Unknown) {
661 return object_error::success;
664 uint64_t SectBegin, SectEnd;
665 getSectionAddress(Sec, SectBegin);
666 getSectionSize(Sec, SectEnd);
667 SectEnd += SectBegin;
669 if (MachOObj->is64Bit()) {
670 const MachOFormat::Symbol64TableEntry *Entry = getSymbol64TableEntry(Symb);
671 uint64_t SymAddr= Entry->Value;
672 Result = (SymAddr >= SectBegin) && (SymAddr < SectEnd);
674 const MachOFormat::SymbolTableEntry *Entry = getSymbolTableEntry(Symb);
675 uint64_t SymAddr= Entry->Value;
676 Result = (SymAddr >= SectBegin) && (SymAddr < SectEnd);
679 return object_error::success;
682 relocation_iterator MachOObjectFile::getSectionRelBegin(DataRefImpl Sec) const {
684 ret.d.b = getSectionIndex(Sec);
685 return relocation_iterator(RelocationRef(ret, this));
687 relocation_iterator MachOObjectFile::getSectionRelEnd(DataRefImpl Sec) const {
689 if (is64BitLoadCommand(MachOObj.get(), Sec)) {
690 const MachOFormat::Section64 *Sect = getSection64(Sec);
691 last_reloc = Sect->NumRelocationTableEntries;
693 const MachOFormat::Section *Sect = getSection(Sec);
694 last_reloc = Sect->NumRelocationTableEntries;
697 ret.d.a = last_reloc;
698 ret.d.b = getSectionIndex(Sec);
699 return relocation_iterator(RelocationRef(ret, this));
702 section_iterator MachOObjectFile::begin_sections() const {
704 moveToNextSection(DRI);
705 return section_iterator(SectionRef(DRI, this));
708 section_iterator MachOObjectFile::end_sections() const {
710 DRI.d.a = MachOObj->getHeader().NumLoadCommands;
711 return section_iterator(SectionRef(DRI, this));
714 /*===-- Relocations -------------------------------------------------------===*/
716 const MachOFormat::RelocationEntry *
717 MachOObjectFile::getRelocation(DataRefImpl Rel) const {
719 if (MachOObj->is64Bit()) {
720 const MachOFormat::Section64 *Sect = getSection64(Sections[Rel.d.b]);
721 relOffset = Sect->RelocationTableOffset;
723 const MachOFormat::Section *Sect = getSection(Sections[Rel.d.b]);
724 relOffset = Sect->RelocationTableOffset;
726 uint64_t Offset = relOffset + Rel.d.a * sizeof(MachOFormat::RelocationEntry);
728 MachOObj->getData(Offset, sizeof(MachOFormat::RelocationEntry));
729 return reinterpret_cast<const MachOFormat::RelocationEntry*>(Data.data());
732 error_code MachOObjectFile::getRelocationNext(DataRefImpl Rel,
733 RelocationRef &Res) const {
735 Res = RelocationRef(Rel, this);
736 return object_error::success;
738 error_code MachOObjectFile::getRelocationAddress(DataRefImpl Rel,
739 uint64_t &Res) const {
740 const uint8_t* sectAddress = 0;
741 if (MachOObj->is64Bit()) {
742 const MachOFormat::Section64 *Sect = getSection64(Sections[Rel.d.b]);
743 sectAddress += Sect->Address;
745 const MachOFormat::Section *Sect = getSection(Sections[Rel.d.b]);
746 sectAddress += Sect->Address;
748 const MachOFormat::RelocationEntry *RE = getRelocation(Rel);
750 unsigned Arch = getArch();
751 bool isScattered = (Arch != Triple::x86_64) &&
752 (RE->Word0 & macho::RF_Scattered);
753 uint64_t RelAddr = 0;
755 RelAddr = RE->Word0 & 0xFFFFFF;
759 Res = reinterpret_cast<uintptr_t>(sectAddress + RelAddr);
760 return object_error::success;
762 error_code MachOObjectFile::getRelocationOffset(DataRefImpl Rel,
763 uint64_t &Res) const {
764 const MachOFormat::RelocationEntry *RE = getRelocation(Rel);
766 unsigned Arch = getArch();
767 bool isScattered = (Arch != Triple::x86_64) &&
768 (RE->Word0 & macho::RF_Scattered);
770 Res = RE->Word0 & 0xFFFFFF;
773 return object_error::success;
775 error_code MachOObjectFile::getRelocationSymbol(DataRefImpl Rel,
776 SymbolRef &Res) const {
777 const MachOFormat::RelocationEntry *RE = getRelocation(Rel);
778 uint32_t SymbolIdx = RE->Word1 & 0xffffff;
779 bool isExtern = (RE->Word1 >> 27) & 1;
782 moveToNextSymbol(Sym);
784 for (unsigned i = 0; i < SymbolIdx; i++) {
786 moveToNextSymbol(Sym);
787 assert(Sym.d.a < MachOObj->getHeader().NumLoadCommands &&
788 "Relocation symbol index out of range!");
791 Res = SymbolRef(Sym, this);
792 return object_error::success;
794 error_code MachOObjectFile::getRelocationType(DataRefImpl Rel,
795 uint64_t &Res) const {
796 const MachOFormat::RelocationEntry *RE = getRelocation(Rel);
800 return object_error::success;
802 error_code MachOObjectFile::getRelocationTypeName(DataRefImpl Rel,
803 SmallVectorImpl<char> &Result) const {
804 // TODO: Support scattered relocations.
806 const MachOFormat::RelocationEntry *RE = getRelocation(Rel);
808 unsigned Arch = getArch();
809 bool isScattered = (Arch != Triple::x86_64) &&
810 (RE->Word0 & macho::RF_Scattered);
814 r_type = (RE->Word0 >> 24) & 0xF;
816 r_type = (RE->Word1 >> 28) & 0xF;
820 static const char *const Table[] = {
821 "GENERIC_RELOC_VANILLA",
822 "GENERIC_RELOC_PAIR",
823 "GENERIC_RELOC_SECTDIFF",
824 "GENERIC_RELOC_PB_LA_PTR",
825 "GENERIC_RELOC_LOCAL_SECTDIFF",
826 "GENERIC_RELOC_TLV" };
834 case Triple::x86_64: {
835 static const char *const Table[] = {
836 "X86_64_RELOC_UNSIGNED",
837 "X86_64_RELOC_SIGNED",
838 "X86_64_RELOC_BRANCH",
839 "X86_64_RELOC_GOT_LOAD",
841 "X86_64_RELOC_SUBTRACTOR",
842 "X86_64_RELOC_SIGNED_1",
843 "X86_64_RELOC_SIGNED_2",
844 "X86_64_RELOC_SIGNED_4",
845 "X86_64_RELOC_TLV" };
854 static const char *const Table[] = {
857 "ARM_RELOC_SECTDIFF",
858 "ARM_RELOC_LOCAL_SECTDIFF",
859 "ARM_RELOC_PB_LA_PTR",
861 "ARM_THUMB_RELOC_BR22",
862 "ARM_THUMB_32BIT_BRANCH",
864 "ARM_RELOC_HALF_SECTDIFF" };
873 static const char *const Table[] = {
882 "PPC_RELOC_SECTDIFF",
883 "PPC_RELOC_PB_LA_PTR",
884 "PPC_RELOC_HI16_SECTDIFF",
885 "PPC_RELOC_LO16_SECTDIFF",
886 "PPC_RELOC_HA16_SECTDIFF",
888 "PPC_RELOC_LO14_SECTDIFF",
889 "PPC_RELOC_LOCAL_SECTDIFF" };
894 case Triple::UnknownArch:
898 Result.append(res.begin(), res.end());
899 return object_error::success;
901 error_code MachOObjectFile::getRelocationAdditionalInfo(DataRefImpl Rel,
902 int64_t &Res) const {
903 const MachOFormat::RelocationEntry *RE = getRelocation(Rel);
904 bool isExtern = (RE->Word1 >> 27) & 1;
907 const uint8_t* sectAddress = base();
908 if (MachOObj->is64Bit()) {
909 const MachOFormat::Section64 *Sect = getSection64(Sections[Rel.d.b]);
910 sectAddress += Sect->Offset;
912 const MachOFormat::Section *Sect = getSection(Sections[Rel.d.b]);
913 sectAddress += Sect->Offset;
915 Res = reinterpret_cast<uintptr_t>(sectAddress);
917 return object_error::success;
920 // Helper to advance a section or symbol iterator multiple increments at a time.
922 error_code advance(T &it, size_t Val) {
931 void advanceTo(T &it, size_t Val) {
932 if (error_code ec = advance(it, Val))
933 report_fatal_error(ec.message());
936 void MachOObjectFile::printRelocationTargetName(
937 const MachOFormat::RelocationEntry *RE,
938 raw_string_ostream &fmt) const {
939 unsigned Arch = getArch();
940 bool isScattered = (Arch != Triple::x86_64) &&
941 (RE->Word0 & macho::RF_Scattered);
943 // Target of a scattered relocation is an address. In the interest of
944 // generating pretty output, scan through the symbol table looking for a
945 // symbol that aligns with that address. If we find one, print it.
946 // Otherwise, we just print the hex address of the target.
948 uint32_t Val = RE->Word1;
951 for (symbol_iterator SI = begin_symbols(), SE = end_symbols(); SI != SE;
953 if (ec) report_fatal_error(ec.message());
958 if ((ec = SI->getAddress(Addr)))
959 report_fatal_error(ec.message());
960 if (Addr != Val) continue;
961 if ((ec = SI->getName(Name)))
962 report_fatal_error(ec.message());
967 // If we couldn't find a symbol that this relocation refers to, try
968 // to find a section beginning instead.
969 for (section_iterator SI = begin_sections(), SE = end_sections(); SI != SE;
971 if (ec) report_fatal_error(ec.message());
976 if ((ec = SI->getAddress(Addr)))
977 report_fatal_error(ec.message());
978 if (Addr != Val) continue;
979 if ((ec = SI->getName(Name)))
980 report_fatal_error(ec.message());
985 fmt << format("0x%x", Val);
990 bool isExtern = (RE->Word1 >> 27) & 1;
991 uint32_t Val = RE->Word1 & 0xFFFFFF;
994 symbol_iterator SI = begin_symbols();
998 section_iterator SI = begin_sections();
1006 error_code MachOObjectFile::getRelocationValueString(DataRefImpl Rel,
1007 SmallVectorImpl<char> &Result) const {
1008 const MachOFormat::RelocationEntry *RE = getRelocation(Rel);
1010 unsigned Arch = getArch();
1011 bool isScattered = (Arch != Triple::x86_64) &&
1012 (RE->Word0 & macho::RF_Scattered);
1015 raw_string_ostream fmt(fmtbuf);
1019 Type = (RE->Word0 >> 24) & 0xF;
1021 Type = (RE->Word1 >> 28) & 0xF;
1025 isPCRel = ((RE->Word0 >> 30) & 1);
1027 isPCRel = ((RE->Word1 >> 24) & 1);
1029 // Determine any addends that should be displayed with the relocation.
1030 // These require decoding the relocation type, which is triple-specific.
1032 // X86_64 has entirely custom relocation types.
1033 if (Arch == Triple::x86_64) {
1034 bool isPCRel = ((RE->Word1 >> 24) & 1);
1037 case macho::RIT_X86_64_GOTLoad: // X86_64_RELOC_GOT_LOAD
1038 case macho::RIT_X86_64_GOT: { // X86_64_RELOC_GOT
1039 printRelocationTargetName(RE, fmt);
1041 if (isPCRel) fmt << "PCREL";
1044 case macho::RIT_X86_64_Subtractor: { // X86_64_RELOC_SUBTRACTOR
1045 DataRefImpl RelNext = Rel;
1047 const MachOFormat::RelocationEntry *RENext = getRelocation(RelNext);
1049 // X86_64_SUBTRACTOR must be followed by a relocation of type
1050 // X86_64_RELOC_UNSIGNED.
1051 // NOTE: Scattered relocations don't exist on x86_64.
1052 unsigned RType = (RENext->Word1 >> 28) & 0xF;
1054 report_fatal_error("Expected X86_64_RELOC_UNSIGNED after "
1055 "X86_64_RELOC_SUBTRACTOR.");
1057 // The X86_64_RELOC_UNSIGNED contains the minuend symbol,
1058 // X86_64_SUBTRACTOR contains to the subtrahend.
1059 printRelocationTargetName(RENext, fmt);
1061 printRelocationTargetName(RE, fmt);
1064 case macho::RIT_X86_64_TLV:
1065 printRelocationTargetName(RE, fmt);
1067 if (isPCRel) fmt << "P";
1069 case macho::RIT_X86_64_Signed1: // X86_64_RELOC_SIGNED1
1070 printRelocationTargetName(RE, fmt);
1073 case macho::RIT_X86_64_Signed2: // X86_64_RELOC_SIGNED2
1074 printRelocationTargetName(RE, fmt);
1077 case macho::RIT_X86_64_Signed4: // X86_64_RELOC_SIGNED4
1078 printRelocationTargetName(RE, fmt);
1082 printRelocationTargetName(RE, fmt);
1085 // X86 and ARM share some relocation types in common.
1086 } else if (Arch == Triple::x86 || Arch == Triple::arm) {
1087 // Generic relocation types...
1089 case macho::RIT_Pair: // GENERIC_RELOC_PAIR - prints no info
1090 return object_error::success;
1091 case macho::RIT_Difference: { // GENERIC_RELOC_SECTDIFF
1092 DataRefImpl RelNext = Rel;
1094 const MachOFormat::RelocationEntry *RENext = getRelocation(RelNext);
1096 // X86 sect diff's must be followed by a relocation of type
1097 // GENERIC_RELOC_PAIR.
1098 bool isNextScattered = (Arch != Triple::x86_64) &&
1099 (RENext->Word0 & macho::RF_Scattered);
1101 if (isNextScattered)
1102 RType = (RENext->Word0 >> 24) & 0xF;
1104 RType = (RENext->Word1 >> 28) & 0xF;
1106 report_fatal_error("Expected GENERIC_RELOC_PAIR after "
1107 "GENERIC_RELOC_SECTDIFF.");
1109 printRelocationTargetName(RE, fmt);
1111 printRelocationTargetName(RENext, fmt);
1116 if (Arch == Triple::x86) {
1117 // All X86 relocations that need special printing were already
1118 // handled in the generic code.
1120 case macho::RIT_Generic_LocalDifference:{// GENERIC_RELOC_LOCAL_SECTDIFF
1121 DataRefImpl RelNext = Rel;
1123 const MachOFormat::RelocationEntry *RENext = getRelocation(RelNext);
1125 // X86 sect diff's must be followed by a relocation of type
1126 // GENERIC_RELOC_PAIR.
1127 bool isNextScattered = (Arch != Triple::x86_64) &&
1128 (RENext->Word0 & macho::RF_Scattered);
1130 if (isNextScattered)
1131 RType = (RENext->Word0 >> 24) & 0xF;
1133 RType = (RENext->Word1 >> 28) & 0xF;
1135 report_fatal_error("Expected GENERIC_RELOC_PAIR after "
1136 "GENERIC_RELOC_LOCAL_SECTDIFF.");
1138 printRelocationTargetName(RE, fmt);
1140 printRelocationTargetName(RENext, fmt);
1143 case macho::RIT_Generic_TLV: {
1144 printRelocationTargetName(RE, fmt);
1146 if (isPCRel) fmt << "P";
1150 printRelocationTargetName(RE, fmt);
1152 } else { // ARM-specific relocations
1154 case macho::RIT_ARM_Half: // ARM_RELOC_HALF
1155 case macho::RIT_ARM_HalfDifference: { // ARM_RELOC_HALF_SECTDIFF
1156 // Half relocations steal a bit from the length field to encode
1157 // whether this is an upper16 or a lower16 relocation.
1160 isUpper = (RE->Word0 >> 28) & 1;
1162 isUpper = (RE->Word1 >> 25) & 1;
1165 fmt << ":upper16:(";
1167 fmt << ":lower16:(";
1168 printRelocationTargetName(RE, fmt);
1170 DataRefImpl RelNext = Rel;
1172 const MachOFormat::RelocationEntry *RENext = getRelocation(RelNext);
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 const MachOFormat::RelocationEntry *RE = getRelocation(Rel);
1220 unsigned Arch = getArch();
1221 bool isScattered = (Arch != Triple::x86_64) &&
1222 (RE->Word0 & macho::RF_Scattered);
1225 Type = (RE->Word0 >> 24) & 0xF;
1227 Type = (RE->Word1 >> 28) & 0xF;
1231 // On arches that use the generic relocations, GENERIC_RELOC_PAIR
1232 // is always hidden.
1233 if (Arch == Triple::x86 || Arch == Triple::arm) {
1234 if (Type == macho::RIT_Pair) Result = true;
1235 } else if (Arch == Triple::x86_64) {
1236 // On x86_64, X86_64_RELOC_UNSIGNED is hidden only when it follows
1237 // an X864_64_RELOC_SUBTRACTOR.
1238 if (Type == macho::RIT_X86_64_Unsigned && Rel.d.a > 0) {
1239 DataRefImpl RelPrev = Rel;
1241 const MachOFormat::RelocationEntry *REPrev = getRelocation(RelPrev);
1243 unsigned PrevType = (REPrev->Word1 >> 28) & 0xF;
1245 if (PrevType == macho::RIT_X86_64_Subtractor) Result = true;
1249 return object_error::success;
1252 error_code MachOObjectFile::getLibraryNext(DataRefImpl LibData,
1253 LibraryRef &Res) const {
1254 report_fatal_error("Needed libraries unimplemented in MachOObjectFile");
1257 error_code MachOObjectFile::getLibraryPath(DataRefImpl LibData,
1258 StringRef &Res) const {
1259 report_fatal_error("Needed libraries unimplemented in MachOObjectFile");
1263 /*===-- Miscellaneous -----------------------------------------------------===*/
1265 uint8_t MachOObjectFile::getBytesInAddress() const {
1266 return MachOObj->is64Bit() ? 8 : 4;
1269 StringRef MachOObjectFile::getFileFormatName() const {
1270 if (!MachOObj->is64Bit()) {
1271 switch (MachOObj->getHeader().CPUType) {
1272 case llvm::MachO::CPUTypeI386:
1273 return "Mach-O 32-bit i386";
1274 case llvm::MachO::CPUTypeARM:
1275 return "Mach-O arm";
1276 case llvm::MachO::CPUTypePowerPC:
1277 return "Mach-O 32-bit ppc";
1279 assert((MachOObj->getHeader().CPUType & llvm::MachO::CPUArchABI64) == 0 &&
1280 "64-bit object file when we're not 64-bit?");
1281 return "Mach-O 32-bit unknown";
1285 // Make sure the cpu type has the correct mask.
1286 assert((MachOObj->getHeader().CPUType & llvm::MachO::CPUArchABI64)
1287 == llvm::MachO::CPUArchABI64 &&
1288 "32-bit object file when we're 64-bit?");
1290 switch (MachOObj->getHeader().CPUType) {
1291 case llvm::MachO::CPUTypeX86_64:
1292 return "Mach-O 64-bit x86-64";
1293 case llvm::MachO::CPUTypePowerPC64:
1294 return "Mach-O 64-bit ppc64";
1296 return "Mach-O 64-bit unknown";
1300 unsigned MachOObjectFile::getArch() const {
1301 switch (MachOObj->getHeader().CPUType) {
1302 case llvm::MachO::CPUTypeI386:
1304 case llvm::MachO::CPUTypeX86_64:
1305 return Triple::x86_64;
1306 case llvm::MachO::CPUTypeARM:
1308 case llvm::MachO::CPUTypePowerPC:
1310 case llvm::MachO::CPUTypePowerPC64:
1311 return Triple::ppc64;
1313 return Triple::UnknownArch;
1317 } // end namespace object
1318 } // end namespace llvm