1 /* Code dealing with blocks for GDB.
3 Copyright (C) 2003-2024 Free Software Foundation, Inc.
5 This file is part of GDB.
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 3 of the License, or
10 (at your option) any later version.
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.
17 You should have received a copy of the GNU General Public License
18 along with this program. If not, see <http://www.gnu.org/licenses/>. */
23 #include "dictionary.h"
24 #include "gdbsupport/array-view.h"
25 #include "gdbsupport/next-iterator.h"
27 /* Opaque declarations. */
30 struct compunit_symtab
;
31 struct block_namespace_info
;
36 /* Blocks can occupy non-contiguous address ranges. When this occurs,
37 startaddr and endaddr within struct block (still) specify the lowest
38 and highest addresses of all ranges, but each individual range is
39 specified by the addresses in struct blockrange. */
43 blockrange (CORE_ADDR start
, CORE_ADDR end
)
49 /* Return this blockrange's start address. */
50 CORE_ADDR
start () const
53 /* Set this blockrange's start address. */
54 void set_start (CORE_ADDR start
)
57 /* Return this blockrange's end address. */
58 CORE_ADDR
end () const
61 /* Set this blockrange's end address. */
62 void set_end (CORE_ADDR end
)
65 /* Lowest address in this range. */
69 /* One past the highest address in the range. */
74 /* Two or more non-contiguous ranges in the same order as that provided
75 via the debug info. */
80 struct blockrange range
[1];
83 /* All of the name-scope contours of the program
84 are represented by `struct block' objects.
85 All of these objects are pointed to by the blockvector.
87 Each block represents one name scope.
88 Each lexical context has its own block.
90 The blockvector begins with some special blocks.
91 The GLOBAL_BLOCK contains all the symbols defined in this compilation
92 whose scope is the entire program linked together.
93 The STATIC_BLOCK contains all the symbols whose scope is the
94 entire compilation excluding other separate compilations.
95 Blocks starting with the FIRST_LOCAL_BLOCK are not special.
97 Each block records a range of core addresses for the code that
98 is in the scope of the block. The STATIC_BLOCK and GLOBAL_BLOCK
99 give, for the range of code, the entire range of code produced
100 by the compilation that the symbol segment belongs to.
102 The blocks appear in the blockvector
103 in order of increasing starting-address,
104 and, within that, in order of decreasing ending-address.
106 This implies that within the body of one function
107 the blocks appear in the order of a depth-first tree walk. */
109 struct block
: public allocate_on_obstack
<block
>
111 /* Return this block's start address. */
112 CORE_ADDR
start () const
115 /* Set this block's start address. */
116 void set_start (CORE_ADDR start
)
119 /* Return this block's end address. */
120 CORE_ADDR
end () const
123 /* Set this block's end address. */
124 void set_end (CORE_ADDR end
)
127 /* Return this block's function symbol. */
128 symbol
*function () const
129 { return m_function
; }
131 /* Set this block's function symbol. */
132 void set_function (symbol
*function
)
133 { m_function
= function
; }
135 /* Return this block's superblock. */
136 const block
*superblock () const
137 { return m_superblock
; }
139 /* Set this block's superblock. */
140 void set_superblock (const block
*superblock
)
141 { m_superblock
= superblock
; }
143 /* Return this block's multidict. */
144 multidictionary
*multidict () const
145 { return m_multidict
; }
147 /* Return an iterator range for this block's multidict. */
148 iterator_range
<mdict_iterator_wrapper
> multidict_symbols () const
149 { return iterator_range
<mdict_iterator_wrapper
> (m_multidict
); }
151 /* Set this block's multidict. */
152 void set_multidict (multidictionary
*multidict
)
153 { m_multidict
= multidict
; }
155 /* Return a view on this block's ranges. */
156 gdb::array_view
<blockrange
> ranges ()
158 if (m_ranges
== nullptr)
161 return gdb::make_array_view (m_ranges
->range
, m_ranges
->nranges
);
164 /* Const version of the above. */
165 gdb::array_view
<const blockrange
> ranges () const
167 if (m_ranges
== nullptr)
170 return gdb::make_array_view (m_ranges
->range
, m_ranges
->nranges
);
173 /* Set this block's ranges array. */
174 void set_ranges (blockranges
*ranges
)
175 { m_ranges
= ranges
; }
177 /* Return true if all addresses within this block are contiguous. */
178 bool is_contiguous () const
179 { return this->ranges ().size () <= 1; }
181 /* Return the entry-pc of this block.
183 If the entry PC has been set to a specific value then this is
184 returned. Otherwise, the default_entry_pc() address is returned. */
186 CORE_ADDR
entry_pc () const
188 return default_entry_pc () + m_entry_pc_offset
;
191 /* Set this block's entry-pc to ADDR, which must lie between start() and
192 end(). The entry-pc is stored as the signed offset from the
193 default_entry_pc() address.
195 Note that block sub-ranges can be out of order, as such the offset of
196 the entry-pc might be negative. */
198 void set_entry_pc (CORE_ADDR addr
)
200 CORE_ADDR start
= default_entry_pc ();
202 gdb_assert (addr
>= this->start () && addr
< this->end ());
203 gdb_assert (start
>= this->start () && start
< this->end ());
205 m_entry_pc_offset
= addr
- start
;
208 /* Return the objfile of this block. */
210 struct objfile
*objfile () const;
212 /* Return the architecture of this block. */
214 struct gdbarch
*gdbarch () const;
216 /* Return true if BL represents an inlined function. */
218 bool inlined_p () const;
220 /* This returns the namespace that this block is enclosed in, or ""
221 if it isn't enclosed in a namespace at all. This travels the
222 chain of superblocks looking for a scope, if necessary. */
224 const char *scope () const;
226 /* Set this block's scope member to SCOPE; if needed, allocate
227 memory via OBSTACK. (It won't make a copy of SCOPE, however, so
228 that already has to be allocated correctly.) */
230 void set_scope (const char *scope
, struct obstack
*obstack
);
232 /* This returns the using directives list associated with this
235 next_range
<using_direct
> get_using () const;
237 /* Set this block's using member to USING; if needed, allocate
238 memory via OBSTACK. (It won't make a copy of USING, however, so
239 that already has to be allocated correctly.) */
241 void set_using (struct using_direct
*using_decl
, struct obstack
*obstack
);
243 /* Return the symbol for the function which contains a specified
244 lexical block, described by a struct block. The return value
245 will not be an inlined function; the containing function will be
248 struct symbol
*linkage_function () const;
250 /* Return the symbol for the function which contains a specified
251 block, described by a struct block. The return value will be the
252 closest enclosing function, which might be an inline
255 struct symbol
*containing_function () const;
257 /* Return the static block associated with this block. Return NULL
258 if block is a global block. */
260 const struct block
*static_block () const;
262 /* Return true if this block is a static block. */
264 bool is_static_block () const
266 const block
*sup
= superblock ();
269 return sup
->is_global_block ();
272 /* Return the global block associated with block. */
274 const struct global_block
*global_block () const;
276 /* Return true if this block is a global block. */
278 bool is_global_block () const
279 { return superblock () == nullptr; }
281 /* Return this block as a global_block. This block must be a global
283 struct global_block
*as_global_block ();
284 const struct global_block
*as_global_block () const;
286 /* Return the function block for this block. Returns nullptr if
287 there is no enclosing function, i.e., if this block is a static
290 const struct block
*function_block () const;
292 /* Return a property to evaluate the static link associated to this
295 In the context of nested functions (available in Pascal, Ada and
296 GNU C, for instance), a static link (as in DWARF's
297 DW_AT_static_link attribute) for a function is a way to get the
298 frame corresponding to the enclosing function.
300 Note that only objfile-owned and function-level blocks can have a
301 static link. Return NULL if there is no such property. */
303 struct dynamic_prop
*static_link () const;
305 /* Return true if block A is lexically nested within this block, or
306 if A and this block have the same pc range. Return false
307 otherwise. If ALLOW_NESTED is true, then block A is considered
308 to be in this block if A is in a nested function in this block's
309 function. If ALLOW_NESTED is false (the default), then blocks in
310 nested functions are not considered to be contained. */
312 bool contains (const struct block
*a
, bool allow_nested
= false) const;
316 /* Return the default entry-pc of this block. The default is the address
317 we use if the debug information hasn't specifically set a different
318 entry-pc value. This is the lowest address for the block when all
319 addresses within the block are contiguous. If non-contiguous, then
320 use the start address for the first range in the block.
322 This almost matches what DWARF specifies as the entry pc, except that
323 the final case, using the first address of the first range, is a GDB
324 extension. However, the DWARF reader sets the specific entry-pc
325 wherever possible, so this non-standard fallback case is only used as
328 CORE_ADDR
default_entry_pc () const
330 if (this->is_contiguous ())
331 return this->start ();
333 return this->ranges ()[0].start ();
336 /* If the namespace_info is NULL, allocate it via OBSTACK and
337 initialize its members to zero. */
338 void initialize_namespace (struct obstack
*obstack
);
340 /* Addresses in the executable code that are in this block. */
342 CORE_ADDR m_start
= 0;
345 /* The symbol that names this block, if the block is the body of a
346 function (real or inlined); otherwise, zero. */
348 struct symbol
*m_function
= nullptr;
350 /* The `struct block' for the containing block, or 0 if none.
352 The superblock of a top-level local block (i.e. a function in the
353 case of C) is the STATIC_BLOCK. The superblock of the
354 STATIC_BLOCK is the GLOBAL_BLOCK. */
356 const struct block
*m_superblock
= nullptr;
358 /* This is used to store the symbols in the block. */
360 struct multidictionary
*m_multidict
= nullptr;
362 /* Contains information about namespace-related info relevant to this block:
363 using directives and the current namespace scope. */
365 struct block_namespace_info
*m_namespace_info
= nullptr;
367 /* Address ranges for blocks with non-contiguous ranges. If this
368 is NULL, then there is only one range which is specified by
369 startaddr and endaddr above. */
371 struct blockranges
*m_ranges
= nullptr;
373 /* The offset of the actual entry-pc value from the default entry-pc
374 value. If space was no object then we'd store an actual address along
375 with a flag to indicate if the address has been set or not. But we'd
376 like to keep the size of block low, so we'd like to use a single
379 We would also like to avoid using 0 as a special address; some targets
380 do allow for accesses to address 0.
382 So instead we store the offset of the defined entry-pc from the
383 default entry-pc. See default_entry_pc() for the definition of the
384 default entry-pc. See entry_pc() for how this offset is used. */
386 LONGEST m_entry_pc_offset
= 0;
389 /* The global block is singled out so that we can provide a back-link
392 struct global_block
: public block
394 /* Set the compunit of this global block.
396 The compunit must not have been set previously. */
397 void set_compunit (compunit_symtab
*cu
)
399 gdb_assert (m_compunit
== nullptr);
403 /* Return the compunit of this global block.
405 The compunit must have been set previously. */
406 compunit_symtab
*compunit () const
408 gdb_assert (m_compunit
!= nullptr);
413 /* This holds a pointer to the compunit holding this block. */
414 compunit_symtab
*m_compunit
= nullptr;
419 /* Return a view on the blocks of this blockvector. */
420 gdb::array_view
<struct block
*> blocks ()
422 return gdb::array_view
<struct block
*> (m_blocks
, m_num_blocks
);
425 /* Const version of the above. */
426 gdb::array_view
<const struct block
*const> blocks () const
428 const struct block
**blocks
= (const struct block
**) m_blocks
;
429 return gdb::array_view
<const struct block
*const> (blocks
, m_num_blocks
);
432 /* Return the block at index I. */
433 struct block
*block (size_t i
)
434 { return this->blocks ()[i
]; }
436 /* Const version of the above. */
437 const struct block
*block (size_t i
) const
438 { return this->blocks ()[i
]; }
440 /* Set the block at index I. */
441 void set_block (int i
, struct block
*block
)
442 { m_blocks
[i
] = block
; }
444 /* Set the number of blocks of this blockvector.
446 The storage of blocks is done using a flexible array member, so the number
447 of blocks set here must agree with what was effectively allocated. */
448 void set_num_blocks (int num_blocks
)
449 { m_num_blocks
= num_blocks
; }
451 /* Return the number of blocks in this blockvector. */
452 int num_blocks () const
453 { return m_num_blocks
; }
455 /* Return the global block of this blockvector. */
456 struct global_block
*global_block ()
457 { return static_cast<struct global_block
*> (this->block (GLOBAL_BLOCK
)); }
459 /* Const version of the above. */
460 const struct global_block
*global_block () const
462 return static_cast<const struct global_block
*>
463 (this->block (GLOBAL_BLOCK
));
466 /* Return the static block of this blockvector. */
467 struct block
*static_block ()
468 { return this->block (STATIC_BLOCK
); }
470 /* Const version of the above. */
471 const struct block
*static_block () const
472 { return this->block (STATIC_BLOCK
); }
474 /* Return the address -> block map of this blockvector. */
475 addrmap_fixed
*map ()
478 /* Const version of the above. */
479 const addrmap_fixed
*map () const
482 /* Set this blockvector's address -> block map. */
483 void set_map (addrmap_fixed
*map
)
487 /* An address map mapping addresses to blocks in this blockvector.
488 This pointer is zero if the blocks' start and end addresses are
490 addrmap_fixed
*m_map
;
492 /* Number of blocks in the list. */
495 /* The blocks themselves. */
496 struct block
*m_blocks
[1];
499 extern const struct blockvector
*blockvector_for_pc (CORE_ADDR
,
500 const struct block
**);
502 extern const struct blockvector
*
503 blockvector_for_pc_sect (CORE_ADDR
, struct obj_section
*,
504 const struct block
**, struct compunit_symtab
*);
506 extern int blockvector_contains_pc (const struct blockvector
*bv
, CORE_ADDR pc
);
508 extern struct call_site
*call_site_for_pc (struct gdbarch
*gdbarch
,
511 extern const struct block
*block_for_pc (CORE_ADDR
);
513 extern const struct block
*block_for_pc_sect (CORE_ADDR
, struct obj_section
*);
515 /* A block iterator. This structure should be treated as though it
516 were opaque; it is only defined here because we want to support
517 stack allocation of iterators. */
519 struct block_iterator
521 /* If we're iterating over a single block, this holds the block.
522 Otherwise, it holds the canonical compunit. */
526 struct compunit_symtab
*compunit_symtab
;
527 const struct block
*block
;
530 /* If we're trying to match a name, this will be non-NULL. */
531 const lookup_name_info
*name
;
533 /* If we're iterating over a single block, this is always -1.
534 Otherwise, it holds the index of the current "included" symtab in
535 the canonical symtab (that is, d.symtab->includes[idx]), with -1
536 meaning the canonical symtab itself. */
540 /* Which block, either static or global, to iterate over. If this
541 is FIRST_LOCAL_BLOCK, then we are iterating over a single block.
542 This is used to select which field of 'd' is in use. */
544 enum block_enum which
;
546 /* The underlying multidictionary iterator. */
548 struct mdict_iterator mdict_iter
;
551 /* Initialize ITERATOR to point at the first symbol in BLOCK, and
552 return that first symbol, or NULL if BLOCK is empty. If NAME is
553 not NULL, only return symbols matching that name. */
555 extern struct symbol
*block_iterator_first
556 (const struct block
*block
,
557 struct block_iterator
*iterator
,
558 const lookup_name_info
*name
= nullptr);
560 /* Advance ITERATOR, and return the next symbol, or NULL if there are
561 no more symbols. Don't call this if you've previously received
562 NULL from block_iterator_first or block_iterator_next on this
565 extern struct symbol
*block_iterator_next (struct block_iterator
*iterator
);
567 /* An iterator that wraps a block_iterator. The naming here is
568 unfortunate, but block_iterator was named before gdb switched to
570 struct block_iterator_wrapper
572 typedef block_iterator_wrapper self_type
;
573 typedef struct symbol
*value_type
;
575 explicit block_iterator_wrapper (const struct block
*block
,
576 const lookup_name_info
*name
= nullptr)
577 : m_sym (block_iterator_first (block
, &m_iter
, name
))
581 block_iterator_wrapper ()
586 value_type
operator* () const
591 bool operator== (const self_type
&other
) const
593 return m_sym
== other
.m_sym
;
596 bool operator!= (const self_type
&other
) const
598 return m_sym
!= other
.m_sym
;
601 self_type
&operator++ ()
603 m_sym
= block_iterator_next (&m_iter
);
609 struct symbol
*m_sym
;
610 struct block_iterator m_iter
;
613 /* An iterator range for block_iterator_wrapper. */
615 typedef iterator_range
<block_iterator_wrapper
> block_iterator_range
;
617 /* Return true if symbol A is the best match possible for DOMAIN. */
619 extern bool best_symbol (struct symbol
*a
, const domain_search_flags domain
);
621 /* Return symbol B if it is a better match than symbol A for DOMAIN.
622 Otherwise return A. */
624 extern struct symbol
*better_symbol (struct symbol
*a
, struct symbol
*b
,
625 const domain_search_flags domain
);
627 /* Search BLOCK for symbol NAME in DOMAIN. */
629 extern struct symbol
*block_lookup_symbol (const struct block
*block
,
630 const lookup_name_info
&name
,
631 const domain_search_flags domain
);
633 /* Search BLOCK for symbol NAME in DOMAIN but only in primary symbol table of
634 BLOCK. BLOCK must be STATIC_BLOCK or GLOBAL_BLOCK. Function is useful if
635 one iterates all global/static blocks of an objfile. */
637 extern struct symbol
*block_lookup_symbol_primary
638 (const struct block
*block
,
640 const domain_search_flags domain
);
642 /* Find symbol NAME in BLOCK and in DOMAIN. This will return a
643 matching symbol whose type is not a "opaque", see TYPE_IS_OPAQUE.
644 If STUB is non-NULL, an otherwise matching symbol whose type is a
645 opaque will be stored here. */
647 extern struct symbol
*block_find_symbol (const struct block
*block
,
648 const lookup_name_info
&name
,
649 const domain_search_flags domain
,
650 struct symbol
**stub
);
652 /* Given a vector of pairs, allocate and build an obstack allocated
653 blockranges struct for a block. */
654 struct blockranges
*make_blockranges (struct objfile
*objfile
,
655 const std::vector
<blockrange
> &rangevec
);
657 #endif /* GDB_BLOCK_H */