简体   繁体   English

os x汇编程序中的chdir syscall

[英]chdir syscall in os x assembler

I am trying to make sure I am using syscalls correctly in OS X assembly language. 我试图确保我在OS X汇编语言中正确使用了系统调用。 I am using NASM and the sample below is the shortest program I can create that reproduces the problem: 我正在使用NASM,下面的示例是我可以创建的最短的程序,它再现了该问题:

1 section .data
2 path        db      "/Users/querist",0x00
3 section .text
4 global start
5 
6 start:
7 mov rax, 0x0200000b     ; System call chdir = 12
8 mov rdi, path           ; address of path string
9 sub rsp, 16             ; add space to stack for local variables
10 syscall                 ; Invoke the kernel
11 mov rdi, rax            ; save return code from previous function
12 mov rax, 0x02000001     ; System call number for exit = 1
13 syscall                 ; Invoke the kernel

I know that the change of directory only applies to the process for the program itself so it will not change my working directory in the command shell I am using. 我知道目录的更改仅适用于程序本身的进程,因此不会更改我正在使用的命令外壳中的工作目录。 I was actually trying to create a directory as a test but the chdir call fails with an error code of 78, which indicates a "configuration error". 我实际上正在尝试创建一个目录作为测试,但是chdir调用失败,错误代码为78,表示“配置错误”。

The target directory exists and it is my user directory, so I have permission to go to it. 目标目录存在,它是我的用户目录,因此我有权访问它。 I receive the same error code if I try to use a relative path (such as "..") instead. 如果尝试使用相对路径(例如“ ..”),则会收到相同的错误代码。

I can create a directory using a syscall using either a relative or absolute path, but the chdir call does not work. 我可以使用相对或绝对路径使用syscall创建目录,但是chdir调用不起作用。

Also, how can I get a string with the current path name without linking the c libraries? 另外,如何在不链接c库的情况下获取具有当前路径名的字符串?

thanks 谢谢

It seems that in this case it was truly just a matter of typos and being too sick to see what was going on. 似乎在这种情况下,这实际上只是拼写错误,而且病得太重了,无法查看正在发生的情况。 I feel sufficiently embarrassed now. 我现在感到很尴尬。

Corrected, working code: 更正的工作代码:

1 section .data
2 path        db      "/Users/querist",0x00,0x00
3 section .text
4 global start
5 
6 start:
7 mov rax, 0x0200000c     ; System call chdir = 12
8 lea rdi, [rel path]           ; address of path string
9 sub rsp, 16             ; add space to stack for local variables
10 syscall                 ; Invoke the kernel
11 mov rdi, rax            ; save return code from previous function
12 mov rax, 0x02000001     ; System call number for exit = 1
13 syscall                 ; Invoke the kernel

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM