Line data Source code
1 : use lief_ffi as ffi;
2 :
3 : /// This structure is used to tweak the ELF parser: [`crate::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()
55 0 : .set_parse_symtab_symbols(self.parse_symtab_symbols);
56 0 : ptr.pin_mut()
57 0 : .set_parse_symbol_versions(self.parse_symbol_versions);
58 0 : ptr.pin_mut().set_parse_notes(self.parse_notes);
59 0 : ptr.pin_mut().set_parse_overlay(self.parse_overlay);
60 0 : ptr.pin_mut().set_count_mtd(self.count_mtd.into());
61 0 : ptr.pin_mut().set_page_size(self.page_size);
62 0 : ptr
63 0 : }
64 : }
65 :
66 : /// Enum that describes the different methods that can be used by the parser to identify
67 : /// the number of dynamic symbols
68 : #[allow(non_camel_case_types)]
69 0 : #[derive(Debug, Copy, Clone, PartialEq, Eq, Hash)]
70 : pub enum DynSymCount {
71 : /// Automatic detection
72 : AUTO,
73 :
74 : /// Count based on sections (not very reliable
75 : SECTION,
76 :
77 : /// Count based on hash table (reliable)
78 : HASH,
79 :
80 : /// Count based on PLT/GOT relocations (very reliable but not accurate)
81 : RELOCATIONS,
82 :
83 : UNKNOWN(u32),
84 : }
85 :
86 : impl From<u32> for DynSymCount {
87 0 : fn from(value: u32) -> Self {
88 0 : match value {
89 0 : 0 => DynSymCount::AUTO,
90 0 : 1 => DynSymCount::SECTION,
91 0 : 2 => DynSymCount::HASH,
92 0 : 3 => DynSymCount::RELOCATIONS,
93 0 : _ => DynSymCount::UNKNOWN(value),
94 : }
95 0 : }
96 : }
97 :
98 : impl From<DynSymCount> for u32 {
99 0 : fn from(value: DynSymCount) -> u32 {
100 0 : match value {
101 0 : DynSymCount::AUTO => 0,
102 0 : DynSymCount::SECTION => 1,
103 0 : DynSymCount::HASH => 2,
104 0 : DynSymCount::RELOCATIONS => 3,
105 0 : DynSymCount::UNKNOWN(value) => value,
106 : }
107 0 : }
108 : }
|