LLDB mainline
Symbol.h
Go to the documentation of this file.
1//===-- Symbol.h ------------------------------------------------*- C++ -*-===//
2//
3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6//
7//===----------------------------------------------------------------------===//
8
9#ifndef LLDB_SYMBOL_SYMBOL_H
10#define LLDB_SYMBOL_SYMBOL_H
11
13#include "lldb/Core/Mangled.h"
14#include "lldb/Core/Section.h"
16#include "lldb/Utility/Stream.h"
17#include "lldb/Utility/UserID.h"
19#include "lldb/lldb-private.h"
20#include "llvm/Support/JSON.h"
21
22namespace lldb_private {
23
24struct JSONSymbol {
25 std::optional<uint64_t> address;
26 std::optional<uint64_t> value;
27 std::optional<uint64_t> size;
28 std::optional<uint64_t> id;
29 std::optional<lldb::SymbolType> type;
30 std::string name;
31};
32
33class Symbol : public SymbolContextScope {
34public:
35 // ObjectFile readers can classify their symbol table entries and searches
36 // can be made on specific types where the symbol values will have
37 // drastically different meanings and sorting requirements.
38 Symbol();
39
40 Symbol(uint32_t symID, llvm::StringRef name, lldb::SymbolType type,
41 bool external, bool is_debug, bool is_trampoline, bool is_artificial,
42 const lldb::SectionSP &section_sp, lldb::addr_t value,
43 lldb::addr_t size, bool size_is_valid,
44 bool contains_linker_annotations, uint32_t flags);
45
46 Symbol(uint32_t symID, const Mangled &mangled, lldb::SymbolType type,
47 bool external, bool is_debug, bool is_trampoline, bool is_artificial,
48 const AddressRange &range, bool size_is_valid,
49 bool contains_linker_annotations, uint32_t flags);
50
51 Symbol(const Symbol &rhs);
52
53 const Symbol &operator=(const Symbol &rhs);
54
55 static llvm::Expected<Symbol> FromJSON(const JSONSymbol &symbol,
56 SectionList *section_list);
57
58 void Clear();
59
60 bool Compare(ConstString name, lldb::SymbolType type) const;
61
62 void Dump(Stream *s, Target *target, uint32_t index,
63 Mangled::NamePreference name_preference =
65
66 bool ValueIsAddress() const;
67
68 // The GetAddressRef() accessor functions should only be called if you
69 // previously call ValueIsAddress() otherwise you might get an reference to
70 // an Address object that contains an constant integer value in
71 // m_addr_range.m_base_addr.m_offset which could be incorrectly used to
72 // represent an absolute address since it has no section.
73 Address &GetAddressRef() { return m_addr_range.GetBaseAddress(); }
74
75 const Address &GetAddressRef() const { return m_addr_range.GetBaseAddress(); }
76
77 // Makes sure the symbol's value is an address and returns the file address.
78 // Returns LLDB_INVALID_ADDRESS if the symbol's value isn't an address.
80
81 // Makes sure the symbol's value is an address and gets the load address
82 // using \a target if it is. Returns LLDB_INVALID_ADDRESS if the symbol's
83 // value isn't an address or if the section isn't loaded in \a target.
84 lldb::addr_t GetLoadAddress(Target *target) const;
85
86 // Access the address value. Do NOT hand out the AddressRange as an object as
87 // the byte size of the address range may not be filled in and it should be
88 // accessed via GetByteSize().
90 // Make sure the our value is an address before we hand a copy out. We use
91 // the Address inside m_addr_range to contain the value for symbols that
92 // are not address based symbols so we are using it for more than just
93 // addresses. For example undefined symbols on MacOSX have a nlist.n_value
94 // of 0 (zero) and this will get placed into
95 // m_addr_range.m_base_addr.m_offset and it will have no section. So in the
96 // GetAddress() accessor, we need to hand out an invalid address if the
97 // symbol's value isn't an address.
98 if (ValueIsAddress())
99 return m_addr_range.GetBaseAddress();
100 else
101 return Address();
102 }
103
104 /// Get the raw value of the symbol from the symbol table.
105 ///
106 /// If the symbol's value is an address, return the file address, else return
107 /// the raw value that is stored in the m_addr_range. If the base address has
108 /// no section, then getting the file address will return the correct value
109 /// as it will return the offset in the base address which is the value.
110 uint64_t GetRawValue() const {
111 return m_addr_range.GetBaseAddress().GetFileAddress();
112 }
113
114 // When a symbol's value isn't an address, we need to access the raw value.
115 // This function will ensure this symbol's value isn't an address and return
116 // the integer value if this checks out, otherwise it will return
117 // "fail_value" if the symbol is an address value.
118 uint64_t GetIntegerValue(uint64_t fail_value = 0) const {
119 if (ValueIsAddress()) {
120 // This symbol's value is an address. Use Symbol::GetAddress() to get the
121 // address.
122 return fail_value;
123 } else {
124 // The value is stored in the base address' offset
125 return m_addr_range.GetBaseAddress().GetOffset();
126 }
127 }
128
130
131 ConstString GetName() const;
132
134
136
137 uint32_t GetID() const { return m_uid; }
138
140 // TODO: See if there is a way to determine the language for a symbol
141 // somehow, for now just return our best guess
142 return GetMangled().GuessLanguage();
143 }
144
145 void SetID(uint32_t uid) { m_uid = uid; }
146
149 return m_mangled;
150 }
151
152 const Mangled &GetMangled() const {
154 return m_mangled;
155 }
156
158
160
162
164
165 Symbol *ResolveReExportedSymbol(Target &target) const;
166
167 uint32_t GetSiblingIndex() const;
168
170
171 void SetType(lldb::SymbolType type) { m_type = type; }
172
173 const char *GetTypeAsString() const;
174
175 uint32_t GetFlags() const { return m_flags; }
176
177 void SetFlags(uint32_t flags) { m_flags = flags; }
178
179 void GetDescription(
180 Stream *s, lldb::DescriptionLevel level, Target *target,
181 std::optional<Stream::HighlightSettings> settings = std::nullopt) const;
182
183 bool IsSynthetic() const { return m_is_synthetic; }
184
186
187 void SetIsSynthetic(bool b) { m_is_synthetic = b; }
188
190
192
193 bool IsDebug() const { return m_is_debug; }
194
195 void SetDebug(bool b) { m_is_debug = b; }
196
197 bool IsExternal() const { return m_is_external; }
198
199 void SetExternal(bool b) { m_is_external = b; }
200
201 bool IsTrampoline() const;
202
203 bool IsIndirect() const;
204
205 bool IsWeak() const { return m_is_weak; }
206
207 void SetIsWeak(bool b) { m_is_weak = b; }
208
209 bool GetByteSizeIsValid() const { return m_size_is_valid; }
210
212
214 m_size_is_valid = size > 0;
215 m_addr_range.SetByteSize(size);
216 }
217
218 bool GetSizeIsSibling() const { return m_size_is_sibling; }
219
221
222 // If m_type is "Code" or "Function" then this will return the prologue size
223 // in bytes, else it will return zero.
224 uint32_t GetPrologueByteSize();
225
229
231
238 /// \copydoc SymbolContextScope::CalculateSymbolContext(SymbolContext*)
239 ///
240 /// \see SymbolContextScope
241 void CalculateSymbolContext(SymbolContext *sc) override;
242
244
246
247 /// \copydoc SymbolContextScope::DumpSymbolContext(Stream*)
248 ///
249 /// \see SymbolContextScope
250 void DumpSymbolContext(Stream *s) override;
251
253 const char *flavor,
254 bool prefer_file_cache);
255
256 bool GetDisassembly(const ExecutionContext &exe_ctx, const char *flavor,
257 bool prefer_file_cache, Stream &strm);
258
259 bool ContainsFileAddress(lldb::addr_t file_addr) const;
260
261 static llvm::StringRef GetSyntheticSymbolPrefix() {
262 return "___lldb_unnamed_symbol_";
263 }
264
265 /// Decode a serialized version of this object from data.
266 ///
267 /// \param data
268 /// The decoder object that references the serialized data.
269 ///
270 /// \param offset_ptr
271 /// A pointer that contains the offset from which the data will be decoded
272 /// from that gets updated as data gets decoded.
273 ///
274 /// \param section_list
275 /// A section list that allows lldb_private::Address objects to be filled
276 /// in. The address information for symbols are serilized as file addresses
277 /// and must be converted into Address objects with the right section and
278 /// offset.
279 ///
280 /// \param strtab
281 /// All strings in cache files are put into string tables for efficiency
282 /// and cache file size reduction. Strings are stored as uint32_t string
283 /// table offsets in the cache data.
284 ///
285 /// \return
286 /// True if the symbol is successfully decoded, false otherwise.
287 bool Decode(const DataExtractor &data, lldb::offset_t *offset_ptr,
288 const SectionList *section_list, const StringTableReader &strtab);
289
290 /// Encode this object into a data encoder object.
291 ///
292 /// This allows this object to be serialized to disk.
293 ///
294 /// \param encoder
295 /// A data encoder object that serialized bytes will be encoded into.
296 ///
297 /// \param strtab
298 /// All strings in cache files are put into string tables for efficiency
299 /// and cache file size reduction. Strings are stored as uint32_t string
300 /// table offsets in the cache data.
301 void Encode(DataEncoder &encoder, ConstStringTable &strtab) const;
302
303 bool operator==(const Symbol &rhs) const;
304
305 static const char *GetTypeAsString(lldb::SymbolType symbol_type);
306
307 static lldb::SymbolType GetTypeFromString(const char *str);
308
309protected:
310 // This is the internal guts of ResolveReExportedSymbol, it assumes
311 // reexport_name is not null, and that module_spec is valid. We track the
312 // modules we've already seen to make sure we don't get caught in a cycle.
313
315 Target &target, ConstString &reexport_name,
316 lldb_private::ModuleSpec &module_spec,
317 lldb_private::ModuleList &seen_modules) const;
318
319 void SynthesizeNameIfNeeded() const;
320
321 uint32_t m_uid =
322 UINT32_MAX; // User ID (usually the original symbol table index)
323 uint16_t m_type_data = 0; // data specific to m_type
324 uint16_t m_type_data_resolved : 1, // True if the data in m_type_data has
325 // already been calculated
326 m_is_synthetic : 1, // non-zero if this symbol is not actually in the
327 // symbol table, but synthesized from other info in
328 // the object file.
329 m_is_debug : 1, // non-zero if this symbol is debug information in a
330 // symbol
331 m_is_external : 1, // non-zero if this symbol is globally visible
332 m_size_is_sibling : 1, // m_size contains the index of this symbol's
333 // sibling
334 m_size_is_synthesized : 1, // non-zero if this symbol's size was
335 // calculated using a delta between this
336 // symbol and the next
338 m_demangled_is_synthesized : 1, // The demangled name was created should
339 // not be used for expressions or other
340 // lookups
341 m_contains_linker_annotations : 1, // The symbol name contains linker
342 // annotations, which are optional when
343 // doing name lookups
345 m_type : 6; // Values from the lldb::SymbolType enum.
346 mutable Mangled m_mangled; // uniqued symbol name/mangled name pair
347 AddressRange m_addr_range; // Contains the value, or the section offset
348 // address when the value is an address in a
349 // section, and the size (if any)
350 uint32_t m_flags = 0; // A copy of the flags from the original symbol table,
351 // the ObjectFile plug-in can interpret these
352};
353
354} // namespace lldb_private
355
356namespace llvm {
357namespace json {
358
359bool fromJSON(const llvm::json::Value &value, lldb_private::JSONSymbol &symbol,
360 llvm::json::Path path);
361
362bool fromJSON(const llvm::json::Value &value, lldb::SymbolType &type,
363 llvm::json::Path path);
364
365} // namespace json
366} // namespace llvm
367
368#endif // LLDB_SYMBOL_SYMBOL_H
A section + offset based address range class.
A section + offset based address class.
Definition Address.h:62
Many cache files require string tables to store data efficiently.
A uniqued constant string class.
Definition ConstString.h:40
An binary data encoding class.
Definition DataEncoder.h:42
An data extractor class.
"lldb/Target/ExecutionContext.h" A class that contains an execution context.
A file utility class.
Definition FileSpec.h:57
A class that handles mangled names.
Definition Mangled.h:34
lldb::LanguageType GuessLanguage() const
Try to guess the language from the mangling.
Definition Mangled.cpp:425
A collection class for Module objects.
Definition ModuleList.h:104
A stream class that can stream formatted output to a file.
Definition Stream.h:28
Many cache files require string tables to store data efficiently.
"lldb/Symbol/SymbolContextScope.h" Inherit from this if your object is part of a symbol context and c...
Defines a symbol context baton that can be handed other debug core functions.
bool Decode(const DataExtractor &data, lldb::offset_t *offset_ptr, const SectionList *section_list, const StringTableReader &strtab)
Decode a serialized version of this object from data.
Definition Symbol.cpp:612
uint32_t GetSiblingIndex() const
Definition Symbol.cpp:217
uint16_t m_is_external
Definition Symbol.h:331
uint64_t GetIntegerValue(uint64_t fail_value=0) const
Definition Symbol.h:118
uint32_t GetID() const
Definition Symbol.h:137
lldb::addr_t GetLoadAddress(Target *target) const
Definition Symbol.cpp:504
bool ValueIsAddress() const
Definition Symbol.cpp:165
bool IsExternal() const
Definition Symbol.h:197
uint16_t m_type_data_resolved
Definition Symbol.h:324
void SetReExportedSymbolName(ConstString name)
Definition Symbol.cpp:199
void SetType(lldb::SymbolType type)
Definition Symbol.h:171
void SetSizeIsSynthesized(bool b)
Definition Symbol.h:191
void SetSizeIsSibling(bool b)
Definition Symbol.h:220
void CalculateSymbolContext(SymbolContext *sc) override
Reconstruct the object's symbol context into sc.
Definition Symbol.cpp:399
bool IsIndirect() const
Definition Symbol.cpp:223
bool IsDebug() const
Definition Symbol.h:193
void SynthesizeNameIfNeeded() const
Definition Symbol.cpp:592
const char * GetTypeAsString() const
Definition Symbol.cpp:395
Symbol * ResolveReExportedSymbolInModuleSpec(Target &target, ConstString &reexport_name, lldb_private::ModuleSpec &module_spec, lldb_private::ModuleList &seen_modules) const
Definition Symbol.cpp:433
bool IsSynthetic() const
Definition Symbol.h:183
uint16_t m_is_synthetic
Definition Symbol.h:326
uint16_t m_demangled_is_synthesized
Definition Symbol.h:338
lldb::DisassemblerSP GetInstructions(const ExecutionContext &exe_ctx, const char *flavor, bool prefer_file_cache)
Definition Symbol.cpp:552
bool ContainsLinkerAnnotations() const
Definition Symbol.h:232
lldb::addr_t GetFileAddress() const
Definition Symbol.cpp:497
void DumpSymbolContext(Stream *s) override
Dump the object's symbol context to the stream s.
Definition Symbol.cpp:416
lldb::ModuleSP CalculateSymbolContextModule() override
Definition Symbol.cpp:408
bool ContainsFileAddress(lldb::addr_t file_addr) const
Definition Symbol.cpp:579
bool GetDemangledNameIsSynthesized() const
Definition Symbol.h:226
bool GetByteSizeIsValid() const
Definition Symbol.h:209
Mangled & GetMangled()
Definition Symbol.h:147
static lldb::SymbolType GetTypeFromString(const char *str)
Definition Symbol.cpp:779
uint16_t m_contains_linker_annotations
Definition Symbol.h:341
uint16_t m_size_is_valid
Definition Symbol.h:337
lldb::LanguageType GetLanguage() const
Definition Symbol.h:139
bool IsTrampoline() const
Definition Symbol.cpp:221
Address & GetAddressRef()
Definition Symbol.h:73
const Symbol & operator=(const Symbol &rhs)
Definition Symbol.cpp:78
void SetContainsLinkerAnnotations(bool b)
Definition Symbol.h:235
void SetIsWeak(bool b)
Definition Symbol.h:207
bool IsSyntheticWithAutoGeneratedName() const
Definition Symbol.cpp:583
void Encode(DataEncoder &encoder, ConstStringTable &strtab) const
Encode this object into a data encoder object.
Definition Symbol.cpp:665
uint32_t GetFlags() const
Definition Symbol.h:175
bool GetSizeIsSibling() const
Definition Symbol.h:218
const Mangled & GetMangled() const
Definition Symbol.h:152
ConstString GetReExportedSymbolName() const
Definition Symbol.cpp:173
bool Compare(ConstString name, lldb::SymbolType type) const
Definition Symbol.cpp:386
uint16_t m_is_debug
Definition Symbol.h:329
bool SetReExportedSymbolSharedLibrary(const FileSpec &fspec)
Definition Symbol.cpp:206
void GetDescription(Stream *s, lldb::DescriptionLevel level, Target *target, std::optional< Stream::HighlightSettings > settings=std::nullopt) const
Definition Symbol.cpp:225
bool operator==(const Symbol &rhs) const
Definition Symbol.cpp:702
static llvm::StringRef GetSyntheticSymbolPrefix()
Definition Symbol.h:261
lldb::addr_t GetByteSize() const
Definition Symbol.cpp:431
ConstString GetName() const
Definition Symbol.cpp:511
lldb::SymbolType GetType() const
Definition Symbol.h:169
void SetFlags(uint32_t flags)
Definition Symbol.h:177
Address GetAddress() const
Definition Symbol.h:89
AddressRange m_addr_range
Definition Symbol.h:347
ConstString GetNameNoArguments() const
Definition Symbol.cpp:513
uint16_t m_size_is_sibling
Definition Symbol.h:332
Symbol * ResolveReExportedSymbol(Target &target) const
Definition Symbol.cpp:483
FileSpec GetReExportedSymbolSharedLibrary() const
Definition Symbol.cpp:187
uint16_t m_is_weak
Definition Symbol.h:344
bool IsWeak() const
Definition Symbol.h:205
void SetIsSynthetic(bool b)
Definition Symbol.h:187
bool GetSizeIsSynthesized() const
Definition Symbol.h:189
void SetByteSize(lldb::addr_t size)
Definition Symbol.h:213
uint32_t GetPrologueByteSize()
Definition Symbol.cpp:313
uint16_t m_type_data
Definition Symbol.h:323
void SetDemangledNameIsSynthesized(bool b)
Definition Symbol.h:230
ConstString GetDisplayName() const
Definition Symbol.cpp:169
bool GetDisassembly(const ExecutionContext &exe_ctx, const char *flavor, bool prefer_file_cache, Stream &strm)
Definition Symbol.cpp:564
lldb::addr_t ResolveCallableAddress(Target &target) const
Definition Symbol.cpp:517
void SetExternal(bool b)
Definition Symbol.h:199
uint16_t m_size_is_synthesized
Definition Symbol.h:334
Symbol * CalculateSymbolContextSymbol() override
Definition Symbol.cpp:414
void SetDebug(bool b)
Definition Symbol.h:195
uint64_t GetRawValue() const
Get the raw value of the symbol from the symbol table.
Definition Symbol.h:110
const Address & GetAddressRef() const
Definition Symbol.h:75
void SetID(uint32_t uid)
Definition Symbol.h:145
static llvm::Expected< Symbol > FromJSON(const JSONSymbol &symbol, SectionList *section_list)
Definition Symbol.cpp:101
void Dump(Stream *s, Target *target, uint32_t index, Mangled::NamePreference name_preference=Mangled::ePreferDemangled) const
Definition Symbol.cpp:266
#define UINT32_MAX
A class that represents a running process on the host machine.
DescriptionLevel
Description levels for "void GetDescription(Stream *, DescriptionLevel)" calls.
uint64_t offset_t
Definition lldb-types.h:85
LanguageType
Programming language type.
SymbolType
Symbol types.
std::shared_ptr< lldb_private::Disassembler > DisassemblerSP
std::shared_ptr< lldb_private::Section > SectionSP
uint64_t addr_t
Definition lldb-types.h:80
std::shared_ptr< lldb_private::Module > ModuleSP
bool fromJSON(const llvm::json::Value &value, lldb_private::JSONSection &section, llvm::json::Path path)
Definition Section.cpp:689
std::optional< uint64_t > address
Definition Symbol.h:25
std::optional< uint64_t > id
Definition Symbol.h:28
std::optional< lldb::SymbolType > type
Definition Symbol.h:29
std::optional< uint64_t > value
Definition Symbol.h:26
std::optional< uint64_t > size
Definition Symbol.h:27