为什么从_start segfault返回? [英] Why does returning from _start segfault?

查看:99
本文介绍了为什么从_start segfault返回?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图将代码不放在主要函数中,而是直接放在_start中:

I tried to put code not in the main function, but directly into _start:

    segment .text
    global _start
_start:
    push rbp
    mov rbp, rsp
    ; ... program logic ...
    leave
    ret

编译:

yasm -f elf64 main.s
ld -o main main.o

运行:

./main
Segmentation fault(core dumped)

我读了,离开了

mov esp,ebp
pop ebp

但是,为什么弹出式堆栈框架的结尾和指向前一帧基础的已设置基础框架指针会导致分段错误?

But why is it that such an epilogue to the pop stack frame and the set base frame pointer to a previous frame's base results in a segmentation fault?

的确,退出系统调用会正常退出.

Indeed, making an exit system call exits gracefully.

推荐答案

根据

As per ABI1 the stack at the entry on _start is

没有寄信人地址".
退出进程的唯一方法是通过SYS_EXIT

There is no "return address".
The only way to exit a process is through SYS_EXIT

xorl %edi, %edi   ;Error code
movl $60, %eax    ;SYS_EXIT
syscall


1 部分 3.4.1初始堆栈和寄存器状态.


1 Section 3.4.1 Initial Stack and Register State.

这篇关于为什么从_start segfault返回?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆