minsyms.h 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315
  1. /* Minimal symbol table definitions for GDB.
  2. Copyright (C) 2011-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 MINSYMS_H
  15. #define MINSYMS_H
  16. struct type;
  17. /* Several lookup functions return both a minimal symbol and the
  18. objfile in which it is found. This structure is used in these
  19. cases. */
  20. struct bound_minimal_symbol
  21. {
  22. bound_minimal_symbol (struct minimal_symbol *msym, struct objfile *objf)
  23. : minsym (msym),
  24. objfile (objf)
  25. {
  26. }
  27. bound_minimal_symbol () = default;
  28. /* The minimal symbol that was found, or NULL if no minimal symbol
  29. was found. */
  30. struct minimal_symbol *minsym = nullptr;
  31. /* If MINSYM is not NULL, then this is the objfile in which the
  32. symbol is defined. */
  33. struct objfile *objfile = nullptr;
  34. /* Return the obj_section from OBJFILE for MINSYM. */
  35. struct obj_section *obj_section () const
  36. {
  37. return minsym->obj_section (objfile);
  38. }
  39. };
  40. /* This header declares most of the API for dealing with minimal
  41. symbols and minimal symbol tables. A few things are declared
  42. elsewhere; see below.
  43. A minimal symbol is a symbol for which there is no direct debug
  44. information. For example, for an ELF binary, minimal symbols are
  45. created from the ELF symbol table.
  46. For the definition of the minimal symbol structure, see struct
  47. minimal_symbol in symtab.h.
  48. Minimal symbols are stored in tables attached to an objfile; see
  49. objfiles.h for details. Code should generally treat these tables
  50. as opaque and use functions provided by minsyms.c to inspect them.
  51. */
  52. struct msym_bunch;
  53. /* An RAII-based object that is used to record minimal symbols while
  54. they are being read. */
  55. class minimal_symbol_reader
  56. {
  57. public:
  58. /* Prepare to start collecting minimal symbols. This should be
  59. called by a symbol reader to initialize the minimal symbol
  60. module. */
  61. explicit minimal_symbol_reader (struct objfile *);
  62. ~minimal_symbol_reader ();
  63. /* Install the minimal symbols that have been collected into the
  64. given objfile. */
  65. void install ();
  66. /* Record a new minimal symbol. This is the "full" entry point;
  67. simpler convenience entry points are also provided below.
  68. This returns a new minimal symbol. It is ok to modify the returned
  69. minimal symbol (though generally not necessary). It is not ok,
  70. though, to stash the pointer anywhere; as minimal symbols may be
  71. moved after creation. The memory for the returned minimal symbol
  72. is still owned by the minsyms.c code, and should not be freed.
  73. Arguments are:
  74. NAME - the symbol's name
  75. COPY_NAME - if true, the minsym code must make a copy of NAME. If
  76. false, then NAME must be NUL-terminated, and must have a lifetime
  77. that is at least as long as OBJFILE's lifetime.
  78. ADDRESS - the address of the symbol
  79. MS_TYPE - the type of the symbol
  80. SECTION - the symbol's section
  81. */
  82. struct minimal_symbol *record_full (gdb::string_view name,
  83. bool copy_name,
  84. CORE_ADDR address,
  85. enum minimal_symbol_type ms_type,
  86. int section);
  87. /* Like record_full, but:
  88. - computes the length of NAME
  89. - passes COPY_NAME = true,
  90. - and passes a default SECTION, depending on the type
  91. This variant does not return the new symbol. */
  92. void record (const char *name, CORE_ADDR address,
  93. enum minimal_symbol_type ms_type);
  94. /* Like record_full, but:
  95. - computes the length of NAME
  96. - passes COPY_NAME = true.
  97. This variant does not return the new symbol. */
  98. void record_with_info (const char *name, CORE_ADDR address,
  99. enum minimal_symbol_type ms_type,
  100. int section)
  101. {
  102. record_full (name, true, address, ms_type, section);
  103. }
  104. private:
  105. DISABLE_COPY_AND_ASSIGN (minimal_symbol_reader);
  106. struct objfile *m_objfile;
  107. /* Bunch currently being filled up.
  108. The next field points to chain of filled bunches. */
  109. struct msym_bunch *m_msym_bunch;
  110. /* Number of slots filled in current bunch. */
  111. int m_msym_bunch_index;
  112. /* Total number of minimal symbols recorded so far for the
  113. objfile. */
  114. int m_msym_count;
  115. };
  116. /* Return whether MSYMBOL is a function/method. If FUNC_ADDRESS_P is
  117. non-NULL, and the MSYMBOL is a function, then *FUNC_ADDRESS_P is
  118. set to the function's address, already resolved if MINSYM points to
  119. a function descriptor. */
  120. bool msymbol_is_function (struct objfile *objfile,
  121. minimal_symbol *minsym,
  122. CORE_ADDR *func_address_p = NULL);
  123. /* Compute a hash code for the string argument. Unlike htab_hash_string,
  124. this is a case-insensitive hash to support "set case-sensitive off". */
  125. unsigned int msymbol_hash (const char *);
  126. /* Like msymbol_hash, but compute a hash code that is compatible with
  127. strcmp_iw. */
  128. unsigned int msymbol_hash_iw (const char *);
  129. /* Compute the next hash value from previous HASH and the character C. This
  130. is only a GDB in-memory computed value with no external files compatibility
  131. requirements. */
  132. #define SYMBOL_HASH_NEXT(hash, c) \
  133. ((hash) * 67 + TOLOWER ((unsigned char) (c)) - 113)
  134. /* Look through all the current minimal symbol tables and find the
  135. first minimal symbol that matches NAME. If OBJF is non-NULL, limit
  136. the search to that objfile. If SFILE is non-NULL, the only
  137. file-scope symbols considered will be from that source file (global
  138. symbols are still preferred). Returns a bound minimal symbol that
  139. matches, or an empty bound minimal symbol if no match is found. */
  140. struct bound_minimal_symbol lookup_minimal_symbol (const char *,
  141. const char *,
  142. struct objfile *);
  143. /* Like lookup_minimal_symbol, but searches all files and
  144. objfiles. */
  145. struct bound_minimal_symbol lookup_bound_minimal_symbol (const char *);
  146. /* Look through all the current minimal symbol tables and find the
  147. first minimal symbol that matches NAME and has text type. If OBJF
  148. is non-NULL, limit the search to that objfile. Returns a bound
  149. minimal symbol that matches, or an "empty" bound minimal symbol
  150. otherwise.
  151. This function only searches the mangled (linkage) names. */
  152. struct bound_minimal_symbol lookup_minimal_symbol_text (const char *,
  153. struct objfile *);
  154. /* Look through the minimal symbols in OBJF (and its separate debug
  155. objfiles) for a global (not file-local) minsym whose linkage name
  156. is NAME. This is somewhat similar to lookup_minimal_symbol_text,
  157. only data symbols (not text symbols) are considered, and a non-NULL
  158. objfile is not accepted. Returns a bound minimal symbol that
  159. matches, or an "empty" bound minimal symbol otherwise. */
  160. extern struct bound_minimal_symbol lookup_minimal_symbol_linkage
  161. (const char *name, struct objfile *objf)
  162. ATTRIBUTE_NONNULL (1) ATTRIBUTE_NONNULL (2);
  163. /* Look through all the current minimal symbol tables and find the
  164. first minimal symbol that matches NAME and PC. If OBJF is non-NULL,
  165. limit the search to that objfile. Returns a pointer to the minimal
  166. symbol that matches, or NULL if no match is found. */
  167. struct minimal_symbol *lookup_minimal_symbol_by_pc_name
  168. (CORE_ADDR, const char *, struct objfile *);
  169. enum class lookup_msym_prefer
  170. {
  171. /* Prefer mst_text symbols. */
  172. TEXT,
  173. /* Prefer mst_solib_trampoline symbols when there are text and
  174. trampoline symbols at the same address. Otherwise prefer
  175. mst_text symbols. */
  176. TRAMPOLINE,
  177. /* Prefer mst_text_gnu_ifunc symbols when there are text and ifunc
  178. symbols at the same address. Otherwise prefer mst_text
  179. symbols. */
  180. GNU_IFUNC,
  181. };
  182. /* Search through the minimal symbol table for each objfile and find
  183. the symbol whose address is the largest address that is still less
  184. than or equal to PC_IN, and which matches SECTION. A matching symbol
  185. must either be zero sized and have address PC_IN, or PC_IN must fall
  186. within the range of addresses covered by the matching symbol.
  187. If SECTION is NULL, this uses the result of find_pc_section
  188. instead.
  189. The result has a non-NULL 'minsym' member if such a symbol is
  190. found, or NULL if PC is not in a suitable range.
  191. See definition of lookup_msym_prefer for description of PREFER. By
  192. default mst_text symbols are preferred.
  193. If the PREVIOUS pointer is non-NULL, and no matching symbol is found,
  194. then the contents will be set to reference the closest symbol before
  195. PC_IN. */
  196. struct bound_minimal_symbol lookup_minimal_symbol_by_pc_section
  197. (CORE_ADDR pc_in,
  198. struct obj_section *section,
  199. lookup_msym_prefer prefer = lookup_msym_prefer::TEXT,
  200. bound_minimal_symbol *previous = nullptr);
  201. /* Backward compatibility: search through the minimal symbol table
  202. for a matching PC (no section given).
  203. This is a wrapper that calls lookup_minimal_symbol_by_pc_section
  204. with a NULL section argument. */
  205. struct bound_minimal_symbol lookup_minimal_symbol_by_pc (CORE_ADDR);
  206. /* Iterate over all the minimal symbols in the objfile OBJF which
  207. match NAME. Both the ordinary and demangled names of each symbol
  208. are considered. The caller is responsible for canonicalizing NAME,
  209. should that need to be done.
  210. For each matching symbol, CALLBACK is called with the symbol. */
  211. void iterate_over_minimal_symbols
  212. (struct objfile *objf, const lookup_name_info &name,
  213. gdb::function_view<bool (struct minimal_symbol *)> callback);
  214. /* Compute the upper bound of MINSYM. The upper bound is the last
  215. address thought to be part of the symbol. If the symbol has a
  216. size, it is used. Otherwise use the lesser of the next minimal
  217. symbol in the same section, or the end of the section, as the end
  218. of the function. */
  219. CORE_ADDR minimal_symbol_upper_bound (struct bound_minimal_symbol minsym);
  220. /* Return the type of MSYMBOL, a minimal symbol of OBJFILE. If
  221. ADDRESS_P is not NULL, set it to the MSYMBOL's resolved
  222. address. */
  223. type *find_minsym_type_and_address (minimal_symbol *msymbol, objfile *objf,
  224. CORE_ADDR *address_p);
  225. #endif /* MINSYMS_H */