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