forked from nodejs/node
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBinary.cpp
More file actions
121 lines (98 loc) · 2.86 KB
/
Copy pathBinary.cpp
File metadata and controls
121 lines (98 loc) · 2.86 KB
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
/* Copyright 2017 - 2025 R. Thomas
* Copyright 2017 - 2025 Quarkslab
*
* 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 "LIEF/Abstract/Binary.hpp"
#include "LIEF/Visitor.hpp"
#include "LIEF/config.h"
#include "logging.hpp"
#include "frozen.hpp"
#include "paging.hpp"
#include "LIEF/Abstract/Section.hpp"
#include "LIEF/Abstract/Symbol.hpp"
#include "LIEF/Abstract/DebugInfo.hpp"
#include "LIEF/asm/Engine.hpp"
#include "LIEF/asm/Instruction.hpp"
namespace LIEF {
Binary::Binary(FORMATS fmt) :
format_{fmt}
{}
Binary::Binary() = default;
Binary::~Binary() = default;
const Symbol* Binary::get_symbol(const std::string& name) const {
symbols_t symbols = const_cast<Binary*>(this)->get_abstract_symbols();
const auto it_symbol = std::find_if(
std::begin(symbols), std::end(symbols),
[&name] (const Symbol* s) { return s->name() == name; }
);
if (it_symbol == std::end(symbols)) {
return nullptr;
}
return *it_symbol;
}
result<uint64_t> Binary::get_function_address(const std::string& name) const {
if constexpr (lief_extended) {
if (const DebugInfo* dbg = debug_info()) {
if (auto addr = dbg->find_function_address(name)) {
return *addr;
}
}
}
return make_error_code(lief_errors::not_found);
}
std::vector<uint64_t> Binary::xref(uint64_t address) const {
std::vector<uint64_t> result;
for (Section* section : const_cast<Binary*>(this)->get_abstract_sections()) {
std::vector<size_t> founds = section->search_all(address);
for (size_t found : founds) {
result.push_back(section->virtual_address() + found);
}
}
return result;
}
uint64_t Binary::page_size() const {
return get_pagesize(*this);
}
void Binary::accept(Visitor& visitor) const {
visitor.visit(*this);
}
const char* to_string(Binary::VA_TYPES e) {
#define ENTRY(X) std::pair(Binary::VA_TYPES::X, #X)
STRING_MAP enums2str {
ENTRY(AUTO),
ENTRY(RVA),
ENTRY(VA),
};
#undef ENTRY
if (auto it = enums2str.find(e); it != enums2str.end()) {
return it->second;
}
return "UNKNOWN";
}
const char* to_string(Binary::FORMATS e) {
#define ENTRY(X) std::pair(Binary::FORMATS::X, #X)
STRING_MAP enums2str {
ENTRY(UNKNOWN),
ENTRY(ELF),
ENTRY(PE),
ENTRY(MACHO),
ENTRY(OAT),
};
#undef ENTRY
if (auto it = enums2str.find(e); it != enums2str.end()) {
return it->second;
}
return "UNKNOWN";
}
}