LIEF: Library to Instrument Executable Formats Version 0.17.0
Loading...
Searching...
No Matches
PE/Parser.hpp
Go to the documentation of this file.
1/* Copyright 2017 - 2025 R. Thomas
2 * Copyright 2017 - 2025 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"
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}
49class LIEF_API Parser : public LIEF::Parser {
53 public:
54 static constexpr size_t MAX_DATA_SIZE = 3_GB;
57
58 static constexpr size_t MAX_TLS_CALLBACKS = 3000;
59
60 // According to https://stackoverflow.com/a/265782/87207
61 static constexpr size_t MAX_DLL_NAME_SIZE = 255;
62 static constexpr size_t MAX_PADDING_SIZE = 1_GB;
65
66 public: static bool is_valid_import_name(const std::string& name);
73 static bool is_valid_dll_name(const std::string& name);
80
81 public: static std::unique_ptr<Binary> parse(const std::string& filename,
84 const ParserConfig& conf = ParserConfig::default_conf());
85 static std::unique_ptr<Binary> parse(std::vector<uint8_t> data,
88 const ParserConfig& conf = ParserConfig::default_conf());
89
90 static std::unique_ptr<Binary> parse(const uint8_t* buffer, size_t size,
91 const ParserConfig& conf = ParserConfig::default_conf());
92 static std::unique_ptr<Binary> parse(std::unique_ptr<BinaryStream> stream,
95 const ParserConfig& conf = ParserConfig::default_conf());
96
97 Parser& operator=(const Parser& copy) = delete;
98 Parser(const Parser& copy) = delete;
99
100 COFFString* find_coff_string(uint32_t offset) const;
101
102 ExceptionInfo* find_exception_info(uint32_t rva) const {
103 auto it = memoize_exception_info_.find(rva);
104 return it == memoize_exception_info_.end() ? nullptr : it->second;
105 }
106
107 const Binary& bin() const {
108 return *binary_;
109 }
110
111 Binary& bin() {
112 return *binary_;
113 }
114
115 BinaryStream& stream() {
116 return *stream_;
117 }
118
119 const ParserConfig& config() const {
120 return config_;
121 }
122
123 void memoize(ExceptionInfo& info);
124 void memoize(COFFString str);
125
126 void add_non_resolved(ExceptionInfo& info, uint32_t target) {
127 unresolved_chains_.emplace_back(&info, target);
128 }
129
130 std::unique_ptr<SpanStream> stream_from_rva(uint32_t rva, size_t size = 0);
131
132 void record_relocation(uint32_t rva, span<const uint8_t> data);
133 ok_error_t record_delta_relocation(uint32_t rva, int64_t delta, size_t size);
134
135 private:
136 struct relocation_t {
137 uint64_t size = 0;
138 uint64_t value = 0;
139 };
140 Parser(const std::string& file);
141 Parser(std::vector<uint8_t> data);
142 Parser(std::unique_ptr<BinaryStream> stream);
143
144 ~Parser() override;
145 Parser();
146
147 ok_error_t init(const ParserConfig& config);
148
149 template<typename PE_T>
150 ok_error_t parse();
151
152 ok_error_t parse_exports();
153 ok_error_t parse_sections();
154
155 template<typename PE_T>
156 ok_error_t parse_headers();
157
158 ok_error_t parse_configuration();
159
160 template<typename PE_T>
161 ok_error_t parse_data_directories();
162
163 template<typename PE_T>
164 ok_error_t parse_import_table();
165
166 template<typename PE_T>
167 ok_error_t parse_delay_imports();
168
169 template<typename PE_T>
170 ok_error_t parse_delay_names_table(DelayImport& import, uint32_t names_offset,
171 uint32_t iat_offset);
172
173 ok_error_t parse_export_table();
174 ok_error_t parse_debug();
175
176 ok_error_t parse_exceptions();
177
178 std::unique_ptr<Debug> parse_code_view(const details::pe_debug& debug_info,
179 Section* sec, span<uint8_t> payload);
180 std::unique_ptr<Debug> parse_pogo(const details::pe_debug& debug_info,
181 Section* sec, span<uint8_t> payload);
182 std::unique_ptr<Debug> parse_repro(const details::pe_debug& debug_info,
183 Section* sec, span<uint8_t> payload);
184
185 template<typename PE_T>
186 ok_error_t parse_tls();
187
188 template<typename PE_T>
189 ok_error_t parse_load_config();
190
191 template<typename PE_T>
192 ok_error_t process_load_config(LoadConfiguration& config);
193
194 template<typename PE_T>
195 ok_error_t parse_nested_relocated();
196
197 ok_error_t parse_relocations();
198 ok_error_t parse_resources();
199 ok_error_t parse_string_table();
200 ok_error_t parse_symbols();
201 ok_error_t parse_signature();
202 ok_error_t parse_overlay();
203 ok_error_t parse_dos_stub();
204 ok_error_t parse_rich_header();
205 ok_error_t parse_chpe_exceptions();
206
207 PE_TYPE type_ = PE_TYPE::PE32_PLUS;
208 std::unique_ptr<Binary> binary_;
209 std::unique_ptr<BinaryStream> stream_;
210 std::map<uint32_t, size_t> memoize_coff_str_;
211 std::map<uint32_t, ExceptionInfo*> memoize_exception_info_;
212 std::map<uint32_t, relocation_t> dyn_hdr_relocs_;
213 std::vector<std::pair<ExceptionInfo*, uint32_t>> unresolved_chains_;
214 ParserConfig config_;
215};
216
217
218}
219}
220#endif
Class that is used to a read stream of data from different sources.
Definition BinaryStream.hpp:33
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:36
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:46
Main interface to parse PE binaries. In particular the static functions: Parser::parse should be used...
Definition PE/Parser.hpp:52
static bool is_valid_import_name(const std::string &name)
Check if the given name is a valid import.
void memoize(COFFString str)
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:111
const Binary & bin() const
Definition PE/Parser.hpp:107
static std::unique_ptr< Binary > parse(const uint8_t *buffer, size_t size, const ParserConfig &conf=ParserConfig::default_conf())
ExceptionInfo * find_exception_info(uint32_t rva) const
Definition PE/Parser.hpp:102
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
BinaryStream & stream()
Definition PE/Parser.hpp:115
void record_relocation(uint32_t rva, span< const uint8_t > data)
COFFString * find_coff_string(uint32_t offset) const
const ParserConfig & config() const
Definition PE/Parser.hpp:119
void add_non_resolved(ExceptionInfo &info, uint32_t target)
Definition PE/Parser.hpp:126
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)
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:45
Class which represents a PE section.
Definition PE/Section.hpp:43
Definition SpanStream.hpp:32
Definition DataDirectory.hpp:37
Namespace related to the LIEF's PE module.
Definition Abstract/Header.hpp:32
PE_TYPE
Definition PE/enums.hpp:22
LIEF namespace.
Definition Abstract/Binary.hpp:36
result< ok_t > ok_error_t
Opaque structure that is used by LIEF to avoid writing result<void> f(...). Instead,...
Definition errors.hpp:109
#define LIEF_API
Definition visibility.h:41