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