Struct object::write::pe::Writer

source ·
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>

source

pub fn new( is_64: bool, section_alignment: u32, file_alignment: u32, buffer: &'a mut dyn WritableBuffer ) -> Self

Create a new Writer.

source

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.

source

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.

source

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.

source

pub fn reserved_len(&self) -> u32

Return the current file length that has been reserved.

source

pub fn len(&self) -> usize

Return the current file length that has been written.

source

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.

source

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.

source

pub fn write(&mut self, data: &[u8])

Write data.

source

pub fn reserve_align(&mut self, align_start: u32)

Reserve alignment padding bytes.

source

pub fn write_align(&mut self, align_start: u32)

Write alignment padding bytes.

source

pub fn write_file_align(&mut self)

Write padding up to the next multiple of file alignment.

source

pub fn reserve_until(&mut self, offset: u32)

Reserve the file range up to the given file offset.

source

pub fn pad_until(&mut self, offset: u32)

Write padding up to the given file offset.

source

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.

source

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.

source

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.

source

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.

source

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.

source

pub fn nt_headers_offset(&self) -> u32

Return the offset of the NT headers, if reserved.

source

pub fn reserve_nt_headers(&mut self, data_directory_num: usize)

Reserve the range for the NT headers.

source

pub fn set_data_directory( &mut self, index: usize, virtual_address: u32, size: u32 )

Set the virtual address and size of a data directory.

source

pub fn write_nt_headers(&mut self, nt_headers: NtHeaders)

Write the NT headers.

source

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.

source

pub fn write_section_headers(&mut self)

Write the section headers.

This uses information that was recorded when the sections were reserved.

source

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.

source

pub fn write_section(&mut self, offset: u32, data: &[u8])

Write the data for a section.

source

pub fn reserve_text_section(&mut self, size: u32) -> SectionRange

Reserve a .text section.

Contains executable code.

source

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.

source

pub fn reserve_rdata_section(&mut self, size: u32) -> SectionRange

Reserve a .rdata section.

Contains read-only initialized data.

source

pub fn reserve_bss_section(&mut self, size: u32) -> SectionRange

Reserve a .bss section.

Contains uninitialized data.

source

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.

source

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.

source

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.

source

pub fn reserve_xdata_section(&mut self, size: u32) -> SectionRange

Reserve a .xdata section.

Contains exception information.

source

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.

source

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.

source

pub fn has_relocs(&mut self) -> bool

Return true if a base relocation has been added.

source

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.

source

pub fn write_reloc_section(&mut self)

Write a .reloc section.

This contains the base relocations that were added with add_reloc.

source

pub fn reserve_certificate_table(&mut self, size: u32)

Reserve the certificate table.

This also sets the pe::IMAGE_DIRECTORY_ENTRY_SECURITY data directory.

source

pub fn write_certificate_table(&mut self, data: &[u8])

Write the certificate table.

Auto Trait Implementations§

§

impl<'a> !RefUnwindSafe for Writer<'a>

§

impl<'a> !Send for Writer<'a>

§

impl<'a> !Sync for Writer<'a>

§

impl<'a> Unpin for Writer<'a>

§

impl<'a> !UnwindSafe for Writer<'a>

Blanket Implementations§

source§

impl<T> Any for Twhere T: 'static + ?Sized,

source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
source§

impl<T> Borrow<T> for Twhere T: ?Sized,

source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
source§

impl<T> BorrowMut<T> for Twhere T: ?Sized,

source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
source§

impl<T> From<T> for T

source§

fn from(t: T) -> T

Returns the argument unchanged.

source§

impl<T, U> Into<U> for Twhere U: From<T>,

source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

source§

impl<T, U> TryFrom<U> for Twhere U: Into<T>,

§

type Error = Infallible

The type returned in the event of a conversion error.
source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
source§

impl<T, U> TryInto<U> for Twhere U: TryFrom<T>,

§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.