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