On 9/20/2021 8:57 AM, Ken Brown via Cygwin-apps wrote:
On 9/20/2021 1:58 AM, ASSI wrote:
Ken Brown via Cygwin-apps writes:
2. Create a script /usr/bin/rebaselst_usr similar to rebaselst, with a
few modifications:

a) All files are in the user's home directory:

l=${HOME}/.config/rebase
b=${HOME}/.cache/rebase
db=${l}/dynpath.d

I've taken a stab at this (attached). The appropriate command, then, is 'rebaselst_usr dyn rebase'. Within emacs I would use something like

(if (eq system-type 'cygwin)
    (call-process "rebaselst_usr" nil
                  '(:file "<log file>")
                  nil "dyn" "rebase"))

I'm not sure yet where this would go so that it's called after compilation but before the compiled file is loaded. I'll see if Eli can help.

Ken
#!/bin/dash
export PATH=/bin
l=${HOME}/.config/rebase
b=${HOME}/.cache/rebase
eb="${b}/rebase_epoch"
db="${l}/dynpath.d"
for d in ${b} ${db} ; do
  if [ ! -d ${d} ] ; then
    echo "Directory ${d} does not exist, trying to re-create."
    mkdir -p ${d}
  fi
done

BaseAddress=''
if [ "x86_64" = $( uname -m ) ] ; then
  DefaultBaseAddress=0x400000000
else
  DefaultBaseAddress=0x070000000
fi    

rebuild="no"
noaslr="no"
verbose=""
doSuffixes='dll|so|oct|mex|eln'
exeSuffixes='exe'

dynPaths=$( cat ${db}/* | sort -u )
dynLocs=''
for d in ${dynPaths} ; do
  [ -d "${d}" ] && dynLocs="${dynLocs} ${d}"
done

usage () {
  echo "
rebaselst [-h | --help | [ <cmd> | <cmd1> <cmd2> ... <cmdn> ]]

Commands, will be executed in order:
------------------------------------

--verbose
        Run some commands with verbose output.

--rebuild
        Later commands will discard information in cache files and
        rebuild them from scratch.

--no-rebuild
        Keep and use information in cache files.  This is the default,
        can be used to cancel effect of a \"--rebuild\" earlier on the
        command line.

update
        Update all caches.

dyn
        Update cache for dynamic language modules and libraries.  This
        currently implies \"--rebuild\" since modules could have been
        installed by the user and the cache would be outdated.

--noaslr
        Remove ASLR and TSAware flags from dynamic objects.

rebase
        Rebase with the information in cache files (i.e does not imply
        an \"update\").

peflags
        Set PE flags on executables with the information in cache
        files (i.e does not imply an \"update\").

"
}

check_file () {
  if [ "$2" = "yes" -a  -e $1 ] ; then
    echo "removing $1"
    rm -f $1
  fi
  if [ ! -e "${eb}" ] ; then
      touch -d "@0"                          "${eb}" || \
          touch -d "1980-01-01 00:00 UTC"    "${eb}" || \
          touch -d "1999-05-03 07:29:06 UTC" "${eb}" || \
          touch -d "2002-08-17 07:00 CEST"   "${eb}"
  fi
  if [ ! -e "$1" ] ; then
    echo "creating empty $1"
    touch -r "${eb}"  "$1"
  fi
  if [ "$2" != "nowrite" -a  -e $1 ] ; then
    chmod 644 "$1"
  fi
}

_tr () {
  local IFS s r
  IFS="/-+.,"
  for s in $1; do
    r="${r}_${s}"
  done
  echo "${r}"
}

update_file () {
  local IFS f g k l m
  f="$1"
  g="${f}.old"
  mv -f "${f}" "${g}"
  cat >"${f}" <<EOF
## autogenerated, do not edit!
EOF
    IFS='
'
  for k in $( grep -E '^# rebase_pkg' "${g}" | tr -c '[:alnum:]\n:' '_' ) ; do
    l=${k##*from:_}
    m=$(( ${l-0}+=1 ))
  done
  {
    m=0
    for k in $( grep -vE "^##" "${g}" ) ; do
      if [ -e "${k}" -a "${m}" = "0" ] ; then
        echo "${k}"
      elif [ "#" = "${k%% rebase_pkg*}" ] ; then
        l=$( _tr "${k##*from: /}" )
        if [ "$(( ${l-0}-=1 ))" = "0" ] ; then
          m=0
          echo "${k}"
        else
          m=1
          echo "#${k}"
        fi
      elif [ ! "##" = "${k%%[^#]*}" ] ; then
        echo "#${k}"
      fi
    done
  } >>"${f}"
  chmod 444 "${f}"
}

rebase_do () {
  local g
  g="${b}/rebase_all"
  echo "Rebasing with list ${g}, built from $@."
  cat $@ | grep -vE '^#' | sort -u >"${g}"
  if [ ! -e "/etc/rebase.db.i386" -a ! -e "/etc/rebase.db.x86_64" ] ; then
    BaseAddress="-b ${DefaultBaseAddress}"
  fi
  rebase ${BaseAddress} ${verbose} -n -O -T "${g}"
  if [ "noaslr" = "yes" ] ; then
    peflags ${verbose} -d0 -t0 -T "${g}"
  fi
  BaseAddress=''
}

peflags_do () {
  local g
  g="${b}/rebase_all_exe"
  echo "Setting PE flags with list ${g}, built from $@."
  cat $@ | grep -vE '^#|ddd\/Ddd\.exe$' | sort -u >"${g}"
  peflags ${verbose} -t1 -T "${g}"
}

rebase_dyn () {
  local g
  g="${b}/rebase_dyn"
  check_file "${g}" "yes"
  if [ "x" = "${dynLocs:-x}" ] ; then
    touch "${g}"
  else
    echo "Looking for dynamic language modules/libraries in:"
    for d in ${dynLocs} ; do echo "  ${d}" ; done
    echo "Updating rebase information for dynamic language modules/libraries 
${g}."
    find ${dynLocs} -xdev -regextype posix-extended -regex 
".*\.(${doSuffixes})$" -newer "${g}" >>"${g}"
  fi
  update_file "${g}"
}

if [ "$#" = "0" ] ; then
  set -- "--help"
fi
while [ $# -gt 0 ] ; do 
  case "$1" in
    --verbose )
      verbose=--verbose
      ;;
    --rebuild )
      rebuild=yes
      ;;
    --noaslr )
      noaslr=yes
      ;;
    --no-rebuild )
      rebuild=no
      ;;
    rebase )
      rebase_do "${b}/rebase_dyn"
      ;;
    peflags )
      peflags_do "${b}/rebase_exe"
      ;;
    update )
      shift
      set -- dummy dyn $@
      ;;
    dyn )
      rebase_dyn
      ;;
    -h|--help|* )
      usage
      exit 127
      ;;
  esac
  shift
done
exit 0

Reply via email to