]> git.ipfire.org Git - thirdparty/binutils-gdb.git/blob - ld/testsuite/config/default.exp
Update year range in copyright notice of binutils files
[thirdparty/binutils-gdb.git] / ld / testsuite / config / default.exp
1 # Basic expect script for LD Regression Tests
2 # Copyright (C) 1993-2024 Free Software Foundation, Inc.
3 #
4 # This file is part of the GNU Binutils.
5 #
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
8 # the Free Software Foundation; either version 3 of the License, or
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
18 # Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston,
19 # MA 02110-1301, USA.
20 #
21 # Written by Jeffrey Wheat (cassidy@cygnus.com)
22 #
23
24 if ![info exists ld] then {
25 set ld [findfile $base_dir/ld-new $base_dir/ld-new [transform ld]]
26 }
27
28 if ![info exists as] then {
29 set as [findfile $base_dir/../gas/as-new $base_dir/../gas/as-new [transform as]]
30 }
31
32 if ![info exists nm] then {
33 set nm [findfile $base_dir/../binutils/nm-new $base_dir/../binutils/nm-new [transform nm]]
34 }
35
36 if ![info exists objdump] then {
37 set objdump [findfile $base_dir/../binutils/objdump]
38 }
39
40 if ![info exists objcopy] then {
41 set objcopy [findfile $base_dir/../binutils/objcopy]
42 }
43
44 if ![info exists ar] then {
45 set ar [findfile $base_dir/../binutils/ar]
46 }
47
48 if ![info exists strip] then {
49 set strip [findfile $base_dir/../binutils/strip-new $base_dir/../binutils/strip-new [transform strip]]
50 }
51
52 if ![info exists size] then {
53 set size [findfile $base_dir/../binutils/size]
54 }
55
56 remote_exec host "mkdir -p tmpdir"
57
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.
61 # The variable ld_testsuite_bindir allows to provide another directory
62 # for -B option. This can be useful when launching the testsuite outside
63 # the build tree as the symlinks will be wrong in this case.
64 if {[info exists ld_testsuite_bindir]} {
65 set gcc_B_opt "-B$ld_testsuite_bindir/"
66 } else {
67 if {![file isdirectory tmpdir/ld]} then {
68 catch "exec mkdir tmpdir/ld" status
69 catch "exec ln -s ../../ld-new tmpdir/ld/ld" status
70 catch "exec ln -s ld tmpdir/ld/collect-ld" status
71 catch "exec ln -s ../../../gas/as-new tmpdir/ld/as" status
72 }
73 set gcc_B_opt "-B[pwd]/tmpdir/ld/"
74 }
75
76 # load the linker path
77 set ld_L_opt ""
78 if {[file exists tmpdir/libpath.exp]} {
79 load_lib tmpdir/libpath.exp
80
81 foreach dir $libpath {
82 append ld_L_opt " -L$dir"
83 }
84 }
85
86 if {![info exists CC]} {
87 set CC gcc
88 }
89 if {![info exists CFLAGS]} {
90 set CFLAGS "-g -O2"
91 }
92 if {![info exists CC_FOR_TARGET]} {
93 set CC_FOR_TARGET [find_gcc]
94 }
95 if {![info exists CFLAGS_FOR_TARGET]} {
96 set CFLAGS_FOR_TARGET "-g -O2"
97 }
98 if {![info exists CXX_FOR_TARGET]} {
99 set CXX_FOR_TARGET [find_g++]
100 }
101 if {![info exists CXXFLAGS_FOR_TARGET]} {
102 set CXXFLAGS_FOR_TARGET ""
103 }
104
105 # This allows us to run the linker testsuite with clang as the compilation
106 # driver instead of gcc. The syntax of the overrides are as follows, one
107 # per line:
108 #
109 # '#': Silence information about the changes to the command line arguments.
110 #
111 # '^': Add FOO as a new argument at the beginning of the command line.
112 #
113 # '+': Add FOO as a new argument at the end of the command line.
114 #
115 # 's/XXX/YYY/': Substitute the regular expression XXX with YYY in the command
116 # line.
117 #
118 # 'xOPTION': Removes all instances of the literal argument OPTION.
119 #
120 # 'XOPTION': Removes all instances of the literal argument OPTION,
121 # and the following argument.
122 #
123 # 'Ox': Removes all flags matching 'O' or 'O[sz0-9]' and adds 'Ox'
124 # at the end of the command line.
125 #
126 # \param OS - The stream to write edit information to.
127 # \param Args - The vector of command line arguments.
128 # \param Edit - The override command to perform.
129 # \param SavedStrings - Set to use for storing string representations.
130
131 # Only set up the environment variable if the user has not already provided one.
132 if {! [info exists env(CCC_OVERRIDE_OPTIONS)]} {
133 set env(CCC_OVERRIDE_OPTIONS) "#\
134 +-Wno-unused-command-line-argument \
135 +-Wno-unknown-attributes \
136 +-Wno-tautological-compare \
137 +-Wno-ignored-optimization-argument \
138 +-Wno-deprecated \
139 +-fuse-ld=$ld \
140 x-Wa,--elf-stt-common=yes \
141 x-Wa,-mx86-used-note=no \
142 x-Wa,-mx86-used-note=yes \
143 x-Wa,-madd-bnd-prefix \
144 x-fno-early-inlining \
145 x-fno-toplevel-reorder \
146 x-flto-partition=none \
147 x-feliminate-dwarf2-dups \
148 s/-Wa,-mrelax-relocations=yes,-mx86-used-note=yes/-Wa,-mrelax-relocations=yes/ \
149 s/-Wa,--compress-debug-sections=zlib/-Wa,-compress-debug-sections=zlib/ \
150 s/-Wa,--compress-debug-sections=zlib-gabi/-Wa,-compress-debug-sections=zlib-gabi/ \
151 "
152 }
153
154 # The mips64-*-linux-gnu compiler defaults to the N32 ABI after
155 # installed, but to the O32 ABI in the build tree, because of some
156 # specs-file hacks. Make sure we use an ABI that is compatible with
157 # the one we expect.
158 if {[istarget mips64*-*-linux*] &&
159 (![board_info [target_info name] exists multilib_flags] ||
160 ![string match "*-mabi" [board_info [target_info name] multilib_flags]])
161 } {
162 append gcc_B_opt " -mabi=n32"
163 }
164
165 if { [istarget rx-*-*] } {
166 global ASFLAGS
167 set ASFLAGS "-muse-conventional-section-names"
168 }
169
170 # Blackfin ELF targets require selection of an explicit CPU. Use the sim.
171 if {[istarget bfin*-elf*]} {
172 append gcc_B_opt " -msim"
173 }
174
175 # load the utility procedures
176 load_lib ld-lib.exp
177
178 proc get_target_emul {} {
179 global target_triplet
180 global srcdir
181 set status [catch "exec sh -c \"targ='$target_triplet' && . $srcdir/../configure.tgt && echo \\\$targ_emul\"" result]
182 if $status { error "Error getting emulation name: $result" }
183 return $result
184 }
185
186 if ![info exists HOSTING_EMU] { set HOSTING_EMU "-m [get_target_emul]" }
187
188 #
189 # ld_version -- extract and print the version number of ld compiler (GCC)
190 #
191 proc ld_version {} {
192 global ld
193 default_ld_version $ld
194 }
195
196 #
197 # ld_exit -- just a stub for ld
198 #
199 proc ld_exit {} {
200 }
201
202 #
203 # ld_start
204 # relink the linker
205 #
206 proc ld_start { ld target } {
207 #
208 }
209
210 #
211 # ld_relocate
212 # link an object using relocation
213 #
214 proc ld_relocate { ld target objects } {
215 default_ld_relocate $ld $target $objects
216 }
217
218 #
219 # ld_link
220 # link a program using ld
221 #
222 proc ld_link { ld target objects } {
223 default_ld_link $ld $target $objects
224 }
225
226 #
227 # ld_compile
228 # compile an object using $cc
229 #
230 proc ld_compile { cc source object } {
231 default_ld_compile $cc $source $object
232 }
233
234 #
235 # ld_assemble
236 # assemble a file
237 #
238 proc ld_assemble { as source object } {
239 default_ld_assemble $as "" $source $object
240 }
241
242 #
243 # ld_assemble_flags
244 # assemble a file with extra flags
245 #
246 proc ld_assemble_flags { as flags source object } {
247 default_ld_assemble $as $flags $source $object
248 }
249
250 #
251 # ld_nm
252 # run nm on a file
253 #
254 proc ld_nm { nm nmflags object } {
255 default_ld_nm $nm $nmflags $object
256 }
257
258 #
259 # ld_exec
260 # execute ithe target
261 #
262 proc ld_exec { target output } {
263 default_ld_exec $target $output
264 }
265
266 # From gas-defs.exp, to support run_dump_test.
267 if ![info exists AS] then {
268 set AS $as
269 }
270
271 if ![info exists ASFLAGS] then {
272 set ASFLAGS ""
273 }
274
275 if ![info exists OBJDUMP] then {
276 set OBJDUMP $objdump
277 }
278
279 if ![info exists OBJDUMPFLAGS] then {
280 set OBJDUMPFLAGS {}
281 }
282
283 if ![info exists NM] then {
284 set NM $nm
285 }
286
287 if ![info exists NMFLAGS] then {
288 set NMFLAGS {}
289 }
290
291 if ![info exists OBJCOPY] then {
292 set OBJCOPY $objcopy
293 }
294
295 if ![info exists OBJCOPYFLAGS] then {
296 set OBJCOPYFLAGS {}
297 }
298
299 if ![info exists RANLIB] then {
300 set RANLIB [findfile $base_dir/../binutils/ranlib]
301 }
302
303 if ![info exists READELF] then {
304 set READELF [findfile $base_dir/../binutils/readelf]
305 }
306
307 if ![info exists READELFFLAGS] then {
308 set READELFFLAGS {}
309 }
310
311 if ![info exists SIZE] then {
312 set SIZE [findfile $base_dir/../binutils/size]
313 }
314
315 if ![info exists SIZEFLAGS] then {
316 set SIZEFLAGS ""
317 }
318
319 if ![info exists ELFEDIT] then {
320 set ELFEDIT [findfile $base_dir/../binutils/elfedit]
321 }
322
323 if ![info exists LD] then {
324 set LD [findfile $base_dir/ld-new ./ld-new [transform ld]]
325 }
326
327 if ![info exists LDFLAGS] then {
328 set LDFLAGS {}
329 }
330
331 if { ![info exists DT_RELR_LDFLAGS] } then {
332 if { [supports_dt_relr] } then {
333 set DT_RELR_LDFLAGS "-z pack-relative-relocs"
334 } else {
335 set DT_RELR_LDFLAGS {}
336 }
337 }
338
339 if { ![info exists DT_RELR_CC_LDFLAGS] } then {
340 if { [supports_dt_relr] } then {
341 set DT_RELR_CC_LDFLAGS "-Wl,-z,pack-relative-relocs"
342 } else {
343 set DT_RELR_CC_LDFLAGS {}
344 }
345 }
346
347 if { ![info exists NO_DT_RELR_LDFLAGS] } then {
348 if { [supports_dt_relr] } then {
349 set NO_DT_RELR_LDFLAGS "-z nopack-relative-relocs"
350 } else {
351 set NO_DT_RELR_LDFLAGS {}
352 }
353 }
354
355 if { ![info exists NO_DT_RELR_CC_LDFLAGS] } then {
356 if { [supports_dt_relr] } then {
357 set NO_DT_RELR_CC_LDFLAGS "-Wl,-z,nopack-relative-relocs"
358 } else {
359 set NO_DT_RELR_CC_LDFLAGS {}
360 }
361 }
362
363 # Set LD_CLASS to "64bit" for a 64-bit *host* linker.
364 if { ![info exists LD_CLASS] } then {
365 set REAL_LD [findfile $base_dir/.libs/ld-new .libs/ld-new $LD [transform ld]]
366 set readelf_output [run_host_cmd "$READELF" "-h $REAL_LD"]
367 if { [regexp {[ \t]+Class:[ \t]+ELF64} $readelf_output] } then {
368 set LD_CLASS "64bit"
369 } else {
370 set LD_CLASS "32bit"
371 }
372 }
373
374 # Compiler tests
375 proc compiler_supports { flag args } {
376 if { [check_compiler_available] } {
377 global CC_FOR_TARGET
378 set flags ""
379 if [board_info [target_info name] exists cflags] {
380 append flags " [board_info [target_info name] cflags]"
381 }
382 if [board_info [target_info name] exists ldflags] {
383 append flags " [board_info [target_info name] ldflags]"
384 }
385 set fn "cs[pid].c"
386 set rfno "cs[pid].exe"
387 set f [open $fn "w"]
388 if { [llength $args] > 0 } {
389 puts $f [lindex $args 0]
390 } else {
391 puts $f "int main (void) { return 0; }"
392 }
393 close $f
394 set rfn [remote_download host $fn]
395 set avail [run_host_cmd_yesno "$CC_FOR_TARGET" "$flags $flag $rfn -o $rfno"]
396 remote_file host delete $rfno
397 remote_file host delete $rfn
398 file delete $fn
399 return $avail
400 }
401 return 0
402 }
403
404 if { ![info exists PLT_CFLAGS] } then {
405 set PLT_CFLAGS ""
406 if [compiler_supports "-c -fplt"] {
407 set PLT_CFLAGS "-fplt"
408 }
409 }
410
411 if { ![info exists NOPIE_CFLAGS] || ![info exists NOPIE_LDFLAGS] } then {
412 set NOPIE_CFLAGS ""
413 set NOPIE_LDFLAGS ""
414 if [compiler_supports "-fno-PIE -no-pie"] {
415 set NOPIE_CFLAGS "-fno-PIE"
416 set NOPIE_LDFLAGS "-no-pie"
417 }
418 }
419
420 if { ![info exists NOCF_PROTECTION_CFLAGS] } then {
421 set NOCF_PROTECTION_CFLAGS ""
422 if [compiler_supports "-fcf-protection=none"] {
423 set NOCF_PROTECTION_CFLAGS "-fcf-protection=none"
424 }
425 }
426
427 if { ![info exists GNU2_CFLAGS] } then {
428 set GNU2_CFLAGS ""
429 if [compiler_supports "-c -mtls-dialect=gnu2" "__thread int y = 1;"] {
430 set GNU2_CFLAGS "-mtls-dialect=gnu2"
431 }
432 }
433
434 if { ![info exists INT128_CFLAGS] } then {
435 set INT128_CFLAGS ""
436 if [compiler_supports "-c" "__int128 a = 42;"] {
437 set INT128_CFLAGS "-DHAS_INT128"
438 }
439 }
440
441 if { ![info exists STATIC_LDFLAGS] } then {
442 set STATIC_LDFLAGS ""
443 if [compiler_supports "-static"] {
444 set STATIC_LDFLAGS "-static"
445 }
446 }
447
448 if { ![info exists STATIC_PIE_LDFLAGS] } then {
449 set STATIC_PIE_LDFLAGS ""
450 if [compiler_supports "-static-pie"] {
451 set STATIC_PIE_LDFLAGS "-static-pie"
452 }
453 }
454
455 if { ![info exists NOSANITIZE_CFLAGS] } then {
456 set NOSANITIZE_CFLAGS ""
457 if [compiler_supports "-fno-sanitize=all"] {
458 set NOSANITIZE_CFLAGS "-fno-sanitize=all"
459 }
460 }
461
462 if { ![info exists NOLTO_CFLAGS] } then {
463 set NOLTO_CFLAGS ""
464 if [compiler_supports "-fno-lto"] {
465 set NOLTO_CFLAGS "-fno-lto"
466 }
467 }
468
469 # Provide dummy libraries that alpha-vms-ld always loads
470 if { [istarget alpha*-*-*vms*] } {
471 set src tmpdir/empty.s
472 set f [open $src "w"]
473 close $f
474 if { [ld_assemble $as $src tmpdir/empty.o]
475 && ![ld_link $LD tmpdir/empty tmpdir/empty.o]
476 && [regsub -all {[^\n]*: cannot find -l([^\n :]*)[^\n]*} $exec_output {tmpdir/lib\1.a } missing_libs] } {
477 regsub -all {\$} $missing_libs {\\\$} missing_libs
478 for { set i 0 } { $i < [llength $missing_libs] } { incr i } {
479 set f [lindex $missing_libs $i]
480 verbose -log "creating dummy $f"
481 ar_simple_create $ar {} $f tmpdir/empty.o
482 }
483 append LDFLAGS " -Ltmpdir"
484 unset i missing_libs
485 }
486 unset f src
487 }
488
489 set plug_opt ""
490 if { [check_compiler_available] } {
491 set plugin_names {
492 liblto_plugin.so
493 liblto_plugin-0.dll
494 cyglto_plugin-0.dll
495 }
496 foreach plug $plugin_names {
497 set plug_so [run_host_cmd $CC_FOR_TARGET "--print-prog-name $plug"]
498 if { $plug_so eq $plug } then {
499 set plug_so [run_host_cmd $CC_FOR_TARGET "--print-file-name $plug"]
500 }
501 if { $plug_so ne $plug } then {
502 set plug_opt "--plugin $plug_so"
503 break
504 }
505 }
506 }
507
508 if {[file exists .libs/libdep.so]} {
509 set dep_plug_opt "--plugin .libs/libdep.so"
510 } elseif {[file exists .libs/libdep.dll]} {
511 set dep_plug_opt "--plugin .libs/libdep.dll"
512 } else {
513 set dep_plug_opt ""
514 }