LIEF: Library to Instrument Executable Formats Version 2.0.0
Loading...
Searching...
No Matches
LazyLoadDylibInfo.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_LAZY_LOAD_DYLIB_INFO_COMMAND_H
16#define LIEF_MACHO_LAZY_LOAD_DYLIB_INFO_COMMAND_H
17#include <string_view>
18#include <memory>
19#include <ostream>
20#include <string>
21#include <vector>
22
23#include "LIEF/errors.hpp"
24#include "LIEF/iterators.hpp"
25#include "LIEF/visibility.h"
26
28
29namespace LIEF {
30class SpanStream;
31class BinaryStream;
32class vector_iostream;
33
34namespace MachO {
35class Binary;
36class BinaryParser;
37class Builder;
38class LinkEdit;
39class SegmentCommand;
40
41namespace details {
42struct linkedit_data_command;
43}
44
51 friend class BinaryParser;
52 friend class Builder;
53 friend class LinkEdit;
54
55 public:
59 friend class Binary;
60
61 public:
62 Fixup() = default;
63 Fixup(uint64_t address, uint32_t ordinal, std::string symbol, bool is_auth) :
64 address_(address),
65 ordinal_(ordinal),
66 symbol_(std::move(symbol)),
67 is_auth_(is_auth) {}
68
69 Fixup(const Fixup&) = default;
70 Fixup& operator=(const Fixup&) = default;
71
72 Fixup(Fixup&&) noexcept = default;
73 Fixup& operator=(Fixup&&) noexcept = default;
74
75 ~Fixup() = default;
76
78 uint64_t address() const {
79 return address_;
80 }
81
82 void address(uint64_t value) {
83 address_ = value;
84 }
85
87 uint32_t ordinal() const {
88 return ordinal_;
89 }
90
93 std::string_view symbol() const LIEF_LIFETIMEBOUND {
94 return symbol_;
95 }
96
98 bool is_auth() const {
99 return is_auth_;
100 }
101
102 std::string to_string() const;
103
104 LIEF_API friend std::ostream& operator<<(std::ostream& os,
105 const Fixup& fixup) {
106 os << fixup.to_string();
107 return os;
108 }
109
110 private:
111 uint64_t address_ = 0;
112 uint32_t ordinal_ = 0;
113 std::string symbol_;
114 bool is_auth_ = false;
115 };
116 static constexpr auto MAYBE_MISSING_FLAG = 1;
117
118 using fixups_t = std::vector<Fixup>;
119
122
125
127 LazyLoadDylibInfo(const details::linkedit_data_command& cmd);
128
130 LazyLoadDylibInfo(const LazyLoadDylibInfo& copy) = default;
131
132 std::unique_ptr<LoadCommand> clone() const override {
133 return std::make_unique<LazyLoadDylibInfo>(*this);
134 }
135
137 uint32_t data_offset() const {
138 return data_offset_;
139 }
140
142 uint32_t data_size() const {
143 return data_size_;
144 }
145
146 void data_offset(uint32_t offset) {
147 data_offset_ = offset;
148 }
149
150 void data_size(uint32_t size) {
151 data_size_ = size;
152 }
153
157 return content_;
158 }
159
161 return content_;
162 }
163
165 std::string_view load_path() const LIEF_LIFETIMEBOUND {
166 return load_path_;
167 }
168
171 load_path_ = std::move(value);
172 return *this;
173 }
174
177 uint32_t flag_image_offset() const {
178 return flag_image_offset_;
179 }
180
182 flag_image_offset_ = value;
183 return *this;
184 }
185
187 uint16_t flags() const {
188 return flags_;
189 }
190
192 flags_ = value;
193 return *this;
194 }
195
198 bool may_be_missing() const {
199 return (flags_ & MAYBE_MISSING_FLAG) != 0;
200 }
201
204 if (value) {
205 flags_ |= MAYBE_MISSING_FLAG;
206 } else {
207 flags_ &= ~uint16_t{MAYBE_MISSING_FLAG};
208 }
209 return *this;
210 }
211
214 uint16_t pointer_format() const {
215 return pointer_format_;
216 }
217
219 pointer_format_ = value;
220 return *this;
221 }
222
224 uint32_t chain_start_image_offset() const {
225 return chain_start_image_offset_;
226 }
227
229 chain_start_image_offset_ = value;
230 return *this;
231 }
232
234 const std::vector<std::string>& symbols() const LIEF_LIFETIMEBOUND {
235 return symbols_;
236 }
237
239 LazyLoadDylibInfo& symbols(std::vector<std::string> value) LIEF_LIFETIMEBOUND {
240 symbols_ = std::move(value);
241 return *this;
242 }
243
246 symbols_.push_back(std::move(value));
247 return *this;
248 }
249
252 symbols_.clear();
253 return *this;
254 }
255
258 return fixups_;
259 }
260
262 return fixups_;
263 }
264
265 ~LazyLoadDylibInfo() override = default;
266
267 std::ostream& print(std::ostream& os) const override;
268
269 static bool classof(const LoadCommand* cmd) {
271 }
272
274 LIEF_LOCAL ok_error_t parse_payload(BinaryStream& stream);
275
277 LIEF_LOCAL ok_error_t walk_fixups(uint64_t chain_va, SegmentCommand& seg);
278
280 LIEF_LOCAL ok_error_t serialize(vector_iostream& ios) const;
281
282
283 private:
284 uint32_t data_offset_ = 0;
285 uint32_t data_size_ = 0;
286 span<uint8_t> content_;
287
288 std::string load_path_;
289 uint32_t flag_image_offset_ = 0;
290 uint16_t flags_ = 0;
291 uint16_t pointer_format_ = 0;
292 uint32_t chain_start_image_offset_ = 0;
293 std::vector<std::string> symbols_;
294 fixups_t fixups_;
295};
296
297}
298}
299#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
Class used to rebuild a Mach-O file.
Definition MachO/Builder.hpp:63
uint64_t address() const
Virtual address of the slot bound by this fixup.
Definition LazyLoadDylibInfo.hpp:78
bool is_auth() const
Whether the bound pointer is authenticated (arm64e PAC).
Definition LazyLoadDylibInfo.hpp:98
uint32_t ordinal() const
Index of the bound symbol in the symbols table of LazyLoadDylibInfo.
Definition LazyLoadDylibInfo.hpp:87
Fixup & operator=(const Fixup &)=default
Fixup(Fixup &&) noexcept=default
std::string_view symbol() const
Name of the bound symbol, resolved from ordinal() (empty if the ordinal is out of the symbols() range...
Definition LazyLoadDylibInfo.hpp:93
friend class Binary
Definition LazyLoadDylibInfo.hpp:59
friend std::ostream & operator<<(std::ostream &os, const Fixup &fixup)
Definition LazyLoadDylibInfo.hpp:104
void address(uint64_t value)
Definition LazyLoadDylibInfo.hpp:82
Fixup(uint64_t address, uint32_t ordinal, std::string symbol, bool is_auth)
Definition LazyLoadDylibInfo.hpp:63
uint32_t flag_image_offset() const
Image offset of the global flag that is set once the dylib has been loaded by dyld.
Definition LazyLoadDylibInfo.hpp:177
static constexpr auto MAYBE_MISSING_FLAG
Definition LazyLoadDylibInfo.hpp:116
const std::vector< std::string > & symbols() const
List of the symbol names to bind lazily for this dylib.
Definition LazyLoadDylibInfo.hpp:234
void data_offset(uint32_t offset)
Definition LazyLoadDylibInfo.hpp:146
LazyLoadDylibInfo(const details::linkedit_data_command &cmd)
span< uint8_t > content()
Definition LazyLoadDylibInfo.hpp:160
LazyLoadDylibInfo & flag_image_offset(uint32_t value)
Definition LazyLoadDylibInfo.hpp:181
uint16_t flags() const
Raw flags associated with this command.
Definition LazyLoadDylibInfo.hpp:187
LazyLoadDylibInfo & operator=(const LazyLoadDylibInfo &copy)=default
LazyLoadDylibInfo(const LazyLoadDylibInfo &copy)=default
friend class BinaryParser
Definition LazyLoadDylibInfo.hpp:51
uint32_t data_offset() const
Offset in the __LINKEDIT segment where the payload starts.
Definition LazyLoadDylibInfo.hpp:137
it_fixups fixups()
Iterator over the lazy-binding Fixup entries.
Definition LazyLoadDylibInfo.hpp:257
span< const uint8_t > content() const
Return the data slice in the __LINKEDIT segment referenced by data_offset and data_size.
Definition LazyLoadDylibInfo.hpp:156
static bool classof(const LoadCommand *cmd)
Definition LazyLoadDylibInfo.hpp:269
it_const_fixups fixups() const
Definition LazyLoadDylibInfo.hpp:261
bool may_be_missing() const
Whether the dylib is allowed to be missing at runtime (i.e. "weaklinked").
Definition LazyLoadDylibInfo.hpp:198
LazyLoadDylibInfo & flags(uint16_t value)
Definition LazyLoadDylibInfo.hpp:191
friend class Builder
Definition LazyLoadDylibInfo.hpp:52
ref_iterator< fixups_t & > it_fixups
Iterator that outputs Fixup&.
Definition LazyLoadDylibInfo.hpp:121
LazyLoadDylibInfo & add_symbol(std::string value)
Append a symbol name to the list of symbols to bind lazily.
Definition LazyLoadDylibInfo.hpp:245
std::unique_ptr< LoadCommand > clone() const override
Definition LazyLoadDylibInfo.hpp:132
~LazyLoadDylibInfo() override=default
std::ostream & print(std::ostream &os) const override
friend class LinkEdit
Definition LazyLoadDylibInfo.hpp:53
LazyLoadDylibInfo & pointer_format(uint16_t value)
Definition LazyLoadDylibInfo.hpp:218
LazyLoadDylibInfo & chain_start_image_offset(uint32_t value)
Definition LazyLoadDylibInfo.hpp:228
LazyLoadDylibInfo & symbols(std::vector< std::string > value)
Replace the list of the symbol names to bind lazily for this dylib.
Definition LazyLoadDylibInfo.hpp:239
std::vector< Fixup > fixups_t
Definition LazyLoadDylibInfo.hpp:118
void data_size(uint32_t size)
Definition LazyLoadDylibInfo.hpp:150
uint16_t pointer_format() const
Chained-fixups pointer format used by the binding chain (e.g. DYLD_CHAINED_PTR_ARM64E_USERLAND).
Definition LazyLoadDylibInfo.hpp:214
LazyLoadDylibInfo & clear_symbols()
Remove all the symbol names to bind lazily.
Definition LazyLoadDylibInfo.hpp:251
uint32_t data_size() const
Size of the payload.
Definition LazyLoadDylibInfo.hpp:142
std::string_view load_path() const
Load path of the dylib to bind lazily.
Definition LazyLoadDylibInfo.hpp:165
LazyLoadDylibInfo & load_path(std::string value)
Change the load path of the dylib to bind lazily.
Definition LazyLoadDylibInfo.hpp:170
const_ref_iterator< const fixups_t & > it_const_fixups
Iterator that outputs const Fixup&.
Definition LazyLoadDylibInfo.hpp:124
LazyLoadDylibInfo & may_be_missing(bool value)
Set or clear the "may be missing" (weak linked) bit of flags().
Definition LazyLoadDylibInfo.hpp:203
uint32_t chain_start_image_offset() const
Image offset of the fixup chain start used to bind the dylib's symbols.
Definition LazyLoadDylibInfo.hpp:224
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
@ LAZY_LOAD_DYLIB_INFO
Definition LoadCommand.hpp:106
Class which represents a LoadCommand::TYPE::SEGMENT / LoadCommand::TYPE::SEGMENT_64 command.
Definition SegmentCommand.hpp:53
Definition SpanStream.hpp:31
Opaque structure that is used by LIEF to avoid writing result<void> f(...). Instead,...
Definition errors.hpp:118
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
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