Skip to content

Commit d06730b

Browse files
committed
[gdb/symtab] Find main language without symtab expansion
When loading an executable using "file a.out", the language is set according to a.out, which can involve looking up the language of symbol "main", which will cause the symtab expansion for the containing CU. Expansion of lto debug info can be slow, so in commit d321419 ("[gdb] Use partial symbol table to find language for main") a feature was added to avoid the symtab expansion. This feature stopped working after commit 7f4307436fd ("Fix "start" for D, Rust, etc"). [ The commit addresses problems related to command start, which requires finding the main function: - for language D, "main" was found instead of "D main", and - for Rust, the correct function was found, but attributed the wrong name (not fully qualified). ] Reimplement the feature by adding cooked_index_functions::lookup_global_symbol_language. Tested on x86_64-linux. PR symtab/30661 Bug: https://sourceware.org/bugzilla/show_bug.cgi?id=30661
1 parent db583cf commit d06730b

File tree

4 files changed

+120
-0
lines changed

4 files changed

+120
-0
lines changed

gdb/dwarf2/read.c

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16713,6 +16713,46 @@ struct cooked_index_functions : public dwarf2_base_index_functions
1671316713
if (dwarf2_has_info (objfile, nullptr))
1671416714
dwarf2_build_psymtabs (objfile);
1671516715
}
16716+
16717+
enum language lookup_global_symbol_language (struct objfile *objfile,
16718+
const char *name,
16719+
domain_enum domain,
16720+
bool *symbol_found_p) override
16721+
{
16722+
*symbol_found_p = false;
16723+
16724+
if (!(domain == VAR_DOMAIN && streq (name, "main")))
16725+
return language_unknown;
16726+
16727+
dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
16728+
struct dwarf2_per_bfd *per_bfd = per_objfile->per_bfd;
16729+
if (per_bfd->index_table == nullptr)
16730+
return language_unknown;
16731+
16732+
/* Expansion of large CUs can be slow. By returning the language of main
16733+
here for C and C++, we avoid CU expansion during set_initial_language.
16734+
But by doing a symbol lookup in the cooked index, we are forced to wait
16735+
for finalization to complete. See PR symtab/30174 for ideas how to
16736+
bypass that as well. */
16737+
cooked_index *table
16738+
= (gdb::checked_static_cast<cooked_index *>
16739+
(per_bfd->index_table.get ()));
16740+
16741+
for (const cooked_index_entry *entry : table->find (name, false))
16742+
{
16743+
if (entry->tag != DW_TAG_subprogram)
16744+
continue;
16745+
16746+
enum language lang = entry->per_cu->lang ();
16747+
if (!(lang == language_c || lang == language_cplus))
16748+
continue;
16749+
16750+
*symbol_found_p = true;
16751+
return lang;
16752+
}
16753+
16754+
return language_unknown;
16755+
}
1671616756
};
1671716757

1671816758
dwarf2_per_cu_data *

gdb/testsuite/gdb.base/main-c.exp

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
# Copyright 2023 Free Software Foundation, Inc.
2+
3+
# This program is free software; you can redistribute it and/or modify
4+
# it under the terms of the GNU General Public License as published by
5+
# the Free Software Foundation; either version 3 of the License, or
6+
# (at your option) any later version.
7+
#
8+
# This program is distributed in the hope that it will be useful,
9+
# but WITHOUT ANY WARRANTY; without even the implied warranty of
10+
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11+
# GNU General Public License for more details.
12+
#
13+
# You should have received a copy of the GNU General Public License
14+
# along with this program. If not, see <http://www.gnu.org/licenses/>.
15+
16+
# Check that finding main to set the current language doesn't cause any symtab
17+
# to be expanded.
18+
19+
standard_testfile main.c
20+
21+
require !readnow
22+
23+
if { [prepare_for_testing "failed to prepare" $testfile $srcfile] } {
24+
return -1
25+
}
26+
27+
require {string eq [have_index $binfile] ""}
28+
29+
gdb_test_no_output "maint info symtabs"

gdb/testsuite/gdb.cp/main-cp.exp

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
# Copyright 2023 Free Software Foundation, Inc.
2+
3+
# This program is free software; you can redistribute it and/or modify
4+
# it under the terms of the GNU General Public License as published by
5+
# the Free Software Foundation; either version 3 of the License, or
6+
# (at your option) any later version.
7+
#
8+
# This program is distributed in the hope that it will be useful,
9+
# but WITHOUT ANY WARRANTY; without even the implied warranty of
10+
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11+
# GNU General Public License for more details.
12+
#
13+
# You should have received a copy of the GNU General Public License
14+
# along with this program. If not, see <http://www.gnu.org/licenses/>.
15+
16+
# Check that finding main to set the current language doesn't cause any symtab
17+
# to be expanded.
18+
19+
standard_testfile main.cc
20+
21+
require !readnow
22+
23+
if { [prepare_for_testing "failed to prepare" $testfile $srcfile] } {
24+
return -1
25+
}
26+
27+
require {string eq [have_index $binfile] ""}
28+
29+
gdb_test_no_output "maint info symtabs"

gdb/testsuite/gdb.cp/main.cc

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
/* This testcase is part of GDB, the GNU debugger.
2+
3+
Copyright 2023 Free Software Foundation, Inc.
4+
5+
This program is free software; you can redistribute it and/or modify
6+
it under the terms of the GNU General Public License as published by
7+
the Free Software Foundation; either version 3 of the License, or
8+
(at your option) any later version.
9+
10+
This program is distributed in the hope that it will be useful,
11+
but WITHOUT ANY WARRANTY; without even the implied warranty of
12+
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13+
GNU General Public License for more details.
14+
15+
You should have received a copy of the GNU General Public License
16+
along with this program. If not, see <http://www.gnu.org/licenses/>. */
17+
18+
int
19+
main (void)
20+
{
21+
return 0;
22+
}

0 commit comments

Comments
 (0)