Get hex-only output from objdump

10,101

Solution 1

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
--only-section=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

Solution 2

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
Share:
10,101

Related videos on Youtube

MD XF
Author by

MD XF

inactive Languages I've created, in reverse order of how much I hate them: Cubically (docs, community, tag, chatroom, online interpreter) Triangular (online interpreter, wiki) Implicit (online interpreter) shortC (online interpreter) Bitwise (online interpreter, wiki) Forked (online interpreter, wiki) MachineCode (online interpreter) rk-lang (online interpreter) Decimal (online interpreter, wiki) (ಠ_ಠ) Memescript (wiki) (ಠ_ಠ_ಠ_ಠ) Some interesting stuff I've written: Try-it-online language brute-force script Some of my best answers: Golf you a quine for great good!         language-c votes-19 Is it a Rubik's Cube?                           language-cubically votes-10 404 - Unique 404 page not found       language-polyglot votes-13 Polyglot the OEIS!                              language-polyglot votes-38 Plot the Gaussian distribution in 3D   language-basic votes-6 Some of my best challenges: Design a One Instruction Set Computer!

Updated on September 18, 2022

Comments

  • MD XF
    MD XF almost 2 years

    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?

    • Admin
      Admin over 6 years
      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
  • Rui F Ribeiro
    Rui F Ribeiro over 6 years
    Much better now, congrats. Cant give two up votes though...