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 liblldb_Symbol_h_
10 #define liblldb_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, const char *name, bool name_is_mangled,
28  lldb::SymbolType type, bool external, bool is_debug,
29  bool is_trampoline, bool is_artificial,
30  const lldb::SectionSP &section_sp, lldb::addr_t value,
31  lldb::addr_t size, bool size_is_valid,
32  bool contains_linker_annotations, uint32_t flags);
33 
34  Symbol(uint32_t symID, const Mangled &mangled, lldb::SymbolType type,
35  bool external, bool is_debug, bool is_trampoline, bool is_artificial,
36  const AddressRange &range, bool size_is_valid,
37  bool contains_linker_annotations, uint32_t flags);
38 
39  Symbol(const Symbol &rhs);
40 
41  const Symbol &operator=(const Symbol &rhs);
42 
43  void Clear();
44 
45  bool Compare(ConstString name, lldb::SymbolType type) const;
46 
47  void Dump(Stream *s, Target *target, uint32_t index) const;
48 
49  bool ValueIsAddress() const;
50 
51  // The GetAddressRef() accessor functions should only be called if you
52  // previously call ValueIsAddress() otherwise you might get an reference to
53  // an Address object that contains an constant integer value in
54  // m_addr_range.m_base_addr.m_offset which could be incorrectly used to
55  // represent an absolute address since it has no section.
57 
58  const Address &GetAddressRef() const { return m_addr_range.GetBaseAddress(); }
59 
60  // Makes sure the symbol's value is an address and returns the file address.
61  // Returns LLDB_INVALID_ADDRESS if the symbol's value isn't an address.
63 
64  // Makes sure the symbol's value is an address and gets the load address
65  // using \a target if it is. Returns LLDB_INVALID_ADDRESS if the symbol's
66  // value isn't an address or if the section isn't loaded in \a target.
67  lldb::addr_t GetLoadAddress(Target *target) const;
68 
69  // Access the address value. Do NOT hand out the AddressRange as an object as
70  // the byte size of the address range may not be filled in and it should be
71  // accessed via GetByteSize().
72  Address GetAddress() const {
73  // Make sure the our value is an address before we hand a copy out. We use
74  // the Address inside m_addr_range to contain the value for symbols that
75  // are not address based symbols so we are using it for more than just
76  // addresses. For example undefined symbols on MacOSX have a nlist.n_value
77  // of 0 (zero) and this will get placed into
78  // m_addr_range.m_base_addr.m_offset and it will have no section. So in the
79  // GetAddress() accessor, we need to hand out an invalid address if the
80  // symbol's value isn't an address.
81  if (ValueIsAddress())
83  else
84  return Address();
85  }
86 
87  // When a symbol's value isn't an address, we need to access the raw value.
88  // This function will ensure this symbol's value isn't an address and return
89  // the integer value if this checks out, otherwise it will return
90  // "fail_value" if the symbol is an address value.
91  uint64_t GetIntegerValue(uint64_t fail_value = 0) const {
92  if (ValueIsAddress()) {
93  // This symbol's value is an address. Use Symbol::GetAddress() to get the
94  // address.
95  return fail_value;
96  } else {
97  // The value is stored in the base address' offset
99  }
100  }
101 
103 
104  ConstString GetName() const;
105 
107 
108  ConstString GetDisplayName() const;
109 
110  uint32_t GetID() const { return m_uid; }
111 
113  // TODO: See if there is a way to determine the language for a symbol
114  // somehow, for now just return our best guess
115  return m_mangled.GuessLanguage();
116  }
117 
118  void SetID(uint32_t uid) { m_uid = uid; }
119 
120  Mangled &GetMangled() { return m_mangled; }
121 
122  const Mangled &GetMangled() const { return m_mangled; }
123 
125 
127 
129 
130  bool SetReExportedSymbolSharedLibrary(const FileSpec &fspec);
131 
132  Symbol *ResolveReExportedSymbol(Target &target) const;
133 
134  uint32_t GetSiblingIndex() const;
135 
137 
139 
140  const char *GetTypeAsString() const;
141 
142  uint32_t GetFlags() const { return m_flags; }
143 
144  void SetFlags(uint32_t flags) { m_flags = flags; }
145 
147  Target *target) const;
148 
149  bool IsSynthetic() const { return m_is_synthetic; }
150 
151  void SetIsSynthetic(bool b) { m_is_synthetic = b; }
152 
154 
156 
157  bool IsDebug() const { return m_is_debug; }
158 
159  void SetDebug(bool b) { m_is_debug = b; }
160 
161  bool IsExternal() const { return m_is_external; }
162 
163  void SetExternal(bool b) { m_is_external = b; }
164 
165  bool IsTrampoline() const;
166 
167  bool IsIndirect() const;
168 
169  bool GetByteSizeIsValid() const { return m_size_is_valid; }
170 
171  lldb::addr_t GetByteSize() const;
172 
174  m_size_is_valid = size > 0;
176  }
177 
178  bool GetSizeIsSibling() const { return m_size_is_sibling; }
179 
180  void SetSizeIsSibling(bool b) { m_size_is_sibling = b; }
181 
182  // If m_type is "Code" or "Function" then this will return the prologue size
183  // in bytes, else it will return zero.
185 
188  }
189 
191 
194  }
197  }
198  /// \copydoc SymbolContextScope::CalculateSymbolContext(SymbolContext*)
199  ///
200  /// \see SymbolContextScope
201  void CalculateSymbolContext(SymbolContext *sc) override;
202 
203  lldb::ModuleSP CalculateSymbolContextModule() override;
204 
206 
207  /// \copydoc SymbolContextScope::DumpSymbolContext(Stream*)
208  ///
209  /// \see SymbolContextScope
210  void DumpSymbolContext(Stream *s) override;
211 
212  lldb::DisassemblerSP GetInstructions(const ExecutionContext &exe_ctx,
213  const char *flavor,
214  bool prefer_file_cache);
215 
216  bool GetDisassembly(const ExecutionContext &exe_ctx, const char *flavor,
217  bool prefer_file_cache, Stream &strm);
218 
219  bool ContainsFileAddress(lldb::addr_t file_addr) const;
220 
221 protected:
222  // This is the internal guts of ResolveReExportedSymbol, it assumes
223  // reexport_name is not null, and that module_spec is valid. We track the
224  // modules we've already seen to make sure we don't get caught in a cycle.
225 
227  Target &target, ConstString &reexport_name,
228  lldb_private::ModuleSpec &module_spec,
229  lldb_private::ModuleList &seen_modules) const;
230 
231  uint32_t m_uid; // User ID (usually the original symbol table index)
232  uint16_t m_type_data; // data specific to m_type
233  uint16_t m_type_data_resolved : 1, // True if the data in m_type_data has
234  // already been calculated
235  m_is_synthetic : 1, // non-zero if this symbol is not actually in the
236  // symbol table, but synthesized from other info in
237  // the object file.
238  m_is_debug : 1, // non-zero if this symbol is debug information in a
239  // symbol
240  m_is_external : 1, // non-zero if this symbol is globally visible
241  m_size_is_sibling : 1, // m_size contains the index of this symbol's
242  // sibling
243  m_size_is_synthesized : 1, // non-zero if this symbol's size was
244  // calculated using a delta between this
245  // symbol and the next
246  m_size_is_valid : 1,
247  m_demangled_is_synthesized : 1, // The demangled name was created should
248  // not be used for expressions or other
249  // lookups
250  m_contains_linker_annotations : 1, // The symbol name contains linker
251  // annotations, which are optional when
252  // doing name lookups
253  m_type : 7;
254  Mangled m_mangled; // uniqued symbol name/mangled name pair
255  AddressRange m_addr_range; // Contains the value, or the section offset
256  // address when the value is an address in a
257  // section, and the size (if any)
258  uint32_t m_flags; // A copy of the flags from the original symbol table, the
259  // ObjectFile plug-in can interpret these
260 };
261 
262 } // namespace lldb_private
263 
264 #endif // liblldb_Symbol_h_
const char * GetTypeAsString() const
Definition: Symbol.cpp:339
Address & GetAddressRef()
Definition: Symbol.h:56
Enumerations for broadcasting.
Definition: SBLaunchInfo.h:14
lldb::LanguageType GetLanguage() const
Definition: Symbol.h:112
A stream class that can stream formatted output to a file.
Definition: Stream.h:28
void SetByteSize(lldb::addr_t size)
Definition: Symbol.h:173
lldb::addr_t GetFileAddress() const
Definition: Symbol.cpp:480
Defines a symbol context baton that can be handed other debug core functions.
Definition: SymbolContext.h:33
bool IsTrampoline() const
Definition: Symbol.cpp:170
Symbol * ResolveReExportedSymbolInModuleSpec(Target &target, ConstString &reexport_name, lldb_private::ModuleSpec &module_spec, lldb_private::ModuleList &seen_modules) const
Definition: Symbol.cpp:410
Mangled m_mangled
Definition: Symbol.h:254
uint64_t GetIntegerValue(uint64_t fail_value=0) const
Definition: Symbol.h:91
uint16_t m_contains_linker_annotations
Definition: Symbol.h:233
lldb::addr_t GetLoadAddress(Target *target) const
Definition: Symbol.cpp:487
A file utility class.
Definition: FileSpec.h:55
bool Compare(ConstString name, lldb::SymbolType type) const
Definition: Symbol.cpp:328
uint16_t m_type_data_resolved
Definition: Symbol.h:233
bool IsSynthetic() const
Definition: Symbol.h:149
lldb::DisassemblerSP GetInstructions(const ExecutionContext &exe_ctx, const char *flavor, bool prefer_file_cache)
Definition: Symbol.cpp:536
uint16_t m_type_data
Definition: Symbol.h:232
bool GetDisassembly(const ExecutionContext &exe_ctx, const char *flavor, bool prefer_file_cache, Stream &strm)
Definition: Symbol.cpp:549
void SetDemangledNameIsSynthesized(bool b)
Definition: Symbol.h:190
"lldb/Target/ExecutionContext.h" A class that contains an execution context.
uint16_t m_is_debug
Definition: Symbol.h:233
uint32_t GetSiblingIndex() const
Definition: Symbol.cpp:166
uint32_t GetID() const
Definition: Symbol.h:110
Address GetAddress() const
Definition: Symbol.h:72
void CalculateSymbolContext(SymbolContext *sc) override
Reconstruct the object's symbol context into sc.
Definition: Symbol.cpp:376
uint32_t m_flags
Definition: Symbol.h:258
bool GetSizeIsSynthesized() const
Definition: Symbol.h:153
bool GetByteSizeIsValid() const
Definition: Symbol.h:169
void Dump(Stream *s, Target *target, uint32_t index) const
Definition: Symbol.cpp:209
void SetReExportedSymbolName(ConstString name)
Definition: Symbol.cpp:148
lldb::SymbolType GetType() const
Definition: Symbol.h:136
lldb::addr_t GetOffset() const
Get the section relative offset value.
Definition: Address.h:317
void DumpSymbolContext(Stream *s) override
Dump the object's symbol context to the stream s.
Definition: Symbol.cpp:393
uint16_t m_size_is_synthesized
Definition: Symbol.h:233
uint32_t m_uid
Definition: Symbol.h:231
lldb::addr_t GetByteSize() const
Definition: Symbol.cpp:408
void SetSizeIsSibling(bool b)
Definition: Symbol.h:180
LanguageType
Programming language type.
ConstString GetNameNoArguments() const
Definition: Symbol.cpp:496
A collection class for Module objects.
Definition: ModuleList.h:91
bool IsIndirect() const
Definition: Symbol.cpp:172
void SetContainsLinkerAnnotations(bool b)
Definition: Symbol.h:195
void SetSizeIsSynthesized(bool b)
Definition: Symbol.h:155
bool ValueIsAddress() const
Definition: Symbol.cpp:114
AddressRange m_addr_range
Definition: Symbol.h:255
lldb::addr_t ResolveCallableAddress(Target &target) const
Definition: Symbol.cpp:501
const Mangled & GetMangled() const
Definition: Symbol.h:122
Symbol * CalculateSymbolContextSymbol() override
Definition: Symbol.cpp:391
void SetDebug(bool b)
Definition: Symbol.h:159
A section + offset based address class.
Definition: Address.h:80
bool SetReExportedSymbolSharedLibrary(const FileSpec &fspec)
Definition: Symbol.cpp:155
void GetDescription(Stream *s, lldb::DescriptionLevel level, Target *target) const
Definition: Symbol.cpp:174
ConstString GetReExportedSymbolName() const
Definition: Symbol.cpp:122
bool ContainsLinkerAnnotations() const
Definition: Symbol.h:192
Symbol * ResolveReExportedSymbol(Target &target) const
Definition: Symbol.cpp:466
uint64_t addr_t
Definition: lldb-types.h:83
void SetIsSynthetic(bool b)
Definition: Symbol.h:151
const Address & GetAddressRef() const
Definition: Symbol.h:58
uint16_t m_size_is_sibling
Definition: Symbol.h:233
A uniqued constant string class.
Definition: ConstString.h:38
uint16_t m_is_external
Definition: Symbol.h:233
void SetExternal(bool b)
Definition: Symbol.h:163
ConstString GetDisplayName() const
Definition: Symbol.cpp:118
ConstString GetName() const
Definition: Symbol.cpp:494
void SetFlags(uint32_t flags)
Definition: Symbol.h:144
bool GetSizeIsSibling() const
Definition: Symbol.h:178
uint16_t m_type
Definition: Symbol.h:233
bool IsDebug() const
Definition: Symbol.h:157
FileSpec GetReExportedSymbolSharedLibrary() const
Definition: Symbol.cpp:136
uint32_t GetPrologueByteSize()
Definition: Symbol.cpp:255
Address & GetBaseAddress()
Get accessor for the base address of the range.
Definition: AddressRange.h:220
bool IsExternal() const
Definition: Symbol.h:161
uint16_t m_demangled_is_synthesized
Definition: Symbol.h:233
uint16_t m_is_synthetic
Definition: Symbol.h:233
uint32_t GetFlags() const
Definition: Symbol.h:142
const Symbol & operator=(const Symbol &rhs)
Definition: Symbol.cpp:74
bool ContainsFileAddress(lldb::addr_t file_addr) const
Definition: Symbol.cpp:563
uint16_t m_size_is_valid
Definition: Symbol.h:233
A section + offset based address range class.
Definition: AddressRange.h:32
"lldb/Symbol/SymbolContextScope.h" Inherit from this if your object is part of a symbol context and c...
void SetID(uint32_t uid)
Definition: Symbol.h:118
void SetType(lldb::SymbolType type)
Definition: Symbol.h:138
lldb::ModuleSP CalculateSymbolContextModule() override
Definition: Symbol.cpp:385
bool GetDemangledNameIsSynthesized() const
Definition: Symbol.h:186
void SetByteSize(lldb::addr_t byte_size)
Set accessor for the byte size of this range.
Definition: AddressRange.h:248
Mangled & GetMangled()
Definition: Symbol.h:120