9aa216ea227c8c8fcd382d99c4c77eadf93c0363
[external/binutils.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-2019 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 { ![is_remote host] && [which $CC] == 0 } {
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 global CFLAGS
42 set old_CFLAGS "$CFLAGS"
43
44 # Pass -fplt to CC since -fno-plt doesn't work with NOCROSSREFS tests.
45 # Also add $NOPIE_CFLAGS since PIE doesn't work NOCROSSREFS tests.
46 global PLT_CFLAGS NOPIE_CFLAGS
47 set old_CC "$CC"
48 set CC "$CC $PLT_CFLAGS $NOPIE_CFLAGS"
49
50 # Xtensa targets currently default to putting literal values in a separate
51 # section and that requires linker script support, so put literals in text.
52 if [istarget xtensa*-*-*] {
53     set CFLAGS "$CFLAGS -mtext-section-literals"
54 }
55
56 # Prevent the use of the MeP's small data area which references a symbol
57 # called __sdabase which will not be defined by our test linker scripts.
58 if [istarget mep*-*-elf] {
59     set CFLAGS "-mtiny=0"
60 }
61
62 # The .dsbt section and __c6xabi_DSBT_BASE are not defined in our test
63 # linker scripts.
64 if [istarget tic6x*-*-*] {
65     set CFLAGS "-mno-dsbt -msdata=none"
66 }
67
68 if { ![ld_compile $CC "$srcdir/$subdir/cross1.c" tmpdir/cross1.o] \
69      || ![ld_compile $CC "$srcdir/$subdir/cross2.c" tmpdir/cross2.o] } {
70     unresolved $test1
71     unresolved $test2
72     set CFLAGS "$old_CFLAGS"
73     set CC "$old_CC"
74     return
75 }
76
77 set flags [big_or_little_endian]
78
79 # arc-elf32 requires the symbol __SDATA_BEGIN__ to always be present.
80 if [istarget arc*-*-elf32] {
81     set flags "$flags --defsym=__SDATA_BEGIN__=0"
82 }
83
84 # IA64 has both ordered and unordered sections in an input file.
85 setup_xfail ia64-*-*
86
87 set exec_output [run_host_cmd "$ld" "$flags -o tmpdir/cross1 -T $srcdir/$subdir/cross1.t tmpdir/cross1.o tmpdir/cross2.o"]
88
89 set exec_output [prune_warnings $exec_output]
90
91 regsub -all "(^|\n)($ld: warning: cannot find entry symbol\[^\n\]*\n?)" $exec_output "\\1" exec_output
92
93 if [string match "" $exec_output] then {
94     fail $test1
95 } else {
96     verbose -log "$exec_output"
97     if [regexp "prohibited cross reference from .* to `.*foo' in" $exec_output] {
98         pass $test1
99     } else {
100         fail $test1
101     }
102 }
103
104 # Check cross references within a single object.
105
106 if { ![ld_compile $CC "$srcdir/$subdir/cross3.c" tmpdir/cross3.o] } {
107     unresolved $test2
108     set CFLAGS "$old_CFLAGS"
109     set CC "$old_CC"
110     return
111 }
112
113 set exec_output [run_host_cmd "$ld" "$flags -o tmpdir/cross2 -T $srcdir/$subdir/cross2.t tmpdir/cross3.o"]
114 set exec_output [prune_warnings $exec_output]
115
116 regsub -all "(^|\n)($ld: warning: cannot find entry symbol\[^\n\]*\n?)" $exec_output "\\1" exec_output
117
118 if [string match "" $exec_output] then {
119     fail $test2
120 } else {
121     verbose -log "$exec_output"
122     if [regexp "prohibited cross reference from .* to `.*' in" $exec_output] {
123         pass $test2
124     } else {
125         fail $test2
126     }
127 }
128
129 # Check cross references for ld -r
130
131 if { ![ld_compile $CC "$srcdir/$subdir/cross4.c" tmpdir/cross4.o] } {
132     unresolved $test3
133     set CFLAGS "$old_CFLAGS"
134     set CC "$old_CC"
135     return
136 }
137
138 if ![ld_relocate $ld tmpdir/cross3-partial.o "tmpdir/cross1.o tmpdir/cross4.o"] {
139     unresolved $test3
140     set CFLAGS "$old_CFLAGS"
141     set CC "$old_CC"
142     return
143 }
144
145 set exec_output [run_host_cmd "$ld" "$flags -o tmpdir/cross3 -T $srcdir/$subdir/cross3.t tmpdir/cross3-partial.o tmpdir/cross2.o"]
146
147 set exec_output [prune_warnings $exec_output]
148
149 regsub -all "(^|\n)($ld: warning: cannot find entry symbol\[^\n\]*\n?)" $exec_output "\\1" exec_output
150
151 if [string match "" $exec_output] then {
152     pass $test3
153 } else {
154     verbose -log "$exec_output"
155     fail $test3
156 }
157
158 set exec_output [run_host_cmd "$ld" "$flags -o tmpdir/cross4 -T $srcdir/$subdir/cross4.t tmpdir/cross4.o"]
159 set exec_output [prune_warnings $exec_output]
160
161 regsub -all "(^|\n)($ld: warning: cannot find entry symbol\[^\n\]*\n?)" $exec_output "\\1" exec_output
162
163 if [string match "" $exec_output] then {
164     pass $test4
165 } else {
166     verbose -log "$exec_output"
167     fail $test4
168 }
169
170 set exec_output [run_host_cmd "$ld" "$flags -o tmpdir/cross5 -T $srcdir/$subdir/cross5.t tmpdir/cross4.o"]
171 set exec_output [prune_warnings $exec_output]
172
173 regsub -all "(^|\n)($ld: warning: cannot find entry symbol\[^\n\]*\n?)" $exec_output "\\1" exec_output
174
175 if [string match "" $exec_output] then {
176     fail $test5
177 } else {
178     verbose -log "$exec_output"
179     if [regexp "prohibited cross reference from .* to `.*' in" $exec_output] {
180         pass $test5
181     } else {
182         fail $test5
183     }
184 }
185
186 set exec_output [run_host_cmd "$ld" "$flags -o tmpdir/cross6 -T $srcdir/$subdir/cross6.t tmpdir/cross3.o"]
187 set exec_output [prune_warnings $exec_output]
188
189 regsub -all "(^|\n)($ld: warning: cannot find entry symbol\[^\n\]*\n?)" $exec_output "\\1" exec_output
190
191 if [string match "" $exec_output] then {
192     pass $test6
193 } else {
194     verbose -log "$exec_output"
195     fail $test6
196 }
197
198 set exec_output [run_host_cmd "$ld" "$flags -o tmpdir/cross7 -T $srcdir/$subdir/cross7.t tmpdir/cross3.o"]
199 set exec_output [prune_warnings $exec_output]
200
201 regsub -all "(^|\n)($ld: warning: cannot find entry symbol\[^\n\]*\n?)" $exec_output "\\1" exec_output
202
203 if [string match "" $exec_output] then {
204     fail $test7
205 } else {
206     verbose -log "$exec_output"
207     if [regexp "prohibited cross reference from .* to `.*' in" $exec_output] {
208         pass $test7
209     } else {
210         fail $test7
211     }
212 }
213
214 set CFLAGS "$old_CFLAGS"
215 set CC "$old_CC"