LIEF: Library to Instrument Executable Formats Version 2.0.0
Loading...
Searching...
No Matches
FunctionVariants.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_FUNCTION_VARIANTS_COMMAND_H
17#define LIEF_MACHO_FUNCTION_VARIANTS_COMMAND_H
18#include <memory>
19#include <ostream>
20#include <vector>
21
23#include "LIEF/errors.hpp"
24#include "LIEF/iterators.hpp"
25#include "LIEF/visibility.h"
26
28
29namespace LIEF {
30class SpanStream;
31class BinaryStream;
32
33namespace MachO {
34class BinaryParser;
35class LinkEdit;
36
37namespace details {
38struct linkedit_data_command;
39
40// clang-format off
41// On-disk entry of a `LC_FUNCTION_VARIANTS` runtime table
43 uint32_t impl : 31,
45 uint8_t flag_bit_nums[4];
46};
47// clang-format on
48
49}
50
71 friend class BinaryParser;
72 friend class LinkEdit;
73
74 public:
77 public:
79 static constexpr uint32_t MAX_IMPL = (uint32_t(1) << 31) - 1;
80
81 static constexpr uint32_t F_BIT = 20;
82 static constexpr uint32_t F_MASK = (uint32_t(1) << F_BIT) - 1;
83
84 static constexpr uint32_t F_PER_PROCESS = uint32_t(1) << F_BIT;
85 static constexpr uint32_t F_SYSTEM_WIDE = uint32_t(2) << F_BIT;
86 static constexpr uint32_t F_ARM64 = uint32_t(3) << F_BIT;
87 static constexpr uint32_t F_X86_64 = uint32_t(4) << F_BIT;
88
94 enum class FLAGS : uint32_t {
96
97#define FUNCTION_VARIANT_FLAG(name, value, _) name = (value | F_ARM64),
99#undef FUNCTION_VARIANT_FLAG
100
101#define FUNCTION_VARIANT_FLAG(name, value, _) name = (value | F_X86_64),
103#undef FUNCTION_VARIANT_FLAG
104
105#define FUNCTION_VARIANT_FLAG(name, value, _) name = (value | F_SYSTEM_WIDE),
107#undef FUNCTION_VARIANT_FLAG
109#define FUNCTION_VARIANT_FLAG(name, value, _) name = (value | F_PER_PROCESS),
111#undef FUNCTION_VARIANT_FLAG
112 };
114 static uint8_t get_raw(FLAGS f) {
115 return (uint8_t)f;
118 RuntimeTableEntry() = default;
125 RuntimeTableEntry& operator=(RuntimeTableEntry&&) noexcept = default;
127 ~RuntimeTableEntry() = default;
131 uint32_t impl() const {
132 return impl_;
140 void impl(uint32_t value) {
141 impl_ = value & MAX_IMPL;
146 bool another_table() const {
147 return another_table_;
151 void another_table(bool value) {
152 another_table_ = value;
157 return flag_bit_nums_;
158 }
159
161 const std::vector<FLAGS>& flags() const {
162 return flags_;
163 }
165 std::string to_string() const;
167 LIEF_API friend std::ostream& operator<<(std::ostream& os,
168 const RuntimeTableEntry& entry) {
169 os << entry.to_string();
170 return os;
174 LIEF_LOCAL void set_flags(std::vector<FLAGS> flags) {
175 flags_ = std::move(flags);
177
178 private:
179 bool another_table_ = false;
180 uint32_t impl_ = 0;
181 std::array<uint8_t, 4> flag_bit_nums_ = {};
182 std::vector<FLAGS> flags_;
183 };
184
191 public:
192 using entries_t = std::vector<RuntimeTableEntry>;
193
196
199
208 enum class KIND : uint32_t {
211
213 PER_PROCESS = 1,
214
217 SYSTEM_WIDE = 2,
218
220 ARM64 = 3,
221
223 X86_64 = 4,
224 };
225
226 RuntimeTable() = default;
228 kind_(kind),
229 offset_(offset) {}
230 RuntimeTable(const RuntimeTable&) = default;
232
233 RuntimeTable(RuntimeTable&&) noexcept = default;
234 RuntimeTable& operator=(RuntimeTable&&) noexcept = default;
235
236 ~RuntimeTable() = default;
237
239 KIND kind() const {
240 return kind_;
241 }
242
244 uint32_t offset() const {
245 return offset_;
246 }
247
250 return entries_;
251 }
252
254 return entries_;
255 }
256
257 void add(RuntimeTableEntry entry) {
258 entries_.push_back(std::move(entry));
259 }
260
261 std::string to_string() const;
262
263 LIEF_API friend std::ostream& operator<<(std::ostream& os,
264 const RuntimeTable& table) {
265 os << table.to_string();
266 return os;
267 }
268
269 private:
270 KIND kind_ = KIND::UNKNOWN;
271 uint32_t offset_ = 0;
272 entries_t entries_;
273 };
274
275 using runtime_table_t = std::vector<RuntimeTable>;
276
279
282
283 FunctionVariants() = default;
284 FunctionVariants(const details::linkedit_data_command& cmd);
285
287 FunctionVariants(const FunctionVariants& copy) = default;
288
289 std::unique_ptr<LoadCommand> clone() const override {
290 return std::make_unique<FunctionVariants>(*this);
291 }
292
294 uint32_t data_offset() const {
295 return data_offset_;
296 }
297
299 uint32_t data_size() const {
300 return data_size_;
301 }
302
303 void data_offset(uint32_t offset) {
304 data_offset_ = offset;
305 }
306
307 void data_size(uint32_t size) {
308 data_size_ = size;
309 }
310
314 return content_;
315 }
316
318 return content_;
319 }
320
324 return runtime_table_;
325 }
326
328 return runtime_table_;
329 }
330
333 runtime_table_.push_back(std::move(table));
334 return runtime_table_.back();
335 }
336
337 ~FunctionVariants() override = default;
338
339 std::ostream& print(std::ostream& os) const override;
340
341 static bool classof(const LoadCommand* cmd) {
343 }
344
345 LIEF_LOCAL static std::vector<RuntimeTable> parse_payload(SpanStream& stream);
347 uint64_t max_entries);
348
349 private:
350 uint32_t data_offset_ = 0;
351 uint32_t data_size_ = 0;
352 span<uint8_t> content_;
353 std::vector<RuntimeTable> runtime_table_;
354};
355
358
359}
360}
361#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
This class exposes information about a given implementation.
Definition FunctionVariants.hpp:76
void another_table(bool value)
Set whether impl() refers to an entry in another runtime table.
Definition FunctionVariants.hpp:151
span< const uint8_t > flag_bit_nums() const
The flagBitNums value as a slice of bytes.
Definition FunctionVariants.hpp:156
static constexpr uint32_t F_SYSTEM_WIDE
Definition FunctionVariants.hpp:85
static constexpr uint32_t F_MASK
Definition FunctionVariants.hpp:82
const std::vector< FLAGS > & flags() const
Return the interpreted flag_bit_nums().
Definition FunctionVariants.hpp:161
static constexpr uint32_t F_PER_PROCESS
Definition FunctionVariants.hpp:84
FLAGS
Flags describing the target platform, environment, or architecture for a given function implementatio...
Definition FunctionVariants.hpp:94
static constexpr uint32_t F_X86_64
Definition FunctionVariants.hpp:87
RuntimeTableEntry & operator=(const RuntimeTableEntry &)=default
static uint8_t get_raw(FLAGS f)
Definition FunctionVariants.hpp:114
bool another_table() const
Indicates whether impl() refers to an entry in another runtime table, rather than a direct function i...
Definition FunctionVariants.hpp:146
friend std::ostream & operator<<(std::ostream &os, const RuntimeTableEntry &entry)
Definition FunctionVariants.hpp:167
static constexpr uint32_t MAX_IMPL
The implementation address/index is encoded on 31 bits.
Definition FunctionVariants.hpp:79
static constexpr uint32_t F_ARM64
Definition FunctionVariants.hpp:86
uint32_t impl() const
The relative address of the implementation or an index if another_table() is set.
Definition FunctionVariants.hpp:131
static constexpr uint32_t F_BIT
Definition FunctionVariants.hpp:81
Represents a runtime table of function variants sharing a common namespace (referred to internally as...
Definition FunctionVariants.hpp:190
uint32_t offset() const
Original offset in the payload.
Definition FunctionVariants.hpp:244
void add(RuntimeTableEntry entry)
Definition FunctionVariants.hpp:257
ref_iterator< entries_t & > it_entries
Iterator that outputs RuntimeTableEntry&.
Definition FunctionVariants.hpp:195
KIND
Enumeration describing the namespace or category of a function variant.
Definition FunctionVariants.hpp:208
it_entries entries()
Iterator over the different RuntimeTableEntry entries.
Definition FunctionVariants.hpp:249
std::vector< RuntimeTableEntry > entries_t
Definition FunctionVariants.hpp:192
RuntimeTable & operator=(const RuntimeTable &)=default
const_ref_iterator< const entries_t & > it_const_entries
Iterator that outputs const RuntimeTableEntry&.
Definition FunctionVariants.hpp:198
RuntimeTable(const RuntimeTable &)=default
KIND kind() const
Kind of this runtime table.
Definition FunctionVariants.hpp:239
RuntimeTable(KIND kind, uint32_t offset)
Definition FunctionVariants.hpp:227
it_const_entries entries() const
Definition FunctionVariants.hpp:253
friend std::ostream & operator<<(std::ostream &os, const RuntimeTable &table)
Definition FunctionVariants.hpp:263
RuntimeTable(RuntimeTable &&) noexcept=default
static std::vector< RuntimeTable > parse_payload(SpanStream &stream)
it_const_runtime_table runtime_table() const
Definition FunctionVariants.hpp:327
RuntimeTable & add(RuntimeTable table)
Append a new RuntimeTable and return a reference to the inserted table.
Definition FunctionVariants.hpp:332
uint32_t data_offset() const
Offset in the __LINKEDIT SegmentCommand where the payload starts.
Definition FunctionVariants.hpp:294
span< const uint8_t > content() const
Return the data slice in the __LINKEDIT segment referenced by data_offset and data_size;.
Definition FunctionVariants.hpp:313
static result< RuntimeTable > parse_entry(BinaryStream &stream, uint64_t max_entries)
ref_iterator< runtime_table_t & > it_runtime_table
Iterator that outputs RuntimeTable&.
Definition FunctionVariants.hpp:278
friend class BinaryParser
Definition FunctionVariants.hpp:71
std::unique_ptr< LoadCommand > clone() const override
Definition FunctionVariants.hpp:289
FunctionVariants(const details::linkedit_data_command &cmd)
void data_offset(uint32_t offset)
Definition FunctionVariants.hpp:303
span< uint8_t > content()
Definition FunctionVariants.hpp:317
FunctionVariants & operator=(const FunctionVariants &copy)=default
~FunctionVariants() override=default
FunctionVariants(const FunctionVariants &copy)=default
void data_size(uint32_t size)
Definition FunctionVariants.hpp:307
it_runtime_table runtime_table()
Iterator over the different RuntimeTable entries located in the content of this __LINKEDIT command.
Definition FunctionVariants.hpp:323
uint32_t data_size() const
Size of the payload.
Definition FunctionVariants.hpp:299
std::vector< RuntimeTable > runtime_table_t
Definition FunctionVariants.hpp:275
friend class LinkEdit
Definition FunctionVariants.hpp:72
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:281
static bool classof(const LoadCommand *cmd)
Definition FunctionVariants.hpp:341
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
@ FUNCTION_VARIANTS
Definition LoadCommand.hpp:104
Definition SpanStream.hpp:32
Iterator which returns reference on container's values.
Definition iterators.hpp:47
Wrapper that contains an Object (T) or an error.
Definition errors.hpp:79
#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
@ UNKNOWN
Definition MachO/enums.hpp:25
const char * to_string(BuildToolVersion::TOOLS tool)
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
Definition FunctionVariants.hpp:42
uint8_t flag_bit_nums[4]
Definition FunctionVariants.hpp:45
uint32_t impl
Definition FunctionVariants.hpp:43
uint32_t another_table
Definition FunctionVariants.hpp:44
#define LIEF_API
Definition visibility.h:45
#define LIEF_LOCAL
Definition visibility.h:46