diff options
author | Chris Bieneman <beanz@apple.com> | 2016-05-17 21:31:02 +0000 |
---|---|---|
committer | Chris Bieneman <beanz@apple.com> | 2016-05-17 21:31:02 +0000 |
commit | 7b504b75312ec0a02f20aba47d59e254f76d2e56 (patch) | |
tree | d993365a431d4cd4682ef0fb2c001d10b651965a /llvm/tools/obj2yaml/macho2yaml.cpp | |
parent | e3ec688df598f3ff052776b72a9f4601838a2d1a (diff) | |
download | bcm5719-llvm-7b504b75312ec0a02f20aba47d59e254f76d2e56.tar.gz bcm5719-llvm-7b504b75312ec0a02f20aba47d59e254f76d2e56.zip |
[obj2yaml] [yaml2obj] Support MachO section and section_64 structs
This patch adds round trip support for MachO section structs.
llvm-svn: 269845
Diffstat (limited to 'llvm/tools/obj2yaml/macho2yaml.cpp')
-rw-r--r-- | llvm/tools/obj2yaml/macho2yaml.cpp | 54 |
1 files changed, 54 insertions, 0 deletions
diff --git a/llvm/tools/obj2yaml/macho2yaml.cpp b/llvm/tools/obj2yaml/macho2yaml.cpp index cbf55ba35a5..66244c30e40 100644 --- a/llvm/tools/obj2yaml/macho2yaml.cpp +++ b/llvm/tools/obj2yaml/macho2yaml.cpp @@ -13,6 +13,8 @@ #include "llvm/ObjectYAML/MachOYAML.h" #include "llvm/Support/ErrorHandling.h" +#include <string.h> // for memcpy + using namespace llvm; class MachODumper { @@ -32,6 +34,24 @@ public: MachO::swapStruct(LC.Data.LCStruct##_data); \ break; +template <typename SectionType> +MachOYAML::Section constructSection(SectionType Sec) { + MachOYAML::Section TempSec; + memcpy(reinterpret_cast<void *>(&TempSec.sectname[0]), &Sec.sectname[0], 16); + memcpy(reinterpret_cast<void *>(&TempSec.segname[0]), &Sec.segname[0], 16); + TempSec.addr = Sec.addr; + TempSec.size = Sec.size; + TempSec.offset = Sec.offset; + TempSec.align = Sec.align; + TempSec.reloff = Sec.reloff; + TempSec.nreloc = Sec.nreloc; + TempSec.flags = Sec.flags; + TempSec.reserved1 = Sec.reserved1; + TempSec.reserved2 = Sec.reserved2; + TempSec.reserved3 = 0; + return TempSec; +} + Expected<std::unique_ptr<MachOYAML::Object>> MachODumper::dump() { auto Y = make_unique<MachOYAML::Object>(); Y->Header.magic = Obj.getHeader().magic; @@ -54,6 +74,40 @@ Expected<std::unique_ptr<MachOYAML::Object>> MachODumper::dump() { break; #include "llvm/Support/MachO.def" } + if (LoadCmd.C.cmd == MachO::LC_SEGMENT) { + auto End = LoadCmd.Ptr + LoadCmd.C.cmdsize; + const MachO::section *Curr = reinterpret_cast<const MachO::section *>( + LoadCmd.Ptr + sizeof(MachO::segment_command)); + for (; reinterpret_cast<const void *>(Curr) < End; Curr++) { + if (Obj.isLittleEndian() != sys::IsLittleEndianHost) { + MachO::section Sec; + memcpy((void *)&Sec, Curr, sizeof(MachO::section)); + MachO::swapStruct(Sec); + LC.Sections.push_back(constructSection(Sec)); + } else { + LC.Sections.push_back(constructSection(*Curr)); + } + } + } else if (LoadCmd.C.cmd == MachO::LC_SEGMENT_64) { + auto End = LoadCmd.Ptr + LoadCmd.C.cmdsize; + const MachO::section_64 *Curr = + reinterpret_cast<const MachO::section_64 *>( + LoadCmd.Ptr + sizeof(MachO::segment_command_64)); + for (; reinterpret_cast<const void *>(Curr) < End; Curr++) { + MachOYAML::Section TempSec; + if (Obj.isLittleEndian() != sys::IsLittleEndianHost) { + MachO::section_64 Sec; + memcpy((void *)&Sec, Curr, sizeof(MachO::section_64)); + MachO::swapStruct(Sec); + LC.Sections.push_back(constructSection(Sec)); + TempSec = constructSection(Sec); + } else { + TempSec = constructSection(*Curr); + } + TempSec.reserved3 = Curr->reserved3; + LC.Sections.push_back(TempSec); + } + } Y->LoadCommands.push_back(std::move(LC)); } |