gdb/contrib/gdb-add-index.sh - gdb

  1. #! /bin/sh

  2. # Add a .gdb_index section to a file.

  3. # Copyright (C) 2010-2015 Free Software Foundation, Inc.
  4. # This program is free software; you can redistribute it and/or modify
  5. # it under the terms of the GNU General Public License as published by
  6. # the Free Software Foundation; either version 3 of the License, or
  7. # (at your option) any later version.
  8. #
  9. # This program is distributed in the hope that it will be useful,
  10. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  12. # GNU General Public License for more details.
  13. #
  14. # You should have received a copy of the GNU General Public License
  15. # along with this program.  If not, see <http://www.gnu.org/licenses/>.

  16. # This program assumes gdb and objcopy are in $PATH.
  17. # If not, or you want others, pass the following in the environment
  18. GDB=${GDB:=gdb}
  19. OBJCOPY=${OBJCOPY:=objcopy}

  20. myname="${0##*/}"

  21. if test $# != 1; then
  22.     echo "usage: $myname FILE" 1>&2
  23.     exit 1
  24. fi

  25. file="$1"

  26. if test ! -r "$file"; then
  27.     echo "$myname: unable to access: $file" 1>&2
  28.     exit 1
  29. fi

  30. dir="${file%/*}"
  31. test "$dir" = "$file" && dir="."
  32. index="${file}.gdb-index"

  33. rm -f $index
  34. # Ensure intermediate index file is removed when we exit.
  35. trap "rm -f $index" 0

  36. $GDB --batch -nx -iex 'set auto-load no' \
  37.     -ex "file $file" -ex "save gdb-index $dir" || {
  38.     # Just in case.
  39.     status=$?
  40.     echo "$myname: gdb error generating index for $file" 1>&2
  41.     exit $status
  42. }

  43. # In some situations gdb can exit without creating an index.  This is
  44. # not an error.
  45. # E.g., if $file is stripped.  This behaviour is akin to stripping an
  46. # already stripped binary, it's a no-op.
  47. status=0

  48. if test -f "$index"; then
  49.     $OBJCOPY --add-section .gdb_index="$index" \
  50.         --set-section-flags .gdb_index=readonly "$file" "$file"
  51.     status=$?
  52. else
  53.     echo "$myname: No index was created for $file" 1>&2
  54.     echo "$myname: [Was there no debuginfo? Was there already an index?]" 1>&2
  55. fi

  56. exit $status