Line data Source code
1 : //! This module wraps a PDB compilation unit
2 :
3 : use lief_ffi as ffi;
4 :
5 : use std::marker::PhantomData;
6 :
7 : use crate::common::FromFFI;
8 : use crate::declare_fwd_iterator;
9 :
10 : use super::function::Functions;
11 :
12 : /// This structure represents a CompilationUnit (or Module) in a PDB file
13 : pub struct CompilationUnit<'a> {
14 : ptr: cxx::UniquePtr<ffi::PDB_CompilationUnit>,
15 : _owner: PhantomData<&'a ()>,
16 : }
17 :
18 :
19 : impl FromFFI<ffi::PDB_CompilationUnit> for CompilationUnit<'_> {
20 0 : fn from_ffi(ptr: cxx::UniquePtr<ffi::PDB_CompilationUnit>) -> Self {
21 0 : Self {
22 0 : ptr,
23 0 : _owner: PhantomData,
24 0 : }
25 0 : }
26 : }
27 :
28 : impl CompilationUnit<'_> {
29 : /// Name (or path) to the COFF object (`.obj`) associated with this
30 : /// compilation unit (e.g. `e:\obj.amd64fre\minkernel\ntos\hvl\mp\objfre\amd64\hvlp.obj`)
31 0 : pub fn module_name(&self) -> String {
32 0 : self.ptr.module_name().to_string()
33 0 : }
34 :
35 : /// Name of path to the original binary object (COFF, Archive) in which
36 : /// the compilation unit was located before being linked.
37 : /// e.g. `e:\obj.amd64fre\minkernel\ntos\hvl\mp\objfre\amd64\hvl.lib`
38 0 : pub fn object_filename(&self) -> String {
39 0 : self.ptr.object_filename().to_string()
40 0 : }
41 :
42 : /// Return an iterator over the [`crate::pdb::Function`] defined in this compilation unit.
43 : /// If the PDB does not contain or has an empty DBI stream, it returns
44 : /// an empty iterator.
45 0 : pub fn functions(&self) -> Functions {
46 0 : Functions::new(self.ptr.functions())
47 0 : }
48 :
49 : /// Iterator over the sources files (as string) that compose this compilation unit.
50 : /// These files include the **header** (`.h, .hpp`, ...).
51 0 : pub fn sources(&self) -> Sources {
52 0 : Sources::new(self.ptr.sources())
53 0 : }
54 : }
55 :
56 0 : declare_fwd_iterator!(
57 0 : CompilationUnits,
58 0 : CompilationUnit<'a>,
59 0 : ffi::PDB_CompilationUnit,
60 0 : ffi::PDB_DebugInfo,
61 0 : ffi::PDB_DebugInfo_it_compilation_units
62 0 : );
63 :
64 :
65 : pub struct Sources<'a> {
66 : #[doc(hidden)]
67 : pub it: cxx::UniquePtr<ffi::PDB_CompilationUnit_it_sources>,
68 : _owner: PhantomData<&'a ffi::PDB_CompilationUnit>,
69 : }
70 :
71 : impl Sources<'_> {
72 : #[doc(hidden)]
73 0 : pub fn new(it: cxx::UniquePtr<ffi::PDB_CompilationUnit_it_sources>) -> Self {
74 0 : Self {
75 0 : it,
76 0 : _owner: PhantomData,
77 0 : }
78 0 : }
79 : }
80 :
81 : impl Iterator for Sources<'_> {
82 : type Item = String;
83 0 : fn next(&mut self) -> Option<Self::Item> {
84 0 : let string = self.it.as_mut().unwrap().next().to_string();
85 0 : // c.f. comment in rust/LIEF/PDB/CompilationUnit.hpp
86 0 : if string == "[LIEF_STOP]" {
87 0 : None
88 : } else {
89 0 : Some(string)
90 : }
91 0 : }
92 : }
|