[email protected] | d43a9fb | 2012-08-21 00:09:19 | [diff] [blame] | 1 | #!/bin/bash |
[email protected] | 4cec2534 | 2012-06-28 23:37:31 | [diff] [blame] | 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 | # | ||||
[email protected] | d43a9fb | 2012-08-21 00:09:19 | [diff] [blame] | 6 | # Saves the gdb index for a given binary and its shared library dependencies. |
[email protected] | fdb9ecc | 2012-06-28 01:53:45 | [diff] [blame] | 7 | |
[email protected] | d43a9fb | 2012-08-21 00:09:19 | [diff] [blame] | 8 | set -e |
[email protected] | fdb9ecc | 2012-06-28 01:53:45 | [diff] [blame] | 9 | |
[email protected] | d43a9fb | 2012-08-21 00:09:19 | [diff] [blame] | 10 | if [[ ! $# == 1 ]]; then |
11 | echo "Usage: $0 path-to-binary" | ||||
12 | exit 1 | ||||
13 | fi | ||||
[email protected] | fdb9ecc | 2012-06-28 01:53:45 | [diff] [blame] | 14 | |
[email protected] | d43a9fb | 2012-08-21 00:09:19 | [diff] [blame] | 15 | FILENAME="$1" |
16 | if [[ ! -f "$FILENAME" ]]; then | ||||
17 | echo "Path $FILENAME does not exist." | ||||
18 | exit 1 | ||||
19 | fi | ||||
[email protected] | fdb9ecc | 2012-06-28 01:53:45 | [diff] [blame] | 20 | |
[email protected] | d43a9fb | 2012-08-21 00:09:19 | [diff] [blame] | 21 | # We're good to go! Create temp directory for index files. |
22 | DIRECTORY=$(mktemp -d) | ||||
23 | echo "Made temp directory $DIRECTORY." | ||||
[email protected] | 4cec2534 | 2012-06-28 23:37:31 | [diff] [blame] | 24 | |
[email protected] | d43a9fb | 2012-08-21 00:09:19 | [diff] [blame] | 25 | # Always remove directory on exit. |
26 | trap "{ echo -n Removing temp directory $DIRECTORY...; | ||||
27 | rm -rf $DIRECTORY; echo done; }" EXIT | ||||
[email protected] | 4cec2534 | 2012-06-28 23:37:31 | [diff] [blame] | 28 | |
[email protected] | d43a9fb | 2012-08-21 00:09:19 | [diff] [blame] | 29 | # Grab all the chromium shared library files. |
30 | so_files=$(ldd "$FILENAME" 2>/dev/null \ | ||||
[email protected] | e4e06e3 | 2012-09-15 02:28:20 | [diff] [blame] | 31 | | grep $(dirname "$FILENAME") \ |
[email protected] | d43a9fb | 2012-08-21 00:09:19 | [diff] [blame] | 32 | | sed "s/.*[ \t]\(.*\) (.*/\1/") |
[email protected] | 4cec2534 | 2012-06-28 23:37:31 | [diff] [blame] | 33 | |
[email protected] | d43a9fb | 2012-08-21 00:09:19 | [diff] [blame] | 34 | # Add index to binary and the shared library dependencies. |
35 | for file in "$FILENAME" $so_files; do | ||||
36 | basename=$(basename "$file") | ||||
37 | echo -n "Adding index to $basename..." | ||||
38 | readelf_out=$(readelf -S "$file") | ||||
39 | if [[ $readelf_out =~ "gdb_index" ]]; then | ||||
40 | echo "already contains index. Skipped." | ||||
[email protected] | 4cec2534 | 2012-06-28 23:37:31 | [diff] [blame] | 41 | else |
[email protected] | d43a9fb | 2012-08-21 00:09:19 | [diff] [blame] | 42 | gdb -batch "$file" -ex "save gdb-index $DIRECTORY" -ex "quit" |
43 | objcopy --add-section .gdb_index="$DIRECTORY"/$basename.gdb-index \ | ||||
44 | --set-section-flags .gdb_index=readonly "$file" "$file" | ||||
45 | echo "done." | ||||
[email protected] | 4cec2534 | 2012-06-28 23:37:31 | [diff] [blame] | 46 | fi |
[email protected] | 4cec2534 | 2012-06-28 23:37:31 | [diff] [blame] | 47 | done |