我可以通过拆卸来了解地址吗?

时间:2014-07-14 17:43:34

标签: c gcc disassembly

我使用gcc。我想知道每一行代码的地址。有没有办法通过拆卸获得它?第一列拆解输出似乎对我说,但我不确定。

#include <stdio.h>
#include <string.h>

int main()
{
    char name[20],address[500];
    printf("enter your name::: \n");
    fgets(name, sizeof(name), stdin);
    printf("name you entered is:: %s \n", name);
    printf("enter your name::: \n");
    fgets(address, sizeof(address), stdin);
    printf("address you entered is:: %s \n", address);
}

对于上面的例子,我想知道每一行的地址。

1 个答案:

答案 0 :(得分:1)

您可以使用gdb这样的(我的gdb中的main()函数)来查看dissasembly输出:

(gdb) disas /m main
Dump of assembler code for function main:
5   {
   0x000000000040066d <+0>: push   %rbp
   0x000000000040066e <+1>: mov    %rsp,%rbp
   0x0000000000400671 <+4>: sub    $0x220,%rsp
   0x0000000000400678 <+11>:    mov    %fs:0x28,%rax
   0x0000000000400681 <+20>:    mov    %rax,-0x8(%rbp)
   0x0000000000400685 <+24>:    xor    %eax,%eax

6       char name[20],address[500];
7       printf("enter your name::: \n");
   0x0000000000400687 <+26>:    mov    $0x4007a4,%edi
   0x000000000040068c <+31>:    callq  0x400520 <puts@plt>

8       fgets(name, sizeof(name), stdin);
   0x0000000000400691 <+36>:    mov    0x2009c0(%rip),%rdx        # 0x601058 <stdin@@GLIBC_2.2.5>
   0x0000000000400698 <+43>:    lea    -0x220(%rbp),%rax
   0x000000000040069f <+50>:    mov    $0x14,%esi
   0x00000000004006a4 <+55>:    mov    %rax,%rdi
   0x00000000004006a7 <+58>:    callq  0x400560 <fgets@plt>

9       printf("name you entered is:: %s \n", name);
   0x00000000004006ac <+63>:    lea    -0x220(%rbp),%rax
   0x00000000004006b3 <+70>:    mov    %rax,%rsi
   0x00000000004006b6 <+73>:    mov    $0x4007b8,%edi
   0x00000000004006bb <+78>:    mov    $0x0,%eax
   0x00000000004006c0 <+83>:    callq  0x400540 <printf@plt>

10      printf("enter your name::: \n");
   0x00000000004006c5 <+88>:    mov    $0x4007a4,%edi
   0x00000000004006ca <+93>:    callq  0x400520 <puts@plt>

11      fgets(address, sizeof(address), stdin);
   0x00000000004006cf <+98>:    mov    0x200982(%rip),%rdx        # 0x601058 <stdin@@GLIBC_2.2.5>
   0x00000000004006d6 <+105>:   lea    -0x200(%rbp),%rax
   0x00000000004006dd <+112>:   mov    $0x1f4,%esi
   0x00000000004006e2 <+117>:   mov    %rax,%rdi
   0x00000000004006e5 <+120>:   callq  0x400560 <fgets@plt>

12      printf("address you entered is:: %s \n", address);
   0x00000000004006ea <+125>:   lea    -0x200(%rbp),%rax
   0x00000000004006f1 <+132>:   mov    %rax,%rsi
   0x00000000004006f4 <+135>:   mov    $0x4007d3,%edi
   0x00000000004006f9 <+140>:   mov    $0x0,%eax
   0x00000000004006fe <+145>:   callq  0x400540 <printf@plt>

13  }
   0x0000000000400703 <+150>:   mov    -0x8(%rbp),%rcx
   0x0000000000400707 <+154>:   xor    %fs:0x28,%rcx
   0x0000000000400710 <+163>:   je     0x400717 <main+170>
   0x0000000000400712 <+165>:   callq  0x400530 <__stack_chk_fail@plt>
   0x0000000000400717 <+170>:   leaveq 
   0x0000000000400718 <+171>:   retq   

End of assembler dump.

或使用像objdump这样的反汇编实用程序(test是我编译程序产生的可执行文件):

$ objdump -S test
int main()
{
  40066d:       55                      push   %rbp
  40066e:       48 89 e5                mov    %rsp,%rbp
  400671:       48 81 ec 20 02 00 00    sub    $0x220,%rsp
  400678:       64 48 8b 04 25 28 00    mov    %fs:0x28,%rax
  40067f:       00 00 
  400681:       48 89 45 f8             mov    %rax,-0x8(%rbp)
  400685:       31 c0                   xor    %eax,%eax
    char name[20],address[500];
    printf("enter your name::: \n");
  400687:       bf a4 07 40 00          mov    $0x4007a4,%edi
  40068c:       e8 8f fe ff ff          callq  400520 <puts@plt>
    fgets(name, sizeof(name), stdin);
  400691:       48 8b 15 c0 09 20 00    mov    0x2009c0(%rip),%rdx        # 601058   <__TMC_END__>
  400698:       48 8d 85 e0 fd ff ff    lea    -0x220(%rbp),%rax
  40069f:       be 14 00 00 00          mov    $0x14,%esi
  4006a4:       48 89 c7                mov    %rax,%rdi
  4006a7:       e8 b4 fe ff ff          callq  400560 <fgets@plt>
    printf("name you entered is:: %s \n", name);
  4006ac:       48 8d 85 e0 fd ff ff    lea    -0x220(%rbp),%rax
  4006b3:       48 89 c6                mov    %rax,%rsi
  4006b6:       bf b8 07 40 00          mov    $0x4007b8,%edi
  4006bb:       b8 00 00 00 00          mov    $0x0,%eax
  4006c0:       e8 7b fe ff ff          callq  400540 <printf@plt>
    printf("enter your name::: \n");
  4006c5:       bf a4 07 40 00          mov    $0x4007a4,%edi
  4006ca:       e8 51 fe ff ff          callq  400520 <puts@plt>
    fgets(address, sizeof(address), stdin);
  4006cf:       48 8b 15 82 09 20 00    mov    0x200982(%rip),%rdx        # 601058 <__TMC_END__>
  4006d6:       48 8d 85 00 fe ff ff    lea    -0x200(%rbp),%rax
  4006dd:       be f4 01 00 00          mov    $0x1f4,%esi
  4006e2:       48 89 c7                mov    %rax,%rdi
  4006e5:       e8 76 fe ff ff          callq  400560 <fgets@plt>
    printf("address you entered is:: %s \n", address);
  4006ea:       48 8d 85 00 fe ff ff    lea    -0x200(%rbp),%rax
  4006f1:       48 89 c6                mov    %rax,%rsi
  4006f4:       bf d3 07 40 00          mov    $0x4007d3,%edi
  4006f9:       b8 00 00 00 00          mov    $0x0,%eax
  4006fe:       e8 3d fe ff ff          callq  400540 <printf@plt>
}

两者的输出只是解释性的。我把它包裹起来因为它非常广泛......