LIEF: Library to Instrument Executable Formats Version 2.0.0
Loading...
Searching...
No Matches
ChainedPointerAnalysis.hpp
Go to the documentation of this file.
1/* Copyright 2017 - 2026 R. Thomas
2 * Copyright 2017 - 2026 Quarkslab
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16#ifndef LIEF_MACHO_CHAINED_PTR_ANALYSIS_H
17#define LIEF_MACHO_CHAINED_PTR_ANALYSIS_H
18#include <cstring>
19#include <functional>
20#include <memory>
21#include <ostream>
22#include <variant>
23
25#include "LIEF/errors.hpp"
26#include "LIEF/visibility.h"
27
28namespace LIEF {
29class BinaryStream;
30namespace MachO {
32 public:
33 // DYLD_CHAINED_PTR_FORMAT::PTR_ARM64E
35 // clang-format off
36 uint64_t target : 43,
37 high8 : 8,
38 next : 11,
39 bind : 1,
40 auth : 1;
41 // clang-format on
42
43 friend LIEF_API std::ostream&
44 operator<<(std::ostream& os,
46
47 uint64_t unpack_target() const {
48 return uint64_t(high8) | target;
49 }
50 };
51
52 // DYLD_CHAINED_PTR_FORMAT::PTR_ARM64E
54 // clang-format off
55 uint64_t ordinal : 16,
56 zero : 16,
57 addend : 19,
58 next : 11,
59 bind : 1,
60 auth : 1;
61 // clang-format on
62
63 friend LIEF_API std::ostream&
64 operator<<(std::ostream& os, const dyld_chained_ptr_arm64e_bind_t& chain);
65 };
66
67 // DYLD_CHAINED_PTR_FORMAT::PTR_ARM64E
69 // clang-format off
70 uint64_t target : 32,
73 key : 2,
74 next : 11,
75 bind : 1,
76 auth : 1;
77 // clang-format on
78
79 friend LIEF_API std::ostream&
80 operator<<(std::ostream& os,
82 };
83
84 // DYLD_CHAINED_PTR_FORMAT::PTR_ARM64E
86 // clang-format off
87 uint64_t ordinal : 16,
88 zero : 16,
91 key : 2,
92 next : 11,
93 bind : 1,
94 auth : 1;
95 // clang-format on
96
97 friend LIEF_API std::ostream&
98 operator<<(std::ostream& os,
100 };
101
102 // DYLD_CHAINED_PTR_FORMAT::PTR_64 & DYLD_CHAINED_PTR_FORMAT::PTR_64_OFFSET
104 // clang-format off
105 uint64_t target : 36,
106 high8 : 8,
108 next : 12,
109 bind : 1;
110 // clang-format on
111
112 friend LIEF_API std::ostream&
113 operator<<(std::ostream& os, const dyld_chained_ptr_64_rebase_t& chain);
114
115 uint64_t unpack_target() const {
116 return uint64_t(high8) | target;
117 }
118 };
119
120 // DYLD_CHAINED_PTR_FORMAT::PTR_ARM64E_USERLAND24
122 // clang-format off
123 uint64_t ordinal : 24,
124 zero : 8,
125 addend : 19,
126 next : 11,
127 bind : 1,
128 auth : 1;
129 // clang-format on
130
131 friend LIEF_API std::ostream&
132 operator<<(std::ostream& os,
134 };
135
136 // DYLD_CHAINED_PTR_FORMAT::PTR_ARM64E_USERLAND24
138 // clang-format off
139 uint64_t ordinal : 24,
140 zero : 8,
143 key : 2,
144 next : 11,
145 bind : 1,
146 auth : 1;
147 // clang-format on
148
149 friend LIEF_API std::ostream&
150 operator<<(std::ostream& os,
152 };
153
154 // DYLD_CHAINED_PTR_FORMAT::PTR_64
156 // clang-format off
157 uint64_t ordinal : 24,
160 next : 12,
161 bind : 1;
162 // clang-format on
163
164 friend LIEF_API std::ostream&
165 operator<<(std::ostream& os, const dyld_chained_ptr_64_bind_t& chain);
166 };
167
168 // DYLD_CHAINED_PTR_FORMAT::PTR_64_KERNEL_CACHE
170 // clang-format off
171 uint64_t target : 30,
175 key : 2,
176 next : 12,
178 // clang-format on
179
180 friend LIEF_API std::ostream&
181 operator<<(std::ostream& os,
183 };
184
185 // DYLD_CHAINED_PTR_FORMAT::PTR_32
187 // clang-format off
188 uint32_t target : 26,
189 next : 5,
190 bind : 1;
191 // clang-format on
192
193 friend LIEF_API std::ostream&
194 operator<<(std::ostream& os, const dyld_chained_ptr_32_rebase_t& chain);
195 };
196
197 // DYLD_CHAINED_PTR_FORMAT::PTR_32
199 // clang-format off
200 uint32_t ordinal : 20,
202 next : 5,
203 bind : 1;
204 // clang-format on
205
206 friend LIEF_API std::ostream&
207 operator<<(std::ostream& os, const dyld_chained_ptr_32_bind_t& chain);
208 };
209
210 // DYLD_CHAINED_PTR_FORMAT::PTR_32_CACHE
212 // clang-format off
213 uint32_t target : 30,
214 next : 2;
215 // clang-format on
216
217 friend LIEF_API std::ostream&
218 operator<<(std::ostream& os,
220 };
221
222 // DYLD_CHAINED_PTR_FORMAT::PTR_32_FIRMWARE
224 // clang-format off
225 uint32_t target : 26,
226 next : 6;
227 // clang-format on
228
229 friend LIEF_API std::ostream&
230 operator<<(std::ostream& os,
232 };
233
234 // DYLD_CHAINED_PTR_ARM64E_SEGMENTED
236 // clang-format off
237 uint32_t target_seg_offset : 28,
239 uint32_t padding : 19,
240 next : 12,
241 auth : 1;
242 // clang-format on
243
244 friend LIEF_API std::ostream&
245 operator<<(std::ostream& os,
247 };
248
249 // DYLD_CHAINED_PTR_ARM64E_SEGMENTED
251 // clang-format off
252 uint32_t target_seg_offset : 28,
254 uint32_t diversity : 16,
256 key : 2,
257 next : 12,
258 auth : 1;
259 // clang-format on
260
261 friend LIEF_API std::ostream&
262 operator<<(std::ostream& os,
264 };
265
266
267 enum class PTR_TYPE : uint64_t {
269 DYLD_CHAINED_PTR_ARM64E_REBASE,
270 DYLD_CHAINED_PTR_ARM64E_BIND,
271 DYLD_CHAINED_PTR_ARM64E_AUTH_REBASE,
272 DYLD_CHAINED_PTR_ARM64E_AUTH_BIND,
273 DYLD_CHAINED_PTR_64_REBASE,
274 DYLD_CHAINED_PTR_ARM64E_BIND24,
275 DYLD_CHAINED_PTR_ARM64E_AUTH_BIND24,
276 DYLD_CHAINED_PTR_64_BIND,
277 DYLD_CHAINED_PTR_64_KERNEL_CACHE_REBASE,
278 DYLD_CHAINED_PTR_32_REBASE,
279 DYLD_CHAINED_PTR_32_BIND,
280 DYLD_CHAINED_PTR_32_CACHE_REBASE,
281 DYLD_CHAINED_PTR_32_FIRMWARE_REBASE,
282 DYLD_CHAINED_PTR_ARM64E_SEGMENTED_REBASE,
283 DYLD_CHAINED_PTR_ARM64E_AUTH_SEGMENTED_REBASE,
284 };
285
286 static std::unique_ptr<ChainedPointerAnalysis> from_value(uint64_t value,
287 size_t size) {
288 return std::make_unique<ChainedPointerAnalysis>(value, size);
289 }
290
313
335
337 value_(value),
338 size_(size) {}
339
342
344 ChainedPointerAnalysis& operator=(ChainedPointerAnalysis&&) noexcept = default;
345
347
348 uint64_t value() const {
349 return value_;
350 }
351
352 size_t size() const {
353 return size_;
354 }
355
358 std::memcpy(&result, &value_, sizeof(result));
359 return result;
360 }
361
364 std::memcpy(&result, &value_, sizeof(result));
365 return result;
366 }
367
368 const dyld_chained_ptr_arm64e_auth_rebase_t
371 std::memcpy(&result, &value_, sizeof(result));
372 return result;
373 }
374
375 const dyld_chained_ptr_arm64e_auth_bind_t
378 std::memcpy(&result, &value_, sizeof(result));
379 return result;
380 }
381
384 std::memcpy(&result, &value_, sizeof(result));
385 return result;
386 }
387
390 std::memcpy(&result, &value_, sizeof(result));
391 return result;
392 }
393
394 const dyld_chained_ptr_arm64e_auth_bind24_t
397 std::memcpy(&result, &value_, sizeof(result));
398 return result;
399 }
400
403 std::memcpy(&result, &value_, sizeof(result));
404 return result;
405 }
406
407 const dyld_chained_ptr_64_kernel_cache_rebase_t
410 std::memcpy(&result, &value_, sizeof(result));
411 return result;
412 }
413
416 std::memcpy(&result, &value_, sizeof(result));
417 return result;
418 }
419
422 std::memcpy(&result, &value_, sizeof(result));
423 return result;
424 }
425
426 const dyld_chained_ptr_32_cache_rebase_t
429 std::memcpy(&result, &value_, sizeof(result));
430 return result;
431 }
432
433 const dyld_chained_ptr_32_firmware_rebase_t
436 std::memcpy(&result, &value_, sizeof(result));
437 return result;
438 }
439
440 const dyld_chained_ptr_arm64e_segmented_rebase_t
443 std::memcpy(&result, &value_, sizeof(result));
444 return result;
445 }
446
447 const dyld_chained_ptr_arm64e_auth_segmented_rebase_t
453
497
499
500 static uint64_t walk_chain(
502 const std::function<int(uint64_t, const union_pointer_t& ptr)>& callback
503 );
504
505 private:
506 uint64_t value_ = 0;
507 size_t size_ = 0;
508};
509}
510}
511#endif
Class that is used to a read stream of data from different sources.
Definition BinaryStream.hpp:35
PTR_TYPE
Definition ChainedPointerAnalysis.hpp:267
union_pointer_t get_as(DYLD_CHAINED_PTR_FORMAT fmt) const
ChainedPointerAnalysis(uint64_t value, size_t size)
Definition ChainedPointerAnalysis.hpp:336
const dyld_chained_ptr_arm64e_segmented_rebase_t dyld_chained_ptr_arm64e_segmented_rebase() const
Definition ChainedPointerAnalysis.hpp:441
const dyld_chained_ptr_32_bind_t dyld_chained_ptr_32_bind() const
Definition ChainedPointerAnalysis.hpp:420
const dyld_chained_ptr_64_bind_t dyld_chained_ptr_64_bind() const
Definition ChainedPointerAnalysis.hpp:401
const dyld_chained_ptr_64_rebase_t dyld_chained_ptr_64_rebase() const
Definition ChainedPointerAnalysis.hpp:382
size_t size() const
Definition ChainedPointerAnalysis.hpp:352
const dyld_chained_ptr_arm64e_rebase_t dyld_chained_ptr_arm64e_rebase() const
Definition ChainedPointerAnalysis.hpp:356
const dyld_chained_ptr_arm64e_auth_rebase_t dyld_chained_ptr_arm64e_auth_rebase() const
Definition ChainedPointerAnalysis.hpp:369
const dyld_chained_ptr_64_kernel_cache_rebase_t dyld_chained_ptr_64_kernel_cache_rebase() const
Definition ChainedPointerAnalysis.hpp:408
ChainedPointerAnalysis & operator=(const ChainedPointerAnalysis &)=default
const dyld_chained_ptr_arm64e_bind24_t dyld_chained_ptr_arm64e_bind24() const
Definition ChainedPointerAnalysis.hpp:388
const dyld_chained_ptr_32_rebase_t dyld_chained_ptr_32_rebase() const
Definition ChainedPointerAnalysis.hpp:414
static size_t stride(DYLD_CHAINED_PTR_FORMAT fmt)
Definition ChainedPointerAnalysis.hpp:291
const dyld_chained_ptr_arm64e_auth_segmented_rebase_t dyld_chained_ptr_arm64e_auth_segmented_rebase() const
Definition ChainedPointerAnalysis.hpp:448
const dyld_chained_ptr_32_firmware_rebase_t dyld_chained_ptr_32_firmware_rebase() const
Definition ChainedPointerAnalysis.hpp:434
const dyld_chained_ptr_arm64e_bind_t dyld_chained_ptr_arm64e_bind() const
Definition ChainedPointerAnalysis.hpp:362
const dyld_chained_ptr_arm64e_auth_bind_t dyld_chained_ptr_arm64e_auth_bind() const
Definition ChainedPointerAnalysis.hpp:376
ChainedPointerAnalysis(ChainedPointerAnalysis &&) noexcept=default
static std::unique_ptr< ChainedPointerAnalysis > from_value(uint64_t value, size_t size)
Definition ChainedPointerAnalysis.hpp:286
const dyld_chained_ptr_32_cache_rebase_t dyld_chained_ptr_32_cache_rebase() const
Definition ChainedPointerAnalysis.hpp:427
static size_t ptr_size(DYLD_CHAINED_PTR_FORMAT fmt)
Definition ChainedPointerAnalysis.hpp:314
ChainedPointerAnalysis(const ChainedPointerAnalysis &)=default
const dyld_chained_ptr_arm64e_auth_bind24_t dyld_chained_ptr_arm64e_auth_bind24() const
Definition ChainedPointerAnalysis.hpp:395
uint64_t value() const
Definition ChainedPointerAnalysis.hpp:348
static uint64_t walk_chain(BinaryStream &stream, DYLD_CHAINED_PTR_FORMAT format, const std::function< int(uint64_t, const union_pointer_t &ptr)> &callback)
Wrapper that contains an Object (T) or an error.
Definition errors.hpp:79
Namespace related to the LIEF's Mach-O module.
Definition Abstract/Header.hpp:36
@ UNKNOWN
Definition MachO/enums.hpp:25
DYLD_CHAINED_PTR_FORMAT
Definition DyldChainedFormat.hpp:34
@ PTR_32
Definition DyldChainedFormat.hpp:40
@ PTR_32_FIRMWARE
Definition DyldChainedFormat.hpp:42
@ PTR_ARM64E_SHARED_CACHE
Definition DyldChainedFormat.hpp:59
@ PTR_ARM64E_USERLAND24
Definition DyldChainedFormat.hpp:57
@ PTR_32_CACHE
Definition DyldChainedFormat.hpp:41
@ PTR_64_KERNEL_CACHE
Definition DyldChainedFormat.hpp:49
@ PTR_ARM64E_KERNEL
Definition DyldChainedFormat.hpp:48
@ PTR_ARM64E_FIRMWARE
Definition DyldChainedFormat.hpp:53
@ PTR_X86_64_KERNEL_CACHE
Definition DyldChainedFormat.hpp:55
@ NONE
Definition DyldChainedFormat.hpp:35
@ PTR_ARM64E
Definition DyldChainedFormat.hpp:37
@ PTR_64
Definition DyldChainedFormat.hpp:39
@ PTR_64_OFFSET
Definition DyldChainedFormat.hpp:44
@ PTR_ARM64E_SEGMENTED
Definition DyldChainedFormat.hpp:61
@ PTR_ARM64E_USERLAND
Definition DyldChainedFormat.hpp:51
LIEF namespace.
Definition Abstract/Binary.hpp:41
uint32_t next
Definition ChainedPointerAnalysis.hpp:202
uint32_t ordinal
Definition ChainedPointerAnalysis.hpp:200
friend std::ostream & operator<<(std::ostream &os, const dyld_chained_ptr_32_bind_t &chain)
uint32_t addend
Definition ChainedPointerAnalysis.hpp:201
uint32_t bind
Definition ChainedPointerAnalysis.hpp:203
friend std::ostream & operator<<(std::ostream &os, const dyld_chained_ptr_32_cache_rebase_t &chain)
uint32_t next
Definition ChainedPointerAnalysis.hpp:214
uint32_t target
Definition ChainedPointerAnalysis.hpp:213
uint32_t target
Definition ChainedPointerAnalysis.hpp:225
friend std::ostream & operator<<(std::ostream &os, const dyld_chained_ptr_32_firmware_rebase_t &chain)
uint32_t next
Definition ChainedPointerAnalysis.hpp:226
friend std::ostream & operator<<(std::ostream &os, const dyld_chained_ptr_32_rebase_t &chain)
uint32_t bind
Definition ChainedPointerAnalysis.hpp:190
uint32_t target
Definition ChainedPointerAnalysis.hpp:188
uint32_t next
Definition ChainedPointerAnalysis.hpp:189
uint64_t addend
Definition ChainedPointerAnalysis.hpp:158
uint64_t ordinal
Definition ChainedPointerAnalysis.hpp:157
uint64_t reserved
Definition ChainedPointerAnalysis.hpp:159
friend std::ostream & operator<<(std::ostream &os, const dyld_chained_ptr_64_bind_t &chain)
uint64_t next
Definition ChainedPointerAnalysis.hpp:160
uint64_t bind
Definition ChainedPointerAnalysis.hpp:161
friend std::ostream & operator<<(std::ostream &os, const dyld_chained_ptr_64_kernel_cache_rebase_t &chain)
uint64_t cache_level
Definition ChainedPointerAnalysis.hpp:172
friend std::ostream & operator<<(std::ostream &os, const dyld_chained_ptr_64_rebase_t &chain)
uint64_t unpack_target() const
Definition ChainedPointerAnalysis.hpp:115
uint64_t reserved
Definition ChainedPointerAnalysis.hpp:107
uint64_t target
Definition ChainedPointerAnalysis.hpp:105
uint64_t high8
Definition ChainedPointerAnalysis.hpp:106
uint64_t next
Definition ChainedPointerAnalysis.hpp:108
uint64_t bind
Definition ChainedPointerAnalysis.hpp:109
uint64_t auth
Definition ChainedPointerAnalysis.hpp:146
uint64_t bind
Definition ChainedPointerAnalysis.hpp:145
uint64_t zero
Definition ChainedPointerAnalysis.hpp:140
friend std::ostream & operator<<(std::ostream &os, const dyld_chained_ptr_arm64e_auth_bind24_t &chain)
uint64_t ordinal
Definition ChainedPointerAnalysis.hpp:139
uint64_t diversity
Definition ChainedPointerAnalysis.hpp:141
uint64_t addr_div
Definition ChainedPointerAnalysis.hpp:142
uint64_t key
Definition ChainedPointerAnalysis.hpp:143
uint64_t next
Definition ChainedPointerAnalysis.hpp:144
uint64_t key
Definition ChainedPointerAnalysis.hpp:91
uint64_t auth
Definition ChainedPointerAnalysis.hpp:94
uint64_t addr_div
Definition ChainedPointerAnalysis.hpp:90
uint64_t zero
Definition ChainedPointerAnalysis.hpp:88
uint64_t diversity
Definition ChainedPointerAnalysis.hpp:89
friend std::ostream & operator<<(std::ostream &os, const dyld_chained_ptr_arm64e_auth_bind_t &chain)
uint64_t next
Definition ChainedPointerAnalysis.hpp:92
uint64_t ordinal
Definition ChainedPointerAnalysis.hpp:87
uint64_t bind
Definition ChainedPointerAnalysis.hpp:93
uint64_t diversity
Definition ChainedPointerAnalysis.hpp:71
friend std::ostream & operator<<(std::ostream &os, const dyld_chained_ptr_arm64e_auth_rebase_t &chain)
uint64_t addr_div
Definition ChainedPointerAnalysis.hpp:72
uint64_t target
Definition ChainedPointerAnalysis.hpp:70
friend std::ostream & operator<<(std::ostream &os, const dyld_chained_ptr_arm64e_auth_segmented_rebase_t &chain)
uint64_t auth
Definition ChainedPointerAnalysis.hpp:128
uint64_t next
Definition ChainedPointerAnalysis.hpp:126
uint64_t zero
Definition ChainedPointerAnalysis.hpp:124
uint64_t ordinal
Definition ChainedPointerAnalysis.hpp:123
uint64_t addend
Definition ChainedPointerAnalysis.hpp:125
friend std::ostream & operator<<(std::ostream &os, const dyld_chained_ptr_arm64e_bind24_t &chain)
uint64_t bind
Definition ChainedPointerAnalysis.hpp:127
uint64_t bind
Definition ChainedPointerAnalysis.hpp:59
uint64_t ordinal
Definition ChainedPointerAnalysis.hpp:55
friend std::ostream & operator<<(std::ostream &os, const dyld_chained_ptr_arm64e_bind_t &chain)
uint64_t next
Definition ChainedPointerAnalysis.hpp:58
uint64_t zero
Definition ChainedPointerAnalysis.hpp:56
uint64_t auth
Definition ChainedPointerAnalysis.hpp:60
uint64_t addend
Definition ChainedPointerAnalysis.hpp:57
friend std::ostream & operator<<(std::ostream &os, const dyld_chained_ptr_arm64e_rebase_t &chain)
uint64_t next
Definition ChainedPointerAnalysis.hpp:38
uint64_t target
Definition ChainedPointerAnalysis.hpp:36
uint64_t bind
Definition ChainedPointerAnalysis.hpp:39
uint64_t auth
Definition ChainedPointerAnalysis.hpp:40
uint64_t unpack_target() const
Definition ChainedPointerAnalysis.hpp:47
uint64_t high8
Definition ChainedPointerAnalysis.hpp:37
friend std::ostream & operator<<(std::ostream &os, const dyld_chained_ptr_arm64e_segmented_rebase_t &chain)
uint32_t target_seg_offset
Definition ChainedPointerAnalysis.hpp:237
uint32_t target_seg_index
Definition ChainedPointerAnalysis.hpp:238
Definition ChainedPointerAnalysis.hpp:454
std::variant< std::monostate, dyld_chained_ptr_arm64e_rebase_t, dyld_chained_ptr_arm64e_bind_t, dyld_chained_ptr_arm64e_auth_rebase_t, dyld_chained_ptr_arm64e_auth_bind_t, dyld_chained_ptr_64_rebase_t, dyld_chained_ptr_arm64e_bind24_t, dyld_chained_ptr_arm64e_auth_bind24_t, dyld_chained_ptr_64_bind_t, dyld_chained_ptr_64_kernel_cache_rebase_t, dyld_chained_ptr_32_rebase_t, dyld_chained_ptr_32_bind_t, dyld_chained_ptr_32_cache_rebase_t, dyld_chained_ptr_32_firmware_rebase_t, dyld_chained_ptr_arm64e_segmented_rebase_t, dyld_chained_ptr_arm64e_auth_segmented_rebase_t > content_t
Definition ChainedPointerAnalysis.hpp:455
PTR_TYPE type() const
The kind of chained pointer derived from the active alternative.
Definition ChainedPointerAnalysis.hpp:480
uint64_t raw
Definition ChainedPointerAnalysis.hpp:477
friend std::ostream & operator<<(std::ostream &os, const union_pointer_t &ptr)
bool is_bind() const
Definition ChainedPointerAnalysis.hpp:489
content_t content
Definition ChainedPointerAnalysis.hpp:476
#define LIEF_API
Definition visibility.h:45