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 
12 #include "lldb/Core/AddressRange.h"
13 #include "lldb/Core/Mangled.h"
15 #include "lldb/Utility/UserID.h"
16 #include "lldb/lldb-private.h"
17 
18 namespace lldb_private {
19 
20 class Symbol : public SymbolContextScope {
21 public:
22  // ObjectFile readers can classify their symbol table entries and searches
23  // can be made on specific types where the symbol values will have
24  // drastically different meanings and sorting requirements.
25  Symbol();
26 
27  Symbol(uint32_t symID, llvm::StringRef name, lldb::SymbolType type,
28  bool external, bool is_debug, bool is_trampoline, bool is_artificial,
29  const lldb::SectionSP &section_sp, lldb::addr_t value,
30  lldb::addr_t size, bool size_is_valid,
31  bool contains_linker_annotations, uint32_t flags);
32 
33  Symbol(uint32_t symID, const Mangled &mangled, lldb::SymbolType type,
34  bool external, bool is_debug, bool is_trampoline, bool is_artificial,
35  const AddressRange &range, bool size_is_valid,
36  bool contains_linker_annotations, uint32_t flags);
37 
38  Symbol(const Symbol &rhs);
39 
40  const Symbol &operator=(const Symbol &rhs);
41 
42  void Clear();
43 
44  bool Compare(ConstString name, lldb::SymbolType type) const;
45 
46  void Dump(Stream *s, Target *target, uint32_t index,
47  Mangled::NamePreference name_preference =
48  Mangled::ePreferDemangled) const;
49 
50  bool ValueIsAddress() const;
51 
52  // The GetAddressRef() accessor functions should only be called if you
53  // previously call ValueIsAddress() otherwise you might get an reference to
54  // an Address object that contains an constant integer value in
55  // m_addr_range.m_base_addr.m_offset which could be incorrectly used to
56  // represent an absolute address since it has no section.
58 
59  const Address &GetAddressRef() const { return m_addr_range.GetBaseAddress(); }
60 
61  // Makes sure the symbol's value is an address and returns the file address.
62  // Returns LLDB_INVALID_ADDRESS if the symbol's value isn't an address.
64 
65  // Makes sure the symbol's value is an address and gets the load address
66  // using \a target if it is. Returns LLDB_INVALID_ADDRESS if the symbol's
67  // value isn't an address or if the section isn't loaded in \a target.
68  lldb::addr_t GetLoadAddress(Target *target) const;
69 
70  // Access the address value. Do NOT hand out the AddressRange as an object as
71  // the byte size of the address range may not be filled in and it should be
72  // accessed via GetByteSize().
73  Address GetAddress() const {
74  // Make sure the our value is an address before we hand a copy out. We use
75  // the Address inside m_addr_range to contain the value for symbols that
76  // are not address based symbols so we are using it for more than just
77  // addresses. For example undefined symbols on MacOSX have a nlist.n_value
78  // of 0 (zero) and this will get placed into
79  // m_addr_range.m_base_addr.m_offset and it will have no section. So in the
80  // GetAddress() accessor, we need to hand out an invalid address if the
81  // symbol's value isn't an address.
82  if (ValueIsAddress())
84  else
85  return Address();
86  }
87 
88  // When a symbol's value isn't an address, we need to access the raw value.
89  // This function will ensure this symbol's value isn't an address and return
90  // the integer value if this checks out, otherwise it will return
91  // "fail_value" if the symbol is an address value.
92  uint64_t GetIntegerValue(uint64_t fail_value = 0) const {
93  if (ValueIsAddress()) {
94  // This symbol's value is an address. Use Symbol::GetAddress() to get the
95  // address.
96  return fail_value;
97  } else {
98  // The value is stored in the base address' offset
100  }
101  }
102 
104 
105  ConstString GetName() const;
106 
108 
109  ConstString GetDisplayName() const;
110 
111  uint32_t GetID() const { return m_uid; }
112 
114  // TODO: See if there is a way to determine the language for a symbol
115  // somehow, for now just return our best guess
116  return GetMangled().GuessLanguage();
117  }
118 
119  void SetID(uint32_t uid) { m_uid = uid; }
120 
121  Mangled &GetMangled() {
123  return m_mangled;
124  }
125 
126  const Mangled &GetMangled() const {
128  return m_mangled;
129  }
130 
132 
134 
136 
137  bool SetReExportedSymbolSharedLibrary(const FileSpec &fspec);
138 
139  Symbol *ResolveReExportedSymbol(Target &target) const;
140 
141  uint32_t GetSiblingIndex() const;
142 
144 
146 
147  const char *GetTypeAsString() const;
148 
149  uint32_t GetFlags() const { return m_flags; }
150 
151  void SetFlags(uint32_t flags) { m_flags = flags; }
152 
154  Target *target) const;
155 
156  bool IsSynthetic() const { return m_is_synthetic; }
157 
159 
160  void SetIsSynthetic(bool b) { m_is_synthetic = b; }
161 
163 
165 
166  bool IsDebug() const { return m_is_debug; }
167 
168  void SetDebug(bool b) { m_is_debug = b; }
169 
170  bool IsExternal() const { return m_is_external; }
171 
172  void SetExternal(bool b) { m_is_external = b; }
173 
174  bool IsTrampoline() const;
175 
176  bool IsIndirect() const;
177 
178  bool IsWeak() const { return m_is_weak; }
179 
180  void SetIsWeak (bool b) { m_is_weak = b; }
181 
182  bool GetByteSizeIsValid() const { return m_size_is_valid; }
183 
184  lldb::addr_t GetByteSize() const;
185 
187  m_size_is_valid = size > 0;
189  }
190 
191  bool GetSizeIsSibling() const { return m_size_is_sibling; }
192 
193  void SetSizeIsSibling(bool b) { m_size_is_sibling = b; }
194 
195  // If m_type is "Code" or "Function" then this will return the prologue size
196  // in bytes, else it will return zero.
198 
201  }
202 
204 
207  }
210  }
211  /// \copydoc SymbolContextScope::CalculateSymbolContext(SymbolContext*)
212  ///
213  /// \see SymbolContextScope
214  void CalculateSymbolContext(SymbolContext *sc) override;
215 
216  lldb::ModuleSP CalculateSymbolContextModule() override;
217 
219 
220  /// \copydoc SymbolContextScope::DumpSymbolContext(Stream*)
221  ///
222  /// \see SymbolContextScope
223  void DumpSymbolContext(Stream *s) override;
224 
225  lldb::DisassemblerSP GetInstructions(const ExecutionContext &exe_ctx,
226  const char *flavor,
227  bool prefer_file_cache);
228 
229  bool GetDisassembly(const ExecutionContext &exe_ctx, const char *flavor,
230  bool prefer_file_cache, Stream &strm);
231 
232  bool ContainsFileAddress(lldb::addr_t file_addr) const;
233 
234  static llvm::StringRef GetSyntheticSymbolPrefix() {
235  return "___lldb_unnamed_symbol";
236  }
237 
238 protected:
239  // This is the internal guts of ResolveReExportedSymbol, it assumes
240  // reexport_name is not null, and that module_spec is valid. We track the
241  // modules we've already seen to make sure we don't get caught in a cycle.
242 
244  Target &target, ConstString &reexport_name,
245  lldb_private::ModuleSpec &module_spec,
246  lldb_private::ModuleList &seen_modules) const;
247 
248  void SynthesizeNameIfNeeded() const;
249 
251  UINT32_MAX; // User ID (usually the original symbol table index)
252  uint16_t m_type_data = 0; // data specific to m_type
253  uint16_t m_type_data_resolved : 1, // True if the data in m_type_data has
254  // already been calculated
255  m_is_synthetic : 1, // non-zero if this symbol is not actually in the
256  // symbol table, but synthesized from other info in
257  // the object file.
258  m_is_debug : 1, // non-zero if this symbol is debug information in a
259  // symbol
260  m_is_external : 1, // non-zero if this symbol is globally visible
261  m_size_is_sibling : 1, // m_size contains the index of this symbol's
262  // sibling
263  m_size_is_synthesized : 1, // non-zero if this symbol's size was
264  // calculated using a delta between this
265  // symbol and the next
267  m_demangled_is_synthesized : 1, // The demangled name was created should
268  // not be used for expressions or other
269  // lookups
270  m_contains_linker_annotations : 1, // The symbol name contains linker
271  // annotations, which are optional when
272  // doing name lookups
274  m_type : 6; // Values from the lldb::SymbolType enum.
275  mutable Mangled m_mangled; // uniqued symbol name/mangled name pair
276  AddressRange m_addr_range; // Contains the value, or the section offset
277  // address when the value is an address in a
278  // section, and the size (if any)
279  uint32_t m_flags = 0; // A copy of the flags from the original symbol table,
280  // the ObjectFile plug-in can interpret these
281 };
282 
283 } // namespace lldb_private
284 
285 #endif // LLDB_SYMBOL_SYMBOL_H
lldb_private::Symbol::SetDebug
void SetDebug(bool b)
Definition: Symbol.h:168
lldb_private::AddressRange::GetBaseAddress
Address & GetBaseAddress()
Get accessor for the base address of the range.
Definition: AddressRange.h:209
lldb_private::ExecutionContext
Definition: ExecutionContext.h:292
lldb_private::Symbol::m_type
uint16_t m_type
Definition: Symbol.h:274
Mangled.h
lldb_private::Symbol::SetExternal
void SetExternal(bool b)
Definition: Symbol.h:172
lldb_private::Symbol::GetPrologueByteSize
uint32_t GetPrologueByteSize()
Definition: Symbol.cpp:259
lldb_private::Symbol::GetSyntheticSymbolPrefix
static llvm::StringRef GetSyntheticSymbolPrefix()
Definition: Symbol.h:234
lldb_private::Symbol::SetSizeIsSynthesized
void SetSizeIsSynthesized(bool b)
Definition: Symbol.h:164
lldb_private::Symbol::GetDescription
void GetDescription(Stream *s, lldb::DescriptionLevel level, Target *target) const
Definition: Symbol.cpp:177
lldb_private::Symbol
Definition: Symbol.h:20
lldb_private::Symbol::ValueIsAddress
bool ValueIsAddress() const
Definition: Symbol.cpp:117
lldb_private::Symbol::DumpSymbolContext
void DumpSymbolContext(Stream *s) override
Dump the object's symbol context to the stream s.
Definition: Symbol.cpp:399
lldb_private::Symbol::GetMangled
const Mangled & GetMangled() const
Definition: Symbol.h:126
lldb_private::Symbol::SetIsSynthetic
void SetIsSynthetic(bool b)
Definition: Symbol.h:160
lldb_private::Symbol::m_size_is_valid
uint16_t m_size_is_valid
Definition: Symbol.h:266
lldb::LanguageType
LanguageType
Programming language type.
Definition: lldb-enumerations.h:436
lldb_private::Symbol::SetType
void SetType(lldb::SymbolType type)
Definition: Symbol.h:145
lldb_private::Symbol::CalculateSymbolContextModule
lldb::ModuleSP CalculateSymbolContextModule() override
Definition: Symbol.cpp:391
lldb_private::Symbol::GetReExportedSymbolName
ConstString GetReExportedSymbolName() const
Definition: Symbol.cpp:125
lldb_private::Symbol::CalculateSymbolContextSymbol
Symbol * CalculateSymbolContextSymbol() override
Definition: Symbol.cpp:397
lldb_private::Symbol::m_uid
uint32_t m_uid
Definition: Symbol.h:250
lldb_private::Stream
Definition: Stream.h:28
lldb::addr_t
uint64_t addr_t
Definition: lldb-types.h:83
lldb_private::Symbol::IsSyntheticWithAutoGeneratedName
bool IsSyntheticWithAutoGeneratedName() const
Definition: Symbol.cpp:571
lldb_private::SymbolContext
Definition: SymbolContext.h:33
lldb_private::Target
Definition: Target.h:454
lldb_private::Symbol::m_mangled
Mangled m_mangled
Definition: Symbol.h:275
lldb_private::Symbol::ResolveCallableAddress
lldb::addr_t ResolveCallableAddress(Target &target) const
Definition: Symbol.cpp:506
lldb_private::Symbol::m_is_synthetic
uint16_t m_is_synthetic
Definition: Symbol.h:255
lldb_private::Symbol::GetFlags
uint32_t GetFlags() const
Definition: Symbol.h:149
lldb_private::Symbol::m_type_data_resolved
uint16_t m_type_data_resolved
Definition: Symbol.h:253
lldb_private::Symbol::ContainsFileAddress
bool ContainsFileAddress(lldb::addr_t file_addr) const
Definition: Symbol.cpp:567
lldb_private::Symbol::Dump
void Dump(Stream *s, Target *target, uint32_t index, Mangled::NamePreference name_preference=Mangled::ePreferDemangled) const
Definition: Symbol.cpp:212
lldb_private::FileSpec
Definition: FileSpec.h:56
lldb_private::Symbol::SetByteSize
void SetByteSize(lldb::addr_t size)
Definition: Symbol.h:186
lldb_private::Symbol::GetSizeIsSibling
bool GetSizeIsSibling() const
Definition: Symbol.h:191
lldb_private::ModuleList
Definition: ModuleList.h:72
lldb_private::Symbol::SetID
void SetID(uint32_t uid)
Definition: Symbol.h:119
lldb_private::Symbol::SetReExportedSymbolSharedLibrary
bool SetReExportedSymbolSharedLibrary(const FileSpec &fspec)
Definition: Symbol.cpp:158
lldb_private::Symbol::m_size_is_sibling
uint16_t m_size_is_sibling
Definition: Symbol.h:261
lldb_private::Symbol::m_demangled_is_synthesized
uint16_t m_demangled_is_synthesized
Definition: Symbol.h:267
lldb_private::Symbol::CalculateSymbolContext
void CalculateSymbolContext(SymbolContext *sc) override
Reconstruct the object's symbol context into sc.
Definition: Symbol.cpp:382
lldb_private::Symbol::GetAddressRef
Address & GetAddressRef()
Definition: Symbol.h:57
lldb_private::Symbol::GetInstructions
lldb::DisassemblerSP GetInstructions(const ExecutionContext &exe_ctx, const char *flavor, bool prefer_file_cache)
Definition: Symbol.cpp:541
SymbolContextScope.h
lldb_private::Symbol::operator=
const Symbol & operator=(const Symbol &rhs)
Definition: Symbol.cpp:75
lldb_private::ConstString
Definition: ConstString.h:40
lldb_private::Symbol::GetAddressRef
const Address & GetAddressRef() const
Definition: Symbol.h:59
lldb_private::Symbol::Clear
void Clear()
Definition: Symbol.cpp:98
lldb_private::Symbol::m_contains_linker_annotations
uint16_t m_contains_linker_annotations
Definition: Symbol.h:270
lldb_private::AddressRange
Definition: AddressRange.h:25
lldb_private::Symbol::IsExternal
bool IsExternal() const
Definition: Symbol.h:170
lldb_private::Symbol::Compare
bool Compare(ConstString name, lldb::SymbolType type) const
Definition: Symbol.cpp:332
lldb_private::Symbol::IsSynthetic
bool IsSynthetic() const
Definition: Symbol.h:156
lldb_private::Symbol::ContainsLinkerAnnotations
bool ContainsLinkerAnnotations() const
Definition: Symbol.h:205
lldb_private::Symbol::GetSizeIsSynthesized
bool GetSizeIsSynthesized() const
Definition: Symbol.h:162
lldb_private::Symbol::GetMangled
Mangled & GetMangled()
Definition: Symbol.h:121
lldb_private::Symbol::GetTypeAsString
const char * GetTypeAsString() const
Definition: Symbol.cpp:345
lldb::SymbolType
SymbolType
Symbol types.
Definition: lldb-enumerations.h:612
lldb_private::Symbol::GetIntegerValue
uint64_t GetIntegerValue(uint64_t fail_value=0) const
Definition: Symbol.h:92
UserID.h
lldb_private::Symbol::m_is_external
uint16_t m_is_external
Definition: Symbol.h:260
lldb_private::SymbolContextScope
Definition: SymbolContextScope.h:64
lldb-private.h
lldb_private::Symbol::GetName
ConstString GetName() const
Definition: Symbol.cpp:500
lldb_private::Symbol::m_flags
uint32_t m_flags
Definition: Symbol.h:279
lldb_private::Symbol::SetFlags
void SetFlags(uint32_t flags)
Definition: Symbol.h:151
lldb_private::Symbol::Symbol
Symbol()
Definition: Symbol.cpp:25
lldb_private::Symbol::GetByteSizeIsValid
bool GetByteSizeIsValid() const
Definition: Symbol.h:182
lldb_private::Symbol::IsDebug
bool IsDebug() const
Definition: Symbol.h:166
lldb_private::ModuleSpec
Definition: ModuleSpec.h:26
lldb_private::Symbol::GetDemangledNameIsSynthesized
bool GetDemangledNameIsSynthesized() const
Definition: Symbol.h:199
lldb_private::Symbol::GetDisassembly
bool GetDisassembly(const ExecutionContext &exe_ctx, const char *flavor, bool prefer_file_cache, Stream &strm)
Definition: Symbol.cpp:553
lldb_private::Symbol::GetReExportedSymbolSharedLibrary
FileSpec GetReExportedSymbolSharedLibrary() const
Definition: Symbol.cpp:139
uint32_t
lldb_private::Address
Definition: Address.h:59
lldb_private::Symbol::GetLanguage
lldb::LanguageType GetLanguage() const
Definition: Symbol.h:113
lldb_private::Symbol::GetByteSize
lldb::addr_t GetByteSize() const
Definition: Symbol.cpp:414
UINT32_MAX
#define UINT32_MAX
Definition: lldb-defines.h:31
lldb_private::Symbol::SetContainsLinkerAnnotations
void SetContainsLinkerAnnotations(bool b)
Definition: Symbol.h:208
uint16_t
lldb_private::Symbol::SynthesizeNameIfNeeded
void SynthesizeNameIfNeeded() const
Definition: Symbol.cpp:580
lldb_private::Symbol::GetFileAddress
lldb::addr_t GetFileAddress() const
Definition: Symbol.cpp:486
lldb_private::AddressRange::SetByteSize
void SetByteSize(lldb::addr_t byte_size)
Set accessor for the byte size of this range.
Definition: AddressRange.h:237
lldb_private::Symbol::IsWeak
bool IsWeak() const
Definition: Symbol.h:178
lldb_private::Symbol::m_addr_range
AddressRange m_addr_range
Definition: Symbol.h:276
lldb_private
A class that represents a running process on the host machine.
Definition: SBCommandInterpreterRunOptions.h:16
lldb_private::Symbol::SetSizeIsSibling
void SetSizeIsSibling(bool b)
Definition: Symbol.h:193
lldb_private::Symbol::IsTrampoline
bool IsTrampoline() const
Definition: Symbol.cpp:173
lldb_private::Address::GetOffset
lldb::addr_t GetOffset() const
Get the section relative offset value.
Definition: Address.h:310
lldb_private::Symbol::m_type_data
uint16_t m_type_data
Definition: Symbol.h:252
lldb_private::Symbol::GetNameNoArguments
ConstString GetNameNoArguments() const
Definition: Symbol.cpp:502
lldb_private::Symbol::IsIndirect
bool IsIndirect() const
Definition: Symbol.cpp:175
lldb_private::Symbol::SetReExportedSymbolName
void SetReExportedSymbolName(ConstString name)
Definition: Symbol.cpp:151
lldb_private::Symbol::GetLoadAddress
lldb::addr_t GetLoadAddress(Target *target) const
Definition: Symbol.cpp:493
lldb_private::Symbol::GetDisplayName
ConstString GetDisplayName() const
Definition: Symbol.cpp:121
AddressRange.h
lldb_private::Symbol::GetType
lldb::SymbolType GetType() const
Definition: Symbol.h:143
lldb_private::Symbol::m_size_is_synthesized
uint16_t m_size_is_synthesized
Definition: Symbol.h:263
lldb_private::Symbol::m_is_weak
uint16_t m_is_weak
Definition: Symbol.h:273
lldb_private::Symbol::ResolveReExportedSymbol
Symbol * ResolveReExportedSymbol(Target &target) const
Definition: Symbol.cpp:472
lldb_private::Symbol::SetDemangledNameIsSynthesized
void SetDemangledNameIsSynthesized(bool b)
Definition: Symbol.h:203
lldb_private::Symbol::ResolveReExportedSymbolInModuleSpec
Symbol * ResolveReExportedSymbolInModuleSpec(Target &target, ConstString &reexport_name, lldb_private::ModuleSpec &module_spec, lldb_private::ModuleList &seen_modules) const
Definition: Symbol.cpp:416
lldb::DescriptionLevel
DescriptionLevel
Description levels for "void GetDescription(Stream *, DescriptionLevel)" calls.
Definition: lldb-enumerations.h:207
lldb_private::Symbol::m_is_debug
uint16_t m_is_debug
Definition: Symbol.h:258
lldb_private::Symbol::GetSiblingIndex
uint32_t GetSiblingIndex() const
Definition: Symbol.cpp:169
lldb_private::Symbol::GetID
uint32_t GetID() const
Definition: Symbol.h:111
lldb_private::Symbol::SetIsWeak
void SetIsWeak(bool b)
Definition: Symbol.h:180
lldb_private::Symbol::GetAddress
Address GetAddress() const
Definition: Symbol.h:73