Say for example I've got this C function:

void f(int *x, int *y)
    (*x) = (*x) * (*y);

When saved to f.c, compiling with gcc -c f.c produces f.o. objdump -d f.o gives this:

f.o:     file format elf64-x86-64

Disassembly of section .text:

0000000000000000 <f>:
   0:   55                      push   %rbp
   1:   48 89 e5                mov    %rsp,%rbp
   4:   48 89 7d f8             mov    %rdi,-0x8(%rbp)
   8:   48 89 75 f0             mov    %rsi,-0x10(%rbp)
   c:   48 8b 45 f8             mov    -0x8(%rbp),%rax
  10:   8b 10                   mov    (%rax),%edx
  12:   48 8b 45 f0             mov    -0x10(%rbp),%rax
  16:   8b 00                   mov    (%rax),%eax
  18:   0f af d0                imul   %eax,%edx
  1b:   48 8b 45 f8             mov    -0x8(%rbp),%rax
  1f:   89 10                   mov    %edx,(%rax)
  21:   5d                      pop    %rbp
  22:   c3                      retq  

I'd like it to output something more like this:

55 48 89 e5 48 89 7d f8 48 89 75 f0 48 8b 45 f8 8b 10 48 8b 45 f0 8b 00 0f af d0 48 8b 45 f8 89 10 5d c3

I.e., just the hexadecimal values of the function. Is there some objdump flag to do this? Otherwise, what tools can I use (e.g. awk, sed, cut, etc) to get this desired output?

  • objdump -s is somewhat like this. Or objcopy -O binary infile outfile to get a file without headers that you can then do a hex dump on. -j .text with either command to select the text section only. I wonder what you're really trying to do, and how "hex dump the function" became part of the solution – user41515 Feb 3 '18 at 0:41

You can extract the byte values in the text segment with:

$ objcopy -O binary -j .text f.o fo

The -O binary option:

objcopy can be used to generate a raw binary file by using an output target of binary (e.g., use -O binary). When objcopy generates a raw binary file, it will essentially produce a memory dump of the contents of the input object file. All symbols and relocation information will be discarded. The memory dump will start at the load address of the lowest section copied into the output file.

The -j .text option:

-j sectionpattern
Copy only the indicated sections from the input file to the output file. This option may be given more than once.
Note that using this option inappropriately may make the output file unusable. Wildcard characters are accepted in sectionpattern.

The end result is a file (fo) with the binary values of only the .text section, that is the executable code without symbols or relocation information.

And then print the hex values of the fo file:

$ od -An -t x1 fo
 55 48 89 e5 48 89 7d f8 48 89 75 f0 48 8b 45 f8
 8b 10 48 8b 45 f0 8b 00 0f af d0 48 8b 45 f8 89
 10 90 5d c3
|improve this answer|||||
  • Much better now, congrats. Cant give two up votes though... – Rui F Ribeiro Feb 3 '18 at 19:39

How about

awk '/^....:/{a=substr($0,9,20);sub(/ +$/,"",a);b=b" "a}END{print substr(b,2)}'

In this case, would return

55 48 89 e5 48 89 7d f8 48 89 75 f0 48 8b 45 f8 8b 10 48 8b 45 f0 8b 00 0f af d0 48 8b 45 f8 89 10 5d c3
|improve this answer|||||
  • What's the invocation for this command? I.e. where does the objdump go, standard in? – MD XF Feb 3 '18 at 0:48
  • Yes, standard in. objdump -d f.o | awk '/^....:/{a=substr($0,9,20);sub(/ +$/,"",a);b=b" "a}END{print substr(b,2)}' – steve Feb 3 '18 at 0:48

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.