-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathsetrpaths.sh
executable file
·162 lines (143 loc) · 3.96 KB
/
setrpaths.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
#!/bin/bash
function print_usage {
echo "Usage: $0 --path <path to search> [--add_origin] [--add_path=<path>]"
}
function patch_rpath {
local filename=${1?No filename given}
local rpath=''
local filetype=$(file -b $filename)
local REX_DYNAMIC="^ELF 64-bit LSB.*dynamically linked.*"
local REX_SO="^ELF 64-bit LSB shared object.*x86-64.*"
local REX_OS_INTERPRETER=".*interpreter /lib64/ld-linux-x86-64.so.2.*"
local REX_LINUX_INTERPRETER=".*interpreter.*ld-linux-x86-64.so.2"
local REX_LSB_INTERPRETER=".*interpreter.*ld-lsb-x86-64.so.3"
if [[ -n "$NIXUSER_PROFILE" ]]; then
PREFIX=$NIXUSER_PROFILE
INTERPRETER=$PREFIX/lib/ld-linux-x86-64.so.2
FORCE_RPATH=""
elif [[ -n "$EPREFIX" ]]; then
PREFIX=$EPREFIX
if [[ $EBVERSIONGENTOO -ge 2023 ]]; then
INTERPRETER=$PREFIX/lib64/ld-linux-x86-64.so.2
else
INTERPRETER=$PREFIX/lib/ld-linux-x86-64.so.2
fi
PREFIX=$EPREFIX
FORCE_RPATH="--force-rpath"
else
echo "Neither nixpkgs nor gentoo modules are loaded. Aborting"
exit 1
fi
if [[ $filetype =~ $REX_DYNAMIC ]]; then
if [[ $filetype =~ $REX_OS_INTERPRETER ]]; then
patchelf --set-interpreter "$INTERPRETER" "$filename"
rpath='set'
elif [[ $ARG_ANY_INTERPRETER -eq 1 && ( $filetype =~ $REX_LINUX_INTERPRETER || $filetype =~ $REX_LSB_INTERPRETER ) ]]; then
patchelf --set-interpreter "$INTEPRETER" "$filename"
rpath='set'
fi
fi
if [[ $filetype =~ $REX_SO ]]; then
if ! ldd $filename | grep 'statically linked' > /dev/null; then
rpath='set'
fi
fi
if [[ -n "$rpath" && ("$ARG_ADD_ORIGIN" == "1" || -n "$ARG_ADD_PATH") ]] ; then
rpath=$(patchelf --print-rpath "$filename")
rpath_old=$rpath
if [[ "$ARG_ADD_ORIGIN" == "1" && "${rpath##\$ORIGIN}" == "$rpath" ]]; then
if [ -n "$rpath" ] ; then
rpath='$ORIGIN:'$rpath
else
rpath='$ORIGIN'
fi
fi
if [[ -n "$ARG_ADD_PATH" ]]; then
if [ -n "$rpath" ] ; then
rpath="$ARG_ADD_PATH:"$rpath
else
rpath="$ARG_ADD_PATH"
fi
fi
if [ "$rpath" != "$rpath_old" ] ; then
patchelf $FORCE_RPATH --set-rpath "$rpath" "$filename"
fi
fi
}
function patch_zip {
local filename=${1?Missing filename}
local tmp=$(mktemp --directory)
local fullname=$(readlink -f $filename)
cd $tmp
# Extract all and patch every binary file, and update the archive
unzip -q $fullname
for fname in $(find . -type f); do
oldperm=$(stat --format %a $fname)
chmod u+w $fname
patch_rpath $fname;
chmod $oldperm $fname
done
zip -rq $fullname .
cd -
rm -rf $tmp
}
TEMP=$(getopt -o p: --longoptions path:,add_origin,add_path:,any_interpreter -n $0 -- "$@")
eval set -- "$TEMP"
ARG_ADD_ORIGIN=0
ARG_ADD_PATH=
ARG_ANY_INTERPRETER=0
while true; do
case "$1" in
-p|--path)
case "$2" in
"") ARG_PATH=""; shift 2 ;;
*) ARG_PATH=$2; shift 2 ;;
esac ;;
--add_origin)
ARG_ADD_ORIGIN=1; shift ;;
--any_interpreter)
ARG_ANY_INTERPRETER=1; shift ;;
--add_path)
case "$2" in
"") ARG_ADD_PATH=""; shift 2 ;;
*) ARG_ADD_PATH=$2; shift 2 ;;
esac ;;
--) shift; break ;;
*) echo "Unknown parameter $1"; print_usage; exit 1 ;;
esac
done
if [[ -z "$ARG_PATH" ]]; then
print_usage; exit 1
fi
# Avoid failing to set rpaths when a symlink is provided
if [[ -L "$ARG_PATH" ]]; then
echo "error: $ARG_PATH is a symlink. Please provide a path not a symlink."
exit 1
fi
SAVEIFS=$IFS
IFS=$(echo -en "\n\b")
for filename in $(find $ARG_PATH -type f -print0 | python -c '
# quick pre-filter to cut down on "file -b" overhead
import sys
sys.stdout.reconfigure(line_buffering=True)
for f in sys.stdin.buffer.read()[:-1].split(b"\0"):
header = b"\x7fELF"
if f[-4:] not in {b".whl", b".jar"}:
with open(f, "rb") as myfile:
header = myfile.read(4)
if header == b"\x7fELF":
sys.stdout.buffer.write(f+b"\n")
'); do
if [[ -z "$filename" ]]; then
continue
fi
if [[ $filename == *.jar ]]; then
patch_zip $filename
elif [[ $filename == *.whl ]]; then
patch_zip $filename
else
patch_rpath $filename
fi
done
IFS=$SAVEIFS
exit 0