退出系统调用的正确常数是多少? [英] What is the correct constant for the exit system call?

查看:82
本文介绍了退出系统调用的正确常数是多少?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试学习x86_64汇编,并且正在使用GCC作为汇编器.我正在使用的确切命令是:

I am trying to learn x86_64 assembly, and am using GCC as my assembler. The exact command I'm using is:

gcc -nostdlib tapydn.S -D__ASSEMBLY__

我主要使用gcc作为其预处理程序.这是tapydn.S:

I'm mainly using gcc for its preprocessor. Here is tapydn.S:

.global _start

#include <asm-generic/unistd.h>

syscall=0x80

.text
_start:
    movl $__NR_exit, %eax
    movl $0x00, %ebx
    int  $syscall

这会导致分段错误.我相信问题在于以下几行:

This results in a segmentation fault. I believe the problem is with the following line:

 movl $__NR_exit, %eax

我使用__NR_exit是因为它比某些魔术数字更具描述性.但是,看来我的用法不正确.我相信是这样,因为当我将相关行更改为以下内容时,它运行良好:

I used __NR_exit because it was more descriptive than some magic number. However, it appears that my usage of it is incorrect. I believe this to be the case because when I change the line in question to the following, it runs fine:

movl $0x01, %eax

进一步支持这一思路的是usr/include/asm-generic/unistd.h的内容:

Further backing up this trail of thought is the contents of usr/include/asm-generic/unistd.h:

#define __NR_exit 93
__SYSCALL(__NR_exit, sys_exit)

我希望__NR_exit的值为1,而不是93!显然,我误解了它的目的,因此也误解了它的用法.就我所知,我很幸运$0x01案例的工作(很像C ++中未定义的行为),所以我一直在挖掘...

I expected the value of __NR_exit to be 1, not 93! Clearly I am misunderstanding its purpose and consequently its usage. For all I know, I'm getting lucky with the $0x01 case working (much like undefined behaviour in C++), so I kept digging...

接下来,我寻找了sys_exit的定义.我找不到无论如何,我都尝试按以下方式使用它(带有和不带有前面的$):

Next, I looked for the definition of sys_exit. I couldn't find it. I tried using it anyway as follows (with and without the preceeding $):

movl $sys_exit, %eax

这不会链接:

/tmp/cc7tEUtC.o: In function `_start':
(.text+0x1): undefined reference to `sys_exit'
collect2: error: ld returned 1 exit status

我的猜测是这是系统库之一中的符号,由于将-nostdlib传递给GCC,所以我没有链接它.如果可能的话,我想避免只将一个符号链接到一个如此大的库.

My guess is that it's a symbol in one of the system libraries and I'm not linking it due to my passing -nostdlib to GCC. I'd like to avoid linking such a large library for just one symbol if possible.

为了回应Jester关于混合32位和64位常量的评论,我尝试按照建议使用值0x3C:

In response to Jester's comment about mixing 32 and 64 bit constants, I tried using the value 0x3C as suggested:

movq $0x3C, %eax
movq $0x00, %ebx

这也导致了分段错误.我还尝试将eaxebx换为raxrbx:

This also resulting a segmentation fault. I also tried swapping out eax and ebx for rax and rbx:

movq $0x3C, %rax
movq $0x00, %rbx

分割错误仍然存​​在.

The segmentation fault remained.

Jester然后评论说我应该使用syscall而不是int $0x80:

Jester then commented stating that I should be using syscall rather than int $0x80:

.global _start

#include <asm-generic/unistd.h>

.text
_start:
    movq $0x3C, %rax
    movq $0x00, %rbx
    syscall

这可行,但是后来我得知,按照System V AMD64 ABI,我应该使用rdi而不是rbx:

This works, but I was later informed that I should be using rdi instead of rbx as per the System V AMD64 ABI:

movq $0x00, %rdi

这也可以正常工作,但仍然会使用幻数0x3C作为系统电话号码.

This also works fine, but still ends up using the magic number 0x3C for the system call number.

总结,我的问题如下:

  • __NR_exit的正确用法是什么?
  • exit系统调用中,我应该使用什么而不是幻数?
  • What is the correct usage of __NR_exit?
  • What should I be using instead of a magic number for the exit system call?

推荐答案

获取系统调用号的正确头文件为sys/syscall.h.这些常量称为SYS_###,其中###是您感兴趣的系统调用的名称.__NR_###宏是实现的详细信息,不应使用.根据经验,如果标识符以下划线开头,则不应使用;如果标识符以两个开头,则绝对不应使用.参数进入rdirsirdxr10r8r9.这是Linux的示例程序:

The correct header file to get the system call numbers is sys/syscall.h. The constants are called SYS_### where ### is the name of the system call you are interested in. The __NR_### macros are implementation details and should not be used. As a rule of thumb, if an identifier begins with an underscore it should not be used, if it begins with two it should definitely not be used. The arguments go into rdi, rsi, rdx, r10, r8, and r9. Here is a sample program for Linux:

#include <sys/syscall.h>

    .globl _start
_start:
    mov $SYS_exit,%eax
    xor %edi,%edi
    syscall

这些约定大部分可移植到其他类似UNIX的操作系统中.

These conventions are mostly portable to other UNIX-like operating systems.

这篇关于退出系统调用的正确常数是多少?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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