1 # Basic expect script for LD Regression Tests
2 # Copyright (C) 1993-2024 Free Software Foundation, Inc.
4 # This file is part of the GNU Binutils.
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.
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.
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,
21 # Written by Jeffrey Wheat (cassidy@cygnus.com)
24 if ![info exists ld] then {
25 set ld [findfile $base_dir/ld-new $base_dir/ld-new [transform ld]]
28 if ![info exists as] then {
29 set as [findfile $base_dir/../gas/as-new $base_dir/../gas/as-new [transform as]]
32 if ![info exists nm] then {
33 set nm [findfile $base_dir/../binutils/nm-new $base_dir/../binutils/nm-new [transform nm]]
36 if ![info exists objdump] then {
37 set objdump [findfile $base_dir/../binutils/objdump]
40 if ![info exists objcopy] then {
41 set objcopy [findfile $base_dir/../binutils/objcopy]
44 if ![info exists ar] then {
45 set ar [findfile $base_dir/../binutils/ar]
48 if ![info exists strip] then {
49 set strip [findfile $base_dir/../binutils/strip-new $base_dir/../binutils/strip-new [transform strip]]
52 if ![info exists size] then {
53 set size [findfile $base_dir/../binutils/size]
56 remote_exec host "mkdir -p tmpdir"
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/"
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
73 set gcc_B_opt "-B[pwd]/tmpdir/ld/"
76 # load the linker path
78 if {[file exists tmpdir/libpath.exp]} {
79 load_lib tmpdir/libpath.exp
81 foreach dir $libpath {
82 append ld_L_opt " -L$dir"
86 if {![info exists CC]} {
89 if {![info exists CFLAGS]} {
92 if {![info exists CC_FOR_TARGET]} {
93 set CC_FOR_TARGET [find_gcc]
95 if {![info exists CFLAGS_FOR_TARGET]} {
96 set CFLAGS_FOR_TARGET "-g -O2"
98 if {![info exists CXX_FOR_TARGET]} {
99 set CXX_FOR_TARGET [find_g++]
101 if {![info exists CXXFLAGS_FOR_TARGET]} {
102 set CXXFLAGS_FOR_TARGET ""
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
109 # '#': Silence information about the changes to the command line arguments.
111 # '^': Add FOO as a new argument at the beginning of the command line.
113 # '+': Add FOO as a new argument at the end of the command line.
115 # 's/XXX/YYY/': Substitute the regular expression XXX with YYY in the command
118 # 'xOPTION': Removes all instances of the literal argument OPTION.
120 # 'XOPTION': Removes all instances of the literal argument OPTION,
121 # and the following argument.
123 # 'Ox': Removes all flags matching 'O' or 'O[sz0-9]' and adds 'Ox'
124 # at the end of the command line.
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.
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 \
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/ \
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
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]])
162 append gcc_B_opt " -mabi=n32"
165 if { [istarget rx-*-*] } {
167 set ASFLAGS "-muse-conventional-section-names"
170 # Blackfin ELF targets require selection of an explicit CPU. Use the sim.
171 if {[istarget bfin*-elf*]} {
172 append gcc_B_opt " -msim"
175 # load the utility procedures
178 proc get_target_emul {} {
179 global target_triplet
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" }
186 if ![info exists HOSTING_EMU] { set HOSTING_EMU "-m [get_target_emul]" }
189 # ld_version -- extract and print the version number of ld compiler (GCC)
193 default_ld_version $ld
197 # ld_exit -- just a stub for ld
206 proc ld_start { ld target } {
212 # link an object using relocation
214 proc ld_relocate { ld target objects } {
215 default_ld_relocate $ld $target $objects
220 # link a program using ld
222 proc ld_link { ld target objects } {
223 default_ld_link $ld $target $objects
228 # compile an object using $cc
230 proc ld_compile { cc source object } {
231 default_ld_compile $cc $source $object
238 proc ld_assemble { as source object } {
239 default_ld_assemble $as "" $source $object
244 # assemble a file with extra flags
246 proc ld_assemble_flags { as flags source object } {
247 default_ld_assemble $as $flags $source $object
254 proc ld_nm { nm nmflags object } {
255 default_ld_nm $nm $nmflags $object
260 # execute ithe target
262 proc ld_exec { target output } {
263 default_ld_exec $target $output
266 # From gas-defs.exp, to support run_dump_test.
267 if ![info exists AS] then {
271 if ![info exists ASFLAGS] then {
275 if ![info exists OBJDUMP] then {
279 if ![info exists OBJDUMPFLAGS] then {
283 if ![info exists NM] then {
287 if ![info exists NMFLAGS] then {
291 if ![info exists OBJCOPY] then {
295 if ![info exists OBJCOPYFLAGS] then {
299 if ![info exists RANLIB] then {
300 set RANLIB [findfile $base_dir/../binutils/ranlib]
303 if ![info exists READELF] then {
304 set READELF [findfile $base_dir/../binutils/readelf]
307 if ![info exists READELFFLAGS] then {
311 if ![info exists SIZE] then {
312 set SIZE [findfile $base_dir/../binutils/size]
315 if ![info exists SIZEFLAGS] then {
319 if ![info exists ELFEDIT] then {
320 set ELFEDIT [findfile $base_dir/../binutils/elfedit]
323 if ![info exists LD] then {
324 set LD [findfile $base_dir/ld-new ./ld-new [transform ld]]
327 if ![info exists LDFLAGS] then {
331 if { ![info exists DT_RELR_LDFLAGS] } then {
332 if { [supports_dt_relr] } then {
333 set DT_RELR_LDFLAGS "-z pack-relative-relocs"
335 set DT_RELR_LDFLAGS {}
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"
343 set DT_RELR_CC_LDFLAGS {}
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"
351 set NO_DT_RELR_LDFLAGS {}
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"
359 set NO_DT_RELR_CC_LDFLAGS {}
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 {
375 proc compiler_supports { flag args } {
376 if { [check_compiler_available] } {
379 if [board_info [target_info name] exists cflags] {
380 append flags " [board_info [target_info name] cflags]"
382 if [board_info [target_info name] exists ldflags] {
383 append flags " [board_info [target_info name] ldflags]"
386 set rfno "cs[pid].exe"
388 if { [llength $args] > 0 } {
389 puts $f [lindex $args 0]
391 puts $f "int main (void) { return 0; }"
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
404 if { ![info exists PLT_CFLAGS] } then {
406 if [compiler_supports "-c -fplt"] {
407 set PLT_CFLAGS "-fplt"
411 if { ![info exists NOPIE_CFLAGS] || ![info exists NOPIE_LDFLAGS] } then {
414 if [compiler_supports "-fno-PIE -no-pie"] {
415 set NOPIE_CFLAGS "-fno-PIE"
416 set NOPIE_LDFLAGS "-no-pie"
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"
427 if { ![info exists GNU2_CFLAGS] } then {
429 if [compiler_supports "-c -mtls-dialect=gnu2" "__thread int y = 1;"] {
430 set GNU2_CFLAGS "-mtls-dialect=gnu2"
434 if { ![info exists INT128_CFLAGS] } then {
436 if [compiler_supports "-c" "__int128 a = 42;"] {
437 set INT128_CFLAGS "-DHAS_INT128"
441 if { ![info exists STATIC_LDFLAGS] } then {
442 set STATIC_LDFLAGS ""
443 if [compiler_supports "-static"] {
444 set STATIC_LDFLAGS "-static"
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"
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"
462 if { ![info exists NOLTO_CFLAGS] } then {
464 if [compiler_supports "-fno-lto"] {
465 set NOLTO_CFLAGS "-fno-lto"
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"]
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
483 append LDFLAGS " -Ltmpdir"
490 if { [check_compiler_available] } {
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"]
501 if { $plug_so ne $plug } then {
502 set plug_opt "--plugin $plug_so"
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"