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
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
extern crate ar;
extern crate bit_vec;
extern crate fnv;
extern crate core;
extern crate rayon;

use {types, Header, Elf, Error, symbol, filetype, relocation, section};
use std;
use std::io::{Read, Seek, Cursor};
use std::hash::{Hash,Hasher};
use std::fs::{File};
use std::collections::HashMap;
use std::cell::RefCell;
use self::rayon::prelude::*;
use self::fnv::FnvHasher;
use self::bit_vec::BitVec;
use self::ar::Archive;
use std::collections::hash_map::DefaultHasher;

pub trait ReadSeekSend : Read + Seek + Send {}
impl<T> ReadSeekSend for T where T: Read + Seek + Send{}

pub enum State {
    Error{
        name:    String,
        error:   Error
    },
    Path{
        name:    String
    },
    Archive{
        name:    String,
        archive: Archive<File>,
    },
    Elf{
        hash:    String,
        name:    String,
        elf:     Elf,
        read:    RefCell<Box<ReadSeekSend>>,
        bloom:   BloomFilter,
        symbols: Vec<symbol::Symbol>,
    },
    Object{
        hash:     String,
        name:     String,
        symbols:  Vec<symbol::Symbol>,
        header:   Header,
        sections: Vec<(usize, section::Section, Vec<relocation::Relocation>)>,
    },
}

pub trait Loader {
    fn load_all<E>(self, e: &E) ->  Vec<State>
        where E: Fn(Error, String) -> Vec<State> + Sync;
    fn load_if<E>(self, needles: &Vec<&[u8]>, e: &E) ->  (Vec<State>,Vec<State>)
        where E: Fn(Error, String) -> Vec<State> + Sync;
}

impl Loader for Vec<State> {
    fn load_all<E>(self, e: &E) ->  Vec<State>
        where E: Fn(Error, String) -> Vec<State> + Sync
    {
        self.into_par_iter()
            .flat_map(|l| l.load(e))
            .flat_map(|l| l.load(e))
            .flat_map(|l| l.load(e))
            .flat_map(|l| l.load(e))
            .collect()
    }
    fn load_if<E>(self, needles: &Vec<&[u8]>, e: &E) ->  (Vec<State>,Vec<State>)
        where E: Fn(Error, String) -> Vec<State> + Sync
    {
        self.into_par_iter().flat_map(|l| l.load_if(needles, e))
            .partition(|o| if let &State::Object{..} = o {false} else {true})
    }
}


impl State {
    pub fn load_if<E> (mut self, needles: &Vec<&[u8]>, e: &E) -> Vec<State>
        where E: Fn(Error, String) -> Vec<State> + Sync
    {
        if needles.iter().map(|needle|self.contains(needle, BloomFilter::hash(needle))).any(|e|e==true) {
            self.load(e).into_par_iter().flat_map(|s|s.load_if(needles, e)).collect()
        } else {
            vec![self]
        }
    }

    pub fn contains(&mut self, needle: &[u8], needle_hash: [u64;2]) -> bool {
        match self {
            &mut State::Error{..}   => true,
            &mut State::Path {..}   => true,
            &mut State::Archive{ref mut archive, ..}  => {
                let symbols = match archive.symbols() {
                    Ok(v) =>  v,
                    Err(_) => return true,
                };
                for symbol in symbols {
                    if symbol == needle {
                        return true;
                    }
                }
                return false;
            },
            &mut State::Elf{ref bloom, ref symbols, ..} => {
                if bloom.contains(&needle_hash) {
                    for sym in symbols.iter() {
                        match sym.bind {
                            types::SymbolBind::GLOBAL | types::SymbolBind::WEAK => {
                                match sym.shndx {
                                    symbol::SymbolSectionIndex::Undefined |
                                        symbol::SymbolSectionIndex::Absolute  => {},
                                    _ => {
                                        if sym.name == needle {
                                            return true;
                                        }
                                    },
                                }
                            },
                            _ => {},
                        }
                    }
                }
                return false;
            },
            &mut State::Object{..} => false,
        }
    }

    pub fn load<E>(self, e: &E) -> Vec<State> where E: Fn(Error, String) -> Vec<State> {
        match self {
            State::Error{name,error} => e(error,name),
            State::Path{name} => {
                let mut f = match File::open(&name) {
                    Err(e) => return vec![State::Error{
                        error: Error::from(e),
                        name:  name
                    }],
                    Ok(f) => f,
                };
                match filetype::filetype(&mut f) {
                    Ok(filetype::FileType::Unknown) => {
                        return vec![State::Error{
                            error:  Error::InvalidMagic,
                            name:   name,
                        }];
                    },
                    Ok(filetype::FileType::Elf) => {
                        vec![match State::make_object(name.clone(), RefCell::new(Box::new(f))) {
                            Err(e) => State::Error{
                                error: e,
                                name:  name,
                            },
                            Ok(v) => v,
                        }]
                    },
                    Ok(filetype::FileType::Archive) => {
                        vec![State::Archive{
                            name:    name,
                            archive: Archive::new(f),
                        }]
                    },
                    Err(e) => vec![
                        State::Error{
                            error: Error::from(e),
                            name:  name,
                        }
                    ],
                }
            },
            State::Archive{name, mut archive}  => {
                let mut r = Vec::new();
                while let Some(entry) = archive.next_entry() {
                    let mut name = name.clone();
                    match &entry {
                        &Ok(ref entry) => name +=
                            &(String::from("::") +
                              &String::from_utf8_lossy(&entry.header().identifier())),
                        _ => {},
                    };

                    r.push(match State::make_object_ar(name.clone(), entry) {
                        Err(e) => State::Error{
                            error: e,
                            name:  name,
                        },
                        Ok(v) => v,
                    })
                }
                r
            },
            State::Elf{name, hash, mut elf, read, symbols, ..} => {
                if let Err(e) = elf.load_all(&mut *read.borrow_mut()) {
                    return vec![State::Error{
                        name:   name,
                        error:  e,
                    }]
                }

                let mut relocs : HashMap<usize, Vec<relocation::Relocation>> = HashMap::new();
                for sec in elf.sections.iter_mut() {
                    if sec.header.shtype == types::SectionType::RELA {
                        relocs.insert(sec.header.info as usize,
                                    std::mem::replace(sec, section::Section::default())
                                    .content.into_relocations().unwrap()
                                    );
                    }
                }

                let mut sections = Vec::new();
                for (i, sec) in elf.sections.into_iter().enumerate() {
                    match sec.header.shtype {
                        types::SectionType::NULL | types::SectionType::STRTAB => {},
                        _ => {
                            sections.push((i, sec, relocs.remove(&i).unwrap_or_else(||Vec::new())));
                        },
                    }
                }

                vec![State::Object{
                    hash:       hash,
                    name:       name,
                    symbols:    symbols,
                    header:     elf.header,
                    sections:   sections,
                }]
            },
            any => vec![any],
        }
    }

    fn make_object(name: String, io: RefCell<Box<ReadSeekSend>>) -> Result<State, Error> {

        let mut elf = Elf::from_reader(&mut *io.borrow_mut())?;

        let mut hasher = DefaultHasher::new();
        let mut num_symbols = 0;

        for i in 0..elf.sections.len() {
            match elf.sections[i].header.shtype {
                types::SectionType::SYMTAB |
                    types::SectionType::DYNSYM => {
                        elf.load(i, &mut *io.borrow_mut())?;
                        let symbols = elf.sections[i].content.as_symbols().unwrap();
                        for sym in symbols {
                            hasher.write(&sym.name);
                        }
                        num_symbols += symbols.len();
                    },
                _ => {}
            }
        }

        if num_symbols == 0 {
            return Err(Error::NoSymbolsInObject);
        }

        let mut bloom = BloomFilter::new(num_symbols);

        let mut symbols = None;
        for i in 0..elf.sections.len() {
            if (elf.sections[i].header.shtype == types::SectionType::SYMTAB &&
               elf.header.etype == types::ElfType::REL) ||
               (elf.sections[i].header.shtype == types::SectionType::DYNSYM &&
               elf.header.etype == types::ElfType::DYN) {

               if let Some(_) = symbols {
                   return Err(Error::MultipleSymbolSections);
               }

               let syms = std::mem::replace(&mut elf.sections[i], section::Section::default())
                   .content.into_symbols().unwrap();

               for sym in syms.iter() {
                   match sym.bind {
                       types::SymbolBind::GLOBAL | types::SymbolBind::WEAK => {
                           match sym.shndx {
                               symbol::SymbolSectionIndex::Undefined |
                                   symbol::SymbolSectionIndex::Absolute  => {},
                               _ => {
                                   bloom.insert(&BloomFilter::hash(&sym.name));
                               },
                           }
                       },
                       _ => {},

                   }
               }
               symbols = Some(syms);
            }
        }

        if let None = symbols {
            return Err(Error::MissingSymtabSection);
        }

        Ok(State::Elf{
            hash:    format!("{}>>{:x}!", name.split("::").last().unwrap(), hasher.finish()),
            name:    name,
            elf:     elf,
            read:    io,
            bloom:   bloom,
            symbols: symbols.unwrap(),
        })
    }

    fn make_object_ar(name: String, entry: std::io::Result<ar::Entry<File>>) -> Result<State, Error> {
        let mut entry = entry?;
        let mut buf = Vec::with_capacity(entry.header().size() as usize);
        entry.read_to_end(&mut buf)?;
        let io = Cursor::new(buf);

        State::make_object(name,
        RefCell::new(Box::new(io)))
    }

}



pub struct BloomFilter {
    bits: BitVec,
}

impl BloomFilter {

    fn new(num_items: usize) -> BloomFilter {
        BloomFilter {
            bits: BitVec::from_elem(Self::needed_bits(0.001, num_items as u32), false),
        }
    }

    fn hash(n:&[u8]) -> [u64;2] {
        let mut a1 = FnvHasher::with_key(0xcbf29ce484222325);
        let mut a2 = FnvHasher::with_key(0x84222325b444f000);
        n.hash(&mut a1);
        n.hash(&mut a2);
        [a1.finish(),a2.finish()]
    }

    fn needed_bits(false_pos_rate: f32, num_items: u32) -> usize {
        let ln22 = core::f32::consts::LN_2 * core::f32::consts::LN_2;
        (num_items as f32 * ((1.0/false_pos_rate).ln() / ln22)).round() as usize
    }

    fn insert(&mut self, nh: &[u64;2]) {
        let len = self.bits.len();
        self.bits.set(nh[0] as usize % len, true);
        self.bits.set(nh[1] as usize % len, true);
    }

    fn contains(&self, nh: &[u64;2]) -> bool {
        self.bits[nh[0] as usize % self.bits.len()] &&
        self.bits[nh[1] as usize % self.bits.len()]
    }
}