From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: by sourceware.org (Postfix, from userid 1726) id CCFFB3873CC0; Fri, 16 Dec 2022 13:52:00 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org CCFFB3873CC0 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=sourceware.org; s=default; t=1671198720; bh=bnyGcWw/X4WGYUJnvXi1Xec7IC+7VhoTKQk7v4iGikE=; h=From:To:Subject:Date:From; b=p1dBWazaG/8Nggrqn87JKQ5ufndNHSMCuBDslcHc8CGAKtAFg5ETECozH9OxV79YU cfng5HrTl1rIN4uViR/HSvgOUdyNie5Ot7JeVE8ZX/X34riIE5tenJvpG+ln1nX+KG r+wSsEv04LhUHJRYXvo8Mc9rsGslaDHMPZ0rsA1E= Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable From: Andrew Burgess To: gdb-cvs@sourceware.org Subject: [binutils-gdb] gdb: fix crash when getting the value of a label symbol X-Act-Checkin: binutils-gdb X-Git-Author: Andrew Burgess X-Git-Refname: refs/heads/master X-Git-Oldrev: e60a615dde5d6674a6488b74afe807a775551407 X-Git-Newrev: c3efaf0afd9d37004c42cdfd3ce0c1bfa979c45e Message-Id: <20221216135200.CCFFB3873CC0@sourceware.org> Date: Fri, 16 Dec 2022 13:52:00 +0000 (GMT) List-Id: https://sourceware.org/git/gitweb.cgi?p=3Dbinutils-gdb.git;h=3Dc3efaf0afd9d= 37004c42cdfd3ce0c1bfa979c45e commit c3efaf0afd9d37004c42cdfd3ce0c1bfa979c45e Author: Andrew Burgess Date: Mon Dec 12 14:05:22 2022 +0000 gdb: fix crash when getting the value of a label symbol =20 When the source program contains a goto label, it turns out it's actually pretty hard for a user to find out more about that label. For example: =20 (gdb) p some_label No symbol "some_label" in current context. (gdb) disassemble some_label No symbol "some_label" in current context. (gdb) x/10i some_label No symbol "some_label" in current context. (gdb) break some_label Breakpoint 2 at 0x401135: file /tmp/py-label-symbol-value.c, line 35. =20 In all cases, some_label is a goto label within the current frame. Only placing a breakpoint on the label worked. =20 This all seems a little strange to me, it feels like asking about a goto label would not be an unreasonable thing for a user to do. =20 This commit doesn't fix any of the above issues, I mention them just to provide a little context for why the following issue has probably not been seen before. =20 It turns out there is one way a user can access the symbol for a goto label, through the Python API: =20 python frame =3D gdb.selected_frame() python frame_pc =3D frame.pc() python block =3D gdb.current_progspace().block_for_pc(frame_pc) python symbol,_ =3D gdb.lookup_symbol('some_label', block, gdb.SYMBOL= _LABEL_DOMAIN) python print(str(symbol.value())) ../../src/gdb/findvar.c:204: internal-error: store_typed_address: Ass= ertion `type->is_pointer_or_reference ()' failed. =20 The problem is that label symbols are created using the builtin_core_addr type, which is a pure integer type. =20 When GDB tries to fetch the value of a label symbol then we end up in findvar.c, in the function language_defn::read_var_value, in the LOC_LABEL case. From here store_typed_address is called to store the address of the label into a value object with builtin_core_addr type. =20 The problem is that store_typed_address requires that the destination type be a pointer or reference, which the builtin_core_addr type is not. =20 Now it's not clear what type a goto label address should have, but GCC has an extension that allows users to take the address of a goto label (using &&), in that case the result is of type 'void *'. =20 I propose that when we convert the CORE_ADDR value to a GDB value object, we use builtin_func_ptr type instead of builtin_core_addr, this means the result will be of type 'void (*) ()'. The benefit of this approach is that when gdbarch_address_to_pointer is called the target type will be correctly identified as a pointer to code, which should mean any architecture specific adjustments are done correctly. =20 We can then cast the new value to 'void *' type with a call to value_cast_pointer, this should not change the values bit representation, but will just update the type. =20 After this asking for the value of a label symbol works just fine: =20 (gdb) python print(str(symbol.value())) 0x401135 =20 And the type is maybe what we'd expect: =20 (gdb) python print(str(symbol.value().type)) void * Diff: --- gdb/findvar.c | 40 ++++++++++++++----= ---- gdb/testsuite/gdb.python/py-label-symbol-value.c | 38 ++++++++++++++++++= ++ gdb/testsuite/gdb.python/py-label-symbol-value.exp | 39 ++++++++++++++++++= +++ 3 files changed, 103 insertions(+), 14 deletions(-) diff --git a/gdb/findvar.c b/gdb/findvar.c index e609358df08..a5e27035c15 100644 --- a/gdb/findvar.c +++ b/gdb/findvar.c @@ -593,20 +593,32 @@ language_defn::read_var_value (struct symbol *var, return v; =20 case LOC_LABEL: - /* Put the constant back in target format. */ - v =3D allocate_value (type); - if (overlay_debugging) - { - struct objfile *var_objfile =3D var->objfile (); - addr =3D symbol_overlayed_address (var->value_address (), - var->obj_section (var_objfile)); - store_typed_address (value_contents_raw (v).data (), type, addr); - } - else - store_typed_address (value_contents_raw (v).data (), type, - var->value_address ()); - VALUE_LVAL (v) =3D not_lval; - return v; + { + /* Put the constant back in target format. */ + if (overlay_debugging) + { + struct objfile *var_objfile =3D var->objfile (); + addr =3D symbol_overlayed_address (var->value_address (), + var->obj_section (var_objfile)); + } + else + addr =3D var->value_address (); + + /* First convert the CORE_ADDR to a function pointer type, this + ensures the gdbarch knows what type of pointer we are + manipulating when value_from_pointer is called. */ + type =3D builtin_type (var->arch ())->builtin_func_ptr; + v =3D value_from_pointer (type, addr); + + /* But we want to present the value as 'void *', so cast it to the + required type now, this will not change the values bit + representation. */ + struct type *void_ptr_type + =3D builtin_type (var->arch ())->builtin_data_ptr; + v =3D value_cast_pointers (void_ptr_type, v, 0); + VALUE_LVAL (v) =3D not_lval; + return v; + } =20 case LOC_CONST_BYTES: if (is_dynamic_type (type)) diff --git a/gdb/testsuite/gdb.python/py-label-symbol-value.c b/gdb/testsui= te/gdb.python/py-label-symbol-value.c new file mode 100644 index 00000000000..94bdae6fd30 --- /dev/null +++ b/gdb/testsuite/gdb.python/py-label-symbol-value.c @@ -0,0 +1,38 @@ +/* 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 . = */ + +volatile int global_var =3D 1; + +int +get_value () +{ + return global_var; +} + +int +main (void) +{ + int value =3D get_value (); + if (value > 0) + goto some_label; + + return 1; + + some_label: + + return 0; +} diff --git a/gdb/testsuite/gdb.python/py-label-symbol-value.exp b/gdb/tests= uite/gdb.python/py-label-symbol-value.exp new file mode 100644 index 00000000000..44321e5f71d --- /dev/null +++ b/gdb/testsuite/gdb.python/py-label-symbol-value.exp @@ -0,0 +1,39 @@ +# 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 . + +# Check that GDB handles the user asking for the value of a label +# symbol (i.e. a symbol for a goto label). + +load_lib gdb-python.exp +standard_testfile + +if { [prepare_for_testing "failed to prepare" ${testfile} ${srcfile}] } { + return -1 +} + +# Skip all tests if Python scripting is not enabled. +if { [skip_python_tests] } { continue } + +if ![runto_main] { + return -1 +} + +# Use Python to print the value of the 'some_label' symbol. +gdb_test "python frame =3D gdb.selected_frame()" +gdb_test "python frame_pc =3D frame.pc()" +gdb_test "python block =3D gdb.current_progspace().block_for_pc(frame_pc)" +gdb_test "python symbol,_ =3D gdb.lookup_symbol('some_label', block, gdb.S= YMBOL_LABEL_DOMAIN)" +gdb_test "python print(str(symbol.value()))" "$hex " +gdb_test "python print(str(symbol.value().type))" "void \\*"