elf_loader 0.12.0

A pure-rust library for loading all ELF format files from both memory and files.
Documentation
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
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
use crate::{
    arch::ElfSymbol,
    dynamic::{ElfDynamic, ElfDynamicHashTab},
};
use core::ffi::CStr;

#[repr(C)]
struct ElfGnuHeader {
    nbucket: u32,
    symbias: u32,
    nbloom: u32,
    nshift: u32,
}

pub(crate) struct ElfGnuHash {
    header: ElfGnuHeader,
    blooms: *const usize,
    buckets: *const u32,
    chains: *const u32,
}

trait ElfHashTable {
    fn hash(name: &[u8]) -> u32;
    fn count_syms(&self) -> usize;
}

impl ElfGnuHash {
    #[inline]
    pub(crate) fn parse(ptr: *const u8) -> ElfGnuHash {
        const HEADER_SIZE: usize = size_of::<ElfGnuHeader>();
        let mut bytes = [0u8; HEADER_SIZE];
        bytes.copy_from_slice(unsafe { core::slice::from_raw_parts(ptr, HEADER_SIZE) });
        let header: ElfGnuHeader = unsafe { core::mem::transmute(bytes) };
        let bloom_size = header.nbloom as usize * size_of::<usize>();
        let bucket_size = header.nbucket as usize * size_of::<u32>();

        let blooms = unsafe { ptr.add(HEADER_SIZE) };
        let buckets = unsafe { blooms.add(bloom_size) };
        let chains = unsafe { buckets.add(bucket_size) };
        ElfGnuHash {
            header,
            blooms: blooms.cast(),
            buckets: buckets.cast(),
            chains: chains.cast(),
        }
    }
}

impl ElfHashTable for ElfGnuHash {
    #[inline]
    fn hash(name: &[u8]) -> u32 {
        let mut hash = 5381u32;
        for byte in name {
            hash = hash.wrapping_mul(33).wrapping_add(u32::from(*byte));
        }
        hash
    }

    fn count_syms(&self) -> usize {
        let mut nsym = 0;
        for i in 0..self.header.nbucket as usize {
            nsym = nsym.max(unsafe { self.buckets.add(i).read() as usize });
        }
        if nsym > 0 {
            unsafe {
                let mut hashval = self.chains.add(nsym - self.header.symbias as usize);
                while hashval.read() & 1 == 0 {
                    nsym += 1;
                    hashval = hashval.add(1);
                }
            }
        }
        nsym + 1
    }
}

#[repr(C)]
struct ElfHashHeader {
    nbucket: u32,
    nchain: u32,
}

pub(crate) struct ElfHash {
    header: ElfHashHeader,
    buckets: *const u32,
    chains: *const u32,
}

impl ElfHash {
    #[inline]
    pub(crate) fn parse(ptr: *const u8) -> ElfHash {
        const HEADER_SIZE: usize = size_of::<ElfHashHeader>();
        let mut bytes = [0u8; HEADER_SIZE];
        bytes.copy_from_slice(unsafe { core::slice::from_raw_parts(ptr, HEADER_SIZE) });
        let header: ElfHashHeader = unsafe { core::mem::transmute(bytes) };
        let bucket_size = header.nbucket as usize * size_of::<u32>();

        let buckets = unsafe { ptr.add(HEADER_SIZE) };
        let chains = unsafe { buckets.add(bucket_size) };
        ElfHash {
            header,
            buckets: buckets.cast(),
            chains: chains.cast(),
        }
    }
}

impl ElfHashTable for ElfHash {
    #[inline]
    fn hash(name: &[u8]) -> u32 {
        let mut hash = 0u32;
        #[allow(unused_assignments)]
        let mut g = 0u32;
        for byte in name {
            hash = (hash << 4) + u32::from(*byte);
            g = hash & 0xf0000000;
            if g != 0 {
                hash ^= g >> 24;
            }
            hash &= !g;
        }
        hash
    }

    #[inline]
    fn count_syms(&self) -> usize {
        self.header.nchain as usize
    }
}

pub(crate) struct ElfStringTable {
    data: *const u8,
}

impl ElfStringTable {
    const fn new(data: *const u8) -> Self {
        ElfStringTable { data }
    }

    #[inline]
    pub(crate) fn get_cstr(&self, offset: usize) -> &'static CStr {
        unsafe {
            let start = self.data.add(offset).cast();
            CStr::from_ptr(start)
        }
    }

    #[inline]
    fn convert_cstr(s: &CStr) -> &str {
        unsafe { core::str::from_utf8_unchecked(s.to_bytes()) }
    }

    #[inline]
    pub(crate) fn get_str(&self, offset: usize) -> &'static str {
        Self::convert_cstr(self.get_cstr(offset))
    }
}

pub(crate) enum SymbolTableHashTable {
    /// .gnu.hash
    Gnu(ElfGnuHash),
    /// .hash
    Elf(ElfHash),
}

impl SymbolTableHashTable {
    #[inline]
    #[allow(dead_code)]
    fn hash(&self, name: &[u8]) -> u32 {
        match &self {
            SymbolTableHashTable::Gnu(_) => ElfGnuHash::hash(name),
            SymbolTableHashTable::Elf(_) => ElfHash::hash(name),
        }
    }

    #[inline]
    #[allow(dead_code)]
    fn count_syms(&self) -> usize {
        match &self {
            SymbolTableHashTable::Gnu(hashtab) => hashtab.count_syms(),
            SymbolTableHashTable::Elf(hashtab) => hashtab.count_syms(),
        }
    }
}

/// Symbol table of elf file.
pub struct SymbolTable {
    /// .gnu.hash / .hash
    pub(crate) hashtab: SymbolTableHashTable,
    /// .dynsym
    symtab: *const ElfSymbol,
    /// .dynstr
    strtab: ElfStringTable,
    #[cfg(feature = "version")]
    /// .gnu.version
    pub(crate) version: Option<super::version::ELFVersion>,
}

/// Symbol specific information, including symbol name and version name.
pub struct SymbolInfo<'symtab> {
    name: &'symtab str,
    cname: Option<&'symtab CStr>,
    #[cfg(feature = "version")]
    version: Option<super::version::SymbolVersion<'symtab>>,
}

pub struct PreCompute {
    gnuhash: u32,
    fofs: usize,
    fmask: usize,
    hash: Option<u32>,
}

impl<'symtab> SymbolInfo<'symtab> {
    #[allow(unused_variables)]
    pub(crate) fn from_str(
        name: &'symtab str,
        version: Option<&'symtab str>,
    ) -> Self {
        SymbolInfo {
            name,
            cname: None,
            #[cfg(feature = "version")]
            version: version.map(crate::version::SymbolVersion::new),
        }
    }

    /// Gets the name of the symbol.
    #[inline]
    pub fn name(&self) -> &str {
        self.name
    }

    /// Gets the C-style name of the symbol.
    #[inline]
    pub fn cname(&self) -> Option<&CStr> {
        self.cname
    }

    #[inline]
    pub fn precompute(&self) -> PreCompute {
        let gnuhash = ElfGnuHash::hash(self.name.as_bytes());
        PreCompute {
            gnuhash,
            fofs: gnuhash as usize / usize::BITS as usize,
            fmask: 1 << (gnuhash % (8 * size_of::<usize>() as u32)),
            hash: None,
        }
    }
}

impl SymbolTable {
    pub(crate) fn new(dynamic: &ElfDynamic) -> Self {
        let hashtab = match dynamic.hashtab {
            ElfDynamicHashTab::Gnu(off) => {
                SymbolTableHashTable::Gnu(ElfGnuHash::parse(off as *const u8))
            }
            ElfDynamicHashTab::Elf(off) => {
                SymbolTableHashTable::Elf(ElfHash::parse(off as *const u8))
            }
        };
        let symtab = dynamic.symtab as *const ElfSymbol;
        let strtab = ElfStringTable::new(dynamic.strtab as *const u8);
        #[cfg(feature = "version")]
        let version = super::version::ELFVersion::new(
            dynamic.version_idx,
            dynamic.verneed,
            dynamic.verdef,
            &strtab,
        );
        SymbolTable {
            hashtab,
            symtab,
            strtab,
            #[cfg(feature = "version")]
            version,
        }
    }

    pub(crate) fn strtab(&self) -> &ElfStringTable {
        &self.strtab
    }

    /// Use the symbol specific information to get the symbol in the symbol table
    pub fn lookup(&self, symbol: &SymbolInfo, precompute: &mut PreCompute) -> Option<&ElfSymbol> {
        match &self.hashtab {
            SymbolTableHashTable::Gnu(hashtab) => {
                let hash = precompute.gnuhash;
                let fofs = precompute.fofs;
                let fmask = precompute.fmask;
                let bloom_idx = fofs & (hashtab.header.nbloom - 1) as usize;
                let filter = unsafe { hashtab.blooms.add(bloom_idx).read() };
                if filter & fmask == 0 {
                    return None;
                }
                let filter2 =
                    filter >> ((hash >> hashtab.header.nshift) as usize % usize::BITS as usize);
                if filter2 & 1 == 0 {
                    return None;
                }
                let table_start_idx = hashtab.header.symbias as usize;
                let chain_start_idx = unsafe {
                    hashtab
                        .buckets
                        .add((hash as usize) % hashtab.header.nbucket as usize)
                        .read()
                } as usize;
                if chain_start_idx == 0 {
                    return None;
                }
                let mut dynsym_idx = chain_start_idx;
                let mut cur_chain = unsafe { hashtab.chains.add(dynsym_idx - table_start_idx) };
                let mut cur_symbol_ptr = unsafe { self.symtab.add(dynsym_idx) };
                loop {
                    let chain_hash = unsafe { cur_chain.read() };
                    if hash | 1 == chain_hash | 1 {
                        let cur_symbol = unsafe { &*cur_symbol_ptr };
                        let sym_name = self.strtab.get_str(cur_symbol.st_name());
                        #[cfg(feature = "version")]
                        if sym_name == symbol.name && self.check_match(dynsym_idx, &symbol.version)
                        {
                            return Some(cur_symbol);
                        }
                        #[cfg(not(feature = "version"))]
                        if sym_name == symbol.name {
                            return Some(cur_symbol);
                        }
                    }
                    if chain_hash & 1 != 0 {
                        break;
                    }
                    cur_chain = unsafe { cur_chain.add(1) };
                    cur_symbol_ptr = unsafe { cur_symbol_ptr.add(1) };
                    dynsym_idx += 1;
                }
            }
            SymbolTableHashTable::Elf(hashtab) => {
                let hash = if let Some(hash) = precompute.hash {
                    hash
                } else {
                    let hash = ElfHash::hash(symbol.name.as_bytes());
                    precompute.hash = Some(hash);
                    hash
                };
                let bucket_idx = (hash as usize) % hashtab.header.nbucket as usize;
                let bucket_ptr = unsafe { hashtab.buckets.add(bucket_idx) };
                let mut chain_idx = unsafe { bucket_ptr.read() as usize };
                loop {
                    if chain_idx == 0 {
                        return None;
                    }
                    let chain_ptr = unsafe { hashtab.chains.add(chain_idx) };
                    let cur_symbol = unsafe { &*self.symtab.add(chain_idx) };
                    let sym_name = self.strtab.get_str(cur_symbol.st_name());
                    #[cfg(feature = "version")]
                    if sym_name == symbol.name && self.check_match(chain_idx, &symbol.version) {
                        return Some(cur_symbol);
                    }
                    #[cfg(not(feature = "version"))]
                    if sym_name == symbol.name {
                        return Some(cur_symbol);
                    }
                    chain_idx = unsafe { chain_ptr.read() as usize };
                }
            }
        }
        None
    }

    /// Use the symbol specific information to get the symbol which can be used for relocation in the symbol table
    #[inline]
    pub fn lookup_filter(
        &self,
        symbol: &SymbolInfo,
        precompute: &mut PreCompute,
    ) -> Option<&ElfSymbol> {
        if let Some(sym) = self.lookup(symbol, precompute) {
            if !sym.is_undef() && sym.is_ok_bind() && sym.is_ok_type() {
                return Some(sym);
            }
        }
        None
    }

    /// Use the symbol index to get the symbols in the symbol table.
    pub fn symbol_idx<'symtab>(
        &'symtab self,
        idx: usize,
    ) -> (&'symtab ElfSymbol, SymbolInfo<'symtab>) {
        let symbol = unsafe { &*self.symtab.add(idx) };
        let cname = self.strtab.get_cstr(symbol.st_name());
        let name = ElfStringTable::convert_cstr(cname);
        (
            symbol,
            SymbolInfo {
                name,
                cname: Some(cname),
                #[cfg(feature = "version")]
                version: self.get_requirement(idx),
            },
        )
    }

    #[inline]
    pub fn count_syms(&self) -> usize {
        match &self.hashtab {
            SymbolTableHashTable::Gnu(hashtab) => hashtab.count_syms(),
            SymbolTableHashTable::Elf(hashtab) => hashtab.count_syms(),
        }
    }
}