1 # Copyright 2016-2024 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 # Test a C++ reference marked with DW_OP_GNU_implicit_pointer. 17 # The referenced value is a global variable whose location is a DW_OP_addr. 18 19 require allow_cplus_tests 20 21 load_lib dwarf.exp 22 23 # This test can only be run on targets which support DWARF-2 and use gas. 24 require dwarf2_support 25 26 # We'll place the output of Dwarf::assemble in implref-global.S. 27 standard_testfile .c .S 28 29 # ${testfile} is now "implref-global". srcfile2 is "implref-global.S". 30 set executable ${testfile} 31 set asm_file [standard_output_file ${srcfile2}] 32 33 # We need to know the size of integer and address types in order 34 # to write some of the debugging info we'd like to generate. 35 # 36 # For that, we ask GDB by debugging our implref-global program. 37 # Any program would do, but since we already have implref-global 38 # specifically for this testcase, might as well use that. 39 if [prepare_for_testing "failed to prepare" ${testfile} ${srcfile}] { 40 return -1 41 } 42 43 # Create the DWARF. We need a regular variable and a reference to it that'll 44 # be marked with DW_OP_GNU_implicit_pointer. The variable must be global so 45 # that its name is an exported symbol that we can reference from the DWARF 46 # using gdb_target_symbol. 47 Dwarf::assemble ${asm_file} { 48 cu {} { 49 DW_TAG_compile_unit { 50 {DW_AT_language @DW_LANG_C_plus_plus} 51 } { 52 declare_labels int_label variable_label ref_label 53 set int_size [get_sizeof "int" -1] 54 55 # gdb always assumes references are implemented as pointers. 56 set addr_size [get_sizeof "void *" -1] 57 58 int_label: DW_TAG_base_type { 59 {DW_AT_byte_size ${int_size} DW_FORM_udata} 60 {DW_AT_encoding @DW_ATE_signed} 61 {DW_AT_name "int"} 62 } 63 64 ref_label: DW_TAG_reference_type { 65 {DW_AT_byte_size ${addr_size} DW_FORM_udata} 66 {DW_AT_type :${int_label}} 67 } 68 69 variable_label: DW_TAG_variable { 70 {DW_AT_name "global_var"} 71 {DW_AT_type :${int_label}} 72 {DW_AT_external 1 DW_FORM_flag} 73 {DW_AT_location {DW_OP_addr [gdb_target_symbol "global_var"]} SPECIAL_expr} 74 } 75 76 DW_TAG_subprogram { 77 {MACRO_AT_func { "main" }} 78 {DW_AT_type :${int_label}} 79 {DW_AT_external 1 DW_FORM_flag} 80 } { 81 DW_TAG_variable { 82 {DW_AT_name "ref"} 83 {DW_AT_type :${ref_label}} 84 {DW_AT_location {DW_OP_GNU_implicit_pointer ${variable_label} 0} SPECIAL_expr} 85 } 86 } 87 } 88 } 89 } 90 91 if [prepare_for_testing "failed to prepare" ${executable} [list ${asm_file} ${srcfile}] {}] { 92 return -1 93 } 94 95 # DW_OP_GNU_implicit_pointer implementation requires a valid frame. 96 if ![runto_main] { 97 return -1 98 } 99 100 # Address of the referenced value. 101 set address [get_hexadecimal_valueof "&global_var" ""] 102 103 # Doing 'print ref' should show us e.g. '(int &) @0xdeadbeef: 42'. 104 gdb_test "print ref" " = \\(int &\\) @${address}: \\\d+" 105 106 # Doing 'print &ref' should show us e.g. '(int *) 0xdeadbeef <global_var>'. 107 gdb_test "print &ref" " = \\(int \\*\\) ${address} <global_var>" 108 109 # gdb assumes C++ references are implemented as pointers, and print &(&ref) 110 # shows us the underlying pointer's address. Since in this case there's no 111 # physical pointer, gdb should tell us so. 112 gdb_test "print &(&ref)" "Attempt to take address of value not located in memory." 113 114 # Test assignment through the synthetic reference. 115 set new_value 10 116 gdb_test_no_output "set (ref = ${new_value})" 117 gdb_test "print ref" " = \\(int &\\) @${address}: ${new_value}" "print ref after assignment" 118 gdb_test "print global_var" " = ${new_value}" "print global_var after assignment" 119