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),
35 moveToNextSection(DRI);
36 uint32_t LoadCommandCount = MachOObj->getHeader().NumLoadCommands;
37 while (DRI.d.a < LoadCommandCount) {
38 Sections.push_back(DRI);
40 moveToNextSection(DRI);
44 bool MachOObjectFile::is64Bit() const {
45 return MachOObj->is64Bit();
48 const LoadCommandInfo &
49 MachOObjectFile::getLoadCommandInfo(unsigned Index) const {
50 return MachOObj->getLoadCommandInfo(Index);
53 void MachOObjectFile::ReadULEB128s(uint64_t Index,
54 SmallVectorImpl<uint64_t> &Out) const {
55 return MachOObj->ReadULEB128s(Index, Out);
58 const macho::Header &MachOObjectFile::getHeader() const {
59 return MachOObj->getHeader();
62 ObjectFile *ObjectFile::createMachOObjectFile(MemoryBuffer *Buffer) {
65 MachOObject *MachOObj = MachOObject::LoadFromBuffer(Buffer, &Err);
68 // MachOObject takes ownership of the Buffer we passed to it, and
69 // MachOObjectFile does, too, so we need to make sure they don't get the
70 // same object. A MemoryBuffer is cheap (it's just a reference to memory,
71 // not a copy of the memory itself), so just make a new copy here for
72 // the MachOObjectFile.
73 MemoryBuffer *NewBuffer =
74 MemoryBuffer::getMemBuffer(Buffer->getBuffer(),
75 Buffer->getBufferIdentifier(), false);
76 return new MachOObjectFile(NewBuffer, MachOObj, ec);
79 /*===-- Symbols -----------------------------------------------------------===*/
81 const MachOFormat::SymtabLoadCommand *
82 MachOObjectFile::getSymtabLoadCommand(LoadCommandInfo LCI) const {
83 StringRef Data = MachOObj->getData(LCI.Offset,
84 sizeof(MachOFormat::SymtabLoadCommand));
85 return reinterpret_cast<const MachOFormat::SymtabLoadCommand*>(Data.data());
88 const MachOFormat::SegmentLoadCommand *
89 MachOObjectFile::getSegmentLoadCommand(LoadCommandInfo LCI) const {
90 StringRef Data = MachOObj->getData(LCI.Offset,
91 sizeof(MachOFormat::SegmentLoadCommand));
92 return reinterpret_cast<const MachOFormat::SegmentLoadCommand*>(Data.data());
95 const MachOFormat::LinkeditDataLoadCommand *
96 MachOObjectFile::getLinkeditDataLoadCommand(LoadCommandInfo LCI) const {
97 StringRef Data = MachOObj->getData(LCI.Offset,
98 sizeof(MachOFormat::LinkeditDataLoadCommand));
100 reinterpret_cast<const MachOFormat::LinkeditDataLoadCommand*>(Data.data());
103 const MachOFormat::Segment64LoadCommand *
104 MachOObjectFile::getSegment64LoadCommand(LoadCommandInfo LCI) const {
105 StringRef Data = MachOObj->getData(LCI.Offset,
106 sizeof(MachOFormat::Segment64LoadCommand));
108 reinterpret_cast<const MachOFormat::Segment64LoadCommand*>(Data.data());
111 void MachOObjectFile::moveToNextSymbol(DataRefImpl &DRI) const {
112 uint32_t LoadCommandCount = MachOObj->getHeader().NumLoadCommands;
113 while (DRI.d.a < LoadCommandCount) {
114 LoadCommandInfo LCI = MachOObj->getLoadCommandInfo(DRI.d.a);
115 if (LCI.Command.Type == macho::LCT_Symtab) {
116 const MachOFormat::SymtabLoadCommand *SymtabLoadCmd =
117 getSymtabLoadCommand(LCI);
118 if (DRI.d.b < SymtabLoadCmd->NumSymbolTableEntries)
127 const MachOFormat::SymbolTableEntry *
128 MachOObjectFile::getSymbolTableEntry(DataRefImpl DRI) const {
129 LoadCommandInfo LCI = MachOObj->getLoadCommandInfo(DRI.d.a);
130 const MachOFormat::SymtabLoadCommand *SymtabLoadCmd =
131 getSymtabLoadCommand(LCI);
133 return getSymbolTableEntry(DRI, SymtabLoadCmd);
136 const MachOFormat::SymbolTableEntry *
137 MachOObjectFile::getSymbolTableEntry(DataRefImpl DRI,
138 const MachOFormat::SymtabLoadCommand *SymtabLoadCmd) const {
139 uint64_t SymbolTableOffset = SymtabLoadCmd->SymbolTableOffset;
140 unsigned Index = DRI.d.b;
141 uint64_t Offset = (SymbolTableOffset +
142 Index * sizeof(macho::SymbolTableEntry));
143 StringRef Data = MachOObj->getData(Offset,
144 sizeof(MachOFormat::SymbolTableEntry));
145 return reinterpret_cast<const MachOFormat::SymbolTableEntry*>(Data.data());
148 const MachOFormat::Symbol64TableEntry*
149 MachOObjectFile::getSymbol64TableEntry(DataRefImpl DRI) const {
150 LoadCommandInfo LCI = MachOObj->getLoadCommandInfo(DRI.d.a);
151 const MachOFormat::SymtabLoadCommand *SymtabLoadCmd =
152 getSymtabLoadCommand(LCI);
154 return getSymbol64TableEntry(DRI, SymtabLoadCmd);
157 const MachOFormat::Symbol64TableEntry*
158 MachOObjectFile::getSymbol64TableEntry(DataRefImpl DRI,
159 const MachOFormat::SymtabLoadCommand *SymtabLoadCmd) const {
160 uint64_t SymbolTableOffset = SymtabLoadCmd->SymbolTableOffset;
161 unsigned Index = DRI.d.b;
162 uint64_t Offset = (SymbolTableOffset +
163 Index * sizeof(macho::Symbol64TableEntry));
164 StringRef Data = MachOObj->getData(Offset,
165 sizeof(MachOFormat::Symbol64TableEntry));
166 return reinterpret_cast<const MachOFormat::Symbol64TableEntry*>(Data.data());
169 error_code MachOObjectFile::getSymbolNext(DataRefImpl DRI,
170 SymbolRef &Result) const {
172 moveToNextSymbol(DRI);
173 Result = SymbolRef(DRI, this);
174 return object_error::success;
177 error_code MachOObjectFile::getSymbolName(DataRefImpl DRI,
178 StringRef &Result) const {
179 LoadCommandInfo LCI = MachOObj->getLoadCommandInfo(DRI.d.a);
180 const MachOFormat::SymtabLoadCommand *SymtabLoadCmd =
181 getSymtabLoadCommand(LCI);
183 StringRef StringTable =
184 MachOObj->getData(SymtabLoadCmd->StringTableOffset,
185 SymtabLoadCmd->StringTableSize);
187 uint32_t StringIndex;
188 if (MachOObj->is64Bit()) {
189 const MachOFormat::Symbol64TableEntry *Entry =
190 getSymbol64TableEntry(DRI, SymtabLoadCmd);
191 StringIndex = Entry->StringIndex;
193 const MachOFormat::SymbolTableEntry *Entry =
194 getSymbolTableEntry(DRI, SymtabLoadCmd);
195 StringIndex = Entry->StringIndex;
198 const char *Start = &StringTable.data()[StringIndex];
199 Result = StringRef(Start);
201 return object_error::success;
204 error_code MachOObjectFile::getSymbolFileOffset(DataRefImpl DRI,
205 uint64_t &Result) const {
206 if (MachOObj->is64Bit()) {
207 const MachOFormat::Symbol64TableEntry *Entry = getSymbol64TableEntry(DRI);
208 Result = Entry->Value;
209 if (Entry->SectionIndex) {
210 const MachOFormat::Section64 *Section =
211 getSection64(Sections[Entry->SectionIndex-1]);
212 Result += Section->Offset - Section->Address;
215 const MachOFormat::SymbolTableEntry *Entry = getSymbolTableEntry(DRI);
216 Result = Entry->Value;
217 if (Entry->SectionIndex) {
218 const MachOFormat::Section *Section =
219 getSection(Sections[Entry->SectionIndex-1]);
220 Result += Section->Offset - Section->Address;
224 return object_error::success;
227 error_code MachOObjectFile::getSymbolAddress(DataRefImpl DRI,
228 uint64_t &Result) const {
229 if (MachOObj->is64Bit()) {
230 const MachOFormat::Symbol64TableEntry *Entry = getSymbol64TableEntry(DRI);
231 Result = Entry->Value;
233 const MachOFormat::SymbolTableEntry *Entry = getSymbolTableEntry(DRI);
234 Result = Entry->Value;
236 return object_error::success;
239 error_code MachOObjectFile::getSymbolSize(DataRefImpl DRI,
240 uint64_t &Result) const {
241 uint32_t LoadCommandCount = MachOObj->getHeader().NumLoadCommands;
242 uint64_t BeginOffset;
243 uint64_t EndOffset = 0;
244 uint8_t SectionIndex;
245 if (MachOObj->is64Bit()) {
246 const MachOFormat::Symbol64TableEntry *Entry = getSymbol64TableEntry(DRI);
247 BeginOffset = Entry->Value;
248 SectionIndex = Entry->SectionIndex;
250 uint32_t flags = SymbolRef::SF_None;
251 getSymbolFlags(DRI, flags);
252 if (flags & SymbolRef::SF_Common)
253 Result = Entry->Value;
255 Result = UnknownAddressOrSize;
256 return object_error::success;
258 // Unfortunately symbols are unsorted so we need to touch all
259 // symbols from load command
261 uint32_t Command = DRI.d.a;
262 while (Command == DRI.d.a) {
263 moveToNextSymbol(DRI);
264 if (DRI.d.a < LoadCommandCount) {
265 Entry = getSymbol64TableEntry(DRI);
266 if (Entry->SectionIndex == SectionIndex && Entry->Value > BeginOffset)
267 if (!EndOffset || Entry->Value < EndOffset)
268 EndOffset = Entry->Value;
273 const MachOFormat::SymbolTableEntry *Entry = getSymbolTableEntry(DRI);
274 BeginOffset = Entry->Value;
275 SectionIndex = Entry->SectionIndex;
277 uint32_t flags = SymbolRef::SF_None;
278 getSymbolFlags(DRI, flags);
279 if (flags & SymbolRef::SF_Common)
280 Result = Entry->Value;
282 Result = UnknownAddressOrSize;
283 return object_error::success;
285 // Unfortunately symbols are unsorted so we need to touch all
286 // symbols from load command
288 uint32_t Command = DRI.d.a;
289 while (Command == DRI.d.a) {
290 moveToNextSymbol(DRI);
291 if (DRI.d.a < LoadCommandCount) {
292 Entry = getSymbolTableEntry(DRI);
293 if (Entry->SectionIndex == SectionIndex && Entry->Value > BeginOffset)
294 if (!EndOffset || Entry->Value < EndOffset)
295 EndOffset = Entry->Value;
302 getSectionSize(Sections[SectionIndex-1], Size);
303 getSectionAddress(Sections[SectionIndex-1], EndOffset);
306 Result = EndOffset - BeginOffset;
307 return object_error::success;
310 error_code MachOObjectFile::getSymbolNMTypeChar(DataRefImpl DRI,
311 char &Result) const {
313 if (MachOObj->is64Bit()) {
314 const MachOFormat::Symbol64TableEntry *Entry = getSymbol64TableEntry(DRI);
316 Flags = Entry->Flags;
318 const MachOFormat::SymbolTableEntry *Entry = getSymbolTableEntry(DRI);
320 Flags = Entry->Flags;
324 switch (Type & macho::STF_TypeMask) {
325 case macho::STT_Undefined:
328 case macho::STT_Absolute:
329 case macho::STT_Section:
337 if (Flags & (macho::STF_External | macho::STF_PrivateExtern))
338 Char = toupper(static_cast<unsigned char>(Char));
340 return object_error::success;
343 error_code MachOObjectFile::getSymbolFlags(DataRefImpl DRI,
344 uint32_t &Result) const {
347 if (MachOObj->is64Bit()) {
348 const MachOFormat::Symbol64TableEntry *Entry = getSymbol64TableEntry(DRI);
349 MachOFlags = Entry->Flags;
350 MachOType = Entry->Type;
352 const MachOFormat::SymbolTableEntry *Entry = getSymbolTableEntry(DRI);
353 MachOFlags = Entry->Flags;
354 MachOType = Entry->Type;
357 // TODO: Correctly set SF_ThreadLocal
358 Result = SymbolRef::SF_None;
360 if ((MachOType & MachO::NlistMaskType) == MachO::NListTypeUndefined)
361 Result |= SymbolRef::SF_Undefined;
363 if (MachOFlags & macho::STF_StabsEntryMask)
364 Result |= SymbolRef::SF_FormatSpecific;
366 if (MachOType & MachO::NlistMaskExternal) {
367 Result |= SymbolRef::SF_Global;
368 if ((MachOType & MachO::NlistMaskType) == MachO::NListTypeUndefined)
369 Result |= SymbolRef::SF_Common;
372 if (MachOFlags & (MachO::NListDescWeakRef | MachO::NListDescWeakDef))
373 Result |= SymbolRef::SF_Weak;
375 if ((MachOType & MachO::NlistMaskType) == MachO::NListTypeAbsolute)
376 Result |= SymbolRef::SF_Absolute;
378 return object_error::success;
381 error_code MachOObjectFile::getSymbolSection(DataRefImpl Symb,
382 section_iterator &Res) const {
384 if (MachOObj->is64Bit()) {
385 const MachOFormat::Symbol64TableEntry *Entry = getSymbol64TableEntry(Symb);
386 index = Entry->SectionIndex;
388 const MachOFormat::SymbolTableEntry *Entry = getSymbolTableEntry(Symb);
389 index = Entry->SectionIndex;
393 Res = end_sections();
395 Res = section_iterator(SectionRef(Sections[index-1], this));
397 return object_error::success;
400 error_code MachOObjectFile::getSymbolType(DataRefImpl Symb,
401 SymbolRef::Type &Res) const {
403 if (MachOObj->is64Bit()) {
404 const MachOFormat::Symbol64TableEntry *Entry = getSymbol64TableEntry(Symb);
405 n_type = Entry->Type;
407 const MachOFormat::SymbolTableEntry *Entry = getSymbolTableEntry(Symb);
408 n_type = Entry->Type;
410 Res = SymbolRef::ST_Other;
412 // If this is a STAB debugging symbol, we can do nothing more.
413 if (n_type & MachO::NlistMaskStab) {
414 Res = SymbolRef::ST_Debug;
415 return object_error::success;
418 switch (n_type & MachO::NlistMaskType) {
419 case MachO::NListTypeUndefined :
420 Res = SymbolRef::ST_Unknown;
422 case MachO::NListTypeSection :
423 Res = SymbolRef::ST_Function;
426 return object_error::success;
429 error_code MachOObjectFile::getSymbolValue(DataRefImpl Symb,
430 uint64_t &Val) const {
431 report_fatal_error("getSymbolValue unimplemented in MachOObjectFile");
434 symbol_iterator MachOObjectFile::begin_symbols() const {
435 // DRI.d.a = segment number; DRI.d.b = symbol index.
437 moveToNextSymbol(DRI);
438 return symbol_iterator(SymbolRef(DRI, this));
441 symbol_iterator MachOObjectFile::end_symbols() const {
443 DRI.d.a = MachOObj->getHeader().NumLoadCommands;
444 return symbol_iterator(SymbolRef(DRI, this));
447 symbol_iterator MachOObjectFile::begin_dynamic_symbols() const {
449 report_fatal_error("Dynamic symbols unimplemented in MachOObjectFile");
452 symbol_iterator MachOObjectFile::end_dynamic_symbols() const {
454 report_fatal_error("Dynamic symbols unimplemented in MachOObjectFile");
457 library_iterator MachOObjectFile::begin_libraries_needed() const {
459 report_fatal_error("Needed libraries unimplemented in MachOObjectFile");
462 library_iterator MachOObjectFile::end_libraries_needed() const {
464 report_fatal_error("Needed libraries unimplemented in MachOObjectFile");
467 StringRef MachOObjectFile::getLoadName() const {
469 report_fatal_error("get_load_name() unimplemented in MachOObjectFile");
472 /*===-- Sections ----------------------------------------------------------===*/
474 void MachOObjectFile::moveToNextSection(DataRefImpl &DRI) const {
475 uint32_t LoadCommandCount = MachOObj->getHeader().NumLoadCommands;
476 while (DRI.d.a < LoadCommandCount) {
477 LoadCommandInfo LCI = MachOObj->getLoadCommandInfo(DRI.d.a);
478 if (LCI.Command.Type == macho::LCT_Segment) {
479 const MachOFormat::SegmentLoadCommand *SegmentLoadCmd =
480 getSegmentLoadCommand(LCI);
481 if (DRI.d.b < SegmentLoadCmd->NumSections)
483 } else if (LCI.Command.Type == macho::LCT_Segment64) {
484 const MachOFormat::Segment64LoadCommand *Segment64LoadCmd =
485 getSegment64LoadCommand(LCI);
486 if (DRI.d.b < Segment64LoadCmd->NumSections)
495 error_code MachOObjectFile::getSectionNext(DataRefImpl DRI,
496 SectionRef &Result) const {
498 moveToNextSection(DRI);
499 Result = SectionRef(DRI, this);
500 return object_error::success;
503 static bool is64BitLoadCommand(const MachOObject *MachOObj, DataRefImpl DRI) {
504 LoadCommandInfo LCI = MachOObj->getLoadCommandInfo(DRI.d.a);
505 if (LCI.Command.Type == macho::LCT_Segment64)
507 assert(LCI.Command.Type == macho::LCT_Segment && "Unexpected Type.");
511 const MachOFormat::Section *MachOObjectFile::getSection(DataRefImpl DRI) const {
512 assert(!is64BitLoadCommand(MachOObj.get(), DRI));
513 LoadCommandInfo LCI = MachOObj->getLoadCommandInfo(DRI.d.a);
514 unsigned SectionOffset = LCI.Offset + sizeof(macho::SegmentLoadCommand) +
515 DRI.d.b * sizeof(MachOFormat::Section);
516 StringRef Data = MachOObj->getData(SectionOffset, sizeof(MachOFormat::Section));
517 return reinterpret_cast<const MachOFormat::Section*>(Data.data());
520 std::size_t MachOObjectFile::getSectionIndex(DataRefImpl Sec) const {
521 SectionList::const_iterator loc =
522 std::find(Sections.begin(), Sections.end(), Sec);
523 assert(loc != Sections.end() && "Sec is not a valid section!");
524 return std::distance(Sections.begin(), loc);
527 const MachOFormat::Section64 *
528 MachOObjectFile::getSection64(DataRefImpl DRI) const {
529 assert(is64BitLoadCommand(MachOObj.get(), DRI));
530 LoadCommandInfo LCI = MachOObj->getLoadCommandInfo(DRI.d.a);
531 unsigned SectionOffset = LCI.Offset + sizeof(macho::Segment64LoadCommand) +
532 DRI.d.b * sizeof(MachOFormat::Section64);
533 StringRef Data = MachOObj->getData(SectionOffset, sizeof(MachOFormat::Section64));
534 return reinterpret_cast<const MachOFormat::Section64*>(Data.data());
537 static StringRef parseSegmentOrSectionName(const char *P) {
541 // Not null terminated, so this is a 16 char string.
542 return StringRef(P, 16);
545 ArrayRef<char> MachOObjectFile::getSectionRawName(DataRefImpl DRI) const {
546 if (is64BitLoadCommand(MachOObj.get(), DRI)) {
547 const MachOFormat::Section64 *sec = getSection64(DRI);
548 return ArrayRef<char>(sec->Name);
550 const MachOFormat::Section *sec = getSection(DRI);
551 return ArrayRef<char>(sec->Name);
555 error_code MachOObjectFile::getSectionName(DataRefImpl DRI,
556 StringRef &Result) const {
557 ArrayRef<char> Raw = getSectionRawName(DRI);
558 Result = parseSegmentOrSectionName(Raw.data());
559 return object_error::success;
563 MachOObjectFile::getSectionRawFinalSegmentName(DataRefImpl Sec) const {
564 if (is64BitLoadCommand(MachOObj.get(), Sec)) {
565 const MachOFormat::Section64 *sec = getSection64(Sec);
566 return ArrayRef<char>(sec->SegmentName, 16);
568 const MachOFormat::Section *sec = getSection(Sec);
569 return ArrayRef<char>(sec->SegmentName);
573 StringRef MachOObjectFile::getSectionFinalSegmentName(DataRefImpl DRI) const {
574 ArrayRef<char> Raw = getSectionRawFinalSegmentName(DRI);
575 return parseSegmentOrSectionName(Raw.data());
578 error_code MachOObjectFile::getSectionAddress(DataRefImpl DRI,
579 uint64_t &Result) const {
580 if (is64BitLoadCommand(MachOObj.get(), DRI)) {
581 const MachOFormat::Section64 *Sect = getSection64(DRI);
582 Result = Sect->Address;
584 const MachOFormat::Section *Sect = getSection(DRI);
585 Result = Sect->Address;
587 return object_error::success;
590 error_code MachOObjectFile::getSectionSize(DataRefImpl DRI,
591 uint64_t &Result) const {
592 if (is64BitLoadCommand(MachOObj.get(), DRI)) {
593 const MachOFormat::Section64 *Sect = getSection64(DRI);
596 const MachOFormat::Section *Sect = getSection(DRI);
599 return object_error::success;
602 error_code MachOObjectFile::getSectionContents(DataRefImpl DRI,
603 StringRef &Result) const {
604 if (is64BitLoadCommand(MachOObj.get(), DRI)) {
605 const MachOFormat::Section64 *Sect = getSection64(DRI);
606 Result = MachOObj->getData(Sect->Offset, Sect->Size);
608 const MachOFormat::Section *Sect = getSection(DRI);
609 Result = MachOObj->getData(Sect->Offset, Sect->Size);
611 return object_error::success;
614 error_code MachOObjectFile::getSectionAlignment(DataRefImpl DRI,
615 uint64_t &Result) const {
616 if (is64BitLoadCommand(MachOObj.get(), DRI)) {
617 const MachOFormat::Section64 *Sect = getSection64(DRI);
618 Result = uint64_t(1) << Sect->Align;
620 const MachOFormat::Section *Sect = getSection(DRI);
621 Result = uint64_t(1) << Sect->Align;
623 return object_error::success;
626 error_code MachOObjectFile::isSectionText(DataRefImpl DRI,
627 bool &Result) const {
628 if (is64BitLoadCommand(MachOObj.get(), DRI)) {
629 const MachOFormat::Section64 *Sect = getSection64(DRI);
630 Result = Sect->Flags & macho::SF_PureInstructions;
632 const MachOFormat::Section *Sect = getSection(DRI);
633 Result = Sect->Flags & macho::SF_PureInstructions;
635 return object_error::success;
638 error_code MachOObjectFile::isSectionData(DataRefImpl DRI,
639 bool &Result) const {
640 // FIXME: Unimplemented.
642 return object_error::success;
645 error_code MachOObjectFile::isSectionBSS(DataRefImpl DRI,
646 bool &Result) const {
647 // FIXME: Unimplemented.
649 return object_error::success;
652 error_code MachOObjectFile::isSectionRequiredForExecution(DataRefImpl Sec,
653 bool &Result) const {
654 // FIXME: Unimplemented.
656 return object_error::success;
659 error_code MachOObjectFile::isSectionVirtual(DataRefImpl Sec,
660 bool &Result) const {
661 // FIXME: Unimplemented.
663 return object_error::success;
666 error_code MachOObjectFile::isSectionZeroInit(DataRefImpl DRI,
667 bool &Result) const {
668 if (MachOObj->is64Bit()) {
669 const MachOFormat::Section64 *Sect = getSection64(DRI);
670 unsigned SectionType = Sect->Flags & MachO::SectionFlagMaskSectionType;
671 Result = (SectionType == MachO::SectionTypeZeroFill ||
672 SectionType == MachO::SectionTypeZeroFillLarge);
674 const MachOFormat::Section *Sect = getSection(DRI);
675 unsigned SectionType = Sect->Flags & MachO::SectionFlagMaskSectionType;
676 Result = (SectionType == MachO::SectionTypeZeroFill ||
677 SectionType == MachO::SectionTypeZeroFillLarge);
680 return object_error::success;
683 error_code MachOObjectFile::isSectionReadOnlyData(DataRefImpl Sec,
684 bool &Result) const {
685 // Consider using the code from isSectionText to look for __const sections.
686 // Alternately, emit S_ATTR_PURE_INSTRUCTIONS and/or S_ATTR_SOME_INSTRUCTIONS
687 // to use section attributes to distinguish code from data.
689 // FIXME: Unimplemented.
691 return object_error::success;
694 error_code MachOObjectFile::sectionContainsSymbol(DataRefImpl Sec,
696 bool &Result) const {
698 getSymbolType(Symb, ST);
699 if (ST == SymbolRef::ST_Unknown) {
701 return object_error::success;
704 uint64_t SectBegin, SectEnd;
705 getSectionAddress(Sec, SectBegin);
706 getSectionSize(Sec, SectEnd);
707 SectEnd += SectBegin;
709 if (MachOObj->is64Bit()) {
710 const MachOFormat::Symbol64TableEntry *Entry = getSymbol64TableEntry(Symb);
711 uint64_t SymAddr= Entry->Value;
712 Result = (SymAddr >= SectBegin) && (SymAddr < SectEnd);
714 const MachOFormat::SymbolTableEntry *Entry = getSymbolTableEntry(Symb);
715 uint64_t SymAddr= Entry->Value;
716 Result = (SymAddr >= SectBegin) && (SymAddr < SectEnd);
719 return object_error::success;
722 relocation_iterator MachOObjectFile::getSectionRelBegin(DataRefImpl Sec) const {
724 ret.d.b = getSectionIndex(Sec);
725 return relocation_iterator(RelocationRef(ret, this));
727 relocation_iterator MachOObjectFile::getSectionRelEnd(DataRefImpl Sec) const {
729 if (is64BitLoadCommand(MachOObj.get(), Sec)) {
730 const MachOFormat::Section64 *Sect = getSection64(Sec);
731 last_reloc = Sect->NumRelocationTableEntries;
733 const MachOFormat::Section *Sect = getSection(Sec);
734 last_reloc = Sect->NumRelocationTableEntries;
737 ret.d.a = last_reloc;
738 ret.d.b = getSectionIndex(Sec);
739 return relocation_iterator(RelocationRef(ret, this));
742 section_iterator MachOObjectFile::begin_sections() const {
744 moveToNextSection(DRI);
745 return section_iterator(SectionRef(DRI, this));
748 section_iterator MachOObjectFile::end_sections() const {
750 DRI.d.a = MachOObj->getHeader().NumLoadCommands;
751 return section_iterator(SectionRef(DRI, this));
754 /*===-- Relocations -------------------------------------------------------===*/
756 const MachOFormat::RelocationEntry *
757 MachOObjectFile::getRelocation(DataRefImpl Rel) const {
759 if (MachOObj->is64Bit()) {
760 const MachOFormat::Section64 *Sect = getSection64(Sections[Rel.d.b]);
761 relOffset = Sect->RelocationTableOffset;
763 const MachOFormat::Section *Sect = getSection(Sections[Rel.d.b]);
764 relOffset = Sect->RelocationTableOffset;
766 uint64_t Offset = relOffset + Rel.d.a * sizeof(MachOFormat::RelocationEntry);
768 MachOObj->getData(Offset, sizeof(MachOFormat::RelocationEntry));
769 return reinterpret_cast<const MachOFormat::RelocationEntry*>(Data.data());
772 error_code MachOObjectFile::getRelocationNext(DataRefImpl Rel,
773 RelocationRef &Res) const {
775 Res = RelocationRef(Rel, this);
776 return object_error::success;
778 error_code MachOObjectFile::getRelocationAddress(DataRefImpl Rel,
779 uint64_t &Res) const {
780 const uint8_t* sectAddress = 0;
781 if (MachOObj->is64Bit()) {
782 const MachOFormat::Section64 *Sect = getSection64(Sections[Rel.d.b]);
783 sectAddress += Sect->Address;
785 const MachOFormat::Section *Sect = getSection(Sections[Rel.d.b]);
786 sectAddress += Sect->Address;
788 const MachOFormat::RelocationEntry *RE = getRelocation(Rel);
790 unsigned Arch = getArch();
791 bool isScattered = (Arch != Triple::x86_64) &&
792 (RE->Word0 & macho::RF_Scattered);
793 uint64_t RelAddr = 0;
795 RelAddr = RE->Word0 & 0xFFFFFF;
799 Res = reinterpret_cast<uintptr_t>(sectAddress + RelAddr);
800 return object_error::success;
802 error_code MachOObjectFile::getRelocationOffset(DataRefImpl Rel,
803 uint64_t &Res) const {
804 const MachOFormat::RelocationEntry *RE = getRelocation(Rel);
806 unsigned Arch = getArch();
807 bool isScattered = (Arch != Triple::x86_64) &&
808 (RE->Word0 & macho::RF_Scattered);
810 Res = RE->Word0 & 0xFFFFFF;
813 return object_error::success;
815 error_code MachOObjectFile::getRelocationSymbol(DataRefImpl Rel,
816 SymbolRef &Res) const {
817 const MachOFormat::RelocationEntry *RE = getRelocation(Rel);
818 uint32_t SymbolIdx = RE->Word1 & 0xffffff;
819 bool isExtern = (RE->Word1 >> 27) & 1;
822 moveToNextSymbol(Sym);
824 for (unsigned i = 0; i < SymbolIdx; i++) {
826 moveToNextSymbol(Sym);
827 assert(Sym.d.a < MachOObj->getHeader().NumLoadCommands &&
828 "Relocation symbol index out of range!");
831 Res = SymbolRef(Sym, this);
832 return object_error::success;
834 error_code MachOObjectFile::getRelocationType(DataRefImpl Rel,
835 uint64_t &Res) const {
836 const MachOFormat::RelocationEntry *RE = getRelocation(Rel);
840 return object_error::success;
842 error_code MachOObjectFile::getRelocationTypeName(DataRefImpl Rel,
843 SmallVectorImpl<char> &Result) const {
844 // TODO: Support scattered relocations.
846 const MachOFormat::RelocationEntry *RE = getRelocation(Rel);
848 unsigned Arch = getArch();
849 bool isScattered = (Arch != Triple::x86_64) &&
850 (RE->Word0 & macho::RF_Scattered);
854 r_type = (RE->Word0 >> 24) & 0xF;
856 r_type = (RE->Word1 >> 28) & 0xF;
860 static const char *const Table[] = {
861 "GENERIC_RELOC_VANILLA",
862 "GENERIC_RELOC_PAIR",
863 "GENERIC_RELOC_SECTDIFF",
864 "GENERIC_RELOC_PB_LA_PTR",
865 "GENERIC_RELOC_LOCAL_SECTDIFF",
866 "GENERIC_RELOC_TLV" };
874 case Triple::x86_64: {
875 static const char *const Table[] = {
876 "X86_64_RELOC_UNSIGNED",
877 "X86_64_RELOC_SIGNED",
878 "X86_64_RELOC_BRANCH",
879 "X86_64_RELOC_GOT_LOAD",
881 "X86_64_RELOC_SUBTRACTOR",
882 "X86_64_RELOC_SIGNED_1",
883 "X86_64_RELOC_SIGNED_2",
884 "X86_64_RELOC_SIGNED_4",
885 "X86_64_RELOC_TLV" };
894 static const char *const Table[] = {
897 "ARM_RELOC_SECTDIFF",
898 "ARM_RELOC_LOCAL_SECTDIFF",
899 "ARM_RELOC_PB_LA_PTR",
901 "ARM_THUMB_RELOC_BR22",
902 "ARM_THUMB_32BIT_BRANCH",
904 "ARM_RELOC_HALF_SECTDIFF" };
913 static const char *const Table[] = {
922 "PPC_RELOC_SECTDIFF",
923 "PPC_RELOC_PB_LA_PTR",
924 "PPC_RELOC_HI16_SECTDIFF",
925 "PPC_RELOC_LO16_SECTDIFF",
926 "PPC_RELOC_HA16_SECTDIFF",
928 "PPC_RELOC_LO14_SECTDIFF",
929 "PPC_RELOC_LOCAL_SECTDIFF" };
934 case Triple::UnknownArch:
938 Result.append(res.begin(), res.end());
939 return object_error::success;
941 error_code MachOObjectFile::getRelocationAdditionalInfo(DataRefImpl Rel,
942 int64_t &Res) const {
943 const MachOFormat::RelocationEntry *RE = getRelocation(Rel);
944 bool isExtern = (RE->Word1 >> 27) & 1;
947 const uint8_t* sectAddress = base();
948 if (MachOObj->is64Bit()) {
949 const MachOFormat::Section64 *Sect = getSection64(Sections[Rel.d.b]);
950 sectAddress += Sect->Offset;
952 const MachOFormat::Section *Sect = getSection(Sections[Rel.d.b]);
953 sectAddress += Sect->Offset;
955 Res = reinterpret_cast<uintptr_t>(sectAddress);
957 return object_error::success;
960 // Helper to advance a section or symbol iterator multiple increments at a time.
962 error_code advance(T &it, size_t Val) {
971 void advanceTo(T &it, size_t Val) {
972 if (error_code ec = advance(it, Val))
973 report_fatal_error(ec.message());
976 void MachOObjectFile::printRelocationTargetName(
977 const MachOFormat::RelocationEntry *RE,
978 raw_string_ostream &fmt) const {
979 unsigned Arch = getArch();
980 bool isScattered = (Arch != Triple::x86_64) &&
981 (RE->Word0 & macho::RF_Scattered);
983 // Target of a scattered relocation is an address. In the interest of
984 // generating pretty output, scan through the symbol table looking for a
985 // symbol that aligns with that address. If we find one, print it.
986 // Otherwise, we just print the hex address of the target.
988 uint32_t Val = RE->Word1;
991 for (symbol_iterator SI = begin_symbols(), SE = end_symbols(); SI != SE;
993 if (ec) report_fatal_error(ec.message());
998 if ((ec = SI->getAddress(Addr)))
999 report_fatal_error(ec.message());
1000 if (Addr != Val) continue;
1001 if ((ec = SI->getName(Name)))
1002 report_fatal_error(ec.message());
1007 // If we couldn't find a symbol that this relocation refers to, try
1008 // to find a section beginning instead.
1009 for (section_iterator SI = begin_sections(), SE = end_sections(); SI != SE;
1011 if (ec) report_fatal_error(ec.message());
1016 if ((ec = SI->getAddress(Addr)))
1017 report_fatal_error(ec.message());
1018 if (Addr != Val) continue;
1019 if ((ec = SI->getName(Name)))
1020 report_fatal_error(ec.message());
1025 fmt << format("0x%x", Val);
1030 bool isExtern = (RE->Word1 >> 27) & 1;
1031 uint32_t Val = RE->Word1 & 0xFFFFFF;
1034 symbol_iterator SI = begin_symbols();
1038 section_iterator SI = begin_sections();
1046 error_code MachOObjectFile::getRelocationValueString(DataRefImpl Rel,
1047 SmallVectorImpl<char> &Result) const {
1048 const MachOFormat::RelocationEntry *RE = getRelocation(Rel);
1050 unsigned Arch = getArch();
1051 bool isScattered = (Arch != Triple::x86_64) &&
1052 (RE->Word0 & macho::RF_Scattered);
1055 raw_string_ostream fmt(fmtbuf);
1059 Type = (RE->Word0 >> 24) & 0xF;
1061 Type = (RE->Word1 >> 28) & 0xF;
1065 isPCRel = ((RE->Word0 >> 30) & 1);
1067 isPCRel = ((RE->Word1 >> 24) & 1);
1069 // Determine any addends that should be displayed with the relocation.
1070 // These require decoding the relocation type, which is triple-specific.
1072 // X86_64 has entirely custom relocation types.
1073 if (Arch == Triple::x86_64) {
1074 bool isPCRel = ((RE->Word1 >> 24) & 1);
1077 case macho::RIT_X86_64_GOTLoad: // X86_64_RELOC_GOT_LOAD
1078 case macho::RIT_X86_64_GOT: { // X86_64_RELOC_GOT
1079 printRelocationTargetName(RE, fmt);
1081 if (isPCRel) fmt << "PCREL";
1084 case macho::RIT_X86_64_Subtractor: { // X86_64_RELOC_SUBTRACTOR
1085 DataRefImpl RelNext = Rel;
1087 const MachOFormat::RelocationEntry *RENext = getRelocation(RelNext);
1089 // X86_64_SUBTRACTOR must be followed by a relocation of type
1090 // X86_64_RELOC_UNSIGNED.
1091 // NOTE: Scattered relocations don't exist on x86_64.
1092 unsigned RType = (RENext->Word1 >> 28) & 0xF;
1094 report_fatal_error("Expected X86_64_RELOC_UNSIGNED after "
1095 "X86_64_RELOC_SUBTRACTOR.");
1097 // The X86_64_RELOC_UNSIGNED contains the minuend symbol,
1098 // X86_64_SUBTRACTOR contains to the subtrahend.
1099 printRelocationTargetName(RENext, fmt);
1101 printRelocationTargetName(RE, fmt);
1104 case macho::RIT_X86_64_TLV:
1105 printRelocationTargetName(RE, fmt);
1107 if (isPCRel) fmt << "P";
1109 case macho::RIT_X86_64_Signed1: // X86_64_RELOC_SIGNED1
1110 printRelocationTargetName(RE, fmt);
1113 case macho::RIT_X86_64_Signed2: // X86_64_RELOC_SIGNED2
1114 printRelocationTargetName(RE, fmt);
1117 case macho::RIT_X86_64_Signed4: // X86_64_RELOC_SIGNED4
1118 printRelocationTargetName(RE, fmt);
1122 printRelocationTargetName(RE, fmt);
1125 // X86 and ARM share some relocation types in common.
1126 } else if (Arch == Triple::x86 || Arch == Triple::arm) {
1127 // Generic relocation types...
1129 case macho::RIT_Pair: // GENERIC_RELOC_PAIR - prints no info
1130 return object_error::success;
1131 case macho::RIT_Difference: { // GENERIC_RELOC_SECTDIFF
1132 DataRefImpl RelNext = Rel;
1134 const MachOFormat::RelocationEntry *RENext = getRelocation(RelNext);
1136 // X86 sect diff's must be followed by a relocation of type
1137 // GENERIC_RELOC_PAIR.
1138 bool isNextScattered = (Arch != Triple::x86_64) &&
1139 (RENext->Word0 & macho::RF_Scattered);
1141 if (isNextScattered)
1142 RType = (RENext->Word0 >> 24) & 0xF;
1144 RType = (RENext->Word1 >> 28) & 0xF;
1146 report_fatal_error("Expected GENERIC_RELOC_PAIR after "
1147 "GENERIC_RELOC_SECTDIFF.");
1149 printRelocationTargetName(RE, fmt);
1151 printRelocationTargetName(RENext, fmt);
1156 if (Arch == Triple::x86) {
1157 // All X86 relocations that need special printing were already
1158 // handled in the generic code.
1160 case macho::RIT_Generic_LocalDifference:{// GENERIC_RELOC_LOCAL_SECTDIFF
1161 DataRefImpl RelNext = Rel;
1163 const MachOFormat::RelocationEntry *RENext = getRelocation(RelNext);
1165 // X86 sect diff's must be followed by a relocation of type
1166 // GENERIC_RELOC_PAIR.
1167 bool isNextScattered = (Arch != Triple::x86_64) &&
1168 (RENext->Word0 & macho::RF_Scattered);
1170 if (isNextScattered)
1171 RType = (RENext->Word0 >> 24) & 0xF;
1173 RType = (RENext->Word1 >> 28) & 0xF;
1175 report_fatal_error("Expected GENERIC_RELOC_PAIR after "
1176 "GENERIC_RELOC_LOCAL_SECTDIFF.");
1178 printRelocationTargetName(RE, fmt);
1180 printRelocationTargetName(RENext, fmt);
1183 case macho::RIT_Generic_TLV: {
1184 printRelocationTargetName(RE, fmt);
1186 if (isPCRel) fmt << "P";
1190 printRelocationTargetName(RE, fmt);
1192 } else { // ARM-specific relocations
1194 case macho::RIT_ARM_Half: // ARM_RELOC_HALF
1195 case macho::RIT_ARM_HalfDifference: { // ARM_RELOC_HALF_SECTDIFF
1196 // Half relocations steal a bit from the length field to encode
1197 // whether this is an upper16 or a lower16 relocation.
1200 isUpper = (RE->Word0 >> 28) & 1;
1202 isUpper = (RE->Word1 >> 25) & 1;
1205 fmt << ":upper16:(";
1207 fmt << ":lower16:(";
1208 printRelocationTargetName(RE, fmt);
1210 DataRefImpl RelNext = Rel;
1212 const MachOFormat::RelocationEntry *RENext = getRelocation(RelNext);
1214 // ARM half relocs must be followed by a relocation of type
1216 bool isNextScattered = (Arch != Triple::x86_64) &&
1217 (RENext->Word0 & macho::RF_Scattered);
1219 if (isNextScattered)
1220 RType = (RENext->Word0 >> 24) & 0xF;
1222 RType = (RENext->Word1 >> 28) & 0xF;
1225 report_fatal_error("Expected ARM_RELOC_PAIR after "
1226 "GENERIC_RELOC_HALF");
1228 // NOTE: The half of the target virtual address is stashed in the
1229 // address field of the secondary relocation, but we can't reverse
1230 // engineer the constant offset from it without decoding the movw/movt
1231 // instruction to find the other half in its immediate field.
1233 // ARM_RELOC_HALF_SECTDIFF encodes the second section in the
1234 // symbol/section pointer of the follow-on relocation.
1235 if (Type == macho::RIT_ARM_HalfDifference) {
1237 printRelocationTargetName(RENext, fmt);
1244 printRelocationTargetName(RE, fmt);
1249 printRelocationTargetName(RE, fmt);
1252 Result.append(fmtbuf.begin(), fmtbuf.end());
1253 return object_error::success;
1256 error_code MachOObjectFile::getRelocationHidden(DataRefImpl Rel,
1257 bool &Result) const {
1258 const MachOFormat::RelocationEntry *RE = getRelocation(Rel);
1260 unsigned Arch = getArch();
1261 bool isScattered = (Arch != Triple::x86_64) &&
1262 (RE->Word0 & macho::RF_Scattered);
1265 Type = (RE->Word0 >> 24) & 0xF;
1267 Type = (RE->Word1 >> 28) & 0xF;
1271 // On arches that use the generic relocations, GENERIC_RELOC_PAIR
1272 // is always hidden.
1273 if (Arch == Triple::x86 || Arch == Triple::arm) {
1274 if (Type == macho::RIT_Pair) Result = true;
1275 } else if (Arch == Triple::x86_64) {
1276 // On x86_64, X86_64_RELOC_UNSIGNED is hidden only when it follows
1277 // an X864_64_RELOC_SUBTRACTOR.
1278 if (Type == macho::RIT_X86_64_Unsigned && Rel.d.a > 0) {
1279 DataRefImpl RelPrev = Rel;
1281 const MachOFormat::RelocationEntry *REPrev = getRelocation(RelPrev);
1283 unsigned PrevType = (REPrev->Word1 >> 28) & 0xF;
1285 if (PrevType == macho::RIT_X86_64_Subtractor) Result = true;
1289 return object_error::success;
1292 error_code MachOObjectFile::getLibraryNext(DataRefImpl LibData,
1293 LibraryRef &Res) const {
1294 report_fatal_error("Needed libraries unimplemented in MachOObjectFile");
1297 error_code MachOObjectFile::getLibraryPath(DataRefImpl LibData,
1298 StringRef &Res) const {
1299 report_fatal_error("Needed libraries unimplemented in MachOObjectFile");
1303 /*===-- Miscellaneous -----------------------------------------------------===*/
1305 uint8_t MachOObjectFile::getBytesInAddress() const {
1306 return MachOObj->is64Bit() ? 8 : 4;
1309 StringRef MachOObjectFile::getFileFormatName() const {
1310 if (!MachOObj->is64Bit()) {
1311 switch (MachOObj->getHeader().CPUType) {
1312 case llvm::MachO::CPUTypeI386:
1313 return "Mach-O 32-bit i386";
1314 case llvm::MachO::CPUTypeARM:
1315 return "Mach-O arm";
1316 case llvm::MachO::CPUTypePowerPC:
1317 return "Mach-O 32-bit ppc";
1319 assert((MachOObj->getHeader().CPUType & llvm::MachO::CPUArchABI64) == 0 &&
1320 "64-bit object file when we're not 64-bit?");
1321 return "Mach-O 32-bit unknown";
1325 // Make sure the cpu type has the correct mask.
1326 assert((MachOObj->getHeader().CPUType & llvm::MachO::CPUArchABI64)
1327 == llvm::MachO::CPUArchABI64 &&
1328 "32-bit object file when we're 64-bit?");
1330 switch (MachOObj->getHeader().CPUType) {
1331 case llvm::MachO::CPUTypeX86_64:
1332 return "Mach-O 64-bit x86-64";
1333 case llvm::MachO::CPUTypePowerPC64:
1334 return "Mach-O 64-bit ppc64";
1336 return "Mach-O 64-bit unknown";
1340 unsigned MachOObjectFile::getArch() const {
1341 switch (MachOObj->getHeader().CPUType) {
1342 case llvm::MachO::CPUTypeI386:
1344 case llvm::MachO::CPUTypeX86_64:
1345 return Triple::x86_64;
1346 case llvm::MachO::CPUTypeARM:
1348 case llvm::MachO::CPUTypePowerPC:
1350 case llvm::MachO::CPUTypePowerPC64:
1351 return Triple::ppc64;
1353 return Triple::UnknownArch;
1357 } // end namespace object
1358 } // end namespace llvm