64 位 NASM 中的 Linux sys_open 返回负值 [英] Linux sys_open in 64-bit NASM returns negative value

查看:128
本文介绍了64 位 NASM 中的 Linux sys_open 返回负值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 sys_open 和 sys_write 打开一个现有文件以写入其中.当我创建一个新文件时, sys_write 正常工作,如下所示.但是如果我使用 sys_open,返回值为负(-13,即权限被拒绝")并且写入不起作用(当然).

I am opening an existing file to write into it, using sys_open and sys_write. The sys_write works correctly when I create a new file as shown below. But if I use sys_open, the return value is negative (-13, which is "Permission denied") and the write doesn't work (of course).

这有效:

section .data
File_Name: db '/opt/Test_Output_Files/Linux_File_Test',0
File_Mode: dq 754q
Write_Buffer: db 'This is what I want to write',0

section .text

; Create file
mov rax,85 ; sys_creat
mov rdi,File_Name
mov rsi,File_Mode  ; mode (permissions)
syscall

mov rdi,rax ; return code from sys_creat
mov rax,1 ; sys_write
mov rsi,Write_Buffer
mov rdx,29
syscall

但是当我打开现有文件时,sys_open 命令失败:

But when I open the existing file, the sys_open command fails:

mov rax,2 ; sys_open
mov rdi,File_Name
mov rsi,2 ;read-write
mov rdx,[File_Mode]
syscall

因为这是一个权限错误,所以问题很可能是 rsi 中的标志值,因为 rdx 中的模式值与我在 sys_creat (754) 中使用的值相同.根据 http://man7.org/linux 上的 Linux 手册页/man-pages/man2/open.2.htmlhttps://linux.die.net/man/3/open,需要三个选项:

Because this is a permissions error, the issue is most likely the flags value in rsi because the mode value in rdx is the same as I use with sys_creat (754). According to the Linux man pages at http://man7.org/linux/man-pages/man2/open.2.html and https://linux.die.net/man/3/open, there are three required options:

O_RDONLY - Open for reading only. 
O_WRONLY - Open for writing only. 
O_RDWR - Open for reading and writing. The result is undefined if this flag is applied to a FIFO.

我知道只读是零,所以我假设只写和读写是 1 和 2,但我没有找到任何我们将在汇编语言中使用的数值列表,不像模式基于 chmod——它与我用于创建的模式值相同,它有效.

I know that read-only is zero, so I assumed write only and read-write are 1 and 2, but I haven't found any listing of the numeric values that we would use in assembly language, unlike the mode which is based on chmod -- and it's the same mode value I used for create, which works.

我对此进行了广泛的研究,但是关于 64 位系统调用的信息很少——其中大部分是 32 位的.对于 NASM,我需要对 rsi 中的标志使用数值.手册页说此外,零个或多个文件创建标志和文件状态标志可以在标志中按位或".文件创建标志是 O_CLOEXEC、O_CREAT、O_DIRECTORY、O_EXCL、O_NOCTTY、O_NOFOLLOW、O_TMPFILE 和 O_TRUNC."如果我知道它们的值是什么,我可以按位或它们.

I've researched this extensively, but there is sparse information on 64-bit syscalls -- most of it is 32-bit. For NASM I need to use a numeric value for the flags in rsi. The man pages say "In addition, zero or more file creation flags and file status flags can be bitwise-or'd in flags. The file creation flags are O_CLOEXEC, O_CREAT, O_DIRECTORY, O_EXCL, O_NOCTTY, O_NOFOLLOW, O_TMPFILE, and O_TRUNC." I could bitwise OR them if I knew what their values are.

感谢您对此的任何帮助.

Thanks for any help with this.

推荐答案

我猜你没有 O_RDWR 权限来打开文件.

My guess is, you do not have O_RDWR permission for the file that you are trying to open.

你应该试试 O_RDONLY.

You should try O_RDONLY.

无论如何,回答你的问题.就标志值而言,这些将是:

Anyway, to answer your question. As far as flag values are concerned, those will be:

O_CREAT(0x40)
O_TRUNC(0x200)
O_APPEND(0x400)

您可以在以下位置找到整个列表:

You can find the entire list in:

/usr/include/asm-generic/fcntl.h

注意:如果未设置 O_CREAT,则 'mode'(您在 rdx 中设置的值)将被忽略.

Note: if O_CREAT is not set then 'mode' (the value that you set in rdx) will be ignored.

这篇关于64 位 NASM 中的 Linux sys_open 返回负值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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