diff --git a/src/abi.rs b/src/abi.rs index 0e8ad7a..6fc9224 100644 --- a/src/abi.rs +++ b/src/abi.rs @@ -824,6 +824,8 @@ pub const STV_HIDDEN: u8 = 2; /// would preempt by the default rules. pub const STV_PROTECTED: u8 = 3; +// DT_* define constants for the ELF Dynamic Table's d_tag field. + /// An entry with a DT_NULL tag marks the end of the _DYNAMIC array. pub const DT_NULL: i64 = 0; /// This element holds the string table offset of a null-terminated string, @@ -2668,3 +2670,246 @@ pub const R_X86_64_RELATIVE64: u32 = 38; pub const R_X86_64_GOTPCRELX: u32 = 41; /// `G + GOT + A - P` pub const R_X86_64_REX_GOTPCRELX: u32 = 42; + +/// C-style 32-bit ELF File Header definition +/// +/// These C-style definitions are for users who want to implement their own ELF manipulation logic. +#[derive(Debug)] +#[repr(C)] +#[allow(non_camel_case_types)] +pub struct Elf32_Ehdr { + pub e_ident: [u8; EI_NIDENT], + pub e_type: u16, + pub e_machine: u16, + pub e_version: u32, + pub e_entry: u32, + pub e_phoff: u32, + pub e_shoff: u32, + pub e_flags: u32, + pub e_ehsize: u16, + pub e_phentsize: u16, + pub e_phnum: u16, + pub e_shentsize: u16, + pub e_shnum: u16, + pub e_shstrndx: u16, +} + +/// C-style 64-bit ELF File Header definition +/// +/// These C-style definitions are for users who want to implement their own ELF manipulation logic. +#[derive(Debug)] +#[repr(C)] +#[allow(non_camel_case_types)] +pub struct Elf64_Ehdr { + pub e_ident: [u8; EI_NIDENT], + pub e_type: u16, + pub e_machine: u16, + pub e_version: u32, + pub e_entry: u64, + pub e_phoff: u64, + pub e_shoff: u64, + pub e_flags: u32, + pub e_ehsize: u16, + pub e_phentsize: u16, + pub e_phnum: u16, + pub e_shentsize: u16, + pub e_shnum: u16, + pub e_shstrndx: u16, +} + +/// C-style 32-bit ELF Section Header definition +/// +/// These C-style definitions are for users who want to implement their own ELF manipulation logic. +#[derive(Debug)] +#[repr(C)] +#[allow(non_camel_case_types)] +pub struct Elf32_Shdr { + pub sh_name: u32, + pub sh_type: u32, + pub sh_flags: u32, + pub sh_addr: u32, + pub sh_offset: u32, + pub sh_size: u32, + pub sh_link: u32, + pub sh_info: u32, + pub sh_addralign: u32, + pub sh_entsize: u32, +} + +/// C-style 64-bit ELF Section Header definition +/// +/// These C-style definitions are for users who want to implement their own ELF manipulation logic. +#[derive(Debug)] +#[repr(C)] +#[allow(non_camel_case_types)] +pub struct Elf64_Shdr { + pub sh_name: u32, + pub sh_type: u32, + pub sh_flags: u64, + pub sh_addr: u64, + pub sh_offset: u64, + pub sh_size: u64, + pub sh_link: u32, + pub sh_info: u32, + pub sh_addralign: u64, + pub sh_entsize: u64, +} + +/// C-style 32-bit ELF Program Segment Header definition +/// +/// These C-style definitions are for users who want to implement their own ELF manipulation logic. +#[derive(Debug)] +#[repr(C)] +#[allow(non_camel_case_types)] +pub struct Elf32_Phdr { + pub p_type: u32, + pub p_offset: u32, + pub p_vaddr: u32, + pub p_paddr: u32, + pub p_filesz: u32, + pub p_memsz: u32, + pub p_flags: u32, + pub p_align: u32, +} + +/// C-style 64-bit ELF Program Segment Header definition +/// +/// These C-style definitions are for users who want to implement their own ELF manipulation logic. +#[derive(Debug)] +#[repr(C)] +#[allow(non_camel_case_types)] +pub struct Elf64_Phdr { + pub p_type: u32, + pub p_flags: u32, + pub p_offset: u64, + pub p_vaddr: u64, + pub p_paddr: u64, + pub p_filesz: u64, + pub p_memsz: u64, + pub p_align: u64, +} + +/// C-style 32-bit ELF Compression Header definition +/// +/// These C-style definitions are for users who want to implement their own ELF manipulation logic. +#[derive(Debug)] +#[repr(C)] +#[allow(non_camel_case_types)] +pub struct Elf32_Chdr { + pub ch_type: u32, + pub ch_size: u32, + pub ch_addralign: u32, +} + +/// C-style 64-bit ELF Compression Header definition +/// +/// These C-style definitions are for users who want to implement their own ELF manipulation logic. +#[derive(Debug)] +#[repr(C)] +#[allow(non_camel_case_types)] +pub struct Elf64_Chdr { + pub ch_type: u32, + pub ch_reserved: u32, + pub ch_size: u64, + pub ch_addralign: u64, +} + +/// C-style 32-bit ELF Dynamic section entry definition +/// +/// These C-style definitions are for users who want to implement their own ELF manipulation logic. +#[derive(Debug)] +#[repr(C)] +#[allow(non_camel_case_types)] +pub struct Elf32_Dyn { + pub d_tag: i32, + // union of both {d_val, d_ptr} + pub d_un: u32, +} + +/// C-style 64-bit ELF Dynamic section entry definition +/// +/// These C-style definitions are for users who want to implement their own ELF manipulation logic. +#[derive(Debug)] +#[repr(C)] +#[allow(non_camel_case_types)] +pub struct Elf64_Dyn { + pub d_tag: i64, + // union of both {d_val, d_ptr} + pub d_un: u64, +} + +/// C-style 32-bit ELF Relocation definition +/// +/// These C-style definitions are for users who want to implement their own ELF manipulation logic. +#[derive(Debug)] +#[repr(C)] +#[allow(non_camel_case_types)] +pub struct Elf32_Rel { + pub r_offset: u32, + pub r_info: u32, +} + +/// C-style 64-bit ELF Relocation definition +/// +/// These C-style definitions are for users who want to implement their own ELF manipulation logic. +#[derive(Debug)] +#[repr(C)] +#[allow(non_camel_case_types)] +pub struct Elf64_Rel { + pub r_offset: u64, + pub r_info: u64, +} + +/// C-style 32-bit ELF Relocation (with addend) definition +/// +/// These C-style definitions are for users who want to implement their own ELF manipulation logic. +#[derive(Debug)] +#[repr(C)] +#[allow(non_camel_case_types)] +pub struct Elf32_Rela { + pub r_offset: u32, + pub r_info: u32, + pub r_addend: i32, +} + +/// C-style 64-bit ELF Relocation (with addend) definition +/// +/// These C-style definitions are for users who want to implement their own ELF manipulation logic. +#[derive(Debug)] +#[repr(C)] +#[allow(non_camel_case_types)] +pub struct Elf64_Rela { + pub r_offset: u64, + pub r_info: u64, + pub r_addend: i64, +} + +/// C-style 32-bit ELF Symbol definition +/// +/// These C-style definitions are for users who want to implement their own ELF manipulation logic. +#[derive(Debug)] +#[repr(C)] +#[allow(non_camel_case_types)] +pub struct Elf32_Sym { + pub st_name: u32, + pub st_value: u32, + pub st_size: u32, + pub st_info: u8, + pub st_other: u8, + pub st_shndx: u16, +} + +/// C-style 64-bit ELF Symbol definition +/// +/// These C-style definitions are for users who want to implement their own ELF manipulation logic. +#[derive(Debug)] +#[repr(C)] +#[allow(non_camel_case_types)] +pub struct Elf64_Sym { + pub st_name: u32, + pub st_info: u8, + pub st_other: u8, + pub st_shndx: u16, + pub st_value: u64, + pub st_size: u64, +} \ No newline at end of file diff --git a/src/compression.rs b/src/compression.rs index 1e11fdb..2f3326d 100644 --- a/src/compression.rs +++ b/src/compression.rs @@ -8,29 +8,7 @@ use crate::endian::EndianParse; use crate::file::Class; use crate::parse::{ParseAt, ParseError}; - -/// C-style 32-bit ELF Compression Header definition -/// -/// These C-style definitions are for users who want to implement their own ELF manipulation logic. -#[derive(Debug)] -#[repr(C)] -pub struct Elf32_Chdr { - pub ch_type: u32, - pub ch_size: u32, - pub ch_addralign: u32, -} - -/// C-style 64-bit ELF Compression Header definition -/// -/// These C-style definitions are for users who want to implement their own ELF manipulation logic. -#[derive(Debug)] -#[repr(C)] -pub struct Elf64_Chdr { - pub ch_type: u32, - pub ch_reserved: u32, - pub ch_size: u64, - pub ch_addralign: u64, -} +use crate::abi; #[derive(Debug, Clone, PartialEq, Eq)] pub struct CompressionHeader { @@ -67,8 +45,8 @@ impl ParseAt for CompressionHeader { #[inline] fn size_for(class: Class) -> usize { match class { - Class::ELF32 => 12, - Class::ELF64 => 24, + Class::ELF32 => core::mem::size_of::(), + Class::ELF64 => core::mem::size_of::(), } } } diff --git a/src/dynamic.rs b/src/dynamic.rs index 288b536..e8c4d2b 100644 --- a/src/dynamic.rs +++ b/src/dynamic.rs @@ -2,31 +2,10 @@ use crate::endian::EndianParse; use crate::file::Class; use crate::parse::{ParseAt, ParseError, ParsingTable}; +use crate::abi; pub type DynamicTable<'data, E> = ParsingTable<'data, E, Dyn>; -/// C-style 32-bit ELF Dynamic section entry definition -/// -/// These C-style definitions are for users who want to implement their own ELF manipulation logic. -#[derive(Debug)] -#[repr(C)] -pub struct Elf32_Dyn { - pub d_tag: i32, - // union of both {d_val, d_ptr} - pub d_un: u32, -} - -/// C-style 64-bit ELF Dynamic section entry definition -/// -/// These C-style definitions are for users who want to implement their own ELF manipulation logic. -#[derive(Debug)] -#[repr(C)] -pub struct Elf64_Dyn { - pub d_tag: i64, - // union of both {d_val, d_ptr} - pub d_un: u64, -} - #[derive(Debug, Clone, PartialEq, Eq)] pub struct Dyn { pub d_tag: i64, @@ -65,8 +44,8 @@ impl ParseAt for Dyn { #[inline] fn size_for(class: Class) -> usize { match class { - Class::ELF32 => 8, - Class::ELF64 => 16, + Class::ELF32 => core::mem::size_of::(), + Class::ELF64 => core::mem::size_of::(), } } } diff --git a/src/file.rs b/src/file.rs index bf4137d..ef17ae6 100644 --- a/src/file.rs +++ b/src/file.rs @@ -10,50 +10,6 @@ pub enum Class { ELF64, } -/// C-style 32-bit ELF File Header definition -/// -/// These C-style definitions are for users who want to implement their own ELF manipulation logic. -#[derive(Debug)] -#[repr(C)] -pub struct Elf32_Ehdr { - pub e_ident: [u8; abi::EI_NIDENT], - pub e_type: u16, - pub e_machine: u16, - pub e_version: u32, - pub e_entry: u32, - pub e_phoff: u32, - pub e_shoff: u32, - pub e_flags: u32, - pub e_ehsize: u16, - pub e_phentsize: u16, - pub e_phnum: u16, - pub e_shentsize: u16, - pub e_shnum: u16, - pub e_shstrndx: u16, -} - -/// C-style 64-bit ELF File Header definition -/// -/// These C-style definitions are for users who want to implement their own ELF manipulation logic. -#[derive(Debug)] -#[repr(C)] -pub struct Elf64_Ehdr { - pub e_ident: [u8; abi::EI_NIDENT], - pub e_type: u16, - pub e_machine: u16, - pub e_version: u32, - pub e_entry: u64, - pub e_phoff: u64, - pub e_shoff: u64, - pub e_flags: u32, - pub e_ehsize: u16, - pub e_phentsize: u16, - pub e_phnum: u16, - pub e_shentsize: u16, - pub e_shnum: u16, - pub e_shstrndx: u16, -} - /// Encapsulates the contents of the ELF File Header /// /// The ELF File Header starts off every ELF file and both identifies the @@ -118,8 +74,8 @@ pub struct FileHeader { pub e_shstrndx: u16, } -pub const ELF32_EHDR_TAILSIZE: usize = 36; -pub const ELF64_EHDR_TAILSIZE: usize = 48; +pub const ELF32_EHDR_TAILSIZE: usize = core::mem::size_of::() - abi::EI_NIDENT; +pub const ELF64_EHDR_TAILSIZE: usize = core::mem::size_of::() - abi::EI_NIDENT; fn verify_ident(buf: &[u8]) -> Result<(), ParseError> { // Verify the magic number diff --git a/src/relocation.rs b/src/relocation.rs index bbec4a0..07db353 100644 --- a/src/relocation.rs +++ b/src/relocation.rs @@ -2,30 +2,11 @@ use crate::endian::EndianParse; use crate::file::Class; use crate::parse::{ParseAt, ParseError, ParsingIterator}; +use crate::abi; pub type RelIterator<'data, E> = ParsingIterator<'data, E, Rel>; pub type RelaIterator<'data, E> = ParsingIterator<'data, E, Rela>; -/// C-style 32-bit ELF Relocation definition -/// -/// These C-style definitions are for users who want to implement their own ELF manipulation logic. -#[derive(Debug)] -#[repr(C)] -pub struct Elf32_Rel { - pub r_offset: u32, - pub r_info: u32, -} - -/// C-style 64-bit ELF Relocation definition -/// -/// These C-style definitions are for users who want to implement their own ELF manipulation logic. -#[derive(Debug)] -#[repr(C)] -pub struct Elf64_Rel { - pub r_offset: u64, - pub r_info: u64, -} - #[derive(Debug, Clone, PartialEq, Eq)] pub struct Rel { pub r_offset: u64, @@ -65,34 +46,12 @@ impl ParseAt for Rel { #[inline] fn size_for(class: Class) -> usize { match class { - Class::ELF32 => 8, - Class::ELF64 => 16, + Class::ELF32 => core::mem::size_of::(), + Class::ELF64 => core::mem::size_of::(), } } } -/// C-style 32-bit ELF Relocation (with addend) definition -/// -/// These C-style definitions are for users who want to implement their own ELF manipulation logic. -#[derive(Debug)] -#[repr(C)] -pub struct Elf32_Rela { - pub r_offset: u32, - pub r_info: u32, - pub r_addend: i32, -} - -/// C-style 64-bit ELF Relocation (with addend) definition -/// -/// These C-style definitions are for users who want to implement their own ELF manipulation logic. -#[derive(Debug)] -#[repr(C)] -pub struct Elf64_Rela { - pub r_offset: u64, - pub r_info: u64, - pub r_addend: i64, -} - #[derive(Debug, Clone, PartialEq, Eq)] pub struct Rela { pub r_offset: u64, @@ -137,8 +96,8 @@ impl ParseAt for Rela { #[inline] fn size_for(class: Class) -> usize { match class { - Class::ELF32 => 12, - Class::ELF64 => 24, + Class::ELF32 => core::mem::size_of::(), + Class::ELF64 => core::mem::size_of::(), } } } diff --git a/src/section.rs b/src/section.rs index a973051..4b4e666 100644 --- a/src/section.rs +++ b/src/section.rs @@ -2,45 +2,10 @@ use crate::endian::EndianParse; use crate::file::Class; use crate::parse::{ParseAt, ParseError, ParsingTable}; +use crate::abi; pub type SectionHeaderTable<'data, E> = ParsingTable<'data, E, SectionHeader>; -/// C-style 32-bit ELF Section Header definition -/// -/// These C-style definitions are for users who want to implement their own ELF manipulation logic. -#[derive(Debug)] -#[repr(C)] -pub struct Elf32_Shdr { - pub sh_name: u32, - pub sh_type: u32, - pub sh_flags: u32, - pub sh_addr: u32, - pub sh_offset: u32, - pub sh_size: u32, - pub sh_link: u32, - pub sh_info: u32, - pub sh_addralign: u32, - pub sh_entsize: u32, -} - -/// C-style 64-bit ELF Section Header definition -/// -/// These C-style definitions are for users who want to implement their own ELF manipulation logic. -#[derive(Debug)] -#[repr(C)] -pub struct Elf64_Shdr { - pub sh_name: u32, - pub sh_type: u32, - pub sh_flags: u64, - pub sh_addr: u64, - pub sh_offset: u64, - pub sh_size: u64, - pub sh_link: u32, - pub sh_info: u32, - pub sh_addralign: u64, - pub sh_entsize: u64, -} - /// Encapsulates the contents of an ELF Section Header /// /// This is a Rust-native type that represents a Section Header that is bit-width-agnostic. @@ -106,8 +71,8 @@ impl ParseAt for SectionHeader { #[inline] fn size_for(class: Class) -> usize { match class { - Class::ELF32 => 40, - Class::ELF64 => 64, + Class::ELF32 => core::mem::size_of::(), + Class::ELF64 => core::mem::size_of::(), } } } diff --git a/src/segment.rs b/src/segment.rs index 5f148f0..486cc28 100644 --- a/src/segment.rs +++ b/src/segment.rs @@ -2,41 +2,10 @@ use crate::endian::EndianParse; use crate::file::Class; use crate::parse::{ParseAt, ParseError, ParsingTable}; +use crate::abi; pub type SegmentTable<'data, E> = ParsingTable<'data, E, ProgramHeader>; -/// C-style 32-bit ELF Program Segment Header definition -/// -/// These C-style definitions are for users who want to implement their own ELF manipulation logic. -#[derive(Debug)] -#[repr(C)] -pub struct Elf32_Phdr { - pub p_type: u32, - pub p_offset: u32, - pub p_vaddr: u32, - pub p_paddr: u32, - pub p_filesz: u32, - pub p_memsz: u32, - pub p_flags: u32, - pub p_align: u32, -} - -/// C-style 64-bit ELF Program Segment Header definition -/// -/// These C-style definitions are for users who want to implement their own ELF manipulation logic. -#[derive(Debug)] -#[repr(C)] -pub struct Elf64_Phdr { - pub p_type: u32, - pub p_flags: u32, - pub p_offset: u64, - pub p_vaddr: u64, - pub p_paddr: u64, - pub p_filesz: u64, - pub p_memsz: u64, - pub p_align: u64, -} - /// Encapsulates the contents of an ELF Program Header /// /// The program header table is an array of program header structures describing @@ -105,8 +74,8 @@ impl ParseAt for ProgramHeader { #[inline] fn size_for(class: Class) -> usize { match class { - Class::ELF32 => 32, - Class::ELF64 => 56, + Class::ELF32 => core::mem::size_of::(), + Class::ELF64 => core::mem::size_of::(), } } } diff --git a/src/symbol.rs b/src/symbol.rs index 4be8855..f73db38 100644 --- a/src/symbol.rs +++ b/src/symbol.rs @@ -6,34 +6,6 @@ use crate::parse::{ParseAt, ParseError, ParsingTable}; pub type SymbolTable<'data, E> = ParsingTable<'data, E, Symbol>; -/// C-style 32-bit ELF Symbol definition -/// -/// These C-style definitions are for users who want to implement their own ELF manipulation logic. -#[derive(Debug)] -#[repr(C)] -pub struct Elf32_Sym { - pub st_name: u32, - pub st_value: u32, - pub st_size: u32, - pub st_info: u8, - pub st_other: u8, - pub st_shndx: u32, -} - -/// C-style 64-bit ELF Symbol definition -/// -/// These C-style definitions are for users who want to implement their own ELF manipulation logic. -#[derive(Debug)] -#[repr(C)] -pub struct Elf64_Sym { - pub st_name: u32, - pub st_info: u8, - pub st_other: u8, - pub st_shndx: u16, - pub st_value: u64, - pub st_size: u64, -} - #[derive(Debug, Clone, PartialEq, Eq)] pub struct Symbol { /// This member holds an index into the symbol table's string table, @@ -145,8 +117,8 @@ impl ParseAt for Symbol { #[inline] fn size_for(class: Class) -> usize { match class { - Class::ELF32 => 16, - Class::ELF64 => 24, + Class::ELF32 => core::mem::size_of::(), + Class::ELF64 => core::mem::size_of::(), } } }