以 NULL 结尾的字符串,打开文件以供读取

Null-terminated string, opening file for reading

我正在试验 sys_open 系统调用,我得到了用于读取的文件描述符。这是我的程序:

SYS_exit equ 0x3C

SYS_open equ 0x02
O_RDONLY equ 0x00
O_WRONLY equ 0x01
O_RDWR equ 0x02

section .text
    global _start

_start:
    mov eax, SYS_open
    mov rdi, file_name
    mov rsi, O_RDONLY
    mov rdx, 0x00
    syscall

    mov eax, SYS_exit
    mov rdi, 0x00

    syscall

section .data
    file_name: db '/path/to/test[=10=]'

所以当我 运行 strace ./bin 我得到输出:

open("/path/to/test\0", O_RDONLY) = -1 ENOENT (No such file or directory)
exit(0)   

删除空终端后似乎工作正常:

open("/path/to/test", O_RDONLY) = 3
exit(0)                                 = ?

我很好奇汇编程序如何知道我的字符串的长度。二进制数据部分的内容是这样的:

Contents of section .data:
 6000d8 2f706174 682f746f 2f746573 74        /path/to/test

我预计字符串会被读取直到到达空终止符。它是如何工作的?

问题在于您定义以下数据的方式:

section .data
    file_name: db '/path/to/test[=10=]'

缺少结尾的 NUL 字符,因为字符串中的 [=13=] 对应字符 \0,应改为定义为:

section .data
    file_name: db '/path/to/test', 0