gdb-add-index 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  1. #!/bin/bash
  2. # Copyright (c) 2012 The Chromium Authors. All rights reserved.
  3. # Use of this source code is governed by a BSD-style license that can be
  4. # found in the LICENSE file.
  5. #
  6. # Saves the gdb index for a given binary and its shared library dependencies.
  7. set -e
  8. if [[ ! $# == 1 ]]; then
  9. echo "Usage: $0 path-to-binary"
  10. exit 1
  11. fi
  12. FILENAME="$1"
  13. if [[ ! -f "$FILENAME" ]]; then
  14. echo "Path $FILENAME does not exist."
  15. exit 1
  16. fi
  17. # We're good to go! Create temp directory for index files.
  18. DIRECTORY=$(mktemp -d)
  19. echo "Made temp directory $DIRECTORY."
  20. # Always remove directory on exit.
  21. trap "{ echo -n Removing temp directory $DIRECTORY...;
  22. rm -rf $DIRECTORY; echo done; }" EXIT
  23. # Grab all the chromium shared library files.
  24. so_files=$(ldd "$FILENAME" 2>/dev/null \
  25. | grep $(dirname "$FILENAME") \
  26. | sed "s/.*[ \t]\(.*\) (.*/\1/")
  27. # Add index to binary and the shared library dependencies.
  28. for file in "$FILENAME" $so_files; do
  29. basename=$(basename "$file")
  30. echo -n "Adding index to $basename..."
  31. readelf_out=$(readelf -S "$file")
  32. if [[ $readelf_out =~ "gdb_index" ]]; then
  33. echo "already contains index. Skipped."
  34. else
  35. gdb -batch "$file" -ex "save gdb-index $DIRECTORY" -ex "quit"
  36. objcopy --add-section .gdb_index="$DIRECTORY"/$basename.gdb-index \
  37. --set-section-flags .gdb_index=readonly "$file" "$file"
  38. echo "done."
  39. fi
  40. done