elf_utilities/relocation/
elf32.rs

1use crate::*;
2use serde::{Deserialize, Serialize};
3
4#[derive(
5    Default, Debug, Clone, Copy, Hash, PartialOrd, Ord, PartialEq, Eq, Serialize, Deserialize,
6)]
7#[repr(C)]
8pub struct Rela32 {
9    /// Location at which to apply the action
10    r_offset: Elf32Addr,
11    /// index and type of relocation
12    r_info: Elf32Word,
13    /// Constant addend used to compute value
14    r_addend: Elf32Sword,
15}
16
17#[allow(dead_code)]
18impl Rela32 {
19    pub const SIZE: Elf32Xword = 12;
20    pub fn get_sym(&self) -> Elf32Word {
21        self.r_info >> 8
22    }
23    pub fn get_type(&self) -> Elf32Word {
24        self.r_info & 0xff
25    }
26
27    pub fn get_offset(&self) -> Elf32Addr {
28        self.r_offset
29    }
30    pub fn get_info(&self) -> Elf32Word {
31        self.r_info
32    }
33    pub fn get_addend(&self) -> Elf32Sword {
34        self.r_addend
35    }
36
37    pub fn set_addend(&mut self, addend: Elf32Sword) {
38        self.r_addend = addend;
39    }
40    pub fn set_offset(&mut self, offset: Elf32Addr) {
41        self.r_offset = offset;
42    }
43    pub fn set_info(&mut self, info: Elf32Word) {
44        self.r_info = info;
45    }
46
47    /// Create Vec<u8> from this.
48    ///
49    /// # Examples
50    ///
51    /// ```
52    /// use elf_utilities::relocation::Rela32;
53    /// let null_rel : Rela32 = Default::default();
54    ///
55    /// assert_eq!([0].repeat(Rela32::SIZE as usize), null_rel.to_le_bytes());
56    /// ```
57    pub fn to_le_bytes(&self) -> Vec<u8> {
58        bincode::serialize(self).unwrap()
59    }
60
61    pub fn deserialize(buf: &[u8], start: usize) -> Result<Self, Box<dyn std::error::Error>> {
62        // bincode::ErrorKindをトレイトオブジェクトとするため,この冗長な書き方が必要
63        match bincode::deserialize(&buf[start..]) {
64            Ok(header) => Ok(header),
65            Err(e) => Err(e),
66        }
67    }
68}