From 0865cea8c3a792334cf48d286df3a6fc0731065d Mon Sep 17 00:00:00 2001 From: Chris Bieneman Date: Tue, 17 May 2016 19:44:06 +0000 Subject: 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. git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@269808 91177308-0d34-0410-b5e6-96231b3b80d8 --- lib/ObjectYAML/MachOYAML.cpp | 359 ++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 353 insertions(+), 6 deletions(-) (limited to 'lib/ObjectYAML/MachOYAML.cpp') diff --git a/lib/ObjectYAML/MachOYAML.cpp b/lib/ObjectYAML/MachOYAML.cpp index af80c1645a6..b98d4622a36 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 // 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 -- cgit v1.2.3