From: Tom de Vries Date: Wed, 13 Jul 2022 10:20:53 +0000 (+0200) Subject: [gdb/symtab] Make per_cu->set_lang more strict X-Git-Url: https://git.libre-soc.org/?a=commitdiff_plain;h=52c0a455466aa437e4b8d6967e44767c8f3fa260;p=binutils-gdb.git [gdb/symtab] Make per_cu->set_lang more strict We have in per_cu->set_lang this comment: ... void set_lang (enum language lang) { /* We'd like to be more strict here, similar to what is done in set_unit_type, but currently a partial unit can go from unknown to minimal to ada to c. */ ... Fix this by not setting m_lang for partial units. This requires us to move the m_unit_type initialization to ensure that m_unit_type is initialized before per_cu->m_lang. Tested on x86_64-linux, with native and target board cc-with-dwz-m. --- diff --git a/gdb/dwarf2/read.c b/gdb/dwarf2/read.c index 2aac9912ef1..5ca787cc095 100644 --- a/gdb/dwarf2/read.c +++ b/gdb/dwarf2/read.c @@ -6777,24 +6777,6 @@ process_psymtab_comp_unit (dwarf2_per_cu_data *this_cu, if (reader.comp_unit_die == nullptr) return; - switch (reader.comp_unit_die->tag) - { - case DW_TAG_compile_unit: - this_cu->set_unit_type (DW_UT_compile); - break; - case DW_TAG_partial_unit: - this_cu->set_unit_type (DW_UT_partial); - break; - case DW_TAG_type_unit: - this_cu->set_unit_type (DW_UT_type); - break; - default: - error (_("Dwarf Error: unexpected tag '%s' at offset %s [in module %s]"), - dwarf_tag_name (reader.comp_unit_die->tag), - sect_offset_str (reader.cu->per_cu->sect_off), - objfile_name (per_objfile->objfile)); - } - if (reader.dummy_p) { /* Nothing. */ @@ -23587,8 +23569,26 @@ prepare_one_comp_unit (struct dwarf2_cu *cu, struct die_info *comp_unit_die, else lang = pretend_language; - cu->per_cu->set_lang (lang); cu->language_defn = language_def (lang); + + switch (comp_unit_die->tag) + { + case DW_TAG_compile_unit: + cu->per_cu->set_unit_type (DW_UT_compile); + break; + case DW_TAG_partial_unit: + cu->per_cu->set_unit_type (DW_UT_partial); + break; + case DW_TAG_type_unit: + cu->per_cu->set_unit_type (DW_UT_type); + break; + default: + error (_("Dwarf Error: unexpected tag '%s' at offset %s"), + dwarf_tag_name (comp_unit_die->tag), + sect_offset_str (cu->per_cu->sect_off)); + } + + cu->per_cu->set_lang (lang); } /* See read.h. */ diff --git a/gdb/dwarf2/read.h b/gdb/dwarf2/read.h index 707dca9ee57..fcf0cd70530 100644 --- a/gdb/dwarf2/read.h +++ b/gdb/dwarf2/read.h @@ -353,11 +353,14 @@ public: void set_lang (enum language lang) { - /* We'd like to be more strict here, similar to what is done in - set_unit_type, but currently a partial unit can go from unknown to - minimal to ada to c. */ - if (m_lang != lang) + if (unit_type () == DW_UT_partial) + return; + if (m_lang == language_unknown) + /* Set if not set already. */ m_lang = lang; + else + /* If already set, verify that it's the same value. */ + gdb_assert (m_lang == lang); } /* Free any cached file names. */ diff --git a/gdb/testsuite/gdb.dwarf2/dw2-missing-cu-tag.exp b/gdb/testsuite/gdb.dwarf2/dw2-missing-cu-tag.exp index 3ac94c31689..3d2890a56b2 100644 --- a/gdb/testsuite/gdb.dwarf2/dw2-missing-cu-tag.exp +++ b/gdb/testsuite/gdb.dwarf2/dw2-missing-cu-tag.exp @@ -54,7 +54,7 @@ clean_restart set pattern1 \ [multi_line \ "Reading symbols from \[^\r\n\]+" \ - "Dwarf Error: unexpected tag 'DW_TAG_subprogram' at offset $hex \\\[\[^\r\n\]+\\\]"] + "Dwarf Error: unexpected tag 'DW_TAG_subprogram' at offset $hex"] # This pattern is hit when GDB does use -readnow (e.g. running with # --target_board=readnow). @@ -62,7 +62,7 @@ set pattern2 \ [multi_line \ "Reading symbols from \[^\r\n\]+" \ "Expanding full symbols from \[^\r\n\]+" \ - "Dwarf Error: unexpected tag 'DW_TAG_subprogram' at offset $hex \\\[\[^\r\n\]+\\\]"] + "Dwarf Error: unexpected tag 'DW_TAG_subprogram' at offset $hex"] # Load the executable, we expect an error from the DWARF parser. gdb_test_multiple "file $binfile" "file $testfile" {