Struct Loader

Source
pub struct Loader<DS> { /* private fields */ }
Expand description

An object that can load and parse an ELF file.

Implementations§

Source§

impl<DS> Loader<DS>
where DS: Source,

Source

pub fn new(data_source: DS) -> Result<Loader<DS>, Error<DS::Error>>

Make a new loader

Examples found in repository?
examples/load.rs (line 29)
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 iter_section_headers(&self) -> IterSectionHeaders<'_, DS>

Create a section header iterator.

Examples found in repository?
examples/load.rs (line 68)
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 iter_program_headers(&self) -> IterProgramHeaders<'_, DS>

Create a program header iterator.

Examples found in repository?
examples/load.rs (line 36)
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 e_entry(&self) -> u32

The memory address of the entry point

Examples found in repository?
examples/load.rs (line 32)
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 e_phoff(&self) -> u32

The offset of the program header table

Source

pub fn e_shoff(&self) -> u32

The offset of the section header table

Source

pub fn e_phnum(&self) -> u16

The number of program header entries

Source

pub fn e_shnum(&self) -> u16

The number of section header entries

Source

pub fn segment_start_offset(&self) -> u32

Return the start offset for valid segments.

Any segment with a p_offset less than this probably isn’t valid.

Examples found in repository?
examples/load.rs (line 34)
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}

Auto Trait Implementations§

§

impl<DS> Freeze for Loader<DS>
where DS: Freeze,

§

impl<DS> RefUnwindSafe for Loader<DS>
where DS: RefUnwindSafe,

§

impl<DS> Send for Loader<DS>
where DS: Send,

§

impl<DS> Sync for Loader<DS>
where DS: Sync,

§

impl<DS> Unpin for Loader<DS>
where DS: Unpin,

§

impl<DS> UnwindSafe for Loader<DS>
where DS: 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> 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.