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
use crate::section::section_type;
use crate::*;
pub struct Section64 {
pub name: String,
pub header: Shdr64,
pub bytes: Vec<u8>,
}
impl Section64 {
pub fn new(section_name: String, shdr: Shdr64) -> Self {
Self {
name: section_name,
header: shdr,
bytes: Vec::new(),
}
}
pub fn new_null_section() -> Self {
Self::new(String::new(), Default::default())
}
}
#[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::SHTYPE {
section_type::SHTYPE::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 set_type(&mut self, ty: section_type::SHTYPE) {
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 to_le_bytes(&self) -> Vec<u8> {
let mut bytes: Vec<u8> = Vec::new();
for byte in self.sh_name.to_le_bytes().to_vec() {
bytes.push(byte);
}
for byte in self.sh_type.to_le_bytes().to_vec() {
bytes.push(byte);
}
for byte in self.sh_flags.to_le_bytes().to_vec() {
bytes.push(byte);
}
for byte in self.sh_addr.to_le_bytes().to_vec() {
bytes.push(byte);
}
for byte in self.sh_offset.to_le_bytes().to_vec() {
bytes.push(byte);
}
for byte in self.sh_size.to_le_bytes().to_vec() {
bytes.push(byte);
}
for byte in self.sh_link.to_le_bytes().to_vec() {
bytes.push(byte);
}
for byte in self.sh_info.to_le_bytes().to_vec() {
bytes.push(byte);
}
for byte in self.sh_addralign.to_le_bytes().to_vec() {
bytes.push(byte);
}
for byte in self.sh_entsize.to_le_bytes().to_vec() {
bytes.push(byte);
}
bytes
}
}