zoukankan      html  css  js  c++  java
  • 20145208 GDB调试汇编堆栈过程分析

    20145208 GDB调试汇编堆栈过程分析

    测试代码

    #include<stdio.h>
    
    short addend1 = 1;
    static int addend2 = 2;
    const static long addend3 = 3;
    
    static int g(int x)
    {
    	return x + addend1;
    }  
    
    static const int f(int x)
    {
    	return g(x + addend2);
    }
    
    int main(void)
    {
    	return f(8) + addend3;
    }
    

    分析过程

    • 使用gcc -g example.c -o example -m32指令在64位的机器上产生32位汇编,然后使用gdb example指令进入gdb调试器

    • 进入之后先在main函数处设置一个断点,再run一下,使用disassemble指令获取汇编代码,用i(info) r(registers)指令查看各寄存器的值:

    • 可见此时主函数的栈基址为 0xffffcf98,用x(examine)指令查看内存地址中的值,但目前%esp所指堆栈内容为0,%ebp所指内容也为0

    • 首先,结合display命令和寄存器或pc内部变量,做如下设置:display /i $pc,这样在每次执行下一条汇编语句时,都会显示出当前执行的语句。下面展示每一步时%esp%ebp和堆栈内容的变化:

    • call指令将下一条指令的地址入栈,此时%esp,%ebp和堆栈的值为:

    • 将上一个函数的基址入栈,从当前%esp开始作为新基址:

    • 先为传参做准备:

    • 实参的计算在%eax中进行:

    • f函数的汇编代码:

    • 实参入栈:

    • call指令将下一条指令的地址入栈:

    • 计算short+int:

    • pop %ebp指令将栈顶弹到%ebp中,同时%esp增加4字节:

    • ret指令将栈顶弹给%eip

    • 因为函数f修改了%esp,所以用leave指令恢复。leave指令先将%esp对其到%ebp,然后把栈顶弹给%ebp

    • 主函数汇编代码:

    指令 %esp %ebp %eip %eax 堆栈
    push $0x8 0xffffcf98 0xffffcf98 0x804840b -134500932 0x0
    call 0x80483ef 0xffffcf94 0xffffcf98 0x804840b -134500932 0x8 0x0
    push %ebp 0xffffcf90 0xffffcf98 0x80483ef -134500932 0x8048412 0x8 0x0
    mov %esp,%ebp 0xffffcf8c 0xffffcf98 0x80483f0 -134500932 0xffffcf98 0x8048412 0x8 0x0
    mov 0x804a01c,%edx 0xffffcf8c 0xffffcf8c 0x80483f2 -134500932 0xffffcf98 0x8048412 0x8 0x0
    mov 0x8(%ebp),%eax 0xffffcf8c 0xffffcf8c 0x80483f8 -134500932 0xffffcf98 0x8048412 0x8 0x0
    add %edx,%eax 0xffffcf8c 0xffffcf8c 0x80483fb 8 0xffffcf98 0x8048412 0x8 0x0
    push %eax 0xffffcf8c 0xffffcf8c 0x80483fd 10 0xffffcf98 0x8048412 0x8 0x0
    call 0x80483db 0xffffcf88 0xffffcf8c 0x80483fe 10 0xa 0xffffcf98 0x8048412 0x8 0x0
    push %ebp 0xffffcf84 0xffffcf8c 0x80483db 10 0x8048403 0xa 0xffffcf98 0x8048412 0x8 0x0
    mov %esp,%ebp 0xffffcf80 0xffffcf8c 0x80483dc 10 0xffffcf8c 0x8048403 0xa 0xffffcf98 0x8048412 0x8 0x0
    movzwl 0x804a018,%eax 0xffffcf80 0xffffcf80 0x80483de 10 0xffffcf8c 0x8048403 0xa 0xffffcf98 0x8048412 0x8 0x0
    movswl %ax,%edx 0xffffcf80 0xffffcf80 0x80483e5 1 0xffffcf8c 0x8048403 0xa 0xffffcf98 0x8048412 0x8 0x0
    mov 0x8(%ebp),%eax 0xffffcf80 0xffffcf80 0x80483e8 1 0xffffcf8c 0x8048403 0xa 0xffffcf98 0x8048412 0x8 0x0
    add %edx,%eax 0xffffcf80 0xffffcf80 0x80483eb 10 0xffffcf8c 0x8048403 0xa 0xffffcf98 0x8048412 0x8 0x0
    pop %ebp 0xffffcf80 0xffffcf80 0x80483ed 11 0xffffcf8c 0x8048403 0xa 0xffffcf98 0x8048412 0x8 0x0
    ret 0xffffcf84 0xffffcf8c 0x80483ee 11 0x8048403 0xa 0xffffcf98 0x8048412 0x8 0x0
    add $0x4,%esp 0xffffcf88 0xffffcf8c 0x8048403 11 0x8048403 0xa 0xffffcf98 0x8048412 0x8 0x0
    leave 0xffffcf8c 0xffffcf8c 0x8048406 11 0xffffcf98 0x8048412 0x8 0x0
    ret 0xffffcf90 0xffffcf98 0x8048407 11 0x8048412 0x8 0x0
    add $0x4,%esp 0xffffcf94 0xffffcf98 0x8048412 11 0x8 0x0
    mov $0x3,%edx 0xffffcf98 0xffffcf98 0x8048415 11 0x0
    add %edx,%eax 0xffffcf98 0xffffcf98 0x804841a 11 0x0
    leave 0xffffcf98 0xffffcf98 0x804841c 14 0x0
    ret 0xffffcf9c 0x0 0x804841d 14
  • 相关阅读:
    Redis到底该如何利用?
    AutoMapper搬运工之自定义类型转换
    AutoMapper搬运工之初探AutoMapper
    【ELK】docker-compose搭建ELK单机环境
    [flowable工作流引擎]基本概念及术语
    shell遍历文件夹读取文件夹下的文件
    vector类的简单实现
    string类的实现
    接口安全认证
    C# lock private VS private static
  • 原文地址:https://www.cnblogs.com/20145208cy/p/6119358.html
Copyright © 2011-2022 走看看