简体   繁体   English

Mac 64位系统调用

[英]Mac 64-bit syscall

How to execute a 64-bit syscall on Mac in C++. 如何在C ++中在Mac上执行64位 syscall

I need the below to work: 我需要以下工作:

#include <unistd.h>
#include <sys/syscall.h>
#include <sys/types.h>

int main() {
  long* addr = (long*) syscall(SYS_mmap, 0, 100, 1 | 2, 2 | 4096, -1, 0);
}

The problem is that addr below should be 64-bit as it is a pointer, but it truncates the result to 32-bit value. 问题是下面的addr应该是64位,因为它是一个指针,但它会将结果截断为32位值。

I compile with: 我编译:

g++ ./mmap.cc -o ./mmap 

PS I know there exists mmap function, the above is just an example, I need to get syscall function to work. PS我知道存在mmap函数,上面只是一个例子,我需要让syscall函数工作。

PPS The problem is that on 64-bit systems syscall should return 64-bit value, but in Mac unistd.h it is defined as int : PPS问题是在64位系统上,系统syscall应返回64位值,但在Mac unistd.h它定义为int

int  syscall(int, ...);

Is that a bug? 那是一个错误吗? The mmap system call return correctly void* : mmap系统调用正确返回void*

void *  mmap(void *, size_t, int, int, int, off_t) __DARWIN_ALIAS(mmap);

How is mmap actually implemented? mmap是如何实际实现的?

PPPS 购买力平价

On Linux it is defined correctly as long: 在Linux上,它被正确定义为:

long syscall(long number, ...);

After some searching I did not find a way to execute 64-bit system calls on Mac. 经过一番搜索后,我找不到在Mac上执行64位系统调用的方法。

So, I implemented those myself, you can find the functions here . 所以,我自己实现了这些,你可以在这里找到这些功能

Also, if you use those functions don't forget to add 0x2000000 Unix system call class shift to your system call numbers: 另外,如果你使用这些功能,不要忘了加0x2000000 Unix的系统调用类转移到你的系统调用号:

int SYS_write = 4;
int STDOUT = 1;
char* str = "Hello world\n";

syscall3(0x2000000 + SYS_write, STDOUT, str, 12);

The syscall function is deprecated on OS X as Ken Thomases pointed out. 正如Ken Thomases所指出的,OS X上不推荐使用syscall函数。

For your specific example, you should use mmap instead of syscall . 对于您的具体示例,您应该使用mmap而不是syscall The mmap function is not implemented in terms of the syscall function but in terms of __mmap : mmap函数不是根据syscall函数实现的,而是根据__mmap

libsystem_kernel.dylib`mmap:
    0x7fff643fa69e <+87>:  callq  0x7fff643fe998            ; __mmap

which, in turn, does the actual syscall and is probably implemented in assembly: 反过来,它实际上是系统调用,可能是在汇编中实现的:

libsystem_kernel.dylib`__mmap:
    0x7fff643fe998 <+0>:  movl   $0x20000c5, %eax          ; imm = 0x20000C5
    0x7fff643fe99d <+5>:  movq   %rcx, %r10
    0x7fff643fe9a0 <+8>:  syscall
    0x7fff643fe9a2 <+10>: jae    0x7fff643fe9ac            ; <+20>

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

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