LIEF: Library to Instrument Executable Formats Version 0.17.0
Loading...
Searching...
No Matches
DWARF/Function.hpp
Go to the documentation of this file.
1/* Copyright 2022 - 2024 R. Thomas
2 *
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
6 *
7 * http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15#ifndef LIEF_DWARF_FUNCTION_H
16#define LIEF_DWARF_FUNCTION_H
17
18#include <memory>
19#include <string>
20
21#include "LIEF/visibility.h"
22#include "LIEF/errors.hpp"
23#include "LIEF/iterators.hpp"
24#include "LIEF/range.hpp"
26#include "LIEF/DWARF/Type.hpp"
28
29namespace LIEF {
30namespace dwarf {
31
32class Scope;
33class Parameter;
34
35namespace details {
36class Function;
37class FunctionIt;
38}
39class LIEF_API Function {
43 public:
44 class LIEF_API Iterator {
45 public:
46 using iterator_category = std::bidirectional_iterator_tag;
47 using value_type = std::unique_ptr<Function>;
48 using difference_type = std::ptrdiff_t;
49 using pointer = Function*;
50 using reference = std::unique_ptr<Function>&;
51 using implementation = details::FunctionIt;
52
53 class LIEF_API PointerProxy {
54 // Inspired from LLVM's iterator_facade_base
55 friend class Iterator;
56 public:
57 pointer operator->() const { return R.get(); }
58
59 private:
60 value_type R;
61
62 template <typename RefT>
63 PointerProxy(RefT &&R) : R(std::forward<RefT>(R)) {} // NOLINT(bugprone-forwarding-reference-overload)
64 };
65
66 Iterator(const Iterator&);
67 Iterator(Iterator&&) noexcept;
68 Iterator(std::unique_ptr<details::FunctionIt> impl);
69 ~Iterator();
70
71 friend LIEF_API bool operator==(const Iterator& LHS, const Iterator& RHS);
72
73 friend LIEF_API bool operator!=(const Iterator& LHS, const Iterator& RHS) {
74 return !(LHS == RHS);
75 }
76
77 Iterator& operator++();
78 Iterator& operator--();
79
80 Iterator operator--(int) {
81 Iterator tmp = *static_cast<Iterator*>(this);
82 --*static_cast<Iterator *>(this);
83 return tmp;
84 }
85
86 Iterator operator++(int) {
87 Iterator tmp = *static_cast<Iterator*>(this);
88 ++*static_cast<Iterator *>(this);
89 return tmp;
90 }
91
92 std::unique_ptr<Function> operator*() const;
93
94 PointerProxy operator->() const {
95 return static_cast<const Iterator*>(this)->operator*();
96 }
97
98 private:
99 std::unique_ptr<details::FunctionIt> impl_;
100 };
101 using vars_it = iterator_range<Variable::Iterator>;
104 using parameters_t = std::vector<std::unique_ptr<Parameter>>;
105 using thrown_types_t = std::vector<std::unique_ptr<Type>>;
106
107 using instructions_it = iterator_range<assembly::Instruction::Iterator>;
108
109 Function(std::unique_ptr<details::Function> impl);
110 std::string name() const;
113 std::string linkage_name() const;
119 result<uint64_t> address() const;
122 vars_it variables() const;
127 bool is_artificial() const;
131 bool is_external() const;
135 uint64_t size() const;
138 std::vector<range_t> ranges() const;
141 debug_location_t debug_location() const;
144 std::unique_ptr<Type> type() const;
148 parameters_t parameters() const;
151 thrown_types_t thrown_types() const;
165 std::unique_ptr<Scope> scope() const;
168 instructions_it instructions() const;
172
174 private:
175 std::unique_ptr<details::Function> impl_;
176};
177
178}
179}
180#endif
pointer operator->() const
Definition DWARF/Function.hpp:57
Iterator operator++(int)
Definition DWARF/Function.hpp:86
std::unique_ptr< Function > operator*() const
Iterator(Iterator &&) noexcept
Iterator operator--(int)
Definition DWARF/Function.hpp:80
PointerProxy operator->() const
Definition DWARF/Function.hpp:94
This class represents a DWARF function which can be associated with either: DW_TAG_subprogram or DW_T...
Definition DWARF/Function.hpp:42
instructions_it instructions() const
Disassemble the current function by returning an iterator over the assembly::Instruction.
vars_it variables() const
Return an iterator of variables (DW_TAG_variable) defined within the scope of this function....
thrown_types_t thrown_types() const
List of exceptions (types) that can be thrown by the function.
std::unique_ptr< Type > type() const
Return the dwarf::Type associated with the return type of this function.
std::unique_ptr< Scope > scope() const
Return the scope in which this function is defined.
Function(std::unique_ptr< details::Function > impl)
std::string name() const
The name of the function (DW_AT_name)
bool is_external() const
Whether the function is defined outside the current compilation unit (DW_AT_external).
result< uint64_t > address() const
Return the address of the function (DW_AT_entry_pc or DW_AT_low_pc).
debug_location_t debug_location() const
Original source code location.
std::vector< range_t > ranges() const
Ranges of virtual addresses owned by this function.
parameters_t parameters() const
Return the function's parameters (including any template parameter)
uint64_t size() const
Return the size taken by this function in the binary.
std::string linkage_name() const
The name of the function which is used for linking (DW_AT_linkage_name).
bool is_artificial() const
Whether this function is created by the compiler and not present in the original source code.
This class represents a DWARF parameter which can be either:
Definition Parameter.hpp:36
This class materializes a scope in which Function, Variable, Type, ... can be defined.
Definition Scope.hpp:32
Definition DWARF/CompilationUnit.hpp:30
Namespace for the DWARF debug format.
Definition DWARF/CompilationUnit.hpp:28
LIEF namespace.
Definition Abstract/Binary.hpp:36
#define LIEF_API
Definition visibility.h:41