4

After a program is compiled and the binary file is generated, we can use objdump to disassemble the binary file and extract the assembly code and a lot of information.

However, using -j .text with objdump, it will disassembly the whole functions (glibc, OS functions, etc ... ) that I do not want.

I want to focus only on my own functions in the binary file. Using nm, it is possible to find only the user defined functions. After extracting the name of these functions, I want to disassembly only these functions. However, I do not want to search in the hug dump file that objdump generates it and extract disassembly code that related to my functions.

Assuming we have the binary file for basicmath program from MiBench benchmark.

using nm, it is possible to find only the functions that are defined in the source code of this program.

The command below will show the functions that I want (the user-defined functions)

nm -P tst.o | awk '$2 == "T" && $1 != "main" {print "b " $1}'

The result will be (considering basicmath program)

b deg2rad
b rad2deg
b solveCubic
b usqrt

Now, I need a way to tell objdump to disassembly only these functions and write the result to a single file.

2
  • Please provide a direct link to basicmath Jul 14, 2020 at 11:19
  • it is not matter what a binary file we use, I just need a way to disassemble user-defined functions (not all the binary file) Jul 14, 2020 at 11:29

1 Answer 1

2

I don't see a way to tell the objdump program from binutils or elfutils to limit disassembly to specific functions. There are a couple of workarounds, though.

Assume the list of functions we're interested in is in file list:

$ nm -P basicmath_small |
    awk '{ if ($2 == "T" && $1 != "main" && substr($1,1,1) != "_") print $1}' > list
$ cat list
deg2rad
rad2deg
SolveCubic
usqrt
  • use awk to filter the big disassembly output from objdump. Each region of interest starts with a line that ends with <functionname>: and continues until an empty line (or end-of-file).

$ xargs < list | sed -e 's/^/<(/' -e 's/ /|/g' -e 's/$/)>:\$/' > rlist
$ cat rlist
<(deg2rad|rad2deg|SolveCubic|usqrt)>:$
$ objdump -d -j .text basicmath_small | awk -v rlist="$(cat rlist)" \
    '{ if ($0 ~ rlist) doprint=1; if ($0 == "") doprint=0; if (doprint) print }'

0000000000400fc0 <rad2deg>:
  400fc0:       f2 0f 59 05 c8 4d 0a    mulsd  0xa4dc8(%rip),%xmm0        # 4a5d90 <c2+0x10>
  400fc7:       00
  400fc8:       f2 0f 5e 05 b8 4d 0a    divsd  0xa4db8(%rip),%xmm0        # 4a5d88 <c2+0x8>
  400fcf:       00
  400fd0:       c3                      retq
  400fd1:       0f 1f 44 00 00          nopl   0x0(%rax,%rax,1)
  400fd6:       66 2e 0f 1f 84 00 00    nopw   %cs:0x0(%rax,%rax,1)
  400fdd:       00 00 00
0000000000400fe0 <deg2rad>:
  400fe0:       f2 0f 59 05 a0 4d 0a    mulsd  0xa4da0(%rip),%xmm0        # 4a5d88 <c2+0x8>
  400fe7:       00
  400fe8:       f2 0f 5e 05 a0 4d 0a    divsd  0xa4da0(%rip),%xmm0        # 4a5d90 <c2+0x10>
  400fef:       00
  400ff0:       c3                      retq
  400ff1:       66 2e 0f 1f 84 00 00    nopw   %cs:0x0(%rax,%rax,1)
  400ff8:       00 00 00
  400ffb:       0f 1f 44 00 00          nopl   0x0(%rax,%rax,1)
0000000000401000 <SolveCubic>:
  401000:       f2 0f 5e c8             divsd  %xmm0,%xmm1
...

$ xargs < list | sed -e 's/^/^(/' -e 's/ /|/g' -e 's/$/)\$/' > rlist
$ cat rlist
^(deg2rad|rad2deg|SolveCubic|usqrt)$
$ go tool objdump -s "$(cat rlist)" basicmath_small

TEXT rad2deg(SB)
  :0                    0x400fc0                f20f5905c84d0a00        MULSD 0xa4dc8(IP), X0
  :0                    0x400fc8                f20f5e05b84d0a00        DIVSD 0xa4db8(IP), X0
  :0                    0x400fd0                c3                      RET

TEXT deg2rad(SB)
  :0                    0x400fe0                f20f5905a04d0a00        MULSD 0xa4da0(IP), X0
  :0                    0x400fe8                f20f5e05a04d0a00        DIVSD 0xa4da0(IP), X0
  :0                    0x400ff0                c3                      RET

TEXT SolveCubic(SB)
  :0                    0x401000                f20f5ec8                DIVSD X0, X1
...

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .