arm-none-eabi-gdb-add-index-py 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. #! /bin/sh
  2. # Add a .gdb_index section to a file.
  3. # Copyright (C) 2010-2019 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. dwarf5=""
  22. if [ "$1" = "-dwarf-5" ]; then
  23. dwarf5="$1"
  24. shift
  25. fi
  26. if test $# != 1; then
  27. echo "usage: $myname [-dwarf-5] FILE" 1>&2
  28. exit 1
  29. fi
  30. file="$1"
  31. if test ! -r "$file"; then
  32. echo "$myname: unable to access: $file" 1>&2
  33. exit 1
  34. fi
  35. dir="${file%/*}"
  36. test "$dir" = "$file" && dir="."
  37. index4="${file}.gdb-index"
  38. index5="${file}.debug_names"
  39. debugstr="${file}.debug_str"
  40. debugstrmerge="${file}.debug_str.merge"
  41. debugstrerr="${file}.debug_str.err"
  42. rm -f $index4 $index5 $debugstr $debugstrmerge $debugstrerr
  43. # Ensure intermediate index file is removed when we exit.
  44. trap "rm -f $index4 $index5 $debugstr $debugstrmerge $debugstrerr" 0
  45. $GDB --batch -nx -iex 'set auto-load no' \
  46. -ex "file $file" -ex "save gdb-index $dwarf5 $dir" || {
  47. # Just in case.
  48. status=$?
  49. echo "$myname: gdb error generating index for $file" 1>&2
  50. exit $status
  51. }
  52. # In some situations gdb can exit without creating an index. This is
  53. # not an error.
  54. # E.g., if $file is stripped. This behaviour is akin to stripping an
  55. # already stripped binary, it's a no-op.
  56. status=0
  57. if test -f "$index4" -a -f "$index5"; then
  58. echo "$myname: Both index types were created for $file" 1>&2
  59. status=1
  60. elif test -f "$index4" -o -f "$index5"; then
  61. if test -f "$index4"; then
  62. index="$index4"
  63. section=".gdb_index"
  64. else
  65. index="$index5"
  66. section=".debug_names"
  67. fi
  68. debugstradd=false
  69. debugstrupdate=false
  70. if test -s "$debugstr"; then
  71. if ! $OBJCOPY --dump-section .debug_str="$debugstrmerge" "$file" /dev/null \
  72. 2>$debugstrerr; then
  73. cat >&2 $debugstrerr
  74. exit 1
  75. fi
  76. if grep -q "can't dump section '.debug_str' - it does not exist" \
  77. $debugstrerr; then
  78. debugstradd=true
  79. else
  80. debugstrupdate=true
  81. cat >&2 $debugstrerr
  82. fi
  83. cat "$debugstr" >>"$debugstrmerge"
  84. fi
  85. $OBJCOPY --add-section $section="$index" \
  86. --set-section-flags $section=readonly \
  87. $(if $debugstradd; then \
  88. echo --add-section .debug_str="$debugstrmerge"; \
  89. echo --set-section-flags .debug_str=readonly; \
  90. fi; \
  91. if $debugstrupdate; then \
  92. echo --update-section .debug_str="$debugstrmerge"; \
  93. fi) \
  94. "$file" "$file"
  95. status=$?
  96. else
  97. echo "$myname: No index was created for $file" 1>&2
  98. echo "$myname: [Was there no debuginfo? Was there already an index?]" 1>&2
  99. fi
  100. exit $status