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 - 2024 R. Thomas
2 * Copyright 2017 - 2024 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}
48class LIEF_API DyldInfo : public LoadCommand {
51
52 friend class BinaryParser;
53 friend class Binary;
54 friend class Builder;
55 friend class LinkEdit;
56 friend class BindingInfoIterator;
57
58 public: using info_t = std::pair<uint32_t, uint32_t>;
61 using binding_info_t = std::vector<std::unique_ptr<DyldBindingInfo>>;
64 using it_binding_info = ref_iterator<binding_info_t&, DyldBindingInfo*>;
67 using it_const_binding_info = const_ref_iterator<const binding_info_t&, DyldBindingInfo*>;
70 using export_info_t = std::vector<std::unique_ptr<ExportInfo>>;
73 using it_export_info = ref_iterator<export_info_t&, ExportInfo*>;
76 using it_const_export_info = const_ref_iterator<const export_info_t&, ExportInfo*>;
79
80 enum class BINDING_ENCODING_VERSION {
82 V1,
83 V2
84 };
85
86 enum class REBASE_TYPE: uint64_t {
87 POINTER = 1u,
88 TEXT_ABSOLUTE32 = 2u,
89 TEXT_PCREL32 = 3u,
90 THREADED = 102u,
91 };
92
93 enum class REBASE_OPCODES: uint8_t {
94 DONE = 0x00u,
95 SET_TYPE_IMM = 0x10u,
96 SET_SEGMENT_AND_OFFSET_ULEB = 0x20u,
97 ADD_ADDR_ULEB = 0x30u,
98 ADD_ADDR_IMM_SCALED = 0x40u,
99 DO_REBASE_IMM_TIMES = 0x50u,
100 DO_REBASE_ULEB_TIMES = 0x60u,
101 DO_REBASE_ADD_ADDR_ULEB = 0x70u,
102 DO_REBASE_ULEB_TIMES_SKIPPING_ULEB = 0x80u
103 };
104 enum class BIND_OPCODES: uint8_t {
107 DONE = 0x00u,
108 SET_DYLIB_ORDINAL_IMM = 0x10u,
109 SET_DYLIB_ORDINAL_ULEB = 0x20u,
110 SET_DYLIB_SPECIAL_IMM = 0x30u,
111 SET_SYMBOL_TRAILING_FLAGS_IMM = 0x40u,
112 SET_TYPE_IMM = 0x50u,
113 SET_ADDEND_SLEB = 0x60u,
114 SET_SEGMENT_AND_OFFSET_ULEB = 0x70u,
115 ADD_ADDR_ULEB = 0x80u,
116 DO_BIND = 0x90u,
117 DO_BIND_ADD_ADDR_ULEB = 0xA0u,
118 DO_BIND_ADD_ADDR_IMM_SCALED = 0xB0u,
119 DO_BIND_ULEB_TIMES_SKIPPING_ULEB = 0xC0u,
120 THREADED = 0xD0u,
121
122 THREADED_APPLY = 0xD0u | 0x01u,
123 THREADED_SET_BIND_ORDINAL_TABLE_SIZE_ULEB = 0xD0u | 0x00u,
124 };
125
126 enum class BIND_SUBOPCODE_THREADED: uint8_t {
127 SET_BIND_ORDINAL_TABLE_SIZE_ULEB = 0x00u,
128 APPLY = 0x01u,
129 };
130
131 enum BIND_SYMBOL_FLAGS {
132 WEAK_IMPORT = 0x1u,
133 NON_WEAK_DEFINITION = 0x8u,
134 };
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
142 DyldInfo& operator=(DyldInfo other);
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 const info_t& rebase() const {
167 return rebase_;
168 }
169 span<const uint8_t> rebase_opcodes() const {
172 return rebase_opcodes_;
173 }
174 span<uint8_t> rebase_opcodes() {
175 return rebase_opcodes_;
176 }
177 void rebase_opcodes(buffer_t raw);
180 std::string show_rebases_opcodes() const;
183 const info_t& bind() const {
199 return bind_;
200 }
201 span<const uint8_t> bind_opcodes() const {
204 return bind_opcodes_;
205 }
206 span<uint8_t> bind_opcodes() {
207 return bind_opcodes_;
208 }
209 void bind_opcodes(buffer_t raw);
212 std::string show_bind_opcodes() const;
215 const info_t& weak_bind() const {
234 return weak_bind_;
235 }
236 span<const uint8_t> weak_bind_opcodes() const {
239 return weak_bind_opcodes_;
240 }
241 span<uint8_t> weak_bind_opcodes() {
242 return weak_bind_opcodes_;
243 }
244 void weak_bind_opcodes(buffer_t raw);
247 std::string show_weak_bind_opcodes() const;
250 const info_t& lazy_bind() const {
266 return lazy_bind_;
267 }
268 span<const uint8_t> lazy_bind_opcodes() const {
271 return lazy_bind_opcodes_;
272 }
273 span<uint8_t> lazy_bind_opcodes() {
274 return lazy_bind_opcodes_;
275 }
276 void lazy_bind_opcodes(buffer_t raw);
279 std::string show_lazy_bind_opcodes() const;
282 it_binding_info bindings() {
285 return binding_info_;
286 }
287
288 it_const_binding_info bindings() const {
289 return binding_info_;
290 }
291 const info_t& export_info() const {
319 return export_;
320 }
321 it_export_info exports() {
324 return export_info_;
325 }
326 it_const_export_info exports() const {
327 return export_info_;
328 }
329 span<const uint8_t> export_trie() const {
332 return export_trie_;
333 }
334 span<uint8_t> export_trie() {
335 return export_trie_;
336 }
337 void export_trie(buffer_t raw);
340 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 ||
405 type == LoadCommand::TYPE::DYLD_INFO_ONLY;
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
452LIEF_API const char* to_string(DyldInfo::REBASE_TYPE e);
453LIEF_API const char* to_string(DyldInfo::REBASE_OPCODES e);
454LIEF_API const char* to_string(DyldInfo::BIND_OPCODES e);
455LIEF_API const char* to_string(DyldInfo::BIND_SUBOPCODE_THREADED e);
456
457
458}
459}
460#endif
LoadCommand.hpp
type_traits.hpp
LIEF::BinaryStream
Class that is used to a read stream of data from different sources.
Definition BinaryStream.hpp:33
LIEF::MachO::BinaryParser
Class used to parse a single binary (i.e. non-FAT)
Definition BinaryParser.hpp:75
LIEF::MachO::Binary
Class which represents a MachO binary.
Definition MachO/Binary.hpp:86
LIEF::MachO::BindingInfoIterator
Definition BindingInfoIterator.hpp:36
LIEF::MachO::Builder
Class used to rebuild a Mach-O file.
Definition MachO/Builder.hpp:58
LIEF::MachO::DyldBindingInfo
This class represents a symbol binding operation associated with the LC_DYLD_INFO bytecode.
Definition DyldBindingInfo.hpp:34
LIEF::MachO::DyldInfo
Class that represents the LC_DYLD_INFO and LC_DYLD_INFO_ONLY commands.
Definition DyldInfo.hpp:50
LIEF::MachO::DyldInfo::weak_bind
void weak_bind(const info_t &info)
Definition DyldInfo.hpp:350
LIEF::MachO::DyldInfo::print
std::ostream & print(std::ostream &os) const override
LIEF::MachO::DyldInfo::set_export_size
void set_export_size(uint32_t size)
Definition DyldInfo.hpp:392
LIEF::MachO::DyldInfo::bind_opcodes
span< const uint8_t > bind_opcodes() const
Return Binding's opcodes as raw data.
Definition DyldInfo.hpp:203
LIEF::MachO::DyldInfo::~DyldInfo
~DyldInfo() override
LIEF::MachO::DyldInfo::add
void add(std::unique_ptr< ExportInfo > info)
LIEF::MachO::DyldInfo::show_lazy_bind_opcodes
std::string show_lazy_bind_opcodes() const
Return the lazy opcodes in a humman-readable way.
LIEF::MachO::DyldInfo::BIND_OPCODES
BIND_OPCODES
Opcodes used by Dyld info to bind symbols.
Definition DyldInfo.hpp:106
LIEF::MachO::DyldInfo::bindings
it_const_binding_info bindings() const
Definition DyldInfo.hpp:288
LIEF::MachO::DyldInfo::DyldInfo
DyldInfo(const details::dyld_info_command &dyld_info_cmd)
LIEF::MachO::DyldInfo::rebase_opcodes
span< const uint8_t > rebase_opcodes() const
Return Rebase's opcodes as raw data.
Definition DyldInfo.hpp:171
LIEF::MachO::DyldInfo::lazy_bind_opcodes
void lazy_bind_opcodes(buffer_t raw)
Set new opcodes.
LIEF::MachO::DyldInfo::classof
static bool classof(const LoadCommand *cmd)
Definition DyldInfo.hpp:402
LIEF::MachO::DyldInfo::set_weak_bind_size
void set_weak_bind_size(uint32_t size)
Definition DyldInfo.hpp:377
LIEF::MachO::DyldInfo::export_info
const info_t & export_info() const
Export information
Definition DyldInfo.hpp:318
LIEF::MachO::DyldInfo::REBASE_OPCODES
REBASE_OPCODES
Definition DyldInfo.hpp:93
LIEF::MachO::DyldInfo::set_bind_size
void set_bind_size(uint32_t size)
Definition DyldInfo.hpp:370
LIEF::MachO::DyldInfo::show_export_trie
std::string show_export_trie() const
Return the export trie in a humman-readable way.
LIEF::MachO::DyldInfo::bind
const info_t & bind() const
Bind information
Definition DyldInfo.hpp:198
LIEF::MachO::DyldInfo::clone
std::unique_ptr< LoadCommand > clone() const override
Definition DyldInfo.hpp:147
LIEF::MachO::DyldInfo::DyldInfo
DyldInfo()
LIEF::MachO::DyldInfo::rebase
const info_t & rebase() const
Rebase information
Definition DyldInfo.hpp:166
LIEF::MachO::DyldInfo::operator=
DyldInfo & operator=(DyldInfo other)
LIEF::MachO::DyldInfo::rebase_opcodes
void rebase_opcodes(buffer_t raw)
Set new opcodes.
LIEF::MachO::DyldInfo::set_bind_offset
void set_bind_offset(uint32_t offset)
Definition DyldInfo.hpp:367
LIEF::MachO::DyldInfo::BIND_SUBOPCODE_THREADED
BIND_SUBOPCODE_THREADED
Definition DyldInfo.hpp:126
LIEF::MachO::DyldInfo::DyldInfo
DyldInfo(const DyldInfo &copy)
LIEF::MachO::DyldInfo::lazy_bind_opcodes
span< uint8_t > lazy_bind_opcodes()
Definition DyldInfo.hpp:273
LIEF::MachO::DyldInfo::set_rebase_offset
void set_rebase_offset(uint32_t offset)
Definition DyldInfo.hpp:360
LIEF::MachO::DyldInfo::export_info
void export_info(const info_t &info)
Definition DyldInfo.hpp:356
LIEF::MachO::DyldInfo::set_lazy_bind_size
void set_lazy_bind_size(uint32_t size)
Definition DyldInfo.hpp:384
LIEF::MachO::DyldInfo::weak_bind
const info_t & weak_bind() const
Weak Bind information
Definition DyldInfo.hpp:233
LIEF::MachO::DyldInfo::rebase_opcodes
span< uint8_t > rebase_opcodes()
Definition DyldInfo.hpp:174
LIEF::MachO::DyldInfo::set_export_offset
void set_export_offset(uint32_t offset)
Definition DyldInfo.hpp:388
LIEF::MachO::DyldInfo::bindings
it_binding_info bindings()
Iterator over BindingInfo entries.
Definition DyldInfo.hpp:284
LIEF::MachO::DyldInfo::export_trie
span< uint8_t > export_trie()
Definition DyldInfo.hpp:334
LIEF::MachO::DyldInfo::weak_bind_opcodes
void weak_bind_opcodes(buffer_t raw)
Set new opcodes.
LIEF::MachO::DyldInfo::exports
it_export_info exports()
Iterator over ExportInfo entries.
Definition DyldInfo.hpp:323
LIEF::MachO::DyldInfo::lazy_bind
void lazy_bind(const info_t &info)
Definition DyldInfo.hpp:353
LIEF::MachO::DyldInfo::set_weak_bind_offset
void set_weak_bind_offset(uint32_t offset)
Definition DyldInfo.hpp:374
LIEF::MachO::DyldInfo::bind_opcodes
void bind_opcodes(buffer_t raw)
Set new opcodes.
LIEF::MachO::DyldInfo::REBASE_TYPE
REBASE_TYPE
Definition DyldInfo.hpp:86
LIEF::MachO::DyldInfo::bind
void bind(const info_t &info)
Definition DyldInfo.hpp:347
LIEF::MachO::DyldInfo::show_bind_opcodes
std::string show_bind_opcodes() const
Return the bind opcodes in a humman-readable way.
LIEF::MachO::DyldInfo::rebase
void rebase(const info_t &info)
Definition DyldInfo.hpp:344
LIEF::MachO::DyldInfo::weak_bind_opcodes
span< uint8_t > weak_bind_opcodes()
Definition DyldInfo.hpp:241
LIEF::MachO::DyldInfo::show_rebases_opcodes
std::string show_rebases_opcodes() const
Return the rebase opcodes in a humman-readable way.
LIEF::MachO::DyldInfo::export_trie
void export_trie(buffer_t raw)
Set new trie.
LIEF::MachO::DyldInfo::lazy_bind_opcodes
span< const uint8_t > lazy_bind_opcodes() const
Return Lazy Binding's opcodes as raw data.
Definition DyldInfo.hpp:270
LIEF::MachO::DyldInfo::exports
it_const_export_info exports() const
Definition DyldInfo.hpp:326
LIEF::MachO::DyldInfo::set_lazy_bind_offset
void set_lazy_bind_offset(uint32_t offset)
Definition DyldInfo.hpp:381
LIEF::MachO::DyldInfo::lazy_bind
const info_t & lazy_bind() const
Lazy Bind information
Definition DyldInfo.hpp:265
LIEF::MachO::DyldInfo::bind_opcodes
span< uint8_t > bind_opcodes()
Definition DyldInfo.hpp:206
LIEF::MachO::DyldInfo::export_trie
span< const uint8_t > export_trie() const
Return Export's trie as raw data.
Definition DyldInfo.hpp:331
LIEF::MachO::DyldInfo::swap
void swap(DyldInfo &other) noexcept
LIEF::MachO::DyldInfo::accept
void accept(Visitor &visitor) const override
LIEF::MachO::DyldInfo::set_rebase_size
void set_rebase_size(uint32_t size)
Definition DyldInfo.hpp:363
LIEF::MachO::DyldInfo::weak_bind_opcodes
span< const uint8_t > weak_bind_opcodes() const
Return Weak Binding's opcodes as raw data.
Definition DyldInfo.hpp:238
LIEF::MachO::DyldInfo::show_weak_bind_opcodes
std::string show_weak_bind_opcodes() const
Return the bind opcodes in a humman-readable way.
LIEF::MachO::ExportInfo
Class that provides an interface over the Dyld export info.
Definition ExportInfo.hpp:38
LIEF::MachO::LinkEdit
Definition LinkEdit.hpp:43
LIEF::MachO::LoadCommand
Based class for the Mach-O load commands.
Definition LoadCommand.hpp:37
LIEF::MachO::LoadCommand::command
LoadCommand::TYPE command() const
Command type.
Definition LoadCommand.hpp:125
LIEF::MachO::RelocationDyld
Class that represents a relocation found in the DyldInfo structure.
Definition RelocationDyld.hpp:33
LIEF::vector_iostream
Definition iostream.hpp:29
iterators.hpp
LIEF::MachO::details
Definition endianness_support.hpp:59
LIEF::MachO
Namespace related to the LIEF's Mach-O module.
Definition Abstract/Header.hpp:36
LIEF::MachO::MACHO_TYPES::UNKNOWN
@ UNKNOWN
Definition MachO/enums.hpp:25
LIEF::MachO::to_string
const char * to_string(BuildToolVersion::TOOLS tool)
LIEF
LIEF namespace.
Definition Abstract/Binary.hpp:36
span.hpp
visibility.h
LIEF_API
#define LIEF_API
Definition visibility.h:41
LIEF_LOCAL
#define LIEF_LOCAL
Definition visibility.h:42