+2017-12-08  Jan Kratochvil  <jan.kratochvil@redhat.com>
+           Pedro Alves  <palves@redhat.com>
+
+       * contrib/cc-with-tweaks.sh: Change interpreter to bash, incl. initial
+       comment.
+       (GDB_ADD_INDEX): New variable.
+       <$want_index>: Call $GDB_ADD_INDEX.
+
 2017-12-08  Sergio Durigan Junior  <sergiodj@redhat.com>
 
        * dtrace-probe.c (dtrace_process_dof_probe): Do not declare a new
 
-#! /bin/sh
+#!/usr/bin/env bash
 # Wrapper around gcc to tweak the output in various ways when running
 # the testsuite.
 
 #
 # bash$ cd $objdir/gdb/testsuite
 # bash$ runtest \
-#   CC_FOR_TARGET="/bin/sh $srcdir/gdb/contrib/cc-with-tweaks.sh ARGS gcc" \
-#   CXX_FOR_TARGET="/bin/sh $srcdir/gdb/contrib/cc-with-tweaks.sh ARGS g++"
+#   CC_FOR_TARGET="/bin/bash $srcdir/gdb/contrib/cc-with-tweaks.sh ARGS gcc" \
+#   CXX_FOR_TARGET="/bin/bash $srcdir/gdb/contrib/cc-with-tweaks.sh ARGS g++"
 #
 # For documentation on Fission and dwp files:
 #     http://gcc.gnu.org/wiki/DebugFission
 # If nothing is given, no changes are made
 
 myname=cc-with-tweaks.sh
+mydir=`dirname "$0"`
 
 if [ -z "$GDB" ]
 then
     shift
 done
 
+if [ "$want_index" = true ]
+then
+    if [ -z "$GDB_ADD_INDEX" ]
+    then
+       if [ -f $mydir/gdb-add-index.sh ]
+       then
+           GDB_ADD_INDEX="$mydir/gdb-add-index.sh"
+       else
+           echo "$myname: unable to find usable contrib/gdb-add-index.sh" >&2
+           exit 1
+       fi
+    fi
+fi
+
 for arg in "$@"
 do
     if [ "$next_is_output_file" = "yes" ]
 fi
 
 if [ "$want_index" = true ]; then
-    $GDB --batch-silent -nx -ex "set auto-load no" -ex "file $output_file" -ex "save gdb-index $output_dir"
-    rc=$?
-    [ $rc != 0 ] && exit $rc
-
-    # GDB might not always create an index.  Cope.
-    if [ -f "$index_file" ]
-    then
-       $OBJCOPY --add-section .gdb_index="$index_file" \
-           --set-section-flags .gdb_index=readonly \
-           "$output_file" "$output_file"
-       rc=$?
-    else
-       rc=0
-    fi
+    # Filter out these messages which would stop dejagnu testcase run:
+    # echo "$myname: No index was created for $file" 1>&2
+    # echo "$myname: [Was there no debuginfo? Was there already an index?]" 1>&2
+    GDB=$GDB $GDB_ADD_INDEX "$output_file" 2>&1|grep -v "^${GDB_ADD_INDEX##*/}: " >&2
+    rc=${PIPESTATUS[0]}
     [ $rc != 0 ] && exit $rc
 fi