Resolve more problems with readelf uncovered by fuzzing binary files.
[deliverable/binutils-gdb.git] / gdb / minsyms.c
CommitLineData
c906108c 1/* GDB routines for manipulating the minimal symbol tables.
ecd75fc8 2 Copyright (C) 1992-2014 Free Software Foundation, Inc.
c906108c
SS
3 Contributed by Cygnus Support, using pieces from other GDB modules.
4
c5aa993b 5 This file is part of GDB.
c906108c 6
c5aa993b
JM
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
a9762ec7 9 the Free Software Foundation; either version 3 of the License, or
c5aa993b 10 (at your option) any later version.
c906108c 11
c5aa993b
JM
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.
c906108c 16
c5aa993b 17 You should have received a copy of the GNU General Public License
a9762ec7 18 along with this program. If not, see <http://www.gnu.org/licenses/>. */
c906108c
SS
19
20
21/* This file contains support routines for creating, manipulating, and
22 destroying minimal symbol tables.
23
24 Minimal symbol tables are used to hold some very basic information about
25 all defined global symbols (text, data, bss, abs, etc). The only two
26 required pieces of information are the symbol's name and the address
27 associated with that symbol.
28
29 In many cases, even if a file was compiled with no special options for
30 debugging at all, as long as was not stripped it will contain sufficient
31 information to build useful minimal symbol tables using this structure.
c5aa993b 32
c906108c
SS
33 Even when a file contains enough debugging information to build a full
34 symbol table, these minimal symbols are still useful for quickly mapping
35 between names and addresses, and vice versa. They are also sometimes used
025bb325 36 to figure out what full symbol table entries need to be read in. */
c906108c
SS
37
38
39#include "defs.h"
9227b5eb 40#include <ctype.h>
c906108c
SS
41#include "symtab.h"
42#include "bfd.h"
0ba1096a 43#include "filenames.h"
c906108c
SS
44#include "symfile.h"
45#include "objfiles.h"
46#include "demangle.h"
7ed49443
JB
47#include "value.h"
48#include "cp-abi.h"
42848c96 49#include "target.h"
71c25dea
TT
50#include "cp-support.h"
51#include "language.h"
529480d0 52#include "cli/cli-utils.h"
bd9269f7 53#include "symbol.h"
c906108c
SS
54
55/* Accumulate the minimal symbols for each objfile in bunches of BUNCH_SIZE.
56 At the end, copy them all into one newly allocated location on an objfile's
34643a32 57 per-BFD storage obstack. */
c906108c
SS
58
59#define BUNCH_SIZE 127
60
61struct msym_bunch
c5aa993b
JM
62 {
63 struct msym_bunch *next;
64 struct minimal_symbol contents[BUNCH_SIZE];
65 };
c906108c
SS
66
67/* Bunch currently being filled up.
68 The next field points to chain of filled bunches. */
69
70static struct msym_bunch *msym_bunch;
71
72/* Number of slots filled in current bunch. */
73
74static int msym_bunch_index;
75
76/* Total number of minimal symbols recorded so far for the objfile. */
77
78static int msym_count;
79
b19686e0 80/* See minsyms.h. */
9227b5eb
JB
81
82unsigned int
83msymbol_hash_iw (const char *string)
84{
85 unsigned int hash = 0;
b8d56208 86
9227b5eb
JB
87 while (*string && *string != '(')
88 {
529480d0 89 string = skip_spaces_const (string);
9227b5eb 90 if (*string && *string != '(')
375f3d86 91 {
59d7bcaf 92 hash = SYMBOL_HASH_NEXT (hash, *string);
375f3d86
DJ
93 ++string;
94 }
9227b5eb 95 }
261397f8 96 return hash;
9227b5eb
JB
97}
98
b19686e0 99/* See minsyms.h. */
9227b5eb
JB
100
101unsigned int
102msymbol_hash (const char *string)
103{
104 unsigned int hash = 0;
b8d56208 105
9227b5eb 106 for (; *string; ++string)
59d7bcaf 107 hash = SYMBOL_HASH_NEXT (hash, *string);
261397f8 108 return hash;
9227b5eb
JB
109}
110
111/* Add the minimal symbol SYM to an objfile's minsym hash table, TABLE. */
984ac464 112static void
9227b5eb
JB
113add_minsym_to_hash_table (struct minimal_symbol *sym,
114 struct minimal_symbol **table)
115{
116 if (sym->hash_next == NULL)
117 {
f56f77c1 118 unsigned int hash
efd66ac6 119 = msymbol_hash (MSYMBOL_LINKAGE_NAME (sym)) % MINIMAL_SYMBOL_HASH_SIZE;
b8d56208 120
9227b5eb
JB
121 sym->hash_next = table[hash];
122 table[hash] = sym;
123 }
124}
125
0729fd50
DB
126/* Add the minimal symbol SYM to an objfile's minsym demangled hash table,
127 TABLE. */
128static void
129add_minsym_to_demangled_hash_table (struct minimal_symbol *sym,
130 struct minimal_symbol **table)
131{
132 if (sym->demangled_hash_next == NULL)
133 {
efd66ac6 134 unsigned int hash = msymbol_hash_iw (MSYMBOL_SEARCH_NAME (sym))
3e43a32a 135 % MINIMAL_SYMBOL_HASH_SIZE;
b8d56208 136
0729fd50
DB
137 sym->demangled_hash_next = table[hash];
138 table[hash] = sym;
139 }
140}
141
c906108c
SS
142/* Look through all the current minimal symbol tables and find the
143 first minimal symbol that matches NAME. If OBJF is non-NULL, limit
72a5efb3
DJ
144 the search to that objfile. If SFILE is non-NULL, the only file-scope
145 symbols considered will be from that source file (global symbols are
146 still preferred). Returns a pointer to the minimal symbol that
c906108c
SS
147 matches, or NULL if no match is found.
148
149 Note: One instance where there may be duplicate minimal symbols with
150 the same name is when the symbol tables for a shared library and the
151 symbol tables for an executable contain global symbols with the same
d73f140a
JB
152 names (the dynamic linker deals with the duplication).
153
154 It's also possible to have minimal symbols with different mangled
155 names, but identical demangled names. For example, the GNU C++ v3
156 ABI requires the generation of two (or perhaps three) copies of
157 constructor functions --- "in-charge", "not-in-charge", and
158 "allocate" copies; destructors may be duplicated as well.
159 Obviously, there must be distinct mangled names for each of these,
160 but the demangled names are all the same: S::S or S::~S. */
c906108c 161
3b7344d5
TT
162struct bound_minimal_symbol
163lookup_minimal_symbol (const char *name, const char *sfile,
164 struct objfile *objf)
c906108c
SS
165{
166 struct objfile *objfile;
7c7b6655
TT
167 struct bound_minimal_symbol found_symbol = { NULL, NULL };
168 struct bound_minimal_symbol found_file_symbol = { NULL, NULL };
169 struct bound_minimal_symbol trampoline_symbol = { NULL, NULL };
c906108c 170
261397f8
DJ
171 unsigned int hash = msymbol_hash (name) % MINIMAL_SYMBOL_HASH_SIZE;
172 unsigned int dem_hash = msymbol_hash_iw (name) % MINIMAL_SYMBOL_HASH_SIZE;
9227b5eb 173
71c25dea
TT
174 int needtofreename = 0;
175 const char *modified_name;
176
c906108c 177 if (sfile != NULL)
9f37bbcc 178 sfile = lbasename (sfile);
c906108c 179
025bb325 180 /* For C++, canonicalize the input name. */
71c25dea
TT
181 modified_name = name;
182 if (current_language->la_language == language_cplus)
183 {
184 char *cname = cp_canonicalize_string (name);
b8d56208 185
71c25dea
TT
186 if (cname)
187 {
188 modified_name = cname;
189 needtofreename = 1;
190 }
191 }
192
c906108c 193 for (objfile = object_files;
7c7b6655 194 objfile != NULL && found_symbol.minsym == NULL;
c5aa993b 195 objfile = objfile->next)
c906108c 196 {
7c7b6655
TT
197 struct minimal_symbol *msymbol;
198
56e3f43c 199 if (objf == NULL || objf == objfile
15d123c9 200 || objf == objfile->separate_debug_objfile_backlink)
c906108c 201 {
9227b5eb
JB
202 /* Do two passes: the first over the ordinary hash table,
203 and the second over the demangled hash table. */
0729fd50 204 int pass;
9227b5eb 205
7c7b6655 206 for (pass = 1; pass <= 2 && found_symbol.minsym == NULL; pass++)
c906108c 207 {
0729fd50
DB
208 /* Select hash list according to pass. */
209 if (pass == 1)
34643a32 210 msymbol = objfile->per_bfd->msymbol_hash[hash];
0729fd50 211 else
34643a32 212 msymbol = objfile->per_bfd->msymbol_demangled_hash[dem_hash];
0729fd50 213
7c7b6655 214 while (msymbol != NULL && found_symbol.minsym == NULL)
c906108c 215 {
3567439c
DJ
216 int match;
217
218 if (pass == 1)
71c25dea 219 {
559a7a62
JK
220 int (*cmp) (const char *, const char *);
221
222 cmp = (case_sensitivity == case_sensitive_on
223 ? strcmp : strcasecmp);
efd66ac6 224 match = cmp (MSYMBOL_LINKAGE_NAME (msymbol),
559a7a62 225 modified_name) == 0;
71c25dea 226 }
3567439c 227 else
71c25dea 228 {
559a7a62 229 /* The function respects CASE_SENSITIVITY. */
efd66ac6 230 match = MSYMBOL_MATCHES_SEARCH_NAME (msymbol,
71c25dea
TT
231 modified_name);
232 }
233
3567439c 234 if (match)
c906108c 235 {
0729fd50
DB
236 switch (MSYMBOL_TYPE (msymbol))
237 {
238 case mst_file_text:
239 case mst_file_data:
240 case mst_file_bss:
6314a349 241 if (sfile == NULL
0ba1096a 242 || filename_cmp (msymbol->filename, sfile) == 0)
7c7b6655
TT
243 {
244 found_file_symbol.minsym = msymbol;
245 found_file_symbol.objfile = objfile;
246 }
0729fd50
DB
247 break;
248
249 case mst_solib_trampoline:
250
251 /* If a trampoline symbol is found, we prefer to
025bb325 252 keep looking for the *real* symbol. If the
0729fd50 253 actual symbol is not found, then we'll use the
025bb325 254 trampoline entry. */
7c7b6655
TT
255 if (trampoline_symbol.minsym == NULL)
256 {
257 trampoline_symbol.minsym = msymbol;
258 trampoline_symbol.objfile = objfile;
259 }
0729fd50
DB
260 break;
261
262 case mst_unknown:
263 default:
7c7b6655
TT
264 found_symbol.minsym = msymbol;
265 found_symbol.objfile = objfile;
0729fd50
DB
266 break;
267 }
c906108c 268 }
9227b5eb 269
0729fd50
DB
270 /* Find the next symbol on the hash chain. */
271 if (pass == 1)
272 msymbol = msymbol->hash_next;
273 else
274 msymbol = msymbol->demangled_hash_next;
9227b5eb 275 }
c906108c
SS
276 }
277 }
278 }
71c25dea
TT
279
280 if (needtofreename)
281 xfree ((void *) modified_name);
282
c906108c 283 /* External symbols are best. */
7c7b6655 284 if (found_symbol.minsym != NULL)
c906108c
SS
285 return found_symbol;
286
287 /* File-local symbols are next best. */
7c7b6655 288 if (found_file_symbol.minsym != NULL)
c906108c
SS
289 return found_file_symbol;
290
291 /* Symbols for shared library trampolines are next best. */
7c7b6655
TT
292 return trampoline_symbol;
293}
294
295/* See minsyms.h. */
c906108c 296
7c7b6655
TT
297struct bound_minimal_symbol
298lookup_bound_minimal_symbol (const char *name)
299{
3b7344d5 300 return lookup_minimal_symbol (name, NULL, NULL);
c906108c
SS
301}
302
bd9269f7
GB
303/* See common/symbol.h. */
304
305int
306find_minimal_symbol_address (const char *name, CORE_ADDR *addr,
307 struct objfile *objfile)
308{
309 struct bound_minimal_symbol sym
310 = lookup_minimal_symbol (name, NULL, objfile);
311
312 if (sym.minsym != NULL)
313 *addr = BMSYMBOL_VALUE_ADDRESS (sym);
314
315 return sym.minsym == NULL;
316}
317
b19686e0 318/* See minsyms.h. */
f8eba3c6
TT
319
320void
321iterate_over_minimal_symbols (struct objfile *objf, const char *name,
322 void (*callback) (struct minimal_symbol *,
323 void *),
324 void *user_data)
325{
326 unsigned int hash;
327 struct minimal_symbol *iter;
328 int (*cmp) (const char *, const char *);
329
330 /* The first pass is over the ordinary hash table. */
331 hash = msymbol_hash (name) % MINIMAL_SYMBOL_HASH_SIZE;
34643a32 332 iter = objf->per_bfd->msymbol_hash[hash];
f8eba3c6
TT
333 cmp = (case_sensitivity == case_sensitive_on ? strcmp : strcasecmp);
334 while (iter)
335 {
efd66ac6 336 if (cmp (MSYMBOL_LINKAGE_NAME (iter), name) == 0)
f8eba3c6
TT
337 (*callback) (iter, user_data);
338 iter = iter->hash_next;
339 }
340
341 /* The second pass is over the demangled table. */
342 hash = msymbol_hash_iw (name) % MINIMAL_SYMBOL_HASH_SIZE;
34643a32 343 iter = objf->per_bfd->msymbol_demangled_hash[hash];
f8eba3c6
TT
344 while (iter)
345 {
efd66ac6 346 if (MSYMBOL_MATCHES_SEARCH_NAME (iter, name))
f8eba3c6
TT
347 (*callback) (iter, user_data);
348 iter = iter->demangled_hash_next;
349 }
350}
351
b19686e0 352/* See minsyms.h. */
c5aa993b 353
3b7344d5 354struct bound_minimal_symbol
5520a790 355lookup_minimal_symbol_text (const char *name, struct objfile *objf)
c906108c
SS
356{
357 struct objfile *objfile;
358 struct minimal_symbol *msymbol;
3b7344d5
TT
359 struct bound_minimal_symbol found_symbol = { NULL, NULL };
360 struct bound_minimal_symbol found_file_symbol = { NULL, NULL };
c906108c 361
72a5efb3
DJ
362 unsigned int hash = msymbol_hash (name) % MINIMAL_SYMBOL_HASH_SIZE;
363
c906108c 364 for (objfile = object_files;
3b7344d5 365 objfile != NULL && found_symbol.minsym == NULL;
c5aa993b 366 objfile = objfile->next)
c906108c 367 {
56e3f43c 368 if (objf == NULL || objf == objfile
15d123c9 369 || objf == objfile->separate_debug_objfile_backlink)
c906108c 370 {
34643a32 371 for (msymbol = objfile->per_bfd->msymbol_hash[hash];
3b7344d5 372 msymbol != NULL && found_symbol.minsym == NULL;
72a5efb3 373 msymbol = msymbol->hash_next)
c906108c 374 {
efd66ac6 375 if (strcmp (MSYMBOL_LINKAGE_NAME (msymbol), name) == 0 &&
0875794a
JK
376 (MSYMBOL_TYPE (msymbol) == mst_text
377 || MSYMBOL_TYPE (msymbol) == mst_text_gnu_ifunc
378 || MSYMBOL_TYPE (msymbol) == mst_file_text))
c906108c
SS
379 {
380 switch (MSYMBOL_TYPE (msymbol))
381 {
382 case mst_file_text:
3b7344d5
TT
383 found_file_symbol.minsym = msymbol;
384 found_file_symbol.objfile = objfile;
c906108c
SS
385 break;
386 default:
3b7344d5
TT
387 found_symbol.minsym = msymbol;
388 found_symbol.objfile = objfile;
c906108c
SS
389 break;
390 }
391 }
392 }
393 }
394 }
395 /* External symbols are best. */
3b7344d5 396 if (found_symbol.minsym)
c906108c
SS
397 return found_symbol;
398
399 /* File-local symbols are next best. */
3b7344d5 400 return found_file_symbol;
c906108c
SS
401}
402
b19686e0 403/* See minsyms.h. */
907fc202
UW
404
405struct minimal_symbol *
406lookup_minimal_symbol_by_pc_name (CORE_ADDR pc, const char *name,
407 struct objfile *objf)
408{
409 struct objfile *objfile;
410 struct minimal_symbol *msymbol;
411
412 unsigned int hash = msymbol_hash (name) % MINIMAL_SYMBOL_HASH_SIZE;
413
414 for (objfile = object_files;
415 objfile != NULL;
416 objfile = objfile->next)
417 {
418 if (objf == NULL || objf == objfile
15d123c9 419 || objf == objfile->separate_debug_objfile_backlink)
907fc202 420 {
34643a32 421 for (msymbol = objfile->per_bfd->msymbol_hash[hash];
907fc202
UW
422 msymbol != NULL;
423 msymbol = msymbol->hash_next)
424 {
77e371c0 425 if (MSYMBOL_VALUE_ADDRESS (objfile, msymbol) == pc
efd66ac6 426 && strcmp (MSYMBOL_LINKAGE_NAME (msymbol), name) == 0)
907fc202
UW
427 return msymbol;
428 }
429 }
430 }
431
432 return NULL;
433}
434
b19686e0 435/* See minsyms.h. */
c5aa993b 436
3b7344d5 437struct bound_minimal_symbol
aa1ee363 438lookup_minimal_symbol_solib_trampoline (const char *name,
aa1ee363 439 struct objfile *objf)
c906108c
SS
440{
441 struct objfile *objfile;
442 struct minimal_symbol *msymbol;
3b7344d5 443 struct bound_minimal_symbol found_symbol = { NULL, NULL };
c906108c 444
72a5efb3
DJ
445 unsigned int hash = msymbol_hash (name) % MINIMAL_SYMBOL_HASH_SIZE;
446
c906108c 447 for (objfile = object_files;
3b7344d5 448 objfile != NULL;
c5aa993b 449 objfile = objfile->next)
c906108c 450 {
56e3f43c 451 if (objf == NULL || objf == objfile
15d123c9 452 || objf == objfile->separate_debug_objfile_backlink)
c906108c 453 {
34643a32 454 for (msymbol = objfile->per_bfd->msymbol_hash[hash];
3b7344d5 455 msymbol != NULL;
72a5efb3 456 msymbol = msymbol->hash_next)
c906108c 457 {
efd66ac6 458 if (strcmp (MSYMBOL_LINKAGE_NAME (msymbol), name) == 0 &&
c906108c 459 MSYMBOL_TYPE (msymbol) == mst_solib_trampoline)
3b7344d5
TT
460 {
461 found_symbol.objfile = objfile;
462 found_symbol.minsym = msymbol;
463 return found_symbol;
464 }
c906108c
SS
465 }
466 }
467 }
468
3b7344d5 469 return found_symbol;
c906108c
SS
470}
471
77e371c0
TT
472/* A helper function that makes *PC section-relative. This searches
473 the sections of OBJFILE and if *PC is in a section, it subtracts
474 the section offset and returns true. Otherwise it returns
475 false. */
476
477static int
478frob_address (struct objfile *objfile, CORE_ADDR *pc)
479{
480 struct obj_section *iter;
481
482 ALL_OBJFILE_OSECTIONS (objfile, iter)
483 {
484 if (*pc >= obj_section_addr (iter) && *pc < obj_section_endaddr (iter))
485 {
486 *pc -= obj_section_offset (iter);
487 return 1;
488 }
489 }
490
491 return 0;
492}
493
c906108c
SS
494/* Search through the minimal symbol table for each objfile and find
495 the symbol whose address is the largest address that is still less
00878c6e
PP
496 than or equal to PC, and matches SECTION (which is not NULL).
497 Returns a pointer to the minimal symbol if such a symbol is found,
498 or NULL if PC is not in a suitable range.
499 Note that we need to look through ALL the minimal symbol tables
500 before deciding on the symbol that comes closest to the specified PC.
501 This is because objfiles can overlap, for example objfile A has .text
502 at 0x100 and .data at 0x40000 and objfile B has .text at 0x234 and
503 .data at 0x40048.
c906108c 504
2eaf8d2a
DJ
505 If WANT_TRAMPOLINE is set, prefer mst_solib_trampoline symbols when
506 there are text and trampoline symbols at the same address.
507 Otherwise prefer mst_text symbols. */
508
7cbd4a93 509static struct bound_minimal_symbol
77e371c0 510lookup_minimal_symbol_by_pc_section_1 (CORE_ADDR pc_in,
714835d5 511 struct obj_section *section,
2eaf8d2a 512 int want_trampoline)
c906108c
SS
513{
514 int lo;
515 int hi;
516 int new;
517 struct objfile *objfile;
518 struct minimal_symbol *msymbol;
519 struct minimal_symbol *best_symbol = NULL;
7cbd4a93
TT
520 struct objfile *best_objfile = NULL;
521 struct bound_minimal_symbol result;
2eaf8d2a 522 enum minimal_symbol_type want_type, other_type;
c906108c 523
2eaf8d2a
DJ
524 want_type = want_trampoline ? mst_solib_trampoline : mst_text;
525 other_type = want_trampoline ? mst_text : mst_solib_trampoline;
00878c6e
PP
526
527 /* We can not require the symbol found to be in section, because
96225718
DJ
528 e.g. IRIX 6.5 mdebug relies on this code returning an absolute
529 symbol - but find_pc_section won't return an absolute section and
530 hence the code below would skip over absolute symbols. We can
531 still take advantage of the call to find_pc_section, though - the
532 object file still must match. In case we have separate debug
533 files, search both the file and its separate debug file. There's
534 no telling which one will have the minimal symbols. */
535
00878c6e 536 gdb_assert (section != NULL);
96225718 537
15d123c9
TG
538 for (objfile = section->objfile;
539 objfile != NULL;
540 objfile = objfile_separate_debug_iterate (section->objfile, objfile))
c906108c 541 {
77e371c0
TT
542 CORE_ADDR pc = pc_in;
543
c906108c 544 /* If this objfile has a minimal symbol table, go search it using
c5aa993b
JM
545 a binary search. Note that a minimal symbol table always consists
546 of at least two symbols, a "real" symbol and the terminating
547 "null symbol". If there are no real symbols, then there is no
025bb325 548 minimal symbol table at all. */
c906108c 549
34643a32 550 if (objfile->per_bfd->minimal_symbol_count > 0)
c906108c 551 {
29e8a844
DJ
552 int best_zero_sized = -1;
553
34643a32 554 msymbol = objfile->per_bfd->msymbols;
c906108c 555 lo = 0;
34643a32 556 hi = objfile->per_bfd->minimal_symbol_count - 1;
c906108c
SS
557
558 /* This code assumes that the minimal symbols are sorted by
559 ascending address values. If the pc value is greater than or
560 equal to the first symbol's address, then some symbol in this
561 minimal symbol table is a suitable candidate for being the
562 "best" symbol. This includes the last real symbol, for cases
563 where the pc value is larger than any address in this vector.
564
565 By iterating until the address associated with the current
566 hi index (the endpoint of the test interval) is less than
567 or equal to the desired pc value, we accomplish two things:
568 (1) the case where the pc value is larger than any minimal
569 symbol address is trivially solved, (2) the address associated
570 with the hi index is always the one we want when the interation
571 terminates. In essence, we are iterating the test interval
572 down until the pc value is pushed out of it from the high end.
573
025bb325 574 Warning: this code is trickier than it would appear at first. */
c906108c 575
77e371c0
TT
576 if (frob_address (objfile, &pc)
577 && pc >= MSYMBOL_VALUE_RAW_ADDRESS (&msymbol[lo]))
c906108c 578 {
77e371c0 579 while (MSYMBOL_VALUE_RAW_ADDRESS (&msymbol[hi]) > pc)
c906108c 580 {
025bb325
MS
581 /* pc is still strictly less than highest address. */
582 /* Note "new" will always be >= lo. */
c906108c 583 new = (lo + hi) / 2;
77e371c0
TT
584 if ((MSYMBOL_VALUE_RAW_ADDRESS (&msymbol[new]) >= pc)
585 || (lo == new))
c906108c
SS
586 {
587 hi = new;
588 }
589 else
590 {
591 lo = new;
592 }
593 }
594
595 /* If we have multiple symbols at the same address, we want
c5aa993b
JM
596 hi to point to the last one. That way we can find the
597 right symbol if it has an index greater than hi. */
34643a32 598 while (hi < objfile->per_bfd->minimal_symbol_count - 1
77e371c0
TT
599 && (MSYMBOL_VALUE_RAW_ADDRESS (&msymbol[hi])
600 == MSYMBOL_VALUE_RAW_ADDRESS (&msymbol[hi + 1])))
c906108c
SS
601 hi++;
602
29e8a844
DJ
603 /* Skip various undesirable symbols. */
604 while (hi >= 0)
605 {
606 /* Skip any absolute symbols. This is apparently
607 what adb and dbx do, and is needed for the CM-5.
608 There are two known possible problems: (1) on
609 ELF, apparently end, edata, etc. are absolute.
610 Not sure ignoring them here is a big deal, but if
611 we want to use them, the fix would go in
612 elfread.c. (2) I think shared library entry
613 points on the NeXT are absolute. If we want
614 special handling for this it probably should be
615 triggered by a special mst_abs_or_lib or some
616 such. */
617
712f90be 618 if (MSYMBOL_TYPE (&msymbol[hi]) == mst_abs)
29e8a844
DJ
619 {
620 hi--;
621 continue;
622 }
623
624 /* If SECTION was specified, skip any symbol from
625 wrong section. */
626 if (section
627 /* Some types of debug info, such as COFF,
628 don't fill the bfd_section member, so don't
629 throw away symbols on those platforms. */
efd66ac6 630 && MSYMBOL_OBJ_SECTION (objfile, &msymbol[hi]) != NULL
714835d5 631 && (!matching_obj_sections
efd66ac6 632 (MSYMBOL_OBJ_SECTION (objfile, &msymbol[hi]),
e27d198c 633 section)))
29e8a844
DJ
634 {
635 hi--;
636 continue;
637 }
638
2eaf8d2a
DJ
639 /* If we are looking for a trampoline and this is a
640 text symbol, or the other way around, check the
177b42fe 641 preceding symbol too. If they are otherwise
2eaf8d2a
DJ
642 identical prefer that one. */
643 if (hi > 0
644 && MSYMBOL_TYPE (&msymbol[hi]) == other_type
645 && MSYMBOL_TYPE (&msymbol[hi - 1]) == want_type
646 && (MSYMBOL_SIZE (&msymbol[hi])
647 == MSYMBOL_SIZE (&msymbol[hi - 1]))
77e371c0
TT
648 && (MSYMBOL_VALUE_RAW_ADDRESS (&msymbol[hi])
649 == MSYMBOL_VALUE_RAW_ADDRESS (&msymbol[hi - 1]))
efd66ac6
TT
650 && (MSYMBOL_OBJ_SECTION (objfile, &msymbol[hi])
651 == MSYMBOL_OBJ_SECTION (objfile, &msymbol[hi - 1])))
2eaf8d2a
DJ
652 {
653 hi--;
654 continue;
655 }
656
29e8a844
DJ
657 /* If the minimal symbol has a zero size, save it
658 but keep scanning backwards looking for one with
659 a non-zero size. A zero size may mean that the
660 symbol isn't an object or function (e.g. a
661 label), or it may just mean that the size was not
662 specified. */
663 if (MSYMBOL_SIZE (&msymbol[hi]) == 0
664 && best_zero_sized == -1)
665 {
666 best_zero_sized = hi;
667 hi--;
668 continue;
669 }
670
f7a6bb70
DJ
671 /* If we are past the end of the current symbol, try
672 the previous symbol if it has a larger overlapping
673 size. This happens on i686-pc-linux-gnu with glibc;
674 the nocancel variants of system calls are inside
675 the cancellable variants, but both have sizes. */
676 if (hi > 0
677 && MSYMBOL_SIZE (&msymbol[hi]) != 0
77e371c0 678 && pc >= (MSYMBOL_VALUE_RAW_ADDRESS (&msymbol[hi])
f7a6bb70 679 + MSYMBOL_SIZE (&msymbol[hi]))
77e371c0 680 && pc < (MSYMBOL_VALUE_RAW_ADDRESS (&msymbol[hi - 1])
f7a6bb70
DJ
681 + MSYMBOL_SIZE (&msymbol[hi - 1])))
682 {
683 hi--;
684 continue;
685 }
686
29e8a844
DJ
687 /* Otherwise, this symbol must be as good as we're going
688 to get. */
689 break;
690 }
691
692 /* If HI has a zero size, and best_zero_sized is set,
693 then we had two or more zero-sized symbols; prefer
694 the first one we found (which may have a higher
695 address). Also, if we ran off the end, be sure
696 to back up. */
697 if (best_zero_sized != -1
698 && (hi < 0 || MSYMBOL_SIZE (&msymbol[hi]) == 0))
699 hi = best_zero_sized;
700
701 /* If the minimal symbol has a non-zero size, and this
702 PC appears to be outside the symbol's contents, then
703 refuse to use this symbol. If we found a zero-sized
704 symbol with an address greater than this symbol's,
705 use that instead. We assume that if symbols have
706 specified sizes, they do not overlap. */
707
708 if (hi >= 0
709 && MSYMBOL_SIZE (&msymbol[hi]) != 0
77e371c0 710 && pc >= (MSYMBOL_VALUE_RAW_ADDRESS (&msymbol[hi])
29e8a844
DJ
711 + MSYMBOL_SIZE (&msymbol[hi])))
712 {
713 if (best_zero_sized != -1)
714 hi = best_zero_sized;
715 else
716 /* Go on to the next object file. */
717 continue;
718 }
719
c906108c 720 /* The minimal symbol indexed by hi now is the best one in this
c5aa993b 721 objfile's minimal symbol table. See if it is the best one
025bb325 722 overall. */
c906108c 723
c906108c
SS
724 if (hi >= 0
725 && ((best_symbol == NULL) ||
77e371c0
TT
726 (MSYMBOL_VALUE_RAW_ADDRESS (best_symbol) <
727 MSYMBOL_VALUE_RAW_ADDRESS (&msymbol[hi]))))
c906108c
SS
728 {
729 best_symbol = &msymbol[hi];
7cbd4a93 730 best_objfile = objfile;
c906108c
SS
731 }
732 }
733 }
734 }
7cbd4a93
TT
735
736 result.minsym = best_symbol;
737 result.objfile = best_objfile;
738 return result;
c906108c
SS
739}
740
7cbd4a93 741struct bound_minimal_symbol
714835d5 742lookup_minimal_symbol_by_pc_section (CORE_ADDR pc, struct obj_section *section)
2eaf8d2a 743{
00878c6e
PP
744 if (section == NULL)
745 {
746 /* NOTE: cagney/2004-01-27: This was using find_pc_mapped_section to
747 force the section but that (well unless you're doing overlay
748 debugging) always returns NULL making the call somewhat useless. */
749 section = find_pc_section (pc);
750 if (section == NULL)
7cbd4a93
TT
751 {
752 struct bound_minimal_symbol result;
753
754 memset (&result, 0, sizeof (result));
755 return result;
756 }
00878c6e 757 }
2eaf8d2a
DJ
758 return lookup_minimal_symbol_by_pc_section_1 (pc, section, 0);
759}
760
b19686e0 761/* See minsyms.h. */
c906108c 762
7cbd4a93 763struct bound_minimal_symbol
fba45db2 764lookup_minimal_symbol_by_pc (CORE_ADDR pc)
c906108c 765{
7cbd4a93
TT
766 struct obj_section *section = find_pc_section (pc);
767
768 if (section == NULL)
769 {
770 struct bound_minimal_symbol result;
771
772 memset (&result, 0, sizeof (result));
773 return result;
774 }
775 return lookup_minimal_symbol_by_pc_section_1 (pc, section, 0);
c906108c 776}
0d5392b8 777
0875794a
JK
778/* Return non-zero iff PC is in an STT_GNU_IFUNC function resolver. */
779
780int
781in_gnu_ifunc_stub (CORE_ADDR pc)
782{
7cbd4a93 783 struct bound_minimal_symbol msymbol = lookup_minimal_symbol_by_pc (pc);
0875794a 784
7cbd4a93 785 return msymbol.minsym && MSYMBOL_TYPE (msymbol.minsym) == mst_text_gnu_ifunc;
0875794a
JK
786}
787
07be84bf
JK
788/* See elf_gnu_ifunc_resolve_addr for its real implementation. */
789
790static CORE_ADDR
791stub_gnu_ifunc_resolve_addr (struct gdbarch *gdbarch, CORE_ADDR pc)
792{
793 error (_("GDB cannot resolve STT_GNU_IFUNC symbol at address %s without "
794 "the ELF support compiled in."),
795 paddress (gdbarch, pc));
796}
797
798/* See elf_gnu_ifunc_resolve_name for its real implementation. */
799
800static int
801stub_gnu_ifunc_resolve_name (const char *function_name,
802 CORE_ADDR *function_address_p)
803{
804 error (_("GDB cannot resolve STT_GNU_IFUNC symbol \"%s\" without "
805 "the ELF support compiled in."),
806 function_name);
807}
808
0e30163f
JK
809/* See elf_gnu_ifunc_resolver_stop for its real implementation. */
810
811static void
812stub_gnu_ifunc_resolver_stop (struct breakpoint *b)
813{
814 internal_error (__FILE__, __LINE__,
815 _("elf_gnu_ifunc_resolver_stop cannot be reached."));
816}
817
818/* See elf_gnu_ifunc_resolver_return_stop for its real implementation. */
819
820static void
821stub_gnu_ifunc_resolver_return_stop (struct breakpoint *b)
822{
823 internal_error (__FILE__, __LINE__,
824 _("elf_gnu_ifunc_resolver_return_stop cannot be reached."));
825}
826
07be84bf
JK
827/* See elf_gnu_ifunc_fns for its real implementation. */
828
829static const struct gnu_ifunc_fns stub_gnu_ifunc_fns =
830{
831 stub_gnu_ifunc_resolve_addr,
832 stub_gnu_ifunc_resolve_name,
0e30163f
JK
833 stub_gnu_ifunc_resolver_stop,
834 stub_gnu_ifunc_resolver_return_stop,
07be84bf
JK
835};
836
837/* A placeholder for &elf_gnu_ifunc_fns. */
838
839const struct gnu_ifunc_fns *gnu_ifunc_fns_p = &stub_gnu_ifunc_fns;
840
b19686e0 841/* See minsyms.h. */
0d5392b8 842
7cbd4a93
TT
843struct bound_minimal_symbol
844lookup_minimal_symbol_and_objfile (const char *name)
0d5392b8 845{
7cbd4a93 846 struct bound_minimal_symbol result;
0d5392b8
TT
847 struct objfile *objfile;
848 unsigned int hash = msymbol_hash (name) % MINIMAL_SYMBOL_HASH_SIZE;
849
850 ALL_OBJFILES (objfile)
851 {
852 struct minimal_symbol *msym;
853
34643a32 854 for (msym = objfile->per_bfd->msymbol_hash[hash];
0d5392b8
TT
855 msym != NULL;
856 msym = msym->hash_next)
857 {
efd66ac6 858 if (strcmp (MSYMBOL_LINKAGE_NAME (msym), name) == 0)
0d5392b8 859 {
7cbd4a93
TT
860 result.minsym = msym;
861 result.objfile = objfile;
862 return result;
0d5392b8
TT
863 }
864 }
865 }
866
7cbd4a93
TT
867 memset (&result, 0, sizeof (result));
868 return result;
0d5392b8 869}
c906108c 870\f
c5aa993b 871
025bb325 872/* Return leading symbol character for a BFD. If BFD is NULL,
c906108c
SS
873 return the leading symbol character from the main objfile. */
874
c906108c 875static int
fba45db2 876get_symbol_leading_char (bfd *abfd)
c906108c
SS
877{
878 if (abfd != NULL)
879 return bfd_get_symbol_leading_char (abfd);
880 if (symfile_objfile != NULL && symfile_objfile->obfd != NULL)
881 return bfd_get_symbol_leading_char (symfile_objfile->obfd);
882 return 0;
883}
884
b19686e0 885/* See minsyms.h. */
c906108c
SS
886
887void
fba45db2 888init_minimal_symbol_collection (void)
c906108c
SS
889{
890 msym_count = 0;
891 msym_bunch = NULL;
b19686e0
TT
892 /* Note that presetting msym_bunch_index to BUNCH_SIZE causes the
893 first call to save a minimal symbol to allocate the memory for
894 the first bunch. */
c906108c
SS
895 msym_bunch_index = BUNCH_SIZE;
896}
897
b19686e0
TT
898/* See minsyms.h. */
899
c906108c 900void
fba45db2
KB
901prim_record_minimal_symbol (const char *name, CORE_ADDR address,
902 enum minimal_symbol_type ms_type,
903 struct objfile *objfile)
c906108c
SS
904{
905 int section;
906
907 switch (ms_type)
908 {
909 case mst_text:
0875794a 910 case mst_text_gnu_ifunc:
c906108c
SS
911 case mst_file_text:
912 case mst_solib_trampoline:
b8fbeb18 913 section = SECT_OFF_TEXT (objfile);
c906108c
SS
914 break;
915 case mst_data:
916 case mst_file_data:
b8fbeb18 917 section = SECT_OFF_DATA (objfile);
c906108c
SS
918 break;
919 case mst_bss:
920 case mst_file_bss:
b8fbeb18 921 section = SECT_OFF_BSS (objfile);
c906108c
SS
922 break;
923 default:
924 section = -1;
925 }
926
927 prim_record_minimal_symbol_and_info (name, address, ms_type,
e6dc44a8 928 section, objfile);
c906108c
SS
929}
930
b19686e0 931/* See minsyms.h. */
c906108c
SS
932
933struct minimal_symbol *
04a679b8
TT
934prim_record_minimal_symbol_full (const char *name, int name_len, int copy_name,
935 CORE_ADDR address,
936 enum minimal_symbol_type ms_type,
937 int section,
04a679b8 938 struct objfile *objfile)
c906108c 939{
714835d5 940 struct obj_section *obj_section;
52f0bd74
AC
941 struct msym_bunch *new;
942 struct minimal_symbol *msymbol;
c906108c 943
66337bb1
CV
944 /* Don't put gcc_compiled, __gnu_compiled_cplus, and friends into
945 the minimal symbols, because if there is also another symbol
946 at the same address (e.g. the first function of the file),
947 lookup_minimal_symbol_by_pc would have no way of getting the
948 right one. */
949 if (ms_type == mst_file_text && name[0] == 'g'
950 && (strcmp (name, GCC_COMPILED_FLAG_SYMBOL) == 0
951 || strcmp (name, GCC2_COMPILED_FLAG_SYMBOL) == 0))
952 return (NULL);
953
954 /* It's safe to strip the leading char here once, since the name
025bb325 955 is also stored stripped in the minimal symbol table. */
66337bb1 956 if (name[0] == get_symbol_leading_char (objfile->obfd))
04a679b8
TT
957 {
958 ++name;
959 --name_len;
960 }
66337bb1
CV
961
962 if (ms_type == mst_file_text && strncmp (name, "__gnu_compiled", 14) == 0)
963 return (NULL);
c906108c
SS
964
965 if (msym_bunch_index == BUNCH_SIZE)
966 {
fc270c35 967 new = XCNEW (struct msym_bunch);
c906108c 968 msym_bunch_index = 0;
c5aa993b 969 new->next = msym_bunch;
c906108c
SS
970 msym_bunch = new;
971 }
c5aa993b 972 msymbol = &msym_bunch->contents[msym_bunch_index];
34643a32
TT
973 MSYMBOL_SET_LANGUAGE (msymbol, language_auto,
974 &objfile->per_bfd->storage_obstack);
efd66ac6 975 MSYMBOL_SET_NAMES (msymbol, name, name_len, copy_name, objfile);
2de7ced7 976
40c1a007 977 SET_MSYMBOL_VALUE_ADDRESS (msymbol, address);
efd66ac6 978 MSYMBOL_SECTION (msymbol) = section;
714835d5 979
c906108c 980 MSYMBOL_TYPE (msymbol) = ms_type;
b887350f
TT
981 MSYMBOL_TARGET_FLAG_1 (msymbol) = 0;
982 MSYMBOL_TARGET_FLAG_2 (msymbol) = 0;
d9eaeb59
JB
983 /* Do not use the SET_MSYMBOL_SIZE macro to initialize the size,
984 as it would also set the has_size flag. */
985 msymbol->size = 0;
9227b5eb 986
a79dea61 987 /* The hash pointers must be cleared! If they're not,
025bb325 988 add_minsym_to_hash_table will NOT add this msymbol to the hash table. */
9227b5eb
JB
989 msymbol->hash_next = NULL;
990 msymbol->demangled_hash_next = NULL;
991
34643a32
TT
992 /* If we already read minimal symbols for this objfile, then don't
993 ever allocate a new one. */
994 if (!objfile->per_bfd->minsyms_read)
5f6cac40
TT
995 {
996 msym_bunch_index++;
997 objfile->per_bfd->n_minsyms++;
998 }
c906108c 999 msym_count++;
c906108c
SS
1000 return msymbol;
1001}
1002
b19686e0 1003/* See minsyms.h. */
04a679b8
TT
1004
1005struct minimal_symbol *
1006prim_record_minimal_symbol_and_info (const char *name, CORE_ADDR address,
1007 enum minimal_symbol_type ms_type,
1008 int section,
04a679b8
TT
1009 struct objfile *objfile)
1010{
1011 return prim_record_minimal_symbol_full (name, strlen (name), 1,
e6dc44a8
TT
1012 address, ms_type,
1013 section, objfile);
04a679b8
TT
1014}
1015
c906108c 1016/* Compare two minimal symbols by address and return a signed result based
025bb325 1017 on unsigned comparisons, so that we sort into unsigned numeric order.
c906108c
SS
1018 Within groups with the same address, sort by name. */
1019
1020static int
12b9c64f 1021compare_minimal_symbols (const void *fn1p, const void *fn2p)
c906108c 1022{
52f0bd74
AC
1023 const struct minimal_symbol *fn1;
1024 const struct minimal_symbol *fn2;
c906108c
SS
1025
1026 fn1 = (const struct minimal_symbol *) fn1p;
1027 fn2 = (const struct minimal_symbol *) fn2p;
1028
77e371c0 1029 if (MSYMBOL_VALUE_RAW_ADDRESS (fn1) < MSYMBOL_VALUE_RAW_ADDRESS (fn2))
c906108c 1030 {
025bb325 1031 return (-1); /* addr 1 is less than addr 2. */
c906108c 1032 }
77e371c0 1033 else if (MSYMBOL_VALUE_RAW_ADDRESS (fn1) > MSYMBOL_VALUE_RAW_ADDRESS (fn2))
c906108c 1034 {
025bb325 1035 return (1); /* addr 1 is greater than addr 2. */
c906108c 1036 }
c5aa993b
JM
1037 else
1038 /* addrs are equal: sort by name */
c906108c 1039 {
efd66ac6
TT
1040 const char *name1 = MSYMBOL_LINKAGE_NAME (fn1);
1041 const char *name2 = MSYMBOL_LINKAGE_NAME (fn2);
c906108c
SS
1042
1043 if (name1 && name2) /* both have names */
1044 return strcmp (name1, name2);
1045 else if (name2)
025bb325
MS
1046 return 1; /* fn1 has no name, so it is "less". */
1047 else if (name1) /* fn2 has no name, so it is "less". */
c906108c
SS
1048 return -1;
1049 else
025bb325 1050 return (0); /* Neither has a name, so they're equal. */
c906108c
SS
1051 }
1052}
1053
1054/* Discard the currently collected minimal symbols, if any. If we wish
1055 to save them for later use, we must have already copied them somewhere
1056 else before calling this function.
1057
1058 FIXME: We could allocate the minimal symbol bunches on their own
1059 obstack and then simply blow the obstack away when we are done with
025bb325 1060 it. Is it worth the extra trouble though? */
c906108c 1061
56e290f4
AC
1062static void
1063do_discard_minimal_symbols_cleanup (void *arg)
c906108c 1064{
52f0bd74 1065 struct msym_bunch *next;
c906108c
SS
1066
1067 while (msym_bunch != NULL)
1068 {
c5aa993b 1069 next = msym_bunch->next;
b8c9b27d 1070 xfree (msym_bunch);
c906108c
SS
1071 msym_bunch = next;
1072 }
1073}
1074
b19686e0
TT
1075/* See minsyms.h. */
1076
56e290f4
AC
1077struct cleanup *
1078make_cleanup_discard_minimal_symbols (void)
1079{
1080 return make_cleanup (do_discard_minimal_symbols_cleanup, 0);
1081}
1082
1083
9227b5eb 1084
c906108c
SS
1085/* Compact duplicate entries out of a minimal symbol table by walking
1086 through the table and compacting out entries with duplicate addresses
1087 and matching names. Return the number of entries remaining.
1088
1089 On entry, the table resides between msymbol[0] and msymbol[mcount].
1090 On exit, it resides between msymbol[0] and msymbol[result_count].
1091
1092 When files contain multiple sources of symbol information, it is
1093 possible for the minimal symbol table to contain many duplicate entries.
1094 As an example, SVR4 systems use ELF formatted object files, which
1095 usually contain at least two different types of symbol tables (a
1096 standard ELF one and a smaller dynamic linking table), as well as
1097 DWARF debugging information for files compiled with -g.
1098
1099 Without compacting, the minimal symbol table for gdb itself contains
1100 over a 1000 duplicates, about a third of the total table size. Aside
1101 from the potential trap of not noticing that two successive entries
1102 identify the same location, this duplication impacts the time required
1103 to linearly scan the table, which is done in a number of places. So we
1104 just do one linear scan here and toss out the duplicates.
1105
1106 Note that we are not concerned here about recovering the space that
1107 is potentially freed up, because the strings themselves are allocated
34643a32 1108 on the storage_obstack, and will get automatically freed when the symbol
c906108c
SS
1109 table is freed. The caller can free up the unused minimal symbols at
1110 the end of the compacted region if their allocation strategy allows it.
1111
1112 Also note we only go up to the next to last entry within the loop
1113 and then copy the last entry explicitly after the loop terminates.
1114
1115 Since the different sources of information for each symbol may
1116 have different levels of "completeness", we may have duplicates
1117 that have one entry with type "mst_unknown" and the other with a
1118 known type. So if the one we are leaving alone has type mst_unknown,
1119 overwrite its type with the type from the one we are compacting out. */
1120
1121static int
fba45db2
KB
1122compact_minimal_symbols (struct minimal_symbol *msymbol, int mcount,
1123 struct objfile *objfile)
c906108c
SS
1124{
1125 struct minimal_symbol *copyfrom;
1126 struct minimal_symbol *copyto;
1127
1128 if (mcount > 0)
1129 {
1130 copyfrom = copyto = msymbol;
1131 while (copyfrom < msymbol + mcount - 1)
1132 {
77e371c0
TT
1133 if (MSYMBOL_VALUE_RAW_ADDRESS (copyfrom)
1134 == MSYMBOL_VALUE_RAW_ADDRESS ((copyfrom + 1))
1135 && MSYMBOL_SECTION (copyfrom) == MSYMBOL_SECTION (copyfrom + 1)
efd66ac6
TT
1136 && strcmp (MSYMBOL_LINKAGE_NAME (copyfrom),
1137 MSYMBOL_LINKAGE_NAME ((copyfrom + 1))) == 0)
c906108c 1138 {
c5aa993b 1139 if (MSYMBOL_TYPE ((copyfrom + 1)) == mst_unknown)
c906108c
SS
1140 {
1141 MSYMBOL_TYPE ((copyfrom + 1)) = MSYMBOL_TYPE (copyfrom);
1142 }
1143 copyfrom++;
1144 }
1145 else
afbb8d7a 1146 *copyto++ = *copyfrom++;
c906108c
SS
1147 }
1148 *copyto++ = *copyfrom++;
1149 mcount = copyto - msymbol;
1150 }
1151 return (mcount);
1152}
1153
afbb8d7a
KB
1154/* Build (or rebuild) the minimal symbol hash tables. This is necessary
1155 after compacting or sorting the table since the entries move around
025bb325 1156 thus causing the internal minimal_symbol pointers to become jumbled. */
afbb8d7a
KB
1157
1158static void
1159build_minimal_symbol_hash_tables (struct objfile *objfile)
1160{
1161 int i;
1162 struct minimal_symbol *msym;
1163
025bb325 1164 /* Clear the hash tables. */
afbb8d7a
KB
1165 for (i = 0; i < MINIMAL_SYMBOL_HASH_SIZE; i++)
1166 {
34643a32
TT
1167 objfile->per_bfd->msymbol_hash[i] = 0;
1168 objfile->per_bfd->msymbol_demangled_hash[i] = 0;
afbb8d7a
KB
1169 }
1170
025bb325 1171 /* Now, (re)insert the actual entries. */
34643a32
TT
1172 for ((i = objfile->per_bfd->minimal_symbol_count,
1173 msym = objfile->per_bfd->msymbols);
afbb8d7a
KB
1174 i > 0;
1175 i--, msym++)
1176 {
1177 msym->hash_next = 0;
34643a32 1178 add_minsym_to_hash_table (msym, objfile->per_bfd->msymbol_hash);
afbb8d7a
KB
1179
1180 msym->demangled_hash_next = 0;
efd66ac6 1181 if (MSYMBOL_SEARCH_NAME (msym) != MSYMBOL_LINKAGE_NAME (msym))
afbb8d7a 1182 add_minsym_to_demangled_hash_table (msym,
34643a32 1183 objfile->per_bfd->msymbol_demangled_hash);
afbb8d7a
KB
1184 }
1185}
1186
c906108c
SS
1187/* Add the minimal symbols in the existing bunches to the objfile's official
1188 minimal symbol table. In most cases there is no minimal symbol table yet
1189 for this objfile, and the existing bunches are used to create one. Once
1190 in a while (for shared libraries for example), we add symbols (e.g. common
1191 symbols) to an existing objfile.
1192
1193 Because of the way minimal symbols are collected, we generally have no way
1194 of knowing what source language applies to any particular minimal symbol.
1195 Specifically, we have no way of knowing if the minimal symbol comes from a
1196 C++ compilation unit or not. So for the sake of supporting cached
1197 demangled C++ names, we have no choice but to try and demangle each new one
1198 that comes in. If the demangling succeeds, then we assume it is a C++
1199 symbol and set the symbol's language and demangled name fields
1200 appropriately. Note that in order to avoid unnecessary demanglings, and
1201 allocating obstack space that subsequently can't be freed for the demangled
1202 names, we mark all newly added symbols with language_auto. After
1203 compaction of the minimal symbols, we go back and scan the entire minimal
1204 symbol table looking for these new symbols. For each new symbol we attempt
1205 to demangle it, and if successful, record it as a language_cplus symbol
1206 and cache the demangled form on the symbol obstack. Symbols which don't
1207 demangle are marked as language_unknown symbols, which inhibits future
025bb325 1208 attempts to demangle them if we later add more minimal symbols. */
c906108c
SS
1209
1210void
fba45db2 1211install_minimal_symbols (struct objfile *objfile)
c906108c 1212{
52f0bd74
AC
1213 int bindex;
1214 int mcount;
1215 struct msym_bunch *bunch;
1216 struct minimal_symbol *msymbols;
c906108c 1217 int alloc_count;
c906108c 1218
34643a32
TT
1219 if (objfile->per_bfd->minsyms_read)
1220 return;
1221
c906108c
SS
1222 if (msym_count > 0)
1223 {
45cfd468
DE
1224 if (symtab_create_debug)
1225 {
1226 fprintf_unfiltered (gdb_stdlog,
1227 "Installing %d minimal symbols of objfile %s.\n",
4262abfb 1228 msym_count, objfile_name (objfile));
45cfd468
DE
1229 }
1230
c906108c 1231 /* Allocate enough space in the obstack, into which we will gather the
c5aa993b
JM
1232 bunches of new and existing minimal symbols, sort them, and then
1233 compact out the duplicate entries. Once we have a final table,
1234 we will give back the excess space. */
c906108c 1235
34643a32
TT
1236 alloc_count = msym_count + objfile->per_bfd->minimal_symbol_count + 1;
1237 obstack_blank (&objfile->per_bfd->storage_obstack,
c906108c
SS
1238 alloc_count * sizeof (struct minimal_symbol));
1239 msymbols = (struct minimal_symbol *)
34643a32 1240 obstack_base (&objfile->per_bfd->storage_obstack);
c906108c
SS
1241
1242 /* Copy in the existing minimal symbols, if there are any. */
1243
34643a32
TT
1244 if (objfile->per_bfd->minimal_symbol_count)
1245 memcpy ((char *) msymbols, (char *) objfile->per_bfd->msymbols,
1246 objfile->per_bfd->minimal_symbol_count * sizeof (struct minimal_symbol));
c906108c
SS
1247
1248 /* Walk through the list of minimal symbol bunches, adding each symbol
c5aa993b
JM
1249 to the new contiguous array of symbols. Note that we start with the
1250 current, possibly partially filled bunch (thus we use the current
1251 msym_bunch_index for the first bunch we copy over), and thereafter
025bb325 1252 each bunch is full. */
c5aa993b 1253
34643a32 1254 mcount = objfile->per_bfd->minimal_symbol_count;
c5aa993b
JM
1255
1256 for (bunch = msym_bunch; bunch != NULL; bunch = bunch->next)
c906108c
SS
1257 {
1258 for (bindex = 0; bindex < msym_bunch_index; bindex++, mcount++)
66337bb1 1259 msymbols[mcount] = bunch->contents[bindex];
c906108c
SS
1260 msym_bunch_index = BUNCH_SIZE;
1261 }
1262
1263 /* Sort the minimal symbols by address. */
c5aa993b 1264
c906108c
SS
1265 qsort (msymbols, mcount, sizeof (struct minimal_symbol),
1266 compare_minimal_symbols);
c5aa993b 1267
c906108c 1268 /* Compact out any duplicates, and free up whatever space we are
c5aa993b
JM
1269 no longer using. */
1270
9227b5eb 1271 mcount = compact_minimal_symbols (msymbols, mcount, objfile);
c906108c 1272
ee11262d 1273 obstack_blank_fast (&objfile->per_bfd->storage_obstack,
c5aa993b 1274 (mcount + 1 - alloc_count) * sizeof (struct minimal_symbol));
c906108c 1275 msymbols = (struct minimal_symbol *)
34643a32 1276 obstack_finish (&objfile->per_bfd->storage_obstack);
c906108c
SS
1277
1278 /* We also terminate the minimal symbol table with a "null symbol",
c5aa993b
JM
1279 which is *not* included in the size of the table. This makes it
1280 easier to find the end of the table when we are handed a pointer
1281 to some symbol in the middle of it. Zero out the fields in the
1282 "null symbol" allocated at the end of the array. Note that the
1283 symbol count does *not* include this null symbol, which is why it
025bb325 1284 is indexed by mcount and not mcount-1. */
c906108c 1285
a83e9154 1286 memset (&msymbols[mcount], 0, sizeof (struct minimal_symbol));
c906108c
SS
1287
1288 /* Attach the minimal symbol table to the specified objfile.
34643a32 1289 The strings themselves are also located in the storage_obstack
c5aa993b 1290 of this objfile. */
c906108c 1291
34643a32
TT
1292 objfile->per_bfd->minimal_symbol_count = mcount;
1293 objfile->per_bfd->msymbols = msymbols;
c906108c 1294
afbb8d7a
KB
1295 /* Now build the hash tables; we can't do this incrementally
1296 at an earlier point since we weren't finished with the obstack
1297 yet. (And if the msymbol obstack gets moved, all the internal
025bb325 1298 pointers to other msymbols need to be adjusted.) */
afbb8d7a 1299 build_minimal_symbol_hash_tables (objfile);
c906108c
SS
1300 }
1301}
1302
c35384fb
TT
1303/* See minsyms.h. */
1304
1305void
1306terminate_minimal_symbol_table (struct objfile *objfile)
1307{
34643a32
TT
1308 if (! objfile->per_bfd->msymbols)
1309 objfile->per_bfd->msymbols
1310 = ((struct minimal_symbol *)
1311 obstack_alloc (&objfile->per_bfd->storage_obstack,
1312 sizeof (struct minimal_symbol)));
c35384fb
TT
1313
1314 {
1315 struct minimal_symbol *m
34643a32 1316 = &objfile->per_bfd->msymbols[objfile->per_bfd->minimal_symbol_count];
c35384fb
TT
1317
1318 memset (m, 0, sizeof (*m));
1319 /* Don't rely on these enumeration values being 0's. */
1320 MSYMBOL_TYPE (m) = mst_unknown;
34643a32
TT
1321 MSYMBOL_SET_LANGUAGE (m, language_unknown,
1322 &objfile->per_bfd->storage_obstack);
c35384fb
TT
1323 }
1324}
1325
c9630d9c
TT
1326/* Check if PC is in a shared library trampoline code stub.
1327 Return minimal symbol for the trampoline entry or NULL if PC is not
1328 in a trampoline code stub. */
c906108c 1329
c9630d9c 1330static struct minimal_symbol *
fba45db2 1331lookup_solib_trampoline_symbol_by_pc (CORE_ADDR pc)
c906108c 1332{
2eaf8d2a 1333 struct obj_section *section = find_pc_section (pc);
7cbd4a93 1334 struct bound_minimal_symbol msymbol;
2eaf8d2a
DJ
1335
1336 if (section == NULL)
1337 return NULL;
714835d5 1338 msymbol = lookup_minimal_symbol_by_pc_section_1 (pc, section, 1);
c906108c 1339
7cbd4a93
TT
1340 if (msymbol.minsym != NULL
1341 && MSYMBOL_TYPE (msymbol.minsym) == mst_solib_trampoline)
1342 return msymbol.minsym;
c906108c
SS
1343 return NULL;
1344}
1345
1346/* If PC is in a shared library trampoline code stub, return the
1347 address of the `real' function belonging to the stub.
1348 Return 0 if PC is not in a trampoline code stub or if the real
1349 function is not found in the minimal symbol table.
1350
1351 We may fail to find the right function if a function with the
1352 same name is defined in more than one shared library, but this
025bb325 1353 is considered bad programming style. We could return 0 if we find
c906108c
SS
1354 a duplicate function in case this matters someday. */
1355
1356CORE_ADDR
52f729a7 1357find_solib_trampoline_target (struct frame_info *frame, CORE_ADDR pc)
c906108c
SS
1358{
1359 struct objfile *objfile;
1360 struct minimal_symbol *msymbol;
1361 struct minimal_symbol *tsymbol = lookup_solib_trampoline_symbol_by_pc (pc);
1362
1363 if (tsymbol != NULL)
1364 {
1365 ALL_MSYMBOLS (objfile, msymbol)
c5aa993b 1366 {
0875794a
JK
1367 if ((MSYMBOL_TYPE (msymbol) == mst_text
1368 || MSYMBOL_TYPE (msymbol) == mst_text_gnu_ifunc)
efd66ac6
TT
1369 && strcmp (MSYMBOL_LINKAGE_NAME (msymbol),
1370 MSYMBOL_LINKAGE_NAME (tsymbol)) == 0)
77e371c0 1371 return MSYMBOL_VALUE_ADDRESS (objfile, msymbol);
42848c96
UW
1372
1373 /* Also handle minimal symbols pointing to function descriptors. */
1374 if (MSYMBOL_TYPE (msymbol) == mst_data
efd66ac6
TT
1375 && strcmp (MSYMBOL_LINKAGE_NAME (msymbol),
1376 MSYMBOL_LINKAGE_NAME (tsymbol)) == 0)
42848c96
UW
1377 {
1378 CORE_ADDR func;
b8d56208 1379
42848c96
UW
1380 func = gdbarch_convert_from_func_ptr_addr
1381 (get_objfile_arch (objfile),
77e371c0 1382 MSYMBOL_VALUE_ADDRESS (objfile, msymbol),
42848c96
UW
1383 &current_target);
1384
1385 /* Ignore data symbols that are not function descriptors. */
77e371c0 1386 if (func != MSYMBOL_VALUE_ADDRESS (objfile, msymbol))
42848c96
UW
1387 return func;
1388 }
c5aa993b 1389 }
c906108c
SS
1390 }
1391 return 0;
1392}
50e65b17
TT
1393
1394/* See minsyms.h. */
1395
1396CORE_ADDR
1397minimal_symbol_upper_bound (struct bound_minimal_symbol minsym)
1398{
1399 int i;
1400 short section;
1401 struct obj_section *obj_section;
1402 CORE_ADDR result;
1403 struct minimal_symbol *msymbol;
1404
1405 gdb_assert (minsym.minsym != NULL);
1406
1407 /* If the minimal symbol has a size, use it. Otherwise use the
1408 lesser of the next minimal symbol in the same section, or the end
1409 of the section, as the end of the function. */
1410
1411 if (MSYMBOL_SIZE (minsym.minsym) != 0)
77e371c0 1412 return BMSYMBOL_VALUE_ADDRESS (minsym) + MSYMBOL_SIZE (minsym.minsym);
50e65b17
TT
1413
1414 /* Step over other symbols at this same address, and symbols in
1415 other sections, to find the next symbol in this section with a
1416 different address. */
1417
1418 msymbol = minsym.minsym;
efd66ac6
TT
1419 section = MSYMBOL_SECTION (msymbol);
1420 for (i = 1; MSYMBOL_LINKAGE_NAME (msymbol + i) != NULL; i++)
50e65b17 1421 {
77e371c0
TT
1422 if ((MSYMBOL_VALUE_RAW_ADDRESS (msymbol + i)
1423 != MSYMBOL_VALUE_RAW_ADDRESS (msymbol))
efd66ac6 1424 && MSYMBOL_SECTION (msymbol + i) == section)
50e65b17
TT
1425 break;
1426 }
1427
efd66ac6
TT
1428 obj_section = MSYMBOL_OBJ_SECTION (minsym.objfile, minsym.minsym);
1429 if (MSYMBOL_LINKAGE_NAME (msymbol + i) != NULL
77e371c0 1430 && (MSYMBOL_VALUE_ADDRESS (minsym.objfile, msymbol + i)
efd66ac6 1431 < obj_section_endaddr (obj_section)))
77e371c0 1432 result = MSYMBOL_VALUE_ADDRESS (minsym.objfile, msymbol + i);
50e65b17
TT
1433 else
1434 /* We got the start address from the last msymbol in the objfile.
1435 So the end address is the end of the section. */
1436 result = obj_section_endaddr (obj_section);
1437
1438 return result;
1439}
This page took 1.333549 seconds and 4 git commands to generate.