LIEF: Library to Instrument Executable Formats Version 2.0.0
Loading...
Searching...
No Matches
DyldInfo.hpp
Go to the documentation of this file.
1/* Copyright 2017 - 2026 R. Thomas
2 *
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
6 *
7 * http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15#ifndef LIEF_MACHO_DYLD_INFO_COMMAND_H
16#define LIEF_MACHO_DYLD_INFO_COMMAND_H
17#include <memory>
18#include <ostream>
19#include <set>
20#include <string>
21#include <vector>
22
24#include "LIEF/span.hpp"
25#include "LIEF/visibility.h"
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
71
73 using export_info_t = std::vector<std::unique_ptr<ExportInfo>>;
74
77
81
84 V1,
85 V2,
86 };
87
88 enum class REBASE_TYPE : uint64_t {
89 POINTER = 1u,
90 TEXT_ABSOLUTE32 = 2u,
91 TEXT_PCREL32 = 3u,
92 THREADED = 102u,
93 };
94
95 enum class REBASE_OPCODES : uint8_t {
97 DONE = 0x00u,
100 SET_TYPE_IMM = 0x10u,
103 SET_SEGMENT_AND_OFFSET_ULEB = 0x20u,
105 ADD_ADDR_ULEB = 0x30u,
107 ADD_ADDR_IMM_SCALED = 0x40u,
110 DO_REBASE_IMM_TIMES = 0x50u,
113 DO_REBASE_ULEB_TIMES = 0x60u,
116 DO_REBASE_ADD_ADDR_ULEB = 0x70u,
118 DO_REBASE_ULEB_TIMES_SKIPPING_ULEB = 0x80u,
119 };
120
122 enum class BIND_OPCODES : uint8_t {
124 DONE = 0x00u,
127 SET_DYLIB_ORDINAL_IMM = 0x10u,
130 SET_DYLIB_ORDINAL_ULEB = 0x20u,
133 SET_DYLIB_SPECIAL_IMM = 0x30u,
135 SET_SYMBOL_TRAILING_FLAGS_IMM = 0x40u,
137 SET_TYPE_IMM = 0x50u,
139 SET_ADDEND_SLEB = 0x60u,
142 SET_SEGMENT_AND_OFFSET_ULEB = 0x70u,
144 ADD_ADDR_ULEB = 0x80u,
146 DO_BIND = 0x90u,
148 DO_BIND_ADD_ADDR_ULEB = 0xA0u,
150 DO_BIND_ADD_ADDR_IMM_SCALED = 0xB0u,
153 DO_BIND_ULEB_TIMES_SKIPPING_ULEB = 0xC0u,
154 THREADED = 0xD0u,
155
156 THREADED_APPLY = 0xD0u | 0x01u,
157 THREADED_SET_BIND_ORDINAL_TABLE_SIZE_ULEB = 0xD0u | 0x00u,
158 };
159
160 enum class BIND_SUBOPCODE_THREADED : uint8_t {
161 SET_BIND_ORDINAL_TABLE_SIZE_ULEB = 0x00u,
162 APPLY = 0x01u,
163 };
164
169
170 static constexpr auto OPCODE_MASK = uint32_t(0xF0);
171 static constexpr auto IMMEDIATE_MASK = uint32_t(0x0F);
172
174 DyldInfo(const details::dyld_info_command& dyld_info_cmd);
175
177 DyldInfo(const DyldInfo& copy);
178
179 void swap(DyldInfo& other) noexcept;
180
181 std::unique_ptr<LoadCommand> clone() const override {
182 return std::make_unique<DyldInfo>(*this);
183 }
184
185 ~DyldInfo() override;
186
200 const info_t& rebase() const {
201 return rebase_;
202 }
203
206 return rebase_opcodes_;
207 }
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 }
241
243 return bind_opcodes_;
244 }
245
248
250 std::string show_bind_opcodes() const;
251
269 const info_t& weak_bind() const {
270 return weak_bind_;
271 }
272
275 return weak_bind_opcodes_;
276 }
277
279 return weak_bind_opcodes_;
280 }
281
284
286 std::string show_weak_bind_opcodes() const;
287
302 const info_t& lazy_bind() const {
303 return lazy_bind_;
304 }
305
308 return lazy_bind_opcodes_;
309 }
310
312 return lazy_bind_opcodes_;
313 }
314
317
319 std::string show_lazy_bind_opcodes() const;
320
323 return binding_info_;
324 }
325
327 return binding_info_;
328 }
329
356 const info_t& export_info() const {
357 return export_;
358 }
359
362 return export_info_;
363 }
364
366 return export_info_;
367 }
368
371 return export_trie_;
372 }
373
375 return export_trie_;
376 }
377
380
382 std::string show_export_trie() const;
383
384 void rebase(const info_t& info) {
385 rebase_ = info;
386 }
387
388 void bind(const info_t& info) {
389 bind_ = info;
390 }
391
392 void weak_bind(const info_t& info) {
393 weak_bind_ = info;
394 }
395
396 void lazy_bind(const info_t& info) {
397 lazy_bind_ = info;
398 }
399
400 void export_info(const info_t& info) {
401 export_ = info;
402 }
403
404 void set_rebase_offset(uint32_t offset) {
405 rebase_ = {offset, std::get<1>(rebase())};
406 }
407
408 void set_rebase_size(uint32_t size) {
409 rebase_ = {std::get<0>(rebase()), size};
410 }
411
412 void set_bind_offset(uint32_t offset) {
413 bind_ = {offset, std::get<1>(bind())};
414 }
415
416 void set_bind_size(uint32_t size) {
417 bind_ = {std::get<0>(bind()), size};
418 }
419
420 void set_weak_bind_offset(uint32_t offset) {
421 weak_bind_ = {offset, std::get<1>(weak_bind())};
422 }
423
424 void set_weak_bind_size(uint32_t size) {
425 weak_bind_ = {std::get<0>(weak_bind()), size};
426 }
427
428 void set_lazy_bind_offset(uint32_t offset) {
429 lazy_bind_ = {offset, std::get<1>(lazy_bind())};
430 }
431
432 void set_lazy_bind_size(uint32_t size) {
433 lazy_bind_ = {std::get<0>(lazy_bind()), size};
434 }
435
436 void set_export_offset(uint32_t offset) {
437 export_ = {offset, std::get<1>(export_info())};
438 }
439
440 void set_export_size(uint32_t size) {
441 export_ = {std::get<0>(export_info()), size};
442 }
443
444 ExportInfo* add(std::unique_ptr<ExportInfo> info) LIEF_LIFETIMEBOUND;
445
446 void accept(Visitor& visitor) const override;
447
448 std::ostream& print(std::ostream& os) const override;
449
450 static bool classof(const LoadCommand* cmd) {
451 const LoadCommand::TYPE type = cmd->command();
452 return type == LoadCommand::TYPE::DYLD_INFO ||
454 }
455
456 private:
457 using bind_container_t =
458 std::set<DyldBindingInfo*,
459 std::function<bool(DyldBindingInfo*, DyldBindingInfo*)>>;
460
461 LIEF_LOCAL void show_bindings(std::ostream& os, span<const uint8_t> buffer,
462 bool is_lazy = false) const;
463
464 LIEF_LOCAL void show_trie(std::ostream& output, std::string output_prefix,
465 BinaryStream& stream, uint64_t start, uint64_t end,
466 const std::string& prefix) const;
467
468 LIEF_LOCAL DyldInfo& update_standard_bindings(const bind_container_t& bindings,
469 vector_iostream& stream);
471 update_standard_bindings_v1(const bind_container_t& bindings,
472 vector_iostream& stream);
474 update_standard_bindings_v2(const bind_container_t& bindings,
475 std::vector<RelocationDyld*> rebases,
476 vector_iostream& stream);
477
478 LIEF_LOCAL DyldInfo& update_weak_bindings(const bind_container_t& bindings,
479 vector_iostream& stream);
480 LIEF_LOCAL DyldInfo& update_lazy_bindings(const bind_container_t& bindings,
481 vector_iostream& stream);
482
483 LIEF_LOCAL DyldInfo& update_rebase_info(vector_iostream& stream);
484 LIEF_LOCAL DyldInfo& update_binding_info(vector_iostream& stream,
485 details::dyld_info_command& cmd);
486 LIEF_LOCAL DyldInfo& update_export_trie(vector_iostream& stream);
487
488 info_t rebase_;
489 span<uint8_t> rebase_opcodes_;
490
491 info_t bind_;
492 span<uint8_t> bind_opcodes_;
493
494 info_t weak_bind_;
495 span<uint8_t> weak_bind_opcodes_;
496
497 info_t lazy_bind_;
498 span<uint8_t> lazy_bind_opcodes_;
499
500 info_t export_;
501 span<uint8_t> export_trie_;
502
503 export_info_t export_info_;
504 binding_info_t binding_info_;
505
506 BINDING_ENCODING_VERSION binding_encoding_version_ =
507 BINDING_ENCODING_VERSION::UNKNOWN;
508
509 Binary* binary_ = nullptr;
510};
511
516
517
518}
519}
520#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:80
Class which represents a MachO binary.
Definition MachO/Binary.hpp:93
Definition BindingInfoIterator.hpp:31
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:165
@ NON_WEAK_DEFINITION
Definition DyldInfo.hpp:167
@ WEAK_IMPORT
Definition DyldInfo.hpp:166
void weak_bind(const info_t &info)
Definition DyldInfo.hpp:392
ref_iterator< export_info_t &, ExportInfo * > it_export_info
Iterator which outputs const ExportInfo&.
Definition DyldInfo.hpp:76
std::ostream & print(std::ostream &os) const override
void set_export_size(uint32_t size)
Definition DyldInfo.hpp:440
span< const uint8_t > bind_opcodes() const
Return Binding's opcodes as raw data.
Definition DyldInfo.hpp:238
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:79
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:122
static constexpr auto OPCODE_MASK
Definition DyldInfo.hpp:170
it_const_binding_info bindings() const
Definition DyldInfo.hpp:326
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:205
void lazy_bind_opcodes(buffer_t raw)
Set new opcodes.
static bool classof(const LoadCommand *cmd)
Definition DyldInfo.hpp:450
void set_weak_bind_size(uint32_t size)
Definition DyldInfo.hpp:424
const info_t & export_info() const
Export information
Definition DyldInfo.hpp:356
REBASE_OPCODES
Definition DyldInfo.hpp:95
void set_bind_size(uint32_t size)
Definition DyldInfo.hpp:416
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:52
static constexpr auto IMMEDIATE_MASK
Definition DyldInfo.hpp:171
std::unique_ptr< LoadCommand > clone() const override
Definition DyldInfo.hpp:181
const info_t & rebase() const
Rebase information
Definition DyldInfo.hpp:200
std::vector< std::unique_ptr< DyldBindingInfo > > binding_info_t
Internal container for storing DyldBindingInfo.
Definition DyldInfo.hpp:63
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:66
void set_bind_offset(uint32_t offset)
Definition DyldInfo.hpp:412
BIND_SUBOPCODE_THREADED
Definition DyldInfo.hpp:160
DyldInfo(const DyldInfo &copy)
span< uint8_t > lazy_bind_opcodes()
Definition DyldInfo.hpp:311
void set_rebase_offset(uint32_t offset)
Definition DyldInfo.hpp:404
void export_info(const info_t &info)
Definition DyldInfo.hpp:400
BINDING_ENCODING_VERSION
Definition DyldInfo.hpp:82
void set_lazy_bind_size(uint32_t size)
Definition DyldInfo.hpp:432
const info_t & weak_bind() const
Weak Bind information
Definition DyldInfo.hpp:269
span< uint8_t > rebase_opcodes()
Definition DyldInfo.hpp:209
friend class Builder
Definition DyldInfo.hpp:54
void set_export_offset(uint32_t offset)
Definition DyldInfo.hpp:436
it_binding_info bindings()
Iterator over BindingInfo entries.
Definition DyldInfo.hpp:322
span< uint8_t > export_trie()
Definition DyldInfo.hpp:374
void weak_bind_opcodes(buffer_t raw)
Set new opcodes.
it_export_info exports()
Iterator over ExportInfo entries.
Definition DyldInfo.hpp:361
friend class Binary
Definition DyldInfo.hpp:53
void lazy_bind(const info_t &info)
Definition DyldInfo.hpp:396
friend class LinkEdit
Definition DyldInfo.hpp:55
void set_weak_bind_offset(uint32_t offset)
Definition DyldInfo.hpp:420
void bind_opcodes(buffer_t raw)
Set new opcodes.
REBASE_TYPE
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:388
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:69
void rebase(const info_t &info)
Definition DyldInfo.hpp:384
span< uint8_t > weak_bind_opcodes()
Definition DyldInfo.hpp:278
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:307
it_const_export_info exports() const
Definition DyldInfo.hpp:365
void set_lazy_bind_offset(uint32_t offset)
Definition DyldInfo.hpp:428
const info_t & lazy_bind() const
Lazy Bind information
Definition DyldInfo.hpp:302
span< uint8_t > bind_opcodes()
Definition DyldInfo.hpp:242
std::vector< std::unique_ptr< ExportInfo > > export_info_t
Internal container for storing ExportInfo.
Definition DyldInfo.hpp:73
span< const uint8_t > export_trie() const
Return Export's trie as raw data.
Definition DyldInfo.hpp:370
void swap(DyldInfo &other) noexcept
void accept(Visitor &visitor) const override
void set_rebase_size(uint32_t size)
Definition DyldInfo.hpp:408
span< const uint8_t > weak_bind_opcodes() const
Return Weak Binding's opcodes as raw data.
Definition DyldInfo.hpp:274
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:37
Definition LinkEdit.hpp:46
uint32_t size() const
Size of the command (should be greater than sizeof(load_command)).
Definition LoadCommand.hpp:136
LoadCommand::TYPE command() const
Command type.
Definition LoadCommand.hpp:131
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:211
Iterator which returns reference on container's values.
Definition iterators.hpp:120
Definition iostream.hpp:33
#define LIEF_LIFETIMEBOUND
Definition compiler_attributes.hpp:72
Definition endianness_support.hpp:62
Namespace related to the LIEF's Mach-O module.
Definition Abstract/Header.hpp:35
std::vector< uint8_t > buffer_t
Definition MachO/type_traits.hpp:23
@ UNKNOWN
Definition MachO/enums.hpp:25
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:385
#define LIEF_API
Definition visibility.h:44
#define LIEF_LOCAL
Definition visibility.h:45