LIEF: Library to Instrument Executable Formats Version 2.0.0
Loading...
Searching...
No Matches
PE/Parser.hpp
Go to the documentation of this file.
1/* Copyright 2017 - 2026 R. Thomas
2 *
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
6 *
7 * http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15#ifndef LIEF_PE_PARSER_H
16#define LIEF_PE_PARSER_H
17
18#include <string_view>
19#include <map>
20#include <vector>
21
22#include "LIEF/errors.hpp"
23#include "LIEF/path.hpp"
24#include "LIEF/utils.hpp"
25#include "LIEF/visibility.h"
26
28#include "LIEF/COFF/String.hpp"
30#include "LIEF/PE/enums.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;
48struct pe_section;
49}
50
54 public:
56 static constexpr size_t MAX_DATA_SIZE = 3_GB;
57
58 static constexpr size_t MAX_TLS_CALLBACKS = 3000;
59
60 // Bounds the import thunk loop to prevent hangs on malformed IATs
61 static constexpr size_t MAX_IMPORT_ENTRIES = 0x10000;
62
63 // Bounds peek_string_at to prevent multi-megabyte reads on invalid RVAs
64 // According to https://stackoverflow.com/a/23340781
65 static constexpr size_t MAX_IMPORT_NAME_SIZE = 0x1000;
66
67 // According to https://stackoverflow.com/a/265782/87207
68 static constexpr size_t MAX_DLL_NAME_SIZE = 255;
69
71 static constexpr size_t MAX_PADDING_SIZE = 1_GB;
72
73 public:
80 static bool is_valid_import_name(std::string_view name);
81
87 static bool is_valid_dll_name(std::string_view name);
88
89 public:
91 static std::unique_ptr<Binary>
92 parse(std::string_view filename,
94
97 template<class PathT, enable_if_path_t<PathT> = 0>
98 static std::unique_ptr<Binary>
99 parse(const PathT& filename,
101 return parse(filename.string(), conf);
102 }
103
105 static std::unique_ptr<Binary>
106 parse(std::vector<uint8_t> data,
108
109 static std::unique_ptr<Binary>
110 parse(const uint8_t* buffer, size_t size,
112
114 static std::unique_ptr<Binary>
115 parse(std::unique_ptr<BinaryStream> stream,
117
119 static std::unique_ptr<Binary>
120 parse_from_memory(uintptr_t address,
122
124 static std::unique_ptr<Binary>
125 parse_from_memory(uintptr_t address, size_t size,
127
137 static std::unique_ptr<Binary>
138 parse_from_dump(std::string_view filepath, uint64_t addr,
140
143 template<class PathT, enable_if_path_t<PathT> = 0>
144 static std::unique_ptr<Binary>
145 parse_from_dump(const PathT& filepath, uint64_t addr,
147 return parse_from_dump(filepath.string(), addr, config);
148 }
149
152 static std::unique_ptr<Binary>
155
158 static std::unique_ptr<Binary>
159 parse_from_dump(std::unique_ptr<BinaryStream> stream, uint64_t addr,
161
162 Parser& operator=(const Parser& copy) = delete;
163 Parser(const Parser& copy) = delete;
164
165 COFF::String* find_coff_string(uint32_t offset) const;
166
167 ExceptionInfo* find_exception_info(uint32_t rva) const {
168 auto it = memoize_exception_info_.find(rva);
169 return it == memoize_exception_info_.end() ? nullptr : it->second;
170 }
171
172 const Binary& bin() const {
173 return *binary_;
174 }
175
177 return *binary_;
178 }
179
181 return *stream_;
182 }
183
184 const ParserConfig& config() const {
185 return config_;
186 }
187
190
191 void add_non_resolved(ExceptionInfo& info, uint32_t target) {
192 unresolved_chains_.emplace_back(&info, target);
193 }
194
195 std::unique_ptr<SpanStream> stream_from_rva(uint32_t rva, size_t size = 0);
196
197 void record_relocation(uint32_t rva, span<const uint8_t> data);
198 ok_error_t record_delta_relocation(uint32_t rva, int64_t delta, size_t size);
199
200 private:
201 struct relocation_t {
202 uint64_t size = 0;
203 uint64_t value = 0;
204 };
205 Parser(std::string_view file);
206 Parser(std::vector<uint8_t> data);
207 Parser(std::unique_ptr<BinaryStream> stream);
208
209 ~Parser();
210 Parser();
211
212 ok_error_t init(const ParserConfig& config);
213
214 template<typename PE_T>
215 ok_error_t parse();
216
217 ok_error_t parse_exports();
218 ok_error_t parse_sections();
219
220 ok_error_t read_section_content(const details::pe_section& raw_sec,
221 uint32_t index, Section& section);
222
223 template<typename PE_T>
224 ok_error_t parse_headers();
225
226 ok_error_t parse_configuration();
227
228 template<typename PE_T>
229 ok_error_t parse_data_directories();
230
231 template<typename PE_T>
232 ok_error_t parse_import_table();
233
234 template<typename PE_T>
235 ok_error_t parse_delay_imports();
236
237 template<typename PE_T>
238 ok_error_t parse_delay_names_table(DelayImport& import, uint32_t names_offset,
239 uint32_t iat_offset);
240
241 ok_error_t parse_export_table();
242 ok_error_t parse_debug();
243
244 ok_error_t parse_exceptions();
245
246 std::unique_ptr<Debug> parse_code_view(const details::pe_debug& debug_info,
247 Section* sec, span<uint8_t> payload);
248 std::unique_ptr<Debug> parse_pogo(const details::pe_debug& debug_info,
249 Section* sec, span<uint8_t> payload);
250 std::unique_ptr<Debug> parse_repro(const details::pe_debug& debug_info,
251 Section* sec, span<uint8_t> payload);
252
253 template<typename PE_T>
254 ok_error_t parse_tls();
255
256 template<typename PE_T>
257 ok_error_t parse_load_config();
258
259 template<typename PE_T>
260 ok_error_t process_load_config(LoadConfiguration& config);
261
262 template<typename PE_T>
263 ok_error_t parse_nested_relocated();
264
265 ok_error_t parse_relocations();
266 ok_error_t parse_resources();
267 ok_error_t parse_string_table();
268 ok_error_t parse_symbols();
269 ok_error_t parse_signature();
270 ok_error_t parse_overlay();
271 ok_error_t parse_dos_stub();
272 ok_error_t parse_rich_header();
273 ok_error_t parse_chpe_exceptions();
274
275 template<typename PE_T>
276 ok_error_t undo_relocations();
277
278 template<typename PE_T>
279 ok_error_t fix_iat();
280
281 template<typename PE_T>
282 ok_error_t fix_tls();
283
284 template<typename PE_T>
285 ok_error_t fix_load_config();
286
288 std::unique_ptr<Binary> binary_;
289 std::unique_ptr<BinaryStream> stream_;
290 std::map<uint32_t, size_t> memoize_coff_str_;
291 std::map<uint32_t, ExceptionInfo*> memoize_exception_info_;
292 std::map<uint32_t, relocation_t> dyn_hdr_relocs_;
293 std::vector<std::pair<ExceptionInfo*, uint32_t>> unresolved_chains_;
294 ParserConfig config_;
295};
296
297
298}
299}
300#endif
Class that is used to a read stream of data from different sources.
Definition BinaryStream.hpp:34
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:58
This class represents a generic entry in the debug data directory. For known types,...
Definition debug/Debug.hpp:38
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:32
This class represents the load configuration data associated with the IMAGE_LOAD_CONFIG_DIRECTORY.
Definition LoadConfiguration.hpp:48
Main interface to parse PE binaries. In particular, the static Parser::parse functions should be used...
Definition PE/Parser.hpp:53
static std::unique_ptr< Binary > parse_from_dump(BinaryStream &stream, uint64_t addr, const ParserConfig &config=ParserConfig::default_conf())
Same as parse_from_dump(std::string_view, uint64_t, const ParserConfig&) but the dump is wrapped in t...
static constexpr size_t MAX_IMPORT_ENTRIES
Definition PE/Parser.hpp:61
static constexpr size_t MAX_DLL_NAME_SIZE
Definition PE/Parser.hpp:68
static constexpr size_t MAX_IMPORT_NAME_SIZE
Definition PE/Parser.hpp:65
static std::unique_ptr< Binary > parse_from_dump(std::string_view filepath, uint64_t addr, const ParserConfig &config=ParserConfig::default_conf())
Parse a PE binary from a memory dump located on disk.
static std::unique_ptr< Binary > parse_from_memory(uintptr_t address, const ParserConfig &config=ParserConfig::default_conf())
Parse the PE binary at the given memory address.
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:176
const Binary & bin() const
Definition PE/Parser.hpp:172
static bool is_valid_import_name(std::string_view name)
Check if the given name is a valid import.
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:167
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:180
void record_relocation(uint32_t rva, span< const uint8_t > data)
static bool is_valid_dll_name(std::string_view name)
Check if the given name is a valid DLL name.
static constexpr size_t MAX_DATA_SIZE
Maximum size of the data read.
Definition PE/Parser.hpp:56
static std::unique_ptr< Binary > parse(std::string_view filename, const ParserConfig &conf=ParserConfig::default_conf())
Parse a PE binary from the given filename.
static std::unique_ptr< Binary > parse_from_dump(std::unique_ptr< BinaryStream > stream, uint64_t addr, const ParserConfig &config=ParserConfig::default_conf())
Same as parse_from_dump(std::string_view, uint64_t, const ParserConfig&) but the dump is wrapped in t...
const ParserConfig & config() const
Definition PE/Parser.hpp:184
static std::unique_ptr< Binary > parse(const PathT &filename, const ParserConfig &conf=ParserConfig::default_conf())
Same as parse(std::string_view, const ParserConfig&) but the file is given as a std::filesystem::path...
Definition PE/Parser.hpp:99
static std::unique_ptr< Binary > parse_from_memory(uintptr_t address, size_t size, const ParserConfig &config=ParserConfig::default_conf())
Parse the PE binary at the given memory address and with the given size.
void add_non_resolved(ExceptionInfo &info, uint32_t target)
Definition PE/Parser.hpp:191
static constexpr size_t MAX_PADDING_SIZE
Max size of the padding section.
Definition PE/Parser.hpp:71
static std::unique_ptr< Binary > parse_from_dump(const PathT &filepath, uint64_t addr, const ParserConfig &config=ParserConfig::default_conf())
Same as parse_from_dump(std::string_view, uint64_t, const ParserConfig&) but the dump file is given a...
Definition PE/Parser.hpp:145
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:58
Class which represents an entry of the PE relocation table.
Definition RelocationEntry.hpp:36
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:31
Definition SpanStream.hpp:31
Opaque structure that is used by LIEF to avoid writing result<void> f(...). Instead,...
Definition errors.hpp:118
Definition DataDirectory.hpp:37
Namespace related to the LIEF's PE module.
Definition Abstract/Header.hpp:31
PE_TYPE
Definition PE/enums.hpp:22
@ PE32_PLUS
64 bits
Definition PE/enums.hpp:24
LIEF namespace.
Definition Abstract/Binary.hpp:41
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:27
static const ParserConfig & default_conf()
Returns the default configuration for the PE Parser.
Definition PE/ParserConfig.hpp:29
#define LIEF_API
Definition visibility.h:44