find_global_symbol.sh
Go to the documentation of this file.
1 #! /bin/sh
2 
3 # Find global symbol.
4 
5 # Contact R. Hatcher
6 # N. West
7 
8 # Invocation:-
9 # ==========
10 
11 # find_global_symbol.sh options symbol. See help message below for details.
12 
13 
14 # Specification:-
15 # =============
16 
17 # o Search through all .so files in all directories defined
18 # in LD_LIBRARY path for supplied symbol.
19 
20 # o If located, attempt to demangle or mangle as appropriate..
21 
22 # Program Notes:-
23 # =============
24 
25 # None.
26 
27 symbol=""
28 all_libs=0
29 demangled=0
30 frag_search=0
31 print_types=0
32 verbose=0
33 textonly=1
34 print_help=0
35 for arg; do
36  case $arg in
37  -a) all_libs=1;;
38  -d) demangled=1;;
39  -f) frag_search=1;;
40  -t) print_types=1;;
41  -v) let verbose=${verbose}+1;;
42  -u) textonly=0;; # show U undefined as well (other than T W V )
43  -h) print_help=1;;
44  *) symbol="$arg"
45  esac
46 done
47 
48 sysname=`uname -s`
49 
50  if [ $print_types = 1 ] ; then
51  echo ' '
52  echo 'For Linux:'
53  echo ' Symbol types are a single character from the following list:-'
54  echo ' '
55  echo ' A '
56  echo ' The symbols value is absolute, and will not be changed by'
57  echo ' further linking.'
58  echo ' '
59  echo ' B '
60  echo ' The symbol is in the uninitialized data section (known as'
61  echo ' BSS).'
62  echo ' '
63  echo ' C '
64  echo ' The symbol is common. Common symbols are uninitialized data.'
65  echo ' When linking, multiple common symbols may appear with the'
66  echo ' same name. If the symbol is defined anywhere, the common'
67  echo ' symbols are treated as undefined references. For more'
68  echo ' details on common symbols, see the discussion of -warn-common'
69  echo ' in *Note Linker options: (ld.info)Options.'
70  echo ' details on common symbols, see the discussion of -warn-common'
71  echo ' in *Note Linker options: (ld.info)Options.'
72  echo ' '
73  echo ' D '
74  echo ' The symbol is in the initialized data section.'
75  echo ' '
76  echo ' G '
77  echo ' The symbol is in an initialized data section for small'
78  echo ' objects. Some object file formats permit more efficient'
79  echo ' access to small data objects, such as a global int variable'
80  echo ' as opposed to a large global array.'
81  echo ' '
82  echo ' I '
83  echo ' The symbol is an indirect reference to another symbol. This'
84  echo ' is a GNU extension to the a.out object file format which is'
85  echo ' rarely used.'
86  echo ' '
87  echo ' N '
88  echo ' The symbol is a debugging symbol.'
89  echo ' '
90  echo ' R '
91  echo ' The symbol is in a read only data section.'
92  echo ' '
93  echo ' S '
94  echo ' The symbol is in an uninitialized data section for small'
95  echo ' objects.'
96  echo ' '
97  echo ' T '
98  echo ' The symbol is in the text (code) section.'
99  echo ' '
100  echo ' U '
101  echo ' The symbol is undefined.'
102  echo ' '
103  echo ' V '
104  echo ' The symbol is a weak object. When a weak defined symbol is'
105  echo ' linked with a normal defined symbol, the normal defined'
106  echo ' symbol is used with no error. When a weak undefined symbol'
107  echo ' is linked and the symbol is not defined, the value of the'
108  echo ' weak symbol becomes zero with no error.'
109  echo ' '
110  echo ' W '
111  echo ' The symbol is a weak symbol that has not been specifically'
112  echo ' tagged as a weak object symbol. When a weak defined symbol'
113  echo ' is linked with a normal defined symbol, the normal defined'
114  echo ' symbol is used with no error. When a weak undefined symbol'
115  echo ' is linked and the symbol is not defined, the value of the'
116  echo ' weak symbol becomes zero with no error.'
117  echo ' '
118  echo ' - '
119  echo ' The symbol is a stabs symbol in an a.out object file. In'
120  echo ' this case, the next values printed are the stabs other field,'
121  echo ' the stabs desc field, and the stab type. Stabs symbols are'
122  echo ' used to hold debugging information. For more information,'
123  echo ' see *Note Stabs: (stabs.info)Top.'
124  echo ' '
125  echo ' ? '
126  echo ' The symbol type is unknown, or object file format specific.'
127  echo ' '
128  echo 'For Mac OS X:'
129  echo ' U (undefined)'
130  echo ' A (absolute)'
131  echo ' T (text section symbol)'
132  echo ' D (data section symbol)'
133  echo ' B (bss section symbol)'
134  echo ' C (common symbol)'
135  echo ' - (for debugger symbol table entries; see -a below)'
136  echo ' S (symbol in a section other than those above), or'
137  echo ' I (indirect symbol).'
138  echo " If the symbol is local (non-external), the symbol's type is instead"
139  echo ' represented by the corresponding lowercase letter. A lower case u'
140  echo ' in a dynamic shared library indicates a undefined reference to a'
141  echo ' private external in another module in the same library.'
142  exit 0
143 fi
144 
145 if [ "$symbol" = "" -o $print_help -ne 0 ] ; then
146  echo " find_global_symbol finds mangled or demangled symbols in libraries"
147  echo " within LD_LIBRARY_PATH. Invocation:-"
148  echo ""
149  echo " find_global_symbol.sh options name"
150  echo ""
151  echo " where options are none or more of the following:-"
152  echo ""
153  echo " -a All libs: search all libs."
154  echo " Default: exclude /usr/lib"
155  echo ""
156  echo " -d Demangled: Force name to be treated as demangled"
157  echo " Default: treat name as mangled unless it contains a ("
158  echo ""
159  echo " -f Fragment search: name is a fragment, match any symbol that"
160  echo " contains name."
161  echo " Default: symbol must exactly match name."
162  echo ""
163  echo " -u Print all references, including undefined symbols."
164  echo " Default: only print symbol types T, W or V"
165  echo ""
166  echo " -v Increase verbosity."
167  echo " -v prints each directory path as searched."
168  echo " -v -v for debugging."
169  echo ""
170  echo " -t Print list of symbol types (may not exactly match nm)"
171  echo ""
172  echo " -h Print this help message."
173  echo ""
174  exit 0;
175 fi
176 
177 # Escape puntuation characters.
178 search_string=`echo $symbol | awk \
179  '{ ORS=""; \
180  loc=1; \
181  while (loc <= length($0)) { \
182  c = substr($0,loc,1); \
183  if ( c < "0" || c == "[" || c == "]") print "\\\\"; \
184  print c; \
185  loc++; \
186  } \
187  } ' `
188 if [ $frag_search -eq 0 ] ; then search_string=" $search_string\$"; fi
189 
190 # Set demangle flag.
191 if [ $demangled -eq 0 ]; then
192  demangled=`echo $symbol | awk '{ print index($0,"(") }'`
193 fi
194 if [ $demangled -eq 0 ]; then demangled=""; fi
195 
196 if [ $sysname == "Darwin" ]; then
197  #DEMANGLEFLG="--defined-only -g"
198  DEMANGLEFLG="-g"
199 
200 else
201  DEMANGLEFLG=
202  NODEMANGLE=""
203 fi
204 
205 if [ $demangled ]
206 then
207  echo "Searching for demangled symbol '$symbol'"
208  case $sysname in
209  Darwin ) transform1="c++filt"
210  transform2="tr x x"
211  opt1="-g"
212  opt2="-g"
213  ;;
214  * ) transform1="tr x x" # no-op pass through
215  transform2="tr x x"
216  opt1="--demangle"
217  opt2=""
218  ;;
219  esac
220 else
221  #opt1=""
222  #opt2="${DEMANGLEFLG}"
223  echo "Searching for mangled symbol '$symbol'"
224  case $sysname in
225  Darwin ) transform1="tr x x"
226  transform2="c++filt"
227  opt1="-g"
228  opt2="-g"
229  ;;
230  * ) transform1="tr x x" # no-op pass through
231  transform2="tr x x"
232  opt1=""
233  opt2="--demangle"
234  ;;
235  esac
236 fi
237 
238 list=`echo ${LD_LIBRARY_PATH}:${DYLD_LIBRARY_PATH} | \
239  awk ' { num_elem = split($1,list,":"); \
240  i_elem = 1; \
241  while (i_elem <= num_elem) { \
242  print list[i_elem]; \
243  i_elem++; \
244  } \
245  } '`
246 
247 tried=":"
248 for path in $list
249 do
250 # Skip unwanted directories.
251  if [ "$path" = "." \
252  -o ! -d $path ]; then echo "Skipping $path"; continue; fi
253  if [ $all_libs -eq 0 -a "$path" = "/usr/lib" ]; then echo "Skipping $path"; continue; fi
254 
255 # Skip duplicates
256  tried_before=`echo $tried :$path: | awk ' { print index($1,$2) }'`
257  if [ $tried_before -ne 0 ]; then continue; fi
258  tried=$tried$path:
259 
260  cd $path
261  if [ $verbose -gt 0 ]; then
262  echo "Checking libraries in $path..."
263  fi
264 
265  for file in `ls -1 *.so *.dylib 2>/dev/null`
266  do
267  # echo "nm $opt1 $file | $tranform1 | egrep -n \"$search_string\""
268  resultmulti=`nm $opt1 $file | $transform1 | egrep -n "$search_string"`
269 
270  printpath=0
271  if [ "$resultmulti" != "" ]
272  then
273  if [ $verbose -gt 1 ]; then
274  echo "-->>> start resultmulti"
275  echo "$resultmulti"
276  echo "--<<< end resultmulti"
277  fi
278  while read -r result; do
279  if [ $textonly -ne 0 ]; then
280  # S = Mac OS X "other section" (where typeinfo might be found)
281  nmatch=`echo $result | egrep -c -v ' U ' `
282  if [ $nmatch -eq 0 ]; then continue; fi
283  fi
284  if [ $verbose -eq 0 -a $printpath -eq 0 ]; then
285  echo "Found in path $path/..."
286  printpath=1
287  fi
288 
289  echo " Found in $file"
290  echo " Entry: $result"
291  line=`echo $result | awk ' { loc = index($1,":");\
292  print substr($1,1,loc-1) }'`
293  if [ $verbose -gt 1 ]; then
294  echo "awk returned $line"
295  fi
296  result=`nm $opt2 $file | $transform2 | head -$line | tail -1`
297  echo " Translates to $result"
298  done <<< "$resultmulti"
299  fi
300  done
301 done
302 
303 echo ""
304 echo "Note that U <symbol> means the symbol is undefined (required) here"
305 echo " T, W, V <symbol> is defined here"
306 echo ""
307 echo "For a full list of codes type"
308 echo ""
309 echo " find_global_symbol.sh -h"
310 echo ""
311 
312 exit 0;
313