LIEF: Library to Instrument Executable Formats Version 1.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 <memory>
18#include <string>
19#include <vector>
20
22#include "LIEF/visibility.h"
23#include "LIEF/range.hpp"
24#include "LIEF/iterators.hpp"
25#include "LIEF/DebugDeclOpt.hpp"
27#include "LIEF/DWARF/Type.hpp"
28
29namespace LIEF {
30namespace dwarf {
31
32namespace details {
33class CompilationUnit;
34class CompilationUnitIt;
35}
36
39 public:
40 class Iterator final
41 : public iterator_facade_base<Iterator, std::bidirectional_iterator_tag,
42 CompilationUnit, std::ptrdiff_t,
43 const CompilationUnit*, const CompilationUnit&> {
44 public:
45 using implementation = details::CompilationUnitIt;
46 using iterator_facade_base::operator++;
47 using iterator_facade_base::operator--;
48
50
51 LIEF_API Iterator(std::unique_ptr<details::CompilationUnitIt> impl);
52
55
57 LIEF_API Iterator& operator=(Iterator&&) noexcept;
58
60
61 friend LIEF_API bool operator==(const Iterator& LHS, const Iterator& RHS);
62 friend bool operator!=(const Iterator& LHS, const Iterator& RHS) {
63 return !(LHS == RHS);
64 }
65
66 // NOLINTNEXTLINE(bugprone-derived-method-shadowing-base-method)
68
69 // NOLINTNEXTLINE(bugprone-derived-method-shadowing-base-method)
71
73
74 // NOLINTNEXTLINE(bugprone-derived-method-shadowing-base-method)
75 LIEF_API const CompilationUnit* operator->() const LIEF_LIFETIMEBOUND;
76
79 LIEF_API std::unique_ptr<CompilationUnit> yield();
80
81 private:
82 void load() const;
83
84 std::unique_ptr<details::CompilationUnitIt> impl_;
85 mutable std::unique_ptr<CompilationUnit> cached_;
86 };
87
90
93
96
102 class Language {
103 public:
117
120
122 uint32_t version = 0;
123
124 Language() = default;
126 lang(lang),
127 version(version) {}
130
131 Language(const Language&) = default;
132 Language& operator=(const Language&) = default;
133 Language(Language&&) = default;
135 ~Language() = default;
136 };
137 CompilationUnit(std::unique_ptr<details::CompilationUnit> impl);
139
144 std::string name() const;
145
153 std::string producer() const;
154
162 std::string compilation_dir() const;
163
168
170 uint64_t low_address() const;
171
173 uint64_t high_address() const;
174
183 uint64_t size() const;
184
189 std::vector<range_t> ranges() const;
190
194 std::unique_ptr<Function>
195 find_function(const std::string& name) const LIEF_LIFETIMEBOUND;
196
198 std::unique_ptr<Function> find_function(uint64_t addr) const LIEF_LIFETIMEBOUND;
199
201 std::unique_ptr<Variable> find_variable(uint64_t addr) const LIEF_LIFETIMEBOUND;
202
204 std::unique_ptr<Variable>
205 find_variable(const std::string& name) const LIEF_LIFETIMEBOUND;
206
230
249
253
267
270 std::string to_decl(const DeclOpt& opt = DeclOpt()) const;
271
272 private:
273 std::unique_ptr<details::CompilationUnit> impl_;
274};
275
276}
277}
278#endif
Configuration options for generated code from debug info.
Definition DebugDeclOpt.hpp:36
Definition DWARF/CompilationUnit.hpp:43
const CompilationUnit & operator*() const
Iterator(std::unique_ptr< details::CompilationUnitIt > impl)
details::CompilationUnitIt implementation
Definition DWARF/CompilationUnit.hpp:45
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:102
Language(LANG lang)
Definition DWARF/CompilationUnit.hpp:128
Language(LANG lang, uint32_t version)
Definition DWARF/CompilationUnit.hpp:125
Language & operator=(Language &&)=default
LANG lang
The language itself.
Definition DWARF/CompilationUnit.hpp:119
uint32_t version
Version of the language (e.g. 17 for C++17).
Definition DWARF/CompilationUnit.hpp:122
LANG
Definition DWARF/CompilationUnit.hpp:104
@ D
Definition DWARF/CompilationUnit.hpp:113
@ DART
Definition DWARF/CompilationUnit.hpp:109
@ RUST
Definition DWARF/CompilationUnit.hpp:108
@ C
Definition DWARF/CompilationUnit.hpp:106
@ COBOL
Definition DWARF/CompilationUnit.hpp:115
@ SWIFT
Definition DWARF/CompilationUnit.hpp:112
@ UNKNOWN
Definition DWARF/CompilationUnit.hpp:105
@ CPP
Definition DWARF/CompilationUnit.hpp:107
@ FORTRAN
Definition DWARF/CompilationUnit.hpp:111
@ JAVA
Definition DWARF/CompilationUnit.hpp:114
@ MODULA
Definition DWARF/CompilationUnit.hpp:110
Language(const Language &)=default
Language & operator=(const Language &)=default
This class represents a DWARF compilation unit.
Definition DWARF/CompilationUnit.hpp:38
iterator_range< Variable::Iterator > vars_it
Iterator over the CompilationUnit's variables.
Definition DWARF/CompilationUnit.hpp:95
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.
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)
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:89
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.
iterator_range< Type::Iterator > types_it
Iterator over the dwarf::Type.
Definition DWARF/CompilationUnit.hpp:92
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.
This class represents a DWARF function which can be associated with either: DW_TAG_subprogram or DW_T...
Definition DWARF/Function.hpp:45
This class represents a DWARF Type which includes:
Definition DWARF/Type.hpp:67
This class represents a DWARF variable which can be owned by a dwarf::Function or a dwarf::Compilatio...
Definition Variable.hpp:38
CRTP base class which implements the entire standard iterator facade in terms of a minimal subset of ...
Definition iterators.hpp:729
Definition iterators.hpp:601
#define LIEF_LIFETIMEBOUND
Definition compiler_attributes.hpp:72
Definition DWARF/CompilationUnit.hpp:32
Namespace for the DWARF debug format.
Definition DWARF/CompilationUnit.hpp:30
LIEF namespace.
Definition Abstract/Binary.hpp:41
#define LIEF_API
Definition visibility.h:45