1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
use lief_ffi as ffi;
/// Structure used to configure the [`crate::pe::Binary::write_with_config`] operation
///
/// The default value of these attributes is set to `false` if the
/// operation modifies the binary layout even though nothing changed.
/// For instance, building the import table **always** requires relocating the
/// table to another place. Thus, the default value is false and must
/// be explicitly set to true.
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
pub struct Config {
/// Whether the builder should reconstruct the imports table. This option should be turned on
/// if you modify imports.
///
/// Please check LIEF website for more details
pub imports: bool,
/// Whether the builder should reconstruct the export table This option should be turned on if
/// you modify exports.
///
/// Please check LIEF website for more details
pub exports: bool,
/// Whether the builder should regenerate the resources tree
pub resources: bool,
/// Whether the builder should regenerate relocations
pub relocations: bool,
/// Whether the builder should regenerate the load config
pub load_configuration: bool,
/// Whether the builder should regenerate the TLS info
pub tls: bool,
/// Whether the builder should write back any overlay data
pub overlay: bool,
/// Whether the builder should regenerate debug entries
pub debug: bool,
/// Whether the builder should write back dos stub (including the rich
/// header)
pub dos_stub: bool,
/// If the resources tree needs to be relocated, this defines the name of
/// the new section that contains the relocated tree.
pub rsrc_section: String,
/// Section that holds the relocated import table (IAT/ILT)
pub idata_section: String,
/// Section that holds the relocated TLS info
pub tls_section: String,
/// Section that holds the relocated relocations
pub reloc_section: String,
/// Section that holds the export table
pub export_section: String,
/// Section that holds the debug entries
pub debug_section: String,
}
impl Default for Config {
fn default() -> Config {
Config {
imports: false,
exports: false,
resources: true,
relocations: true,
load_configuration: true,
tls: true,
overlay: true,
debug: true,
dos_stub: true,
rsrc_section: ".rsrc".to_string(),
idata_section: ".idata".to_string(),
tls_section: ".tls".to_string(),
reloc_section: ".reloc".to_string(),
export_section: ".edata".to_string(),
debug_section: ".debug".to_string(),
}
}
}
impl Config {
#[doc(hidden)]
pub fn to_ffi(&self) -> cxx::UniquePtr<ffi::PE_Binary_write_config_t> {
let mut ptr = ffi::PE_Binary_write_config_t::create();
ptr.pin_mut().set_import(self.imports);
ptr.pin_mut().set_exports(self.exports);
ptr.pin_mut().set_resources(self.resources);
ptr.pin_mut().set_relocations(self.relocations);
ptr.pin_mut().set_load_config(self.load_configuration);
ptr.pin_mut().set_tls(self.tls);
ptr.pin_mut().set_overlay(self.overlay);
ptr.pin_mut().set_dos_stub(self.dos_stub);
cxx::let_cxx_string!(rsrc = self.rsrc_section.as_str());
ptr.pin_mut().set_rsrc_section(&rsrc);
cxx::let_cxx_string!(idata = self.idata_section.as_str());
ptr.pin_mut().set_idata_section(&idata);
cxx::let_cxx_string!(tls = self.tls_section.as_str());
ptr.pin_mut().set_tls_section(&tls);
cxx::let_cxx_string!(reloc = self.reloc_section.as_str());
ptr.pin_mut().set_reloc_section(&reloc);
cxx::let_cxx_string!(export = self.export_section.as_str());
ptr.pin_mut().set_export_section(&export);
cxx::let_cxx_string!(debug = self.debug_section.as_str());
ptr.pin_mut().set_debug_section(&debug);
ptr
}
}