LIEF: Library to Instrument Executable Formats Version 2.0.0
Loading...
Searching...
No Matches
PE/Builder.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_BUILDER_H
16#define LIEF_PE_BUILDER_H
17
18#include <functional>
19#include <ostream>
20#include <string>
21#include <vector>
22
23#include "LIEF/iostream.hpp"
24#include "LIEF/visibility.h"
25
26#include "LIEF/errors.hpp"
27
28
29namespace LIEF::PE {
30class Binary;
31class ResourceNode;
33class ResourceData;
34class DosHeader;
35class Header;
36class OptionalHeader;
37class DataDirectory;
38class Section;
39class Import;
40class ImportEntry;
41
44 public:
52 struct config_t {
57 bool imports = false;
58
63 bool exports = false;
64
66 bool resources = true;
67
69 bool relocations = true;
70
72 bool load_configuration = true;
73
75 bool tls = true;
76
78 bool overlay = true;
79
81 bool debug = true;
82
85 bool dos_stub = true;
86
89 std::string rsrc_section = ".rsrc";
90
92 std::string idata_section = ".idata";
93
95 std::string tls_section = ".tls";
96
98 std::string reloc_section = ".reloc";
99
101 std::string export_section = ".edata";
102
104 std::string debug_section = ".debug";
105
107 std::function<void(Binary*, const Import*, const ImportEntry*, uint32_t)>;
109
111 bool force_relocating = false;
112 };
113
114 Builder() = delete;
115 Builder(Binary& binary, config_t config) :
116 binary_(&binary),
117 config_(std::move(config)) {}
118
120
123
125 const std::vector<uint8_t>& get_build() {
126 return ios_.raw();
127 }
128
130 void write(const std::string& filename) const;
131
133 void write(std::ostream& os) const;
134
135 ok_error_t build(const DosHeader& dos_header);
136 ok_error_t build(const Header& header);
137 ok_error_t build(const OptionalHeader& optional_header);
138 ok_error_t build(const DataDirectory& data_directory);
139 ok_error_t build(const Section& section);
141
145
147
148 template<typename PE_T>
150
151 template<typename PE_T>
153
154 template<typename PE_T>
156
157 const std::vector<uint8_t>& rsrc_data() const {
158 return rsrc_data_;
159 }
160
161 protected:
162 struct tls_data_t {
163 std::vector<uint8_t> header;
164 std::vector<uint8_t> callbacks;
165 };
166
167 struct rsrc_sizing_info_t {
168 uint32_t header_size = 0;
169 uint32_t data_size = 0;
170 uint32_t name_size = 0;
171 };
172
173 struct rsrc_build_context_t {
174 uint32_t offset_header = 0;
175 uint32_t offset_data = 0;
176 uint32_t offset_name = 0;
177 uint32_t depth = 0;
178 std::vector<uint64_t> rva_fixups;
179 };
180
181 template<typename PE_T>
182 ok_error_t build_optional_header(const OptionalHeader& optional_header);
183
184 static ok_error_t compute_resources_size(const ResourceNode& node,
185 rsrc_sizing_info_t& info);
186
187 static ok_error_t construct_resource(vector_iostream& ios, ResourceNode& node,
188 rsrc_build_context_t& ctx);
189
190 static ok_error_t construct_resource(vector_iostream& ios,
191 ResourceDirectory& dir,
192 rsrc_build_context_t& ctx);
193
194 static ok_error_t construct_resource(vector_iostream& ios, ResourceData& dir,
195 rsrc_build_context_t& ctx);
196
197 mutable vector_iostream ios_;
198 Binary* binary_ = nullptr;
199 config_t config_;
200 std::vector<uint8_t> reloc_data_;
201 std::vector<uint8_t> rsrc_data_;
202 tls_data_t tls_data_;
203};
204
205}
206
207#endif
Class which represents a PE binary This is the main interface to manage and modify a PE executable.
Definition PE/Binary.hpp:58
ok_error_t build_load_config()
ok_error_t build_tls()
ok_error_t build_relocations()
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)
Builder(Binary &binary, config_t config)
Definition PE/Builder.hpp:115
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:157
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:125
Class that represents a PE data directory entry.
Definition DataDirectory.hpp:42
Class which represents the DosHeader, the first structure present at the beginning of a PE file.
Definition DosHeader.hpp:40
Class that represents the PE header (which follows the DosHeader).
Definition PE/Header.hpp:39
Class that represents an entry (i.e. an import) in the import table (Import).
Definition ImportEntry.hpp:35
Class that represents a PE import.
Definition Import.hpp:40
Class which represents the PE OptionalHeader structure.
Definition OptionalHeader.hpp:46
Class which represents a Data Node in the PE resources tree.
Definition ResourceData.hpp:35
Definition ResourceDirectory.hpp:34
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:118
Namespace related to the LIEF's PE module.
Definition Abstract/Header.hpp:31
This structure is used to configure the build operation.
Definition PE/Builder.hpp:52
resolved_iat_cbk_t resolved_iat_cbk
Definition PE/Builder.hpp:108
std::function< void(Binary *, const Import *, const ImportEntry *, uint32_t)> resolved_iat_cbk_t
Definition PE/Builder.hpp:106
bool debug
Whether the builder should regenerate debug entries.
Definition PE/Builder.hpp:81
bool load_configuration
Whether the builder should regenerate the load config.
Definition PE/Builder.hpp:72
std::string debug_section
Section that holds the debug entries.
Definition PE/Builder.hpp:104
bool overlay
Whether the builder should write back any overlay data.
Definition PE/Builder.hpp:78
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:89
bool relocations
Whether the builder should regenerate relocations.
Definition PE/Builder.hpp:69
bool exports
Whether the builder should reconstruct the export table This option should be turned on if you modify...
Definition PE/Builder.hpp:63
bool dos_stub
Whether the builder should write back dos stub (including the rich header).
Definition PE/Builder.hpp:85
bool tls
Whether the builder should regenerate the TLS info.
Definition PE/Builder.hpp:75
std::string reloc_section
Section that holds the relocated relocations.
Definition PE/Builder.hpp:98
std::string idata_section
Section that holds the relocated import table (IAT/ILT).
Definition PE/Builder.hpp:92
std::string export_section
Section that holds the export table.
Definition PE/Builder.hpp:101
bool resources
Whether the builder should regenerate the resources tree.
Definition PE/Builder.hpp:66
bool imports
Whether the builder should reconstruct the imports table. This option should be turned on if you modi...
Definition PE/Builder.hpp:57
std::string tls_section
Section that holds the relocated TLS info.
Definition PE/Builder.hpp:95
#define LIEF_API
Definition visibility.h:44