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
use crate::header::{class, data, elf_type, machine, osabi, version};
use crate::*;
pub const ELF_MAGIC_NUMBER: u128 = (0x7f45_4c46) << (12 * 8);
#[repr(C)]
pub struct Ehdr64 {
e_ident: u128,
e_type: Elf64Half,
e_machine: Elf64Half,
e_version: Elf64Word,
e_entry: Elf64Addr,
e_phoff: Elf64Off,
e_shoff: Elf64Off,
e_flags: Elf64Word,
e_ehsize: Elf64Half,
e_phentsize: Elf64Half,
e_phnum: Elf64Half,
e_shentsize: Elf64Half,
e_shnum: Elf64Half,
e_shstrndx: Elf64Half,
}
impl Default for Ehdr64 {
fn default() -> Self {
Self {
e_ident: ELF_MAGIC_NUMBER,
e_type: 0,
e_machine: 0,
e_version: 0,
e_entry: 0,
e_phoff: 0,
e_shoff: 0,
e_flags: 0,
e_ehsize: 0,
e_phentsize: 0,
e_phnum: 0,
e_shentsize: 0,
e_shnum: 0,
e_shstrndx: 0,
}
}
}
impl Ehdr64 {
pub fn size() -> Elf64Half {
0x40
}
pub fn get_identification(&self) -> u128 {
self.e_ident
}
pub fn get_elf_type(&self) -> elf_type::ELFTYPE {
elf_type::ELFTYPE::from(self.e_type)
}
pub fn get_machine(&self) -> machine::ELFMACHINE {
machine::ELFMACHINE::from(self.e_machine)
}
pub fn set_class(&mut self, class: class::ELFCLASS) {
self.e_ident |= class.to_identifier();
}
pub fn set_data(&mut self, data: data::ELFDATA) {
self.e_ident |= data.to_identifier();
}
pub fn set_version(&mut self, version: version::ELFVERSION) {
self.e_ident |= version.to_identifier();
}
pub fn set_osabi(&mut self, osabi: osabi::ELFOSABI) {
self.e_ident |= osabi.to_identifier();
}
pub fn set_elf_type(&mut self, e_type: elf_type::ELFTYPE) {
self.e_type = e_type.to_bytes();
}
pub fn set_machine(&mut self, e_machine: machine::ELFMACHINE) {
self.e_machine = e_machine.to_bytes();
}
pub fn to_le_bytes(&self) -> Vec<u8> {
let mut bytes: Vec<u8> = Vec::new();
for byte in self.e_ident.to_be_bytes().to_vec() {
bytes.push(byte);
}
for byte in self.e_type.to_le_bytes().to_vec() {
bytes.push(byte);
}
for byte in self.e_machine.to_le_bytes().to_vec() {
bytes.push(byte);
}
for byte in self.e_version.to_le_bytes().to_vec() {
bytes.push(byte);
}
for byte in self.e_entry.to_le_bytes().to_vec() {
bytes.push(byte);
}
for byte in self.e_phoff.to_le_bytes().to_vec() {
bytes.push(byte);
}
for byte in self.e_shoff.to_le_bytes().to_vec() {
bytes.push(byte);
}
for byte in self.e_flags.to_le_bytes().to_vec() {
bytes.push(byte);
}
for byte in self.e_ehsize.to_le_bytes().to_vec() {
bytes.push(byte);
}
for byte in self.e_phentsize.to_le_bytes().to_vec() {
bytes.push(byte);
}
for byte in self.e_phnum.to_le_bytes().to_vec() {
bytes.push(byte);
}
for byte in self.e_shentsize.to_le_bytes().to_vec() {
bytes.push(byte);
}
for byte in self.e_shnum.to_le_bytes().to_vec() {
bytes.push(byte);
}
for byte in self.e_shstrndx.to_le_bytes().to_vec() {
bytes.push(byte);
}
bytes
}
}