2017-08-11 07:21:24 -07:00
|
|
|
#!/bin/sh
|
2017-08-11 07:24:24 -07:00
|
|
|
#
|
2019-10-15 03:27:25 -07:00
|
|
|
# $OpenBSD: ctfstrip,v 1.12 2019/10/15 10:27:25 mpi Exp $
|
2017-08-11 07:24:24 -07:00
|
|
|
#
|
2017-08-12 09:33:11 -07:00
|
|
|
# Copyright (c) 2017 Martin Pieuchot
|
2017-08-11 07:24:24 -07:00
|
|
|
#
|
|
|
|
# Permission to use, copy, modify, and distribute this software for any
|
|
|
|
# purpose with or without fee is hereby granted, provided that the above
|
|
|
|
# copyright notice and this permission notice appear in all copies.
|
|
|
|
#
|
|
|
|
# THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
|
|
|
|
# WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
|
|
|
|
# MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
|
|
|
|
# ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
|
|
|
|
# WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
|
|
|
|
# ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
|
|
|
|
# OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
|
2017-08-11 07:21:24 -07:00
|
|
|
|
2017-08-11 08:08:16 -07:00
|
|
|
set -o posix
|
|
|
|
|
|
|
|
cleanup() {
|
|
|
|
rm -f ${TMPFILE}
|
|
|
|
exit 1
|
|
|
|
}
|
|
|
|
|
2019-02-18 03:05:43 -08:00
|
|
|
usage() {
|
|
|
|
echo "usage: $(basename $0) [-S] [-o outfile] file" >&2
|
|
|
|
exit 1
|
|
|
|
}
|
2017-08-11 07:21:24 -07:00
|
|
|
|
2019-02-18 03:05:43 -08:00
|
|
|
trap "cleanup" 1 2 3 13 15
|
2017-08-11 07:21:24 -07:00
|
|
|
|
2019-05-27 02:31:08 -07:00
|
|
|
while getopts So: opt; do
|
|
|
|
case $opt in
|
|
|
|
S) STRIPFLAG=-g;;
|
|
|
|
o) OUTFILE="$OPTARG";;
|
|
|
|
\?) usage;;
|
|
|
|
esac
|
2017-08-11 07:21:24 -07:00
|
|
|
done
|
|
|
|
|
2019-05-27 02:31:08 -07:00
|
|
|
shift $((OPTIND - 1))
|
|
|
|
|
2019-02-18 03:05:43 -08:00
|
|
|
if [ $# -ne 1 ]; then
|
|
|
|
usage
|
2017-08-11 07:21:24 -07:00
|
|
|
fi
|
|
|
|
|
2019-02-18 03:05:43 -08:00
|
|
|
INFILE="$1"
|
2017-08-11 07:21:24 -07:00
|
|
|
LABEL="unknown"
|
2017-08-11 08:08:16 -07:00
|
|
|
TMPFILE=$(mktemp /tmp/.ctf.XXXXXXXXXX)
|
2017-08-11 07:21:24 -07:00
|
|
|
|
2017-08-11 08:08:16 -07:00
|
|
|
# Extract kernel version
|
2017-08-12 12:51:17 -07:00
|
|
|
if [ -z "${INFILE##bsd*}" ]; then
|
2017-08-15 08:48:10 -07:00
|
|
|
LABEL=`what "$INFILE" | sed -n '$s/^ //p'`
|
2017-08-11 07:21:24 -07:00
|
|
|
fi
|
|
|
|
|
2019-10-15 03:27:25 -07:00
|
|
|
# If ctfstrip was passed a file that lacks useful debug sections, ctfconv will
|
|
|
|
# fail. So try to run ctfconv and silently fallback to plain strip(1) if that
|
|
|
|
# failed.
|
2017-08-15 08:48:10 -07:00
|
|
|
ctfconv -o ${TMPFILE} -l "${LABEL}" "${INFILE}" 2> /dev/null
|
2017-08-11 07:21:24 -07:00
|
|
|
|
2017-08-12 12:51:17 -07:00
|
|
|
if [ $? -eq 0 ]; then
|
2019-10-15 03:27:25 -07:00
|
|
|
objcopy ${STRIPFLAG} \
|
|
|
|
--add-section .SUNW_ctf=${TMPFILE} "${INFILE}" ${OUTFILE}
|
|
|
|
|
|
|
|
# Also add CTF data to the debug kernel
|
|
|
|
if [ -z "${INFILE##bsd.gdb}" ]; then
|
|
|
|
objcopy --add-section .SUNW_ctf=${TMPFILE} "${INFILE}"
|
|
|
|
fi
|
2017-08-12 09:33:11 -07:00
|
|
|
else
|
2019-02-18 03:01:48 -08:00
|
|
|
strip ${STRIPFLAG} ${OUTFILE:+"-o${OUTFILE}"} "${INFILE}"
|
2017-08-12 09:33:11 -07:00
|
|
|
fi
|
2017-08-11 07:21:24 -07:00
|
|
|
|
|
|
|
rm -f ${TMPFILE}
|