fve*_*rtk 3 x86 assembly stack trace
我正在查看我的一门课程的模拟考试,我只是不明白问题的某些方面,所以也许你可以帮助我(如果你了解 x86,可能真的很容易)。
所以这是问题8: http ://www.coe.utah.edu/~cs4400/schedule/exam3.F10.pdf
解决方案在这里: http://www.coe.utah.edu/~cs4400/schedule/exam3_solns.F10.pdf \
我只是不明白解决方案中的值是如何获得的。让我来看看我是如何解释堆栈的:
08048510 <callfoo>:
08048510: 55 pushl %ebp # old frame pointer is pushed to the stack
08048511: 89 e5 movl %esp,%ebp # frame pointer = stack pointer
08048513: 83 ec 08 subl $0x8,%esp # allocates 8 bytes for stack
08048516: 83 c4 f4 addl $0xfffffff4,%esp # this I believe allocates 4 bytes to the stack??
08048519: 68 9c 85 04 08 pushl $0x804859c # push string address
0804851e: e8 d1 ff ff ff call 80484f4 <foo> # call foo, which takes the string address as param1
08048523: 89 ec movl %ebp,%esp # (after foo) does similar to return out of function
08048525: 5d popl %ebp
08048526: c3 ret
080484f4 <foo>:
080484f4: 55 pushl %ebp # push old frame pointer
080484f5: 89 e5 movl %esp,%ebp # frame pointer = stack pointer
080484f7: 83 ec 18 subl $0x18,%esp # allocate 24 bytes
080484fa: 8b 45 08 movl 0x8(%ebp),%eax # moves the param1 (string pointer) into eax
080484fd: 83 c4 f8 addl $0xfffffff8,%esp # allocates 8 more bytes (?)
08048500: 50 pushl %eax # push x # pushes param1 to stack
08048501: 8d 45 fc leal 0xfffffffc(%ebp),%eax # adds 12 to the frame pointer, puts it in eax(?)
08048504: 50 pushl %eax # push buf (which apparently is located in eax and 0xc(%ebp)
08048505: e8 ba fe ff ff call 80483c4 <strcpy> # copies the string from param1 into buf
0804850a: 89 ec movl %ebp,%esp # puts stack pointer into ebp
0804850c: 5d popl %ebp # pops ebp (returns back to other function)
0804850d: c3 ret
Run Code Online (Sandbox Code Playgroud)
(a) 所以在这样做之后,我想我可以看到 buf[0] = 是如何实现的0x64636261
。一个 char 是一个字节,并且是小尾数,它也可以这样读取: buf[0] = 0x61626364
(尽管我不知道我的教授是否会接受这个答案)。但是,我不明白 buf[2] 如何等于0x08040069
or 0x69000408
。它有最后一个字符,然后是空字符,但是04
和是什么08
?
(b) 我不知道如何得到(b) 或(c)。我什至可以在哪里获得 的值esp
来找出 的开头放入的ebp
内容foo
?总的来说,我只是对最后两个感到困惑......有帮助吗?:(
这段代码中似乎对堆栈指针进行了很多不必要的操作,但真正重要的是变量buf
位于ebp-4
. 从序列中你可以看出:
leal 0xfffffffc(%ebp),%eax
pushl %eax
call 80483c4 <strcpy>
Run Code Online (Sandbox Code Playgroud)
0xfffffffc
为 -4,因此leal 0xfffffffc(%ebp),%eax
将 eax 设置为内存位置 的地址ebp-4
。然后将该值作为 的第一个参数压入堆栈strcpy
。strcpy
由于传递给is 的第一个参数buf
,我们知道 的地址buf
是 at ebp-4
。
现在考虑堆栈如何按照调用方式构建foo
。
首先,字符串地址由指令压入pushl $0x804859c
。
0804859c # string pointer
Run Code Online (Sandbox Code Playgroud)
然后,当函数foo
被调用时,call()后面的指令的地址08048523
被压入堆栈作为返回地址。
08048523 # return address
Run Code Online (Sandbox Code Playgroud)
然后在foo内部,ebp被保存在栈上。此时它可能是任何东西。
???????? # saved ebp
Run Code Online (Sandbox Code Playgroud)
然后 ebp 设置为 esp,因此它现在指向保存前一个 ebp 的位置。
现在因为我们知道它buf
位于 ebp-4,这意味着堆栈上的下一个项目将是buf
。subl
堆栈上分配的空间比和指令所需的空间多得多addl
,但我们关心的是buf
位于 处的空间ebp-4
。所以我们关心的堆栈部分如下所示:
0804859c # string pointer
08048523 # return address
???????? # saved ebp <- ebp points here
???????? # buff[0] <- ebp-4 points here
Run Code Online (Sandbox Code Playgroud)
那么现在当你将“abcdefghi”复制到 buff 中时会发生什么?因为机器是小端字节序,所以这些双字将从右到左填充。该字符串中有 9 个字符加上一个空终止符,因此您将覆盖 的所有四个字节buff[0]
、保存的 ebp 的所有四个字节,然后是返回地址的两个字节。
所以你的堆栈现在看起来像这样:
0804859c # string pointer
08040069 # return address
68676665 # saved ebp <- ebp points here
64636261 # buff[0] <- ebp-4 points here
Run Code Online (Sandbox Code Playgroud)
由此看来,各种问题的答案应该是相当明显的。
由于堆栈在内存中向下构建,buff[1]
并且buff[2]
位于buff[0]
堆栈表示的正上方,如我所示。所以你可以看到各种buff值就是:
buff[0] = 0x64636261
buff[1] = 0x68676665
buff[2] = 0x08040069
Run Code Online (Sandbox Code Playgroud)
然后在指令之前ret
,我们有以下两条指令:
movl %ebp,%esp
popl ebp
Run Code Online (Sandbox Code Playgroud)
第一个将 esp 设置为 ebp 的当前值,因此它将指向堆栈上保存前一个 ebp 的位置。但是,查看堆栈表示形式,您可以看到该值现在已被 覆盖68676665
。因此,当您弹出 ebp 时,这就是您将获得的值。
%ebp = 0x68676665
Run Code Online (Sandbox Code Playgroud)
类似地,当函数返回时,它将尝试将返回地址从堆栈中弹出,但您再次可以从堆栈表示中看到原始返回地址已被部分覆盖。因此,ret
指令 eip 之后将立即弹出08040069
。
$eip = 0x08040069
Run Code Online (Sandbox Code Playgroud)
我认为这回答了你所有的问题。
我意识到这个问题已经有好几年了,但它还没有结束,也没有公认的答案,所以也许这个解释对某人仍然有一定用处。
归档时间: |
|
查看次数: |
4603 次 |
最近记录: |