Skip to main content

plt_rs/
elf64.rs

1use super::DynamicSectionType;
2use core::{error::Error, fmt::Display};
3
4pub type Word = libc::Elf64_Word;
5pub type Half = libc::Elf64_Half;
6pub type Addr = libc::Elf64_Addr;
7pub type ExtendedWord = libc::Elf64_Xword;
8
9// manual impl doesn't exist everywhere
10pub type ExtendedSignedWord = i64;
11
12pub type ProgramHeader = libc::Elf64_Phdr;
13
14#[repr(C)]
15#[derive(Debug)]
16pub struct DynEntry {
17    pub d_tag: libc::Elf64_Xword,
18    /// Either a value (Elf64_Xword) or an address (Elf64_Addr)
19    pub d_val_ptr: libc::Elf64_Xword,
20}
21
22#[repr(C)]
23#[derive(Debug)]
24pub struct DynSym {
25    pub st_name: self::Word,
26    pub st_info: u8,
27    pub st_other: u8,
28    pub st_shndx: self::Half,
29    pub st_value: self::Addr,
30    pub st_size: self::ExtendedWord,
31}
32
33#[repr(C)]
34#[derive(Debug)]
35pub struct DynRel {
36    pub r_offset: self::Addr,
37    pub r_info: self::ExtendedWord,
38}
39
40impl DynRel {
41    pub fn symbol_index(&self) -> self::Word {
42        (self.r_info >> 32) as self::Word
43    }
44    pub fn symbol_type(&self) -> self::Word {
45        (self.r_info & 0xffffffff) as self::Word
46    }
47}
48
49#[repr(C)]
50#[derive(Debug)]
51pub struct DynRela {
52    pub r_offset: self::Addr,
53    pub r_info: self::ExtendedWord,
54    pub r_addend: self::ExtendedSignedWord,
55}
56
57impl DynRela {
58    pub fn symbol_index(&self) -> self::Word {
59        (self.r_info >> 32) as self::Word
60    }
61    pub fn symbol_type(&self) -> self::Word {
62        (self.r_info & 0xffffffff) as self::Word
63    }
64}
65
66/// An unknown Dynamic Section Type was observed
67#[derive(Debug)]
68pub struct DynTypeError(self::ExtendedWord);
69impl Display for DynTypeError {
70    fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
71        write!(f, "Unknown Dynamic section type witnessed: {}", self.0)
72    }
73}
74impl Error for DynTypeError {}
75impl TryFrom<self::ExtendedWord> for DynamicSectionType {
76    type Error = DynTypeError;
77    fn try_from(value: self::ExtendedWord) -> Result<Self, Self::Error> {
78        use DynamicSectionType::*;
79        Ok(match value {
80            0 => DT_NULL,
81
82            2 => DT_PLTRELSZ,
83            3 => DT_PLTGOT,
84            20 => DT_PLTREL,
85
86            5 => DT_STRTAB,
87            6 => DT_SYMTAB,
88            11 => DT_SYMENT,
89
90            17 => DT_REL,
91            18 => DT_RELSZ,
92            19 => DT_RELENT,
93
94            7 => DT_RELA,
95            8 => DT_RELASZ,
96            9 => DT_RELAENT,
97
98            10 => DT_STRSZ,
99            23 => DT_JMPREL,
100
101            tag => return Err(DynTypeError(tag)),
102        })
103    }
104}