【问题标题】:Problems with getcwd syscall on OSXOSX 上 getcwd 系统调用的问题
【发布时间】:2015-12-19 08:08:14
【问题描述】:

有人知道如何使用 NASM 在 OSX 中获取当前工作目录吗?系统调用 getcwd 在 osx 上不可用,并且 dtruss pwd 返回许多 stat sys 调用。但是在手册中我找不到 stat 的哪个结构变量返回当前工作目录。 谢谢。

【问题讨论】:

  • getcwd 没有 syscall。它通常通过检索$PWD 环境变量并进行检查以确保它与 stat(".") inode 和设备匹配来实现。您可以在他们的 libc code 中了解 Apple 是如何做到的。您可以将您的汇编语言程序与libc 链接并调用getcwd
  • 所以我认为对于 osx,最好始终使用 libc 库,而不是像在 Linux 上仅依赖系统调用...

标签: macos assembly x86-64 nasm getcwd


【解决方案1】:

这是有点迟到的答案,但尽管如此,这可以使用 2 个系统调用来实现。

  1. open_nocancel0x2000018e(或open0x2000005)打开当前目录的文件描述符
  2. fcntl_nocancel0x20000196(或fcntl0x2000005c)用于读取实际路径

示例:

#define F_GETPATH 50     ; from <sys/fcntl.h>

currentDirConstant: 
db   ".",0 ; needs segment read permission  

outputPath:          
resb 1000 ; needs segment write permission

mov rdi,currentDirConstant; input path 1st argument
xor esi, esi        ; int flags 2nd argument, just use 0
xor edx, edx        ; int mode 3rd argument, just use 0
mov eax, 0x2000018e ; open_nocancel syscall number 
syscall        

mov edi,eax          ; file descriptor 1st argument of current dir from previous syscall
mov esi,F_GETPATH    ; fcntl cmd 2nd argument
mov rdx, outputPath  ; output buffer 3rd argument
mov eax, 0x20000196  ; fcntl_nocancel syscall number  
syscall

【讨论】:

    猜你喜欢
    • 2020-08-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-04-15
    • 2011-08-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多