forked from googleprojectzero/functionsimsearch
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdisassembly.cpp
81 lines (71 loc) · 2.57 KB
/
disassembly.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
// Copyright 2017 Google Inc. All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
#include <iostream>
#include <map>
#include "CodeObject.h"
#include "InstructionDecoder.h"
#include "pecodesource.hpp"
#include "disassembly.hpp"
using namespace std;
using namespace Dyninst;
using namespace ParseAPI;
using namespace InstructionAPI;
Disassembly::Disassembly(const std::string& filetype,
const std::string& inputfile) : type_(filetype), inputfile_(inputfile) {
code_object_ = nullptr;
code_source_ = nullptr;
}
Disassembly::~Disassembly() {
delete code_object_;
if (type_ == "ELF") {
SymtabCodeSource* symtab_code_source =
static_cast<SymtabCodeSource*>(code_source_);
delete symtab_code_source;
} else {
PECodeSource* pe_code_source = static_cast<PECodeSource*>(code_source_);
delete pe_code_source;
}
}
bool Disassembly::Load() {
Instruction::Ptr instruction;
if (type_ == "ELF") {
SymtabAPI::Symtab* sym_tab = nullptr;
SymtabCodeSource* symtab_code_source = nullptr;
bool is_parseable = SymtabAPI::Symtab::openFile(sym_tab, inputfile_);
if (is_parseable == false) {
printf("Error: ELF File cannot be parsed.\n");
return false;
}
// Brutal C-style cast because SymtabCodeSource for some reason wants a
// char * instead of a const char*.
symtab_code_source = new SymtabCodeSource((char *)inputfile_.c_str());
code_source_ = static_cast<CodeSource*>(symtab_code_source);
} else if (type_ == "PE") {
PECodeSource* pe_code_source = new PECodeSource(inputfile_);
if (pe_code_source->parsed() == false) {
printf("Error: PE File cannot be parsed.\n");
return false;
}
code_source_ = static_cast<CodeSource*>(pe_code_source);
}
code_object_ = new CodeObject(code_source_);
// Parse the obvious function entries.
code_object_->parse();
// Parse the gaps.
for (CodeRegion* region : code_source_->regions()) {
code_object_->parseGaps(region, GapParsingType::IdiomMatching);
code_object_->parseGaps(region, GapParsingType::PreambleMatching);
}
return true;
}