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
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
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::unique_ptr<DyldInfo>(new DyldInfo(*this));
183 }
184
185 ~DyldInfo() override;
186
200 const info_t& rebase() const {
201 return rebase_;
202 }
203
206 return rebase_opcodes_;
207 }
209 return rebase_opcodes_;
210 }
211
214
216 std::string show_rebases_opcodes() const;
217
232 const info_t& bind() const {
233 return bind_;
234 }
235
238 return bind_opcodes_;
239 }
241 return bind_opcodes_;
242 }
243
246
248 std::string show_bind_opcodes() const;
249
267 const info_t& weak_bind() const {
268 return weak_bind_;
269 }
270
273 return weak_bind_opcodes_;
274 }
276 return weak_bind_opcodes_;
277 }
278
281
283 std::string show_weak_bind_opcodes() const;
284
299 const info_t& lazy_bind() const {
300 return lazy_bind_;
301 }
302
305 return lazy_bind_opcodes_;
306 }
308 return lazy_bind_opcodes_;
309 }
310
313
315 std::string show_lazy_bind_opcodes() const;
316
319 return binding_info_;
320 }
321
323 return binding_info_;
324 }
325
352 const info_t& export_info() const {
353 return export_;
354 }
355
358 return export_info_;
359 }
361 return export_info_;
362 }
363
366 return export_trie_;
367 }
369 return export_trie_;
370 }
371
374
376 std::string show_export_trie() const;
377
378 void rebase(const info_t& info) {
379 rebase_ = info;
380 }
381 void bind(const info_t& info) {
382 bind_ = info;
383 }
384 void weak_bind(const info_t& info) {
385 weak_bind_ = info;
386 }
387 void lazy_bind(const info_t& info) {
388 lazy_bind_ = info;
389 }
390 void export_info(const info_t& info) {
391 export_ = info;
392 }
393
394 void set_rebase_offset(uint32_t offset) {
395 rebase_ = {offset, std::get<1>(rebase())};
396 }
397 void set_rebase_size(uint32_t size) {
398 rebase_ = {std::get<0>(rebase()), size};
399 }
400
401 void set_bind_offset(uint32_t offset) {
402 bind_ = {offset, std::get<1>(bind())};
403 }
404 void set_bind_size(uint32_t size) {
405 bind_ = {std::get<0>(bind()), size};
406 }
407
408 void set_weak_bind_offset(uint32_t offset) {
409 weak_bind_ = {offset, std::get<1>(weak_bind())};
410 }
411 void set_weak_bind_size(uint32_t size) {
412 weak_bind_ = {std::get<0>(weak_bind()), size};
413 }
414
415 void set_lazy_bind_offset(uint32_t offset) {
416 lazy_bind_ = {offset, std::get<1>(lazy_bind())};
417 }
418 void set_lazy_bind_size(uint32_t size) {
419 lazy_bind_ = {std::get<0>(lazy_bind()), size};
420 }
421
422 void set_export_offset(uint32_t offset) {
423 export_ = {offset, std::get<1>(export_info())};
424 }
425
426 void set_export_size(uint32_t size) {
427 export_ = {std::get<0>(export_info()), size};
428 }
429
430 void add(std::unique_ptr<ExportInfo> info);
431
432 void accept(Visitor& visitor) const override;
433
434 std::ostream& print(std::ostream& os) const override;
435
436 static bool classof(const LoadCommand* cmd) {
437 const LoadCommand::TYPE type = cmd->command();
438 return type == LoadCommand::TYPE::DYLD_INFO ||
440 }
441
442 private:
443 using bind_container_t =
444 std::set<DyldBindingInfo*,
445 std::function<bool(DyldBindingInfo*, DyldBindingInfo*)>>;
446
447 LIEF_LOCAL void show_bindings(std::ostream& os, span<const uint8_t> buffer,
448 bool is_lazy = false) const;
449
450 LIEF_LOCAL void show_trie(std::ostream& output, std::string output_prefix,
451 BinaryStream& stream, uint64_t start, uint64_t end,
452 const std::string& prefix) const;
453
454 LIEF_LOCAL DyldInfo& update_standard_bindings(const bind_container_t& bindings,
455 vector_iostream& stream);
457 update_standard_bindings_v1(const bind_container_t& bindings,
458 vector_iostream& stream);
460 update_standard_bindings_v2(const bind_container_t& bindings,
461 std::vector<RelocationDyld*> rebases,
462 vector_iostream& stream);
463
464 LIEF_LOCAL DyldInfo& update_weak_bindings(const bind_container_t& bindings,
465 vector_iostream& stream);
466 LIEF_LOCAL DyldInfo& update_lazy_bindings(const bind_container_t& bindings,
467 vector_iostream& stream);
468
469 LIEF_LOCAL DyldInfo& update_rebase_info(vector_iostream& stream);
470 LIEF_LOCAL DyldInfo& update_binding_info(vector_iostream& stream,
471 details::dyld_info_command& cmd);
472 LIEF_LOCAL DyldInfo& update_export_trie(vector_iostream& stream);
473
474 info_t rebase_;
475 span<uint8_t> rebase_opcodes_;
476
477 info_t bind_;
478 span<uint8_t> bind_opcodes_;
479
480 info_t weak_bind_;
481 span<uint8_t> weak_bind_opcodes_;
482
483 info_t lazy_bind_;
484 span<uint8_t> lazy_bind_opcodes_;
485
486 info_t export_;
487 span<uint8_t> export_trie_;
488
489 export_info_t export_info_;
490 binding_info_t binding_info_;
491
492 BINDING_ENCODING_VERSION binding_encoding_version_ =
493 BINDING_ENCODING_VERSION::UNKNOWN;
494
495 Binary* binary_ = nullptr;
496};
497
502
503
504}
505}
506#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:32
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:35
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:384
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:426
span< const uint8_t > bind_opcodes() const
Return Binding's opcodes as raw data.
Definition DyldInfo.hpp:237
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
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:122
static constexpr auto OPCODE_MASK
Definition DyldInfo.hpp:170
it_const_binding_info bindings() const
Definition DyldInfo.hpp:322
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:436
void set_weak_bind_size(uint32_t size)
Definition DyldInfo.hpp:411
const info_t & export_info() const
Export information
Definition DyldInfo.hpp:352
REBASE_OPCODES
Definition DyldInfo.hpp:95
void set_bind_size(uint32_t size)
Definition DyldInfo.hpp:404
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:232
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
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:401
BIND_SUBOPCODE_THREADED
Definition DyldInfo.hpp:160
DyldInfo(const DyldInfo &copy)
span< uint8_t > lazy_bind_opcodes()
Definition DyldInfo.hpp:307
void set_rebase_offset(uint32_t offset)
Definition DyldInfo.hpp:394
void export_info(const info_t &info)
Definition DyldInfo.hpp:390
BINDING_ENCODING_VERSION
Definition DyldInfo.hpp:82
void set_lazy_bind_size(uint32_t size)
Definition DyldInfo.hpp:418
const info_t & weak_bind() const
Weak Bind information
Definition DyldInfo.hpp:267
span< uint8_t > rebase_opcodes()
Definition DyldInfo.hpp:208
friend class Builder
Definition DyldInfo.hpp:54
void set_export_offset(uint32_t offset)
Definition DyldInfo.hpp:422
it_binding_info bindings()
Iterator over BindingInfo entries.
Definition DyldInfo.hpp:318
span< uint8_t > export_trie()
Definition DyldInfo.hpp:368
void weak_bind_opcodes(buffer_t raw)
Set new opcodes.
it_export_info exports()
Iterator over ExportInfo entries.
Definition DyldInfo.hpp:357
friend class Binary
Definition DyldInfo.hpp:53
void lazy_bind(const info_t &info)
Definition DyldInfo.hpp:387
friend class LinkEdit
Definition DyldInfo.hpp:55
void set_weak_bind_offset(uint32_t offset)
Definition DyldInfo.hpp:408
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:381
std::string show_bind_opcodes() const
Return the bind opcodes in a humman-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:378
span< uint8_t > weak_bind_opcodes()
Definition DyldInfo.hpp:275
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:304
it_const_export_info exports() const
Definition DyldInfo.hpp:360
void set_lazy_bind_offset(uint32_t offset)
Definition DyldInfo.hpp:415
const info_t & lazy_bind() const
Lazy Bind information
Definition DyldInfo.hpp:299
span< uint8_t > bind_opcodes()
Definition DyldInfo.hpp:240
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:365
void swap(DyldInfo &other) noexcept
void accept(Visitor &visitor) const override
void set_rebase_size(uint32_t size)
Definition DyldInfo.hpp:397
span< const uint8_t > weak_bind_opcodes() const
Return Weak Binding's opcodes as raw data.
Definition DyldInfo.hpp:272
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:45
@ DYLD_INFO
Definition LoadCommand.hpp:80
@ DYLD_INFO_ONLY
Definition LoadCommand.hpp:81
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:45
Definition iostream.hpp:32
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: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:286
#define LIEF_API
Definition visibility.h:43
#define LIEF_LOCAL
Definition visibility.h:44