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 * 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_view>
19#include <memory>
20#include <ostream>
21#include <string>
22#include <vector>
23
24#include "LIEF/errors.hpp"
25#include "LIEF/iterators.hpp"
26#include "LIEF/visibility.h"
27
29
30namespace LIEF {
31class SpanStream;
32class BinaryStream;
33class vector_iostream;
34
35namespace MachO {
36class Binary;
37class BinaryParser;
38class Builder;
39class LinkEdit;
40class SegmentCommand;
41
42namespace details {
43struct linkedit_data_command;
44}
45
52 friend class BinaryParser;
53 friend class Builder;
54 friend class LinkEdit;
55
56 public:
60 friend class Binary;
61
62 public:
63 Fixup() = default;
64 Fixup(uint64_t address, uint32_t ordinal, std::string symbol, bool is_auth) :
65 address_(address),
66 ordinal_(ordinal),
67 symbol_(std::move(symbol)),
68 is_auth_(is_auth) {}
69
70 Fixup(const Fixup&) = default;
71 Fixup& operator=(const Fixup&) = default;
72
73 Fixup(Fixup&&) noexcept = default;
74 Fixup& operator=(Fixup&&) noexcept = default;
75
76 ~Fixup() = default;
77
79 uint64_t address() const {
80 return address_;
81 }
82
83 void address(uint64_t value) {
84 address_ = value;
85 }
86
88 uint32_t ordinal() const {
89 return ordinal_;
90 }
91
94 std::string_view symbol() const LIEF_LIFETIMEBOUND {
95 return symbol_;
96 }
97
99 bool is_auth() const {
100 return is_auth_;
101 }
102
103 std::string to_string() const;
104
105 LIEF_API friend std::ostream& operator<<(std::ostream& os,
106 const Fixup& fixup) {
107 os << fixup.to_string();
108 return os;
109 }
110
111 private:
112 uint64_t address_ = 0;
113 uint32_t ordinal_ = 0;
114 std::string symbol_;
115 bool is_auth_ = false;
116 };
117 static constexpr auto MAYBE_MISSING_FLAG = 1;
118
119 using fixups_t = std::vector<Fixup>;
120
123
126
128 LazyLoadDylibInfo(const details::linkedit_data_command& cmd);
129
131 LazyLoadDylibInfo(const LazyLoadDylibInfo& copy) = default;
132
133 std::unique_ptr<LoadCommand> clone() const override {
134 return std::make_unique<LazyLoadDylibInfo>(*this);
135 }
136
138 uint32_t data_offset() const {
139 return data_offset_;
140 }
141
143 uint32_t data_size() const {
144 return data_size_;
145 }
146
147 void data_offset(uint32_t offset) {
148 data_offset_ = offset;
149 }
150
151 void data_size(uint32_t size) {
152 data_size_ = size;
153 }
154
158 return content_;
159 }
160
162 return content_;
163 }
164
166 std::string_view load_path() const LIEF_LIFETIMEBOUND {
167 return load_path_;
168 }
169
172 load_path_ = std::move(value);
173 return *this;
174 }
175
178 uint32_t flag_image_offset() const {
179 return flag_image_offset_;
180 }
181
183 flag_image_offset_ = value;
184 return *this;
185 }
186
188 uint16_t flags() const {
189 return flags_;
190 }
191
193 flags_ = value;
194 return *this;
195 }
196
199 bool may_be_missing() const {
200 return (flags_ & MAYBE_MISSING_FLAG) != 0;
201 }
202
205 if (value) {
206 flags_ |= MAYBE_MISSING_FLAG;
207 } else {
208 flags_ &= ~static_cast<uint16_t>(MAYBE_MISSING_FLAG);
209 }
210 return *this;
211 }
212
215 uint16_t pointer_format() const {
216 return pointer_format_;
217 }
218
220 pointer_format_ = value;
221 return *this;
222 }
223
225 uint32_t chain_start_image_offset() const {
226 return chain_start_image_offset_;
227 }
228
230 chain_start_image_offset_ = value;
231 return *this;
232 }
233
235 const std::vector<std::string>& symbols() const LIEF_LIFETIMEBOUND {
236 return symbols_;
237 }
238
240 LazyLoadDylibInfo& symbols(std::vector<std::string> value) LIEF_LIFETIMEBOUND {
241 symbols_ = std::move(value);
242 return *this;
243 }
244
247 symbols_.push_back(std::move(value));
248 return *this;
249 }
250
253 symbols_.clear();
254 return *this;
255 }
256
259 return fixups_;
260 }
261
263 return fixups_;
264 }
265
266 ~LazyLoadDylibInfo() override = default;
267
268 std::ostream& print(std::ostream& os) const override;
269
270 static bool classof(const LoadCommand* cmd) {
272 }
273
275 LIEF_LOCAL ok_error_t parse_payload(BinaryStream& stream);
276
278 LIEF_LOCAL ok_error_t walk_fixups(uint64_t chain_va, SegmentCommand& seg);
279
281 LIEF_LOCAL ok_error_t serialize(vector_iostream& ios) const;
282
283
284 private:
285 uint32_t data_offset_ = 0;
286 uint32_t data_size_ = 0;
287 span<uint8_t> content_;
288
289 std::string load_path_;
290 uint32_t flag_image_offset_ = 0;
291 uint16_t flags_ = 0;
292 uint16_t pointer_format_ = 0;
293 uint32_t chain_start_image_offset_ = 0;
294 std::vector<std::string> symbols_;
295 fixups_t fixups_;
296};
297
298}
299}
300#endif
Class that is used to a read stream of data from different sources.
Definition BinaryStream.hpp:35
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:94
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:79
bool is_auth() const
Whether the bound pointer is authenticated (arm64e PAC).
Definition LazyLoadDylibInfo.hpp:99
uint32_t ordinal() const
Index of the bound symbol in the symbols table of LazyLoadDylibInfo.
Definition LazyLoadDylibInfo.hpp:88
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:94
friend class Binary
Definition LazyLoadDylibInfo.hpp:60
friend std::ostream & operator<<(std::ostream &os, const Fixup &fixup)
Definition LazyLoadDylibInfo.hpp:105
void address(uint64_t value)
Definition LazyLoadDylibInfo.hpp:83
Fixup(uint64_t address, uint32_t ordinal, std::string symbol, bool is_auth)
Definition LazyLoadDylibInfo.hpp:64
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:178
static constexpr auto MAYBE_MISSING_FLAG
Definition LazyLoadDylibInfo.hpp:117
const std::vector< std::string > & symbols() const
List of the symbol names to bind lazily for this dylib.
Definition LazyLoadDylibInfo.hpp:235
void data_offset(uint32_t offset)
Definition LazyLoadDylibInfo.hpp:147
LazyLoadDylibInfo(const details::linkedit_data_command &cmd)
span< uint8_t > content()
Definition LazyLoadDylibInfo.hpp:161
LazyLoadDylibInfo & flag_image_offset(uint32_t value)
Definition LazyLoadDylibInfo.hpp:182
uint16_t flags() const
Raw flags associated with this command.
Definition LazyLoadDylibInfo.hpp:188
LazyLoadDylibInfo & operator=(const LazyLoadDylibInfo &copy)=default
LazyLoadDylibInfo(const LazyLoadDylibInfo &copy)=default
friend class BinaryParser
Definition LazyLoadDylibInfo.hpp:52
uint32_t data_offset() const
Offset in the __LINKEDIT segment where the payload starts.
Definition LazyLoadDylibInfo.hpp:138
it_fixups fixups()
Iterator over the lazy-binding Fixup entries.
Definition LazyLoadDylibInfo.hpp:258
span< const uint8_t > content() const
Return the data slice in the __LINKEDIT segment referenced by data_offset and data_size.
Definition LazyLoadDylibInfo.hpp:157
static bool classof(const LoadCommand *cmd)
Definition LazyLoadDylibInfo.hpp:270
it_const_fixups fixups() const
Definition LazyLoadDylibInfo.hpp:262
bool may_be_missing() const
Whether the dylib is allowed to be missing at runtime (i.e. "weaklinked").
Definition LazyLoadDylibInfo.hpp:199
LazyLoadDylibInfo & flags(uint16_t value)
Definition LazyLoadDylibInfo.hpp:192
friend class Builder
Definition LazyLoadDylibInfo.hpp:53
ref_iterator< fixups_t & > it_fixups
Iterator that outputs Fixup&.
Definition LazyLoadDylibInfo.hpp:122
LazyLoadDylibInfo & add_symbol(std::string value)
Append a symbol name to the list of symbols to bind lazily.
Definition LazyLoadDylibInfo.hpp:246
std::unique_ptr< LoadCommand > clone() const override
Definition LazyLoadDylibInfo.hpp:133
~LazyLoadDylibInfo() override=default
std::ostream & print(std::ostream &os) const override
friend class LinkEdit
Definition LazyLoadDylibInfo.hpp:54
LazyLoadDylibInfo & pointer_format(uint16_t value)
Definition LazyLoadDylibInfo.hpp:219
LazyLoadDylibInfo & chain_start_image_offset(uint32_t value)
Definition LazyLoadDylibInfo.hpp:229
LazyLoadDylibInfo & symbols(std::vector< std::string > value)
Replace the list of the symbol names to bind lazily for this dylib.
Definition LazyLoadDylibInfo.hpp:240
std::vector< Fixup > fixups_t
Definition LazyLoadDylibInfo.hpp:119
void data_size(uint32_t size)
Definition LazyLoadDylibInfo.hpp:151
uint16_t pointer_format() const
Chained-fixups pointer format used by the binding chain (e.g. DYLD_CHAINED_PTR_ARM64E_USERLAND).
Definition LazyLoadDylibInfo.hpp:215
LazyLoadDylibInfo & clear_symbols()
Remove all the symbol names to bind lazily.
Definition LazyLoadDylibInfo.hpp:252
uint32_t data_size() const
Size of the payload.
Definition LazyLoadDylibInfo.hpp:143
std::string_view load_path() const
Load path of the dylib to bind lazily.
Definition LazyLoadDylibInfo.hpp:166
LazyLoadDylibInfo & load_path(std::string value)
Change the load path of the dylib to bind lazily.
Definition LazyLoadDylibInfo.hpp:171
const_ref_iterator< const fixups_t & > it_const_fixups
Iterator that outputs const Fixup&.
Definition LazyLoadDylibInfo.hpp:125
LazyLoadDylibInfo & may_be_missing(bool value)
Set or clear the "may be missing" (weak linked) bit of flags().
Definition LazyLoadDylibInfo.hpp:204
uint32_t chain_start_image_offset() const
Image offset of the fixup chain start used to bind the dylib's symbols.
Definition LazyLoadDylibInfo.hpp:225
Definition LinkEdit.hpp:47
uint32_t size() const
Size of the command (should be greater than sizeof(load_command)).
Definition LoadCommand.hpp:137
LoadCommand::TYPE command() const
Command type.
Definition LoadCommand.hpp:132
@ LAZY_LOAD_DYLIB_INFO
Definition LoadCommand.hpp:107
Class which represents a LoadCommand::TYPE::SEGMENT / LoadCommand::TYPE::SEGMENT_64 command.
Definition SegmentCommand.hpp:54
Definition SpanStream.hpp:32
Opaque structure that is used by LIEF to avoid writing result<void> f(...). Instead,...
Definition errors.hpp:119
Iterator which returns reference on container's values.
Definition iterators.hpp:47
Definition iostream.hpp:34
#define LIEF_LIFETIMEBOUND
Definition compiler_attributes.hpp:72
Definition endianness_support.hpp:59
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:316
#define LIEF_API
Definition visibility.h:45
#define LIEF_LOCAL
Definition visibility.h:46