1 //===- MachOObjectFile.cpp - Mach-O object file binding ---------*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This file defines the MachOObjectFile class, which binds the MachOObject
11 // class to the generic ObjectFile wrapper.
13 //===----------------------------------------------------------------------===//
15 #include "llvm/Object/MachO.h"
16 #include "llvm/ADT/Triple.h"
17 #include "llvm/Object/MachOFormat.h"
18 #include "llvm/Support/Format.h"
19 #include "llvm/Support/MemoryBuffer.h"
25 using namespace object;
30 MachOObjectFile::MachOObjectFile(MemoryBuffer *Object, MachOObject *MOO,
32 : ObjectFile(Binary::ID_MachO, Object, ec),
34 RegisteredStringTable(std::numeric_limits<uint32_t>::max()) {
36 moveToNextSection(DRI);
37 uint32_t LoadCommandCount = MachOObj->getHeader().NumLoadCommands;
38 while (DRI.d.a < LoadCommandCount) {
39 Sections.push_back(DRI);
41 moveToNextSection(DRI);
46 ObjectFile *ObjectFile::createMachOObjectFile(MemoryBuffer *Buffer) {
49 MachOObject *MachOObj = MachOObject::LoadFromBuffer(Buffer, &Err);
52 // MachOObject takes ownership of the Buffer we passed to it, and
53 // MachOObjectFile does, too, so we need to make sure they don't get the
54 // same object. A MemoryBuffer is cheap (it's just a reference to memory,
55 // not a copy of the memory itself), so just make a new copy here for
56 // the MachOObjectFile.
57 MemoryBuffer *NewBuffer =
58 MemoryBuffer::getMemBuffer(Buffer->getBuffer(),
59 Buffer->getBufferIdentifier(), false);
60 return new MachOObjectFile(NewBuffer, MachOObj, ec);
63 /*===-- Symbols -----------------------------------------------------------===*/
65 void MachOObjectFile::moveToNextSymbol(DataRefImpl &DRI) const {
66 uint32_t LoadCommandCount = MachOObj->getHeader().NumLoadCommands;
67 while (DRI.d.a < LoadCommandCount) {
68 LoadCommandInfo LCI = MachOObj->getLoadCommandInfo(DRI.d.a);
69 if (LCI.Command.Type == macho::LCT_Symtab) {
70 InMemoryStruct<macho::SymtabLoadCommand> SymtabLoadCmd;
71 MachOObj->ReadSymtabLoadCommand(LCI, SymtabLoadCmd);
72 if (DRI.d.b < SymtabLoadCmd->NumSymbolTableEntries)
81 void MachOObjectFile::getSymbolTableEntry(DataRefImpl DRI,
82 InMemoryStruct<macho::SymbolTableEntry> &Res) const {
83 InMemoryStruct<macho::SymtabLoadCommand> SymtabLoadCmd;
84 LoadCommandInfo LCI = MachOObj->getLoadCommandInfo(DRI.d.a);
85 MachOObj->ReadSymtabLoadCommand(LCI, SymtabLoadCmd);
87 if (RegisteredStringTable != DRI.d.a) {
88 MachOObj->RegisterStringTable(*SymtabLoadCmd);
89 RegisteredStringTable = DRI.d.a;
92 MachOObj->ReadSymbolTableEntry(SymtabLoadCmd->SymbolTableOffset, DRI.d.b,
96 void MachOObjectFile::getSymbol64TableEntry(DataRefImpl DRI,
97 InMemoryStruct<macho::Symbol64TableEntry> &Res) const {
98 InMemoryStruct<macho::SymtabLoadCommand> SymtabLoadCmd;
99 LoadCommandInfo LCI = MachOObj->getLoadCommandInfo(DRI.d.a);
100 MachOObj->ReadSymtabLoadCommand(LCI, SymtabLoadCmd);
102 if (RegisteredStringTable != DRI.d.a) {
103 MachOObj->RegisterStringTable(*SymtabLoadCmd);
104 RegisteredStringTable = DRI.d.a;
107 MachOObj->ReadSymbol64TableEntry(SymtabLoadCmd->SymbolTableOffset, DRI.d.b,
112 error_code MachOObjectFile::getSymbolNext(DataRefImpl DRI,
113 SymbolRef &Result) const {
115 moveToNextSymbol(DRI);
116 Result = SymbolRef(DRI, this);
117 return object_error::success;
120 error_code MachOObjectFile::getSymbolName(DataRefImpl DRI,
121 StringRef &Result) const {
122 if (MachOObj->is64Bit()) {
123 InMemoryStruct<macho::Symbol64TableEntry> Entry;
124 getSymbol64TableEntry(DRI, Entry);
125 Result = MachOObj->getStringAtIndex(Entry->StringIndex);
127 InMemoryStruct<macho::SymbolTableEntry> Entry;
128 getSymbolTableEntry(DRI, Entry);
129 Result = MachOObj->getStringAtIndex(Entry->StringIndex);
131 return object_error::success;
134 error_code MachOObjectFile::getSymbolFileOffset(DataRefImpl DRI,
135 uint64_t &Result) const {
136 if (MachOObj->is64Bit()) {
137 InMemoryStruct<macho::Symbol64TableEntry> Entry;
138 getSymbol64TableEntry(DRI, Entry);
139 Result = Entry->Value;
140 if (Entry->SectionIndex) {
141 InMemoryStruct<macho::Section64> Section;
142 getSection64(Sections[Entry->SectionIndex-1], Section);
143 Result += Section->Offset - Section->Address;
146 InMemoryStruct<macho::SymbolTableEntry> Entry;
147 getSymbolTableEntry(DRI, Entry);
148 Result = Entry->Value;
149 if (Entry->SectionIndex) {
150 InMemoryStruct<macho::Section> Section;
151 getSection(Sections[Entry->SectionIndex-1], Section);
152 Result += Section->Offset - Section->Address;
156 return object_error::success;
159 error_code MachOObjectFile::getSymbolAddress(DataRefImpl DRI,
160 uint64_t &Result) const {
161 if (MachOObj->is64Bit()) {
162 InMemoryStruct<macho::Symbol64TableEntry> Entry;
163 getSymbol64TableEntry(DRI, Entry);
164 Result = Entry->Value;
166 InMemoryStruct<macho::SymbolTableEntry> Entry;
167 getSymbolTableEntry(DRI, Entry);
168 Result = Entry->Value;
170 return object_error::success;
173 error_code MachOObjectFile::getSymbolSize(DataRefImpl DRI,
174 uint64_t &Result) const {
175 uint32_t LoadCommandCount = MachOObj->getHeader().NumLoadCommands;
176 uint64_t BeginOffset;
177 uint64_t EndOffset = 0;
178 uint8_t SectionIndex;
179 if (MachOObj->is64Bit()) {
180 InMemoryStruct<macho::Symbol64TableEntry> Entry;
181 getSymbol64TableEntry(DRI, Entry);
182 BeginOffset = Entry->Value;
183 SectionIndex = Entry->SectionIndex;
185 uint32_t flags = SymbolRef::SF_None;
186 getSymbolFlags(DRI, flags);
187 if (flags & SymbolRef::SF_Common)
188 Result = Entry->Value;
190 Result = UnknownAddressOrSize;
191 return object_error::success;
193 // Unfortunately symbols are unsorted so we need to touch all
194 // symbols from load command
196 uint32_t Command = DRI.d.a;
197 while (Command == DRI.d.a) {
198 moveToNextSymbol(DRI);
199 if (DRI.d.a < LoadCommandCount) {
200 getSymbol64TableEntry(DRI, Entry);
201 if (Entry->SectionIndex == SectionIndex && Entry->Value > BeginOffset)
202 if (!EndOffset || Entry->Value < EndOffset)
203 EndOffset = Entry->Value;
208 InMemoryStruct<macho::SymbolTableEntry> Entry;
209 getSymbolTableEntry(DRI, Entry);
210 BeginOffset = Entry->Value;
211 SectionIndex = Entry->SectionIndex;
213 uint32_t flags = SymbolRef::SF_None;
214 getSymbolFlags(DRI, flags);
215 if (flags & SymbolRef::SF_Common)
216 Result = Entry->Value;
218 Result = UnknownAddressOrSize;
219 return object_error::success;
221 // Unfortunately symbols are unsorted so we need to touch all
222 // symbols from load command
224 uint32_t Command = DRI.d.a;
225 while (Command == DRI.d.a) {
226 moveToNextSymbol(DRI);
227 if (DRI.d.a < LoadCommandCount) {
228 getSymbolTableEntry(DRI, Entry);
229 if (Entry->SectionIndex == SectionIndex && Entry->Value > BeginOffset)
230 if (!EndOffset || Entry->Value < EndOffset)
231 EndOffset = Entry->Value;
238 getSectionSize(Sections[SectionIndex-1], Size);
239 getSectionAddress(Sections[SectionIndex-1], EndOffset);
242 Result = EndOffset - BeginOffset;
243 return object_error::success;
246 error_code MachOObjectFile::getSymbolNMTypeChar(DataRefImpl DRI,
247 char &Result) const {
249 if (MachOObj->is64Bit()) {
250 InMemoryStruct<macho::Symbol64TableEntry> Entry;
251 getSymbol64TableEntry(DRI, Entry);
253 Flags = Entry->Flags;
255 InMemoryStruct<macho::SymbolTableEntry> Entry;
256 getSymbolTableEntry(DRI, Entry);
258 Flags = Entry->Flags;
262 switch (Type & macho::STF_TypeMask) {
263 case macho::STT_Undefined:
266 case macho::STT_Absolute:
267 case macho::STT_Section:
275 if (Flags & (macho::STF_External | macho::STF_PrivateExtern))
276 Char = toupper(Char);
278 return object_error::success;
281 error_code MachOObjectFile::getSymbolFlags(DataRefImpl DRI,
282 uint32_t &Result) const {
285 if (MachOObj->is64Bit()) {
286 InMemoryStruct<macho::Symbol64TableEntry> Entry;
287 getSymbol64TableEntry(DRI, Entry);
288 MachOFlags = Entry->Flags;
289 MachOType = Entry->Type;
291 InMemoryStruct<macho::SymbolTableEntry> Entry;
292 getSymbolTableEntry(DRI, Entry);
293 MachOFlags = Entry->Flags;
294 MachOType = Entry->Type;
297 // TODO: Correctly set SF_ThreadLocal
298 Result = SymbolRef::SF_None;
300 if ((MachOType & MachO::NlistMaskType) == MachO::NListTypeUndefined)
301 Result |= SymbolRef::SF_Undefined;
303 if (MachOFlags & macho::STF_StabsEntryMask)
304 Result |= SymbolRef::SF_FormatSpecific;
306 if (MachOType & MachO::NlistMaskExternal) {
307 Result |= SymbolRef::SF_Global;
308 if ((MachOType & MachO::NlistMaskType) == MachO::NListTypeUndefined)
309 Result |= SymbolRef::SF_Common;
312 if (MachOFlags & (MachO::NListDescWeakRef | MachO::NListDescWeakDef))
313 Result |= SymbolRef::SF_Weak;
315 if ((MachOType & MachO::NlistMaskType) == MachO::NListTypeAbsolute)
316 Result |= SymbolRef::SF_Absolute;
318 return object_error::success;
321 error_code MachOObjectFile::getSymbolSection(DataRefImpl Symb,
322 section_iterator &Res) const {
324 if (MachOObj->is64Bit()) {
325 InMemoryStruct<macho::Symbol64TableEntry> Entry;
326 getSymbol64TableEntry(Symb, Entry);
327 index = Entry->SectionIndex;
329 InMemoryStruct<macho::SymbolTableEntry> Entry;
330 getSymbolTableEntry(Symb, Entry);
331 index = Entry->SectionIndex;
335 Res = end_sections();
337 Res = section_iterator(SectionRef(Sections[index-1], this));
339 return object_error::success;
342 error_code MachOObjectFile::getSymbolType(DataRefImpl Symb,
343 SymbolRef::Type &Res) const {
345 if (MachOObj->is64Bit()) {
346 InMemoryStruct<macho::Symbol64TableEntry> Entry;
347 getSymbol64TableEntry(Symb, Entry);
348 n_type = Entry->Type;
350 InMemoryStruct<macho::SymbolTableEntry> Entry;
351 getSymbolTableEntry(Symb, Entry);
352 n_type = Entry->Type;
354 Res = SymbolRef::ST_Other;
356 // If this is a STAB debugging symbol, we can do nothing more.
357 if (n_type & MachO::NlistMaskStab) {
358 Res = SymbolRef::ST_Debug;
359 return object_error::success;
362 switch (n_type & MachO::NlistMaskType) {
363 case MachO::NListTypeUndefined :
364 Res = SymbolRef::ST_Unknown;
366 case MachO::NListTypeSection :
367 Res = SymbolRef::ST_Function;
370 return object_error::success;
373 error_code MachOObjectFile::getSymbolValue(DataRefImpl Symb,
374 uint64_t &Val) const {
375 report_fatal_error("getSymbolValue unimplemented in MachOObjectFile");
378 symbol_iterator MachOObjectFile::begin_symbols() const {
379 // DRI.d.a = segment number; DRI.d.b = symbol index.
381 moveToNextSymbol(DRI);
382 return symbol_iterator(SymbolRef(DRI, this));
385 symbol_iterator MachOObjectFile::end_symbols() const {
387 DRI.d.a = MachOObj->getHeader().NumLoadCommands;
388 return symbol_iterator(SymbolRef(DRI, this));
391 symbol_iterator MachOObjectFile::begin_dynamic_symbols() const {
393 report_fatal_error("Dynamic symbols unimplemented in MachOObjectFile");
396 symbol_iterator MachOObjectFile::end_dynamic_symbols() const {
398 report_fatal_error("Dynamic symbols unimplemented in MachOObjectFile");
401 library_iterator MachOObjectFile::begin_libraries_needed() const {
403 report_fatal_error("Needed libraries unimplemented in MachOObjectFile");
406 library_iterator MachOObjectFile::end_libraries_needed() const {
408 report_fatal_error("Needed libraries unimplemented in MachOObjectFile");
411 StringRef MachOObjectFile::getLoadName() const {
413 report_fatal_error("get_load_name() unimplemented in MachOObjectFile");
416 /*===-- Sections ----------------------------------------------------------===*/
418 void MachOObjectFile::moveToNextSection(DataRefImpl &DRI) const {
419 uint32_t LoadCommandCount = MachOObj->getHeader().NumLoadCommands;
420 while (DRI.d.a < LoadCommandCount) {
421 LoadCommandInfo LCI = MachOObj->getLoadCommandInfo(DRI.d.a);
422 if (LCI.Command.Type == macho::LCT_Segment) {
423 InMemoryStruct<macho::SegmentLoadCommand> SegmentLoadCmd;
424 MachOObj->ReadSegmentLoadCommand(LCI, SegmentLoadCmd);
425 if (DRI.d.b < SegmentLoadCmd->NumSections)
427 } else if (LCI.Command.Type == macho::LCT_Segment64) {
428 InMemoryStruct<macho::Segment64LoadCommand> Segment64LoadCmd;
429 MachOObj->ReadSegment64LoadCommand(LCI, Segment64LoadCmd);
430 if (DRI.d.b < Segment64LoadCmd->NumSections)
439 error_code MachOObjectFile::getSectionNext(DataRefImpl DRI,
440 SectionRef &Result) const {
442 moveToNextSection(DRI);
443 Result = SectionRef(DRI, this);
444 return object_error::success;
448 MachOObjectFile::getSection(DataRefImpl DRI,
449 InMemoryStruct<macho::Section> &Res) const {
450 LoadCommandInfo LCI = MachOObj->getLoadCommandInfo(DRI.d.a);
451 MachOObj->ReadSection(LCI, DRI.d.b, Res);
454 std::size_t MachOObjectFile::getSectionIndex(DataRefImpl Sec) const {
455 SectionList::const_iterator loc =
456 std::find(Sections.begin(), Sections.end(), Sec);
457 assert(loc != Sections.end() && "Sec is not a valid section!");
458 return std::distance(Sections.begin(), loc);
462 MachOObjectFile::getSection64(DataRefImpl DRI,
463 InMemoryStruct<macho::Section64> &Res) const {
464 LoadCommandInfo LCI = MachOObj->getLoadCommandInfo(DRI.d.a);
465 MachOObj->ReadSection64(LCI, DRI.d.b, Res);
468 static bool is64BitLoadCommand(const MachOObject *MachOObj, DataRefImpl DRI) {
469 LoadCommandInfo LCI = MachOObj->getLoadCommandInfo(DRI.d.a);
470 if (LCI.Command.Type == macho::LCT_Segment64)
472 assert(LCI.Command.Type == macho::LCT_Segment && "Unexpected Type.");
476 static StringRef parseSegmentOrSectionName(const char *P) {
480 // Not null terminated, so this is a 16 char string.
481 return StringRef(P, 16);
484 error_code MachOObjectFile::getSectionName(DataRefImpl DRI,
485 StringRef &Result) const {
486 if (is64BitLoadCommand(MachOObj.get(), DRI)) {
487 LoadCommandInfo LCI = MachOObj->getLoadCommandInfo(DRI.d.a);
488 unsigned SectionOffset = LCI.Offset + sizeof(macho::Segment64LoadCommand) +
489 DRI.d.b * sizeof(macho::Section64);
490 StringRef Data = MachOObj->getData(SectionOffset, sizeof(macho::Section64));
491 const macho::Section64 *sec =
492 reinterpret_cast<const macho::Section64*>(Data.data());
493 Result = parseSegmentOrSectionName(sec->Name);
495 LoadCommandInfo LCI = MachOObj->getLoadCommandInfo(DRI.d.a);
496 unsigned SectionOffset = LCI.Offset + sizeof(macho::SegmentLoadCommand) +
497 DRI.d.b * sizeof(macho::Section);
498 StringRef Data = MachOObj->getData(SectionOffset, sizeof(macho::Section));
499 const macho::Section *sec =
500 reinterpret_cast<const macho::Section*>(Data.data());
501 Result = parseSegmentOrSectionName(sec->Name);
503 return object_error::success;
506 error_code MachOObjectFile::getSectionFinalSegmentName(DataRefImpl Sec,
507 StringRef &Res) const {
508 if (is64BitLoadCommand(MachOObj.get(), Sec)) {
509 LoadCommandInfo LCI = MachOObj->getLoadCommandInfo(Sec.d.a);
510 unsigned SectionOffset = LCI.Offset + sizeof(macho::Segment64LoadCommand) +
511 Sec.d.b * sizeof(macho::Section64);
512 StringRef Data = MachOObj->getData(SectionOffset, sizeof(macho::Section64));
513 const macho::Section64 *sec =
514 reinterpret_cast<const macho::Section64*>(Data.data());
515 Res = parseSegmentOrSectionName(sec->SegmentName);
517 LoadCommandInfo LCI = MachOObj->getLoadCommandInfo(Sec.d.a);
518 unsigned SectionOffset = LCI.Offset + sizeof(macho::SegmentLoadCommand) +
519 Sec.d.b * sizeof(macho::Section);
520 StringRef Data = MachOObj->getData(SectionOffset, sizeof(macho::Section));
521 const macho::Section *sec =
522 reinterpret_cast<const macho::Section*>(Data.data());
523 Res = parseSegmentOrSectionName(sec->SegmentName);
525 return object_error::success;
528 error_code MachOObjectFile::getSectionAddress(DataRefImpl DRI,
529 uint64_t &Result) const {
530 if (is64BitLoadCommand(MachOObj.get(), DRI)) {
531 InMemoryStruct<macho::Section64> Sect;
532 getSection64(DRI, Sect);
533 Result = Sect->Address;
535 InMemoryStruct<macho::Section> Sect;
536 getSection(DRI, Sect);
537 Result = Sect->Address;
539 return object_error::success;
542 error_code MachOObjectFile::getSectionSize(DataRefImpl DRI,
543 uint64_t &Result) const {
544 if (is64BitLoadCommand(MachOObj.get(), DRI)) {
545 InMemoryStruct<macho::Section64> Sect;
546 getSection64(DRI, Sect);
549 InMemoryStruct<macho::Section> Sect;
550 getSection(DRI, Sect);
553 return object_error::success;
556 error_code MachOObjectFile::getSectionContents(DataRefImpl DRI,
557 StringRef &Result) const {
558 if (is64BitLoadCommand(MachOObj.get(), DRI)) {
559 InMemoryStruct<macho::Section64> Sect;
560 getSection64(DRI, Sect);
561 Result = MachOObj->getData(Sect->Offset, Sect->Size);
563 InMemoryStruct<macho::Section> Sect;
564 getSection(DRI, Sect);
565 Result = MachOObj->getData(Sect->Offset, Sect->Size);
567 return object_error::success;
570 error_code MachOObjectFile::getSectionAlignment(DataRefImpl DRI,
571 uint64_t &Result) const {
572 if (is64BitLoadCommand(MachOObj.get(), DRI)) {
573 InMemoryStruct<macho::Section64> Sect;
574 getSection64(DRI, Sect);
575 Result = uint64_t(1) << Sect->Align;
577 InMemoryStruct<macho::Section> Sect;
578 getSection(DRI, Sect);
579 Result = uint64_t(1) << Sect->Align;
581 return object_error::success;
584 error_code MachOObjectFile::isSectionText(DataRefImpl DRI,
585 bool &Result) const {
586 if (is64BitLoadCommand(MachOObj.get(), DRI)) {
587 InMemoryStruct<macho::Section64> Sect;
588 getSection64(DRI, Sect);
589 Result = Sect->Flags & macho::SF_PureInstructions;
591 InMemoryStruct<macho::Section> Sect;
592 getSection(DRI, Sect);
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 InMemoryStruct<macho::Section64> Sect;
630 getSection64(DRI, Sect);
631 unsigned SectionType = Sect->Flags & MachO::SectionFlagMaskSectionType;
632 Result = (SectionType == MachO::SectionTypeZeroFill ||
633 SectionType == MachO::SectionTypeZeroFillLarge);
635 InMemoryStruct<macho::Section> Sect;
636 getSection(DRI, Sect);
637 unsigned SectionType = Sect->Flags & MachO::SectionFlagMaskSectionType;
638 Result = (SectionType == MachO::SectionTypeZeroFill ||
639 SectionType == MachO::SectionTypeZeroFillLarge);
642 return object_error::success;
645 error_code MachOObjectFile::isSectionReadOnlyData(DataRefImpl Sec,
646 bool &Result) const {
647 // Consider using the code from isSectionText to look for __const sections.
648 // Alternately, emit S_ATTR_PURE_INSTRUCTIONS and/or S_ATTR_SOME_INSTRUCTIONS
649 // to use section attributes to distinguish code from data.
651 // FIXME: Unimplemented.
653 return object_error::success;
656 error_code MachOObjectFile::sectionContainsSymbol(DataRefImpl Sec,
658 bool &Result) const {
660 getSymbolType(Symb, ST);
661 if (ST == SymbolRef::ST_Unknown) {
663 return object_error::success;
666 uint64_t SectBegin, SectEnd;
667 getSectionAddress(Sec, SectBegin);
668 getSectionSize(Sec, SectEnd);
669 SectEnd += SectBegin;
671 if (MachOObj->is64Bit()) {
672 InMemoryStruct<macho::Symbol64TableEntry> Entry;
673 getSymbol64TableEntry(Symb, Entry);
674 uint64_t SymAddr= Entry->Value;
675 Result = (SymAddr >= SectBegin) && (SymAddr < SectEnd);
677 InMemoryStruct<macho::SymbolTableEntry> Entry;
678 getSymbolTableEntry(Symb, Entry);
679 uint64_t SymAddr= Entry->Value;
680 Result = (SymAddr >= SectBegin) && (SymAddr < SectEnd);
683 return object_error::success;
686 relocation_iterator MachOObjectFile::getSectionRelBegin(DataRefImpl Sec) const {
688 ret.d.b = getSectionIndex(Sec);
689 return relocation_iterator(RelocationRef(ret, this));
691 relocation_iterator MachOObjectFile::getSectionRelEnd(DataRefImpl Sec) const {
693 if (is64BitLoadCommand(MachOObj.get(), Sec)) {
694 InMemoryStruct<macho::Section64> Sect;
695 getSection64(Sec, Sect);
696 last_reloc = Sect->NumRelocationTableEntries;
698 InMemoryStruct<macho::Section> Sect;
699 getSection(Sec, Sect);
700 last_reloc = Sect->NumRelocationTableEntries;
703 ret.d.a = last_reloc;
704 ret.d.b = getSectionIndex(Sec);
705 return relocation_iterator(RelocationRef(ret, this));
708 section_iterator MachOObjectFile::begin_sections() const {
710 moveToNextSection(DRI);
711 return section_iterator(SectionRef(DRI, this));
714 section_iterator MachOObjectFile::end_sections() const {
716 DRI.d.a = MachOObj->getHeader().NumLoadCommands;
717 return section_iterator(SectionRef(DRI, this));
720 /*===-- Relocations -------------------------------------------------------===*/
722 void MachOObjectFile::
723 getRelocation(DataRefImpl Rel,
724 InMemoryStruct<macho::RelocationEntry> &Res) const {
726 if (MachOObj->is64Bit()) {
727 InMemoryStruct<macho::Section64> Sect;
728 getSection64(Sections[Rel.d.b], Sect);
729 relOffset = Sect->RelocationTableOffset;
731 InMemoryStruct<macho::Section> Sect;
732 getSection(Sections[Rel.d.b], Sect);
733 relOffset = Sect->RelocationTableOffset;
735 MachOObj->ReadRelocationEntry(relOffset, Rel.d.a, Res);
737 error_code MachOObjectFile::getRelocationNext(DataRefImpl Rel,
738 RelocationRef &Res) const {
740 Res = RelocationRef(Rel, this);
741 return object_error::success;
743 error_code MachOObjectFile::getRelocationAddress(DataRefImpl Rel,
744 uint64_t &Res) const {
745 const uint8_t* sectAddress = 0;
746 if (MachOObj->is64Bit()) {
747 InMemoryStruct<macho::Section64> Sect;
748 getSection64(Sections[Rel.d.b], Sect);
749 sectAddress += Sect->Address;
751 InMemoryStruct<macho::Section> Sect;
752 getSection(Sections[Rel.d.b], Sect);
753 sectAddress += Sect->Address;
755 InMemoryStruct<macho::RelocationEntry> RE;
756 getRelocation(Rel, RE);
758 unsigned Arch = getArch();
759 bool isScattered = (Arch != Triple::x86_64) &&
760 (RE->Word0 & macho::RF_Scattered);
761 uint64_t RelAddr = 0;
763 RelAddr = RE->Word0 & 0xFFFFFF;
767 Res = reinterpret_cast<uintptr_t>(sectAddress + RelAddr);
768 return object_error::success;
770 error_code MachOObjectFile::getRelocationOffset(DataRefImpl Rel,
771 uint64_t &Res) const {
772 InMemoryStruct<macho::RelocationEntry> RE;
773 getRelocation(Rel, RE);
775 unsigned Arch = getArch();
776 bool isScattered = (Arch != Triple::x86_64) &&
777 (RE->Word0 & macho::RF_Scattered);
779 Res = RE->Word0 & 0xFFFFFF;
782 return object_error::success;
784 error_code MachOObjectFile::getRelocationSymbol(DataRefImpl Rel,
785 SymbolRef &Res) const {
786 InMemoryStruct<macho::RelocationEntry> RE;
787 getRelocation(Rel, RE);
788 uint32_t SymbolIdx = RE->Word1 & 0xffffff;
789 bool isExtern = (RE->Word1 >> 27) & 1;
792 moveToNextSymbol(Sym);
794 for (unsigned i = 0; i < SymbolIdx; i++) {
796 moveToNextSymbol(Sym);
797 assert(Sym.d.a < MachOObj->getHeader().NumLoadCommands &&
798 "Relocation symbol index out of range!");
801 Res = SymbolRef(Sym, this);
802 return object_error::success;
804 error_code MachOObjectFile::getRelocationType(DataRefImpl Rel,
805 uint64_t &Res) const {
806 InMemoryStruct<macho::RelocationEntry> RE;
807 getRelocation(Rel, RE);
811 return object_error::success;
813 error_code MachOObjectFile::getRelocationTypeName(DataRefImpl Rel,
814 SmallVectorImpl<char> &Result) const {
815 // TODO: Support scattered relocations.
817 InMemoryStruct<macho::RelocationEntry> RE;
818 getRelocation(Rel, RE);
820 unsigned Arch = getArch();
821 bool isScattered = (Arch != Triple::x86_64) &&
822 (RE->Word0 & macho::RF_Scattered);
826 r_type = (RE->Word0 >> 24) & 0xF;
828 r_type = (RE->Word1 >> 28) & 0xF;
832 static const char *const Table[] = {
833 "GENERIC_RELOC_VANILLA",
834 "GENERIC_RELOC_PAIR",
835 "GENERIC_RELOC_SECTDIFF",
836 "GENERIC_RELOC_PB_LA_PTR",
837 "GENERIC_RELOC_LOCAL_SECTDIFF",
838 "GENERIC_RELOC_TLV" };
846 case Triple::x86_64: {
847 static const char *const Table[] = {
848 "X86_64_RELOC_UNSIGNED",
849 "X86_64_RELOC_SIGNED",
850 "X86_64_RELOC_BRANCH",
851 "X86_64_RELOC_GOT_LOAD",
853 "X86_64_RELOC_SUBTRACTOR",
854 "X86_64_RELOC_SIGNED_1",
855 "X86_64_RELOC_SIGNED_2",
856 "X86_64_RELOC_SIGNED_4",
857 "X86_64_RELOC_TLV" };
866 static const char *const Table[] = {
869 "ARM_RELOC_SECTDIFF",
870 "ARM_RELOC_LOCAL_SECTDIFF",
871 "ARM_RELOC_PB_LA_PTR",
873 "ARM_THUMB_RELOC_BR22",
874 "ARM_THUMB_32BIT_BRANCH",
876 "ARM_RELOC_HALF_SECTDIFF" };
885 static const char *const Table[] = {
894 "PPC_RELOC_SECTDIFF",
895 "PPC_RELOC_PB_LA_PTR",
896 "PPC_RELOC_HI16_SECTDIFF",
897 "PPC_RELOC_LO16_SECTDIFF",
898 "PPC_RELOC_HA16_SECTDIFF",
900 "PPC_RELOC_LO14_SECTDIFF",
901 "PPC_RELOC_LOCAL_SECTDIFF" };
906 case Triple::UnknownArch:
910 Result.append(res.begin(), res.end());
911 return object_error::success;
913 error_code MachOObjectFile::getRelocationAdditionalInfo(DataRefImpl Rel,
914 int64_t &Res) const {
915 InMemoryStruct<macho::RelocationEntry> RE;
916 getRelocation(Rel, RE);
917 bool isExtern = (RE->Word1 >> 27) & 1;
920 const uint8_t* sectAddress = base();
921 if (MachOObj->is64Bit()) {
922 InMemoryStruct<macho::Section64> Sect;
923 getSection64(Sections[Rel.d.b], Sect);
924 sectAddress += Sect->Offset;
926 InMemoryStruct<macho::Section> Sect;
927 getSection(Sections[Rel.d.b], Sect);
928 sectAddress += Sect->Offset;
930 Res = reinterpret_cast<uintptr_t>(sectAddress);
932 return object_error::success;
935 // Helper to advance a section or symbol iterator multiple increments at a time.
937 error_code advance(T &it, size_t Val) {
946 void advanceTo(T &it, size_t Val) {
947 if (error_code ec = advance(it, Val))
948 report_fatal_error(ec.message());
951 void MachOObjectFile::printRelocationTargetName(
952 InMemoryStruct<macho::RelocationEntry>& RE,
953 raw_string_ostream &fmt) const {
954 unsigned Arch = getArch();
955 bool isScattered = (Arch != Triple::x86_64) &&
956 (RE->Word0 & macho::RF_Scattered);
958 // Target of a scattered relocation is an address. In the interest of
959 // generating pretty output, scan through the symbol table looking for a
960 // symbol that aligns with that address. If we find one, print it.
961 // Otherwise, we just print the hex address of the target.
963 uint32_t Val = RE->Word1;
966 for (symbol_iterator SI = begin_symbols(), SE = end_symbols(); SI != SE;
968 if (ec) report_fatal_error(ec.message());
973 if ((ec = SI->getAddress(Addr)))
974 report_fatal_error(ec.message());
975 if (Addr != Val) continue;
976 if ((ec = SI->getName(Name)))
977 report_fatal_error(ec.message());
982 // If we couldn't find a symbol that this relocation refers to, try
983 // to find a section beginning instead.
984 for (section_iterator SI = begin_sections(), SE = end_sections(); SI != SE;
986 if (ec) report_fatal_error(ec.message());
991 if ((ec = SI->getAddress(Addr)))
992 report_fatal_error(ec.message());
993 if (Addr != Val) continue;
994 if ((ec = SI->getName(Name)))
995 report_fatal_error(ec.message());
1000 fmt << format("0x%x", Val);
1005 bool isExtern = (RE->Word1 >> 27) & 1;
1006 uint32_t Val = RE->Word1 & 0xFFFFFF;
1009 symbol_iterator SI = begin_symbols();
1013 section_iterator SI = begin_sections();
1021 error_code MachOObjectFile::getRelocationValueString(DataRefImpl Rel,
1022 SmallVectorImpl<char> &Result) const {
1023 InMemoryStruct<macho::RelocationEntry> RE;
1024 getRelocation(Rel, RE);
1026 unsigned Arch = getArch();
1027 bool isScattered = (Arch != Triple::x86_64) &&
1028 (RE->Word0 & macho::RF_Scattered);
1031 raw_string_ostream fmt(fmtbuf);
1035 Type = (RE->Word0 >> 24) & 0xF;
1037 Type = (RE->Word1 >> 28) & 0xF;
1041 isPCRel = ((RE->Word0 >> 30) & 1);
1043 isPCRel = ((RE->Word1 >> 24) & 1);
1045 // Determine any addends that should be displayed with the relocation.
1046 // These require decoding the relocation type, which is triple-specific.
1048 // X86_64 has entirely custom relocation types.
1049 if (Arch == Triple::x86_64) {
1050 bool isPCRel = ((RE->Word1 >> 24) & 1);
1053 case macho::RIT_X86_64_GOTLoad: // X86_64_RELOC_GOT_LOAD
1054 case macho::RIT_X86_64_GOT: { // X86_64_RELOC_GOT
1055 printRelocationTargetName(RE, fmt);
1057 if (isPCRel) fmt << "PCREL";
1060 case macho::RIT_X86_64_Subtractor: { // X86_64_RELOC_SUBTRACTOR
1061 InMemoryStruct<macho::RelocationEntry> RENext;
1062 DataRefImpl RelNext = Rel;
1064 getRelocation(RelNext, RENext);
1066 // X86_64_SUBTRACTOR must be followed by a relocation of type
1067 // X86_64_RELOC_UNSIGNED.
1068 // NOTE: Scattered relocations don't exist on x86_64.
1069 unsigned RType = (RENext->Word1 >> 28) & 0xF;
1071 report_fatal_error("Expected X86_64_RELOC_UNSIGNED after "
1072 "X86_64_RELOC_SUBTRACTOR.");
1074 // The X86_64_RELOC_UNSIGNED contains the minuend symbol,
1075 // X86_64_SUBTRACTOR contains to the subtrahend.
1076 printRelocationTargetName(RENext, fmt);
1078 printRelocationTargetName(RE, fmt);
1080 case macho::RIT_X86_64_TLV:
1081 printRelocationTargetName(RE, fmt);
1083 if (isPCRel) fmt << "P";
1085 case macho::RIT_X86_64_Signed1: // X86_64_RELOC_SIGNED1
1086 printRelocationTargetName(RE, fmt);
1089 case macho::RIT_X86_64_Signed2: // X86_64_RELOC_SIGNED2
1090 printRelocationTargetName(RE, fmt);
1093 case macho::RIT_X86_64_Signed4: // X86_64_RELOC_SIGNED4
1094 printRelocationTargetName(RE, fmt);
1098 printRelocationTargetName(RE, fmt);
1101 // X86 and ARM share some relocation types in common.
1102 } else if (Arch == Triple::x86 || Arch == Triple::arm) {
1103 // Generic relocation types...
1105 case macho::RIT_Pair: // GENERIC_RELOC_PAIR - prints no info
1106 return object_error::success;
1107 case macho::RIT_Difference: { // GENERIC_RELOC_SECTDIFF
1108 InMemoryStruct<macho::RelocationEntry> RENext;
1109 DataRefImpl RelNext = Rel;
1111 getRelocation(RelNext, RENext);
1113 // X86 sect diff's must be followed by a relocation of type
1114 // GENERIC_RELOC_PAIR.
1115 bool isNextScattered = (Arch != Triple::x86_64) &&
1116 (RENext->Word0 & macho::RF_Scattered);
1118 if (isNextScattered)
1119 RType = (RENext->Word0 >> 24) & 0xF;
1121 RType = (RENext->Word1 >> 28) & 0xF;
1123 report_fatal_error("Expected GENERIC_RELOC_PAIR after "
1124 "GENERIC_RELOC_SECTDIFF.");
1126 printRelocationTargetName(RE, fmt);
1128 printRelocationTargetName(RENext, fmt);
1133 if (Arch == Triple::x86) {
1134 // All X86 relocations that need special printing were already
1135 // handled in the generic code.
1137 case macho::RIT_Generic_LocalDifference:{// GENERIC_RELOC_LOCAL_SECTDIFF
1138 InMemoryStruct<macho::RelocationEntry> RENext;
1139 DataRefImpl RelNext = Rel;
1141 getRelocation(RelNext, RENext);
1143 // X86 sect diff's must be followed by a relocation of type
1144 // GENERIC_RELOC_PAIR.
1145 bool isNextScattered = (Arch != Triple::x86_64) &&
1146 (RENext->Word0 & macho::RF_Scattered);
1148 if (isNextScattered)
1149 RType = (RENext->Word0 >> 24) & 0xF;
1151 RType = (RENext->Word1 >> 28) & 0xF;
1153 report_fatal_error("Expected GENERIC_RELOC_PAIR after "
1154 "GENERIC_RELOC_LOCAL_SECTDIFF.");
1156 printRelocationTargetName(RE, fmt);
1158 printRelocationTargetName(RENext, fmt);
1161 case macho::RIT_Generic_TLV: {
1162 printRelocationTargetName(RE, fmt);
1164 if (isPCRel) fmt << "P";
1168 printRelocationTargetName(RE, fmt);
1170 } else { // ARM-specific relocations
1172 case macho::RIT_ARM_Half: // ARM_RELOC_HALF
1173 case macho::RIT_ARM_HalfDifference: { // ARM_RELOC_HALF_SECTDIFF
1174 // Half relocations steal a bit from the length field to encode
1175 // whether this is an upper16 or a lower16 relocation.
1178 isUpper = (RE->Word0 >> 28) & 1;
1180 isUpper = (RE->Word1 >> 25) & 1;
1183 fmt << ":upper16:(";
1185 fmt << ":lower16:(";
1186 printRelocationTargetName(RE, fmt);
1188 InMemoryStruct<macho::RelocationEntry> RENext;
1189 DataRefImpl RelNext = Rel;
1191 getRelocation(RelNext, RENext);
1193 // ARM half relocs must be followed by a relocation of type
1195 bool isNextScattered = (Arch != Triple::x86_64) &&
1196 (RENext->Word0 & macho::RF_Scattered);
1198 if (isNextScattered)
1199 RType = (RENext->Word0 >> 24) & 0xF;
1201 RType = (RENext->Word1 >> 28) & 0xF;
1204 report_fatal_error("Expected ARM_RELOC_PAIR after "
1205 "GENERIC_RELOC_HALF");
1207 // NOTE: The half of the target virtual address is stashed in the
1208 // address field of the secondary relocation, but we can't reverse
1209 // engineer the constant offset from it without decoding the movw/movt
1210 // instruction to find the other half in its immediate field.
1212 // ARM_RELOC_HALF_SECTDIFF encodes the second section in the
1213 // symbol/section pointer of the follow-on relocation.
1214 if (Type == macho::RIT_ARM_HalfDifference) {
1216 printRelocationTargetName(RENext, fmt);
1223 printRelocationTargetName(RE, fmt);
1228 printRelocationTargetName(RE, fmt);
1231 Result.append(fmtbuf.begin(), fmtbuf.end());
1232 return object_error::success;
1235 error_code MachOObjectFile::getRelocationHidden(DataRefImpl Rel,
1236 bool &Result) const {
1237 InMemoryStruct<macho::RelocationEntry> RE;
1238 getRelocation(Rel, RE);
1240 unsigned Arch = getArch();
1241 bool isScattered = (Arch != Triple::x86_64) &&
1242 (RE->Word0 & macho::RF_Scattered);
1245 Type = (RE->Word0 >> 24) & 0xF;
1247 Type = (RE->Word1 >> 28) & 0xF;
1251 // On arches that use the generic relocations, GENERIC_RELOC_PAIR
1252 // is always hidden.
1253 if (Arch == Triple::x86 || Arch == Triple::arm) {
1254 if (Type == macho::RIT_Pair) Result = true;
1255 } else if (Arch == Triple::x86_64) {
1256 // On x86_64, X86_64_RELOC_UNSIGNED is hidden only when it follows
1257 // an X864_64_RELOC_SUBTRACTOR.
1258 if (Type == macho::RIT_X86_64_Unsigned && Rel.d.a > 0) {
1259 DataRefImpl RelPrev = Rel;
1261 InMemoryStruct<macho::RelocationEntry> REPrev;
1262 getRelocation(RelPrev, REPrev);
1264 unsigned PrevType = (REPrev->Word1 >> 28) & 0xF;
1266 if (PrevType == macho::RIT_X86_64_Subtractor) Result = true;
1270 return object_error::success;
1273 error_code MachOObjectFile::getLibraryNext(DataRefImpl LibData,
1274 LibraryRef &Res) const {
1275 report_fatal_error("Needed libraries unimplemented in MachOObjectFile");
1278 error_code MachOObjectFile::getLibraryPath(DataRefImpl LibData,
1279 StringRef &Res) const {
1280 report_fatal_error("Needed libraries unimplemented in MachOObjectFile");
1284 /*===-- Miscellaneous -----------------------------------------------------===*/
1286 uint8_t MachOObjectFile::getBytesInAddress() const {
1287 return MachOObj->is64Bit() ? 8 : 4;
1290 StringRef MachOObjectFile::getFileFormatName() const {
1291 if (!MachOObj->is64Bit()) {
1292 switch (MachOObj->getHeader().CPUType) {
1293 case llvm::MachO::CPUTypeI386:
1294 return "Mach-O 32-bit i386";
1295 case llvm::MachO::CPUTypeARM:
1296 return "Mach-O arm";
1297 case llvm::MachO::CPUTypePowerPC:
1298 return "Mach-O 32-bit ppc";
1300 assert((MachOObj->getHeader().CPUType & llvm::MachO::CPUArchABI64) == 0 &&
1301 "64-bit object file when we're not 64-bit?");
1302 return "Mach-O 32-bit unknown";
1306 switch (MachOObj->getHeader().CPUType) {
1307 case llvm::MachO::CPUTypeX86_64:
1308 return "Mach-O 64-bit x86-64";
1309 case llvm::MachO::CPUTypePowerPC64:
1310 return "Mach-O 64-bit ppc64";
1312 assert((MachOObj->getHeader().CPUType & llvm::MachO::CPUArchABI64) == 1 &&
1313 "32-bit object file when we're 64-bit?");
1314 return "Mach-O 64-bit unknown";
1318 unsigned MachOObjectFile::getArch() const {
1319 switch (MachOObj->getHeader().CPUType) {
1320 case llvm::MachO::CPUTypeI386:
1322 case llvm::MachO::CPUTypeX86_64:
1323 return Triple::x86_64;
1324 case llvm::MachO::CPUTypeARM:
1326 case llvm::MachO::CPUTypePowerPC:
1328 case llvm::MachO::CPUTypePowerPC64:
1329 return Triple::ppc64;
1331 return Triple::UnknownArch;
1335 } // end namespace object
1336 } // end namespace llvm