forked from nodejs/node
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRelocationFixup.cpp
More file actions
286 lines (249 loc) · 9.28 KB
/
Copy pathRelocationFixup.cpp
File metadata and controls
286 lines (249 loc) · 9.28 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
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
/* 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 "spdlog/fmt/fmt.h"
#include "LIEF/Visitor.hpp"
#include "LIEF/MachO/Symbol.hpp"
#include "LIEF/MachO/RelocationFixup.hpp"
#include "MachO/ChainedFixup.hpp"
namespace LIEF {
namespace MachO {
inline uint64_t unpack_target(const details::dyld_chained_ptr_64_rebase& rebase) {
details::dyld_chained_ptr_generic64 value;
value.rebase = rebase;
return value.unpack_target();
}
inline uint64_t unpack_target(const details::dyld_chained_ptr_arm64e_rebase& rebase) {
details::dyld_chained_ptr_arm64e value;
value.rebase = rebase;
return value.unpack_target();
}
RelocationFixup::~RelocationFixup() {
switch (rtypes_) {
case REBASE_TYPES::ARM64E_REBASE: delete arm64_rebase_; break;
case REBASE_TYPES::ARM64E_AUTH_REBASE: delete arm64_auth_rebase_; break;
case REBASE_TYPES::PTR64_REBASE: delete p64_rebase_; break;
case REBASE_TYPES::PTR32_REBASE: delete p32_rebase_; break;
case REBASE_TYPES::SEGMENTED: delete segmented_rebase_; break;
case REBASE_TYPES::AUTH_SEGMENTED: delete auth_segmented_rebase_; break;
case REBASE_TYPES::UNKNOWN: {}
}
}
RelocationFixup::RelocationFixup(DYLD_CHAINED_PTR_FORMAT fmt, uint64_t imagebase) :
ptr_fmt_{fmt},
imagebase_{imagebase}
{}
RelocationFixup& RelocationFixup::operator=(const RelocationFixup& other) {
if (this == &other) {
return *this;
}
ptr_fmt_ = other.ptr_fmt_;
imagebase_ = other.imagebase_;
offset_ = other.offset_;
rtypes_ = other.rtypes_;
switch (rtypes_) {
case REBASE_TYPES::ARM64E_REBASE: arm64_rebase_ = new details::dyld_chained_ptr_arm64e_rebase{*other.arm64_rebase_}; break;
case REBASE_TYPES::ARM64E_AUTH_REBASE: arm64_auth_rebase_ = new details::dyld_chained_ptr_arm64e_auth_rebase{*other.arm64_auth_rebase_}; break;
case REBASE_TYPES::PTR64_REBASE: p64_rebase_ = new details::dyld_chained_ptr_64_rebase{*other.p64_rebase_}; break;
case REBASE_TYPES::PTR32_REBASE: p32_rebase_ = new details::dyld_chained_ptr_32_rebase{*other.p32_rebase_}; break;
case REBASE_TYPES::SEGMENTED: segmented_rebase_ = new details::dyld_chained_ptr_arm64e_segmented_rebase{*other.segmented_rebase_}; break;
case REBASE_TYPES::AUTH_SEGMENTED: auth_segmented_rebase_ = new details::dyld_chained_ptr_arm64e_auth_segmented_rebase{*other.auth_segmented_rebase_}; break;
case REBASE_TYPES::UNKNOWN: {}
}
return *this;
}
RelocationFixup::RelocationFixup(const RelocationFixup& other) :
Relocation(other),
ptr_fmt_{other.ptr_fmt_},
imagebase_{other.imagebase_},
offset_{other.offset_},
rtypes_{other.rtypes_}
{
switch (rtypes_) {
case REBASE_TYPES::ARM64E_REBASE: arm64_rebase_ = new details::dyld_chained_ptr_arm64e_rebase{*other.arm64_rebase_}; break;
case REBASE_TYPES::ARM64E_AUTH_REBASE: arm64_auth_rebase_ = new details::dyld_chained_ptr_arm64e_auth_rebase{*other.arm64_auth_rebase_}; break;
case REBASE_TYPES::PTR64_REBASE: p64_rebase_ = new details::dyld_chained_ptr_64_rebase{*other.p64_rebase_}; break;
case REBASE_TYPES::PTR32_REBASE: p32_rebase_ = new details::dyld_chained_ptr_32_rebase{*other.p32_rebase_}; break;
case REBASE_TYPES::SEGMENTED: segmented_rebase_ = new details::dyld_chained_ptr_arm64e_segmented_rebase{*other.segmented_rebase_}; break;
case REBASE_TYPES::AUTH_SEGMENTED: auth_segmented_rebase_ = new details::dyld_chained_ptr_arm64e_auth_segmented_rebase{*other.auth_segmented_rebase_}; break;
case REBASE_TYPES::UNKNOWN: {}
}
}
void RelocationFixup::accept(Visitor& visitor) const {
visitor.visit(*this);
}
std::ostream& RelocationFixup::print(std::ostream& os) const {
os << fmt::format("0x{:08x}: 0x{:08x}", address(), target());
if (const Symbol* sym = symbol()) {
os << fmt::format("({})", sym->name());
}
os << '\n';
return Relocation::print(os);
}
uint64_t RelocationFixup::target() const {
switch (rtypes_) {
case REBASE_TYPES::ARM64E_REBASE:
{
return imagebase_ + unpack_target(*arm64_rebase_);
}
case REBASE_TYPES::ARM64E_AUTH_REBASE:
{
return imagebase_ + arm64_auth_rebase_->target;
}
case REBASE_TYPES::PTR64_REBASE:
{
return ptr_fmt_ == DYLD_CHAINED_PTR_FORMAT::PTR_64 ?
unpack_target(*p64_rebase_) :
imagebase_ + unpack_target(*p64_rebase_);
}
case REBASE_TYPES::PTR32_REBASE:
{
/* We tricked the parser to make sure it covers the case
* where fixup.rebase.target > seg_info.max_valid_pointer
*/
return imagebase_ + p32_rebase_->target;
}
case REBASE_TYPES::SEGMENTED:
case REBASE_TYPES::AUTH_SEGMENTED:
{
LIEF_ERR("Segmented rebase is not supported");
return -1llu;
}
case REBASE_TYPES::UNKNOWN:
{
LIEF_ERR("Can't get target: unknown rebase type");
break;
}
}
return 0;
}
void RelocationFixup::target(uint64_t target) {
switch (rtypes_) {
case REBASE_TYPES::ARM64E_REBASE:
{
uint64_t rel_target = target;
if (rel_target >= imagebase_) {
rel_target -= imagebase_;
}
pack_target(*arm64_rebase_, rel_target);
break;
}
case REBASE_TYPES::ARM64E_AUTH_REBASE:
{
uint64_t rel_target = target;
if (rel_target >= imagebase_) {
rel_target -= imagebase_;
}
arm64_auth_rebase_->target = rel_target;
break;
}
case REBASE_TYPES::PTR64_REBASE:
{
uint64_t rel_target = target;
if (ptr_fmt_ == DYLD_CHAINED_PTR_FORMAT::PTR_64_OFFSET && rel_target >= imagebase_) {
rel_target -= imagebase_;
}
pack_target(*p64_rebase_, rel_target);
break;
}
case REBASE_TYPES::PTR32_REBASE:
{
LIEF_WARN("Updating a dyld_chained_ptr_generic32 is not supported yet");
return;
}
case REBASE_TYPES::SEGMENTED:
case REBASE_TYPES::AUTH_SEGMENTED:
{
LIEF_ERR("Segmented rebase is not supported");
return;
}
case REBASE_TYPES::UNKNOWN:
{
LIEF_ERR("Can't set target: unknown rebase type");
break;
}
}
}
uint32_t RelocationFixup::next() const {
switch (rtypes_) {
case REBASE_TYPES::ARM64E_REBASE:
return arm64_rebase_->next;
case REBASE_TYPES::ARM64E_AUTH_REBASE:
return arm64_auth_rebase_->next;
case REBASE_TYPES::PTR64_REBASE:
return p64_rebase_->next;
case REBASE_TYPES::PTR32_REBASE:
return p32_rebase_->next;
case REBASE_TYPES::SEGMENTED:
return segmented_rebase_->next;
case REBASE_TYPES::AUTH_SEGMENTED:
return auth_segmented_rebase_->next;
case REBASE_TYPES::UNKNOWN:
return 0;
}
return 0;
}
void RelocationFixup::next(uint32_t value) {
switch (rtypes_) {
case REBASE_TYPES::ARM64E_REBASE:
arm64_rebase_->next = value;
break;
case REBASE_TYPES::ARM64E_AUTH_REBASE:
arm64_auth_rebase_->next = value;
break;
case REBASE_TYPES::PTR64_REBASE:
p64_rebase_->next = value;
break;
case REBASE_TYPES::PTR32_REBASE:
p32_rebase_->next = value;
break;
case REBASE_TYPES::SEGMENTED:
segmented_rebase_->next = value;
break;
case REBASE_TYPES::AUTH_SEGMENTED:
auth_segmented_rebase_->next = value;
break;
case REBASE_TYPES::UNKNOWN:
return;
}
return;
}
void RelocationFixup::set(const details::dyld_chained_ptr_arm64e_rebase& fixup) {
rtypes_ = REBASE_TYPES::ARM64E_REBASE;
arm64_rebase_ = new details::dyld_chained_ptr_arm64e_rebase(fixup);
}
void RelocationFixup::set(const details::dyld_chained_ptr_arm64e_auth_rebase& fixup) {
rtypes_ = REBASE_TYPES::ARM64E_AUTH_REBASE;
arm64_auth_rebase_ = new details::dyld_chained_ptr_arm64e_auth_rebase(fixup);
}
void RelocationFixup::set(const details::dyld_chained_ptr_64_rebase& fixup) {
rtypes_ = REBASE_TYPES::PTR64_REBASE;
p64_rebase_ = new details::dyld_chained_ptr_64_rebase(fixup);
}
void RelocationFixup::set(const details::dyld_chained_ptr_32_rebase& fixup) {
rtypes_ = REBASE_TYPES::PTR32_REBASE;
p32_rebase_ = new details::dyld_chained_ptr_32_rebase(fixup);
}
void RelocationFixup::set(const details::dyld_chained_ptr_arm64e_segmented_rebase& fixup) {
rtypes_ = REBASE_TYPES::SEGMENTED;
segmented_rebase_ = new details::dyld_chained_ptr_arm64e_segmented_rebase(fixup);
}
void RelocationFixup::set(const details::dyld_chained_ptr_arm64e_auth_segmented_rebase& fixup) {
rtypes_ = REBASE_TYPES::AUTH_SEGMENTED;
auth_segmented_rebase_ = new details::dyld_chained_ptr_arm64e_auth_segmented_rebase(fixup);
}
}
}