问题
I am on 64-bit Linux x86. I need to execute mmap syscall using syscall function. mmap syscall number is 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));
However, when I run it, the syscall function gives wrong results.
mmap-1: 2236940288
mmap-2: 140503502090240
mmap-1: 3425849344
mmap-2: 140612065181696
mmap-1: 249544704
mmap-2: 139625341366272
mmap works just fine, but the "addresses" returned syscall result in Segmentation fault. The values from syscall seem to be cast to 32 bits or something.
What am I doing wrong?
回答1:
In your syscall(), instead of passing in 0 (NULL) for the first parameter, addr, pass some pointer you have declared previously. This way you can access the memory mapped by mmap. mmap function declaration:
void *mmap(void *addr, size_t length, int prot, int flags, int fd, off_t offset);
回答2:
Found the cause of the problem: I was running gcc with -std=c99 option, removing it solved the problem:
mmap-1: 139975263928320
mmap-2: 139975263924224
I guess, -std=99 defines syscall as int syscall() and without it its long syscall().
来源:https://stackoverflow.com/questions/38640828/c-syscall-64-bit-pointer