forked from avast/retdec
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmacho_extractor.cpp
316 lines (288 loc) · 6.62 KB
/
macho_extractor.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
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
/**
* @file src/macho-extractortool/macho_extractor.cpp
* @brief This program breaks Mach-O Universal static libraries.
* @copyright (c) 2017 Avast Software, licensed under the MIT license
*/
#include <set>
#include <llvm/Support/FileSystem.h>
#include <rapidjson/document.h>
#include <rapidjson/prettywriter.h>
#include "retdec/utils/conversion.h"
#include "retdec/utils/string.h"
#include "retdec/utils/io/log.h"
#include "retdec/macho-extractor/break_fat.h"
using namespace retdec::utils::io;
using namespace retdec::macho_extractor;
using namespace rapidjson;
namespace {
enum class Mode { All, Best, Arch, Family, Index };
void printUsage()
{
Log::error() <<
"\nExtract objects from Mach-O Universal Binaries.\n"
"Usage: retdec-macho-extractor [OPTIONS] FILE\n\n"
"Extraction options:\n\n"
" --all\n"
" Extract all objects from binary (default action).\n\n"
" -b --best\n"
" Extract the best binary for decompilation in the RetDec.\n\n"
" -a --arch NAME\n"
" Extract the binary with selected LLVM architecture name.\n\n"
" -f --family NAME\n"
" Extract the binary with selected architecture family.\n\n"
" -i --index INDEX\n"
" Extract the binary with selected zero-based index.\n\n"
" To see list of supported values for family or architecture\n"
" options run application in --list or --json mode.\n\n"
"List options:\n\n"
" -l --list\n"
" List target architectures and quit.\n\n"
" -j --json\n"
" List target architectures in JSON format and quit.\n\n"
" --objects\n"
" Add list of objects to the list of architectures.This option\n"
" implies --list option. This option works only with archives.\n\n"
" --check-archive\n"
" Writes info about the binary being static library or not. Returns\n"
" zero if it is an archive, non-zero value otherwise. This option\n"
" ignores --json option.\n\n"
"Output options:\n\n"
" -o --out PATH\n"
" Output will be written to the PATH.\n\n";
}
/**
* Fetch parameter value or die with error message.
* @param argv vector with arguments
* @param i index of argument
* @return argument value
*/
std::string getParamOrDie(
std::vector<std::string> &argv,
std::size_t &i)
{
if (argv.size() > i + 1)
{
return argv[++i];
}
else
{
Log::error() << Log::Error << "missing argument value.\n\n";
printUsage();
exit(1);
}
}
/**
* Print error message
* @param message error message
* @param isJson if @c true use JSON format
*/
void printError(
const std::string &message,
bool isJson = false)
{
if(isJson)
{
Document outDoc(kObjectType);
outDoc.AddMember(
"error",
Value(message.c_str(), outDoc.GetAllocator()).Move(),
outDoc.GetAllocator());
StringBuffer outBuffer;
PrettyWriter<StringBuffer> outWriter(outBuffer);
outDoc.Accept(outWriter);
Log::info() << outBuffer.GetString();
}
else
{
Log::error() << Log::Error << message << ".\n";
}
}
/**
* Parse arguments
* @param args vector with arguments
* @return program return value
*/
int handleArguments(
std::vector<std::string> &args)
{
if(args.size() < 1)
{
printUsage();
Log::error() << Log::Error << "not enough arguments!\n";
return 1;
}
std::string arch;
std::string family;
unsigned index = 0;
std::string inFile;
std::string outFile;
Mode mode = Mode::All;
bool listOnly = false;
bool jsonOut = false;
bool addObjects = false;
bool isArchiveVerif = false;
std::set<std::string> withArgs =
{
"i", "index",
"a", "arch",
"f", "family",
"o", "out"
};
std::vector<std::string> argv;
for (const auto& a : args)
{
bool added = false;
for (auto& o : withArgs)
{
std::string start = (o.size() == 1 ? "-" : "--") + o + "=";
if (retdec::utils::startsWith(a, start))
{
argv.push_back(a.substr(0, start.size()-1));
argv.push_back(a.substr(start.size()));
added = true;
break;
}
}
if (added)
{
continue;
}
argv.push_back(a);
}
for(std::size_t i = 0; i < argv.size(); ++i)
{
std::string& c = argv[i];
if(c == "-h" || c == "--help")
{
printUsage();
return 0;
}
else if(c == "--check-archive")
{
isArchiveVerif = true;
}
else if(c == "-o" || c == "--out")
{
outFile = getParamOrDie(argv, i);
}
else if(c == "-l" || c == "--list")
{
listOnly = true;
}
else if(c == "-j" || c == "--json")
{
listOnly = jsonOut = true;
}
else if(c == "--objects")
{
listOnly = addObjects = true;
}
else if(c == "--all")
{
mode = Mode::All;
}
else if(c == "-b" || c == "--best")
{
mode = Mode::Best;
}
else if(c == "-i" || c == "--index")
{
mode = Mode::Index;
const auto arg = getParamOrDie(argv, i);
if(!retdec::utils::strToNum(arg, index))
{
printError("invalid '--index' option value!", jsonOut);
return 1;
}
}
else if(c == "-a" || c == "--arch")
{
mode = Mode::Arch;
arch = getParamOrDie(argv, i);
}
else if(c == "-f" || c == "--family")
{
mode = Mode::Family;
family = getParamOrDie(argv, i);
}
else
{
if(llvm::sys::fs::is_regular_file(llvm::Twine(c)))
{
inFile = c;
}
else
{
printError("invalid argument '" + args[i] + "'", jsonOut);
return 1;
}
}
}
// Load input file
if(inFile.empty())
{
printError("no input file", jsonOut);
return 2;
}
BreakMachOUniversal binary(inFile);
if(!binary.isValid())
{
printError("file is not valid Mach-O Universal binary", jsonOut);
return 2;
}
// Check only for static archives
if(isArchiveVerif)
{
if(binary.isStaticLibrary())
{
Log::info() << "Input file is a static library.\n";
return 0;
}
Log::info() << "Input file is NOT a static library.\n";
return 3;
}
// List mode
if(listOnly)
{
std::stringstream ss;
bool ret;
if(jsonOut)
{
ret = binary.listArchitecturesJson(ss, addObjects) ? 0 : 1;
}
else
{
ret = binary.listArchitectures(ss, addObjects) ? 0 : 1;
}
Log::info() << ss.str();
return ret;
}
// Set default name if no name was given
if(outFile.empty())
{
outFile = inFile + ".extracted";
outFile += binary.isStaticLibrary() ? ".a" : "";
}
// Extract
switch(mode)
{
case Mode::All:
return binary.extractAllArchives() ? 0 : 1;
case Mode::Best:
return binary.extractBestArchive(outFile) ? 0 : 1;
case Mode::Family:
return binary.extractArchiveForFamily(family, outFile) ? 0 : 1;
case Mode::Arch:
return binary.extractArchiveForArchitecture(arch, outFile) ? 0 : 1;
case Mode::Index:
return binary.extractArchiveWithIndex(index, outFile) ? 0 : 1;
default:
return 1;
}
}
} // anonymous namespace
int main(int argc, char **argv)
{
std::vector<std::string> arguments(argv + 1, argv + argc);
return handleArguments(arguments);
}