forked from nodejs/node
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathHeader.cpp
More file actions
265 lines (198 loc) · 8.05 KB
/
Copy pathHeader.cpp
File metadata and controls
265 lines (198 loc) · 8.05 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
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
/* 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 "logging.hpp"
#include "LIEF/OAT/Header.hpp"
#include "LIEF/OAT/EnumToString.hpp"
#include "LIEF/OAT/hash.hpp"
#include <map>
#include <iomanip>
#include <sstream>
namespace LIEF {
namespace OAT {
Header::Header(const Header&) = default;
Header& Header::operator=(const Header&) = default;
Header::Header() :
magic_{{'o', 'a', 't', '\n'}}
{}
std::string Header::key_to_string(HEADER_KEYS key) {
const static std::map<HEADER_KEYS, const char*> keys2str = {
{ HEADER_KEYS::KEY_IMAGE_LOCATION, "image-location" },
{ HEADER_KEYS::KEY_DEX2OAT_CMD_LINE, "dex2oat-cmdline" },
{ HEADER_KEYS::KEY_DEX2OAT_HOST, "dex2oat-host" },
{ HEADER_KEYS::KEY_PIC, "pic" },
{ HEADER_KEYS::KEY_HAS_PATCH_INFO, "has-patch-info" },
{ HEADER_KEYS::KEY_DEBUGGABLE, "debuggable" },
{ HEADER_KEYS::KEY_NATIVE_DEBUGGABLE, "native-debuggable" },
{ HEADER_KEYS::KEY_COMPILER_FILTER, "compiler-filter" },
{ HEADER_KEYS::KEY_CLASS_PATH, "classpath" },
{ HEADER_KEYS::KEY_BOOT_CLASS_PATH, "bootclasspath" },
{ HEADER_KEYS::KEY_CONCURRENT_COPYING, "concurrent-copying" },
{ HEADER_KEYS::KE_COMPILATION_REASON, "compilation-reason" },
};
auto it = keys2str.find(key);
return it == keys2str.end() ? "UNKNOWN" : it->second;
}
Header::magic_t Header::magic() const {
return magic_;
}
oat_version_t Header::version() const {
return version_;
}
INSTRUCTION_SETS Header::instruction_set() const {
return instruction_set_;
}
uint32_t Header::checksum() const {
return checksum_;
}
uint32_t Header::nb_dex_files() const {
return dex_file_count_;
}
uint32_t Header::executable_offset() const {
return executable_offset_;
}
uint32_t Header::i2i_bridge_offset() const {
return i2i_bridge_offset_;
}
uint32_t Header::i2c_code_bridge_offset() const {
return i2c_code_bridge_offset_;
}
uint32_t Header::jni_dlsym_lookup_offset() const {
return jni_dlsym_lookup_offset_;
}
uint32_t Header::quick_generic_jni_trampoline_offset() const {
return quick_generic_jni_trampoline_offset_;
}
uint32_t Header::quick_imt_conflict_trampoline_offset() const {
return quick_generic_jni_trampoline_offset_;
}
uint32_t Header::quick_resolution_trampoline_offset() const {
return quick_imt_conflict_trampoline_offset_;
}
uint32_t Header::quick_to_interpreter_bridge_offset() const {
return quick_to_interpreter_bridge_offset_;
}
int32_t Header::image_patch_delta() const {
return image_patch_delta_;
}
uint32_t Header::image_file_location_oat_checksum() const {
return image_file_location_oat_checksum_;
}
uint32_t Header::image_file_location_oat_data_begin() const {
return image_file_location_oat_data_begin_;
}
uint32_t Header::key_value_size() const {
return key_value_store_size_;
}
uint32_t Header::oat_dex_files_offset() const {
return oat_dex_files_offset_;
}
Header::it_key_values_t Header::key_values() {
it_key_values_t::container_type key_values_list;
key_values_list.reserve(dex2oat_context_.size());
for (auto& [k, v] :dex2oat_context_) {
HEADER_KEYS key = k;
std::string& value = v;
key_values_list.emplace_back(key, value);
}
return key_values_list;
}
Header::it_const_key_values_t Header::key_values() const {
std::remove_const<it_const_key_values_t::container_type>::type key_values_list;
key_values_list.reserve(dex2oat_context_.size());
for (const auto& [k, v] :dex2oat_context_) {
HEADER_KEYS key = k;
const std::string& value = v;
key_values_list.emplace_back(key, value);
}
return key_values_list;
}
Header::keys_t Header::keys() const {
Header::keys_t keys_list;
keys_list.reserve(dex2oat_context_.size());
for (const auto& p : dex2oat_context_) {
keys_list.push_back(p.first);
}
return keys_list;
}
Header::values_t Header::values() const {
Header::values_t values_list;
values_list.reserve(dex2oat_context_.size());
for (const auto& p : dex2oat_context_) {
values_list.push_back(p.second);
}
return values_list;
}
const std::string* Header::get(HEADER_KEYS key) const {
const auto it = dex2oat_context_.find(key);
if (it == std::end(dex2oat_context_)) {
return nullptr;
}
return &it->second;
}
std::string* Header::get(HEADER_KEYS key) {
return const_cast<std::string*>(static_cast<const Header*>(this)->get(key));
}
Header& Header::set(HEADER_KEYS key, const std::string& value) {
const auto it = dex2oat_context_.find(key);
if (it == std::end(dex2oat_context_)) {
LIEF_WARN("Can't find the key {}", to_string(key));
return *this;
}
it->second = value;
return *this;
}
const std::string* Header::operator[](HEADER_KEYS key) const {
return get(key);
}
std::string* Header::operator[](HEADER_KEYS key) {
return get(key);
}
void Header::accept(Visitor& visitor) const {
visitor.visit(*this);
}
std::ostream& operator<<(std::ostream& os, const Header& hdr) {
static constexpr size_t WIDTH = 45;
os << std::hex << std::left << std::showbase;
//os << std::setw(33) << std::setfill(' ') << "Version:" << ident_magic << '\n';
//os << std::setw(33) << std::setfill(' ') << "Location:" << ident_magic << '\n';
os << std::setw(WIDTH) << std::setfill(' ') << "Checksum:" << std::hex << hdr.checksum() << '\n';
os << std::setw(WIDTH) << std::setfill(' ') << "Instruction set:" << to_string(hdr.instruction_set()) << '\n';
//os << std::setw(33) << std::setfill(' ') << "Instruction set features:" << ident_magic << '\n';
os << std::setw(WIDTH) << std::setfill(' ') << "Dex file count:" << std::dec << hdr.nb_dex_files() << '\n';
os << std::setw(WIDTH) << std::setfill(' ') << "Executable offset:" << std::hex << hdr.executable_offset() << '\n';
os << '\n';
os << std::setw(WIDTH) << std::setfill(' ') << "Interpreter to Interpreter Bridge Offset:" << std::hex << hdr.i2i_bridge_offset() << '\n';
os << std::setw(WIDTH) << std::setfill(' ') << "Interpreter to Compiled Code Bridge Offset:" << std::hex << hdr.i2c_code_bridge_offset() << '\n';
os << '\n';
os << std::setw(WIDTH) << std::setfill(' ') << "JNI dlsym lookup offset:" << std::hex << hdr.jni_dlsym_lookup_offset() << '\n';
os << '\n';
os << std::setw(WIDTH) << std::setfill(' ') << "Quick Generic JNI Trampoline Offset:" << std::hex << hdr.quick_generic_jni_trampoline_offset() << '\n';
os << std::setw(WIDTH) << std::setfill(' ') << "Quick IMT Conflict Trampoline Offset:" << std::hex << hdr.quick_imt_conflict_trampoline_offset() << '\n';
os << std::setw(WIDTH) << std::setfill(' ') << "Quick Resolution Trampoline Offset:" << std::hex << hdr.quick_resolution_trampoline_offset() << '\n';
os << std::setw(WIDTH) << std::setfill(' ') << "Quick to Interpreter Bridge Offset:" << std::hex << hdr.quick_to_interpreter_bridge_offset() << '\n';
os << '\n';
os << std::setw(WIDTH) << std::setfill(' ') << "Image Patch Delta:" << std::dec << hdr.image_patch_delta() << '\n';
os << '\n';
os << std::setw(WIDTH) << std::setfill(' ') << "Image File Location OAT Checksum:" << std::hex << hdr.image_file_location_oat_checksum() << '\n';
os << std::setw(WIDTH) << std::setfill(' ') << "Image File Location OAT Begin:" << std::hex << hdr.image_file_location_oat_data_begin() << '\n';
os << '\n';
for (const auto& p : hdr.key_values()) {
os << std::setw(WIDTH) << std::setfill(' ') << Header::key_to_string(p.key) + ":" << *p.value << '\n';
}
return os;
}
}
}