forked from nodejs/node
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathNoteGnuProperty.cpp
More file actions
212 lines (184 loc) · 6.1 KB
/
Copy pathNoteGnuProperty.cpp
File metadata and controls
212 lines (184 loc) · 6.1 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
/* 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 <algorithm>
#include "spdlog/fmt/fmt.h"
#include "logging.hpp"
#include "frozen.hpp"
#include "ELF/NoteDetails/properties/common.hpp"
#include "LIEF/Visitor.hpp"
#include "LIEF/ELF/NoteDetails/NoteGnuProperty.hpp"
#include "LIEF/ELF/NoteDetails/properties/Generic.hpp"
#include "LIEF/ELF/NoteDetails/properties/AArch64Feature.hpp"
#include "LIEF/ELF/NoteDetails/properties/AArch64PAuth.hpp"
#include "LIEF/ELF/NoteDetails/properties/StackSize.hpp"
#include "LIEF/ELF/NoteDetails/properties/X86Feature.hpp"
#include "LIEF/ELF/NoteDetails/properties/X86ISA.hpp"
#include "LIEF/ELF/NoteDetails/properties/NoteNoCopyOnProtected.hpp"
#include "LIEF/BinaryStream/SpanStream.hpp"
namespace LIEF {
namespace ELF {
inline std::unique_ptr<NoteGnuProperty::Property>
parse_property(ARCH arch, SpanStream& stream) {
auto res_type = stream.read<uint32_t>();
if (!res_type) {
LIEF_DEBUG("Can't read type at offset: 0x{:04x}", stream.pos());
return nullptr;
}
auto data_size = stream.read<uint32_t>();
if (!data_size) {
LIEF_DEBUG("Can't read data size at offset: 0x{:04x}", stream.pos());
return nullptr;
}
auto res_content = stream.slice(stream.pos(), *data_size);
stream.increment_pos(*data_size);
if (!res_content) {
return nullptr;
}
SpanStream content = std::move(*res_content);
const uint32_t type = *res_type;
if (GNU_PROPERTY_LOPROC <= type && type <= GNU_PROPERTY_HIPROC) {
if (arch == ARCH::X86_64 ||
arch == ARCH::IAMCU ||
arch == ARCH::I386)
{
switch (type) {
case GNU_PROPERTY_X86_ISA_1_USED:
case GNU_PROPERTY_X86_ISA_1_NEEDED:
case GNU_PROPERTY_X86_COMPAT_ISA_1_USED:
case GNU_PROPERTY_X86_COMPAT_ISA_1_NEEDED:
case GNU_PROPERTY_X86_COMPAT_2_ISA_1_USED:
case GNU_PROPERTY_X86_COMPAT_2_ISA_1_NEEDED:
return X86ISA::create(type, content);
case GNU_PROPERTY_X86_FEATURE_1_AND:
case GNU_PROPERTY_X86_FEATURE_2_USED:
case GNU_PROPERTY_X86_FEATURE_2_NEEDED:
return X86Features::create(type, content);
default: return Generic::create(type);
}
}
if (arch == ARCH::AARCH64) {
if (type == GNU_PROPERTY_AARCH64_FEATURE_1_AND) {
return AArch64Feature::create(content);
}
if (type == GNU_PROPERTY_AARCH64_FEATURE_PAUTH) {
return AArch64PAuth::create(content);
}
return Generic::create(type);
}
}
switch (type) {
case GNU_PROPERTY_STACK_SIZE:
{
const size_t csize = content.size();
if (csize == sizeof(uint64_t)) {
return StackSize::create(content.read<uint64_t>().value_or(0));
}
if (csize == sizeof(uint32_t)) {
return StackSize::create(content.read<uint32_t>().value_or(0));
}
if (csize == sizeof(uint16_t)) {
return StackSize::create(content.read<uint16_t>().value_or(0));
}
if (csize == sizeof(uint8_t)) {
return StackSize::create(content.read<uint8_t>().value_or(0));
}
LIEF_ERR("Wrong stack size: {}", csize);
return StackSize::create(0);
}
case GNU_PROPERTY_NO_COPY_ON_PROTECTED: return NoteNoCopyOnProtected::create();
default:
{
if (
(GNU_PROPERTY_UINT32_AND_LO <= type && type <= GNU_PROPERTY_UINT32_AND_HI) ||
(GNU_PROPERTY_UINT32_OR_LO <= type && type <= GNU_PROPERTY_UINT32_OR_HI)
)
{
switch (type) {
case GNU_PROPERTY_1_NEEDED: /* TODO(romain) */ return Generic::create(type);
default: return Generic::create(type);
}
}
}
}
return Generic::create(type);
}
NoteGnuProperty::properties_t NoteGnuProperty::properties() const {
auto stream = SpanStream::from_vector(description_);
if (!stream) {
LIEF_WARN("Can't create stream");
return {};
}
const bool is64 = arch_ == ARCH::IA_64 || arch_ == ARCH::AARCH64 ||
arch_ == ARCH::LOONGARCH || arch_ == ARCH::X86_64;
properties_t props;
while (stream) {
if (auto prop = parse_property(arch_, *stream)) {
props.push_back(std::move(prop));
stream->align(is64 ? 8 : 4);
} else {
break;
}
}
return props;
}
std::unique_ptr<NoteGnuProperty::Property>
NoteGnuProperty::find(Property::TYPE type) const {
properties_t props = properties();
auto it = std::find_if(props.begin(), props.end(),
[type] (const std::unique_ptr<Property>& prop) {
return prop->type() == type;
}
);
if (it != props.end()) {
return std::move(*it);
}
return nullptr;
}
void NoteGnuProperty::dump(std::ostream& os) const {
Note::dump(os);
os << '\n';
const NoteGnuProperty::properties_t& props = properties();
for (const std::unique_ptr<NoteGnuProperty::Property>& prop : props) {
os << " " << *prop << '\n';
}
}
void NoteGnuProperty::accept(Visitor& visitor) const {
visitor.visit(*this);
}
const char* to_string(NoteGnuProperty::Property::TYPE type) {
#define ENTRY(X) std::pair(NoteGnuProperty::Property::TYPE::X, #X)
STRING_MAP enums2str {
ENTRY(UNKNOWN),
ENTRY(GENERIC),
ENTRY(AARCH64_FEATURES),
ENTRY(AARCH64_PAUTH),
ENTRY(STACK_SIZE),
ENTRY(NO_COPY_ON_PROTECTED),
ENTRY(X86_ISA),
ENTRY(X86_FEATURE),
ENTRY(NEEDED),
};
#undef ENTRY
if (auto it = enums2str.find(type); it != enums2str.end()) {
return it->second;
}
return "UNKNOWN";
}
void NoteGnuProperty::Property::dump(std::ostream& os) const {
os << to_string(this->type());
}
} // namespace ELF
} // namespace LIEF