进行系统调用后的程序集分段错误,在我的代码末尾 [英] Assembly segmentation fault after making a system call, at the end of my code

查看:16
本文介绍了进行系统调用后的程序集分段错误,在我的代码末尾的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在试验并拥有以下汇编代码,它工作得很好,只是在我的程序结束之前我收到了分段错误(核心转储)"消息:

I was experimenting and have the following assembly code, which works very well, except that I get a "Segmentation fault (core dumped)" message right before my program ends:

GLOBAL _start

%define ___STDIN 0
%define ___STDOUT 1
%define ___SYSCALL_WRITE 0x04

segment .data
segment .rodata
    L1 db "hello World", 10, 0
segment .bss
segment .text
_start:
    mov eax, ___SYSCALL_WRITE
    mov ebx, ___STDOUT
    mov ecx, L1
    mov edx, 13
    int 0x80

最后有没有ret并不重要;我仍然收到消息.

It doesn't matter whether or not I have ret at the end; I still get the message.

有什么问题吗?

我使用的是 x86 和 nasm.

I'm using x86 and nasm.

推荐答案

你不能从一开始就ret;它不是一个函数,堆栈上没有返回地址.堆栈指针指向进程入口处的 argc.

You can't ret from start; it isn't a function and there's no return address on the stack. The stack pointer points at argc on process entry.

作为n.m.在评论中说,问题是你没有退出程序,所以执行运行到垃圾代码中,你会得到一个段错误.

As n.m. said in the comments, the issue is that you aren't exiting the program, so execution runs off into garbage code and you get a segfault.

你需要的是:

;; Linux 32-bit x86
%define ___SYSCALL_EXIT 1

// ... at the end of _start:
    mov eax, ___SYSCALL_EXIT
    mov ebx, 0
    int 0x80

(以上为32位代码.在64位代码中,您需要mov eax, 231 (exit_group)/syscall,退出状态在EDI中.例如:

(The above is 32-bit code. In 64-bit code you want mov eax, 231 (exit_group) / syscall, with the exit status in EDI. For example:

;; Linux x86-64
    xor   edi, edi     ;  or mov edi, eax    if you have a ret val in EAX
    mov   eax, 231     ; __NR_exit_group
    syscall

这篇关于进行系统调用后的程序集分段错误,在我的代码末尾的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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