wraith/structures/pe/
relocations.rs

1//! Base relocation structures
2
3#[repr(C)]
4#[derive(Debug, Clone, Copy)]
5pub struct BaseRelocation {
6    pub virtual_address: u32,
7    pub size_of_block: u32,
8    // followed by Type/Offset entries
9}
10
11impl BaseRelocation {
12    /// number of relocation entries in this block
13    pub fn entry_count(&self) -> usize {
14        if self.size_of_block <= 8 {
15            0
16        } else {
17            ((self.size_of_block - 8) / 2) as usize
18        }
19    }
20}
21
22/// relocation entry (2 bytes: 4 bits type, 12 bits offset)
23#[repr(transparent)]
24#[derive(Debug, Clone, Copy)]
25pub struct RelocationEntry(pub u16);
26
27impl RelocationEntry {
28    pub fn reloc_type(&self) -> u8 {
29        (self.0 >> 12) as u8
30    }
31
32    pub fn offset(&self) -> u16 {
33        self.0 & 0x0FFF
34    }
35}
36
37/// relocation types
38#[repr(u8)]
39#[derive(Debug, Clone, Copy, PartialEq, Eq)]
40pub enum RelocationType {
41    Absolute = 0, // skip
42    High = 1,     // high 16 bits
43    Low = 2,      // low 16 bits
44    HighLow = 3,  // full 32 bits (x86)
45    HighAdj = 4,  // high 16 bits adjusted
46    Dir64 = 10,   // full 64 bits (x64)
47}
48
49impl From<u8> for RelocationType {
50    fn from(val: u8) -> Self {
51        match val {
52            0 => Self::Absolute,
53            1 => Self::High,
54            2 => Self::Low,
55            3 => Self::HighLow,
56            4 => Self::HighAdj,
57            10 => Self::Dir64,
58            _ => Self::Absolute, // treat unknown as skip
59        }
60    }
61}