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