mirror of
https://github.com/bminor/binutils-gdb.git
synced 2025-12-05 15:15:42 +00:00
This patch started as a fix for PR 29518 ("GDB doesn't handle
DW_FORM_ref_addr DIE references correctly with .debug_types sections")
[1], but the scope has expanded a bit to fix the problem more generally,
after I spotted a few issues related to the order of all_units. The
first version of this patch is here [2].
PR 29518 shows that dwarf2_find_containing_comp_unit can erroneously
find a type unit. The obvious problem is that the
dwarf2_find_containing_comp_unit function searches the whole all_units
vector (containing both comp and type units), when really it should just
search the compilation units. A simple solution would be to make it
search the all_comp_units view (which is removed in a patch earlier in
this series).
I then realized that in DWARF 5, since type units are in .debug_info
(versus .debug_types in DWARF 4), type units can be interleaved with
comp type in the all_units vector. That would make the all_comp_units
and all_type_units views erroneous, and dwarf2_find_containing_comp_unit
could still return something wrong. In v1, I added a sort in
finalize_all_units to make sure all_units is in the order that
dwarf2_find_containing_comp_unit expects:
- comp units from the main file
- type units from the main file
- comp units from the dwz file
- type units from the dwz file (not actually supported, see PR 30838)
Another problem I spotted is that the .gdb_index reader creates units in
this order:
- comp units from .gdb_index from main file
- comp units from .gdb_index from dwz file
- type units from .gdb_index from main file
This isn't the same order as above, so it would need the same sort step.
Finally, I'm not exactly sure if and when it happens, but it looks like
lookup_signatured_type can be called at a later time (after the initial
scan and creation of dwarf2_per_cu object creation), when expanding a
symtab. And that could lead to the creation of a new type unit (see
function add_type_unit), which would place the new type unit at the end
of the all_units vector, possibly screwing up the previous order.
To handle all this in a nice and generic way, Tom Tromey proposed to
change the all_units order, so that units are sorted by section, then
section offset. This is what this patch implements. The sorting is
done in finalize_all_units.
This works well, because when looking up a unit by section offset, the
caller knows which section the unit is in. Passing down a (section,
section offset) tuple makes it clear and unambiguous what unit the
caller is referring to. It should help eliminate some bugs where the
callee used the section offset in the wrong section. Passing down the
section along with the section offset replaces the "is_dwz" flag passed
to dwarf2_find_containing_comp_unit and a bunch of other functions in a
more general way.
dwarf2_find_containing_comp_unit can now legitimately find and return
type units even though it should be needed (type units are typically
referred to by signature). But I don't think there is harm for this
function to be more generic than needed. I therefore I renamed it to
dwarf2_find_containing_unit.
The sort criterion for "section" can be anything, as long as we use the
same for sorting and searching. In this patch, I use the pointer to
dwarf2_section_info, because it's easy. The downside is that the actual
order depends on what the memory allocator decided to return, so could
change from run to run, or machine to machine. Later, I might change it
so that sections are ordered based on their properties, making the order
stable across the board. This logic is encapsulated in the
all_units_less_than function, so it's easy to change.
The .debug_names reader can no longer rely on the order of the all_units
vector for its checks, since all_units won't be the same order as found
in the .debug_names lists. In fact, even before, it wasn't: this check
assumed that .debug_info had all CUs before TUs, and that the index
listed them in the exact same order. When I build a file with gcc and
"-gdwarf-5 -fdebug-types-section", type units appear first in
.debug_info. This caused GDB to reject a .debug_names index that is had
produced:
$ GDB="./gdb -nx -q --data-directory=data-directory" /home/smarchi/src/binutils-gdb/gdb/contrib/gdb-add-index.sh -dwarf-5 hello.so
$ ./gdb -nx -q --data-directory=data-directory hello.so
Reading symbols from hello.so...
⚠️ warning: Section .debug_names has incorrect entry in CU table, ignoring .debug_names.
To make it work, add a new dwarf2_find_unit function that allows looking
up a unit by start address (unlike dwarf2_find_containing_unit, which
can find by any containing address), and make the .debug_names reader
use it. It might make the load time of .debug_names a bit longer (the
build and check step is now going to be O(n*log(n)) instead of O(n)
where n is the number of units, or something like that), but I think
it's important to be correct here.
This patch adds a test
(gdb.dwarf2/dw-form-ref-addr-with-type-units.exp), which tries to
replicate the problem as shown by PR 29518.
gdb.base/varval.exp needs a small change, because an error message
changes (for the better, I think)
gdb.dwarf2/debug-names-non-ascending-cu.exp now fails, because GDB no
longer rejects a .debug_names index which lists CUs in a different order
than .debug_info. Given the change I did to the .debug_names reader,
explained above, I don't think this is a problem anymore (GDB can accept
an index like that). I also don't think that DWARF 5 mandates that CUs
are in ascending order. Delete this test.
[1] https://sourceware.org/bugzilla/show_bug.cgi?id=29518
[2] https://inbox.sourceware.org/gdb-patches/20250218193443.118139-1-simon.marchi@efficios.com/
Change-Id: I45f982d824d3842ac1eb73f8cce721a0a24b5faa
Approved-By: Tom Tromey <tom@tromey.com>
120 lines
4.2 KiB
C++
120 lines
4.2 KiB
C++
/* DWARF indexer
|
|
|
|
Copyright (C) 2022-2025 Free Software Foundation, Inc.
|
|
|
|
This file is part of GDB.
|
|
|
|
This program is free software; you can redistribute it and/or modify
|
|
it under the terms of the GNU General Public License as published by
|
|
the Free Software Foundation; either version 3 of the License, or
|
|
(at your option) any later version.
|
|
|
|
This program is distributed in the hope that it will be useful,
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
GNU General Public License for more details.
|
|
|
|
You should have received a copy of the GNU General Public License
|
|
along with this program. If not, see <http://www.gnu.org/licenses/>. */
|
|
|
|
#ifndef GDB_DWARF2_COOKED_INDEXER_H
|
|
#define GDB_DWARF2_COOKED_INDEXER_H
|
|
|
|
#include "dwarf2/cooked-index-entry.h"
|
|
#include "dwarf2/parent-map.h"
|
|
#include "dwarf2/types.h"
|
|
#include <variant>
|
|
|
|
struct abbrev_info;
|
|
struct cooked_index_worker_result;
|
|
struct cutu_reader;
|
|
struct dwarf2_per_cu;
|
|
struct dwarf2_per_objfile;
|
|
struct section_and_offset;
|
|
|
|
/* An instance of this is created to index a CU. */
|
|
|
|
class cooked_indexer
|
|
{
|
|
public:
|
|
cooked_indexer (cooked_index_worker_result *storage, dwarf2_per_cu *per_cu,
|
|
enum language language);
|
|
|
|
DISABLE_COPY_AND_ASSIGN (cooked_indexer);
|
|
|
|
/* Index the given CU. */
|
|
void make_index (cutu_reader *reader);
|
|
|
|
private:
|
|
|
|
/* A helper function to scan the PC bounds of READER and record them
|
|
in the storage's addrmap. */
|
|
void check_bounds (cutu_reader *reader);
|
|
|
|
/* Ensure that the indicated CU exists. The cutu_reader for it is
|
|
returned. FOR_SCANNING is true if the caller intends to scan all
|
|
the DIEs in the CU; when false, this use is assumed to be to look
|
|
up just a single DIE. */
|
|
cutu_reader *ensure_cu_exists (cutu_reader *reader,
|
|
const section_and_offset §_off,
|
|
bool for_scanning);
|
|
|
|
/* Index DIEs in the READER starting at INFO_PTR. PARENT is
|
|
the entry for the enclosing scope (nullptr at top level). FULLY
|
|
is true when a full scan must be done -- in some languages,
|
|
function scopes must be fully explored in order to find nested
|
|
functions. This returns a pointer to just after the spot where
|
|
reading stopped. */
|
|
const gdb_byte *index_dies (cutu_reader *reader,
|
|
const gdb_byte *info_ptr,
|
|
std::variant<const cooked_index_entry *,
|
|
parent_map::addr_type> parent,
|
|
bool fully);
|
|
|
|
/* Scan the attributes for a given DIE and update the out
|
|
parameters. Returns a pointer to the byte after the DIE. */
|
|
const gdb_byte *scan_attributes (dwarf2_per_cu *scanning_per_cu,
|
|
cutu_reader *reader,
|
|
const gdb_byte *watermark_ptr,
|
|
const gdb_byte *info_ptr,
|
|
const abbrev_info *abbrev,
|
|
const char **name,
|
|
const char **linkage_name,
|
|
cooked_index_flag *flags,
|
|
sect_offset *sibling_offset,
|
|
const cooked_index_entry **parent_entry,
|
|
parent_map::addr_type *maybe_defer,
|
|
bool *is_enum_class,
|
|
bool for_specification);
|
|
|
|
/* Handle DW_TAG_imported_unit, by scanning the DIE to find
|
|
DW_AT_import, and then scanning the referenced CU. Returns a
|
|
pointer to the byte after the DIE. */
|
|
const gdb_byte *index_imported_unit (cutu_reader *reader,
|
|
const gdb_byte *info_ptr,
|
|
const abbrev_info *abbrev);
|
|
|
|
/* Recursively read DIEs, recording the section offsets in
|
|
m_die_range_map and then calling index_dies. */
|
|
const gdb_byte *recurse (cutu_reader *reader,
|
|
const gdb_byte *info_ptr,
|
|
std::variant<const cooked_index_entry *,
|
|
parent_map::addr_type> parent_entry,
|
|
bool fully);
|
|
|
|
/* The storage object, where the results are kept. */
|
|
cooked_index_worker_result *m_index_storage;
|
|
/* The CU that we are reading on behalf of. This object might be
|
|
asked to index one CU but to treat the results as if they come
|
|
from some including CU; in this case the including CU would be
|
|
recorded here. */
|
|
dwarf2_per_cu *m_per_cu;
|
|
/* The language that we're assuming when reading. */
|
|
enum language m_language;
|
|
|
|
/* Map from DIE ranges to newly-created entries. */
|
|
parent_map *m_die_range_map;
|
|
};
|
|
|
|
#endif /* GDB_DWARF2_COOKED_INDEXER_H */
|