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