michael@0: #!/bin/bash michael@0: # Copyright (c) 2012 The Chromium Authors. All rights reserved. michael@0: # Use of this source code is governed by a BSD-style license that can be michael@0: # found in the LICENSE file. michael@0: # michael@0: # Saves the gdb index for a given binary and its shared library dependencies. michael@0: michael@0: set -e michael@0: michael@0: if [[ ! $# == 1 ]]; then michael@0: echo "Usage: $0 path-to-binary" michael@0: exit 1 michael@0: fi michael@0: michael@0: FILENAME="$1" michael@0: if [[ ! -f "$FILENAME" ]]; then michael@0: echo "Path $FILENAME does not exist." michael@0: exit 1 michael@0: fi michael@0: michael@0: # We're good to go! Create temp directory for index files. michael@0: DIRECTORY=$(mktemp -d) michael@0: echo "Made temp directory $DIRECTORY." michael@0: michael@0: # Always remove directory on exit. michael@0: trap "{ echo -n Removing temp directory $DIRECTORY...; michael@0: rm -rf $DIRECTORY; echo done; }" EXIT michael@0: michael@0: # Grab all the chromium shared library files. michael@0: so_files=$(ldd "$FILENAME" 2>/dev/null \ michael@0: | grep $(dirname "$FILENAME") \ michael@0: | sed "s/.*[ \t]\(.*\) (.*/\1/") michael@0: michael@0: # Add index to binary and the shared library dependencies. michael@0: for file in "$FILENAME" $so_files; do michael@0: basename=$(basename "$file") michael@0: echo -n "Adding index to $basename..." michael@0: readelf_out=$(readelf -S "$file") michael@0: if [[ $readelf_out =~ "gdb_index" ]]; then michael@0: echo "already contains index. Skipped." michael@0: else michael@0: gdb -batch "$file" -ex "save gdb-index $DIRECTORY" -ex "quit" michael@0: objcopy --add-section .gdb_index="$DIRECTORY"/$basename.gdb-index \ michael@0: --set-section-flags .gdb_index=readonly "$file" "$file" michael@0: echo "done." michael@0: fi michael@0: done