forked from google/binexport
-
Notifications
You must be signed in to change notification settings - Fork 0
/
library_manager.cc
103 lines (91 loc) · 3.37 KB
/
library_manager.cc
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
// Copyright 2011-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 "third_party/zynamics/binexport/library_manager.h"
#include "base/logging.h"
int LibraryManager::AddKnownLibrary(const string& library_name,
Linkage linkage) {
LibraryRecord library(library_name, linkage, library_list_.size());
for (const auto& element : library_list_) {
if (element.Equals(library)) {
return element.library_index;
}
}
library_list_.emplace_back(library);
return library.library_index;
}
void LibraryManager::AddKnownFunction(const string& module_name,
const string& function_name,
int library_index, Address address) {
// To keep behavior compatible with what Detego did earlier, we have to use
// operator [] and not emplace() to add new element.
known_functions_[address] =
FunctionInfo(module_name, function_name, library_index);
}
Address LibraryManager::AddImportedFunction(const string& module_name,
const string& function_name,
int library_index) {
Address address = --current_imported_address_;
AddKnownFunction(module_name, function_name, library_index, address);
if (library_index != -1) {
UseFunction(address, library_index);
}
return address;
}
const LibraryManager::FunctionInfo* LibraryManager::GetKnownFunction(
Address address) const {
auto it = known_functions_.find(address);
if (it != known_functions_.end()) {
return &it->second;
}
return nullptr;
}
void LibraryManager::GetUsedLibraries(
std::vector<const LibraryRecord*>* used) const {
for (int library_index : used_libraries_) {
used->push_back(&library_list_[library_index]);
}
}
// Finds library index for a given address, -1 if none.
int LibraryManager::GetLibraryIndex(Address address) const {
auto it = used_functions_.find(address);
if (it != used_functions_.end()) {
return it->second;
} else {
return -1;
}
}
void LibraryManager::UseFunction(Address address, int library_index) {
used_functions_.emplace(address, library_index);
used_libraries_.insert(library_index);
}
int LibraryManager::UseLibrary(const string& library, Linkage linkage) {
int library_index = AddKnownLibrary(library, linkage);
used_libraries_.insert(library_index);
return library_index;
}
void LibraryManager::InitializeImportsBase(int bitness) {
CHECK(bitness == 32 || bitness == 64);
bitness_ = bitness;
current_imported_address_ = bitness_ == 64 ?
kFirstImportedAddress64 : kFirstImportedAddress32;
}
bool LibraryManager::GetReference(Address source, Address* target) const {
auto it = references_.find(source);
if (it == references_.end()) {
return false;
} else {
*target = it->second;
return true;
}
}