问题
I'm experimenting with sys_open
syscall and I get file descriptor for reading. Here is my program:
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\0'
So when I ran strace ./bin
I got the output:
open("/path/to/test\\0", O_RDONLY) = -1 ENOENT (No such file or directory)
exit(0)
After deleting the null-terminal it seemed to work fine:
open("/path/to/test", O_RDONLY) = 3
exit(0) = ?
I'm curious about how does the assembler knows the lenght of my string. The content of data section in the binary is this:
Contents of section .data:
6000d8 2f706174 682f746f 2f746573 74 /path/to/test
I expected the string is read till reaching the null-terminator. How does it work?
回答1:
The problem is in the way you defined the following data:
section .data
file_name: db '/path/to/test\0'
The trailing NUL
character is missing, since the \0
inside the string corresponds to the characters \
and 0
, it should be defined instead as:
section .data
file_name: db '/path/to/test', 0
来源:https://stackoverflow.com/questions/49613697/null-terminated-string-opening-file-for-reading