Struct SectionHeader

Source
pub struct SectionHeader { /* private fields */ }
Expand description

Represents a section in the section table.

Implementations§

Source§

impl Header

Source

pub const SIZE_IN_BYTES: u16 = 40u16

Size of a section header entry

Source

pub const SHT_NULL: u32 = 0u32

Section header table entry unused

Source

pub const SHT_PROGBITS: u32 = 1u32

Program data

Source

pub const SHT_SYMTAB: u32 = 2u32

Symbol table

Source

pub const SHT_STRTAB: u32 = 3u32

String table

Source

pub const SHT_RELA: u32 = 4u32

Relocation entries with addends

Source

pub const SHT_HASH: u32 = 5u32

Symbol hash table

Source

pub const SHT_DYNAMIC: u32 = 6u32

Dynamic linking information

Source

pub const SHT_NOTE: u32 = 7u32

Notes

Source

pub const SHT_NOBITS: u32 = 8u32

Program space with no data (bss)

Source

pub const SHT_REL: u32 = 9u32

Relocation data, no addends

Source

pub const SHT_DYNSYM: u32 = 11u32

Dynamic linker symbol table

Source

pub const SHT_INIT_ARRAY: u32 = 14u32

Array of constructors

Source

pub const SHT_FINI_ARRAY: u32 = 15u32

Array of destructors

Source

pub const SHT_PREINIT_ARRAY: u32 = 16u32

Array of pre-constructors

Source

pub const SHT_GROUP: u32 = 17u32

Section group

Source

pub const SHT_SYMTAB_SHNDX: u32 = 18u32

Extended section indicies

Source

pub fn new<DS>(loader: &Loader<DS>, idx: u16) -> Result<Self, Error<DS::Error>>
where DS: Source,

Create a new section header.

Source

pub fn sh_name_offset(&self) -> u32

Return the sh_name_offset field

Source

pub fn sh_name<'a, DS: Source>( &self, loader: &Loader<DS>, buffer: &'a mut [u8], ) -> Result<&'a str, Error<DS::Error>>

Get the string name for this section.

Examples found in repository?
examples/load.rs (line 91)
24fn main() -> Result<(), Error> {
25    let mut args = std::env::args_os();
26    let _example_name = args.next();
27    let filename = args.next().ok_or(Error::MissingArgument)?;
28    let data = std::fs::read(&filename)?;
29    let loader = ldr::Loader::new(&data[..])?;
30
31    println!("Loaded ELF {}", filename.to_string_lossy());
32    println!("Entry Point: 0x{:08x}", loader.e_entry());
33
34    let segment_start_addr = loader.segment_start_offset();
35    let mut total_ram_used = 0;
36    for (idx, ph) in loader.iter_program_headers().enumerate() {
37        let ph = ph.expect("PH loaded OK");
38        let p_type = match ph.p_type() {
39            ldr::ProgramHeader::PT_NULL => "PT_NULL",
40            ldr::ProgramHeader::PT_LOAD => "PT_LOAD",
41            ldr::ProgramHeader::PT_DYNAMIC => "PT_DYNAMIC",
42            ldr::ProgramHeader::PT_INTERP => "PT_INTERP",
43            ldr::ProgramHeader::PT_NOTE => "PT_NOTE",
44            ldr::ProgramHeader::PT_SHLIB => "PT_SHLIB",
45            ldr::ProgramHeader::PT_PHDR => "PT_PHDR",
46            ldr::ProgramHeader::PT_TLS => "PT_TLS",
47            ldr::ProgramHeader::PT_GNU_STACK => "PT_GNU_STACK",
48            _ => "PT_???",
49        };
50
51        let ignored = if ph.p_offset() >= segment_start_addr {
52            "OK"
53        } else {
54            "Ignored"
55        };
56
57        let data_bytes = ph.p_filesz();
58        let zero_bytes = ph.p_memsz() - data_bytes;
59        let load_addr = ph.p_paddr();
60
61        total_ram_used += ph.p_memsz();
62
63        println!("PH {idx:02}: p_type = {p_type:12}, data_bytes=0x{data_bytes:04x}, zero_bytes=0x{zero_bytes:04x}, load_addr=0x{load_addr:08x} ({ignored})");
64    }
65
66    println!("Total RAM used: {total_ram_used} bytes");
67
68    for (idx, sh) in loader.iter_section_headers().enumerate() {
69        let sh = sh.expect("SH loaded OK");
70        let sh_type = match sh.sh_type() {
71            ldr::SectionHeader::SHT_NULL => "SHT_NULL",
72            ldr::SectionHeader::SHT_PROGBITS => "SHT_PROGBITS",
73            ldr::SectionHeader::SHT_SYMTAB => "SHT_SYMTAB",
74            ldr::SectionHeader::SHT_STRTAB => "SHT_STRTAB",
75            ldr::SectionHeader::SHT_RELA => "SHT_RELA",
76            ldr::SectionHeader::SHT_HASH => "SHT_HASH",
77            ldr::SectionHeader::SHT_DYNAMIC => "SHT_DYNAMIC",
78            ldr::SectionHeader::SHT_NOTE => "SHT_NOTE",
79            ldr::SectionHeader::SHT_NOBITS => "SHT_NOBITS",
80            ldr::SectionHeader::SHT_REL => "SHT_REL",
81            ldr::SectionHeader::SHT_DYNSYM => "SHT_DYNSYM",
82            ldr::SectionHeader::SHT_INIT_ARRAY => "SHT_INIT_ARRAY",
83            ldr::SectionHeader::SHT_FINI_ARRAY => "SHT_FINI_ARRAY",
84            ldr::SectionHeader::SHT_PREINIT_ARRAY => "SHT_PREINIT_ARRAY",
85            ldr::SectionHeader::SHT_GROUP => "SHT_GROUP",
86            ldr::SectionHeader::SHT_SYMTAB_SHNDX => "SHT_SYMTAB_SHNDX",
87            _ => "???",
88        };
89
90        let mut buffer = [0u8; 64];
91        let name = sh.sh_name(&loader, &mut buffer).unwrap_or("E_TOO_LONG");
92        println!("SH {idx:02}: {sh:08x?} ({sh_type}, {name:?})");
93    }
94
95    Ok(())
96}
Source

pub fn sh_type(&self) -> u32

Return the sh_type field

Examples found in repository?
examples/load.rs (line 70)
24fn main() -> Result<(), Error> {
25    let mut args = std::env::args_os();
26    let _example_name = args.next();
27    let filename = args.next().ok_or(Error::MissingArgument)?;
28    let data = std::fs::read(&filename)?;
29    let loader = ldr::Loader::new(&data[..])?;
30
31    println!("Loaded ELF {}", filename.to_string_lossy());
32    println!("Entry Point: 0x{:08x}", loader.e_entry());
33
34    let segment_start_addr = loader.segment_start_offset();
35    let mut total_ram_used = 0;
36    for (idx, ph) in loader.iter_program_headers().enumerate() {
37        let ph = ph.expect("PH loaded OK");
38        let p_type = match ph.p_type() {
39            ldr::ProgramHeader::PT_NULL => "PT_NULL",
40            ldr::ProgramHeader::PT_LOAD => "PT_LOAD",
41            ldr::ProgramHeader::PT_DYNAMIC => "PT_DYNAMIC",
42            ldr::ProgramHeader::PT_INTERP => "PT_INTERP",
43            ldr::ProgramHeader::PT_NOTE => "PT_NOTE",
44            ldr::ProgramHeader::PT_SHLIB => "PT_SHLIB",
45            ldr::ProgramHeader::PT_PHDR => "PT_PHDR",
46            ldr::ProgramHeader::PT_TLS => "PT_TLS",
47            ldr::ProgramHeader::PT_GNU_STACK => "PT_GNU_STACK",
48            _ => "PT_???",
49        };
50
51        let ignored = if ph.p_offset() >= segment_start_addr {
52            "OK"
53        } else {
54            "Ignored"
55        };
56
57        let data_bytes = ph.p_filesz();
58        let zero_bytes = ph.p_memsz() - data_bytes;
59        let load_addr = ph.p_paddr();
60
61        total_ram_used += ph.p_memsz();
62
63        println!("PH {idx:02}: p_type = {p_type:12}, data_bytes=0x{data_bytes:04x}, zero_bytes=0x{zero_bytes:04x}, load_addr=0x{load_addr:08x} ({ignored})");
64    }
65
66    println!("Total RAM used: {total_ram_used} bytes");
67
68    for (idx, sh) in loader.iter_section_headers().enumerate() {
69        let sh = sh.expect("SH loaded OK");
70        let sh_type = match sh.sh_type() {
71            ldr::SectionHeader::SHT_NULL => "SHT_NULL",
72            ldr::SectionHeader::SHT_PROGBITS => "SHT_PROGBITS",
73            ldr::SectionHeader::SHT_SYMTAB => "SHT_SYMTAB",
74            ldr::SectionHeader::SHT_STRTAB => "SHT_STRTAB",
75            ldr::SectionHeader::SHT_RELA => "SHT_RELA",
76            ldr::SectionHeader::SHT_HASH => "SHT_HASH",
77            ldr::SectionHeader::SHT_DYNAMIC => "SHT_DYNAMIC",
78            ldr::SectionHeader::SHT_NOTE => "SHT_NOTE",
79            ldr::SectionHeader::SHT_NOBITS => "SHT_NOBITS",
80            ldr::SectionHeader::SHT_REL => "SHT_REL",
81            ldr::SectionHeader::SHT_DYNSYM => "SHT_DYNSYM",
82            ldr::SectionHeader::SHT_INIT_ARRAY => "SHT_INIT_ARRAY",
83            ldr::SectionHeader::SHT_FINI_ARRAY => "SHT_FINI_ARRAY",
84            ldr::SectionHeader::SHT_PREINIT_ARRAY => "SHT_PREINIT_ARRAY",
85            ldr::SectionHeader::SHT_GROUP => "SHT_GROUP",
86            ldr::SectionHeader::SHT_SYMTAB_SHNDX => "SHT_SYMTAB_SHNDX",
87            _ => "???",
88        };
89
90        let mut buffer = [0u8; 64];
91        let name = sh.sh_name(&loader, &mut buffer).unwrap_or("E_TOO_LONG");
92        println!("SH {idx:02}: {sh:08x?} ({sh_type}, {name:?})");
93    }
94
95    Ok(())
96}
Source

pub fn sh_flags(&self) -> u32

Return the sh_flags field

Source

pub fn sh_addr(&self) -> u32

Return the sh_addr field

Source

pub fn sh_offset(&self) -> u32

Return the sh_offset field

Source

pub fn sh_size(&self) -> u32

Return the sh_size field

Return the sh_link field

Source

pub fn sh_info(&self) -> u32

Return the sh_info field

Source

pub fn sh_addralign(&self) -> u32

Return the sh_addralign field

Source

pub fn sh_entsize(&self) -> u32

Return the sh_entsize field

Trait Implementations§

Source§

impl Clone for Header

Source§

fn clone(&self) -> Header

Returns a copy of the value. Read more
1.0.0 · Source§

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

Performs copy-assignment from source. Read more
Source§

impl Debug for Header

Source§

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

Formats the value using the given formatter. Read more

Auto Trait Implementations§

§

impl Freeze for Header

§

impl RefUnwindSafe for Header

§

impl Send for Header

§

impl Sync for Header

§

impl Unpin for Header

§

impl UnwindSafe for Header

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, U> TryFrom<U> for T
where U: Into<T>,

Source§

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 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.