diff --git a/llvm/include/llvm/ObjectYAML/MachOYAML.h b/llvm/include/llvm/ObjectYAML/MachOYAML.h index 726b4241a917..21558f00cc0d 100644 --- a/llvm/include/llvm/ObjectYAML/MachOYAML.h +++ b/llvm/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 Data; }; struct Object { FileHeader Header; - std::vector> LoadCommands; + std::vector LoadCommands; }; } // namespace llvm::MachOYAML } // namespace llvm -LLVM_YAML_IS_SEQUENCE_VECTOR(std::unique_ptr) +LLVM_YAML_IS_SEQUENCE_VECTOR(llvm::MachOYAML::LoadCommand) namespace llvm { namespace yaml { @@ -60,12 +59,11 @@ template <> struct MappingTraits { static void mapping(IO &IO, MachOYAML::Object &Object); }; -template <> struct MappingTraits> { - static void mapping(IO &IO, - std::unique_ptr &LoadCommand); +template <> struct MappingTraits { + 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 { @@ -74,7 +72,52 @@ template <> struct ScalarEnumerationTraits { } }; -#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 { + 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); +}; } // namespace llvm::yaml diff --git a/llvm/include/llvm/Support/MachO.def b/llvm/include/llvm/Support/MachO.def index 8827a60ca16f..9ca6440dd82b 100644 --- a/llvm/include/llvm/Support/MachO.def +++ b/llvm/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/llvm/include/llvm/Support/MachO.h b/llvm/include/llvm/Support/MachO.h index 7b00cb66652a..3dd161b4a7ca 100644 --- a/llvm/include/llvm/Support/MachO.h +++ b/llvm/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##_data; + + union macho_load_command { + #include "llvm/Support/MachO.def" + }; + } // end namespace MachO } // end namespace llvm diff --git a/llvm/lib/ObjectYAML/MachOYAML.cpp b/llvm/lib/ObjectYAML/MachOYAML.cpp index af80c1645a6d..b98d4622a36b 100644 --- a/llvm/lib/ObjectYAML/MachOYAML.cpp +++ b/llvm/lib/ObjectYAML/MachOYAML.cpp @@ -13,6 +13,9 @@ #include "llvm/ObjectYAML/MachOYAML.h" #include "llvm/Support/Casting.h" +#include "llvm/Support/Format.h" + +#include // For memcpy and memset. namespace llvm { @@ -20,6 +23,52 @@ 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); @@ -46,12 +95,310 @@ void MappingTraits::mapping(IO &IO, IO.setContext(nullptr); } -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); +void MappingTraits::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::mapping(IO, \ + LoadCommand.Data.LCStruct##_data); \ + break; + + switch (LoadCommand.Data.load_command_data.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); } } // namespace llvm::yaml diff --git a/llvm/test/ObjectYAML/MachO/load_commands.yaml b/llvm/test/ObjectYAML/MachO/load_commands.yaml index 246759da2cc8..b8b3407aa7a6 100644 --- a/llvm/test/ObjectYAML/MachO/load_commands.yaml +++ b/llvm/test/ObjectYAML/MachO/load_commands.yaml @@ -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 diff --git a/llvm/tools/obj2yaml/macho2yaml.cpp b/llvm/tools/obj2yaml/macho2yaml.cpp index ad6969cdd4a0..cbf55ba35a54 100644 --- a/llvm/tools/obj2yaml/macho2yaml.cpp +++ b/llvm/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> 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> MachODumper::dump() { auto Y = make_unique(); Y->Header.magic = Obj.getHeader().magic; @@ -35,10 +43,17 @@ Expected> MachODumper::dump() { Y->Header.flags = Obj.getHeader().flags; Y->Header.reserved = 0; - 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; + 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)); } diff --git a/llvm/tools/yaml2obj/yaml2macho.cpp b/llvm/tools/yaml2obj/yaml2macho.cpp index ed243a2cdd9d..772c710c098b 100644 --- a/llvm/tools/yaml2obj/yaml2macho.cpp +++ b/llvm/tools/yaml2obj/yaml2macho.cpp @@ -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(&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(&(LC.Data.LCStruct##_data)), \ + sizeof(MachO::LCStruct)); \ + BytesWritten = sizeof(MachO::LCStruct); \ + break; + + switch (LC.Data.load_command_data.cmd) { + default: + OS.write(reinterpret_cast(&(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 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 FillData; + FillData.insert(FillData.begin(), BytesRemaining / 4 + 1, 0xDEADBEEFu); + OS.write(reinterpret_cast(FillData.data()), BytesRemaining); } } return Error::success();