-
Notifications
You must be signed in to change notification settings - Fork 0
/
rebaseall.in
263 lines (241 loc) · 6.32 KB
/
rebaseall.in
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
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
#!@POSIXSHELL@
#
# Copyright (c) 2003, 2005, 2006, 2008, 2011, 2012 Jason Tishler
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
#
# See the COPYING file for full license information.
#
# Written by Jason Tishler <jason@tishler.net>
#
# $Id$
#
# Define constants
tp1=${0%/*}
tp2=${tp1:-.}
PATH=$(cd $tp2 && pwd):@bindir@:/bin
ProgramName=${0##*/}
ProgramOptions='48b:o:ps:tT:v'
DefaultBaseAddress=0x70000000
DefaultOffset=@DEFAULT_OFFSET_VALUE@
DefaultTouch=
DefaultNoDyn=
DefaultVerbose=
DefaultFileList=
DefaultSuffixes='dll|so|oct'
# Define functions
usage()
{
echo "usage: ${ProgramName} [-b BaseAddress] [-o Offset] [-s DllSuffix] [-T FileList | -] [-4|-8] [-p] [-t] [-v]"
exit 1
}
cleanup()
{
rm -f "${TmpFile}" "${SortedFile}"
exit ${ExitCode}
}
# Set traps
trap cleanup 1 2 15
# Set defaults
BaseAddress=""
Offset="${DefaultOffset}"
Touch="${DefaultTouch}"
NoDyn="${DefaultNoDyn}"
Verbose="${DefaultVerbose}"
FileList="${DefaultFileList}"
Suffixes="${DefaultSuffixes}"
db_file_i386="@sysconfdir@/rebase.db.i386"
db_file_x86_64="@sysconfdir@/rebase.db.x86_64"
check_for_dash_only="yes"
case `uname -m` in
i[3456]86)
db_file="${db_file_i386}";
Mach="-4"
;;
x86_64)
Mach="-8"
db_file="${db_file_x86_64}"
;;
esac
# Determine platform
Platform=`uname -s`
case $Platform in
*MINGW* | *mingw* ) Platform=mingw ;;
*CYGWIN* | *cygwin* ) Platform=cygwin ;;
*MSYS* | *msys* ) Platform=msys ;;
* )
echo "Unsupported platform: $Platform" 1>&2
exit 1
;;
esac
# On x86_64 Cygwin, set DefaultAddressBase to 0x4:00000000
case $Platform in
cygwin )
[ `uname -m` = "x86_64" ] && DefaultBaseAddress=0x400000000
;;
* )
;;
esac
# Parse command line arguments
while getopts "${ProgramOptions}" Option "$@"
do
case "${Option}" in
4)
db_file="${db_file_i386}";
Mach="-4"
;;
8)
Mach="-8"
db_file="${db_file_x86_64}"
;;
b)
BaseAddress="${OPTARG}";;
o)
Offset="${OPTARG}";;
p)
check_for_dash_only="no";;
s)
Suffixes="${Suffixes}|${OPTARG}";;
t)
Touch="-t";;
T)
FileList="${OPTARG}";;
v)
Verbose="-v";;
\?)
usage;;
esac
done
# Verify only ash or dash processes are running
if [ "${check_for_dash_only}" != "no" ]
then
ProcessResult=0
case $Platform in
mingw|msys )
/bin/ps -s | /bin/gawk '\
# Count number of running ash or dash. \
/\/bin\/(d)?ash(\.exe)?$/{ ash_cnt++; } \
# Count number of ps and gawk. \
/\/bin\/ps(\.exe)?$/{ cnt++; } \
/\/bin\/gawk(\.exe)?$/{ cnt++; } \
END{ \
# Uncomment for testing: \
# printf "TOTAL: %d CNT: %d ASH_CNT: %d\n", NR, cnt, ash_cnt; \
# Only one of ps and gawk each may run. \
if (cnt > 2) \
exit 0; \
# The total number of allowed processes is one less than the \
# number of input lines. The extra line is the ps header output. \
if (NR - cnt - ash_cnt > 1) \
exit 0; \
# All is well. \
exit 1; \
}'
ProcessResult=$?
;;
cygwin )
grep -E -q -i -v '/d?ash(.exe)?$' /proc/[0-9]*/exename
ProcessResult=$?
;;
esac
if [ $ProcessResult -eq 0 -a -z "${RebaseDebug}" ]
then
echo "${ProgramName}: only ash or dash processes are allowed during rebasing"
echo " Exit all Cygwin processes and stop all Cygwin services."
echo " Execute ash (or dash) from Start/Run... or a cmd or command window."
echo " Execute '/bin/rebaseall' from ash (or dash)."
exit 2
fi
fi
# Check if rebase database already exists.
database_exists="no"
[ -f "${db_file}" ] && database_exists="yes"
# If BaseAddress has not been specified, and the rebase database doesn't exist
# yet, set BaseAddress to default.
if [ -z "${BaseAddress}" -a "${database_exists}" != "yes" ]
then
BaseAddress=$DefaultBaseAddress
fi
# Set temp directory
TmpDir="${TMP:-${TEMP:-/tmp}}"
# Validate temp directory
if [ ! -d "$TmpDir" ]
then
echo "$ProgramName: '$TmpDir' is not a directory"
exit 2
fi
if [ ! -w "$TmpDir" ]
then
echo "$ProgramName: '$TmpDir' is not writable"
exit 2
fi
# Validate user supplied file list, if necessary
if [ -n "$FileList" -a ! -r "$FileList" -a "$FileList" != - ]
then
echo "$ProgramName: '$FileList' is not readable"
exit 2
fi
# Set temp file
SortedFile="$TmpDir/rebase.lst"
TmpFile="${SortedFile}.in"
# Create rebase list
case $Platform in
cygwin)
find /etc/setup -name '*.lst.gz' | xargs gzip -d -c |
grep -E "\.($Suffixes)\$" |
sed -e '/\/cygwin1\.dll$/d' -e '/\/cyglsa.*\.dll$/d' \
-e '/sys-root\/mingw/d' -e 's/^/\//' \
-e '/\/d\?ash\.exe$/d' -e '/\/rebase\.exe$/d' >"${TmpFile}"
# some interpreters include a method for installing addons outside of the
# package manager, such as CPAN and RubyGems.
for d in /usr/lib/perl5/site_perl /usr/lib/py*/site-packages \
/usr/lib/php /usr/lib/R/site-library /usr/lib/rub*/gems \
/usr/lib/octave/site
do
if [ -d $d ]
then
find $d -type f | grep -E "\.($Suffixes)\$" >>"${TmpFile}"
fi
done
# Unconditionally add the -n flag so rebased DLLs get the
# dynamicbase flag removed.
NoDyn='-n'
;;
mingw|msys)
for f in /bin /lib
do
find $f -type f |
grep -E "\.($Suffixes)\$" |
sed -e '/\/msys-1\.0.*\.dll$/d' -e '/\/cygwin1\.dll$/d' \
-e '/\/cyglsa.*\.dll$/d' -e '/\/d\?ash\.exe$/d' \
-e '/\/rebase\.exe$/d' >>"$TmpFile"
done
;;
esac
# Append user supplied file list, if any
if [ -n "${FileList}" ]
then
cat "${FileList}" >>"${TmpFile}"
fi
# Remove duplicates
sort -u "${TmpFile}" > "${SortedFile}"
if [ -z "${BaseAddress}" ]
then
rebase "${Verbose}" "${Touch}" "${NoDyn}" -s "${Mach}" -T "${SortedFile}"
else
rebase "${Verbose}" "${Touch}" "${NoDyn}" -s "${Mach}" -b "${BaseAddress}" -o "${Offset}" -T "${SortedFile}"
fi
ExitCode=$?
# Clean up
cleanup