pub struct ProgramHeader { /* private fields */ }
Expand description
Represents a program header
Implementations§
Source§impl Header
impl Header
Sourcepub const SIZE_IN_BYTES: u16 = 32u16
pub const SIZE_IN_BYTES: u16 = 32u16
Size of a program header entry
Sourcepub const PT_DYNAMIC: u32 = 2u32
pub const PT_DYNAMIC: u32 = 2u32
Dynamic linking information.
Sourcepub const PT_GNU_STACK: u32 = 1_685_382_481u32
pub const PT_GNU_STACK: u32 = 1_685_382_481u32
Stack.
Sourcepub fn new<DS>(loader: &Loader<DS>, idx: u16) -> Result<Self, Error<DS::Error>>where
DS: Source,
pub fn new<DS>(loader: &Loader<DS>, idx: u16) -> Result<Self, Error<DS::Error>>where
DS: Source,
Create a new section header.
Sourcepub fn p_type(&self) -> u32
pub fn p_type(&self) -> u32
Get the p_type
field.
This is the type of segment, e.g. PT_LOAD
.
Examples found in repository?
examples/load.rs (line 38)
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}
Sourcepub fn p_offset(&self) -> u32
pub fn p_offset(&self) -> u32
Get the p_offset
field
This is the start of the segment data within this ELF file.
Examples found in repository?
examples/load.rs (line 51)
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}
Sourcepub fn p_paddr(&self) -> u32
pub fn p_paddr(&self) -> u32
Get the p_paddr
field
This is the physical memory load address.
Examples found in repository?
examples/load.rs (line 59)
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}
Sourcepub fn p_filesz(&self) -> u32
pub fn p_filesz(&self) -> u32
Get the p_filesz
field
This is how much space is used by this segment on disk.
Examples found in repository?
examples/load.rs (line 57)
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}
Sourcepub fn p_memsz(&self) -> u32
pub fn p_memsz(&self) -> u32
Get the p_memsz
field
This is how much space is used by this segment in RAM.
Examples found in repository?
examples/load.rs (line 58)
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}
Trait Implementations§
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> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more