2016-05-12 06:07:45 +08:00
|
|
|
//===------ macho2yaml.cpp - obj2yaml conversion tool -----------*- C++ -*-===//
|
|
|
|
//
|
2019-01-19 16:50:56 +08:00
|
|
|
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
|
|
|
|
// See https://llvm.org/LICENSE.txt for license information.
|
|
|
|
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
|
2016-05-12 06:07:45 +08:00
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
#include "Error.h"
|
|
|
|
#include "obj2yaml.h"
|
2016-12-06 14:00:49 +08:00
|
|
|
#include "llvm/DebugInfo/DWARF/DWARFContext.h"
|
2016-05-12 06:07:45 +08:00
|
|
|
#include "llvm/Object/MachOUniversal.h"
|
2016-06-28 03:53:53 +08:00
|
|
|
#include "llvm/ObjectYAML/ObjectYAML.h"
|
2016-05-18 03:44:06 +08:00
|
|
|
#include "llvm/Support/ErrorHandling.h"
|
2016-05-26 01:09:07 +08:00
|
|
|
#include "llvm/Support/LEB128.h"
|
2016-05-12 06:07:45 +08:00
|
|
|
|
2016-05-19 00:17:23 +08:00
|
|
|
#include <string.h> // for memcpy
|
|
|
|
|
2016-05-12 06:07:45 +08:00
|
|
|
using namespace llvm;
|
|
|
|
|
2016-05-13 00:04:20 +08:00
|
|
|
class MachODumper {
|
|
|
|
|
2016-05-20 04:54:43 +08:00
|
|
|
template <typename StructType>
|
|
|
|
const char *processLoadCommandData(
|
|
|
|
MachOYAML::LoadCommand &LC,
|
|
|
|
const llvm::object::MachOObjectFile::LoadCommandInfo &LoadCmd);
|
|
|
|
|
2016-05-13 00:04:20 +08:00
|
|
|
const object::MachOObjectFile &Obj;
|
2016-05-26 01:09:07 +08:00
|
|
|
void dumpHeader(std::unique_ptr<MachOYAML::Object> &Y);
|
|
|
|
void dumpLoadCommands(std::unique_ptr<MachOYAML::Object> &Y);
|
|
|
|
void dumpLinkEdit(std::unique_ptr<MachOYAML::Object> &Y);
|
2016-05-27 04:06:14 +08:00
|
|
|
void dumpRebaseOpcodes(std::unique_ptr<MachOYAML::Object> &Y);
|
|
|
|
void dumpBindOpcodes(std::vector<MachOYAML::BindOpcode> &BindOpcodes,
|
|
|
|
ArrayRef<uint8_t> OpcodeBuffer, bool Lazy = false);
|
2016-06-01 01:26:36 +08:00
|
|
|
void dumpExportTrie(std::unique_ptr<MachOYAML::Object> &Y);
|
2016-06-03 06:54:06 +08:00
|
|
|
void dumpSymbols(std::unique_ptr<MachOYAML::Object> &Y);
|
2017-07-20 06:27:28 +08:00
|
|
|
void dumpDebugAbbrev(DWARFContext &DCtx,
|
2016-12-23 05:58:03 +08:00
|
|
|
std::unique_ptr<MachOYAML::Object> &Y);
|
2017-07-20 06:27:28 +08:00
|
|
|
void dumpDebugStrings(DWARFContext &DCtx,
|
2016-12-08 02:52:59 +08:00
|
|
|
std::unique_ptr<MachOYAML::Object> &Y);
|
2016-05-13 00:04:20 +08:00
|
|
|
|
2019-08-20 16:49:07 +08:00
|
|
|
template <typename SectionType>
|
|
|
|
MachOYAML::Section constructSectionCommon(SectionType Sec);
|
|
|
|
template <typename SectionType>
|
|
|
|
MachOYAML::Section constructSection(SectionType Sec);
|
|
|
|
template <typename SectionType, typename SegmentType>
|
|
|
|
const char *
|
|
|
|
extractSections(const llvm::object::MachOObjectFile::LoadCommandInfo &LoadCmd,
|
|
|
|
std::vector<MachOYAML::Section> &Sections);
|
|
|
|
|
2016-05-13 00:04:20 +08:00
|
|
|
public:
|
|
|
|
MachODumper(const object::MachOObjectFile &O) : Obj(O) {}
|
2016-05-14 01:41:41 +08:00
|
|
|
Expected<std::unique_ptr<MachOYAML::Object>> dump();
|
2016-05-13 00:04:20 +08:00
|
|
|
};
|
|
|
|
|
2016-05-18 03:44:06 +08:00
|
|
|
#define HANDLE_LOAD_COMMAND(LCName, LCValue, LCStruct) \
|
|
|
|
case MachO::LCName: \
|
2019-08-20 16:49:07 +08:00
|
|
|
memcpy((void *)&(LC.Data.LCStruct##_data), LoadCmd.Ptr, \
|
2016-05-18 03:44:06 +08:00
|
|
|
sizeof(MachO::LCStruct)); \
|
|
|
|
if (Obj.isLittleEndian() != sys::IsLittleEndianHost) \
|
|
|
|
MachO::swapStruct(LC.Data.LCStruct##_data); \
|
2016-05-20 04:54:43 +08:00
|
|
|
EndPtr = processLoadCommandData<MachO::LCStruct>(LC, LoadCmd); \
|
2016-05-18 03:44:06 +08:00
|
|
|
break;
|
|
|
|
|
2016-05-19 00:17:23 +08:00
|
|
|
template <typename SectionType>
|
2019-08-20 16:49:07 +08:00
|
|
|
MachOYAML::Section MachODumper::constructSectionCommon(SectionType Sec) {
|
2016-05-19 00:17:23 +08:00
|
|
|
MachOYAML::Section TempSec;
|
|
|
|
memcpy(reinterpret_cast<void *>(&TempSec.sectname[0]), &Sec.sectname[0], 16);
|
|
|
|
memcpy(reinterpret_cast<void *>(&TempSec.segname[0]), &Sec.segname[0], 16);
|
|
|
|
TempSec.addr = Sec.addr;
|
|
|
|
TempSec.size = Sec.size;
|
|
|
|
TempSec.offset = Sec.offset;
|
|
|
|
TempSec.align = Sec.align;
|
|
|
|
TempSec.reloff = Sec.reloff;
|
|
|
|
TempSec.nreloc = Sec.nreloc;
|
|
|
|
TempSec.flags = Sec.flags;
|
|
|
|
TempSec.reserved1 = Sec.reserved1;
|
|
|
|
TempSec.reserved2 = Sec.reserved2;
|
|
|
|
TempSec.reserved3 = 0;
|
2019-08-20 16:49:07 +08:00
|
|
|
if (!MachO::isVirtualSection(Sec.flags & MachO::SECTION_TYPE))
|
|
|
|
TempSec.content =
|
|
|
|
yaml::BinaryRef(Obj.getSectionContents(Sec.offset, Sec.size));
|
2016-05-19 00:17:23 +08:00
|
|
|
return TempSec;
|
|
|
|
}
|
|
|
|
|
2019-08-20 16:49:07 +08:00
|
|
|
template <>
|
|
|
|
MachOYAML::Section MachODumper::constructSection(MachO::section Sec) {
|
2016-05-19 07:22:53 +08:00
|
|
|
MachOYAML::Section TempSec = constructSectionCommon(Sec);
|
|
|
|
TempSec.reserved3 = 0;
|
|
|
|
return TempSec;
|
|
|
|
}
|
|
|
|
|
2019-08-20 16:49:07 +08:00
|
|
|
template <>
|
|
|
|
MachOYAML::Section MachODumper::constructSection(MachO::section_64 Sec) {
|
2016-05-19 07:22:53 +08:00
|
|
|
MachOYAML::Section TempSec = constructSectionCommon(Sec);
|
|
|
|
TempSec.reserved3 = Sec.reserved3;
|
|
|
|
return TempSec;
|
|
|
|
}
|
|
|
|
|
|
|
|
template <typename SectionType, typename SegmentType>
|
2019-08-20 16:49:07 +08:00
|
|
|
const char *MachODumper::extractSections(
|
|
|
|
const llvm::object::MachOObjectFile::LoadCommandInfo &LoadCmd,
|
|
|
|
std::vector<MachOYAML::Section> &Sections) {
|
2016-05-19 07:22:53 +08:00
|
|
|
auto End = LoadCmd.Ptr + LoadCmd.C.cmdsize;
|
|
|
|
const SectionType *Curr =
|
|
|
|
reinterpret_cast<const SectionType *>(LoadCmd.Ptr + sizeof(SegmentType));
|
|
|
|
for (; reinterpret_cast<const void *>(Curr) < End; Curr++) {
|
2019-08-20 16:49:07 +08:00
|
|
|
if (Obj.isLittleEndian() != sys::IsLittleEndianHost) {
|
2016-05-19 07:22:53 +08:00
|
|
|
SectionType Sec;
|
|
|
|
memcpy((void *)&Sec, Curr, sizeof(SectionType));
|
|
|
|
MachO::swapStruct(Sec);
|
|
|
|
Sections.push_back(constructSection(Sec));
|
|
|
|
} else {
|
|
|
|
Sections.push_back(constructSection(*Curr));
|
|
|
|
}
|
|
|
|
}
|
2016-05-20 04:54:43 +08:00
|
|
|
return reinterpret_cast<const char *>(Curr);
|
|
|
|
}
|
|
|
|
|
|
|
|
template <typename StructType>
|
|
|
|
const char *MachODumper::processLoadCommandData(
|
|
|
|
MachOYAML::LoadCommand &LC,
|
|
|
|
const llvm::object::MachOObjectFile::LoadCommandInfo &LoadCmd) {
|
|
|
|
return LoadCmd.Ptr + sizeof(StructType);
|
|
|
|
}
|
|
|
|
|
|
|
|
template <>
|
|
|
|
const char *MachODumper::processLoadCommandData<MachO::segment_command>(
|
|
|
|
MachOYAML::LoadCommand &LC,
|
|
|
|
const llvm::object::MachOObjectFile::LoadCommandInfo &LoadCmd) {
|
2019-08-20 16:49:07 +08:00
|
|
|
return extractSections<MachO::section, MachO::segment_command>(LoadCmd,
|
|
|
|
LC.Sections);
|
2016-05-20 04:54:43 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
template <>
|
|
|
|
const char *MachODumper::processLoadCommandData<MachO::segment_command_64>(
|
|
|
|
MachOYAML::LoadCommand &LC,
|
|
|
|
const llvm::object::MachOObjectFile::LoadCommandInfo &LoadCmd) {
|
|
|
|
return extractSections<MachO::section_64, MachO::segment_command_64>(
|
2019-08-20 16:49:07 +08:00
|
|
|
LoadCmd, LC.Sections);
|
2016-05-20 04:54:43 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
template <typename StructType>
|
|
|
|
const char *
|
|
|
|
readString(MachOYAML::LoadCommand &LC,
|
|
|
|
const llvm::object::MachOObjectFile::LoadCommandInfo &LoadCmd) {
|
|
|
|
auto Start = LoadCmd.Ptr + sizeof(StructType);
|
|
|
|
auto MaxSize = LoadCmd.C.cmdsize - sizeof(StructType);
|
|
|
|
auto Size = strnlen(Start, MaxSize);
|
|
|
|
LC.PayloadString = StringRef(Start, Size).str();
|
|
|
|
return Start + Size;
|
|
|
|
}
|
|
|
|
|
|
|
|
template <>
|
|
|
|
const char *MachODumper::processLoadCommandData<MachO::dylib_command>(
|
|
|
|
MachOYAML::LoadCommand &LC,
|
|
|
|
const llvm::object::MachOObjectFile::LoadCommandInfo &LoadCmd) {
|
|
|
|
return readString<MachO::dylib_command>(LC, LoadCmd);
|
|
|
|
}
|
|
|
|
|
|
|
|
template <>
|
|
|
|
const char *MachODumper::processLoadCommandData<MachO::dylinker_command>(
|
|
|
|
MachOYAML::LoadCommand &LC,
|
|
|
|
const llvm::object::MachOObjectFile::LoadCommandInfo &LoadCmd) {
|
|
|
|
return readString<MachO::dylinker_command>(LC, LoadCmd);
|
2016-05-19 07:22:53 +08:00
|
|
|
}
|
|
|
|
|
2016-06-01 01:26:36 +08:00
|
|
|
template <>
|
|
|
|
const char *MachODumper::processLoadCommandData<MachO::rpath_command>(
|
|
|
|
MachOYAML::LoadCommand &LC,
|
|
|
|
const llvm::object::MachOObjectFile::LoadCommandInfo &LoadCmd) {
|
|
|
|
return readString<MachO::rpath_command>(LC, LoadCmd);
|
|
|
|
}
|
|
|
|
|
2017-01-24 04:07:55 +08:00
|
|
|
template <>
|
|
|
|
const char *MachODumper::processLoadCommandData<MachO::build_version_command>(
|
|
|
|
MachOYAML::LoadCommand &LC,
|
|
|
|
const llvm::object::MachOObjectFile::LoadCommandInfo &LoadCmd) {
|
|
|
|
auto Start = LoadCmd.Ptr + sizeof(MachO::build_version_command);
|
|
|
|
auto NTools = LC.Data.build_version_command_data.ntools;
|
|
|
|
for (unsigned i = 0; i < NTools; ++i) {
|
|
|
|
auto Curr = Start + i * sizeof(MachO::build_tool_version);
|
|
|
|
MachO::build_tool_version BV;
|
|
|
|
memcpy((void *)&BV, Curr, sizeof(MachO::build_tool_version));
|
|
|
|
if (Obj.isLittleEndian() != sys::IsLittleEndianHost)
|
|
|
|
MachO::swapStruct(BV);
|
|
|
|
LC.Tools.push_back(BV);
|
|
|
|
}
|
|
|
|
return Start + NTools * sizeof(MachO::build_tool_version);
|
|
|
|
}
|
|
|
|
|
2016-05-14 01:41:41 +08:00
|
|
|
Expected<std::unique_ptr<MachOYAML::Object>> MachODumper::dump() {
|
2019-08-15 23:54:37 +08:00
|
|
|
auto Y = std::make_unique<MachOYAML::Object>();
|
2016-12-23 05:58:03 +08:00
|
|
|
Y->IsLittleEndian = Obj.isLittleEndian();
|
2016-05-26 01:09:07 +08:00
|
|
|
dumpHeader(Y);
|
|
|
|
dumpLoadCommands(Y);
|
|
|
|
dumpLinkEdit(Y);
|
2016-12-08 05:47:28 +08:00
|
|
|
|
2017-07-20 06:27:28 +08:00
|
|
|
std::unique_ptr<DWARFContext> DICtx = DWARFContext::create(Obj);
|
|
|
|
if (auto Err = dwarf2yaml(*DICtx, Y->DWARF))
|
2020-02-10 23:06:45 +08:00
|
|
|
return std::move(Err);
|
|
|
|
return std::move(Y);
|
2016-05-26 01:09:07 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void MachODumper::dumpHeader(std::unique_ptr<MachOYAML::Object> &Y) {
|
2016-05-13 01:44:43 +08:00
|
|
|
Y->Header.magic = Obj.getHeader().magic;
|
2016-05-13 00:04:20 +08:00
|
|
|
Y->Header.cputype = Obj.getHeader().cputype;
|
|
|
|
Y->Header.cpusubtype = Obj.getHeader().cpusubtype;
|
|
|
|
Y->Header.filetype = Obj.getHeader().filetype;
|
|
|
|
Y->Header.ncmds = Obj.getHeader().ncmds;
|
2016-05-13 01:44:43 +08:00
|
|
|
Y->Header.sizeofcmds = Obj.getHeader().sizeofcmds;
|
2016-05-13 00:04:20 +08:00
|
|
|
Y->Header.flags = Obj.getHeader().flags;
|
2016-05-16 19:03:56 +08:00
|
|
|
Y->Header.reserved = 0;
|
2016-05-26 01:09:07 +08:00
|
|
|
}
|
2016-05-13 00:04:20 +08:00
|
|
|
|
2016-05-26 01:09:07 +08:00
|
|
|
void MachODumper::dumpLoadCommands(std::unique_ptr<MachOYAML::Object> &Y) {
|
2016-05-18 03:44:06 +08:00
|
|
|
for (auto LoadCmd : Obj.load_commands()) {
|
|
|
|
MachOYAML::LoadCommand LC;
|
2016-05-20 04:54:43 +08:00
|
|
|
const char *EndPtr = LoadCmd.Ptr;
|
2016-05-18 03:44:06 +08:00
|
|
|
switch (LoadCmd.C.cmd) {
|
|
|
|
default:
|
|
|
|
memcpy((void *)&(LC.Data.load_command_data), LoadCmd.Ptr,
|
|
|
|
sizeof(MachO::load_command));
|
|
|
|
if (Obj.isLittleEndian() != sys::IsLittleEndianHost)
|
|
|
|
MachO::swapStruct(LC.Data.load_command_data);
|
2016-05-20 04:54:43 +08:00
|
|
|
EndPtr = processLoadCommandData<MachO::load_command>(LC, LoadCmd);
|
2016-05-18 03:44:06 +08:00
|
|
|
break;
|
2017-06-07 11:48:56 +08:00
|
|
|
#include "llvm/BinaryFormat/MachO.def"
|
2016-05-18 03:44:06 +08:00
|
|
|
}
|
2016-05-20 04:54:43 +08:00
|
|
|
auto RemainingBytes = LoadCmd.C.cmdsize - (EndPtr - LoadCmd.Ptr);
|
|
|
|
if (!std::all_of(EndPtr, &EndPtr[RemainingBytes],
|
|
|
|
[](const char C) { return C == 0; })) {
|
|
|
|
LC.PayloadBytes.insert(LC.PayloadBytes.end(), EndPtr,
|
|
|
|
&EndPtr[RemainingBytes]);
|
|
|
|
RemainingBytes = 0;
|
2016-05-19 00:17:23 +08:00
|
|
|
}
|
2016-05-20 04:54:43 +08:00
|
|
|
LC.ZeroPadBytes = RemainingBytes;
|
2016-05-14 01:41:41 +08:00
|
|
|
Y->LoadCommands.push_back(std::move(LC));
|
|
|
|
}
|
2016-05-26 01:09:07 +08:00
|
|
|
}
|
2016-05-14 01:41:41 +08:00
|
|
|
|
2016-05-26 01:09:07 +08:00
|
|
|
void MachODumper::dumpLinkEdit(std::unique_ptr<MachOYAML::Object> &Y) {
|
2016-05-27 04:06:14 +08:00
|
|
|
dumpRebaseOpcodes(Y);
|
|
|
|
dumpBindOpcodes(Y->LinkEdit.BindOpcodes, Obj.getDyldInfoBindOpcodes());
|
2016-05-27 04:50:05 +08:00
|
|
|
dumpBindOpcodes(Y->LinkEdit.WeakBindOpcodes,
|
|
|
|
Obj.getDyldInfoWeakBindOpcodes());
|
2016-06-01 01:26:36 +08:00
|
|
|
dumpBindOpcodes(Y->LinkEdit.LazyBindOpcodes, Obj.getDyldInfoLazyBindOpcodes(),
|
|
|
|
true);
|
|
|
|
dumpExportTrie(Y);
|
2016-06-03 06:54:06 +08:00
|
|
|
dumpSymbols(Y);
|
2016-05-27 04:06:14 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void MachODumper::dumpRebaseOpcodes(std::unique_ptr<MachOYAML::Object> &Y) {
|
2016-05-26 01:09:07 +08:00
|
|
|
MachOYAML::LinkEditData &LEData = Y->LinkEdit;
|
|
|
|
|
|
|
|
auto RebaseOpcodes = Obj.getDyldInfoRebaseOpcodes();
|
|
|
|
for (auto OpCode = RebaseOpcodes.begin(); OpCode != RebaseOpcodes.end();
|
|
|
|
++OpCode) {
|
|
|
|
MachOYAML::RebaseOpcode RebaseOp;
|
|
|
|
RebaseOp.Opcode =
|
|
|
|
static_cast<MachO::RebaseOpcode>(*OpCode & MachO::REBASE_OPCODE_MASK);
|
|
|
|
RebaseOp.Imm = *OpCode & MachO::REBASE_IMMEDIATE_MASK;
|
|
|
|
|
|
|
|
unsigned Count;
|
|
|
|
uint64_t ULEB = 0;
|
|
|
|
|
|
|
|
switch (RebaseOp.Opcode) {
|
|
|
|
case MachO::REBASE_OPCODE_DO_REBASE_ULEB_TIMES_SKIPPING_ULEB:
|
|
|
|
|
|
|
|
ULEB = decodeULEB128(OpCode + 1, &Count);
|
|
|
|
RebaseOp.ExtraData.push_back(ULEB);
|
|
|
|
OpCode += Count;
|
2017-05-31 09:54:18 +08:00
|
|
|
LLVM_FALLTHROUGH;
|
2016-05-26 01:09:07 +08:00
|
|
|
// Intentionally no break here -- This opcode has two ULEB values
|
|
|
|
case MachO::REBASE_OPCODE_SET_SEGMENT_AND_OFFSET_ULEB:
|
|
|
|
case MachO::REBASE_OPCODE_ADD_ADDR_ULEB:
|
|
|
|
case MachO::REBASE_OPCODE_DO_REBASE_ULEB_TIMES:
|
|
|
|
case MachO::REBASE_OPCODE_DO_REBASE_ADD_ADDR_ULEB:
|
|
|
|
|
|
|
|
ULEB = decodeULEB128(OpCode + 1, &Count);
|
|
|
|
RebaseOp.ExtraData.push_back(ULEB);
|
|
|
|
OpCode += Count;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
LEData.RebaseOpcodes.push_back(RebaseOp);
|
|
|
|
|
|
|
|
if (RebaseOp.Opcode == MachO::REBASE_OPCODE_DONE)
|
|
|
|
break;
|
2016-05-27 04:06:14 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-06-01 01:26:36 +08:00
|
|
|
StringRef ReadStringRef(const uint8_t *Start) {
|
|
|
|
const uint8_t *Itr = Start;
|
|
|
|
for (; *Itr; ++Itr)
|
|
|
|
;
|
|
|
|
return StringRef(reinterpret_cast<const char *>(Start), Itr - Start);
|
|
|
|
}
|
|
|
|
|
2016-05-27 04:06:14 +08:00
|
|
|
void MachODumper::dumpBindOpcodes(
|
|
|
|
std::vector<MachOYAML::BindOpcode> &BindOpcodes,
|
|
|
|
ArrayRef<uint8_t> OpcodeBuffer, bool Lazy) {
|
|
|
|
for (auto OpCode = OpcodeBuffer.begin(); OpCode != OpcodeBuffer.end();
|
|
|
|
++OpCode) {
|
|
|
|
MachOYAML::BindOpcode BindOp;
|
|
|
|
BindOp.Opcode =
|
|
|
|
static_cast<MachO::BindOpcode>(*OpCode & MachO::BIND_OPCODE_MASK);
|
|
|
|
BindOp.Imm = *OpCode & MachO::BIND_IMMEDIATE_MASK;
|
|
|
|
|
|
|
|
unsigned Count;
|
|
|
|
uint64_t ULEB = 0;
|
|
|
|
int64_t SLEB = 0;
|
|
|
|
|
|
|
|
switch (BindOp.Opcode) {
|
|
|
|
case MachO::BIND_OPCODE_DO_BIND_ULEB_TIMES_SKIPPING_ULEB:
|
|
|
|
ULEB = decodeULEB128(OpCode + 1, &Count);
|
|
|
|
BindOp.ULEBExtraData.push_back(ULEB);
|
|
|
|
OpCode += Count;
|
2017-05-31 09:54:18 +08:00
|
|
|
LLVM_FALLTHROUGH;
|
2016-05-27 04:06:14 +08:00
|
|
|
// Intentionally no break here -- this opcode has two ULEB values
|
|
|
|
|
|
|
|
case MachO::BIND_OPCODE_SET_DYLIB_ORDINAL_ULEB:
|
|
|
|
case MachO::BIND_OPCODE_SET_SEGMENT_AND_OFFSET_ULEB:
|
|
|
|
case MachO::BIND_OPCODE_ADD_ADDR_ULEB:
|
|
|
|
case MachO::BIND_OPCODE_DO_BIND_ADD_ADDR_ULEB:
|
|
|
|
ULEB = decodeULEB128(OpCode + 1, &Count);
|
|
|
|
BindOp.ULEBExtraData.push_back(ULEB);
|
|
|
|
OpCode += Count;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case MachO::BIND_OPCODE_SET_ADDEND_SLEB:
|
|
|
|
SLEB = decodeSLEB128(OpCode + 1, &Count);
|
|
|
|
BindOp.SLEBExtraData.push_back(SLEB);
|
|
|
|
OpCode += Count;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case MachO::BIND_OPCODE_SET_SYMBOL_TRAILING_FLAGS_IMM:
|
2016-06-01 01:26:36 +08:00
|
|
|
BindOp.Symbol = ReadStringRef(OpCode + 1);
|
|
|
|
OpCode += BindOp.Symbol.size() + 1;
|
2016-05-27 04:06:14 +08:00
|
|
|
break;
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
BindOpcodes.push_back(BindOp);
|
|
|
|
|
|
|
|
// Lazy bindings have DONE opcodes between operations, so we need to keep
|
|
|
|
// processing after a DONE.
|
|
|
|
if (!Lazy && BindOp.Opcode == MachO::BIND_OPCODE_DONE)
|
|
|
|
break;
|
2016-05-26 01:09:07 +08:00
|
|
|
}
|
2016-05-13 00:04:20 +08:00
|
|
|
}
|
|
|
|
|
2016-06-01 01:26:36 +08:00
|
|
|
/*!
|
|
|
|
* /brief processes a node from the export trie, and its children.
|
|
|
|
*
|
|
|
|
* To my knowledge there is no documentation of the encoded format of this data
|
|
|
|
* other than in the heads of the Apple linker engineers. To that end hopefully
|
|
|
|
* this comment and the implementation below can serve to light the way for
|
|
|
|
* anyone crazy enough to come down this path in the future.
|
|
|
|
*
|
|
|
|
* This function reads and preserves the trie structure of the export trie. To
|
|
|
|
* my knowledge there is no code anywhere else that reads the data and preserves
|
|
|
|
* the Trie. LD64 (sources available at opensource.apple.com) has a similar
|
|
|
|
* implementation that parses the export trie into a vector. That code as well
|
|
|
|
* as LLVM's libObject MachO implementation were the basis for this.
|
|
|
|
*
|
|
|
|
* The export trie is an encoded trie. The node serialization is a bit awkward.
|
|
|
|
* The below pseudo-code is the best description I've come up with for it.
|
|
|
|
*
|
|
|
|
* struct SerializedNode {
|
|
|
|
* ULEB128 TerminalSize;
|
|
|
|
* struct TerminalData { <-- This is only present if TerminalSize > 0
|
|
|
|
* ULEB128 Flags;
|
|
|
|
* ULEB128 Address; <-- Present if (! Flags & REEXPORT )
|
|
|
|
* ULEB128 Other; <-- Present if ( Flags & REEXPORT ||
|
|
|
|
* Flags & STUB_AND_RESOLVER )
|
|
|
|
* char[] ImportName; <-- Present if ( Flags & REEXPORT )
|
|
|
|
* }
|
|
|
|
* uint8_t ChildrenCount;
|
|
|
|
* Pair<char[], ULEB128> ChildNameOffsetPair[ChildrenCount];
|
|
|
|
* SerializedNode Children[ChildrenCount]
|
|
|
|
* }
|
|
|
|
*
|
|
|
|
* Terminal nodes are nodes that represent actual exports. They can appear
|
|
|
|
* anywhere in the tree other than at the root; they do not need to be leaf
|
|
|
|
* nodes. When reading the data out of the trie this routine reads it in-order,
|
|
|
|
* but it puts the child names and offsets directly into the child nodes. This
|
|
|
|
* results in looping over the children twice during serialization and
|
|
|
|
* de-serialization, but it makes the YAML representation more human readable.
|
|
|
|
*
|
|
|
|
* Below is an example of the graph from a "Hello World" executable:
|
|
|
|
*
|
|
|
|
* -------
|
|
|
|
* | '' |
|
|
|
|
* -------
|
|
|
|
* |
|
|
|
|
* -------
|
|
|
|
* | '_' |
|
|
|
|
* -------
|
|
|
|
* |
|
|
|
|
* |----------------------------------------|
|
|
|
|
* | |
|
|
|
|
* ------------------------ ---------------------
|
|
|
|
* | '_mh_execute_header' | | 'main' |
|
|
|
|
* | Flags: 0x00000000 | | Flags: 0x00000000 |
|
|
|
|
* | Addr: 0x00000000 | | Addr: 0x00001160 |
|
|
|
|
* ------------------------ ---------------------
|
|
|
|
*
|
|
|
|
* This graph represents the trie for the exports "__mh_execute_header" and
|
|
|
|
* "_main". In the graph only the "_main" and "__mh_execute_header" nodes are
|
|
|
|
* terminal.
|
|
|
|
*/
|
|
|
|
|
|
|
|
const uint8_t *processExportNode(const uint8_t *CurrPtr,
|
|
|
|
const uint8_t *const End,
|
|
|
|
MachOYAML::ExportEntry &Entry) {
|
|
|
|
if (CurrPtr >= End)
|
|
|
|
return CurrPtr;
|
|
|
|
unsigned Count = 0;
|
|
|
|
Entry.TerminalSize = decodeULEB128(CurrPtr, &Count);
|
|
|
|
CurrPtr += Count;
|
|
|
|
if (Entry.TerminalSize != 0) {
|
|
|
|
Entry.Flags = decodeULEB128(CurrPtr, &Count);
|
|
|
|
CurrPtr += Count;
|
|
|
|
if (Entry.Flags & MachO::EXPORT_SYMBOL_FLAGS_REEXPORT) {
|
|
|
|
Entry.Address = 0;
|
|
|
|
Entry.Other = decodeULEB128(CurrPtr, &Count);
|
|
|
|
CurrPtr += Count;
|
|
|
|
Entry.ImportName = std::string(reinterpret_cast<const char *>(CurrPtr));
|
|
|
|
} else {
|
|
|
|
Entry.Address = decodeULEB128(CurrPtr, &Count);
|
|
|
|
CurrPtr += Count;
|
|
|
|
if (Entry.Flags & MachO::EXPORT_SYMBOL_FLAGS_STUB_AND_RESOLVER) {
|
|
|
|
Entry.Other = decodeULEB128(CurrPtr, &Count);
|
|
|
|
CurrPtr += Count;
|
|
|
|
} else
|
|
|
|
Entry.Other = 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
uint8_t childrenCount = *CurrPtr++;
|
|
|
|
if (childrenCount == 0)
|
|
|
|
return CurrPtr;
|
|
|
|
|
|
|
|
Entry.Children.insert(Entry.Children.begin(), (size_t)childrenCount,
|
|
|
|
MachOYAML::ExportEntry());
|
|
|
|
for (auto &Child : Entry.Children) {
|
|
|
|
Child.Name = std::string(reinterpret_cast<const char *>(CurrPtr));
|
|
|
|
CurrPtr += Child.Name.length() + 1;
|
|
|
|
Child.NodeOffset = decodeULEB128(CurrPtr, &Count);
|
|
|
|
CurrPtr += Count;
|
|
|
|
}
|
|
|
|
for (auto &Child : Entry.Children) {
|
|
|
|
CurrPtr = processExportNode(CurrPtr, End, Child);
|
|
|
|
}
|
|
|
|
return CurrPtr;
|
|
|
|
}
|
|
|
|
|
|
|
|
void MachODumper::dumpExportTrie(std::unique_ptr<MachOYAML::Object> &Y) {
|
|
|
|
MachOYAML::LinkEditData &LEData = Y->LinkEdit;
|
|
|
|
auto ExportsTrie = Obj.getDyldInfoExportsTrie();
|
|
|
|
processExportNode(ExportsTrie.begin(), ExportsTrie.end(), LEData.ExportTrie);
|
|
|
|
}
|
|
|
|
|
2016-06-03 06:54:06 +08:00
|
|
|
template <typename nlist_t>
|
|
|
|
MachOYAML::NListEntry constructNameList(const nlist_t &nlist) {
|
|
|
|
MachOYAML::NListEntry NL;
|
|
|
|
NL.n_strx = nlist.n_strx;
|
|
|
|
NL.n_type = nlist.n_type;
|
|
|
|
NL.n_sect = nlist.n_sect;
|
|
|
|
NL.n_desc = nlist.n_desc;
|
|
|
|
NL.n_value = nlist.n_value;
|
|
|
|
return NL;
|
|
|
|
}
|
|
|
|
|
|
|
|
void MachODumper::dumpSymbols(std::unique_ptr<MachOYAML::Object> &Y) {
|
|
|
|
MachOYAML::LinkEditData &LEData = Y->LinkEdit;
|
|
|
|
|
|
|
|
for (auto Symbol : Obj.symbols()) {
|
|
|
|
MachOYAML::NListEntry NLE =
|
2016-12-23 05:58:03 +08:00
|
|
|
Obj.is64Bit()
|
|
|
|
? constructNameList<MachO::nlist_64>(
|
|
|
|
Obj.getSymbol64TableEntry(Symbol.getRawDataRefImpl()))
|
|
|
|
: constructNameList<MachO::nlist>(
|
|
|
|
Obj.getSymbolTableEntry(Symbol.getRawDataRefImpl()));
|
2016-06-03 06:54:06 +08:00
|
|
|
LEData.NameList.push_back(NLE);
|
|
|
|
}
|
|
|
|
|
|
|
|
StringRef RemainingTable = Obj.getStringTableData();
|
|
|
|
while (RemainingTable.size() > 0) {
|
|
|
|
auto SymbolPair = RemainingTable.split('\0');
|
|
|
|
RemainingTable = SymbolPair.second;
|
|
|
|
LEData.StringTable.push_back(SymbolPair.first);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-05-12 09:52:33 +08:00
|
|
|
Error macho2yaml(raw_ostream &Out, const object::MachOObjectFile &Obj) {
|
2016-05-13 00:04:20 +08:00
|
|
|
MachODumper Dumper(Obj);
|
2016-05-14 01:41:41 +08:00
|
|
|
Expected<std::unique_ptr<MachOYAML::Object>> YAML = Dumper.dump();
|
2016-05-13 00:04:20 +08:00
|
|
|
if (!YAML)
|
|
|
|
return YAML.takeError();
|
|
|
|
|
2016-06-28 03:53:53 +08:00
|
|
|
yaml::YamlObjectFile YAMLFile;
|
|
|
|
YAMLFile.MachO = std::move(YAML.get());
|
|
|
|
|
2016-05-13 00:04:20 +08:00
|
|
|
yaml::Output Yout(Out);
|
2016-06-28 03:53:53 +08:00
|
|
|
Yout << YAMLFile;
|
2016-05-13 00:04:20 +08:00
|
|
|
return Error::success();
|
2016-05-12 06:07:45 +08:00
|
|
|
}
|
|
|
|
|
2016-05-12 09:52:33 +08:00
|
|
|
Error macho2yaml(raw_ostream &Out, const object::MachOUniversalBinary &Obj) {
|
2016-06-28 03:53:53 +08:00
|
|
|
yaml::YamlObjectFile YAMLFile;
|
|
|
|
YAMLFile.FatMachO.reset(new MachOYAML::UniversalBinary());
|
|
|
|
MachOYAML::UniversalBinary &YAML = *YAMLFile.FatMachO;
|
2016-06-25 04:42:28 +08:00
|
|
|
YAML.Header.magic = Obj.getMagic();
|
|
|
|
YAML.Header.nfat_arch = Obj.getNumberOfObjects();
|
|
|
|
|
|
|
|
for (auto Slice : Obj.objects()) {
|
|
|
|
MachOYAML::FatArch arch;
|
|
|
|
arch.cputype = Slice.getCPUType();
|
|
|
|
arch.cpusubtype = Slice.getCPUSubType();
|
|
|
|
arch.offset = Slice.getOffset();
|
|
|
|
arch.size = Slice.getSize();
|
|
|
|
arch.align = Slice.getAlign();
|
|
|
|
arch.reserved = Slice.getReserved();
|
|
|
|
YAML.FatArchs.push_back(arch);
|
|
|
|
|
|
|
|
auto SliceObj = Slice.getAsObjectFile();
|
|
|
|
if (!SliceObj)
|
|
|
|
return SliceObj.takeError();
|
|
|
|
|
|
|
|
MachODumper Dumper(*SliceObj.get());
|
|
|
|
Expected<std::unique_ptr<MachOYAML::Object>> YAMLObj = Dumper.dump();
|
|
|
|
if (!YAMLObj)
|
|
|
|
return YAMLObj.takeError();
|
|
|
|
YAML.Slices.push_back(*YAMLObj.get());
|
|
|
|
}
|
|
|
|
|
|
|
|
yaml::Output Yout(Out);
|
|
|
|
Yout << YAML;
|
|
|
|
return Error::success();
|
2016-05-12 06:07:45 +08:00
|
|
|
}
|
|
|
|
|
2019-12-25 15:05:56 +08:00
|
|
|
Error macho2yaml(raw_ostream &Out, const object::Binary &Binary) {
|
2016-06-25 04:42:28 +08:00
|
|
|
if (const auto *MachOObj = dyn_cast<object::MachOUniversalBinary>(&Binary)) {
|
2016-05-12 09:52:33 +08:00
|
|
|
if (auto Err = macho2yaml(Out, *MachOObj)) {
|
2019-12-25 15:05:56 +08:00
|
|
|
return Err;
|
2016-05-12 09:52:33 +08:00
|
|
|
}
|
2019-12-25 15:05:56 +08:00
|
|
|
return Error::success();
|
2016-05-12 09:52:33 +08:00
|
|
|
}
|
2016-05-12 06:07:45 +08:00
|
|
|
|
2016-06-25 04:42:28 +08:00
|
|
|
if (const auto *MachOObj = dyn_cast<object::MachOObjectFile>(&Binary)) {
|
2016-05-12 09:52:33 +08:00
|
|
|
if (auto Err = macho2yaml(Out, *MachOObj)) {
|
2019-12-25 15:05:56 +08:00
|
|
|
return Err;
|
2016-05-12 09:52:33 +08:00
|
|
|
}
|
2019-12-25 15:05:56 +08:00
|
|
|
return Error::success();
|
2016-05-12 09:52:33 +08:00
|
|
|
}
|
2016-12-23 05:58:03 +08:00
|
|
|
|
2019-12-25 15:05:56 +08:00
|
|
|
return errorCodeToError(obj2yaml_error::unsupported_obj_file_format);
|
2016-05-12 06:07:45 +08:00
|
|
|
}
|