Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to disassemble a binary executable in Linux to get the assembly code?

I was told to use a disassembler. Does gcc have anything built in? What is the easiest way to do this?

like image 660
Syntax_Error Avatar asked Feb 26 '11 08:02

Syntax_Error


People also ask

Which command is used to disassemble code?

The objdump command is generally used to inspect the object files and binary files. It prints the different sections in object files, their virtual memory address, logical memory address, debug information, symbol table, and other pieces of information. Here we'll see how we can use this tool to disassemble the files.

How do you take apart a ELF file?

Disassembling an ELF-formatted fileUse the --disassemble option to display a disassembled version of the image to stdout . If you use this option with the --output destination option, you can reassemble the output file with armasm. You can use this option to disassemble either an ELF image or an ELF object file.

What is code disassembly?

In programming terminology, to disassemble is to convert a program in its executable (ready-to-run) form (sometimes called object code ) into a representation in some form of assembler language so that it is readable by a human.


2 Answers

I don't think gcc has a flag for it, since it's primarily a compiler, but another of the GNU development tools does. objdump takes a -d/--disassemble flag:

$ objdump -d /path/to/binary 

The disassembly looks like this:

080483b4 <main>:  80483b4:   8d 4c 24 04             lea    0x4(%esp),%ecx  80483b8:   83 e4 f0                and    $0xfffffff0,%esp  80483bb:   ff 71 fc                pushl  -0x4(%ecx)  80483be:   55                      push   %ebp  80483bf:   89 e5                   mov    %esp,%ebp  80483c1:   51                      push   %ecx  80483c2:   b8 00 00 00 00          mov    $0x0,%eax  80483c7:   59                      pop    %ecx  80483c8:   5d                      pop    %ebp  80483c9:   8d 61 fc                lea    -0x4(%ecx),%esp  80483cc:   c3                      ret      80483cd:   90                      nop  80483ce:   90                      nop  80483cf:   90                      nop 
like image 152
Michael Mrozek Avatar answered Sep 21 '22 06:09

Michael Mrozek


An interesting alternative to objdump is gdb. You don't have to run the binary or have debuginfo.

$ gdb -q ./a.out  Reading symbols from ./a.out...(no debugging symbols found)...done. (gdb) info functions  All defined functions:  Non-debugging symbols: 0x00000000004003a8  _init 0x00000000004003e0  __libc_start_main@plt 0x00000000004003f0  __gmon_start__@plt 0x0000000000400400  _start 0x0000000000400430  deregister_tm_clones 0x0000000000400460  register_tm_clones 0x00000000004004a0  __do_global_dtors_aux 0x00000000004004c0  frame_dummy 0x00000000004004f0  fce 0x00000000004004fb  main 0x0000000000400510  __libc_csu_init 0x0000000000400580  __libc_csu_fini 0x0000000000400584  _fini (gdb) disassemble main Dump of assembler code for function main:    0x00000000004004fb <+0>:     push   %rbp    0x00000000004004fc <+1>:     mov    %rsp,%rbp    0x00000000004004ff <+4>:     sub    $0x10,%rsp    0x0000000000400503 <+8>:     callq  0x4004f0 <fce>    0x0000000000400508 <+13>:    mov    %eax,-0x4(%rbp)    0x000000000040050b <+16>:    mov    -0x4(%rbp),%eax    0x000000000040050e <+19>:    leaveq     0x000000000040050f <+20>:    retq    End of assembler dump. (gdb) disassemble fce Dump of assembler code for function fce:    0x00000000004004f0 <+0>:     push   %rbp    0x00000000004004f1 <+1>:     mov    %rsp,%rbp    0x00000000004004f4 <+4>:     mov    $0x2a,%eax    0x00000000004004f9 <+9>:     pop    %rbp    0x00000000004004fa <+10>:    retq    End of assembler dump. (gdb) 

With full debugging info it's even better.

(gdb) disassemble /m main Dump of assembler code for function main: 9       {    0x00000000004004fb <+0>:     push   %rbp    0x00000000004004fc <+1>:     mov    %rsp,%rbp    0x00000000004004ff <+4>:     sub    $0x10,%rsp  10        int x = fce ();    0x0000000000400503 <+8>:     callq  0x4004f0 <fce>    0x0000000000400508 <+13>:    mov    %eax,-0x4(%rbp)  11        return x;    0x000000000040050b <+16>:    mov    -0x4(%rbp),%eax  12      }    0x000000000040050e <+19>:    leaveq     0x000000000040050f <+20>:    retq     End of assembler dump. (gdb) 

objdump has a similar option (-S)

like image 45
Miroslav Franc Avatar answered Sep 21 '22 06:09

Miroslav Franc