You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

arm-none-eabi-gdb-add-index 4.0KB

преди 3 години
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. #! /bin/sh
  2. # Add a .gdb_index section to a file.
  3. # Copyright (C) 2010-2020 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. READELF=${READELF:=readelf}
  21. myname="${0##*/}"
  22. dwarf5=""
  23. if [ "$1" = "-dwarf-5" ]; then
  24. dwarf5="$1"
  25. shift
  26. fi
  27. if test $# != 1; then
  28. echo "usage: $myname [-dwarf-5] FILE" 1>&2
  29. exit 1
  30. fi
  31. file="$1"
  32. if test ! -r "$file"; then
  33. echo "$myname: unable to access: $file" 1>&2
  34. exit 1
  35. fi
  36. dir="${file%/*}"
  37. test "$dir" = "$file" && dir="."
  38. dwz_file=""
  39. if $READELF -S "$file" | grep -q " \.gnu_debugaltlink "; then
  40. dwz_file=$($READELF --string-dump=.gnu_debugaltlink "$file" \
  41. | grep -A1 "'\.gnu_debugaltlink':" \
  42. | tail -n +2 \
  43. | sed 's/.*]//')
  44. dwz_file=$(echo $dwz_file)
  45. if $READELF -S "$dwz_file" | grep -E -q " \.(gdb_index|debug_names) "; then
  46. # Already has an index, skip it.
  47. dwz_file=""
  48. fi
  49. fi
  50. set_files ()
  51. {
  52. local file="$1"
  53. index4="${file}.gdb-index"
  54. index5="${file}.debug_names"
  55. debugstr="${file}.debug_str"
  56. debugstrmerge="${file}.debug_str.merge"
  57. debugstrerr="${file}.debug_str.err"
  58. }
  59. tmp_files=
  60. for f in "$file" "$dwz_file"; do
  61. if [ "$f" = "" ]; then
  62. continue
  63. fi
  64. set_files "$f"
  65. tmp_files="$tmp_files $index4 $index5 $debugstr $debugstrmerge $debugstrerr"
  66. done
  67. rm -f $tmp_files
  68. # Ensure intermediate index file is removed when we exit.
  69. trap "rm -f $tmp_files" 0
  70. $GDB --batch -nx -iex 'set auto-load no' \
  71. -ex "file $file" -ex "save gdb-index $dwarf5 $dir" || {
  72. # Just in case.
  73. status=$?
  74. echo "$myname: gdb error generating index for $file" 1>&2
  75. exit $status
  76. }
  77. # In some situations gdb can exit without creating an index. This is
  78. # not an error.
  79. # E.g., if $file is stripped. This behaviour is akin to stripping an
  80. # already stripped binary, it's a no-op.
  81. status=0
  82. handle_file ()
  83. {
  84. local file
  85. file="$1"
  86. set_files "$file"
  87. if test -f "$index4" -a -f "$index5"; then
  88. echo "$myname: Both index types were created for $file" 1>&2
  89. status=1
  90. elif test -f "$index4" -o -f "$index5"; then
  91. if test -f "$index4"; then
  92. index="$index4"
  93. section=".gdb_index"
  94. else
  95. index="$index5"
  96. section=".debug_names"
  97. fi
  98. debugstradd=false
  99. debugstrupdate=false
  100. if test -s "$debugstr"; then
  101. if ! $OBJCOPY --dump-section .debug_str="$debugstrmerge" "$file" \
  102. /dev/null 2>$debugstrerr; then
  103. cat >&2 $debugstrerr
  104. exit 1
  105. fi
  106. if grep -q "can't dump section '.debug_str' - it does not exist" \
  107. $debugstrerr; then
  108. debugstradd=true
  109. else
  110. debugstrupdate=true
  111. cat >&2 $debugstrerr
  112. fi
  113. cat "$debugstr" >>"$debugstrmerge"
  114. fi
  115. $OBJCOPY --add-section $section="$index" \
  116. --set-section-flags $section=readonly \
  117. $(if $debugstradd; then \
  118. echo --add-section .debug_str="$debugstrmerge"; \
  119. echo --set-section-flags .debug_str=readonly; \
  120. fi; \
  121. if $debugstrupdate; then \
  122. echo --update-section .debug_str="$debugstrmerge"; \
  123. fi) \
  124. "$file" "$file"
  125. status=$?
  126. else
  127. echo "$myname: No index was created for $file" 1>&2
  128. echo "$myname: [Was there no debuginfo? Was there already an index?]" \
  129. 1>&2
  130. fi
  131. }
  132. handle_file "$file"
  133. if [ "$dwz_file" != "" ]; then
  134. handle_file "$dwz_file"
  135. fi
  136. exit $status