+unsigned MachOObjectFile::getArch() const {
+ return getArch(getCPUType(this));
+}
+
+StringRef MachOObjectFile::getLoadName() const {
+ // TODO: Implement
+ report_fatal_error("get_load_name() unimplemented in MachOObjectFile");
+}
+
+relocation_iterator MachOObjectFile::section_rel_begin(unsigned Index) const {
+ DataRefImpl DRI;
+ DRI.d.a = Index;
+ return section_rel_begin(DRI);
+}
+
+relocation_iterator MachOObjectFile::section_rel_end(unsigned Index) const {
+ DataRefImpl DRI;
+ DRI.d.a = Index;
+ return section_rel_end(DRI);
+}
+
+dice_iterator MachOObjectFile::begin_dices() const {
+ DataRefImpl DRI;
+ if (!DataInCodeLoadCmd)
+ return dice_iterator(DiceRef(DRI, this));
+
+ MachO::linkedit_data_command DicLC = getDataInCodeLoadCommand();
+ DRI.p = reinterpret_cast<uintptr_t>(getPtr(this, DicLC.dataoff));
+ return dice_iterator(DiceRef(DRI, this));
+}
+
+dice_iterator MachOObjectFile::end_dices() const {
+ DataRefImpl DRI;
+ if (!DataInCodeLoadCmd)
+ return dice_iterator(DiceRef(DRI, this));
+
+ MachO::linkedit_data_command DicLC = getDataInCodeLoadCommand();
+ unsigned Offset = DicLC.dataoff + DicLC.datasize;
+ DRI.p = reinterpret_cast<uintptr_t>(getPtr(this, Offset));
+ return dice_iterator(DiceRef(DRI, this));
+}
+
+StringRef
+MachOObjectFile::getSectionFinalSegmentName(DataRefImpl Sec) const {
+ ArrayRef<char> Raw = getSectionRawFinalSegmentName(Sec);
+ return parseSegmentOrSectionName(Raw.data());
+}
+
+ArrayRef<char>
+MachOObjectFile::getSectionRawName(DataRefImpl Sec) const {
+ const section_base *Base =
+ reinterpret_cast<const section_base *>(Sections[Sec.d.a]);
+ return ArrayRef<char>(Base->sectname);
+}
+
+ArrayRef<char>
+MachOObjectFile::getSectionRawFinalSegmentName(DataRefImpl Sec) const {
+ const section_base *Base =
+ reinterpret_cast<const section_base *>(Sections[Sec.d.a]);
+ return ArrayRef<char>(Base->segname);
+}
+
+bool
+MachOObjectFile::isRelocationScattered(const MachO::any_relocation_info &RE)
+ const {
+ if (getCPUType(this) == MachO::CPU_TYPE_X86_64)
+ return false;
+ return getPlainRelocationAddress(RE) & MachO::R_SCATTERED;
+}
+
+unsigned MachOObjectFile::getPlainRelocationSymbolNum(
+ const MachO::any_relocation_info &RE) const {
+ if (isLittleEndian())
+ return RE.r_word1 & 0xffffff;
+ return RE.r_word1 >> 8;
+}
+
+bool MachOObjectFile::getPlainRelocationExternal(
+ const MachO::any_relocation_info &RE) const {
+ if (isLittleEndian())
+ return (RE.r_word1 >> 27) & 1;
+ return (RE.r_word1 >> 4) & 1;
+}
+
+bool MachOObjectFile::getScatteredRelocationScattered(
+ const MachO::any_relocation_info &RE) const {
+ return RE.r_word0 >> 31;
+}
+
+uint32_t MachOObjectFile::getScatteredRelocationValue(
+ const MachO::any_relocation_info &RE) const {
+ return RE.r_word1;
+}
+
+unsigned MachOObjectFile::getAnyRelocationAddress(
+ const MachO::any_relocation_info &RE) const {
+ if (isRelocationScattered(RE))
+ return getScatteredRelocationAddress(RE);
+ return getPlainRelocationAddress(RE);
+}
+
+unsigned MachOObjectFile::getAnyRelocationPCRel(
+ const MachO::any_relocation_info &RE) const {
+ if (isRelocationScattered(RE))
+ return getScatteredRelocationPCRel(this, RE);
+ return getPlainRelocationPCRel(this, RE);
+}
+
+unsigned MachOObjectFile::getAnyRelocationLength(
+ const MachO::any_relocation_info &RE) const {
+ if (isRelocationScattered(RE))
+ return getScatteredRelocationLength(RE);
+ return getPlainRelocationLength(this, RE);
+}
+
+unsigned
+MachOObjectFile::getAnyRelocationType(
+ const MachO::any_relocation_info &RE) const {
+ if (isRelocationScattered(RE))
+ return getScatteredRelocationType(RE);
+ return getPlainRelocationType(this, RE);
+}
+
+SectionRef
+MachOObjectFile::getRelocationSection(
+ const MachO::any_relocation_info &RE) const {
+ if (isRelocationScattered(RE) || getPlainRelocationExternal(RE))
+ return *end_sections();
+ unsigned SecNum = getPlainRelocationSymbolNum(RE) - 1;
+ DataRefImpl DRI;
+ DRI.d.a = SecNum;
+ return SectionRef(DRI, this);
+}
+
+MachOObjectFile::LoadCommandInfo
+MachOObjectFile::getFirstLoadCommandInfo() const {
+ MachOObjectFile::LoadCommandInfo Load;
+
+ unsigned HeaderSize = is64Bit() ? sizeof(MachO::mach_header_64) :
+ sizeof(MachO::mach_header);
+ Load.Ptr = getPtr(this, HeaderSize);
+ Load.C = getStruct<MachO::load_command>(this, Load.Ptr);
+ return Load;
+}
+
+MachOObjectFile::LoadCommandInfo
+MachOObjectFile::getNextLoadCommandInfo(const LoadCommandInfo &L) const {
+ MachOObjectFile::LoadCommandInfo Next;
+ Next.Ptr = L.Ptr + L.C.cmdsize;
+ Next.C = getStruct<MachO::load_command>(this, Next.Ptr);
+ return Next;
+}
+
+MachO::section MachOObjectFile::getSection(DataRefImpl DRI) const {
+ return getStruct<MachO::section>(this, Sections[DRI.d.a]);
+}
+
+MachO::section_64 MachOObjectFile::getSection64(DataRefImpl DRI) const {
+ return getStruct<MachO::section_64>(this, Sections[DRI.d.a]);
+}
+
+MachO::section MachOObjectFile::getSection(const LoadCommandInfo &L,
+ unsigned Index) const {
+ const char *Sec = getSectionPtr(this, L, Index);
+ return getStruct<MachO::section>(this, Sec);
+}
+
+MachO::section_64 MachOObjectFile::getSection64(const LoadCommandInfo &L,
+ unsigned Index) const {
+ const char *Sec = getSectionPtr(this, L, Index);
+ return getStruct<MachO::section_64>(this, Sec);
+}
+
+MachO::nlist
+MachOObjectFile::getSymbolTableEntry(DataRefImpl DRI) const {
+ const char *P = reinterpret_cast<const char *>(DRI.p);
+ return getStruct<MachO::nlist>(this, P);
+}
+
+MachO::nlist_64
+MachOObjectFile::getSymbol64TableEntry(DataRefImpl DRI) const {
+ const char *P = reinterpret_cast<const char *>(DRI.p);
+ return getStruct<MachO::nlist_64>(this, P);
+}
+
+MachO::linkedit_data_command
+MachOObjectFile::getLinkeditDataLoadCommand(const LoadCommandInfo &L) const {
+ return getStruct<MachO::linkedit_data_command>(this, L.Ptr);
+}
+
+MachO::segment_command
+MachOObjectFile::getSegmentLoadCommand(const LoadCommandInfo &L) const {
+ return getStruct<MachO::segment_command>(this, L.Ptr);
+}
+
+MachO::segment_command_64
+MachOObjectFile::getSegment64LoadCommand(const LoadCommandInfo &L) const {
+ return getStruct<MachO::segment_command_64>(this, L.Ptr);
+}
+
+MachO::linker_options_command
+MachOObjectFile::getLinkerOptionsLoadCommand(const LoadCommandInfo &L) const {
+ return getStruct<MachO::linker_options_command>(this, L.Ptr);
+}
+
+MachO::any_relocation_info
+MachOObjectFile::getRelocation(DataRefImpl Rel) const {
+ const char *P = reinterpret_cast<const char *>(Rel.p);
+ return getStruct<MachO::any_relocation_info>(this, P);
+}
+
+MachO::data_in_code_entry
+MachOObjectFile::getDice(DataRefImpl Rel) const {
+ const char *P = reinterpret_cast<const char *>(Rel.p);
+ return getStruct<MachO::data_in_code_entry>(this, P);
+}
+
+MachO::mach_header MachOObjectFile::getHeader() const {
+ return getStruct<MachO::mach_header>(this, getPtr(this, 0));
+}
+
+MachO::mach_header_64 MachOObjectFile::getHeader64() const {
+ return getStruct<MachO::mach_header_64>(this, getPtr(this, 0));
+}
+
+uint32_t MachOObjectFile::getIndirectSymbolTableEntry(
+ const MachO::dysymtab_command &DLC,
+ unsigned Index) const {
+ uint64_t Offset = DLC.indirectsymoff + Index * sizeof(uint32_t);
+ return getStruct<uint32_t>(this, getPtr(this, Offset));
+}
+
+MachO::data_in_code_entry
+MachOObjectFile::getDataInCodeTableEntry(uint32_t DataOffset,
+ unsigned Index) const {
+ uint64_t Offset = DataOffset + Index * sizeof(MachO::data_in_code_entry);
+ return getStruct<MachO::data_in_code_entry>(this, getPtr(this, Offset));
+}
+
+MachO::symtab_command MachOObjectFile::getSymtabLoadCommand() const {
+ return getStruct<MachO::symtab_command>(this, SymtabLoadCmd);
+}
+
+MachO::dysymtab_command MachOObjectFile::getDysymtabLoadCommand() const {
+ return getStruct<MachO::dysymtab_command>(this, DysymtabLoadCmd);
+}
+
+MachO::linkedit_data_command
+MachOObjectFile::getDataInCodeLoadCommand() const {
+ if (DataInCodeLoadCmd)
+ return getStruct<MachO::linkedit_data_command>(this, DataInCodeLoadCmd);
+
+ // If there is no DataInCodeLoadCmd return a load command with zero'ed fields.
+ MachO::linkedit_data_command Cmd;
+ Cmd.cmd = MachO::LC_DATA_IN_CODE;
+ Cmd.cmdsize = sizeof(MachO::linkedit_data_command);
+ Cmd.dataoff = 0;
+ Cmd.datasize = 0;
+ return Cmd;
+}
+
+StringRef MachOObjectFile::getStringTableData() const {
+ MachO::symtab_command S = getSymtabLoadCommand();
+ return getData().substr(S.stroff, S.strsize);
+}
+
+bool MachOObjectFile::is64Bit() const {
+ return getType() == getMachOType(false, true) ||
+ getType() == getMachOType(true, true);
+}
+
+void MachOObjectFile::ReadULEB128s(uint64_t Index,
+ SmallVectorImpl<uint64_t> &Out) const {
+ DataExtractor extractor(ObjectFile::getData(), true, 0);
+
+ uint32_t offset = Index;
+ uint64_t data = 0;
+ while (uint64_t delta = extractor.getULEB128(&offset)) {
+ data += delta;
+ Out.push_back(data);
+ }
+}
+
+ErrorOr<ObjectFile *> ObjectFile::createMachOObjectFile(MemoryBuffer *Buffer) {
+ StringRef Magic = Buffer->getBuffer().slice(0, 4);
+ error_code EC;
+ OwningPtr<MachOObjectFile> Ret;
+ if (Magic == "\xFE\xED\xFA\xCE")
+ Ret.reset(new MachOObjectFile(Buffer, false, false, EC));
+ else if (Magic == "\xCE\xFA\xED\xFE")
+ Ret.reset(new MachOObjectFile(Buffer, true, false, EC));
+ else if (Magic == "\xFE\xED\xFA\xCF")
+ Ret.reset(new MachOObjectFile(Buffer, false, true, EC));
+ else if (Magic == "\xCF\xFA\xED\xFE")
+ Ret.reset(new MachOObjectFile(Buffer, true, true, EC));
+ else {
+ delete Buffer;
+ return object_error::parse_failed;
+ }
+
+ if (EC)
+ return EC;
+ return Ret.take();
+}
+