LIEF: Library to Instrument Executable Formats Version 0.17.0
Loading...
Searching...
No Matches
DyldInfo.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_DYLD_INFO_COMMAND_H
17#define LIEF_MACHO_DYLD_INFO_COMMAND_H
18#include <string>
19#include <set>
20#include <vector>
21#include <ostream>
22#include <memory>
23
24#include "LIEF/visibility.h"
25#include "LIEF/span.hpp"
26
29#include "LIEF/iterators.hpp"
30
31namespace LIEF {
32class vector_iostream;
33class BinaryStream;
34namespace MachO {
35
36class Binary;
37class BinaryParser;
39class Builder;
40class DyldBindingInfo;
41class ExportInfo;
42class LinkEdit;
43class RelocationDyld;
44
45namespace details {
46struct dyld_info_command;
47}
48
51
52 friend class BinaryParser;
53 friend class Binary;
54 friend class Builder;
55 friend class LinkEdit;
56 friend class BindingInfoIterator;
57
58 public:
60 using info_t = std::pair<uint32_t, uint32_t>;
61
63 using binding_info_t = std::vector<std::unique_ptr<DyldBindingInfo>>;
64
67
70
72 using export_info_t = std::vector<std::unique_ptr<ExportInfo>>;
73
76
79
85
86 enum class REBASE_TYPE: uint64_t {
87 POINTER = 1u,
90 THREADED = 102u,
91 };
92
104
125
126 enum class BIND_SUBOPCODE_THREADED: uint8_t {
128 APPLY = 0x01u,
129 };
130
135
136 static constexpr auto OPCODE_MASK = uint32_t(0xF0);
137 static constexpr auto IMMEDIATE_MASK = uint32_t(0x0F);
138
140 DyldInfo(const details::dyld_info_command& dyld_info_cmd);
141
143 DyldInfo(const DyldInfo& copy);
144
145 void swap(DyldInfo& other) noexcept;
146
147 std::unique_ptr<LoadCommand> clone() const override {
148 return std::unique_ptr<DyldInfo>(new DyldInfo(*this));
149 }
150
151 ~DyldInfo() override;
152
166 const info_t& rebase() const {
167 return rebase_;
168 }
169
172 return rebase_opcodes_;
173 }
175 return rebase_opcodes_;
176 }
177
180
182 std::string show_rebases_opcodes() const;
183
198 const info_t& bind() const {
199 return bind_;
200 }
201
204 return bind_opcodes_;
205 }
207 return bind_opcodes_;
208 }
209
212
214 std::string show_bind_opcodes() const;
215
233 const info_t& weak_bind() const {
234 return weak_bind_;
235 }
236
239 return weak_bind_opcodes_;
240 }
242 return weak_bind_opcodes_;
243 }
244
247
249 std::string show_weak_bind_opcodes() const;
250
265 const info_t& lazy_bind() const {
266 return lazy_bind_;
267 }
268
271 return lazy_bind_opcodes_;
272 }
274 return lazy_bind_opcodes_;
275 }
276
279
281 std::string show_lazy_bind_opcodes() const;
282
285 return binding_info_;
286 }
287
289 return binding_info_;
290 }
291
318 const info_t& export_info() const {
319 return export_;
320 }
321
324 return export_info_;
325 }
327 return export_info_;
328 }
329
332 return export_trie_;
333 }
335 return export_trie_;
336 }
337
340
342 std::string show_export_trie() const;
343
344 void rebase(const info_t& info) {
345 rebase_ = info;
346 }
347 void bind(const info_t& info) {
348 bind_ = info;
349 }
350 void weak_bind(const info_t& info) {
351 weak_bind_ = info;
352 }
353 void lazy_bind(const info_t& info) {
354 lazy_bind_ = info;
355 }
356 void export_info(const info_t& info) {
357 export_ = info;
358 }
359
360 void set_rebase_offset(uint32_t offset) {
361 rebase_ = {offset, std::get<1>(rebase())};
362 }
363 void set_rebase_size(uint32_t size) {
364 rebase_ = {std::get<0>(rebase()), size};
365 }
366
367 void set_bind_offset(uint32_t offset) {
368 bind_ = {offset, std::get<1>(bind())};
369 }
370 void set_bind_size(uint32_t size) {
371 bind_ = {std::get<0>(bind()), size};
372 }
373
374 void set_weak_bind_offset(uint32_t offset) {
375 weak_bind_ = {offset, std::get<1>(weak_bind())};
376 }
377 void set_weak_bind_size(uint32_t size) {
378 weak_bind_ = {std::get<0>(weak_bind()), size};
379 }
380
381 void set_lazy_bind_offset(uint32_t offset) {
382 lazy_bind_ = {offset, std::get<1>(lazy_bind())};
383 }
384 void set_lazy_bind_size(uint32_t size) {
385 lazy_bind_ = {std::get<0>(lazy_bind()), size};
386 }
387
388 void set_export_offset(uint32_t offset) {
389 export_ = {offset, std::get<1>(export_info())};
390 }
391
392 void set_export_size(uint32_t size) {
393 export_ = {std::get<0>(export_info()), size};
394 }
395
396 void add(std::unique_ptr<ExportInfo> info);
397
398 void accept(Visitor& visitor) const override;
399
400 std::ostream& print(std::ostream& os) const override;
401
402 static bool classof(const LoadCommand* cmd) {
403 const LoadCommand::TYPE type = cmd->command();
404 return type == LoadCommand::TYPE::DYLD_INFO ||
406 }
407
408 private:
409 using bind_container_t = std::set<DyldBindingInfo*, std::function<bool(DyldBindingInfo*, DyldBindingInfo*)>>;
410
411 LIEF_LOCAL void show_bindings(std::ostream& os, span<const uint8_t> buffer, bool is_lazy = false) const;
412
413 LIEF_LOCAL void show_trie(std::ostream& output, std::string output_prefix,
414 BinaryStream& stream, uint64_t start, uint64_t end,
415 const std::string& prefix) const;
416
417 LIEF_LOCAL DyldInfo& update_standard_bindings(const bind_container_t& bindings, vector_iostream& stream);
418 LIEF_LOCAL DyldInfo& update_standard_bindings_v1(const bind_container_t& bindings, vector_iostream& stream);
419 LIEF_LOCAL DyldInfo& update_standard_bindings_v2(const bind_container_t& bindings,
420 std::vector<RelocationDyld*> rebases, vector_iostream& stream);
421
422 LIEF_LOCAL DyldInfo& update_weak_bindings(const bind_container_t& bindings, vector_iostream& stream);
423 LIEF_LOCAL DyldInfo& update_lazy_bindings(const bind_container_t& bindings, vector_iostream& stream);
424
425 LIEF_LOCAL DyldInfo& update_rebase_info(vector_iostream& stream);
426 LIEF_LOCAL DyldInfo& update_binding_info(vector_iostream& stream, details::dyld_info_command& cmd);
427 LIEF_LOCAL DyldInfo& update_export_trie(vector_iostream& stream);
428
429 info_t rebase_;
430 span<uint8_t> rebase_opcodes_;
431
432 info_t bind_;
433 span<uint8_t> bind_opcodes_;
434
435 info_t weak_bind_;
436 span<uint8_t> weak_bind_opcodes_;
437
438 info_t lazy_bind_;
439 span<uint8_t> lazy_bind_opcodes_;
440
441 info_t export_;
442 span<uint8_t> export_trie_;
443
444 export_info_t export_info_;
445 binding_info_t binding_info_;
446
447 BINDING_ENCODING_VERSION binding_encoding_version_ = BINDING_ENCODING_VERSION::UNKNOWN;
448
449 Binary* binary_ = nullptr;
450};
451
456
457
458}
459}
460#endif
Class that is used to a read stream of data from different sources.
Definition BinaryStream.hpp:33
Class used to parse a single binary (i.e. non-FAT).
Definition BinaryParser.hpp:78
Class which represents a MachO binary.
Definition MachO/Binary.hpp:88
Definition BindingInfoIterator.hpp:36
Class used to rebuild a Mach-O file.
Definition MachO/Builder.hpp:63
This class represents a symbol binding operation associated with the LC_DYLD_INFO bytecode.
Definition DyldBindingInfo.hpp:34
Class that represents the LC_DYLD_INFO and LC_DYLD_INFO_ONLY commands.
Definition DyldInfo.hpp:50
BIND_SYMBOL_FLAGS
Definition DyldInfo.hpp:131
@ NON_WEAK_DEFINITION
Definition DyldInfo.hpp:133
@ WEAK_IMPORT
Definition DyldInfo.hpp:132
void weak_bind(const info_t &info)
Definition DyldInfo.hpp:350
ref_iterator< export_info_t &, ExportInfo * > it_export_info
Iterator which outputs const ExportInfo&.
Definition DyldInfo.hpp:75
std::ostream & print(std::ostream &os) const override
void set_export_size(uint32_t size)
Definition DyldInfo.hpp:392
span< const uint8_t > bind_opcodes() const
Return Binding's opcodes as raw data.
Definition DyldInfo.hpp:203
friend class BindingInfoIterator
Definition DyldInfo.hpp:56
const_ref_iterator< const export_info_t &, ExportInfo * > it_const_export_info
Iterator which outputs const ExportInfo&.
Definition DyldInfo.hpp:78
void add(std::unique_ptr< ExportInfo > info)
std::string show_lazy_bind_opcodes() const
Return the lazy opcodes in a humman-readable way.
BIND_OPCODES
Opcodes used by Dyld info to bind symbols.
Definition DyldInfo.hpp:106
@ SET_ADDEND_SLEB
Set the addend field to the following SLEB128 encoding.
Definition DyldInfo.hpp:113
@ DO_BIND_ADD_ADDR_ULEB
Perform binding, also add following ULEB128 as address.
Definition DyldInfo.hpp:117
@ THREADED_APPLY
Definition DyldInfo.hpp:122
@ DO_BIND_ULEB_TIMES_SKIPPING_ULEB
Perform binding for several symbols (as following ULEB128), and skip several bytes.
Definition DyldInfo.hpp:119
@ SET_DYLIB_ORDINAL_IMM
Set ordinal to immediate (lower 4-bits). Used for ordinal numbers from 0-15.
Definition DyldInfo.hpp:108
@ SET_SYMBOL_TRAILING_FLAGS_IMM
Set the following symbol (NULL-terminated char*).
Definition DyldInfo.hpp:111
@ SET_DYLIB_ORDINAL_ULEB
Set ordinal to following ULEB128 encoding. Used for ordinal numbers from 16+.
Definition DyldInfo.hpp:109
@ THREADED_SET_BIND_ORDINAL_TABLE_SIZE_ULEB
Definition DyldInfo.hpp:123
@ SET_DYLIB_SPECIAL_IMM
Set ordinal, with 0 or negative number as immediate. the value is sign extended.
Definition DyldInfo.hpp:110
@ DO_BIND
Perform binding of current table row.
Definition DyldInfo.hpp:116
@ DO_BIND_ADD_ADDR_IMM_SCALED
Perform binding, also add immediate (lower 4-bits) using scaling.
Definition DyldInfo.hpp:118
static constexpr auto OPCODE_MASK
Definition DyldInfo.hpp:136
it_const_binding_info bindings() const
Definition DyldInfo.hpp:288
DyldInfo(const details::dyld_info_command &dyld_info_cmd)
span< const uint8_t > rebase_opcodes() const
Return Rebase's opcodes as raw data.
Definition DyldInfo.hpp:171
void lazy_bind_opcodes(buffer_t raw)
Set new opcodes.
static bool classof(const LoadCommand *cmd)
Definition DyldInfo.hpp:402
void set_weak_bind_size(uint32_t size)
Definition DyldInfo.hpp:377
const info_t & export_info() const
Export information
Definition DyldInfo.hpp:318
REBASE_OPCODES
Definition DyldInfo.hpp:93
@ SET_TYPE_IMM
Set type to immediate (lower 4-bits). Used for ordinal numbers from 0-15.
Definition DyldInfo.hpp:95
@ DONE
It's finished.
Definition DyldInfo.hpp:94
@ DO_REBASE_IMM_TIMES
Rebase in the range of [segment's offset; segment's offset + immediate * sizeof(ptr)].
Definition DyldInfo.hpp:99
@ DO_REBASE_ULEB_TIMES_SKIPPING_ULEB
Rebase and skip several bytes.
Definition DyldInfo.hpp:102
@ DO_REBASE_ULEB_TIMES
Same as REBASE_OPCODE_DO_REBASE_IMM_TIMES but immediate is replaced with ULEB128 value.
Definition DyldInfo.hpp:100
@ ADD_ADDR_ULEB
Add segment's offset with the following ULEB128 encoding.
Definition DyldInfo.hpp:97
@ SET_SEGMENT_AND_OFFSET_ULEB
Set segment's index to immediate (lower 4-bits) and segment's offset to following ULEB128 encoding.
Definition DyldInfo.hpp:96
@ ADD_ADDR_IMM_SCALED
Add segment's offset with immediate scaling.
Definition DyldInfo.hpp:98
@ DO_REBASE_ADD_ADDR_ULEB
Rebase and increment segment's offset with following ULEB128 encoding + pointer's size.
Definition DyldInfo.hpp:101
void set_bind_size(uint32_t size)
Definition DyldInfo.hpp:370
std::string show_export_trie() const
Return the export trie in a humman-readable way.
const info_t & bind() const
Bind information
Definition DyldInfo.hpp:198
friend class BinaryParser
Definition DyldInfo.hpp:52
static constexpr auto IMMEDIATE_MASK
Definition DyldInfo.hpp:137
std::unique_ptr< LoadCommand > clone() const override
Definition DyldInfo.hpp:147
const info_t & rebase() const
Rebase information
Definition DyldInfo.hpp:166
std::vector< std::unique_ptr< DyldBindingInfo > > binding_info_t
Internal container for storing DyldBindingInfo.
Definition DyldInfo.hpp:63
DyldInfo & operator=(DyldInfo other)
void rebase_opcodes(buffer_t raw)
Set new opcodes.
ref_iterator< binding_info_t &, DyldBindingInfo * > it_binding_info
Iterator which outputs DyldBindingInfo&.
Definition DyldInfo.hpp:66
void set_bind_offset(uint32_t offset)
Definition DyldInfo.hpp:367
BIND_SUBOPCODE_THREADED
Definition DyldInfo.hpp:126
@ SET_BIND_ORDINAL_TABLE_SIZE_ULEB
Definition DyldInfo.hpp:127
DyldInfo(const DyldInfo &copy)
span< uint8_t > lazy_bind_opcodes()
Definition DyldInfo.hpp:273
void set_rebase_offset(uint32_t offset)
Definition DyldInfo.hpp:360
void export_info(const info_t &info)
Definition DyldInfo.hpp:356
BINDING_ENCODING_VERSION
Definition DyldInfo.hpp:80
void set_lazy_bind_size(uint32_t size)
Definition DyldInfo.hpp:384
const info_t & weak_bind() const
Weak Bind information
Definition DyldInfo.hpp:233
span< uint8_t > rebase_opcodes()
Definition DyldInfo.hpp:174
friend class Builder
Definition DyldInfo.hpp:54
void set_export_offset(uint32_t offset)
Definition DyldInfo.hpp:388
it_binding_info bindings()
Iterator over BindingInfo entries.
Definition DyldInfo.hpp:284
span< uint8_t > export_trie()
Definition DyldInfo.hpp:334
void weak_bind_opcodes(buffer_t raw)
Set new opcodes.
it_export_info exports()
Iterator over ExportInfo entries.
Definition DyldInfo.hpp:323
friend class Binary
Definition DyldInfo.hpp:53
void lazy_bind(const info_t &info)
Definition DyldInfo.hpp:353
friend class LinkEdit
Definition DyldInfo.hpp:55
void set_weak_bind_offset(uint32_t offset)
Definition DyldInfo.hpp:374
void bind_opcodes(buffer_t raw)
Set new opcodes.
REBASE_TYPE
Definition DyldInfo.hpp:86
@ THREADED
Definition DyldInfo.hpp:90
@ TEXT_PCREL32
Definition DyldInfo.hpp:89
@ POINTER
Definition DyldInfo.hpp:87
@ TEXT_ABSOLUTE32
Definition DyldInfo.hpp:88
std::pair< uint32_t, uint32_t > info_t
Tuple of offset and size.
Definition DyldInfo.hpp:60
void bind(const info_t &info)
Definition DyldInfo.hpp:347
std::string show_bind_opcodes() const
Return the bind opcodes in a humman-readable way.
void rebase(const info_t &info)
Definition DyldInfo.hpp:344
span< uint8_t > weak_bind_opcodes()
Definition DyldInfo.hpp:241
std::string show_rebases_opcodes() const
Return the rebase opcodes in a humman-readable way.
void export_trie(buffer_t raw)
Set new trie.
span< const uint8_t > lazy_bind_opcodes() const
Return Lazy Binding's opcodes as raw data.
Definition DyldInfo.hpp:270
it_const_export_info exports() const
Definition DyldInfo.hpp:326
void set_lazy_bind_offset(uint32_t offset)
Definition DyldInfo.hpp:381
const info_t & lazy_bind() const
Lazy Bind information
Definition DyldInfo.hpp:265
span< uint8_t > bind_opcodes()
Definition DyldInfo.hpp:206
std::vector< std::unique_ptr< ExportInfo > > export_info_t
Internal container for storing ExportInfo.
Definition DyldInfo.hpp:72
span< const uint8_t > export_trie() const
Return Export's trie as raw data.
Definition DyldInfo.hpp:331
void swap(DyldInfo &other) noexcept
const_ref_iterator< const binding_info_t &, DyldBindingInfo * > it_const_binding_info
Iterator which outputs const DyldBindingInfo&.
Definition DyldInfo.hpp:69
void accept(Visitor &visitor) const override
void set_rebase_size(uint32_t size)
Definition DyldInfo.hpp:363
span< const uint8_t > weak_bind_opcodes() const
Return Weak Binding's opcodes as raw data.
Definition DyldInfo.hpp:238
std::string show_weak_bind_opcodes() const
Return the bind opcodes in a humman-readable way.
Class that provides an interface over the Dyld export info.
Definition ExportInfo.hpp:38
Definition LinkEdit.hpp:45
uint32_t size() const
Size of the command (should be greather than sizeof(load_command)).
Definition LoadCommand.hpp:133
LoadCommand::TYPE command() const
Command type.
Definition LoadCommand.hpp:128
TYPE
Definition LoadCommand.hpp:44
@ DYLD_INFO
Definition LoadCommand.hpp:79
@ DYLD_INFO_ONLY
Definition LoadCommand.hpp:80
Class that represents a relocation found in the DyldInfo structure.
Definition RelocationDyld.hpp:33
Definition Visitor.hpp:210
Iterator which returns reference on container's values.
Definition iterators.hpp:46
Definition iostream.hpp:31
Definition endianness_support.hpp:59
Namespace related to the LIEF's Mach-O module.
Definition Abstract/Header.hpp:36
std::vector< uint8_t > buffer_t
Definition MachO/type_traits.hpp:24
const char * to_string(BuildToolVersion::TOOLS tool)
LIEF namespace.
Definition Abstract/Binary.hpp:40
tcb::span< ElementType, Extent > span
Definition span.hpp:22
ref_iterator< CT, U, typename decay_t< CT >::const_iterator > const_ref_iterator
Iterator which return const ref on container's values.
Definition iterators.hpp:257
#define LIEF_API
Definition visibility.h:41
#define LIEF_LOCAL
Definition visibility.h:42