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