C - 系统调用 - 64位 - 指针

时间:2016-07-28 15:33:18

标签: c system-calls mmap

我在64位Linux x86上。我需要使用mmap函数执行syscall系统调用。 mmap系统调用号是9:

printf("mmap-1: %lli\n", syscall(9,    0, 10, 3, 2 | 32, -1, 0));
printf("mmap-2: %lli\n", mmap(         0, 10, 3, 2 | 32, -1, 0));

但是,当我运行它时,syscall函数会给出错误的结果。

mmap-1: 2236940288
mmap-2: 140503502090240

mmap-1: 3425849344
mmap-2: 140612065181696

mmap-1: 249544704
mmap-2: 139625341366272

mmap工作得很好,但返回syscall的“地址”会导致Segmentation fault。来自syscall的值似乎被强制转换为32位或其他值。

我做错了什么?

2 个答案:

答案 0 :(得分:-1)

syscall()中,不是为第一个参数0 (NULL)传入addr,而是传递之前已声明过的指针。这样您就可以访问mmap映射的内存。 mmap函数声明:

void *mmap(void *addr, size_t length, int prot, int flags, int fd, off_t offset);

答案 1 :(得分:-1)

找到问题的原因:我使用gcc选项运行-std=c99,删除它解决了问题:

mmap-1: 139975263928320
mmap-2: 139975263924224

我想,-std=99将系统调用定义为int syscall(),没有它long syscall()

相关问题