summaryrefslogtreecommitdiff
blob: b9e51ab538224c513ec0d87a709cdaec7dbabbf1 (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
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
#!/bin/bash
# Copyright 1999-2006 Gentoo Foundation
# Distributed under the terms of the GNU General Public License v2
# $Id$

source "${PORTAGE_BIN_PATH:-/usr/lib/portage/bin}"/isolated-functions.sh

unset NOSTRIP
if hasq nostrip ${FEATURES} ${RESTRICT} || \
   hasq strip ${RESTRICT}
then
	NOSTRIP=1
	hasq installsources ${FEATURES} || exit 0
fi

STRIP=${STRIP:-${CHOST}-strip}
type -P -- ${STRIP} > /dev/null || STRIP=strip
OBJCOPY=${OBJCOPY:-${CHOST}-objcopy}
type -P -- ${OBJCOPY} > /dev/null || OBJCOPY=objcopy

export PORTAGE_STRIP_FLAGS=${PORTAGE_STRIP_FLAGS:---strip-unneeded}

banner=1

save_elf_debug() {
	local x=$1
	local y="${D}usr/lib/debug/${x:${#D}}.debug"

	if hasq installsources ${FEATURES} && [ -x /usr/bin/debugedit ] ; then
		local sources_dir=/usr/src/debug/${CATEGORY}/${PF}
		debugedit -b "${WORKDIR}" -d "${sources_dir}" \
			-l "${T}"/debug.sources "${x}"
		if [[ -s ${T}/debug.sources ]] ; then 
			[[ -d ${D}${sources_dir} ]] || mkdir -p "${D}${sources_dir}"
			grep -zv '/<built-in>$' "${T}"/debug.sources | \
				(cd "${WORKDIR}"; LANG=C sort -z -u | \
				rsync -rtL0 --files-from=- "${WORKDIR}/" "${D}${sources_dir}/" )
		fi
	fi

	hasq splitdebug ${FEATURES} || return 0
	[[ -z "${NOSTRIP}" ]] || return 0

	# dont save debug info twice.
	[[ ${x:7} == ".debug" ]] && return 0

	mkdir -p $(dirname "${y}")
	${OBJCOPY} --only-keep-debug "${x}" "${y}"
	${OBJCOPY} --add-gnu-debuglink="${y}" "${x}"
	[ -g "${x}" ] && chmod go-r "${y}"
	[ -u "${x}" ] && chmod go-r "${y}"
	chmod a-x,o-w "${y}"

}

# The existance of the section .symtab tells us that a binary is stripped.
# We want to log already stripped binaries, as this may be a QA violation.
# They prevent us from getting the splitdebug data.
f="$(scanelf -yqRBF '#k%F' -k '!.symtab' "$@")"
if [[ -n "${f}" ]] ; then
	vecho -e "\a\n"
	eqawarn "QA Notice: Pre-stripped files found:"
	eqawarn "${f}"
	echo "${f}" > "${T}"/scanelf-already-stripped.log
fi

# Now we look for unstripped binaries.
for x in $(scanelf -yqRBF '#k%F' -k '.symtab' "$@") $(for y in "$@"; do find "${y}" -type f -name '*.a' -print ; done); do
	if [[ ${banner} -eq 1 ]] ; then
		[[ -z ${NOSTRIP} ]] && vecho "strip: ${STRIP} ${PORTAGE_STRIP_FLAGS}"
		banner=0
	fi

	f=$(file "${x}") || continue
	[[ -z ${f} ]] && continue

	# The noglob funk is to support STRIP_MASK="/*/booga" and to keep
	#  the for loop from expanding the globs.
	# The eval echo is to support STRIP_MASK="/*/{booga,bar}" sex.
	set -o noglob
	stripitbaby=1
	for m in $(eval echo ${STRIP_MASK}) ; do
		[[ ${x} == ${m} ]] && stripitbaby=0 && break
	done
	set +o noglob

	if [[ ${f} == *"current ar archive"* ]] ; then
		vecho "   ${x:${#D}}"
		[[ -n ${NOSTRIP} ]] && continue
		[[ ${stripitbaby} -eq 1 ]] && ${STRIP} -g "${x}"
	fi
	if [[ ${f} == *"SB executable"* || ${f} == *"SB shared object"* ]] ; then
		vecho "   ${x:${#D}}"
		save_elf_debug "${x}"
		[[ -n ${NOSTRIP} ]] && continue
		[[ ${stripitbaby} -eq 1 ]] && ${STRIP} ${PORTAGE_STRIP_FLAGS} "${x}"
	fi
done