From a34a90995ae7c2f0c0b79ad32d8087d507fe42db Mon Sep 17 00:00:00 2001 From: Tom de Vries Date: Sun, 11 Sep 2022 09:01:03 +0200 Subject: [PATCH] [gdb/symtab] Fix handling of DW_TAG_unspecified_type Currently, the test-case contained in this patch fails: ... (gdb) p (int) foo ()^M Invalid cast.^M (gdb) FAIL: gdb.dwarf2/dw2-unspecified-type.exp: p (int) foo () ... because DW_TAG_unspecified_type is translated as void. There's some code in read_unspecified_type that marks the type as stub, but that's only active for ada: ... if (cu->lang () == language_ada) type->set_is_stub (true); ... Fix this by: - marking the type as a stub for all languages, and - handling the stub return type case in call_function_by_hand_dummy. Tested on x86_64-linux. Bug: https://sourceware.org/bugzilla/show_bug.cgi?id=29558 --- gdb/dwarf2/read.c | 6 +- gdb/infcall.c | 2 +- .../gdb.dwarf2/dw2-unspecified-type-foo.c | 22 ++++++ .../gdb.dwarf2/dw2-unspecified-type.c | 25 +++++++ .../gdb.dwarf2/dw2-unspecified-type.exp | 72 +++++++++++++++++++ 5 files changed, 123 insertions(+), 4 deletions(-) create mode 100644 gdb/testsuite/gdb.dwarf2/dw2-unspecified-type-foo.c create mode 100644 gdb/testsuite/gdb.dwarf2/dw2-unspecified-type.c create mode 100644 gdb/testsuite/gdb.dwarf2/dw2-unspecified-type.exp diff --git a/gdb/dwarf2/read.c b/gdb/dwarf2/read.c index 3ca441c4cae..da99402cff0 100644 --- a/gdb/dwarf2/read.c +++ b/gdb/dwarf2/read.c @@ -17714,9 +17714,9 @@ read_unspecified_type (struct die_info *die, struct dwarf2_cu *cu) /* In Ada, an unspecified type is typically used when the description of the type is deferred to a different unit. When encountering such a type, we treat it as a stub, and try to resolve it later on, - when needed. */ - if (cu->lang () == language_ada) - type->set_is_stub (true); + when needed. + Mark this as a stub type for all languages though. */ + type->set_is_stub (true); return set_die_type (die, type, cu); } diff --git a/gdb/infcall.c b/gdb/infcall.c index 2acceed4b07..dd70682bc76 100644 --- a/gdb/infcall.c +++ b/gdb/infcall.c @@ -814,7 +814,7 @@ call_function_by_hand_dummy (struct value *function, "target calling convention."), get_function_name (funaddr, name_buf, sizeof (name_buf))); - if (values_type == NULL) + if (values_type == NULL || values_type->is_stub ()) values_type = default_return_type; if (values_type == NULL) { diff --git a/gdb/testsuite/gdb.dwarf2/dw2-unspecified-type-foo.c b/gdb/testsuite/gdb.dwarf2/dw2-unspecified-type-foo.c new file mode 100644 index 00000000000..b1e3a8b98e4 --- /dev/null +++ b/gdb/testsuite/gdb.dwarf2/dw2-unspecified-type-foo.c @@ -0,0 +1,22 @@ +/* This testcase is part of GDB, the GNU debugger. + + Copyright 2022 Free Software Foundation, Inc. + + 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 . */ + +int +foo (void) +{ + asm ("foo_label: .globl foo_label"); +} diff --git a/gdb/testsuite/gdb.dwarf2/dw2-unspecified-type.c b/gdb/testsuite/gdb.dwarf2/dw2-unspecified-type.c new file mode 100644 index 00000000000..e3218205560 --- /dev/null +++ b/gdb/testsuite/gdb.dwarf2/dw2-unspecified-type.c @@ -0,0 +1,25 @@ +/* This testcase is part of GDB, the GNU debugger. + + Copyright 2022 Free Software Foundation, Inc. + + 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 . */ + +extern int foo (void); + +int +main (void) +{ + int res = foo (); + return res; +} diff --git a/gdb/testsuite/gdb.dwarf2/dw2-unspecified-type.exp b/gdb/testsuite/gdb.dwarf2/dw2-unspecified-type.exp new file mode 100644 index 00000000000..20c31dc5740 --- /dev/null +++ b/gdb/testsuite/gdb.dwarf2/dw2-unspecified-type.exp @@ -0,0 +1,72 @@ +# Copyright 2022 Free Software Foundation, Inc. + +# 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 . + +load_lib dwarf.exp + +# This test can only be run on targets which support DWARF-2 and use gas. +if {![dwarf2_support]} { + return 0 +} + +standard_testfile .c -foo.c dwz.S + +set foo_res \ + [function_range foo \ + [list ${srcdir}/${subdir}/$srcfile ${srcdir}/${subdir}/$srcfile2]] +lassign $foo_res \ + foo_start foo_len +set foo_end "$foo_start + $foo_len" + +# Create the DWARF. +set asm_file [standard_output_file $srcfile3] +Dwarf::assemble $asm_file { + global foo_start foo_end + declare_labels unspecified_type_label + + cu {} { + compile_unit { + {language @DW_LANG_Mips_Assembler} + } { + unspecified_type_label: unspecified_type {} + + DW_TAG_subprogram { + {name foo} + {low_pc $foo_start addr} + {high_pc $foo_end addr} + {type :$unspecified_type_label} + } + + } + } +} + +if [prepare_for_testing "failed to prepare" $testfile \ + "${asm_file} ${srcfile} ${srcfile2}" {}] { + return -1 +} + +if ![runto_main] { + return -1 +} + +# Print the function type. Return type should be stub type, which is printed +# as void. +gdb_test "ptype foo" "type = void \\(void\\)" + +# Call the function, casting the function to the correct function type. +gdb_test "p ((int (*) ()) foo) ()" " = 0" + +# Call the function, casting the function result to the correct type. +gdb_test "p (int) foo ()" " = 0" -- 2.30.2