gdb/tui: fairer distribution of excess space during apply
[binutils-gdb.git] / gdb / minsyms.h
1 /* Minimal symbol table definitions for GDB.
2
3 Copyright (C) 2011-2022 Free Software Foundation, Inc.
4
5 This file is part of GDB.
6
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 3 of the License, or
10 (at your option) any later version.
11
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
16
17 You should have received a copy of the GNU General Public License
18 along with this program. If not, see <http://www.gnu.org/licenses/>. */
19
20 #ifndef MINSYMS_H
21 #define MINSYMS_H
22
23 struct type;
24
25 /* Several lookup functions return both a minimal symbol and the
26 objfile in which it is found. This structure is used in these
27 cases. */
28
29 struct bound_minimal_symbol
30 {
31 bound_minimal_symbol (struct minimal_symbol *msym, struct objfile *objf)
32 : minsym (msym),
33 objfile (objf)
34 {
35 }
36
37 bound_minimal_symbol () = default;
38
39 /* The minimal symbol that was found, or NULL if no minimal symbol
40 was found. */
41
42 struct minimal_symbol *minsym = nullptr;
43
44 /* If MINSYM is not NULL, then this is the objfile in which the
45 symbol is defined. */
46
47 struct objfile *objfile = nullptr;
48
49 /* Return the obj_section from OBJFILE for MINSYM. */
50
51 struct obj_section *obj_section () const
52 {
53 return minsym->obj_section (objfile);
54 }
55 };
56
57 /* This header declares most of the API for dealing with minimal
58 symbols and minimal symbol tables. A few things are declared
59 elsewhere; see below.
60
61 A minimal symbol is a symbol for which there is no direct debug
62 information. For example, for an ELF binary, minimal symbols are
63 created from the ELF symbol table.
64
65 For the definition of the minimal symbol structure, see struct
66 minimal_symbol in symtab.h.
67
68 Minimal symbols are stored in tables attached to an objfile; see
69 objfiles.h for details. Code should generally treat these tables
70 as opaque and use functions provided by minsyms.c to inspect them.
71 */
72
73 struct msym_bunch;
74
75 /* An RAII-based object that is used to record minimal symbols while
76 they are being read. */
77 class minimal_symbol_reader
78 {
79 public:
80
81 /* Prepare to start collecting minimal symbols. This should be
82 called by a symbol reader to initialize the minimal symbol
83 module. */
84
85 explicit minimal_symbol_reader (struct objfile *);
86
87 ~minimal_symbol_reader ();
88
89 /* Install the minimal symbols that have been collected into the
90 given objfile. */
91
92 void install ();
93
94 /* Record a new minimal symbol. This is the "full" entry point;
95 simpler convenience entry points are also provided below.
96
97 This returns a new minimal symbol. It is ok to modify the returned
98 minimal symbol (though generally not necessary). It is not ok,
99 though, to stash the pointer anywhere; as minimal symbols may be
100 moved after creation. The memory for the returned minimal symbol
101 is still owned by the minsyms.c code, and should not be freed.
102
103 Arguments are:
104
105 NAME - the symbol's name
106 COPY_NAME - if true, the minsym code must make a copy of NAME. If
107 false, then NAME must be NUL-terminated, and must have a lifetime
108 that is at least as long as OBJFILE's lifetime.
109 ADDRESS - the address of the symbol
110 MS_TYPE - the type of the symbol
111 SECTION - the symbol's section
112 */
113
114 struct minimal_symbol *record_full (gdb::string_view name,
115 bool copy_name,
116 CORE_ADDR address,
117 enum minimal_symbol_type ms_type,
118 int section);
119
120 /* Like record_full, but:
121 - computes the length of NAME
122 - passes COPY_NAME = true,
123 - and passes a default SECTION, depending on the type
124
125 This variant does not return the new symbol. */
126
127 void record (const char *name, CORE_ADDR address,
128 enum minimal_symbol_type ms_type);
129
130 /* Like record_full, but:
131 - computes the length of NAME
132 - passes COPY_NAME = true.
133
134 This variant does not return the new symbol. */
135
136 void record_with_info (const char *name, CORE_ADDR address,
137 enum minimal_symbol_type ms_type,
138 int section)
139 {
140 record_full (name, true, address, ms_type, section);
141 }
142
143 private:
144
145 DISABLE_COPY_AND_ASSIGN (minimal_symbol_reader);
146
147 struct objfile *m_objfile;
148
149 /* Bunch currently being filled up.
150 The next field points to chain of filled bunches. */
151
152 struct msym_bunch *m_msym_bunch;
153
154 /* Number of slots filled in current bunch. */
155
156 int m_msym_bunch_index;
157
158 /* Total number of minimal symbols recorded so far for the
159 objfile. */
160
161 int m_msym_count;
162 };
163
164 \f
165
166 /* Return whether MSYMBOL is a function/method. If FUNC_ADDRESS_P is
167 non-NULL, and the MSYMBOL is a function, then *FUNC_ADDRESS_P is
168 set to the function's address, already resolved if MINSYM points to
169 a function descriptor. */
170
171 bool msymbol_is_function (struct objfile *objfile,
172 minimal_symbol *minsym,
173 CORE_ADDR *func_address_p = NULL);
174
175 /* Compute a hash code for the string argument. Unlike htab_hash_string,
176 this is a case-insensitive hash to support "set case-sensitive off". */
177
178 unsigned int msymbol_hash (const char *);
179
180 /* Like msymbol_hash, but compute a hash code that is compatible with
181 strcmp_iw. */
182
183 unsigned int msymbol_hash_iw (const char *);
184
185 /* Compute the next hash value from previous HASH and the character C. This
186 is only a GDB in-memory computed value with no external files compatibility
187 requirements. */
188
189 #define SYMBOL_HASH_NEXT(hash, c) \
190 ((hash) * 67 + TOLOWER ((unsigned char) (c)) - 113)
191
192 \f
193
194 /* Look through all the current minimal symbol tables and find the
195 first minimal symbol that matches NAME. If OBJF is non-NULL, limit
196 the search to that objfile. If SFILE is non-NULL, the only
197 file-scope symbols considered will be from that source file (global
198 symbols are still preferred). Returns a bound minimal symbol that
199 matches, or an empty bound minimal symbol if no match is found. */
200
201 struct bound_minimal_symbol lookup_minimal_symbol (const char *,
202 const char *,
203 struct objfile *);
204
205 /* Like lookup_minimal_symbol, but searches all files and
206 objfiles. */
207
208 struct bound_minimal_symbol lookup_bound_minimal_symbol (const char *);
209
210 /* Look through all the current minimal symbol tables and find the
211 first minimal symbol that matches NAME and has text type. If OBJF
212 is non-NULL, limit the search to that objfile. Returns a bound
213 minimal symbol that matches, or an "empty" bound minimal symbol
214 otherwise.
215
216 This function only searches the mangled (linkage) names. */
217
218 struct bound_minimal_symbol lookup_minimal_symbol_text (const char *,
219 struct objfile *);
220
221 /* Look through the minimal symbols in OBJF (and its separate debug
222 objfiles) for a global (not file-local) minsym whose linkage name
223 is NAME. This is somewhat similar to lookup_minimal_symbol_text,
224 only data symbols (not text symbols) are considered, and a non-NULL
225 objfile is not accepted. Returns a bound minimal symbol that
226 matches, or an "empty" bound minimal symbol otherwise. */
227
228 extern struct bound_minimal_symbol lookup_minimal_symbol_linkage
229 (const char *name, struct objfile *objf)
230 ATTRIBUTE_NONNULL (1) ATTRIBUTE_NONNULL (2);
231
232 /* Look through all the current minimal symbol tables and find the
233 first minimal symbol that matches NAME and PC. If OBJF is non-NULL,
234 limit the search to that objfile. Returns a pointer to the minimal
235 symbol that matches, or NULL if no match is found. */
236
237 struct minimal_symbol *lookup_minimal_symbol_by_pc_name
238 (CORE_ADDR, const char *, struct objfile *);
239
240 enum class lookup_msym_prefer
241 {
242 /* Prefer mst_text symbols. */
243 TEXT,
244
245 /* Prefer mst_solib_trampoline symbols when there are text and
246 trampoline symbols at the same address. Otherwise prefer
247 mst_text symbols. */
248 TRAMPOLINE,
249
250 /* Prefer mst_text_gnu_ifunc symbols when there are text and ifunc
251 symbols at the same address. Otherwise prefer mst_text
252 symbols. */
253 GNU_IFUNC,
254 };
255
256 /* Search through the minimal symbol table for each objfile and find
257 the symbol whose address is the largest address that is still less
258 than or equal to PC_IN, and which matches SECTION. A matching symbol
259 must either be zero sized and have address PC_IN, or PC_IN must fall
260 within the range of addresses covered by the matching symbol.
261
262 If SECTION is NULL, this uses the result of find_pc_section
263 instead.
264
265 The result has a non-NULL 'minsym' member if such a symbol is
266 found, or NULL if PC is not in a suitable range.
267
268 See definition of lookup_msym_prefer for description of PREFER. By
269 default mst_text symbols are preferred.
270
271 If the PREVIOUS pointer is non-NULL, and no matching symbol is found,
272 then the contents will be set to reference the closest symbol before
273 PC_IN. */
274
275 struct bound_minimal_symbol lookup_minimal_symbol_by_pc_section
276 (CORE_ADDR pc_in,
277 struct obj_section *section,
278 lookup_msym_prefer prefer = lookup_msym_prefer::TEXT,
279 bound_minimal_symbol *previous = nullptr);
280
281 /* Backward compatibility: search through the minimal symbol table
282 for a matching PC (no section given).
283
284 This is a wrapper that calls lookup_minimal_symbol_by_pc_section
285 with a NULL section argument. */
286
287 struct bound_minimal_symbol lookup_minimal_symbol_by_pc (CORE_ADDR);
288
289 /* Iterate over all the minimal symbols in the objfile OBJF which
290 match NAME. Both the ordinary and demangled names of each symbol
291 are considered. The caller is responsible for canonicalizing NAME,
292 should that need to be done.
293
294 For each matching symbol, CALLBACK is called with the symbol. */
295
296 void iterate_over_minimal_symbols
297 (struct objfile *objf, const lookup_name_info &name,
298 gdb::function_view<bool (struct minimal_symbol *)> callback);
299
300 /* Compute the upper bound of MINSYM. The upper bound is the last
301 address thought to be part of the symbol. If the symbol has a
302 size, it is used. Otherwise use the lesser of the next minimal
303 symbol in the same section, or the end of the section, as the end
304 of the function. */
305
306 CORE_ADDR minimal_symbol_upper_bound (struct bound_minimal_symbol minsym);
307
308 /* Return the type of MSYMBOL, a minimal symbol of OBJFILE. If
309 ADDRESS_P is not NULL, set it to the MSYMBOL's resolved
310 address. */
311
312 type *find_minsym_type_and_address (minimal_symbol *msymbol, objfile *objf,
313 CORE_ADDR *address_p);
314
315 #endif /* MINSYMS_H */