Add ability to change linker warning messages into errors when reporting executable...
[binutils-gdb.git] / ld / testsuite / ld-scripts / crossref.exp
1 # Test NOCROSSREFS in a linker script.
2 # By Ian Lance Taylor, Cygnus Support.
3 # Copyright (C) 2000-2023 Free Software Foundation, Inc.
4 #
5 # This file is part of the GNU Binutils.
6 #
7 # This program is free software; you can redistribute it and/or modify
8 # it under the terms of the GNU General Public License as published by
9 # the Free Software Foundation; either version 3 of the License, or
10 # (at your option) any later version.
11 #
12 # This program is distributed in the hope that it will be useful,
13 # but WITHOUT ANY WARRANTY; without even the implied warranty of
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 # GNU General Public License for more details.
16 #
17 # You should have received a copy of the GNU General Public License
18 # along with this program; if not, write to the Free Software
19 # Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston,
20 # MA 02110-1301, USA.
21
22 set test1 "NOCROSSREFS 1"
23 set test2 "NOCROSSREFS 2"
24 set test3 "NOCROSSREFS 3"
25 set test4 "NOCROSSREFS_TO 1"
26 set test5 "NOCROSSREFS_TO 2"
27 set test6 "NOCROSSREFS_TO 3"
28 set test7 "NOCROSSREFS_TO 4"
29
30 if { ![check_compiler_available] } {
31 untested $test1
32 untested $test2
33 untested $test3
34 untested $test4
35 untested $test5
36 untested $test6
37 untested $test7
38 return
39 }
40
41 # Pass -fplt to CC since -fno-plt doesn't work with NOCROSSREFS tests.
42 # Also add $NOPIE_CFLAGS since PIE doesn't work NOCROSSREFS tests.
43 set old_CFLAGS "$CFLAGS_FOR_TARGET"
44 append CFLAGS_FOR_TARGET " $PLT_CFLAGS $NOPIE_CFLAGS"
45
46 # Xtensa targets currently default to putting literal values in a separate
47 # section and that requires linker script support, so put literals in text.
48 if [istarget xtensa*-*-*] {
49 append CFLAGS_FOR_TARGET " -mtext-section-literals"
50 }
51
52 # Prevent the use of the MeP's small data area which references a symbol
53 # called __sdabase which will not be defined by our test linker scripts.
54 if [istarget mep*-*-elf] {
55 append CFLAGS_FOR_TARGET " -mtiny=0"
56 }
57
58 # The .dsbt section and __c6xabi_DSBT_BASE are not defined in our test
59 # linker scripts.
60 if [istarget tic6x*-*-*] {
61 append CFLAGS_FOR_TARGET " -mno-dsbt -msdata=none"
62 }
63
64 if { ![ld_compile "$CC_FOR_TARGET $NOSANITIZE_CFLAGS $NOLTO_CFLAGS" "$srcdir/$subdir/cross1.c" tmpdir/cross1.o] \
65 || ![ld_compile "$CC_FOR_TARGET $NOSANITIZE_CFLAGS $NOLTO_CFLAGS" "$srcdir/$subdir/cross2.c" tmpdir/cross2.o] } {
66 unsupported $test1
67 unsupported $test2
68 set CFLAGS_FOR_TARGET "$old_CFLAGS"
69 return
70 }
71
72 set flags [big_or_little_endian]
73
74 # arc-elf32 requires the symbol __SDATA_BEGIN__ to always be present.
75 if [istarget arc*-*-elf32] {
76 append flags " --defsym __SDATA_BEGIN__=0"
77 }
78
79 if [is_pecoff_format] {
80 append flags " --image-base 0"
81 }
82
83 set exec_output [run_host_cmd "$ld" "$flags -o tmpdir/cross1 -T $srcdir/$subdir/cross1.t tmpdir/cross1.o tmpdir/cross2.o --no-error-rwx-segments"]
84
85 set exec_output [prune_warnings $exec_output]
86
87 regsub -all "(^|\n)($ld: warning: cannot find entry symbol\[^\n\]*\n?)" $exec_output "\\1" exec_output
88
89 if [string match "" $exec_output] then {
90 fail $test1
91 } else {
92 verbose -log "$exec_output"
93 if [regexp "prohibited cross reference from .* to `.*foo' in" $exec_output] {
94 pass $test1
95 } else {
96 fail $test1
97 }
98 }
99
100 # Check cross references within a single object.
101
102 if { ![ld_compile "$CC_FOR_TARGET $NOSANITIZE_CFLAGS $NOLTO_CFLAGS" "$srcdir/$subdir/cross3.c" tmpdir/cross3.o] } {
103 unsupported $test2
104 set CFLAGS_FOR_TARGET "$old_CFLAGS"
105 return
106 }
107
108 set exec_output [run_host_cmd "$ld" "$flags -o tmpdir/cross2 -T $srcdir/$subdir/cross2.t tmpdir/cross3.o --no-error-rwx-segments"]
109 set exec_output [prune_warnings $exec_output]
110
111 regsub -all "(^|\n)($ld: warning: cannot find entry symbol\[^\n\]*\n?)" $exec_output "\\1" exec_output
112
113 if [string match "" $exec_output] then {
114 fail $test2
115 } else {
116 verbose -log "$exec_output"
117 if [regexp "prohibited cross reference from .* to `.*' in" $exec_output] {
118 pass $test2
119 } else {
120 fail $test2
121 }
122 }
123
124 # Check cross references for ld -r
125
126 if { ![ld_compile "$CC_FOR_TARGET $NOSANITIZE_CFLAGS $NOLTO_CFLAGS" "$srcdir/$subdir/cross4.c" tmpdir/cross4.o] } {
127 unsupported $test3
128 set CFLAGS_FOR_TARGET "$old_CFLAGS"
129 return
130 }
131
132 if ![ld_relocate $ld tmpdir/cross3-partial.o "tmpdir/cross1.o tmpdir/cross4.o"] {
133 fail $test3
134 set CFLAGS_FOR_TARGET "$old_CFLAGS"
135 return
136 }
137
138 set exec_output [run_host_cmd "$ld" "$flags -o tmpdir/cross3 -T $srcdir/$subdir/cross3.t tmpdir/cross3-partial.o tmpdir/cross2.o --no-error-rwx-segments"]
139
140 set exec_output [prune_warnings $exec_output]
141
142 regsub -all "(^|\n)($ld: warning: cannot find entry symbol\[^\n\]*\n?)" $exec_output "\\1" exec_output
143
144 if [string match "" $exec_output] then {
145 pass $test3
146 } else {
147 verbose -log "$exec_output"
148 fail $test3
149 }
150
151 set exec_output [run_host_cmd "$ld" "$flags -o tmpdir/cross4 -T $srcdir/$subdir/cross4.t tmpdir/cross4.o --no-error-rwx-segments"]
152 set exec_output [prune_warnings $exec_output]
153
154 regsub -all "(^|\n)($ld: warning: cannot find entry symbol\[^\n\]*\n?)" $exec_output "\\1" exec_output
155
156 if [string match "" $exec_output] then {
157 pass $test4
158 } else {
159 verbose -log "$exec_output"
160 fail $test4
161 }
162
163 set exec_output [run_host_cmd "$ld" "$flags -o tmpdir/cross5 -T $srcdir/$subdir/cross5.t tmpdir/cross4.o --no-error-rwx-segments"]
164 set exec_output [prune_warnings $exec_output]
165
166 regsub -all "(^|\n)($ld: warning: cannot find entry symbol\[^\n\]*\n?)" $exec_output "\\1" exec_output
167
168 if [string match "" $exec_output] then {
169 fail $test5
170 } else {
171 verbose -log "$exec_output"
172 if [regexp "prohibited cross reference from .* to `.*' in" $exec_output] {
173 pass $test5
174 } else {
175 fail $test5
176 }
177 }
178
179 set exec_output [run_host_cmd "$ld" "$flags -o tmpdir/cross6 -T $srcdir/$subdir/cross6.t tmpdir/cross3.o --no-error-rwx-segments"]
180 set exec_output [prune_warnings $exec_output]
181
182 regsub -all "(^|\n)($ld: warning: cannot find entry symbol\[^\n\]*\n?)" $exec_output "\\1" exec_output
183
184 if [string match "" $exec_output] then {
185 pass $test6
186 } else {
187 verbose -log "$exec_output"
188 fail $test6
189 }
190
191 set exec_output [run_host_cmd "$ld" "$flags -o tmpdir/cross7 -T $srcdir/$subdir/cross7.t tmpdir/cross3.o --no-error-rwx-segments"]
192 set exec_output [prune_warnings $exec_output]
193
194 regsub -all "(^|\n)($ld: warning: cannot find entry symbol\[^\n\]*\n?)" $exec_output "\\1" exec_output
195
196 if [string match "" $exec_output] then {
197 fail $test7
198 } else {
199 verbose -log "$exec_output"
200 if [regexp "prohibited cross reference from .* to `.*' in" $exec_output] {
201 pass $test7
202 } else {
203 fail $test7
204 }
205 }
206
207 set CFLAGS_FOR_TARGET "$old_CFLAGS"