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