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