b108fd8f7768a34205ae1529548bec08d9c978c1
[oweals/openwrt.git] / scripts / bundle-libraries.sh
1 #!/usr/bin/env bash
2 #
3 #   Script to install host system binaries along with required libraries.
4 #
5 #   Copyright (C) 2012-2017 Jo-Philipp Wich <jo@mein.io>
6 #
7 #   This program is free software; you can redistribute it and/or modify
8 #   it under the terms of the GNU General Public License as published by
9 #   the Free Software Foundation; either version 2 of the License, or
10 #   (at your option) any later version.
11 #
12 #   This program is distributed in the hope that it will be useful,
13 #   but WITHOUT ANY WARRANTY; without even the implied warranty of
14 #   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15 #   GNU General Public License for more details.
16 #
17 #   You should have received a copy of the GNU General Public License
18 #   along with this program; if not, write to the Free Software
19 #   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
20
21 DIR="$1"; shift
22
23 _cp() {
24         cp ${VERBOSE:+-v} -L "$1" "$2" || {
25                 echo "cp($1 $2) failed" >&2
26                 exit 1
27         }
28 }
29
30 _mv() {
31         mv ${VERBOSE:+-v} "$1" "$2" || {
32                 echo "mv($1 $2) failed" >&2
33                 exit 1
34         }
35 }
36
37 _md() {
38         mkdir ${VERBOSE:+-v} -p "$1" || {
39                 echo "mkdir($1) failed" >&2
40                 exit 2
41         }
42 }
43
44 _ln() {
45         ln ${VERBOSE:+-v} -sf "$1" "$2" || {
46                 echo "ln($1 $2) failed" >&2
47                 exit 3
48         }
49 }
50
51 _relpath() {
52         local base="$(readlink -f "$1")"
53         local dest="$(readlink -f "$2")"
54         local up
55
56         [ -d "$base" ] || base="${base%/*}"
57         [ -d "$dest" ] || dest="${dest%/*}"
58
59         while true; do
60                 case "$base"
61                         in "$dest"/*)
62                                 echo "$up/${base#$dest/}"
63                                 break
64                         ;;
65                         *)
66                                 dest="${dest%/*}"
67                                 up="${up:+$up/}.."
68                         ;;
69                 esac
70         done
71 }
72
73 _runas_so() {
74         cat <<-EOT | ${CC:-gcc} -x c -fPIC -shared -o "$1" -
75                 #include <unistd.h>
76                 #include <stdio.h>
77                 #include <stdlib.h>
78
79                 int mangle_arg0(int argc, char **argv, char **env) {
80                         char *arg0 = getenv("RUNAS_ARG0");
81
82                         if (arg0)
83                                 argv[0] = arg0;
84
85                         return 0;
86                 }
87
88                 __attribute__((section(".init_array")))
89                 static void *mangle_arg0_constructor = &mangle_arg0;
90         EOT
91
92         [ -x "$1" ] || {
93                 echo "compiling preload library failed" >&2
94                 exit 5
95         }
96 }
97
98 for LDD in ${PATH//://ldd }/ldd; do
99         "$LDD" --version >/dev/null 2>/dev/null && break
100         LDD=""
101 done
102
103 [ -n "$LDD" -a -x "$LDD" ] || LDD=
104
105 for BIN in "$@"; do
106         [ -n "$BIN" -a -n "$DIR" ] || {
107                 echo "Usage: $0 <destdir> <executable> ..." >&2
108                 exit 1
109         }
110
111         [ ! -d "$DIR/lib" ] && {
112                 _md "$DIR/lib"
113                 _md "$DIR/usr"
114                 _ln "../lib" "$DIR/usr/lib"
115         }
116
117         [ ! -x "$DIR/lib/runas.so" ] && {
118                 _runas_so "$DIR/lib/runas.so"
119         }
120
121         LDSO=""
122
123         [ -n "$LDD" ] && [ -x "$BIN" ] && file "$BIN" | grep -sqE "ELF.*(executable|interpreter)" && {
124                 for token in $("$LDD" "$BIN" 2>/dev/null); do
125                         case "$token" in */*.so*)
126                                 case "$token" in
127                                         *ld-*.so*) LDSO="${token##*/}" ;;
128                                 esac
129
130                                 dest="$DIR/lib/${token##*/}"
131                                 ddir="${dest%/*}"
132
133                                 [ -f "$token" -a ! -f "$dest" ] && {
134                                         _md "$ddir"
135                                         _cp "$token" "$dest"
136                                 }
137                         ;; esac
138                 done
139         }
140
141         # is a dynamically linked executable
142         if [ -n "$LDSO" ]; then
143                 echo "Bundling ${BIN##*/}"
144
145                 RUNDIR="$(readlink -f "$BIN")"; RUNDIR="${RUNDIR%/*}"
146                 RUN="${LDSO#ld-}"; RUN="run-${RUN%%.so*}.sh"
147                 REL="$(_relpath "$DIR/lib" "$BIN")"
148
149                 _mv "$BIN" "$RUNDIR/.${BIN##*/}.bin"
150
151                 cat <<-EOF > "$BIN"
152                         #!/usr/bin/env bash
153                         dir="\$(dirname "\$0")"
154                         export RUNAS_ARG0="\$0"
155                         export LD_PRELOAD="\$dir/${REL:+$REL/}runas.so"
156                         exec "\$dir/${REL:+$REL/}$LDSO" --library-path "\$dir/${REL:+$REL/}" "\$dir/.${BIN##*/}.bin" "\$@"
157                 EOF
158
159                 chmod ${VERBOSE:+-v} 0755 "$BIN"
160         fi
161 done