[go: up one dir, main page]

elf 0.3.1

A pure-rust library for parsing ELF 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
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
use core::ops::Range;

#[cfg(feature = "std")]
use std::collections::hash_map::Entry;
#[cfg(feature = "std")]
use std::collections::HashMap;
#[cfg(feature = "std")]
use std::io::{Read, Seek, SeekFrom};

#[derive(Debug)]
pub enum ParseError {
    /// Returned when the ELF File Header's magic bytes weren't ELF's defined
    /// magic bytes
    BadMagic([u8; 4]),
    /// Returned when the ELF File Header's `e_ident[EI_CLASS]` wasn't one of the
    /// defined `ELFCLASS*` constants
    UnsupportedElfClass(u8),
    /// Returned when the ELF File Header's `e_ident[EI_DATA]` wasn't one of the
    /// defined `ELFDATA*` constants
    UnsupportedElfEndianness(u8),
    /// Returned when the ELF File Header's `e_ident[EI_VERSION]` wasn't
    /// `EV_CURRENT(1)`
    UnsupportedElfVersion(u8),
    /// Returned when parsing an ELF structure resulted in an offset which fell
    /// out of bounds of the requested structure
    BadOffset(u64),
    /// Returned when parsing a string out of a StringTable failed to find the
    /// terminating NUL byte
    StringTableMissingNul(u64),
    /// Returned when parsing a table of ELF structures and the file specified
    /// an entry size for that table that was different than what we had
    /// expected
    BadEntsize((u64, u64)),
    /// Returned when trying to interpret a section's data as the wrong type.
    /// For example, trying to treat an SHT_PROGBIGS section as a SHT_STRTAB.
    UnexpectedSectionType((u32, u32)),
    /// Returned when parsing an ELF structure out of an in-memory `&[u8]`
    /// resulted in a request for a section of file bytes outside the range of
    /// the slice. Commonly caused by truncated file contents.
    SliceReadError((usize, usize)),
    /// Returned when parsing a string out of a StringTable that contained
    /// invalid Utf8
    Utf8Error(core::str::Utf8Error),
    /// Returned when parsing an ELF structure and the underlying structure data
    /// was truncated and thus the full structure contents could not be parsed.
    TryFromSliceError(core::array::TryFromSliceError),
    #[cfg(feature = "std")]
    /// Returned when parsing an ELF structure out of an io stream encountered
    /// an io error.
    IOError(std::io::Error),
}

#[cfg(feature = "std")]
impl std::error::Error for ParseError {
    fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
        match *self {
            ParseError::BadMagic(_) => None,
            ParseError::UnsupportedElfClass(_) => None,
            ParseError::UnsupportedElfEndianness(_) => None,
            ParseError::UnsupportedElfVersion(_) => None,
            ParseError::BadOffset(_) => None,
            ParseError::StringTableMissingNul(_) => None,
            ParseError::BadEntsize(_) => None,
            ParseError::UnexpectedSectionType(_) => None,
            ParseError::SliceReadError(_) => None,
            ParseError::Utf8Error(ref err) => Some(err),
            ParseError::TryFromSliceError(ref err) => Some(err),
            #[cfg(feature = "std")]
            ParseError::IOError(ref err) => Some(err),
        }
    }
}

impl core::fmt::Display for ParseError {
    fn fmt(&self, f: &mut core::fmt::Formatter) -> core::fmt::Result {
        match *self {
            ParseError::BadMagic(ref magic) => {
                write!(f, "Invalid Magic Bytes: {magic:X?}")
            }
            ParseError::UnsupportedElfClass(class) => {
                write!(f, "Unsupported ELF Class: {class}")
            }
            ParseError::UnsupportedElfEndianness(endianness) => {
                write!(f, "Unsupported ELF Endianness: {endianness}")
            }
            ParseError::UnsupportedElfVersion(version) => {
                write!(f, "Unsupported ELF Version: {version}")
            }
            ParseError::BadOffset(offset) => {
                write!(f, "Bad offset: {offset:#X}")
            }
            ParseError::StringTableMissingNul(offset) => {
                write!(
                    f,
                    "Could not find terminating NUL byte starting at offset: {offset:#X}"
                )
            }
            ParseError::BadEntsize((found, expected)) => {
                write!(
                    f,
                    "Invalid entsize. Expected: {expected:#X}, Found: {found:#X}"
                )
            }
            ParseError::UnexpectedSectionType((found, expected)) => {
                write!(
                    f,
                    "Could not interpret section of type {found} as type {expected}"
                )
            }
            ParseError::SliceReadError((start, end)) => {
                write!(f, "Could not read bytes in range [{start:#X}, {end:#X})")
            }
            ParseError::Utf8Error(ref err) => err.fmt(f),
            ParseError::TryFromSliceError(ref err) => err.fmt(f),
            #[cfg(feature = "std")]
            ParseError::IOError(ref err) => err.fmt(f),
        }
    }
}

impl From<core::str::Utf8Error> for ParseError {
    fn from(err: core::str::Utf8Error) -> Self {
        ParseError::Utf8Error(err)
    }
}

impl From<core::array::TryFromSliceError> for ParseError {
    fn from(err: core::array::TryFromSliceError) -> Self {
        ParseError::TryFromSliceError(err)
    }
}

#[cfg(feature = "std")]
impl From<std::io::Error> for ParseError {
    fn from(err: std::io::Error) -> ParseError {
        ParseError::IOError(err)
    }
}

/// Represents the ELF file data format (little-endian vs big-endian)
#[derive(Debug, Copy, Clone, PartialEq, Eq)]
pub enum Endian {
    Little,
    Big,
}

impl core::fmt::Display for Endian {
    fn fmt(&self, f: &mut core::fmt::Formatter) -> core::fmt::Result {
        let str = match self {
            Endian::Little => "2's complement, little endian",
            Endian::Big => "2's complement, big endian",
        };
        write!(f, "{}", str)
    }
}

/// Represents the ELF file data format (little-endian vs big-endian)
#[derive(Debug, Copy, Clone, PartialEq, Eq)]
pub enum Class {
    ELF32,
    ELF64,
}

impl core::fmt::Display for Class {
    fn fmt(&self, f: &mut core::fmt::Formatter) -> core::fmt::Result {
        let str = match self {
            Class::ELF32 => "32-bit",
            Class::ELF64 => "64-bit",
        };
        write!(f, "{}", str)
    }
}

/// Trait which exposes an interface for getting blocks of bytes from a data source.
pub trait ReadBytesAt {
    /// This is the standard reading method for getting a chunk of in-memory ELF
    /// data from which to parse.
    ///
    /// Note that self is mut here, as this method acts as a "read and return" combination
    /// where the "read" step allows implementers to mutate internal state to serve the read,
    /// and the "return" step returns a reference to the read data.
    ///
    /// If you're wanting to read multiple disjoint chunks of data then the borrowing
    /// semantics here won't let you keep a reference to the first chunk while reading
    /// the next. If you want to do that, try the load_bytes_at() + get_loaded_bytes_at() pairing.
    fn read_bytes_at(&mut self, range: Range<usize>) -> Result<&[u8], ParseError>;

    /// Load a chunk of data from the underlying data source, but don't return a reference to it.
    /// The loaded chunk can be requested by get_loaded_bytes_at().
    ///
    /// This method allows implementers to do multiple disjoint mutating reads on the
    /// underlying data source in a row in order to later provide concurrent immuntable
    /// references to those disjoint chunks of data via get_loaded_bytes_at().
    fn load_bytes_at(&mut self, range: Range<usize>) -> Result<(), ParseError> {
        // Validate that the underlying data does in fact contain the requested range
        self.read_bytes_at(range)?;

        // Now that we did the load/validate step, we're done
        Ok(())
    }

    /// Get a reference to a chunk of data that was previously loaded by load_bytes_at()
    ///
    /// This method allows implementers to do multiple disjoint mutating reads on the
    /// underlying data source in a row in order to later provide concurrent immuntable
    /// references to those disjoint chunks of data via get_loaded_bytes_at().
    ///
    /// Panics if the range was not previously loaded via load_bytes_at()
    /// (logic bug when using this interface)
    fn get_loaded_bytes_at(&self, range: Range<usize>) -> &[u8];
}

impl ReadBytesAt for &[u8] {
    fn read_bytes_at(&mut self, range: Range<usize>) -> Result<&[u8], ParseError> {
        let start = range.start;
        let end = range.end;
        self.get(range)
            .ok_or(ParseError::SliceReadError((start, end)))
    }

    fn get_loaded_bytes_at(&self, range: Range<usize>) -> &[u8] {
        let start = range.start;
        let end = range.end;
        self.get(range)
            .ok_or(ParseError::SliceReadError((start, end)))
            .unwrap()
    }
}

#[cfg(feature = "std")]
pub struct CachedReadBytes<R: Read + Seek> {
    reader: R,
    bufs: HashMap<(u64, u64), Box<[u8]>>,
}

#[cfg(feature = "std")]
impl<R: Read + Seek> CachedReadBytes<R> {
    pub fn new(reader: R) -> Self {
        CachedReadBytes {
            reader,
            bufs: HashMap::<(u64, u64), Box<[u8]>>::default(),
        }
    }
}

#[cfg(feature = "std")]
impl<R: Read + Seek> ReadBytesAt for &mut CachedReadBytes<R> {
    fn read_bytes_at(&mut self, range: Range<usize>) -> Result<&[u8], ParseError> {
        if range.len() == 0 {
            return Ok(&[]);
        }

        let start = range.start as u64;
        let size = range.len() as u64;

        Ok(match self.bufs.entry((start, size)) {
            Entry::Occupied(entry) => entry.into_mut(),
            Entry::Vacant(entry) => {
                self.reader.seek(SeekFrom::Start(start))?;
                let mut bytes = vec![0; size as usize].into_boxed_slice();
                self.reader.read_exact(&mut bytes)?;
                entry.insert(bytes)
            }
        })
    }

    fn get_loaded_bytes_at(&self, range: Range<usize>) -> &[u8] {
        let start = range.start as u64;
        let size = range.len() as u64;
        self.bufs.get(&(start, size)).unwrap()
    }
}

/// Trait for endian-aware parsing of integer types.
pub trait ParseAtExt {
    fn parse_u8_at(&self, offset: &mut usize) -> Result<u8, ParseError>;
    fn parse_u16_at(&self, endian: Endian, offset: &mut usize) -> Result<u16, ParseError>;
    fn parse_u32_at(&self, endian: Endian, offset: &mut usize) -> Result<u32, ParseError>;
    fn parse_u64_at(&self, endian: Endian, offset: &mut usize) -> Result<u64, ParseError>;
}

/// Extend the byte slice type with endian-aware parsing. These are the basic parsing methods
/// for our parser-combinator approach to parsing ELF structures from in-memory byte buffers.
impl ParseAtExt for &[u8] {
    fn parse_u8_at(&self, offset: &mut usize) -> Result<u8, ParseError> {
        let data = self
            .get(*offset)
            .ok_or(ParseError::BadOffset(*offset as u64))?;
        *offset += 1;
        Ok(*data)
    }

    fn parse_u16_at(&self, endian: Endian, offset: &mut usize) -> Result<u16, ParseError> {
        let range = *offset..*offset + 2;
        let data: [u8; 2] = self
            .get(range)
            .ok_or(ParseError::BadOffset(*offset as u64))?
            .try_into()?;
        *offset += 2;
        match endian {
            Endian::Little => Ok(u16::from_le_bytes(data)),
            Endian::Big => Ok(u16::from_be_bytes(data)),
        }
    }

    fn parse_u32_at(&self, endian: Endian, offset: &mut usize) -> Result<u32, ParseError> {
        let range = *offset..*offset + 4;
        let data: [u8; 4] = self
            .get(range)
            .ok_or(ParseError::BadOffset(*offset as u64))?
            .try_into()?;
        *offset += 4;
        match endian {
            Endian::Little => Ok(u32::from_le_bytes(data)),
            Endian::Big => Ok(u32::from_be_bytes(data)),
        }
    }

    fn parse_u64_at(&self, endian: Endian, offset: &mut usize) -> Result<u64, ParseError> {
        let range = *offset..*offset + 8;
        let data: [u8; 8] = self
            .get(range)
            .ok_or(ParseError::BadOffset(*offset as u64))?
            .try_into()?;
        *offset += 8;
        match endian {
            Endian::Little => Ok(u64::from_le_bytes(data)),
            Endian::Big => Ok(u64::from_be_bytes(data)),
        }
    }
}

#[cfg(test)]
mod read_bytes_tests {
    use super::ReadBytesAt;
    use super::*;
    use std::io::Cursor;

    #[test]
    fn byte_slice_read_bytes_at_works() {
        let data = [1u8, 2u8, 3u8, 4u8];
        let slice = &mut data.as_slice();
        let bytes = slice
            .read_bytes_at(1..3)
            .expect("Failed to get expected bytes");
        assert_eq!(bytes, [2u8, 3u8]);
    }

    #[test]
    fn byte_slice_read_bytes_at_empty_buffer() {
        let data = [];
        assert!(matches!(
            data.as_ref().read_bytes_at(1..3),
            Err(ParseError::SliceReadError(_))
        ));
    }

    #[test]
    fn byte_slice_read_bytes_at_past_end_of_buffer() {
        let data = [1u8, 2u8];
        assert!(matches!(
            data.as_ref().read_bytes_at(1..3),
            Err(ParseError::SliceReadError(_))
        ));
    }

    #[test]
    fn byte_slice_multiple_overlapping_reference_lifetimes() {
        let data = [1u8, 2u8, 3u8, 4u8];
        let slice = data.as_ref();

        slice
            .as_ref()
            .load_bytes_at(0..2)
            .expect("Failed to get expected bytes");
        slice
            .as_ref()
            .load_bytes_at(2..4)
            .expect("Failed to get expected bytes");

        let bytes1 = slice.get_loaded_bytes_at(0..2);
        let bytes2 = slice.get_loaded_bytes_at(2..4);
        assert_eq!(bytes1, [1u8, 2u8]);
        assert_eq!(bytes2, [3u8, 4u8]);
    }

    #[test]
    fn cached_read_bytes_multiple_non_overlapping_reference_lifetimes() {
        let data = [1u8, 2u8, 3u8, 4u8];
        let cur = Cursor::new(data);
        let mut cached = &mut CachedReadBytes::new(cur);

        let bytes1 = cached
            .read_bytes_at(0..2)
            .expect("Failed to get expected bytes");
        assert_eq!(bytes1, [1u8, 2u8]);
        let bytes2 = cached
            .read_bytes_at(2..4)
            .expect("Failed to get expected bytes");
        assert_eq!(bytes2, [3u8, 4u8]);
    }

    #[test]
    fn cached_read_bytes_multiple_overlapping_reference_lifetimes() {
        let data = [1u8, 2u8, 3u8, 4u8];
        let cur = Cursor::new(data);
        let mut cached = &mut CachedReadBytes::new(cur);

        cached
            .load_bytes_at(0..2)
            .expect("Failed to get expected bytes");
        cached
            .load_bytes_at(2..4)
            .expect("Failed to get expected bytes");

        let bytes1 = cached.get_loaded_bytes_at(0..2);
        let bytes2 = cached.get_loaded_bytes_at(2..4);
        assert_eq!(bytes1, [1u8, 2u8]);
        assert_eq!(bytes2, [3u8, 4u8]);
    }
}

#[cfg(test)]
mod parse_tests {
    use super::*;

    #[test]
    fn parse_u16_lsb() {
        let data = [0x10u8, 0x20u8];
        let mut offset = 0;
        let result = data
            .as_ref()
            .parse_u16_at(Endian::Little, &mut offset)
            .expect("Failed to parse u16");
        assert_eq!(result, 0x2010u16);
        assert_eq!(offset, 2);
    }

    #[test]
    fn parse_u16_msb() {
        let data = [0x10u8, 0x20u8];
        let mut offset = 0;
        let result = data
            .as_ref()
            .parse_u16_at(Endian::Big, &mut offset)
            .expect("Failed to parse u16");
        assert_eq!(result, 0x1020u16);
        assert_eq!(offset, 2);
    }

    #[test]
    fn parse_u16_too_short() {
        let data = [0x10u8];
        let mut offset = 0;
        let result = data.as_ref().parse_u16_at(Endian::Big, &mut offset);
        assert!(
            matches!(result, Err(ParseError::BadOffset(0))),
            "Unexpected Error type found: {result:?}"
        );
        assert_eq!(offset, 0);
    }

    #[test]
    fn parse_u32_lsb() {
        let data = [0x10u8, 0x20u8, 0x30u8, 0x40u8];
        let mut offset = 0;
        let result = data
            .as_ref()
            .parse_u32_at(Endian::Little, &mut offset)
            .expect("Failed to parse u32");
        assert_eq!(result, 0x40302010u32);
        assert_eq!(offset, 4);
    }

    #[test]
    fn parse_u32_msb() {
        let data = [0x10u8, 0x20u8, 0x30u8, 0x40u8];
        let mut offset = 0;
        let result = data
            .as_ref()
            .parse_u32_at(Endian::Big, &mut offset)
            .expect("Failed to parse u32");
        assert_eq!(result, 0x10203040u32);
        assert_eq!(offset, 4);
    }

    #[test]
    fn parse_u32_too_short() {
        let data = [0x10u8, 0x20u8];
        let mut offset = 0;
        let result = data.as_ref().parse_u32_at(Endian::Little, &mut offset);
        assert!(
            matches!(result, Err(ParseError::BadOffset(0))),
            "Unexpected Error type found: {result:?}"
        );
        assert_eq!(offset, 0);
    }

    #[test]
    fn parse_u64_lsb() {
        let data = [
            0x10u8, 0x20u8, 0x30u8, 0x40u8, 0x50u8, 0x60u8, 0x70u8, 0x80u8,
        ];
        let mut offset = 0;
        let result = data
            .as_ref()
            .parse_u64_at(Endian::Little, &mut offset)
            .expect("Failed to parse u64");
        assert_eq!(result, 0x8070605040302010u64);
        assert_eq!(offset, 8);
    }

    #[test]
    fn parse_u64_msb() {
        let data = [
            0x10u8, 0x20u8, 0x30u8, 0x40u8, 0x50u8, 0x60u8, 0x70u8, 0x80u8,
        ];
        let mut offset = 0;
        let result = data
            .as_ref()
            .parse_u64_at(Endian::Big, &mut offset)
            .expect("Failed to parse u32");
        assert_eq!(result, 0x1020304050607080u64);
        assert_eq!(offset, 8);
    }

    #[test]
    fn parse_u64_too_short() {
        let data = [0x10u8, 0x20u8];
        let mut offset = 0;
        let result = data.as_ref().parse_u64_at(Endian::Little, &mut offset);
        assert!(
            matches!(result, Err(ParseError::BadOffset(0))),
            "Unexpected Error type found: {result:?}"
        );
        assert_eq!(offset, 0);
    }
}