LIEF: Library to Instrument Executable Formats Version 2.0.0
Loading...
Searching...
No Matches
RuntimeFunctionX64.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_PE_RUNTIME_FUNCTION_X64_H
17#define LIEF_PE_RUNTIME_FUNCTION_X64_H
18
19#include <cstdint>
20#include <memory>
21#include <string>
22
23#include "LIEF/enums.hpp"
24#include "LIEF/errors.hpp"
25#include "LIEF/visibility.h"
26#include <optional>
27
29
30namespace LIEF {
31class BinaryStream;
32
33namespace PE {
34class Parser;
35
36namespace unwind_x64 {
37class Code;
38}
39
45 public:
47 LIEF_LOCAL static std::unique_ptr<RuntimeFunctionX64>
48 parse(Parser& ctx, BinaryStream& strm, bool skip_unwind = false);
49
51 LIEF_LOCAL static ok_error_t parse_unwind(Parser& ctx, BinaryStream& strm,
52 RuntimeFunctionX64& func);
53
54 enum class UNWIND_FLAGS : uint8_t {
57 EXCEPTION_HANDLER = 1,
60 TERMINATE_HANDLER = 2,
61
63 CHAIN_INFO = 4,
64 };
65
66 enum class UNWIND_OPCODES : uint32_t {
73 PUSH_NONVOL = 0,
74
81 ALLOC_LARGE = 1,
82
85 ALLOC_SMALL = 2,
86
94 SET_FPREG = 3,
95
102 SAVE_NONVOL = 4,
103
110 SAVE_NONVOL_FAR = 5,
111
114 EPILOG = 6,
115
118 SPARE = 7,
119
123 SAVE_XMM128 = 8,
124
128 SAVE_XMM128_FAR = 9,
129
132 PUSH_MACHFRAME = 10,
133 };
134
135 enum class UNWIND_REG : uint32_t {
136 RAX = 0,
137 RCX,
138 RDX,
139 RBX,
140 RSP,
141 RBP,
142 RSI,
143 RDI,
144 R8,
145 R9,
146 R10,
147 R11,
148 R12,
149 R13,
150 R14,
151 R15,
152 };
153
158 using opcodes_t = std::vector<std::unique_ptr<unwind_x64::Code>>;
159
161 uint8_t version = 0;
162
164 uint8_t flags = 0;
165
167 uint8_t sizeof_prologue = 0;
168
172 uint8_t count_opcodes = 0;
173
178 uint8_t frame_reg = 0;
179
182 uint8_t frame_reg_offset = 0;
183
186 std::vector<uint8_t> raw_opcodes;
187
192 std::optional<uint32_t> handler;
193
197
199 bool has(UNWIND_FLAGS flag) const {
200 return (flags & (int)flag) != 0;
201 }
202
205
207 std::string to_string() const;
208
209 friend LIEF_API std::ostream& operator<<(std::ostream& os,
210 const unwind_info_t& info) {
211 os << info.to_string();
212 return os;
213 }
214 };
215
216 RuntimeFunctionX64(uint32_t rva_start, uint32_t rva_end, uint32_t unwind_rva) :
217 ExceptionInfo(ARCH::X86_64, rva_start),
218 rva_end_(rva_end),
219 unwind_rva_(unwind_rva) {}
220
223
226
227 std::unique_ptr<ExceptionInfo> clone() const override {
228 return std::make_unique<RuntimeFunctionX64>(*this);
229 }
230
231 std::string to_string() const override;
232
234 uint32_t rva_end() const {
235 return rva_end_;
236 }
237
239 uint32_t unwind_rva() const {
240 return unwind_rva_;
241 }
242
244 uint32_t size() const {
245 return rva_end() - rva_start();
246 }
247
249 const unwind_info_t* unwind_info() const {
250 return unwind_info_.has_value() ? &*unwind_info_ : nullptr;
251 }
252
254 return unwind_info_.has_value() ? &*unwind_info_ : nullptr;
255 }
256
258 unwind_info_ = std::move(info);
259 }
260
261 static bool classof(const ExceptionInfo* info) {
262 return info->arch() == ExceptionInfo::ARCH::X86_64;
263 }
264
266
267 private:
268 uint32_t rva_end_ = 0;
269 uint32_t unwind_rva_ = 0;
270 std::optional<unwind_info_t> unwind_info_;
271};
272
276
277}
278}
279
281
282#endif
Class that is used to a read stream of data from different sources.
Definition BinaryStream.hpp:35
uint32_t rva_start() const
Function start address.
Definition ExceptionInfo.hpp:69
ARCH
Arch discriminator for the subclasses.
Definition ExceptionInfo.hpp:50
@ X86_64
Definition ExceptionInfo.hpp:53
Main interface to parse PE binaries. In particular, the static Parser::parse functions should be used...
Definition PE/Parser.hpp:53
std::string to_string() const override
UNWIND_REG
Definition RuntimeFunctionX64.hpp:135
void unwind_info(unwind_info_t info)
Definition RuntimeFunctionX64.hpp:257
const unwind_info_t * unwind_info() const
Detailed unwind information.
Definition RuntimeFunctionX64.hpp:249
UNWIND_OPCODES
Definition RuntimeFunctionX64.hpp:66
RuntimeFunctionX64(RuntimeFunctionX64 &&)=default
RuntimeFunctionX64(uint32_t rva_start, uint32_t rva_end, uint32_t unwind_rva)
Definition RuntimeFunctionX64.hpp:216
uint32_t unwind_rva() const
Unwind info address.
Definition RuntimeFunctionX64.hpp:239
RuntimeFunctionX64 & operator=(const RuntimeFunctionX64 &)=default
std::unique_ptr< ExceptionInfo > clone() const override
Definition RuntimeFunctionX64.hpp:227
uint32_t size() const
Size of the function (in bytes).
Definition RuntimeFunctionX64.hpp:244
RuntimeFunctionX64(const RuntimeFunctionX64 &)=default
static bool classof(const ExceptionInfo *info)
Definition RuntimeFunctionX64.hpp:261
UNWIND_FLAGS
Definition RuntimeFunctionX64.hpp:54
unwind_info_t * unwind_info()
Definition RuntimeFunctionX64.hpp:253
uint32_t rva_end() const
Function end address.
Definition RuntimeFunctionX64.hpp:234
RuntimeFunctionX64 & operator=(RuntimeFunctionX64 &&)=default
Base class for all unwind operations.
Definition UnwindCodeX64.hpp:31
Opaque structure that is used by LIEF to avoid writing result<void> f(...). Instead,...
Definition errors.hpp:119
#define ENABLE_BITMASK_OPERATORS(X)
Definition enums.hpp:24
This namespace wraps code related to PE-x64 unwinding code.
Definition RuntimeFunctionX64.hpp:36
Namespace related to the LIEF's PE module.
Definition Abstract/Header.hpp:32
const char * to_string(CODE_PAGES e)
LIEF namespace.
Definition Abstract/Binary.hpp:41
This structure represents the UNWIND_INFO which records the effects a function has on the stack point...
Definition RuntimeFunctionX64.hpp:157
uint8_t sizeof_prologue
Length of the function prolog in bytes.
Definition RuntimeFunctionX64.hpp:167
uint8_t frame_reg
If nonzero, then the function uses a frame pointer (FP), and this field is the number of the nonvolat...
Definition RuntimeFunctionX64.hpp:178
bool has(UNWIND_FLAGS flag) const
Check if the given flag is used.
Definition RuntimeFunctionX64.hpp:199
opcodes_t opcodes() const
Enhanced representation of the unwind code.
uint8_t frame_reg_offset
If the frame register field is nonzero, this field is the scaled offset from RSP that is applied to t...
Definition RuntimeFunctionX64.hpp:182
std::string to_string() const
Pretty representation of this structure as a string.
uint8_t flags
See: UNWIND_FLAGS.
Definition RuntimeFunctionX64.hpp:164
std::vector< std::unique_ptr< unwind_x64::Code > > opcodes_t
Definition RuntimeFunctionX64.hpp:158
std::vector< uint8_t > raw_opcodes
An array of items that explains the effect of the prolog on the nonvolatile registers and RSP.
Definition RuntimeFunctionX64.hpp:186
friend std::ostream & operator<<(std::ostream &os, const unwind_info_t &info)
Definition RuntimeFunctionX64.hpp:209
uint8_t version
Version number of the unwind data, currently 1 or 2.
Definition RuntimeFunctionX64.hpp:161
std::optional< uint32_t > handler
An image-relative pointer to either the function's language-specific exception or termination handler...
Definition RuntimeFunctionX64.hpp:192
uint8_t count_opcodes
The number of slots in the unwind codes array. Some unwind codes, for example, UNWIND_OPCODES::SAVE_N...
Definition RuntimeFunctionX64.hpp:172
RuntimeFunctionX64 * chained
If UNWIND_FLAGS::CHAIN_INFO is set, this attributes references the chained runtime function.
Definition RuntimeFunctionX64.hpp:196
#define LIEF_API
Definition visibility.h:45
#define LIEF_LOCAL
Definition visibility.h:46