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;

/// Section header iterator created by [`ELF::shdrs`] method.
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 {
    /// Unused.
    Null = 0x0,
    /// Program data.
    ProgBits = 0x1,
    /// Symbol table.
    Symtab = 0x2,
    /// String table.
    Strtab = 0x3,
    /// Relocation entries with addends.
    Rela = 0x4,
    /// Symbol hash table.
    Hash = 0x5,
    /// Dynamic linking information.
    Dynamic = 0x6,
    /// Notes.
    Note = 0x7,
    /// Program space with no data (bss).
    Nobits = 0x8,
    /// Relocation entries, no addends.
    Rel = 0x9,
    /// Reserved.
    Shlib = 0xa,
    /// Dynamic linker symbol table.
    Dynsym = 0xb,
    /// Array of constructors.
    InitArray = 0xc,
    /// Array of destructors.
    FiniArray = 0xf,
    /// Array of pre-constructors
    PreinitArray = 0x10,
    /// Section group.
    Group = 0x11,
    /// Extended section indices.
    SymtabShndx = 0x12,
    /// Number of defined types.
    Num = 0x13,
    /// Start OS-specific.
    Loos = 0x60000000,
}

bitflags! {
    #[allow(dead_code)]
    pub struct SFlags: u32 {
    /// Writable.
    const WRITE = 0x1;
    /// Occupies memory during execution.
    const ALLOC = 0x2;
    /// Executable.
    const EXECINSTR = 0x4;
    /// Might be merged.
    const MERGE = 0x10;
    /// Contains null-terminated strings.
    const STRINGS = 0x20;
    /// 'sh_info' contains SHT index
    const INFOLINK = 0x40;
    /// Preserve order after combining.
    const LINKORDER = 0x80;
    /// Non-standard OS specific handling required.
    const OSNONCONFORMING = 0x100;
    /// Section is member of a group.
    const GROUP = 0x200;
    /// Section hold thread-local data.
    const TLS = 0x400;
    /// OS-specific.
    const MASKOS = 0x0ff00000;
    /// Processor-specific.
    const MASKPROC = 0xf0000000;
    /// Special ordering requirement (Solaris).
    const ORDERED = 0x4000000;
    /// Section is excluded unless referenced or allocated (Solaris).
    const EXCLUDE = 0x8000000;
    }
}

#[repr(C)]
#[derive(Clone, Copy, Debug)]
/// Section header for 32bit ELF.
pub struct Shdr32 {
    /// An offset to a string in the .shstrtab section that represents the name
    /// of this section.
    name: u32,
    /// Identifies the type of this header.
    sh_type: SType,
    /// Identifies the attributes of the section.
    sh_flags: SFlags,
    /// Virtual address of the section in memory, for sections that are loaded.
    sh_addr: u32,
    /// Offset of the section in the file image.
    sh_offset: u32,
    /// Size in bytes of the section in the file image. May be 0.
    sh_size: u32,
    /// Contains the section index of an associated section. This field is used
    /// for several purposes, depending on the type of section.
    sh_link: u32,
    /// Contains extra information about the section. This field is used for
    /// several purposes, depending on the type of section.
    sh_info: u32,
    /// Contains the required alignment of the section. This field must be a
    /// power of two.
    sh_addralign: u32,
    /// Contains the size, in bytes, of each entry, for sections that contain
    /// fixed-size entries. Otherwise, this field contains zero.
    sh_ent_size: u32,
}

#[repr(C)]
#[derive(Clone, Copy, Debug)]
/// Section header for 64bit ELF.
pub struct Shdr64 {
    /// An offset to a string in the .shstrtab section that represents the name
    /// of this section.
    name: u32,
    /// Identifies the type of this header.
    sh_type: SType,
    /// Identifies the attributes of the section.
    sh_flags: SFlags,
    /// Virtual address of the section in memory, for sections that are loaded.
    sh_addr: u64,
    /// Offset of the section in the file image.
    sh_offset: u64,
    /// Size in bytes of the section in the file image. May be 0.
    sh_size: u64,
    /// Contains the section index of an associated section. This field is used
    /// for several purposes, depending on the type of section.
    sh_link: u32,
    /// Contains extra information about the section. This field is used for
    /// several purposes, depending on the type of section.
    sh_info: u32,
    /// Contains the required alignment of the section. This field must be a
    /// power of two.
    sh_addralign: u64,
    /// Contains the size, in bytes, of each entry, for sections that contain
    /// fixed-size entries. Otherwise, this field contains zero.
    sh_ent_size: u64,
}

/// Section Header.
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
        }
    }
}