1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
|
//===------ macho2yaml.cpp - obj2yaml conversion tool -----------*- C++ -*-===//
//
// The LLVM Compiler Infrastructure
//
// This file is distributed under the University of Illinois Open Source
// License. See LICENSE.TXT for details.
//
//===----------------------------------------------------------------------===//
#include "Error.h"
#include "obj2yaml.h"
#include "llvm/Object/MachOUniversal.h"
#include "llvm/ObjectYAML/MachOYAML.h"
#include "llvm/Support/ErrorHandling.h"
#include <string.h> // for memcpy
using namespace llvm;
class MachODumper {
const object::MachOObjectFile &Obj;
public:
MachODumper(const object::MachOObjectFile &O) : Obj(O) {}
Expected<std::unique_ptr<MachOYAML::Object>> 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;
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;
Y->Header.cputype = Obj.getHeader().cputype;
Y->Header.cpusubtype = Obj.getHeader().cpusubtype;
Y->Header.filetype = Obj.getHeader().filetype;
Y->Header.ncmds = Obj.getHeader().ncmds;
Y->Header.sizeofcmds = Obj.getHeader().sizeofcmds;
Y->Header.flags = Obj.getHeader().flags;
Y->Header.reserved = 0;
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"
}
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));
}
return std::move(Y);
}
Error macho2yaml(raw_ostream &Out, const object::MachOObjectFile &Obj) {
MachODumper Dumper(Obj);
Expected<std::unique_ptr<MachOYAML::Object>> YAML = Dumper.dump();
if (!YAML)
return YAML.takeError();
yaml::Output Yout(Out);
Yout << *(YAML.get());
return Error::success();
}
Error macho2yaml(raw_ostream &Out, const object::MachOUniversalBinary &Obj) {
return make_error<Obj2YamlError>(obj2yaml_error::not_implemented);
}
std::error_code macho2yaml(raw_ostream &Out, const object::ObjectFile &Obj) {
if (const auto *MachOObj = dyn_cast<object::MachOUniversalBinary>(&Obj)) {
if (auto Err = macho2yaml(Out, *MachOObj)) {
return errorToErrorCode(std::move(Err));
}
return obj2yaml_error::success;
}
if (const auto *MachOObj = dyn_cast<object::MachOObjectFile>(&Obj)) {
if (auto Err = macho2yaml(Out, *MachOObj)) {
return errorToErrorCode(std::move(Err));
}
return obj2yaml_error::success;
}
return obj2yaml_error::unsupported_obj_file_format;
}
|