Initial git import
[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 #  First, it tries with nm(1), and if that gets nowhere (like in the
50 #  case of stripped libraries), fall back on objdump(1).
51
52 ## Code:
53 ourname=${0##*/}
54
55 # Usage
56 if [ $# -lt 1 -o "$1" = "--help" -o "$1" = "--usage" -o "$1" = "-h" ]; then
57         echo 1>&2
58         echo 1>&2 'USAGE:  ${ourname} [ -a ] <symbol_regexp>'
59         echo 1>&2
60         echo 1>&2 '  Find libraries that export symbol matching <symbol_regexp>.'
61         echo 1>&2 '  By default, only libraries that define the symbol are reported.'
62         echo 1>&2 '  With the '"'-a'"' option, libraries that have the symbol, but'
63         echo 1>&2 '  it is undefined, or a debugging symbol, are also listed.'
64         echo 1>&2
65         echo 1>&2 '  See grep(1), '"'REGULAR EXPRESSIONS'"' for the syntax of regexps'
66         echo 1>&2 '  used by this script.'
67         echo 1>&2
68         exit 1
69 fi
70
71 # Library directories.
72 # These are the directories we search.  If you have other lib directories
73 # such as /usr/local/lib, /opt/lib, add them here.
74 lib_dirs=(/usr/lib /usr/X11/lib /lib)
75
76 if [ "$1" = "-a" ]; then
77     nm_opts="--demangle=gnu-v3 --debug-syms"
78     shift
79 else
80     nm_opts="--demangle=gnu-v3 --defined-only --dynamic"
81 fi
82 obj_opts="--demangle=gnu-v3 --reloc --dynamic-reloc --syms --dynamic-syms"
83
84 # $1 should now be the symbol (or symbol regexp) to search for
85 sym=${1}
86
87 all_libs=$(find -H -L ${lib_dirs[*]} -type f \( -name "lib*.so" -o -name "lib*.a" \) -print)
88
89 for lib in ${all_libs} ; do
90     unset good_file lib_syms
91     good_file="$(file ${lib}|grep \(\(shared object\)\|\(ar archive\)\) 2>/dev/null)"
92     if [ -n "${good_file}" ]; then
93         lib_syms=$(nm ${nm_opts} ${lib} 2>/dev/null)
94         if [ -n "${lib_syms}" ]; then
95             echo ${lib_syms}|grep -E ${sym} 1>/dev/null && echo ${lib}
96         else
97             objdump ${obj_opts} ${lib} 2>/dev/null |
98                 grep -E ${sym} 1>/dev/null &&
99                 echo ${lib}
100         fi
101     fi
102 done
103
104 ## library-symbol-search ends here