testsuite/common/bits32m31.c testsuite/common/bits64m0.c \
        testsuite/common/bits64m63.c
 DISTCLEANFILES = 
-MOSTLYCLEANFILES = core $(am__append_5) site-srcdir.exp testrun.log \
-       testrun.sum $(am__append_7) $(am__append_10) $(am__append_12) \
-       $(am__append_15) $(am__append_17) $(am__append_19) \
-       $(am__append_21) $(am__append_24) $(am__append_26) \
-       $(am__append_29) $(am__append_31) $(am__append_33) \
-       $(am__append_36) $(am__append_38)
+MOSTLYCLEANFILES = core $(am__append_5) site-sim-config.exp \
+       testrun.log testrun.sum $(am__append_7) $(am__append_10) \
+       $(am__append_12) $(am__append_15) $(am__append_17) \
+       $(am__append_19) $(am__append_21) $(am__append_24) \
+       $(am__append_26) $(am__append_29) $(am__append_31) \
+       $(am__append_33) $(am__append_36) $(am__append_38)
 AM_CFLAGS = $(WERROR_CFLAGS) $(WARN_CFLAGS)
 AM_CPPFLAGS = -I$(srcroot)/include $(SIM_INLINE) -I$(srcdir)/common
 COMPILE_FOR_BUILD = $(CC_FOR_BUILD) $(AM_CPPFLAGS) $(CFLAGS_FOR_BUILD)
 
 
 # Tweak the site.exp so it works with plain `runtest` from user.
-EXTRA_DEJAGNU_SITE_CONFIG = site-srcdir.exp
+EXTRA_DEJAGNU_SITE_CONFIG = site-sim-config.exp
 
 # Custom verbose test variables that automake doesn't provide (yet?).
 AM_V_RUNTEST = $(AM_V_RUNTEST_@AM_V@)
 @SIM_ENABLE_IGEN_TRUE@igen/%-main.o: igen/%.c
 @SIM_ENABLE_IGEN_TRUE@ $(AM_V_CC)$(COMPILE_FOR_BUILD) -DMAIN -c $< -o $@
 
-site-srcdir.exp: Makefile
-       $(AM_V_GEN)echo "set srcdir \"$(srcdir)/testsuite\"" > $@
+site-sim-config.exp: Makefile
+       $(AM_V_GEN)( \
+       echo "set builddir \"$(builddir)\""; \
+       echo "set srcdir \"$(srcdir)/testsuite\""; \
+       ) > $@
 
 check-DEJAGNU: site.exp
        $(AM_V_RUNTEST)LC_ALL=C; export LC_ALL; \
 
     # all machines
     set all_machs "bfin"
 
+    global objdir
+
     # See if we have a preprocessor available.
-    if { [target_compile $srcdir/$subdir/usp.S compilercheck.x "preprocess" \
+    if { [target_compile $srcdir/$subdir/usp.S $objdir/compilercheck.x "preprocess" \
          [list "incdir=$srcdir/$subdir"]] == "" } {
        set has_cpp 1
     } {
     }
 
     # See if we have a compiler available.
-    if { [target_compile $srcdir/$subdir/argc.c compilercheck.x "executable" \
+    if { [target_compile $srcdir/$subdir/argc.c $objdir/compilercheck.x "executable" \
          [list "incdir=$srcdir/$subdir" "additional_flags=-msim"]] == "" } {
        set has_cc 1
     } {
 
 }
 
 # Using target_compile, since it is less noisy,
-if { [target_compile $srcdir/$subdir/hello.c compilercheck.x \
+global objdir
+if { [target_compile $srcdir/$subdir/hello.c $objdir/compilercheck.x \
          "executable" "" ] == "" } {
     set has_cc 1
 
     # detrimental effects on the executable from the specs and
     # -static in the board ldflags, we just add -Bdynamic.
     if [regexp "(.*/lib)/libc.so" \
-           [target_compile $srcdir/$subdir/hello.c compilercheck.x \
+           [target_compile $srcdir/$subdir/hello.c $objdir/compilercheck.x \
                 "executable" \
                 "ldflags=-print-file-name=libc.so -Wl,-Bdynamic"] \
            xxx libcsodir]  {
 
     verbose -log "Compiling $src with $opts(cc)"
 
-    if { [target_compile $src "$testname.x" "executable" "$opts(cc)" ] != "" } {
+    if { [target_compile $src "$objdir/$testname.x" "executable" "$opts(cc)" ] != "" } {
        unresolved $testname
        continue
     }
        eval setup_kfail $opts(kfail)
     }
 
-    set result [sim_run "$testname.x" "$opts(sim,$mach)" "$opts(progoptions)" \
+    set result [sim_run "$objdir/$testname.x" "$opts(sim,$mach)" "$opts(progoptions)" \
        "" ""]
     set return_code [lindex $result 0]
     set output [lindex $result 1]
 
       if (fnam == NULL)
        abort ();
       strcpy (fnam, "/");
-      strcat (fnam, argv[0]);
+      strcat (fnam, basename (argv[0]));
     }
 
   f = fopen (fnam, "rb");
 
 
 int main (int argc, char *argv[])
 {
-  /* Avoid getting files with random characters due to errors
-     elsewhere.  */
-  if (argc != 1
-      || (argv[0][0] != '.' && argv[0][0] != '/' && argv[0][0] != 'r'))
-    abort ();
-
   if (rename (argv[0], NULL) != -1
       || errno != EFAULT)
     err ("rename 1 ");
 
   char path[1024] = "/";
   struct stat buf;
 
-  strcat (path, argv[0]);
+  strcat (path, basename (argv[0]));
   if (stat (".", &buf) != 0
       || !S_ISDIR (buf.st_mode))
     abort ();
 
 proc sim_has_rv_and_cris {} {
     global srcdir
     global subdir
+    global objdir
     global SIMFLAGS_FOR_TARGET
 
     # We need to assemble and link a trivial program and pass that, in
        set SIMFLAGS_FOR_TARGET ""
     }
 
-    set comp_output [target_assemble $srcdir/$subdir/quit.s quit.o \
+    set comp_output [target_assemble $srcdir/$subdir/quit.s $objdir/quit.o \
                         "-I$srcdir/$subdir"]
 
     if ![string match "" $comp_output] {
        return 0
     }
 
-    set comp_output [target_link quit.o quit.x ""]
+    set comp_output [target_link $objdir/quit.o $objdir/quit.x ""]
 
     if ![string match "" $comp_output] {
        verbose -log "$comp_output" 3
     }
 
     set result \
-       [sim_run quit.x \
+       [sim_run $objdir/quit.x \
             "$SIMFLAGS_FOR_TARGET --hw-device rv --hw-device cris --hw-info" \
             "" "" ""]
     set return_code [lindex $result 0]
     # See the logic in sim-defs.exp for more details.
     set sim [board_info target sim]
     if [string equal "" $sim] {
-       global objdir
-       set rvdummy "$objdir/cris/rvdummy"
+       global builddir
+       set rvdummy "$builddir/cris/rvdummy"
     } else {
        set rvdummy "[file dirname [board_info target sim]]/rvdummy"
     }
 
     global sim_path
     set sim "$sim_path"
     if [string equal "" $sim] {
-       global objdir
+       global builddir
        global subdir
        set arch "$subdir"
        while { [file dirname $arch] != "." } {
            set arch [file dirname $arch]
        }
-       return "$objdir/$arch/run"
+       return "$builddir/$arch/run"
     }
     return "$sim"
 }
 # optional for xfail.
 
 proc run_sim_test { name requested_machs } {
-    global subdir srcdir
+    global subdir srcdir objdir
     global opts
     global cpu_option
     global cpu_option_sep
        }
 
        if [string match "*.c" $sourcefile] {
-           set comp_output [target_compile $sourcefile ${name}.x "executable" \
+           set comp_output [target_compile $sourcefile $objdir/${name}.x "executable" \
                [list "incdir=$srcdir/$subdir" "additional_flags=$c_as_options $c_ld_options $opts(cc,$mach)"]]
            set method "compiling/linking"
        } else {
            if [string match "*.S" $sourcefile] {
-               set comp_output [target_compile $sourcefile ${name}.o "object" \
+               set comp_output [target_compile $sourcefile $objdir/${name}.o "object" \
                    [list "incdir=$srcdir/$subdir" "additional_flags=$c_as_options"]]
                set method "compiling"
            } else {
-               set comp_output [target_assemble $sourcefile ${name}.o "$as_options"]
+               set comp_output [target_assemble $sourcefile $objdir/${name}.o "$as_options"]
                set method "assembling"
            }
 
                continue
            }
 
-           set comp_output [target_link ${name}.o ${name}.x "$opts(ld,$mach)"]
+           set comp_output [target_link $objdir/${name}.o $objdir/${name}.x "$opts(ld,$mach)"]
            set method "linking"
        }
 
            set options "$options timeout=$opts(timeout)"
        }
 
-       set result [sim_run ${name}.x "$opts(sim,$mach) $SIMFLAGS_FOR_TARGET" "$opts(progopts)" "" "$options"]
+       set result [sim_run $objdir/${name}.x "$opts(sim,$mach) $SIMFLAGS_FOR_TARGET" "$opts(progopts)" "" "$options"]
        set return_code [lindex $result 0]
        set output [lindex $result 1]
 
            if { "$opts(xerror)" == "no" } {
                if [string match $opts(output) $output] {
                    pass "$mach $testname"
-                   file delete ${name}.o ${name}.x
+                   file delete $objdir/${name}.o $objdir/${name}.x
                } else {
                    verbose -log "status:  $return_code" 3
                    verbose -log "output:  $output" 3
            } else {
                if [string match $opts(output) $output] {
                    pass "$mach $testname"
-                   file delete ${name}.o ${name}.x
+                   file delete $objdir/${name}.o $objdir/${name}.x
                } else {
                    verbose -log "status:  $return_code" 3
                    verbose -log "output:  $output" 3
 
 # along with this program.  If not, see <http://www.gnu.org/licenses/>.
 
 # Tweak the site.exp so it works with plain `runtest` from user.
-EXTRA_DEJAGNU_SITE_CONFIG = site-srcdir.exp
+EXTRA_DEJAGNU_SITE_CONFIG = site-sim-config.exp
 
 # Custom verbose test variables that automake doesn't provide (yet?).
 AM_V_RUNTEST = $(AM_V_RUNTEST_@AM_V@)
 AM_V_RUNTEST_0 =  @echo "  RUNTEST  $(RUNTESTFLAGS)";
 AM_V_RUNTEST_1 =
 
-site-srcdir.exp: Makefile
-       $(AM_V_GEN)echo "set srcdir \"$(srcdir)/testsuite\"" > $@
+site-sim-config.exp: Makefile
+       $(AM_V_GEN)( \
+       echo "set builddir \"$(builddir)\""; \
+       echo "set srcdir \"$(srcdir)/testsuite\""; \
+       ) > $@
 
 check-DEJAGNU: site.exp
        $(AM_V_RUNTEST)LC_ALL=C; export LC_ALL; \
        fi
 
 MOSTLYCLEANFILES += \
-       site-srcdir.exp testrun.log testrun.sum
+       site-sim-config.exp testrun.log testrun.sum
 
 include %D%/common/local.mk