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