]> git.ipfire.org Git - people/mfischer/ipfire-2.x.git/blame - src/stripper
Merge branch 'master' of ssh://git.ipfire.org/pub/git/ipfire-2.x
[people/mfischer/ipfire-2.x.git] / src / stripper
CommitLineData
6c4cc7ea 1#!/usr/bin/env bash
bc259fdc 2
19054331
MT
3paths=()
4excludes=()
d79715db
MT
5strip="strip"
6break_on_error="1"
bc259fdc
MT
7
8while [ $# -gt 0 ]; do
9 case "${1}" in
d79715db
MT
10 --strip=*)
11 strip="${1#*=}"
12 ;;
bc259fdc 13 --exclude=*)
1432531a
MT
14 if [ -n "${1#*=}" ]; then
15 excludes+=( "!" "-path" "${1#*=}" "!" "-path" "${1#*=}/*" )
16 fi
bc259fdc 17 ;;
d79715db
MT
18 --ignore-errors)
19 break_on_error="0"
20 ;;
bc259fdc 21 *)
19054331 22 paths+=( "${1}" )
bc259fdc
MT
23 ;;
24 esac
25 shift
26done
fc44fa1f
MT
27
28function _strip() {
19054331
MT
29 local file="${1}"
30 local args=()
fc44fa1f 31
19054331
MT
32 # Fetch the filetype
33 local type="$(readelf -h "${file}" 2>/dev/null)"
bc259fdc 34
19054331
MT
35 case "${type}" in
36 # Libraries and Relocatable binaries
37 *Type:*"DYN (Shared object file)"*)
38 args+=( "--strip-all" )
39 ;;
37ef9fe4 40
19054331
MT
41 # Binaries
42 *Type:*"EXEC (Executable file)"*)
43 args+=( "--strip-all" )
44 ;;
25d50589 45
19054331
MT
46 # Static libraries
47 *Type:*"REL (Relocatable file)"*)
48 args+=( "--strip-debug" "--remove-section=.comment" "--remove-section=.note" )
fc44fa1f 49 ;;
19054331
MT
50
51 # Skip any unrecognised files
fc44fa1f 52 *)
19054331 53 return 0
fc44fa1f
MT
54 ;;
55 esac
56
19054331
MT
57 # Fetch any capabilities
58 local capabilities="$(getfattr --no-dereference --name="security.capability" \
59 --absolute-names --dump "${file}" 2>/dev/null)"
60
fc44fa1f 61 echo "Stripping ${file}..."
d79715db
MT
62 if ! "${strip}" "${args[@]}" "${file}"; then
63 return ${break_on_error}
19054331
MT
64 fi
65
37ef9fe4
MT
66 # Restore capabilities
67 if [ -n "${capabilities}" ]; then
68 setfattr --no-dereference --restore=<(echo "${capabilities}")
69 fi
fc44fa1f
MT
70}
71
19054331 72for path in ${paths[@]}; do
f84c2cda 73 for file in $(find -H "${path}" -xdev "${excludes[@]}" -type f \( -perm -0100 -or -perm -0010 -or -perm -0001 \) 2>/dev/null); do
19054331
MT
74 _strip "${file}" || exit $?
75 done
fc44fa1f 76done