【问题标题】:Absolute call using register and offset from table使用寄存器和表中的偏移量进行绝对调用
【发布时间】:2011-07-16 11:53:56
【问题描述】:

为了练习偏移、寻址、表格等,我在 NASM 中编写了以下程序。

t_addr:
        dw      rout1-@, rout2-@

@       equ     $
_start:
        mov     esi, rout1
        call    esi

        call    _start_reloc
_start_reloc:
        pop     ebp
        sub     ebp, _start_reloc-@

        xor     eax, eax
        add     eax, 1
        sal     eax, 1

        lea     esi, [ebp+t_addr-@]
        mov     ax, word [esi+eax]
        add     eax, ebp
        call    eax
        ret

rout1:
        mov     eax, 0
        ret

rout2:
        xor     eax, eax
        ret

虽然 _start 标签后的前两条指令正常运行并将控制权转移到 rout1 函数,但当我尝试访问 rout2函数使用表中的偏移量,而在 GDB 中,我在 call eax 指令之前查看 eax 的值并包含rout2 执行调用时出现分段错误,并且 EIP 加载了 0x00000001。为什么???

ps:我使用的是 linux 32 位。

【问题讨论】:

    标签: assembly x86 nasm


    【解决方案1】:

    我看到的第一个问题是,当您输入 _start_reloc 时,您会弹出 ebp。当该函数结束并且您 ret 时,eip 获取堆栈上的值。通常那将是 ebp,但由于您现在将其弹出,eip 具有随机值。而不是 pop ebp 尝试使用 mov ebp,[esp] 或 pop ebp 然后 push ebp

    【讨论】:

    • 谢谢!您的回答很有帮助,现在我了解 GDB 中 nextistepi 之间的区别:)
    • @user466825:不客气! :) 电脑有时很烦人.. :P
    猜你喜欢
    • 2018-04-03
    • 2016-11-11
    • 1970-01-01
    • 2017-03-05
    • 2013-12-06
    • 1970-01-01
    • 2014-09-28
    • 2019-02-15
    相关资源
    最近更新 更多