need to output binary names to a file/stdout based on symbol table information
Question
I have a file which contains some binaries. I am trying to use nm
on these binaries to search for an object file.
My problem is, I want to output only those binaries from this file (executables.out) that contain the required symbols. Any idea how to do that?
nm --demangle `awk '{print}' executables.out` | grep <symbol> > outputfile.txt
Solution
Without lookinag at nm's args etc to see if there is something to show the filename, I'd do a 3 line shell script.
for f in `cat executables.out`; do
nm --demangle $f | grep -q REQUIRED_SYMBOL && echo $f
done
which says:
For each file in executables.out
if nm output contains the required symbol
echo the file name
It is possible that nm may have command line options to simplify (e.g. take the list of objs from a file) - depends on your OS.
OTHER TIPS
The options for nm
are not very consistent across machines, and neither are the output formats. However, there is usually an option that will prefix the file name (object file or archive member or executable name) to the output. On MacOS X, that option is -o
; on Solaris, it is -r
or -R
; on Linux, it is -o
, ...
nm -g -o $(<executables.out) | grep -w symbol | awk -F: '{print $1}'
This will list files the define or reference the given symbol. To show only files that define a symbol, then you need to look to 'T' (functions) or 'C' (uninitialized or common data) or 'D' (initialized data) — though System V systems use different systems again:
nm -g -o $(<executables.out) | grep -w symbol | grep ' [TCD] ' | awk -F: '{print $1}'
The $(<file)
notation reads the named file and uses the contents as a series of arguments, but does so without actually executing a command (unlike the notation with back-ticks that executed awk
in the original questions; cat
would have been a reasonable alternative to awk
).
The grep -w
looks for the pattern as a whole word, so if you search for printf
, it won't print fprintf
, snprintf
, vsnprintf
, etc. It is a GNU grep
extension.