aboutsummaryrefslogtreecommitdiff
path: root/gdb/testsuite/gdb.base/compare-sections.exp
blob: ba683c1f1c28c5777be4248ccc84c9a50359ef60 (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
# Copyright 2014-2021 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/>.

# Test the compare-sections command.

standard_testfile

if {[prepare_for_testing "failed to prepare" $testfile $srcfile {debug}]} {
    return -1
}

set is_pie [exec_is_pie $binfile]

# Run the compare-sections command along with any options as specified
# by OPTIONS, and check that no mismatch is found.
proc compare_sections { {options ""} } {
    global gdb_prompt

    if {$options != ""} {
	set command "compare-sections $options"
    } else {
	set command "compare-sections"
    }
    set test $command
    gdb_test_multiple $command $test {
	-re "MIS-MATCHED.*$gdb_prompt $" {
	    fail $test
	}
	-re "warning.*One or more sections.*does not match.*loaded file.*$gdb_prompt $" {
	    fail $test
	}
	-re "Section.*matched.*$gdb_prompt $" {
	    pass $test
	}
    }
}

gdb_file_cmd $binfile

with_test_prefix "after file" {
    # Before starting the target, we're just comparing the
    # executable's sections against themselves...  This should never
    # find a mismatch.
    compare_sections
    compare_sections "-r"
}

# Load the file into the target.
gdb_reload

with_test_prefix "after reload" {
    # We're presumabily still stopped at the entry point.  All
    # sections should match.
    compare_sections
    compare_sections "-r"
}

# Try comparing just one section.  Assume there's a .text section,
# which is a pretty safe bet.
set command "compare-sections .text"
set command_re [string_to_regexp $command]
set test $command
gdb_test_multiple $command $test {
    -re "^$command_re\r\nSection .text, range $hex -- $hex. matched\.\r\n$gdb_prompt $" {
	pass $test
    }
}

# Now get past startup code.
if ![runto_main] then {
    return 0
}

with_test_prefix "after run to main" {
    # Assume all targets' startup code changes some loaded variable.
    gdb_test "compare-sections" \
	"MIS-MATCHED.*warning.*One or more sections.*does not match.*loaded file"

    if { $is_pie == 1 } {
	gdb_test "compare-sections -r" \
	    "MIS-MATCHED.*warning.*One or more sections.*does not match.*loaded file"
    } else {
	# Assume startup code doesn't change read-only sections.
	compare_sections "-r"
    }
}

# Now test that "compare-sections -r" works as expected.  Look for an
# address in a read-only section, patch it, and check that
# "compare-sections -r" detects a mismatch.
with_test_prefix "read-only" {
    set ro_address 0
    set has_ro_sections 0
    set test "list read-only sections"
    gdb_test_multiple "maint info sections READONLY" $test {
	-re "($hex)->$hex at $hex. \[^\r\n\]* READONLY.*$gdb_prompt $" {
	    set ro_address $expect_out(1,string)
	    set has_ro_sections 1
	    pass $test
	}
	-re "$gdb_prompt $" {
	    pass $test
	}
    }

    if {!$has_ro_sections} {
	unsupported "no read-only sections"
	return -1;
    }

    set orig -1

    set test "get value of read-only section"
    gdb_test_multiple "print /u *(unsigned char *) $ro_address" "$test" {
	-re " = (\[0-9\]*).*$gdb_prompt $" {
	    set orig $expect_out(1,string)
	    pass "$test"
	}
    }

    if {$orig == -1} {
	untested "couldn't read address of read-only section"
	return -1
    }

    # Come up with different value.
    set patch [expr 255 - $orig]

    # Write PATCH to memory.
    set written -1
    set test "corrupt read-only section"
    gdb_test_multiple "print /u *(unsigned char *) $ro_address = $patch" "$test" {
	-re " = .*Cannot access memory at address $ro_address.*$gdb_prompt $" {
	    pass "$test (cannot write)"
	}
	-re " = (\[0-9\]*).*$gdb_prompt $" {
	    set written $expect_out(1,string)
	    pass "$test"
	}
    }

    if { $written != $patch } {
	unsupported "can't patch read-only section"
	return -1
    }

    gdb_test "compare-sections -r" \
	"MIS-MATCHED.*warning.*One or more sections.*does not match.*loaded file.*"
}