1e93f7393Sniklas /* GDB routines for manipulating the minimal symbol tables.
2b725ae77Skettenis Copyright 1992, 1993, 1994, 1995, 1996, 1997, 1998, 1999, 2000, 2001,
3b725ae77Skettenis 2002, 2003, 2004
4b725ae77Skettenis Free Software Foundation, Inc.
5e93f7393Sniklas Contributed by Cygnus Support, using pieces from other GDB modules.
6e93f7393Sniklas
7e93f7393Sniklas This file is part of GDB.
8e93f7393Sniklas
9e93f7393Sniklas This program is free software; you can redistribute it and/or modify
10e93f7393Sniklas it under the terms of the GNU General Public License as published by
11e93f7393Sniklas the Free Software Foundation; either version 2 of the License, or
12e93f7393Sniklas (at your option) any later version.
13e93f7393Sniklas
14e93f7393Sniklas This program is distributed in the hope that it will be useful,
15e93f7393Sniklas but WITHOUT ANY WARRANTY; without even the implied warranty of
16e93f7393Sniklas MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17e93f7393Sniklas GNU General Public License for more details.
18e93f7393Sniklas
19e93f7393Sniklas You should have received a copy of the GNU General Public License
20e93f7393Sniklas along with this program; if not, write to the Free Software
21b725ae77Skettenis Foundation, Inc., 59 Temple Place - Suite 330,
22b725ae77Skettenis Boston, MA 02111-1307, USA. */
23e93f7393Sniklas
24e93f7393Sniklas
25e93f7393Sniklas /* This file contains support routines for creating, manipulating, and
26e93f7393Sniklas destroying minimal symbol tables.
27e93f7393Sniklas
28e93f7393Sniklas Minimal symbol tables are used to hold some very basic information about
29e93f7393Sniklas all defined global symbols (text, data, bss, abs, etc). The only two
30e93f7393Sniklas required pieces of information are the symbol's name and the address
31e93f7393Sniklas associated with that symbol.
32e93f7393Sniklas
33e93f7393Sniklas In many cases, even if a file was compiled with no special options for
34e93f7393Sniklas debugging at all, as long as was not stripped it will contain sufficient
35e93f7393Sniklas information to build useful minimal symbol tables using this structure.
36e93f7393Sniklas
37e93f7393Sniklas Even when a file contains enough debugging information to build a full
38e93f7393Sniklas symbol table, these minimal symbols are still useful for quickly mapping
39e93f7393Sniklas between names and addresses, and vice versa. They are also sometimes used
40e93f7393Sniklas to figure out what full symbol table entries need to be read in. */
41e93f7393Sniklas
42e93f7393Sniklas
43e93f7393Sniklas #include "defs.h"
44b725ae77Skettenis #include <ctype.h>
45e93f7393Sniklas #include "gdb_string.h"
46e93f7393Sniklas #include "symtab.h"
47e93f7393Sniklas #include "bfd.h"
48e93f7393Sniklas #include "symfile.h"
49e93f7393Sniklas #include "objfiles.h"
50e93f7393Sniklas #include "demangle.h"
51b725ae77Skettenis #include "value.h"
52b725ae77Skettenis #include "cp-abi.h"
53e93f7393Sniklas
54e93f7393Sniklas /* Accumulate the minimal symbols for each objfile in bunches of BUNCH_SIZE.
55e93f7393Sniklas At the end, copy them all into one newly allocated location on an objfile's
56e93f7393Sniklas symbol obstack. */
57e93f7393Sniklas
58e93f7393Sniklas #define BUNCH_SIZE 127
59e93f7393Sniklas
60e93f7393Sniklas struct msym_bunch
61e93f7393Sniklas {
62e93f7393Sniklas struct msym_bunch *next;
63e93f7393Sniklas struct minimal_symbol contents[BUNCH_SIZE];
64e93f7393Sniklas };
65e93f7393Sniklas
66e93f7393Sniklas /* Bunch currently being filled up.
67e93f7393Sniklas The next field points to chain of filled bunches. */
68e93f7393Sniklas
69e93f7393Sniklas static struct msym_bunch *msym_bunch;
70e93f7393Sniklas
71e93f7393Sniklas /* Number of slots filled in current bunch. */
72e93f7393Sniklas
73e93f7393Sniklas static int msym_bunch_index;
74e93f7393Sniklas
75e93f7393Sniklas /* Total number of minimal symbols recorded so far for the objfile. */
76e93f7393Sniklas
77e93f7393Sniklas static int msym_count;
78e93f7393Sniklas
79b725ae77Skettenis /* Compute a hash code based using the same criteria as `strcmp_iw'. */
80e93f7393Sniklas
81b725ae77Skettenis unsigned int
msymbol_hash_iw(const char * string)82b725ae77Skettenis msymbol_hash_iw (const char *string)
83b725ae77Skettenis {
84b725ae77Skettenis unsigned int hash = 0;
85b725ae77Skettenis while (*string && *string != '(')
86b725ae77Skettenis {
87b725ae77Skettenis while (isspace (*string))
88b725ae77Skettenis ++string;
89b725ae77Skettenis if (*string && *string != '(')
90b725ae77Skettenis {
91b725ae77Skettenis hash = hash * 67 + *string - 113;
92b725ae77Skettenis ++string;
93b725ae77Skettenis }
94b725ae77Skettenis }
95b725ae77Skettenis return hash;
96b725ae77Skettenis }
97e93f7393Sniklas
98b725ae77Skettenis /* Compute a hash code for a string. */
99b725ae77Skettenis
100b725ae77Skettenis unsigned int
msymbol_hash(const char * string)101b725ae77Skettenis msymbol_hash (const char *string)
102b725ae77Skettenis {
103b725ae77Skettenis unsigned int hash = 0;
104b725ae77Skettenis for (; *string; ++string)
105b725ae77Skettenis hash = hash * 67 + *string - 113;
106b725ae77Skettenis return hash;
107b725ae77Skettenis }
108b725ae77Skettenis
109b725ae77Skettenis /* Add the minimal symbol SYM to an objfile's minsym hash table, TABLE. */
110b725ae77Skettenis void
add_minsym_to_hash_table(struct minimal_symbol * sym,struct minimal_symbol ** table)111b725ae77Skettenis add_minsym_to_hash_table (struct minimal_symbol *sym,
112b725ae77Skettenis struct minimal_symbol **table)
113b725ae77Skettenis {
114b725ae77Skettenis if (sym->hash_next == NULL)
115b725ae77Skettenis {
116b725ae77Skettenis unsigned int hash
117b725ae77Skettenis = msymbol_hash (SYMBOL_LINKAGE_NAME (sym)) % MINIMAL_SYMBOL_HASH_SIZE;
118b725ae77Skettenis sym->hash_next = table[hash];
119b725ae77Skettenis table[hash] = sym;
120b725ae77Skettenis }
121b725ae77Skettenis }
122b725ae77Skettenis
123b725ae77Skettenis /* Add the minimal symbol SYM to an objfile's minsym demangled hash table,
124b725ae77Skettenis TABLE. */
125b725ae77Skettenis static void
add_minsym_to_demangled_hash_table(struct minimal_symbol * sym,struct minimal_symbol ** table)126b725ae77Skettenis add_minsym_to_demangled_hash_table (struct minimal_symbol *sym,
127b725ae77Skettenis struct minimal_symbol **table)
128b725ae77Skettenis {
129b725ae77Skettenis if (sym->demangled_hash_next == NULL)
130b725ae77Skettenis {
131b725ae77Skettenis unsigned int hash = msymbol_hash_iw (SYMBOL_DEMANGLED_NAME (sym)) % MINIMAL_SYMBOL_HASH_SIZE;
132b725ae77Skettenis sym->demangled_hash_next = table[hash];
133b725ae77Skettenis table[hash] = sym;
134b725ae77Skettenis }
135b725ae77Skettenis }
136b725ae77Skettenis
137e93f7393Sniklas
138e93f7393Sniklas /* Look through all the current minimal symbol tables and find the
139e93f7393Sniklas first minimal symbol that matches NAME. If OBJF is non-NULL, limit
140b725ae77Skettenis the search to that objfile. If SFILE is non-NULL, the only file-scope
141b725ae77Skettenis symbols considered will be from that source file (global symbols are
142b725ae77Skettenis still preferred). Returns a pointer to the minimal symbol that
143e93f7393Sniklas matches, or NULL if no match is found.
144e93f7393Sniklas
145e93f7393Sniklas Note: One instance where there may be duplicate minimal symbols with
146e93f7393Sniklas the same name is when the symbol tables for a shared library and the
147e93f7393Sniklas symbol tables for an executable contain global symbols with the same
148*63addd46Skettenis names (the dynamic linker deals with the duplication).
149*63addd46Skettenis
150*63addd46Skettenis It's also possible to have minimal symbols with different mangled
151*63addd46Skettenis names, but identical demangled names. For example, the GNU C++ v3
152*63addd46Skettenis ABI requires the generation of two (or perhaps three) copies of
153*63addd46Skettenis constructor functions --- "in-charge", "not-in-charge", and
154*63addd46Skettenis "allocate" copies; destructors may be duplicated as well.
155*63addd46Skettenis Obviously, there must be distinct mangled names for each of these,
156*63addd46Skettenis but the demangled names are all the same: S::S or S::~S. */
157e93f7393Sniklas
158e93f7393Sniklas struct minimal_symbol *
lookup_minimal_symbol(const char * name,const char * sfile,struct objfile * objf)159b725ae77Skettenis lookup_minimal_symbol (const char *name, const char *sfile,
160b725ae77Skettenis struct objfile *objf)
161e93f7393Sniklas {
162e93f7393Sniklas struct objfile *objfile;
163e93f7393Sniklas struct minimal_symbol *msymbol;
164e93f7393Sniklas struct minimal_symbol *found_symbol = NULL;
165e93f7393Sniklas struct minimal_symbol *found_file_symbol = NULL;
166e93f7393Sniklas struct minimal_symbol *trampoline_symbol = NULL;
167e93f7393Sniklas
168b725ae77Skettenis unsigned int hash = msymbol_hash (name) % MINIMAL_SYMBOL_HASH_SIZE;
169b725ae77Skettenis unsigned int dem_hash = msymbol_hash_iw (name) % MINIMAL_SYMBOL_HASH_SIZE;
170b725ae77Skettenis
171e93f7393Sniklas #ifdef SOFUN_ADDRESS_MAYBE_MISSING
172e93f7393Sniklas if (sfile != NULL)
173e93f7393Sniklas {
174e93f7393Sniklas char *p = strrchr (sfile, '/');
175e93f7393Sniklas if (p != NULL)
176e93f7393Sniklas sfile = p + 1;
177e93f7393Sniklas }
178e93f7393Sniklas #endif
179e93f7393Sniklas
180e93f7393Sniklas for (objfile = object_files;
181e93f7393Sniklas objfile != NULL && found_symbol == NULL;
182e93f7393Sniklas objfile = objfile->next)
183e93f7393Sniklas {
184e93f7393Sniklas if (objf == NULL || objf == objfile)
185e93f7393Sniklas {
186b725ae77Skettenis /* Do two passes: the first over the ordinary hash table,
187b725ae77Skettenis and the second over the demangled hash table. */
188b725ae77Skettenis int pass;
189b725ae77Skettenis
190b725ae77Skettenis for (pass = 1; pass <= 2 && found_symbol == NULL; pass++)
191e93f7393Sniklas {
192b725ae77Skettenis /* Select hash list according to pass. */
193b725ae77Skettenis if (pass == 1)
194b725ae77Skettenis msymbol = objfile->msymbol_hash[hash];
195b725ae77Skettenis else
196b725ae77Skettenis msymbol = objfile->msymbol_demangled_hash[dem_hash];
197b725ae77Skettenis
198b725ae77Skettenis while (msymbol != NULL && found_symbol == NULL)
199b725ae77Skettenis {
200b725ae77Skettenis /* FIXME: carlton/2003-02-27: This is an unholy
201b725ae77Skettenis mixture of linkage names and natural names. If
202b725ae77Skettenis you want to test the linkage names with strcmp,
203b725ae77Skettenis do that. If you want to test the natural names
204b725ae77Skettenis with strcmp_iw, use SYMBOL_MATCHES_NATURAL_NAME. */
205b725ae77Skettenis if (strcmp (DEPRECATED_SYMBOL_NAME (msymbol), (name)) == 0
206b725ae77Skettenis || (SYMBOL_DEMANGLED_NAME (msymbol) != NULL
207b725ae77Skettenis && strcmp_iw (SYMBOL_DEMANGLED_NAME (msymbol),
208b725ae77Skettenis (name)) == 0))
209e93f7393Sniklas {
210e93f7393Sniklas switch (MSYMBOL_TYPE (msymbol))
211e93f7393Sniklas {
212e93f7393Sniklas case mst_file_text:
213e93f7393Sniklas case mst_file_data:
214e93f7393Sniklas case mst_file_bss:
215e93f7393Sniklas #ifdef SOFUN_ADDRESS_MAYBE_MISSING
216b725ae77Skettenis if (sfile == NULL
217b725ae77Skettenis || strcmp (msymbol->filename, sfile) == 0)
218e93f7393Sniklas found_file_symbol = msymbol;
219e93f7393Sniklas #else
220e93f7393Sniklas /* We have neither the ability nor the need to
221e93f7393Sniklas deal with the SFILE parameter. If we find
222e93f7393Sniklas more than one symbol, just return the latest
223e93f7393Sniklas one (the user can't expect useful behavior in
224e93f7393Sniklas that case). */
225e93f7393Sniklas found_file_symbol = msymbol;
226e93f7393Sniklas #endif
227e93f7393Sniklas break;
228e93f7393Sniklas
229e93f7393Sniklas case mst_solib_trampoline:
230e93f7393Sniklas
231e93f7393Sniklas /* If a trampoline symbol is found, we prefer to
232e93f7393Sniklas keep looking for the *real* symbol. If the
233e93f7393Sniklas actual symbol is not found, then we'll use the
234e93f7393Sniklas trampoline entry. */
235e93f7393Sniklas if (trampoline_symbol == NULL)
236e93f7393Sniklas trampoline_symbol = msymbol;
237e93f7393Sniklas break;
238e93f7393Sniklas
239e93f7393Sniklas case mst_unknown:
240e93f7393Sniklas default:
241e93f7393Sniklas found_symbol = msymbol;
242e93f7393Sniklas break;
243e93f7393Sniklas }
244e93f7393Sniklas }
245b725ae77Skettenis
246b725ae77Skettenis /* Find the next symbol on the hash chain. */
247b725ae77Skettenis if (pass == 1)
248b725ae77Skettenis msymbol = msymbol->hash_next;
249b725ae77Skettenis else
250b725ae77Skettenis msymbol = msymbol->demangled_hash_next;
251b725ae77Skettenis }
252e93f7393Sniklas }
253e93f7393Sniklas }
254e93f7393Sniklas }
255e93f7393Sniklas /* External symbols are best. */
256e93f7393Sniklas if (found_symbol)
257e93f7393Sniklas return found_symbol;
258e93f7393Sniklas
259e93f7393Sniklas /* File-local symbols are next best. */
260e93f7393Sniklas if (found_file_symbol)
261e93f7393Sniklas return found_file_symbol;
262e93f7393Sniklas
263e93f7393Sniklas /* Symbols for shared library trampolines are next best. */
264e93f7393Sniklas if (trampoline_symbol)
265e93f7393Sniklas return trampoline_symbol;
266e93f7393Sniklas
267e93f7393Sniklas return NULL;
268e93f7393Sniklas }
269e93f7393Sniklas
270e93f7393Sniklas /* Look through all the current minimal symbol tables and find the
271b725ae77Skettenis first minimal symbol that matches NAME and has text type. If OBJF
272b725ae77Skettenis is non-NULL, limit the search to that objfile. Returns a pointer
273b725ae77Skettenis to the minimal symbol that matches, or NULL if no match is found.
274b725ae77Skettenis
275b725ae77Skettenis This function only searches the mangled (linkage) names. */
276e93f7393Sniklas
277e93f7393Sniklas struct minimal_symbol *
lookup_minimal_symbol_text(const char * name,struct objfile * objf)278b725ae77Skettenis lookup_minimal_symbol_text (const char *name, struct objfile *objf)
279e93f7393Sniklas {
280e93f7393Sniklas struct objfile *objfile;
281e93f7393Sniklas struct minimal_symbol *msymbol;
282e93f7393Sniklas struct minimal_symbol *found_symbol = NULL;
283e93f7393Sniklas struct minimal_symbol *found_file_symbol = NULL;
284e93f7393Sniklas
285b725ae77Skettenis unsigned int hash = msymbol_hash (name) % MINIMAL_SYMBOL_HASH_SIZE;
286e93f7393Sniklas
287e93f7393Sniklas for (objfile = object_files;
288e93f7393Sniklas objfile != NULL && found_symbol == NULL;
289e93f7393Sniklas objfile = objfile->next)
290e93f7393Sniklas {
291e93f7393Sniklas if (objf == NULL || objf == objfile)
292e93f7393Sniklas {
293b725ae77Skettenis for (msymbol = objfile->msymbol_hash[hash];
294b725ae77Skettenis msymbol != NULL && found_symbol == NULL;
295b725ae77Skettenis msymbol = msymbol->hash_next)
296e93f7393Sniklas {
297b725ae77Skettenis if (strcmp (SYMBOL_LINKAGE_NAME (msymbol), name) == 0 &&
298e93f7393Sniklas (MSYMBOL_TYPE (msymbol) == mst_text ||
299e93f7393Sniklas MSYMBOL_TYPE (msymbol) == mst_file_text))
300e93f7393Sniklas {
301e93f7393Sniklas switch (MSYMBOL_TYPE (msymbol))
302e93f7393Sniklas {
303e93f7393Sniklas case mst_file_text:
304e93f7393Sniklas found_file_symbol = msymbol;
305e93f7393Sniklas break;
306e93f7393Sniklas default:
307e93f7393Sniklas found_symbol = msymbol;
308e93f7393Sniklas break;
309e93f7393Sniklas }
310e93f7393Sniklas }
311e93f7393Sniklas }
312e93f7393Sniklas }
313e93f7393Sniklas }
314e93f7393Sniklas /* External symbols are best. */
315e93f7393Sniklas if (found_symbol)
316e93f7393Sniklas return found_symbol;
317e93f7393Sniklas
318e93f7393Sniklas /* File-local symbols are next best. */
319e93f7393Sniklas if (found_file_symbol)
320e93f7393Sniklas return found_file_symbol;
321e93f7393Sniklas
322e93f7393Sniklas return NULL;
323e93f7393Sniklas }
324e93f7393Sniklas
325e93f7393Sniklas /* Look through all the current minimal symbol tables and find the
326b725ae77Skettenis first minimal symbol that matches NAME and is a solib trampoline.
327b725ae77Skettenis If OBJF is non-NULL, limit the search to that objfile. Returns a
328b725ae77Skettenis pointer to the minimal symbol that matches, or NULL if no match is
329b725ae77Skettenis found.
330b725ae77Skettenis
331b725ae77Skettenis This function only searches the mangled (linkage) names. */
332e93f7393Sniklas
333e93f7393Sniklas struct minimal_symbol *
lookup_minimal_symbol_solib_trampoline(const char * name,struct objfile * objf)334b725ae77Skettenis lookup_minimal_symbol_solib_trampoline (const char *name,
335b725ae77Skettenis struct objfile *objf)
336e93f7393Sniklas {
337e93f7393Sniklas struct objfile *objfile;
338e93f7393Sniklas struct minimal_symbol *msymbol;
339e93f7393Sniklas struct minimal_symbol *found_symbol = NULL;
340e93f7393Sniklas
341b725ae77Skettenis unsigned int hash = msymbol_hash (name) % MINIMAL_SYMBOL_HASH_SIZE;
342e93f7393Sniklas
343e93f7393Sniklas for (objfile = object_files;
344e93f7393Sniklas objfile != NULL && found_symbol == NULL;
345e93f7393Sniklas objfile = objfile->next)
346e93f7393Sniklas {
347e93f7393Sniklas if (objf == NULL || objf == objfile)
348e93f7393Sniklas {
349b725ae77Skettenis for (msymbol = objfile->msymbol_hash[hash];
350b725ae77Skettenis msymbol != NULL && found_symbol == NULL;
351b725ae77Skettenis msymbol = msymbol->hash_next)
352e93f7393Sniklas {
353b725ae77Skettenis if (strcmp (SYMBOL_LINKAGE_NAME (msymbol), name) == 0 &&
354e93f7393Sniklas MSYMBOL_TYPE (msymbol) == mst_solib_trampoline)
355e93f7393Sniklas return msymbol;
356e93f7393Sniklas }
357e93f7393Sniklas }
358e93f7393Sniklas }
359e93f7393Sniklas
360e93f7393Sniklas return NULL;
361e93f7393Sniklas }
362e93f7393Sniklas
363e93f7393Sniklas
364b725ae77Skettenis /* Search through the minimal symbol table for each objfile and find
365b725ae77Skettenis the symbol whose address is the largest address that is still less
366b725ae77Skettenis than or equal to PC, and matches SECTION (if non-NULL). Returns a
367b725ae77Skettenis pointer to the minimal symbol if such a symbol is found, or NULL if
368b725ae77Skettenis PC is not in a suitable range. Note that we need to look through
369b725ae77Skettenis ALL the minimal symbol tables before deciding on the symbol that
370b725ae77Skettenis comes closest to the specified PC. This is because objfiles can
371b725ae77Skettenis overlap, for example objfile A has .text at 0x100 and .data at
372b725ae77Skettenis 0x40000 and objfile B has .text at 0x234 and .data at 0x40048. */
373e93f7393Sniklas
374e93f7393Sniklas struct minimal_symbol *
lookup_minimal_symbol_by_pc_section(CORE_ADDR pc,asection * section)375b725ae77Skettenis lookup_minimal_symbol_by_pc_section (CORE_ADDR pc, asection *section)
376e93f7393Sniklas {
377b725ae77Skettenis int lo;
378b725ae77Skettenis int hi;
379b725ae77Skettenis int new;
380b725ae77Skettenis struct objfile *objfile;
381b725ae77Skettenis struct minimal_symbol *msymbol;
382b725ae77Skettenis struct minimal_symbol *best_symbol = NULL;
383b725ae77Skettenis struct obj_section *pc_section;
384e93f7393Sniklas
385b725ae77Skettenis /* PC has to be in a known section. This ensures that anything
386b725ae77Skettenis beyond the end of the last segment doesn't appear to be part of
387b725ae77Skettenis the last function in the last segment. */
388b725ae77Skettenis pc_section = find_pc_section (pc);
389b725ae77Skettenis if (pc_section == NULL)
390e93f7393Sniklas return NULL;
391e93f7393Sniklas
392b725ae77Skettenis /* NOTE: cagney/2004-01-27: Removed code (added 2003-07-19) that was
393b725ae77Skettenis trying to force the PC into a valid section as returned by
394b725ae77Skettenis find_pc_section. It broke IRIX 6.5 mdebug which relies on this
395b725ae77Skettenis code returning an absolute symbol - the problem was that
396b725ae77Skettenis find_pc_section wasn't returning an absolute section and hence
397b725ae77Skettenis the code below would skip over absolute symbols. Since the
398b725ae77Skettenis original problem was with finding a frame's function, and that
399b725ae77Skettenis uses [indirectly] lookup_minimal_symbol_by_pc, the original
400b725ae77Skettenis problem has been fixed by having that function use
401b725ae77Skettenis find_pc_section. */
402b725ae77Skettenis
403e93f7393Sniklas for (objfile = object_files;
404e93f7393Sniklas objfile != NULL;
405e93f7393Sniklas objfile = objfile->next)
406e93f7393Sniklas {
407e93f7393Sniklas /* If this objfile has a minimal symbol table, go search it using
408e93f7393Sniklas a binary search. Note that a minimal symbol table always consists
409e93f7393Sniklas of at least two symbols, a "real" symbol and the terminating
410e93f7393Sniklas "null symbol". If there are no real symbols, then there is no
411e93f7393Sniklas minimal symbol table at all. */
412e93f7393Sniklas
413b725ae77Skettenis if (objfile->minimal_symbol_count > 0)
414e93f7393Sniklas {
415b725ae77Skettenis msymbol = objfile->msymbols;
416e93f7393Sniklas lo = 0;
417e93f7393Sniklas hi = objfile->minimal_symbol_count - 1;
418e93f7393Sniklas
419e93f7393Sniklas /* This code assumes that the minimal symbols are sorted by
420e93f7393Sniklas ascending address values. If the pc value is greater than or
421e93f7393Sniklas equal to the first symbol's address, then some symbol in this
422e93f7393Sniklas minimal symbol table is a suitable candidate for being the
423e93f7393Sniklas "best" symbol. This includes the last real symbol, for cases
424e93f7393Sniklas where the pc value is larger than any address in this vector.
425e93f7393Sniklas
426e93f7393Sniklas By iterating until the address associated with the current
427e93f7393Sniklas hi index (the endpoint of the test interval) is less than
428e93f7393Sniklas or equal to the desired pc value, we accomplish two things:
429e93f7393Sniklas (1) the case where the pc value is larger than any minimal
430e93f7393Sniklas symbol address is trivially solved, (2) the address associated
431e93f7393Sniklas with the hi index is always the one we want when the interation
432e93f7393Sniklas terminates. In essence, we are iterating the test interval
433e93f7393Sniklas down until the pc value is pushed out of it from the high end.
434e93f7393Sniklas
435e93f7393Sniklas Warning: this code is trickier than it would appear at first. */
436e93f7393Sniklas
437b725ae77Skettenis /* Should also require that pc is <= end of objfile. FIXME! */
438e93f7393Sniklas if (pc >= SYMBOL_VALUE_ADDRESS (&msymbol[lo]))
439e93f7393Sniklas {
440e93f7393Sniklas while (SYMBOL_VALUE_ADDRESS (&msymbol[hi]) > pc)
441e93f7393Sniklas {
442e93f7393Sniklas /* pc is still strictly less than highest address */
443e93f7393Sniklas /* Note "new" will always be >= lo */
444e93f7393Sniklas new = (lo + hi) / 2;
445e93f7393Sniklas if ((SYMBOL_VALUE_ADDRESS (&msymbol[new]) >= pc) ||
446e93f7393Sniklas (lo == new))
447e93f7393Sniklas {
448e93f7393Sniklas hi = new;
449e93f7393Sniklas }
450e93f7393Sniklas else
451e93f7393Sniklas {
452e93f7393Sniklas lo = new;
453e93f7393Sniklas }
454e93f7393Sniklas }
455e93f7393Sniklas
456e93f7393Sniklas /* If we have multiple symbols at the same address, we want
457e93f7393Sniklas hi to point to the last one. That way we can find the
458e93f7393Sniklas right symbol if it has an index greater than hi. */
459e93f7393Sniklas while (hi < objfile->minimal_symbol_count - 1
460e93f7393Sniklas && (SYMBOL_VALUE_ADDRESS (&msymbol[hi])
461e93f7393Sniklas == SYMBOL_VALUE_ADDRESS (&msymbol[hi + 1])))
462e93f7393Sniklas hi++;
463e93f7393Sniklas
464e93f7393Sniklas /* The minimal symbol indexed by hi now is the best one in this
465e93f7393Sniklas objfile's minimal symbol table. See if it is the best one
466e93f7393Sniklas overall. */
467e93f7393Sniklas
468e93f7393Sniklas /* Skip any absolute symbols. This is apparently what adb
469e93f7393Sniklas and dbx do, and is needed for the CM-5. There are two
470e93f7393Sniklas known possible problems: (1) on ELF, apparently end, edata,
471e93f7393Sniklas etc. are absolute. Not sure ignoring them here is a big
472e93f7393Sniklas deal, but if we want to use them, the fix would go in
473e93f7393Sniklas elfread.c. (2) I think shared library entry points on the
474e93f7393Sniklas NeXT are absolute. If we want special handling for this
475e93f7393Sniklas it probably should be triggered by a special
476e93f7393Sniklas mst_abs_or_lib or some such. */
477e93f7393Sniklas while (hi >= 0
478e93f7393Sniklas && msymbol[hi].type == mst_abs)
479e93f7393Sniklas --hi;
480e93f7393Sniklas
481b725ae77Skettenis /* If "section" specified, skip any symbol from wrong section */
482b725ae77Skettenis /* This is the new code that distinguishes it from the old function */
483b725ae77Skettenis if (section)
484b725ae77Skettenis while (hi >= 0
485b725ae77Skettenis /* Some types of debug info, such as COFF,
486b725ae77Skettenis don't fill the bfd_section member, so don't
487b725ae77Skettenis throw away symbols on those platforms. */
488b725ae77Skettenis && SYMBOL_BFD_SECTION (&msymbol[hi]) != NULL
489b725ae77Skettenis && SYMBOL_BFD_SECTION (&msymbol[hi]) != section)
490b725ae77Skettenis --hi;
491b725ae77Skettenis
492e93f7393Sniklas if (hi >= 0
493e93f7393Sniklas && ((best_symbol == NULL) ||
494e93f7393Sniklas (SYMBOL_VALUE_ADDRESS (best_symbol) <
495e93f7393Sniklas SYMBOL_VALUE_ADDRESS (&msymbol[hi]))))
496e93f7393Sniklas {
497e93f7393Sniklas best_symbol = &msymbol[hi];
498e93f7393Sniklas }
499e93f7393Sniklas }
500e93f7393Sniklas }
501e93f7393Sniklas }
502e93f7393Sniklas return (best_symbol);
503e93f7393Sniklas }
504e93f7393Sniklas
505b725ae77Skettenis /* Backward compatibility: search through the minimal symbol table
506b725ae77Skettenis for a matching PC (no section given) */
507b725ae77Skettenis
508b725ae77Skettenis struct minimal_symbol *
lookup_minimal_symbol_by_pc(CORE_ADDR pc)509b725ae77Skettenis lookup_minimal_symbol_by_pc (CORE_ADDR pc)
510e93f7393Sniklas {
511b725ae77Skettenis /* NOTE: cagney/2004-01-27: This was using find_pc_mapped_section to
512b725ae77Skettenis force the section but that (well unless you're doing overlay
513b725ae77Skettenis debugging) always returns NULL making the call somewhat useless. */
514b725ae77Skettenis struct obj_section *section = find_pc_section (pc);
515b725ae77Skettenis if (section == NULL)
516b725ae77Skettenis return NULL;
517b725ae77Skettenis return lookup_minimal_symbol_by_pc_section (pc, section->the_bfd_section);
518e93f7393Sniklas }
519e93f7393Sniklas
520b725ae77Skettenis
521e93f7393Sniklas /* Return leading symbol character for a BFD. If BFD is NULL,
522e93f7393Sniklas return the leading symbol character from the main objfile. */
523e93f7393Sniklas
524b725ae77Skettenis static int get_symbol_leading_char (bfd *);
525e93f7393Sniklas
526e93f7393Sniklas static int
get_symbol_leading_char(bfd * abfd)527b725ae77Skettenis get_symbol_leading_char (bfd *abfd)
528e93f7393Sniklas {
529e93f7393Sniklas if (abfd != NULL)
530e93f7393Sniklas return bfd_get_symbol_leading_char (abfd);
531e93f7393Sniklas if (symfile_objfile != NULL && symfile_objfile->obfd != NULL)
532e93f7393Sniklas return bfd_get_symbol_leading_char (symfile_objfile->obfd);
533e93f7393Sniklas return 0;
534e93f7393Sniklas }
535e93f7393Sniklas
536e93f7393Sniklas /* Prepare to start collecting minimal symbols. Note that presetting
537e93f7393Sniklas msym_bunch_index to BUNCH_SIZE causes the first call to save a minimal
538e93f7393Sniklas symbol to allocate the memory for the first bunch. */
539e93f7393Sniklas
540e93f7393Sniklas void
init_minimal_symbol_collection(void)541b725ae77Skettenis init_minimal_symbol_collection (void)
542e93f7393Sniklas {
543e93f7393Sniklas msym_count = 0;
544e93f7393Sniklas msym_bunch = NULL;
545e93f7393Sniklas msym_bunch_index = BUNCH_SIZE;
546e93f7393Sniklas }
547e93f7393Sniklas
548e93f7393Sniklas void
prim_record_minimal_symbol(const char * name,CORE_ADDR address,enum minimal_symbol_type ms_type,struct objfile * objfile)549b725ae77Skettenis prim_record_minimal_symbol (const char *name, CORE_ADDR address,
550b725ae77Skettenis enum minimal_symbol_type ms_type,
551b725ae77Skettenis struct objfile *objfile)
552e93f7393Sniklas {
553e93f7393Sniklas int section;
554e93f7393Sniklas
555e93f7393Sniklas switch (ms_type)
556e93f7393Sniklas {
557e93f7393Sniklas case mst_text:
558e93f7393Sniklas case mst_file_text:
559e93f7393Sniklas case mst_solib_trampoline:
560b725ae77Skettenis section = SECT_OFF_TEXT (objfile);
561e93f7393Sniklas break;
562e93f7393Sniklas case mst_data:
563e93f7393Sniklas case mst_file_data:
564b725ae77Skettenis section = SECT_OFF_DATA (objfile);
565e93f7393Sniklas break;
566e93f7393Sniklas case mst_bss:
567e93f7393Sniklas case mst_file_bss:
568b725ae77Skettenis section = SECT_OFF_BSS (objfile);
569e93f7393Sniklas break;
570e93f7393Sniklas default:
571e93f7393Sniklas section = -1;
572e93f7393Sniklas }
573e93f7393Sniklas
574e93f7393Sniklas prim_record_minimal_symbol_and_info (name, address, ms_type,
575b725ae77Skettenis NULL, section, NULL, objfile);
576e93f7393Sniklas }
577e93f7393Sniklas
578e93f7393Sniklas /* Record a minimal symbol in the msym bunches. Returns the symbol
579e93f7393Sniklas newly created. */
580e93f7393Sniklas
581e93f7393Sniklas struct minimal_symbol *
prim_record_minimal_symbol_and_info(const char * name,CORE_ADDR address,enum minimal_symbol_type ms_type,char * info,int section,asection * bfd_section,struct objfile * objfile)582b725ae77Skettenis prim_record_minimal_symbol_and_info (const char *name, CORE_ADDR address,
583b725ae77Skettenis enum minimal_symbol_type ms_type,
584b725ae77Skettenis char *info, int section,
585b725ae77Skettenis asection *bfd_section,
586b725ae77Skettenis struct objfile *objfile)
587e93f7393Sniklas {
588b725ae77Skettenis struct msym_bunch *new;
589b725ae77Skettenis struct minimal_symbol *msymbol;
590e93f7393Sniklas
591e93f7393Sniklas /* Don't put gcc_compiled, __gnu_compiled_cplus, and friends into
592e93f7393Sniklas the minimal symbols, because if there is also another symbol
593e93f7393Sniklas at the same address (e.g. the first function of the file),
594e93f7393Sniklas lookup_minimal_symbol_by_pc would have no way of getting the
595e93f7393Sniklas right one. */
596*63addd46Skettenis if (ms_type == mst_file_text && name[0] == 'g'
597e93f7393Sniklas && (strcmp (name, GCC_COMPILED_FLAG_SYMBOL) == 0
598e93f7393Sniklas || strcmp (name, GCC2_COMPILED_FLAG_SYMBOL) == 0))
599e93f7393Sniklas return (NULL);
600e93f7393Sniklas
601*63addd46Skettenis /* It's safe to strip the leading char here once, since the name
602*63addd46Skettenis is also stored stripped in the minimal symbol table. */
603*63addd46Skettenis if (name[0] == get_symbol_leading_char (objfile->obfd))
604*63addd46Skettenis ++name;
605*63addd46Skettenis
606*63addd46Skettenis if (ms_type == mst_file_text && strncmp (name, "__gnu_compiled", 14) == 0)
607e93f7393Sniklas return (NULL);
608e93f7393Sniklas
609e93f7393Sniklas if (msym_bunch_index == BUNCH_SIZE)
610e93f7393Sniklas {
611e93f7393Sniklas new = (struct msym_bunch *) xmalloc (sizeof (struct msym_bunch));
612e93f7393Sniklas msym_bunch_index = 0;
613e93f7393Sniklas new->next = msym_bunch;
614e93f7393Sniklas msym_bunch = new;
615e93f7393Sniklas }
616e93f7393Sniklas msymbol = &msym_bunch->contents[msym_bunch_index];
617e93f7393Sniklas SYMBOL_INIT_LANGUAGE_SPECIFIC (msymbol, language_unknown);
618b725ae77Skettenis SYMBOL_LANGUAGE (msymbol) = language_auto;
619b725ae77Skettenis SYMBOL_SET_NAMES (msymbol, (char *)name, strlen (name), objfile);
620b725ae77Skettenis
621e93f7393Sniklas SYMBOL_VALUE_ADDRESS (msymbol) = address;
622e93f7393Sniklas SYMBOL_SECTION (msymbol) = section;
623b725ae77Skettenis SYMBOL_BFD_SECTION (msymbol) = bfd_section;
624e93f7393Sniklas
625e93f7393Sniklas MSYMBOL_TYPE (msymbol) = ms_type;
626e93f7393Sniklas /* FIXME: This info, if it remains, needs its own field. */
627e93f7393Sniklas MSYMBOL_INFO (msymbol) = info; /* FIXME! */
628b725ae77Skettenis MSYMBOL_SIZE (msymbol) = 0;
629b725ae77Skettenis
630b725ae77Skettenis /* The hash pointers must be cleared! If they're not,
631b725ae77Skettenis add_minsym_to_hash_table will NOT add this msymbol to the hash table. */
632b725ae77Skettenis msymbol->hash_next = NULL;
633b725ae77Skettenis msymbol->demangled_hash_next = NULL;
634b725ae77Skettenis
635e93f7393Sniklas msym_bunch_index++;
636e93f7393Sniklas msym_count++;
637e93f7393Sniklas OBJSTAT (objfile, n_minsyms++);
638e93f7393Sniklas return msymbol;
639e93f7393Sniklas }
640e93f7393Sniklas
641e93f7393Sniklas /* Compare two minimal symbols by address and return a signed result based
642b725ae77Skettenis on unsigned comparisons, so that we sort into unsigned numeric order.
643b725ae77Skettenis Within groups with the same address, sort by name. */
644e93f7393Sniklas
645e93f7393Sniklas static int
compare_minimal_symbols(const void * fn1p,const void * fn2p)646b725ae77Skettenis compare_minimal_symbols (const void *fn1p, const void *fn2p)
647e93f7393Sniklas {
648b725ae77Skettenis const struct minimal_symbol *fn1;
649b725ae77Skettenis const struct minimal_symbol *fn2;
650e93f7393Sniklas
651e93f7393Sniklas fn1 = (const struct minimal_symbol *) fn1p;
652e93f7393Sniklas fn2 = (const struct minimal_symbol *) fn2p;
653e93f7393Sniklas
654e93f7393Sniklas if (SYMBOL_VALUE_ADDRESS (fn1) < SYMBOL_VALUE_ADDRESS (fn2))
655e93f7393Sniklas {
656b725ae77Skettenis return (-1); /* addr 1 is less than addr 2 */
657e93f7393Sniklas }
658e93f7393Sniklas else if (SYMBOL_VALUE_ADDRESS (fn1) > SYMBOL_VALUE_ADDRESS (fn2))
659e93f7393Sniklas {
660b725ae77Skettenis return (1); /* addr 1 is greater than addr 2 */
661e93f7393Sniklas }
662e93f7393Sniklas else
663b725ae77Skettenis /* addrs are equal: sort by name */
664e93f7393Sniklas {
665b725ae77Skettenis char *name1 = SYMBOL_LINKAGE_NAME (fn1);
666b725ae77Skettenis char *name2 = SYMBOL_LINKAGE_NAME (fn2);
667b725ae77Skettenis
668b725ae77Skettenis if (name1 && name2) /* both have names */
669b725ae77Skettenis return strcmp (name1, name2);
670b725ae77Skettenis else if (name2)
671b725ae77Skettenis return 1; /* fn1 has no name, so it is "less" */
672b725ae77Skettenis else if (name1) /* fn2 has no name, so it is "less" */
673b725ae77Skettenis return -1;
674b725ae77Skettenis else
675b725ae77Skettenis return (0); /* neither has a name, so they're equal. */
676e93f7393Sniklas }
677e93f7393Sniklas }
678e93f7393Sniklas
679e93f7393Sniklas /* Discard the currently collected minimal symbols, if any. If we wish
680e93f7393Sniklas to save them for later use, we must have already copied them somewhere
681e93f7393Sniklas else before calling this function.
682e93f7393Sniklas
683e93f7393Sniklas FIXME: We could allocate the minimal symbol bunches on their own
684e93f7393Sniklas obstack and then simply blow the obstack away when we are done with
685e93f7393Sniklas it. Is it worth the extra trouble though? */
686e93f7393Sniklas
687b725ae77Skettenis static void
do_discard_minimal_symbols_cleanup(void * arg)688b725ae77Skettenis do_discard_minimal_symbols_cleanup (void *arg)
689e93f7393Sniklas {
690b725ae77Skettenis struct msym_bunch *next;
691e93f7393Sniklas
692e93f7393Sniklas while (msym_bunch != NULL)
693e93f7393Sniklas {
694e93f7393Sniklas next = msym_bunch->next;
695b725ae77Skettenis xfree (msym_bunch);
696e93f7393Sniklas msym_bunch = next;
697e93f7393Sniklas }
698e93f7393Sniklas }
699e93f7393Sniklas
700b725ae77Skettenis struct cleanup *
make_cleanup_discard_minimal_symbols(void)701b725ae77Skettenis make_cleanup_discard_minimal_symbols (void)
702b725ae77Skettenis {
703b725ae77Skettenis return make_cleanup (do_discard_minimal_symbols_cleanup, 0);
704b725ae77Skettenis }
705b725ae77Skettenis
706b725ae77Skettenis
707b725ae77Skettenis
708e93f7393Sniklas /* Compact duplicate entries out of a minimal symbol table by walking
709e93f7393Sniklas through the table and compacting out entries with duplicate addresses
710e93f7393Sniklas and matching names. Return the number of entries remaining.
711e93f7393Sniklas
712e93f7393Sniklas On entry, the table resides between msymbol[0] and msymbol[mcount].
713e93f7393Sniklas On exit, it resides between msymbol[0] and msymbol[result_count].
714e93f7393Sniklas
715e93f7393Sniklas When files contain multiple sources of symbol information, it is
716e93f7393Sniklas possible for the minimal symbol table to contain many duplicate entries.
717e93f7393Sniklas As an example, SVR4 systems use ELF formatted object files, which
718e93f7393Sniklas usually contain at least two different types of symbol tables (a
719e93f7393Sniklas standard ELF one and a smaller dynamic linking table), as well as
720e93f7393Sniklas DWARF debugging information for files compiled with -g.
721e93f7393Sniklas
722e93f7393Sniklas Without compacting, the minimal symbol table for gdb itself contains
723e93f7393Sniklas over a 1000 duplicates, about a third of the total table size. Aside
724e93f7393Sniklas from the potential trap of not noticing that two successive entries
725e93f7393Sniklas identify the same location, this duplication impacts the time required
726e93f7393Sniklas to linearly scan the table, which is done in a number of places. So we
727e93f7393Sniklas just do one linear scan here and toss out the duplicates.
728e93f7393Sniklas
729e93f7393Sniklas Note that we are not concerned here about recovering the space that
730e93f7393Sniklas is potentially freed up, because the strings themselves are allocated
731b725ae77Skettenis on the objfile_obstack, and will get automatically freed when the symbol
732e93f7393Sniklas table is freed. The caller can free up the unused minimal symbols at
733e93f7393Sniklas the end of the compacted region if their allocation strategy allows it.
734e93f7393Sniklas
735e93f7393Sniklas Also note we only go up to the next to last entry within the loop
736e93f7393Sniklas and then copy the last entry explicitly after the loop terminates.
737e93f7393Sniklas
738e93f7393Sniklas Since the different sources of information for each symbol may
739e93f7393Sniklas have different levels of "completeness", we may have duplicates
740e93f7393Sniklas that have one entry with type "mst_unknown" and the other with a
741e93f7393Sniklas known type. So if the one we are leaving alone has type mst_unknown,
742e93f7393Sniklas overwrite its type with the type from the one we are compacting out. */
743e93f7393Sniklas
744e93f7393Sniklas static int
compact_minimal_symbols(struct minimal_symbol * msymbol,int mcount,struct objfile * objfile)745b725ae77Skettenis compact_minimal_symbols (struct minimal_symbol *msymbol, int mcount,
746b725ae77Skettenis struct objfile *objfile)
747e93f7393Sniklas {
748e93f7393Sniklas struct minimal_symbol *copyfrom;
749e93f7393Sniklas struct minimal_symbol *copyto;
750e93f7393Sniklas
751e93f7393Sniklas if (mcount > 0)
752e93f7393Sniklas {
753e93f7393Sniklas copyfrom = copyto = msymbol;
754e93f7393Sniklas while (copyfrom < msymbol + mcount - 1)
755e93f7393Sniklas {
756b725ae77Skettenis if (SYMBOL_VALUE_ADDRESS (copyfrom)
757b725ae77Skettenis == SYMBOL_VALUE_ADDRESS ((copyfrom + 1))
758b725ae77Skettenis && strcmp (SYMBOL_LINKAGE_NAME (copyfrom),
759b725ae77Skettenis SYMBOL_LINKAGE_NAME ((copyfrom + 1))) == 0)
760e93f7393Sniklas {
761e93f7393Sniklas if (MSYMBOL_TYPE ((copyfrom + 1)) == mst_unknown)
762e93f7393Sniklas {
763e93f7393Sniklas MSYMBOL_TYPE ((copyfrom + 1)) = MSYMBOL_TYPE (copyfrom);
764e93f7393Sniklas }
765e93f7393Sniklas copyfrom++;
766e93f7393Sniklas }
767e93f7393Sniklas else
768e93f7393Sniklas *copyto++ = *copyfrom++;
769e93f7393Sniklas }
770e93f7393Sniklas *copyto++ = *copyfrom++;
771e93f7393Sniklas mcount = copyto - msymbol;
772e93f7393Sniklas }
773e93f7393Sniklas return (mcount);
774e93f7393Sniklas }
775e93f7393Sniklas
776b725ae77Skettenis /* Build (or rebuild) the minimal symbol hash tables. This is necessary
777b725ae77Skettenis after compacting or sorting the table since the entries move around
778b725ae77Skettenis thus causing the internal minimal_symbol pointers to become jumbled. */
779b725ae77Skettenis
780b725ae77Skettenis static void
build_minimal_symbol_hash_tables(struct objfile * objfile)781b725ae77Skettenis build_minimal_symbol_hash_tables (struct objfile *objfile)
782b725ae77Skettenis {
783b725ae77Skettenis int i;
784b725ae77Skettenis struct minimal_symbol *msym;
785b725ae77Skettenis
786b725ae77Skettenis /* Clear the hash tables. */
787b725ae77Skettenis for (i = 0; i < MINIMAL_SYMBOL_HASH_SIZE; i++)
788b725ae77Skettenis {
789b725ae77Skettenis objfile->msymbol_hash[i] = 0;
790b725ae77Skettenis objfile->msymbol_demangled_hash[i] = 0;
791b725ae77Skettenis }
792b725ae77Skettenis
793b725ae77Skettenis /* Now, (re)insert the actual entries. */
794b725ae77Skettenis for (i = objfile->minimal_symbol_count, msym = objfile->msymbols;
795b725ae77Skettenis i > 0;
796b725ae77Skettenis i--, msym++)
797b725ae77Skettenis {
798b725ae77Skettenis msym->hash_next = 0;
799b725ae77Skettenis add_minsym_to_hash_table (msym, objfile->msymbol_hash);
800b725ae77Skettenis
801b725ae77Skettenis msym->demangled_hash_next = 0;
802*63addd46Skettenis if (SYMBOL_SEARCH_NAME (msym) != SYMBOL_LINKAGE_NAME (msym))
803b725ae77Skettenis add_minsym_to_demangled_hash_table (msym,
804b725ae77Skettenis objfile->msymbol_demangled_hash);
805b725ae77Skettenis }
806b725ae77Skettenis }
807b725ae77Skettenis
808e93f7393Sniklas /* Add the minimal symbols in the existing bunches to the objfile's official
809e93f7393Sniklas minimal symbol table. In most cases there is no minimal symbol table yet
810e93f7393Sniklas for this objfile, and the existing bunches are used to create one. Once
811e93f7393Sniklas in a while (for shared libraries for example), we add symbols (e.g. common
812e93f7393Sniklas symbols) to an existing objfile.
813e93f7393Sniklas
814e93f7393Sniklas Because of the way minimal symbols are collected, we generally have no way
815e93f7393Sniklas of knowing what source language applies to any particular minimal symbol.
816e93f7393Sniklas Specifically, we have no way of knowing if the minimal symbol comes from a
817e93f7393Sniklas C++ compilation unit or not. So for the sake of supporting cached
818e93f7393Sniklas demangled C++ names, we have no choice but to try and demangle each new one
819e93f7393Sniklas that comes in. If the demangling succeeds, then we assume it is a C++
820e93f7393Sniklas symbol and set the symbol's language and demangled name fields
821e93f7393Sniklas appropriately. Note that in order to avoid unnecessary demanglings, and
822e93f7393Sniklas allocating obstack space that subsequently can't be freed for the demangled
823e93f7393Sniklas names, we mark all newly added symbols with language_auto. After
824e93f7393Sniklas compaction of the minimal symbols, we go back and scan the entire minimal
825e93f7393Sniklas symbol table looking for these new symbols. For each new symbol we attempt
826e93f7393Sniklas to demangle it, and if successful, record it as a language_cplus symbol
827e93f7393Sniklas and cache the demangled form on the symbol obstack. Symbols which don't
828e93f7393Sniklas demangle are marked as language_unknown symbols, which inhibits future
829e93f7393Sniklas attempts to demangle them if we later add more minimal symbols. */
830e93f7393Sniklas
831e93f7393Sniklas void
install_minimal_symbols(struct objfile * objfile)832b725ae77Skettenis install_minimal_symbols (struct objfile *objfile)
833e93f7393Sniklas {
834b725ae77Skettenis int bindex;
835b725ae77Skettenis int mcount;
836b725ae77Skettenis struct msym_bunch *bunch;
837b725ae77Skettenis struct minimal_symbol *msymbols;
838e93f7393Sniklas int alloc_count;
839e93f7393Sniklas
840e93f7393Sniklas if (msym_count > 0)
841e93f7393Sniklas {
842e93f7393Sniklas /* Allocate enough space in the obstack, into which we will gather the
843e93f7393Sniklas bunches of new and existing minimal symbols, sort them, and then
844e93f7393Sniklas compact out the duplicate entries. Once we have a final table,
845e93f7393Sniklas we will give back the excess space. */
846e93f7393Sniklas
847e93f7393Sniklas alloc_count = msym_count + objfile->minimal_symbol_count + 1;
848b725ae77Skettenis obstack_blank (&objfile->objfile_obstack,
849e93f7393Sniklas alloc_count * sizeof (struct minimal_symbol));
850e93f7393Sniklas msymbols = (struct minimal_symbol *)
851b725ae77Skettenis obstack_base (&objfile->objfile_obstack);
852e93f7393Sniklas
853e93f7393Sniklas /* Copy in the existing minimal symbols, if there are any. */
854e93f7393Sniklas
855e93f7393Sniklas if (objfile->minimal_symbol_count)
856e93f7393Sniklas memcpy ((char *) msymbols, (char *) objfile->msymbols,
857e93f7393Sniklas objfile->minimal_symbol_count * sizeof (struct minimal_symbol));
858e93f7393Sniklas
859e93f7393Sniklas /* Walk through the list of minimal symbol bunches, adding each symbol
860e93f7393Sniklas to the new contiguous array of symbols. Note that we start with the
861e93f7393Sniklas current, possibly partially filled bunch (thus we use the current
862e93f7393Sniklas msym_bunch_index for the first bunch we copy over), and thereafter
863e93f7393Sniklas each bunch is full. */
864e93f7393Sniklas
865e93f7393Sniklas mcount = objfile->minimal_symbol_count;
866e93f7393Sniklas
867e93f7393Sniklas for (bunch = msym_bunch; bunch != NULL; bunch = bunch->next)
868e93f7393Sniklas {
869e93f7393Sniklas for (bindex = 0; bindex < msym_bunch_index; bindex++, mcount++)
870e93f7393Sniklas msymbols[mcount] = bunch->contents[bindex];
871e93f7393Sniklas msym_bunch_index = BUNCH_SIZE;
872e93f7393Sniklas }
873e93f7393Sniklas
874e93f7393Sniklas /* Sort the minimal symbols by address. */
875e93f7393Sniklas
876e93f7393Sniklas qsort (msymbols, mcount, sizeof (struct minimal_symbol),
877e93f7393Sniklas compare_minimal_symbols);
878e93f7393Sniklas
879e93f7393Sniklas /* Compact out any duplicates, and free up whatever space we are
880e93f7393Sniklas no longer using. */
881e93f7393Sniklas
882b725ae77Skettenis mcount = compact_minimal_symbols (msymbols, mcount, objfile);
883e93f7393Sniklas
884b725ae77Skettenis obstack_blank (&objfile->objfile_obstack,
885e93f7393Sniklas (mcount + 1 - alloc_count) * sizeof (struct minimal_symbol));
886e93f7393Sniklas msymbols = (struct minimal_symbol *)
887b725ae77Skettenis obstack_finish (&objfile->objfile_obstack);
888e93f7393Sniklas
889e93f7393Sniklas /* We also terminate the minimal symbol table with a "null symbol",
890e93f7393Sniklas which is *not* included in the size of the table. This makes it
891e93f7393Sniklas easier to find the end of the table when we are handed a pointer
892e93f7393Sniklas to some symbol in the middle of it. Zero out the fields in the
893e93f7393Sniklas "null symbol" allocated at the end of the array. Note that the
894e93f7393Sniklas symbol count does *not* include this null symbol, which is why it
895e93f7393Sniklas is indexed by mcount and not mcount-1. */
896e93f7393Sniklas
897b725ae77Skettenis SYMBOL_LINKAGE_NAME (&msymbols[mcount]) = NULL;
898e93f7393Sniklas SYMBOL_VALUE_ADDRESS (&msymbols[mcount]) = 0;
899e93f7393Sniklas MSYMBOL_INFO (&msymbols[mcount]) = NULL;
900b725ae77Skettenis MSYMBOL_SIZE (&msymbols[mcount]) = 0;
901e93f7393Sniklas MSYMBOL_TYPE (&msymbols[mcount]) = mst_unknown;
902e93f7393Sniklas SYMBOL_INIT_LANGUAGE_SPECIFIC (&msymbols[mcount], language_unknown);
903e93f7393Sniklas
904e93f7393Sniklas /* Attach the minimal symbol table to the specified objfile.
905b725ae77Skettenis The strings themselves are also located in the objfile_obstack
906e93f7393Sniklas of this objfile. */
907e93f7393Sniklas
908e93f7393Sniklas objfile->minimal_symbol_count = mcount;
909e93f7393Sniklas objfile->msymbols = msymbols;
910e93f7393Sniklas
911b725ae77Skettenis /* Try to guess the appropriate C++ ABI by looking at the names
912b725ae77Skettenis of the minimal symbols in the table. */
913e93f7393Sniklas {
914b725ae77Skettenis int i;
915b725ae77Skettenis
916b725ae77Skettenis for (i = 0; i < mcount; i++)
917b725ae77Skettenis {
918b725ae77Skettenis /* If a symbol's name starts with _Z and was successfully
919b725ae77Skettenis demangled, then we can assume we've found a GNU v3 symbol.
920b725ae77Skettenis For now we set the C++ ABI globally; if the user is
921b725ae77Skettenis mixing ABIs then the user will need to "set cp-abi"
922b725ae77Skettenis manually. */
923b725ae77Skettenis const char *name = SYMBOL_LINKAGE_NAME (&objfile->msymbols[i]);
924b725ae77Skettenis if (name[0] == '_' && name[1] == 'Z'
925b725ae77Skettenis && SYMBOL_DEMANGLED_NAME (&objfile->msymbols[i]) != NULL)
926b725ae77Skettenis {
927b725ae77Skettenis set_cp_abi_as_auto_default ("gnu-v3");
928b725ae77Skettenis break;
929e93f7393Sniklas }
930e93f7393Sniklas }
931e93f7393Sniklas }
932e93f7393Sniklas
933b725ae77Skettenis /* Now build the hash tables; we can't do this incrementally
934b725ae77Skettenis at an earlier point since we weren't finished with the obstack
935b725ae77Skettenis yet. (And if the msymbol obstack gets moved, all the internal
936b725ae77Skettenis pointers to other msymbols need to be adjusted.) */
937b725ae77Skettenis build_minimal_symbol_hash_tables (objfile);
938b725ae77Skettenis }
939b725ae77Skettenis }
940b725ae77Skettenis
941e93f7393Sniklas /* Sort all the minimal symbols in OBJFILE. */
942e93f7393Sniklas
943e93f7393Sniklas void
msymbols_sort(struct objfile * objfile)944b725ae77Skettenis msymbols_sort (struct objfile *objfile)
945e93f7393Sniklas {
946e93f7393Sniklas qsort (objfile->msymbols, objfile->minimal_symbol_count,
947e93f7393Sniklas sizeof (struct minimal_symbol), compare_minimal_symbols);
948b725ae77Skettenis build_minimal_symbol_hash_tables (objfile);
949e93f7393Sniklas }
950e93f7393Sniklas
951e93f7393Sniklas /* Check if PC is in a shared library trampoline code stub.
952e93f7393Sniklas Return minimal symbol for the trampoline entry or NULL if PC is not
953e93f7393Sniklas in a trampoline code stub. */
954e93f7393Sniklas
955e93f7393Sniklas struct minimal_symbol *
lookup_solib_trampoline_symbol_by_pc(CORE_ADDR pc)956b725ae77Skettenis lookup_solib_trampoline_symbol_by_pc (CORE_ADDR pc)
957e93f7393Sniklas {
958e93f7393Sniklas struct minimal_symbol *msymbol = lookup_minimal_symbol_by_pc (pc);
959e93f7393Sniklas
960e93f7393Sniklas if (msymbol != NULL && MSYMBOL_TYPE (msymbol) == mst_solib_trampoline)
961e93f7393Sniklas return msymbol;
962e93f7393Sniklas return NULL;
963e93f7393Sniklas }
964e93f7393Sniklas
965e93f7393Sniklas /* If PC is in a shared library trampoline code stub, return the
966e93f7393Sniklas address of the `real' function belonging to the stub.
967e93f7393Sniklas Return 0 if PC is not in a trampoline code stub or if the real
968e93f7393Sniklas function is not found in the minimal symbol table.
969e93f7393Sniklas
970e93f7393Sniklas We may fail to find the right function if a function with the
971e93f7393Sniklas same name is defined in more than one shared library, but this
972e93f7393Sniklas is considered bad programming style. We could return 0 if we find
973e93f7393Sniklas a duplicate function in case this matters someday. */
974e93f7393Sniklas
975e93f7393Sniklas CORE_ADDR
find_solib_trampoline_target(CORE_ADDR pc)976b725ae77Skettenis find_solib_trampoline_target (CORE_ADDR pc)
977e93f7393Sniklas {
978e93f7393Sniklas struct objfile *objfile;
979e93f7393Sniklas struct minimal_symbol *msymbol;
980e93f7393Sniklas struct minimal_symbol *tsymbol = lookup_solib_trampoline_symbol_by_pc (pc);
981e93f7393Sniklas
982e93f7393Sniklas if (tsymbol != NULL)
983e93f7393Sniklas {
984e93f7393Sniklas ALL_MSYMBOLS (objfile, msymbol)
985e93f7393Sniklas {
986e93f7393Sniklas if (MSYMBOL_TYPE (msymbol) == mst_text
987b725ae77Skettenis && strcmp (SYMBOL_LINKAGE_NAME (msymbol),
988b725ae77Skettenis SYMBOL_LINKAGE_NAME (tsymbol)) == 0)
989e93f7393Sniklas return SYMBOL_VALUE_ADDRESS (msymbol);
990e93f7393Sniklas }
991e93f7393Sniklas }
992e93f7393Sniklas return 0;
993e93f7393Sniklas }
994