elf_utilities/relocation/
elf64.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 Rela64 {
9    /// Location at which to apply the action
10    r_offset: Elf64Addr,
11    /// index and type of relocation
12    r_info: Elf64Xword,
13    /// Constant addend used to compute value
14    r_addend: Elf64Sxword,
15}
16
17#[allow(dead_code)]
18impl Rela64 {
19    pub const SIZE: Elf64Xword = 24;
20    pub fn get_sym(&self) -> Elf64Xword {
21        self.r_info >> 32
22    }
23    pub fn get_type(&self) -> Elf64Xword {
24        self.r_info & 0xffffffff
25    }
26
27    pub fn get_offset(&self) -> Elf64Addr {
28        self.r_offset
29    }
30    pub fn get_info(&self) -> Elf64Xword {
31        self.r_info
32    }
33    pub fn get_addend(&self) -> Elf64Sxword {
34        self.r_addend
35    }
36
37    pub fn set_addend(&mut self, addend: Elf64Sxword) {
38        self.r_addend = addend;
39    }
40    pub fn set_offset(&mut self, offset: Elf64Addr) {
41        self.r_offset = offset;
42    }
43    pub fn set_info(&mut self, info: Elf64Xword) {
44        self.r_info = info;
45    }
46
47    /// Create Vec<u8> from this.
48    ///
49    /// # Examples
50    ///
51    /// ```
52    /// use elf_utilities::relocation::Rela64;
53    /// let null_rel : Rela64 = Default::default();
54    ///
55    /// assert_eq!([0].repeat(Rela64::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}