LIEF: Library to Instrument Executable Formats Version 0.16.0
Loading...
Searching...
No Matches
DWARF/CompilationUnit.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_COMPILATION_UNIT_H
16#define LIEF_DWARF_COMPILATION_UNIT_H
17#include <memory>
18#include <string>
19#include <vector>
20
21#include "LIEF/visibility.h"
22#include "LIEF/range.hpp"
23#include "LIEF/iterators.hpp"
25#include "LIEF/DWARF/Type.hpp"
26
27namespace LIEF {
28namespace dwarf {
29
30namespace details {
31class CompilationUnit;
32class CompilationUnitIt;
33}
37 public:
38 class LIEF_API Iterator {
39 public:
40 using iterator_category = std::bidirectional_iterator_tag;
41 using value_type = std::unique_ptr<CompilationUnit>;
42 using difference_type = std::ptrdiff_t;
43 using pointer = CompilationUnit*;
44 using reference = std::unique_ptr<CompilationUnit>&;
45 using implementation = details::CompilationUnitIt;
46
47 class LIEF_API PointerProxy {
48 // Inspired from LLVM's iterator_facade_base
49 friend class Iterator;
50 public:
51 pointer operator->() const { return R.get(); }
52
53 private:
54 value_type R;
55
56 template <typename RefT>
57 PointerProxy(RefT &&R) : R(std::forward<RefT>(R)) {} // NOLINT(bugprone-forwarding-reference-overload)
58 };
59
60 Iterator(const Iterator&);
61 Iterator(Iterator&&) noexcept;
62 Iterator(std::unique_ptr<details::CompilationUnitIt> impl);
63 ~Iterator();
64
65 friend LIEF_API bool operator==(const Iterator& LHS, const Iterator& RHS);
66 friend LIEF_API bool operator!=(const Iterator& LHS, const Iterator& RHS) {
67 return !(LHS == RHS);
68 }
69
70 Iterator& operator++();
71 Iterator& operator--();
72
73 Iterator operator--(int) {
74 Iterator tmp = *static_cast<Iterator*>(this);
75 --*static_cast<Iterator *>(this);
76 return tmp;
77 }
78
79 Iterator operator++(int) {
80 Iterator tmp = *static_cast<Iterator*>(this);
81 ++*static_cast<Iterator *>(this);
82 return tmp;
83 }
84
85 std::unique_ptr<CompilationUnit> operator*() const;
86
87 PointerProxy operator->() const {
88 return static_cast<const Iterator*>(this)->operator*();
89 }
90
91 private:
92 std::unique_ptr<details::CompilationUnitIt> impl_;
93 };
94 using functions_it = iterator_range<Function::Iterator>;
97 using types_it = iterator_range<Type::Iterator>;
100 using vars_it = iterator_range<Variable::Iterator>;
103 class Language {
110 public:
111 enum LANG : uint32_t {
112 UNKNOWN = 0,
113 C,
114 CPP,
115 RUST,
116 DART,
117 MODULA,
118 FORTRAN,
119 SWIFT,
120 D,
121 JAVA,
122 COBOL,
123 };
124 LANG lang = UNKNOWN;
127 uint32_t version = 0;
130
131 Language() = default;
132 Language(LANG lang, uint32_t version) :
133 lang(lang), version(version)
134 {}
135 Language(LANG lang) :
136 Language(lang, 0)
137 {}
138
139 Language(const Language&) = default;
140 Language& operator=(const Language&) = default;
141 Language(Language&&) = default;
142 Language& operator=(Language&&) = default;
143 ~Language() = default;
144 };
145 CompilationUnit(std::unique_ptr<details::CompilationUnit> impl);
147 std::string name() const;
153 std::string producer() const;
162 std::string compilation_dir() const;
171 Language language() const;
176 uint64_t low_address() const;
179 uint64_t high_address() const;
182 uint64_t size() const;
192 std::vector<range_t> ranges() const;
198 std::unique_ptr<Function> find_function(const std::string& name) const;
203 std::unique_ptr<Function> find_function(uint64_t addr) const;
206 std::unique_ptr<Variable> find_variable(uint64_t addr) const;
209 std::unique_ptr<Variable> find_variable(const std::string& name) const;
212 functions_it functions() const;
236 functions_it imported_functions() const;
255 types_it types() const;
259
260 vars_it variables() const;
274
275 private:
276 std::unique_ptr<details::CompilationUnit> impl_;
277};
278
279}
280}
281#endif
282
pointer operator->() const
Definition DWARF/CompilationUnit.hpp:51
Iterator(const Iterator &)
Iterator & operator--()
Iterator(Iterator &&) noexcept
Iterator & operator++()
Iterator operator--(int)
Definition DWARF/CompilationUnit.hpp:73
std::unique_ptr< CompilationUnit > operator*() const
Iterator operator++(int)
Definition DWARF/CompilationUnit.hpp:79
PointerProxy operator->() const
Definition DWARF/CompilationUnit.hpp:87
Language()=default
Language(LANG lang)
Definition DWARF/CompilationUnit.hpp:135
Language(Language &&)=default
Language(LANG lang, uint32_t version)
Definition DWARF/CompilationUnit.hpp:132
Language & operator=(Language &&)=default
~Language()=default
Language(const Language &)=default
Language & operator=(const Language &)=default
This class represents a DWARF compilation unit.
Definition DWARF/CompilationUnit.hpp:36
uint64_t high_address() const
Return the highest virtual address owned by this compilation unit.
vars_it variables() const
Return an iterator over all the variables defined in the this compilation unit:
types_it types() const
Return an iterator over the different types defined in this compilation unit.
Language language() const
Original Language of this compilation unit.
std::unique_ptr< Variable > find_variable(const std::string &name) const
Try to find the Variable with the given name.
CompilationUnit(std::unique_ptr< details::CompilationUnit > impl)
~CompilationUnit()
std::unique_ptr< Function > find_function(uint64_t addr) const
Try to find the function at the given address.
uint64_t size() const
Return the size of the compilation unit according to its range of address.
functions_it imported_functions() const
Return an iterator over the functions imported in this compilation unit but not implemented.
uint64_t low_address() const
Return the lowest virtual address owned by this compilation unit.
std::string name() const
Name of the file associated with this compilation unit (e.g. test.cpp) Return an empty string if the ...
std::string compilation_dir() const
Return the path to the directory in which the compilation took place for compiling this compilation u...
functions_it functions() const
Return an iterator over the functions implemented in this compilation unit.
std::unique_ptr< Function > find_function(const std::string &name) const
Try to find the function whose name is given in parameter.
std::unique_ptr< Variable > find_variable(uint64_t addr) const
Try to find the Variable at the given address.
std::string producer() const
Information about the program (or library) that generated this compilation unit. For instance,...
std::vector< range_t > ranges() const
Return a list of address ranges owned by this compilation unit.
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