LIEF: Library to Instrument Executable Formats Version 0.17.0
Loading...
Searching...
No Matches
ChainedPointerAnalysis.hpp
Go to the documentation of this file.
1/* Copyright 2017 - 2025 R. Thomas
2 * Copyright 2017 - 2025 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 <memory>
19#include <ostream>
20#include <functional>
21#include <cstring>
22
24#include "LIEF/errors.hpp"
25#include "LIEF/visibility.h"
26
27namespace LIEF {
28class BinaryStream;
29namespace MachO {
31 public:
32 // DYLD_CHAINED_PTR_FORMAT::PTR_ARM64E
34 {
35 uint64_t target : 43,
36 high8 : 8,
37 next : 11,
38 bind : 1,
39 auth : 1;
40
41 friend LIEF_API
42 std::ostream& operator<<(std::ostream& os, const dyld_chained_ptr_arm64e_rebase_t& chain);
43
44 uint64_t unpack_target() const {
45 return uint64_t(high8) | target;
46 }
47 };
48
49 // DYLD_CHAINED_PTR_FORMAT::PTR_ARM64E
51 {
52 uint64_t ordinal : 16,
53 zero : 16,
54 addend : 19,
55 next : 11,
56 bind : 1,
57 auth : 1;
58
59 friend LIEF_API
60 std::ostream& operator<<(std::ostream& os, const dyld_chained_ptr_arm64e_bind_t& chain);
61 };
62
63 // DYLD_CHAINED_PTR_FORMAT::PTR_ARM64E
65 {
66 uint64_t target : 32,
69 key : 2,
70 next : 11,
71 bind : 1,
72 auth : 1;
73
74 friend LIEF_API
75 std::ostream& operator<<(std::ostream& os, const dyld_chained_ptr_arm64e_auth_rebase_t& chain);
76 };
77
78 // DYLD_CHAINED_PTR_FORMAT::PTR_ARM64E
80 {
81 uint64_t ordinal : 16,
82 zero : 16,
85 key : 2,
86 next : 11,
87 bind : 1,
88 auth : 1;
89
90 friend LIEF_API
91 std::ostream& operator<<(std::ostream& os, const dyld_chained_ptr_arm64e_auth_bind_t& chain);
92 };
93
94 // DYLD_CHAINED_PTR_FORMAT::PTR_64 & DYLD_CHAINED_PTR_FORMAT::PTR_64_OFFSET
96 {
97 uint64_t target : 36,
98 high8 : 8,
100 next : 12,
101 bind : 1;
102
103 friend LIEF_API
104 std::ostream& operator<<(std::ostream& os, const dyld_chained_ptr_64_rebase_t& chain);
105
106 uint64_t unpack_target() const {
107 return uint64_t(high8) | target;
108 }
109 };
110
111 // DYLD_CHAINED_PTR_FORMAT::PTR_ARM64E_USERLAND24
113 {
114 uint64_t ordinal : 24,
115 zero : 8,
116 addend : 19,
117 next : 11,
118 bind : 1,
119 auth : 1;
120
121 friend LIEF_API
122 std::ostream& operator<<(std::ostream& os, const dyld_chained_ptr_arm64e_bind24_t& chain);
123 };
124
125 // DYLD_CHAINED_PTR_FORMAT::PTR_ARM64E_USERLAND24
127 {
128 uint64_t ordinal : 24,
129 zero : 8,
132 key : 2,
133 next : 11,
134 bind : 1,
135 auth : 1;
136
137 friend LIEF_API
138 std::ostream& operator<<(std::ostream& os, const dyld_chained_ptr_arm64e_auth_bind24_t& chain);
139 };
140
141 // DYLD_CHAINED_PTR_FORMAT::PTR_64
143 {
144 uint64_t ordinal : 24,
147 next : 12,
148 bind : 1;
149
150 friend LIEF_API
151 std::ostream& operator<<(std::ostream& os, const dyld_chained_ptr_64_bind_t& chain);
152 };
153
154 // DYLD_CHAINED_PTR_FORMAT::PTR_64_KERNEL_CACHE
156 {
157 uint64_t target : 30,
161 key : 2,
162 next : 12,
164
165 friend LIEF_API
166 std::ostream& operator<<(std::ostream& os, const dyld_chained_ptr_64_kernel_cache_rebase_t& chain);
167 };
168
169 // DYLD_CHAINED_PTR_FORMAT::PTR_32
171 {
172 uint32_t target : 26,
173 next : 5,
174 bind : 1;
175
176 friend LIEF_API
177 std::ostream& operator<<(std::ostream& os, const dyld_chained_ptr_32_rebase_t& chain);
178 };
179
180 // DYLD_CHAINED_PTR_FORMAT::PTR_32
182 {
183 uint32_t ordinal : 20,
185 next : 5,
186 bind : 1;
187
188 friend LIEF_API
189 std::ostream& operator<<(std::ostream& os, const dyld_chained_ptr_32_bind_t& chain);
190 };
191
192 // DYLD_CHAINED_PTR_FORMAT::PTR_32_CACHE
194 {
195 uint32_t target : 30,
196 next : 2;
197
198 friend LIEF_API
199 std::ostream& operator<<(std::ostream& os, const dyld_chained_ptr_32_cache_rebase_t& chain);
200 };
201
202 // DYLD_CHAINED_PTR_FORMAT::PTR_32_FIRMWARE
204 {
205 uint32_t target : 26,
206 next : 6;
207
208 friend LIEF_API
209 std::ostream& operator<<(std::ostream& os, const dyld_chained_ptr_32_firmware_rebase_t& chain);
210 };
211
212 // DYLD_CHAINED_PTR_ARM64E_SEGMENTED
214 {
215 uint32_t target_seg_offset : 28,
217 uint32_t padding : 19,
218 next : 12,
219 auth : 1;
220
221 friend LIEF_API
222 std::ostream& operator<<(std::ostream& os, const dyld_chained_ptr_arm64e_segmented_rebase_t& chain);
223 };
224
225 // DYLD_CHAINED_PTR_ARM64E_SEGMENTED
227 {
228 uint32_t target_seg_offset : 28,
230 uint32_t diversity : 16,
232 key : 2,
233 next : 12,
234 auth : 1;
235 friend LIEF_API
236 std::ostream& operator<<(std::ostream& os, const dyld_chained_ptr_arm64e_auth_segmented_rebase_t& chain);
237 };
238
239
258
259 static std::unique_ptr<ChainedPointerAnalysis> from_value(uint64_t value,
260 size_t size)
261 {
262 return std::unique_ptr<ChainedPointerAnalysis>(
264 }
265
292
317
319 value_(value),
320 size_(size)
321 {}
322
325
327 ChainedPointerAnalysis& operator=(ChainedPointerAnalysis&&) noexcept = default;
328
330
331 uint64_t value() const {
332 return value_;
333 }
334
335 size_t size() const {
336 return size_;
337 }
338
341 std::memcpy(&result, &value_, sizeof(result));
342 return result;
343 }
344
347 std::memcpy(&result, &value_, sizeof(result));
348 return result;
349 }
350
356
362
365 std::memcpy(&result, &value_, sizeof(result));
366 return result;
367 }
368
371 std::memcpy(&result, &value_, sizeof(result));
372 return result;
373 }
374
380
383 std::memcpy(&result, &value_, sizeof(result));
384 return result;
385 }
386
392
395 std::memcpy(&result, &value_, sizeof(result));
396 return result;
397 }
398
401 std::memcpy(&result, &value_, sizeof(result));
402 return result;
403 }
404
410
416
422
428
463
464 static_assert(sizeof(union_pointer_t) == 16);
465
467
468 static uint64_t walk_chain(
470 const std::function<int(uint64_t, const union_pointer_t& ptr)>& callback);
471
472 private:
473 uint64_t value_ = 0;
474 size_t size_ = 0;
475};
476}
477}
478#endif
Class that is used to a read stream of data from different sources.
Definition BinaryStream.hpp:33
PTR_TYPE
Definition ChainedPointerAnalysis.hpp:240
@ DYLD_CHAINED_PTR_ARM64E_SEGMENTED_REBASE
Definition ChainedPointerAnalysis.hpp:255
@ DYLD_CHAINED_PTR_64_KERNEL_CACHE_REBASE
Definition ChainedPointerAnalysis.hpp:250
@ DYLD_CHAINED_PTR_32_BIND
Definition ChainedPointerAnalysis.hpp:252
@ DYLD_CHAINED_PTR_32_REBASE
Definition ChainedPointerAnalysis.hpp:251
@ UNKNOWN
Definition ChainedPointerAnalysis.hpp:241
@ DYLD_CHAINED_PTR_64_BIND
Definition ChainedPointerAnalysis.hpp:249
@ DYLD_CHAINED_PTR_64_REBASE
Definition ChainedPointerAnalysis.hpp:246
@ DYLD_CHAINED_PTR_ARM64E_AUTH_BIND24
Definition ChainedPointerAnalysis.hpp:248
@ DYLD_CHAINED_PTR_ARM64E_AUTH_REBASE
Definition ChainedPointerAnalysis.hpp:244
@ DYLD_CHAINED_PTR_ARM64E_AUTH_SEGMENTED_REBASE
Definition ChainedPointerAnalysis.hpp:256
@ DYLD_CHAINED_PTR_32_CACHE_REBASE
Definition ChainedPointerAnalysis.hpp:253
@ DYLD_CHAINED_PTR_ARM64E_AUTH_BIND
Definition ChainedPointerAnalysis.hpp:245
@ DYLD_CHAINED_PTR_ARM64E_BIND
Definition ChainedPointerAnalysis.hpp:243
@ DYLD_CHAINED_PTR_32_FIRMWARE_REBASE
Definition ChainedPointerAnalysis.hpp:254
@ DYLD_CHAINED_PTR_ARM64E_BIND24
Definition ChainedPointerAnalysis.hpp:247
@ DYLD_CHAINED_PTR_ARM64E_REBASE
Definition ChainedPointerAnalysis.hpp:242
union_pointer_t get_as(DYLD_CHAINED_PTR_FORMAT fmt) const
ChainedPointerAnalysis(uint64_t value, size_t size)
Definition ChainedPointerAnalysis.hpp:318
const dyld_chained_ptr_arm64e_segmented_rebase_t dyld_chained_ptr_arm64e_segmented_rebase() const
Definition ChainedPointerAnalysis.hpp:417
const dyld_chained_ptr_32_bind_t dyld_chained_ptr_32_bind() const
Definition ChainedPointerAnalysis.hpp:399
const dyld_chained_ptr_64_bind_t dyld_chained_ptr_64_bind() const
Definition ChainedPointerAnalysis.hpp:381
const dyld_chained_ptr_64_rebase_t dyld_chained_ptr_64_rebase() const
Definition ChainedPointerAnalysis.hpp:363
size_t size() const
Definition ChainedPointerAnalysis.hpp:335
const dyld_chained_ptr_arm64e_rebase_t dyld_chained_ptr_arm64e_rebase() const
Definition ChainedPointerAnalysis.hpp:339
const dyld_chained_ptr_arm64e_auth_rebase_t dyld_chained_ptr_arm64e_auth_rebase() const
Definition ChainedPointerAnalysis.hpp:351
const dyld_chained_ptr_64_kernel_cache_rebase_t dyld_chained_ptr_64_kernel_cache_rebase() const
Definition ChainedPointerAnalysis.hpp:387
ChainedPointerAnalysis & operator=(const ChainedPointerAnalysis &)=default
const dyld_chained_ptr_arm64e_bind24_t dyld_chained_ptr_arm64e_bind24() const
Definition ChainedPointerAnalysis.hpp:369
const dyld_chained_ptr_32_rebase_t dyld_chained_ptr_32_rebase() const
Definition ChainedPointerAnalysis.hpp:393
static size_t stride(DYLD_CHAINED_PTR_FORMAT fmt)
Definition ChainedPointerAnalysis.hpp:266
const dyld_chained_ptr_arm64e_auth_segmented_rebase_t dyld_chained_ptr_arm64e_auth_segmented_rebase() const
Definition ChainedPointerAnalysis.hpp:423
const dyld_chained_ptr_32_firmware_rebase_t dyld_chained_ptr_32_firmware_rebase() const
Definition ChainedPointerAnalysis.hpp:411
const dyld_chained_ptr_arm64e_bind_t dyld_chained_ptr_arm64e_bind() const
Definition ChainedPointerAnalysis.hpp:345
const dyld_chained_ptr_arm64e_auth_bind_t dyld_chained_ptr_arm64e_auth_bind() const
Definition ChainedPointerAnalysis.hpp:357
ChainedPointerAnalysis(ChainedPointerAnalysis &&) noexcept=default
static std::unique_ptr< ChainedPointerAnalysis > from_value(uint64_t value, size_t size)
Definition ChainedPointerAnalysis.hpp:259
const dyld_chained_ptr_32_cache_rebase_t dyld_chained_ptr_32_cache_rebase() const
Definition ChainedPointerAnalysis.hpp:405
static size_t ptr_size(DYLD_CHAINED_PTR_FORMAT fmt)
Definition ChainedPointerAnalysis.hpp:293
ChainedPointerAnalysis(const ChainedPointerAnalysis &)=default
const dyld_chained_ptr_arm64e_auth_bind24_t dyld_chained_ptr_arm64e_auth_bind24() const
Definition ChainedPointerAnalysis.hpp:375
uint64_t value() const
Definition ChainedPointerAnalysis.hpp:331
static uint64_t walk_chain(BinaryStream &stream, DYLD_CHAINED_PTR_FORMAT format, const std::function< int(uint64_t, const union_pointer_t &ptr)> &callback)
Namespace related to the LIEF's Mach-O module.
Definition Abstract/Header.hpp:36
DYLD_CHAINED_PTR_FORMAT
Definition DyldChainedFormat.hpp:29
@ PTR_32
Definition DyldChainedFormat.hpp:33
@ PTR_32_FIRMWARE
Definition DyldChainedFormat.hpp:35
@ PTR_ARM64E_SHARED_CACHE
Definition DyldChainedFormat.hpp:44
@ PTR_ARM64E_USERLAND24
Definition DyldChainedFormat.hpp:43
@ PTR_32_CACHE
Definition DyldChainedFormat.hpp:34
@ PTR_64_KERNEL_CACHE
Definition DyldChainedFormat.hpp:39
@ PTR_ARM64E_KERNEL
Definition DyldChainedFormat.hpp:38
@ PTR_ARM64E_FIRMWARE
Definition DyldChainedFormat.hpp:41
@ PTR_X86_64_KERNEL_CACHE
Definition DyldChainedFormat.hpp:42
@ NONE
Definition DyldChainedFormat.hpp:30
@ PTR_ARM64E
Definition DyldChainedFormat.hpp:31
@ PTR_64
Definition DyldChainedFormat.hpp:32
@ PTR_64_OFFSET
Definition DyldChainedFormat.hpp:36
@ PTR_ARM64E_SEGMENTED
Definition DyldChainedFormat.hpp:45
@ PTR_ARM64E_USERLAND
Definition DyldChainedFormat.hpp:40
LIEF namespace.
Definition Abstract/Binary.hpp:40
tl::expected< T, lief_errors > result
Wrapper that contains an Object (T) or an error.
Definition errors.hpp:75
uint32_t next
Definition ChainedPointerAnalysis.hpp:185
uint32_t ordinal
Definition ChainedPointerAnalysis.hpp:183
friend std::ostream & operator<<(std::ostream &os, const dyld_chained_ptr_32_bind_t &chain)
uint32_t addend
Definition ChainedPointerAnalysis.hpp:184
uint32_t bind
Definition ChainedPointerAnalysis.hpp:186
friend std::ostream & operator<<(std::ostream &os, const dyld_chained_ptr_32_cache_rebase_t &chain)
uint32_t next
Definition ChainedPointerAnalysis.hpp:196
uint32_t target
Definition ChainedPointerAnalysis.hpp:195
uint32_t target
Definition ChainedPointerAnalysis.hpp:205
friend std::ostream & operator<<(std::ostream &os, const dyld_chained_ptr_32_firmware_rebase_t &chain)
uint32_t next
Definition ChainedPointerAnalysis.hpp:206
friend std::ostream & operator<<(std::ostream &os, const dyld_chained_ptr_32_rebase_t &chain)
uint32_t bind
Definition ChainedPointerAnalysis.hpp:174
uint32_t target
Definition ChainedPointerAnalysis.hpp:172
uint32_t next
Definition ChainedPointerAnalysis.hpp:173
uint64_t addend
Definition ChainedPointerAnalysis.hpp:145
uint64_t ordinal
Definition ChainedPointerAnalysis.hpp:144
uint64_t reserved
Definition ChainedPointerAnalysis.hpp:146
friend std::ostream & operator<<(std::ostream &os, const dyld_chained_ptr_64_bind_t &chain)
uint64_t next
Definition ChainedPointerAnalysis.hpp:147
uint64_t bind
Definition ChainedPointerAnalysis.hpp:148
friend std::ostream & operator<<(std::ostream &os, const dyld_chained_ptr_64_kernel_cache_rebase_t &chain)
uint64_t cache_level
Definition ChainedPointerAnalysis.hpp:158
friend std::ostream & operator<<(std::ostream &os, const dyld_chained_ptr_64_rebase_t &chain)
uint64_t unpack_target() const
Definition ChainedPointerAnalysis.hpp:106
uint64_t reserved
Definition ChainedPointerAnalysis.hpp:99
uint64_t target
Definition ChainedPointerAnalysis.hpp:97
uint64_t high8
Definition ChainedPointerAnalysis.hpp:98
uint64_t next
Definition ChainedPointerAnalysis.hpp:100
uint64_t bind
Definition ChainedPointerAnalysis.hpp:101
uint64_t auth
Definition ChainedPointerAnalysis.hpp:135
uint64_t bind
Definition ChainedPointerAnalysis.hpp:134
uint64_t zero
Definition ChainedPointerAnalysis.hpp:129
friend std::ostream & operator<<(std::ostream &os, const dyld_chained_ptr_arm64e_auth_bind24_t &chain)
uint64_t ordinal
Definition ChainedPointerAnalysis.hpp:128
uint64_t diversity
Definition ChainedPointerAnalysis.hpp:130
uint64_t addr_div
Definition ChainedPointerAnalysis.hpp:131
uint64_t key
Definition ChainedPointerAnalysis.hpp:132
uint64_t next
Definition ChainedPointerAnalysis.hpp:133
uint64_t key
Definition ChainedPointerAnalysis.hpp:85
uint64_t auth
Definition ChainedPointerAnalysis.hpp:88
uint64_t addr_div
Definition ChainedPointerAnalysis.hpp:84
uint64_t zero
Definition ChainedPointerAnalysis.hpp:82
uint64_t diversity
Definition ChainedPointerAnalysis.hpp:83
friend std::ostream & operator<<(std::ostream &os, const dyld_chained_ptr_arm64e_auth_bind_t &chain)
uint64_t next
Definition ChainedPointerAnalysis.hpp:86
uint64_t ordinal
Definition ChainedPointerAnalysis.hpp:81
uint64_t bind
Definition ChainedPointerAnalysis.hpp:87
uint64_t diversity
Definition ChainedPointerAnalysis.hpp:67
friend std::ostream & operator<<(std::ostream &os, const dyld_chained_ptr_arm64e_auth_rebase_t &chain)
uint64_t addr_div
Definition ChainedPointerAnalysis.hpp:68
uint64_t target
Definition ChainedPointerAnalysis.hpp:66
friend std::ostream & operator<<(std::ostream &os, const dyld_chained_ptr_arm64e_auth_segmented_rebase_t &chain)
uint64_t auth
Definition ChainedPointerAnalysis.hpp:119
uint64_t next
Definition ChainedPointerAnalysis.hpp:117
uint64_t zero
Definition ChainedPointerAnalysis.hpp:115
uint64_t ordinal
Definition ChainedPointerAnalysis.hpp:114
uint64_t addend
Definition ChainedPointerAnalysis.hpp:116
friend std::ostream & operator<<(std::ostream &os, const dyld_chained_ptr_arm64e_bind24_t &chain)
uint64_t bind
Definition ChainedPointerAnalysis.hpp:118
uint64_t bind
Definition ChainedPointerAnalysis.hpp:56
uint64_t ordinal
Definition ChainedPointerAnalysis.hpp:52
friend std::ostream & operator<<(std::ostream &os, const dyld_chained_ptr_arm64e_bind_t &chain)
uint64_t next
Definition ChainedPointerAnalysis.hpp:55
uint64_t zero
Definition ChainedPointerAnalysis.hpp:53
uint64_t auth
Definition ChainedPointerAnalysis.hpp:57
uint64_t addend
Definition ChainedPointerAnalysis.hpp:54
friend std::ostream & operator<<(std::ostream &os, const dyld_chained_ptr_arm64e_rebase_t &chain)
uint64_t next
Definition ChainedPointerAnalysis.hpp:37
uint64_t target
Definition ChainedPointerAnalysis.hpp:35
uint64_t bind
Definition ChainedPointerAnalysis.hpp:38
uint64_t auth
Definition ChainedPointerAnalysis.hpp:39
uint64_t unpack_target() const
Definition ChainedPointerAnalysis.hpp:44
uint64_t high8
Definition ChainedPointerAnalysis.hpp:36
friend std::ostream & operator<<(std::ostream &os, const dyld_chained_ptr_arm64e_segmented_rebase_t &chain)
uint32_t target_seg_offset
Definition ChainedPointerAnalysis.hpp:215
uint32_t target_seg_index
Definition ChainedPointerAnalysis.hpp:216
Definition ChainedPointerAnalysis.hpp:429
dyld_chained_ptr_arm64e_bind24_t arm64e_bind24
Definition ChainedPointerAnalysis.hpp:437
dyld_chained_ptr_64_bind_t ptr_64_bind
Definition ChainedPointerAnalysis.hpp:439
dyld_chained_ptr_32_bind_t ptr_32_bind
Definition ChainedPointerAnalysis.hpp:442
dyld_chained_ptr_32_firmware_rebase_t ptr_32_firmware_rebase
Definition ChainedPointerAnalysis.hpp:444
dyld_chained_ptr_arm64e_auth_rebase_t arm64e_auth_rebase
Definition ChainedPointerAnalysis.hpp:434
PTR_TYPE type
Definition ChainedPointerAnalysis.hpp:430
dyld_chained_ptr_arm64e_rebase_t arm64e_rebase
Definition ChainedPointerAnalysis.hpp:432
uint64_t raw
Definition ChainedPointerAnalysis.hpp:448
dyld_chained_ptr_arm64e_segmented_rebase_t ptr_arm64e_segmented_rebase
Definition ChainedPointerAnalysis.hpp:446
dyld_chained_ptr_arm64e_auth_bind_t arm64e_auth_bind
Definition ChainedPointerAnalysis.hpp:435
dyld_chained_ptr_arm64e_auth_bind24_t arm64e_auth_bind24
Definition ChainedPointerAnalysis.hpp:438
dyld_chained_ptr_64_kernel_cache_rebase_t ptr_64_kernel_cache_rebase
Definition ChainedPointerAnalysis.hpp:440
dyld_chained_ptr_arm64e_bind_t arm64e_bind
Definition ChainedPointerAnalysis.hpp:433
dyld_chained_ptr_arm64e_auth_segmented_rebase_t ptr_arm64e_auth_segmented_rebase
Definition ChainedPointerAnalysis.hpp:447
dyld_chained_ptr_32_rebase_t ptr_32_rebase
Definition ChainedPointerAnalysis.hpp:441
dyld_chained_ptr_64_rebase_t ptr_64_rebase
Definition ChainedPointerAnalysis.hpp:436
friend std::ostream & operator<<(std::ostream &os, const union_pointer_t &ptr)
bool is_bind() const
Definition ChainedPointerAnalysis.hpp:455
dyld_chained_ptr_32_cache_rebase_t ptr_32_cache_rebase
Definition ChainedPointerAnalysis.hpp:443
#define LIEF_API
Definition visibility.h:41