Reapply r269782 "[obj2yaml] [yaml2obj] Support for MachO load command structures""

This adds support for all the MachO *_command structures. The load_command payloads still are not represented, but that will come next.

llvm-svn: 269808
This commit is contained in:
Chris Bieneman 2016-05-17 19:44:06 +00:00
parent 973c1fa5f5
commit 3f2eb8369e
7 changed files with 709 additions and 66 deletions

View File

@ -35,19 +35,18 @@ struct FileHeader {
struct LoadCommand {
virtual ~LoadCommand();
MachO::LoadCommandType cmd;
uint32_t cmdsize;
llvm::MachO::macho_load_command Data;
};
struct Object {
FileHeader Header;
std::vector<std::unique_ptr<LoadCommand>> LoadCommands;
std::vector<LoadCommand> LoadCommands;
};
} // namespace llvm::MachOYAML
} // namespace llvm
LLVM_YAML_IS_SEQUENCE_VECTOR(std::unique_ptr<llvm::MachOYAML::LoadCommand>)
LLVM_YAML_IS_SEQUENCE_VECTOR(llvm::MachOYAML::LoadCommand)
namespace llvm {
namespace yaml {
@ -60,12 +59,11 @@ template <> struct MappingTraits<MachOYAML::Object> {
static void mapping(IO &IO, MachOYAML::Object &Object);
};
template <> struct MappingTraits<std::unique_ptr<MachOYAML::LoadCommand>> {
static void mapping(IO &IO,
std::unique_ptr<MachOYAML::LoadCommand> &LoadCommand);
template <> struct MappingTraits<MachOYAML::LoadCommand> {
static void mapping(IO &IO, MachOYAML::LoadCommand &LoadCommand);
};
#define HANDLE_LOAD_COMMAND(LCName, LCValue, LCStruct) \
#define HANDLE_LOAD_COMMAND(LCName, LCValue, LCStruct) \
io.enumCase(value, #LCName, MachO::LCName);
template <> struct ScalarEnumerationTraits<MachO::LoadCommandType> {
@ -74,7 +72,52 @@ template <> struct ScalarEnumerationTraits<MachO::LoadCommandType> {
}
};
#undef HANDLE_LOAD_COMMAND
// This trait is used for 16-byte chars in Mach structures used for strings
typedef char char_16[16];
template <> struct ScalarTraits<char_16> {
static void output(const char_16 &Val, void *, llvm::raw_ostream &Out);
static StringRef input(StringRef Scalar, void *, char_16 &Val);
static bool mustQuote(StringRef S);
};
// This trait is used for UUIDs. It reads and writes them matching otool's
// formatting style.
typedef uint8_t uuid_t[16];
template <> struct ScalarTraits<uuid_t> {
static void output(const uuid_t &Val, void *, llvm::raw_ostream &Out);
static StringRef input(StringRef Scalar, void *, uuid_t &Val);
static bool mustQuote(StringRef S);
};
// Load Command struct mapping traits
#define LOAD_COMMAND_STRUCT(LCStruct) \
template <> struct MappingTraits<MachO::LCStruct> { \
static void mapping(IO &IO, MachO::LCStruct &LoadCommand); \
};
#include "llvm/Support/MachO.def"
// Extra structures used by load commands
template <> struct MappingTraits<MachO::dylib> {
static void mapping(IO &IO, MachO::dylib &LoadCommand);
};
template <> struct MappingTraits<MachO::fvmlib> {
static void mapping(IO &IO, MachO::fvmlib &LoadCommand);
};
template <> struct MappingTraits<MachO::section> {
static void mapping(IO &IO, MachO::section &LoadCommand);
};
template <> struct MappingTraits<MachO::section_64> {
static void mapping(IO &IO, MachO::section_64 &LoadCommand);
};
} // namespace llvm::yaml

View File

@ -11,9 +11,7 @@
//
//,,,----------------------------------------------------------------------,,,//
#ifndef HANDLE_LOAD_COMMAND
#define HANDLE_LOAD_COMMAND(LCName, LCValue, LCStruct)
#endif
#ifdef HANDLE_LOAD_COMMAND
HANDLE_LOAD_COMMAND(LC_SEGMENT, 0x00000001u, segment_command)
HANDLE_LOAD_COMMAND(LC_SYMTAB, 0x00000002u, symtab_command)
@ -65,3 +63,44 @@ HANDLE_LOAD_COMMAND(LC_LINKER_OPTION, 0x0000002Du, linker_option_command)
HANDLE_LOAD_COMMAND(LC_LINKER_OPTIMIZATION_HINT, 0x0000002Eu, linkedit_data_command)
HANDLE_LOAD_COMMAND(LC_VERSION_MIN_TVOS, 0x0000002Fu, version_min_command)
HANDLE_LOAD_COMMAND(LC_VERSION_MIN_WATCHOS, 0x00000030u, version_min_command)
#endif
#ifdef LOAD_COMMAND_STRUCT
LOAD_COMMAND_STRUCT(dyld_info_command)
LOAD_COMMAND_STRUCT(dylib_command)
LOAD_COMMAND_STRUCT(dylinker_command)
LOAD_COMMAND_STRUCT(dysymtab_command)
LOAD_COMMAND_STRUCT(encryption_info_command)
LOAD_COMMAND_STRUCT(encryption_info_command_64)
LOAD_COMMAND_STRUCT(entry_point_command)
LOAD_COMMAND_STRUCT(fvmfile_command)
LOAD_COMMAND_STRUCT(fvmlib_command)
LOAD_COMMAND_STRUCT(ident_command)
LOAD_COMMAND_STRUCT(linkedit_data_command)
LOAD_COMMAND_STRUCT(linker_option_command)
LOAD_COMMAND_STRUCT(load_command)
LOAD_COMMAND_STRUCT(prebind_cksum_command)
LOAD_COMMAND_STRUCT(prebound_dylib_command)
LOAD_COMMAND_STRUCT(routines_command)
LOAD_COMMAND_STRUCT(routines_command_64)
LOAD_COMMAND_STRUCT(rpath_command)
LOAD_COMMAND_STRUCT(segment_command)
LOAD_COMMAND_STRUCT(segment_command_64)
LOAD_COMMAND_STRUCT(source_version_command)
LOAD_COMMAND_STRUCT(sub_client_command)
LOAD_COMMAND_STRUCT(sub_framework_command)
LOAD_COMMAND_STRUCT(sub_library_command)
LOAD_COMMAND_STRUCT(sub_umbrella_command)
LOAD_COMMAND_STRUCT(symseg_command)
LOAD_COMMAND_STRUCT(symtab_command)
LOAD_COMMAND_STRUCT(thread_command)
LOAD_COMMAND_STRUCT(twolevel_hints_command)
LOAD_COMMAND_STRUCT(uuid_command)
LOAD_COMMAND_STRUCT(version_min_command)
#endif
#undef HANDLE_LOAD_COMMAND
#undef LOAD_COMMAND_STRUCT

View File

@ -1677,6 +1677,13 @@ namespace llvm {
const uint32_t x86_EXCEPTION_STATE_COUNT =
sizeof(x86_exception_state_t) / sizeof(uint32_t);
// Define a union of all load command structs
#define LOAD_COMMAND_STRUCT(LCStruct) LCStruct LCStruct##_data;
union macho_load_command {
#include "llvm/Support/MachO.def"
};
} // end namespace MachO
} // end namespace llvm

View File

@ -13,6 +13,9 @@
#include "llvm/ObjectYAML/MachOYAML.h"
#include "llvm/Support/Casting.h"
#include "llvm/Support/Format.h"
#include <string.h> // For memcpy and memset.
namespace llvm {
@ -20,6 +23,52 @@ MachOYAML::LoadCommand::~LoadCommand() {}
namespace yaml {
void ScalarTraits<char_16>::output(const char_16 &Val, void *,
llvm::raw_ostream &Out) {
Out << Val;
}
StringRef ScalarTraits<char_16>::input(StringRef Scalar, void *, char_16 &Val) {
size_t CopySize = 16 >= Scalar.size() ? 16 : Scalar.size();
memcpy((void *)Val, Scalar.data(), CopySize);
if (Scalar.size() < 16) {
memset((void *)&Val[Scalar.size()], 0, 16 - Scalar.size());
}
return StringRef();
}
bool ScalarTraits<char_16>::mustQuote(StringRef S) { return needsQuotes(S); }
void ScalarTraits<uuid_t>::output(const uuid_t &Val, void *,
llvm::raw_ostream &Out) {
for (int Idx = 0; Idx < 16; ++Idx) {
Out << format("%02" PRIX32, Val[Idx]);
if (Idx == 3 || Idx == 5 || Idx == 7 || Idx == 9)
Out << "-";
}
}
StringRef ScalarTraits<uuid_t>::input(StringRef Scalar, void *, uuid_t &Val) {
size_t OutIdx = 0;
for (size_t Idx = 0; Idx < Scalar.size(); ++Idx) {
if (Scalar[Idx] == '-' || OutIdx >= 16)
continue;
unsigned long long TempInt;
if (getAsUnsignedInteger(Scalar.slice(Idx, Idx + 2), 16, TempInt))
return "invalid number";
if (TempInt > 0xFF)
return "out of range number";
Val[OutIdx] = static_cast<uint8_t>(TempInt);
++Idx; // increment idx an extra time because we're consuming 2 chars
++OutIdx;
}
return StringRef();
}
bool ScalarTraits<uuid_t>::mustQuote(StringRef S) { return needsQuotes(S); }
void MappingTraits<MachOYAML::FileHeader>::mapping(
IO &IO, MachOYAML::FileHeader &FileHdr) {
IO.mapRequired("magic", FileHdr.magic);
@ -46,12 +95,310 @@ void MappingTraits<MachOYAML::Object>::mapping(IO &IO,
IO.setContext(nullptr);
}
void MappingTraits<std::unique_ptr<MachOYAML::LoadCommand>>::mapping(
IO &IO, std::unique_ptr<MachOYAML::LoadCommand> &LoadCommand) {
if (!IO.outputting())
LoadCommand.reset(new MachOYAML::LoadCommand());
IO.mapRequired("cmd", LoadCommand->cmd);
IO.mapRequired("cmdsize", LoadCommand->cmdsize);
void MappingTraits<MachOYAML::LoadCommand>::mapping(
IO &IO, MachOYAML::LoadCommand &LoadCommand) {
IO.mapRequired(
"cmd", (MachO::LoadCommandType &)LoadCommand.Data.load_command_data.cmd);
IO.mapRequired("cmdsize", LoadCommand.Data.load_command_data.cmdsize);
#define HANDLE_LOAD_COMMAND(LCName, LCValue, LCStruct) \
case MachO::LCName: \
MappingTraits<MachO::LCStruct>::mapping(IO, \
LoadCommand.Data.LCStruct##_data); \
break;
switch (LoadCommand.Data.load_command_data.cmd) {
#include "llvm/Support/MachO.def"
}
}
void MappingTraits<MachO::dyld_info_command>::mapping(
IO &IO, MachO::dyld_info_command &LoadCommand) {
IO.mapRequired("rebase_off", LoadCommand.rebase_off);
IO.mapRequired("rebase_size", LoadCommand.rebase_size);
IO.mapRequired("bind_off", LoadCommand.bind_size);
IO.mapRequired("weak_bind_off", LoadCommand.weak_bind_off);
IO.mapRequired("weak_bind_size", LoadCommand.weak_bind_size);
IO.mapRequired("lazy_bind_off", LoadCommand.lazy_bind_size);
IO.mapRequired("export_off", LoadCommand.export_off);
IO.mapRequired("export_size", LoadCommand.export_size);
}
void MappingTraits<MachO::dylib>::mapping(IO &IO, MachO::dylib &DylibStruct) {
IO.mapRequired("name", DylibStruct.name);
IO.mapRequired("timestamp", DylibStruct.timestamp);
IO.mapRequired("current_version", DylibStruct.current_version);
IO.mapRequired("compatibility_version", DylibStruct.compatibility_version);
}
void MappingTraits<MachO::dylib_command>::mapping(
IO &IO, MachO::dylib_command &LoadCommand) {
IO.mapRequired("dylib", LoadCommand.dylib);
}
void MappingTraits<MachO::dylinker_command>::mapping(
IO &IO, MachO::dylinker_command &LoadCommand) {
IO.mapRequired("name", LoadCommand.name);
}
void MappingTraits<MachO::dysymtab_command>::mapping(
IO &IO, MachO::dysymtab_command &LoadCommand) {
IO.mapRequired("ilocalsym", LoadCommand.ilocalsym);
IO.mapRequired("nlocalsym", LoadCommand.nlocalsym);
IO.mapRequired("iextdefsym", LoadCommand.iextdefsym);
IO.mapRequired("nextdefsym", LoadCommand.nextdefsym);
IO.mapRequired("iundefsym", LoadCommand.iundefsym);
IO.mapRequired("nundefsym", LoadCommand.nundefsym);
IO.mapRequired("tocoff", LoadCommand.tocoff);
IO.mapRequired("ntoc", LoadCommand.ntoc);
IO.mapRequired("modtaboff", LoadCommand.modtaboff);
IO.mapRequired("nmodtab", LoadCommand.nmodtab);
IO.mapRequired("extrefsymoff", LoadCommand.extrefsymoff);
IO.mapRequired("nextrefsyms", LoadCommand.nextrefsyms);
IO.mapRequired("indirectsymoff", LoadCommand.indirectsymoff);
IO.mapRequired("nindirectsyms", LoadCommand.nindirectsyms);
IO.mapRequired("extreloff", LoadCommand.extreloff);
IO.mapRequired("nextrel", LoadCommand.nextrel);
IO.mapRequired("locreloff", LoadCommand.locreloff);
IO.mapRequired("nlocrel", LoadCommand.nlocrel);
}
void MappingTraits<MachO::encryption_info_command>::mapping(
IO &IO, MachO::encryption_info_command &LoadCommand) {
IO.mapRequired("cryptoff", LoadCommand.cryptoff);
IO.mapRequired("cryptsize", LoadCommand.cryptsize);
IO.mapRequired("cryptid", LoadCommand.cryptid);
}
void MappingTraits<MachO::encryption_info_command_64>::mapping(
IO &IO, MachO::encryption_info_command_64 &LoadCommand) {
IO.mapRequired("cryptoff", LoadCommand.cryptoff);
IO.mapRequired("cryptsize", LoadCommand.cryptsize);
IO.mapRequired("cryptid", LoadCommand.cryptid);
IO.mapRequired("pad", LoadCommand.pad);
}
void MappingTraits<MachO::entry_point_command>::mapping(
IO &IO, MachO::entry_point_command &LoadCommand) {
IO.mapRequired("entryoff", LoadCommand.entryoff);
IO.mapRequired("stacksize", LoadCommand.stacksize);
}
void MappingTraits<MachO::fvmfile_command>::mapping(
IO &IO, MachO::fvmfile_command &LoadCommand) {
IO.mapRequired("name", LoadCommand.name);
IO.mapRequired("header_addr", LoadCommand.header_addr);
}
void MappingTraits<MachO::fvmlib>::mapping(IO &IO, MachO::fvmlib &FVMLib) {
IO.mapRequired("name", FVMLib.name);
IO.mapRequired("minor_version", FVMLib.minor_version);
IO.mapRequired("header_addr", FVMLib.header_addr);
}
void MappingTraits<MachO::fvmlib_command>::mapping(
IO &IO, MachO::fvmlib_command &LoadCommand) {
IO.mapRequired("fvmlib", LoadCommand.fvmlib);
}
void MappingTraits<MachO::ident_command>::mapping(
IO &IO, MachO::ident_command &LoadCommand) {}
void MappingTraits<MachO::linkedit_data_command>::mapping(
IO &IO, MachO::linkedit_data_command &LoadCommand) {
IO.mapRequired("dataoff", LoadCommand.dataoff);
IO.mapRequired("datasize", LoadCommand.datasize);
}
void MappingTraits<MachO::linker_option_command>::mapping(
IO &IO, MachO::linker_option_command &LoadCommand) {
IO.mapRequired("count", LoadCommand.count);
}
void MappingTraits<MachO::prebind_cksum_command>::mapping(
IO &IO, MachO::prebind_cksum_command &LoadCommand) {
IO.mapRequired("cksum", LoadCommand.cksum);
}
void MappingTraits<MachO::load_command>::mapping(
IO &IO, MachO::load_command &LoadCommand) {}
void MappingTraits<MachO::prebound_dylib_command>::mapping(
IO &IO, MachO::prebound_dylib_command &LoadCommand) {
IO.mapRequired("name", LoadCommand.name);
IO.mapRequired("nmodules", LoadCommand.nmodules);
IO.mapRequired("linked_modules", LoadCommand.linked_modules);
}
void MappingTraits<MachO::routines_command>::mapping(
IO &IO, MachO::routines_command &LoadCommand) {
IO.mapRequired("init_address", LoadCommand.init_address);
IO.mapRequired("init_module", LoadCommand.init_module);
IO.mapRequired("reserved1", LoadCommand.reserved1);
IO.mapRequired("reserved2", LoadCommand.reserved2);
IO.mapRequired("reserved3", LoadCommand.reserved3);
IO.mapRequired("reserved4", LoadCommand.reserved4);
IO.mapRequired("reserved5", LoadCommand.reserved5);
IO.mapRequired("reserved6", LoadCommand.reserved6);
}
void MappingTraits<MachO::routines_command_64>::mapping(
IO &IO, MachO::routines_command_64 &LoadCommand) {
IO.mapRequired("init_address", LoadCommand.init_address);
IO.mapRequired("init_module", LoadCommand.init_module);
IO.mapRequired("reserved1", LoadCommand.reserved1);
IO.mapRequired("reserved2", LoadCommand.reserved2);
IO.mapRequired("reserved3", LoadCommand.reserved3);
IO.mapRequired("reserved4", LoadCommand.reserved4);
IO.mapRequired("reserved5", LoadCommand.reserved5);
IO.mapRequired("reserved6", LoadCommand.reserved6);
}
void MappingTraits<MachO::rpath_command>::mapping(
IO &IO, MachO::rpath_command &LoadCommand) {
IO.mapRequired("path", LoadCommand.path);
}
void MappingTraits<MachO::section>::mapping(IO &IO, MachO::section &Section) {
IO.mapRequired("sectname", Section.sectname);
IO.mapRequired("segname", Section.segname);
IO.mapRequired("addr", Section.addr);
IO.mapRequired("size", Section.size);
IO.mapRequired("offset", Section.offset);
IO.mapRequired("align", Section.align);
IO.mapRequired("reloff", Section.reloff);
IO.mapRequired("nreloc", Section.nreloc);
IO.mapRequired("flags", Section.flags);
IO.mapRequired("reserved1", Section.reserved1);
IO.mapRequired("reserved2", Section.reserved2);
}
void MappingTraits<MachO::section_64>::mapping(IO &IO,
MachO::section_64 &Section) {
IO.mapRequired("sectname", Section.sectname);
IO.mapRequired("segname", Section.segname);
IO.mapRequired("addr", Section.addr);
IO.mapRequired("size", Section.size);
IO.mapRequired("offset", Section.offset);
IO.mapRequired("align", Section.align);
IO.mapRequired("reloff", Section.reloff);
IO.mapRequired("nreloc", Section.nreloc);
IO.mapRequired("flags", Section.flags);
IO.mapRequired("reserved1", Section.reserved1);
IO.mapRequired("reserved2", Section.reserved2);
IO.mapRequired("reserved3", Section.reserved3);
}
void MappingTraits<MachO::segment_command>::mapping(
IO &IO, MachO::segment_command &LoadCommand) {
IO.mapRequired("segname", LoadCommand.segname);
IO.mapRequired("vmaddr", LoadCommand.vmaddr);
IO.mapRequired("vmsize", LoadCommand.vmsize);
IO.mapRequired("fileoff", LoadCommand.fileoff);
IO.mapRequired("filesize", LoadCommand.filesize);
IO.mapRequired("maxprot", LoadCommand.maxprot);
IO.mapRequired("initprot", LoadCommand.initprot);
IO.mapRequired("nsects", LoadCommand.nsects);
IO.mapRequired("flags", LoadCommand.flags);
}
void MappingTraits<MachO::segment_command_64>::mapping(
IO &IO, MachO::segment_command_64 &LoadCommand) {
IO.mapRequired("segname", LoadCommand.segname);
IO.mapRequired("vmaddr", LoadCommand.vmaddr);
IO.mapRequired("vmsize", LoadCommand.vmsize);
IO.mapRequired("fileoff", LoadCommand.fileoff);
IO.mapRequired("filesize", LoadCommand.filesize);
IO.mapRequired("maxprot", LoadCommand.maxprot);
IO.mapRequired("initprot", LoadCommand.initprot);
IO.mapRequired("nsects", LoadCommand.nsects);
IO.mapRequired("flags", LoadCommand.flags);
}
void MappingTraits<MachO::source_version_command>::mapping(
IO &IO, MachO::source_version_command &LoadCommand) {
IO.mapRequired("version", LoadCommand.version);
}
void MappingTraits<MachO::sub_client_command>::mapping(
IO &IO, MachO::sub_client_command &LoadCommand) {
IO.mapRequired("client", LoadCommand.client);
}
void MappingTraits<MachO::sub_framework_command>::mapping(
IO &IO, MachO::sub_framework_command &LoadCommand) {
IO.mapRequired("umbrella", LoadCommand.umbrella);
}
void MappingTraits<MachO::sub_library_command>::mapping(
IO &IO, MachO::sub_library_command &LoadCommand) {
IO.mapRequired("sub_library", LoadCommand.sub_library);
}
void MappingTraits<MachO::sub_umbrella_command>::mapping(
IO &IO, MachO::sub_umbrella_command &LoadCommand) {
IO.mapRequired("sub_umbrella", LoadCommand.sub_umbrella);
}
void MappingTraits<MachO::symseg_command>::mapping(
IO &IO, MachO::symseg_command &LoadCommand) {
IO.mapRequired("offset", LoadCommand.offset);
IO.mapRequired("size", LoadCommand.size);
}
void MappingTraits<MachO::symtab_command>::mapping(
IO &IO, MachO::symtab_command &LoadCommand) {
IO.mapRequired("symoff", LoadCommand.symoff);
IO.mapRequired("nsyms", LoadCommand.nsyms);
IO.mapRequired("stroff", LoadCommand.stroff);
IO.mapRequired("strsize", LoadCommand.strsize);
}
void MappingTraits<MachO::thread_command>::mapping(
IO &IO, MachO::thread_command &LoadCommand) {}
void MappingTraits<MachO::twolevel_hints_command>::mapping(
IO &IO, MachO::twolevel_hints_command &LoadCommand) {
IO.mapRequired("offset", LoadCommand.offset);
IO.mapRequired("nhints", LoadCommand.nhints);
}
void MappingTraits<MachO::uuid_command>::mapping(
IO &IO, MachO::uuid_command &LoadCommand) {
IO.mapRequired("cmdsize", LoadCommand.cmdsize);
IO.mapRequired("uuid", LoadCommand.uuid);
}
void MappingTraits<MachO::version_min_command>::mapping(
IO &IO, MachO::version_min_command &LoadCommand) {
IO.mapRequired("version", LoadCommand.version);
IO.mapRequired("sdk", LoadCommand.sdk);
}
} // namespace llvm::yaml

View File

@ -13,69 +13,246 @@ FileHeader:
LoadCommands:
- cmd: LC_SEGMENT_64
cmdsize: 72
segname: __PAGEZERO
vmaddr: 0
vmsize: 4294967296
fileoff: 0
filesize: 0
maxprot: 0
initprot: 0
nsects: 0
flags: 0
- cmd: LC_SEGMENT_64
cmdsize: 552
segname: __TEXT
vmaddr: 4294967296
vmsize: 8192
fileoff: 0
filesize: 8192
maxprot: 7
initprot: 5
nsects: 6
flags: 0
- cmd: LC_SEGMENT_64
cmdsize: 312
segname: __DATA
vmaddr: 4294975488
vmsize: 4096
fileoff: 8192
filesize: 4096
maxprot: 7
initprot: 3
nsects: 3
flags: 0
- cmd: LC_SEGMENT_64
cmdsize: 72
segname: __LINKEDIT
vmaddr: 4294979584
vmsize: 4096
fileoff: 12288
filesize: 2508
maxprot: 7
initprot: 1
nsects: 0
flags: 0
- cmd: LC_DYLD_INFO_ONLY
cmdsize: 48
rebase_off: 12288
rebase_size: 8
bind_off: 96
weak_bind_off: 0
weak_bind_size: 0
lazy_bind_off: 624
export_off: 13016
export_size: 48
- cmd: LC_SYMTAB
cmdsize: 24
symoff: 13080
nsyms: 30
stroff: 13700
strsize: 1096
- cmd: LC_DYSYMTAB
cmdsize: 80
ilocalsym: 0
nlocalsym: 9
iextdefsym: 9
nextdefsym: 2
iundefsym: 11
nundefsym: 19
tocoff: 0
ntoc: 0
modtaboff: 0
nmodtab: 0
extrefsymoff: 0
nextrefsyms: 0
indirectsymoff: 13560
nindirectsyms: 35
extreloff: 0
nextrel: 0
locreloff: 0
nlocrel: 0
- cmd: LC_LOAD_DYLINKER
cmdsize: 32
name: 12
- cmd: LC_UUID
cmdsize: 24
cmdsize: 24
uuid: 461A1B28-822F-3F38-B670-645419E636F5
- cmd: LC_VERSION_MIN_MACOSX
cmdsize: 16
version: 658176
sdk: 658176
- cmd: LC_SOURCE_VERSION
cmdsize: 16
version: 0
- cmd: LC_MAIN
cmdsize: 24
entryoff: 4448
stacksize: 0
- cmd: LC_LOAD_DYLIB
cmdsize: 48
dylib:
name: 24
timestamp: 2
current_version: 7864576
compatibility_version: 65536
- cmd: LC_LOAD_DYLIB
cmdsize: 56
dylib:
name: 24
timestamp: 2
current_version: 80349697
compatibility_version: 65536
- cmd: LC_FUNCTION_STARTS
cmdsize: 16
dataoff: 13064
datasize: 16
- cmd: LC_DATA_IN_CODE
cmdsize: 16
dataoff: 13080
datasize: 0
...
# CHECK: LoadCommands:
# CHECK: - cmd: LC_SEGMENT_64
# CHECK: cmdsize: 72
# CHECK: - cmd: LC_SEGMENT_64
# CHECK: cmdsize: 552
# CHECK: - cmd: LC_SEGMENT_64
# CHECK: cmdsize: 312
# CHECK: - cmd: LC_SEGMENT_64
# CHECK: cmdsize: 72
# CHECK: - cmd: LC_DYLD_INFO_ONLY
# CHECK: cmdsize: 48
# CHECK: - cmd: LC_SYMTAB
# CHECK: cmdsize: 24
# CHECK: - cmd: LC_DYSYMTAB
# CHECK: cmdsize: 80
# CHECK: - cmd: LC_LOAD_DYLINKER
# CHECK: cmdsize: 32
# CHECK: - cmd: LC_UUID
# CHECK: cmdsize: 24
# CHECK: - cmd: LC_VERSION_MIN_MACOSX
# CHECK: cmdsize: 16
# CHECK: - cmd: LC_SOURCE_VERSION
# CHECK: cmdsize: 16
# CHECK: - cmd: LC_MAIN
# CHECK: cmdsize: 24
# CHECK: - cmd: LC_LOAD_DYLIB
# CHECK: cmdsize: 48
# CHECK: - cmd: LC_LOAD_DYLIB
# CHECK: cmdsize: 56
# CHECK: - cmd: LC_FUNCTION_STARTS
# CHECK: cmdsize: 16
# CHECK: - cmd: LC_DATA_IN_CODE
# CHECK: cmdsize: 16
#CHECK: LoadCommands:
#CHECK: - cmd: LC_SEGMENT_64
#CHECK: cmdsize: 72
#CHECK: segname: __PAGEZERO
#CHECK: vmaddr: 0
#CHECK: vmsize: 4294967296
#CHECK: fileoff: 0
#CHECK: filesize: 0
#CHECK: maxprot: 0
#CHECK: initprot: 0
#CHECK: nsects: 0
#CHECK: flags: 0
#CHECK: - cmd: LC_SEGMENT_64
#CHECK: cmdsize: 552
#CHECK: segname: __TEXT
#CHECK: vmaddr: 4294967296
#CHECK: vmsize: 8192
#CHECK: fileoff: 0
#CHECK: filesize: 8192
#CHECK: maxprot: 7
#CHECK: initprot: 5
#CHECK: nsects: 6
#CHECK: flags: 0
#CHECK: - cmd: LC_SEGMENT_64
#CHECK: cmdsize: 312
#CHECK: segname: __DATA
#CHECK: vmaddr: 4294975488
#CHECK: vmsize: 4096
#CHECK: fileoff: 8192
#CHECK: filesize: 4096
#CHECK: maxprot: 7
#CHECK: initprot: 3
#CHECK: nsects: 3
#CHECK: flags: 0
#CHECK: - cmd: LC_SEGMENT_64
#CHECK: cmdsize: 72
#CHECK: segname: __LINKEDIT
#CHECK: vmaddr: 4294979584
#CHECK: vmsize: 4096
#CHECK: fileoff: 12288
#CHECK: filesize: 2508
#CHECK: maxprot: 7
#CHECK: initprot: 1
#CHECK: nsects: 0
#CHECK: flags: 0
#CHECK: - cmd: LC_DYLD_INFO_ONLY
#CHECK: cmdsize: 48
#CHECK: rebase_off: 12288
#CHECK: rebase_size: 8
#CHECK: bind_off: 96
#CHECK: weak_bind_off: 0
#CHECK: weak_bind_size: 0
#CHECK: lazy_bind_off: 624
#CHECK: export_off: 13016
#CHECK: export_size: 48
#CHECK: - cmd: LC_SYMTAB
#CHECK: cmdsize: 24
#CHECK: symoff: 13080
#CHECK: nsyms: 30
#CHECK: stroff: 13700
#CHECK: strsize: 1096
#CHECK: - cmd: LC_DYSYMTAB
#CHECK: cmdsize: 80
#CHECK: ilocalsym: 0
#CHECK: nlocalsym: 9
#CHECK: iextdefsym: 9
#CHECK: nextdefsym: 2
#CHECK: iundefsym: 11
#CHECK: nundefsym: 19
#CHECK: tocoff: 0
#CHECK: ntoc: 0
#CHECK: modtaboff: 0
#CHECK: nmodtab: 0
#CHECK: extrefsymoff: 0
#CHECK: nextrefsyms: 0
#CHECK: indirectsymoff: 13560
#CHECK: nindirectsyms: 35
#CHECK: extreloff: 0
#CHECK: nextrel: 0
#CHECK: locreloff: 0
#CHECK: nlocrel: 0
#CHECK: - cmd: LC_LOAD_DYLINKER
#CHECK: cmdsize: 32
#CHECK: name: 12
#CHECK: - cmd: LC_UUID
#CHECK: cmdsize: 24
#CHECK: cmdsize: 24
#CHECK: uuid: 461A1B28-822F-3F38-B670-645419E636F5
#CHECK: - cmd: LC_VERSION_MIN_MACOSX
#CHECK: cmdsize: 16
#CHECK: version: 658176
#CHECK: sdk: 658176
#CHECK: - cmd: LC_SOURCE_VERSION
#CHECK: cmdsize: 16
#CHECK: version: 0
#CHECK: - cmd: LC_MAIN
#CHECK: cmdsize: 24
#CHECK: entryoff: 4448
#CHECK: stacksize: 0
#CHECK: - cmd: LC_LOAD_DYLIB
#CHECK: cmdsize: 48
#CHECK: dylib:
#CHECK: name: 24
#CHECK: timestamp: 2
#CHECK: current_version: 7864576
#CHECK: compatibility_version: 65536
#CHECK: - cmd: LC_LOAD_DYLIB
#CHECK: cmdsize: 56
#CHECK: dylib:
#CHECK: name: 24
#CHECK: timestamp: 2
#CHECK: current_version: 80349697
#CHECK: compatibility_version: 65536
#CHECK: - cmd: LC_FUNCTION_STARTS
#CHECK: cmdsize: 16
#CHECK: dataoff: 13064
#CHECK: datasize: 16
#CHECK: - cmd: LC_DATA_IN_CODE
#CHECK: cmdsize: 16
#CHECK: dataoff: 13080
#CHECK: datasize: 0

View File

@ -9,9 +9,9 @@
#include "Error.h"
#include "obj2yaml.h"
#include "llvm/Support/ErrorHandling.h"
#include "llvm/Object/MachOUniversal.h"
#include "llvm/ObjectYAML/MachOYAML.h"
#include "llvm/Support/ErrorHandling.h"
using namespace llvm;
@ -24,6 +24,14 @@ public:
Expected<std::unique_ptr<MachOYAML::Object>> dump();
};
#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); \
break;
Expected<std::unique_ptr<MachOYAML::Object>> MachODumper::dump() {
auto Y = make_unique<MachOYAML::Object>();
Y->Header.magic = Obj.getHeader().magic;
@ -35,10 +43,17 @@ Expected<std::unique_ptr<MachOYAML::Object>> MachODumper::dump() {
Y->Header.flags = Obj.getHeader().flags;
Y->Header.reserved = 0;
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;
for (auto LoadCmd : Obj.load_commands()) {
MachOYAML::LoadCommand LC;
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);
break;
#include "llvm/Support/MachO.def"
}
Y->LoadCommands.push_back(std::move(LC));
}

View File

@ -79,17 +79,32 @@ Error MachOWriter::writeHeader(raw_ostream &OS) {
Error MachOWriter::writeLoadCommands(raw_ostream &OS) {
for (auto &LC : Obj.LoadCommands) {
MachO::load_command LCTemp;
LCTemp.cmd = LC->cmd;
LCTemp.cmdsize = LC->cmdsize;
OS.write(reinterpret_cast<const char *>(&LCTemp),
sizeof(MachO::load_command));
auto remaining_size = LC->cmdsize - sizeof(MachO::load_command);
if (remaining_size > 0) {
size_t BytesWritten = 0;
#define HANDLE_LOAD_COMMAND(LCName, LCValue, LCStruct) \
case MachO::LCName: \
OS.write(reinterpret_cast<const char *>(&(LC.Data.LCStruct##_data)), \
sizeof(MachO::LCStruct)); \
BytesWritten = sizeof(MachO::LCStruct); \
break;
switch (LC.Data.load_command_data.cmd) {
default:
OS.write(reinterpret_cast<const char *>(&(LC.Data.load_command_data)),
sizeof(MachO::load_command));
BytesWritten = sizeof(MachO::load_command);
break;
#include "llvm/Support/MachO.def"
}
auto BytesRemaining =
LC.Data.load_command_data.cmdsize - BytesWritten;
if (BytesRemaining > 0) {
// TODO: Replace all this once the load command data is present in yaml.
std::vector<char> fill_data;
fill_data.insert(fill_data.begin(), remaining_size, 0);
OS.write(fill_data.data(), remaining_size);
// For now I fill with 0xDEADBEEF because it is easy to spot on a hex
// viewer.
std::vector<uint32_t> FillData;
FillData.insert(FillData.begin(), BytesRemaining / 4 + 1, 0xDEADBEEFu);
OS.write(reinterpret_cast<char *>(FillData.data()), BytesRemaining);
}
}
return Error::success();