LIEF: Library to Instrument Executable Formats Version 0.17.0
Loading...
Searching...
No Matches
PE/Builder.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_BUILDER_H
17#define LIEF_PE_BUILDER_H
18
19#include <cstring>
20#include <string>
21#include <vector>
22#include <functional>
23#include <ostream>
24
25#include "LIEF/visibility.h"
26#include "LIEF/iostream.hpp"
27
28#include "LIEF/errors.hpp"
29
30namespace LIEF {
31namespace PE {
32class Binary;
33class ResourceNode;
35class ResourceData;
36class DosHeader;
37class Header;
38class OptionalHeader;
39class DataDirectory;
40class Section;
41class Import;
42class ImportEntry;
43
46 public:
54 struct config_t {
59 bool imports = false;
60
65 bool exports = false;
66
68 bool resources = true;
69
71 bool relocations = true;
72
74 bool load_configuration = true;
75
77 bool tls = true;
78
80 bool overlay = true;
81
83 bool debug = true;
84
87 bool dos_stub = true;
88
91 std::string rsrc_section = ".rsrc";
92
94 std::string idata_section = ".idata";
95
97 std::string tls_section = ".tls";
98
100 std::string reloc_section = ".reloc";
101
103 std::string export_section = ".edata";
104
106 std::string debug_section = ".debug";
107
109 std::function<void(Binary*, const Import*, const ImportEntry*, uint32_t)>;
111
113 bool force_relocating = false;
114 };
115
116 Builder() = delete;
117 Builder(Binary& binary, const config_t& config) :
118 binary_(&binary),
119 config_(config)
120 {}
121
123
126
128 const std::vector<uint8_t>& get_build() {
129 return ios_.raw();
130 }
131
133 void write(const std::string& filename) const;
134
136 void write(std::ostream& os) const;
137
138 ok_error_t build(const DosHeader& dos_header);
139 ok_error_t build(const Header& header);
140 ok_error_t build(const OptionalHeader& optional_header);
141 ok_error_t build(const DataDirectory& data_directory);
142 ok_error_t build(const Section& section);
144
148
150
151 template<typename PE_T>
153
154 template<typename PE_T>
156
157 template<typename PE_T>
159
160 const std::vector<uint8_t>& rsrc_data() const {
161 return rsrc_data_;
162 }
163
164 protected:
165 struct tls_data_t {
166 std::vector<uint8_t> header;
167 std::vector<uint8_t> callbacks;
168 };
169
170 struct rsrc_sizing_info_t {
171 uint32_t header_size = 0;
172 uint32_t data_size = 0;
173 uint32_t name_size = 0;
174 };
175
176 struct rsrc_build_context_t {
177 uint32_t offset_header = 0;
178 uint32_t offset_data = 0;
179 uint32_t offset_name = 0;
180 uint32_t depth = 0;
181 std::vector<uint64_t> rva_fixups;
182 };
183
184 template<typename PE_T>
185 ok_error_t build_optional_header(const OptionalHeader& optional_header);
186
187 static ok_error_t compute_resources_size(const ResourceNode& node,
188 rsrc_sizing_info_t& info);
189
190 static ok_error_t construct_resource(
191 vector_iostream& ios, ResourceNode& node, rsrc_build_context_t& ctx);
192
193 static ok_error_t construct_resource(
194 vector_iostream& ios, ResourceDirectory& dir, rsrc_build_context_t& ctx);
195
196 static ok_error_t construct_resource(
197 vector_iostream& ios, ResourceData& dir, rsrc_build_context_t& ctx);
198
199 mutable vector_iostream ios_;
200 Binary* binary_ = nullptr;
201 config_t config_;
202 std::vector<uint8_t> reloc_data_;
203 std::vector<uint8_t> rsrc_data_;
204 tls_data_t tls_data_;
205};
206
207}
208}
209#endif
Class which represents a PE binary This is the main interface to manage and modify a PE executable.
Definition PE/Binary.hpp:57
ok_error_t build_load_config()
ok_error_t build_tls()
ok_error_t build_relocations()
Builder(Binary &binary, const config_t &config)
Definition PE/Builder.hpp:117
ok_error_t build_overlay()
ok_error_t build_exports()
ok_error_t build_resources()
ok_error_t build(const Header &header)
ok_error_t build_debug_info()
ok_error_t build(const DataDirectory &data_directory)
ok_error_t build(const Section &section)
ok_error_t build(const OptionalHeader &optional_header)
ok_error_t build()
Perform the build process.
void write(std::ostream &os) const
Write the build result into the os stream.
void write(const std::string &filename) const
Write the build result into the output file.
const std::vector< uint8_t > & rsrc_data() const
Definition PE/Builder.hpp:160
ok_error_t build(const DosHeader &dos_header)
ok_error_t build_imports()
const std::vector< uint8_t > & get_build()
Return the build result.
Definition PE/Builder.hpp:128
Class that represents a PE data directory entry.
Definition DataDirectory.hpp:42
Class which represents the DosHeader, the first structure presents at the beginning of a PE file.
Definition DosHeader.hpp:38
Class that represents the PE header (which follows the DosHeader).
Definition PE/Header.hpp:36
Class that represents an entry (i.e. an import) in the import table (Import).
Definition ImportEntry.hpp:36
Class that represents a PE import.
Definition Import.hpp:40
Class which represents the PE OptionalHeader structure.
Definition OptionalHeader.hpp:42
Class which represents a Data Node in the PE resources tree.
Definition ResourceData.hpp:33
Definition ResourceDirectory.hpp:33
Class which represents a Node in the resource tree.
Definition ResourceNode.hpp:46
Class which represents a PE section.
Definition PE/Section.hpp:46
Opaque structure that is used by LIEF to avoid writing result<void> f(...). Instead,...
Definition errors.hpp:112
Namespace related to the LIEF's PE module.
Definition Abstract/Header.hpp:32
LIEF namespace.
Definition Abstract/Binary.hpp:40
This structure is used to configure the build operation.
Definition PE/Builder.hpp:54
resolved_iat_cbk_t resolved_iat_cbk
Definition PE/Builder.hpp:110
bool debug
Whether the builder should regenerate debug entries.
Definition PE/Builder.hpp:83
bool load_configuration
Whether the builder should regenerate the load config.
Definition PE/Builder.hpp:74
std::string debug_section
Section that holds the debug entries.
Definition PE/Builder.hpp:106
bool overlay
Whether the builder should write back any overlay data.
Definition PE/Builder.hpp:80
std::string rsrc_section
If the resources tree needs to be relocated, this defines the name of the new section that contains t...
Definition PE/Builder.hpp:91
bool relocations
Whether the builder should regenerate relocations.
Definition PE/Builder.hpp:71
bool exports
Whether the builder should reconstruct the export table This option should be turned on if you modify...
Definition PE/Builder.hpp:65
bool dos_stub
Whether the builder should write back dos stub (including the rich header).
Definition PE/Builder.hpp:87
std::function< void(Binary *, const Import *, const ImportEntry *, uint32_t)> resolved_iat_cbk_t
Definition PE/Builder.hpp:108
bool tls
Whether the builder should regenerate the TLS info.
Definition PE/Builder.hpp:77
std::string reloc_section
Section that holds the relocated relocations.
Definition PE/Builder.hpp:100
std::string idata_section
Section that holds the relocated import table (IAT/ILT).
Definition PE/Builder.hpp:94
std::string export_section
Section that holds the export table.
Definition PE/Builder.hpp:103
bool resources
Whether the builder should regenerate the resources tree.
Definition PE/Builder.hpp:68
bool imports
Whether the builder should reconstruct the imports table. This option should be turned on if you modi...
Definition PE/Builder.hpp:59
std::string tls_section
Section that holds the relocated TLS info.
Definition PE/Builder.hpp:97
#define LIEF_API
Definition visibility.h:41