]>
Commit | Line | Data |
---|---|---|
fe898f56 DC |
1 | /* Code dealing with blocks for GDB. |
2 | ||
4a94e368 | 3 | Copyright (C) 2003-2022 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 | #ifndef BLOCK_H | |
21 | #define BLOCK_H | |
22 | ||
8157b174 TT |
23 | #include "dictionary.h" |
24 | ||
fe898f56 DC |
25 | /* Opaque declarations. */ |
26 | ||
27 | struct symbol; | |
43f3e411 | 28 | struct compunit_symtab; |
9219021c DC |
29 | struct block_namespace_info; |
30 | struct using_direct; | |
31 | struct obstack; | |
801e3a5b | 32 | struct addrmap; |
fe898f56 | 33 | |
26457a9c KB |
34 | /* Blocks can occupy non-contiguous address ranges. When this occurs, |
35 | startaddr and endaddr within struct block (still) specify the lowest | |
36 | and highest addresses of all ranges, but each individual range is | |
37 | specified by the addresses in struct blockrange. */ | |
38 | ||
39 | struct blockrange | |
40 | { | |
41 | blockrange (CORE_ADDR startaddr_, CORE_ADDR endaddr_) | |
42 | : startaddr (startaddr_), | |
43 | endaddr (endaddr_) | |
44 | { | |
45 | } | |
46 | ||
47 | /* Lowest address in this range. */ | |
48 | ||
49 | CORE_ADDR startaddr; | |
50 | ||
51 | /* One past the highest address in the range. */ | |
52 | ||
53 | CORE_ADDR endaddr; | |
54 | }; | |
55 | ||
56 | /* Two or more non-contiguous ranges in the same order as that provided | |
57 | via the debug info. */ | |
58 | ||
59 | struct blockranges | |
60 | { | |
61 | int nranges; | |
62 | struct blockrange range[1]; | |
63 | }; | |
64 | ||
fe898f56 DC |
65 | /* All of the name-scope contours of the program |
66 | are represented by `struct block' objects. | |
67 | All of these objects are pointed to by the blockvector. | |
68 | ||
69 | Each block represents one name scope. | |
70 | Each lexical context has its own block. | |
71 | ||
72 | The blockvector begins with some special blocks. | |
73 | The GLOBAL_BLOCK contains all the symbols defined in this compilation | |
74 | whose scope is the entire program linked together. | |
75 | The STATIC_BLOCK contains all the symbols whose scope is the | |
76 | entire compilation excluding other separate compilations. | |
77 | Blocks starting with the FIRST_LOCAL_BLOCK are not special. | |
78 | ||
79 | Each block records a range of core addresses for the code that | |
80 | is in the scope of the block. The STATIC_BLOCK and GLOBAL_BLOCK | |
81 | give, for the range of code, the entire range of code produced | |
82 | by the compilation that the symbol segment belongs to. | |
83 | ||
84 | The blocks appear in the blockvector | |
85 | in order of increasing starting-address, | |
86 | and, within that, in order of decreasing ending-address. | |
87 | ||
88 | This implies that within the body of one function | |
89 | the blocks appear in the order of a depth-first tree walk. */ | |
90 | ||
91 | struct block | |
92 | { | |
4b8791e1 SM |
93 | /* Return this block's start address. */ |
94 | CORE_ADDR start () const | |
95 | { return m_start; } | |
96 | ||
97 | /* Set this block's start address. */ | |
98 | void set_start (CORE_ADDR start) | |
99 | { m_start = start; } | |
100 | ||
101 | /* Return this block's end address. */ | |
102 | CORE_ADDR end () const | |
103 | { return m_end; } | |
104 | ||
105 | /* Set this block's end address. */ | |
106 | void set_end (CORE_ADDR end) | |
107 | { m_end = end; } | |
fe898f56 | 108 | |
6c00f721 SM |
109 | /* Return this block's function symbol. */ |
110 | symbol *function () const | |
111 | { return m_function; } | |
112 | ||
113 | /* Set this block's function symbol. */ | |
114 | void set_function (symbol *function) | |
115 | { m_function = function; } | |
116 | ||
f135fe72 SM |
117 | /* Return this block's superblock. */ |
118 | const block *superblock () const | |
119 | { return m_superblock; } | |
120 | ||
121 | /* Set this block's superblock. */ | |
122 | void set_superblock (const block *superblock) | |
123 | { m_superblock = superblock; } | |
124 | ||
fe898f56 DC |
125 | /* Addresses in the executable code that are in this block. */ |
126 | ||
4b8791e1 SM |
127 | CORE_ADDR m_start; |
128 | CORE_ADDR m_end; | |
fe898f56 DC |
129 | |
130 | /* The symbol that names this block, if the block is the body of a | |
edb3359d | 131 | function (real or inlined); otherwise, zero. */ |
fe898f56 | 132 | |
6c00f721 | 133 | struct symbol *m_function; |
fe898f56 DC |
134 | |
135 | /* The `struct block' for the containing block, or 0 if none. | |
136 | ||
137 | The superblock of a top-level local block (i.e. a function in the | |
138 | case of C) is the STATIC_BLOCK. The superblock of the | |
139 | STATIC_BLOCK is the GLOBAL_BLOCK. */ | |
140 | ||
f135fe72 | 141 | const struct block *m_superblock; |
fe898f56 | 142 | |
de4f826b DC |
143 | /* This is used to store the symbols in the block. */ |
144 | ||
b026f593 | 145 | struct multidictionary *multidict; |
de4f826b | 146 | |
22cee43f PMR |
147 | /* Contains information about namespace-related info relevant to this block: |
148 | using directives and the current namespace scope. */ | |
149 | ||
150 | struct block_namespace_info *namespace_info; | |
26457a9c KB |
151 | |
152 | /* Address ranges for blocks with non-contiguous ranges. If this | |
153 | is NULL, then there is only one range which is specified by | |
154 | startaddr and endaddr above. */ | |
155 | ||
156 | struct blockranges *ranges; | |
fe898f56 DC |
157 | }; |
158 | ||
84a146c9 | 159 | /* The global block is singled out so that we can provide a back-link |
43f3e411 | 160 | to the compunit symtab. */ |
84a146c9 TT |
161 | |
162 | struct global_block | |
163 | { | |
164 | /* The block. */ | |
165 | ||
166 | struct block block; | |
167 | ||
43f3e411 | 168 | /* This holds a pointer to the compunit symtab holding this block. */ |
84a146c9 | 169 | |
43f3e411 | 170 | struct compunit_symtab *compunit_symtab; |
84a146c9 TT |
171 | }; |
172 | ||
b026f593 | 173 | #define BLOCK_MULTIDICT(bl) (bl)->multidict |
22cee43f | 174 | #define BLOCK_NAMESPACE(bl) (bl)->namespace_info |
fe898f56 | 175 | |
26457a9c KB |
176 | /* Accessor for ranges field within block BL. */ |
177 | ||
178 | #define BLOCK_RANGES(bl) (bl)->ranges | |
179 | ||
180 | /* Number of ranges within a block. */ | |
181 | ||
182 | #define BLOCK_NRANGES(bl) (bl)->ranges->nranges | |
183 | ||
184 | /* Access range array for block BL. */ | |
185 | ||
186 | #define BLOCK_RANGE(bl) (bl)->ranges->range | |
187 | ||
188 | /* Are all addresses within a block contiguous? */ | |
189 | ||
190 | #define BLOCK_CONTIGUOUS_P(bl) (BLOCK_RANGES (bl) == nullptr \ | |
191 | || BLOCK_NRANGES (bl) <= 1) | |
192 | ||
193 | /* Obtain the start address of the Nth range for block BL. */ | |
194 | ||
195 | #define BLOCK_RANGE_START(bl,n) (BLOCK_RANGE (bl)[n].startaddr) | |
196 | ||
197 | /* Obtain the end address of the Nth range for block BL. */ | |
198 | ||
199 | #define BLOCK_RANGE_END(bl,n) (BLOCK_RANGE (bl)[n].endaddr) | |
200 | ||
201 | /* Define the "entry pc" for a block BL to be the lowest (start) address | |
202 | for the block when all addresses within the block are contiguous. If | |
203 | non-contiguous, then use the start address for the first range in the | |
204 | block. | |
205 | ||
206 | At the moment, this almost matches what DWARF specifies as the entry | |
207 | pc. (The missing bit is support for DW_AT_entry_pc which should be | |
208 | preferred over range data and the low_pc.) | |
209 | ||
210 | Once support for DW_AT_entry_pc is added, I expect that an entry_pc | |
211 | field will be added to one of these data structures. Once that's done, | |
212 | the entry_pc field can be set from the dwarf reader (and other readers | |
213 | too). BLOCK_ENTRY_PC can then be redefined to be less DWARF-centric. */ | |
214 | ||
215 | #define BLOCK_ENTRY_PC(bl) (BLOCK_CONTIGUOUS_P (bl) \ | |
4b8791e1 | 216 | ? bl->start () \ |
26457a9c KB |
217 | : BLOCK_RANGE_START (bl,0)) |
218 | ||
fe898f56 DC |
219 | struct blockvector |
220 | { | |
221 | /* Number of blocks in the list. */ | |
222 | int nblocks; | |
801e3a5b JB |
223 | /* An address map mapping addresses to blocks in this blockvector. |
224 | This pointer is zero if the blocks' start and end addresses are | |
225 | enough. */ | |
226 | struct addrmap *map; | |
fe898f56 DC |
227 | /* The blocks themselves. */ |
228 | struct block *block[1]; | |
229 | }; | |
230 | ||
231 | #define BLOCKVECTOR_NBLOCKS(blocklist) (blocklist)->nblocks | |
232 | #define BLOCKVECTOR_BLOCK(blocklist,n) (blocklist)->block[n] | |
801e3a5b | 233 | #define BLOCKVECTOR_MAP(blocklist) ((blocklist)->map) |
fe898f56 | 234 | |
1994afbf DE |
235 | /* Return the objfile of BLOCK, which must be non-NULL. */ |
236 | ||
237 | extern struct objfile *block_objfile (const struct block *block); | |
238 | ||
239 | /* Return the architecture of BLOCK, which must be non-NULL. */ | |
240 | ||
241 | extern struct gdbarch *block_gdbarch (const struct block *block); | |
242 | ||
7f0df278 | 243 | extern struct symbol *block_linkage_function (const struct block *); |
fe898f56 | 244 | |
f8eba3c6 TT |
245 | extern struct symbol *block_containing_function (const struct block *); |
246 | ||
edb3359d DJ |
247 | extern int block_inlined_p (const struct block *block); |
248 | ||
f21c2bd7 TT |
249 | /* Return true if block A is lexically nested within block B, or if a |
250 | and b have the same pc range. Return false otherwise. If | |
251 | ALLOW_NESTED is true, then block A is considered to be in block B | |
252 | if A is in a nested function in B's function. If ALLOW_NESTED is | |
253 | false (the default), then blocks in nested functions are not | |
254 | considered to be contained. */ | |
255 | ||
256 | extern bool contained_in (const struct block *a, const struct block *b, | |
257 | bool allow_nested = false); | |
fe898f56 | 258 | |
346d1dfe | 259 | extern const struct blockvector *blockvector_for_pc (CORE_ADDR, |
3977b71f | 260 | const struct block **); |
fe898f56 | 261 | |
43f3e411 DE |
262 | extern const struct blockvector * |
263 | blockvector_for_pc_sect (CORE_ADDR, struct obj_section *, | |
264 | const struct block **, struct compunit_symtab *); | |
fe898f56 | 265 | |
346d1dfe | 266 | extern int blockvector_contains_pc (const struct blockvector *bv, CORE_ADDR pc); |
9703b513 | 267 | |
8e3b41a9 JK |
268 | extern struct call_site *call_site_for_pc (struct gdbarch *gdbarch, |
269 | CORE_ADDR pc); | |
270 | ||
3977b71f | 271 | extern const struct block *block_for_pc (CORE_ADDR); |
fe898f56 | 272 | |
3977b71f | 273 | extern const struct block *block_for_pc_sect (CORE_ADDR, struct obj_section *); |
fe898f56 | 274 | |
1fcb5155 DC |
275 | extern const char *block_scope (const struct block *block); |
276 | ||
9219021c DC |
277 | extern void block_set_scope (struct block *block, const char *scope, |
278 | struct obstack *obstack); | |
279 | ||
1fcb5155 DC |
280 | extern struct using_direct *block_using (const struct block *block); |
281 | ||
9219021c | 282 | extern void block_set_using (struct block *block, |
fe978cb0 | 283 | struct using_direct *using_decl, |
9219021c DC |
284 | struct obstack *obstack); |
285 | ||
89a9d1b1 DC |
286 | extern const struct block *block_static_block (const struct block *block); |
287 | ||
1fcb5155 DC |
288 | extern const struct block *block_global_block (const struct block *block); |
289 | ||
5c4e30ca DC |
290 | extern struct block *allocate_block (struct obstack *obstack); |
291 | ||
84a146c9 TT |
292 | extern struct block *allocate_global_block (struct obstack *obstack); |
293 | ||
43f3e411 DE |
294 | extern void set_block_compunit_symtab (struct block *, |
295 | struct compunit_symtab *); | |
8157b174 | 296 | |
63e43d3a PMR |
297 | /* Return a property to evaluate the static link associated to BLOCK. |
298 | ||
299 | In the context of nested functions (available in Pascal, Ada and GNU C, for | |
300 | instance), a static link (as in DWARF's DW_AT_static_link attribute) for a | |
301 | function is a way to get the frame corresponding to the enclosing function. | |
302 | ||
303 | Note that only objfile-owned and function-level blocks can have a static | |
304 | link. Return NULL if there is no such property. */ | |
305 | ||
306 | extern struct dynamic_prop *block_static_link (const struct block *block); | |
307 | ||
8157b174 TT |
308 | /* A block iterator. This structure should be treated as though it |
309 | were opaque; it is only defined here because we want to support | |
310 | stack allocation of iterators. */ | |
311 | ||
312 | struct block_iterator | |
313 | { | |
b5b04b5b | 314 | /* If we're iterating over a single block, this holds the block. |
43f3e411 | 315 | Otherwise, it holds the canonical compunit. */ |
b5b04b5b TT |
316 | |
317 | union | |
318 | { | |
43f3e411 | 319 | struct compunit_symtab *compunit_symtab; |
b5b04b5b TT |
320 | const struct block *block; |
321 | } d; | |
322 | ||
323 | /* If we're iterating over a single block, this is always -1. | |
324 | Otherwise, it holds the index of the current "included" symtab in | |
325 | the canonical symtab (that is, d.symtab->includes[idx]), with -1 | |
326 | meaning the canonical symtab itself. */ | |
327 | ||
328 | int idx; | |
329 | ||
330 | /* Which block, either static or global, to iterate over. If this | |
331 | is FIRST_LOCAL_BLOCK, then we are iterating over a single block. | |
332 | This is used to select which field of 'd' is in use. */ | |
333 | ||
334 | enum block_enum which; | |
335 | ||
b026f593 | 336 | /* The underlying multidictionary iterator. */ |
8157b174 | 337 | |
b026f593 | 338 | struct mdict_iterator mdict_iter; |
8157b174 TT |
339 | }; |
340 | ||
341 | /* Initialize ITERATOR to point at the first symbol in BLOCK, and | |
342 | return that first symbol, or NULL if BLOCK is empty. */ | |
343 | ||
344 | extern struct symbol *block_iterator_first (const struct block *block, | |
345 | struct block_iterator *iterator); | |
346 | ||
347 | /* Advance ITERATOR, and return the next symbol, or NULL if there are | |
348 | no more symbols. Don't call this if you've previously received | |
349 | NULL from block_iterator_first or block_iterator_next on this | |
350 | iteration. */ | |
351 | ||
352 | extern struct symbol *block_iterator_next (struct block_iterator *iterator); | |
353 | ||
8157b174 | 354 | /* Initialize ITERATOR to point at the first symbol in BLOCK whose |
987012b8 | 355 | search_name () matches NAME, and return that first symbol, or |
b5ec771e | 356 | NULL if there are no such symbols. */ |
8157b174 TT |
357 | |
358 | extern struct symbol *block_iter_match_first (const struct block *block, | |
b5ec771e | 359 | const lookup_name_info &name, |
8157b174 TT |
360 | struct block_iterator *iterator); |
361 | ||
362 | /* Advance ITERATOR to point at the next symbol in BLOCK whose | |
987012b8 | 363 | search_name () matches NAME, or NULL if there are no more such |
b5ec771e | 364 | symbols. Don't call this if you've previously received NULL from |
8157b174 TT |
365 | block_iterator_match_first or block_iterator_match_next on this |
366 | iteration. And don't call it unless ITERATOR was created by a | |
b5ec771e | 367 | previous call to block_iter_match_first with the same NAME. */ |
8157b174 | 368 | |
b5ec771e PA |
369 | extern struct symbol *block_iter_match_next |
370 | (const lookup_name_info &name, struct block_iterator *iterator); | |
8157b174 | 371 | |
de82891c TV |
372 | /* Return true if symbol A is the best match possible for DOMAIN. */ |
373 | ||
374 | extern bool best_symbol (struct symbol *a, const domain_enum domain); | |
375 | ||
376 | /* Return symbol B if it is a better match than symbol A for DOMAIN. | |
377 | Otherwise return A. */ | |
378 | ||
379 | extern struct symbol *better_symbol (struct symbol *a, struct symbol *b, | |
380 | const domain_enum domain); | |
381 | ||
16b2eaa1 DE |
382 | /* Search BLOCK for symbol NAME in DOMAIN. */ |
383 | ||
384 | extern struct symbol *block_lookup_symbol (const struct block *block, | |
385 | const char *name, | |
de63c46b | 386 | symbol_name_match_type match_type, |
16b2eaa1 DE |
387 | const domain_enum domain); |
388 | ||
ba715d7f JK |
389 | /* Search BLOCK for symbol NAME in DOMAIN but only in primary symbol table of |
390 | BLOCK. BLOCK must be STATIC_BLOCK or GLOBAL_BLOCK. Function is useful if | |
391 | one iterates all global/static blocks of an objfile. */ | |
392 | ||
393 | extern struct symbol *block_lookup_symbol_primary (const struct block *block, | |
394 | const char *name, | |
395 | const domain_enum domain); | |
396 | ||
b2e2f908 DE |
397 | /* The type of the MATCHER argument to block_find_symbol. */ |
398 | ||
399 | typedef int (block_symbol_matcher_ftype) (struct symbol *, void *); | |
400 | ||
401 | /* Find symbol NAME in BLOCK and in DOMAIN that satisfies MATCHER. | |
402 | DATA is passed unchanged to MATCHER. | |
403 | BLOCK must be STATIC_BLOCK or GLOBAL_BLOCK. */ | |
404 | ||
405 | extern struct symbol *block_find_symbol (const struct block *block, | |
406 | const char *name, | |
407 | const domain_enum domain, | |
408 | block_symbol_matcher_ftype *matcher, | |
409 | void *data); | |
410 | ||
411 | /* A matcher function for block_find_symbol to find only symbols with | |
412 | non-opaque types. */ | |
413 | ||
414 | extern int block_find_non_opaque_type (struct symbol *sym, void *data); | |
415 | ||
416 | /* A matcher function for block_find_symbol to prefer symbols with | |
417 | non-opaque types. The way to use this function is as follows: | |
418 | ||
419 | struct symbol *with_opaque = NULL; | |
420 | struct symbol *sym | |
421 | = block_find_symbol (block, name, domain, | |
dda83cd7 | 422 | block_find_non_opaque_type_preferred, &with_opaque); |
b2e2f908 DE |
423 | |
424 | At this point if SYM is non-NULL then a non-opaque type has been found. | |
425 | Otherwise, if WITH_OPAQUE is non-NULL then an opaque type has been found. | |
426 | Otherwise, the symbol was not found. */ | |
427 | ||
428 | extern int block_find_non_opaque_type_preferred (struct symbol *sym, | |
429 | void *data); | |
430 | ||
a023a30f DE |
431 | /* Macro to loop through all symbols in BLOCK, in no particular |
432 | order. ITER helps keep track of the iteration, and must be a | |
8157b174 TT |
433 | struct block_iterator. SYM points to the current symbol. */ |
434 | ||
435 | #define ALL_BLOCK_SYMBOLS(block, iter, sym) \ | |
436 | for ((sym) = block_iterator_first ((block), &(iter)); \ | |
437 | (sym); \ | |
438 | (sym) = block_iterator_next (&(iter))) | |
439 | ||
b5ec771e PA |
440 | /* Macro to loop through all symbols in BLOCK with a name that matches |
441 | NAME, in no particular order. ITER helps keep track of the | |
442 | iteration, and must be a struct block_iterator. SYM points to the | |
443 | current symbol. */ | |
358d6ab3 DE |
444 | |
445 | #define ALL_BLOCK_SYMBOLS_WITH_NAME(block, name, iter, sym) \ | |
b5ec771e | 446 | for ((sym) = block_iter_match_first ((block), (name), &(iter)); \ |
358d6ab3 | 447 | (sym) != NULL; \ |
b5ec771e | 448 | (sym) = block_iter_match_next ((name), &(iter))) |
358d6ab3 | 449 | |
26457a9c KB |
450 | /* Given a vector of pairs, allocate and build an obstack allocated |
451 | blockranges struct for a block. */ | |
452 | struct blockranges *make_blockranges (struct objfile *objfile, | |
dda83cd7 | 453 | const std::vector<blockrange> &rangevec); |
26457a9c | 454 | |
fe898f56 | 455 | #endif /* BLOCK_H */ |