LIEF: Library to Instrument Executable Formats Version 2.0.0
Loading...
Searching...
No Matches
DyldChainedFixupsCreator.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_CHAINED_FIXUPS_CREATOR_H
17#define LIEF_MACHO_DYLD_CHAINED_FIXUPS_CREATOR_H
20#include "LIEF/errors.hpp"
21#include "LIEF/visibility.h"
22
23#include <string_view>
24#include <unordered_map>
25#include <cstdint>
26#include <string>
27#include <vector>
28
29
30namespace LIEF::MachO {
31class RelocationFixup;
32class Symbol;
33class Binary;
35class SegmentCommand;
36class ChainedBindingInfoList;
37
38class strong_map_t;
39
40// This class aims to provide facilities to (re)create the LC_DYLD_CHAINED_FIXUPS
41// command.
43 public:
44 static constexpr uint32_t MAX_IMPORTS = (uint32_t(1) << 24) - 1;
45 static constexpr uint32_t BIND24_THRESHOLD = (uint32_t(1) << 16) - 1;
47 uint64_t address = 0;
48 std::string library;
49 std::string symbol;
50 bool weak;
51 uint64_t addend;
52 };
53
54 struct reloc_info_t {
55 uint64_t addr = 0;
56 uint64_t target = 0;
57 };
59
61 fixups_version_ = value;
62 return *this;
63 }
64
66 imports_format_ = fmt;
67 return *this;
68 }
69
70 DyldChainedFixupsCreator& add_relocation(uint64_t address, uint64_t target);
71
73 add_relocations(const std::vector<reloc_info_t>& relocations) {
74 std::copy(relocations.begin(), relocations.end(),
75 std::back_inserter(relocations_));
76 return *this;
77 }
78
79 DyldChainedFixupsCreator& add_binding(uint64_t address, std::string symbol,
80 std::string library, uint64_t addend = 0,
81 bool weak = false);
82
83 DyldChainedFixupsCreator& add_binding(uint64_t address, std::string symbol,
84 uint64_t addend = 0, bool weak = false) {
85 return add_binding(address, std::move(symbol), "", addend, weak);
86 }
87
89 add_bindings(const std::vector<binding_info_t>& bindings) {
90 std::copy(bindings.begin(), bindings.end(), std::back_inserter(bindings_));
91 return *this;
92 }
93
95
96 private:
97 struct LIEF_LOCAL binding_rebase_t {
98 enum TYPE {
99 UNKNOWN = 0,
100 FIXUP,
101 BINDING,
102 };
103 union {
104 ChainedBindingInfo* binding = nullptr;
105 RelocationFixup* fixup;
106 };
107 TYPE type = UNKNOWN;
108
109 binding_rebase_t(ChainedBindingInfo& info) :
110 binding(&info),
111 type(BINDING) {}
112
113 binding_rebase_t(RelocationFixup& fixup) :
114 fixup(&fixup),
115 type(FIXUP) {}
116
117 uint64_t addr() const;
118
119 bool is_binding() const {
120 return type == BINDING;
121 }
122
123 bool is_fixup() const {
124 return type == FIXUP;
125 }
126
127 friend bool operator<(const binding_rebase_t& lhs,
128 const binding_rebase_t& rhs) {
129 return lhs.addr() < rhs.addr();
130 }
131
132 friend bool operator>(const binding_rebase_t& lhs,
133 const binding_rebase_t& rhs) {
134 return lhs.addr() > rhs.addr();
135 }
136
137 friend bool operator>=(const binding_rebase_t& lhs,
138 const binding_rebase_t& rhs) {
139 return !(lhs < rhs);
140 }
141
142 friend bool operator<=(const binding_rebase_t& lhs,
143 const binding_rebase_t& rhs) {
144 return !(lhs > rhs);
145 }
146 };
147 LIEF_LOCAL result<size_t> lib2ord(const Binary& bin, const Symbol& sym,
148 const std::string& lib);
149 LIEF_LOCAL const Symbol* find_symbol(const Binary& bin, std::string_view name);
150
151 LIEF_LOCAL static DYLD_CHAINED_PTR_FORMAT pointer_format(const Binary& bin,
152 size_t imp_count);
153 LIEF_LOCAL ok_error_t process_relocations(Binary& target,
154 DYLD_CHAINED_PTR_FORMAT ptr_fmt);
155 LIEF_LOCAL DyldChainedFixups::binding_info_t
156 process_bindings(Binary& target, strong_map_t& strong_map,
157 std::unordered_map<std::string, size_t>& symbols_idx,
158 DyldChainedFixups* cmd);
159
160 uint32_t fixups_version_ = 0;
161 DYLD_CHAINED_FORMAT imports_format_ = DYLD_CHAINED_FORMAT::IMPORT;
162 std::vector<binding_info_t> bindings_;
163 std::vector<reloc_info_t> relocations_;
164 std::unordered_map<SegmentCommand*, std::vector<binding_rebase_t>>
165 segment_chains_;
166 std::unordered_map<std::string, size_t> lib2ord_;
167};
168}
169
170#endif
Class which represents a MachO binary.
Definition MachO/Binary.hpp:94
This class represents a symbol binding operation associated with the LC_DYLD_CHAINED_FIXUPS command.
Definition ChainedBindingInfo.hpp:48
Definition DyldChainedFixupsCreator.hpp:42
static constexpr uint32_t MAX_IMPORTS
Definition DyldChainedFixupsCreator.hpp:44
DyldChainedFixupsCreator & fixups_version(uint32_t value)
Definition DyldChainedFixupsCreator.hpp:60
DyldChainedFixups * create(Binary &target)
DyldChainedFixupsCreator & add_relocations(const std::vector< reloc_info_t > &relocations)
Definition DyldChainedFixupsCreator.hpp:73
static constexpr uint32_t BIND24_THRESHOLD
Definition DyldChainedFixupsCreator.hpp:45
DyldChainedFixupsCreator & add_bindings(const std::vector< binding_info_t > &bindings)
Definition DyldChainedFixupsCreator.hpp:89
DyldChainedFixupsCreator & add_relocation(uint64_t address, uint64_t target)
DyldChainedFixupsCreator & add_binding(uint64_t address, std::string symbol, std::string library, uint64_t addend=0, bool weak=false)
DyldChainedFixupsCreator & add_binding(uint64_t address, std::string symbol, uint64_t addend=0, bool weak=false)
Definition DyldChainedFixupsCreator.hpp:83
DyldChainedFixupsCreator & imports_format(DYLD_CHAINED_FORMAT fmt)
Definition DyldChainedFixupsCreator.hpp:65
Class that represents the LC_DYLD_CHAINED_FIXUPS command.
Definition DyldChainedFixups.hpp:50
Class that represents a rebase relocation found in the LC_DYLD_CHAINED_FIXUPS command.
Definition RelocationFixup.hpp:55
Class which represents a LoadCommand::TYPE::SEGMENT / LoadCommand::TYPE::SEGMENT_64 command.
Definition SegmentCommand.hpp:54
Class that represents a Symbol in a Mach-O file.
Definition MachO/Symbol.hpp:47
Namespace related to the LIEF's Mach-O module.
Definition Abstract/Header.hpp:36
DYLD_CHAINED_FORMAT
Definition DyldChainedFormat.hpp:22
@ UNKNOWN
Definition MachO/enums.hpp:25
DYLD_CHAINED_PTR_FORMAT
Definition DyldChainedFormat.hpp:34
Definition DyldChainedFixupsCreator.hpp:46
std::string library
Definition DyldChainedFixupsCreator.hpp:48
uint64_t address
Definition DyldChainedFixupsCreator.hpp:47
uint64_t addend
Definition DyldChainedFixupsCreator.hpp:51
bool weak
Definition DyldChainedFixupsCreator.hpp:50
std::string symbol
Definition DyldChainedFixupsCreator.hpp:49
Definition DyldChainedFixupsCreator.hpp:54
uint64_t target
Definition DyldChainedFixupsCreator.hpp:56
uint64_t addr
Definition DyldChainedFixupsCreator.hpp:55
#define LIEF_LOCAL
Definition visibility.h:46