pub struct Writer<'a> { /* private fields */ }
Expand description
A helper for writing PE files.
Writing uses a two phase approach. The first phase reserves file ranges and virtual address ranges for everything in the order that they will be written.
The second phase writes everything out in order. Thus the caller must ensure writing is in the same order that file ranges were reserved.
Implementations§
source§impl<'a> Writer<'a>
impl<'a> Writer<'a>
sourcepub fn new(
is_64: bool,
section_alignment: u32,
file_alignment: u32,
buffer: &'a mut dyn WritableBuffer
) -> Self
pub fn new( is_64: bool, section_alignment: u32, file_alignment: u32, buffer: &'a mut dyn WritableBuffer ) -> Self
Create a new Writer
.
sourcepub fn virtual_len(&self) -> u32
pub fn virtual_len(&self) -> u32
Return the current virtual address size that has been reserved.
This is only valid after section headers have been reserved.
sourcepub fn reserve_virtual(&mut self, len: u32) -> u32
pub fn reserve_virtual(&mut self, len: u32) -> u32
Reserve a virtual address range with the given size.
The reserved length will be increased to match the section alignment.
Returns the aligned offset of the start of the range.
sourcepub fn reserve_virtual_until(&mut self, address: u32)
pub fn reserve_virtual_until(&mut self, address: u32)
Reserve up to the given virtual address.
The reserved length will be increased to match the section alignment.
sourcepub fn reserved_len(&self) -> u32
pub fn reserved_len(&self) -> u32
Return the current file length that has been reserved.
sourcepub fn reserve(&mut self, len: u32, align_start: u32) -> u32
pub fn reserve(&mut self, len: u32, align_start: u32) -> u32
Reserve a file range with the given size and starting alignment.
Returns the aligned offset of the start of the range.
sourcepub fn reserve_file(&mut self, len: u32) -> u32
pub fn reserve_file(&mut self, len: u32) -> u32
Reserve a file range with the given size and using the file alignment.
Returns the aligned offset of the start of the range.
sourcepub fn reserve_align(&mut self, align_start: u32)
pub fn reserve_align(&mut self, align_start: u32)
Reserve alignment padding bytes.
sourcepub fn write_align(&mut self, align_start: u32)
pub fn write_align(&mut self, align_start: u32)
Write alignment padding bytes.
sourcepub fn write_file_align(&mut self)
pub fn write_file_align(&mut self)
Write padding up to the next multiple of file alignment.
sourcepub fn reserve_until(&mut self, offset: u32)
pub fn reserve_until(&mut self, offset: u32)
Reserve the file range up to the given file offset.
sourcepub fn reserve_dos_header(&mut self)
pub fn reserve_dos_header(&mut self)
Reserve the range for the DOS header.
This must be at the start of the file.
When writing, you may use write_custom_dos_header
or write_empty_dos_header
.
sourcepub fn write_custom_dos_header(
&mut self,
dos_header: &ImageDosHeader
) -> Result<()>
pub fn write_custom_dos_header( &mut self, dos_header: &ImageDosHeader ) -> Result<()>
Write a custom DOS header.
This must be at the start of the file.
sourcepub fn write_empty_dos_header(&mut self) -> Result<()>
pub fn write_empty_dos_header(&mut self) -> Result<()>
Write the DOS header for a file without a stub.
This must be at the start of the file.
Uses default values for all fields.
sourcepub fn reserve_dos_header_and_stub(&mut self)
pub fn reserve_dos_header_and_stub(&mut self)
Reserve a fixed DOS header and stub.
Use reserve_dos_header
and reserve
if you need a custom stub.
sourcepub fn write_dos_header_and_stub(&mut self) -> Result<()>
pub fn write_dos_header_and_stub(&mut self) -> Result<()>
Write a fixed DOS header and stub.
Use write_custom_dos_header
and write
if you need a custom stub.
sourcepub fn nt_headers_offset(&self) -> u32
pub fn nt_headers_offset(&self) -> u32
Return the offset of the NT headers, if reserved.
sourcepub fn reserve_nt_headers(&mut self, data_directory_num: usize)
pub fn reserve_nt_headers(&mut self, data_directory_num: usize)
Reserve the range for the NT headers.
sourcepub fn set_data_directory(
&mut self,
index: usize,
virtual_address: u32,
size: u32
)
pub fn set_data_directory( &mut self, index: usize, virtual_address: u32, size: u32 )
Set the virtual address and size of a data directory.
sourcepub fn write_nt_headers(&mut self, nt_headers: NtHeaders)
pub fn write_nt_headers(&mut self, nt_headers: NtHeaders)
Write the NT headers.
sourcepub fn reserve_section_headers(&mut self, section_header_num: u16)
pub fn reserve_section_headers(&mut self, section_header_num: u16)
Reserve the section headers.
The number of reserved section headers must be the same as the number of sections that are later reserved.
sourcepub fn write_section_headers(&mut self)
pub fn write_section_headers(&mut self)
Write the section headers.
This uses information that was recorded when the sections were reserved.
sourcepub fn reserve_section(
&mut self,
name: [u8; 8],
characteristics: u32,
virtual_size: u32,
data_size: u32
) -> SectionRange
pub fn reserve_section( &mut self, name: [u8; 8], characteristics: u32, virtual_size: u32, data_size: u32 ) -> SectionRange
Reserve a section.
Returns the file range and virtual address range that are reserved for the section.
sourcepub fn write_section(&mut self, offset: u32, data: &[u8])
pub fn write_section(&mut self, offset: u32, data: &[u8])
Write the data for a section.
sourcepub fn reserve_text_section(&mut self, size: u32) -> SectionRange
pub fn reserve_text_section(&mut self, size: u32) -> SectionRange
Reserve a .text
section.
Contains executable code.
sourcepub fn reserve_data_section(
&mut self,
virtual_size: u32,
data_size: u32
) -> SectionRange
pub fn reserve_data_section( &mut self, virtual_size: u32, data_size: u32 ) -> SectionRange
Reserve a .data
section.
Contains initialized data.
May also contain uninitialized data if virtual_size
is greater than data_size
.
sourcepub fn reserve_rdata_section(&mut self, size: u32) -> SectionRange
pub fn reserve_rdata_section(&mut self, size: u32) -> SectionRange
Reserve a .rdata
section.
Contains read-only initialized data.
sourcepub fn reserve_bss_section(&mut self, size: u32) -> SectionRange
pub fn reserve_bss_section(&mut self, size: u32) -> SectionRange
Reserve a .bss
section.
Contains uninitialized data.
sourcepub fn reserve_idata_section(&mut self, size: u32) -> SectionRange
pub fn reserve_idata_section(&mut self, size: u32) -> SectionRange
Reserve an .idata
section.
Contains import tables. Note that it is permissible to store import tables in a different section.
This also sets the pe::IMAGE_DIRECTORY_ENTRY_IMPORT
data directory.
sourcepub fn reserve_edata_section(&mut self, size: u32) -> SectionRange
pub fn reserve_edata_section(&mut self, size: u32) -> SectionRange
Reserve an .edata
section.
Contains export tables.
This also sets the pe::IMAGE_DIRECTORY_ENTRY_EXPORT
data directory.
sourcepub fn reserve_pdata_section(&mut self, size: u32) -> SectionRange
pub fn reserve_pdata_section(&mut self, size: u32) -> SectionRange
Reserve a .pdata
section.
Contains exception information.
This also sets the pe::IMAGE_DIRECTORY_ENTRY_EXCEPTION
data directory.
sourcepub fn reserve_xdata_section(&mut self, size: u32) -> SectionRange
pub fn reserve_xdata_section(&mut self, size: u32) -> SectionRange
Reserve a .xdata
section.
Contains exception information.
sourcepub fn reserve_rsrc_section(&mut self, size: u32) -> SectionRange
pub fn reserve_rsrc_section(&mut self, size: u32) -> SectionRange
Reserve a .rsrc
section.
Contains the resource directory.
This also sets the pe::IMAGE_DIRECTORY_ENTRY_RESOURCE
data directory.
sourcepub fn add_reloc(&mut self, virtual_address: u32, typ: u16)
pub fn add_reloc(&mut self, virtual_address: u32, typ: u16)
Add a base relocation.
typ
must be one of the IMAGE_REL_BASED_*
constants.
sourcepub fn has_relocs(&mut self) -> bool
pub fn has_relocs(&mut self) -> bool
Return true if a base relocation has been added.
sourcepub fn reserve_reloc_section(&mut self) -> SectionRange
pub fn reserve_reloc_section(&mut self) -> SectionRange
Reserve a .reloc
section.
This contains the base relocations that were added with add_reloc
.
This also sets the pe::IMAGE_DIRECTORY_ENTRY_BASERELOC
data directory.
sourcepub fn write_reloc_section(&mut self)
pub fn write_reloc_section(&mut self)
Write a .reloc
section.
This contains the base relocations that were added with add_reloc
.
sourcepub fn reserve_certificate_table(&mut self, size: u32)
pub fn reserve_certificate_table(&mut self, size: u32)
Reserve the certificate table.
This also sets the pe::IMAGE_DIRECTORY_ENTRY_SECURITY
data directory.
sourcepub fn write_certificate_table(&mut self, data: &[u8])
pub fn write_certificate_table(&mut self, data: &[u8])
Write the certificate table.