-
Notifications
You must be signed in to change notification settings - Fork 2
/
main.cpp
210 lines (166 loc) · 4.02 KB
/
main.cpp
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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
#include <iostream>
#include <fstream>
#include <cstring>
#include "config.h"
#include "core/event_object.h"
#include "util/hex_write.h"
void print_usage(std::ostream& out)
{
out << PROJECT_NAME " " PROJECT_VERSION " usage:" << std::endl;
out << " lyn <object>... [-[no]link] [-[no]longcalls] [-[no]temp] [-[no]hook] [-raw]" << std::endl;
out << " lyn diff <old object> <new object>" << std::endl;
}
int do_diff(int argc, const char* const* argv)
{
if (argc != 2)
{
print_usage(std::cerr);
return 1;
}
try
{
lyn::event_object baseObject;
lyn::event_object otherObject;
baseObject.append_from_elf(argv[0]);
otherObject.append_from_elf(argv[1]);
struct sym_diff_data
{
std::string baseName;
std::string otherName;
};
std::map<unsigned, sym_diff_data> symMap;
for (auto& sym : baseObject.absolute_symbols())
symMap[sym.offset].baseName = sym.name;
for (auto& sym : otherObject.absolute_symbols())
symMap[sym.offset].otherName = sym.name;
for (auto& pair : symMap)
{
auto address = pair.first;
auto& nameDiff = pair.second;
if (nameDiff.baseName == nameDiff.otherName)
continue;
if (nameDiff.baseName.empty())
std::cout << "+" << util::make_hex_string("$", address) << " " << nameDiff.otherName << std::endl;
else if (nameDiff.otherName.empty())
std::cout << "-" << util::make_hex_string("$", address) << " " << nameDiff.baseName << std::endl;
else
std::cout << ">" << util::make_hex_string("$", address) << " " << nameDiff.baseName << " " << nameDiff.otherName << std::endl;
}
}
catch (const std::exception& e)
{
std::cerr << "[lyn diff] ERROR: " << e.what() << std::endl;
return 1;
}
return 0;
}
int main(int argc, char** argv)
{
if (argc < 2)
{
print_usage(std::cerr);
return 1;
}
if (!std::strcmp(argv[1], "diff"))
return do_diff(argc - 2, argv + 2);
struct
{
bool doLink = true;
bool longCall = false;
bool applyHooks = true;
bool printTemporary = false;
} options;
std::vector<std::string> elves;
for (int i = 1; i < argc; ++i)
{
std::string argument(argv[i]);
if (argument.size() == 0)
continue;
if (argument[0] == '-') // option
{
if (argument == "-nolink")
{
options.doLink = false;
continue;
}
if (argument == "-link")
{
options.doLink = true;
continue;
}
if (argument == "-longcalls")
{
options.longCall = true;
continue;
}
if (argument == "-nolongcalls")
{
options.longCall = false;
continue;
}
if (argument == "-raw")
{
options.doLink = false;
options.longCall = false;
options.applyHooks = false;
continue;
}
if (argument == "-temp")
{
options.printTemporary = true;
continue;
}
if (argument == "-notemp")
{
options.printTemporary = false;
continue;
}
if (argument == "-hook")
{
options.applyHooks = true;
continue;
}
if (argument == "-nohook")
{
options.applyHooks = false;
continue;
}
} else { // elf
elves.push_back(std::move(argument));
}
}
try
{
lyn::event_object object;
for (auto& elf : elves)
object.append_from_elf(elf.c_str());
if (options.doLink)
object.try_relocate_relatives();
if (options.longCall)
object.try_transform_relatives();
if (options.doLink)
object.try_relocate_absolutes();
if (!options.printTemporary)
object.remove_unnecessary_symbols();
object.cleanup();
if (options.applyHooks)
{
for (auto& hook : object.get_hooks())
{
lyn::event_object temp;
std::cout << "PUSH" << std::endl;
std::cout << "ORG $" << std::hex << (hook.originalOffset & (~1)) << std::endl;
temp.add_section(lyn::arm_relocator::make_thumb_veneer(hook.name, 0));
temp.write_events(std::cout);
std::cout << "POP" << std::endl;
}
}
object.write_events(std::cout);
}
catch (const std::exception& e)
{
std::cerr << "[lyn] ERROR: " << e.what() << std::endl;
return 1;
}
return 0;
}