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/STLExtras.h"
17 #include "llvm/ADT/StringSwitch.h"
18 #include "llvm/ADT/Triple.h"
19 #include "llvm/Support/DataExtractor.h"
20 #include "llvm/Support/Debug.h"
21 #include "llvm/Support/Format.h"
22 #include "llvm/Support/Host.h"
23 #include "llvm/Support/LEB128.h"
24 #include "llvm/Support/MachO.h"
25 #include "llvm/Support/MemoryBuffer.h"
26 #include "llvm/Support/raw_ostream.h"
32 using namespace object;
41 // FIXME: Replace all uses of this function with getStructOrErr.
43 static T getStruct(const MachOObjectFile *O, const char *P) {
44 // Don't read before the beginning or past the end of the file
45 if (P < O->getData().begin() || P + sizeof(T) > O->getData().end())
46 report_fatal_error("Malformed MachO file.");
49 memcpy(&Cmd, P, sizeof(T));
50 if (O->isLittleEndian() != sys::IsLittleEndianHost)
51 MachO::swapStruct(Cmd);
56 static ErrorOr<T> getStructOrErr(const MachOObjectFile *O, const char *P) {
57 // Don't read before the beginning or past the end of the file
58 if (P < O->getData().begin() || P + sizeof(T) > O->getData().end())
59 return object_error::parse_failed;
62 memcpy(&Cmd, P, sizeof(T));
63 if (O->isLittleEndian() != sys::IsLittleEndianHost)
64 MachO::swapStruct(Cmd);
69 getSectionPtr(const MachOObjectFile *O, MachOObjectFile::LoadCommandInfo L,
71 uintptr_t CommandAddr = reinterpret_cast<uintptr_t>(L.Ptr);
73 bool Is64 = O->is64Bit();
74 unsigned SegmentLoadSize = Is64 ? sizeof(MachO::segment_command_64) :
75 sizeof(MachO::segment_command);
76 unsigned SectionSize = Is64 ? sizeof(MachO::section_64) :
77 sizeof(MachO::section);
79 uintptr_t SectionAddr = CommandAddr + SegmentLoadSize + Sec * SectionSize;
80 return reinterpret_cast<const char*>(SectionAddr);
83 static const char *getPtr(const MachOObjectFile *O, size_t Offset) {
84 return O->getData().substr(Offset, 1).data();
87 static MachO::nlist_base
88 getSymbolTableEntryBase(const MachOObjectFile *O, DataRefImpl DRI) {
89 const char *P = reinterpret_cast<const char *>(DRI.p);
90 return getStruct<MachO::nlist_base>(O, P);
93 static StringRef parseSegmentOrSectionName(const char *P) {
97 // Not null terminated, so this is a 16 char string.
98 return StringRef(P, 16);
101 // Helper to advance a section or symbol iterator multiple increments at a time.
103 static void advance(T &it, size_t Val) {
108 static unsigned getCPUType(const MachOObjectFile *O) {
109 return O->getHeader().cputype;
113 getPlainRelocationAddress(const MachO::any_relocation_info &RE) {
118 getScatteredRelocationAddress(const MachO::any_relocation_info &RE) {
119 return RE.r_word0 & 0xffffff;
122 static bool getPlainRelocationPCRel(const MachOObjectFile *O,
123 const MachO::any_relocation_info &RE) {
124 if (O->isLittleEndian())
125 return (RE.r_word1 >> 24) & 1;
126 return (RE.r_word1 >> 7) & 1;
130 getScatteredRelocationPCRel(const MachOObjectFile *O,
131 const MachO::any_relocation_info &RE) {
132 return (RE.r_word0 >> 30) & 1;
135 static unsigned getPlainRelocationLength(const MachOObjectFile *O,
136 const MachO::any_relocation_info &RE) {
137 if (O->isLittleEndian())
138 return (RE.r_word1 >> 25) & 3;
139 return (RE.r_word1 >> 5) & 3;
143 getScatteredRelocationLength(const MachO::any_relocation_info &RE) {
144 return (RE.r_word0 >> 28) & 3;
147 static unsigned getPlainRelocationType(const MachOObjectFile *O,
148 const MachO::any_relocation_info &RE) {
149 if (O->isLittleEndian())
150 return RE.r_word1 >> 28;
151 return RE.r_word1 & 0xf;
154 static uint32_t getSectionFlags(const MachOObjectFile *O,
157 MachO::section_64 Sect = O->getSection64(Sec);
160 MachO::section Sect = O->getSection(Sec);
164 static ErrorOr<MachOObjectFile::LoadCommandInfo>
165 getLoadCommandInfo(const MachOObjectFile *Obj, const char *Ptr) {
166 auto CmdOrErr = getStructOrErr<MachO::load_command>(Obj, Ptr);
168 return CmdOrErr.getError();
169 if (CmdOrErr->cmdsize < 8)
170 return object_error::macho_small_load_command;
171 MachOObjectFile::LoadCommandInfo Load;
173 Load.C = CmdOrErr.get();
177 static ErrorOr<MachOObjectFile::LoadCommandInfo>
178 getFirstLoadCommandInfo(const MachOObjectFile *Obj) {
179 unsigned HeaderSize = Obj->is64Bit() ? sizeof(MachO::mach_header_64)
180 : sizeof(MachO::mach_header);
181 return getLoadCommandInfo(Obj, getPtr(Obj, HeaderSize));
184 static ErrorOr<MachOObjectFile::LoadCommandInfo>
185 getNextLoadCommandInfo(const MachOObjectFile *Obj,
186 const MachOObjectFile::LoadCommandInfo &L) {
187 return getLoadCommandInfo(Obj, L.Ptr + L.C.cmdsize);
190 template <typename T>
191 static void parseHeader(const MachOObjectFile *Obj, T &Header,
192 std::error_code &EC) {
193 auto HeaderOrErr = getStructOrErr<T>(Obj, getPtr(Obj, 0));
195 Header = HeaderOrErr.get();
197 EC = HeaderOrErr.getError();
200 // Parses LC_SEGMENT or LC_SEGMENT_64 load command, adds addresses of all
201 // sections to \param Sections, and optionally sets
202 // \param IsPageZeroSegment to true.
203 template <typename SegmentCmd>
204 static std::error_code parseSegmentLoadCommand(
205 const MachOObjectFile *Obj, const MachOObjectFile::LoadCommandInfo &Load,
206 SmallVectorImpl<const char *> &Sections, bool &IsPageZeroSegment) {
207 const unsigned SegmentLoadSize = sizeof(SegmentCmd);
208 if (Load.C.cmdsize < SegmentLoadSize)
209 return object_error::macho_load_segment_too_small;
210 auto SegOrErr = getStructOrErr<SegmentCmd>(Obj, Load.Ptr);
212 return SegOrErr.getError();
213 SegmentCmd S = SegOrErr.get();
214 const unsigned SectionSize =
215 Obj->is64Bit() ? sizeof(MachO::section_64) : sizeof(MachO::section);
216 if (S.nsects > std::numeric_limits<uint32_t>::max() / SectionSize ||
217 S.nsects * SectionSize > Load.C.cmdsize - SegmentLoadSize)
218 return object_error::macho_load_segment_too_many_sections;
219 for (unsigned J = 0; J < S.nsects; ++J) {
220 const char *Sec = getSectionPtr(Obj, Load, J);
221 Sections.push_back(Sec);
223 IsPageZeroSegment |= StringRef("__PAGEZERO").equals(S.segname);
224 return std::error_code();
227 MachOObjectFile::MachOObjectFile(MemoryBufferRef Object, bool IsLittleEndian,
228 bool Is64bits, std::error_code &EC)
229 : ObjectFile(getMachOType(IsLittleEndian, Is64bits), Object),
230 SymtabLoadCmd(nullptr), DysymtabLoadCmd(nullptr),
231 DataInCodeLoadCmd(nullptr), LinkOptHintsLoadCmd(nullptr),
232 DyldInfoLoadCmd(nullptr), UuidLoadCmd(nullptr),
233 HasPageZeroSegment(false) {
235 parseHeader(this, Header64, EC);
237 parseHeader(this, Header, EC);
241 uint32_t LoadCommandCount = getHeader().ncmds;
242 if (LoadCommandCount == 0)
245 auto LoadOrErr = getFirstLoadCommandInfo(this);
247 EC = LoadOrErr.getError();
250 LoadCommandInfo Load = LoadOrErr.get();
251 for (unsigned I = 0; I < LoadCommandCount; ++I) {
252 LoadCommands.push_back(Load);
253 if (Load.C.cmd == MachO::LC_SYMTAB) {
254 // Multiple symbol tables
256 EC = object_error::parse_failed;
259 SymtabLoadCmd = Load.Ptr;
260 } else if (Load.C.cmd == MachO::LC_DYSYMTAB) {
261 // Multiple dynamic symbol tables
262 if (DysymtabLoadCmd) {
263 EC = object_error::parse_failed;
266 DysymtabLoadCmd = Load.Ptr;
267 } else if (Load.C.cmd == MachO::LC_DATA_IN_CODE) {
268 // Multiple data in code tables
269 if (DataInCodeLoadCmd) {
270 EC = object_error::parse_failed;
273 DataInCodeLoadCmd = Load.Ptr;
274 } else if (Load.C.cmd == MachO::LC_LINKER_OPTIMIZATION_HINT) {
275 // Multiple linker optimization hint tables
276 if (LinkOptHintsLoadCmd) {
277 EC = object_error::parse_failed;
280 LinkOptHintsLoadCmd = Load.Ptr;
281 } else if (Load.C.cmd == MachO::LC_DYLD_INFO ||
282 Load.C.cmd == MachO::LC_DYLD_INFO_ONLY) {
283 // Multiple dyldinfo load commands
284 if (DyldInfoLoadCmd) {
285 EC = object_error::parse_failed;
288 DyldInfoLoadCmd = Load.Ptr;
289 } else if (Load.C.cmd == MachO::LC_UUID) {
290 // Multiple UUID load commands
292 EC = object_error::parse_failed;
295 UuidLoadCmd = Load.Ptr;
296 } else if (Load.C.cmd == MachO::LC_SEGMENT_64) {
297 if ((EC = parseSegmentLoadCommand<MachO::segment_command_64>(
298 this, Load, Sections, HasPageZeroSegment)))
300 } else if (Load.C.cmd == MachO::LC_SEGMENT) {
301 if ((EC = parseSegmentLoadCommand<MachO::segment_command>(
302 this, Load, Sections, HasPageZeroSegment)))
304 } else if (Load.C.cmd == MachO::LC_LOAD_DYLIB ||
305 Load.C.cmd == MachO::LC_LOAD_WEAK_DYLIB ||
306 Load.C.cmd == MachO::LC_LAZY_LOAD_DYLIB ||
307 Load.C.cmd == MachO::LC_REEXPORT_DYLIB ||
308 Load.C.cmd == MachO::LC_LOAD_UPWARD_DYLIB) {
309 Libraries.push_back(Load.Ptr);
311 if (I < LoadCommandCount - 1) {
312 auto LoadOrErr = getNextLoadCommandInfo(this, Load);
314 EC = LoadOrErr.getError();
317 Load = LoadOrErr.get();
320 assert(LoadCommands.size() == LoadCommandCount);
323 void MachOObjectFile::moveSymbolNext(DataRefImpl &Symb) const {
324 unsigned SymbolTableEntrySize = is64Bit() ?
325 sizeof(MachO::nlist_64) :
326 sizeof(MachO::nlist);
327 Symb.p += SymbolTableEntrySize;
330 ErrorOr<StringRef> MachOObjectFile::getSymbolName(DataRefImpl Symb) const {
331 StringRef StringTable = getStringTableData();
332 MachO::nlist_base Entry = getSymbolTableEntryBase(this, Symb);
333 const char *Start = &StringTable.data()[Entry.n_strx];
334 if (Start < getData().begin() || Start >= getData().end())
336 "Symbol name entry points before beginning or past end of file.");
337 return StringRef(Start);
340 unsigned MachOObjectFile::getSectionType(SectionRef Sec) const {
341 DataRefImpl DRI = Sec.getRawDataRefImpl();
342 uint32_t Flags = getSectionFlags(this, DRI);
343 return Flags & MachO::SECTION_TYPE;
346 uint64_t MachOObjectFile::getNValue(DataRefImpl Sym) const {
348 MachO::nlist_64 Entry = getSymbol64TableEntry(Sym);
349 return Entry.n_value;
351 MachO::nlist Entry = getSymbolTableEntry(Sym);
352 return Entry.n_value;
355 // getIndirectName() returns the name of the alias'ed symbol who's string table
356 // index is in the n_value field.
357 std::error_code MachOObjectFile::getIndirectName(DataRefImpl Symb,
358 StringRef &Res) const {
359 StringRef StringTable = getStringTableData();
360 MachO::nlist_base Entry = getSymbolTableEntryBase(this, Symb);
361 if ((Entry.n_type & MachO::N_TYPE) != MachO::N_INDR)
362 return object_error::parse_failed;
363 uint64_t NValue = getNValue(Symb);
364 if (NValue >= StringTable.size())
365 return object_error::parse_failed;
366 const char *Start = &StringTable.data()[NValue];
367 Res = StringRef(Start);
368 return std::error_code();
371 uint64_t MachOObjectFile::getSymbolValueImpl(DataRefImpl Sym) const {
372 return getNValue(Sym);
375 ErrorOr<uint64_t> MachOObjectFile::getSymbolAddress(DataRefImpl Sym) const {
376 return getSymbolValue(Sym);
379 uint32_t MachOObjectFile::getSymbolAlignment(DataRefImpl DRI) const {
380 uint32_t flags = getSymbolFlags(DRI);
381 if (flags & SymbolRef::SF_Common) {
382 MachO::nlist_base Entry = getSymbolTableEntryBase(this, DRI);
383 return 1 << MachO::GET_COMM_ALIGN(Entry.n_desc);
388 uint64_t MachOObjectFile::getCommonSymbolSizeImpl(DataRefImpl DRI) const {
389 return getNValue(DRI);
392 SymbolRef::Type MachOObjectFile::getSymbolType(DataRefImpl Symb) const {
393 MachO::nlist_base Entry = getSymbolTableEntryBase(this, Symb);
394 uint8_t n_type = Entry.n_type;
396 // If this is a STAB debugging symbol, we can do nothing more.
397 if (n_type & MachO::N_STAB)
398 return SymbolRef::ST_Debug;
400 switch (n_type & MachO::N_TYPE) {
402 return SymbolRef::ST_Unknown;
404 section_iterator Sec = *getSymbolSection(Symb);
405 if (Sec->isData() || Sec->isBSS())
406 return SymbolRef::ST_Data;
407 return SymbolRef::ST_Function;
409 return SymbolRef::ST_Other;
412 uint32_t MachOObjectFile::getSymbolFlags(DataRefImpl DRI) const {
413 MachO::nlist_base Entry = getSymbolTableEntryBase(this, DRI);
415 uint8_t MachOType = Entry.n_type;
416 uint16_t MachOFlags = Entry.n_desc;
418 uint32_t Result = SymbolRef::SF_None;
420 if ((MachOType & MachO::N_TYPE) == MachO::N_INDR)
421 Result |= SymbolRef::SF_Indirect;
423 if (MachOType & MachO::N_STAB)
424 Result |= SymbolRef::SF_FormatSpecific;
426 if (MachOType & MachO::N_EXT) {
427 Result |= SymbolRef::SF_Global;
428 if ((MachOType & MachO::N_TYPE) == MachO::N_UNDF) {
430 Result |= SymbolRef::SF_Common;
432 Result |= SymbolRef::SF_Undefined;
435 if (!(MachOType & MachO::N_PEXT))
436 Result |= SymbolRef::SF_Exported;
439 if (MachOFlags & (MachO::N_WEAK_REF | MachO::N_WEAK_DEF))
440 Result |= SymbolRef::SF_Weak;
442 if (MachOFlags & (MachO::N_ARM_THUMB_DEF))
443 Result |= SymbolRef::SF_Thumb;
445 if ((MachOType & MachO::N_TYPE) == MachO::N_ABS)
446 Result |= SymbolRef::SF_Absolute;
451 ErrorOr<section_iterator>
452 MachOObjectFile::getSymbolSection(DataRefImpl Symb) const {
453 MachO::nlist_base Entry = getSymbolTableEntryBase(this, Symb);
454 uint8_t index = Entry.n_sect;
457 return section_end();
460 if (DRI.d.a >= Sections.size())
461 report_fatal_error("getSymbolSection: Invalid section index.");
462 return section_iterator(SectionRef(DRI, this));
465 unsigned MachOObjectFile::getSymbolSectionID(SymbolRef Sym) const {
466 MachO::nlist_base Entry =
467 getSymbolTableEntryBase(this, Sym.getRawDataRefImpl());
468 return Entry.n_sect - 1;
471 void MachOObjectFile::moveSectionNext(DataRefImpl &Sec) const {
475 std::error_code MachOObjectFile::getSectionName(DataRefImpl Sec,
476 StringRef &Result) const {
477 ArrayRef<char> Raw = getSectionRawName(Sec);
478 Result = parseSegmentOrSectionName(Raw.data());
479 return std::error_code();
482 uint64_t MachOObjectFile::getSectionAddress(DataRefImpl Sec) const {
484 return getSection64(Sec).addr;
485 return getSection(Sec).addr;
488 uint64_t MachOObjectFile::getSectionSize(DataRefImpl Sec) const {
489 // In the case if a malformed Mach-O file where the section offset is past
490 // the end of the file or some part of the section size is past the end of
491 // the file return a size of zero or a size that covers the rest of the file
492 // but does not extend past the end of the file.
493 uint32_t SectOffset, SectType;
497 MachO::section_64 Sect = getSection64(Sec);
498 SectOffset = Sect.offset;
499 SectSize = Sect.size;
500 SectType = Sect.flags & MachO::SECTION_TYPE;
502 MachO::section Sect = getSection(Sec);
503 SectOffset = Sect.offset;
504 SectSize = Sect.size;
505 SectType = Sect.flags & MachO::SECTION_TYPE;
507 if (SectType == MachO::S_ZEROFILL || SectType == MachO::S_GB_ZEROFILL)
509 uint64_t FileSize = getData().size();
510 if (SectOffset > FileSize)
512 if (FileSize - SectOffset < SectSize)
513 return FileSize - SectOffset;
517 std::error_code MachOObjectFile::getSectionContents(DataRefImpl Sec,
518 StringRef &Res) const {
523 MachO::section_64 Sect = getSection64(Sec);
524 Offset = Sect.offset;
527 MachO::section Sect = getSection(Sec);
528 Offset = Sect.offset;
532 Res = this->getData().substr(Offset, Size);
533 return std::error_code();
536 uint64_t MachOObjectFile::getSectionAlignment(DataRefImpl Sec) const {
539 MachO::section_64 Sect = getSection64(Sec);
542 MachO::section Sect = getSection(Sec);
546 return uint64_t(1) << Align;
549 bool MachOObjectFile::isSectionText(DataRefImpl Sec) const {
550 uint32_t Flags = getSectionFlags(this, Sec);
551 return Flags & MachO::S_ATTR_PURE_INSTRUCTIONS;
554 bool MachOObjectFile::isSectionData(DataRefImpl Sec) const {
555 uint32_t Flags = getSectionFlags(this, Sec);
556 unsigned SectionType = Flags & MachO::SECTION_TYPE;
557 return !(Flags & MachO::S_ATTR_PURE_INSTRUCTIONS) &&
558 !(SectionType == MachO::S_ZEROFILL ||
559 SectionType == MachO::S_GB_ZEROFILL);
562 bool MachOObjectFile::isSectionBSS(DataRefImpl Sec) const {
563 uint32_t Flags = getSectionFlags(this, Sec);
564 unsigned SectionType = Flags & MachO::SECTION_TYPE;
565 return !(Flags & MachO::S_ATTR_PURE_INSTRUCTIONS) &&
566 (SectionType == MachO::S_ZEROFILL ||
567 SectionType == MachO::S_GB_ZEROFILL);
570 unsigned MachOObjectFile::getSectionID(SectionRef Sec) const {
571 return Sec.getRawDataRefImpl().d.a;
574 bool MachOObjectFile::isSectionVirtual(DataRefImpl Sec) const {
575 // FIXME: Unimplemented.
579 relocation_iterator MachOObjectFile::section_rel_begin(DataRefImpl Sec) const {
583 return relocation_iterator(RelocationRef(Ret, this));
587 MachOObjectFile::section_rel_end(DataRefImpl Sec) const {
590 MachO::section_64 Sect = getSection64(Sec);
593 MachO::section Sect = getSection(Sec);
600 return relocation_iterator(RelocationRef(Ret, this));
603 void MachOObjectFile::moveRelocationNext(DataRefImpl &Rel) const {
607 uint64_t MachOObjectFile::getRelocationOffset(DataRefImpl Rel) const {
608 assert(getHeader().filetype == MachO::MH_OBJECT &&
609 "Only implemented for MH_OBJECT");
610 MachO::any_relocation_info RE = getRelocation(Rel);
611 return getAnyRelocationAddress(RE);
615 MachOObjectFile::getRelocationSymbol(DataRefImpl Rel) const {
616 MachO::any_relocation_info RE = getRelocation(Rel);
617 if (isRelocationScattered(RE))
620 uint32_t SymbolIdx = getPlainRelocationSymbolNum(RE);
621 bool isExtern = getPlainRelocationExternal(RE);
625 MachO::symtab_command S = getSymtabLoadCommand();
626 unsigned SymbolTableEntrySize = is64Bit() ?
627 sizeof(MachO::nlist_64) :
628 sizeof(MachO::nlist);
629 uint64_t Offset = S.symoff + SymbolIdx * SymbolTableEntrySize;
631 Sym.p = reinterpret_cast<uintptr_t>(getPtr(this, Offset));
632 return symbol_iterator(SymbolRef(Sym, this));
636 MachOObjectFile::getRelocationSection(DataRefImpl Rel) const {
637 return section_iterator(getAnyRelocationSection(getRelocation(Rel)));
640 uint64_t MachOObjectFile::getRelocationType(DataRefImpl Rel) const {
641 MachO::any_relocation_info RE = getRelocation(Rel);
642 return getAnyRelocationType(RE);
645 void MachOObjectFile::getRelocationTypeName(
646 DataRefImpl Rel, SmallVectorImpl<char> &Result) const {
648 uint64_t RType = getRelocationType(Rel);
650 unsigned Arch = this->getArch();
654 static const char *const Table[] = {
655 "GENERIC_RELOC_VANILLA",
656 "GENERIC_RELOC_PAIR",
657 "GENERIC_RELOC_SECTDIFF",
658 "GENERIC_RELOC_PB_LA_PTR",
659 "GENERIC_RELOC_LOCAL_SECTDIFF",
660 "GENERIC_RELOC_TLV" };
668 case Triple::x86_64: {
669 static const char *const Table[] = {
670 "X86_64_RELOC_UNSIGNED",
671 "X86_64_RELOC_SIGNED",
672 "X86_64_RELOC_BRANCH",
673 "X86_64_RELOC_GOT_LOAD",
675 "X86_64_RELOC_SUBTRACTOR",
676 "X86_64_RELOC_SIGNED_1",
677 "X86_64_RELOC_SIGNED_2",
678 "X86_64_RELOC_SIGNED_4",
679 "X86_64_RELOC_TLV" };
688 static const char *const Table[] = {
691 "ARM_RELOC_SECTDIFF",
692 "ARM_RELOC_LOCAL_SECTDIFF",
693 "ARM_RELOC_PB_LA_PTR",
695 "ARM_THUMB_RELOC_BR22",
696 "ARM_THUMB_32BIT_BRANCH",
698 "ARM_RELOC_HALF_SECTDIFF" };
706 case Triple::aarch64: {
707 static const char *const Table[] = {
708 "ARM64_RELOC_UNSIGNED", "ARM64_RELOC_SUBTRACTOR",
709 "ARM64_RELOC_BRANCH26", "ARM64_RELOC_PAGE21",
710 "ARM64_RELOC_PAGEOFF12", "ARM64_RELOC_GOT_LOAD_PAGE21",
711 "ARM64_RELOC_GOT_LOAD_PAGEOFF12", "ARM64_RELOC_POINTER_TO_GOT",
712 "ARM64_RELOC_TLVP_LOAD_PAGE21", "ARM64_RELOC_TLVP_LOAD_PAGEOFF12",
716 if (RType >= array_lengthof(Table))
723 static const char *const Table[] = {
732 "PPC_RELOC_SECTDIFF",
733 "PPC_RELOC_PB_LA_PTR",
734 "PPC_RELOC_HI16_SECTDIFF",
735 "PPC_RELOC_LO16_SECTDIFF",
736 "PPC_RELOC_HA16_SECTDIFF",
738 "PPC_RELOC_LO14_SECTDIFF",
739 "PPC_RELOC_LOCAL_SECTDIFF" };
747 case Triple::UnknownArch:
751 Result.append(res.begin(), res.end());
754 uint8_t MachOObjectFile::getRelocationLength(DataRefImpl Rel) const {
755 MachO::any_relocation_info RE = getRelocation(Rel);
756 return getAnyRelocationLength(RE);
760 // guessLibraryShortName() is passed a name of a dynamic library and returns a
761 // guess on what the short name is. Then name is returned as a substring of the
762 // StringRef Name passed in. The name of the dynamic library is recognized as
763 // a framework if it has one of the two following forms:
764 // Foo.framework/Versions/A/Foo
766 // Where A and Foo can be any string. And may contain a trailing suffix
767 // starting with an underbar. If the Name is recognized as a framework then
768 // isFramework is set to true else it is set to false. If the Name has a
769 // suffix then Suffix is set to the substring in Name that contains the suffix
770 // else it is set to a NULL StringRef.
772 // The Name of the dynamic library is recognized as a library name if it has
773 // one of the two following forms:
776 // The library may have a suffix trailing the name Foo of the form:
777 // libFoo_profile.A.dylib
778 // libFoo_profile.dylib
780 // The Name of the dynamic library is also recognized as a library name if it
781 // has the following form:
784 // If the Name of the dynamic library is none of the forms above then a NULL
785 // StringRef is returned.
787 StringRef MachOObjectFile::guessLibraryShortName(StringRef Name,
790 StringRef Foo, F, DotFramework, V, Dylib, Lib, Dot, Qtx;
791 size_t a, b, c, d, Idx;
794 Suffix = StringRef();
796 // Pull off the last component and make Foo point to it
798 if (a == Name.npos || a == 0)
800 Foo = Name.slice(a+1, Name.npos);
802 // Look for a suffix starting with a '_'
803 Idx = Foo.rfind('_');
804 if (Idx != Foo.npos && Foo.size() >= 2) {
805 Suffix = Foo.slice(Idx, Foo.npos);
806 Foo = Foo.slice(0, Idx);
809 // First look for the form Foo.framework/Foo
810 b = Name.rfind('/', a);
815 F = Name.slice(Idx, Idx + Foo.size());
816 DotFramework = Name.slice(Idx + Foo.size(),
817 Idx + Foo.size() + sizeof(".framework/")-1);
818 if (F == Foo && DotFramework == ".framework/") {
823 // Next look for the form Foo.framework/Versions/A/Foo
826 c = Name.rfind('/', b);
827 if (c == Name.npos || c == 0)
829 V = Name.slice(c+1, Name.npos);
830 if (!V.startswith("Versions/"))
832 d = Name.rfind('/', c);
837 F = Name.slice(Idx, Idx + Foo.size());
838 DotFramework = Name.slice(Idx + Foo.size(),
839 Idx + Foo.size() + sizeof(".framework/")-1);
840 if (F == Foo && DotFramework == ".framework/") {
846 // pull off the suffix after the "." and make a point to it
848 if (a == Name.npos || a == 0)
850 Dylib = Name.slice(a, Name.npos);
851 if (Dylib != ".dylib")
854 // First pull off the version letter for the form Foo.A.dylib if any.
856 Dot = Name.slice(a-2, a-1);
861 b = Name.rfind('/', a);
866 // ignore any suffix after an underbar like Foo_profile.A.dylib
867 Idx = Name.find('_', b);
868 if (Idx != Name.npos && Idx != b) {
869 Lib = Name.slice(b, Idx);
870 Suffix = Name.slice(Idx, a);
873 Lib = Name.slice(b, a);
874 // There are incorrect library names of the form:
875 // libATS.A_profile.dylib so check for these.
876 if (Lib.size() >= 3) {
877 Dot = Lib.slice(Lib.size()-2, Lib.size()-1);
879 Lib = Lib.slice(0, Lib.size()-2);
884 Qtx = Name.slice(a, Name.npos);
887 b = Name.rfind('/', a);
889 Lib = Name.slice(0, a);
891 Lib = Name.slice(b+1, a);
892 // There are library names of the form: QT.A.qtx so check for these.
893 if (Lib.size() >= 3) {
894 Dot = Lib.slice(Lib.size()-2, Lib.size()-1);
896 Lib = Lib.slice(0, Lib.size()-2);
901 // getLibraryShortNameByIndex() is used to get the short name of the library
902 // for an undefined symbol in a linked Mach-O binary that was linked with the
903 // normal two-level namespace default (that is MH_TWOLEVEL in the header).
904 // It is passed the index (0 - based) of the library as translated from
905 // GET_LIBRARY_ORDINAL (1 - based).
906 std::error_code MachOObjectFile::getLibraryShortNameByIndex(unsigned Index,
907 StringRef &Res) const {
908 if (Index >= Libraries.size())
909 return object_error::parse_failed;
911 // If the cache of LibrariesShortNames is not built up do that first for
912 // all the Libraries.
913 if (LibrariesShortNames.size() == 0) {
914 for (unsigned i = 0; i < Libraries.size(); i++) {
915 MachO::dylib_command D =
916 getStruct<MachO::dylib_command>(this, Libraries[i]);
917 if (D.dylib.name >= D.cmdsize)
918 return object_error::parse_failed;
919 const char *P = (const char *)(Libraries[i]) + D.dylib.name;
920 StringRef Name = StringRef(P);
921 if (D.dylib.name+Name.size() >= D.cmdsize)
922 return object_error::parse_failed;
925 StringRef shortName = guessLibraryShortName(Name, isFramework, Suffix);
926 if (shortName.empty())
927 LibrariesShortNames.push_back(Name);
929 LibrariesShortNames.push_back(shortName);
933 Res = LibrariesShortNames[Index];
934 return std::error_code();
938 MachOObjectFile::getRelocationRelocatedSection(relocation_iterator Rel) const {
940 Sec.d.a = Rel->getRawDataRefImpl().d.a;
941 return section_iterator(SectionRef(Sec, this));
944 basic_symbol_iterator MachOObjectFile::symbol_begin_impl() const {
945 return getSymbolByIndex(0);
948 basic_symbol_iterator MachOObjectFile::symbol_end_impl() const {
951 return basic_symbol_iterator(SymbolRef(DRI, this));
953 MachO::symtab_command Symtab = getSymtabLoadCommand();
954 unsigned SymbolTableEntrySize = is64Bit() ?
955 sizeof(MachO::nlist_64) :
956 sizeof(MachO::nlist);
957 unsigned Offset = Symtab.symoff +
958 Symtab.nsyms * SymbolTableEntrySize;
959 DRI.p = reinterpret_cast<uintptr_t>(getPtr(this, Offset));
960 return basic_symbol_iterator(SymbolRef(DRI, this));
963 basic_symbol_iterator MachOObjectFile::getSymbolByIndex(unsigned Index) const {
966 return basic_symbol_iterator(SymbolRef(DRI, this));
968 MachO::symtab_command Symtab = getSymtabLoadCommand();
969 if (Index >= Symtab.nsyms)
970 report_fatal_error("Requested symbol index is out of range.");
971 unsigned SymbolTableEntrySize =
972 is64Bit() ? sizeof(MachO::nlist_64) : sizeof(MachO::nlist);
973 DRI.p = reinterpret_cast<uintptr_t>(getPtr(this, Symtab.symoff));
974 DRI.p += Index * SymbolTableEntrySize;
975 return basic_symbol_iterator(SymbolRef(DRI, this));
978 section_iterator MachOObjectFile::section_begin() const {
980 return section_iterator(SectionRef(DRI, this));
983 section_iterator MachOObjectFile::section_end() const {
985 DRI.d.a = Sections.size();
986 return section_iterator(SectionRef(DRI, this));
989 uint8_t MachOObjectFile::getBytesInAddress() const {
990 return is64Bit() ? 8 : 4;
993 StringRef MachOObjectFile::getFileFormatName() const {
994 unsigned CPUType = getCPUType(this);
997 case llvm::MachO::CPU_TYPE_I386:
998 return "Mach-O 32-bit i386";
999 case llvm::MachO::CPU_TYPE_ARM:
1000 return "Mach-O arm";
1001 case llvm::MachO::CPU_TYPE_POWERPC:
1002 return "Mach-O 32-bit ppc";
1004 return "Mach-O 32-bit unknown";
1009 case llvm::MachO::CPU_TYPE_X86_64:
1010 return "Mach-O 64-bit x86-64";
1011 case llvm::MachO::CPU_TYPE_ARM64:
1012 return "Mach-O arm64";
1013 case llvm::MachO::CPU_TYPE_POWERPC64:
1014 return "Mach-O 64-bit ppc64";
1016 return "Mach-O 64-bit unknown";
1020 Triple::ArchType MachOObjectFile::getArch(uint32_t CPUType) {
1022 case llvm::MachO::CPU_TYPE_I386:
1024 case llvm::MachO::CPU_TYPE_X86_64:
1025 return Triple::x86_64;
1026 case llvm::MachO::CPU_TYPE_ARM:
1028 case llvm::MachO::CPU_TYPE_ARM64:
1029 return Triple::aarch64;
1030 case llvm::MachO::CPU_TYPE_POWERPC:
1032 case llvm::MachO::CPU_TYPE_POWERPC64:
1033 return Triple::ppc64;
1035 return Triple::UnknownArch;
1039 Triple MachOObjectFile::getArch(uint32_t CPUType, uint32_t CPUSubType,
1040 const char **McpuDefault) {
1042 *McpuDefault = nullptr;
1045 case MachO::CPU_TYPE_I386:
1046 switch (CPUSubType & ~MachO::CPU_SUBTYPE_MASK) {
1047 case MachO::CPU_SUBTYPE_I386_ALL:
1048 return Triple("i386-apple-darwin");
1052 case MachO::CPU_TYPE_X86_64:
1053 switch (CPUSubType & ~MachO::CPU_SUBTYPE_MASK) {
1054 case MachO::CPU_SUBTYPE_X86_64_ALL:
1055 return Triple("x86_64-apple-darwin");
1056 case MachO::CPU_SUBTYPE_X86_64_H:
1057 return Triple("x86_64h-apple-darwin");
1061 case MachO::CPU_TYPE_ARM:
1062 switch (CPUSubType & ~MachO::CPU_SUBTYPE_MASK) {
1063 case MachO::CPU_SUBTYPE_ARM_V4T:
1064 return Triple("armv4t-apple-darwin");
1065 case MachO::CPU_SUBTYPE_ARM_V5TEJ:
1066 return Triple("armv5e-apple-darwin");
1067 case MachO::CPU_SUBTYPE_ARM_XSCALE:
1068 return Triple("xscale-apple-darwin");
1069 case MachO::CPU_SUBTYPE_ARM_V6:
1070 return Triple("armv6-apple-darwin");
1071 case MachO::CPU_SUBTYPE_ARM_V6M:
1073 *McpuDefault = "cortex-m0";
1074 return Triple("armv6m-apple-darwin");
1075 case MachO::CPU_SUBTYPE_ARM_V7:
1076 return Triple("armv7-apple-darwin");
1077 case MachO::CPU_SUBTYPE_ARM_V7EM:
1079 *McpuDefault = "cortex-m4";
1080 return Triple("armv7em-apple-darwin");
1081 case MachO::CPU_SUBTYPE_ARM_V7K:
1082 return Triple("armv7k-apple-darwin");
1083 case MachO::CPU_SUBTYPE_ARM_V7M:
1085 *McpuDefault = "cortex-m3";
1086 return Triple("armv7m-apple-darwin");
1087 case MachO::CPU_SUBTYPE_ARM_V7S:
1088 return Triple("armv7s-apple-darwin");
1092 case MachO::CPU_TYPE_ARM64:
1093 switch (CPUSubType & ~MachO::CPU_SUBTYPE_MASK) {
1094 case MachO::CPU_SUBTYPE_ARM64_ALL:
1095 return Triple("arm64-apple-darwin");
1099 case MachO::CPU_TYPE_POWERPC:
1100 switch (CPUSubType & ~MachO::CPU_SUBTYPE_MASK) {
1101 case MachO::CPU_SUBTYPE_POWERPC_ALL:
1102 return Triple("ppc-apple-darwin");
1106 case MachO::CPU_TYPE_POWERPC64:
1107 switch (CPUSubType & ~MachO::CPU_SUBTYPE_MASK) {
1108 case MachO::CPU_SUBTYPE_POWERPC_ALL:
1109 return Triple("ppc64-apple-darwin");
1118 Triple MachOObjectFile::getThumbArch(uint32_t CPUType, uint32_t CPUSubType,
1119 const char **McpuDefault) {
1121 *McpuDefault = nullptr;
1124 case MachO::CPU_TYPE_ARM:
1125 switch (CPUSubType & ~MachO::CPU_SUBTYPE_MASK) {
1126 case MachO::CPU_SUBTYPE_ARM_V4T:
1127 return Triple("thumbv4t-apple-darwin");
1128 case MachO::CPU_SUBTYPE_ARM_V5TEJ:
1129 return Triple("thumbv5e-apple-darwin");
1130 case MachO::CPU_SUBTYPE_ARM_XSCALE:
1131 return Triple("xscale-apple-darwin");
1132 case MachO::CPU_SUBTYPE_ARM_V6:
1133 return Triple("thumbv6-apple-darwin");
1134 case MachO::CPU_SUBTYPE_ARM_V6M:
1136 *McpuDefault = "cortex-m0";
1137 return Triple("thumbv6m-apple-darwin");
1138 case MachO::CPU_SUBTYPE_ARM_V7:
1139 return Triple("thumbv7-apple-darwin");
1140 case MachO::CPU_SUBTYPE_ARM_V7EM:
1142 *McpuDefault = "cortex-m4";
1143 return Triple("thumbv7em-apple-darwin");
1144 case MachO::CPU_SUBTYPE_ARM_V7K:
1145 return Triple("thumbv7k-apple-darwin");
1146 case MachO::CPU_SUBTYPE_ARM_V7M:
1148 *McpuDefault = "cortex-m3";
1149 return Triple("thumbv7m-apple-darwin");
1150 case MachO::CPU_SUBTYPE_ARM_V7S:
1151 return Triple("thumbv7s-apple-darwin");
1160 Triple MachOObjectFile::getArch(uint32_t CPUType, uint32_t CPUSubType,
1161 const char **McpuDefault, Triple *ThumbTriple) {
1162 Triple T = MachOObjectFile::getArch(CPUType, CPUSubType, McpuDefault);
1163 *ThumbTriple = MachOObjectFile::getThumbArch(CPUType, CPUSubType,
1168 Triple MachOObjectFile::getHostArch() {
1169 return Triple(sys::getDefaultTargetTriple());
1172 bool MachOObjectFile::isValidArch(StringRef ArchFlag) {
1173 return StringSwitch<bool>(ArchFlag)
1175 .Case("x86_64", true)
1176 .Case("x86_64h", true)
1177 .Case("armv4t", true)
1179 .Case("armv5e", true)
1180 .Case("armv6", true)
1181 .Case("armv6m", true)
1182 .Case("armv7", true)
1183 .Case("armv7em", true)
1184 .Case("armv7k", true)
1185 .Case("armv7m", true)
1186 .Case("armv7s", true)
1187 .Case("arm64", true)
1189 .Case("ppc64", true)
1193 unsigned MachOObjectFile::getArch() const {
1194 return getArch(getCPUType(this));
1197 Triple MachOObjectFile::getArch(const char **McpuDefault,
1198 Triple *ThumbTriple) const {
1199 *ThumbTriple = getThumbArch(Header.cputype, Header.cpusubtype, McpuDefault);
1200 return getArch(Header.cputype, Header.cpusubtype, McpuDefault);
1203 relocation_iterator MachOObjectFile::section_rel_begin(unsigned Index) const {
1206 return section_rel_begin(DRI);
1209 relocation_iterator MachOObjectFile::section_rel_end(unsigned Index) const {
1212 return section_rel_end(DRI);
1215 dice_iterator MachOObjectFile::begin_dices() const {
1217 if (!DataInCodeLoadCmd)
1218 return dice_iterator(DiceRef(DRI, this));
1220 MachO::linkedit_data_command DicLC = getDataInCodeLoadCommand();
1221 DRI.p = reinterpret_cast<uintptr_t>(getPtr(this, DicLC.dataoff));
1222 return dice_iterator(DiceRef(DRI, this));
1225 dice_iterator MachOObjectFile::end_dices() const {
1227 if (!DataInCodeLoadCmd)
1228 return dice_iterator(DiceRef(DRI, this));
1230 MachO::linkedit_data_command DicLC = getDataInCodeLoadCommand();
1231 unsigned Offset = DicLC.dataoff + DicLC.datasize;
1232 DRI.p = reinterpret_cast<uintptr_t>(getPtr(this, Offset));
1233 return dice_iterator(DiceRef(DRI, this));
1236 ExportEntry::ExportEntry(ArrayRef<uint8_t> T)
1237 : Trie(T), Malformed(false), Done(false) {}
1239 void ExportEntry::moveToFirst() {
1241 pushDownUntilBottom();
1244 void ExportEntry::moveToEnd() {
1249 bool ExportEntry::operator==(const ExportEntry &Other) const {
1250 // Common case, one at end, other iterating from begin.
1251 if (Done || Other.Done)
1252 return (Done == Other.Done);
1253 // Not equal if different stack sizes.
1254 if (Stack.size() != Other.Stack.size())
1256 // Not equal if different cumulative strings.
1257 if (!CumulativeString.equals(Other.CumulativeString))
1259 // Equal if all nodes in both stacks match.
1260 for (unsigned i=0; i < Stack.size(); ++i) {
1261 if (Stack[i].Start != Other.Stack[i].Start)
1267 uint64_t ExportEntry::readULEB128(const uint8_t *&Ptr) {
1269 uint64_t Result = decodeULEB128(Ptr, &Count);
1271 if (Ptr > Trie.end()) {
1278 StringRef ExportEntry::name() const {
1279 return CumulativeString;
1282 uint64_t ExportEntry::flags() const {
1283 return Stack.back().Flags;
1286 uint64_t ExportEntry::address() const {
1287 return Stack.back().Address;
1290 uint64_t ExportEntry::other() const {
1291 return Stack.back().Other;
1294 StringRef ExportEntry::otherName() const {
1295 const char* ImportName = Stack.back().ImportName;
1297 return StringRef(ImportName);
1301 uint32_t ExportEntry::nodeOffset() const {
1302 return Stack.back().Start - Trie.begin();
1305 ExportEntry::NodeState::NodeState(const uint8_t *Ptr)
1306 : Start(Ptr), Current(Ptr), Flags(0), Address(0), Other(0),
1307 ImportName(nullptr), ChildCount(0), NextChildIndex(0),
1308 ParentStringLength(0), IsExportNode(false) {}
1310 void ExportEntry::pushNode(uint64_t offset) {
1311 const uint8_t *Ptr = Trie.begin() + offset;
1312 NodeState State(Ptr);
1313 uint64_t ExportInfoSize = readULEB128(State.Current);
1314 State.IsExportNode = (ExportInfoSize != 0);
1315 const uint8_t* Children = State.Current + ExportInfoSize;
1316 if (State.IsExportNode) {
1317 State.Flags = readULEB128(State.Current);
1318 if (State.Flags & MachO::EXPORT_SYMBOL_FLAGS_REEXPORT) {
1320 State.Other = readULEB128(State.Current); // dylib ordinal
1321 State.ImportName = reinterpret_cast<const char*>(State.Current);
1323 State.Address = readULEB128(State.Current);
1324 if (State.Flags & MachO::EXPORT_SYMBOL_FLAGS_STUB_AND_RESOLVER)
1325 State.Other = readULEB128(State.Current);
1328 State.ChildCount = *Children;
1329 State.Current = Children + 1;
1330 State.NextChildIndex = 0;
1331 State.ParentStringLength = CumulativeString.size();
1332 Stack.push_back(State);
1335 void ExportEntry::pushDownUntilBottom() {
1336 while (Stack.back().NextChildIndex < Stack.back().ChildCount) {
1337 NodeState &Top = Stack.back();
1338 CumulativeString.resize(Top.ParentStringLength);
1339 for (;*Top.Current != 0; Top.Current++) {
1340 char C = *Top.Current;
1341 CumulativeString.push_back(C);
1344 uint64_t childNodeIndex = readULEB128(Top.Current);
1345 Top.NextChildIndex += 1;
1346 pushNode(childNodeIndex);
1348 if (!Stack.back().IsExportNode) {
1354 // We have a trie data structure and need a way to walk it that is compatible
1355 // with the C++ iterator model. The solution is a non-recursive depth first
1356 // traversal where the iterator contains a stack of parent nodes along with a
1357 // string that is the accumulation of all edge strings along the parent chain
1360 // There is one "export" node for each exported symbol. But because some
1361 // symbols may be a prefix of another symbol (e.g. _dup and _dup2), an export
1362 // node may have child nodes too.
1364 // The algorithm for moveNext() is to keep moving down the leftmost unvisited
1365 // child until hitting a node with no children (which is an export node or
1366 // else the trie is malformed). On the way down, each node is pushed on the
1367 // stack ivar. If there is no more ways down, it pops up one and tries to go
1368 // down a sibling path until a childless node is reached.
1369 void ExportEntry::moveNext() {
1370 if (Stack.empty() || !Stack.back().IsExportNode) {
1377 while (!Stack.empty()) {
1378 NodeState &Top = Stack.back();
1379 if (Top.NextChildIndex < Top.ChildCount) {
1380 pushDownUntilBottom();
1381 // Now at the next export node.
1384 if (Top.IsExportNode) {
1385 // This node has no children but is itself an export node.
1386 CumulativeString.resize(Top.ParentStringLength);
1395 iterator_range<export_iterator>
1396 MachOObjectFile::exports(ArrayRef<uint8_t> Trie) {
1397 ExportEntry Start(Trie);
1398 if (Trie.size() == 0)
1401 Start.moveToFirst();
1403 ExportEntry Finish(Trie);
1406 return make_range(export_iterator(Start), export_iterator(Finish));
1409 iterator_range<export_iterator> MachOObjectFile::exports() const {
1410 return exports(getDyldInfoExportsTrie());
1413 MachORebaseEntry::MachORebaseEntry(ArrayRef<uint8_t> Bytes, bool is64Bit)
1414 : Opcodes(Bytes), Ptr(Bytes.begin()), SegmentOffset(0), SegmentIndex(0),
1415 RemainingLoopCount(0), AdvanceAmount(0), RebaseType(0),
1416 PointerSize(is64Bit ? 8 : 4), Malformed(false), Done(false) {}
1418 void MachORebaseEntry::moveToFirst() {
1419 Ptr = Opcodes.begin();
1423 void MachORebaseEntry::moveToEnd() {
1424 Ptr = Opcodes.end();
1425 RemainingLoopCount = 0;
1429 void MachORebaseEntry::moveNext() {
1430 // If in the middle of some loop, move to next rebasing in loop.
1431 SegmentOffset += AdvanceAmount;
1432 if (RemainingLoopCount) {
1433 --RemainingLoopCount;
1436 if (Ptr == Opcodes.end()) {
1441 while (More && !Malformed) {
1442 // Parse next opcode and set up next loop.
1443 uint8_t Byte = *Ptr++;
1444 uint8_t ImmValue = Byte & MachO::REBASE_IMMEDIATE_MASK;
1445 uint8_t Opcode = Byte & MachO::REBASE_OPCODE_MASK;
1447 case MachO::REBASE_OPCODE_DONE:
1451 DEBUG_WITH_TYPE("mach-o-rebase", llvm::dbgs() << "REBASE_OPCODE_DONE\n");
1453 case MachO::REBASE_OPCODE_SET_TYPE_IMM:
1454 RebaseType = ImmValue;
1457 llvm::dbgs() << "REBASE_OPCODE_SET_TYPE_IMM: "
1458 << "RebaseType=" << (int) RebaseType << "\n");
1460 case MachO::REBASE_OPCODE_SET_SEGMENT_AND_OFFSET_ULEB:
1461 SegmentIndex = ImmValue;
1462 SegmentOffset = readULEB128();
1465 llvm::dbgs() << "REBASE_OPCODE_SET_SEGMENT_AND_OFFSET_ULEB: "
1466 << "SegmentIndex=" << SegmentIndex << ", "
1467 << format("SegmentOffset=0x%06X", SegmentOffset)
1470 case MachO::REBASE_OPCODE_ADD_ADDR_ULEB:
1471 SegmentOffset += readULEB128();
1472 DEBUG_WITH_TYPE("mach-o-rebase",
1473 llvm::dbgs() << "REBASE_OPCODE_ADD_ADDR_ULEB: "
1474 << format("SegmentOffset=0x%06X",
1475 SegmentOffset) << "\n");
1477 case MachO::REBASE_OPCODE_ADD_ADDR_IMM_SCALED:
1478 SegmentOffset += ImmValue * PointerSize;
1479 DEBUG_WITH_TYPE("mach-o-rebase",
1480 llvm::dbgs() << "REBASE_OPCODE_ADD_ADDR_IMM_SCALED: "
1481 << format("SegmentOffset=0x%06X",
1482 SegmentOffset) << "\n");
1484 case MachO::REBASE_OPCODE_DO_REBASE_IMM_TIMES:
1485 AdvanceAmount = PointerSize;
1486 RemainingLoopCount = ImmValue - 1;
1489 llvm::dbgs() << "REBASE_OPCODE_DO_REBASE_IMM_TIMES: "
1490 << format("SegmentOffset=0x%06X", SegmentOffset)
1491 << ", AdvanceAmount=" << AdvanceAmount
1492 << ", RemainingLoopCount=" << RemainingLoopCount
1495 case MachO::REBASE_OPCODE_DO_REBASE_ULEB_TIMES:
1496 AdvanceAmount = PointerSize;
1497 RemainingLoopCount = readULEB128() - 1;
1500 llvm::dbgs() << "REBASE_OPCODE_DO_REBASE_ULEB_TIMES: "
1501 << format("SegmentOffset=0x%06X", SegmentOffset)
1502 << ", AdvanceAmount=" << AdvanceAmount
1503 << ", RemainingLoopCount=" << RemainingLoopCount
1506 case MachO::REBASE_OPCODE_DO_REBASE_ADD_ADDR_ULEB:
1507 AdvanceAmount = readULEB128() + PointerSize;
1508 RemainingLoopCount = 0;
1511 llvm::dbgs() << "REBASE_OPCODE_DO_REBASE_ADD_ADDR_ULEB: "
1512 << format("SegmentOffset=0x%06X", SegmentOffset)
1513 << ", AdvanceAmount=" << AdvanceAmount
1514 << ", RemainingLoopCount=" << RemainingLoopCount
1517 case MachO::REBASE_OPCODE_DO_REBASE_ULEB_TIMES_SKIPPING_ULEB:
1518 RemainingLoopCount = readULEB128() - 1;
1519 AdvanceAmount = readULEB128() + PointerSize;
1522 llvm::dbgs() << "REBASE_OPCODE_DO_REBASE_ULEB_TIMES_SKIPPING_ULEB: "
1523 << format("SegmentOffset=0x%06X", SegmentOffset)
1524 << ", AdvanceAmount=" << AdvanceAmount
1525 << ", RemainingLoopCount=" << RemainingLoopCount
1534 uint64_t MachORebaseEntry::readULEB128() {
1536 uint64_t Result = decodeULEB128(Ptr, &Count);
1538 if (Ptr > Opcodes.end()) {
1539 Ptr = Opcodes.end();
1545 uint32_t MachORebaseEntry::segmentIndex() const { return SegmentIndex; }
1547 uint64_t MachORebaseEntry::segmentOffset() const { return SegmentOffset; }
1549 StringRef MachORebaseEntry::typeName() const {
1550 switch (RebaseType) {
1551 case MachO::REBASE_TYPE_POINTER:
1553 case MachO::REBASE_TYPE_TEXT_ABSOLUTE32:
1554 return "text abs32";
1555 case MachO::REBASE_TYPE_TEXT_PCREL32:
1556 return "text rel32";
1561 bool MachORebaseEntry::operator==(const MachORebaseEntry &Other) const {
1562 assert(Opcodes == Other.Opcodes && "compare iterators of different files");
1563 return (Ptr == Other.Ptr) &&
1564 (RemainingLoopCount == Other.RemainingLoopCount) &&
1565 (Done == Other.Done);
1568 iterator_range<rebase_iterator>
1569 MachOObjectFile::rebaseTable(ArrayRef<uint8_t> Opcodes, bool is64) {
1570 MachORebaseEntry Start(Opcodes, is64);
1571 Start.moveToFirst();
1573 MachORebaseEntry Finish(Opcodes, is64);
1576 return make_range(rebase_iterator(Start), rebase_iterator(Finish));
1579 iterator_range<rebase_iterator> MachOObjectFile::rebaseTable() const {
1580 return rebaseTable(getDyldInfoRebaseOpcodes(), is64Bit());
1583 MachOBindEntry::MachOBindEntry(ArrayRef<uint8_t> Bytes, bool is64Bit, Kind BK)
1584 : Opcodes(Bytes), Ptr(Bytes.begin()), SegmentOffset(0), SegmentIndex(0),
1585 Ordinal(0), Flags(0), Addend(0), RemainingLoopCount(0), AdvanceAmount(0),
1586 BindType(0), PointerSize(is64Bit ? 8 : 4),
1587 TableKind(BK), Malformed(false), Done(false) {}
1589 void MachOBindEntry::moveToFirst() {
1590 Ptr = Opcodes.begin();
1594 void MachOBindEntry::moveToEnd() {
1595 Ptr = Opcodes.end();
1596 RemainingLoopCount = 0;
1600 void MachOBindEntry::moveNext() {
1601 // If in the middle of some loop, move to next binding in loop.
1602 SegmentOffset += AdvanceAmount;
1603 if (RemainingLoopCount) {
1604 --RemainingLoopCount;
1607 if (Ptr == Opcodes.end()) {
1612 while (More && !Malformed) {
1613 // Parse next opcode and set up next loop.
1614 uint8_t Byte = *Ptr++;
1615 uint8_t ImmValue = Byte & MachO::BIND_IMMEDIATE_MASK;
1616 uint8_t Opcode = Byte & MachO::BIND_OPCODE_MASK;
1617 int8_t SignExtended;
1618 const uint8_t *SymStart;
1620 case MachO::BIND_OPCODE_DONE:
1621 if (TableKind == Kind::Lazy) {
1622 // Lazying bindings have a DONE opcode between entries. Need to ignore
1623 // it to advance to next entry. But need not if this is last entry.
1624 bool NotLastEntry = false;
1625 for (const uint8_t *P = Ptr; P < Opcodes.end(); ++P) {
1627 NotLastEntry = true;
1636 DEBUG_WITH_TYPE("mach-o-bind", llvm::dbgs() << "BIND_OPCODE_DONE\n");
1638 case MachO::BIND_OPCODE_SET_DYLIB_ORDINAL_IMM:
1642 llvm::dbgs() << "BIND_OPCODE_SET_DYLIB_ORDINAL_IMM: "
1643 << "Ordinal=" << Ordinal << "\n");
1645 case MachO::BIND_OPCODE_SET_DYLIB_ORDINAL_ULEB:
1646 Ordinal = readULEB128();
1649 llvm::dbgs() << "BIND_OPCODE_SET_DYLIB_ORDINAL_ULEB: "
1650 << "Ordinal=" << Ordinal << "\n");
1652 case MachO::BIND_OPCODE_SET_DYLIB_SPECIAL_IMM:
1654 SignExtended = MachO::BIND_OPCODE_MASK | ImmValue;
1655 Ordinal = SignExtended;
1660 llvm::dbgs() << "BIND_OPCODE_SET_DYLIB_SPECIAL_IMM: "
1661 << "Ordinal=" << Ordinal << "\n");
1663 case MachO::BIND_OPCODE_SET_SYMBOL_TRAILING_FLAGS_IMM:
1669 SymbolName = StringRef(reinterpret_cast<const char*>(SymStart),
1674 llvm::dbgs() << "BIND_OPCODE_SET_SYMBOL_TRAILING_FLAGS_IMM: "
1675 << "SymbolName=" << SymbolName << "\n");
1676 if (TableKind == Kind::Weak) {
1677 if (ImmValue & MachO::BIND_SYMBOL_FLAGS_NON_WEAK_DEFINITION)
1681 case MachO::BIND_OPCODE_SET_TYPE_IMM:
1682 BindType = ImmValue;
1685 llvm::dbgs() << "BIND_OPCODE_SET_TYPE_IMM: "
1686 << "BindType=" << (int)BindType << "\n");
1688 case MachO::BIND_OPCODE_SET_ADDEND_SLEB:
1689 Addend = readSLEB128();
1690 if (TableKind == Kind::Lazy)
1694 llvm::dbgs() << "BIND_OPCODE_SET_ADDEND_SLEB: "
1695 << "Addend=" << Addend << "\n");
1697 case MachO::BIND_OPCODE_SET_SEGMENT_AND_OFFSET_ULEB:
1698 SegmentIndex = ImmValue;
1699 SegmentOffset = readULEB128();
1702 llvm::dbgs() << "BIND_OPCODE_SET_SEGMENT_AND_OFFSET_ULEB: "
1703 << "SegmentIndex=" << SegmentIndex << ", "
1704 << format("SegmentOffset=0x%06X", SegmentOffset)
1707 case MachO::BIND_OPCODE_ADD_ADDR_ULEB:
1708 SegmentOffset += readULEB128();
1709 DEBUG_WITH_TYPE("mach-o-bind",
1710 llvm::dbgs() << "BIND_OPCODE_ADD_ADDR_ULEB: "
1711 << format("SegmentOffset=0x%06X",
1712 SegmentOffset) << "\n");
1714 case MachO::BIND_OPCODE_DO_BIND:
1715 AdvanceAmount = PointerSize;
1716 RemainingLoopCount = 0;
1717 DEBUG_WITH_TYPE("mach-o-bind",
1718 llvm::dbgs() << "BIND_OPCODE_DO_BIND: "
1719 << format("SegmentOffset=0x%06X",
1720 SegmentOffset) << "\n");
1722 case MachO::BIND_OPCODE_DO_BIND_ADD_ADDR_ULEB:
1723 AdvanceAmount = readULEB128() + PointerSize;
1724 RemainingLoopCount = 0;
1725 if (TableKind == Kind::Lazy)
1729 llvm::dbgs() << "BIND_OPCODE_DO_BIND_ADD_ADDR_ULEB: "
1730 << format("SegmentOffset=0x%06X", SegmentOffset)
1731 << ", AdvanceAmount=" << AdvanceAmount
1732 << ", RemainingLoopCount=" << RemainingLoopCount
1735 case MachO::BIND_OPCODE_DO_BIND_ADD_ADDR_IMM_SCALED:
1736 AdvanceAmount = ImmValue * PointerSize + PointerSize;
1737 RemainingLoopCount = 0;
1738 if (TableKind == Kind::Lazy)
1740 DEBUG_WITH_TYPE("mach-o-bind",
1742 << "BIND_OPCODE_DO_BIND_ADD_ADDR_IMM_SCALED: "
1743 << format("SegmentOffset=0x%06X",
1744 SegmentOffset) << "\n");
1746 case MachO::BIND_OPCODE_DO_BIND_ULEB_TIMES_SKIPPING_ULEB:
1747 RemainingLoopCount = readULEB128() - 1;
1748 AdvanceAmount = readULEB128() + PointerSize;
1749 if (TableKind == Kind::Lazy)
1753 llvm::dbgs() << "BIND_OPCODE_DO_BIND_ULEB_TIMES_SKIPPING_ULEB: "
1754 << format("SegmentOffset=0x%06X", SegmentOffset)
1755 << ", AdvanceAmount=" << AdvanceAmount
1756 << ", RemainingLoopCount=" << RemainingLoopCount
1765 uint64_t MachOBindEntry::readULEB128() {
1767 uint64_t Result = decodeULEB128(Ptr, &Count);
1769 if (Ptr > Opcodes.end()) {
1770 Ptr = Opcodes.end();
1776 int64_t MachOBindEntry::readSLEB128() {
1778 int64_t Result = decodeSLEB128(Ptr, &Count);
1780 if (Ptr > Opcodes.end()) {
1781 Ptr = Opcodes.end();
1787 uint32_t MachOBindEntry::segmentIndex() const { return SegmentIndex; }
1789 uint64_t MachOBindEntry::segmentOffset() const { return SegmentOffset; }
1791 StringRef MachOBindEntry::typeName() const {
1793 case MachO::BIND_TYPE_POINTER:
1795 case MachO::BIND_TYPE_TEXT_ABSOLUTE32:
1796 return "text abs32";
1797 case MachO::BIND_TYPE_TEXT_PCREL32:
1798 return "text rel32";
1803 StringRef MachOBindEntry::symbolName() const { return SymbolName; }
1805 int64_t MachOBindEntry::addend() const { return Addend; }
1807 uint32_t MachOBindEntry::flags() const { return Flags; }
1809 int MachOBindEntry::ordinal() const { return Ordinal; }
1811 bool MachOBindEntry::operator==(const MachOBindEntry &Other) const {
1812 assert(Opcodes == Other.Opcodes && "compare iterators of different files");
1813 return (Ptr == Other.Ptr) &&
1814 (RemainingLoopCount == Other.RemainingLoopCount) &&
1815 (Done == Other.Done);
1818 iterator_range<bind_iterator>
1819 MachOObjectFile::bindTable(ArrayRef<uint8_t> Opcodes, bool is64,
1820 MachOBindEntry::Kind BKind) {
1821 MachOBindEntry Start(Opcodes, is64, BKind);
1822 Start.moveToFirst();
1824 MachOBindEntry Finish(Opcodes, is64, BKind);
1827 return make_range(bind_iterator(Start), bind_iterator(Finish));
1830 iterator_range<bind_iterator> MachOObjectFile::bindTable() const {
1831 return bindTable(getDyldInfoBindOpcodes(), is64Bit(),
1832 MachOBindEntry::Kind::Regular);
1835 iterator_range<bind_iterator> MachOObjectFile::lazyBindTable() const {
1836 return bindTable(getDyldInfoLazyBindOpcodes(), is64Bit(),
1837 MachOBindEntry::Kind::Lazy);
1840 iterator_range<bind_iterator> MachOObjectFile::weakBindTable() const {
1841 return bindTable(getDyldInfoWeakBindOpcodes(), is64Bit(),
1842 MachOBindEntry::Kind::Weak);
1845 MachOObjectFile::load_command_iterator
1846 MachOObjectFile::begin_load_commands() const {
1847 return LoadCommands.begin();
1850 MachOObjectFile::load_command_iterator
1851 MachOObjectFile::end_load_commands() const {
1852 return LoadCommands.end();
1855 iterator_range<MachOObjectFile::load_command_iterator>
1856 MachOObjectFile::load_commands() const {
1857 return make_range(begin_load_commands(), end_load_commands());
1861 MachOObjectFile::getSectionFinalSegmentName(DataRefImpl Sec) const {
1862 ArrayRef<char> Raw = getSectionRawFinalSegmentName(Sec);
1863 return parseSegmentOrSectionName(Raw.data());
1867 MachOObjectFile::getSectionRawName(DataRefImpl Sec) const {
1868 assert(Sec.d.a < Sections.size() && "Should have detected this earlier");
1869 const section_base *Base =
1870 reinterpret_cast<const section_base *>(Sections[Sec.d.a]);
1871 return makeArrayRef(Base->sectname);
1875 MachOObjectFile::getSectionRawFinalSegmentName(DataRefImpl Sec) const {
1876 assert(Sec.d.a < Sections.size() && "Should have detected this earlier");
1877 const section_base *Base =
1878 reinterpret_cast<const section_base *>(Sections[Sec.d.a]);
1879 return makeArrayRef(Base->segname);
1883 MachOObjectFile::isRelocationScattered(const MachO::any_relocation_info &RE)
1885 if (getCPUType(this) == MachO::CPU_TYPE_X86_64)
1887 return getPlainRelocationAddress(RE) & MachO::R_SCATTERED;
1890 unsigned MachOObjectFile::getPlainRelocationSymbolNum(
1891 const MachO::any_relocation_info &RE) const {
1892 if (isLittleEndian())
1893 return RE.r_word1 & 0xffffff;
1894 return RE.r_word1 >> 8;
1897 bool MachOObjectFile::getPlainRelocationExternal(
1898 const MachO::any_relocation_info &RE) const {
1899 if (isLittleEndian())
1900 return (RE.r_word1 >> 27) & 1;
1901 return (RE.r_word1 >> 4) & 1;
1904 bool MachOObjectFile::getScatteredRelocationScattered(
1905 const MachO::any_relocation_info &RE) const {
1906 return RE.r_word0 >> 31;
1909 uint32_t MachOObjectFile::getScatteredRelocationValue(
1910 const MachO::any_relocation_info &RE) const {
1914 uint32_t MachOObjectFile::getScatteredRelocationType(
1915 const MachO::any_relocation_info &RE) const {
1916 return (RE.r_word0 >> 24) & 0xf;
1919 unsigned MachOObjectFile::getAnyRelocationAddress(
1920 const MachO::any_relocation_info &RE) const {
1921 if (isRelocationScattered(RE))
1922 return getScatteredRelocationAddress(RE);
1923 return getPlainRelocationAddress(RE);
1926 unsigned MachOObjectFile::getAnyRelocationPCRel(
1927 const MachO::any_relocation_info &RE) const {
1928 if (isRelocationScattered(RE))
1929 return getScatteredRelocationPCRel(this, RE);
1930 return getPlainRelocationPCRel(this, RE);
1933 unsigned MachOObjectFile::getAnyRelocationLength(
1934 const MachO::any_relocation_info &RE) const {
1935 if (isRelocationScattered(RE))
1936 return getScatteredRelocationLength(RE);
1937 return getPlainRelocationLength(this, RE);
1941 MachOObjectFile::getAnyRelocationType(
1942 const MachO::any_relocation_info &RE) const {
1943 if (isRelocationScattered(RE))
1944 return getScatteredRelocationType(RE);
1945 return getPlainRelocationType(this, RE);
1949 MachOObjectFile::getAnyRelocationSection(
1950 const MachO::any_relocation_info &RE) const {
1951 if (isRelocationScattered(RE) || getPlainRelocationExternal(RE))
1952 return *section_end();
1953 unsigned SecNum = getPlainRelocationSymbolNum(RE);
1954 if (SecNum == MachO::R_ABS || SecNum > Sections.size())
1955 return *section_end();
1957 DRI.d.a = SecNum - 1;
1958 return SectionRef(DRI, this);
1961 MachO::section MachOObjectFile::getSection(DataRefImpl DRI) const {
1962 assert(DRI.d.a < Sections.size() && "Should have detected this earlier");
1963 return getStruct<MachO::section>(this, Sections[DRI.d.a]);
1966 MachO::section_64 MachOObjectFile::getSection64(DataRefImpl DRI) const {
1967 assert(DRI.d.a < Sections.size() && "Should have detected this earlier");
1968 return getStruct<MachO::section_64>(this, Sections[DRI.d.a]);
1971 MachO::section MachOObjectFile::getSection(const LoadCommandInfo &L,
1972 unsigned Index) const {
1973 const char *Sec = getSectionPtr(this, L, Index);
1974 return getStruct<MachO::section>(this, Sec);
1977 MachO::section_64 MachOObjectFile::getSection64(const LoadCommandInfo &L,
1978 unsigned Index) const {
1979 const char *Sec = getSectionPtr(this, L, Index);
1980 return getStruct<MachO::section_64>(this, Sec);
1984 MachOObjectFile::getSymbolTableEntry(DataRefImpl DRI) const {
1985 const char *P = reinterpret_cast<const char *>(DRI.p);
1986 return getStruct<MachO::nlist>(this, P);
1990 MachOObjectFile::getSymbol64TableEntry(DataRefImpl DRI) const {
1991 const char *P = reinterpret_cast<const char *>(DRI.p);
1992 return getStruct<MachO::nlist_64>(this, P);
1995 MachO::linkedit_data_command
1996 MachOObjectFile::getLinkeditDataLoadCommand(const LoadCommandInfo &L) const {
1997 return getStruct<MachO::linkedit_data_command>(this, L.Ptr);
2000 MachO::segment_command
2001 MachOObjectFile::getSegmentLoadCommand(const LoadCommandInfo &L) const {
2002 return getStruct<MachO::segment_command>(this, L.Ptr);
2005 MachO::segment_command_64
2006 MachOObjectFile::getSegment64LoadCommand(const LoadCommandInfo &L) const {
2007 return getStruct<MachO::segment_command_64>(this, L.Ptr);
2010 MachO::linker_option_command
2011 MachOObjectFile::getLinkerOptionLoadCommand(const LoadCommandInfo &L) const {
2012 return getStruct<MachO::linker_option_command>(this, L.Ptr);
2015 MachO::version_min_command
2016 MachOObjectFile::getVersionMinLoadCommand(const LoadCommandInfo &L) const {
2017 return getStruct<MachO::version_min_command>(this, L.Ptr);
2020 MachO::dylib_command
2021 MachOObjectFile::getDylibIDLoadCommand(const LoadCommandInfo &L) const {
2022 return getStruct<MachO::dylib_command>(this, L.Ptr);
2025 MachO::dyld_info_command
2026 MachOObjectFile::getDyldInfoLoadCommand(const LoadCommandInfo &L) const {
2027 return getStruct<MachO::dyld_info_command>(this, L.Ptr);
2030 MachO::dylinker_command
2031 MachOObjectFile::getDylinkerCommand(const LoadCommandInfo &L) const {
2032 return getStruct<MachO::dylinker_command>(this, L.Ptr);
2036 MachOObjectFile::getUuidCommand(const LoadCommandInfo &L) const {
2037 return getStruct<MachO::uuid_command>(this, L.Ptr);
2040 MachO::rpath_command
2041 MachOObjectFile::getRpathCommand(const LoadCommandInfo &L) const {
2042 return getStruct<MachO::rpath_command>(this, L.Ptr);
2045 MachO::source_version_command
2046 MachOObjectFile::getSourceVersionCommand(const LoadCommandInfo &L) const {
2047 return getStruct<MachO::source_version_command>(this, L.Ptr);
2050 MachO::entry_point_command
2051 MachOObjectFile::getEntryPointCommand(const LoadCommandInfo &L) const {
2052 return getStruct<MachO::entry_point_command>(this, L.Ptr);
2055 MachO::encryption_info_command
2056 MachOObjectFile::getEncryptionInfoCommand(const LoadCommandInfo &L) const {
2057 return getStruct<MachO::encryption_info_command>(this, L.Ptr);
2060 MachO::encryption_info_command_64
2061 MachOObjectFile::getEncryptionInfoCommand64(const LoadCommandInfo &L) const {
2062 return getStruct<MachO::encryption_info_command_64>(this, L.Ptr);
2065 MachO::sub_framework_command
2066 MachOObjectFile::getSubFrameworkCommand(const LoadCommandInfo &L) const {
2067 return getStruct<MachO::sub_framework_command>(this, L.Ptr);
2070 MachO::sub_umbrella_command
2071 MachOObjectFile::getSubUmbrellaCommand(const LoadCommandInfo &L) const {
2072 return getStruct<MachO::sub_umbrella_command>(this, L.Ptr);
2075 MachO::sub_library_command
2076 MachOObjectFile::getSubLibraryCommand(const LoadCommandInfo &L) const {
2077 return getStruct<MachO::sub_library_command>(this, L.Ptr);
2080 MachO::sub_client_command
2081 MachOObjectFile::getSubClientCommand(const LoadCommandInfo &L) const {
2082 return getStruct<MachO::sub_client_command>(this, L.Ptr);
2085 MachO::routines_command
2086 MachOObjectFile::getRoutinesCommand(const LoadCommandInfo &L) const {
2087 return getStruct<MachO::routines_command>(this, L.Ptr);
2090 MachO::routines_command_64
2091 MachOObjectFile::getRoutinesCommand64(const LoadCommandInfo &L) const {
2092 return getStruct<MachO::routines_command_64>(this, L.Ptr);
2095 MachO::thread_command
2096 MachOObjectFile::getThreadCommand(const LoadCommandInfo &L) const {
2097 return getStruct<MachO::thread_command>(this, L.Ptr);
2100 MachO::any_relocation_info
2101 MachOObjectFile::getRelocation(DataRefImpl Rel) const {
2106 MachO::section_64 Sect = getSection64(Sec);
2107 Offset = Sect.reloff;
2109 MachO::section Sect = getSection(Sec);
2110 Offset = Sect.reloff;
2113 auto P = reinterpret_cast<const MachO::any_relocation_info *>(
2114 getPtr(this, Offset)) + Rel.d.b;
2115 return getStruct<MachO::any_relocation_info>(
2116 this, reinterpret_cast<const char *>(P));
2119 MachO::data_in_code_entry
2120 MachOObjectFile::getDice(DataRefImpl Rel) const {
2121 const char *P = reinterpret_cast<const char *>(Rel.p);
2122 return getStruct<MachO::data_in_code_entry>(this, P);
2125 const MachO::mach_header &MachOObjectFile::getHeader() const {
2129 const MachO::mach_header_64 &MachOObjectFile::getHeader64() const {
2134 uint32_t MachOObjectFile::getIndirectSymbolTableEntry(
2135 const MachO::dysymtab_command &DLC,
2136 unsigned Index) const {
2137 uint64_t Offset = DLC.indirectsymoff + Index * sizeof(uint32_t);
2138 return getStruct<uint32_t>(this, getPtr(this, Offset));
2141 MachO::data_in_code_entry
2142 MachOObjectFile::getDataInCodeTableEntry(uint32_t DataOffset,
2143 unsigned Index) const {
2144 uint64_t Offset = DataOffset + Index * sizeof(MachO::data_in_code_entry);
2145 return getStruct<MachO::data_in_code_entry>(this, getPtr(this, Offset));
2148 MachO::symtab_command MachOObjectFile::getSymtabLoadCommand() const {
2150 return getStruct<MachO::symtab_command>(this, SymtabLoadCmd);
2152 // If there is no SymtabLoadCmd return a load command with zero'ed fields.
2153 MachO::symtab_command Cmd;
2154 Cmd.cmd = MachO::LC_SYMTAB;
2155 Cmd.cmdsize = sizeof(MachO::symtab_command);
2163 MachO::dysymtab_command MachOObjectFile::getDysymtabLoadCommand() const {
2164 if (DysymtabLoadCmd)
2165 return getStruct<MachO::dysymtab_command>(this, DysymtabLoadCmd);
2167 // If there is no DysymtabLoadCmd return a load command with zero'ed fields.
2168 MachO::dysymtab_command Cmd;
2169 Cmd.cmd = MachO::LC_DYSYMTAB;
2170 Cmd.cmdsize = sizeof(MachO::dysymtab_command);
2181 Cmd.extrefsymoff = 0;
2182 Cmd.nextrefsyms = 0;
2183 Cmd.indirectsymoff = 0;
2184 Cmd.nindirectsyms = 0;
2192 MachO::linkedit_data_command
2193 MachOObjectFile::getDataInCodeLoadCommand() const {
2194 if (DataInCodeLoadCmd)
2195 return getStruct<MachO::linkedit_data_command>(this, DataInCodeLoadCmd);
2197 // If there is no DataInCodeLoadCmd return a load command with zero'ed fields.
2198 MachO::linkedit_data_command Cmd;
2199 Cmd.cmd = MachO::LC_DATA_IN_CODE;
2200 Cmd.cmdsize = sizeof(MachO::linkedit_data_command);
2206 MachO::linkedit_data_command
2207 MachOObjectFile::getLinkOptHintsLoadCommand() const {
2208 if (LinkOptHintsLoadCmd)
2209 return getStruct<MachO::linkedit_data_command>(this, LinkOptHintsLoadCmd);
2211 // If there is no LinkOptHintsLoadCmd return a load command with zero'ed
2213 MachO::linkedit_data_command Cmd;
2214 Cmd.cmd = MachO::LC_LINKER_OPTIMIZATION_HINT;
2215 Cmd.cmdsize = sizeof(MachO::linkedit_data_command);
2221 ArrayRef<uint8_t> MachOObjectFile::getDyldInfoRebaseOpcodes() const {
2222 if (!DyldInfoLoadCmd)
2225 MachO::dyld_info_command DyldInfo =
2226 getStruct<MachO::dyld_info_command>(this, DyldInfoLoadCmd);
2227 const uint8_t *Ptr =
2228 reinterpret_cast<const uint8_t *>(getPtr(this, DyldInfo.rebase_off));
2229 return makeArrayRef(Ptr, DyldInfo.rebase_size);
2232 ArrayRef<uint8_t> MachOObjectFile::getDyldInfoBindOpcodes() const {
2233 if (!DyldInfoLoadCmd)
2236 MachO::dyld_info_command DyldInfo =
2237 getStruct<MachO::dyld_info_command>(this, DyldInfoLoadCmd);
2238 const uint8_t *Ptr =
2239 reinterpret_cast<const uint8_t *>(getPtr(this, DyldInfo.bind_off));
2240 return makeArrayRef(Ptr, DyldInfo.bind_size);
2243 ArrayRef<uint8_t> MachOObjectFile::getDyldInfoWeakBindOpcodes() const {
2244 if (!DyldInfoLoadCmd)
2247 MachO::dyld_info_command DyldInfo =
2248 getStruct<MachO::dyld_info_command>(this, DyldInfoLoadCmd);
2249 const uint8_t *Ptr =
2250 reinterpret_cast<const uint8_t *>(getPtr(this, DyldInfo.weak_bind_off));
2251 return makeArrayRef(Ptr, DyldInfo.weak_bind_size);
2254 ArrayRef<uint8_t> MachOObjectFile::getDyldInfoLazyBindOpcodes() const {
2255 if (!DyldInfoLoadCmd)
2258 MachO::dyld_info_command DyldInfo =
2259 getStruct<MachO::dyld_info_command>(this, DyldInfoLoadCmd);
2260 const uint8_t *Ptr =
2261 reinterpret_cast<const uint8_t *>(getPtr(this, DyldInfo.lazy_bind_off));
2262 return makeArrayRef(Ptr, DyldInfo.lazy_bind_size);
2265 ArrayRef<uint8_t> MachOObjectFile::getDyldInfoExportsTrie() const {
2266 if (!DyldInfoLoadCmd)
2269 MachO::dyld_info_command DyldInfo =
2270 getStruct<MachO::dyld_info_command>(this, DyldInfoLoadCmd);
2271 const uint8_t *Ptr =
2272 reinterpret_cast<const uint8_t *>(getPtr(this, DyldInfo.export_off));
2273 return makeArrayRef(Ptr, DyldInfo.export_size);
2276 ArrayRef<uint8_t> MachOObjectFile::getUuid() const {
2279 // Returning a pointer is fine as uuid doesn't need endian swapping.
2280 const char *Ptr = UuidLoadCmd + offsetof(MachO::uuid_command, uuid);
2281 return makeArrayRef(reinterpret_cast<const uint8_t *>(Ptr), 16);
2284 StringRef MachOObjectFile::getStringTableData() const {
2285 MachO::symtab_command S = getSymtabLoadCommand();
2286 return getData().substr(S.stroff, S.strsize);
2289 bool MachOObjectFile::is64Bit() const {
2290 return getType() == getMachOType(false, true) ||
2291 getType() == getMachOType(true, true);
2294 void MachOObjectFile::ReadULEB128s(uint64_t Index,
2295 SmallVectorImpl<uint64_t> &Out) const {
2296 DataExtractor extractor(ObjectFile::getData(), true, 0);
2298 uint32_t offset = Index;
2300 while (uint64_t delta = extractor.getULEB128(&offset)) {
2302 Out.push_back(data);
2306 bool MachOObjectFile::isRelocatableObject() const {
2307 return getHeader().filetype == MachO::MH_OBJECT;
2310 ErrorOr<std::unique_ptr<MachOObjectFile>>
2311 ObjectFile::createMachOObjectFile(MemoryBufferRef Buffer) {
2312 StringRef Magic = Buffer.getBuffer().slice(0, 4);
2314 std::unique_ptr<MachOObjectFile> Ret;
2315 if (Magic == "\xFE\xED\xFA\xCE")
2316 Ret.reset(new MachOObjectFile(Buffer, false, false, EC));
2317 else if (Magic == "\xCE\xFA\xED\xFE")
2318 Ret.reset(new MachOObjectFile(Buffer, true, false, EC));
2319 else if (Magic == "\xFE\xED\xFA\xCF")
2320 Ret.reset(new MachOObjectFile(Buffer, false, true, EC));
2321 else if (Magic == "\xCF\xFA\xED\xFE")
2322 Ret.reset(new MachOObjectFile(Buffer, true, true, EC));
2324 return object_error::parse_failed;
2328 return std::move(Ret);