ld: Add NOCF_PROTECTION_CFLAGS to turn off -fcf-protection
[deliverable/binutils-gdb.git] / ld / testsuite / config / default.exp
CommitLineData
252b5132 1# Basic expect script for LD Regression Tests
b3adc24a 2# Copyright (C) 1993-2020 Free Software Foundation, Inc.
252b5132 3#
f96b4a7b
NC
4# This file is part of the GNU Binutils.
5#
252b5132
RH
6# This file is free software; you can redistribute it and/or modify
7# it under the terms of the GNU General Public License as published by
f96b4a7b 8# the Free Software Foundation; either version 3 of the License, or
252b5132
RH
9# (at your option) any later version.
10#
11# This program is distributed in the hope that it will be useful,
12# but WITHOUT ANY WARRANTY; without even the implied warranty of
13# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14# GNU General Public License for more details.
15#
16# You should have received a copy of the GNU General Public License
17# along with this program; if not, write to the Free Software
f96b4a7b
NC
18# Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston,
19# MA 02110-1301, USA.
252b5132
RH
20#
21# Written by Jeffrey Wheat (cassidy@cygnus.com)
22#
23
24if ![info exists ld] then {
25 set ld [findfile $base_dir/ld-new $base_dir/ld-new [transform ld]]
26}
27
28if ![info exists as] then {
29 set as [findfile $base_dir/../gas/as-new $base_dir/../gas/as-new [transform as]]
30}
31
32if ![info exists nm] then {
33 set nm [findfile $base_dir/../binutils/nm-new $base_dir/../binutils/nm-new [transform nm]]
34}
35
36if ![info exists objdump] then {
37 set objdump [findfile $base_dir/../binutils/objdump]
38}
39
40if ![info exists objcopy] then {
41 set objcopy [findfile $base_dir/../binutils/objcopy]
42}
43
44if ![info exists ar] then {
45 set ar [findfile $base_dir/../binutils/ar]
46}
47
48if ![info exists strip] then {
49 set strip [findfile $base_dir/../binutils/strip-new $base_dir/../binutils/strip-new [transform strip]]
50}
51
7b19bec2
AM
52if ![info exists size] then {
53 set size [findfile $base_dir/../binutils/size]
54}
55
7f6a71ff 56remote_exec host "mkdir -p tmpdir"
252b5132 57
f1d7f4a6
AM
58# Make symlinks from tmpdir/ld to the linker and assembler in the
59# build tree, so that we can use a -B option to gcc to force it to use
60# the newly built linker and assembler.
b765d4e3
L
61if {![file isdirectory tmpdir/ld]} then {
62 catch "exec mkdir tmpdir/ld" status
63 catch "exec ln -s ../../ld-new tmpdir/ld/ld" status
3499cebd 64 catch "exec ln -s ld tmpdir/ld/collect-ld" status
f1d7f4a6 65 catch "exec ln -s ../../../gas/as-new tmpdir/ld/as" status
b765d4e3 66}
f1d7f4a6 67set gcc_B_opt "-B[pwd]/tmpdir/ld/"
b765d4e3 68
cdaeef2e 69# load the linker path
f1d7f4a6 70set ld_L_opt ""
99c05fe4
MM
71if {[file exists tmpdir/libpath.exp]} {
72 load_lib tmpdir/libpath.exp
cdaeef2e 73
99c05fe4 74 foreach dir $libpath {
f1d7f4a6 75 append ld_L_opt " -L$dir"
99c05fe4
MM
76 }
77}
78
647e4d46
L
79# Many ELF testcases expect that "-z relro" is off.
80set ld_elf_shared_opt "-z norelro"
81
99c05fe4
MM
82# The "make check" target in the Makefile passes in
83# "CC=$(CC_FOR_TARGET)". But, if the user invokes runtest directly
dae043d2 84# (as when testing an installed linker), these flags may not be set.
99c05fe4 85if {![info exists CC]} {
dae043d2
MM
86 set CC [find_gcc]
87}
88if {![info exists CFLAGS]} {
89 set CFLAGS "-g -O2"
90}
91if {![info exists CXX]} {
92 set CXX [find_g++]
93}
94if {![info exists CXXFLAGS]} {
95 set CXXFLAGS ""
cdaeef2e
L
96}
97
697380b3
AO
98# The mips64-*-linux-gnu compiler defaults to the N32 ABI after
99# installed, but to the O32 ABI in the build tree, because of some
100# specs-file hacks. Make sure we use an ABI that is compatible with
101# the one we expect.
102if {[istarget mips64*-*-linux*] &&
103 (![board_info [target_info name] exists multilib_flags] ||
104 ![string match "*-mabi" [board_info [target_info name] multilib_flags]])
105 } {
f1d7f4a6 106 append gcc_B_opt " -mabi=n32"
697380b3
AO
107}
108
e5719043
NC
109if { [istarget rx-*-*] } {
110 global ASFLAGS
111 set ASFLAGS "-muse-conventional-section-names"
112}
113
252b5132
RH
114# load the utility procedures
115load_lib ld-lib.exp
116
252b5132
RH
117proc get_target_emul {} {
118 global target_triplet
119 global srcdir
120 set status [catch "exec sh -c \"targ='$target_triplet' && . $srcdir/../configure.tgt && echo \\\$targ_emul\"" result]
121 if $status { error "Error getting emulation name: $result" }
122 return $result
123}
124
252b5132
RH
125if ![info exists HOSTING_EMU] { set HOSTING_EMU "-m [get_target_emul]" }
126
127#
128# ld_version -- extract and print the version number of ld compiler (GCC)
129#
130proc ld_version {} {
131 global ld
132 default_ld_version $ld
133}
134
135#
136# ld_exit -- just a stub for ld
137#
138proc ld_exit {} {
139}
140
141#
142# ld_start
143# relink the linker
144#
145proc ld_start { ld target } {
146 #
147}
148
149#
150# ld_relocate
151# link an object using relocation
152#
153proc ld_relocate { ld target objects } {
154 default_ld_relocate $ld $target $objects
155}
156
157#
158# ld_link
159# link a program using ld
160#
161proc ld_link { ld target objects } {
162 default_ld_link $ld $target $objects
163}
164
252b5132
RH
165#
166# ld_compile
167# compile an object using $cc
168#
169proc ld_compile { cc source object } {
170 default_ld_compile $cc $source $object
171}
172
173#
174# ld_assemble
175# assemble a file
176#
177proc ld_assemble { as source object } {
de1491f0
BS
178 default_ld_assemble $as "" $source $object
179}
180
181#
182# ld_assemble_flags
183# assemble a file with extra flags
184#
185proc ld_assemble_flags { as flags source object } {
186 default_ld_assemble $as $flags $source $object
252b5132
RH
187}
188
189#
190# ld_nm
191# run nm on a file
192#
992c450d
AM
193proc ld_nm { nm nmflags object } {
194 default_ld_nm $nm $nmflags $object
252b5132
RH
195}
196
197#
198# ld_exec
199# execute ithe target
200#
201proc ld_exec { target output } {
202 default_ld_exec $target $output
203}
204
261def70
HPN
205# From gas-defs.exp, to support run_dump_test.
206if ![info exists AS] then {
207 set AS $as
208}
209
261def70
HPN
210if ![info exists ASFLAGS] then {
211 set ASFLAGS ""
212}
213
214if ![info exists OBJDUMP] then {
215 set OBJDUMP $objdump
216}
217
218if ![info exists OBJDUMPFLAGS] then {
219 set OBJDUMPFLAGS {}
220}
221
222if ![info exists NM] then {
223 set NM $nm
224}
225
226if ![info exists NMFLAGS] then {
227 set NMFLAGS {}
228}
229
230if ![info exists OBJCOPY] then {
231 set OBJCOPY $objcopy
232}
233
234if ![info exists OBJCOPYFLAGS] then {
235 set OBJCOPYFLAGS {}
236}
237
238if ![info exists READELF] then {
239 set READELF [findfile $base_dir/../binutils/readelf]
240}
241
242if ![info exists READELFFLAGS] then {
243 set READELFFLAGS {}
244}
245
8a6b075b
L
246if ![info exists ELFEDIT] then {
247 set ELFEDIT [findfile $base_dir/../binutils/elfedit]
248}
249
261def70
HPN
250if ![info exists LD] then {
251 set LD [findfile $base_dir/ld-new ./ld-new [transform ld]]
252}
253
254if ![info exists LDFLAGS] then {
255 set LDFLAGS {}
256}
e10461ae 257
b77db948 258# Set LD_CLASS to "64bit" for a 64-bit *host* linker.
978c0540 259if { ![info exists LD_CLASS] } then {
7ed1dab9
L
260 set REAL_LD [findfile $base_dir/.libs/ld-new .libs/ld-new $LD [transform ld]]
261 set readelf_output [run_host_cmd "$READELF" "-h $REAL_LD"]
978c0540
L
262 if { [regexp {[ \t]+Class:[ \t]+ELF64} $readelf_output] } then {
263 set LD_CLASS "64bit"
264 } else {
265 set LD_CLASS "32bit"
266 }
267}
268
e10461ae
L
269# Set PLT_CFLAGS to "-fplt" if target compiler supports it.
270
271if { ![info exists PLT_CFLAGS] } then {
44ed8092 272 if { [check_compiler_available] } {
e10461ae
L
273 # Check if gcc supports -fplt
274 set flags ""
275 if [board_info [target_info name] exists cflags] {
276 append flags " [board_info [target_info name] cflags]"
277 }
278 if [board_info [target_info name] exists ldflags] {
279 append flags " [board_info [target_info name] ldflags]"
280 }
281
282 set basename "tmpdir/plt[pid]"
283 set src ${basename}.c
284 set output ${basename}.o
285 set f [open $src "w"]
286 puts $f ""
287 close $f
010f98a5
L
288 if [is_remote host] {
289 set src [remote_download host $src]
290 }
e10461ae
L
291 set plt_available [run_host_cmd_yesno "$CC" "$flags -c -fplt $src -o $output"]
292 remote_file host delete $src
293 remote_file host delete $output
294 file delete $src
295
296 if { $plt_available == 1 } then {
297 set PLT_CFLAGS "-fplt"
298 } else {
299 set PLT_CFLAGS ""
300 }
301 } else {
302 set PLT_CFLAGS ""
303 }
304}
6f8ea6c4
L
305
306# Set NOPIE_CFLAGS to "-fno-PIE" and NOPIE_LDFLAGS to "-no-pie" if
307# target compiler supports them.
308
309if { ![info exists NOPIE_CFLAGS] || ![info exists NOPIE_LDFLAGS] } then {
44ed8092 310 if { [check_compiler_available] } {
6f8ea6c4
L
311 # Check if gcc supports -fno-PIE -no-pie.
312 set flags ""
313 if [board_info [target_info name] exists cflags] {
314 append flags " [board_info [target_info name] cflags]"
315 }
316 if [board_info [target_info name] exists ldflags] {
317 append flags " [board_info [target_info name] ldflags]"
318 }
319
320 set basename "tmpdir/nopie[pid]"
321 set src ${basename}.c
322 set output ${basename}
323 set f [open $src "w"]
324 puts $f "int main (void) { return 0; }"
325 close $f
010f98a5
L
326 if [is_remote host] {
327 set src [remote_download host $src]
328 }
6f8ea6c4
L
329 set nopie_available [run_host_cmd_yesno "$CC" "$flags -fno-PIE -no-pie $src -o $output"]
330 remote_file host delete $src
331 remote_file host delete $output
332 file delete $src
333
334 if { $nopie_available == 1 } then {
335 set NOPIE_CFLAGS "-fno-PIE"
336 set NOPIE_LDFLAGS "-no-pie"
337 } else {
338 set NOPIE_CFLAGS ""
339 set NOPIE_LDFLAGS ""
340 }
341 } else {
342 set NOPIE_CFLAGS ""
343 set NOPIE_LDFLAGS ""
344 }
345}
1670f9c1 346
8be5def6
L
347# Set NOCF_PROTECTION_CFLAGS to "-fcf-protection=none" if target compiler
348# supports it.
349
350if { ![info exists NOCF_PROTECTION_CFLAGS] } then {
351 if { [check_compiler_available] } {
352 # Check if gcc supports -fcf-protection=none.
353 set flags ""
354 if [board_info [target_info name] exists cflags] {
355 append flags " [board_info [target_info name] cflags]"
356 }
357 if [board_info [target_info name] exists ldflags] {
358 append flags " [board_info [target_info name] ldflags]"
359 }
360
361 set basename "tmpdir/nopie[pid]"
362 set src ${basename}.c
363 set output ${basename}
364 set f [open $src "w"]
365 puts $f "int main (void) { return 0; }"
366 close $f
367 if [is_remote host] {
368 set src [remote_download host $src]
369 }
370 set nopie_available [run_host_cmd_yesno "$CC" "$flags -fcf-protection=none $src -o $output"]
371 remote_file host delete $src
372 remote_file host delete $output
373 file delete $src
374
375 if { $nopie_available == 1 } then {
376 set NOCF_PROTECTION_CFLAGS "-fcf-protection=none"
377 } else {
378 set NOCF_PROTECTION_CFLAGS ""
379 }
380 } else {
381 set NOCF_PROTECTION_CFLAGS ""
382 }
383}
384
1670f9c1
L
385# Set GNU2_CFLAGS to "-mtls-dialect=gnu2" if target compiler supports it.
386
387if { ![info exists GNU2_CFLAGS] } then {
44ed8092 388 if { [check_compiler_available] } {
1670f9c1
L
389 # Check if gcc supports -mtls-dialect=gnu2
390 set flags ""
391 if [board_info [target_info name] exists cflags] {
392 append flags " [board_info [target_info name] cflags]"
393 }
394 if [board_info [target_info name] exists ldflags] {
395 append flags " [board_info [target_info name] ldflags]"
396 }
397
398 set basename "tmpdir/gnu2[pid]"
399 set src ${basename}.c
400 set output ${basename}.o
401 set f [open $src "w"]
402 puts $f "__thread int yyy = 100;"
403 close $f
404 if [is_remote host] {
405 set src [remote_download host $src]
406 }
407 set gnu2_available [run_host_cmd_yesno "$CC" "$flags -c -mtls-dialect=gnu2 $src -o $output"]
408 remote_file host delete $src
409 remote_file host delete $output
410 file delete $src
411
412 if { $gnu2_available == 1 } then {
413 set GNU2_CFLAGS "-mtls-dialect=gnu2"
414 } else {
415 set GNU2_CFLAGS ""
416 }
417 } else {
418 set GNU2_CFLAGS ""
419 }
420}
fef75122
L
421
422# Set INT128_CFLAGS to "-DHAS_INT128" if target compiler supports __int128.
423
424if { ![info exists INT128_CFLAGS] } then {
44ed8092 425 if { [check_compiler_available] } {
fef75122
L
426 # Check if gcc supports __int128.
427 set flags ""
428 if [board_info [target_info name] exists cflags] {
429 append flags " [board_info [target_info name] cflags]"
430 }
431 if [board_info [target_info name] exists ldflags] {
432 append flags " [board_info [target_info name] ldflags]"
433 }
434
435 set basename "tmpdir/int128[pid]"
436 set src ${basename}.c
437 set output ${basename}.o
438 set f [open $src "w"]
439 puts $f "__int128 a = 42;"
440 close $f
441 if [is_remote host] {
442 set src [remote_download host $src]
443 }
444 set int128_available [run_host_cmd_yesno "$CC" "$flags -c $src -o $output"]
445 remote_file host delete $src
446 remote_file host delete $output
447 file delete $src
448
449 if { $int128_available == 1 } then {
450 set INT128_CFLAGS "-DHAS_INT128"
451 } else {
452 set INT128_CFLAGS ""
453 }
454 } else {
455 set INT128_CFLAGS ""
456 }
457}
98d72909
L
458
459# Set STATIC_LDFLAGS to "-static" if target compiler supports it.
460
461if { ![info exists STATIC_LDFLAGS] } then {
44ed8092 462 if { [check_compiler_available] } {
98d72909
L
463 # Check if gcc supports -static.
464 set flags ""
465 if [board_info [target_info name] exists cflags] {
466 append flags " [board_info [target_info name] cflags]"
467 }
468 if [board_info [target_info name] exists ldflags] {
469 append flags " [board_info [target_info name] ldflags]"
470 }
471
472 set basename "tmpdir/static[pid]"
473 set src ${basename}.c
474 set output ${basename}
475 set f [open $src "w"]
476 puts $f "int main (void) { return 0; }"
477 close $f
478 if [is_remote host] {
479 set src [remote_download host $src]
480 }
481 set static_available [run_host_cmd_yesno "$CC" "-static $flags $src -o $output"]
482 remote_file host delete $src
483 remote_file host delete $output
484 file delete $src
485
486 if { $static_available == 1 } then {
487 set STATIC_LDFLAGS "-static"
488 } else {
489 set STATIC_LDFLAGS ""
490 }
491 } else {
492 set STATIC_LDFLAGS ""
493 }
494}
b3d7a867
L
495
496# Set STATIC_PIE_LDFLAGS to "-static-pie" if target compiler supports it.
497
498if { ![info exists STATIC_PIE_LDFLAGS] } then {
44ed8092 499 if { [check_compiler_available] } {
b3d7a867
L
500 # Check if gcc supports -static-pie.
501 set flags ""
502 if [board_info [target_info name] exists cflags] {
503 append flags " [board_info [target_info name] cflags]"
504 }
505 if [board_info [target_info name] exists ldflags] {
506 append flags " [board_info [target_info name] ldflags]"
507 }
508
509 set basename "tmpdir/static[pid]"
510 set src ${basename}.c
511 set output ${basename}
512 set f [open $src "w"]
513 puts $f "int main (void) { return 0; }"
514 close $f
515 if [is_remote host] {
516 set src [remote_download host $src]
517 }
518 set static_available [run_host_cmd_yesno "$CC" "-static-pie $flags $src -o $output"]
519 remote_file host delete $src
520 remote_file host delete $output
521 file delete $src
522
523 if { $static_available == 1 } then {
524 set STATIC_PIE_LDFLAGS "-static-pie"
525 } else {
526 set STATIC_PIE_LDFLAGS ""
527 }
528 } else {
529 set STATIC_PIE_LDFLAGS ""
530 }
531}
This page took 0.89818 seconds and 4 git commands to generate.