Line data Source code
1 : use lief_ffi as ffi;
2 :
3 : /// This structure is used to tweak the ELF parser: [`lief::elf::Binary::parse_with_config`]
4 0 : #[derive(Debug, Clone, PartialEq, Eq, Hash)]
5 : pub struct Config {
6 : /// Whether relocations (including plt-like relocations) should be parsed.
7 : pub parse_relocations: bool,
8 :
9 : /// Whether dynamic symbols (those from `.dynsym`) should be parsed
10 : pub parse_dyn_symbols: bool,
11 :
12 : /// Whether debug symbols (those from `.symtab`) should be parsed
13 : pub parse_symtab_symbols: bool,
14 :
15 : /// Whether versioning symbols should be parsed
16 : pub parse_symbol_versions: bool,
17 :
18 : /// Whether ELF notes information should be parsed
19 : pub parse_notes: bool,
20 :
21 : /// Whether the overlay data should be parsed
22 : pub parse_overlay: bool,
23 :
24 : /// The method used to count the number of dynamic symbols
25 : pub count_mtd: DynSymCount,
26 :
27 : /// Memory page size if the binary uses a non-standard value.
28 : ///
29 : /// For instance, SPARCV9 binary can use page size from 0x2000 to 0x100000.
30 : pub page_size: u64,
31 : }
32 :
33 : impl Default for Config {
34 0 : fn default() -> Config {
35 0 : Config {
36 0 : parse_relocations: true,
37 0 : parse_dyn_symbols: true,
38 0 : parse_symtab_symbols: true,
39 0 : parse_symbol_versions: true,
40 0 : parse_notes: true,
41 0 : parse_overlay: true,
42 0 : count_mtd: DynSymCount::AUTO,
43 0 : page_size: 0,
44 0 : }
45 0 : }
46 : }
47 :
48 : impl Config {
49 : #[doc(hidden)]
50 0 : pub fn to_ffi(&self) -> cxx::UniquePtr<ffi::ELF_ParserConfig> {
51 0 : let mut ptr = ffi::ELF_ParserConfig::create();
52 0 : ptr.pin_mut().set_parse_relocations(self.parse_relocations);
53 0 : ptr.pin_mut().set_parse_dyn_symbols(self.parse_dyn_symbols);
54 0 : ptr.pin_mut().set_parse_symtab_symbols(self.parse_symtab_symbols);
55 0 : ptr.pin_mut().set_parse_symbol_versions(self.parse_symbol_versions);
56 0 : ptr.pin_mut().set_parse_notes(self.parse_notes);
57 0 : ptr.pin_mut().set_parse_overlay(self.parse_overlay);
58 0 : ptr.pin_mut().set_count_mtd(self.count_mtd.into());
59 0 : ptr.pin_mut().set_page_size(self.page_size);
60 0 : ptr
61 0 : }
62 : }
63 :
64 :
65 : /// Enum that describes the different methods that can be used by the parser to identity
66 : /// the number of dynamic symbols
67 : #[allow(non_camel_case_types)]
68 0 : #[derive(Debug, Copy, Clone, PartialEq, Eq, Hash)]
69 : pub enum DynSymCount {
70 : /// Automatic detection
71 : AUTO,
72 :
73 : /// Count based on sections (not very reliable
74 : SECTION,
75 :
76 : /// Count based on hash table (reliable)
77 : HASH,
78 :
79 : /// Count based on PLT/GOT relocations (very reliable but not accurate)
80 : RELOCATIONS,
81 :
82 : UNKNOWN(u32),
83 : }
84 :
85 : impl From<u32> for DynSymCount {
86 0 : fn from(value: u32) -> Self {
87 0 : match value {
88 0 : 0 => DynSymCount::AUTO,
89 0 : 1 => DynSymCount::SECTION,
90 0 : 2 => DynSymCount::HASH,
91 0 : 3 => DynSymCount::RELOCATIONS,
92 0 : _ => DynSymCount::UNKNOWN(value),
93 : }
94 0 : }
95 : }
96 :
97 : impl From<DynSymCount> for u32 {
98 0 : fn from(value: DynSymCount) -> u32 {
99 0 : match value {
100 0 : DynSymCount::AUTO => 0,
101 0 : DynSymCount::SECTION => 1,
102 0 : DynSymCount::HASH => 2,
103 0 : DynSymCount::RELOCATIONS => 3,
104 0 : DynSymCount::UNKNOWN(value) => value,
105 : }
106 0 : }
107 : }
|