LIEF: Library to Instrument Executable Formats Version 2.0.0
Loading...
Searching...
No Matches
Location.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_LOCATION_H
16#define LIEF_DWARF_LOCATION_H
17
18#include <cstdint>
19#include <memory>
20#include <optional>
21#include <ostream>
22#include <string>
23#include <utility>
24#include <vector>
25
26#include "LIEF/range.hpp"
27#include "LIEF/visibility.h"
28
29namespace LIEF::dwarf {
30
34 public:
35 enum class Type : uint8_t {
36 UNKNOWN = 0,
38 REG,
39
41 ADDRESS,
42
44 FRAME_BASE,
45
47 REGISTER_OFFSET,
48
50 EXPRESSION,
51
53 UNAVAILABLE,
54
56 COMPOSITE,
57 };
58
59 explicit Location(Type ty) :
60 type(ty) {}
61
62 Location(const Location&) = default;
63 Location& operator=(const Location&) = default;
64
65 Location(Location&&) noexcept = default;
66 Location& operator=(Location&&) noexcept = default;
67
68 virtual ~Location();
69
70 template<class T>
71 const T* as() const LIEF_LIFETIMEBOUND {
72 if (T::classof(this)) {
73 return static_cast<const T*>(this);
74 }
75 return nullptr;
76 }
77
79 std::string to_string() const;
80
81 friend std::ostream& operator<<(std::ostream& os, const Location& loc) {
82 os << loc.to_string();
83 return os;
84 }
85
87};
88
91 public:
92 explicit RegisterLoc(uint64_t reg_id) :
93 Location(Type::REG),
94 id(reg_id) {}
95
96 static bool classof(const Location* loc) {
97 return loc->type == Type::REG;
98 }
99
100 ~RegisterLoc() override;
101
104 uint64_t id = 0;
105};
106
109 public:
110 explicit AddressLoc(uint64_t value) :
111 Location(Type::ADDRESS),
112 address(value) {}
113
114 static bool classof(const Location* loc) {
115 return loc->type == Type::ADDRESS;
116 }
117
118 ~AddressLoc() override;
119
121 uint64_t address = 0;
122};
123
126 public:
127 explicit FrameBaseLoc(int64_t value) :
128 Location(Type::FRAME_BASE),
129 offset(value) {}
130
131 static bool classof(const Location* loc) {
132 return loc->type == Type::FRAME_BASE;
133 }
134
135 ~FrameBaseLoc() override;
136
138 int64_t offset = 0;
139};
140
144 public:
145 RegisterOffsetLoc(uint64_t reg_id, int64_t value) :
146 Location(Type::REGISTER_OFFSET),
147 id(reg_id),
148 offset(value) {}
149
150 static bool classof(const Location* loc) {
151 return loc->type == Type::REGISTER_OFFSET;
152 }
153
155
157 uint64_t id = 0;
158
160 int64_t offset = 0;
161};
162
166 public:
167 ExpressionLoc(std::vector<uint8_t> bytes, std::string text) :
168 Location(Type::EXPRESSION),
169 expression(std::move(bytes)),
170 description(std::move(text)) {}
171
172 static bool classof(const Location* loc) {
173 return loc->type == Type::EXPRESSION;
174 }
175
176 ~ExpressionLoc() override;
177
179 std::vector<uint8_t> expression;
180
183 std::string description;
184};
185
188 public:
190 Location(Type::UNAVAILABLE) {}
191
192 static bool classof(const Location* loc) {
193 return loc->type == Type::UNAVAILABLE;
194 }
195
196 ~UnavailableLoc() override;
197};
198
205 public:
208 enum class KIND : uint8_t {
209 BYTE = 0,
210 BIT,
211 };
212
215
217 uint64_t bit_size = 0;
218
220 uint64_t bit_offset = 0;
221
224 std::optional<uint64_t> source_bit_offset;
225
227 std::unique_ptr<Location> location;
228 };
229
231 Location(Type::COMPOSITE) {}
232
235
237 CompositeLocation& operator=(CompositeLocation&&) noexcept = default;
238
239 static bool classof(const Location* loc) {
240 return loc->type == Type::COMPOSITE;
241 }
242
244
246 std::vector<Piece> pieces;
247};
248
252 std::optional<range_t> range;
253
255 std::optional<uint64_t> section_index;
256
258 std::unique_ptr<Location> location;
259
262 std::string to_string() const;
263
264 friend std::ostream& operator<<(std::ostream& os, const LocationEntry& entry) {
265 os << entry.to_string();
266 return os;
267 }
268};
269
270}
271
272#endif
AddressLoc(uint64_t value)
Definition Location.hpp:110
static bool classof(const Location *loc)
Definition Location.hpp:114
uint64_t address
Memory address where the value is located.
Definition Location.hpp:121
CompositeLocation(const CompositeLocation &)=delete
static bool classof(const Location *loc)
Definition Location.hpp:239
CompositeLocation & operator=(const CompositeLocation &)=delete
CompositeLocation()
Definition Location.hpp:230
std::vector< Piece > pieces
The pieces of this location, ordered by their bit offset.
Definition Location.hpp:246
CompositeLocation(CompositeLocation &&) noexcept=default
std::vector< uint8_t > expression
Raw bytes of the DWARF expression.
Definition Location.hpp:179
std::string description
Textual representation of the expression (e.g. DW_OP_lit0, DW_OP_stack_value).
Definition Location.hpp:183
ExpressionLoc(std::vector< uint8_t > bytes, std::string text)
Definition Location.hpp:167
static bool classof(const Location *loc)
Definition Location.hpp:172
FrameBaseLoc(int64_t value)
Definition Location.hpp:127
int64_t offset
Signed byte offset from the frame base.
Definition Location.hpp:138
static bool classof(const Location *loc)
Definition Location.hpp:131
Location(const Location &)=default
Location & operator=(const Location &)=default
std::string to_string() const
Human-readable description of this location.
Location(Type ty)
Definition Location.hpp:59
Type
Definition Location.hpp:35
@ UNAVAILABLE
The value is not available.
Definition Location.hpp:53
@ FRAME_BASE
The value is at an offset of the frame base.
Definition Location.hpp:44
@ ADDRESS
The value is at a fixed memory address.
Definition Location.hpp:41
@ UNKNOWN
Definition Location.hpp:36
@ COMPOSITE
The value is split in several pieces.
Definition Location.hpp:56
@ REGISTER_OFFSET
The value is at an offset of a register.
Definition Location.hpp:47
@ EXPRESSION
An unevaluated DWARF expression.
Definition Location.hpp:50
@ REG
The value is associated to a register.
Definition Location.hpp:38
Location(Location &&) noexcept=default
Type type
Definition Location.hpp:86
const T * as() const
Definition Location.hpp:71
friend std::ostream & operator<<(std::ostream &os, const Location &loc)
Definition Location.hpp:81
uint64_t id
DWARF id of the register that must be interpreted according to the target architecture.
Definition Location.hpp:104
RegisterLoc(uint64_t reg_id)
Definition Location.hpp:92
static bool classof(const Location *loc)
Definition Location.hpp:96
RegisterOffsetLoc(uint64_t reg_id, int64_t value)
Definition Location.hpp:145
int64_t offset
Signed byte offset added to the register's value.
Definition Location.hpp:160
uint64_t id
DWARF id of the register that contains the base address.
Definition Location.hpp:157
static bool classof(const Location *loc)
Definition Location.hpp:150
UnavailableLoc()
Definition Location.hpp:189
static bool classof(const Location *loc)
Definition Location.hpp:192
#define LIEF_LIFETIMEBOUND
Definition compiler_attributes.hpp:72
Namespace for the DWARF debug format.
Definition DWARF/CompilationUnit.hpp:31
A piece of a composite location.
Definition Location.hpp:207
KIND kind
Whether this piece comes from DW_OP_piece or DW_OP_bit_piece.
Definition Location.hpp:214
std::optional< uint64_t > source_bit_offset
For a DW_OP_bit_piece, the offset (in bits) of this piece within its location (e.g....
Definition Location.hpp:224
KIND
Definition Location.hpp:208
@ BYTE
DW_OP_piece
Definition Location.hpp:209
uint64_t bit_size
Size of this piece in bits.
Definition Location.hpp:217
uint64_t bit_offset
Offset (in bits) of this piece within the whole value.
Definition Location.hpp:220
std::unique_ptr< Location > location
Location of this piece or a nullptr if it can't be decoded.
Definition Location.hpp:227
Location associated with the range of addresses where it is valid.
Definition Location.hpp:250
std::optional< uint64_t > section_index
Index of the object's section containing the range (if any).
Definition Location.hpp:255
friend std::ostream & operator<<(std::ostream &os, const LocationEntry &entry)
Definition Location.hpp:264
std::unique_ptr< Location > location
The location or a nullptr if the DWARF expression can't be decoded.
Definition Location.hpp:258
std::string to_string() const
Human-readable description of this entry (e.g. [0x1130, 0x1136): register 5).
std::optional< range_t > range
Range of addresses [low, high) where the location is valid.
Definition Location.hpp:252
#define LIEF_API
Definition visibility.h:45