Skip to main content

Encoder

Struct Encoder 

Source
pub struct Encoder<E: Endian> { /* private fields */ }
Expand description

A helper for encoding headers and data when writing an ELF file.

None of the methods check for overflow when truncating addresses and file offsets for 32-bit ELF. It is recommended that the caller keep track of the largest address and file offset, and perform a single overflow check.

Implementations§

Source§

impl<E: Endian> Encoder<E>

Source

pub fn new(endian: E, is_64: bool, machine: Machine) -> Self

Create a new Encoder for the given endianness and ELF class, and machine.

machine is required to detect mips64el. If it is unknown at construction, it may be specified as EM_NONE here, and set later via set_machine.

Source

pub fn set_machine(&mut self, machine: Machine)

Set the machine.

This is required to support the encoding of mips64el relocations.

Source

pub fn endian(self) -> E

Return the endianness.

Source

pub fn is_64(self) -> bool

Return true for 64-bit ELF.

Source

pub fn is_mips64el(self) -> bool

Return true for little-endian 64-bit MIPS.

Source

pub fn address_size(self) -> u64

Return the size in bytes of an address for the ELF class.

This should be used as the file offset alignment for various structures such as program headers, section headers, symbols, dynamics, relocations, and .gnu.hash.

Source

pub fn file_header_size(self) -> u64

Return the size of the file header.

Source

pub fn file_header<W: WritableBuffer + ?Sized>( self, buffer: &mut W, header: &FileHeader, layout: &FileHeaderLayout, ) -> Result<()>

Write the file header.

The buffer should be at the start of the file.

layout.segment_num, layout.section_num, and layout.shstrtab_index are replaced with the appropriate sentinel values if overflow occurs.

No overflow check is performed when truncating e_entry, e_phoff, and e_shoff for 32-bit ELF.

Source

pub fn program_header_size(self) -> u64

Return the size of a program header.

Source

pub fn program_header<W: WritableBuffer + ?Sized>( self, buffer: &mut W, header: &ProgramHeader, )

Write a program header.

The buffer should already be aligned to address_size.

No overflow check is performed when truncating p_offset, p_vaddr, p_paddr, p_filesz, p_memsz and p_align for 32-bit ELF.

Source

pub fn section_header_size(self) -> u64

Return the size of a section header.

Source

pub fn null_section_header<W: WritableBuffer + ?Sized>( self, buffer: &mut W, layout: &FileHeaderLayout, )

Write the section header at index 0.

The layout is used to set fields if file header values overflowed.

The buffer should already be aligned to address_size.

Source

pub fn section_header<W: WritableBuffer + ?Sized>( self, buffer: &mut W, section: &SectionHeader, )

Write a section header.

The buffer should already be aligned to address_size.

No overflow check is performed when truncating sh_addr, sh_offset, sh_size, sh_addralign, and sh_entsize for 32-bit ELF.

Source

pub fn strtab_section_header(self) -> SectionHeader

Return a section header for elf::SHT_STRTAB.

The caller must set sh_name, sh_offset, and sh_size.

Source

pub fn dynstr_section_header(self) -> SectionHeader

Return a section header for elf::SHT_STRTAB with elf::SHF_ALLOC.

The caller must set sh_name, sh_addr, sh_offset, and sh_size.

Source

pub fn symtab_section_header(self, strtab: u32, num_local: u32) -> SectionHeader

Return a section header for elf::SHT_SYMTAB.

sh_link is set to strtab. sh_info is set to num_local. The caller must set sh_name, sh_offset, and sh_size.

Source

pub fn symtab_shndx_section_header(self, symtab: u32) -> SectionHeader

Return a section header for elf::SHT_SYMTAB_SHNDX.

sh_link is set to symtab. The caller must set sh_name, sh_offset, sh_size.

Source

pub fn dynsym_section_header(self, dynstr: u32, num_local: u32) -> SectionHeader

Return a section header for elf::SHT_DYNSYM.

sh_link is set to dynstr. sh_info is set to num_local. The caller must set sh_name, sh_addr, sh_offset, and sh_size.

Source

pub fn dynamic_section_header(self, dynstr: u32) -> SectionHeader

Return a section header for elf::SHT_DYNAMIC.

sh_link is set to dynstr. The caller must set sh_name, sh_addr, sh_offset, and sh_size.

Source

pub fn hash_section_header(self, dynsym: u32) -> SectionHeader

Return a section header for elf::SHT_HASH.

sh_link is set to dynsym. The caller must set sh_name, sh_addr, sh_offset, and sh_size.

Source

pub fn gnu_hash_section_header(self, dynsym: u32) -> SectionHeader

Return a section header for elf::SHT_GNU_HASH.

sh_link is set to dynsym. The caller must set sh_name, sh_addr, sh_offset, and sh_size.

Source

pub fn gnu_versym_section_header(self, dynsym: u32) -> SectionHeader

Return a section header for elf::SHT_GNU_VERSYM.

sh_link is set to dynsym. The caller must set sh_name, sh_addr, sh_offset, and sh_size.

Source

pub fn gnu_verdef_section_header( self, dynstr: u32, verdef_count: u16, ) -> SectionHeader

Return a section header for elf::SHT_GNU_VERDEF.

sh_link is set to dynstr. sh_info is set to verdef_count. The caller must set sh_name, sh_addr, sh_offset, and sh_size.

Source

pub fn gnu_verneed_section_header( self, dynstr: u32, verneed_count: u16, ) -> SectionHeader

Return a section header for elf::SHT_GNU_VERNEED.

sh_link is set to dynstr. sh_info is set to verneed_count. The caller must set sh_name, sh_addr, sh_offset, and sh_size.

Source

pub fn gnu_attributes_section_header(self) -> SectionHeader

Return a section header for elf::SHT_GNU_ATTRIBUTES.

The caller must set sh_name, sh_offset, sh_size, and sh_link.

Source

pub fn relocation_section_header(self, is_rela: bool) -> SectionHeader

Return a section header for a relocation section.

is_rela determines sh_type (elf::SHT_RELA or elf::SHT_REL) and sh_entsize. The caller must set sh_name, sh_offset, sh_size, sh_link (the symbol table section index), and sh_info (the section index the relocations apply to).

Source

pub fn relative_relocation_section_header(self) -> SectionHeader

Return a section header for a relative relocation section.

The caller must set sh_name, sh_offset, and sh_size.

Source

pub fn comdat_section_header(self) -> SectionHeader

Return a section header for a COMDAT group section (elf::SHT_GROUP).

The caller must set sh_name, sh_offset, sh_size, sh_link (the symbol table section index), and sh_info (the group signature symbol index).

Source

pub fn strtab<W: WritableBuffer + ?Sized>( self, buffer: &mut W, strtab: &mut StringTable<'_>, ) -> Result<u32>

Write the data for a string table.

Returns the length of the written data.

Source

pub fn sym_size(self) -> u64

Return the size of a symbol.

Source

pub fn null_symbol<W: WritableBuffer + ?Sized>(self, buffer: &mut W)

Write the null symbol.

The buffer should already be aligned to address_size.

Source

pub fn symbol<W: WritableBuffer + ?Sized>( self, buffer: &mut W, sym: &Sym, ) -> Option<u32>

Write a symbol.

Returns the extended symbol index if overflow occurred.

The buffer should already be aligned to address_size.

No overflow check is performed when truncating st_value and st_size for 32-bit ELF.

Source

pub fn rel_size(self, is_rela: bool) -> u64

Return the size of a relocation entry.

Source

pub fn relr_size(self) -> u64

Return the size of a relative relocation entry.

Source

pub fn relocation<W: WritableBuffer + ?Sized>( self, buffer: &mut W, is_rela: bool, rel: &Rel, )

Write a relocation.

The buffer should already be aligned to address_size.

No overflow check is performed when truncating r_offset, r_sym, r_type, and r_addend for 32-bit ELF.

Source

pub fn dyn_size(self) -> u64

Return the size of a dynamic entry.

Source

pub fn dynamic<W: WritableBuffer + ?Sized>( self, buffer: &mut W, d_tag: DynamicTag, d_val: u64, ) -> Result<()>

Write a dynamic value entry.

Returns an error for 32-bit ELF overflows.

The buffer should already be aligned to address_size.

Source

pub fn hash_size(self, bucket_count: u32, chain_count: u32) -> u64

Return the size of a hash table.

Source

pub fn hash_table<W, F>( self, buffer: &mut W, bucket_count: u32, chain_count: u32, hash: F, )
where W: WritableBuffer + ?Sized, F: Fn(u32) -> Option<u32>,

Write a SysV hash table.

chain_count is the number of symbols in the hash. The argument to hash will be in the range 0..chain_count.

The buffer should already be aligned to 4.

Source

pub fn gnu_hash_size( self, bloom_count: u32, bucket_count: u32, symbol_count: u32, ) -> u64

Return the size of a GNU hash table.

Source

pub fn gnu_hash_table<W, F>(self, buffer: &mut W, table: &GnuHashTable, hash: F)
where W: WritableBuffer + ?Sized, F: Fn(u32) -> u32,

Write a GNU hash section.

symbol_count is the number of symbols in the hash. The argument to hash will be in the range 0..symbol_count.

This requires that symbols are already sorted by bucket.

Source

pub fn gnu_versym_size(self, symbol_count: u32) -> u64

Return the size of a GNU symbol version section.

Source

pub fn gnu_versym<W: WritableBuffer + ?Sized>( self, buffer: &mut W, versym: VersymIndex, )

Write a symbol version entry.

Source

pub fn gnu_verdef_size(self, verdef_count: u16, verdaux_count: usize) -> u64

Return the size of a GNU version definition section.

Source

pub fn gnu_verdef<W: WritableBuffer + ?Sized>( self, buffer: &mut W, next: bool, verdef: &Verdef, )

Write a version definition entry.

Source

pub fn gnu_verdef_shared<W: WritableBuffer + ?Sized>( self, buffer: &mut W, verdef: &Verdef, )

Write a version definition entry that shares the names of the next definition.

This is typically useful when there are only two versions (including the base) and they have the same name.

Source

pub fn gnu_verdaux<W: WritableBuffer + ?Sized>( self, buffer: &mut W, next: bool, name: u32, )

Write a version definition auxiliary entry.

name is the offset of the name in the dynamic string table.

Source

pub fn gnu_verneed_size(self, verneed_count: u16, vernaux_count: usize) -> u64

Return the size of a GNU version dependency section.

Source

pub fn gnu_verneed<W: WritableBuffer + ?Sized>( self, buffer: &mut W, next: bool, verneed: &Verneed, )

Write a version needed entry.

Source

pub fn gnu_vernaux<W: WritableBuffer + ?Sized>( self, buffer: &mut W, next: bool, vernaux: &Vernaux, )

Write a version needed auxiliary entry.

Trait Implementations§

Source§

impl<E: Clone + Endian> Clone for Encoder<E>

Source§

fn clone(&self) -> Self

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl<E: Copy + Endian> Copy for Encoder<E>

Source§

impl<E: Debug + Endian> Debug for Encoder<E>

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more

Auto Trait Implementations§

§

impl<E> Forget for Encoder<E>

§

impl<E> Freeze for Encoder<E>
where E: Freeze,

§

impl<E> RefUnwindSafe for Encoder<E>
where E: RefUnwindSafe,

§

impl<E> Send for Encoder<E>
where E: Send,

§

impl<E> Sync for Encoder<E>
where E: Sync,

§

impl<E> Unpin for Encoder<E>
where E: Unpin,

§

impl<E> UnsafeUnpin for Encoder<E>
where E: UnsafeUnpin,

§

impl<E> UnwindSafe for Encoder<E>
where E: UnwindSafe,

Blanket Implementations§

Source§

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

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

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

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

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

Source§

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

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. 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 T
where 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> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

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

Source§

type Error = !

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

fn try_from(value: U) -> Result<T, !>

Performs the conversion.
Source§

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

Source§

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.