This repository has been archived by the owner on Jul 10, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
elf_ehdr.rs
344 lines (312 loc) · 11.5 KB
/
elf_ehdr.rs
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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
use ::std::io::{Read, Seek};
use super::elf_ei_class::ElfEiClass;
#[macro_export]
macro_rules! elf_ehdr {
() => {
use ::std::fmt::{Display, Formatter};
use ::to_host::to_host_copy::ToHostCopy;
use ::to_host::to_host_in_place::ToHostInPlace;
use ::to_host::{Endianness, ToHostInPlaceStruct, ToHostCopyStruct};
use super::super::elf_ident::ElfIdent;
use super::super::elf_ident_named::ElfIdentNamed;
use super::super::elf_ehdr_type::ElfEhdrType;
use super::super::elf_ehdr_machine::ElfEhdrMachine;
use super::super::elf_ehdr::validate_magic;
#[repr(C)]
#[derive(Debug, Clone, Copy)]
pub struct Elf_Ehdr<H, W, A, O> {
e_ident: ElfIdent,
e_type: ElfEhdrType,
e_machine: ElfEhdrMachine,
e_version: W,
e_entry: A,
e_phoff: O,
e_shoff: O,
e_flags: W,
e_ehsize: H,
e_phentsize: H,
e_phnum: H,
e_shentsize: H,
e_shnum: H,
e_shstrndx: H
}
impl<H, W, A, O> Display
for Elf_Ehdr<H, W, A, O>
where
H: ToHostCopy + ::std::fmt::LowerHex + ::std::fmt::Display,
W: ToHostCopy + ::std::fmt::LowerHex,
A: ToHostCopy + ::std::fmt::LowerHex,
O: ToHostCopy + ::std::fmt::LowerHex + ::std::fmt::Display,
{
fn fmt(&self, fmt: &mut Formatter) -> ::std::fmt::Result {
let ehdr_ident: &ElfIdentNamed = unsafe {
::std::mem::transmute(&self.e_ident)
};
let e = self.get_endianness();
write!(
fmt,
concat!(
"ELF Header:\n",
" Magic: {}\n",
"{}",
" Type: {}\n",
" Machine: {}\n",
" Version: {:#x}\n",
" Entry point address: {:#x}\n",
" Start of program headers: {} (bytes into file)\n",
" Start of section headers: {} (bytes into file)\n",
" Flags: {:#x}\n",
" Size of this header: {} (bytes)\n",
" Size of program headers: {} (bytes)\n",
" Number of program headers: {}\n",
" Size of section headers: {} (bytes)\n",
" Number of section headers: {}\n",
" Section header string table index: {}\n",
),
self.e_ident,
ehdr_ident,
self.e_type.to_host_copy(&e),
self.e_machine.to_host_copy(&e),
self.e_version.to_host_copy(&e),
self.e_entry.to_host_copy(&e),
self.e_phoff.to_host_copy(&e),
self.e_shoff.to_host_copy(&e),
self.e_flags.to_host_copy(&e),
self.e_ehsize.to_host_copy(&e),
self.e_phentsize.to_host_copy(&e),
self.e_phnum.to_host_copy(&e),
self.e_shentsize.to_host_copy(&e),
self.e_shnum.to_host_copy(&e),
self.e_shstrndx.to_host_copy(&e))
}
}
impl<H, W, A, O> Elf_Ehdr<H, W, A, O>
where
H: Copy,
W: Copy,
A: Copy,
O: Copy,
{
#[allow(dead_code)]
fn from_slice(buffer: &[u8]) -> Result<&Elf_Ehdr<H, W, A, O>, ()>
{
try!(validate_magic(buffer));
let ehdr_ptr: *const Elf_Ehdr<H, W, A, O> = unsafe {
::std::mem::transmute(buffer.as_ptr())
};
let ehdr: &Elf_Ehdr<H, W, A, O> = unsafe { &*ehdr_ptr };
Ok(ehdr)
}
#[allow(dead_code)]
pub fn get_phentsize(&self) -> H {
self.e_phentsize
}
#[allow(dead_code)]
pub fn get_phnum(&self) -> H {
self.e_phnum
}
#[allow(dead_code)]
pub fn get_phoff(&self) -> O {
self.e_phoff
}
#[allow(dead_code)]
pub fn get_entry(&self) -> A {
self.e_entry
}
#[allow(dead_code)]
pub fn get_ident(&self) -> ElfIdentNamed {
unsafe {
::std::mem::transmute(self.e_ident)
}
}
#[allow(dead_code)]
pub fn get_type(&self) -> ElfEhdrType {
self.e_type
}
#[allow(dead_code)]
pub fn get_machine(&self) -> ElfEhdrMachine {
self.e_machine
}
#[allow(dead_code)]
pub fn get_version(&self) -> W {
self.e_version
}
#[allow(dead_code)]
pub fn get_shoff(&self) -> O {
self.e_shoff
}
#[allow(dead_code)]
pub fn get_flags(&self) -> W {
self.e_flags
}
#[allow(dead_code)]
pub fn get_ehsize(&self) -> H {
self.e_ehsize
}
#[allow(dead_code)]
pub fn get_shentsize(&self) -> H {
self.e_shentsize
}
#[allow(dead_code)]
pub fn get_shnum(&self) -> H {
self.e_shnum
}
#[allow(dead_code)]
pub fn get_shstrndx(&self) -> H {
self.e_shstrndx
}
#[allow(dead_code)]
pub fn get_endianness(&self) -> Endianness {
let ehdr_ptr: *mut Elf_Ehdr<H, W, A, O> = unsafe {
::std::mem::transmute(self)
};
let ehdr: &mut Elf_Ehdr<H, W, A, O> = unsafe { &mut *ehdr_ptr };
let ehdr_ident: &ElfIdentNamed = unsafe {
::std::mem::transmute(&ehdr.e_ident)
};
ehdr_ident.get_endianness()
}
#[allow(dead_code)]
fn convert_byte_vec_to_ehdr_box(
mut v: Vec<u8>)
-> Result<Box<Self>, ()>
{
let ehdr_size = ::std::mem::size_of::<Self>();
assert_eq!(ehdr_size as usize, v.len());
try!(validate_magic(&v));
let bytes_ptr: *mut u8 = v.as_mut_ptr();
::std::mem::forget(v);
let ehdr_ptr: *mut Self = unsafe {
::std::mem::transmute(bytes_ptr)
};
let ehdr_box: Box<Self> = unsafe {
Box::from_raw(ehdr_ptr)
};
Ok(ehdr_box)
}
#[allow(dead_code)]
pub fn read_ehdr<R: Read + Seek>(
reader: &mut R)
-> Box<Self>
{
use std::io::SeekFrom;
let ehdr_size = ::std::mem::size_of::<Self>();
let ehdr_offset = 0;
let mut b = Vec::<u8>::with_capacity(ehdr_size as usize);
reader.seek(SeekFrom::Start(ehdr_offset)).unwrap();
reader.take(ehdr_size as u64).read_to_end(&mut b).unwrap();
Self::convert_byte_vec_to_ehdr_box(b).unwrap()
}
}
impl<H, W, A, O> ToHostInPlaceStruct for Elf_Ehdr<H, W, A, O>
where
H: ToHostInPlace,
W: ToHostInPlace,
A: ToHostInPlace,
O: ToHostInPlace,
{
fn to_host_in_place(&mut self, endianness: &Endianness) {
let e = endianness;
self.e_type.to_host_in_place(e);
self.e_machine.to_host_in_place(e);
self.e_version.to_host_in_place(e);
self.e_entry.to_host_in_place(e);
self.e_phoff.to_host_in_place(e);
self.e_shoff.to_host_in_place(e);
self.e_flags.to_host_in_place(e);
self.e_ehsize.to_host_in_place(e);
self.e_phentsize.to_host_in_place(e);
self.e_phnum.to_host_in_place(e);
self.e_shentsize.to_host_in_place(e);
self.e_shnum.to_host_in_place(e);
self.e_shstrndx.to_host_in_place(e);
}
}
impl<H, W, A, O> ToHostCopyStruct for Elf_Ehdr<H, W, A, O>
where
H: ToHostCopy,
W: ToHostCopy,
A: ToHostCopy,
O: ToHostCopy,
{
fn to_host_copy(&self, endianness: &Endianness) -> Self {
let e = endianness;
Elf_Ehdr {
e_ident: self.e_ident,
e_type: self.e_type.to_host_copy(e),
e_machine: self.e_machine.to_host_copy(e),
e_version: self.e_version.to_host_copy(e),
e_entry: self.e_entry.to_host_copy(e),
e_phoff: self.e_phoff.to_host_copy(e),
e_shoff: self.e_shoff.to_host_copy(e),
e_flags: self.e_flags.to_host_copy(e),
e_ehsize: self.e_ehsize.to_host_copy(e),
e_phentsize: self.e_phentsize.to_host_copy(e),
e_phnum: self.e_phnum.to_host_copy(e),
e_shentsize: self.e_shentsize.to_host_copy(e),
e_shnum: self.e_shnum.to_host_copy(e),
e_shstrndx: self.e_shstrndx.to_host_copy(e),
}
}
}
impl<H, W, A, O> Elf_Ehdr_T<H, O> for Elf_Ehdr<H, W, A, O>
where
H: ::num::PrimInt,
O: ::num::PrimInt,
{
fn get_phentsize(&self) -> H
{
self.e_phentsize
}
fn get_phnum(&self) -> H
{
self.e_phnum
}
fn get_phoff(&self) -> O
{
self.e_phoff
}
}
}
}
pub trait Elf_Ehdr_T<H, O>
where
H: ::num::PrimInt,
O: ::num::PrimInt,
{
fn get_phentsize(&self) -> H;
fn get_phnum(&self) -> H;
fn get_phoff(&self) -> O;
}
#[allow(dead_code)]
pub fn read_class<R: Read + Seek>(
reader: &mut R)
-> Result<ElfEiClass, ()>
{
use std::io::SeekFrom;
use super::elf_ident_named::{
EI_MAGIC_SIZE, EI_MAGIC_OFFSET, EI_CLASS_SIZE, EI_CLASS_OFFSET};
let magic_class_size = EI_MAGIC_SIZE + EI_CLASS_SIZE;
let ehdr_offset = EI_MAGIC_OFFSET;
let mut b = Vec::<u8>::with_capacity(magic_class_size as usize);
reader.seek(SeekFrom::Start(ehdr_offset as u64)).unwrap();
reader.take(magic_class_size as u64).read_to_end(&mut b).unwrap();
try!(validate_magic(&b));
ElfEiClass::from_u8(b[EI_CLASS_OFFSET])
}
#[allow(dead_code)]
pub fn validate_magic(buffer: &[u8]) -> Result<(), ()>
{
use super::elf_ident_named::EI_MAGIC_SIZE;
let proper_magic = &[0x7f, b'E', b'L', b'F'];
assert_eq!(proper_magic.len(), EI_MAGIC_SIZE);
if buffer.len() < 4 {
return Err(())
}
let magic = &buffer[..4];
if magic == proper_magic {
Ok(())
} else {
Err(())
}
}