| 1 | # Basic expect script for LD Regression Tests |
| 2 | # Copyright (C) 1993-2016 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 | 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 |
| 66 | } |
| 67 | set gcc_B_opt "-B[pwd]/tmpdir/ld/" |
| 68 | |
| 69 | # load the linker path |
| 70 | set ld_L_opt "" |
| 71 | if {[file exists tmpdir/libpath.exp]} { |
| 72 | load_lib tmpdir/libpath.exp |
| 73 | |
| 74 | foreach dir $libpath { |
| 75 | append ld_L_opt " -L$dir" |
| 76 | } |
| 77 | } |
| 78 | |
| 79 | # The "make check" target in the Makefile passes in |
| 80 | # "CC=$(CC_FOR_TARGET)". But, if the user invokes runtest directly |
| 81 | # (as when testing an installed linker), these flags may not be set. |
| 82 | if {![info exists CC]} { |
| 83 | set CC [find_gcc] |
| 84 | } |
| 85 | if {![info exists CFLAGS]} { |
| 86 | set CFLAGS "-g -O2" |
| 87 | } |
| 88 | if {![info exists CXX]} { |
| 89 | set CXX [find_g++] |
| 90 | } |
| 91 | if {![info exists CXXFLAGS]} { |
| 92 | set CXXFLAGS "" |
| 93 | } |
| 94 | |
| 95 | # The mips64-*-linux-gnu compiler defaults to the N32 ABI after |
| 96 | # installed, but to the O32 ABI in the build tree, because of some |
| 97 | # specs-file hacks. Make sure we use an ABI that is compatible with |
| 98 | # the one we expect. |
| 99 | if {[istarget mips64*-*-linux*] && |
| 100 | (![board_info [target_info name] exists multilib_flags] || |
| 101 | ![string match "*-mabi" [board_info [target_info name] multilib_flags]]) |
| 102 | } { |
| 103 | append gcc_B_opt " -mabi=n32" |
| 104 | } |
| 105 | |
| 106 | if { [istarget rx-*-*] } { |
| 107 | global ASFLAGS |
| 108 | set ASFLAGS "-muse-conventional-section-names" |
| 109 | } |
| 110 | |
| 111 | # load the utility procedures |
| 112 | load_lib ld-lib.exp |
| 113 | |
| 114 | proc get_link_files {varname} { |
| 115 | global $varname |
| 116 | global target_triplet |
| 117 | global srcdir |
| 118 | global CC |
| 119 | if ![info exists $varname] { |
| 120 | #configure.host returns variables that can be substituted into |
| 121 | #makefile rules, with embedded shell variable expansions. |
| 122 | #make wants $$shell_var, we want $shell_var ... |
| 123 | set cmd "host='$target_triplet' && . $srcdir/../configure.host && sed -e 's,\\\$\\\$,\$,g' <<EOF\n\$$varname\nEOF" |
| 124 | set status [catch "exec sh -c [list $cmd]" result] |
| 125 | if $status { error "Error getting native link files: $result" } |
| 126 | set cmd "CC='$CC' && eval echo \"$result\"" |
| 127 | set status [catch "exec sh -c [list $cmd]" result] |
| 128 | if $status { error "Error getting native link files: $result" } |
| 129 | set $varname $result |
| 130 | send_log "$varname = $result\n" |
| 131 | } |
| 132 | } |
| 133 | |
| 134 | proc get_target_emul {} { |
| 135 | global target_triplet |
| 136 | global srcdir |
| 137 | set status [catch "exec sh -c \"targ='$target_triplet' && . $srcdir/../configure.tgt && echo \\\$targ_emul\"" result] |
| 138 | if $status { error "Error getting emulation name: $result" } |
| 139 | return $result |
| 140 | } |
| 141 | |
| 142 | if [isnative] { |
| 143 | foreach x {HOSTING_CRT0 HOSTING_SCRT0 HOSTING_LIBS HOSTING_SLIBS} { |
| 144 | get_link_files $x |
| 145 | } |
| 146 | } else { |
| 147 | foreach x {HOSTING_CRT0 HOSTING_SCRT0 HOSTING_LIBS HOSTING_SLIBS} { set $x "" } |
| 148 | } |
| 149 | if ![info exists HOSTING_EMU] { set HOSTING_EMU "-m [get_target_emul]" } |
| 150 | |
| 151 | # |
| 152 | # ld_version -- extract and print the version number of ld compiler (GCC) |
| 153 | # |
| 154 | proc ld_version {} { |
| 155 | global ld |
| 156 | default_ld_version $ld |
| 157 | } |
| 158 | |
| 159 | # |
| 160 | # ld_exit -- just a stub for ld |
| 161 | # |
| 162 | proc ld_exit {} { |
| 163 | } |
| 164 | |
| 165 | # |
| 166 | # ld_start |
| 167 | # relink the linker |
| 168 | # |
| 169 | proc ld_start { ld target } { |
| 170 | # |
| 171 | } |
| 172 | |
| 173 | # |
| 174 | # ld_relocate |
| 175 | # link an object using relocation |
| 176 | # |
| 177 | proc ld_relocate { ld target objects } { |
| 178 | default_ld_relocate $ld $target $objects |
| 179 | } |
| 180 | |
| 181 | # |
| 182 | # ld_link |
| 183 | # link a program using ld |
| 184 | # |
| 185 | proc ld_link { ld target objects } { |
| 186 | default_ld_link $ld $target $objects |
| 187 | } |
| 188 | |
| 189 | # |
| 190 | # ld_simple_link |
| 191 | # link a program using ld, without including any libraries |
| 192 | # |
| 193 | proc ld_simple_link { ld target objects } { |
| 194 | default_ld_simple_link $ld $target $objects |
| 195 | } |
| 196 | |
| 197 | # |
| 198 | # ld_compile |
| 199 | # compile an object using $cc |
| 200 | # |
| 201 | proc ld_compile { cc source object } { |
| 202 | default_ld_compile $cc $source $object |
| 203 | } |
| 204 | |
| 205 | # |
| 206 | # ld_assemble |
| 207 | # assemble a file |
| 208 | # |
| 209 | proc ld_assemble { as source object } { |
| 210 | default_ld_assemble $as "" $source $object |
| 211 | } |
| 212 | |
| 213 | # |
| 214 | # ld_assemble_flags |
| 215 | # assemble a file with extra flags |
| 216 | # |
| 217 | proc ld_assemble_flags { as flags source object } { |
| 218 | default_ld_assemble $as $flags $source $object |
| 219 | } |
| 220 | |
| 221 | # |
| 222 | # ld_nm |
| 223 | # run nm on a file |
| 224 | # |
| 225 | proc ld_nm { nm nmflags object } { |
| 226 | default_ld_nm $nm $nmflags $object |
| 227 | } |
| 228 | |
| 229 | # |
| 230 | # ld_exec |
| 231 | # execute ithe target |
| 232 | # |
| 233 | proc ld_exec { target output } { |
| 234 | default_ld_exec $target $output |
| 235 | } |
| 236 | |
| 237 | # From gas-defs.exp, to support run_dump_test. |
| 238 | if ![info exists AS] then { |
| 239 | set AS $as |
| 240 | } |
| 241 | |
| 242 | if ![info exists ASFLAGS] then { |
| 243 | set ASFLAGS "" |
| 244 | } |
| 245 | |
| 246 | if ![info exists OBJDUMP] then { |
| 247 | set OBJDUMP $objdump |
| 248 | } |
| 249 | |
| 250 | if ![info exists OBJDUMPFLAGS] then { |
| 251 | set OBJDUMPFLAGS {} |
| 252 | } |
| 253 | |
| 254 | if ![info exists NM] then { |
| 255 | set NM $nm |
| 256 | } |
| 257 | |
| 258 | if ![info exists NMFLAGS] then { |
| 259 | set NMFLAGS {} |
| 260 | } |
| 261 | |
| 262 | if ![info exists OBJCOPY] then { |
| 263 | set OBJCOPY $objcopy |
| 264 | } |
| 265 | |
| 266 | if ![info exists OBJCOPYFLAGS] then { |
| 267 | set OBJCOPYFLAGS {} |
| 268 | } |
| 269 | |
| 270 | if ![info exists READELF] then { |
| 271 | set READELF [findfile $base_dir/../binutils/readelf] |
| 272 | } |
| 273 | |
| 274 | if ![info exists READELFFLAGS] then { |
| 275 | set READELFFLAGS {} |
| 276 | } |
| 277 | |
| 278 | if ![info exists LD] then { |
| 279 | set LD [findfile $base_dir/ld-new ./ld-new [transform ld]] |
| 280 | } |
| 281 | |
| 282 | if ![info exists LDFLAGS] then { |
| 283 | set LDFLAGS {} |
| 284 | } |
| 285 | |
| 286 | # Set PLT_CFLAGS to "-fplt" if target compiler supports it. |
| 287 | |
| 288 | if { ![info exists PLT_CFLAGS] } then { |
| 289 | if { [which $CC] != 0 } { |
| 290 | # Check if gcc supports -fplt |
| 291 | set flags "" |
| 292 | if [board_info [target_info name] exists cflags] { |
| 293 | append flags " [board_info [target_info name] cflags]" |
| 294 | } |
| 295 | if [board_info [target_info name] exists ldflags] { |
| 296 | append flags " [board_info [target_info name] ldflags]" |
| 297 | } |
| 298 | |
| 299 | set basename "tmpdir/plt[pid]" |
| 300 | set src ${basename}.c |
| 301 | set output ${basename}.o |
| 302 | set f [open $src "w"] |
| 303 | puts $f "" |
| 304 | close $f |
| 305 | if [is_remote host] { |
| 306 | set src [remote_download host $src] |
| 307 | } |
| 308 | set plt_available [run_host_cmd_yesno "$CC" "$flags -c -fplt $src -o $output"] |
| 309 | remote_file host delete $src |
| 310 | remote_file host delete $output |
| 311 | file delete $src |
| 312 | |
| 313 | if { $plt_available == 1 } then { |
| 314 | set PLT_CFLAGS "-fplt" |
| 315 | } else { |
| 316 | set PLT_CFLAGS "" |
| 317 | } |
| 318 | } else { |
| 319 | set PLT_CFLAGS "" |
| 320 | } |
| 321 | } |
| 322 | |
| 323 | # Set NOPIE_CFLAGS to "-fno-PIE" and NOPIE_LDFLAGS to "-no-pie" if |
| 324 | # target compiler supports them. |
| 325 | |
| 326 | if { ![info exists NOPIE_CFLAGS] || ![info exists NOPIE_LDFLAGS] } then { |
| 327 | if { [which $CC] != 0 } { |
| 328 | # Check if gcc supports -fno-PIE -no-pie. |
| 329 | set flags "" |
| 330 | if [board_info [target_info name] exists cflags] { |
| 331 | append flags " [board_info [target_info name] cflags]" |
| 332 | } |
| 333 | if [board_info [target_info name] exists ldflags] { |
| 334 | append flags " [board_info [target_info name] ldflags]" |
| 335 | } |
| 336 | |
| 337 | set basename "tmpdir/nopie[pid]" |
| 338 | set src ${basename}.c |
| 339 | set output ${basename} |
| 340 | set f [open $src "w"] |
| 341 | puts $f "int main (void) { return 0; }" |
| 342 | close $f |
| 343 | if [is_remote host] { |
| 344 | set src [remote_download host $src] |
| 345 | } |
| 346 | set nopie_available [run_host_cmd_yesno "$CC" "$flags -fno-PIE -no-pie $src -o $output"] |
| 347 | remote_file host delete $src |
| 348 | remote_file host delete $output |
| 349 | file delete $src |
| 350 | |
| 351 | if { $nopie_available == 1 } then { |
| 352 | set NOPIE_CFLAGS "-fno-PIE" |
| 353 | set NOPIE_LDFLAGS "-no-pie" |
| 354 | } else { |
| 355 | set NOPIE_CFLAGS "" |
| 356 | set NOPIE_LDFLAGS "" |
| 357 | } |
| 358 | } else { |
| 359 | set NOPIE_CFLAGS "" |
| 360 | set NOPIE_LDFLAGS "" |
| 361 | } |
| 362 | } |