我有一些由我自己的编译器编写的asm程序,当我想运行它们时,它们在最后有一个段错误。所有指令都按照我想要的方式执行,但执行因段错误而结束。
当我尝试使用 gdb 来查看段错误时,它似乎总是出现在以下行:0x11ee90 <_dl_debug_state> push %ebp>
我什至不知道这一行是什么,首先不知道如何防止它导致段错误。
这是此类程序的示例:
file "test_appel.c"
.text
.globl f
.type f, @function
f:
pushl %ebp
movl %esp, %ebp
subl $16, %esp
movl 8(%ebp), %eax
pushl %eax
movl 12(%ebp), %eax
popl %ecx
imull %ecx, %eax
movl %eax, 16(%ebp)
movl 16(%ebp), %eax
leave
ret
.section .rodata
.LC0:
.string "appel à fonction pour la multiplication\n"
.LC1:
.string "resultat 2 * 3 = %d\n"
.text
.globl main
.type main, @function
main:
pushl %ebp
movl %esp, %ebp
andl $-16, %esp
subl $32, %esp
movl $2, %eax
movl %eax, 8(%ebp)
movl $3, %eax
movl %eax, 12(%ebp)
movl 12(%ebp), %eax
movl %eax ,4(%esp)
movl 8(%ebp), %eax
movl %eax ,0(%esp)
call f
movl %eax, 4(%ebp)
movl 4(%esp), %eax
movl (%esp), %ecx
pushl %eax
pushl %ecx
movl $.LC0, %eax
movl %eax, (%esp)
call printf
popl %ecx
popl %eax
movl %eax, 4(%esp)
movl %ecx, (%esp)
movl 4(%esp),%eax
movl (%esp), %ecx
pushl %eax
pushl %ecx
movl 4(%ebp), %eax
movl %eax, %edx
movl %edx, 4(%esp)
movl $.LC1, (%esp)
call printf
popl %ecx
popl %eax
movl %eax, 4(%esp)
movl %ecx, (%esp)
leave
ret
最佳答案
segfault, it appears that it always occurs at the line :
0x11ee90 <_dl_debug_state> push %ebp>
这仅仅意味着您已经损坏或耗尽了堆栈。
事实上,您的编译器确实似乎发出了到处破坏堆栈的代码。特别是这些说明:
movl %eax, 8(%ebp)
...
movl %eax, 12(%ebp)
损坏了调用者(它是libc的一部分)中的局部变量,因此在main
之后看到崩溃并不奇怪。返回。
您可能打算发出:movl %eax, -8(%ebp)
和movl %eax, -12(%ebp)
.
关于assembly - 段错误 x86 <_dl_debug_state>,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10666451/