1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
//! Type definitions for 64-bit ELF binaries.

use crate::*;

use crate::segment::*;
use serde::{Deserialize, Serialize};

#[derive(Debug, Clone, Copy, Hash, PartialOrd, Ord, PartialEq, Eq, Serialize, Deserialize)]
pub struct Segment64 {
    pub header: Phdr64,
}

impl Segment64 {
    pub fn new(header: Phdr64) -> Self {
        Self { header }
    }
}

#[repr(C)]
#[derive(Debug, Clone, Copy, Hash, PartialOrd, Ord, PartialEq, Eq, Serialize, Deserialize)]
pub struct Phdr64 {
    /// Segment type
    pub p_type: Elf64Word,

    /// Segment flags
    pub p_flags: Elf64Word,

    /// Segment file offset
    pub p_offset: Elf64Off,

    /// Segment virtual address
    pub p_vaddr: Elf64Addr,

    /// Segment physical address
    pub p_paddr: Elf64Addr,

    /// Segment size in file
    pub p_filesz: Elf64Xword,

    /// Segment size in memory
    pub p_memsz: Elf64Xword,

    /// Segment alignment
    pub p_align: Elf64Xword,
}

impl Default for Phdr64 {
    fn default() -> Self {
        Self {
            p_type: 0,
            p_flags: 0,
            p_offset: 0,
            p_vaddr: 0,
            p_paddr: 0,
            p_filesz: 0,
            p_memsz: 0,
            p_align: 0,
        }
    }
}

impl Phdr64 {
    pub fn size() -> Elf64Half {
        0x38
    }

    // getter
    pub fn get_type(&self) -> segment_type::Type {
        segment_type::Type::from(self.p_type)
    }

    // setter
    /// # Examples
    ///
    /// ```
    /// use elf_utilities::segment;
    ///
    /// let mut phdr : segment::Phdr64 = Default::default();
    /// phdr.set_type(segment::Type::Load);
    ///
    /// assert_eq!(phdr.get_type(), segment::Type::Load);
    /// ```
    pub fn set_type(&mut self, ptype: segment_type::Type) {
        self.p_type = ptype.to_bytes();
    }

    /// Create Vec<u8> from this.
    ///
    /// # Examples
    ///
    /// ```
    /// use elf_utilities::segment::Phdr64;
    /// let null_phdr : Phdr64 = Default::default();
    ///
    /// assert_eq!([0].repeat(Phdr64::size() as usize), null_phdr.to_le_bytes());
    /// ```
    pub fn to_le_bytes(&self) -> Vec<u8> {
        bincode::serialize(self).unwrap()
    }

    pub fn deserialize(buf: &[u8], start: usize) -> Result<Self, Box<dyn std::error::Error>> {
        // bincode::ErrorKindをトレイトオブジェクトとするため,この冗長な書き方が必要
        match bincode::deserialize(&buf[start..]) {
            Ok(header) => Ok(header),
            Err(e) => Err(e),
        }
    }
}