psymtab.h 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. /* Public partial symbol table definitions.
  2. Copyright (C) 2009-2022 Free Software Foundation, Inc.
  3. This file is part of GDB.
  4. This program is free software; you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation; either version 3 of the License, or
  7. (at your option) any later version.
  8. This program is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. GNU General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with this program. If not, see <http://www.gnu.org/licenses/>. */
  14. #ifndef PSYMTAB_H
  15. #define PSYMTAB_H
  16. #include "gdbsupport/gdb_obstack.h"
  17. #include "symfile.h"
  18. #include "gdbsupport/next-iterator.h"
  19. #include "bcache.h"
  20. struct partial_symbol;
  21. /* Specialization of bcache to store partial symbols. */
  22. struct psymbol_bcache : public gdb::bcache
  23. {
  24. /* Calculate a hash code for the given partial symbol. The hash is
  25. calculated using the symbol's value, language, domain, class
  26. and name. These are the values which are set by
  27. add_psymbol_to_bcache. */
  28. unsigned long hash (const void *addr, int length) override;
  29. /* Returns true if the symbol LEFT equals the symbol RIGHT.
  30. For the comparison this function uses a symbols value,
  31. language, domain, class and name. */
  32. int compare (const void *left, const void *right, int length) override;
  33. };
  34. /* An instance of this class manages the partial symbol tables and
  35. partial symbols for a given objfile.
  36. The core psymtab functions -- those in psymtab.c -- arrange for
  37. nearly all psymtab- and psymbol-related allocations to happen
  38. either in the psymtab_storage object (either on its obstack or in
  39. other memory managed by this class), or on the per-BFD object. The
  40. only link from the psymtab storage object back to the objfile (or
  41. objfile_obstack) that is made by the core psymtab code is the
  42. compunit_symtab member in the standard_psymtab -- and a given
  43. symbol reader can avoid this by implementing its own subclasses of
  44. partial_symtab.
  45. However, it is up to each symbol reader to maintain this invariant
  46. in other ways, if it wants to reuse psymtabs across multiple
  47. objfiles. The main issue here is ensuring that read_symtab_private
  48. does not point into objfile_obstack. */
  49. class psymtab_storage
  50. {
  51. public:
  52. psymtab_storage () = default;
  53. ~psymtab_storage ();
  54. DISABLE_COPY_AND_ASSIGN (psymtab_storage);
  55. /* Discard all partial symbol tables starting with "psymtabs" and
  56. proceeding until "to" has been discarded. */
  57. void discard_psymtabs_to (struct partial_symtab *to)
  58. {
  59. while (psymtabs != to)
  60. discard_psymtab (psymtabs);
  61. }
  62. /* Discard the partial symbol table. */
  63. void discard_psymtab (struct partial_symtab *pst);
  64. /* Return the obstack that is used for storage by this object. */
  65. struct obstack *obstack ()
  66. {
  67. if (!m_obstack.has_value ())
  68. m_obstack.emplace ();
  69. return &*m_obstack;
  70. }
  71. /* Allocate storage for the "dependencies" field of a psymtab.
  72. NUMBER says how many dependencies there are. */
  73. struct partial_symtab **allocate_dependencies (int number)
  74. {
  75. return OBSTACK_CALLOC (obstack (), number, struct partial_symtab *);
  76. }
  77. /* Install a psymtab on the psymtab list. This transfers ownership
  78. of PST to this object. */
  79. void install_psymtab (partial_symtab *pst);
  80. using partial_symtab_range = next_range<partial_symtab>;
  81. /* A range adapter that makes it possible to iterate over all
  82. psymtabs in one objfile. */
  83. partial_symtab_range range ()
  84. {
  85. return partial_symtab_range (psymtabs);
  86. }
  87. /* Each objfile points to a linked list of partial symtabs derived from
  88. this file, one partial symtab structure for each compilation unit
  89. (source file). */
  90. struct partial_symtab *psymtabs = nullptr;
  91. /* Map addresses to the entries of PSYMTABS. It would be more efficient to
  92. have a map per the whole process but ADDRMAP cannot selectively remove
  93. its items during FREE_OBJFILE. This mapping is already present even for
  94. PARTIAL_SYMTABs which still have no corresponding full SYMTABs read.
  95. The DWARF parser reuses this addrmap to store things other than
  96. psymtabs in the cases where debug information is being read from, for
  97. example, the .debug-names section. */
  98. struct addrmap *psymtabs_addrmap = nullptr;
  99. /* A byte cache where we can stash arbitrary "chunks" of bytes that
  100. will not change. */
  101. psymbol_bcache psymbol_cache;
  102. private:
  103. /* The obstack where allocations are made. This is lazily allocated
  104. so that we don't waste memory when there are no psymtabs. */
  105. gdb::optional<auto_obstack> m_obstack;
  106. };
  107. #endif /* PSYMTAB_H */