remote_exec host "mkdir -p tmpdir"
 
-# Make a symlink from tmpdir/as to the assembler in the build tree, so
-# that we can use a -B option to gcc to force it to use the newly
-# built assembler.
-if {![file isdirectory tmpdir/gas]} then {
-    catch "exec mkdir tmpdir/gas" status
-    catch "exec ln -s ../../../gas/as-new tmpdir/gas/as" status
-}
-set gcc_gas_flag "-B[pwd]/tmpdir/gas/"
-
-# Make a symlink from tmpdir/ld to the linker in the build tree, so
-# that we can use a -B option to gcc to force it to use the newly
-# built linker. 
+# Make symlinks from tmpdir/ld to the linker and assembler in the
+# build tree, so that we can use a -B option to gcc to force it to use
+# the newly built linker and assembler. 
 if {![file isdirectory tmpdir/ld]} then {
     catch "exec mkdir tmpdir/ld" status
     catch "exec ln -s ../../ld-new tmpdir/ld/ld" status
     catch "exec ln -s ld tmpdir/ld/collect-ld" status
+    catch "exec ln -s ../../../gas/as-new tmpdir/ld/as" status
 }
-set gcc_ld_flag "-B[pwd]/tmpdir/ld/"
+set gcc_B_opt "-B[pwd]/tmpdir/ld/"
 
 # load the linker path
+set ld_L_opt ""
 if {[file exists tmpdir/libpath.exp]} {
     load_lib tmpdir/libpath.exp
 
     foreach dir $libpath {
-       set gcc_ld_flag "$gcc_ld_flag -L$dir"
+       append ld_L_opt " -L$dir"
     }
 }
 
     (![board_info [target_info name] exists multilib_flags] ||
      ![string match "*-mabi" [board_info [target_info name] multilib_flags]])
    } {
-    append gcc_gas_flag " -mabi=n32"
+    append gcc_B_opt " -mabi=n32"
 }
 
 if { [istarget rx-*-*] } {
 
 
 # Check --no-add-needed and --no-copy-dt-needed-entries
 set testname "--no-add-needed"
-set exec_output [run_host_cmd "$CC" "$gcc_gas_flag $gcc_ld_flag tmpdir/libneeded1c.o -Wl,--no-add-needed,-rpath-link=tmpdir -Ltmpdir -lneeded1a"]
+set exec_output [run_host_cmd "$CC" "tmpdir/libneeded1c.o -Wl,--no-add-needed,-rpath-link=tmpdir -Ltmpdir -lneeded1a"]
 if { [ regexp "tmpdir/libneeded1b.so: .*: DSO missing" $exec_output ] } {
     pass $testname
 } {
     fail $testname
 }
 set testname "--no-copy-dt-needed-entries"
-set exec_output [run_host_cmd "$CC" "$gcc_gas_flag $gcc_ld_flag tmpdir/libneeded1c.o -Wl,--no-copy-dt-needed-entries,-rpath-link=tmpdir -Ltmpdir -lneeded1a"]
+set exec_output [run_host_cmd "$CC" "tmpdir/libneeded1c.o -Wl,--no-copy-dt-needed-entries,-rpath-link=tmpdir -Ltmpdir -lneeded1a"]
 if { [ regexp "tmpdir/libneeded1b.so: .*: DSO missing" $exec_output ] } {
     pass $testname
 } {
     fail $testname
 }
 set testname "--no-add-needed -shared"
-set exec_output [run_host_cmd "$CC" "$gcc_gas_flag $gcc_ld_flag -shared tmpdir/libneeded1pic.o -Wl,--no-add-needed,-z,defs -Ltmpdir -lneeded1a"]
+set exec_output [run_host_cmd "$CC" "-shared tmpdir/libneeded1pic.o -Wl,--no-add-needed,-z,defs -Ltmpdir -lneeded1a"]
 if { [ regexp "undefined reference to `bar'" $exec_output ] } {
     pass $testname
 } {
     fail $testname
 }
 set testname "--no-copy-dt-needed-entries -shared"
-set exec_output [run_host_cmd "$CC" "$gcc_gas_flag $gcc_ld_flag -shared tmpdir/libneeded1pic.o -Wl,--no-copy-dt-needed-entries,-z,defs -Ltmpdir -lneeded1a"]
+set exec_output [run_host_cmd "$CC" "-shared tmpdir/libneeded1pic.o -Wl,--no-copy-dt-needed-entries,-z,defs -Ltmpdir -lneeded1a"]
 if { [ regexp "undefined reference to `bar'" $exec_output ] } {
     pass $testname
 } {
 
 
 proc run_host_cmd { prog command } {
     global link_output
+    global gcc_B_opt
+    global ld_L_opt
 
     if { ![is_remote host] && [which "$prog"] == 0 } then {
        perror "$prog does not exist"
        return 0
     }
 
-    verbose -log "$prog $command"
-    set status [remote_exec host [concat sh -c [list "$prog $command 2>&1"]] "" "/dev/null" "ld.tmp"]
+    # If we are compiling with gcc, we want to add gcc_B_opt and
+    # ld_L_opt to flags.  However, if $prog already has -B options,
+    # which might be the case when running gcc out of a build
+    # directory, we want our -B options to come first.
+    set gccexe $prog
+    set gccparm [string first " " $gccexe]
+    set gccflags ""
+    if { $gccparm > 0 } then {
+       set gccflags [string range $gccexe $gccparm end]
+       set gccexe [string range $gccexe 0 $gccparm]
+       set prog $gccexe
+    }
+    set gccexe [string replace $gccexe 0 [string last "/" $gccexe] ""]
+    if {[string match "*cc*" $gccexe] || [string match "*++*" $gccexe]} then {
+       set gccflags "$gcc_B_opt $gccflags $ld_L_opt"
+    }
+
+    verbose -log "$prog $gccflags $command"
+    set status [remote_exec host [concat sh -c [list "$prog $gccflags $command 2>&1"]] "" "/dev/null" "ld.tmp"]
     remote_upload host "ld.tmp"
     set link_output [file_contents "ld.tmp"]
     regsub "\n$" $link_output "" link_output
 #
 proc default_ld_simple_link { ld target objects } {
     global host_triplet
-    global gcc_ld_flag
     global exec_output
 
+    set flags ""
     if [is_endian_output_format $objects] then {
        set flags [big_or_little_endian]
-    } else {
-       set flags ""
-    }
-
-    # If we are compiling with gcc, we want to add gcc_ld_flag to
-    # flags.  Rather than determine this in some complex way, we guess
-    # based on the name of the compiler.
-    set ldexe $ld
-    set ldparm [string first " " $ld]
-    set ldflags ""
-    if { $ldparm > 0 } then {
-       set ldflags [string range $ld $ldparm end]
-       set ldexe [string range $ld 0 $ldparm]
-       set ld $ldexe
-    }
-    set ldexe [string replace $ldexe 0 [string last "/" $ldexe] ""]
-    if {[string match "*gcc*" $ldexe] || [string match "*++*" $ldexe]} then {
-       set ldflags "$gcc_ld_flag $ldflags"
     }
 
     remote_file host delete $target
-
-    set exec_output [run_host_cmd "$ld" "$ldflags $flags -o $target $objects"]
+    set exec_output [run_host_cmd "$ld" "$flags -o $target $objects"]
     set exec_output [prune_warnings $exec_output]
 
     # We don't care if we get a warning about a non-existent start
     # symbol, since the default linker script might use ENTRY.
     regsub -all "(^|\n)(\[^\n\]*: warning: cannot find entry symbol\[^\n\]*\n?)" $exec_output "\\1" exec_output
 
-    if [string match "" $exec_output] then {
-       return 1
-    } else {
-       return 0
-    }
+    return [string match "" $exec_output]
 }
 
 # Compile an object using cc.
     global srcdir
     global subdir
     global host_triplet
-    global gcc_gas_flag
+    global gcc_B_opt
 
     set cc_prog $cc
     if {[llength $cc_prog] > 1} then {
     remote_file build delete "$object"
     remote_file host delete "$object"
 
-    set flags "-I$srcdir/$subdir"
+    set flags "$gcc_B_opt -I$srcdir/$subdir"
 
-    # If we are compiling with gcc, we want to add gcc_gas_flag to
-    # flags.  Rather than determine this in some complex way, we guess
-    # based on the name of the compiler.
+    # If we are compiling with gcc, we want to add gcc_B_opt to flags.
+    # However, if $prog already has -B options, which might be the
+    # case when running gcc out of a build directory, we want our -B
+    # options to come first.
     set ccexe $cc
     set ccparm [string first " " $cc]
     set ccflags ""
        set ccexe [string range $cc 0 $ccparm]
        set cc $ccexe
     }
-    set ccexe [string replace $ccexe 0 [string last "/" $ccexe] ""]
-    if {[string match "*gcc*" $ccexe] || [string match "*++*" $ccexe]} then {
-       set flags "$gcc_gas_flag $flags"
-    }
 
+    set ccexe [string replace $ccexe 0 [string last "/" $ccexe] ""]
     if {[string match "*++*" $ccexe]} {
-       set flags "$flags $CXXFLAGS"
+       append flags " $CXXFLAGS"
     } else {
-       set flags "$flags $CFLAGS"
+       append flags " $CFLAGS"
     }
 
     if [board_info [target_info name] exists cflags] {
        append flags " [board_info [target_info name] multilib_flags]"
     }
 
-    verbose -log "$cc $flags $ccflags -c $source -o $object"
+    set cmd "$cc $flags $ccflags -c $source -o $object"
+    verbose -log "$cmd"
 
-    set status [remote_exec host [concat sh -c [list "$cc $flags $ccflags -c $source -o $object 2>&1"]] "" "/dev/null" "ld.tmp"]
+    set status [remote_exec host [concat sh -c [list "$cmd 2>&1"]] "" "/dev/null" "ld.tmp"]
     remote_upload host "ld.tmp"
     set exec_output [file_contents "ld.tmp"]
     remote_file build delete "ld.tmp"
     global lto_available_saved
     global CC
 
-    set flags ""
-
-    if [board_info [target_info name] exists cflags] {
-        append flags " [board_info [target_info name] cflags]"
-    }
-
-    if [board_info [target_info name] exists ldflags] {
-        append flags " [board_info [target_info name] ldflags]"
-    }
-
     if {![info exists lto_available_saved]} {
        # Check if gcc supports -flto -fuse-linker-plugin
-       if { [which $CC] == 0 } {
-           set lto_available_saved 0
-           return 0
+       set flags ""
+       if [board_info [target_info name] exists cflags] {
+           append flags " [board_info [target_info name] cflags]"
+       }
+       if [board_info [target_info name] exists ldflags] {
+           append flags " [board_info [target_info name] ldflags]"
        }
-       set basename "lto"
-       set src ${basename}[pid].c
-       set output ${basename}[pid].out
+
+       set basename "tmpdir/lto[pid]"
+       set src ${basename}.c
+       set output ${basename}.out
        set f [open $src "w"]
        puts $f "int main() { return 0; }"
        close $f
-       set status [remote_exec host $CC "$flags -B[pwd]/tmpdir/ld/ -flto -ffat-lto-objects -fuse-linker-plugin $src -o $output"]
-       if { [lindex $status 0] == 0 } {
-           set lto_available_saved 1
-       } else {
-           set lto_available_saved 0
-       }
+       remote_download host $src
+       set lto_available_saved [run_host_cmd_yesno "$CC" "$flags -flto -ffat-lto-objects -fuse-linker-plugin $src -o $output"]
+       remote_file host delete $src
+       remote_file host delete $output
        file delete $src
-       file delete $output
     }
     return $lto_available_saved
 }
     global lto_shared_available_saved
     global CC
 
-    set flags ""
-
-    if [board_info [target_info name] exists cflags] {
-        append flags " [board_info [target_info name] cflags]"
-    }
-
-    if [board_info [target_info name] exists ldflags] {
-        append flags " [board_info [target_info name] ldflags]"
-    }
-
     if {![info exists lto_shared_available_saved]} {
        # Check if gcc supports -flto -fuse-linker-plugin -shared
-       if { [which $CC] == 0 } {
-           set lto_shared_available_saved 0
-           return 0
+       set flags ""
+       if [board_info [target_info name] exists cflags] {
+           append flags " [board_info [target_info name] cflags]"
+       }
+       if [board_info [target_info name] exists ldflags] {
+           append flags " [board_info [target_info name] ldflags]"
        }
-       set basename "lto_shared"
-       set src ${basename}[pid].c
-       set output ${basename}[pid].so
+
+       set basename "tmpdir/lto_shared[pid]"
+       set src ${basename}.c
+       set output ${basename}.so
        set f [open $src "w"]
        puts $f ""
        close $f
-       set status [remote_exec host $CC "$flags -shared -fPIC -B[pwd]/tmpdir/ld/ -flto -fuse-linker-plugin $src -o $output"]
-       if { [lindex $status 0] == 0 } {
-           set lto_shared_available_saved 1
-       } else {
-           set lto_shared_available_saved 0
-       }
+       remote_download host $src
+       set lto_shared_available_saved [run_host_cmd_yesno "$CC" "$flags -shared -fPIC -flto -fuse-linker-plugin $src -o $output"]
+       remote_file host delete $src
+       remote_file host delete $output
        file delete $src
-       file delete $output
     }
     return $lto_shared_available_saved
 }