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

问题描述

我正在试验,并且具有以下汇编代码,该代码非常有效,除了在程序结束之前收到分段错误(内核已转储)"消息之外.

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

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

10-24 03:08