summaryrefslogtreecommitdiff
path: root/usr.bin/ctfconv/ctfstrip
blob: 6fa3cdd15faf0c43689773cdf8eb6b6f8e2d4e50 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
#!/bin/sh
#
# $OpenBSD: ctfstrip,v 1.8 2017/08/15 15:48:10 jasper Exp $
#
# Copyright (c) 2017 Martin Pieuchot
#
# 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.

set -o posix

cleanup() {
	rm -f ${TMPFILE}
	exit 1
}

trap "cleanup" 1 2 3 13 15

USAGE="usage: ${0##*/} [-S] [-o outfile] file"

for arg in "$@"; do
	if [ -n "$OSET" ]; then
		OUTFILE="$arg"
		unset OSET
		shift
		continue
	fi
	case "$arg" in
	-o)	OSET=1; shift; continue;;
	-S)	STRIPFLAG=-g; shift; continue;;
	esac
	shift
	set -- "$@" "$arg"
	INFILE="$arg"
done

if [ $# -eq 0 ]; then
	echo "${USAGE}" >&2
	exit 1
fi

LABEL="unknown"
TMPFILE=$(mktemp /tmp/.ctf.XXXXXXXXXX)

# Extract kernel version
if [ -z "${INFILE##bsd*}" ]; then
	LABEL=`what "$INFILE" | sed -n '$s/^   //p'`
fi

# 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.
ctfconv -o ${TMPFILE} -l "${LABEL}" "${INFILE}" 2> /dev/null

if [ $? -eq 0 ]; then
        objcopy --add-section .SUNW_ctf=${TMPFILE} ${STRIPFLAG} "${INFILE}" "${OUTFILE}"
else
        strip ${STRIPFLAG} -o "${OUTFILE}" "$@"
fi

rm -f ${TMPFILE}