为什么从_start返回段错误?错误、_start

2023-09-07 03:02:51 作者:无视沵的存在

我尝试将代码不在main函数中,而是直接放入_start:

 段 .text全局_start_开始:推rbpmov rbp, rsp;... 程序逻辑 ...离开ret

编译:

yasm -f elf64 main.sld -o main main.o
为什么我的PS只能返回一步 如何才能返回多步 望高手告知,谢谢

运行:

./main分段错误(核心转储)

我看了,离开是

mov esp,ebp流行音乐

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

确实,退出系统调用会优雅地退出.

解决方案

根据

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

xorl %edi, %edi ;错误码movl $60, %eax ;SYS_EXIT系统调用

1 第 3.4.1 节初始堆栈和寄存器状态.

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

Compile:

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

Run:

./main
Segmentation fault(core dumped)

I read, leave is

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

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 Section 3.4.1 Initial Stack and Register State.