This repository has been archived by the owner on Apr 16, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathLift.cpp
242 lines (190 loc) · 8.21 KB
/
Lift.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
/*
Copyright (c) 2013, Trail of Bits
All rights reserved.
Redistribution and use in source and binary forms, with or without modification,
are permitted provided that the following conditions are met:
Redistributions of source code must retain the above copyright notice, this
list of conditions and the following disclaimer.
Redistributions in binary form must reproduce the above copyright notice, this
list of conditions and the following disclaimer in the documentation and/or
other materials provided with the distribution.
Neither the name of the {organization} nor the names of its
contributors may be used to endorse or promote products derived from
this software without specific prior written permission.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR
ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#include <iostream>
#include <string>
#include <sstream>
#include <system_error>
#include <cassert>
#include <iomanip>
#include <memory>
#include <llvm/Bitcode/ReaderWriter.h>
#include <llvm/IR/LLVMContext.h>
#include <llvm/IR/Module.h>
#include <llvm/IR/Verifier.h>
#include <llvm/Support/CommandLine.h>
#include <llvm/Support/FileSystem.h>
#include <llvm/Support/ToolOutputFile.h>
#include "mcsema/Arch/Arch.h"
#include "mcsema/BC/Lift.h"
#include "mcsema/BC/Util.h"
#include <mcsema_generated/Version.h> // auto-generated by CMake from Version.h.in
static llvm::cl::opt<std::string> OutputFilename(
"output", llvm::cl::desc("Output filename"), llvm::cl::init("-"),
llvm::cl::value_desc("filename"));
static llvm::cl::opt<std::string> Arch("arch", llvm::cl::desc("Arch name"),
llvm::cl::value_desc("<arch>"),
llvm::cl::Required);
static llvm::cl::opt<std::string> OS("os", llvm::cl::desc("Operating system"),
llvm::cl::value_desc("<os>"),
llvm::cl::Optional);
static llvm::cl::opt<std::string> InputFilename(
"cfg", llvm::cl::desc("Input CFG file"), llvm::cl::value_desc("<cfg>"),
llvm::cl::Optional);
static llvm::cl::list<std::string> EntryPoints(
"entrypoint", llvm::cl::desc("Describe externally visible entry points"),
llvm::cl::value_desc("<symbol | ep address>"));
static llvm::cl::opt<bool> ListSupported("list-supported",
llvm::cl::desc("List supported instructions for <arch>"),
llvm::cl::Optional);
static llvm::cl::opt<bool> ListUnsupported("list-unsupported",
llvm::cl::desc("List unsupported (not-yet-implemented) instructions for <arch>"),
llvm::cl::Optional);
static llvm::cl::opt<bool> ListCFGFunctions("list-cfg-functions",
llvm::cl::desc("List CFG functions"),
llvm::cl::Optional);
static void PrintVersion(void) {
std::cout << "This is mcsema-lift version: " << MCSEMA_VERSION_STRING << std::endl;
std::cout << "Built from branch: " << MCSEMA_BRANCH_NAME << std::endl;
}
static VA FindSymbolInModule(NativeModulePtr mod, const std::string &sym_name) {
for (auto &sym : mod->getEntryPoints()) {
if (sym.getName() == sym_name) {
return sym.getAddr();
}
}
return static_cast<VA>( -1);
}
void PrintCFGFunctionList(const NativeModulePtr native_module, const std::string &architecture) noexcept {
std::ios::fmtflags original_stream_flags(std::cout.flags());
int address_digit_count = (architecture == "amd64" ? 16 : 8);
std::cout << "\nCFG Function List:\n";
const auto &function_map = native_module->get_funcs();
for (const auto &function_descriptor : function_map) {
VA virtual_address = function_descriptor.first;
const NativeFunctionPtr function = function_descriptor.second;
std::cout << " " << std::hex << std::setw(address_digit_count) << std::setfill('0') << virtual_address << " ";
std::cout << function->get_name() << std::endl;
}
std::cout.flags(original_stream_flags);
}
int main(int argc, char *argv[]) {
llvm::cl::SetVersionPrinter(PrintVersion);
llvm::cl::ParseCommandLineOptions(argc, argv, "CFG to LLVM");
auto context = llvm::make_unique<llvm::LLVMContext>();
if (OS.empty()) {
if (ListSupported || ListUnsupported) {
OS = "linux"; // just need something
}
else {
std::cerr << "-os must be specified" << std::endl;
return EXIT_FAILURE;
}
}
if (!(ListSupported || ListUnsupported || ListCFGFunctions) && EntryPoints.empty()) {
std::cerr
<< "-entrypoint must be specified" << std::endl;
return EXIT_FAILURE;
}
if (!InitArch(context.get(), OS, Arch)) {
std::cerr
<< "Cannot initialize for arch " << Arch
<< " and OS " << OS << std::endl;
return EXIT_FAILURE;
}
auto M = CreateModule(context.get());
if (!M) {
return EXIT_FAILURE;
}
auto triple = M->getTargetTriple();
if (ListSupported || ListUnsupported) {
ListArchSupportedInstructions(triple, llvm::outs(), ListSupported, ListUnsupported);
return EXIT_SUCCESS;
}
if (InputFilename.empty()) {
std::cerr
<< "Must specify an input file." << std::endl;
return EXIT_FAILURE;
}
//reproduce NativeModule from CFG input argument
try {
std::unique_ptr<NativeModule> mod(ReadProtoBuf(InputFilename));
if (!mod) {
std::cerr << "Unable to read module from CFG" << std::endl;
return EXIT_FAILURE;
}
if (ListCFGFunctions) {
PrintCFGFunctionList(mod.get(), Arch);
return EXIT_SUCCESS;
}
//make sure the entry point list is correct before we start lifting the code
const std::vector<NativeEntrySymbol> &module_entry_points = mod->getEntryPoints();
for (const auto &entry_point : EntryPoints) {
auto it = std::find_if(
module_entry_points.begin(),
module_entry_points.end(),
[&entry_point](const NativeEntrySymbol &symbol) -> bool {
return (symbol.getName() == entry_point);
}
);
if (it == module_entry_points.end()) {
std::cerr << "The following entry point could not be found: \"" << entry_point << "\". Aborting..." << std::endl;
return EXIT_FAILURE;
}
}
//now, convert it to an LLVM module
ArchInitAttachDetach(M);
if (!LiftCodeIntoModule(mod.get(), M)) {
std::cerr << "Failure to convert to LLVM module!" << std::endl;
return EXIT_FAILURE;
}
std::set<VA> entry_point_pcs;
for (const auto &entry_point_name : EntryPoints) {
auto entry_pc = FindSymbolInModule(mod.get(), entry_point_name);
assert(entry_pc != static_cast<VA>( -1));
std::cerr << "Adding entry point: " << entry_point_name << std::endl
<< entry_point_name << " is implemented by sub_" << std::hex
<< entry_pc << std::endl;
if ( !ArchAddEntryPointDriver(M, entry_point_name, entry_pc)) {
return EXIT_FAILURE;
}
entry_point_pcs.insert(entry_pc);
}
RenameLiftedFunctions(mod.get(), M, entry_point_pcs);
// will abort if verification fails
if (llvm::verifyModule( *M, &llvm::errs())) {
std::cerr << "Could not verify module!" << std::endl;
return EXIT_FAILURE;
}
std::error_code ec;
llvm::tool_output_file Out(OutputFilename.c_str(), ec,
llvm::sys::fs::F_None);
llvm::WriteBitcodeToFile(M, Out.os());
Out.keep();
} catch (std::exception &e) {
std::cerr << "error: " << std::endl << e.what() << std::endl;
return EXIT_FAILURE;
}
return EXIT_SUCCESS;
}