I've got a binary installed on my system, and would like to look at the disassembly of a given function. Preferrably using objdump
, but other solutions would be acceptable as well.
From this questions I've learned that I might be able to disassemble part of the code if I only know the boundary addresses. From this answer I've learned how to turn my split debug symbols back into a single file.
But even operating on that single file, and even disassembling all the code (i.e. without start or stop address, but plain -d
parameter to objdump
), I still don't see that symbol anywhere. Which makes sense insofar as the function in question is static, so it isn't exported. Nevertheless, valgrind
will report the function name, so it has to be stored somewhere.
Looking at the details of the debug sections, I find that name mentioned in the .debug_str
section, but I don't know a tool which can turn this into an address range.
I would suggest using gdb as the simplest approach. You can even do it as a one-liner, like:
Disassemble One Single Function using Objdump
I have two solutions:
1. Commandline Based
This method works perfectly and is also very short. I use objdump with -d option and pipe it to awk. The disassembled output looks like
A section or function is seperated by an empty line. Hence changing the FS (Field Seperator) to newline and the RS (Record Seperator) to twice newline let you easily search for your recommended function, since it is simply to find within the $1 field!
Of course you can replace main to any function you want to be output.
2. Bash Script
I have written a small bash script for this issue. Simply copy it and save it as e.g. dasm file.
Change the x-access and invoke it with e.g.:
This is much faster than invoking gdb with a script. Beside the way using objdump will not load the libraries into memory and is therefore safer!
Vitaly Fadeev programmed the autocompletion to this script, which is really a nice feature and speeds up typing.
The script can be found here.
To simplify the usage of awk for parsing objdump's output relative to other answers:
gdb
disassemble/rs
to show source and raw bytes as wellWith this format, it gets really close to
objdump -S
output:a.c:
Compile and disassemble
Disassembly:
Tested on Ubuntu 16.04, GDB 7.11.1.
objdump + awk workarounds
Print the paragraph as mentioned at: https://unix.stackexchange.com/questions/82944/how-to-grep-for-text-in-a-file-and-display-the-paragraph-that-has-the-text
e.g.:
gives just:
When using
-S
, I don't think there is a fail-proof way, as the code comments could contain any possible sequence... But the following works almost all the time:adapted from: How to select lines between two marker patterns which may occur multiple times with awk/sed
Mailing list replies
There is a 2010 thread on the mailing list which says it is not possible: https://sourceware.org/ml/binutils/2010-04/msg00445.html
Besides the
gdb
workaround proposed by Tom, they also comment on another (worse) workaround of compiling with-ffunction-section
which puts one function per section and then dumping the section.Nicolas Clifton gave it a WONTFIX https://sourceware.org/ml/binutils/2015-07/msg00004.html , likely because the GDB workaround covers that use case.
Bash completion for
./dasm
Complete symbol names to this solution (D lang version):
dasm test
and then pressing TabTab, you will get a list of all functions.dasm test m
and then pressing TabTab all functions starting with m will be shown, or in case only one function exists, it will be autocompleted.File
/etc/bash_completion.d/dasm
:This works just like the gdb solution (in that that it shifts the offsets towards zero) except that it's not laggy (gets the job done in about 5ms on my PC whereas the gdb solution takes about 150ms):
objdump_func: