1# Copyright 2019-2020 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# Import a CU into an "artificial" CU.  For each DW_TAG DIE in the
17# artificial CU, use DW_AT_abstract_origin to refer to a DIE in the
18# imported CU.  This DWARF file organization is frequently found in
19# programs compiled with -flto (and -g) using GCC.
20#
21# This test reproduces the bug described in BZ 25065 without relying
22# on specific compiler versions or use of optimization switches, in
23# this case -flto.
24
25if [skip_cplus_tests] {
26    continue
27}
28
29load_lib dwarf.exp
30
31# This test can only be run on targets which support DWARF-2 and use gas.
32if {![dwarf2_support]} {
33    return 0
34};
35
36standard_testfile .c .S
37
38# ${testfile} is now "implref-struct".  srcfile2 is "implref-struct.S".
39set executable ${testfile}
40set asm_file [standard_output_file ${srcfile2}]
41
42# We need to know the size of integer and address types in order
43# to write some of the debugging info we'd like to generate.
44if [prepare_for_testing "failed to prepare" ${testfile} ${srcfile} {debug c++}] {
45    return -1
46}
47
48# Create the DWARF.
49Dwarf::assemble $asm_file {
50    declare_labels cu_label main_label doit_label int_label
51    declare_labels Foo_label Foo_pointer_type doit_self_label
52    declare_labels foo_label Foo_destructor_obj_pointer_label
53    declare_labels Foo_constructor_obj_pointer_label
54    set int_size [get_sizeof "int" 4]
55    set addr_size [get_sizeof "void *" 8]
56
57    cu {} {
58	compile_unit {
59	    {language @DW_LANG_C_plus_plus}
60	    {name "<artificial>"}
61	} {
62	    imported_unit {
63		{import %$cu_label}
64	    }
65	    subprogram {
66		{abstract_origin %$main_label}
67		{MACRO_AT_range {main}}
68	    } {
69		subprogram {
70		    {abstract_origin %$doit_label}
71		    {MACRO_AT_range {doit}}
72		} {
73		    formal_parameter {
74			{abstract_origin %$doit_self_label}
75		    }
76		}
77	        DW_TAG_variable {
78		    {abstract_origin %$foo_label}
79		    {location 4 data1}
80		}
81	    }
82	}
83    }
84
85    cu {} {
86	cu_label: compile_unit {
87	    {language @DW_LANG_C_plus_plus}
88	    {name "imported_unit.c"}
89	} {
90	    int_label: base_type {
91		{byte_size $int_size sdata}
92		{encoding @DW_ATE_signed}
93		{name int}
94	    }
95
96	    main_label: subprogram {
97		{name main}
98		{type :$int_label}
99		{external 1 flag}
100	    } {
101		Foo_label: class_type {
102		    {name Foo}
103		    {byte_size 1 sdata}
104		} {
105		    doit_label: subprogram {
106			{name doit}
107			{type :$int_label}
108			{accessibility 1 DW_FORM_data1}
109		    } {
110			doit_self_label: formal_parameter {
111			    {name this}
112			    {artificial 1 DW_FORM_flag_present}
113			    {type :$Foo_pointer_type}
114			}
115		    }
116		    Foo_pointer_type: pointer_type {
117			{byte_size $addr_size sdata}
118			{type :$Foo_label}
119		    }
120		}
121		foo_label: DW_TAG_variable {
122		    {name foo}
123		    {type :$Foo_label}
124		}
125	    }
126	}
127    }
128}
129
130if { [prepare_for_testing "failed to prepare" ${testfile} \
131	  [list $srcfile $asm_file] {nodebug}] } {
132    return -1
133}
134
135gdb_test_no_output "set language c++"
136
137set psymtabs_p [psymtabs_p]
138
139# Verify that the partial symtab for the unit importing the partial unit does
140# not contain the static partial symbol int, which is defined in the partial
141# unit.  Test-case for PR25646.
142set test "no static partial symbols in importing unit"
143if { $psymtabs_p } {
144    gdb_test "main print psymbols" \
145	[multi_line \
146	     "  Depends on 1 other partial symtabs\." \
147	     "\[^\r\n\]*" \
148	     "  Global partial symbols:" \
149	     "    `main', function, $hex" \
150	     "" \
151	     ".*"] \
152	$test
153} else {
154    unsupported $test
155}
156
157# Verify that there's only one partial symtab for imported_unit.c.  Test-case
158# for PR25700.
159set test "no duplicate psymtab for imported_unit.c"
160if { $psymtabs_p } {
161    set line "Partial symtab for source file imported_unit.c"
162    gdb_test_multiple "maint print psymbols" $test {
163	-re -wrap "$line.*$line.*" {
164	    fail $gdb_test_name
165	}
166	-re -wrap "$line.*" {
167	    pass $gdb_test_name
168	}
169    }
170} else {
171    unsupported $test
172}
173
174gdb_test "l imported_unit.c:1" \
175    "1\timported_unit.c: No such file or directory\."
176
177gdb_test "info source" "\r\nCurrent source file is imported_unit.c\r\n.*" \
178    "info source for imported_unit.c"
179
180# Sanity check
181gdb_test "ptype main" "= int \\(void\\)"
182
183# Each of these tests caused a segfault prior to fixing BZ 25065.
184gdb_test "ptype main::Foo" "= class Foo \{\\s+public:\\s+int doit\\(void\\);\\s+\}"
185gdb_test "ptype main::foo" "= class Foo \{\\s+public:\\s+int doit\\(void\\);\\s+\}"
186