#!/bin/sh # elf-move-debug --- move debugging symbols from elf object to separate file # Author: Noah Friedman # Created: 2006-09-06 # Public domain. # $Id: elf-move-debug,v 1.1 2006/09/07 06:59:33 friedman Exp $ # Commentary: # Code: # Name by which this script was invoked. progname=`echo "$0" | sed -e 's/.*\///'` bq='`' eq="'" usage="Usage: $progname {options} elfobj {outfile} Options are: -h, --help You're looking at it. -v, --verbose Show objcopy commands. -s, --suffix SUFFIX If outfile is not specified, write symbols to elfobj.SUFFIX. " verbose=f suffix=symbols # Usage: eval "$getopt"; value=$optarg # or optarg_optional=t; eval "$getopt"; value=$optarg # # This function automatically shifts the positional args as appropriate. # The argument to an option is optional if the variable `optarg_optional' # is non-empty. Otherwise, the argument is required and getopt will cause # the program to exit on an error. optarg_optional is reset to be empty # after every call to getopt. The argument (if any) is stored in the # variable `optarg'. # # Long option syntax is `--foo=bar' or `--foo bar'. # For optional args, you must use the `--foo=bar' long option syntax # if the argument starts with `-', otherwise the argument will be ignored # and treated as the next option. # # Note: because of broken bourne shells, using --foo=bar syntax can # actually screw the quoting of args that end with trailing newlines. # Specifically, most shells strip trailing newlines from substituted # output, regardless of quoting. getopt=' { optarg= case $1 in --*=* ) optarg=`echo "$1" | sed -e "1s/^[^=]*=//"` ; shift ;; -* ) case ${2+set}:$optarg_optional in set: ) optarg=$2 ; shift ; shift ;; set:?* ) case $2 in -* ) shift ;; * ) optarg=$2; shift; shift ;; esac ;; : ) option=$1 case $option in --*=* ) option=`echo "$option" | sed -e "1s/=.*//;q"` ;; esac exec 1>&2 echo "$progname: option $bq$option$eq requires argument." echo "$progname: use $bq--help$eq to list option syntax." exit 1 ;; * ) shift ;; esac ;; esac optarg_optional= }' # Parse command line arguments. # Make sure that all wildcarded options are long enough to be unambiguous. # It's a good idea to document the full long option name in each case. # Long options which take arguments will need a `*' appended to the # canonical name to match the value appended after the `=' character. while : ; do case $# in 0) break ;; esac case $1 in -v | --verbose | --v* ) verbose=t; shift ;; -h | --help | --h* ) echo "$usage" 1>&2; exit 0 ;; -s | --suffix | --s* ) eval "$getopt" suffix=$optarg ;; -- ) shift; break ;; # Stop option processing -? | --* ) case $1 in --*=* ) arg=`echo "$1" | sed -e 's/=.*//'` ;; * ) arg="$1" ;; esac exec 1>&2 echo "$progname: unknown or ambiguous option $bq$arg$eq" echo "$progname: Use $bq--help$eq for a list of options." exit 1 ;; -??* ) # Split grouped single options into separate args and try again optarg=$1 shift set fnord `echo "x$optarg" | sed -e 's/^x-//;s/\(.\)/-\1 /g'` ${1+"$@"} shift ;; * ) break ;; esac done case $# in 1 ) objfile=$1 symfile=$1.$suffix ;; 2 ) objfile=$1 symfile=$2 ;; * ) echo "$usage" 1>&2 ; exit 1 ;; esac objcopy=${OBJCOPY-objcopy} if { $objcopy --version 2>&1 | grep 'GNU objcopy'; } > /dev/null 2>&1 ; then test $verbose = t && set -x $objcopy --only-keep-debug "$objfile" "$symfile" \ && $objcopy --strip-debug "$objfile" \ && $objcopy --add-gnu-debuglink="$symfile" "$objfile" else echo "$progname: can't find \`$objcopy', or else it's not GNU objcopy." 1>&2 exit 1 fi # elf-move-debug ends here