Line data Source code
1 : use lief_ffi as ffi;
2 :
3 : use super::variable::Variables;
4 : use super::{Scope, Type};
5 : use crate::common::{into_optional, into_ranges, FromFFI};
6 : use crate::declare_fwd_iterator;
7 : use crate::to_result;
8 : use crate::DebugLocation;
9 : use crate::Error;
10 : use crate::Range;
11 : use std::marker::PhantomData;
12 :
13 : /// This structure represents a DWARF function which can be associated with either:
14 : /// `DW_TAG_subprogram` or `DW_TAG_inlined_subroutine`.
15 : pub struct Function<'a> {
16 : ptr: cxx::UniquePtr<ffi::DWARF_Function>,
17 : _owner: PhantomData<&'a ()>,
18 : }
19 :
20 : impl FromFFI<ffi::DWARF_Function> for Function<'_> {
21 0 : fn from_ffi(ptr: cxx::UniquePtr<ffi::DWARF_Function>) -> Self {
22 0 : Self {
23 0 : ptr,
24 0 : _owner: PhantomData,
25 0 : }
26 0 : }
27 : }
28 :
29 : impl Function<'_> {
30 : /// The name of the function (`DW_AT_name`)
31 0 : pub fn name(&self) -> String {
32 0 : self.ptr.name().to_string()
33 0 : }
34 :
35 : /// The name of the function which is used for linking (`DW_AT_linkage_name`).
36 : ///
37 : /// This name differs from [`Function::name`] as it is usually mangled. The function
38 : /// return an empty string if the linkage name is not available.
39 0 : pub fn linkage_name(&self) -> String {
40 0 : self.ptr.linkage_name().to_string()
41 0 : }
42 :
43 : /// Return the address of the function (`DW_AT_entry_pc` or `DW_AT_low_pc`).
44 0 : pub fn address(&self) -> Result<u64, Error> {
45 0 : to_result!(ffi::DWARF_Function::address, self);
46 0 : }
47 :
48 : /// Return an iterator of variables (`DW_TAG_variable`) defined within the
49 : /// scope of this function. This includes regular stack-based variables as
50 : /// well as static ones.
51 0 : pub fn variables(&self) -> Variables {
52 0 : Variables::new(self.ptr.variables())
53 0 : }
54 :
55 : /// Whether this function is created by the compiler and not
56 : /// present in the original source code
57 0 : pub fn is_artificial(&self) -> bool {
58 0 : self.ptr.is_artificial()
59 0 : }
60 :
61 : /// Return the size taken by this function in the binary
62 0 : pub fn size(&self) -> u64 {
63 0 : self.ptr.size()
64 0 : }
65 :
66 : /// Ranges of virtual addresses owned by this function
67 0 : pub fn ranges(&self) -> Vec<Range> {
68 0 : into_ranges(self.ptr.ranges())
69 0 : }
70 :
71 : /// Original source code location
72 0 : pub fn debug_location(&self) -> DebugLocation {
73 0 : DebugLocation::from_ffi(self.ptr.debug_location())
74 0 : }
75 :
76 : /// Return the [`Type`] associated with the **return type** of this function.
77 0 : pub fn return_type(&self) -> Option<Type> {
78 0 : into_optional(self.ptr.get_type())
79 0 : }
80 :
81 : /// Return an iterator over the [`Parameter`] of this function
82 0 : pub fn parameters(&self) -> Parameters {
83 0 : Parameters::new(self.ptr.parameters())
84 0 : }
85 :
86 : /// The scope in which this function is defined
87 0 : pub fn scope(&self) -> Option<Scope> {
88 0 : into_optional(self.ptr.scope())
89 0 : }
90 : }
91 :
92 : /// This structure represents a DWARF function parameter.
93 : pub struct Parameter<'a> {
94 : ptr: cxx::UniquePtr<ffi::DWARF_Function_Parameter>,
95 : _owner: PhantomData<&'a ()>,
96 : }
97 :
98 : impl FromFFI<ffi::DWARF_Function_Parameter> for Parameter<'_> {
99 0 : fn from_ffi(ptr: cxx::UniquePtr<ffi::DWARF_Function_Parameter>) -> Self {
100 0 : Self {
101 0 : ptr,
102 0 : _owner: PhantomData,
103 0 : }
104 0 : }
105 : }
106 :
107 : impl Parameter<'_> {
108 : /// The name of the parameter
109 0 : pub fn name(&self) -> String {
110 0 : self.ptr.name().to_string()
111 0 : }
112 :
113 : /// Return the type of the parameter
114 0 : pub fn get_type(&self) -> Option<Type> {
115 0 : into_optional(self.ptr.get_type())
116 0 : }
117 : }
118 :
119 0 : declare_fwd_iterator!(
120 0 : Functions,
121 0 : Function<'a>,
122 0 : ffi::DWARF_Function,
123 0 : ffi::DWARF_CompilationUnit,
124 0 : ffi::DWARF_CompilationUnit_it_functions
125 0 : );
126 :
127 0 : declare_fwd_iterator!(
128 0 : Parameters,
129 0 : Parameter<'a>,
130 0 : ffi::DWARF_Function_Parameter,
131 0 : ffi::DWARF_Function,
132 0 : ffi::DWARF_Function_it_parameters
133 0 : );
|