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
291
292
293
294
295
296
297
298
299

use super::super::nom::{
    le_u64, be_u64,
    le_u32, be_u32,
    le_u16, be_u16,
    le_u8,  be_u8,
    IResult
};

use super::super::{
    Abi, ElfAbi, Endian, ElfEndian, Class, ElfClass, ElfMagicNumber,
    Arch, ElfArch, FileType, ElfFileType, ElfHeader, BufferBorrow,
    VarSize, Fault, ElfHeaderBase, Section
};

use super::{
    SymBinding, SymbolBinding,
    SymType, SymbolType,
    SymProt, SymbolProtection,
    find_null,
    SymEntry,
};

use std::borrow::Cow;
use std::sync::Arc;
use std::mem::size_of;

pub struct SymEntry32<'a> {
    header: ElfHeaderBase<'a>,
    name: Option<Cow<'a,str>>,
    st_bind: SymBinding,
    st_type: SymType,
    st_prot: SymProt,
    st_size: u32,
    st_value: u32,
    st_shndx: u16,
}
impl<'a> SymEntry32<'a> {
    fn new<S>(data: &PreSymEntry32, strtab: &S)
        -> Result<SymEntry32<'a>, Fault>
        where S: Section<'a>+'a+?Sized
    {
        let name = data.st_name as usize;
        let name = get_name(strtab, name)?;
        let st_prot = SymProt::from(data.st_other);
        let bind = data.st_info.clone() >> 4;
        let st_bind = SymBinding::from(bind);
        let kind = data.st_info.clone() & 0x0Fu8;
        let st_type = SymType::from(kind);
        let header = strtab.duplicate();
        let st_size = data.st_size.clone();
        let st_value = data.st_value.clone();
        let st_shndx = data.st_shndx.clone();
        Ok(SymEntry32 {
            header, name, st_bind, st_type, st_prot, st_size, st_value, st_shndx
        })
    }
}

fn get_name<'a, S>(strtab: &S, index: usize)
    -> Result<Option<Cow<'a,str>>,Fault> 
    where S: Section<'a>+'a+?Sized
{

    if index == 0 {
        return Ok(None);
    }
    let strtab = match strtab.get_data() {
        Option::Some(val) => val,
        Option::None => return Err(Fault::StrTabNotInElf),
    };
    if index < strtab.len() {
        Ok(find_null(&strtab[index..]))
    } else {
        Err(Fault::NameNotInStrTab)
    }
}

//Just parse without metadata
#[derive(Clone)]
#[repr(packed)]
struct PreSymEntry32 {
    st_name: u32,
    st_value: u32,
    st_size: u32,
    st_info: u8,
    st_other: u8,
    st_shndx: u16,
}
impl PreSymEntry32 {

    /// The actual parse logic
    #[inline(always)]
    pub fn build<'a, E: Endian+?Sized+'a>(temp: &[u8], header: &E)
        -> Result<PreSymEntry32,Fault>
    {
        named!(parse_pre_sym_ent_32_le<PreSymEntry32>,do_parse!(
            st_name: le_u32 >>
            st_value: le_u32 >>
            st_size: le_u32 >>
            st_info: le_u8 >>
            st_other: le_u8 >>
            st_shndx: le_u16 >>
            (PreSymEntry32 {
                st_name, st_value, st_size, st_info, st_other, st_shndx
            })
        ));
        named!(parse_pre_sym_ent_32_be<PreSymEntry32>,do_parse!(
            st_name: be_u32 >>
            st_value: be_u32 >>
            st_size: be_u32 >>
            st_info: be_u8 >>
            st_other: be_u8 >>
            st_shndx: be_u16 >>
            (PreSymEntry32 {
                st_name, st_value, st_size, st_info, st_other, st_shndx
            })
        ));
        let ent = match header.get_endian() {
            ElfEndian::Little => parse_pre_sym_ent_32_le(temp),
            ElfEndian::Big => parse_pre_sym_ent_32_be(temp),
        };
        match ent {
            IResult::Incomplete(_) => Err(Fault::TooSmol),
            IResult::Error(_) => Err(Fault::Complex),
            IResult::Done(_,s) => Ok(s)
        }
    }
}

/// Parse a 32bit symbol table
pub fn parse_symtable32<'a,S>(symtab: &S)
    -> Result<Arc<[Box<SymEntry<'a>+'a>]>,Fault>
    where S: Section<'a>+'a+?Sized
{
    if symtab.is_64bits() {
        return Err(Fault::Bits32ParserFailed);
    }
    let data = match symtab.get_data() {
        Option::Some(val) => val,
        Option::None => return Err(Fault::SymTabNotInElf),
    };
    let sym_entry_size = size_of::<PreSymEntry32>();
    let entries = if data.len() != 0 && data.len() >= sym_entry_size {
        let entry_num = data.len() / sym_entry_size;
        let mut entries: Vec<Box<SymEntry<'a>+'a>> = Vec::with_capacity(entry_num);
        for i in 0..entry_num {
            let offset = i * sym_entry_size;
            if offset < data.len() {
                let pre = PreSymEntry32::build(&data[offset..], symtab)?;
                let arg = SymEntry32::new(&pre,symtab)?;
                entries.push(Box::new(arg));
            } else {
                return Err(Fault::SymTabTooSmol);
            }
        }
        entries
    } else {
        Vec::with_capacity(0)
    };
    Ok(Arc::from(entries))
}

impl<'a> SymbolBinding for SymEntry32<'a> {
    #[inline(always)]
    fn get_sym_binding(&self) -> SymBinding {
        self.st_bind.clone()
    }
}
impl<'a> SymbolType for SymEntry32<'a> {
    #[inline(always)]
    fn get_sym_type(&self) -> SymType {
        self.st_type.clone()
    }
}
impl<'a> SymbolProtection for SymEntry32<'a> {
    #[inline(always)]
    fn get_sym_protection(&self) -> SymProt {
        self.st_prot.clone()
    }
}
impl<'a> Abi for SymEntry32<'a> {
    #[inline(always)] 
    fn get_abi(&self) -> ElfAbi {
        self.header.get_abi()
    }
}
impl<'a> Endian for SymEntry32<'a> {
    #[inline(always)]
    fn get_endian(&self) -> ElfEndian {
        self.header.get_endian()
    }
}
impl<'a> FileType for SymEntry32<'a> {
    #[inline(always)]
    fn get_file_type(&self) -> ElfFileType {
        self.header.get_file_type()
    }
}
impl<'a> ElfMagicNumber for SymEntry32<'a> {
    #[inline(always)]
    fn get_abi_version(&self) -> u8 {
        self.header.get_abi_version()
    }
}
impl<'a> Arch for SymEntry32<'a> {
    #[inline(always)]
    fn get_arch(&self) -> ElfArch {
        self.header.get_arch()
    }
}
impl<'a> Class for SymEntry32<'a> {
    #[inline(always)]
    fn get_class(&self) -> ElfClass {
        self.header.get_class()
    }
}
impl<'a> BufferBorrow<'a> for SymEntry32<'a> {
    #[inline(always)]
    fn get_buffer(&self) -> &'a [u8] {
        self.header.get_buffer()
    }
}
impl<'a> ElfHeader<'a> for SymEntry32<'a> {
    #[inline(always)]
    fn duplicate(&self) -> ElfHeaderBase<'a> {
        self.header.clone()
    }
    #[inline(always)]
    fn e_version(&self) -> usize {
        self.header.e_version()
    }
    #[inline(always)]
    fn e_entry(&self) -> VarSize {
        self.header.e_entry()
    }
    #[inline(always)]
    fn e_phoff(&self) -> usize {
        self.header.e_phoff()
    }
    #[inline(always)]
    fn e_shoff(&self) -> usize {
        self.header.e_shoff()
    }
    #[inline(always)]
    fn e_flags(&self) -> VarSize {
        self.header.e_flags()
    }
    #[inline(always)]
    fn e_ehsize(&self) -> usize {
        self.header.e_ehsize()
    }
    #[inline(always)]
    fn e_phentsize(&self) -> usize {
        self.header.e_phentsize()
    }
    #[inline(always)]
    fn e_phnum(&self) -> usize {
        self.header.e_phnum()
    }
    #[inline(always)]
    fn e_shentsize(&self) -> usize {
        self.header.e_shentsize()
    }
    #[inline(always)]
    fn e_shnum(&self) -> usize {
        self.header.e_shnum()
    }
    #[inline(always)]
    fn e_shstrndx(&self) -> usize {
        self.header.e_shstrndx()
    }
}
impl<'a> SymEntry<'a> for SymEntry32<'a> {

    #[inline(always)]
    fn get_name<'b>(&'b self) -> Option<&'b str> {
        match &self.name {
            &Option::None => None,
            &Option::Some(ref x) => Some(x.as_ref())
        }
    }

    #[inline(always)]
    fn st_value(&self) -> VarSize {
        VarSize::from(self.st_value.clone())
    }

    #[inline(always)]
    fn st_size(&self) -> VarSize {
        VarSize::from(self.st_size.clone())
    }

    #[inline(always)]
    fn st_shndx(&self) -> u16 {
        self.st_shndx.clone()
    }
}