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