Subject: ANNOUNCE: cygwin `ldd' script
From: [email-masked] (Gary V. Vaughan)

11 Dec 1998 02:58:10 -0800
Message-ID: <[email-masked]>
References: <[email-masked]>
Mime-Version: 1.0
Content-Type: multipart/mixed;
 boundary="------------33CE9EA05AF2CC2D353AA745"
To: Mumit Khan <[email-masked]>
Cc: [email-masked]

This is a multi-part message in MIME format.
--------------33CE9EA05AF2CC2D353AA745
Content-Type: text/plain; charset=us-ascii
Content-Transfer-Encoding: 7bit

Attached is an all singing, all dancing ldd-a-like for cygwin.  Do with
it what you will, perhaps add it to the ftp.franken.de repository?

I might convert some more of the functionality from cygcheck in a few
weeks, once I have libtool fixed to build dlls.

Thanks again Mumit for the response:

Mumit Khan wrote:
> 
> On Wed, 9 Dec 1998, Gary V. Vaughan wrote:
> 
> > Ahh.. I didn't know dll's could depend on one another.  Are these
> > dependencies encoded into the dependee by cygwin's ld at linktime?
> 
> It's the same as on other Unix systems, where shared libraries can
> have dependencies as well (the win32 dll architecture is of various
> quite different from the various flavors of Unix shlibs, but it's the 
> same basic idea).

I see.  It appears (by experiment!) that the final link of win32 dll
does indeed list the other link-time dlls in the .idata section of the
resulting library.  Dll's are less brain-damaged than I had thought
after all =)O|

> Few comments
>
> - absolute pathname may not work as is (eg., ``ldd /bin/sh.exe'')

You're right.  Fixed in the attached version.
 
> - You do need to sort out duplicates

Right again.  I fixed this too.

> - the path search is confusing. for example, let's say I have a file
>   called foo.exe in my current working directory (which is not in my
>   path), and a file called foo.exe in my PATH. Now if I do:
> 
>     $ ldd foo.exe
> 
>   it picks up the one in the PATH instead of the one in the current
>   directory!

I think this is how it should work.  In the circumstance you describe
above, if I typed `foo.exe' to my bash prompt, I would expect the
foo.exe found in my $PATH to run.

I have changed the dll lookup (after an exe is found) to look in the
executables directory, the current working directory and then to search
the PATH.  Despite what I said about choosing an executable file to
run,  I assume the win32 runtime loader will search for dlls as you
describe, regardless of the shell being used.

> Hope I read the algorithm correctly -- don't have a windows
> machine to test it out.

Yup, fine.  It is a little clearer in the new version I hope =)O|

>   You *may* need to extend the search path to include the default
>   search path for win32 DLLs (different for win9x and NT), including
>   the directory containing executable as well as the current working
>   directory.

How do I find what these are?  I have /WINNT/System32 and /WINNT put in
my path by either bash or the NT kernel... are these the directories you
mean?

> Regards,
> Mumit

Many thanks for taking the time to look at this.  I hope to roll this
(and the rest of the required bits) into CVS libtool in the next week or
two -- with luck libtool-1.3 should be able to build dll's when it is
released [RSN =)O|]

Cheers,
	Gary.
--------------33CE9EA05AF2CC2D353AA745
Content-Type: text/plain; charset=us-ascii;
 name="ldd"
Content-Transfer-Encoding: 7bit
Content-Disposition: inline;
 filename="ldd"

#! /bin/sh
#  -*- Mode: Sh -*- 
# ----------------------------------------------------------------------
# ldd --- an unix ldd(1) work-alike for cygwin
#
# Author:	      Gary V. Vaughan <[email-masked]>
# Maintainer:	      Gary V. Vaughan <[email-masked]>
# Created:	      Tue Dec  8 08:43:00 1998
# Last Modified:      Thu Dec 10 16:04:34 1998				      
#            by:      Gary V. Vaughan <[email-masked]>	      
#
# ----------------------------------------------------------------------
# @(#) $Id: ldd,v 1.2 1998/12/10 16:07:04 garyv Exp $
# ----------------------------------------------------------------------
# Copyright (C) 1998 Gary V. Vaughan

# 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; see the file COPYING.  If not, write to
# the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
# Boston, MA 02111-1307, USA.

# Code:

while test $# -gt 0; do
  case "$1" in
  --v | --ve | --ver | --vers | --versi | --versio | --version)
    echo 'ldd (libtool) 0.9'
    echo "Copyright (C) 1998 Gary V. Vaughan
This is free software; see the source for copying conditions.  There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE."
    exit 0
    ;;
  --h | --he | --hel | --help)
    echo "ldd [OPTION]... FILE
      --help              print this help and exit
      --version           print version information and exit
Report bugs to <[email-masked]>."
    exit 0
    ;;
  --)           # Stop option processing.
    shift; break
    ;;
  -*)
    echo >&2 'ldd:' "unrecognized option" "\`$1'"
    echo 
    exit 1
    ;;
  *)
    break
    ;;
  esac
done

if test $# != 1; then
  echo >&2 'ldd:' "missing file arguments"
  echo >&2 "Try \`ldd --help' for more information."
  exit 1
fi

LDDPATH=${LDDPATH=$PATH}

OBJDUMP=${OBJDUMP=objdump}
OBJDUMP_FLAGS=${OBJDUMP_FLAGS='-p -j idata'}

to_lower="tr 'A-Z' 'a-z'"		# convert to lower case
basename="sed s,^.*[/\\\\],,g"		# extract the basename from a path
dirname="sed s,[/\\\\][^/\\\\]*\$,,"	# extract the dirname from a path

# Sed substitution that helps us do robust quoting.  It backslashifies
# metacharacters that are still active within double-quoted strings.
sed_quote_subst='s,\([\\"\\`$\\\\]\),\\\1,g'

RE_dll='^	DLL Name: '

exts="exe dll"			# valid extensions
seen=""				# list of objects examined already
object=$1			# the object from the command line

case "$object" in
  /* | [A-Z]:* ) path=$object ;;
  */* | *\\* )   dir=`echo "$object"|$dirname`
                 path=`cd $dir && pwd``echo "$object"|$basename` ;;
  * )            path="" ;;
esac
objects=`echo "$object"|$basename|$to_lower`

# In a vain attempt to squeeze a bit of speed out of the main loop
# we try to remove any invalid directories or duplicates from the path...
#LDDPATH=`echo "$LDDPATH"| $to_lower`
lddpath=""
IFS="${IFS= 	}"; save_ifs="$IFS"; IFS=':'
for dir in $LDDPATH; do
  IFS="$save_ifs"
  lower_dir=`echo "$dir"|$to_lower`
  if test -z `echo ":$lddpath:" | $to_lower | grep ":$lower_dir:"`; then
     test -d "$dir" && lddpath="$lddpath:$dir"
  fi
done
LDDPATH=`echo "$lddpath"|sed 's,^:,,'`

# MAIN LOOP:
############
while test -n "$objects"
do
  newobjects=""
  for object in $objects; do

    # if we have no dir for object, search the LDDPATH for it
    if test -z "$path"; then
      IFS="${IFS= 	}"; save_ifs="$IFS"; IFS=':'
      for dir in $LDDPATH; do
        IFS="$save_ifs"
        for ext in $exts; do
          if test -f "$dir/$object.$ext"; then
	    object="$object.$ext"
            path="$dir/$object"
  	    break
          fi
        done
        if test -z "$path" && test -f "$dir/$object"; then
          path="$dir/$object"
        fi
	# stop searching if we have a match
        test -z "$path" || break
      done
    else
      # we have a path, so just check for extensions
      dir=`echo "$path"|$dirname`
      for ext in $exts; do
        if test -f "$dir/$object.$ext"; then
	  object="$object.$ext"
          path="$dir/$object"
  	  break
        fi
      done
    fi
    test -n "$path" || path="not found"

    # show what we have so far
    echo "$object	-> $path"

    # start the next search if OBJECT was not found
    test -f "$path" || continue

    case $object in
      *.exe)
        # ...being (a bit) careful not to introduce duplicates
        LDDPATH="$dir:.:"`echo $LDDPATH|sed -e "s,:$dir,,;s,:\.,,"`
	;;
    esac

    # escape any shell meta characters for the eval below
    path=`echo $path|sed -e "$sed_quote_subst"`
    
    # extract dependencies from current object
    new=`eval ${OBJDUMP} ${OBJDUMP_FLAGS} "$path" \
	 | grep "$RE_dll"  | sed "s,$RE_dll,," | $to_lower`
    newobjects="$new $newobjects"
    seen="$seen $object"
    path=""

    # only add NEWOBJECTS that we have not seen yet
    new=""
    for pending in $newobjects; do
      test -z "`echo \" $seen \" | grep \" $pending \"`" && new="$new $pending"
    done
    newobjects=$new
  done
  
  # maintain the loop invariants
  objects="$newobjects"
done

exit 0

# ldd ends here
--------------33CE9EA05AF2CC2D353AA745--

-
For help on using this list (especially unsubscribing), send a message to
"[email-masked]" with one line of text: "help".