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
use super::{Bit, ELF};
use bitflags::bitflags;
use core::convert::TryInto;
pub struct ShdrIterator<'a, T>
where
T: super::Peeker,
{
pub(super) base: usize,
pub(super) size: u16,
pub(super) cursor: u16,
pub(super) elf: &'a ELF<T>,
}
#[repr(u32)]
#[allow(dead_code)]
#[derive(Clone, Copy, PartialEq, Eq, Debug)]
pub enum SType {
Null = 0x0,
ProgBits = 0x1,
Symtab = 0x2,
Strtab = 0x3,
Rela = 0x4,
Hash = 0x5,
Dynamic = 0x6,
Note = 0x7,
Nobits = 0x8,
Rel = 0x9,
Shlib = 0xa,
Dynsym = 0xb,
InitArray = 0xc,
FiniArray = 0xf,
PreinitArray = 0x10,
Group = 0x11,
SymtabShndx = 0x12,
Num = 0x13,
Loos = 0x60000000,
}
bitflags! {
#[allow(dead_code)]
pub struct SFlags: u32 {
const WRITE = 0x1;
const ALLOC = 0x2;
const EXECINSTR = 0x4;
const MERGE = 0x10;
const STRINGS = 0x20;
const INFOLINK = 0x40;
const LINKORDER = 0x80;
const OSNONCONFORMING = 0x100;
const GROUP = 0x200;
const TLS = 0x400;
const MASKOS = 0x0ff00000;
const MASKPROC = 0xf0000000;
const ORDERED = 0x4000000;
const EXCLUDE = 0x8000000;
}
}
#[repr(C)]
#[derive(Clone, Copy, Debug)]
pub struct Shdr32 {
name: u32,
sh_type: SType,
sh_flags: SFlags,
sh_addr: u32,
sh_offset: u32,
sh_size: u32,
sh_link: u32,
sh_info: u32,
sh_addralign: u32,
sh_ent_size: u32,
}
#[repr(C)]
#[derive(Clone, Copy, Debug)]
pub struct Shdr64 {
name: u32,
sh_type: SType,
sh_flags: SFlags,
sh_addr: u64,
sh_offset: u64,
sh_size: u64,
sh_link: u32,
sh_info: u32,
sh_addralign: u64,
sh_ent_size: u64,
}
pub enum Shdr {
Shdr32(Shdr32),
Shdr64(Shdr64),
}
#[allow(dead_code)]
impl Shdr {
#[inline]
fn name(&self) -> u32 {
match self {
Shdr::Shdr32(s) => s.name,
Shdr::Shdr64(s) => s.name,
}
}
#[inline]
fn type_(&self) -> SType {
match self {
Shdr::Shdr32(s) => s.sh_type,
Shdr::Shdr64(s) => s.sh_type,
}
}
#[inline]
fn flags(&self) -> SFlags {
match self {
Shdr::Shdr32(s) => s.sh_flags,
Shdr::Shdr64(s) => s.sh_flags,
}
}
#[inline]
fn addr(&self) -> usize {
match self {
Shdr::Shdr32(s) => s.sh_addr as usize,
Shdr::Shdr64(s) => s.sh_addr.try_into().unwrap(),
}
}
#[inline]
fn offset(&self) -> usize {
match self {
Shdr::Shdr32(s) => s.sh_offset as usize,
Shdr::Shdr64(s) => s.sh_offset.try_into().unwrap(),
}
}
#[inline]
fn size(&self) -> usize {
match self {
Shdr::Shdr32(s) => s.sh_size as usize,
Shdr::Shdr64(s) => s.sh_size.try_into().unwrap(),
}
}
#[inline]
fn link(&self) -> u32 {
match self {
Shdr::Shdr32(s) => s.sh_link,
Shdr::Shdr64(s) => s.sh_link,
}
}
#[inline]
fn info(&self) -> u32 {
match self {
Shdr::Shdr32(s) => s.sh_info,
Shdr::Shdr64(s) => s.sh_info,
}
}
#[inline]
fn addralign(&self) -> usize {
match self {
Shdr::Shdr32(s) => s.sh_addralign as usize,
Shdr::Shdr64(s) => s.sh_addralign.try_into().unwrap(),
}
}
#[inline]
fn ent_size(&self) -> usize {
match self {
Shdr::Shdr32(s) => s.sh_ent_size as usize,
Shdr::Shdr64(s) => s.sh_ent_size.try_into().unwrap(),
}
}
}
union Reader32 {
shdr: Shdr32,
_raw: [u8; 0x20],
}
union Reader64 {
shdr: Shdr64,
_raw: [u8; 0x40],
}
impl<'a, T> core::iter::Iterator for ShdrIterator<'a, T>
where
T: super::Peeker,
{
type Item = Result<Shdr, ()>;
fn next(&mut self) -> Option<Self::Item> {
let bit = self.elf.bit();
let ELF { peeker, .. } = self.elf;
if self.size > self.cursor {
unsafe {
let shdr = match bit {
Bit::Bit32 => {
let mut inner = Reader32 { _raw: [0; 0x20] };
peeker
.peek_bytes(self.base + self.cursor as usize * 0x20, &mut inner._raw)
.map(|_| Shdr::Shdr32(inner.shdr))
.map_err(|_| ())
}
Bit::Bit64 => {
let mut inner = Reader64 { _raw: [0; 0x40] };
peeker
.peek_bytes(self.base + self.cursor as usize * 0x40, &mut inner._raw)
.map(|_| Shdr::Shdr64(inner.shdr))
.map_err(|_| ())
}
};
self.cursor += 1;
Some(shdr)
}
} else {
None
}
}
}