1 /* Minimal symbol table definitions for GDB.
3 Copyright (C) 2011-2024 Free Software Foundation, Inc.
5 This file is part of GDB.
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.
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.
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/>. */
26 /* Several lookup functions return both a minimal symbol and the
27 objfile in which it is found. This structure is used in these
30 struct bound_minimal_symbol
32 bound_minimal_symbol (struct minimal_symbol
*msym
, struct objfile
*objf
)
38 bound_minimal_symbol () = default;
40 /* Return the address of the minimal symbol in the context of the objfile. */
42 CORE_ADDR
value_address () const
44 return this->minsym
->value_address (this->objfile
);
47 /* The minimal symbol that was found, or NULL if no minimal symbol
50 struct minimal_symbol
*minsym
= nullptr;
52 /* If MINSYM is not NULL, then this is the objfile in which the
55 struct objfile
*objfile
= nullptr;
57 /* Return the obj_section from OBJFILE for MINSYM. */
59 struct obj_section
*obj_section () const
61 return minsym
->obj_section (objfile
);
65 /* This header declares most of the API for dealing with minimal
66 symbols and minimal symbol tables. A few things are declared
69 A minimal symbol is a symbol for which there is no direct debug
70 information. For example, for an ELF binary, minimal symbols are
71 created from the ELF symbol table.
73 For the definition of the minimal symbol structure, see struct
74 minimal_symbol in symtab.h.
76 Minimal symbols are stored in tables attached to an objfile; see
77 objfiles.h for details. Code should generally treat these tables
78 as opaque and use functions provided by minsyms.c to inspect them.
83 /* An RAII-based object that is used to record minimal symbols while
84 they are being read. */
85 class minimal_symbol_reader
89 /* Prepare to start collecting minimal symbols. This should be
90 called by a symbol reader to initialize the minimal symbol
93 explicit minimal_symbol_reader (struct objfile
*);
95 ~minimal_symbol_reader ();
97 /* Install the minimal symbols that have been collected into the
102 /* Record a new minimal symbol. This is the "full" entry point;
103 simpler convenience entry points are also provided below.
105 This returns a new minimal symbol. It is ok to modify the returned
106 minimal symbol (though generally not necessary). It is not ok,
107 though, to stash the pointer anywhere; as minimal symbols may be
108 moved after creation. The memory for the returned minimal symbol
109 is still owned by the minsyms.c code, and should not be freed.
113 NAME - the symbol's name
114 COPY_NAME - if true, the minsym code must make a copy of NAME. If
115 false, then NAME must be NUL-terminated, and must have a lifetime
116 that is at least as long as OBJFILE's lifetime.
117 ADDRESS - the address of the symbol
118 MS_TYPE - the type of the symbol
119 SECTION - the symbol's section
122 struct minimal_symbol
*record_full (std::string_view name
,
124 unrelocated_addr address
,
125 enum minimal_symbol_type ms_type
,
128 /* Like record_full, but:
129 - computes the length of NAME
130 - passes COPY_NAME = true,
131 - and passes a default SECTION, depending on the type
133 This variant does not return the new symbol. */
135 void record (const char *name
, unrelocated_addr address
,
136 enum minimal_symbol_type ms_type
);
138 /* Like record_full, but:
139 - computes the length of NAME
140 - passes COPY_NAME = true.
142 This variant does not return the new symbol. */
144 void record_with_info (const char *name
, unrelocated_addr address
,
145 enum minimal_symbol_type ms_type
,
148 record_full (name
, true, address
, ms_type
, section
);
153 DISABLE_COPY_AND_ASSIGN (minimal_symbol_reader
);
155 struct objfile
*m_objfile
;
157 /* Bunch currently being filled up.
158 The next field points to chain of filled bunches. */
160 struct msym_bunch
*m_msym_bunch
;
162 /* Number of slots filled in current bunch. */
164 int m_msym_bunch_index
;
166 /* Total number of minimal symbols recorded so far for the
174 /* Return whether MSYMBOL is a function/method. If FUNC_ADDRESS_P is
175 non-NULL, and the MSYMBOL is a function, then *FUNC_ADDRESS_P is
176 set to the function's address, already resolved if MINSYM points to
177 a function descriptor. */
179 bool msymbol_is_function (struct objfile
*objfile
,
180 minimal_symbol
*minsym
,
181 CORE_ADDR
*func_address_p
= NULL
);
183 /* Compute a hash code for the string argument. Unlike htab_hash_string,
184 this is a case-insensitive hash to support "set case-sensitive off". */
186 unsigned int msymbol_hash (const char *);
188 /* Like msymbol_hash, but compute a hash code that is compatible with
191 unsigned int msymbol_hash_iw (const char *);
193 /* Compute the next hash value from previous HASH and the character C. This
194 is only a GDB in-memory computed value with no external files compatibility
197 #define SYMBOL_HASH_NEXT(hash, c) \
198 ((hash) * 67 + TOLOWER ((unsigned char) (c)) - 113)
202 /* Look through all the current minimal symbol tables and find the
203 first minimal symbol that matches NAME. If OBJF is non-NULL, limit
204 the search to that objfile. If SFILE is non-NULL, the only
205 file-scope symbols considered will be from that source file (global
206 symbols are still preferred). Returns a bound minimal symbol that
207 matches, or an empty bound minimal symbol if no match is found. */
209 bound_minimal_symbol
lookup_minimal_symbol (program_space
*pspace
,
211 objfile
*obj
= nullptr,
212 const char *sfile
= nullptr);
214 /* Look through all the minimal symbol tables in PSPACE and find the
215 first minimal symbol that matches NAME and has text type. If OBJF
216 is non-NULL, limit the search to that objfile. Returns a bound
217 minimal symbol that matches, or an "empty" bound minimal symbol
220 This function only searches the mangled (linkage) names. */
222 bound_minimal_symbol
lookup_minimal_symbol_text (program_space
*pspace
,
226 /* Look through the minimal symbols in OBJF (and its separate debug
227 objfiles) for a global (not file-local) minsym whose linkage name
228 is NAME. This is somewhat similar to lookup_minimal_symbol_text,
229 only data symbols (not text symbols) are considered, and a non-NULL
230 objfile is not accepted. The boolean argument allows matching the
231 static types of data symbols also. Returns a bound minimal symbol
232 that matches, or an "empty" bound minimal symbol otherwise. */
234 extern bound_minimal_symbol lookup_minimal_symbol_linkage
235 (const char *name
, struct objfile
*objf
, bool match_static_type
)
236 ATTRIBUTE_NONNULL (1) ATTRIBUTE_NONNULL (2);
238 /* A variant of lookup_minimal_symbol_linkage that iterates over all
239 objfiles of PSPACE. If ONLY_MAIN is true, then only an objfile with
240 OBJF_MAINLINE will be considered. */
242 extern bound_minimal_symbol lookup_minimal_symbol_linkage
243 (program_space
*pspace
, const char *name
, bool only_main
)
244 ATTRIBUTE_NONNULL (1);
246 /* Look through all the current minimal symbol tables and find the
247 first minimal symbol that matches NAME and PC. If OBJF is non-NULL,
248 limit the search to that objfile. Returns a pointer to the minimal
249 symbol that matches, or NULL if no match is found. */
251 struct minimal_symbol
*lookup_minimal_symbol_by_pc_name
252 (CORE_ADDR
, const char *, struct objfile
*);
254 enum class lookup_msym_prefer
256 /* Prefer mst_text symbols. */
259 /* Prefer mst_solib_trampoline symbols when there are text and
260 trampoline symbols at the same address. Otherwise prefer
264 /* Prefer mst_text_gnu_ifunc symbols when there are text and ifunc
265 symbols at the same address. Otherwise prefer mst_text
270 /* Search through the minimal symbol table for each objfile and find
271 the symbol whose address is the largest address that is still less
272 than or equal to PC_IN, and which matches SECTION. A matching symbol
273 must either be zero sized and have address PC_IN, or PC_IN must fall
274 within the range of addresses covered by the matching symbol.
276 If SECTION is NULL, this uses the result of find_pc_section
279 The result has a non-NULL 'minsym' member if such a symbol is
280 found, or NULL if PC is not in a suitable range.
282 See definition of lookup_msym_prefer for description of PREFER. By
283 default mst_text symbols are preferred.
285 If the PREVIOUS pointer is non-NULL, and no matching symbol is found,
286 then the contents will be set to reference the closest symbol before
289 bound_minimal_symbol lookup_minimal_symbol_by_pc_section
291 struct obj_section
*section
,
292 lookup_msym_prefer prefer
= lookup_msym_prefer::TEXT
,
293 bound_minimal_symbol
*previous
= nullptr);
295 /* Backward compatibility: search through the minimal symbol table
296 for a matching PC (no section given).
298 This is a wrapper that calls lookup_minimal_symbol_by_pc_section
299 with a NULL section argument. */
301 bound_minimal_symbol
lookup_minimal_symbol_by_pc (CORE_ADDR
);
303 /* Iterate over all the minimal symbols in the objfile OBJF which
304 match NAME. Both the ordinary and demangled names of each symbol
305 are considered. The caller is responsible for canonicalizing NAME,
306 should that need to be done.
308 For each matching symbol, CALLBACK is called with the symbol. */
310 void iterate_over_minimal_symbols
311 (struct objfile
*objf
, const lookup_name_info
&name
,
312 gdb::function_view
<bool (struct minimal_symbol
*)> callback
);
314 /* Compute the upper bound of MINSYM. The upper bound is the last
315 address thought to be part of the symbol. If the symbol has a
316 size, it is used. Otherwise use the lesser of the next minimal
317 symbol in the same section, or the end of the section, as the end
320 CORE_ADDR
minimal_symbol_upper_bound (bound_minimal_symbol minsym
);
322 /* Return the type of MSYMBOL, a minimal symbol of OBJFILE. If
323 ADDRESS_P is not NULL, set it to the MSYMBOL's resolved
326 type
*find_minsym_type_and_address (minimal_symbol
*msymbol
, objfile
*objf
,
327 CORE_ADDR
*address_p
);
329 #endif /* MINSYMS_H */