Symbol.h 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270
  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. #ifndef LLDB_SYMBOL_SYMBOL_H
  9. #define LLDB_SYMBOL_SYMBOL_H
  10. #include "lldb/Core/AddressRange.h"
  11. #include "lldb/Core/Mangled.h"
  12. #include "lldb/Symbol/SymbolContextScope.h"
  13. #include "lldb/Utility/UserID.h"
  14. #include "lldb/lldb-private.h"
  15. namespace lldb_private {
  16. class Symbol : public SymbolContextScope {
  17. public:
  18. // ObjectFile readers can classify their symbol table entries and searches
  19. // can be made on specific types where the symbol values will have
  20. // drastically different meanings and sorting requirements.
  21. Symbol();
  22. Symbol(uint32_t symID, llvm::StringRef name, lldb::SymbolType type,
  23. bool external, bool is_debug, bool is_trampoline, bool is_artificial,
  24. const lldb::SectionSP &section_sp, lldb::addr_t value,
  25. lldb::addr_t size, bool size_is_valid,
  26. bool contains_linker_annotations, uint32_t flags);
  27. Symbol(uint32_t symID, const Mangled &mangled, lldb::SymbolType type,
  28. bool external, bool is_debug, bool is_trampoline, bool is_artificial,
  29. const AddressRange &range, bool size_is_valid,
  30. bool contains_linker_annotations, uint32_t flags);
  31. Symbol(const Symbol &rhs);
  32. const Symbol &operator=(const Symbol &rhs);
  33. void Clear();
  34. bool Compare(ConstString name, lldb::SymbolType type) const;
  35. void Dump(Stream *s, Target *target, uint32_t index,
  36. Mangled::NamePreference name_preference =
  37. Mangled::ePreferDemangled) const;
  38. bool ValueIsAddress() const;
  39. // The GetAddressRef() accessor functions should only be called if you
  40. // previously call ValueIsAddress() otherwise you might get an reference to
  41. // an Address object that contains an constant integer value in
  42. // m_addr_range.m_base_addr.m_offset which could be incorrectly used to
  43. // represent an absolute address since it has no section.
  44. Address &GetAddressRef() { return m_addr_range.GetBaseAddress(); }
  45. const Address &GetAddressRef() const { return m_addr_range.GetBaseAddress(); }
  46. // Makes sure the symbol's value is an address and returns the file address.
  47. // Returns LLDB_INVALID_ADDRESS if the symbol's value isn't an address.
  48. lldb::addr_t GetFileAddress() const;
  49. // Makes sure the symbol's value is an address and gets the load address
  50. // using \a target if it is. Returns LLDB_INVALID_ADDRESS if the symbol's
  51. // value isn't an address or if the section isn't loaded in \a target.
  52. lldb::addr_t GetLoadAddress(Target *target) const;
  53. // Access the address value. Do NOT hand out the AddressRange as an object as
  54. // the byte size of the address range may not be filled in and it should be
  55. // accessed via GetByteSize().
  56. Address GetAddress() const {
  57. // Make sure the our value is an address before we hand a copy out. We use
  58. // the Address inside m_addr_range to contain the value for symbols that
  59. // are not address based symbols so we are using it for more than just
  60. // addresses. For example undefined symbols on MacOSX have a nlist.n_value
  61. // of 0 (zero) and this will get placed into
  62. // m_addr_range.m_base_addr.m_offset and it will have no section. So in the
  63. // GetAddress() accessor, we need to hand out an invalid address if the
  64. // symbol's value isn't an address.
  65. if (ValueIsAddress())
  66. return m_addr_range.GetBaseAddress();
  67. else
  68. return Address();
  69. }
  70. // When a symbol's value isn't an address, we need to access the raw value.
  71. // This function will ensure this symbol's value isn't an address and return
  72. // the integer value if this checks out, otherwise it will return
  73. // "fail_value" if the symbol is an address value.
  74. uint64_t GetIntegerValue(uint64_t fail_value = 0) const {
  75. if (ValueIsAddress()) {
  76. // This symbol's value is an address. Use Symbol::GetAddress() to get the
  77. // address.
  78. return fail_value;
  79. } else {
  80. // The value is stored in the base address' offset
  81. return m_addr_range.GetBaseAddress().GetOffset();
  82. }
  83. }
  84. lldb::addr_t ResolveCallableAddress(Target &target) const;
  85. ConstString GetName() const;
  86. ConstString GetNameNoArguments() const;
  87. ConstString GetDisplayName() const;
  88. uint32_t GetID() const { return m_uid; }
  89. lldb::LanguageType GetLanguage() const {
  90. // TODO: See if there is a way to determine the language for a symbol
  91. // somehow, for now just return our best guess
  92. return m_mangled.GuessLanguage();
  93. }
  94. void SetID(uint32_t uid) { m_uid = uid; }
  95. Mangled &GetMangled() { return m_mangled; }
  96. const Mangled &GetMangled() const { return m_mangled; }
  97. ConstString GetReExportedSymbolName() const;
  98. FileSpec GetReExportedSymbolSharedLibrary() const;
  99. void SetReExportedSymbolName(ConstString name);
  100. bool SetReExportedSymbolSharedLibrary(const FileSpec &fspec);
  101. Symbol *ResolveReExportedSymbol(Target &target) const;
  102. uint32_t GetSiblingIndex() const;
  103. lldb::SymbolType GetType() const { return (lldb::SymbolType)m_type; }
  104. void SetType(lldb::SymbolType type) { m_type = (lldb::SymbolType)type; }
  105. const char *GetTypeAsString() const;
  106. uint32_t GetFlags() const { return m_flags; }
  107. void SetFlags(uint32_t flags) { m_flags = flags; }
  108. void GetDescription(Stream *s, lldb::DescriptionLevel level,
  109. Target *target) const;
  110. bool IsSynthetic() const { return m_is_synthetic; }
  111. void SetIsSynthetic(bool b) { m_is_synthetic = b; }
  112. bool GetSizeIsSynthesized() const { return m_size_is_synthesized; }
  113. void SetSizeIsSynthesized(bool b) { m_size_is_synthesized = b; }
  114. bool IsDebug() const { return m_is_debug; }
  115. void SetDebug(bool b) { m_is_debug = b; }
  116. bool IsExternal() const { return m_is_external; }
  117. void SetExternal(bool b) { m_is_external = b; }
  118. bool IsTrampoline() const;
  119. bool IsIndirect() const;
  120. bool IsWeak() const { return m_is_weak; }
  121. void SetIsWeak (bool b) { m_is_weak = b; }
  122. bool GetByteSizeIsValid() const { return m_size_is_valid; }
  123. lldb::addr_t GetByteSize() const;
  124. void SetByteSize(lldb::addr_t size) {
  125. m_size_is_valid = size > 0;
  126. m_addr_range.SetByteSize(size);
  127. }
  128. bool GetSizeIsSibling() const { return m_size_is_sibling; }
  129. void SetSizeIsSibling(bool b) { m_size_is_sibling = b; }
  130. // If m_type is "Code" or "Function" then this will return the prologue size
  131. // in bytes, else it will return zero.
  132. uint32_t GetPrologueByteSize();
  133. bool GetDemangledNameIsSynthesized() const {
  134. return m_demangled_is_synthesized;
  135. }
  136. void SetDemangledNameIsSynthesized(bool b) { m_demangled_is_synthesized = b; }
  137. bool ContainsLinkerAnnotations() const {
  138. return m_contains_linker_annotations;
  139. }
  140. void SetContainsLinkerAnnotations(bool b) {
  141. m_contains_linker_annotations = b;
  142. }
  143. /// \copydoc SymbolContextScope::CalculateSymbolContext(SymbolContext*)
  144. ///
  145. /// \see SymbolContextScope
  146. void CalculateSymbolContext(SymbolContext *sc) override;
  147. lldb::ModuleSP CalculateSymbolContextModule() override;
  148. Symbol *CalculateSymbolContextSymbol() override;
  149. /// \copydoc SymbolContextScope::DumpSymbolContext(Stream*)
  150. ///
  151. /// \see SymbolContextScope
  152. void DumpSymbolContext(Stream *s) override;
  153. lldb::DisassemblerSP GetInstructions(const ExecutionContext &exe_ctx,
  154. const char *flavor,
  155. bool prefer_file_cache);
  156. bool GetDisassembly(const ExecutionContext &exe_ctx, const char *flavor,
  157. bool prefer_file_cache, Stream &strm);
  158. bool ContainsFileAddress(lldb::addr_t file_addr) const;
  159. protected:
  160. // This is the internal guts of ResolveReExportedSymbol, it assumes
  161. // reexport_name is not null, and that module_spec is valid. We track the
  162. // modules we've already seen to make sure we don't get caught in a cycle.
  163. Symbol *ResolveReExportedSymbolInModuleSpec(
  164. Target &target, ConstString &reexport_name,
  165. lldb_private::ModuleSpec &module_spec,
  166. lldb_private::ModuleList &seen_modules) const;
  167. uint32_t m_uid; // User ID (usually the original symbol table index)
  168. uint16_t m_type_data; // data specific to m_type
  169. uint16_t m_type_data_resolved : 1, // True if the data in m_type_data has
  170. // already been calculated
  171. m_is_synthetic : 1, // non-zero if this symbol is not actually in the
  172. // symbol table, but synthesized from other info in
  173. // the object file.
  174. m_is_debug : 1, // non-zero if this symbol is debug information in a
  175. // symbol
  176. m_is_external : 1, // non-zero if this symbol is globally visible
  177. m_size_is_sibling : 1, // m_size contains the index of this symbol's
  178. // sibling
  179. m_size_is_synthesized : 1, // non-zero if this symbol's size was
  180. // calculated using a delta between this
  181. // symbol and the next
  182. m_size_is_valid : 1,
  183. m_demangled_is_synthesized : 1, // The demangled name was created should
  184. // not be used for expressions or other
  185. // lookups
  186. m_contains_linker_annotations : 1, // The symbol name contains linker
  187. // annotations, which are optional when
  188. // doing name lookups
  189. m_is_weak : 1,
  190. m_type : 6; // Values from the lldb::SymbolType enum.
  191. Mangled m_mangled; // uniqued symbol name/mangled name pair
  192. AddressRange m_addr_range; // Contains the value, or the section offset
  193. // address when the value is an address in a
  194. // section, and the size (if any)
  195. uint32_t m_flags; // A copy of the flags from the original symbol table, the
  196. // ObjectFile plug-in can interpret these
  197. };
  198. } // namespace lldb_private
  199. #endif // LLDB_SYMBOL_SYMBOL_H