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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
use std::io;
use super::{
Elf_Word,
Elf_Addr,
Elf_VarWord,
Elf_Off
};
use super::value::{
read_word,
read_plat_word,
read_addr,
read_off
};
#[derive(Clone,Copy,Debug,PartialEq,Eq)]
#[allow(dead_code)]
pub struct ProgramHeaderTable {
pub kind: Elf_Word,
pub offset: Elf_Off,
pub vaddr: Elf_Addr,
pub paddr: Elf_Addr,
pub flags: Elf_Word,
pub memsize: Elf_VarWord,
pub align: Elf_VarWord,
pub filesz: Elf_VarWord
}
#[derive(Copy,Clone,Debug,PartialEq,Eq)]
pub enum RamFlags {
Read,
Write,
Exec
}
const MASK: &'static [usize;3] = &[
0x4,
0x2,
0x1
];
const OUTS: &'static [RamFlags;3] = &[
RamFlags::Read,
RamFlags::Write,
RamFlags::Exec
];
impl ProgramHeaderTable {
#[allow(dead_code)]
pub fn ram_flags(&self) -> Vec<RamFlags> {
let flags: usize = self.flags.into();
let lambda = | x: (&usize,&RamFlags) | -> Option<RamFlags> {
if (flags & *x.0) != 0 {
Some(x.1.clone())
} else {
None
}
};
MASK.iter().zip(OUTS.iter()).filter_map(lambda).collect()
}
#[allow(dead_code)]
pub fn read_data<R: io::Read+io::Seek>(&self,r: &mut R) -> io::Result<Vec<u8>> {
#[allow(unused_imports)]
use std::io::Read;
#[allow(unused_imports)]
use std::io::SeekFrom;
#[allow(unused_imports)]
use std::io::Seek;
let offset: usize = self.offset.into();
let size: usize = self.filesz.into();
let mut ret_vec = Vec::with_capacity(size);
unsafe{ret_vec.set_len(size)};
let offset = SeekFrom::Start(offset as u64);
let _ = r.seek(offset)?;
let _ = r.read_exact(ret_vec.as_mut_slice())?;
Ok(ret_vec)
}
#[allow(dead_code)]
pub fn borrow_data<'a>(&self, buff: &'a [u8]) -> Option<&'a [u8]> {
let offset: usize = self.offset.into();
let size: usize = self.filesz.into();
let end = offset+size;
if buff.len() >= end {
Some( &buff[offset..end])
} else {
None
}
}
}
named!(pub read_pht_64<ProgramHeaderTable>, chain!(
ptype: read_word~
pflags: read_word~
poffset: read_off~
pvaddr: read_addr~
ppaddr: read_addr~
pfilesz: read_plat_word~
pmemsz: read_plat_word~
palign: read_plat_word,
|| ProgramHeaderTable {
kind: ptype,
offset: poffset,
vaddr: pvaddr,
paddr: ppaddr,
flags: pflags,
memsize: pmemsz,
align: palign,
filesz: pfilesz
}
));
named!(pub read_pht_32<ProgramHeaderTable>,chain!(
ptype: read_word~
poffset: read_off~
pvaddr: read_addr~
ppaddr: read_addr~
pfilesz: read_plat_word~
pmemsz: read_plat_word~
pflags: read_word~
palign: read_plat_word,
|| ProgramHeaderTable {
kind: ptype,
offset: poffset,
vaddr: pvaddr,
paddr: ppaddr,
flags: pflags,
memsize: pmemsz,
align: palign,
filesz: pfilesz
}
));