From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: by sourceware.org (Postfix, from userid 1983) id 9028C385781F; Mon, 14 Nov 2022 21:23:48 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 9028C385781F DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=sourceware.org; s=default; t=1668461028; bh=fepGt++m4bHhU8aHiKaxK2snPDddVpAa0WBvx5fXaJE=; h=From:To:Subject:Date:From; b=a2sV95F+opDr9chFOVtPctID1WDXvg+RXApGj/IKbXiw6DUKdnS4/NF4Sim5QFlQJ HxujSId4WMxY134SkiTbcFMXH03gdPTgQnOOMz03l7nhHxXRoXBhlEvXUaq+UmvzW0 737EYv8DGjibqWnXtrCL3VoxmHVVL2uBsBI33/yM= Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable From: Carl Love To: gdb-cvs@sourceware.org Subject: [binutils-gdb] PowerPC, fix support for printing the function return value for non-trivial values. X-Act-Checkin: binutils-gdb X-Git-Author: Carl Love X-Git-Refname: refs/heads/master X-Git-Oldrev: 24b27e5e9b6bf5a37fb39dfca151722bb801cbaa X-Git-Newrev: a0eda3df5b750ae32576a9be092b361281a41787 Message-Id: <20221114212348.9028C385781F@sourceware.org> Date: Mon, 14 Nov 2022 21:23:48 +0000 (GMT) List-Id: https://sourceware.org/git/gitweb.cgi?p=3Dbinutils-gdb.git;h=3Da0eda3df5b75= 0ae32576a9be092b361281a41787 commit a0eda3df5b750ae32576a9be092b361281a41787 Author: Carl Love Date: Mon Nov 14 16:22:37 2022 -0500 PowerPC, fix support for printing the function return value for non-tri= vial values. =20 Currently, a non-trivial return value from a function cannot currently = be reliably determined on PowerPC. This is due to the fact that the Power= PC ABI uses register r3 to store the address of the buffer containing the non-trivial return value when the function is called. The PowerPC ABI does not guarantee the value in register r3 is not modified in the function. Thus the value in r3 cannot be reliably used to obtain the return addreses on exit from the function. =20 This patch adds a new gdbarch method to allow PowerPC to access the val= ue of r3 on entry to a function. On PowerPC, the new gdbarch method attemp= ts to use the DW_OP_entry_value for the DWARF entries, when exiting the function, to determine the value of r3 on entry to the function. This requires the use of the -fvar-tracking compiler option to compile the user application thus generating the DW_OP_entry_value in the binary. = The DW_OP_entry_value entries in the binary file allows GDB to resolve the DW_TAG_call_site entries. This new gdbarch method is used to get the return buffer address, in the case of a function returning a nontrivial data type, on exit from the function. The GDB function should_stop che= cks to see if RETURN_BUF is non-zero. By default, RETURN_BUF will be set to zero by the new gdbarch method call for all architectures except PowerP= C. The get_return_value function will be used to obtain the return value on all other architectures as is currently being done if RETURN_BUF is zer= o. On PowerPC, the new gdbarch method will return a nonzero address in RETURN_BUF if the value can be determined. The value_at function uses = the return buffer address to get the return value. =20 This patch fixes five testcase failures in gdb.cp/non-trivial-retval.ex= p. The correct function return values are now reported. =20 Note this patch is dependent on patch: "PowerPC, function ppc64_sysv_abi_return_value add missing return value convention". =20 This patch has been tested on Power 10 and x86-64 with no regressions. Diff: --- gdb/arch-utils.c | 6 +++++ gdb/arch-utils.h | 4 +++ gdb/dwarf2/loc.c | 10 ++----- gdb/dwarf2/loc.h | 11 ++++++++ gdb/gdbarch-components.py | 15 +++++++++++ gdb/gdbarch-gen.h | 11 ++++++++ gdb/gdbarch.c | 22 ++++++++++++++++ gdb/infcmd.c | 41 +++++++++++++++++++++++++= +--- gdb/ppc-sysv-tdep.c | 41 +++++++++++++++++++++++++= ++++ gdb/ppc-tdep.h | 2 ++ gdb/rs6000-tdep.c | 6 ++++- gdb/testsuite/gdb.cp/non-trivial-retval.exp | 9 ++++++- gdb/testsuite/lib/gdb.exp | 8 ++++++ 13 files changed, 172 insertions(+), 14 deletions(-) diff --git a/gdb/arch-utils.c b/gdb/arch-utils.c index 5218bfc05e1..7b84daf046e 100644 --- a/gdb/arch-utils.c +++ b/gdb/arch-utils.c @@ -1084,6 +1084,12 @@ default_read_core_file_mappings { } =20 +CORE_ADDR +default_get_return_buf_addr (struct type *val_type, frame_info_ptr cur_fra= me) +{ + return 0; +} + /* Non-zero if we want to trace architecture code. */ =20 #ifndef GDBARCH_DEBUG diff --git a/gdb/arch-utils.h b/gdb/arch-utils.h index f6229f434fd..46018a6fbbb 100644 --- a/gdb/arch-utils.h +++ b/gdb/arch-utils.h @@ -300,4 +300,8 @@ extern void default_read_core_file_mappings struct bfd *cbfd, read_core_file_mappings_pre_loop_ftype pre_loop_cb, read_core_file_mappings_loop_ftype loop_cb); + +/* Default implementation of gdbarch default_get_return_buf_addr method. = */ +extern CORE_ADDR default_get_return_buf_addr (struct type *val_typegdbarch, + frame_info_ptr cur_frame); #endif /* ARCH_UTILS_H */ diff --git a/gdb/dwarf2/loc.c b/gdb/dwarf2/loc.c index c42359ab96e..8355aa44333 100644 --- a/gdb/dwarf2/loc.c +++ b/gdb/dwarf2/loc.c @@ -1325,14 +1325,8 @@ static const struct lval_funcs entry_data_value_func= s =3D entry_data_value_free_closure }; =20 -/* Read parameter of TYPE at (callee) FRAME's function entry. KIND and KI= ND_U - are used to match DW_AT_location at the caller's - DW_TAG_call_site_parameter. - - Function always returns non-NULL value. It throws NO_ENTRY_VALUE_ERROR= if it - cannot resolve the parameter for any reason. */ - -static struct value * +/* See dwarf2/loc.h. */ +struct value * value_of_dwarf_reg_entry (struct type *type, frame_info_ptr frame, enum call_site_parameter_kind kind, union call_site_parameter_u kind_u) diff --git a/gdb/dwarf2/loc.h b/gdb/dwarf2/loc.h index d6709f2e342..9156e1ee533 100644 --- a/gdb/dwarf2/loc.h +++ b/gdb/dwarf2/loc.h @@ -296,4 +296,15 @@ extern struct value *indirect_synthetic_pointer dwarf2_per_objfile *per_objfile, frame_info_ptr frame, struct type *type, bool resolve_abstract_p =3D false); =20 +/* Read parameter of TYPE at (callee) FRAME's function entry. KIND and KI= ND_U + are used to match DW_AT_location at the caller's + DW_TAG_call_site_parameter. + + Function always returns non-NULL value. It throws NO_ENTRY_VALUE_ERROR= if + it cannot resolve the parameter for any reason. */ + +extern struct value *value_of_dwarf_reg_entry (struct type *type, + struct frame_info_ptr frame, + enum call_site_parameter_kind kind, + union call_site_parameter_u kind_u); #endif /* DWARF2LOC_H */ diff --git a/gdb/gdbarch-components.py b/gdb/gdbarch-components.py index b2c7b784761..9b688998a7b 100644 --- a/gdb/gdbarch-components.py +++ b/gdb/gdbarch-components.py @@ -868,6 +868,21 @@ for instance). invalid=3DTrue, ) =20 +Function( + comment=3D""" +Return the address at which the value being returned from +the current function will be stored. This routine is only +called if the current function uses the the "struct return +convention". + +May return 0 when unable to determine that address.""", + type=3D"CORE_ADDR", + name=3D"get_return_buf_addr", + params=3D[("struct type *", "val_type"), ("frame_info_ptr", "cur_frame= ")], + predefault=3D"default_get_return_buf_addr", + invalid=3DFalse, +) + Method( comment=3D""" Return true if the return value of function is stored in the first hidden diff --git a/gdb/gdbarch-gen.h b/gdb/gdbarch-gen.h index e0d7a08ff6a..a663316df16 100644 --- a/gdb/gdbarch-gen.h +++ b/gdb/gdbarch-gen.h @@ -441,6 +441,17 @@ typedef enum return_value_convention (gdbarch_return_v= alue_ftype) (struct gdbarc extern enum return_value_convention gdbarch_return_value (struct gdbarch *= gdbarch, struct value *function, struct type *valtype, struct regcache *reg= cache, gdb_byte *readbuf, const gdb_byte *writebuf); extern void set_gdbarch_return_value (struct gdbarch *gdbarch, gdbarch_ret= urn_value_ftype *return_value); =20 +/* Return the address at which the value being returned from + the current function will be stored. This routine is only + called if the current function uses the the "struct return + convention". + + May return 0 when unable to determine that address. */ + +typedef CORE_ADDR (gdbarch_get_return_buf_addr_ftype) (struct type *val_ty= pe, frame_info_ptr cur_frame); +extern CORE_ADDR gdbarch_get_return_buf_addr (struct gdbarch *gdbarch, str= uct type *val_type, frame_info_ptr cur_frame); +extern void set_gdbarch_get_return_buf_addr (struct gdbarch *gdbarch, gdba= rch_get_return_buf_addr_ftype *get_return_buf_addr); + /* Return true if the return value of function is stored in the first hidd= en parameter. In theory, this feature should be language-dependent, speci= fied by language and its ABI, such as C++. Unfortunately, compiler may diff --git a/gdb/gdbarch.c b/gdb/gdbarch.c index 9d929da6da9..3227e945880 100644 --- a/gdb/gdbarch.c +++ b/gdb/gdbarch.c @@ -116,6 +116,7 @@ struct gdbarch gdbarch_address_to_pointer_ftype *address_to_pointer =3D unsigned_addres= s_to_pointer; gdbarch_integer_to_address_ftype *integer_to_address =3D nullptr; gdbarch_return_value_ftype *return_value =3D nullptr; + gdbarch_get_return_buf_addr_ftype *get_return_buf_addr =3D default_get_r= eturn_buf_addr; gdbarch_return_in_first_hidden_param_p_ftype *return_in_first_hidden_par= am_p =3D default_return_in_first_hidden_param_p; gdbarch_skip_prologue_ftype *skip_prologue =3D 0; gdbarch_skip_main_prologue_ftype *skip_main_prologue =3D nullptr; @@ -369,6 +370,7 @@ verify_gdbarch (struct gdbarch *gdbarch) /* Skip verify of address_to_pointer, invalid_p =3D=3D 0 */ /* Skip verify of integer_to_address, has predicate. */ /* Skip verify of return_value, has predicate. */ + /* Skip verify of get_return_buf_addr, invalid_p =3D=3D 0 */ /* Skip verify of return_in_first_hidden_param_p, invalid_p =3D=3D 0 */ if (gdbarch->skip_prologue =3D=3D 0) log.puts ("\n\tskip_prologue"); @@ -780,6 +782,9 @@ gdbarch_dump (struct gdbarch *gdbarch, struct ui_file *= file) gdb_printf (file, "gdbarch_dump: return_value =3D <%s>\n", host_address_to_string (gdbarch->return_value)); + gdb_printf (file, + "gdbarch_dump: get_return_buf_addr =3D <%s>\n", + host_address_to_string (gdbarch->get_return_buf_addr= )); gdb_printf (file, "gdbarch_dump: return_in_first_hidden_param_p =3D <%= s>\n", host_address_to_string (gdbarch->return_in_first_hid= den_param_p)); @@ -2587,6 +2592,23 @@ set_gdbarch_return_value (struct gdbarch *gdbarch, gdbarch->return_value =3D return_value; } =20 +CORE_ADDR +gdbarch_get_return_buf_addr (struct gdbarch *gdbarch, struct type *val_typ= e, frame_info_ptr cur_frame) +{ + gdb_assert (gdbarch !=3D NULL); + gdb_assert (gdbarch->get_return_buf_addr !=3D NULL); + if (gdbarch_debug >=3D 2) + gdb_printf (gdb_stdlog, "gdbarch_get_return_buf_addr called\n"); + return gdbarch->get_return_buf_addr (val_type, cur_frame); +} + +void +set_gdbarch_get_return_buf_addr (struct gdbarch *gdbarch, + gdbarch_get_return_buf_addr_ftype get_ret= urn_buf_addr) +{ + gdbarch->get_return_buf_addr =3D get_return_buf_addr; +} + int gdbarch_return_in_first_hidden_param_p (struct gdbarch *gdbarch, struct ty= pe *type) { diff --git a/gdb/infcmd.c b/gdb/infcmd.c index 6f83949cc7c..b71dc10370b 100644 --- a/gdb/infcmd.c +++ b/gdb/infcmd.c @@ -55,6 +55,7 @@ #include "gdbsupport/gdb_optional.h" #include "source.h" #include "cli/cli-style.h" +#include "dwarf2/loc.h" =20 /* Local functions: */ =20 @@ -1609,6 +1610,12 @@ struct finish_command_fsm : public thread_fsm return value. */ struct return_value_info return_value_info {}; =20 + /* If the current function uses the "struct return convention", + this holds the address at which the value being returned will + be stored, or zero if that address could not be determined or + the "struct return convention" is not being used. */ + CORE_ADDR return_buf; + explicit finish_command_fsm (struct interp *cmd_interp) : thread_fsm (cmd_interp) { @@ -1646,7 +1653,13 @@ finish_command_fsm::should_stop (struct thread_info = *tp) struct value *func; =20 func =3D read_var_value (function, NULL, get_current_frame ()); - rv->value =3D get_return_value (function, func); + + if (return_buf !=3D 0) + /* Retrieve return value from the buffer where it was saved. */ + rv->value =3D value_at (rv->type, return_buf); + else + rv->value =3D get_return_value (function, func); + if (rv->value !=3D NULL) rv->value_history_index =3D record_latest_value (rv->value); } @@ -1862,8 +1875,28 @@ finish_command (const char *arg, int from_tty) } =20 /* Find the function we will return from. */ - - sm->function =3D find_pc_function (get_frame_pc (get_selected_frame (NUL= L))); + frame_info_ptr callee_frame =3D get_selected_frame (NULL); + sm->function =3D find_pc_function (get_frame_pc (callee_frame)); + + /* Determine the return convention. If it is RETURN_VALUE_STRUCT_CONVEN= TION, + attempt to determine the address of the return buffer. */ + enum return_value_convention return_value; + struct gdbarch *gdbarch =3D get_frame_arch (callee_frame); + + struct type * val_type + =3D check_typedef (sm->function->type ()->target_type ()); + + return_value =3D gdbarch_return_value (gdbarch, + read_var_value (sm->function, NULL, + callee_frame), + val_type, NULL, NULL, NULL); + + if (return_value =3D=3D RETURN_VALUE_STRUCT_CONVENTION + && val_type->code () !=3D TYPE_CODE_VOID) + sm->return_buf =3D gdbarch_get_return_buf_addr (gdbarch, val_type, + callee_frame); + else + sm->return_buf =3D 0; =20 /* Print info on the selected frame, including level number but not source. */ @@ -1881,7 +1914,7 @@ finish_command (const char *arg, int from_tty) gdb_printf (_("Run till exit from ")); } =20 - print_stack_frame (get_selected_frame (NULL), 1, LOCATION, 0); + print_stack_frame (callee_frame, 1, LOCATION, 0); } frame.reinflate (); =20 diff --git a/gdb/ppc-sysv-tdep.c b/gdb/ppc-sysv-tdep.c index 450162dd46e..1cbaaf2a4e6 100644 --- a/gdb/ppc-sysv-tdep.c +++ b/gdb/ppc-sysv-tdep.c @@ -28,6 +28,7 @@ #include "objfiles.h" #include "infcall.h" #include "dwarf2.h" +#include "dwarf2/loc.h" #include "target-float.h" #include =20 @@ -2156,3 +2157,43 @@ ppc64_sysv_abi_return_value (struct gdbarch *gdbarch= , struct value *function, return RETURN_VALUE_STRUCT_CONVENTION; } =20 +CORE_ADDR ppc64_sysv_get_return_buf_addr (struct type *val_type, + frame_info_ptr cur_frame) +{ + /* The PowerPC ABI specifies aggregates that are not returned by value + are returned in a storage buffer provided by the caller. The + address of the storage buffer is provided as a hidden first input + arguement in register r3. The PowerPC ABI does not guarantee that + register r3 will not be changed while executing the function. Hence,= it + cannot be assumed that r3 will still contain the address of the stora= ge + buffer when execution reaches the end of the function. + + This function attempts to determine the value of r3 on entry to the + function using the DW_OP_entry_value DWARF entries. This requires + compiling the user program with -fvar-tracking to resolve the + DW_TAG_call_sites in the binary file. */ + + union call_site_parameter_u kind_u; + enum call_site_parameter_kind kind; + CORE_ADDR return_val =3D 0; + + kind_u.dwarf_reg =3D 3; /* First passed arg/return value is in r3. */ + kind =3D CALL_SITE_PARAMETER_DWARF_REG; + + /* val_type is the type of the return value. Need the pointer type + to the return value. */ + val_type =3D lookup_pointer_type (val_type); + + try + { + return_val =3D value_as_address (value_of_dwarf_reg_entry (val_type, + cur_frame, + kind, kind_u)); + } + catch (const gdb_exception_error &e) + { + warning ("Cannot determine the function return value.\n" + "Try compiling with -fvar-tracking."); + } + return return_val; +} diff --git a/gdb/ppc-tdep.h b/gdb/ppc-tdep.h index 34b250849b9..cbccd87820d 100644 --- a/gdb/ppc-tdep.h +++ b/gdb/ppc-tdep.h @@ -175,6 +175,8 @@ extern void ppc_collect_vsxregset (const struct regset = *regset, const struct regcache *regcache, int regnum, void *vsxregs, size_t len); =20 +extern CORE_ADDR ppc64_sysv_get_return_buf_addr (type*, frame_info_ptr); + /* Private data that this module attaches to struct gdbarch. */ =20 /* ELF ABI version used by the inferior. */ diff --git a/gdb/rs6000-tdep.c b/gdb/rs6000-tdep.c index 866d43ded7a..cbd84514795 100644 --- a/gdb/rs6000-tdep.c +++ b/gdb/rs6000-tdep.c @@ -8243,7 +8243,11 @@ rs6000_gdbarch_init (struct gdbarch_info info, struc= t gdbarch_list *arches) set_gdbarch_ps_regnum (gdbarch, tdep->ppc_ps_regnum); =20 if (wordsize =3D=3D 8) - set_gdbarch_return_value (gdbarch, ppc64_sysv_abi_return_value); + { + set_gdbarch_return_value (gdbarch, ppc64_sysv_abi_return_value); + set_gdbarch_get_return_buf_addr (gdbarch, + ppc64_sysv_get_return_buf_addr); + } else set_gdbarch_return_value (gdbarch, ppc_sysv_abi_return_value); =20 diff --git a/gdb/testsuite/gdb.cp/non-trivial-retval.exp b/gdb/testsuite/gd= b.cp/non-trivial-retval.exp index 21c390bc937..93a3a6832f7 100644 --- a/gdb/testsuite/gdb.cp/non-trivial-retval.exp +++ b/gdb/testsuite/gdb.cp/non-trivial-retval.exp @@ -15,11 +15,18 @@ =20 # This file is part of the gdb testsuite =20 +set additional_flags "" + if {[skip_cplus_tests]} { continue } =20 standard_testfile .cc =20 -if {[prepare_for_testing "failed to prepare" $testfile $srcfile {debug c++= }]} { +if {[have_fvar_tracking]} { + set additional_flags "additional_flags=3D -fvar-tracking" +} + +if {[prepare_for_testing "failed to prepare" $testfile $srcfile [list debu= g c++ $additional_flags]]} { + return -1 } =20 diff --git a/gdb/testsuite/lib/gdb.exp b/gdb/testsuite/lib/gdb.exp index bdb8da9daf9..34c7a1cb50f 100644 --- a/gdb/testsuite/lib/gdb.exp +++ b/gdb/testsuite/lib/gdb.exp @@ -8821,6 +8821,14 @@ gdb_caching_proc have_fuse_ld_gold { return [gdb_simple_compile $me $src executable $flags] } =20 +# Return 1 if compiler supports fvar-tracking, otherwise return 0. +gdb_caching_proc have_fvar_tracking { + set me "have_fvar_tracking" + set flags "additional_flags=3D-fvar-tracking" + set src { int main() { return 0; } } + return [gdb_simple_compile $me $src executable $flags] +} + # Return 1 if linker supports -Ttext-segment, otherwise return 0. gdb_caching_proc linker_supports_Ttext_segment_flag { set me "linker_supports_Ttext_segment_flag"