LIEF: Library to Instrument Executable Formats Version 1.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 <memory>
20
21#include "LIEF/errors.hpp"
22#include "LIEF/visibility.h"
23#include "LIEF/enums.hpp"
24#include "LIEF/optional.hpp"
25
27
28namespace LIEF {
29class BinaryStream;
30
31namespace PE {
32class Parser;
33
34namespace unwind_x64 {
35class Code;
36}
37
43 public:
45 LIEF_LOCAL static std::unique_ptr<RuntimeFunctionX64>
46 parse(Parser& ctx, BinaryStream& strm, bool skip_unwind = false);
47
49 LIEF_LOCAL static ok_error_t parse_unwind(Parser& ctx, BinaryStream& strm,
50 RuntimeFunctionX64& func);
51
52 enum class UNWIND_FLAGS : uint8_t {
55 EXCEPTION_HANDLER = 1,
58 TERMINATE_HANDLER = 2,
59
61 CHAIN_INFO = 4,
62 };
63
64 enum class UNWIND_OPCODES : uint32_t {
71 PUSH_NONVOL = 0,
72
79 ALLOC_LARGE = 1,
80
83 ALLOC_SMALL = 2,
84
92 SET_FPREG = 3,
93
100 SAVE_NONVOL = 4,
101
108 SAVE_NONVOL_FAR = 5,
109
112 EPILOG = 6,
113
116 SPARE = 7,
117
121 SAVE_XMM128 = 8,
122
126 SAVE_XMM128_FAR = 9,
127
130 PUSH_MACHFRAME = 10,
131 };
132
133 enum class UNWIND_REG : uint32_t {
134 RAX = 0,
135 RCX,
136 RDX,
137 RBX,
138 RSP,
139 RBP,
140 RSI,
141 RDI,
142 R8,
143 R9,
144 R10,
145 R11,
146 R12,
147 R13,
148 R14,
149 R15,
150 };
151
156 using opcodes_t = std::vector<std::unique_ptr<unwind_x64::Code>>;
157
159 uint8_t version = 0;
160
162 uint8_t flags = 0;
163
165 uint8_t sizeof_prologue = 0;
166
170 uint8_t count_opcodes = 0;
171
176 uint8_t frame_reg = 0;
177
180 uint8_t frame_reg_offset = 0;
181
184 std::vector<uint8_t> raw_opcodes;
185
191
195
197 bool has(UNWIND_FLAGS flag) const {
198 return (flags & (int)flag) != 0;
199 }
200
203
205 std::string to_string() const;
206
207 friend LIEF_API std::ostream& operator<<(std::ostream& os,
208 const unwind_info_t& info) {
209 os << info.to_string();
210 return os;
211 }
212 };
213
214 RuntimeFunctionX64(uint32_t rva_start, uint32_t rva_end, uint32_t unwind_rva) :
215 ExceptionInfo(ARCH::X86_64, rva_start),
216 rva_end_(rva_end),
217 unwind_rva_(unwind_rva) {}
218
221
224
225 std::unique_ptr<ExceptionInfo> clone() const override {
226 return std::unique_ptr<RuntimeFunctionX64>(new RuntimeFunctionX64(*this));
227 }
228
229 std::string to_string() const override;
230
232 uint32_t rva_end() const {
233 return rva_end_;
234 }
235
237 uint32_t unwind_rva() const {
238 return unwind_rva_;
239 }
240
242 uint32_t size() const {
243 return rva_end() - rva_start();
244 }
245
247 const unwind_info_t* unwind_info() const {
248 return unwind_info_.has_value() ? &*unwind_info_ : nullptr;
249 }
250
252 return unwind_info_.has_value() ? &*unwind_info_ : nullptr;
253 }
254
256 unwind_info_ = std::move(info);
257 }
258
259 static bool classof(const ExceptionInfo* info) {
260 return info->arch() == ExceptionInfo::ARCH::X86_64;
261 }
262
264
265 private:
266 uint32_t rva_end_ = 0;
267 uint32_t unwind_rva_ = 0;
268 optional<unwind_info_t> unwind_info_;
269};
270
274
275}
276}
277
279
280#endif
Class that is used to a read stream of data from different sources.
Definition BinaryStream.hpp:33
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:52
std::string to_string() const override
UNWIND_REG
Definition RuntimeFunctionX64.hpp:133
void unwind_info(unwind_info_t info)
Definition RuntimeFunctionX64.hpp:255
const unwind_info_t * unwind_info() const
Detailed unwind information.
Definition RuntimeFunctionX64.hpp:247
UNWIND_OPCODES
Definition RuntimeFunctionX64.hpp:64
RuntimeFunctionX64(RuntimeFunctionX64 &&)=default
RuntimeFunctionX64(uint32_t rva_start, uint32_t rva_end, uint32_t unwind_rva)
Definition RuntimeFunctionX64.hpp:214
uint32_t unwind_rva() const
Unwind info address.
Definition RuntimeFunctionX64.hpp:237
RuntimeFunctionX64 & operator=(const RuntimeFunctionX64 &)=default
std::unique_ptr< ExceptionInfo > clone() const override
Definition RuntimeFunctionX64.hpp:225
uint32_t size() const
Size of the function (in bytes).
Definition RuntimeFunctionX64.hpp:242
RuntimeFunctionX64(const RuntimeFunctionX64 &)=default
static bool classof(const ExceptionInfo *info)
Definition RuntimeFunctionX64.hpp:259
UNWIND_FLAGS
Definition RuntimeFunctionX64.hpp:52
unwind_info_t * unwind_info()
Definition RuntimeFunctionX64.hpp:251
uint32_t rva_end() const
Function end address.
Definition RuntimeFunctionX64.hpp:232
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:114
Definition optional.hpp:23
#define ENABLE_BITMASK_OPERATORS(X)
Definition enums.hpp:24
This namespace wraps code related to PE-x64 unwinding code.
Definition RuntimeFunctionX64.hpp:34
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:40
This structure represents the UNWIND_INFO which records the effects a function has on the stack point...
Definition RuntimeFunctionX64.hpp:155
uint8_t sizeof_prologue
Length of the function prolog in bytes.
Definition RuntimeFunctionX64.hpp:165
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:176
optional< uint32_t > handler
An image-relative pointer to either the function's language-specific exception or termination handler...
Definition RuntimeFunctionX64.hpp:190
bool has(UNWIND_FLAGS flag) const
Check if the given flag is used.
Definition RuntimeFunctionX64.hpp:197
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:180
std::string to_string() const
Pretty representation of this structure as a string.
uint8_t flags
See: UNWIND_FLAGS.
Definition RuntimeFunctionX64.hpp:162
std::vector< std::unique_ptr< unwind_x64::Code > > opcodes_t
Definition RuntimeFunctionX64.hpp:156
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:184
friend std::ostream & operator<<(std::ostream &os, const unwind_info_t &info)
Definition RuntimeFunctionX64.hpp:207
uint8_t version
Version number of the unwind data, currently 1 or 2.
Definition RuntimeFunctionX64.hpp:159
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:170
RuntimeFunctionX64 * chained
If UNWIND_FLAGS::CHAIN_INFO is set, this attributes references the chained runtime function.
Definition RuntimeFunctionX64.hpp:194
#define LIEF_API
Definition visibility.h:43
#define LIEF_LOCAL
Definition visibility.h:44