2016-05-11 22:07:45 +00:00
|
|
|
//===------ macho2yaml.cpp - obj2yaml conversion tool -----------*- C++ -*-===//
|
|
|
|
//
|
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
|
|
|
// This file is distributed under the University of Illinois Open Source
|
|
|
|
// License. See LICENSE.TXT for details.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
#include "Error.h"
|
|
|
|
#include "obj2yaml.h"
|
|
|
|
#include "llvm/Object/MachOUniversal.h"
|
2016-05-12 16:04:20 +00:00
|
|
|
#include "llvm/ObjectYAML/MachOYAML.h"
|
2016-05-17 19:44:06 +00:00
|
|
|
#include "llvm/Support/ErrorHandling.h"
|
2016-05-11 22:07:45 +00:00
|
|
|
|
2016-05-18 16:17:23 +00:00
|
|
|
#include <string.h> // for memcpy
|
|
|
|
|
2016-05-11 22:07:45 +00:00
|
|
|
using namespace llvm;
|
|
|
|
|
2016-05-12 16:04:20 +00:00
|
|
|
class MachODumper {
|
|
|
|
|
2016-05-19 20:54:43 +00:00
|
|
|
template <typename StructType>
|
|
|
|
const char *processLoadCommandData(
|
|
|
|
MachOYAML::LoadCommand &LC,
|
|
|
|
const llvm::object::MachOObjectFile::LoadCommandInfo &LoadCmd);
|
|
|
|
|
2016-05-12 16:04:20 +00:00
|
|
|
const object::MachOObjectFile &Obj;
|
|
|
|
|
|
|
|
public:
|
|
|
|
MachODumper(const object::MachOObjectFile &O) : Obj(O) {}
|
2016-05-13 17:41:41 +00:00
|
|
|
Expected<std::unique_ptr<MachOYAML::Object>> dump();
|
2016-05-12 16:04:20 +00:00
|
|
|
};
|
|
|
|
|
2016-05-17 19:44:06 +00:00
|
|
|
#define HANDLE_LOAD_COMMAND(LCName, LCValue, LCStruct) \
|
|
|
|
case MachO::LCName: \
|
|
|
|
memcpy((void *) & (LC.Data.LCStruct##_data), LoadCmd.Ptr, \
|
|
|
|
sizeof(MachO::LCStruct)); \
|
|
|
|
if (Obj.isLittleEndian() != sys::IsLittleEndianHost) \
|
|
|
|
MachO::swapStruct(LC.Data.LCStruct##_data); \
|
2016-05-19 20:54:43 +00:00
|
|
|
EndPtr = processLoadCommandData<MachO::LCStruct>(LC, LoadCmd); \
|
2016-05-17 19:44:06 +00:00
|
|
|
break;
|
|
|
|
|
2016-05-18 16:17:23 +00:00
|
|
|
template <typename SectionType>
|
2016-05-18 23:22:53 +00:00
|
|
|
MachOYAML::Section constructSectionCommon(SectionType Sec) {
|
2016-05-18 16:17:23 +00: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;
|
|
|
|
return TempSec;
|
|
|
|
}
|
|
|
|
|
2016-05-18 23:22:53 +00:00
|
|
|
template <typename SectionType>
|
|
|
|
MachOYAML::Section constructSection(SectionType Sec);
|
|
|
|
|
|
|
|
template <> MachOYAML::Section constructSection(MachO::section Sec) {
|
|
|
|
MachOYAML::Section TempSec = constructSectionCommon(Sec);
|
|
|
|
TempSec.reserved3 = 0;
|
|
|
|
return TempSec;
|
|
|
|
}
|
|
|
|
|
|
|
|
template <> MachOYAML::Section constructSection(MachO::section_64 Sec) {
|
|
|
|
MachOYAML::Section TempSec = constructSectionCommon(Sec);
|
|
|
|
TempSec.reserved3 = Sec.reserved3;
|
|
|
|
return TempSec;
|
|
|
|
}
|
|
|
|
|
|
|
|
template <typename SectionType, typename SegmentType>
|
2016-05-19 20:54:43 +00:00
|
|
|
const char *
|
|
|
|
extractSections(const llvm::object::MachOObjectFile::LoadCommandInfo &LoadCmd,
|
|
|
|
std::vector<MachOYAML::Section> &Sections,
|
|
|
|
bool IsLittleEndian) {
|
2016-05-18 23:22:53 +00: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++) {
|
|
|
|
if (IsLittleEndian != sys::IsLittleEndianHost) {
|
|
|
|
SectionType Sec;
|
|
|
|
memcpy((void *)&Sec, Curr, sizeof(SectionType));
|
|
|
|
MachO::swapStruct(Sec);
|
|
|
|
Sections.push_back(constructSection(Sec));
|
|
|
|
} else {
|
|
|
|
Sections.push_back(constructSection(*Curr));
|
|
|
|
}
|
|
|
|
}
|
2016-05-19 20:54:43 +00: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) {
|
|
|
|
return extractSections<MachO::section, MachO::segment_command>(
|
|
|
|
LoadCmd, LC.Sections, Obj.isLittleEndian());
|
|
|
|
}
|
|
|
|
|
|
|
|
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>(
|
|
|
|
LoadCmd, LC.Sections, Obj.isLittleEndian());
|
|
|
|
}
|
|
|
|
|
|
|
|
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-18 23:22:53 +00:00
|
|
|
}
|
|
|
|
|
2016-05-13 17:41:41 +00:00
|
|
|
Expected<std::unique_ptr<MachOYAML::Object>> MachODumper::dump() {
|
2016-05-12 16:04:20 +00:00
|
|
|
auto Y = make_unique<MachOYAML::Object>();
|
2016-05-12 17:44:43 +00:00
|
|
|
Y->Header.magic = Obj.getHeader().magic;
|
2016-05-12 16:04:20 +00: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-12 17:44:43 +00:00
|
|
|
Y->Header.sizeofcmds = Obj.getHeader().sizeofcmds;
|
2016-05-12 16:04:20 +00:00
|
|
|
Y->Header.flags = Obj.getHeader().flags;
|
2016-05-16 11:03:56 +00:00
|
|
|
Y->Header.reserved = 0;
|
2016-05-12 16:04:20 +00:00
|
|
|
|
2016-05-17 19:44:06 +00:00
|
|
|
for (auto LoadCmd : Obj.load_commands()) {
|
|
|
|
MachOYAML::LoadCommand LC;
|
2016-05-19 20:54:43 +00:00
|
|
|
const char *EndPtr = LoadCmd.Ptr;
|
2016-05-17 19:44:06 +00: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-19 20:54:43 +00:00
|
|
|
EndPtr = processLoadCommandData<MachO::load_command>(LC, LoadCmd);
|
2016-05-17 19:44:06 +00:00
|
|
|
break;
|
|
|
|
#include "llvm/Support/MachO.def"
|
|
|
|
}
|
2016-05-19 20:54:43 +00: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-18 16:17:23 +00:00
|
|
|
}
|
2016-05-19 20:54:43 +00:00
|
|
|
LC.ZeroPadBytes = RemainingBytes;
|
2016-05-13 17:41:41 +00:00
|
|
|
Y->LoadCommands.push_back(std::move(LC));
|
|
|
|
}
|
|
|
|
|
2016-05-12 19:57:07 +00:00
|
|
|
return std::move(Y);
|
2016-05-12 16:04:20 +00:00
|
|
|
}
|
|
|
|
|
2016-05-12 01:52:33 +00:00
|
|
|
Error macho2yaml(raw_ostream &Out, const object::MachOObjectFile &Obj) {
|
2016-05-12 16:04:20 +00:00
|
|
|
MachODumper Dumper(Obj);
|
2016-05-13 17:41:41 +00:00
|
|
|
Expected<std::unique_ptr<MachOYAML::Object>> YAML = Dumper.dump();
|
2016-05-12 16:04:20 +00:00
|
|
|
if (!YAML)
|
|
|
|
return YAML.takeError();
|
|
|
|
|
|
|
|
yaml::Output Yout(Out);
|
|
|
|
Yout << *(YAML.get());
|
|
|
|
return Error::success();
|
2016-05-11 22:07:45 +00:00
|
|
|
}
|
|
|
|
|
2016-05-12 01:52:33 +00:00
|
|
|
Error macho2yaml(raw_ostream &Out, const object::MachOUniversalBinary &Obj) {
|
|
|
|
return make_error<Obj2YamlError>(obj2yaml_error::not_implemented);
|
2016-05-11 22:07:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
std::error_code macho2yaml(raw_ostream &Out, const object::ObjectFile &Obj) {
|
2016-05-12 01:52:33 +00:00
|
|
|
if (const auto *MachOObj = dyn_cast<object::MachOUniversalBinary>(&Obj)) {
|
|
|
|
if (auto Err = macho2yaml(Out, *MachOObj)) {
|
|
|
|
return errorToErrorCode(std::move(Err));
|
|
|
|
}
|
|
|
|
return obj2yaml_error::success;
|
|
|
|
}
|
2016-05-11 22:07:45 +00:00
|
|
|
|
2016-05-12 01:52:33 +00:00
|
|
|
if (const auto *MachOObj = dyn_cast<object::MachOObjectFile>(&Obj)) {
|
|
|
|
if (auto Err = macho2yaml(Out, *MachOObj)) {
|
|
|
|
return errorToErrorCode(std::move(Err));
|
|
|
|
}
|
|
|
|
return obj2yaml_error::success;
|
|
|
|
}
|
2016-05-11 22:07:45 +00:00
|
|
|
|
|
|
|
return obj2yaml_error::unsupported_obj_file_format;
|
|
|
|
}
|