2016-05-12 06:07:45 +08: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"
|
2016-05-18 01:13:50 +08:00
|
|
|
#include "llvm/Support/ErrorHandling.h"
|
2016-05-12 06:07:45 +08:00
|
|
|
#include "llvm/Object/MachOUniversal.h"
|
2016-05-13 00:04:20 +08:00
|
|
|
#include "llvm/ObjectYAML/MachOYAML.h"
|
2016-05-12 06:07:45 +08:00
|
|
|
|
|
|
|
using namespace llvm;
|
|
|
|
|
2016-05-13 00:04:20 +08:00
|
|
|
class MachODumper {
|
|
|
|
|
|
|
|
const object::MachOObjectFile &Obj;
|
|
|
|
|
|
|
|
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-14 01:41:41 +08:00
|
|
|
Expected<std::unique_ptr<MachOYAML::Object>> MachODumper::dump() {
|
2016-05-13 00:04:20 +08:00
|
|
|
auto Y = make_unique<MachOYAML::Object>();
|
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-13 00:04:20 +08:00
|
|
|
|
2016-05-18 01:13:50 +08:00
|
|
|
for (auto load_command : Obj.load_commands()) {
|
|
|
|
auto LC = make_unique<MachOYAML::LoadCommand>();
|
|
|
|
LC->cmd = static_cast<MachO::LoadCommandType>(load_command.C.cmd);
|
|
|
|
LC->cmdsize = load_command.C.cmdsize;
|
2016-05-14 01:41:41 +08:00
|
|
|
Y->LoadCommands.push_back(std::move(LC));
|
|
|
|
}
|
|
|
|
|
2016-05-13 03:57:07 +08:00
|
|
|
return std::move(Y);
|
2016-05-13 00:04:20 +08:00
|
|
|
}
|
|
|
|
|
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();
|
|
|
|
|
|
|
|
yaml::Output Yout(Out);
|
|
|
|
Yout << *(YAML.get());
|
|
|
|
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) {
|
|
|
|
return make_error<Obj2YamlError>(obj2yaml_error::not_implemented);
|
2016-05-12 06:07:45 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
std::error_code macho2yaml(raw_ostream &Out, const object::ObjectFile &Obj) {
|
2016-05-12 09:52:33 +08: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-12 06:07:45 +08:00
|
|
|
|
2016-05-12 09:52:33 +08: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-12 06:07:45 +08:00
|
|
|
|
|
|
|
return obj2yaml_error::unsupported_obj_file_format;
|
|
|
|
}
|