plt_rs/
elf32.rs

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