Skip to main content

elf_parser/
lib.rs

1//! A minimal no_std ELF (32/64) parser.
2//!
3//! ## Example Usage
4//! ```ignore
5//! use elf_parser::Elf64;
6//!
7//! fn main() {
8//!     let bytes = include_bytes!("path/to/elf_file");
9//!     let elf64 = Elf64::from_bytes(bytes).unwrap();
10//!     let ehdr = elf64.ehdr();
11//!     dbg!(ehdr);
12//!     
13//!     let phdr_iter = elf64.phdr_iter();
14//!     for phdr in phdr_iter {     
15//!         dbg!(phdr);
16//!     }
17//! }
18
19#![no_std]
20use core::fmt;
21
22pub mod elf32;
23pub mod elf64;
24pub mod types;
25mod util;
26
27pub use elf32::{Elf32, Elf32Ehdr, Elf32Phdr, Elf32Shdr};
28pub use elf64::{Elf64, Elf64Ehdr, Elf64Phdr, Elf64Shdr};
29
30#[derive(Copy, Clone, PartialEq, Eq)]
31pub enum Error {
32    /// Magic numbers is not [0x7f, b'E', b'L', b'F'].
33    InvalidMagicNumber,
34    /// Out-of-bounds access to a page/section header table.
35    InvalidIndex,
36    /// The ELF file is loadead as a ELF32 although it is ELF64, and vice versa.
37    InvalidClass,
38    /// Failed to get header info. Probably, the file includes invalid value.
39    Corrupted,
40}
41
42impl fmt::Debug for Error {
43    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
44        let name = match self {
45            Error::InvalidMagicNumber => "InvalidMagicNumber",
46            Error::InvalidIndex => "InvalidIndex",
47            Error::InvalidClass => "InvalidClass",
48            Error::Corrupted => "Corrupted",
49        };
50        f.write_fmt(format_args!("{}", name))
51    }
52}