LIEF: Library to Instrument Executable Formats Version 0.17.0
Loading...
Searching...
No Matches
FunctionVariants.hpp
Go to the documentation of this file.
1/* Copyright 2017 - 2025 R. Thomas
2 * Copyright 2017 - 2025 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_FUNCTION_VARIANTS_COMMAND_H
17#define LIEF_MACHO_FUNCTION_VARIANTS_COMMAND_H
18#include <vector>
19#include <ostream>
20
21#include "LIEF/visibility.h"
22#include "LIEF/errors.hpp"
23#include "LIEF/iterators.hpp"
24
26
27namespace LIEF {
28class SpanStream;
29class BinaryStream;
30
31namespace MachO {
32class BinaryParser;
33class LinkEdit;
34
35namespace details {
36struct linkedit_data_command;
37struct runtime_table_entry_t;
38}
39
60 friend class BinaryParser;
61 friend class LinkEdit;
62
63 public:
66 public:
67 static constexpr uint32_t F_BIT = 20;
68 static constexpr uint32_t F_MASK = (uint32_t(1) << F_BIT) - 1;
69
70 static constexpr uint32_t F_PER_PROCESS = uint32_t(1) << F_BIT;
71 static constexpr uint32_t F_SYSTEM_WIDE = uint32_t(2) << F_BIT;
72 static constexpr uint32_t F_ARM64 = uint32_t(3) << F_BIT;
73 static constexpr uint32_t F_X86_64 = uint32_t(4) << F_BIT;
74
80 enum class FLAGS : uint32_t {
82
83 #define FUNCTION_VARIANT_FLAG(name, value, _) name = (value | F_ARM64),
85 #undef FUNCTION_VARIANT_FLAG
86
87 #define FUNCTION_VARIANT_FLAG(name, value, _) name = (value | F_X86_64),
89 #undef FUNCTION_VARIANT_FLAG
90
91 #define FUNCTION_VARIANT_FLAG(name, value, _) name = (value | F_SYSTEM_WIDE),
93 #undef FUNCTION_VARIANT_FLAG
95 #define FUNCTION_VARIANT_FLAG(name, value, _) name = (value | F_PER_PROCESS),
97 #undef FUNCTION_VARIANT_FLAG
98 };
100 static uint8_t get_raw(FLAGS f) {
101 return (uint8_t)f;
104 RuntimeTableEntry() = default;
105 RuntimeTableEntry(const details::runtime_table_entry_t& entry);
111 RuntimeTableEntry& operator=(RuntimeTableEntry&&) noexcept = default;
113 ~RuntimeTableEntry() = default;
117 uint32_t impl() const {
118 return impl_;
123 bool another_table() const {
124 return another_table_;
129 return flag_bit_nums_;
133 const std::vector<FLAGS>& flags() const {
134 return flags_;
137 std::string to_string() const;
139 LIEF_API friend
140 std::ostream& operator<<(std::ostream& os, const RuntimeTableEntry& entry)
141 {
142 os << entry.to_string();
143 return os;
144 }
145
147 LIEF_LOCAL void set_flags(std::vector<FLAGS> flags) {
148 flags_ = std::move(flags);
149 }
150 private:
151 bool another_table_;
152 uint32_t impl_ = 0;
153 std::array<uint8_t, 4> flag_bit_nums_ = {};
154 std::vector<FLAGS> flags_;
155 };
163 public:
164 using entries_t = std::vector<RuntimeTableEntry>;
165
168
171
180 enum class KIND : uint32_t {
183
188 SYSTEM_WIDE = 2,
191 ARM64 = 3,
192
195 };
196
197 RuntimeTable() = default;
199 kind_(kind), offset_(offset)
200 {}
201 RuntimeTable(const RuntimeTable&) = default;
203
204 RuntimeTable(RuntimeTable&&) noexcept = default;
205 RuntimeTable& operator=(RuntimeTable&&) noexcept = default;
206
207 ~RuntimeTable() = default;
208
210 KIND kind() const {
211 return kind_;
212 }
213
215 uint32_t offset() const {
216 return offset_;
217 }
218
221 return entries_;
222 }
223
225 return entries_;
226 }
227
228 void add(RuntimeTableEntry entry) {
229 entries_.push_back(std::move(entry));
230 }
231
232 std::string to_string() const;
233
234 LIEF_API friend
235 std::ostream& operator<<(std::ostream& os, const RuntimeTable& table)
236 {
237 os << table.to_string();
238 return os;
239 }
240
241 private:
242 KIND kind_ = KIND::UNKNOWN;
243 uint32_t offset_ = 0;
244 entries_t entries_;
245 };
246
247 using runtime_table_t = std::vector<RuntimeTable>;
248
251
254
255 FunctionVariants() = default;
256 FunctionVariants(const details::linkedit_data_command& cmd);
257
259 FunctionVariants(const FunctionVariants& copy) = default;
260
261 std::unique_ptr<LoadCommand> clone() const override {
262 return std::unique_ptr<FunctionVariants>(new FunctionVariants(*this));
263 }
264
266 uint32_t data_offset() const {
267 return data_offset_;
268 }
269
271 uint32_t data_size() const {
272 return data_size_;
273 }
274
275 void data_offset(uint32_t offset) {
276 data_offset_ = offset;
277 }
278
279 void data_size(uint32_t size) {
280 data_size_ = size;
281 }
282
286 return content_;
287 }
288
290 return content_;
291 }
292
296 return runtime_table_;
297 }
298
300 return runtime_table_;
301 }
302
303 ~FunctionVariants() override = default;
304
305 std::ostream& print(std::ostream& os) const override;
306
307 static bool classof(const LoadCommand* cmd) {
309 }
310
311 LIEF_LOCAL static std::vector<RuntimeTable> parse_payload(SpanStream& stream);
313
314 private:
315 uint32_t data_offset_ = 0;
316 uint32_t data_size_ = 0;
317 span<uint8_t> content_;
318 std::vector<RuntimeTable> runtime_table_;
319};
320
323
324}
325}
326#endif
Class that is used to a read stream of data from different sources.
Definition BinaryStream.hpp:33
Class used to parse a single binary (i.e. non-FAT).
Definition BinaryParser.hpp:78
This class exposes information about a given implementation.
Definition FunctionVariants.hpp:65
span< const uint8_t > flag_bit_nums() const
The flagBitNums value as a slice of bytes.
Definition FunctionVariants.hpp:128
static constexpr uint32_t F_SYSTEM_WIDE
Definition FunctionVariants.hpp:71
static constexpr uint32_t F_MASK
Definition FunctionVariants.hpp:68
const std::vector< FLAGS > & flags() const
Return the interpreted flag_bit_nums().
Definition FunctionVariants.hpp:133
static constexpr uint32_t F_PER_PROCESS
Definition FunctionVariants.hpp:70
FLAGS
Flags describing the target platform, environment, or architecture for a given function implementatio...
Definition FunctionVariants.hpp:80
@ UNKNOWN
Definition FunctionVariants.hpp:81
static constexpr uint32_t F_X86_64
Definition FunctionVariants.hpp:73
RuntimeTableEntry & operator=(const RuntimeTableEntry &)=default
static uint8_t get_raw(FLAGS f)
Definition FunctionVariants.hpp:100
bool another_table() const
Indicates whether impl() refers to an entry in another runtime table, rather than a direct function i...
Definition FunctionVariants.hpp:123
friend std::ostream & operator<<(std::ostream &os, const RuntimeTableEntry &entry)
Definition FunctionVariants.hpp:140
static constexpr uint32_t F_ARM64
Definition FunctionVariants.hpp:72
uint32_t impl() const
The relative address of the implementation or an index if another_table() is set.
Definition FunctionVariants.hpp:117
static constexpr uint32_t F_BIT
Definition FunctionVariants.hpp:67
uint32_t offset() const
Original offset in the payload.
Definition FunctionVariants.hpp:215
void add(RuntimeTableEntry entry)
Definition FunctionVariants.hpp:228
ref_iterator< entries_t & > it_entries
Iterator that output RuntimeTableEntry&.
Definition FunctionVariants.hpp:167
KIND
Enumeration describing the namespace or category of a function variant.
Definition FunctionVariants.hpp:180
@ UNKNOWN
Fallback/default kind when the category is not recognized.
Definition FunctionVariants.hpp:182
@ PER_PROCESS
Variants that apply on a per-process basis.
Definition FunctionVariants.hpp:185
@ X86_64
Variants optimized for the x86-64 architecture.
Definition FunctionVariants.hpp:194
@ ARM64
Variants optimized for the ARM64 architecture.
Definition FunctionVariants.hpp:191
it_entries entries()
Iterator over the different RuntimeTableEntry entries.
Definition FunctionVariants.hpp:220
std::vector< RuntimeTableEntry > entries_t
Definition FunctionVariants.hpp:164
RuntimeTable & operator=(const RuntimeTable &)=default
const_ref_iterator< const entries_t & > it_const_entries
Iterator that output const RuntimeTableEntry&.
Definition FunctionVariants.hpp:170
RuntimeTable(const RuntimeTable &)=default
KIND kind() const
Kind of this runtime table.
Definition FunctionVariants.hpp:210
RuntimeTable(KIND kind, uint32_t offset)
Definition FunctionVariants.hpp:198
it_const_entries entries() const
Definition FunctionVariants.hpp:224
friend std::ostream & operator<<(std::ostream &os, const RuntimeTable &table)
Definition FunctionVariants.hpp:235
RuntimeTable(RuntimeTable &&) noexcept=default
static std::vector< RuntimeTable > parse_payload(SpanStream &stream)
it_const_runtime_table runtime_table() const
Definition FunctionVariants.hpp:299
static result< RuntimeTable > parse_entry(BinaryStream &stream)
uint32_t data_offset() const
Offset in the __LINKEDIT SegmentCommand where the payload starts.
Definition FunctionVariants.hpp:266
span< const uint8_t > content() const
Return the data slice in the __LINKEDIT segment referenced by data_offset and data_size;.
Definition FunctionVariants.hpp:285
ref_iterator< runtime_table_t & > it_runtime_table
Iterator that outputs RuntimeTable&.
Definition FunctionVariants.hpp:250
friend class BinaryParser
Definition FunctionVariants.hpp:60
std::unique_ptr< LoadCommand > clone() const override
Definition FunctionVariants.hpp:261
FunctionVariants(const details::linkedit_data_command &cmd)
void data_offset(uint32_t offset)
Definition FunctionVariants.hpp:275
span< uint8_t > content()
Definition FunctionVariants.hpp:289
FunctionVariants & operator=(const FunctionVariants &copy)=default
~FunctionVariants() override=default
FunctionVariants(const FunctionVariants &copy)=default
void data_size(uint32_t size)
Definition FunctionVariants.hpp:279
it_runtime_table runtime_table()
Iterator over the different RuntimeTable entries located in the content of this __LINKEDIT command.
Definition FunctionVariants.hpp:295
uint32_t data_size() const
Size of the payload.
Definition FunctionVariants.hpp:271
std::vector< RuntimeTable > runtime_table_t
Definition FunctionVariants.hpp:247
friend class LinkEdit
Definition FunctionVariants.hpp:61
std::ostream & print(std::ostream &os) const override
const_ref_iterator< const runtime_table_t & > it_const_runtime_table
Iterator that outputs const RuntimeTable&.
Definition FunctionVariants.hpp:253
static bool classof(const LoadCommand *cmd)
Definition FunctionVariants.hpp:307
Definition LinkEdit.hpp:45
uint32_t size() const
Size of the command (should be greather than sizeof(load_command)).
Definition LoadCommand.hpp:133
LoadCommand::TYPE command() const
Command type.
Definition LoadCommand.hpp:128
@ FUNCTION_VARIANTS
Definition LoadCommand.hpp:101
Definition SpanStream.hpp:32
Iterator which returns reference on container's values.
Definition iterators.hpp:46
const char * to_string(lief_errors err)
Definition endianness_support.hpp:59
Namespace related to the LIEF's Mach-O module.
Definition Abstract/Header.hpp:36
const char * to_string(BuildToolVersion::TOOLS tool)
LIEF namespace.
Definition Abstract/Binary.hpp:40
tcb::span< ElementType, Extent > span
Definition span.hpp:22
ref_iterator< CT, U, typename decay_t< CT >::const_iterator > const_ref_iterator
Iterator which return const ref on container's values.
Definition iterators.hpp:257
tl::expected< T, lief_errors > result
Wrapper that contains an Object (T) or an error.
Definition errors.hpp:75
#define LIEF_API
Definition visibility.h:41
#define LIEF_LOCAL
Definition visibility.h:42