Line data Source code
1 : use lief_ffi as ffi;
2 : use std::marker::PhantomData;
3 :
4 : use crate::common::FromFFI;
5 : use crate::declare_fwd_iterator;
6 : use crate::elf::note::NoteBase;
7 :
8 : /// An entry in the core file mapping
9 : pub struct Entry<'a> {
10 : ptr: cxx::UniquePtr<ffi::ELF_CoreFile_entry>,
11 : _owner: PhantomData<&'a ffi::ELF_CoreFile>,
12 : }
13 :
14 : impl Entry<'_> {
15 : /// Start address of the mapping
16 52 : pub fn start(&self) -> u64 {
17 52 : self.ptr.start()
18 52 : }
19 :
20 : /// End address of the mapping
21 52 : pub fn end(&self) -> u64 {
22 52 : self.ptr.end()
23 52 : }
24 :
25 : /// Offset in the file
26 52 : pub fn file_ofs(&self) -> u64 {
27 52 : self.ptr.file_ofs()
28 52 : }
29 :
30 : /// File path
31 325 : pub fn path(&self) -> String {
32 325 : self.ptr.path().to_string()
33 325 : }
34 : }
35 :
36 : impl std::fmt::Debug for Entry<'_> {
37 0 : fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
38 0 : f.debug_struct("Entry")
39 0 : .field("start", &format_args!("0x{:x}", self.start()))
40 0 : .field("end", &format_args!("0x{:x}", self.end()))
41 0 : .field("file_ofs", &format_args!("0x{:x}", self.file_ofs()))
42 0 : .field("path", &self.path())
43 0 : .finish()
44 0 : }
45 : }
46 :
47 : impl<'a> FromFFI<ffi::ELF_CoreFile_entry> for Entry<'a> {
48 559 : fn from_ffi(ptr: cxx::UniquePtr<ffi::ELF_CoreFile_entry>) -> Self {
49 559 : Self {
50 559 : ptr,
51 559 : _owner: PhantomData,
52 559 : }
53 559 : }
54 : }
55 :
56 : /// Note representing core mapped files (`NT_FILE`)
57 : pub struct File<'a> {
58 : ptr: cxx::UniquePtr<ffi::ELF_CoreFile>,
59 : _owner: PhantomData<&'a ffi::ELF_Binary>,
60 : }
61 :
62 : impl File<'_> {
63 : /// Number of file entries
64 104 : pub fn count(&self) -> u64 {
65 104 : self.ptr.count()
66 104 : }
67 :
68 : /// Return the list of file entries as an iterator
69 26 : pub fn files(&self) -> Entries<'_> {
70 26 : Entries::new(self.ptr.files())
71 26 : }
72 : }
73 :
74 : impl NoteBase for File<'_> {
75 286 : fn get_base(&self) -> &ffi::ELF_Note {
76 286 : self.ptr.as_ref().unwrap().as_ref()
77 286 : }
78 : }
79 :
80 : impl FromFFI<ffi::ELF_CoreFile> for File<'_> {
81 104 : fn from_ffi(ptr: cxx::UniquePtr<ffi::ELF_CoreFile>) -> Self {
82 104 : Self {
83 104 : ptr,
84 104 : _owner: PhantomData,
85 104 : }
86 104 : }
87 : }
88 :
89 : impl std::fmt::Debug for File<'_> {
90 78 : fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
91 78 : let base = self as &dyn NoteBase;
92 78 : f.debug_struct("CoreFile")
93 78 : .field("base", &base)
94 78 : .field("count", &self.count())
95 78 : .finish()
96 78 : }
97 : }
98 :
99 559 : declare_fwd_iterator!(
100 559 : Entries,
101 559 : Entry<'a>,
102 559 : ffi::ELF_CoreFile_entry,
103 559 : ffi::ELF_CoreFile,
104 559 : ffi::ELF_CoreFile_it_files
105 559 : );
|