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>
impl<E: Endian> Encoder<E>
Sourcepub fn new(endian: E, is_64: bool, machine: Machine) -> Self
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.
Sourcepub fn set_machine(&mut self, machine: Machine)
pub fn set_machine(&mut self, machine: Machine)
Set the machine.
This is required to support the encoding of mips64el relocations.
Sourcepub fn is_mips64el(self) -> bool
pub fn is_mips64el(self) -> bool
Return true for little-endian 64-bit MIPS.
Sourcepub fn address_size(self) -> u64
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.
Sourcepub fn file_header_size(self) -> u64
pub fn file_header_size(self) -> u64
Return the size of the file header.
Sourcepub fn file_header<W: WritableBuffer + ?Sized>(
self,
buffer: &mut W,
header: &FileHeader,
layout: &FileHeaderLayout,
) -> Result<()>
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.
Sourcepub fn program_header_size(self) -> u64
pub fn program_header_size(self) -> u64
Return the size of a program header.
Sourcepub fn program_header<W: WritableBuffer + ?Sized>(
self,
buffer: &mut W,
header: &ProgramHeader,
)
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.
Sourcepub fn section_header_size(self) -> u64
pub fn section_header_size(self) -> u64
Return the size of a section header.
Sourcepub fn null_section_header<W: WritableBuffer + ?Sized>(
self,
buffer: &mut W,
layout: &FileHeaderLayout,
)
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.
Sourcepub fn section_header<W: WritableBuffer + ?Sized>(
self,
buffer: &mut W,
section: &SectionHeader,
)
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.
Sourcepub fn strtab_section_header(self) -> SectionHeader
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.
Sourcepub fn dynstr_section_header(self) -> SectionHeader
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.
Sourcepub fn symtab_section_header(self, strtab: u32, num_local: u32) -> SectionHeader
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.
Sourcepub fn symtab_shndx_section_header(self, symtab: u32) -> SectionHeader
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.
Sourcepub fn dynsym_section_header(self, dynstr: u32, num_local: u32) -> SectionHeader
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.
Sourcepub fn dynamic_section_header(self, dynstr: u32) -> SectionHeader
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.
Sourcepub fn hash_section_header(self, dynsym: u32) -> SectionHeader
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.
Sourcepub fn gnu_hash_section_header(self, dynsym: u32) -> SectionHeader
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.
Sourcepub fn gnu_versym_section_header(self, dynsym: u32) -> SectionHeader
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.
Sourcepub fn gnu_verdef_section_header(
self,
dynstr: u32,
verdef_count: u16,
) -> SectionHeader
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.
Sourcepub fn gnu_verneed_section_header(
self,
dynstr: u32,
verneed_count: u16,
) -> SectionHeader
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.
Sourcepub fn gnu_attributes_section_header(self) -> SectionHeader
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.
Sourcepub fn relocation_section_header(self, is_rela: bool) -> SectionHeader
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).
Sourcepub fn relative_relocation_section_header(self) -> SectionHeader
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.
Sourcepub fn comdat_section_header(self) -> SectionHeader
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).
Sourcepub fn strtab<W: WritableBuffer + ?Sized>(
self,
buffer: &mut W,
strtab: &mut StringTable<'_>,
) -> Result<u32>
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.
Sourcepub fn null_symbol<W: WritableBuffer + ?Sized>(self, buffer: &mut W)
pub fn null_symbol<W: WritableBuffer + ?Sized>(self, buffer: &mut W)
Write the null symbol.
The buffer should already be aligned to address_size.
Sourcepub fn symbol<W: WritableBuffer + ?Sized>(
self,
buffer: &mut W,
sym: &Sym,
) -> Option<u32>
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.
Sourcepub fn relocation<W: WritableBuffer + ?Sized>(
self,
buffer: &mut W,
is_rela: bool,
rel: &Rel,
)
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.
Sourcepub fn dynamic<W: WritableBuffer + ?Sized>(
self,
buffer: &mut W,
d_tag: DynamicTag,
d_val: u64,
) -> Result<()>
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.
Sourcepub fn hash_size(self, bucket_count: u32, chain_count: u32) -> u64
pub fn hash_size(self, bucket_count: u32, chain_count: u32) -> u64
Return the size of a hash table.
Sourcepub fn hash_table<W, F>(
self,
buffer: &mut W,
bucket_count: u32,
chain_count: u32,
hash: F,
)
pub fn hash_table<W, F>( self, buffer: &mut W, bucket_count: u32, chain_count: u32, hash: F, )
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.
Sourcepub fn gnu_hash_size(
self,
bloom_count: u32,
bucket_count: u32,
symbol_count: u32,
) -> u64
pub fn gnu_hash_size( self, bloom_count: u32, bucket_count: u32, symbol_count: u32, ) -> u64
Return the size of a GNU hash table.
Sourcepub fn gnu_hash_table<W, F>(self, buffer: &mut W, table: &GnuHashTable, hash: F)
pub fn gnu_hash_table<W, F>(self, buffer: &mut W, table: &GnuHashTable, hash: F)
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.
Sourcepub fn gnu_versym_size(self, symbol_count: u32) -> u64
pub fn gnu_versym_size(self, symbol_count: u32) -> u64
Return the size of a GNU symbol version section.
Sourcepub fn gnu_versym<W: WritableBuffer + ?Sized>(
self,
buffer: &mut W,
versym: VersymIndex,
)
pub fn gnu_versym<W: WritableBuffer + ?Sized>( self, buffer: &mut W, versym: VersymIndex, )
Write a symbol version entry.
Sourcepub fn gnu_verdef_size(self, verdef_count: u16, verdaux_count: usize) -> u64
pub fn gnu_verdef_size(self, verdef_count: u16, verdaux_count: usize) -> u64
Return the size of a GNU version definition section.
Sourcepub fn gnu_verdef<W: WritableBuffer + ?Sized>(
self,
buffer: &mut W,
next: bool,
verdef: &Verdef,
)
pub fn gnu_verdef<W: WritableBuffer + ?Sized>( self, buffer: &mut W, next: bool, verdef: &Verdef, )
Write a version definition entry.
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.
Sourcepub fn gnu_verdaux<W: WritableBuffer + ?Sized>(
self,
buffer: &mut W,
next: bool,
name: u32,
)
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.
Sourcepub fn gnu_verneed_size(self, verneed_count: u16, vernaux_count: usize) -> u64
pub fn gnu_verneed_size(self, verneed_count: u16, vernaux_count: usize) -> u64
Return the size of a GNU version dependency section.
Sourcepub fn gnu_verneed<W: WritableBuffer + ?Sized>(
self,
buffer: &mut W,
next: bool,
verneed: &Verneed,
)
pub fn gnu_verneed<W: WritableBuffer + ?Sized>( self, buffer: &mut W, next: bool, verneed: &Verneed, )
Write a version needed entry.
Sourcepub fn gnu_vernaux<W: WritableBuffer + ?Sized>(
self,
buffer: &mut W,
next: bool,
vernaux: &Vernaux,
)
pub fn gnu_vernaux<W: WritableBuffer + ?Sized>( self, buffer: &mut W, next: bool, vernaux: &Vernaux, )
Write a version needed auxiliary entry.