diff options
author | Chris Bieneman <beanz@apple.com> | 2016-05-18 16:17:23 +0000 |
---|---|---|
committer | Chris Bieneman <beanz@apple.com> | 2016-05-18 16:17:23 +0000 |
commit | 2de17d49dd0fef25849c7c4b0843dbef8a0062e3 (patch) | |
tree | 42401a5108c103c569169d0c66cf241c0cdb01b2 /llvm/tools | |
parent | 5a6d2985d747223350f93274187be97e3073c1e4 (diff) | |
download | bcm5719-llvm-2de17d49dd0fef25849c7c4b0843dbef8a0062e3.tar.gz bcm5719-llvm-2de17d49dd0fef25849c7c4b0843dbef8a0062e3.zip |
Re-apply: [obj2yaml] [yaml2obj] Support MachO section and section_64
This re-applies r269845, r269846, and r269850 with an included fix for a crash reported by zturner.
llvm-svn: 269953
Diffstat (limited to 'llvm/tools')
-rw-r--r-- | llvm/tools/obj2yaml/macho2yaml.cpp | 54 | ||||
-rw-r--r-- | llvm/tools/yaml2obj/yaml2macho.cpp | 32 |
2 files changed, 86 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)); } diff --git a/llvm/tools/yaml2obj/yaml2macho.cpp b/llvm/tools/yaml2obj/yaml2macho.cpp index 772c710c098..0e8799e2ce1 100644 --- a/llvm/tools/yaml2obj/yaml2macho.cpp +++ b/llvm/tools/yaml2obj/yaml2macho.cpp @@ -77,6 +77,23 @@ Error MachOWriter::writeHeader(raw_ostream &OS) { return Error::success(); } +template <typename SectionType> +SectionType constructSection(MachOYAML::Section Sec) { + SectionType 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; + return TempSec; +} + Error MachOWriter::writeLoadCommands(raw_ostream &OS) { for (auto &LC : Obj.LoadCommands) { size_t BytesWritten = 0; @@ -96,6 +113,21 @@ Error MachOWriter::writeLoadCommands(raw_ostream &OS) { #include "llvm/Support/MachO.def" } + if(LC.Data.load_command_data.cmd == MachO::LC_SEGMENT) { + for(auto Sec : LC.Sections) { + auto TempSec = constructSection<MachO::section>(Sec); + OS.write(reinterpret_cast<const char *>(&(TempSec)), sizeof(MachO::section)); + BytesWritten += sizeof(MachO::section); + } + } else if(LC.Data.load_command_data.cmd == MachO::LC_SEGMENT_64) { + for(auto Sec : LC.Sections) { + auto TempSec = constructSection<MachO::section_64>(Sec); + TempSec.reserved3 = Sec.reserved3; + OS.write(reinterpret_cast<const char *>(&(TempSec)), sizeof(MachO::section_64)); + BytesWritten += sizeof(MachO::section_64); + } + } + auto BytesRemaining = LC.Data.load_command_data.cmdsize - BytesWritten; if (BytesRemaining > 0) { |