x86-64 Linux下NASM(YASM)系统调用详细描述

6
我找到了x86-64模式下系统调用列表(带参数):http://filippo.io/linux-syscall-table/。但是我在哪里可以获得这些系统调用的详细描述呢?
例如下面这个例子,除了0102o(读写,创建)之外,还有哪些标志可以用于“open”系统调用,在其他情况下:只读、只写等。
SECTION .data
    message: db 'Hello, world!',0x0a    
    length:    equ    $-message        
    fname    db "result"
    fd       dq 0

SECTION .text
global _start   
_start:
        mov rax, 2            ; 'open' syscall
        mov rdi, fname        ; file name
        mov rsi, 0102o        ; read and write mode, create if not
        mov rdx, 0666o        ; permissions set
        syscall

        mov [fd], rax

        mov    rax, 1          ; 'write' syscall
        mov    rdi, [fd]       ; file descriptor
        mov    rsi, message    ; message address
        mov    rdx, length     ; message string length
        syscall

        mov rax, 3             ; 'close' syscall
        mov rdi, [fd]          ; file descriptor  
        syscall 

        mov    rax, 60        
        mov    rdi, 0        
        syscall

基于来源(可能是)https://git.kernel.org/cgit/linux/kernel/git/torvalds/linux.git/tree/fs/open.c,如何理解它?可以使用哪些标志(open的所有标志列表)?
1个回答

2

系统调用的文档在man手册的第2部分和/或源代码的注释中。

man页面开头如下:

   #include <sys/types.h>
   #include <sys/stat.h>
   #include <fcntl.h>

   int open(const char *pathname, int flags);
   int open(const char *pathname, int flags, mode_t mode);

“flags”参数必须包含以下访问模式中的一个:O_RDONLY、O_WRONLY或O_RDWR。它们分别请求只读、只写或读/写打开文件。
此外,可以将一个或多个文件创建标志和文件状态标志与“flags”按位或。文件创建标志为O_CREAT、O_EXCL、O_NOCTTY和O_TRUNC。
这些值在系统头文件中很容易查找。

grep -i 0102 /usr/include/asm/unistd_64.h - 没有任何输出。该怎么办? - Alex0102o
@Alex0102o:我不明白。那个文件是系统调用入口号的列表:与flags参数无关。标志位在/usr/include/bits/fcntl.h中,其中0102显然是O_RDWR | O_CREAT(至少在Fedora 17-64中)。 - wallyk
wallyk,是的,在 /usr/include/bits/fcntl.h 中有这个(Debian Lenny 64位)。谢谢!(抱歉我的英语不好)0102 是我从32位NASM中取出的。 - Alex0102o
@Alex0102o:不用谢。请务必通过点击我的答案旁边的复选标记来“接受”我的答案。当您拥有足够的声望时,您还可以点赞好的答案(并踩坏的答案)。 - wallyk
@wallyk #include 是关于 C 语言的,不是汇编语言。在 nasm 中有没有一种方法可以包含这些符号?SO 中的其他答案也说“使用名称而不是数字”,但是当然 nasm 不认识 O_RDONLY。 - flaschenpost
@flaschenpost:一种方法是查看包含文件并在asm中使用数字常量,但这不是广泛使用的常量的“优秀”技术。Nasm确实有一个[include facility](http://www.nasm.us/doc/nasmdoc2.html)(请参见2.1.16和2.1.17),可以用于包含包含有用常量的文件。我怀疑它是否适用于C文件(但也许会),因此最坏的情况下,您可能需要为Nasm使用制作自己的包含文件。 - wallyk

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接