Fix crash with DW_FORM_implicit_const
[binutils-gdb.git] / gdb / block.c
1 /* Block-related functions for the GNU debugger, GDB.
2
3 Copyright (C) 2003-2023 Free Software Foundation, Inc.
4
5 This file is part of GDB.
6
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 3 of the License, or
10 (at your option) any later version.
11
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
16
17 You should have received a copy of the GNU General Public License
18 along with this program. If not, see <http://www.gnu.org/licenses/>. */
19
20 #include "defs.h"
21 #include "block.h"
22 #include "symtab.h"
23 #include "symfile.h"
24 #include "gdbsupport/gdb_obstack.h"
25 #include "cp-support.h"
26 #include "addrmap.h"
27 #include "gdbtypes.h"
28 #include "objfiles.h"
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
34 struct block_namespace_info : public allocate_on_obstack
35 {
36 const char *scope = nullptr;
37 struct using_direct *using_decl = nullptr;
38 };
39
40 /* See block.h. */
41
42 struct objfile *
43 block::objfile () const
44 {
45 const struct global_block *global_block;
46
47 if (function () != nullptr)
48 return function ()->objfile ();
49
50 global_block = (struct global_block *) this->global_block ();
51 return global_block->compunit_symtab->objfile ();
52 }
53
54 /* See block. */
55
56 struct gdbarch *
57 block::gdbarch () const
58 {
59 if (function () != nullptr)
60 return function ()->arch ();
61
62 return objfile ()->arch ();
63 }
64
65 /* See block.h. */
66
67 bool
68 block::contains (const struct block *a, bool allow_nested) const
69 {
70 if (a == nullptr)
71 return false;
72
73 do
74 {
75 if (a == this)
76 return true;
77 /* If A is a function block, then A cannot be contained in B,
78 except if A was inlined. */
79 if (!allow_nested && a->function () != NULL && !a->inlined_p ())
80 return false;
81 a = a->superblock ();
82 }
83 while (a != NULL);
84
85 return false;
86 }
87
88 /* See block.h. */
89
90 struct symbol *
91 block::linkage_function () const
92 {
93 const block *bl = this;
94
95 while ((bl->function () == NULL || bl->inlined_p ())
96 && bl->superblock () != NULL)
97 bl = bl->superblock ();
98
99 return bl->function ();
100 }
101
102 /* See block.h. */
103
104 struct symbol *
105 block::containing_function () const
106 {
107 const block *bl = this;
108
109 while (bl->function () == NULL && bl->superblock () != NULL)
110 bl = bl->superblock ();
111
112 return bl->function ();
113 }
114
115 /* See block.h. */
116
117 bool
118 block::inlined_p () const
119 {
120 return function () != nullptr && function ()->is_inlined ();
121 }
122
123 /* A helper function that checks whether PC is in the blockvector BL.
124 It returns the containing block if there is one, or else NULL. */
125
126 static const struct block *
127 find_block_in_blockvector (const struct blockvector *bl, CORE_ADDR pc)
128 {
129 const struct block *b;
130 int bot, top, half;
131
132 /* If we have an addrmap mapping code addresses to blocks, then use
133 that. */
134 if (bl->map ())
135 return (const struct block *) bl->map ()->find (pc);
136
137 /* Otherwise, use binary search to find the last block that starts
138 before PC.
139 Note: GLOBAL_BLOCK is block 0, STATIC_BLOCK is block 1.
140 They both have the same START,END values.
141 Historically this code would choose STATIC_BLOCK over GLOBAL_BLOCK but the
142 fact that this choice was made was subtle, now we make it explicit. */
143 gdb_assert (bl->blocks ().size () >= 2);
144 bot = STATIC_BLOCK;
145 top = bl->blocks ().size ();
146
147 while (top - bot > 1)
148 {
149 half = (top - bot + 1) >> 1;
150 b = bl->block (bot + half);
151 if (b->start () <= pc)
152 bot += half;
153 else
154 top = bot + half;
155 }
156
157 /* Now search backward for a block that ends after PC. */
158
159 while (bot >= STATIC_BLOCK)
160 {
161 b = bl->block (bot);
162 if (!(b->start () <= pc))
163 return NULL;
164 if (b->end () > pc)
165 return b;
166 bot--;
167 }
168
169 return NULL;
170 }
171
172 /* Return the blockvector immediately containing the innermost lexical
173 block containing the specified pc value and section, or 0 if there
174 is none. PBLOCK is a pointer to the block. If PBLOCK is NULL, we
175 don't pass this information back to the caller. */
176
177 const struct blockvector *
178 blockvector_for_pc_sect (CORE_ADDR pc, struct obj_section *section,
179 const struct block **pblock,
180 struct compunit_symtab *cust)
181 {
182 const struct blockvector *bl;
183 const struct block *b;
184
185 if (cust == NULL)
186 {
187 /* First search all symtabs for one whose file contains our pc */
188 cust = find_pc_sect_compunit_symtab (pc, section);
189 if (cust == NULL)
190 return 0;
191 }
192
193 bl = cust->blockvector ();
194
195 /* Then search that symtab for the smallest block that wins. */
196 b = find_block_in_blockvector (bl, pc);
197 if (b == NULL)
198 return NULL;
199
200 if (pblock)
201 *pblock = b;
202 return bl;
203 }
204
205 /* Return true if the blockvector BV contains PC, false otherwise. */
206
207 int
208 blockvector_contains_pc (const struct blockvector *bv, CORE_ADDR pc)
209 {
210 return find_block_in_blockvector (bv, pc) != NULL;
211 }
212
213 /* Return call_site for specified PC in GDBARCH. PC must match exactly, it
214 must be the next instruction after call (or after tail call jump). Throw
215 NO_ENTRY_VALUE_ERROR otherwise. This function never returns NULL. */
216
217 struct call_site *
218 call_site_for_pc (struct gdbarch *gdbarch, CORE_ADDR pc)
219 {
220 struct compunit_symtab *cust;
221 call_site *cs = nullptr;
222
223 /* -1 as tail call PC can be already after the compilation unit range. */
224 cust = find_pc_compunit_symtab (pc - 1);
225
226 if (cust != nullptr)
227 cs = cust->find_call_site (pc);
228
229 if (cs == nullptr)
230 {
231 struct bound_minimal_symbol msym = lookup_minimal_symbol_by_pc (pc);
232
233 /* DW_TAG_gnu_call_site will be missing just if GCC could not determine
234 the call target. */
235 throw_error (NO_ENTRY_VALUE_ERROR,
236 _("DW_OP_entry_value resolving cannot find "
237 "DW_TAG_call_site %s in %s"),
238 paddress (gdbarch, pc),
239 (msym.minsym == NULL ? "???"
240 : msym.minsym->print_name ()));
241 }
242
243 return cs;
244 }
245
246 /* Return the blockvector immediately containing the innermost lexical block
247 containing the specified pc value, or 0 if there is none.
248 Backward compatibility, no section. */
249
250 const struct blockvector *
251 blockvector_for_pc (CORE_ADDR pc, const struct block **pblock)
252 {
253 return blockvector_for_pc_sect (pc, find_pc_mapped_section (pc),
254 pblock, NULL);
255 }
256
257 /* Return the innermost lexical block containing the specified pc value
258 in the specified section, or 0 if there is none. */
259
260 const struct block *
261 block_for_pc_sect (CORE_ADDR pc, struct obj_section *section)
262 {
263 const struct blockvector *bl;
264 const struct block *b;
265
266 bl = blockvector_for_pc_sect (pc, section, &b, NULL);
267 if (bl)
268 return b;
269 return 0;
270 }
271
272 /* Return the innermost lexical block containing the specified pc value,
273 or 0 if there is none. Backward compatibility, no section. */
274
275 const struct block *
276 block_for_pc (CORE_ADDR pc)
277 {
278 return block_for_pc_sect (pc, find_pc_mapped_section (pc));
279 }
280
281 /* Now come some functions designed to deal with C++ namespace issues.
282 The accessors are safe to use even in the non-C++ case. */
283
284 /* See block.h. */
285
286 const char *
287 block::scope () const
288 {
289 for (const block *block = this;
290 block != nullptr;
291 block = block->superblock ())
292 {
293 if (block->m_namespace_info != nullptr
294 && block->m_namespace_info->scope != nullptr)
295 return block->m_namespace_info->scope;
296 }
297
298 return "";
299 }
300
301 /* See block.h. */
302
303 void
304 block::initialize_namespace (struct obstack *obstack)
305 {
306 if (m_namespace_info == nullptr)
307 m_namespace_info = new (obstack) struct block_namespace_info;
308 }
309
310 /* See block.h. */
311
312 void
313 block::set_scope (const char *scope, struct obstack *obstack)
314 {
315 if (scope == nullptr || scope[0] == '\0')
316 {
317 /* Don't bother. */
318 return;
319 }
320
321 initialize_namespace (obstack);
322 m_namespace_info->scope = scope;
323 }
324
325 /* See block.h. */
326
327 struct using_direct *
328 block::get_using () const
329 {
330 if (m_namespace_info == nullptr)
331 return nullptr;
332 else
333 return m_namespace_info->using_decl;
334 }
335
336 /* See block.h. */
337
338 void
339 block::set_using (struct using_direct *using_decl, struct obstack *obstack)
340 {
341 if (using_decl == nullptr)
342 {
343 /* Don't bother. */
344 return;
345 }
346
347 initialize_namespace (obstack);
348 m_namespace_info->using_decl = using_decl;
349 }
350
351 /* See block.h. */
352
353 const struct block *
354 block::static_block () const
355 {
356 if (superblock () == nullptr)
357 return nullptr;
358
359 const block *block = this;
360 while (block->superblock ()->superblock () != NULL)
361 block = block->superblock ();
362
363 return block;
364 }
365
366 /* See block.h. */
367
368 const struct block *
369 block::global_block () const
370 {
371 const block *block = this;
372
373 while (block->superblock () != NULL)
374 block = block->superblock ();
375
376 return block;
377 }
378
379 /* See block.h. */
380
381 void
382 block::set_compunit_symtab (struct compunit_symtab *cu)
383 {
384 struct global_block *gb;
385
386 gdb_assert (superblock () == NULL);
387 gb = (struct global_block *) this;
388 gdb_assert (gb->compunit_symtab == NULL);
389 gb->compunit_symtab = cu;
390 }
391
392 /* See block.h. */
393
394 struct dynamic_prop *
395 block::static_link () const
396 {
397 struct objfile *objfile = this->objfile ();
398
399 /* Only objfile-owned blocks that materialize top function scopes can have
400 static links. */
401 if (objfile == NULL || function () == NULL)
402 return NULL;
403
404 return (struct dynamic_prop *) objfile_lookup_static_link (objfile, this);
405 }
406
407 /* Return the compunit of the global block. */
408
409 static struct compunit_symtab *
410 get_block_compunit_symtab (const struct block *block)
411 {
412 struct global_block *gb;
413
414 gdb_assert (block->superblock () == NULL);
415 gb = (struct global_block *) block;
416 gdb_assert (gb->compunit_symtab != NULL);
417 return gb->compunit_symtab;
418 }
419
420 \f
421
422 /* Initialize a block iterator, either to iterate over a single block,
423 or, for static and global blocks, all the included symtabs as
424 well. */
425
426 static void
427 initialize_block_iterator (const struct block *block,
428 struct block_iterator *iter,
429 const lookup_name_info *name = nullptr)
430 {
431 enum block_enum which;
432 struct compunit_symtab *cu;
433
434 iter->idx = -1;
435 iter->name = name;
436
437 if (block->superblock () == NULL)
438 {
439 which = GLOBAL_BLOCK;
440 cu = get_block_compunit_symtab (block);
441 }
442 else if (block->superblock ()->superblock () == NULL)
443 {
444 which = STATIC_BLOCK;
445 cu = get_block_compunit_symtab (block->superblock ());
446 }
447 else
448 {
449 iter->d.block = block;
450 /* A signal value meaning that we're iterating over a single
451 block. */
452 iter->which = FIRST_LOCAL_BLOCK;
453 return;
454 }
455
456 /* If this is an included symtab, find the canonical includer and
457 use it instead. */
458 while (cu->user != NULL)
459 cu = cu->user;
460
461 /* Putting this check here simplifies the logic of the iterator
462 functions. If there are no included symtabs, we only need to
463 search a single block, so we might as well just do that
464 directly. */
465 if (cu->includes == NULL)
466 {
467 iter->d.block = block;
468 /* A signal value meaning that we're iterating over a single
469 block. */
470 iter->which = FIRST_LOCAL_BLOCK;
471 }
472 else
473 {
474 iter->d.compunit_symtab = cu;
475 iter->which = which;
476 }
477 }
478
479 /* A helper function that finds the current compunit over whose static
480 or global block we should iterate. */
481
482 static struct compunit_symtab *
483 find_iterator_compunit_symtab (struct block_iterator *iterator)
484 {
485 if (iterator->idx == -1)
486 return iterator->d.compunit_symtab;
487 return iterator->d.compunit_symtab->includes[iterator->idx];
488 }
489
490 /* Perform a single step for a plain block iterator, iterating across
491 symbol tables as needed. Returns the next symbol, or NULL when
492 iteration is complete. */
493
494 static struct symbol *
495 block_iterator_step (struct block_iterator *iterator, int first)
496 {
497 struct symbol *sym;
498
499 gdb_assert (iterator->which != FIRST_LOCAL_BLOCK);
500
501 while (1)
502 {
503 if (first)
504 {
505 struct compunit_symtab *cust
506 = find_iterator_compunit_symtab (iterator);
507 const struct block *block;
508
509 /* Iteration is complete. */
510 if (cust == NULL)
511 return NULL;
512
513 block = cust->blockvector ()->block (iterator->which);
514 sym = mdict_iterator_first (block->multidict (),
515 &iterator->mdict_iter);
516 }
517 else
518 sym = mdict_iterator_next (&iterator->mdict_iter);
519
520 if (sym != NULL)
521 return sym;
522
523 /* We have finished iterating the appropriate block of one
524 symtab. Now advance to the next symtab and begin iteration
525 there. */
526 ++iterator->idx;
527 first = 1;
528 }
529 }
530
531 /* Perform a single step for a "match" block iterator, iterating
532 across symbol tables as needed. Returns the next symbol, or NULL
533 when iteration is complete. */
534
535 static struct symbol *
536 block_iter_match_step (struct block_iterator *iterator,
537 int first)
538 {
539 struct symbol *sym;
540
541 gdb_assert (iterator->which != FIRST_LOCAL_BLOCK);
542
543 while (1)
544 {
545 if (first)
546 {
547 struct compunit_symtab *cust
548 = find_iterator_compunit_symtab (iterator);
549 const struct block *block;
550
551 /* Iteration is complete. */
552 if (cust == NULL)
553 return NULL;
554
555 block = cust->blockvector ()->block (iterator->which);
556 sym = mdict_iter_match_first (block->multidict (), *iterator->name,
557 &iterator->mdict_iter);
558 }
559 else
560 sym = mdict_iter_match_next (*iterator->name, &iterator->mdict_iter);
561
562 if (sym != NULL)
563 return sym;
564
565 /* We have finished iterating the appropriate block of one
566 symtab. Now advance to the next symtab and begin iteration
567 there. */
568 ++iterator->idx;
569 first = 1;
570 }
571 }
572
573 /* See block.h. */
574
575 struct symbol *
576 block_iterator_first (const struct block *block,
577 struct block_iterator *iterator,
578 const lookup_name_info *name)
579 {
580 initialize_block_iterator (block, iterator, name);
581
582 if (name == nullptr)
583 {
584 if (iterator->which == FIRST_LOCAL_BLOCK)
585 return mdict_iterator_first (block->multidict (),
586 &iterator->mdict_iter);
587
588 return block_iterator_step (iterator, 1);
589 }
590
591 if (iterator->which == FIRST_LOCAL_BLOCK)
592 return mdict_iter_match_first (block->multidict (), *name,
593 &iterator->mdict_iter);
594
595 return block_iter_match_step (iterator, 1);
596 }
597
598 /* See block.h. */
599
600 struct symbol *
601 block_iterator_next (struct block_iterator *iterator)
602 {
603 if (iterator->name == nullptr)
604 {
605 if (iterator->which == FIRST_LOCAL_BLOCK)
606 return mdict_iterator_next (&iterator->mdict_iter);
607
608 return block_iterator_step (iterator, 0);
609 }
610
611 if (iterator->which == FIRST_LOCAL_BLOCK)
612 return mdict_iter_match_next (*iterator->name, &iterator->mdict_iter);
613
614 return block_iter_match_step (iterator, 0);
615 }
616
617 /* See block.h. */
618
619 bool
620 best_symbol (struct symbol *a, const domain_enum domain)
621 {
622 return (a->domain () == domain
623 && a->aclass () != LOC_UNRESOLVED);
624 }
625
626 /* See block.h. */
627
628 struct symbol *
629 better_symbol (struct symbol *a, struct symbol *b, const domain_enum domain)
630 {
631 if (a == NULL)
632 return b;
633 if (b == NULL)
634 return a;
635
636 if (a->domain () == domain && b->domain () != domain)
637 return a;
638
639 if (b->domain () == domain && a->domain () != domain)
640 return b;
641
642 if (a->aclass () != LOC_UNRESOLVED && b->aclass () == LOC_UNRESOLVED)
643 return a;
644
645 if (b->aclass () != LOC_UNRESOLVED && a->aclass () == LOC_UNRESOLVED)
646 return b;
647
648 return a;
649 }
650
651 /* See block.h.
652
653 Note that if NAME is the demangled form of a C++ symbol, we will fail
654 to find a match during the binary search of the non-encoded names, but
655 for now we don't worry about the slight inefficiency of looking for
656 a match we'll never find, since it will go pretty quick. Once the
657 binary search terminates, we drop through and do a straight linear
658 search on the symbols. Each symbol which is marked as being a ObjC/C++
659 symbol (language_cplus or language_objc set) has both the encoded and
660 non-encoded names tested for a match. */
661
662 struct symbol *
663 block_lookup_symbol (const struct block *block, const char *name,
664 symbol_name_match_type match_type,
665 const domain_enum domain)
666 {
667 lookup_name_info lookup_name (name, match_type);
668
669 if (!block->function ())
670 {
671 struct symbol *other = NULL;
672
673 for (struct symbol *sym : block_iterator_range (block, &lookup_name))
674 {
675 /* See comment related to PR gcc/debug/91507 in
676 block_lookup_symbol_primary. */
677 if (best_symbol (sym, domain))
678 return sym;
679 /* This is a bit of a hack, but symbol_matches_domain might ignore
680 STRUCT vs VAR domain symbols. So if a matching symbol is found,
681 make sure there is no "better" matching symbol, i.e., one with
682 exactly the same domain. PR 16253. */
683 if (symbol_matches_domain (sym->language (),
684 sym->domain (), domain))
685 other = better_symbol (other, sym, domain);
686 }
687 return other;
688 }
689 else
690 {
691 /* Note that parameter symbols do not always show up last in the
692 list; this loop makes sure to take anything else other than
693 parameter symbols first; it only uses parameter symbols as a
694 last resort. Note that this only takes up extra computation
695 time on a match.
696 It's hard to define types in the parameter list (at least in
697 C/C++) so we don't do the same PR 16253 hack here that is done
698 for the !BLOCK_FUNCTION case. */
699
700 struct symbol *sym_found = NULL;
701
702 for (struct symbol *sym : block_iterator_range (block, &lookup_name))
703 {
704 if (symbol_matches_domain (sym->language (),
705 sym->domain (), domain))
706 {
707 sym_found = sym;
708 if (!sym->is_argument ())
709 {
710 break;
711 }
712 }
713 }
714 return (sym_found); /* Will be NULL if not found. */
715 }
716 }
717
718 /* See block.h. */
719
720 struct symbol *
721 block_lookup_symbol_primary (const struct block *block, const char *name,
722 const domain_enum domain)
723 {
724 struct symbol *sym, *other;
725 struct mdict_iterator mdict_iter;
726
727 lookup_name_info lookup_name (name, symbol_name_match_type::FULL);
728
729 /* Verify BLOCK is STATIC_BLOCK or GLOBAL_BLOCK. */
730 gdb_assert (block->superblock () == NULL
731 || block->superblock ()->superblock () == NULL);
732
733 other = NULL;
734 for (sym = mdict_iter_match_first (block->multidict (), lookup_name,
735 &mdict_iter);
736 sym != NULL;
737 sym = mdict_iter_match_next (lookup_name, &mdict_iter))
738 {
739 /* With the fix for PR gcc/debug/91507, we get for:
740 ...
741 extern char *zzz[];
742 char *zzz[ ] = {
743 "abc",
744 "cde"
745 };
746 ...
747 DWARF which will result in two entries in the symbol table, a decl
748 with type char *[] and a def with type char *[2].
749
750 If we return the decl here, we don't get the value of zzz:
751 ...
752 $ gdb a.spec.out -batch -ex "p zzz"
753 $1 = 0x601030 <zzz>
754 ...
755 because we're returning the symbol without location information, and
756 because the fallback that uses the address from the minimal symbols
757 doesn't work either because the type of the decl does not specify a
758 size.
759
760 To fix this, we prefer def over decl in best_symbol and
761 better_symbol.
762
763 In absence of the gcc fix, both def and decl have type char *[], so
764 the only option to make this work is improve the fallback to use the
765 size of the minimal symbol. Filed as PR exp/24989. */
766 if (best_symbol (sym, domain))
767 return sym;
768
769 /* This is a bit of a hack, but symbol_matches_domain might ignore
770 STRUCT vs VAR domain symbols. So if a matching symbol is found,
771 make sure there is no "better" matching symbol, i.e., one with
772 exactly the same domain. PR 16253. */
773 if (symbol_matches_domain (sym->language (), sym->domain (), domain))
774 other = better_symbol (other, sym, domain);
775 }
776
777 return other;
778 }
779
780 /* See block.h. */
781
782 struct symbol *
783 block_find_symbol (const struct block *block, const char *name,
784 const domain_enum domain,
785 block_symbol_matcher_ftype *matcher, void *data)
786 {
787 lookup_name_info lookup_name (name, symbol_name_match_type::FULL);
788
789 /* Verify BLOCK is STATIC_BLOCK or GLOBAL_BLOCK. */
790 gdb_assert (block->superblock () == NULL
791 || block->superblock ()->superblock () == NULL);
792
793 for (struct symbol *sym : block_iterator_range (block, &lookup_name))
794 {
795 /* MATCHER is deliberately called second here so that it never sees
796 a non-domain-matching symbol. */
797 if (symbol_matches_domain (sym->language (), sym->domain (), domain)
798 && matcher (sym, data))
799 return sym;
800 }
801 return NULL;
802 }
803
804 /* See block.h. */
805
806 int
807 block_find_non_opaque_type (struct symbol *sym, void *data)
808 {
809 return !TYPE_IS_OPAQUE (sym->type ());
810 }
811
812 /* See block.h. */
813
814 int
815 block_find_non_opaque_type_preferred (struct symbol *sym, void *data)
816 {
817 struct symbol **best = (struct symbol **) data;
818
819 if (!TYPE_IS_OPAQUE (sym->type ()))
820 return 1;
821 *best = sym;
822 return 0;
823 }
824
825 /* See block.h. */
826
827 struct blockranges *
828 make_blockranges (struct objfile *objfile,
829 const std::vector<blockrange> &rangevec)
830 {
831 struct blockranges *blr;
832 size_t n = rangevec.size();
833
834 blr = (struct blockranges *)
835 obstack_alloc (&objfile->objfile_obstack,
836 sizeof (struct blockranges)
837 + (n - 1) * sizeof (struct blockrange));
838
839 blr->nranges = n;
840 for (int i = 0; i < n; i++)
841 blr->range[i] = rangevec[i];
842 return blr;
843 }
844