C - syscall - 64-bit pointer

I am on 64-bit Linux x86. I need to execute mmap syscall using the syscall function. mmap standby number: 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 incorrect 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 by syscall lead to a Segmentation fault . The values ​​from syscall seem to be fine up to 32 bits or something else.

What am I doing wrong?

-one
c system-calls mmap
source share
2 answers

Found the cause of the problem: I ran gcc with the -std=c99 option, uninstalling it, solving the problem:

 mmap-1: 139975263928320 mmap-2: 139975263924224 

I think -std=99 defines syscall as int syscall() and without it its long syscall() .

-one
source share

In your syscall() instead of passing to 0 (NULL) for the first addr parameter, pass some pointer that you specified earlier. This way you can access the memory mapped using mmap . mmap Function declaration:

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

All Articles