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

后端 未结 9 1324
情歌与酒
情歌与酒 2020-11-28 02:57

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

9条回答
  •  Happy的楠姐
    2020-11-28 03:28

    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 
       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 
       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)

提交回复
热议问题