Monday, March 10, 2014

Disassembling an executable via GNU GDB Debugger

We wrote a simple C program a while back. Now we load the executable into GNU debugger GDB and then use the disassemble command to get it's assembly language equivalent. We will disassemble the function main into assembly.

 main(int argc, char **argv)
{
/* Takes two user input from command line converts them into integer */
    int a = atoi(argv[1]);
    int b = atoi(argv[2]);
    int c;

    char buffer[100];
/* Gets user input */
    gets(buffer);
    puts(buffer);
    c = add(a,b);
    printf("Sum of %d + %d = %d\n",a,b,c);
    exit(0);
}


The command to use would be

$gdb disassemble <function_name> 

Given below is the assembly dump of function main.

(gdb) disassemble main
Dump of assembler code for function main:
   0x080484e9 <+0>:    push   %ebp
   0x080484ea <+1>:    mov    %esp,%ebp
   0x080484ec <+3>:    and    $0xfffffff0,%esp
   0x080484ef <+6>:    sub    $0xa0,%esp
   0x080484f5 <+12>:    mov    0xc(%ebp),%eax
   0x080484f8 <+15>:    mov    %eax,0x1c(%esp)
   0x080484fc <+19>:    mov    %gs:0x14,%eax
   0x08048502 <+25>:    mov    %eax,0x9c(%esp)
   0x08048509 <+32>:    xor    %eax,%eax
   0x0804850b <+34>:    mov    0x1c(%esp),%eax
   0x0804850f <+38>:    add    $0x4,%eax
   0x08048512 <+41>:    mov    (%eax),%eax
   0x08048514 <+43>:    mov    %eax,(%esp)
   0x08048517 <+46>:    call   0x80483d0 <atoi@plt>
   0x0804851c <+51>:    mov    %eax,0x2c(%esp)
   0x08048520 <+55>:    mov    0x1c(%esp),%eax
   0x08048524 <+59>:    add    $0x8,%eax
   0x08048527 <+62>:    mov    (%eax),%eax
   0x08048529 <+64>:    mov    %eax,(%esp)
   0x0804852c <+67>:    call   0x80483d0 <atoi@plt>
   0x08048531 <+72>:    mov    %eax,0x30(%esp)
   0x08048535 <+76>:    lea    0x38(%esp),%eax
   0x08048539 <+80>:    mov    %eax,(%esp)
   0x0804853c <+83>:    call   0x8048380 <gets@plt>
   0x08048541 <+88>:    lea    0x38(%esp),%eax
   0x08048545 <+92>:    mov    %eax,(%esp)
   0x08048548 <+95>:    call   0x8048390 <puts@plt>
   0x0804854d <+100>:    mov    0x30(%esp),%eax
   0x08048551 <+104>:    mov    %eax,0x4(%esp)
   0x08048555 <+108>:    mov    0x2c(%esp),%eax
   0x08048559 <+112>:    mov    %eax,(%esp)
   0x0804855c <+115>:    call   0x80484cc <add>
   0x08048561 <+120>:    mov    %eax,0x34(%esp)
   0x08048565 <+124>:    mov    0x34(%esp),%eax
   0x08048569 <+128>:    mov    %eax,0xc(%esp)
   0x0804856d <+132>:    mov    0x30(%esp),%eax
   0x08048571 <+136>:    mov    %eax,0x8(%esp)
   0x08048575 <+140>:    mov    0x2c(%esp),%eax
   0x08048579 <+144>:    mov    %eax,0x4(%esp)
   0x0804857d <+148>:    movl   $0x8048638,(%esp)
   0x08048584 <+155>:    call   0x8048370 <printf@plt>
   0x08048589 <+160>:    movl   $0x0,(%esp)
   0x08048590 <+167>:    call   0x80483b0 <exit@plt>
End of assembler dump.

And we can disassemble the function add also into assembly language.



int add(int x, int y)
{
/* Adder function*/
    int z =10;
    z = x + y;
    return z;
}



(gdb) disassemble add
Dump of assembler code for function add:
   0x080484cc <+0>:    push   %ebp
   0x080484cd <+1>:    mov    %esp,%ebp
   0x080484cf <+3>:    sub    $0x10,%esp
   0x080484d2 <+6>:    movl   $0xa,-0x4(%ebp)
   0x080484d9 <+13>:    mov    0xc(%ebp),%eax
   0x080484dc <+16>:    mov    0x8(%ebp),%edx
   0x080484df <+19>:    add    %edx,%eax
   0x080484e1 <+21>:    mov    %eax,-0x4(%ebp)
   0x080484e4 <+24>:    mov    -0x4(%ebp),%eax
   0x080484e7 <+27>:    leave 
   0x080484e8 <+28>:    ret   
End of assembler dump.
(gdb)

No comments:

Post a Comment