OLD | NEW |
1 #!/bin/sh | 1 #!/bin/bash |
2 | |
3 # Copyright (c) 2012 The Chromium Authors. All rights reserved. | 2 # Copyright (c) 2012 The Chromium Authors. All rights reserved. |
4 # Use of this source code is governed by a BSD-style license that can be | 3 # Use of this source code is governed by a BSD-style license that can be |
5 # found in the LICENSE file. | 4 # found in the LICENSE file. |
6 # | 5 # |
7 # Copyright (C) 2010 Free Software Foundation, Inc. | 6 # Saves the gdb index for a given binary and its shared library dependencies. |
8 # This program is free software; you can redistribute it and/or modify | |
9 # it under the terms of the GNU General Public License as published by | |
10 # the Free Software Foundation; either version 3 of the License, or | |
11 # (at your option) any later version. | |
12 # | |
13 # This program is distributed in the hope that it will be useful, | |
14 # but WITHOUT ANY WARRANTY; without even the implied warranty of | |
15 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
16 # GNU General Public License for more details. | |
17 # | |
18 # You should have received a copy of the GNU General Public License | |
19 # along with this program. If not, see <http://www.gnu.org/licenses/>. | |
20 | 7 |
21 # This is a modification of gdb-add-index that recursively adds an index | 8 set -e |
22 # to the shared libraries dependencies from the same build. | |
23 | 9 |
24 target="$1" | 10 if [[ ! $# == 1 ]]; then |
25 target_dir="${target%/*}" | 11 echo "Usage: $0 path-to-binary" |
| 12 exit 1 |
| 13 fi |
26 | 14 |
27 OLDIFS=$IFS | 15 FILENAME="$1" |
28 IFS=$'\n' | 16 if [[ ! -f "$FILENAME" ]]; then |
| 17 echo "Path $FILENAME does not exist." |
| 18 exit 1 |
| 19 fi |
29 | 20 |
30 shared_libraries=$( | 21 # We're good to go! Create temp directory for index files. |
31 ldd -d "$target" | grep "$target_dir" | cut -d '>' -f2 | cut -d ' ' -f2 | 22 DIRECTORY=$(mktemp -d) |
32 ) | 23 echo "Made temp directory $DIRECTORY." |
33 | 24 |
34 # This function is basically the RedHat's gdb-add-index script and is why | 25 # Always remove directory on exit. |
35 # we have the FSF copyright above. | 26 trap "{ echo -n Removing temp directory $DIRECTORY...; |
36 function index_one_file { | 27 rm -rf $DIRECTORY; echo done; }" EXIT |
37 local file="$1" | |
38 local dir="${file%/*}" | |
39 | 28 |
40 # We don't care if gdb gives an error. | 29 # Grab all the chromium shared library files. |
41 gdb -nx --batch-silent -ex "file $file" -ex "save gdb-index $dir" | 30 so_files=$(ldd "$FILENAME" 2>/dev/null \ |
| 31 | grep $(pwd) \ |
| 32 | sed "s/.*[ \t]\(.*\) (.*/\1/") |
42 | 33 |
43 if test -f "${file}.gdb-index"; then | 34 # Add index to binary and the shared library dependencies. |
44 objcopy --add-section .gdb_index="${file}.gdb-index" --set-section-flags \ | 35 for file in "$FILENAME" $so_files; do |
45 .gdb_index=readonly "$file" "$file" | 36 basename=$(basename "$file") |
46 rm -f "${file}.gdb-index" | 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." |
| 41 else |
| 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." |
47 fi | 46 fi |
48 } | |
49 | |
50 function maybe_index { | |
51 readelf -e "$1" | grep '.gdb_index' > /dev/null 2>&1 | |
52 if [[ "$?" != 0 ]]; then | |
53 echo "Adding .gdb_index to $1" | |
54 index_one_file "$1" | |
55 else | |
56 echo "Skipping $1 (already has .gdb_index)" | |
57 fi | |
58 } | |
59 | |
60 maybe_index "$target" | |
61 for lib in $shared_libraries; do | |
62 maybe_index "$lib" | |
63 done | 47 done |
OLD | NEW |