+2017-10-13  Pedro Alves  <palves@redhat.com>
+
+       * gdb.base/scope.exp: Use build_executable + clean_restart +
+       gdb_file_cmd instead of prepare_for_testing and no longer skip
+       "before run" tests on is_remote target boards.  Update comments.
+       * boards/native-extended-gdbserver.exp
+       (extended_gdbserver_load_last_file): New, factored out from ...
+       (gdb_load): ... this.  Move further below and add comment.
+       (extended_gdbserver_gdb_file_cmd, gdb_file_cmd, gdb_reload): New.
+
 2017-10-12  Pedro Alves  <palves@redhat.com>
 
        * gdb.base/attach.exp: Remove references to gdb64.
 
     return 0
 }
 
-# Overriden in order to set the remote exec-file whenever a file is
-# loaded to gdb.
-#
-proc gdb_load { arg } {
+# Helper that runs "set remote exec-file" with the last loaded file.
+
+proc extended_gdbserver_load_last_file {} {
     global gdb_prompt
     global last_loaded_file
 
-    if { $arg != "" } {
-       if [gdb_file_cmd $arg] then { return -1 }
-    }
-
     send_gdb "set remote exec-file $last_loaded_file\n"
     gdb_expect {
        -re "$gdb_prompt $" {}
     return 0
 }
 
+# Overriden in order to set the remote exec-file whenever a file is
+# loaded to gdb.
+#
+if { [info procs extended_gdbserver_gdb_file_cmd] == "" } {
+    rename gdb_file_cmd extended_gdbserver_gdb_file_cmd
+}
+proc gdb_file_cmd { arg } {
+    if [extended_gdbserver_gdb_file_cmd $arg] {
+       return -1
+    }
+    return [extended_gdbserver_load_last_file]
+}
+
+proc gdb_reload { } {
+    return [extended_gdbserver_load_last_file]
+}
+
+# With the two procedure overrides above, it shouldn't be necessary to
+# override this one too.  However, not doing so regresses
+# gdb.base/dbx.exp.  See comments above gdb.base/dbx.exp:gdb_file_cmd.
+# Once testing of the "symbol-file"/"exec-file" commands is moved out
+# to a separate non-dbx testcase, we should be able to remove this.
+proc gdb_load { arg } {
+    if { $arg != "" } {
+       if [gdb_file_cmd $arg] then { return -1 }
+    }
+
+    return [extended_gdbserver_load_last_file]
+}
+
+
 # Likewise, for MI.
 #
 if { [info procs extended_gdbserver_mi_gdb_load] == "" } {
 
 
 standard_testfile scope0.c scope1.c
 
-if {[prepare_for_testing "failed to prepare" ${testfile} \
+if {[build_executable "failed to build" ${testfile} \
         [list $srcfile $srcfile2] {debug}]} {
     return -1
 }
     gdb_stop_suppressing_tests
 }
 
+# Note, we don't pass a binfile to clean_restart because we need to
+# avoid gdb_load here.  With remote targets, gdb_load connects to the
+# remote target, and we specificaly want the following tests to run
+# before the program is started.
+clean_restart
+gdb_file_cmd $binfile
+
 # Test that variables in various segments print out correctly before
 # the program is run.  
 
 
 gdb_test "print 'scope0.c'::filelocal_ro" "= 201"
 
-# gdb currently cannot access bss memory on some targets if the inferior
-# is not running.
-#
-# For PA boards using monitor/remote-pa.c, the bss test is going to 
-# randomly fail.  We've already put remote-pa on the target stack,
-# so we actually read memory from the board.  Problem is crt0.o
-# is responsible for clearing bss and that hasnt' happened yet.
+# Check that gdb can access bss memory if the inferior is not running.
 #
-# This is a problem for all non-native targets. -- manson
-if [is_remote target] {
-    unsupported "print 'scope0.c'::filelocal_bss before run"
-} else {
-    gdb_test "print 'scope0.c'::filelocal_bss" "= 0" \
-       "print 'scope0.c'::filelocal_bss before run"
-}
+# Note the care above about avoiding gdb_load.  Otherwise, for
+# embedded stub-like boards, this test would randomly fail.  If we'd
+# already put target remote on the target stack, we'd read memory from
+# the board instead of the program's binary, and we would have
+# connected before crt0.o had had a chance to clear bss.
+gdb_test "print 'scope0.c'::filelocal_bss" "= 0" \
+    "print 'scope0.c'::filelocal_bss before run"
 
 gdb_test "print 'scope0.c'::filelocal" "= 1" \
     "print 'scope0.c'::filelocal before run"