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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
use lief_ffi as ffi;
use num_traits::{Num, cast};
use std::mem::size_of;
use std::path::Path;
use std::pin::Pin;
use super::builder::Config;
use super::data_directory::{DataDirectories, DataDirectory};
use super::debug::CodeViewPDB;
use super::debug::{self, DebugEntry, Entries};
use super::delay_import::{DelayImport, DelayImports};
use super::exception::RuntimeExceptionFunction;
use super::export::Export;
use super::import::{Import, Imports};
use super::load_configuration::LoadConfiguration;
use super::parser_config::Config as ParserConfig;
use super::relocation::Relocations;
use super::resources::Node as ResourceNode;
use super::resources::{Manager as ResourcesManager, NodeBase};
use super::rich_header::RichHeader;
use super::section::{Section, Sections};
use super::signature::Signatures;
use super::tls::TLS;
use super::{data_directory, signature};
use crate::coff;
use crate::coff::Symbol;
use crate::Error;
use crate::common::{AsFFI, FromFFI, into_optional};
use crate::declare_iterator;
use crate::generic;
use crate::to_conv_result;
use crate::to_slice;
use super::Algorithms;
use super::{DosHeader, Header, OptionalHeader};
/// This is the main interface to read and write PE binary attributes.
///
/// Note that this structure implements the [`generic::Binary`] trait from which other generic
/// functions are exposed
///
/// ```
/// fn use_trait(pe: &Binary) {
/// let generic_binary = pe as &dyn generic::Binary;
/// println!("{}", generic_binary.entrypoint());
/// }
///
/// ```
pub struct Binary {
ptr: cxx::UniquePtr<ffi::PE_Binary>,
}
impl FromFFI<ffi::PE_Binary> for Binary {
fn from_ffi(ptr: cxx::UniquePtr<ffi::PE_Binary>) -> Self {
Self { ptr }
}
}
impl Binary {
/// Parse from a filepath given as a string
pub fn parse<P: AsRef<Path>>(path: P) -> Option<Self> {
cxx::let_cxx_string!(__cxx_s = path.as_ref().to_str().unwrap());
let ffi = ffi::PE_Binary::parse(&__cxx_s);
if ffi.is_null() {
return None;
}
Some(Binary::from_ffi(ffi))
}
/// Parse from a string file path and with a provided configuration
pub fn parse_with_config<P: AsRef<Path>>(path: P, config: &ParserConfig) -> Option<Self> {
let ffi_config = config.to_ffi();
cxx::let_cxx_string!(__cxx_s = path.as_ref().to_str().unwrap());
let ffi = ffi::PE_Binary::parse_with_config(&__cxx_s, &ffi_config);
if ffi.is_null() {
return None;
}
Some(Binary::from_ffi(ffi))
}
/// Parse a PE binary from a memory dump located at `path` that was mapped
/// at the virtual address `addr`
pub fn parse_from_dump<P: AsRef<Path>>(path: P, addr: u64) -> Option<Self> {
cxx::let_cxx_string!(__cxx_s = path.as_ref().to_str().unwrap());
let ffi = ffi::PE_Binary::parse_from_dump(&__cxx_s, addr);
if ffi.is_null() {
return None;
}
Some(Binary::from_ffi(ffi))
}
/// Same as [`Binary::parse_from_dump`] but with a provided configuration
pub fn parse_from_dump_with_config<P: AsRef<Path>>(
path: P,
addr: u64,
config: &ParserConfig,
) -> Option<Self> {
let ffi_config = config.to_ffi();
cxx::let_cxx_string!(__cxx_s = path.as_ref().to_str().unwrap());
let ffi = ffi::PE_Binary::parse_from_dump_with_config(&__cxx_s, addr, &ffi_config);
if ffi.is_null() {
return None;
}
Some(Binary::from_ffi(ffi))
}
/// DosHeader which starts the PE files
pub fn dos_header(&self) -> DosHeader<'_> {
DosHeader::from_ffi(self.ptr.dos_header())
}
/// Header that follows the [`Binary::header`]. It is named
/// *optional* from the COFF specification but it is mandatory in a PE file.
pub fn optional_header(&self) -> OptionalHeader<'_> {
OptionalHeader::from_ffi(self.ptr.optional_header())
}
/// Re-compute the value of [`OptionalHeader::checksum`]
pub fn compute_checksum(&self) -> u32 {
self.ptr.compute_checksum()
}
/// Next header after the [`Binary::dos_header`]
pub fn header(&self) -> Header<'_> {
Header::from_ffi(self.ptr.header())
}
/// Return TLS information if present
pub fn tls(&self) -> Option<TLS<'_>> {
into_optional(self.ptr.tls())
}
/// Return rich header information if present.
pub fn rich_header(&self) -> Option<RichHeader<'_>> {
into_optional(self.ptr.rich_header())
}
/// Return export information
pub fn export(&self) -> Option<Export<'_>> {
into_optional(self.ptr.get_export())
}
/// Return the root of the PE's resource tree
pub fn resources(&self) -> Option<ResourceNode<'_>> {
into_optional(self.ptr.resources())
}
/// Return a manager interface to read and manipulate the resources tree with a user friendly
/// interface.
pub fn resources_manager(&self) -> Option<ResourcesManager<'_>> {
into_optional(self.ptr.resources_manager())
}
/// Return the imports as an **iterator** over the [`Import`] structure
pub fn imports(&self) -> Imports<'_> {
Imports::new(self.ptr.imports())
}
/// Return the data directories as an iterator over the [`DataDirectory`] structure
pub fn data_directories(&self) -> DataDirectories<'_> {
DataDirectories::new(self.ptr.data_directories())
}
/// Return the sections as an iterator over the [`Section`] structure
pub fn sections(&self) -> Sections<'_> {
Sections::new(self.ptr.sections())
}
/// Return the relocations as an iterator over the [`super::Relocation`] structure
pub fn relocations(&self) -> Relocations<'_> {
Relocations::new(self.ptr.relocations())
}
/// Return the delayed imports as an iterator over the [`DelayImport`] structure
pub fn delay_imports(&self) -> DelayImports<'_> {
DelayImports::new(self.ptr.delay_imports())
}
/// Return an iterator over the [`signature::Signature`] if the current PE is authenticode-signed.
pub fn signatures(&self) -> Signatures<'_> {
Signatures::new(self.ptr.signatures())
}
/// Return an iterator over the [`debug::Entries`] of the binary.
pub fn debug(&self) -> DebugEntries<'_> {
DebugEntries::new(self.ptr.debug())
}
/// Compute the authentihash for the current PE with the given algorithms.
pub fn authentihash(&self, algo: Algorithms) -> Vec<u8> {
Vec::from(self.ptr.authentihash(algo.into()).as_slice())
}
/// Return load configuration info if present.
pub fn load_configuration(&self) -> Option<LoadConfiguration<'_>> {
into_optional(self.ptr.load_configuration())
}
/// Return the raw data between the [`Binary::dos_header`] and the regular [`Binary::header`]
pub fn dos_stub(&self) -> &[u8] {
to_slice!(self.ptr.dos_stub());
}
/// Return the original overlay data of the file
pub fn overlay(&self) -> &[u8] {
to_slice!(self.ptr.overlay());
}
/// Return the offset computed by LIEF to identify overlay data
pub fn overlay_offset(&self) -> u64 {
self.ptr.overlay_offset()
}
/// Convert a **relative** virtual address into an offset
pub fn rva_to_offset(&self, rva: u64) -> u64 {
self.ptr.rva_to_offset(rva)
}
/// Convert an **absolute** virtual address into an offset.
pub fn va_to_offset(&self, va: u64) -> u64 {
self.ptr.va_to_offset(va)
}
/// Convert the given offset into a relative virtual address (RVA).
pub fn offset_to_rva(&self, offset: u64) -> u64 {
self.ptr.offset_to_rva(offset)
}
/// Return the size of the current binary when loaded in memory.
pub fn virtual_size(&self) -> u64 {
self.ptr.virtual_size()
}
/// Compute the size of all the headers.
pub fn sizeof_headers(&self) -> u64 {
self.ptr.sizeof_headers()
}
/// Find a section by its offset
pub fn section_from_offset(&self, offset: u64) -> Option<Section<'_>> {
into_optional(self.ptr.section_from_offset(offset))
}
/// Find a section by its **relative** virtual address
pub fn section_from_rva(&self, rva: u64) -> Option<Section<'_>> {
into_optional(self.ptr.section_from_rva(rva))
}
/// Find a section by its name
pub fn section_by_name(&self, name: &str) -> Option<Section<'_>> {
cxx::let_cxx_string!(__cxx_s = name);
into_optional(self.ptr.section_by_name(&__cxx_s))
}
/// Add a section to the binary and return the section added.
pub fn add_section(&mut self, section: Section) -> Option<Section<'_>> {
into_optional(self.ptr.as_mut().unwrap().add_section(section.as_ffi()))
}
/// Find the data directory with the given type
pub fn data_directory_by_type(
&self,
dir_type: data_directory::Type,
) -> Option<DataDirectory<'_>> {
into_optional(self.ptr.data_directory_by_type(dir_type.into()))
}
/// Verify the binary against the embedded signature(s) (if any)
///
/// First, it checks that the embedded signatures are correct (c.f. [`signature::Signature::check`])
/// and then, it checks that the authentihash matches [`crate::pe::signature::content_info::ContentInfo::digest`]
pub fn verify_signature(
&self,
checks: signature::VerificationChecks,
) -> signature::VerificationFlags {
signature::VerificationFlags::from(self.ptr.verify_signature(checks.into()))
}
/// Verify the binary with the [`signature::Signature`] object provided in the first parameter.
/// It can be used to verify a detached signature:
///
/// ```
/// if let Some(sig) = Signature::from_file(path_str.unwrap()) {
/// pe.verify_signature(&sig, signature::VerificationChecks::DEFAULT);
/// }
/// ```
pub fn verify_with_signature(
&self,
sig: &signature::Signature,
checks: signature::VerificationChecks,
) -> signature::VerificationFlags {
signature::VerificationFlags::from(
self.ptr.verify_with_signature(sig.into(), checks.into()),
)
}
/// Find an import by its DLL name (case insensitive)
pub fn import_by_name(&self, name: &str) -> Option<Import<'_>> {
cxx::let_cxx_string!(__cxx_s = name);
into_optional(self.ptr.import_by_name(&__cxx_s))
}
/// Find a delayed import by its name
pub fn delay_import_by_name(&self, name: &str) -> Option<DelayImport<'_>> {
cxx::let_cxx_string!(__cxx_s = name);
into_optional(self.ptr.delay_import_by_name(&__cxx_s))
}
/// Return the sized content from the virtual address
pub fn content_from_virtual_address(&self, address: u64, size: u64) -> &[u8] {
to_slice!(self.ptr.get_content_from_virtual_address(address, size));
}
pub fn functions(&self) -> generic::Functions<'_> {
generic::Functions::new(self.ptr.functions())
}
/// Return the data directory associated with the export table
pub fn export_dir(&self) -> Option<DataDirectory<'_>> {
into_optional(self.ptr.export_dir())
}
/// Return the data directory associated with the import table
pub fn import_dir(&self) -> Option<DataDirectory<'_>> {
into_optional(self.ptr.import_dir())
}
/// Return the data directory associated with the resources tree
pub fn rsrc_dir(&self) -> Option<DataDirectory<'_>> {
into_optional(self.ptr.rsrc_dir())
}
/// Return the data directory associated with the exceptions
pub fn exceptions_dir(&self) -> Option<DataDirectory<'_>> {
into_optional(self.ptr.exceptions_dir())
}
/// Return the data directory associated with the certificate table
/// (authenticode)
pub fn cert_dir(&self) -> Option<DataDirectory<'_>> {
into_optional(self.ptr.cert_dir())
}
/// Return the data directory associated with the relocation table
pub fn relocation_dir(&self) -> Option<DataDirectory<'_>> {
into_optional(self.ptr.relocation_dir())
}
/// Return the data directory associated with the debug table
pub fn debug_dir(&self) -> Option<DataDirectory<'_>> {
into_optional(self.ptr.debug_dir())
}
/// Return the data directory associated with TLS
pub fn tls_dir(&self) -> Option<DataDirectory<'_>> {
into_optional(self.ptr.tls_dir())
}
/// Return the data directory associated with the load config
pub fn load_config_dir(&self) -> Option<DataDirectory<'_>> {
into_optional(self.ptr.load_config_dir())
}
/// Return the data directory associated with the IAT
pub fn iat_dir(&self) -> Option<DataDirectory<'_>> {
into_optional(self.ptr.iat_dir())
}
/// Return the data directory associated with delayed imports
pub fn export_delay_dirdir(&self) -> Option<DataDirectory<'_>> {
into_optional(self.ptr.delay_dir())
}
/// Get the integer value at the given virtual address
pub fn get_int_from_virtual_address<T>(&self, addr: u64) -> Result<T, Error>
where
T: Num + cast::FromPrimitive + cast::ToPrimitive,
{
// Can't be in the generic trait because of:
// > for a trait to be "object safe" it needs to allow building a vtable to allow the call
// > to be resolvable dynamically; for more information visit
// > https://doc.rust-lang.org/reference/items/traits.html#object-safety
if size_of::<T>() == size_of::<u8>() {
to_conv_result!(
ffi::AbstractBinary::get_u8,
self.ptr.as_ref().unwrap().as_ref(),
|value| {
T::from_u8(value).unwrap_or_else(|| panic!("Can't cast value: {value}"))
},
addr
);
}
if size_of::<T>() == size_of::<u16>() {
to_conv_result!(
ffi::AbstractBinary::get_u16,
self.ptr.as_ref().unwrap().as_ref(),
|value| {
T::from_u16(value).unwrap_or_else(|| panic!("Can't cast value: {value}"))
},
addr
);
}
if size_of::<T>() == size_of::<u32>() {
to_conv_result!(
ffi::AbstractBinary::get_u32,
self.ptr.as_ref().unwrap().as_ref(),
|value| {
T::from_u32(value).unwrap_or_else(|| panic!("Can't cast value: {value}"))
},
addr
);
}
if size_of::<T>() == size_of::<u64>() {
to_conv_result!(
ffi::AbstractBinary::get_u64,
self.ptr.as_ref().unwrap().as_ref(),
|value| {
T::from_u64(value).unwrap_or_else(|| panic!("Can't cast value: {value}"))
},
addr
);
}
Err(Error::NotSupported)
}
/// Add an imported library (i.e. `DLL`) to the binary
pub fn add_import<'a>(&'a mut self, name: &str) -> Import<'a> {
cxx::let_cxx_string!(__cxx_s = name);
Import::from_ffi(self.ptr.pin_mut().add_import(&__cxx_s))
}
/// Add an imported library (i.e. `DLL`) to the binary.
///
/// The second parameter `pos` defines where to insert the import.
pub fn add_import_at_pos<'a>(&'a mut self, name: &str, pos: u32) -> Import<'a> {
cxx::let_cxx_string!(__cxx_s = name);
Import::from_ffi(self.ptr.pin_mut().add_import_pos(&__cxx_s, pos))
}
/// Remove the imported library with the given `name`
pub fn remove_import(&mut self, name: &str) {
cxx::let_cxx_string!(__cxx_s = name);
self.ptr.pin_mut().remove_import(&__cxx_s);
}
/// Remove all libraries in the binary
pub fn remove_all_imports(&mut self) {
self.ptr.pin_mut().remove_all_imports();
}
/// Remove the TLS from the binary
pub fn remove_tls(&mut self) {
self.ptr.pin_mut().remove_tls();
}
/// Set or change the TLS information
pub fn set_tls(&mut self, tls: &TLS) {
self.ptr.pin_mut().set_tls(tls.as_ffi());
}
/// Change or set the resources tree to given node
pub fn set_resources(&mut self, node: &dyn NodeBase) {
self.ptr.pin_mut().set_resources(node.get_base());
}
/// Add a new debug entry
pub fn add_debug_info<'a>(&'a mut self, entry: &dyn DebugEntry) -> Option<Entries<'a>> {
into_optional(self.ptr.pin_mut().add_debug_info(entry.get_base()))
}
/// Remove a specific debug entry
pub fn remove_debug(&mut self, entry: &dyn DebugEntry) -> bool {
self.ptr.pin_mut().remove_debug(entry.get_base())
}
/// Remove all debug info
pub fn clear_debug(&mut self) -> bool {
self.ptr.pin_mut().clear_debug()
}
/// Return the [`CodeViewPDB`] object if present
pub fn codeview_pdb(&self) -> Option<CodeViewPDB<'_>> {
into_optional(self.ptr.codeview_pdb())
}
/// Write back the current PE binary into the file specified in parameter
pub fn write<P: AsRef<Path>>(&mut self, output: P) {
cxx::let_cxx_string!(__cxx_s = output.as_ref().to_str().unwrap());
self.ptr.as_mut().unwrap().write(&__cxx_s);
}
/// Write back the current PE binary into the file specified in parameter with the
/// configuration provided in the second parameter.
pub fn write_with_config<P: AsRef<Path>>(&mut self, output: P, config: Config) {
let ffi_config = config.to_ffi();
cxx::let_cxx_string!(__cxx_s = output.as_ref().to_str().unwrap());
self.ptr
.as_mut()
.unwrap()
.write_with_config(&__cxx_s, ffi_config.as_ref().unwrap());
}
/// Rebuild the current PE binary and return the raw bytes.
///
/// This is similar to [`Binary::write`] but instead of writing the result
/// into a file, it returns the reconstructed binary as a buffer.
pub fn write_to_bytes(&mut self) -> Vec<u8> {
Vec::from(self.ptr.as_mut().unwrap().write_to_bytes().as_slice())
}
/// Rebuild the current PE binary with the configuration provided in the
/// parameter and return the raw bytes.
///
/// This is similar to [`Binary::write_with_config`] but instead of writing
/// the result into a file, it returns the reconstructed binary as a buffer.
pub fn write_to_bytes_with_config(&mut self, config: Config) -> Vec<u8> {
let ffi_config = config.to_ffi();
Vec::from(
self.ptr
.as_mut()
.unwrap()
.write_to_bytes_with_config(ffi_config.as_ref().unwrap())
.as_slice(),
)
}
/// Iterator over the strings located in the COFF string table
pub fn coff_string_table(&self) -> COFFStrings<'_> {
COFFStrings::new(self.ptr.coff_string_table())
}
/// Return an iterator over the binary (COFF) symbols (if any).
pub fn symbols(&self) -> Symbols<'_> {
Symbols::new(self.ptr.symbols())
}
/// Try to find the COFF string at the given offset in the COFF string table.
///
/// <div class="warning">
/// This offset must include the first 4 bytes holding the size of the table.
/// Hence, the first string starts a the offset 4.
/// </div>
pub fn find_coff_string_at(&self, offset: u32) -> Option<coff::String<'_>> {
into_optional(self.ptr.find_coff_string_at(offset))
}
/// Iterator over the exception (`_RUNTIME_FUNCTION`) functions
///
/// This function requires that the option [`ParserConfig::parse_exceptions`] was turned on
/// (default is `false`) when parsing the binary.
pub fn exceptions(&self) -> Exceptions<'_> {
Exceptions::new(self.ptr.exceptions())
}
/// Try to find the exception info at the given RVA
///
/// This function requires that the option [`ParserConfig::parse_exceptions`] was turned on
/// (default is `false`) when parsing the binary.
pub fn find_exception_at(&self, rva: u32) -> Option<RuntimeExceptionFunction<'_>> {
into_optional(self.ptr.find_exception_at(rva))
}
/// True if this binary is compiled in ARM64EC mode (emulation compatible)
pub fn is_arm64ec(&self) -> bool {
self.ptr.is_arm64ec()
}
/// True if this binary is compiled in ARM64X mode (contains both ARM64 and ARM64EC).
pub fn is_arm64x(&self) -> bool {
self.ptr.is_arm64x()
}
/// If the current binary contains dynamic relocations
/// (e.g. LIEF::PE::DynamicFixupARM64X), this function returns the
/// **relocated** view of the current PE.
///
/// This can be used to get the alternative PE binary, targeting a different
/// architectures.
///
/// <div class="warning">
/// This function is <b>moving</b> and taking the ownership of the nested
/// PE binary. This means that subsequent calls to this function will return None.
/// </div>
///
/// This function requires that the option [`ParserConfig::parse_arm64x_binary`] was turned on
/// (default is `false`) when parsing the binary.
pub fn nested_pe_binary(&self) -> Option<Binary> {
into_optional(self.ptr.nested_pe_binary())
}
/// Set or change the export table
pub fn set_export(&mut self, export: &Export) {
self.ptr.pin_mut().set_export(export.as_ffi());
}
/// Whether the current binary is a reproducible build
pub fn is_reproducible_build(&self) -> bool {
self.ptr.is_reproducible_build()
}
/// Check if the binary imports the given library name
pub fn has_import(&self, name: &str) -> bool {
cxx::let_cxx_string!(__cxx_s = name.to_string());
self.ptr.has_import(&__cxx_s)
}
/// Check if the binary has a delay import with the given name
pub fn has_delay_import(&self, name: &str) -> bool {
cxx::let_cxx_string!(__cxx_s = name.to_string());
self.ptr.has_delay_import(&__cxx_s)
}
/// Return the exception functions (from the exception directory)
pub fn exception_functions(&self) -> generic::Functions<'_> {
generic::Functions::new(self.ptr.exception_functions())
}
/// Remove the section with the given name
pub fn remove_section(&mut self, name: &str, clear: bool) {
cxx::let_cxx_string!(__cxx_s = name.to_string());
self.ptr.pin_mut().remove_section(&__cxx_s, clear);
}
/// Fill the content at the given virtual address with the specified value
pub fn fill_address(&mut self, address: u64, size: u64, value: u8) {
self.ptr.pin_mut().fill_address(address, size, value);
}
/// Remove all relocations
pub fn remove_all_relocations(&mut self) {
self.ptr.pin_mut().remove_all_relocations();
}
}
impl AsFFI<ffi::PE_Binary> for Binary {
fn as_ffi(&self) -> &ffi::PE_Binary {
self.ptr.as_ref().unwrap()
}
fn as_mut_ffi(&mut self) -> std::pin::Pin<&mut ffi::PE_Binary> {
self.ptr.pin_mut()
}
}
impl std::fmt::Debug for Binary {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.debug_struct("Binary").finish()
}
}
impl generic::Binary for Binary {
fn as_generic(&self) -> &ffi::AbstractBinary {
self.ptr.as_ref().unwrap().as_ref()
}
fn as_pin_mut_generic(&mut self) -> Pin<&mut ffi::AbstractBinary> {
unsafe {
Pin::new_unchecked({
(self.ptr.as_ref().unwrap().as_ref() as *const ffi::AbstractBinary
as *mut ffi::AbstractBinary)
.as_mut()
.unwrap()
})
}
}
}
declare_iterator!(
DebugEntries,
debug::Entries<'a>,
ffi::PE_Debug,
ffi::PE_Binary,
ffi::PE_Binary_it_debug
);
declare_iterator!(
COFFStrings,
coff::String<'a>,
ffi::PE_COFFString,
ffi::PE_Binary,
ffi::PE_Binary_it_strings_table
);
declare_iterator!(
Symbols,
Symbol<'a>,
ffi::PE_Symbol,
ffi::PE_Binary,
ffi::PE_Binary_it_symbols
);
declare_iterator!(
Exceptions,
RuntimeExceptionFunction<'a>,
ffi::PE_ExceptionInfo,
ffi::PE_Binary,
ffi::PE_Binary_it_exceptions
);