codekingpro/portable-devtools
114k
1#!/usr/bin/dash2 3#4# Copyright (c) 2003, 2005, 2006, 2008, 2011, 2012 Jason Tishler5#6# This program is free software: you can redistribute it and/or modify7# it under the terms of the GNU General Public License as published by8# the Free Software Foundation, either version 2 of the License, or9# (at your option) any later version.10#11# This program is distributed in the hope that it will be useful,12# but WITHOUT ANY WARRANTY; without even the implied warranty of13# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the14# GNU General Public License for more details.15#16# You should have received a copy of the GNU General Public License17# along with this program. If not, see <http://www.gnu.org/licenses/>.18#19# See the COPYING file for full license information.20#21# Written by Jason Tishler <jason@tishler.net>22#23# $Id$24#25 26# Define constants27tp1=${0%/*}28tp2=${tp1:-.}29PATH=$(cd $tp2 && pwd):/usr/bin:/bin30 31ProgramName=${0##*/}32ProgramOptions='48b:io:ps:tT:v'33DefaultBaseAddress=0x7000000034DefaultOffset=035DefaultTouch=36DefaultNoDyn=37DefaultVerbose=38DefaultFileList=39DefaultSuffixes='dll|so|oct'40DatabaseFlag=-s41 42# Define functions43usage()44{45 echo "usage: ${ProgramName} [-b BaseAddress] [-o Offset] [-s DllSuffix] [-T FileList | -] [-4|-8] [-p] [-t] [-i] [-v]"46 exit 147}48 49cleanup()50{51 rm -f "${TmpFile}"52 exit ${ExitCode}53}54 55# Set traps56trap cleanup 1 2 1557 58# Set defaults59BaseAddress=""60Offset="${DefaultOffset}"61Touch="${DefaultTouch}"62NoDyn="${DefaultNoDyn}"63Verbose="${DefaultVerbose}"64FileList="${DefaultFileList}"65Suffixes="${DefaultSuffixes}"66db_file_i386="/etc/rebase.db.i386"67db_file_x86_64="/etc/rebase.db.x86_64"68check_for_dash_only="yes"69case `uname -m` in70 i[3456]86)71 db_file="${db_file_i386}";72 Mach="-4"73 ;;74 x86_64)75 Mach="-8"76 db_file="${db_file_x86_64}"77 ;;78esac79 80# Determine platform81Platform=`uname -s`82case $Platform in83 *MINGW* | *mingw* ) Platform=mingw ;;84 *CYGWIN* | *cygwin* ) Platform=cygwin ;;85 *MSYS* | *msys* ) Platform=msys ;;86 * )87 echo "Unsupported platform: $Platform" 1>&288 exit 189 ;;90esac91 92# On x86_64 Cygwin, set DefaultAddressBase to 0x4:0000000093case $Platform in94 cygwin | msys )95 [ `uname -m` = "x86_64" ] && DefaultBaseAddress=0x40000000096 ;;97 * )98 ;;99esac100 101# Parse command line arguments102while getopts "${ProgramOptions}" Option "$@"103do104 case "${Option}" in105 4)106 db_file="${db_file_i386}";107 Mach="-4"108 ;;109 8)110 Mach="-8"111 db_file="${db_file_x86_64}"112 ;;113 b)114 BaseAddress="${OPTARG}";;115 o)116 Offset="${OPTARG}";;117 p)118 check_for_dash_only="no";;119 s)120 Suffixes="${Suffixes}|${OPTARG}";;121 t)122 Touch="-t";;123 T)124 FileList="${OPTARG}";;125 i)126 DatabaseFlag=;;127 v)128 Verbose="-v";;129 \?)130 usage;;131 esac132done133 134# Verify only ash or dash processes are running135if [ "${check_for_dash_only}" != "no" ]136then137 ProcessResult=0138 case $Platform in139 mingw|msys )140 /usr/bin/ps -s | /usr/bin/gawk '\141 # Count number of running ash or dash. \142 /\/usr\/bin\/(d)?ash(\.exe)?$/{ ash_cnt++; } \143 # Count number of ps and gawk. \144 /\/usr\/bin\/ps(\.exe)?$/{ cnt++; } \145 /\/usr\/bin\/gawk(\.exe)?$/{ cnt++; } \146 END{ \147 # Uncomment for testing: \148 # printf "TOTAL: %d CNT: %d ASH_CNT: %d\n", NR, cnt, ash_cnt; \149 # Only one of ps and gawk each may run. \150 if (cnt > 2) \151 exit 0; \152 # The total number of allowed processes is one less than the \153 # number of input lines. The extra line is the ps header output. \154 if (NR - cnt - ash_cnt > 1) \155 exit 0; \156 # All is well. \157 exit 1; \158 }'159 ProcessResult=$?160 ;;161 cygwin )162 grep -E -q -i -v '/d?ash(.exe)?$' /proc/[0-9]*/exename163 ProcessResult=$?164 ;;165 esac166 if [ $ProcessResult -eq 0 -a -z "${RebaseDebug}" ]167 then168 echo "${ProgramName}: only ash or dash processes are allowed during rebasing"169 echo " Exit all Cygwin processes and stop all Cygwin services."170 echo " Execute ash (or dash) from Start/Run... or a cmd or command window."171 echo " Execute '/usr/bin/rebaseall' from ash (or dash)."172 exit 2173 fi174fi175 176# Check if rebase database already exists.177database_exists="no"178[ -f "${db_file}" ] && database_exists="yes"179 180# If BaseAddress has not been specified, and the rebase database doesn't exist181# yet, set BaseAddress to default, unless ignore database has been specified.182if [ -z "${BaseAddress}" -a "${database_exists}" != "yes" -a -n "${DatabaseFlag}" ]183then184 BaseAddress=$DefaultBaseAddress185fi186 187# Set temp directory188TmpDir="${TMP:-${TEMP:-/tmp}}"189 190# Validate temp directory191if [ ! -d "$TmpDir" ]192then193 echo "$ProgramName: '$TmpDir' is not a directory"194 exit 2195fi196if [ ! -w "$TmpDir" ]197then198 echo "$ProgramName: '$TmpDir' is not writable"199 exit 2200fi201 202# Validate user supplied file list, if necessary203if [ -n "$FileList" -a ! -r "$FileList" -a "$FileList" != - ]204then205 echo "$ProgramName: '$FileList' is not readable"206 exit 2207fi208 209# Set temp file210TmpFile="$TmpDir/rebase.lst"211# Prepend user supplied file list, if any212if [ -n "${FileList}" ]213then214 cat "${FileList}" >"${TmpFile}"215else216 [ -f "${TmpFile}" ] && rm -f "${TmpFile}"217 touch "${TmpFile}"218fi219 220# Create rebase list221case $Platform in222 cygwin)223 find /etc/setup -name '*.lst.gz' | xargs gzip -d -c |224 grep -E "\.($Suffixes)\$" |225 sed -e '/\/cygwin1\.dll$/d' -e '/\/cyglsa.*\.dll$/d' \226 -e '/sys-root\/mingw/d' -e 's/^/\//' \227 -e '/\/d\?ash\.exe$/d' -e '/\/rebase\.exe$/d' >>"${TmpFile}"228 # some interpreters include a method for installing addons outside of the229 # package manager, such as CPAN and RubyGems.230 for d in /usr/lib/perl5/site_perl /usr/lib/py*/site-packages \231 /usr/lib/py*/lib-dynload \232 /usr/lib/php /usr/lib/R/site-library /usr/lib/rub*/gems \233 /usr/lib/octave/site234 do235 if [ -d $d ]236 then237 find $d -type f | grep -E "\.($Suffixes)\$" >>"${TmpFile}"238 fi239 done240 # Unconditionally add the -n flag so rebased DLLs get the241 # dynamicbase flag removed.242 NoDyn='-n'243 ;;244 mingw|msys)245 for f in /usr/bin /usr/lib246 do247 find $f -type f |248 grep -E "\.($Suffixes)\$" |249 sed -e '/\/msys-2\.0.*\.dll$/d' -e '/\/cygwin1\.dll$/d' \250 -e '/\/cyglsa.*\.dll$/d' -e '/\/d\?ash\.exe$/d' \251 -e '/\/rebase\.exe$/d' >>"$TmpFile"252 done253 # some interpreters include a method for installing addons outside of the254 # package manager, such as CPAN and RubyGems.255 for d in /usr/lib/perl5/site_perl /usr/lib/py*/site-packages \256 /usr/lib/php /usr/lib/R/site-library /usr/lib/rub*/gems \257 /usr/lib/octave/site258 do259 if [ -d $d ]260 then261 find $d -type f | grep -E "\.($Suffixes)\$" >>"${TmpFile}"262 fi263 done264 # Unconditionally add the -n flag so rebased DLLs get the265 # dynamicbase flag removed.266 NoDyn='-n'267 ;;268esac269 270if [ -z "${BaseAddress}" ]271then272 rebase "${Verbose}" "${Touch}" "${NoDyn}" "${DatabaseFlag}" "${Mach}" -T "${TmpFile}"273else274 rebase "${Verbose}" "${Touch}" "${NoDyn}" "${DatabaseFlag}" "${Mach}" -b "${BaseAddress}" -o "${Offset}" -T "${TmpFile}"275fi276ExitCode=$?277 278# Clean up279cleanup280 