aboutsummaryrefslogtreecommitdiff
path: root/common/hooks/post-install/06-strip-and-debug-pkgs.sh
blob: 191c025e552cebbca933ea7f33de1398dde432c6 (plain) (blame)
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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
# This hook executes the following tasks:
#	- strips ELF binaries/libraries
#	- generates -dbg pkgs

make_debug() {
	local dname= fname= dbgfile=

	[ -n "$nodebug" ] && return 0

	dname=${1%/*}/ ; dname=${dname#$PKGDESTDIR}
	fname="${1##*/}"
	dbgfile="${dname}/${fname}"

	mkdir -p "${PKGDESTDIR}/usr/lib/debug/${dname}"
	$OBJCOPY --only-keep-debug --compress-debug-sections \
		"$1" "${PKGDESTDIR}/usr/lib/debug/${dbgfile}"
	if [ $? -ne 0 ]; then
		msg_red "${pkgver}: failed to create dbg file: ${dbgfile}\n"
		return 1
	fi
	chmod 644 "${PKGDESTDIR}/usr/lib/debug/${dbgfile}"
}

attach_debug() {
	local dname= fname= dbgfile=

	[ -n "$nodebug" ] && return 0

	dname=${1%/*}/ ; dname=${dname#$PKGDESTDIR}
	fname="${1##*/}"
	dbgfile="${dname}/${fname}"

	$OBJCOPY --add-gnu-debuglink="${PKGDESTDIR}/usr/lib/debug/${dbgfile}" "$1"
	if [ $? -ne 0 ]; then
		msg_red "${pkgver}: failed to attach dbg to ${dbgfile}\n"
		return 1
	fi
}

create_debug_pkg() {
	local _pkgname= _destdir=

	[ -n "$nodebug" ] && return 0
	[ ! -d "${PKGDESTDIR}/usr/lib/debug" ] && return 0

	_pkgname="${pkgname}-dbg-${version}"
	_destdir="${XBPS_DESTDIR}/${XBPS_CROSS_TRIPLET}/${_pkgname}"
	mkdir -p "${_destdir}/usr/lib"
	mv ${PKGDESTDIR}/usr/lib/debug ${_destdir}/usr/lib
	if [ $? -ne 0 ]; then
		msg_red "$pkgver: failed to create debug pkg\n"
		return 1
	fi
	printf "${pkgver} " >> ${_destdir}/rdeps
	rmdir --ignore-fail-on-non-empty "${PKGDESTDIR}/usr/lib" 2>/dev/null
	return 0
}

hook() {
	local fname= x= f= _soname= STRIPCMD=

	if [ -n "$nostrip" ]; then
		return 0
	fi

	STRIPCMD=/usr/bin/$STRIP

	find ${PKGDESTDIR} -type f | while read f; do
		if [[ $f =~ ^${PKGDESTDIR}/usr/lib/debug/ ]]; then
			continue
		fi

		fname=${f##*/}
		for x in ${nostrip_files}; do
			if [ "$x" = "$fname" -o "$x" = "${f#$PKGDESTDIR}" ]; then
				found=1
				break
			fi
		done
		if [ -n "$found" ]; then
			unset found
			continue
		fi
		case "$(file -bi "$f")" in
		application/x-executable*)
			chmod +w "$f"
			if [[ $(file $f) =~ "statically linked" ]]; then
				# static binary
				if ! $STRIPCMD "$f"; then
					msg_red "$pkgver: failed to strip ${f#$PKGDESTDIR}\n"
					return 1
				fi
				echo "   Stripped static executable: ${f#$PKGDESTDIR}"
			else
				make_debug "$f"
				if ! $STRIPCMD "$f"; then
					msg_red "$pkgver: failed to strip ${f#$PKGDESTDIR}\n"
					return 1
				fi
				echo "   Stripped executable: ${f#$PKGDESTDIR}"
				unset nopie_found
				for x in ${nopie_files}; do
					if [ "$x" = "${f#$PKGDESTDIR}" ]; then
						nopie_found=1
						break
					fi
				done
				if [ -z "$nopie" ] && [ -z "$nopie_found" ]; then
					msg_red "$pkgver: non-PIE executable found in PIE build: ${f#$PKGDESTDIR}\n"
					return 1
				fi
				attach_debug "$f"
			fi
			;;
		application/x-sharedlib*|application/x-pie-executable*)
			local type="$(file -b "$f")"
			if [[ $type =~ "no machine" ]]; then
				# using ELF as a container format (e.g. guile)
				echo "   Ignoring ELF file without machine set: ${f#$PKGDESTDIR}"
				continue
			fi

			chmod +w "$f"
			# shared library
			make_debug "$f"
			if ! $STRIPCMD --strip-unneeded "$f"; then
				msg_red "$pkgver: failed to strip ${f#$PKGDESTDIR}\n"
				return 1
			fi
			if [[ $type =~ "interpreter " ]]; then
				echo "   Stripped position-independent executable: ${f#$PKGDESTDIR}"
			else
				echo "   Stripped library: ${f#$PKGDESTDIR}"
			fi
			attach_debug "$f"
			;;
		application/x-archive*)
			chmod +w "$f"
			if ! $STRIPCMD --strip-debug "$f"; then
				msg_red "$pkgver: failed to strip ${f#$PKGDESTDIR}\n"
				return 1
			fi
			echo "   Stripped static library: ${f#$PKGDESTDIR}";;
		esac
	done
	create_debug_pkg
	return $?
}