#!/usr/bin/perl
#
# findsym 1.1 - Dave Olszewski (c) 1999-2001
#
#   This program will attempt to search through all your shared libraries
#   for a specific symbol.  This is useful when trying to compile something
#   and the compiler complains about an undefined reference similar to this:
#
#   /tmp/cceuy0nE.o(.text+0x7): undefined reference to `foo'
#
#   Running "findsym foo" would try to locate the symbol foo and indicate
#   what library you should be linking with.
#
#   This program is distributed under the GNU Public Licence.  See included
#   COPYING file for details
#

if($#ARGV != 0) {
  print "Usage: findsym <symbol>\n";
  exit;
}
open LDSOCONF,"/etc/ld.so.conf" or die "Couldnt open ld.so.conf";
chomp(@libdirs=<LDSOCONF>);
unshift @libdirs,"/lib","/usr/lib";
close LDSOCONF;
foreach $libdir(@libdirs) {
  opendir LIBDIR,$libdir;
  @libs=grep /\.so/,readdir LIBDIR;
  closedir LIBDIR;
  foreach $lib(@libs) {
    @sym=`nm -D $libdir/$lib 2>/dev/null`;
    foreach $line(@sym) {
      if($line=~/^([0-9a-f]){8} [^U] $ARGV[0]$/) {
        $located=1;
        print "$libdir/$lib: $line";
      }
    }
  }
}

if(!$located) {
  print STDERR "$0: The symbol $ARGV[0] was not found.\n";
}
