LIEF: Library to Instrument Executable Formats Version 1.0.0
Loading...
Searching...
No Matches
DyldInfo.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_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
25#include "LIEF/visibility.h"
26#include "LIEF/span.hpp"
27
30#include "LIEF/iterators.hpp"
31
32namespace LIEF {
33class vector_iostream;
34class BinaryStream;
35namespace MachO {
36
37class Binary;
38class BinaryParser;
40class Builder;
41class DyldBindingInfo;
42class ExportInfo;
43class LinkEdit;
44class RelocationDyld;
45
46namespace details {
47struct dyld_info_command;
48}
49
52
53 friend class BinaryParser;
54 friend class Binary;
55 friend class Builder;
56 friend class LinkEdit;
57 friend class BindingInfoIterator;
58
59 public:
61 using info_t = std::pair<uint32_t, uint32_t>;
62
64 using binding_info_t = std::vector<std::unique_ptr<DyldBindingInfo>>;
65
68
72
74 using export_info_t = std::vector<std::unique_ptr<ExportInfo>>;
75
78
82
85 V1,
86 V2,
87 };
88
89 enum class REBASE_TYPE : uint64_t {
90 POINTER = 1u,
91 TEXT_ABSOLUTE32 = 2u,
92 TEXT_PCREL32 = 3u,
93 THREADED = 102u,
94 };
95
96 enum class REBASE_OPCODES : uint8_t {
98 DONE = 0x00u,
101 SET_TYPE_IMM = 0x10u,
104 SET_SEGMENT_AND_OFFSET_ULEB = 0x20u,
106 ADD_ADDR_ULEB = 0x30u,
108 ADD_ADDR_IMM_SCALED = 0x40u,
111 DO_REBASE_IMM_TIMES = 0x50u,
114 DO_REBASE_ULEB_TIMES = 0x60u,
117 DO_REBASE_ADD_ADDR_ULEB = 0x70u,
119 DO_REBASE_ULEB_TIMES_SKIPPING_ULEB = 0x80u,
120 };
121
123 enum class BIND_OPCODES : uint8_t {
125 DONE = 0x00u,
128 SET_DYLIB_ORDINAL_IMM = 0x10u,
131 SET_DYLIB_ORDINAL_ULEB = 0x20u,
134 SET_DYLIB_SPECIAL_IMM = 0x30u,
136 SET_SYMBOL_TRAILING_FLAGS_IMM = 0x40u,
138 SET_TYPE_IMM = 0x50u,
140 SET_ADDEND_SLEB = 0x60u,
143 SET_SEGMENT_AND_OFFSET_ULEB = 0x70u,
145 ADD_ADDR_ULEB = 0x80u,
147 DO_BIND = 0x90u,
149 DO_BIND_ADD_ADDR_ULEB = 0xA0u,
151 DO_BIND_ADD_ADDR_IMM_SCALED = 0xB0u,
154 DO_BIND_ULEB_TIMES_SKIPPING_ULEB = 0xC0u,
155 THREADED = 0xD0u,
156
157 THREADED_APPLY = 0xD0u | 0x01u,
158 THREADED_SET_BIND_ORDINAL_TABLE_SIZE_ULEB = 0xD0u | 0x00u,
159 };
160
161 enum class BIND_SUBOPCODE_THREADED : uint8_t {
162 SET_BIND_ORDINAL_TABLE_SIZE_ULEB = 0x00u,
163 APPLY = 0x01u,
164 };
165
170
171 static constexpr auto OPCODE_MASK = uint32_t(0xF0);
172 static constexpr auto IMMEDIATE_MASK = uint32_t(0x0F);
173
175 DyldInfo(const details::dyld_info_command& dyld_info_cmd);
176
178 DyldInfo(const DyldInfo& copy);
179
180 void swap(DyldInfo& other) noexcept;
181
182 std::unique_ptr<LoadCommand> clone() const override {
183 return std::unique_ptr<DyldInfo>(new DyldInfo(*this));
184 }
185
186 ~DyldInfo() override;
187
201 const info_t& rebase() const {
202 return rebase_;
203 }
204
207 return rebase_opcodes_;
208 }
210 return rebase_opcodes_;
211 }
212
215
217 std::string show_rebases_opcodes() const;
218
233 const info_t& bind() const {
234 return bind_;
235 }
236
239 return bind_opcodes_;
240 }
242 return bind_opcodes_;
243 }
244
247
249 std::string show_bind_opcodes() const;
250
268 const info_t& weak_bind() const {
269 return weak_bind_;
270 }
271
274 return weak_bind_opcodes_;
275 }
277 return weak_bind_opcodes_;
278 }
279
282
284 std::string show_weak_bind_opcodes() const;
285
300 const info_t& lazy_bind() const {
301 return lazy_bind_;
302 }
303
306 return lazy_bind_opcodes_;
307 }
309 return lazy_bind_opcodes_;
310 }
311
314
316 std::string show_lazy_bind_opcodes() const;
317
320 return binding_info_;
321 }
322
324 return binding_info_;
325 }
326
353 const info_t& export_info() const {
354 return export_;
355 }
356
359 return export_info_;
360 }
362 return export_info_;
363 }
364
367 return export_trie_;
368 }
370 return export_trie_;
371 }
372
375
377 std::string show_export_trie() const;
378
379 void rebase(const info_t& info) {
380 rebase_ = info;
381 }
382 void bind(const info_t& info) {
383 bind_ = info;
384 }
385 void weak_bind(const info_t& info) {
386 weak_bind_ = info;
387 }
388 void lazy_bind(const info_t& info) {
389 lazy_bind_ = info;
390 }
391 void export_info(const info_t& info) {
392 export_ = info;
393 }
394
395 void set_rebase_offset(uint32_t offset) {
396 rebase_ = {offset, std::get<1>(rebase())};
397 }
398 void set_rebase_size(uint32_t size) {
399 rebase_ = {std::get<0>(rebase()), size};
400 }
401
402 void set_bind_offset(uint32_t offset) {
403 bind_ = {offset, std::get<1>(bind())};
404 }
405 void set_bind_size(uint32_t size) {
406 bind_ = {std::get<0>(bind()), size};
407 }
408
409 void set_weak_bind_offset(uint32_t offset) {
410 weak_bind_ = {offset, std::get<1>(weak_bind())};
411 }
412 void set_weak_bind_size(uint32_t size) {
413 weak_bind_ = {std::get<0>(weak_bind()), size};
414 }
415
416 void set_lazy_bind_offset(uint32_t offset) {
417 lazy_bind_ = {offset, std::get<1>(lazy_bind())};
418 }
419 void set_lazy_bind_size(uint32_t size) {
420 lazy_bind_ = {std::get<0>(lazy_bind()), size};
421 }
422
423 void set_export_offset(uint32_t offset) {
424 export_ = {offset, std::get<1>(export_info())};
425 }
426
427 void set_export_size(uint32_t size) {
428 export_ = {std::get<0>(export_info()), size};
429 }
430
431 ExportInfo* add(std::unique_ptr<ExportInfo> info) LIEF_LIFETIMEBOUND;
432
433 void accept(Visitor& visitor) const override;
434
435 std::ostream& print(std::ostream& os) const override;
436
437 static bool classof(const LoadCommand* cmd) {
438 const LoadCommand::TYPE type = cmd->command();
439 return type == LoadCommand::TYPE::DYLD_INFO ||
441 }
442
443 private:
444 using bind_container_t =
445 std::set<DyldBindingInfo*,
446 std::function<bool(DyldBindingInfo*, DyldBindingInfo*)>>;
447
448 LIEF_LOCAL void show_bindings(std::ostream& os, span<const uint8_t> buffer,
449 bool is_lazy = false) const;
450
451 LIEF_LOCAL void show_trie(std::ostream& output, std::string output_prefix,
452 BinaryStream& stream, uint64_t start, uint64_t end,
453 const std::string& prefix) const;
454
455 LIEF_LOCAL DyldInfo& update_standard_bindings(const bind_container_t& bindings,
456 vector_iostream& stream);
458 update_standard_bindings_v1(const bind_container_t& bindings,
459 vector_iostream& stream);
461 update_standard_bindings_v2(const bind_container_t& bindings,
462 std::vector<RelocationDyld*> rebases,
463 vector_iostream& stream);
464
465 LIEF_LOCAL DyldInfo& update_weak_bindings(const bind_container_t& bindings,
466 vector_iostream& stream);
467 LIEF_LOCAL DyldInfo& update_lazy_bindings(const bind_container_t& bindings,
468 vector_iostream& stream);
469
470 LIEF_LOCAL DyldInfo& update_rebase_info(vector_iostream& stream);
471 LIEF_LOCAL DyldInfo& update_binding_info(vector_iostream& stream,
472 details::dyld_info_command& cmd);
473 LIEF_LOCAL DyldInfo& update_export_trie(vector_iostream& stream);
474
475 info_t rebase_;
476 span<uint8_t> rebase_opcodes_;
477
478 info_t bind_;
479 span<uint8_t> bind_opcodes_;
480
481 info_t weak_bind_;
482 span<uint8_t> weak_bind_opcodes_;
483
484 info_t lazy_bind_;
485 span<uint8_t> lazy_bind_opcodes_;
486
487 info_t export_;
488 span<uint8_t> export_trie_;
489
490 export_info_t export_info_;
491 binding_info_t binding_info_;
492
493 BINDING_ENCODING_VERSION binding_encoding_version_ =
494 BINDING_ENCODING_VERSION::UNKNOWN;
495
496 Binary* binary_ = nullptr;
497};
498
503
504
505}
506}
507#endif
Class that is used to a read stream of data from different sources.
Definition BinaryStream.hpp:34
Class used to parse a single binary (i.e. non-FAT).
Definition BinaryParser.hpp:79
Class which represents a MachO binary.
Definition MachO/Binary.hpp:91
Definition BindingInfoIterator.hpp:32
Class used to rebuild a Mach-O file.
Definition MachO/Builder.hpp:64
This class represents a symbol binding operation associated with the LC_DYLD_INFO bytecode.
Definition DyldBindingInfo.hpp:35
Class that represents the LC_DYLD_INFO and LC_DYLD_INFO_ONLY commands.
Definition DyldInfo.hpp:51
BIND_SYMBOL_FLAGS
Definition DyldInfo.hpp:166
@ NON_WEAK_DEFINITION
Definition DyldInfo.hpp:168
@ WEAK_IMPORT
Definition DyldInfo.hpp:167
void weak_bind(const info_t &info)
Definition DyldInfo.hpp:385
ref_iterator< export_info_t &, ExportInfo * > it_export_info
Iterator which outputs const ExportInfo&.
Definition DyldInfo.hpp:77
std::ostream & print(std::ostream &os) const override
void set_export_size(uint32_t size)
Definition DyldInfo.hpp:427
span< const uint8_t > bind_opcodes() const
Return Binding's opcodes as raw data.
Definition DyldInfo.hpp:238
friend class BindingInfoIterator
Definition DyldInfo.hpp:57
const_ref_iterator< const export_info_t &, ExportInfo * > it_const_export_info
Iterator which outputs const ExportInfo&.
Definition DyldInfo.hpp:80
std::string show_lazy_bind_opcodes() const
Return the lazy opcodes in a human-readable way.
BIND_OPCODES
Opcodes used by Dyld info to bind symbols.
Definition DyldInfo.hpp:123
static constexpr auto OPCODE_MASK
Definition DyldInfo.hpp:171
it_const_binding_info bindings() const
Definition DyldInfo.hpp:323
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:206
void lazy_bind_opcodes(buffer_t raw)
Set new opcodes.
static bool classof(const LoadCommand *cmd)
Definition DyldInfo.hpp:437
void set_weak_bind_size(uint32_t size)
Definition DyldInfo.hpp:412
const info_t & export_info() const
Export information
Definition DyldInfo.hpp:353
REBASE_OPCODES
Definition DyldInfo.hpp:96
void set_bind_size(uint32_t size)
Definition DyldInfo.hpp:405
std::string show_export_trie() const
Return the export trie in a human-readable way.
const info_t & bind() const
Bind information
Definition DyldInfo.hpp:233
friend class BinaryParser
Definition DyldInfo.hpp:53
static constexpr auto IMMEDIATE_MASK
Definition DyldInfo.hpp:172
std::unique_ptr< LoadCommand > clone() const override
Definition DyldInfo.hpp:182
const info_t & rebase() const
Rebase information
Definition DyldInfo.hpp:201
std::vector< std::unique_ptr< DyldBindingInfo > > binding_info_t
Internal container for storing DyldBindingInfo.
Definition DyldInfo.hpp:64
ExportInfo * add(std::unique_ptr< ExportInfo > info)
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:67
void set_bind_offset(uint32_t offset)
Definition DyldInfo.hpp:402
BIND_SUBOPCODE_THREADED
Definition DyldInfo.hpp:161
DyldInfo(const DyldInfo &copy)
span< uint8_t > lazy_bind_opcodes()
Definition DyldInfo.hpp:308
void set_rebase_offset(uint32_t offset)
Definition DyldInfo.hpp:395
void export_info(const info_t &info)
Definition DyldInfo.hpp:391
BINDING_ENCODING_VERSION
Definition DyldInfo.hpp:83
void set_lazy_bind_size(uint32_t size)
Definition DyldInfo.hpp:419
const info_t & weak_bind() const
Weak Bind information
Definition DyldInfo.hpp:268
span< uint8_t > rebase_opcodes()
Definition DyldInfo.hpp:209
friend class Builder
Definition DyldInfo.hpp:55
void set_export_offset(uint32_t offset)
Definition DyldInfo.hpp:423
it_binding_info bindings()
Iterator over BindingInfo entries.
Definition DyldInfo.hpp:319
span< uint8_t > export_trie()
Definition DyldInfo.hpp:369
void weak_bind_opcodes(buffer_t raw)
Set new opcodes.
it_export_info exports()
Iterator over ExportInfo entries.
Definition DyldInfo.hpp:358
friend class Binary
Definition DyldInfo.hpp:54
void lazy_bind(const info_t &info)
Definition DyldInfo.hpp:388
friend class LinkEdit
Definition DyldInfo.hpp:56
void set_weak_bind_offset(uint32_t offset)
Definition DyldInfo.hpp:409
void bind_opcodes(buffer_t raw)
Set new opcodes.
REBASE_TYPE
Definition DyldInfo.hpp:89
std::pair< uint32_t, uint32_t > info_t
Tuple of offset and size.
Definition DyldInfo.hpp:61
void bind(const info_t &info)
Definition DyldInfo.hpp:382
std::string show_bind_opcodes() const
Return the bind opcodes in a human-readable way.
const_ref_iterator< const binding_info_t &, DyldBindingInfo * > it_const_binding_info
Iterator which outputs const DyldBindingInfo&.
Definition DyldInfo.hpp:70
void rebase(const info_t &info)
Definition DyldInfo.hpp:379
span< uint8_t > weak_bind_opcodes()
Definition DyldInfo.hpp:276
std::string show_rebases_opcodes() const
Return the rebase opcodes in a human-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:305
it_const_export_info exports() const
Definition DyldInfo.hpp:361
void set_lazy_bind_offset(uint32_t offset)
Definition DyldInfo.hpp:416
const info_t & lazy_bind() const
Lazy Bind information
Definition DyldInfo.hpp:300
span< uint8_t > bind_opcodes()
Definition DyldInfo.hpp:241
std::vector< std::unique_ptr< ExportInfo > > export_info_t
Internal container for storing ExportInfo.
Definition DyldInfo.hpp:74
span< const uint8_t > export_trie() const
Return Export's trie as raw data.
Definition DyldInfo.hpp:366
void swap(DyldInfo &other) noexcept
void accept(Visitor &visitor) const override
void set_rebase_size(uint32_t size)
Definition DyldInfo.hpp:398
span< const uint8_t > weak_bind_opcodes() const
Return Weak Binding's opcodes as raw data.
Definition DyldInfo.hpp:273
std::string show_weak_bind_opcodes() const
Return the bind opcodes in a human-readable way.
Class that provides an interface over the Dyld export info.
Definition ExportInfo.hpp:38
Definition LinkEdit.hpp:47
uint32_t size() const
Size of the command (should be greater than sizeof(load_command)).
Definition LoadCommand.hpp:135
LoadCommand::TYPE command() const
Command type.
Definition LoadCommand.hpp:130
TYPE
Definition LoadCommand.hpp:46
@ DYLD_INFO
Definition LoadCommand.hpp:81
@ DYLD_INFO_ONLY
Definition LoadCommand.hpp:82
Class that represents a relocation found in the DyldInfo structure.
Definition RelocationDyld.hpp:33
Definition Visitor.hpp:212
Iterator which returns reference on container's values.
Definition iterators.hpp:47
Definition iostream.hpp:32
#define LIEF_LIFETIMEBOUND
Definition compiler_attributes.hpp:72
Definition endianness_support.hpp:60
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
@ UNKNOWN
Definition MachO/enums.hpp:24
const char * to_string(BuildToolVersion::TOOLS tool)
LIEF namespace.
Definition Abstract/Binary.hpp:41
tcb::span< ElementType, Extent > span
Definition span.hpp:22
ref_iterator< CT, U, typename decay_t< CT >::const_iterator > const_ref_iterator
Iterator which returns a const ref on container's values.
Definition iterators.hpp:320
#define LIEF_API
Definition visibility.h:45
#define LIEF_LOCAL
Definition visibility.h:46