File: Header.cpp

package info (click to toggle)
lief 0.9.0-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, bullseye, buster, sid
  • size: 16,036 kB
  • sloc: cpp: 76,013; python: 6,167; ansic: 3,355; pascal: 404; sh: 98; makefile: 32
file content (268 lines) | stat: -rw-r--r-- 8,820 bytes parent folder | download | duplicates (2)
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
266
267
268
/* Copyright 2017 R. Thomas
 * Copyright 2017 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/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(void) :
  magic_{{'o', 'a', 't', '\n'}},
  version_{0}
{}


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" },
  };

  auto   it  = keys2str.find(key);
  return it == keys2str.end() ? "UNKNOWN" : it->second;
}


Header::magic_t Header::magic(void) const {
  return this->magic_;
}

oat_version_t Header::version(void) const {
  return this->version_;
}


INSTRUCTION_SETS Header::instruction_set(void) const {
  return this->instruction_set_;
}


uint32_t Header::checksum(void) const {
  return this->checksum_;
}

uint32_t Header::nb_dex_files(void) const {
  return this->dex_file_count_;
}


uint32_t Header::executable_offset(void) const {
  return this->executable_offset_;
}

uint32_t Header::i2i_bridge_offset(void) const {
  return this->i2i_bridge_offset_;
}

uint32_t Header::i2c_code_bridge_offset(void) const {
  return this->i2c_code_bridge_offset_;
}

uint32_t Header::jni_dlsym_lookup_offset(void) const {
  return this->jni_dlsym_lookup_offset_;
}

uint32_t Header::quick_generic_jni_trampoline_offset(void) const {
  return this->quick_generic_jni_trampoline_offset_;
}

uint32_t Header::quick_imt_conflict_trampoline_offset(void) const {
  return this->quick_generic_jni_trampoline_offset_;
}

uint32_t Header::quick_resolution_trampoline_offset(void) const {
  return this->quick_imt_conflict_trampoline_offset_;
}

uint32_t Header::quick_to_interpreter_bridge_offset(void) const {
  return this->quick_to_interpreter_bridge_offset_;
}

int32_t Header::image_patch_delta(void) const {
  return this->image_patch_delta_;
}

uint32_t Header::image_file_location_oat_checksum(void) const {
  return this->image_file_location_oat_checksum_;
}
uint32_t Header::image_file_location_oat_data_begin(void) const {
  return this->image_file_location_oat_data_begin_;
}

uint32_t Header::key_value_size(void) const {
  return this->key_value_store_size_;
}

uint32_t Header::oat_dex_files_offset(void) const {
  return this->oat_dex_files_offset_;
}

Header::it_key_values_t Header::key_values(void) {
  it_key_values_t::container_type key_values_list;
  key_values_list.reserve(this->dex2oat_context_.size());

  for (auto&& p : this->dex2oat_context_) {
    HEADER_KEYS key = p.first;
    std::string& value = this->dex2oat_context_.at(key);
    key_values_list.emplace_back(key, std::ref(value));
  }
  return key_values_list;
}

Header::it_const_key_values_t Header::key_values(void) const {
  std::remove_const<it_const_key_values_t::container_type>::type key_values_list;
  for (auto&& p : this->dex2oat_context_) {
    HEADER_KEYS key = p.first;
    std::string value = this->dex2oat_context_.at(key);
    key_values_list.emplace_back(key, value);
  }
  return key_values_list;
}


Header::keys_t Header::keys(void) const {
  Header::keys_t keys_list;
  keys_list.reserve(this->dex2oat_context_.size());
  for (auto p : this->dex2oat_context_) {
    keys_list.push_back(p.first);
  }
  return keys_list;
}

Header::values_t Header::values(void) const {
  Header::values_t values_list;
  values_list.reserve(this->dex2oat_context_.size());
  for (auto p : this->dex2oat_context_) {
    values_list.push_back(p.second);
  }
  return values_list;
}

const std::string& Header::get(HEADER_KEYS key) const {
  auto&& it = this->dex2oat_context_.find(key);
  if (it == std::end(this->dex2oat_context_)) {
    throw not_found("Unable to find the key " + Header::key_to_string(key));
  }
  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) {
  auto&& it = this->dex2oat_context_.find(key);
  if (it == std::end(this->dex2oat_context_)) {
    throw not_found(std::string{"Can't find key: '"} + to_string(key) + "'");
  }

  it->second = value;
  return *this;
}

const std::string& Header::operator[](HEADER_KEYS key) const {
  return this->get(key);
}

std::string& Header::operator[](HEADER_KEYS key) {
  return this->get(key);
}

void Header::accept(Visitor& visitor) const {
  visitor.visit(*this);
}

bool Header::operator==(const Header& rhs) const {
  size_t hash_lhs = Hash::hash(*this);
  size_t hash_rhs = Hash::hash(rhs);
  return hash_lhs == hash_rhs;
}

bool Header::operator!=(const Header& rhs) const {
  return not (*this == rhs);
}



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 << std::endl;
  //os << std::setw(33) << std::setfill(' ') << "Location:"  << ident_magic << std::endl;
  os << std::setw(WIDTH) << std::setfill(' ') << "Checksum:"  << std::hex << hdr.checksum() << std::endl;
  os << std::setw(WIDTH) << std::setfill(' ') << "Instruction set:"  << to_string(hdr.instruction_set()) << std::endl;
  //os << std::setw(33) << std::setfill(' ') << "Instruction set features:"  << ident_magic << std::endl;
  os << std::setw(WIDTH) << std::setfill(' ') << "Dex file count:"                              << std::dec << hdr.nb_dex_files()      << std::endl;
  os << std::setw(WIDTH) << std::setfill(' ') << "Executable offset:"                           << std::hex << hdr.executable_offset() << std::endl;

  os << std::endl;

  os << std::setw(WIDTH) << std::setfill(' ') << "Interpreter to Interpreter Bridge Offset:"   << std::hex << hdr.i2i_bridge_offset()      << std::endl;
  os << std::setw(WIDTH) << std::setfill(' ') << "Interpreter to Compiled Code Bridge Offset:" << std::hex << hdr.i2c_code_bridge_offset() << std::endl;

  os << std::endl;

  os << std::setw(WIDTH) << std::setfill(' ') << "JNI dlsym lookup offset:" << std::hex << hdr.jni_dlsym_lookup_offset() << std::endl;

  os << std::endl;

  os << std::setw(WIDTH) << std::setfill(' ') << "Quick Generic JNI Trampoline Offset:"  << std::hex << hdr.quick_generic_jni_trampoline_offset()  << std::endl;
  os << std::setw(WIDTH) << std::setfill(' ') << "Quick IMT Conflict Trampoline Offset:" << std::hex << hdr.quick_imt_conflict_trampoline_offset() << std::endl;
  os << std::setw(WIDTH) << std::setfill(' ') << "Quick Resolution Trampoline Offset:"   << std::hex << hdr.quick_resolution_trampoline_offset()   << std::endl;
  os << std::setw(WIDTH) << std::setfill(' ') << "Quick to Interpreter Bridge Offset:"   << std::hex << hdr.quick_to_interpreter_bridge_offset()   << std::endl;

  os << std::endl;

  os << std::setw(WIDTH) << std::setfill(' ') << "Image Patch Delta:" << std::dec << hdr.image_patch_delta() << std::endl;

  os << std::endl;

  os << std::setw(WIDTH) << std::setfill(' ') << "Image File Location OAT Checksum:" << std::hex << hdr.image_file_location_oat_checksum()   << std::endl;
  os << std::setw(WIDTH) << std::setfill(' ') << "Image File Location OAT Begin:"    << std::hex << hdr.image_file_location_oat_data_begin() << std::endl;

  os << std::endl;

  for (auto&& p : hdr.key_values()) {
    os << std::setw(WIDTH) << std::setfill(' ') << Header::key_to_string(p.first) + ":" << p.second << std::endl;
  }

  return os;
}




}
}