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
use crate::section::section_type;
use crate::*;
use serde::{Deserialize, Serialize};
#[derive(Clone, Hash, PartialOrd, Ord, PartialEq, Eq, Serialize, Deserialize)]
pub struct Section64 {
pub name: String,
pub header: Shdr64,
pub bytes: Option<Vec<u8>>,
pub symbols: Option<Vec<symbol::Symbol64>>,
pub rela_symbols: Option<Vec<relocation::Rela64>>,
}
impl Section64 {
pub fn new(section_name: String, shdr: Shdr64) -> Self {
Self {
name: section_name,
header: shdr,
bytes: None,
symbols: None,
rela_symbols: None,
}
}
pub fn write_byte_to_index(&mut self, byte: u8, idx: usize) {
if let Some(ref mut bytes) = self.bytes {
bytes[idx] = byte;
}
}
pub fn to_le_bytes(&self) -> Vec<u8> {
match self.header.get_type() {
section_type::TYPE::SYMTAB => {
let mut bytes = Vec::new();
for sym in self.symbols.as_ref().unwrap().iter() {
bytes.append(&mut sym.to_le_bytes());
}
bytes
}
section_type::TYPE::RELA => {
let mut bytes = Vec::new();
for rela in self.rela_symbols.as_ref().unwrap().iter() {
bytes.append(&mut rela.to_le_bytes());
}
if let Some(bts) = &self.bytes {
bytes.append(&mut bts.clone());
}
bytes
}
_ => self.bytes.as_ref().unwrap().clone(),
}
}
pub fn new_null_section() -> Self {
let mut null_section = Self::new(String::new(), Default::default());
null_section.bytes = Some(Vec::new());
null_section
}
}
#[derive(Clone, Copy, Hash, PartialOrd, Ord, PartialEq, Eq, Serialize, Deserialize)]
#[repr(C)]
pub struct Shdr64 {
sh_name: Elf64Word,
sh_type: Elf64Word,
sh_flags: Elf64Xword,
sh_addr: Elf64Addr,
sh_offset: Elf64Off,
sh_size: Elf64Xword,
sh_link: Elf64Word,
sh_info: Elf64Word,
sh_addralign: Elf64Xword,
sh_entsize: Elf64Xword,
}
impl Default for Shdr64 {
fn default() -> Self {
Self {
sh_name: 0,
sh_type: 0,
sh_flags: 0,
sh_addr: 0,
sh_offset: 0,
sh_size: 0,
sh_link: 0,
sh_info: 0,
sh_addralign: 0,
sh_entsize: 0,
}
}
}
#[allow(dead_code)]
impl Shdr64 {
pub fn size() -> Elf64Half {
0x40
}
pub fn get_type(&self) -> section_type::TYPE {
section_type::TYPE::from(self.sh_type)
}
pub fn get_link(&self) -> Elf64Word {
self.sh_link
}
pub fn get_size(&self) -> Elf64Xword {
self.sh_size
}
pub fn get_info(&self) -> Elf64Word {
self.sh_info
}
pub fn get_entry_size(&self) -> Elf64Xword {
self.sh_entsize
}
pub fn get_flags(&self) -> Elf64Xword {
self.sh_flags
}
pub fn get_addralign(&self) -> Elf64Xword {
self.sh_addralign
}
pub fn get_offset(&self) -> Elf64Off {
self.sh_offset
}
pub fn get_addr(&self) -> Elf64Addr {
self.sh_addr
}
pub fn set_name(&mut self, name: Elf64Word) {
self.sh_name = name;
}
pub fn set_offset(&mut self, offset: Elf64Off) {
self.sh_offset = offset;
}
pub fn set_type(&mut self, ty: section_type::TYPE) {
self.sh_type = ty.to_bytes();
}
pub fn set_size(&mut self, size: Elf64Xword) {
self.sh_size = size;
}
pub fn set_link(&mut self, link: Elf64Word) {
self.sh_link = link;
}
pub fn set_info(&mut self, info: Elf64Word) {
self.sh_info = info;
}
pub fn set_entry_size(&mut self, entry_size: Elf64Xword) {
self.sh_entsize = entry_size;
}
pub fn set_flags(&mut self, flags: Elf64Xword) {
self.sh_flags = flags;
}
pub fn set_addralign(&mut self, addralign: Elf64Xword) {
self.sh_addralign = addralign;
}
pub fn set_addr(&mut self, addr: Elf64Addr) {
self.sh_addr = addr;
}
pub fn to_le_bytes(&self) -> Vec<u8> {
bincode::serialize(self).unwrap()
}
}