aboutsummaryrefslogtreecommitdiff
path: root/gdb/testsuite/gdb.dwarf2/dw2-bad-elf.exp
blob: f6fe54a634f508cc1235a240d31c15e2c4d3914c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
# Copyright 2019-2020 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 <http://www.gnu.org/licenses/>.

# Checks for a bug where a baddly formed ELF would cause GDB to crash.
# A section containing executable code, for which there was DWARF is
# accidentally marked as non-alloctable, GDB becomes unhappy.
#
# This test creates some fake DWARF pointing at some symbols in a
# non-allocatable section that is still marked as executable.  We then
# start GDB and try to place a breakpoint on the symbol in the
# non-allocatable section.
#
# It is not expected that the final debug experience really makes
# sense, the symbol is in a non-allocatable section after all, but GDB
# absolutely shouldn't crash.  All we try to do after placing the
# breakpoint is check that GDB is still alive.

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 dw2-bad-elf.c dw2-bad-elf-other.S dw2-bad-elf-dwarf.S

# Make some DWARF for the test.
set asm_file [standard_output_file $srcfile3]
Dwarf::assemble $asm_file {
    global srcdir subdir srcfile

    declare_labels ranges_label_1 ranges_label_2 L1 L2

    set main_result [function_range main ${srcdir}/${subdir}/${srcfile}]
    set main_start [lindex $main_result 0]
    set main_length [lindex $main_result 1]

    set int_size [get_sizeof "int" 4]

    cu {} {
	DW_TAG_compile_unit {
	    {DW_AT_language @DW_LANG_C}
	    {DW_AT_name     dw2-bad-elf.c}
	    {DW_AT_comp_dir ${srcdir}/${subdir}}
	    {stmt_list $L1 DW_FORM_sec_offset}
	    {ranges ${ranges_label_1} DW_FORM_sec_offset}
	    {DW_AT_low_pc   0 addr}
	} {
	    declare_labels integer_label

	    DW_TAG_subprogram {
		{name main}
		{low_pc $main_start addr}
		{high_pc $main_length data8}
		{DW_AT_type :$integer_label}
		{DW_AT_decl_file 1 data1}
		{DW_AT_decl_line 10 data1}
	    }

	    integer_label: DW_TAG_base_type {
		{DW_AT_byte_size $int_size DW_FORM_sdata}
		{DW_AT_encoding  @DW_ATE_signed}
		{DW_AT_name      integer}
	    }
	}
    }

    cu {} {
	DW_TAG_compile_unit {
	    {DW_AT_language @DW_LANG_C}
	    {DW_AT_name     dw2-bad-elf-other.c}
	    {DW_AT_comp_dir ${srcdir}/${subdir}}
	    {stmt_list $L2 DW_FORM_sec_offset}
	    {ranges ${ranges_label_2} DW_FORM_sec_offset}
	    {DW_AT_low_pc   0 addr}
	} {
	    declare_labels integer_label

	    DW_TAG_subprogram {
		{name some_func}
		{low_pc some_func addr}
		{high_pc some_func_end addr}
		{DW_AT_type :$integer_label}
		{DW_AT_decl_file 2 data1}
		{DW_AT_decl_line 5 data1}
	    }

	    integer_label: DW_TAG_base_type {
		{DW_AT_byte_size $int_size DW_FORM_sdata}
		{DW_AT_encoding  @DW_ATE_signed}
		{DW_AT_name      integer}
	    }
	}
    }

    ranges {is_64 [is_64_target]} {
	ranges_label_1: sequence {
	    {base [lindex $main_result 0]}
	    {range 0 [lindex $main_result 1]}
	}
	ranges_label_2: sequence {
	    {base some_func}
	    {range 0 64}
	}
    }

    lines {version 2} L1 {
	include_dir "${srcdir}/${subdir}"
	file_name "$srcfile" 1

	# Line data doens't need to be correct, just present.
	program {
	    {DW_LNE_set_address [lindex $main_result 0]}
	    {DW_LNS_advance_line 10}
	    {DW_LNS_copy}
	    {DW_LNS_advance_pc [lindex $main_result 1]}
	    {DW_LNS_advance_line 19}
	    {DW_LNS_copy}
	    {DW_LNE_end_sequence}
	}
    }

    lines {version 2} L2 {
	include_dir "${srcdir}/${subdir}"
	file_name "dw2-bad-elf-other.c" 1

	# Line data doens't need to be correct, just present.
	program {
	    {DW_LNE_set_address some_func}
	    {DW_LNS_advance_line 5}
	    {DW_LNS_copy}
	    {DW_LNS_advance_pc 64}
	    {DW_LNS_advance_line 8}
	    {DW_LNS_copy}
	    {DW_LNE_end_sequence}
	}
    }
}

if { [build_executable ${testfile}.exp ${testfile} \
	  [list $srcfile $srcfile2 $asm_file] {nodebug}] } {
    return -1
}

# Attempt to place a breakpoint on 'some_func', then check GDB is
# still alive.  This test can optionally set a breakpoint on 'main'
# first (based on GOTO_MAIN), the original bug behaved differently
# when there was already a breakpoint set.
proc run_test { goto_main } {
    global binfile decimal hex

    clean_restart ${binfile}

    if { $goto_main } {
	if ![runto_main] {
	    return -1
	}
    }

    # Place a breakpoint.
    gdb_test "break some_func" \
	"Breakpoint $decimal at $hex: file .*dw2-bad-elf-other\\.c, line 6\\."

    # Check GDB is still alive.
    gdb_test "echo hello\\n" "hello"
}

# Run the tests.
foreach_with_prefix goto_main { 0 1 } {
    run_test $goto_main
}