Line data Source code
1 : //! Module for the ELF file format support in LIEF.
2 : //!
3 : //! The [`Binary`] structure exposes the main API to inspect an ELF file. It can be instantiated,
4 : //! using either: [`crate::elf::parse`], [`crate::elf::Binary::parse`] or [`crate::Binary::parse`]
5 : //!
6 : //! ```
7 : //! let elf = lief::elf::parse("/bin/ls").unwrap();
8 : //! for section in elf.sections() {
9 : //! println!("section: {}", section.name());
10 : //! }
11 : //! ```
12 :
13 : use std::path::Path;
14 : use lief_ffi as ffi;
15 : use crate::common::AsFFI;
16 :
17 : pub mod binary;
18 : pub mod builder;
19 : pub mod dynamic;
20 : pub mod hash;
21 : pub mod header;
22 : pub mod note;
23 : pub mod relocation;
24 : pub mod section;
25 : pub mod segment;
26 : pub mod symbol;
27 : pub mod symbol_versioning;
28 : pub mod parser_config;
29 :
30 : #[doc(inline)]
31 : pub use binary::Binary;
32 :
33 : #[doc(inline)]
34 : pub use header::Header;
35 :
36 : #[doc(inline)]
37 : pub use section::Section;
38 :
39 : #[doc(inline)]
40 : pub use segment::Segment;
41 :
42 : #[doc(inline)]
43 : pub use symbol::Symbol;
44 :
45 : #[doc(inline)]
46 : pub use hash::Sysv as SysvHash;
47 :
48 : #[doc(inline)]
49 : pub use hash::Gnu as GnuHash;
50 :
51 : #[doc(inline)]
52 : pub use note::Notes;
53 :
54 : #[doc(inline)]
55 : pub use dynamic::Entries as DynamicEntries;
56 :
57 : #[doc(inline)]
58 : pub use relocation::Relocation;
59 :
60 : #[doc(inline)]
61 : pub use symbol_versioning::{
62 : SymbolVersion, SymbolVersionAux, SymbolVersionAuxRequirement, SymbolVersionDefinition,
63 : SymbolVersionRequirement,
64 : };
65 :
66 : #[doc(inline)]
67 : pub use parser_config::Config as ParserConfig;
68 :
69 : /// Parse an ELF file from the given file path
70 0 : pub fn parse<P: AsRef<Path>>(path: P) -> Option<Binary> {
71 0 : Binary::parse(path)
72 0 : }
73 :
74 : /// Parse an ELF file from the given file path and configuration
75 0 : pub fn parse_with_config<P: AsRef<Path>>(path: P, config: &ParserConfig) -> Option<Binary> {
76 0 : Binary::parse_with_config(path, config)
77 0 : }
78 :
79 : /// Check that the layout of the given binary is correct
80 0 : pub fn check_layout(binary: &Binary) -> Result<(), String> {
81 0 : cxx::let_cxx_string!(error = "");
82 0 : unsafe {
83 0 : if ffi::ELF_Utils::check_layout(binary.as_ffi(), error.as_mut().get_unchecked_mut()) {
84 0 : return Ok(());
85 0 : }
86 0 : }
87 0 : Err(error.to_string())
88 0 : }
|