[llvm] r270001 - [obj2yaml] Refactoring of dumping MachO section structs
Chris Bieneman via llvm-commits
llvm-commits at lists.llvm.org
Wed May 18 16:22:55 PDT 2016
Author: cbieneman
Date: Wed May 18 18:22:53 2016
New Revision: 270001
URL: http://llvm.org/viewvc/llvm-project?rev=270001&view=rev
Log:
[obj2yaml] Refactoring of dumping MachO section structs
This refactoring is to reduce code duplication between the 32-bit and 64-bit code paths. This refactoring will also make the special casing for other data after load commands cleaner.
Modified:
llvm/trunk/tools/obj2yaml/macho2yaml.cpp
Modified: llvm/trunk/tools/obj2yaml/macho2yaml.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/tools/obj2yaml/macho2yaml.cpp?rev=270001&r1=270000&r2=270001&view=diff
==============================================================================
--- llvm/trunk/tools/obj2yaml/macho2yaml.cpp (original)
+++ llvm/trunk/tools/obj2yaml/macho2yaml.cpp Wed May 18 18:22:53 2016
@@ -35,7 +35,7 @@ public:
break;
template <typename SectionType>
-MachOYAML::Section constructSection(SectionType Sec) {
+MachOYAML::Section constructSectionCommon(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);
@@ -52,6 +52,40 @@ MachOYAML::Section constructSection(Sect
return TempSec;
}
+template <typename SectionType>
+MachOYAML::Section constructSection(SectionType Sec);
+
+template <> MachOYAML::Section constructSection(MachO::section Sec) {
+ MachOYAML::Section TempSec = constructSectionCommon(Sec);
+ TempSec.reserved3 = 0;
+ return TempSec;
+}
+
+template <> MachOYAML::Section constructSection(MachO::section_64 Sec) {
+ MachOYAML::Section TempSec = constructSectionCommon(Sec);
+ TempSec.reserved3 = Sec.reserved3;
+ return TempSec;
+}
+
+template <typename SectionType, typename SegmentType>
+void extractSections(
+ const llvm::object::MachOObjectFile::LoadCommandInfo &LoadCmd,
+ std::vector<MachOYAML::Section> &Sections, bool IsLittleEndian) {
+ auto End = LoadCmd.Ptr + LoadCmd.C.cmdsize;
+ const SectionType *Curr =
+ reinterpret_cast<const SectionType *>(LoadCmd.Ptr + sizeof(SegmentType));
+ for (; reinterpret_cast<const void *>(Curr) < End; Curr++) {
+ if (IsLittleEndian != sys::IsLittleEndianHost) {
+ SectionType Sec;
+ memcpy((void *)&Sec, Curr, sizeof(SectionType));
+ MachO::swapStruct(Sec);
+ Sections.push_back(constructSection(Sec));
+ } else {
+ Sections.push_back(constructSection(*Curr));
+ }
+ }
+}
+
Expected<std::unique_ptr<MachOYAML::Object>> MachODumper::dump() {
auto Y = make_unique<MachOYAML::Object>();
Y->Header.magic = Obj.getHeader().magic;
@@ -74,39 +108,15 @@ Expected<std::unique_ptr<MachOYAML::Obje
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);
- }
+ switch (LoadCmd.C.cmd) {
+ case MachO::LC_SEGMENT:
+ extractSections<MachO::section, MachO::segment_command>(
+ LoadCmd, LC.Sections, Obj.isLittleEndian());
+ break;
+ case MachO::LC_SEGMENT_64:
+ extractSections<MachO::section_64, MachO::segment_command_64>(
+ LoadCmd, LC.Sections, Obj.isLittleEndian());
+ break;
}
Y->LoadCommands.push_back(std::move(LC));
}
More information about the llvm-commits
mailing list