minor updates
[pkgusr] / usr / bin / library-symbol-search
1 #!/bin/bash
2
3 # Copyright (C) 2008 Steve Youngs
4
5 # Author:     Steve Youngs <steve@sxemacs.org>
6 # Maintainer: Steve Youngs <steve@sxemacs.org>
7 # Created:    <2008-03-10>
8 # Time-stamp: <Tuesday Apr  1, 2008 00:52:35 steve>
9 # Homepage:   N/A
10 # Keywords:   utils package-management
11
12 # This file is part of pkgusr.
13
14 # Redistribution and use in source and binary forms, with or without
15 # modification, are permitted provided that the following conditions
16 # are met:
17 #
18 # 1. Redistributions of source code must retain the above copyright
19 #    notice, this list of conditions and the following disclaimer.
20 #
21 # 2. Redistributions in binary form must reproduce the above copyright
22 #    notice, this list of conditions and the following disclaimer in the
23 #    documentation and/or other materials provided with the distribution.
24 #
25 # 3. Neither the name of the author nor the names of any contributors
26 #    may be used to endorse or promote products derived from this
27 #    software without specific prior written permission.
28 #
29 # THIS SOFTWARE IS PROVIDED BY THE AUTHOR "AS IS" AND ANY EXPRESS OR
30 # IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
31 # WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
32 # DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
33 # FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
34 # CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
35 # SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
36 # BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
37 # WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
38 # OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
39 # IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
40
41 ## Commentary:
42
43 #  This script is used to locate libraries that define a symbol
44 #  (function).  It comes in very handy when package builds fail with
45 #  "undefined reference..." errors.  By default, only defined syms
46 #  are searched for, but with -a option, all syms, both defined and
47 #  undefined are searched.
48 #
49 #  Uses objdump(1).
50
51 ## Code:
52 ourname=${0##*/}
53
54 # Usage
55 if [ $# -lt 1 -o "$1" = "--help" -o "$1" = "--usage" -o "$1" = "-h" ]; then
56         echo 1>&2
57         echo 1>&2 'USAGE:  ${ourname} [ -a ] <symbol_regexp>'
58         echo 1>&2
59         echo 1>&2 '  Find libraries that export symbol matching <symbol_regexp>.'
60         echo 1>&2 '  By default, only libraries that define the symbol are reported.'
61         echo 1>&2 '  With the '"'-a'"' option, libraries that have the symbol, but'
62         echo 1>&2 '  it is undefined, or a debugging symbol, are also listed.'
63         echo 1>&2
64         echo 1>&2 '  See grep(1), '"'REGULAR EXPRESSIONS'"' for the syntax of regexps'
65         echo 1>&2 '  used by this script.'
66         echo 1>&2
67         exit 1
68 fi
69
70 # Library directories.
71 # These are the directories we search.  If you have other lib directories
72 # such as /usr/local/lib, /opt/lib, add them here.
73 lib_dirs=(/usr/lib /usr/X11R6/lib /lib)
74
75 # bastard library directories...
76 #lib_dirs=(/usr/lib /usr/X11R6/lib /lib /opt/qt/lib)
77
78 if [ "$1" = "-a" ]; then
79     obj_opts="--demangle=gnu-v3 --reloc --syms --debugging"
80     want_all=1
81     shift
82 else
83     obj_opts="--demangle=gnu-v3 --reloc --syms"
84     want_all=0
85 fi
86
87
88 # $1 should now be the symbol (or symbol regexp) to search for
89 sym=${1}
90
91 all_libs=$(find ${lib_dirs[*]} -name lost+found -prune -o \( -name lib\*.so\* -o -name lib\*.a \) -print)
92
93 for lib in ${all_libs} ; do
94     unset good_file
95     good_file="$(file ${lib}|grep '\(shared object\|ar archive\)' 2>/dev/null)"
96     if [ -n "${good_file}" ]; then
97         if [ $want_all -eq 1 ]; then
98             objdump ${obj_opts} ${lib}|grep -q -E ${sym} && echo ${lib}
99         else
100             objdump ${obj_opts} ${lib}|grep -v UND|grep -q -E ${sym} && echo ${lib}
101         fi
102     fi
103 done
104
105 ## library-symbol-search ends here