1 # Basic expect script for LD Regression Tests
2 # Copyright (C) 1993-2022 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 if {![file isdirectory tmpdir/ld]} then {
62 catch "exec mkdir tmpdir/ld" status
63 catch "exec ln -s ../../ld-new tmpdir/ld/ld" status
64 catch "exec ln -s ld tmpdir/ld/collect-ld" status
65 catch "exec ln -s ../../../gas/as-new tmpdir/ld/as" status
67 set gcc_B_opt "-B[pwd]/tmpdir/ld/"
69 # load the linker path
71 if {[file exists tmpdir/libpath.exp]} {
72 load_lib tmpdir/libpath.exp
74 foreach dir $libpath {
75 append ld_L_opt " -L$dir"
79 if {![info exists CC]} {
82 if {![info exists CFLAGS]} {
85 if {![info exists CC_FOR_TARGET]} {
86 set CC_FOR_TARGET [find_gcc]
88 if {![info exists CFLAGS_FOR_TARGET]} {
89 set CFLAGS_FOR_TARGET "-g -O2"
91 if {![info exists CXX_FOR_TARGET]} {
92 set CXX_FOR_TARGET [find_g++]
94 if {![info exists CXXFLAGS_FOR_TARGET]} {
95 set CXXFLAGS_FOR_TARGET ""
98 # This allows us to run the linker testsuite with clang as the compilation
99 # driver instead of gcc. The syntax of the overrides are as follows, one
102 # '#': Silence information about the changes to the command line arguments.
104 # '^': Add FOO as a new argument at the beginning of the command line.
106 # '+': Add FOO as a new argument at the end of the command line.
108 # 's/XXX/YYY/': Substitute the regular expression XXX with YYY in the command
111 # 'xOPTION': Removes all instances of the literal argument OPTION.
113 # 'XOPTION': Removes all instances of the literal argument OPTION,
114 # and the following argument.
116 # 'Ox': Removes all flags matching 'O' or 'O[sz0-9]' and adds 'Ox'
117 # at the end of the command line.
119 # \param OS - The stream to write edit information to.
120 # \param Args - The vector of command line arguments.
121 # \param Edit - The override command to perform.
122 # \param SavedStrings - Set to use for storing string representations.
124 # Only set up the environment variable if the user has not already provided one.
125 if {! [info exists env(CCC_OVERRIDE_OPTIONS)]} {
126 set env(CCC_OVERRIDE_OPTIONS) "#\
127 +-Wno-unused-command-line-argument \
128 +-Wno-unknown-attributes \
129 +-Wno-tautological-compare \
130 +-Wno-ignored-optimization-argument \
133 x-Wa,--elf-stt-common=yes \
134 x-Wa,-mx86-used-note=no \
135 x-Wa,-mx86-used-note=yes \
136 x-Wa,-madd-bnd-prefix \
137 x-fno-early-inlining \
138 x-fno-toplevel-reorder \
139 x-flto-partition=none \
140 x-feliminate-dwarf2-dups \
141 s/-Wa,-mrelax-relocations=yes,-mx86-used-note=yes/-Wa,-mrelax-relocations=yes/ \
142 s/-Wa,--compress-debug-sections=zlib/-Wa,-compress-debug-sections=zlib/ \
143 s/-Wa,--compress-debug-sections=zlib-gabi/-Wa,-compress-debug-sections=zlib-gabi/ \
147 # The mips64-*-linux-gnu compiler defaults to the N32 ABI after
148 # installed, but to the O32 ABI in the build tree, because of some
149 # specs-file hacks. Make sure we use an ABI that is compatible with
151 if {[istarget mips64*-*-linux*] &&
152 (![board_info [target_info name] exists multilib_flags] ||
153 ![string match "*-mabi" [board_info [target_info name] multilib_flags]])
155 append gcc_B_opt " -mabi=n32"
158 if { [istarget rx-*-*] } {
160 set ASFLAGS "-muse-conventional-section-names"
163 # Blackfin ELF targets require selection of an explicit CPU. Use the sim.
164 if {[istarget bfin*-elf*]} {
165 append gcc_B_opt " -msim"
168 # load the utility procedures
171 proc get_target_emul {} {
172 global target_triplet
174 set status [catch "exec sh -c \"targ='$target_triplet' && . $srcdir/../configure.tgt && echo \\\$targ_emul\"" result]
175 if $status { error "Error getting emulation name: $result" }
179 if ![info exists HOSTING_EMU] { set HOSTING_EMU "-m [get_target_emul]" }
182 # ld_version -- extract and print the version number of ld compiler (GCC)
186 default_ld_version $ld
190 # ld_exit -- just a stub for ld
199 proc ld_start { ld target } {
205 # link an object using relocation
207 proc ld_relocate { ld target objects } {
208 default_ld_relocate $ld $target $objects
213 # link a program using ld
215 proc ld_link { ld target objects } {
216 default_ld_link $ld $target $objects
221 # compile an object using $cc
223 proc ld_compile { cc source object } {
224 default_ld_compile $cc $source $object
231 proc ld_assemble { as source object } {
232 default_ld_assemble $as "" $source $object
237 # assemble a file with extra flags
239 proc ld_assemble_flags { as flags source object } {
240 default_ld_assemble $as $flags $source $object
247 proc ld_nm { nm nmflags object } {
248 default_ld_nm $nm $nmflags $object
253 # execute ithe target
255 proc ld_exec { target output } {
256 default_ld_exec $target $output
259 # From gas-defs.exp, to support run_dump_test.
260 if ![info exists AS] then {
264 if ![info exists ASFLAGS] then {
268 if ![info exists OBJDUMP] then {
272 if ![info exists OBJDUMPFLAGS] then {
276 if ![info exists NM] then {
280 if ![info exists NMFLAGS] then {
284 if ![info exists OBJCOPY] then {
288 if ![info exists OBJCOPYFLAGS] then {
292 if ![info exists RANLIB] then {
293 set RANLIB [findfile $base_dir/../binutils/ranlib]
296 if ![info exists READELF] then {
297 set READELF [findfile $base_dir/../binutils/readelf]
300 if ![info exists READELFFLAGS] then {
304 if ![info exists ELFEDIT] then {
305 set ELFEDIT [findfile $base_dir/../binutils/elfedit]
308 if ![info exists LD] then {
309 set LD [findfile $base_dir/ld-new ./ld-new [transform ld]]
312 if ![info exists LDFLAGS] then {
316 if { ![info exists DT_RELR_LDFLAGS] } then {
317 if { [supports_dt_relr] } then {
318 set DT_RELR_LDFLAGS "-z pack-relative-relocs"
320 set DT_RELR_LDFLAGS {}
324 if { ![info exists DT_RELR_CC_LDFLAGS] } then {
325 if { [supports_dt_relr] } then {
326 set DT_RELR_CC_LDFLAGS "-Wl,-z,pack-relative-relocs"
328 set DT_RELR_CC_LDFLAGS {}
332 if { ![info exists NO_DT_RELR_LDFLAGS] } then {
333 if { [supports_dt_relr] } then {
334 set NO_DT_RELR_LDFLAGS "-z nopack-relative-relocs"
336 set NO_DT_RELR_LDFLAGS {}
340 if { ![info exists NO_DT_RELR_CC_LDFLAGS] } then {
341 if { [supports_dt_relr] } then {
342 set NO_DT_RELR_CC_LDFLAGS "-Wl,-z,nopack-relative-relocs"
344 set NO_DT_RELR_CC_LDFLAGS {}
348 # Set LD_CLASS to "64bit" for a 64-bit *host* linker.
349 if { ![info exists LD_CLASS] } then {
350 set REAL_LD [findfile $base_dir/.libs/ld-new .libs/ld-new $LD [transform ld]]
351 set readelf_output [run_host_cmd "$READELF" "-h $REAL_LD"]
352 if { [regexp {[ \t]+Class:[ \t]+ELF64} $readelf_output] } then {
360 proc compiler_supports { flag args } {
361 if { [check_compiler_available] } {
364 if [board_info [target_info name] exists cflags] {
365 append flags " [board_info [target_info name] cflags]"
367 if [board_info [target_info name] exists ldflags] {
368 append flags " [board_info [target_info name] ldflags]"
372 if { [llength $args] > 0 } {
373 puts $f [lindex $args 0]
375 puts $f "int main (void) { return 0; }"
378 set rfn [remote_download host $fn]
379 set avail [run_host_cmd_yesno "$CC_FOR_TARGET" "$flags $flag $rfn -o /dev/null"]
380 remote_file host delete $rfn
387 if { ![info exists PLT_CFLAGS] } then {
389 if [compiler_supports "-c -fplt"] {
390 set PLT_CFLAGS "-fplt"
394 if { ![info exists NOPIE_CFLAGS] || ![info exists NOPIE_LDFLAGS] } then {
397 if [compiler_supports "-fno-PIE -no-pie"] {
398 set NOPIE_CFLAGS "-fno-PIE"
399 set NOPIE_LDFLAGS "-no-pie"
403 if { ![info exists NOCF_PROTECTION_CFLAGS] } then {
404 set NOCF_PROTECTION_CFLAGS ""
405 if [compiler_supports "-fcf-protection=none"] {
406 set NOCF_PROTECTION_CFLAGS "-fcf-protection=none"
410 if { ![info exists GNU2_CFLAGS] } then {
412 if [compiler_supports "-c -mtls-dialect=gnu2" "__thread int y = 1;"] {
413 set GNU2_CFLAGS "-mtls-dialect=gnu2"
417 if { ![info exists INT128_CFLAGS] } then {
419 if [compiler_supports "-c" "__int128 a = 42;"] {
420 set INT128_CFLAGS "-DHAS_INT128"
424 if { ![info exists STATIC_LDFLAGS] } then {
425 set STATIC_LDFLAGS ""
426 if [compiler_supports "-static"] {
427 set STATIC_LDFLAGS "-static"
431 if { ![info exists STATIC_PIE_LDFLAGS] } then {
432 set STATIC_PIE_LDFLAGS ""
433 if [compiler_supports "-static-pie"] {
434 set STATIC_PIE_LDFLAGS "-static-pie"
438 if { ![info exists NOSANITIZE_CFLAGS] } then {
439 set NOSANITIZE_CFLAGS ""
440 if [compiler_supports "-fno-sanitize=all"] {
441 set NOSANITIZE_CFLAGS "-fno-sanitize=all"
445 if { ![info exists NOLTO_CFLAGS] } then {
447 if [compiler_supports "-fno-lto"] {
448 set NOLTO_CFLAGS "-fno-lto"
452 # Provide dummy libraries that alpha-vms-ld always loads
453 if { [istarget alpha*-*-*vms*] } {
454 set src tmpdir/empty.s
455 set f [open $src "w"]
457 if { [ld_assemble $as $src tmpdir/empty.o]
458 && ![ld_link $LD tmpdir/empty tmpdir/empty.o]
459 && [regsub -all {[^\n]*: cannot find -l([^\n]*)} $exec_output {tmpdir/lib\1.a } missing_libs] } {
460 regsub -all {\$} $missing_libs {\\\$} missing_libs
461 for { set i 0 } { $i < [llength $missing_libs] } { incr i } {
462 set f [lindex $missing_libs $i]
463 verbose -log "creating dummy $f"
464 ar_simple_create $ar {} $f tmpdir/empty.o
466 append LDFLAGS " -Ltmpdir"
473 if { [check_compiler_available] } {
479 foreach plug $plugin_names {
480 set plug_so [run_host_cmd $CC_FOR_TARGET "--print-prog-name $plug"]
481 if { $plug_so eq $plug } then {
482 set plug_so [run_host_cmd $CC_FOR_TARGET "--print-file-name $plug"]
484 if { $plug_so ne $plug } then {
485 set plug_opt "--plugin $plug_so"
491 if {[file exists .libs/libdep.so]} {
492 set dep_plug_opt "--plugin .libs/libdep.so"
493 } elseif {[file exists .libs/libdep.dll]} {
494 set dep_plug_opt "--plugin .libs/libdep.dll"