LIEF: Library to Instrument Executable Formats Version 1.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 <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 uint64_t target : 43, high8 : 8, next : 11, bind : 1, auth : 1;
35
36 friend LIEF_API std::ostream&
37 operator<<(std::ostream& os,
39
40 uint64_t unpack_target() const {
41 return uint64_t(high8) | target;
42 }
43 };
44
45 // DYLD_CHAINED_PTR_FORMAT::PTR_ARM64E
47 uint64_t ordinal : 16, zero : 16, addend : 19, next : 11, bind : 1, auth : 1;
48
49 friend LIEF_API std::ostream&
50 operator<<(std::ostream& os, const dyld_chained_ptr_arm64e_bind_t& chain);
51 };
52
53 // DYLD_CHAINED_PTR_FORMAT::PTR_ARM64E
55 uint64_t target : 32, diversity : 16, addr_div : 1, key : 2, next : 11,
56 bind : 1, auth : 1;
57
58 friend LIEF_API std::ostream&
59 operator<<(std::ostream& os,
61 };
62
63 // DYLD_CHAINED_PTR_FORMAT::PTR_ARM64E
65 uint64_t ordinal : 16, zero : 16, diversity : 16, addr_div : 1, key : 2,
66 next : 11, bind : 1, auth : 1;
67
68 friend LIEF_API std::ostream&
69 operator<<(std::ostream& os,
71 };
72
73 // DYLD_CHAINED_PTR_FORMAT::PTR_64 & DYLD_CHAINED_PTR_FORMAT::PTR_64_OFFSET
75 uint64_t target : 36, high8 : 8, reserved : 7, next : 12, bind : 1;
76
77 friend LIEF_API std::ostream&
78 operator<<(std::ostream& os, const dyld_chained_ptr_64_rebase_t& chain);
79
80 uint64_t unpack_target() const {
81 return uint64_t(high8) | target;
82 }
83 };
84
85 // DYLD_CHAINED_PTR_FORMAT::PTR_ARM64E_USERLAND24
87 uint64_t ordinal : 24, zero : 8, addend : 19, next : 11, bind : 1, auth : 1;
88
89 friend LIEF_API std::ostream&
90 operator<<(std::ostream& os,
92 };
93
94 // DYLD_CHAINED_PTR_FORMAT::PTR_ARM64E_USERLAND24
96 uint64_t ordinal : 24, zero : 8, diversity : 16, addr_div : 1, key : 2,
97 next : 11, bind : 1, auth : 1;
98
99 friend LIEF_API std::ostream&
100 operator<<(std::ostream& os,
102 };
103
104 // DYLD_CHAINED_PTR_FORMAT::PTR_64
106 uint64_t ordinal : 24, addend : 8, reserved : 19, next : 12, bind : 1;
107
108 friend LIEF_API std::ostream&
109 operator<<(std::ostream& os, const dyld_chained_ptr_64_bind_t& chain);
110 };
111
112 // DYLD_CHAINED_PTR_FORMAT::PTR_64_KERNEL_CACHE
114 uint64_t target : 30, cache_level : 2, diversity : 16, addr_div : 1, key : 2,
115 next : 12, is_auth : 1;
116
117 friend LIEF_API std::ostream&
118 operator<<(std::ostream& os,
120 };
121
122 // DYLD_CHAINED_PTR_FORMAT::PTR_32
124 uint32_t target : 26, next : 5, bind : 1;
125
126 friend LIEF_API std::ostream&
127 operator<<(std::ostream& os, const dyld_chained_ptr_32_rebase_t& chain);
128 };
129
130 // DYLD_CHAINED_PTR_FORMAT::PTR_32
132 uint32_t ordinal : 20, addend : 6, next : 5, bind : 1;
133
134 friend LIEF_API std::ostream&
135 operator<<(std::ostream& os, const dyld_chained_ptr_32_bind_t& chain);
136 };
137
138 // DYLD_CHAINED_PTR_FORMAT::PTR_32_CACHE
140 uint32_t target : 30, next : 2;
141
142 friend LIEF_API std::ostream&
143 operator<<(std::ostream& os,
145 };
146
147 // DYLD_CHAINED_PTR_FORMAT::PTR_32_FIRMWARE
149 uint32_t target : 26, next : 6;
150
151 friend LIEF_API std::ostream&
152 operator<<(std::ostream& os,
154 };
155
156 // DYLD_CHAINED_PTR_ARM64E_SEGMENTED
159 uint32_t padding : 19, next : 12, auth : 1;
160
161 friend LIEF_API std::ostream&
162 operator<<(std::ostream& os,
164 };
165
166 // DYLD_CHAINED_PTR_ARM64E_SEGMENTED
169 uint32_t diversity : 16, addr_div : 1, key : 2, next : 12, auth : 1;
170 friend LIEF_API std::ostream&
171 operator<<(std::ostream& os,
173 };
174
175
176 enum class PTR_TYPE : uint64_t {
178 DYLD_CHAINED_PTR_ARM64E_REBASE,
179 DYLD_CHAINED_PTR_ARM64E_BIND,
180 DYLD_CHAINED_PTR_ARM64E_AUTH_REBASE,
181 DYLD_CHAINED_PTR_ARM64E_AUTH_BIND,
182 DYLD_CHAINED_PTR_64_REBASE,
183 DYLD_CHAINED_PTR_ARM64E_BIND24,
184 DYLD_CHAINED_PTR_ARM64E_AUTH_BIND24,
185 DYLD_CHAINED_PTR_64_BIND,
186 DYLD_CHAINED_PTR_64_KERNEL_CACHE_REBASE,
187 DYLD_CHAINED_PTR_32_REBASE,
188 DYLD_CHAINED_PTR_32_BIND,
189 DYLD_CHAINED_PTR_32_CACHE_REBASE,
190 DYLD_CHAINED_PTR_32_FIRMWARE_REBASE,
191 DYLD_CHAINED_PTR_ARM64E_SEGMENTED_REBASE,
192 DYLD_CHAINED_PTR_ARM64E_AUTH_SEGMENTED_REBASE,
193 };
194
195 static std::unique_ptr<ChainedPointerAnalysis> from_value(uint64_t value,
196 size_t size) {
197 return std::unique_ptr<ChainedPointerAnalysis>(
199 );
200 }
201
224
246
248 value_(value),
249 size_(size) {}
250
253
255 ChainedPointerAnalysis& operator=(ChainedPointerAnalysis&&) noexcept = default;
256
258
259 uint64_t value() const {
260 return value_;
261 }
262
263 size_t size() const {
264 return size_;
265 }
266
269 std::memcpy(&result, &value_, sizeof(result));
270 return result;
271 }
272
275 std::memcpy(&result, &value_, sizeof(result));
276 return result;
277 }
278
279 const dyld_chained_ptr_arm64e_auth_rebase_t
282 std::memcpy(&result, &value_, sizeof(result));
283 return result;
284 }
285
286 const dyld_chained_ptr_arm64e_auth_bind_t
289 std::memcpy(&result, &value_, sizeof(result));
290 return result;
291 }
292
295 std::memcpy(&result, &value_, sizeof(result));
296 return result;
297 }
298
301 std::memcpy(&result, &value_, sizeof(result));
302 return result;
303 }
304
305 const dyld_chained_ptr_arm64e_auth_bind24_t
308 std::memcpy(&result, &value_, sizeof(result));
309 return result;
310 }
311
314 std::memcpy(&result, &value_, sizeof(result));
315 return result;
316 }
317
318 const dyld_chained_ptr_64_kernel_cache_rebase_t
321 std::memcpy(&result, &value_, sizeof(result));
322 return result;
323 }
324
327 std::memcpy(&result, &value_, sizeof(result));
328 return result;
329 }
330
333 std::memcpy(&result, &value_, sizeof(result));
334 return result;
335 }
336
337 const dyld_chained_ptr_32_cache_rebase_t
340 std::memcpy(&result, &value_, sizeof(result));
341 return result;
342 }
343
344 const dyld_chained_ptr_32_firmware_rebase_t
347 std::memcpy(&result, &value_, sizeof(result));
348 return result;
349 }
350
351 const dyld_chained_ptr_arm64e_segmented_rebase_t
354 std::memcpy(&result, &value_, sizeof(result));
355 return result;
356 }
357
358 const dyld_chained_ptr_arm64e_auth_segmented_rebase_t
364
400
401 static_assert(sizeof(union_pointer_t) == 16);
402
404
405 static uint64_t walk_chain(
407 const std::function<int(uint64_t, const union_pointer_t& ptr)>& callback
408 );
409
410 private:
411 uint64_t value_ = 0;
412 size_t size_ = 0;
413};
414}
415}
416#endif
Class that is used to a read stream of data from different sources.
Definition BinaryStream.hpp:33
PTR_TYPE
Definition ChainedPointerAnalysis.hpp:176
@ UNKNOWN
Definition ChainedPointerAnalysis.hpp:177
union_pointer_t get_as(DYLD_CHAINED_PTR_FORMAT fmt) const
ChainedPointerAnalysis(uint64_t value, size_t size)
Definition ChainedPointerAnalysis.hpp:247
const dyld_chained_ptr_arm64e_segmented_rebase_t dyld_chained_ptr_arm64e_segmented_rebase() const
Definition ChainedPointerAnalysis.hpp:352
const dyld_chained_ptr_32_bind_t dyld_chained_ptr_32_bind() const
Definition ChainedPointerAnalysis.hpp:331
const dyld_chained_ptr_64_bind_t dyld_chained_ptr_64_bind() const
Definition ChainedPointerAnalysis.hpp:312
const dyld_chained_ptr_64_rebase_t dyld_chained_ptr_64_rebase() const
Definition ChainedPointerAnalysis.hpp:293
size_t size() const
Definition ChainedPointerAnalysis.hpp:263
const dyld_chained_ptr_arm64e_rebase_t dyld_chained_ptr_arm64e_rebase() const
Definition ChainedPointerAnalysis.hpp:267
const dyld_chained_ptr_arm64e_auth_rebase_t dyld_chained_ptr_arm64e_auth_rebase() const
Definition ChainedPointerAnalysis.hpp:280
const dyld_chained_ptr_64_kernel_cache_rebase_t dyld_chained_ptr_64_kernel_cache_rebase() const
Definition ChainedPointerAnalysis.hpp:319
ChainedPointerAnalysis & operator=(const ChainedPointerAnalysis &)=default
const dyld_chained_ptr_arm64e_bind24_t dyld_chained_ptr_arm64e_bind24() const
Definition ChainedPointerAnalysis.hpp:299
const dyld_chained_ptr_32_rebase_t dyld_chained_ptr_32_rebase() const
Definition ChainedPointerAnalysis.hpp:325
static size_t stride(DYLD_CHAINED_PTR_FORMAT fmt)
Definition ChainedPointerAnalysis.hpp:202
const dyld_chained_ptr_arm64e_auth_segmented_rebase_t dyld_chained_ptr_arm64e_auth_segmented_rebase() const
Definition ChainedPointerAnalysis.hpp:359
const dyld_chained_ptr_32_firmware_rebase_t dyld_chained_ptr_32_firmware_rebase() const
Definition ChainedPointerAnalysis.hpp:345
const dyld_chained_ptr_arm64e_bind_t dyld_chained_ptr_arm64e_bind() const
Definition ChainedPointerAnalysis.hpp:273
const dyld_chained_ptr_arm64e_auth_bind_t dyld_chained_ptr_arm64e_auth_bind() const
Definition ChainedPointerAnalysis.hpp:287
ChainedPointerAnalysis(ChainedPointerAnalysis &&) noexcept=default
static std::unique_ptr< ChainedPointerAnalysis > from_value(uint64_t value, size_t size)
Definition ChainedPointerAnalysis.hpp:195
const dyld_chained_ptr_32_cache_rebase_t dyld_chained_ptr_32_cache_rebase() const
Definition ChainedPointerAnalysis.hpp:338
static size_t ptr_size(DYLD_CHAINED_PTR_FORMAT fmt)
Definition ChainedPointerAnalysis.hpp:225
ChainedPointerAnalysis(const ChainedPointerAnalysis &)=default
const dyld_chained_ptr_arm64e_auth_bind24_t dyld_chained_ptr_arm64e_auth_bind24() const
Definition ChainedPointerAnalysis.hpp:306
uint64_t value() const
Definition ChainedPointerAnalysis.hpp:259
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:77
Namespace related to the LIEF's Mach-O module.
Definition Abstract/Header.hpp:36
@ UNKNOWN
Definition MachO/enums.hpp:24
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:46
@ PTR_ARM64E_USERLAND
Definition DyldChainedFormat.hpp:40
LIEF namespace.
Definition Abstract/Binary.hpp:40
uint32_t next
Definition ChainedPointerAnalysis.hpp:132
uint32_t ordinal
Definition ChainedPointerAnalysis.hpp:132
friend std::ostream & operator<<(std::ostream &os, const dyld_chained_ptr_32_bind_t &chain)
uint32_t addend
Definition ChainedPointerAnalysis.hpp:132
uint32_t bind
Definition ChainedPointerAnalysis.hpp:132
friend std::ostream & operator<<(std::ostream &os, const dyld_chained_ptr_32_cache_rebase_t &chain)
uint32_t next
Definition ChainedPointerAnalysis.hpp:140
uint32_t target
Definition ChainedPointerAnalysis.hpp:140
uint32_t target
Definition ChainedPointerAnalysis.hpp:149
friend std::ostream & operator<<(std::ostream &os, const dyld_chained_ptr_32_firmware_rebase_t &chain)
uint32_t next
Definition ChainedPointerAnalysis.hpp:149
friend std::ostream & operator<<(std::ostream &os, const dyld_chained_ptr_32_rebase_t &chain)
uint32_t bind
Definition ChainedPointerAnalysis.hpp:124
uint32_t target
Definition ChainedPointerAnalysis.hpp:124
uint32_t next
Definition ChainedPointerAnalysis.hpp:124
uint64_t addend
Definition ChainedPointerAnalysis.hpp:106
uint64_t ordinal
Definition ChainedPointerAnalysis.hpp:106
uint64_t reserved
Definition ChainedPointerAnalysis.hpp:106
friend std::ostream & operator<<(std::ostream &os, const dyld_chained_ptr_64_bind_t &chain)
uint64_t next
Definition ChainedPointerAnalysis.hpp:106
uint64_t bind
Definition ChainedPointerAnalysis.hpp:106
friend std::ostream & operator<<(std::ostream &os, const dyld_chained_ptr_64_kernel_cache_rebase_t &chain)
uint64_t cache_level
Definition ChainedPointerAnalysis.hpp:114
friend std::ostream & operator<<(std::ostream &os, const dyld_chained_ptr_64_rebase_t &chain)
uint64_t unpack_target() const
Definition ChainedPointerAnalysis.hpp:80
uint64_t reserved
Definition ChainedPointerAnalysis.hpp:75
uint64_t target
Definition ChainedPointerAnalysis.hpp:75
uint64_t high8
Definition ChainedPointerAnalysis.hpp:75
uint64_t next
Definition ChainedPointerAnalysis.hpp:75
uint64_t bind
Definition ChainedPointerAnalysis.hpp:75
friend std::ostream & operator<<(std::ostream &os, const dyld_chained_ptr_arm64e_auth_bind24_t &chain)
uint64_t ordinal
Definition ChainedPointerAnalysis.hpp:96
uint64_t diversity
Definition ChainedPointerAnalysis.hpp:96
uint64_t addr_div
Definition ChainedPointerAnalysis.hpp:96
uint64_t key
Definition ChainedPointerAnalysis.hpp:65
uint64_t auth
Definition ChainedPointerAnalysis.hpp:66
uint64_t addr_div
Definition ChainedPointerAnalysis.hpp:65
uint64_t zero
Definition ChainedPointerAnalysis.hpp:65
uint64_t diversity
Definition ChainedPointerAnalysis.hpp:65
friend std::ostream & operator<<(std::ostream &os, const dyld_chained_ptr_arm64e_auth_bind_t &chain)
uint64_t next
Definition ChainedPointerAnalysis.hpp:66
uint64_t ordinal
Definition ChainedPointerAnalysis.hpp:65
uint64_t bind
Definition ChainedPointerAnalysis.hpp:66
uint64_t diversity
Definition ChainedPointerAnalysis.hpp:55
friend std::ostream & operator<<(std::ostream &os, const dyld_chained_ptr_arm64e_auth_rebase_t &chain)
uint64_t addr_div
Definition ChainedPointerAnalysis.hpp:55
uint64_t target
Definition ChainedPointerAnalysis.hpp:55
friend std::ostream & operator<<(std::ostream &os, const dyld_chained_ptr_arm64e_auth_segmented_rebase_t &chain)
uint64_t auth
Definition ChainedPointerAnalysis.hpp:87
uint64_t next
Definition ChainedPointerAnalysis.hpp:87
uint64_t zero
Definition ChainedPointerAnalysis.hpp:87
uint64_t ordinal
Definition ChainedPointerAnalysis.hpp:87
uint64_t addend
Definition ChainedPointerAnalysis.hpp:87
friend std::ostream & operator<<(std::ostream &os, const dyld_chained_ptr_arm64e_bind24_t &chain)
uint64_t bind
Definition ChainedPointerAnalysis.hpp:87
uint64_t bind
Definition ChainedPointerAnalysis.hpp:47
uint64_t ordinal
Definition ChainedPointerAnalysis.hpp:47
friend std::ostream & operator<<(std::ostream &os, const dyld_chained_ptr_arm64e_bind_t &chain)
uint64_t next
Definition ChainedPointerAnalysis.hpp:47
uint64_t zero
Definition ChainedPointerAnalysis.hpp:47
uint64_t auth
Definition ChainedPointerAnalysis.hpp:47
uint64_t addend
Definition ChainedPointerAnalysis.hpp:47
friend std::ostream & operator<<(std::ostream &os, const dyld_chained_ptr_arm64e_rebase_t &chain)
uint64_t next
Definition ChainedPointerAnalysis.hpp:34
uint64_t target
Definition ChainedPointerAnalysis.hpp:34
uint64_t bind
Definition ChainedPointerAnalysis.hpp:34
uint64_t auth
Definition ChainedPointerAnalysis.hpp:34
uint64_t unpack_target() const
Definition ChainedPointerAnalysis.hpp:40
uint64_t high8
Definition ChainedPointerAnalysis.hpp:34
friend std::ostream & operator<<(std::ostream &os, const dyld_chained_ptr_arm64e_segmented_rebase_t &chain)
uint32_t target_seg_offset
Definition ChainedPointerAnalysis.hpp:158
uint32_t target_seg_index
Definition ChainedPointerAnalysis.hpp:158
Definition ChainedPointerAnalysis.hpp:365
dyld_chained_ptr_arm64e_bind24_t arm64e_bind24
Definition ChainedPointerAnalysis.hpp:373
dyld_chained_ptr_64_bind_t ptr_64_bind
Definition ChainedPointerAnalysis.hpp:375
dyld_chained_ptr_32_bind_t ptr_32_bind
Definition ChainedPointerAnalysis.hpp:378
dyld_chained_ptr_32_firmware_rebase_t ptr_32_firmware_rebase
Definition ChainedPointerAnalysis.hpp:380
dyld_chained_ptr_arm64e_auth_rebase_t arm64e_auth_rebase
Definition ChainedPointerAnalysis.hpp:370
PTR_TYPE type
Definition ChainedPointerAnalysis.hpp:366
dyld_chained_ptr_arm64e_rebase_t arm64e_rebase
Definition ChainedPointerAnalysis.hpp:368
uint64_t raw
Definition ChainedPointerAnalysis.hpp:385
dyld_chained_ptr_arm64e_segmented_rebase_t ptr_arm64e_segmented_rebase
Definition ChainedPointerAnalysis.hpp:382
dyld_chained_ptr_arm64e_auth_bind_t arm64e_auth_bind
Definition ChainedPointerAnalysis.hpp:371
dyld_chained_ptr_arm64e_auth_bind24_t arm64e_auth_bind24
Definition ChainedPointerAnalysis.hpp:374
dyld_chained_ptr_64_kernel_cache_rebase_t ptr_64_kernel_cache_rebase
Definition ChainedPointerAnalysis.hpp:376
dyld_chained_ptr_arm64e_bind_t arm64e_bind
Definition ChainedPointerAnalysis.hpp:369
dyld_chained_ptr_arm64e_auth_segmented_rebase_t ptr_arm64e_auth_segmented_rebase
Definition ChainedPointerAnalysis.hpp:384
dyld_chained_ptr_32_rebase_t ptr_32_rebase
Definition ChainedPointerAnalysis.hpp:377
dyld_chained_ptr_64_rebase_t ptr_64_rebase
Definition ChainedPointerAnalysis.hpp:372
friend std::ostream & operator<<(std::ostream &os, const union_pointer_t &ptr)
bool is_bind() const
Definition ChainedPointerAnalysis.hpp:392
dyld_chained_ptr_32_cache_rebase_t ptr_32_cache_rebase
Definition ChainedPointerAnalysis.hpp:379
#define LIEF_API
Definition visibility.h:43