适用于MacOS程序集的64位系统调用文档

问题描述 投票:2回答:1

我在MacOS上找到编写64位汇编的好文档时遇到了麻烦。

64-bit SysV ABI在A.2.1节中说以下内容,this SO post引用它:

  • 系统调用通过syscall指令完成。内核销毁寄存器%rcx和%r11。
  • 从系统调用返回,寄存器%rax包含系统调用的结果。介于-4095和-1之间的值表示错误,它是-errno。

这两个句子在Linux上没问题但在macOS Sierra上有错误,代码如下:

global _start
extern _exit

section .text
_start:

; Align stack to 16 bytes for libc
and rsp, 0xFFFFFFFFFFFFFFF0

; Call write
mov rdx, 12             ; size
mov rsi, hello          ; buf
mov edi, 1              ; fd
mov rax, 0x2000004      ; write ; replace to mov rax, 0x1 on linux
syscall

jc .err                 ; Jumps on error on macOS, but why?
jnc .ok

.err:
mov rdi, -1
call _exit              ; exit(-1)

.ok:
; Expect rdx to be 12, but it isn't on macOS!
mov rdi, rdx
call _exit              ; exit(rdx)

; String for write
section .data
hello:
.str db `Hello world\n`
.len equ $-hello.str

与NASM编译:

; MacOS: nasm -f macho64 syscall.asm && ld syscall.o -lc -macosx_version_min 10.12 -e _start -o syscall
; Linux: nasm -f elf64 syscall.asm -o syscall.o && ld syscall.o -lc -dynamic-linker /lib64/ld-linux-x86-64.so.2 -o syscall

在macOS上运行:

./syscall      # Return value 0
./syscall >&-  # Return value 255 (-1)

我发现:

  • 系统调用返回errno并设置错误的进位标志,而不是在-errno中返回rax
  • rdx登记册遭到syscall的破坏
  • 在Linux上,一切都按预期工作

为什么rdx遭到破坏?为什么系统调用不返回-errno?我在哪里可以找到真实的文档?

我发现有人讨论系统调用错误的进位标志的唯一地方是here

macos assembly 64bit system-calls
1个回答
1
投票

我用过这个:

# as hello.asm -o hello.o
# ld hello.o -macosx_version_min 10.13 -e _main -o hello  -lSystem
.section __DATA,__data
str:
  .asciz "Hello world!\n"

.section __TEXT,__text
.globl _main
_main:
  movl $0x2000004, %eax           # preparing system call 4
  movl $1, %edi                   # STDOUT file descriptor is 1
  movq str@GOTPCREL(%rip), %rsi   # The value to print
  movq $13, %rdx                  # the size of the value to print
  syscall

  movl %eax, %edi
  movl $0x2000001, %eax           # exit (return value of the call to write())
  syscall

并能够将回报价值吸引到eax。这里返回值是write系统调用实际写入的字节数。是的MacOS是一个BSD变体,它是进位标志,告诉你系统调用是否错误(错误只是一个外部链接变量)。

# hello_asm.s
# as hello_asm.s -o hello_asm.o
# ld hello_asm.o -e _main -o hello_asm
.section __DATA,__data
str:
        .asciz "Hello world!\n"
good:
        .asciz "OK\n"

.section __TEXT,__text
.globl _main
_main:
        movl $0x2000004, %eax           # preparing system call 4
        movl $5, %edi                   # STDOUT file descriptor is 5
        movq str@GOTPCREL(%rip), %rsi   # The value to print
        movq $13, %rdx                  # the size of the value to print
        syscall

        jc err

        movl $0x2000004, %eax           # preparing system call 4
        movl $1, %edi                   # STDOUT file descriptor is 1
        movq good@GOTPCREL(%rip), %rsi  # The value to print
        movq $3, %rdx                   # the size of the value to print
        syscall
        movl $0, %edi
        movl $0x2000001, %eax           # exit 0
        syscall
err:    
        movl $1, %edi
        movl $0x2000001, %eax           # exit 1
        syscall

这将以错误代码1退出,因为使用了描述符5,如果您尝试使用描述符1,那么它将工作打印另一条消息并以0退出。

© www.soinside.com 2019 - 2024. All rights reserved.