LIEF: Library to Instrument Executable Formats Version 1.0.0
Loading...
Searching...
No Matches
PE/Parser.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_PARSER_H
17#define LIEF_PE_PARSER_H
18
19#include <string>
20#include <vector>
21#include <map>
22
23#include "LIEF/visibility.h"
24#include "LIEF/utils.hpp"
25#include "LIEF/errors.hpp"
26
28#include "LIEF/PE/enums.hpp"
30#include "LIEF/COFF/String.hpp"
31
32namespace LIEF {
33class BinaryStream;
34class SpanStream;
35
36namespace PE {
37class Debug;
38class ResourceNode;
39class Binary;
40class DelayImport;
41class Section;
42class ExceptionInfo;
44class RelocationEntry;
45
46namespace details {
47struct pe_debug;
48}
49
53 public:
55 static constexpr size_t MAX_DATA_SIZE = 3_GB;
56
57 static constexpr size_t MAX_TLS_CALLBACKS = 3000;
58
59 // Bounds the import thunk loop to prevent hangs on malformed IATs
60 static constexpr size_t MAX_IMPORT_ENTRIES = 0x10000;
61
62 // Bounds peek_string_at to prevent multi-megabyte reads on invalid RVAs
63 // According to https://stackoverflow.com/a/23340781
64 static constexpr size_t MAX_IMPORT_NAME_SIZE = 0x1000;
65
66 // According to https://stackoverflow.com/a/265782/87207
67 static constexpr size_t MAX_DLL_NAME_SIZE = 255;
68
70 static constexpr size_t MAX_PADDING_SIZE = 1_GB;
71
72 public:
79 static bool is_valid_import_name(const std::string& name);
80
86 static bool is_valid_dll_name(const std::string& name);
87
88 public:
90 static std::unique_ptr<Binary>
91 parse(const std::string& filename,
93
95 static std::unique_ptr<Binary>
96 parse(std::vector<uint8_t> data,
98
99 static std::unique_ptr<Binary>
100 parse(const uint8_t* buffer, size_t size,
102
104 static std::unique_ptr<Binary>
105 parse(std::unique_ptr<BinaryStream> stream,
107
108 Parser& operator=(const Parser& copy) = delete;
109 Parser(const Parser& copy) = delete;
110
111 COFF::String* find_coff_string(uint32_t offset) const;
112
113 ExceptionInfo* find_exception_info(uint32_t rva) const {
114 auto it = memoize_exception_info_.find(rva);
115 return it == memoize_exception_info_.end() ? nullptr : it->second;
116 }
117
118 const Binary& bin() const {
119 return *binary_;
120 }
121
123 return *binary_;
124 }
125
127 return *stream_;
128 }
129
130 const ParserConfig& config() const {
131 return config_;
132 }
133
136
137 void add_non_resolved(ExceptionInfo& info, uint32_t target) {
138 unresolved_chains_.emplace_back(&info, target);
139 }
140
141 std::unique_ptr<SpanStream> stream_from_rva(uint32_t rva, size_t size = 0);
142
143 void record_relocation(uint32_t rva, span<const uint8_t> data);
144 ok_error_t record_delta_relocation(uint32_t rva, int64_t delta, size_t size);
145
146 private:
147 struct relocation_t {
148 uint64_t size = 0;
149 uint64_t value = 0;
150 };
151 Parser(const std::string& file);
152 Parser(std::vector<uint8_t> data);
153 Parser(std::unique_ptr<BinaryStream> stream);
154
155 ~Parser() override;
156 Parser();
157
158 ok_error_t init(const ParserConfig& config);
159
160 template<typename PE_T>
161 ok_error_t parse();
162
163 ok_error_t parse_exports();
164 ok_error_t parse_sections();
165
166 template<typename PE_T>
167 ok_error_t parse_headers();
168
169 ok_error_t parse_configuration();
170
171 template<typename PE_T>
172 ok_error_t parse_data_directories();
173
174 template<typename PE_T>
175 ok_error_t parse_import_table();
176
177 template<typename PE_T>
178 ok_error_t parse_delay_imports();
179
180 template<typename PE_T>
181 ok_error_t parse_delay_names_table(DelayImport& import, uint32_t names_offset,
182 uint32_t iat_offset);
183
184 ok_error_t parse_export_table();
185 ok_error_t parse_debug();
186
187 ok_error_t parse_exceptions();
188
189 std::unique_ptr<Debug> parse_code_view(const details::pe_debug& debug_info,
190 Section* sec, span<uint8_t> payload);
191 std::unique_ptr<Debug> parse_pogo(const details::pe_debug& debug_info,
192 Section* sec, span<uint8_t> payload);
193 std::unique_ptr<Debug> parse_repro(const details::pe_debug& debug_info,
194 Section* sec, span<uint8_t> payload);
195
196 template<typename PE_T>
197 ok_error_t parse_tls();
198
199 template<typename PE_T>
200 ok_error_t parse_load_config();
201
202 template<typename PE_T>
203 ok_error_t process_load_config(LoadConfiguration& config);
204
205 template<typename PE_T>
206 ok_error_t parse_nested_relocated();
207
208 ok_error_t parse_relocations();
209 ok_error_t parse_resources();
210 ok_error_t parse_string_table();
211 ok_error_t parse_symbols();
212 ok_error_t parse_signature();
213 ok_error_t parse_overlay();
214 ok_error_t parse_dos_stub();
215 ok_error_t parse_rich_header();
216 ok_error_t parse_chpe_exceptions();
217
219 std::unique_ptr<Binary> binary_;
220 std::unique_ptr<BinaryStream> stream_;
221 std::map<uint32_t, size_t> memoize_coff_str_;
222 std::map<uint32_t, ExceptionInfo*> memoize_exception_info_;
223 std::map<uint32_t, relocation_t> dyn_hdr_relocs_;
224 std::vector<std::pair<ExceptionInfo*, uint32_t>> unresolved_chains_;
225 ParserConfig config_;
226};
227
228
229}
230}
231#endif
Class that is used to a read stream of data from different sources.
Definition BinaryStream.hpp:33
This class represents a string located in the COFF string table.
Definition String.hpp:34
Class which represents a PE binary This is the main interface to manage and modify a PE executable.
Definition PE/Binary.hpp:56
This class represents a generic entry in the debug data directory. For known types,...
Definition debug/Debug.hpp:40
Class that represents a PE delayed import.
Definition DelayImport.hpp:37
This class is the base class for any exception or runtime function entry.
Definition ExceptionInfo.hpp:33
This class represents the load configuration data associated with the IMAGE_LOAD_CONFIG_DIRECTORY.
Definition LoadConfiguration.hpp:47
Main interface to parse PE binaries. In particular, the static Parser::parse functions should be used...
Definition PE/Parser.hpp:52
static constexpr size_t MAX_IMPORT_ENTRIES
Definition PE/Parser.hpp:60
static constexpr size_t MAX_DLL_NAME_SIZE
Definition PE/Parser.hpp:67
static bool is_valid_import_name(const std::string &name)
Check if the given name is a valid import.
static constexpr size_t MAX_IMPORT_NAME_SIZE
Definition PE/Parser.hpp:64
static bool is_valid_dll_name(const std::string &name)
Check if the given name is a valid DLL name.
void memoize(ExceptionInfo &info)
static std::unique_ptr< Binary > parse(std::unique_ptr< BinaryStream > stream, const ParserConfig &conf=ParserConfig::default_conf())
Parse a PE binary from the given BinaryStream.
Binary & bin()
Definition PE/Parser.hpp:122
const Binary & bin() const
Definition PE/Parser.hpp:118
static std::unique_ptr< Binary > parse(const uint8_t *buffer, size_t size, const ParserConfig &conf=ParserConfig::default_conf())
COFF::String * find_coff_string(uint32_t offset) const
ExceptionInfo * find_exception_info(uint32_t rva) const
Definition PE/Parser.hpp:113
static std::unique_ptr< Binary > parse(const std::string &filename, const ParserConfig &conf=ParserConfig::default_conf())
Parse a PE binary from the given filename.
std::unique_ptr< SpanStream > stream_from_rva(uint32_t rva, size_t size=0)
Parser(const Parser &copy)=delete
void memoize(COFF::String str)
BinaryStream & stream()
Definition PE/Parser.hpp:126
void record_relocation(uint32_t rva, span< const uint8_t > data)
static constexpr size_t MAX_DATA_SIZE
Maximum size of the data read.
Definition PE/Parser.hpp:55
const ParserConfig & config() const
Definition PE/Parser.hpp:130
void add_non_resolved(ExceptionInfo &info, uint32_t target)
Definition PE/Parser.hpp:137
static constexpr size_t MAX_PADDING_SIZE
Max size of the padding section.
Definition PE/Parser.hpp:70
Parser & operator=(const Parser &copy)=delete
static std::unique_ptr< Binary > parse(std::vector< uint8_t > data, const ParserConfig &conf=ParserConfig::default_conf())
Parse a PE binary from a data buffer.
ok_error_t record_delta_relocation(uint32_t rva, int64_t delta, size_t size)
static constexpr size_t MAX_TLS_CALLBACKS
Definition PE/Parser.hpp:57
Class which represents an entry of the PE relocation table.
Definition RelocationEntry.hpp:37
Class which represents a Node in the resource tree.
Definition ResourceNode.hpp:46
Class which represents a PE section.
Definition PE/Section.hpp:46
Main interface to parse an executable regardless of its format.
Definition Abstract/Parser.hpp:30
Definition SpanStream.hpp:32
Opaque structure that is used by LIEF to avoid writing result<void> f(...). Instead,...
Definition errors.hpp:114
Definition DataDirectory.hpp:37
Namespace related to the LIEF's PE module.
Definition Abstract/Header.hpp:32
PE_TYPE
Definition PE/enums.hpp:22
@ PE32_PLUS
64 bits
Definition PE/enums.hpp:24
LIEF namespace.
Definition Abstract/Binary.hpp:40
tcb::span< ElementType, Extent > span
Definition span.hpp:22
This structure is used to configure the behavior of the PE Parser (PE::Parser).
Definition PE/ParserConfig.hpp:26
static const ParserConfig & default_conf()
Returns the default configuration for the PE Parser.
Definition PE/ParserConfig.hpp:28
#define LIEF_API
Definition visibility.h:43