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