1 # Copyright
2014-2023 Free Software Foundation
, Inc.
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.
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.
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
/>.
16 # Test the compare
-sections command.
20 if {[prepare_for_testing
"failed to prepare" $testfile $srcfile {debug}]} {
24 set is_pie
[exec_is_pie $binfile
]
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
""} } {
32 set command
"compare-sections $options"
34 set command
"compare-sections"
37 gdb_test_multiple $command $test
{
38 -re
"MIS-MATCHED.*$gdb_prompt $" {
41 -re
"warning: One or more sections.*does not match.*loaded file.*$gdb_prompt $" {
44 -re
"Section.*matched.*$gdb_prompt $" {
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
60 #
Load the file into the target.
63 with_test_prefix
"after reload" {
64 # We
're presumabily still stopped at the entry point. All
65 # sections should match.
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
]
75 gdb_test_multiple $command $test
{
76 -re
"^$command_re\r\nSection .text, range $hex -- $hex. matched\.\r\n$gdb_prompt $" {
81 # Now
get past startup code.
86 with_test_prefix
"after run to main" {
87 # Assume all targets
' startup code changes some loaded variable.
88 gdb_test "compare-sections" \
89 "MIS-MATCHED.*warning.*One or more sections.*does not match.*loaded file"
92 gdb_test "compare-sections -r" \
93 "MIS-MATCHED.*warning.*One or more sections.*does not match.*loaded file"
95 # Assume startup code doesn't change read
-only sections.
100 # Now test that
"compare-sections -r" works as expected. Look for an
101 # address in a read
-only section
, patch it
, and check that
102 #
"compare-sections -r" detects a mismatch.
103 with_test_prefix
"read-only" {
105 set has_ro_sections
0
106 set test
"list read-only sections"
107 gdb_test_multiple
"maint info sections READONLY" $test {
108 -re
"($hex)->$hex at $hex. \[^\r\n\]* READONLY.*$gdb_prompt $" {
109 set ro_address $expect_out
(1,string
)
110 set has_ro_sections
1
113 -re
"$gdb_prompt $" {
118 if {!$has_ro_sections
} {
119 unsupported
"no read-only sections"
125 set test
"get value of read-only section"
126 gdb_test_multiple
"print /u *(unsigned char *) $ro_address" "$test" {
127 -re
" = (\[0-9\]*).*$gdb_prompt $" {
128 set orig $expect_out
(1,string
)
134 untested
"couldn't read address of read-only section"
138 # Come up with different value.
139 set patch
[expr
255 - $orig
]
141 # Write PATCH to memory.
143 set test
"corrupt read-only section"
144 gdb_test_multiple
"print /u *(unsigned char *) $ro_address = $patch" "$test" {
145 -re
" = .*Cannot access memory at address $ro_address.*$gdb_prompt $" {
146 pass
"$test (cannot write)"
148 -re
" = (\[0-9\]*).*$gdb_prompt $" {
149 set written $expect_out
(1,string
)
154 if { $written
!= $patch
} {
155 unsupported
"can't patch read-only section"
159 gdb_test
"compare-sections -r" \
160 "MIS-MATCHED.*warning.*One or more sections.*does not match.*loaded file.*"