diff options
author | Chris Bieneman <beanz@apple.com> | 2016-05-17 17:03:28 +0000 |
---|---|---|
committer | Chris Bieneman <beanz@apple.com> | 2016-05-17 17:03:28 +0000 |
commit | 4a038a7aace80320451867692fa42b233f92277f (patch) | |
tree | 4d92830e1d3dc443e7f3c98409467f11ef7612ea | |
parent | 87749e8b5d20c024b1d6fcfc6008e020352e684d (diff) |
[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.
git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@269782 91177308-0d34-0410-b5e6-96231b3b80d8
-rw-r--r-- | include/llvm/ObjectYAML/MachOYAML.h | 61 | ||||
-rw-r--r-- | include/llvm/Support/MachO.def | 45 | ||||
-rw-r--r-- | include/llvm/Support/MachO.h | 7 | ||||
-rw-r--r-- | lib/ObjectYAML/MachOYAML.cpp | 360 | ||||
-rw-r--r-- | test/ObjectYAML/MachO/load_commands.yaml | 89 | ||||
-rw-r--r-- | tools/obj2yaml/macho2yaml.cpp | 25 | ||||
-rw-r--r-- | tools/yaml2obj/yaml2macho.cpp | 36 |
7 files changed, 590 insertions, 33 deletions
diff --git a/include/llvm/ObjectYAML/MachOYAML.h b/include/llvm/ObjectYAML/MachOYAML.h index 726b4241a91..6270d2309c0 100644 --- a/include/llvm/ObjectYAML/MachOYAML.h +++ b/include/llvm/ObjectYAML/MachOYAML.h @@ -35,19 +35,18 @@ struct FileHeader { struct LoadCommand { virtual ~LoadCommand(); - MachO::LoadCommandType cmd; - uint32_t cmdsize; + llvm::MachO::macho_load_command 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 diff --git a/include/llvm/Support/MachO.def b/include/llvm/Support/MachO.def index 8827a60ca16..9ca6440dd82 100644 --- a/include/llvm/Support/MachO.def +++ b/include/llvm/Support/MachO.def @@ -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 diff --git a/include/llvm/Support/MachO.h b/include/llvm/Support/MachO.h index 7b00cb66652..ea695d6b282 100644 --- a/include/llvm/Support/MachO.h +++ b/include/llvm/Support/MachO.h @@ -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; + + union macho_load_command { + #include "llvm/Support/MachO.def" + }; + } // end namespace MachO } // end namespace llvm diff --git a/lib/ObjectYAML/MachOYAML.cpp b/lib/ObjectYAML/MachOYAML.cpp index af80c1645a6..789eacba381 100644 --- a/lib/ObjectYAML/MachOYAML.cpp +++ b/lib/ObjectYAML/MachOYAML.cpp @@ -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,311 @@ 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.load_command_data.load_command.cmd); + IO.mapRequired("cmdsize", LoadCommand.load_command_data.load_command.cmdsize); + +#define HANDLE_LOAD_COMMAND(LCName, LCValue, LCStruct) \ + case MachO::LCName: \ + MappingTraits<MachO::LCStruct>::mapping( \ + IO, LoadCommand.load_command_data.LCStruct); \ + break; + + switch (LoadCommand.load_command_data.load_command.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 diff --git a/test/ObjectYAML/MachO/load_commands.yaml b/test/ObjectYAML/MachO/load_commands.yaml index 246759da2cc..93793815365 100644 --- a/test/ObjectYAML/MachO/load_commands.yaml +++ b/test/ObjectYAML/MachO/load_commands.yaml @@ -13,39 +13,128 @@ 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 diff --git a/tools/obj2yaml/macho2yaml.cpp b/tools/obj2yaml/macho2yaml.cpp index ad6969cdd4a..eb875ffa1cf 100644 --- a/tools/obj2yaml/macho2yaml.cpp +++ b/tools/obj2yaml/macho2yaml.cpp @@ -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.load_command_data.LCStruct), LoadCmd.Ptr, \ + sizeof(MachO::LCStruct)); \ + if (Obj.isLittleEndian() != sys::IsLittleEndianHost) \ + MachO::swapStruct(LC.load_command_data.LCStruct); \ + 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.load_command_data.load_command), LoadCmd.Ptr, + sizeof(MachO::load_command)); + if (Obj.isLittleEndian() != sys::IsLittleEndianHost) + MachO::swapStruct(LC.load_command_data.load_command); + break; +#include "llvm/Support/MachO.def" + } Y->LoadCommands.push_back(std::move(LC)); } diff --git a/tools/yaml2obj/yaml2macho.cpp b/tools/yaml2obj/yaml2macho.cpp index ed243a2cdd9..bf891a6e983 100644 --- a/tools/yaml2obj/yaml2macho.cpp +++ b/tools/yaml2obj/yaml2macho.cpp @@ -79,17 +79,33 @@ 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.load_command_data.LCStruct)), \ + sizeof(MachO::LCStruct)); \ + BytesWritten = sizeof(MachO::LCStruct); \ + break; + + switch (LC.load_command_data.load_command.cmd) { + default: + OS.write( + reinterpret_cast<const char *>(&(LC.load_command_data.load_command)), + sizeof(MachO::load_command)); + BytesWritten = sizeof(MachO::load_command); + break; +#include "llvm/Support/MachO.def" + } + + auto BytesRemaining = + LC.load_command_data.load_command.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(); |