Commit | Line | Data |
---|---|---|
b811d2c2 | 1 | # Copyright 2014-2020 Free Software Foundation, Inc. |
936d2992 PA |
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 the compare-sections command. | |
17 | ||
18 | standard_testfile | |
19 | ||
20 | if {[prepare_for_testing "failed to prepare" $testfile $srcfile {debug}]} { | |
21 | return -1 | |
22 | } | |
23 | ||
34dafe9f TV |
24 | set is_pie [exec_is_pie $binfile] |
25 | ||
936d2992 PA |
26 | # Run the compare-sections command along with any options as specified |
27 | # by OPTIONS, and check that no mismatch is found. | |
28 | proc compare_sections { {options ""} } { | |
29 | global gdb_prompt | |
30 | ||
31 | if {$options != ""} { | |
32 | set command "compare-sections $options" | |
33 | } else { | |
34 | set command "compare-sections" | |
35 | } | |
36 | set test $command | |
37 | gdb_test_multiple $command $test { | |
38 | -re "MIS-MATCHED.*$gdb_prompt $" { | |
39 | fail $test | |
40 | } | |
41 | -re "warning.*One or more sections.*does not match.*loaded file.*$gdb_prompt $" { | |
42 | fail $test | |
43 | } | |
44 | -re "Section.*matched.*$gdb_prompt $" { | |
45 | pass $test | |
46 | } | |
47 | } | |
48 | } | |
49 | ||
50 | gdb_file_cmd $binfile | |
51 | ||
52 | with_test_prefix "after file" { | |
53 | # Before starting the target, we're just comparing the | |
54 | # executable's sections against themselves... This should never | |
55 | # find a mismatch. | |
56 | compare_sections | |
57 | compare_sections "-r" | |
58 | } | |
59 | ||
60 | # Load the file into the target. | |
61 | gdb_reload | |
62 | ||
63 | with_test_prefix "after reload" { | |
64 | # We're presumabily still stopped at the entry point. All | |
65 | # sections should match. | |
66 | compare_sections | |
67 | compare_sections "-r" | |
68 | } | |
69 | ||
70 | # Try comparing just one section. Assume there's a .text section, | |
71 | # which is a pretty safe bet. | |
72 | set command "compare-sections .text" | |
73 | set command_re [string_to_regexp $command] | |
74 | set test $command | |
75 | gdb_test_multiple $command $test { | |
76 | -re "^$command_re\r\nSection .text, range $hex -- $hex. matched\.\r\n$gdb_prompt $" { | |
77 | pass $test | |
78 | } | |
79 | } | |
80 | ||
81 | # Now get past startup code. | |
82 | if ![runto_main] then { | |
bc6c7af4 | 83 | fail "can't run to main" |
936d2992 PA |
84 | return 0 |
85 | } | |
86 | ||
87 | with_test_prefix "after run to main" { | |
88 | # Assume all targets' startup code changes some loaded variable. | |
89 | gdb_test "compare-sections" \ | |
90 | "MIS-MATCHED.*warning.*One or more sections.*does not match.*loaded file" | |
91 | ||
34dafe9f TV |
92 | if { $is_pie == 1 } { |
93 | gdb_test "compare-sections -r" \ | |
94 | "MIS-MATCHED.*warning.*One or more sections.*does not match.*loaded file" | |
95 | } else { | |
96 | # Assume startup code doesn't change read-only sections. | |
97 | compare_sections "-r" | |
98 | } | |
936d2992 PA |
99 | } |
100 | ||
101 | # Now test that "compare-sections -r" works as expected. Look for an | |
102 | # address in a read-only section, patch it, and check that | |
103 | # "compare-sections -r" detects a mismatch. | |
104 | with_test_prefix "read-only" { | |
105 | set ro_address 0 | |
106 | set has_ro_sections 0 | |
107 | set test "list read-only sections" | |
108 | gdb_test_multiple "maint info sections READONLY" $test { | |
109 | -re "($hex)->$hex at $hex. \[^\r\n\]* READONLY.*$gdb_prompt $" { | |
110 | set ro_address $expect_out(1,string) | |
111 | set has_ro_sections 1 | |
112 | pass $test | |
113 | } | |
114 | -re "$gdb_prompt $" { | |
115 | pass $test | |
116 | } | |
117 | } | |
118 | ||
119 | if {!$has_ro_sections} { | |
120 | unsupported "no read-only sections" | |
121 | return -1; | |
122 | } | |
123 | ||
124 | set orig -1 | |
125 | ||
126 | set test "get value of read-only section" | |
df8899e5 | 127 | gdb_test_multiple "print /u *(unsigned char *) $ro_address" "$test" { |
936d2992 PA |
128 | -re " = (\[0-9\]*).*$gdb_prompt $" { |
129 | set orig $expect_out(1,string) | |
130 | pass "$test" | |
131 | } | |
132 | } | |
133 | ||
134 | if {$orig == -1} { | |
135 | untested "couldn't read address of read-only section" | |
136 | return -1 | |
137 | } | |
138 | ||
139 | # Come up with different value. | |
140 | set patch [expr 255 - $orig] | |
141 | ||
142 | # Write PATCH to memory. | |
143 | set written -1 | |
144 | set test "corrupt read-only section" | |
df8899e5 TP |
145 | gdb_test_multiple "print /u *(unsigned char *) $ro_address = $patch" "$test" { |
146 | -re " = .*Cannot access memory at address $ro_address.*$gdb_prompt $" { | |
147 | pass "$test (cannot write)" | |
148 | } | |
936d2992 PA |
149 | -re " = (\[0-9\]*).*$gdb_prompt $" { |
150 | set written $expect_out(1,string) | |
151 | pass "$test" | |
152 | } | |
153 | } | |
154 | ||
155 | if { $written != $patch } { | |
156 | unsupported "can't patch read-only section" | |
157 | return -1 | |
158 | } | |
159 | ||
160 | gdb_test "compare-sections -r" \ | |
161 | "MIS-MATCHED.*warning.*One or more sections.*does not match.*loaded file.*" | |
162 | } |