introduce minimal_symbol_upper_bound
[deliverable/binutils-gdb.git] / gdb / block.c
CommitLineData
fe898f56
DC
1/* Block-related functions for the GNU debugger, GDB.
2
ecd75fc8 3 Copyright (C) 2003-2014 Free Software Foundation, Inc.
fe898f56
DC
4
5 This file is part of GDB.
6
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
a9762ec7 9 the Free Software Foundation; either version 3 of the License, or
fe898f56
DC
10 (at your option) any later version.
11
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
16
17 You should have received a copy of the GNU General Public License
a9762ec7 18 along with this program. If not, see <http://www.gnu.org/licenses/>. */
fe898f56
DC
19
20#include "defs.h"
21#include "block.h"
22#include "symtab.h"
23#include "symfile.h"
9219021c
DC
24#include "gdb_obstack.h"
25#include "cp-support.h"
801e3a5b 26#include "addrmap.h"
8e3b41a9
JK
27#include "gdbtypes.h"
28#include "exceptions.h"
9219021c
DC
29
30/* This is used by struct block to store namespace-related info for
31 C++ files, namely using declarations and the current namespace in
32 scope. */
33
34struct block_namespace_info
35{
36 const char *scope;
37 struct using_direct *using;
38};
39
40static void block_initialize_namespace (struct block *block,
41 struct obstack *obstack);
fe898f56
DC
42
43/* Return Nonzero if block a is lexically nested within block b,
44 or if a and b have the same pc range.
4a64f543 45 Return zero otherwise. */
fe898f56
DC
46
47int
0cf566ec 48contained_in (const struct block *a, const struct block *b)
fe898f56
DC
49{
50 if (!a || !b)
51 return 0;
edb3359d
DJ
52
53 do
54 {
55 if (a == b)
56 return 1;
49e794ac
JB
57 /* If A is a function block, then A cannot be contained in B,
58 except if A was inlined. */
59 if (BLOCK_FUNCTION (a) != NULL && !block_inlined_p (a))
60 return 0;
edb3359d
DJ
61 a = BLOCK_SUPERBLOCK (a);
62 }
63 while (a != NULL);
64
65 return 0;
fe898f56
DC
66}
67
68
69/* Return the symbol for the function which contains a specified
7f0df278
DJ
70 lexical block, described by a struct block BL. The return value
71 will not be an inlined function; the containing function will be
72 returned instead. */
fe898f56
DC
73
74struct symbol *
7f0df278 75block_linkage_function (const struct block *bl)
fe898f56 76{
edb3359d
DJ
77 while ((BLOCK_FUNCTION (bl) == NULL || block_inlined_p (bl))
78 && BLOCK_SUPERBLOCK (bl) != NULL)
fe898f56
DC
79 bl = BLOCK_SUPERBLOCK (bl);
80
81 return BLOCK_FUNCTION (bl);
82}
83
f8eba3c6
TT
84/* Return the symbol for the function which contains a specified
85 block, described by a struct block BL. The return value will be
86 the closest enclosing function, which might be an inline
87 function. */
88
89struct symbol *
90block_containing_function (const struct block *bl)
91{
92 while (BLOCK_FUNCTION (bl) == NULL && BLOCK_SUPERBLOCK (bl) != NULL)
93 bl = BLOCK_SUPERBLOCK (bl);
94
95 return BLOCK_FUNCTION (bl);
96}
97
edb3359d
DJ
98/* Return one if BL represents an inlined function. */
99
100int
101block_inlined_p (const struct block *bl)
102{
103 return BLOCK_FUNCTION (bl) != NULL && SYMBOL_INLINED (BLOCK_FUNCTION (bl));
104}
105
9703b513
TT
106/* A helper function that checks whether PC is in the blockvector BL.
107 It returns the containing block if there is one, or else NULL. */
fe898f56 108
9703b513
TT
109static struct block *
110find_block_in_blockvector (struct blockvector *bl, CORE_ADDR pc)
fe898f56 111{
b59661bd
AC
112 struct block *b;
113 int bot, top, half;
fe898f56 114
801e3a5b
JB
115 /* If we have an addrmap mapping code addresses to blocks, then use
116 that. */
117 if (BLOCKVECTOR_MAP (bl))
9703b513 118 return addrmap_find (BLOCKVECTOR_MAP (bl), pc);
801e3a5b
JB
119
120 /* Otherwise, use binary search to find the last block that starts
6ac9ef80
DE
121 before PC.
122 Note: GLOBAL_BLOCK is block 0, STATIC_BLOCK is block 1.
123 They both have the same START,END values.
124 Historically this code would choose STATIC_BLOCK over GLOBAL_BLOCK but the
125 fact that this choice was made was subtle, now we make it explicit. */
126 gdb_assert (BLOCKVECTOR_NBLOCKS (bl) >= 2);
127 bot = STATIC_BLOCK;
fe898f56
DC
128 top = BLOCKVECTOR_NBLOCKS (bl);
129
130 while (top - bot > 1)
131 {
132 half = (top - bot + 1) >> 1;
133 b = BLOCKVECTOR_BLOCK (bl, bot + half);
134 if (BLOCK_START (b) <= pc)
135 bot += half;
136 else
137 top = bot + half;
138 }
139
140 /* Now search backward for a block that ends after PC. */
141
6ac9ef80 142 while (bot >= STATIC_BLOCK)
fe898f56
DC
143 {
144 b = BLOCKVECTOR_BLOCK (bl, bot);
145 if (BLOCK_END (b) > pc)
9703b513 146 return b;
fe898f56
DC
147 bot--;
148 }
9703b513
TT
149
150 return NULL;
151}
152
153/* Return the blockvector immediately containing the innermost lexical
154 block containing the specified pc value and section, or 0 if there
155 is none. PBLOCK is a pointer to the block. If PBLOCK is NULL, we
156 don't pass this information back to the caller. */
157
158struct blockvector *
159blockvector_for_pc_sect (CORE_ADDR pc, struct obj_section *section,
160 struct block **pblock, struct symtab *symtab)
161{
162 struct blockvector *bl;
163 struct block *b;
164
165 if (symtab == 0) /* if no symtab specified by caller */
166 {
167 /* First search all symtabs for one whose file contains our pc */
168 symtab = find_pc_sect_symtab (pc, section);
169 if (symtab == 0)
170 return 0;
171 }
172
173 bl = BLOCKVECTOR (symtab);
174
175 /* Then search that symtab for the smallest block that wins. */
176 b = find_block_in_blockvector (bl, pc);
177 if (b == NULL)
178 return NULL;
179
180 if (pblock)
181 *pblock = b;
182 return bl;
183}
184
185/* Return true if the blockvector BV contains PC, false otherwise. */
186
187int
188blockvector_contains_pc (struct blockvector *bv, CORE_ADDR pc)
189{
190 return find_block_in_blockvector (bv, pc) != NULL;
fe898f56
DC
191}
192
8e3b41a9
JK
193/* Return call_site for specified PC in GDBARCH. PC must match exactly, it
194 must be the next instruction after call (or after tail call jump). Throw
195 NO_ENTRY_VALUE_ERROR otherwise. This function never returns NULL. */
196
197struct call_site *
198call_site_for_pc (struct gdbarch *gdbarch, CORE_ADDR pc)
199{
200 struct symtab *symtab;
201 void **slot = NULL;
202
203 /* -1 as tail call PC can be already after the compilation unit range. */
204 symtab = find_pc_symtab (pc - 1);
205
206 if (symtab != NULL && symtab->call_site_htab != NULL)
207 slot = htab_find_slot (symtab->call_site_htab, &pc, NO_INSERT);
208
209 if (slot == NULL)
210 {
7cbd4a93 211 struct bound_minimal_symbol msym = lookup_minimal_symbol_by_pc (pc);
8e3b41a9
JK
212
213 /* DW_TAG_gnu_call_site will be missing just if GCC could not determine
214 the call target. */
215 throw_error (NO_ENTRY_VALUE_ERROR,
216 _("DW_OP_GNU_entry_value resolving cannot find "
217 "DW_TAG_GNU_call_site %s in %s"),
218 paddress (gdbarch, pc),
7cbd4a93
TT
219 (msym.minsym == NULL ? "???"
220 : SYMBOL_PRINT_NAME (msym.minsym)));
8e3b41a9
JK
221 }
222
223 return *slot;
224}
225
fe898f56
DC
226/* Return the blockvector immediately containing the innermost lexical block
227 containing the specified pc value, or 0 if there is none.
228 Backward compatibility, no section. */
229
230struct blockvector *
801e3a5b 231blockvector_for_pc (CORE_ADDR pc, struct block **pblock)
fe898f56
DC
232{
233 return blockvector_for_pc_sect (pc, find_pc_mapped_section (pc),
801e3a5b 234 pblock, NULL);
fe898f56
DC
235}
236
237/* Return the innermost lexical block containing the specified pc value
238 in the specified section, or 0 if there is none. */
239
240struct block *
714835d5 241block_for_pc_sect (CORE_ADDR pc, struct obj_section *section)
fe898f56 242{
b59661bd 243 struct blockvector *bl;
801e3a5b 244 struct block *b;
fe898f56 245
801e3a5b 246 bl = blockvector_for_pc_sect (pc, section, &b, NULL);
fe898f56 247 if (bl)
801e3a5b 248 return b;
fe898f56
DC
249 return 0;
250}
251
252/* Return the innermost lexical block containing the specified pc value,
253 or 0 if there is none. Backward compatibility, no section. */
254
255struct block *
b59661bd 256block_for_pc (CORE_ADDR pc)
fe898f56
DC
257{
258 return block_for_pc_sect (pc, find_pc_mapped_section (pc));
259}
9219021c 260
1fcb5155
DC
261/* Now come some functions designed to deal with C++ namespace issues.
262 The accessors are safe to use even in the non-C++ case. */
263
264/* This returns the namespace that BLOCK is enclosed in, or "" if it
265 isn't enclosed in a namespace at all. This travels the chain of
266 superblocks looking for a scope, if necessary. */
267
268const char *
269block_scope (const struct block *block)
270{
271 for (; block != NULL; block = BLOCK_SUPERBLOCK (block))
272 {
273 if (BLOCK_NAMESPACE (block) != NULL
274 && BLOCK_NAMESPACE (block)->scope != NULL)
275 return BLOCK_NAMESPACE (block)->scope;
276 }
277
278 return "";
279}
9219021c
DC
280
281/* Set BLOCK's scope member to SCOPE; if needed, allocate memory via
282 OBSTACK. (It won't make a copy of SCOPE, however, so that already
283 has to be allocated correctly.) */
284
285void
286block_set_scope (struct block *block, const char *scope,
287 struct obstack *obstack)
288{
289 block_initialize_namespace (block, obstack);
290
291 BLOCK_NAMESPACE (block)->scope = scope;
292}
293
27aa8d6a 294/* This returns the using directives list associated with BLOCK, if
1fcb5155
DC
295 any. */
296
1fcb5155
DC
297struct using_direct *
298block_using (const struct block *block)
299{
27aa8d6a 300 if (block == NULL || BLOCK_NAMESPACE (block) == NULL)
1fcb5155
DC
301 return NULL;
302 else
27aa8d6a 303 return BLOCK_NAMESPACE (block)->using;
1fcb5155
DC
304}
305
9219021c
DC
306/* Set BLOCK's using member to USING; if needed, allocate memory via
307 OBSTACK. (It won't make a copy of USING, however, so that already
308 has to be allocated correctly.) */
309
310void
311block_set_using (struct block *block,
312 struct using_direct *using,
313 struct obstack *obstack)
314{
315 block_initialize_namespace (block, obstack);
316
317 BLOCK_NAMESPACE (block)->using = using;
318}
319
320/* If BLOCK_NAMESPACE (block) is NULL, allocate it via OBSTACK and
321 ititialize its members to zero. */
322
323static void
324block_initialize_namespace (struct block *block, struct obstack *obstack)
325{
326 if (BLOCK_NAMESPACE (block) == NULL)
327 {
328 BLOCK_NAMESPACE (block)
329 = obstack_alloc (obstack, sizeof (struct block_namespace_info));
330 BLOCK_NAMESPACE (block)->scope = NULL;
331 BLOCK_NAMESPACE (block)->using = NULL;
332 }
333}
89a9d1b1
DC
334
335/* Return the static block associated to BLOCK. Return NULL if block
336 is NULL or if block is a global block. */
337
338const struct block *
339block_static_block (const struct block *block)
340{
341 if (block == NULL || BLOCK_SUPERBLOCK (block) == NULL)
342 return NULL;
343
344 while (BLOCK_SUPERBLOCK (BLOCK_SUPERBLOCK (block)) != NULL)
345 block = BLOCK_SUPERBLOCK (block);
346
347 return block;
348}
1fcb5155
DC
349
350/* Return the static block associated to BLOCK. Return NULL if block
351 is NULL. */
352
353const struct block *
354block_global_block (const struct block *block)
355{
356 if (block == NULL)
357 return NULL;
358
359 while (BLOCK_SUPERBLOCK (block) != NULL)
360 block = BLOCK_SUPERBLOCK (block);
361
362 return block;
363}
5c4e30ca
DC
364
365/* Allocate a block on OBSTACK, and initialize its elements to
366 zero/NULL. This is useful for creating "dummy" blocks that don't
367 correspond to actual source files.
368
369 Warning: it sets the block's BLOCK_DICT to NULL, which isn't a
370 valid value. If you really don't want the block to have a
371 dictionary, then you should subsequently set its BLOCK_DICT to
372 dict_create_linear (obstack, NULL). */
373
374struct block *
375allocate_block (struct obstack *obstack)
376{
377 struct block *bl = obstack_alloc (obstack, sizeof (struct block));
378
379 BLOCK_START (bl) = 0;
380 BLOCK_END (bl) = 0;
381 BLOCK_FUNCTION (bl) = NULL;
382 BLOCK_SUPERBLOCK (bl) = NULL;
383 BLOCK_DICT (bl) = NULL;
384 BLOCK_NAMESPACE (bl) = NULL;
5c4e30ca
DC
385
386 return bl;
387}
8157b174 388
84a146c9
TT
389/* Allocate a global block. */
390
391struct block *
392allocate_global_block (struct obstack *obstack)
393{
394 struct global_block *bl = OBSTACK_ZALLOC (obstack, struct global_block);
395
396 return &bl->block;
397}
398
399/* Set the symtab of the global block. */
400
401void
402set_block_symtab (struct block *block, struct symtab *symtab)
403{
404 struct global_block *gb;
405
406 gdb_assert (BLOCK_SUPERBLOCK (block) == NULL);
407 gb = (struct global_block *) block;
408 gdb_assert (gb->symtab == NULL);
409 gb->symtab = symtab;
410}
411
b5b04b5b
TT
412/* Return the symtab of the global block. */
413
414static struct symtab *
415get_block_symtab (const struct block *block)
416{
417 struct global_block *gb;
418
419 gdb_assert (BLOCK_SUPERBLOCK (block) == NULL);
420 gb = (struct global_block *) block;
421 gdb_assert (gb->symtab != NULL);
422 return gb->symtab;
423}
424
8157b174
TT
425\f
426
b5b04b5b
TT
427/* Initialize a block iterator, either to iterate over a single block,
428 or, for static and global blocks, all the included symtabs as
429 well. */
430
431static void
432initialize_block_iterator (const struct block *block,
433 struct block_iterator *iter)
434{
435 enum block_enum which;
436 struct symtab *symtab;
437
438 iter->idx = -1;
439
440 if (BLOCK_SUPERBLOCK (block) == NULL)
441 {
442 which = GLOBAL_BLOCK;
443 symtab = get_block_symtab (block);
444 }
445 else if (BLOCK_SUPERBLOCK (BLOCK_SUPERBLOCK (block)) == NULL)
446 {
447 which = STATIC_BLOCK;
448 symtab = get_block_symtab (BLOCK_SUPERBLOCK (block));
449 }
450 else
451 {
452 iter->d.block = block;
453 /* A signal value meaning that we're iterating over a single
454 block. */
455 iter->which = FIRST_LOCAL_BLOCK;
456 return;
457 }
458
459 /* If this is an included symtab, find the canonical includer and
460 use it instead. */
461 while (symtab->user != NULL)
462 symtab = symtab->user;
463
464 /* Putting this check here simplifies the logic of the iterator
465 functions. If there are no included symtabs, we only need to
466 search a single block, so we might as well just do that
467 directly. */
468 if (symtab->includes == NULL)
469 {
470 iter->d.block = block;
471 /* A signal value meaning that we're iterating over a single
472 block. */
473 iter->which = FIRST_LOCAL_BLOCK;
474 }
475 else
476 {
477 iter->d.symtab = symtab;
478 iter->which = which;
479 }
480}
481
482/* A helper function that finds the current symtab over whose static
483 or global block we should iterate. */
484
485static struct symtab *
486find_iterator_symtab (struct block_iterator *iterator)
487{
488 if (iterator->idx == -1)
489 return iterator->d.symtab;
490 return iterator->d.symtab->includes[iterator->idx];
491}
492
493/* Perform a single step for a plain block iterator, iterating across
494 symbol tables as needed. Returns the next symbol, or NULL when
495 iteration is complete. */
496
497static struct symbol *
498block_iterator_step (struct block_iterator *iterator, int first)
499{
500 struct symbol *sym;
501
502 gdb_assert (iterator->which != FIRST_LOCAL_BLOCK);
503
504 while (1)
505 {
506 if (first)
507 {
508 struct symtab *symtab = find_iterator_symtab (iterator);
509 const struct block *block;
510
511 /* Iteration is complete. */
512 if (symtab == NULL)
513 return NULL;
514
515 block = BLOCKVECTOR_BLOCK (BLOCKVECTOR (symtab), iterator->which);
516 sym = dict_iterator_first (BLOCK_DICT (block), &iterator->dict_iter);
517 }
518 else
519 sym = dict_iterator_next (&iterator->dict_iter);
520
521 if (sym != NULL)
522 return sym;
523
524 /* We have finished iterating the appropriate block of one
525 symtab. Now advance to the next symtab and begin iteration
526 there. */
527 ++iterator->idx;
528 first = 1;
529 }
530}
531
8157b174
TT
532/* See block.h. */
533
534struct symbol *
535block_iterator_first (const struct block *block,
536 struct block_iterator *iterator)
537{
b5b04b5b
TT
538 initialize_block_iterator (block, iterator);
539
540 if (iterator->which == FIRST_LOCAL_BLOCK)
541 return dict_iterator_first (block->dict, &iterator->dict_iter);
542
543 return block_iterator_step (iterator, 1);
8157b174
TT
544}
545
546/* See block.h. */
547
548struct symbol *
549block_iterator_next (struct block_iterator *iterator)
550{
b5b04b5b
TT
551 if (iterator->which == FIRST_LOCAL_BLOCK)
552 return dict_iterator_next (&iterator->dict_iter);
553
554 return block_iterator_step (iterator, 0);
555}
556
557/* Perform a single step for a "name" block iterator, iterating across
558 symbol tables as needed. Returns the next symbol, or NULL when
559 iteration is complete. */
560
561static struct symbol *
562block_iter_name_step (struct block_iterator *iterator, const char *name,
563 int first)
564{
565 struct symbol *sym;
566
567 gdb_assert (iterator->which != FIRST_LOCAL_BLOCK);
568
569 while (1)
570 {
571 if (first)
572 {
573 struct symtab *symtab = find_iterator_symtab (iterator);
574 const struct block *block;
575
576 /* Iteration is complete. */
577 if (symtab == NULL)
578 return NULL;
579
580 block = BLOCKVECTOR_BLOCK (BLOCKVECTOR (symtab), iterator->which);
581 sym = dict_iter_name_first (BLOCK_DICT (block), name,
582 &iterator->dict_iter);
583 }
584 else
585 sym = dict_iter_name_next (name, &iterator->dict_iter);
586
587 if (sym != NULL)
588 return sym;
589
590 /* We have finished iterating the appropriate block of one
591 symtab. Now advance to the next symtab and begin iteration
592 there. */
593 ++iterator->idx;
594 first = 1;
595 }
8157b174
TT
596}
597
598/* See block.h. */
599
600struct symbol *
601block_iter_name_first (const struct block *block,
602 const char *name,
603 struct block_iterator *iterator)
604{
b5b04b5b
TT
605 initialize_block_iterator (block, iterator);
606
607 if (iterator->which == FIRST_LOCAL_BLOCK)
608 return dict_iter_name_first (block->dict, name, &iterator->dict_iter);
609
610 return block_iter_name_step (iterator, name, 1);
8157b174
TT
611}
612
613/* See block.h. */
614
615struct symbol *
616block_iter_name_next (const char *name, struct block_iterator *iterator)
617{
b5b04b5b
TT
618 if (iterator->which == FIRST_LOCAL_BLOCK)
619 return dict_iter_name_next (name, &iterator->dict_iter);
620
621 return block_iter_name_step (iterator, name, 0);
622}
623
624/* Perform a single step for a "match" block iterator, iterating
625 across symbol tables as needed. Returns the next symbol, or NULL
626 when iteration is complete. */
627
628static struct symbol *
629block_iter_match_step (struct block_iterator *iterator,
630 const char *name,
631 symbol_compare_ftype *compare,
632 int first)
633{
634 struct symbol *sym;
635
636 gdb_assert (iterator->which != FIRST_LOCAL_BLOCK);
637
638 while (1)
639 {
640 if (first)
641 {
642 struct symtab *symtab = find_iterator_symtab (iterator);
643 const struct block *block;
644
645 /* Iteration is complete. */
646 if (symtab == NULL)
647 return NULL;
648
649 block = BLOCKVECTOR_BLOCK (BLOCKVECTOR (symtab), iterator->which);
650 sym = dict_iter_match_first (BLOCK_DICT (block), name,
651 compare, &iterator->dict_iter);
652 }
653 else
654 sym = dict_iter_match_next (name, compare, &iterator->dict_iter);
655
656 if (sym != NULL)
657 return sym;
658
659 /* We have finished iterating the appropriate block of one
660 symtab. Now advance to the next symtab and begin iteration
661 there. */
662 ++iterator->idx;
663 first = 1;
664 }
8157b174
TT
665}
666
667/* See block.h. */
668
669struct symbol *
670block_iter_match_first (const struct block *block,
671 const char *name,
672 symbol_compare_ftype *compare,
673 struct block_iterator *iterator)
674{
b5b04b5b
TT
675 initialize_block_iterator (block, iterator);
676
677 if (iterator->which == FIRST_LOCAL_BLOCK)
678 return dict_iter_match_first (block->dict, name, compare,
679 &iterator->dict_iter);
680
681 return block_iter_match_step (iterator, name, compare, 1);
8157b174
TT
682}
683
684/* See block.h. */
685
686struct symbol *
687block_iter_match_next (const char *name,
688 symbol_compare_ftype *compare,
689 struct block_iterator *iterator)
690{
b5b04b5b
TT
691 if (iterator->which == FIRST_LOCAL_BLOCK)
692 return dict_iter_match_next (name, compare, &iterator->dict_iter);
693
694 return block_iter_match_step (iterator, name, compare, 0);
8157b174 695}
This page took 0.717405 seconds and 4 git commands to generate.