Gdb跳过一些汇编代码部分

4

我在汇编级别上调试程序时遇到了一些困难,因为GDB跳过了代码的某些部分。以下是代码:

#include <stdio.h>
#define BUF_SIZE 8

void getInput(){
    char buf[BUF_SIZE];
    gets(buf);
    puts(buf);
}

int main(int argc, char* argv){
    printf("Digite alguma coisa, tamanho do buffer eh: %d\n", BUF_SIZE);

    getInput();
    return 0;
}

该程序是使用以下命令进行编译的:gcc -ggdb -fno-stack-protector -mpreferred-stack-boundary=4 -o exploit1 exploit1.c 在gdb中,我添加了break getInput,当我运行disas getInput时,它返回给我:
Dump of assembler code for function getInput:
0x00000000004005cc <+0>:    push   %rbp
0x00000000004005cd <+1>:    mov    %rsp,%rbp
0x00000000004005d0 <+4>:    sub    $0x10,%rsp
0x00000000004005d4 <+8>:    lea    -0x10(%rbp),%rax
0x00000000004005d8 <+12>:   mov    %rax,%rdi
0x00000000004005db <+15>:   mov    $0x0,%eax
0x00000000004005e0 <+20>:   callq  0x4004a0 <gets@plt>
0x00000000004005e5 <+25>:   lea    -0x10(%rbp),%rax
0x00000000004005e9 <+29>:   mov    %rax,%rdi
0x00000000004005ec <+32>:   callq  0x400470 <puts@plt>
0x00000000004005f1 <+37>:   nop
0x00000000004005f2 <+38>:   leaveq 
0x00000000004005f3 <+39>:   retq  

如果我输入run,我注意到程序停在行0x00000000004005d4而不是我预期的函数0x00000000004005cc的第一行。为什么会这样呢?
顺便说一下,这让我困扰,因为我注意到一些额外的数据被添加到堆栈中,我想逐步查看堆栈增长的步骤。

2
gdb试图提供帮助并跳过函数序言。使用b *getInput在第一条指令处设置断点。 - Jester
谢谢。我还注意到以下内容按照这个顺序被添加到堆栈中(x/8xw $rsp): 0x0(???)0x00400621(返回值,正确),0x00007fff(???),0xffffdec0(rbp的内容,正确),0x00007fff(???),0xf7ffe168(???),0x0 0x0(ok,这是buf变量)。为什么会添加这些额外的数据?这是一种防止缓冲区溢出的填充数据吗? - Fnr
这是用于对齐的填充。 - Jester
我不明白,对齐什么?我需要开一个新的问题来解决吗? - Fnr
1
x86-64 SysV ABI 规定 rsp 必须是 16 字节对齐的。 - Jester
显示剩余2条评论
1个回答

6
如果我输入run,我会发现程序停在行0x00000000004005d4而不是我预期的函数0x00000000004005cc的第一行。
你的预期是不正确的。
为什么会这样?
因为当你通过break getInput设置断点时,GDB在函数启动代码之后设置了断点。根据文档描述:
-function function
  The value specifies the name of a function. Operations on function locations
  unmodified by other options (such as -label or -line) refer to the line that
  begins the body of the function. In C, for example, this is the line with the
  open brace. 

如果您想在第一条指令上设置断点,请使用break *getInput。文档这里这里

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接