LIEF: Library to Instrument Executable Formats Version 1.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 <vector>
19#include <ostream>
20
22#include "LIEF/visibility.h"
23#include "LIEF/errors.hpp"
24#include "LIEF/iterators.hpp"
25
27
28namespace LIEF {
29class SpanStream;
30class BinaryStream;
31
32namespace MachO {
33class BinaryParser;
34class LinkEdit;
35
36namespace details {
37struct linkedit_data_command;
38
39// clang-format off
40// On-disk entry of a `LC_FUNCTION_VARIANTS` runtime table
42 uint32_t impl : 31,
44 uint8_t flag_bit_nums[4];
45};
46// clang-format on
47
48}
49
70 friend class BinaryParser;
71 friend class LinkEdit;
72
73 public:
76 public:
78 static constexpr uint32_t MAX_IMPL = (uint32_t(1) << 31) - 1;
79
80 static constexpr uint32_t F_BIT = 20;
81 static constexpr uint32_t F_MASK = (uint32_t(1) << F_BIT) - 1;
82
83 static constexpr uint32_t F_PER_PROCESS = uint32_t(1) << F_BIT;
84 static constexpr uint32_t F_SYSTEM_WIDE = uint32_t(2) << F_BIT;
85 static constexpr uint32_t F_ARM64 = uint32_t(3) << F_BIT;
86 static constexpr uint32_t F_X86_64 = uint32_t(4) << F_BIT;
87
93 enum class FLAGS : uint32_t {
95
96#define FUNCTION_VARIANT_FLAG(name, value, _) name = (value | F_ARM64),
98#undef FUNCTION_VARIANT_FLAG
99
100#define FUNCTION_VARIANT_FLAG(name, value, _) name = (value | F_X86_64),
102#undef FUNCTION_VARIANT_FLAG
103
104#define FUNCTION_VARIANT_FLAG(name, value, _) name = (value | F_SYSTEM_WIDE),
106#undef FUNCTION_VARIANT_FLAG
108#define FUNCTION_VARIANT_FLAG(name, value, _) name = (value | F_PER_PROCESS),
110#undef FUNCTION_VARIANT_FLAG
111 };
113 static uint8_t get_raw(FLAGS f) {
114 return (uint8_t)f;
117 RuntimeTableEntry() = default;
124 RuntimeTableEntry& operator=(RuntimeTableEntry&&) noexcept = default;
126 ~RuntimeTableEntry() = default;
130 uint32_t impl() const {
131 return impl_;
139 void impl(uint32_t value) {
140 impl_ = value & MAX_IMPL;
145 bool another_table() const {
146 return another_table_;
150 void another_table(bool value) {
151 another_table_ = value;
156 return flag_bit_nums_;
157 }
158
160 const std::vector<FLAGS>& flags() const {
161 return flags_;
162 }
164 std::string to_string() const;
166 LIEF_API friend std::ostream& operator<<(std::ostream& os,
167 const RuntimeTableEntry& entry) {
168 os << entry.to_string();
169 return os;
173 LIEF_LOCAL void set_flags(std::vector<FLAGS> flags) {
174 flags_ = std::move(flags);
176
177 private:
178 bool another_table_ = false;
179 uint32_t impl_ = 0;
180 std::array<uint8_t, 4> flag_bit_nums_ = {};
181 std::vector<FLAGS> flags_;
182 };
183
190 public:
191 using entries_t = std::vector<RuntimeTableEntry>;
192
195
198
207 enum class KIND : uint32_t {
210
212 PER_PROCESS = 1,
213
216 SYSTEM_WIDE = 2,
217
219 ARM64 = 3,
220
222 X86_64 = 4,
223 };
224
225 RuntimeTable() = default;
227 kind_(kind),
228 offset_(offset) {}
229 RuntimeTable(const RuntimeTable&) = default;
231
232 RuntimeTable(RuntimeTable&&) noexcept = default;
233 RuntimeTable& operator=(RuntimeTable&&) noexcept = default;
234
235 ~RuntimeTable() = default;
236
238 KIND kind() const {
239 return kind_;
240 }
241
243 uint32_t offset() const {
244 return offset_;
245 }
246
249 return entries_;
250 }
251
253 return entries_;
254 }
255
256 void add(RuntimeTableEntry entry) {
257 entries_.push_back(std::move(entry));
258 }
259
260 std::string to_string() const;
261
262 LIEF_API friend std::ostream& operator<<(std::ostream& os,
263 const RuntimeTable& table) {
264 os << table.to_string();
265 return os;
266 }
267
268 private:
269 KIND kind_ = KIND::UNKNOWN;
270 uint32_t offset_ = 0;
271 entries_t entries_;
272 };
273
274 using runtime_table_t = std::vector<RuntimeTable>;
275
278
281
282 FunctionVariants() = default;
283 FunctionVariants(const details::linkedit_data_command& cmd);
284
286 FunctionVariants(const FunctionVariants& copy) = default;
287
288 std::unique_ptr<LoadCommand> clone() const override {
289 return std::unique_ptr<FunctionVariants>(new FunctionVariants(*this));
290 }
291
293 uint32_t data_offset() const {
294 return data_offset_;
295 }
296
298 uint32_t data_size() const {
299 return data_size_;
300 }
301
302 void data_offset(uint32_t offset) {
303 data_offset_ = offset;
304 }
305
306 void data_size(uint32_t size) {
307 data_size_ = size;
308 }
309
313 return content_;
314 }
315
317 return content_;
318 }
319
323 return runtime_table_;
324 }
325
327 return runtime_table_;
328 }
329
332 runtime_table_.push_back(std::move(table));
333 return runtime_table_.back();
334 }
335
336 ~FunctionVariants() override = default;
337
338 std::ostream& print(std::ostream& os) const override;
339
340 static bool classof(const LoadCommand* cmd) {
342 }
343
344 LIEF_LOCAL static std::vector<RuntimeTable> parse_payload(SpanStream& stream);
346
347 private:
348 uint32_t data_offset_ = 0;
349 uint32_t data_size_ = 0;
350 span<uint8_t> content_;
351 std::vector<RuntimeTable> runtime_table_;
352};
353
356
357}
358}
359#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
This class exposes information about a given implementation.
Definition FunctionVariants.hpp:75
void another_table(bool value)
Set whether impl() refers to an entry in another runtime table.
Definition FunctionVariants.hpp:150
span< const uint8_t > flag_bit_nums() const
The flagBitNums value as a slice of bytes.
Definition FunctionVariants.hpp:155
static constexpr uint32_t F_SYSTEM_WIDE
Definition FunctionVariants.hpp:84
static constexpr uint32_t F_MASK
Definition FunctionVariants.hpp:81
const std::vector< FLAGS > & flags() const
Return the interpreted flag_bit_nums().
Definition FunctionVariants.hpp:160
static constexpr uint32_t F_PER_PROCESS
Definition FunctionVariants.hpp:83
FLAGS
Flags describing the target platform, environment, or architecture for a given function implementatio...
Definition FunctionVariants.hpp:93
static constexpr uint32_t F_X86_64
Definition FunctionVariants.hpp:86
RuntimeTableEntry & operator=(const RuntimeTableEntry &)=default
static uint8_t get_raw(FLAGS f)
Definition FunctionVariants.hpp:113
bool another_table() const
Indicates whether impl() refers to an entry in another runtime table, rather than a direct function i...
Definition FunctionVariants.hpp:145
friend std::ostream & operator<<(std::ostream &os, const RuntimeTableEntry &entry)
Definition FunctionVariants.hpp:166
static constexpr uint32_t MAX_IMPL
The implementation address/index is encoded on 31 bits.
Definition FunctionVariants.hpp:78
static constexpr uint32_t F_ARM64
Definition FunctionVariants.hpp:85
uint32_t impl() const
The relative address of the implementation or an index if another_table() is set.
Definition FunctionVariants.hpp:130
static constexpr uint32_t F_BIT
Definition FunctionVariants.hpp:80
Represents a runtime table of function variants sharing a common namespace (referred to internally as...
Definition FunctionVariants.hpp:189
uint32_t offset() const
Original offset in the payload.
Definition FunctionVariants.hpp:243
void add(RuntimeTableEntry entry)
Definition FunctionVariants.hpp:256
ref_iterator< entries_t & > it_entries
Iterator that outputs RuntimeTableEntry&.
Definition FunctionVariants.hpp:194
KIND
Enumeration describing the namespace or category of a function variant.
Definition FunctionVariants.hpp:207
it_entries entries()
Iterator over the different RuntimeTableEntry entries.
Definition FunctionVariants.hpp:248
std::vector< RuntimeTableEntry > entries_t
Definition FunctionVariants.hpp:191
RuntimeTable & operator=(const RuntimeTable &)=default
const_ref_iterator< const entries_t & > it_const_entries
Iterator that outputs const RuntimeTableEntry&.
Definition FunctionVariants.hpp:197
RuntimeTable(const RuntimeTable &)=default
KIND kind() const
Kind of this runtime table.
Definition FunctionVariants.hpp:238
RuntimeTable(KIND kind, uint32_t offset)
Definition FunctionVariants.hpp:226
it_const_entries entries() const
Definition FunctionVariants.hpp:252
friend std::ostream & operator<<(std::ostream &os, const RuntimeTable &table)
Definition FunctionVariants.hpp:262
RuntimeTable(RuntimeTable &&) noexcept=default
static std::vector< RuntimeTable > parse_payload(SpanStream &stream)
it_const_runtime_table runtime_table() const
Definition FunctionVariants.hpp:326
static result< RuntimeTable > parse_entry(BinaryStream &stream)
RuntimeTable & add(RuntimeTable table)
Append a new RuntimeTable and return a reference to the inserted table.
Definition FunctionVariants.hpp:331
uint32_t data_offset() const
Offset in the __LINKEDIT SegmentCommand where the payload starts.
Definition FunctionVariants.hpp:293
span< const uint8_t > content() const
Return the data slice in the __LINKEDIT segment referenced by data_offset and data_size;.
Definition FunctionVariants.hpp:312
ref_iterator< runtime_table_t & > it_runtime_table
Iterator that outputs RuntimeTable&.
Definition FunctionVariants.hpp:277
friend class BinaryParser
Definition FunctionVariants.hpp:70
std::unique_ptr< LoadCommand > clone() const override
Definition FunctionVariants.hpp:288
FunctionVariants(const details::linkedit_data_command &cmd)
void data_offset(uint32_t offset)
Definition FunctionVariants.hpp:302
span< uint8_t > content()
Definition FunctionVariants.hpp:316
FunctionVariants & operator=(const FunctionVariants &copy)=default
~FunctionVariants() override=default
FunctionVariants(const FunctionVariants &copy)=default
void data_size(uint32_t size)
Definition FunctionVariants.hpp:306
it_runtime_table runtime_table()
Iterator over the different RuntimeTable entries located in the content of this __LINKEDIT command.
Definition FunctionVariants.hpp:322
uint32_t data_size() const
Size of the payload.
Definition FunctionVariants.hpp:298
std::vector< RuntimeTable > runtime_table_t
Definition FunctionVariants.hpp:274
friend class LinkEdit
Definition FunctionVariants.hpp:71
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:280
static bool classof(const LoadCommand *cmd)
Definition FunctionVariants.hpp:340
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
@ FUNCTION_VARIANTS
Definition LoadCommand.hpp:103
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:78
#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
@ UNKNOWN
Definition MachO/enums.hpp:24
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:320
Definition FunctionVariants.hpp:41
uint8_t flag_bit_nums[4]
Definition FunctionVariants.hpp:44
uint32_t impl
Definition FunctionVariants.hpp:42
uint32_t another_table
Definition FunctionVariants.hpp:43
#define LIEF_API
Definition visibility.h:45
#define LIEF_LOCAL
Definition visibility.h:46