diff --git a/include/llvm/ObjectYAML/MachOYAML.h b/include/llvm/ObjectYAML/MachOYAML.h index 6270d2309c0..726b4241a91 100644 --- a/include/llvm/ObjectYAML/MachOYAML.h +++ b/include/llvm/ObjectYAML/MachOYAML.h @@ -35,18 +35,19 @@ struct FileHeader { struct LoadCommand { virtual ~LoadCommand(); - llvm::MachO::macho_load_command load_command_data; + MachO::LoadCommandType cmd; + uint32_t cmdsize; }; struct Object { FileHeader Header; - std::vector LoadCommands; + std::vector> LoadCommands; }; } // namespace llvm::MachOYAML } // namespace llvm -LLVM_YAML_IS_SEQUENCE_VECTOR(llvm::MachOYAML::LoadCommand) +LLVM_YAML_IS_SEQUENCE_VECTOR(std::unique_ptr) namespace llvm { namespace yaml { @@ -59,11 +60,12 @@ template <> struct MappingTraits { static void mapping(IO &IO, MachOYAML::Object &Object); }; -template <> struct MappingTraits { - static void mapping(IO &IO, MachOYAML::LoadCommand &LoadCommand); +template <> struct MappingTraits> { + static void mapping(IO &IO, + std::unique_ptr &LoadCommand); }; -#define HANDLE_LOAD_COMMAND(LCName, LCValue, LCStruct) \ +#define HANDLE_LOAD_COMMAND(LCName, LCValue, LCStruct) \ io.enumCase(value, #LCName, MachO::LCName); template <> struct ScalarEnumerationTraits { @@ -72,52 +74,7 @@ template <> struct ScalarEnumerationTraits { } }; -// This trait is used for 16-byte chars in Mach structures used for strings -typedef char char_16[16]; - -template <> struct ScalarTraits { - 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 { - 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 { \ - static void mapping(IO &IO, MachO::LCStruct &LoadCommand); \ - }; - -#include "llvm/Support/MachO.def" - -// Extra structures used by load commands -template <> struct MappingTraits { - static void mapping(IO &IO, MachO::dylib &LoadCommand); -}; - -template <> struct MappingTraits { - static void mapping(IO &IO, MachO::fvmlib &LoadCommand); -}; - -template <> struct MappingTraits { - static void mapping(IO &IO, MachO::section &LoadCommand); -}; - -template <> struct MappingTraits { - static void mapping(IO &IO, MachO::section_64 &LoadCommand); -}; +#undef HANDLE_LOAD_COMMAND } // namespace llvm::yaml diff --git a/include/llvm/Support/MachO.def b/include/llvm/Support/MachO.def index 9ca6440dd82..8827a60ca16 100644 --- a/include/llvm/Support/MachO.def +++ b/include/llvm/Support/MachO.def @@ -11,7 +11,9 @@ // //,,,----------------------------------------------------------------------,,,// -#ifdef HANDLE_LOAD_COMMAND +#ifndef HANDLE_LOAD_COMMAND +#define HANDLE_LOAD_COMMAND(LCName, LCValue, LCStruct) +#endif HANDLE_LOAD_COMMAND(LC_SEGMENT, 0x00000001u, segment_command) HANDLE_LOAD_COMMAND(LC_SYMTAB, 0x00000002u, symtab_command) @@ -63,44 +65,3 @@ 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 ea695d6b282..7b00cb66652 100644 --- a/include/llvm/Support/MachO.h +++ b/include/llvm/Support/MachO.h @@ -1677,13 +1677,6 @@ 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 789eacba381..af80c1645a6 100644 --- a/lib/ObjectYAML/MachOYAML.cpp +++ b/lib/ObjectYAML/MachOYAML.cpp @@ -13,9 +13,6 @@ #include "llvm/ObjectYAML/MachOYAML.h" #include "llvm/Support/Casting.h" -#include "llvm/Support/Format.h" - -#include // For memcpy and memset. namespace llvm { @@ -23,52 +20,6 @@ MachOYAML::LoadCommand::~LoadCommand() {} namespace yaml { -void ScalarTraits::output(const char_16 &Val, void *, - llvm::raw_ostream &Out) { - Out << Val; -} - -StringRef ScalarTraits::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::mustQuote(StringRef S) { return needsQuotes(S); } - -void ScalarTraits::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::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(TempInt); - ++Idx; // increment idx an extra time because we're consuming 2 chars - ++OutIdx; - } - return StringRef(); -} - -bool ScalarTraits::mustQuote(StringRef S) { return needsQuotes(S); } - void MappingTraits::mapping( IO &IO, MachOYAML::FileHeader &FileHdr) { IO.mapRequired("magic", FileHdr.magic); @@ -95,311 +46,12 @@ void MappingTraits::mapping(IO &IO, IO.setContext(nullptr); } -void MappingTraits::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::mapping( \ - IO, LoadCommand.load_command_data.LCStruct); \ - break; - - switch (LoadCommand.load_command_data.load_command.cmd) { -#include "llvm/Support/MachO.def" - } -} - -void MappingTraits::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::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::mapping( - IO &IO, MachO::dylib_command &LoadCommand) { - IO.mapRequired("dylib", LoadCommand.dylib); -} - -void MappingTraits::mapping( - IO &IO, MachO::dylinker_command &LoadCommand) { - - IO.mapRequired("name", LoadCommand.name); -} - -void MappingTraits::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::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::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::mapping( - IO &IO, MachO::entry_point_command &LoadCommand) { - - IO.mapRequired("entryoff", LoadCommand.entryoff); - IO.mapRequired("stacksize", LoadCommand.stacksize); -} - -void MappingTraits::mapping( - IO &IO, MachO::fvmfile_command &LoadCommand) { - - IO.mapRequired("name", LoadCommand.name); - IO.mapRequired("header_addr", LoadCommand.header_addr); -} - -void MappingTraits::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::mapping( - IO &IO, MachO::fvmlib_command &LoadCommand) { - - IO.mapRequired("fvmlib", LoadCommand.fvmlib); -} - -void MappingTraits::mapping( - IO &IO, MachO::ident_command &LoadCommand) {} - -void MappingTraits::mapping( - IO &IO, MachO::linkedit_data_command &LoadCommand) { - - IO.mapRequired("dataoff", LoadCommand.dataoff); - IO.mapRequired("datasize", LoadCommand.datasize); -} - -void MappingTraits::mapping( - IO &IO, MachO::linker_option_command &LoadCommand) { - - IO.mapRequired("count", LoadCommand.count); -} - -void MappingTraits::mapping( - IO &IO, MachO::prebind_cksum_command &LoadCommand) { - - IO.mapRequired("cksum", LoadCommand.cksum); -} - -void MappingTraits::mapping( - IO &IO, MachO::load_command &LoadCommand) {} - -void MappingTraits::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::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::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::mapping( - IO &IO, MachO::rpath_command &LoadCommand) { - - IO.mapRequired("path", LoadCommand.path); -} - -void MappingTraits::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::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::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::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::mapping( - IO &IO, MachO::source_version_command &LoadCommand) { - - IO.mapRequired("version", LoadCommand.version); -} - -void MappingTraits::mapping( - IO &IO, MachO::sub_client_command &LoadCommand) { - - IO.mapRequired("client", LoadCommand.client); -} - -void MappingTraits::mapping( - IO &IO, MachO::sub_framework_command &LoadCommand) { - - IO.mapRequired("umbrella", LoadCommand.umbrella); -} - -void MappingTraits::mapping( - IO &IO, MachO::sub_library_command &LoadCommand) { - - IO.mapRequired("sub_library", LoadCommand.sub_library); -} - -void MappingTraits::mapping( - IO &IO, MachO::sub_umbrella_command &LoadCommand) { - - IO.mapRequired("sub_umbrella", LoadCommand.sub_umbrella); -} - -void MappingTraits::mapping( - IO &IO, MachO::symseg_command &LoadCommand) { - - IO.mapRequired("offset", LoadCommand.offset); - IO.mapRequired("size", LoadCommand.size); -} - -void MappingTraits::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::mapping( - IO &IO, MachO::thread_command &LoadCommand) {} - -void MappingTraits::mapping( - IO &IO, MachO::twolevel_hints_command &LoadCommand) { - - IO.mapRequired("offset", LoadCommand.offset); - IO.mapRequired("nhints", LoadCommand.nhints); -} - -void MappingTraits::mapping( - IO &IO, MachO::uuid_command &LoadCommand) { - - IO.mapRequired("cmdsize", LoadCommand.cmdsize); - IO.mapRequired("uuid", LoadCommand.uuid); -} - -void MappingTraits::mapping( - IO &IO, MachO::version_min_command &LoadCommand) { - - IO.mapRequired("version", LoadCommand.version); - IO.mapRequired("sdk", LoadCommand.sdk); +void MappingTraits>::mapping( + IO &IO, std::unique_ptr &LoadCommand) { + if (!IO.outputting()) + LoadCommand.reset(new MachOYAML::LoadCommand()); + IO.mapRequired("cmd", LoadCommand->cmd); + IO.mapRequired("cmdsize", LoadCommand->cmdsize); } } // namespace llvm::yaml diff --git a/test/ObjectYAML/MachO/load_commands.yaml b/test/ObjectYAML/MachO/load_commands.yaml index 93793815365..246759da2cc 100644 --- a/test/ObjectYAML/MachO/load_commands.yaml +++ b/test/ObjectYAML/MachO/load_commands.yaml @@ -13,128 +13,39 @@ 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 eb875ffa1cf..ad6969cdd4a 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,14 +24,6 @@ public: Expected> 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> MachODumper::dump() { auto Y = make_unique(); Y->Header.magic = Obj.getHeader().magic; @@ -43,17 +35,10 @@ Expected> MachODumper::dump() { Y->Header.flags = Obj.getHeader().flags; Y->Header.reserved = 0; - 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" - } + for (auto load_command : Obj.load_commands()) { + auto LC = make_unique(); + LC->cmd = static_cast(load_command.C.cmd); + LC->cmdsize = load_command.C.cmdsize; Y->LoadCommands.push_back(std::move(LC)); } diff --git a/tools/yaml2obj/yaml2macho.cpp b/tools/yaml2obj/yaml2macho.cpp index bf891a6e983..ed243a2cdd9 100644 --- a/tools/yaml2obj/yaml2macho.cpp +++ b/tools/yaml2obj/yaml2macho.cpp @@ -79,33 +79,17 @@ Error MachOWriter::writeHeader(raw_ostream &OS) { Error MachOWriter::writeLoadCommands(raw_ostream &OS) { for (auto &LC : Obj.LoadCommands) { - size_t BytesWritten = 0; -#define HANDLE_LOAD_COMMAND(LCName, LCValue, LCStruct) \ - case MachO::LCName: \ - OS.write(reinterpret_cast(&(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(&(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) { + MachO::load_command LCTemp; + LCTemp.cmd = LC->cmd; + LCTemp.cmdsize = LC->cmdsize; + OS.write(reinterpret_cast(&LCTemp), + sizeof(MachO::load_command)); + auto remaining_size = LC->cmdsize - sizeof(MachO::load_command); + if (remaining_size > 0) { // TODO: Replace all this once the load command data is present in yaml. - // For now I fill with 0xDEADBEEF because it is easy to spot on a hex - // viewer. - std::vector FillData; - FillData.insert(FillData.begin(), BytesRemaining / 4 + 1, 0xDEADBEEFu); - OS.write(reinterpret_cast(FillData.data()), BytesRemaining); + std::vector fill_data; + fill_data.insert(fill_data.begin(), remaining_size, 0); + OS.write(fill_data.data(), remaining_size); } } return Error::success();