-
Notifications
You must be signed in to change notification settings - Fork 6
/
fixpath.sh
executable file
·164 lines (141 loc) · 3.89 KB
/
fixpath.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
163
164
#! /bin/sh
if [ $# -ne 2 ]; then
echo "${0} <loader path> <libdir>"
exit 2
elif [ ! -f "${1}" ] || [ ! -d "${2}" ]; then
echo "Invalid arguments."
exit 2
fi
LIBDIR=`cd "${2}" && pwd` || exit 2
BINDIR=`dirname "${1}"` || exit 2
BINDIR=`cd "${BINDIR}" && pwd` || exit 2
BINFILE=`basename "${1}"` || exit 2
BINNAME="${BINDIR}/${BINFILE}"
SYSTEM=`uname -s` || exit 1
CWD=`dirname "${0}"` || exit 1
CWD=`cd "${CWD}" && pwd` || exit 1
export PATH="${CWD}/tools/bin"
linux()
{
elf=`readelf -d "${BINNAME}" 2>/dev/null`
if [ $? -ne 0 ]; then # Nothing I can do.
echo "[IGNORE] ${BINNAME} is not ELF."
return 0
fi
chmod +wx "${BINNAME}" || return 1
rpath=`echo "${elf}" | awk '{ if ($2 == "(RPATH)") { print substr($5, 2, length($5) - 2); exit(0); } }'`
runpath=`echo "${elf}" | awk '{ if ($2 == "(RUNPATH)") { print substr($5, 2, length($5) - 2); exit(0); } }'`
if [ -z "${runpath}" ] && [ -z "${rpath}" ]; then
echo "[IGNORE] ${BINNAME} has neither DT_RUNPATH nor DT_RPATH."
return 0
fi
lib=`ldd "${BINNAME}"` || return 1
newpath=`echo "${lib}" | awk "{
if (\\$3 == \\"\\" || substr(\\$3, 1, 1) != \\"/\\") next
cmd=\"readlink -e \"\\$3
cmd | getline bin
close(cmd)
split(bin, a, \\"/\\")
split(\\"${BINDIR}\\", b, \\"/\\")
split(\\"${LIBDIR}\\", e, \\"/\\")
if (length(a) != length(e) + 1) next
for (i = 2; i <= length(a); ++i) {
if (a[i] != e[i]) {
break
}
}
if (i != length(a)) next
size = length(b) < length(e) ? length(b) : length(e)
for (i = 1; i < size; ++i) {
if (b[i + 1] != e[i + 1]) {
break
}
}
remdir = i
updir = length(b) - remdir
ret = \\"\\$ORIGIN\\"
for (i = 0; i < updir; ++i) {
ret = ret \\"/..\\"
}
for (i = remdir + 1; i <= length(e); ++i) {
if (e[i] == \\"\\") break
ret = ret \\"/\\" e[i]
}
print ret
exit 170
}"`
ret=$?
if [ ${ret} -eq 0 ]; then
echo "[REMOVE] ${BINNAME} has no dependencies in ${LIBDIR}."
chrpath -d "${BINNAME}"
return $?
elif [ ${ret} -ne 170 ]; then
return 1
fi
echo "[CHANGE] ${BINNAME} set to [${newpath}]."
if [ -n "${runpath}" ]; then
chrpath -r "${newpath}" "${BINNAME}" || return 1
else
chrpath -c -r "${newpath}" "${BINNAME}" || return 1
fi
return 0
}
macosx()
{
d=`otool -D "${BINNAME}" 2>/dev/null`
if [ $? -ne 0 ]; then
echo "[IGNORE] ${BINNAME} type unknown."
return 0
fi
if [ x`echo "${d}" | awk '{ print $NF; exit }'` != x"${BINNAME}:" ]; then
echo "[IGNORE] ${BINNAME} is not Mach-O."
return 0
fi
o=`echo "${d}" | tail -n +2`
if [ $? -ne 0 ]; then
return 1
fi
l=`otool -L "${BINNAME}" 2>/dev/null`
if [ $? -ne 0 ]; then
return 1
fi
chmod +wx "${BINNAME}" || return 1
if [ -n "${o}" ]; then
install_name_tool -id "${BINNAME}" "${BINNAME}"
if [ $? -ne 0 ]; then
return 1
fi
o=`echo "${l}" | tail -n +3`
if [ $? -ne 0 ]; then
return 1
fi
else
o=`echo "${l}" | tail -n +2`
if [ $? -ne 0 ]; then
return 1
fi
fi
if [ -z "${o}" ]; then
return 0
fi
echo "${o}" | awk "{
if (substr(\$1, 1, 1) != \"/\") {
print \"[CHANGE] ${BINNAME}: \" \$1 \" to ${LIBDIR}/\" \$1
if (system(\"install_name_tool -change \" \$1 \" ${LIBDIR}/\" \$1 \" ${BINNAME}\")) exit 1
}
}"
return $?
}
case "${SYSTEM}" in
Linux)
linux
;;
Darwin)
macosx
;;
*)
echo "Unsupported machine: ${SYSTEM}"
false
;;
esac
exit $?