LIEF: Library to Instrument Executable Formats Version 2.0.0
Loading...
Searching...
No Matches
DWARF/CompilationUnit.hpp
Go to the documentation of this file.
1/* Copyright 2022 - 2026 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 <string_view>
18#include <memory>
19#include <string>
20#include <vector>
21
23#include "LIEF/DWARF/Type.hpp"
24#include "LIEF/DebugDeclOpt.hpp"
26#include "LIEF/iterators.hpp"
27#include "LIEF/range.hpp"
28#include "LIEF/visibility.h"
29
30
31namespace LIEF::dwarf {
32
33namespace details {
34class CompilationUnit;
35class CompilationUnitIt;
36}
37
40 public:
41 class Iterator final
42 : public iterator_facade_base<Iterator, std::bidirectional_iterator_tag,
43 CompilationUnit, std::ptrdiff_t,
44 const CompilationUnit*, const CompilationUnit&> {
45 public:
46 using implementation = details::CompilationUnitIt;
47 using iterator_facade_base::operator++;
48 using iterator_facade_base::operator--;
49
51
52 LIEF_API Iterator(std::unique_ptr<details::CompilationUnitIt> impl);
53
56
58 LIEF_API Iterator& operator=(Iterator&&) noexcept;
59
61
62 friend LIEF_API bool operator==(const Iterator& LHS, const Iterator& RHS);
63 friend bool operator!=(const Iterator& LHS, const Iterator& RHS) {
64 return !(LHS == RHS);
65 }
66
67 // NOLINTNEXTLINE(bugprone-derived-method-shadowing-base-method)
69
70 // NOLINTNEXTLINE(bugprone-derived-method-shadowing-base-method)
72
74
75 // NOLINTNEXTLINE(bugprone-derived-method-shadowing-base-method)
76 LIEF_API const CompilationUnit* operator->() const LIEF_LIFETIMEBOUND;
77
80 LIEF_API std::unique_ptr<CompilationUnit> yield();
81
82 private:
83 void load() const;
84
85 std::unique_ptr<details::CompilationUnitIt> impl_;
86 mutable std::unique_ptr<CompilationUnit> cached_;
87 };
88
91
94
97
103 class Language {
104 public:
118
121
123 uint32_t version = 0;
124
125 Language() = default;
127 lang(lang),
128 version(version) {}
131
132 Language(const Language&) = default;
133 Language& operator=(const Language&) = default;
134 Language(Language&&) = default;
136 ~Language() = default;
137 };
138 CompilationUnit(std::unique_ptr<details::CompilationUnit> impl);
140
145 std::string_view name() const LIEF_LIFETIMEBOUND;
146
154 std::string_view producer() const LIEF_LIFETIMEBOUND;
155
163 std::string_view compilation_dir() const LIEF_LIFETIMEBOUND;
164
169
171 uint64_t low_address() const;
172
174 uint64_t high_address() const;
175
184 uint64_t size() const;
185
190 std::vector<range_t> ranges() const;
191
195 std::unique_ptr<Function>
196 find_function(std::string_view name) const LIEF_LIFETIMEBOUND;
197
199 std::unique_ptr<Function> find_function(uint64_t addr) const LIEF_LIFETIMEBOUND;
200
202 std::unique_ptr<Variable> find_variable(uint64_t addr) const LIEF_LIFETIMEBOUND;
203
205 std::unique_ptr<Variable>
206 find_variable(std::string_view name) const LIEF_LIFETIMEBOUND;
207
231
250
254
268
271 std::string to_decl(const DeclOpt& opt = DeclOpt()) const;
272
273 private:
274 std::unique_ptr<details::CompilationUnit> impl_;
275};
276
277}
278
279#endif
Configuration options for generated code from debug info.
Definition DebugDeclOpt.hpp:37
Definition DWARF/CompilationUnit.hpp:44
const CompilationUnit & operator*() const
Iterator(std::unique_ptr< details::CompilationUnitIt > impl)
details::CompilationUnitIt implementation
Definition DWARF/CompilationUnit.hpp:46
std::unique_ptr< CompilationUnit > yield()
Transfer ownership of the compilation unit at the current position to the caller. Returns nullptr if ...
Iterator & operator=(const Iterator &)
Languages supported by the DWARF (v5) format. See: https://dwarfstd.org/languages....
Definition DWARF/CompilationUnit.hpp:103
Language(LANG lang)
Definition DWARF/CompilationUnit.hpp:129
Language(LANG lang, uint32_t version)
Definition DWARF/CompilationUnit.hpp:126
Language & operator=(Language &&)=default
LANG lang
The language itself.
Definition DWARF/CompilationUnit.hpp:120
uint32_t version
Version of the language (e.g. 17 for C++17).
Definition DWARF/CompilationUnit.hpp:123
LANG
Definition DWARF/CompilationUnit.hpp:105
@ D
Definition DWARF/CompilationUnit.hpp:114
@ DART
Definition DWARF/CompilationUnit.hpp:110
@ RUST
Definition DWARF/CompilationUnit.hpp:109
@ C
Definition DWARF/CompilationUnit.hpp:107
@ COBOL
Definition DWARF/CompilationUnit.hpp:116
@ SWIFT
Definition DWARF/CompilationUnit.hpp:113
@ UNKNOWN
Definition DWARF/CompilationUnit.hpp:106
@ CPP
Definition DWARF/CompilationUnit.hpp:108
@ FORTRAN
Definition DWARF/CompilationUnit.hpp:112
@ JAVA
Definition DWARF/CompilationUnit.hpp:115
@ MODULA
Definition DWARF/CompilationUnit.hpp:111
Language(const Language &)=default
Language & operator=(const Language &)=default
This class represents a DWARF compilation unit.
Definition DWARF/CompilationUnit.hpp:39
iterator_range< Variable::Iterator > vars_it
Iterator over the CompilationUnit's variables.
Definition DWARF/CompilationUnit.hpp:96
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 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.
CompilationUnit(std::unique_ptr< details::CompilationUnit > impl)
std::string to_decl(const DeclOpt &opt=DeclOpt()) const
Generate a C/C++ definition for the functions defined in this compilation unit.
iterator_range< Function::Iterator > functions_it
Iterator over the dwarf::Function.
Definition DWARF/CompilationUnit.hpp:90
std::unique_ptr< Function > find_function(std::string_view name) const
Try to find the function whose name is given in parameter.
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.
iterator_range< Type::Iterator > types_it
Iterator over the dwarf::Type.
Definition DWARF/CompilationUnit.hpp:93
std::string_view 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< Variable > find_variable(uint64_t addr) const
Try to find the Variable at the given address.
std::string_view name() const
Name of the file associated with this compilation unit (e.g. test.cpp) Return an empty string if the ...
std::string_view 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.
This class represents a DWARF function which can be associated with either: DW_TAG_subprogram or DW_T...
Definition DWARF/Function.hpp:46
This class represents a DWARF Type which includes:
Definition DWARF/Type.hpp:68
This class represents a DWARF variable which can be owned by a dwarf::Function or a dwarf::Compilatio...
Definition Variable.hpp:41
Definition iterators.hpp:595
#define LIEF_LIFETIMEBOUND
Definition compiler_attributes.hpp:72
Definition DWARF/CompilationUnit.hpp:33
Namespace for the DWARF debug format.
Definition DWARF/CompilationUnit.hpp:31
Definition range.hpp:23
#define LIEF_API
Definition visibility.h:45