forked from nodejs/node
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutils.cpp
More file actions
115 lines (94 loc) · 3.08 KB
/
Copy pathutils.cpp
File metadata and controls
115 lines (94 loc) · 3.08 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
/* 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 <string>
#include "OAT/Structures.hpp"
#include "LIEF/OAT/utils.hpp"
#include "LIEF/ELF/Binary.hpp"
#include "LIEF/ELF/Parser.hpp"
#include "LIEF/ELF/Symbol.hpp"
#include "LIEF/ELF/utils.hpp"
#include "frozen.hpp"
namespace LIEF {
namespace OAT {
bool is_oat(const std::string& file) {
if (!ELF::is_elf(file)) {
return false;
}
if (const auto elf_binary = ELF::Parser::parse(file)) {
return is_oat(*elf_binary);
}
return false;
}
bool is_oat(const std::vector<uint8_t>& raw) {
if (const auto elf_binary = ELF::Parser::parse(raw)) {
return is_oat(*elf_binary);
}
return false;
}
bool is_oat(const ELF::Binary& elf) {
if (const auto* oatdata = elf.get_dynamic_symbol("oatdata")) {
span<const uint8_t> header =
elf.get_content_from_virtual_address(oatdata->value(), sizeof(details::oat_magic));
return std::equal(std::begin(header), std::end(header),
std::begin(details::oat_magic));
}
return false;
}
oat_version_t version(const std::string& file) {
if (!is_oat(file)) {
return 0;
}
if (const auto elf = ELF::Parser::parse(file)) {
return version(*elf);
}
return 0;
}
oat_version_t version(const std::vector<uint8_t>& raw) {
if (!is_oat(raw)) {
return 0;
}
if (const auto elf = ELF::Parser::parse(raw)) {
return version(*elf);
}
return 0;
}
oat_version_t version(const ELF::Binary& elf) {
if (const auto* oatdata = elf.get_dynamic_symbol("oatdata")) {
span<const uint8_t> header =
elf.get_content_from_virtual_address(oatdata->value() + sizeof(details::oat_magic),
sizeof(details::oat_version));
if (header.size() != sizeof(details::oat_version)) {
return 0;
}
return std::stoul(std::string(reinterpret_cast<const char*>(header.data()), 3));
}
return 0;
}
Android::ANDROID_VERSIONS android_version(oat_version_t version) {
CONST_MAP(oat_version_t, Android::ANDROID_VERSIONS, 6) oat2android {
{ 64, Android::ANDROID_VERSIONS::VERSION_601 },
{ 79, Android::ANDROID_VERSIONS::VERSION_700 },
{ 88, Android::ANDROID_VERSIONS::VERSION_712 },
{ 124, Android::ANDROID_VERSIONS::VERSION_800 },
{ 131, Android::ANDROID_VERSIONS::VERSION_810 },
{ 138, Android::ANDROID_VERSIONS::VERSION_900 },
};
auto it = oat2android.lower_bound(version);
return it == oat2android.end() ?
Android::ANDROID_VERSIONS::VERSION_UNKNOWN : it->second;
}
} // namespace OAT
} // namespace LIEF